Sign in Join
Wiki copytemporary

Porting from Mac Pascal: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Porting from Mac Pascal

OldNewLine
1+Although FPC today supports many [[Mac Pascal]] constructs, there are a few of issues one has to be aware of. Tips to make the code work with GPC are also welcome.
2+
3+## Setting the compilation mode
4+
5+First of all, ensure that the compiler is in [[Mode MacPas|Mac Pascal]] mode. You can do this by either using the -Mmacpas command line switch, or by adding {\$mode macpas} at the top of all of your source files.
6+
7+## Unit Names
8+
9+Unit names in FPC must match the source file name. Unit names must also differ from any object/class names you use. In Metrowerks Pascal it was possible to have the unit name and an object that was subsequently defined in that unit be the same name.
10+
11+## Uses clause
12+
13+Depending on the compiler you're moving from, you may have a simple or complex uses clause to include all of the toolbox calls. Either way, it can all be replaced with:
14+
15+``` pascal
16+ uses
17+ MacOSAll;
18+```
19+
20+## Ordinal sizes
21+
22+In Traditional Mac Pascal, the types Byte and Char occupies 2 bytes, except in packed records and in arrays. If you need data to be sharable (e g through files), you need to be aware of this. One remedy is to make such records packed. If possible use SignedByte (which actually occupy only one byte) or Integer (or better SInt16 et. al.).
23+
24+## Set sizes (Style in particular)
25+
26+Sets (eg: mySet = (opt1, opt2, opt3);) may be different sizes if you stored them to disk using CodeWarrior. This is evident with the MacOS type Style:
27+
28+sizeof(Style) = 1 in CodeWarrior sizeof(Style) = 4 in FPC
29+
30+If you want to convert on the fly, in your structures, replace Style with Byte, then do a conversion in code.
31+
32+``` pascal
33+ function ByteToStyle(byteStyle: byte): Style;
34+
35+ var
36+ returnValue: Style;
37+ count: StyleItem;
38+
39+ begin
40+ returnValue:= [];
41+ for count:= bold to extend do
42+ if ((byteStyle shr Ord(count)) and 1) <> 0
43+ then returnValue:= returnValue + [count];
44+ ByteToStyle:= returnValue;
45+ end; { ByteToStyle }
46+```
47+
48+FPC 2.2.2 and later support packing sets to smaller sizes, by using the {\$packset 1} directive. Note however that the set format is considered opaque (i.e., they're not guaranteed to have any particular internal layout), and that the internal format differs between little and big endian systems.
49+
50+## Missing CW Functions
51+
52+CodeWarrior provided built in functions for stripping the Hi and Lo words from a longint. Although most uses of this have been deprecated over the years (like menu commands,) you may still need the functions for old code. Here are substitutions:
53+
54+``` pascal
55+ function HiWrd(aLong: cardinal): word;
56+
57+ begin
58+ HiWrd:= hi(aLong);
59+ end; { HiWrd }
60+
61+ function LoWrd(aLong: cardinal): word;
62+
63+ begin
64+ LoWrd:= lo(aLong);
65+ end; { LoWrd }
66+```
67+
68+## UPP differences
69+
70+In Codewarrior Pascal, one declared a UPP generically with an "@functionName" parameter. With Carbon, Apple introduced specific UPP creator functions, and with the change to FPC, you simply pass the functionName without the "@" symbol.
71+
72+Thus,
73+
74+``` pascal
75+ theRoutine:= NewAEEventHandlerUPP(@HandleODOC); // CW Apple Event Handler
76+```
77+
78+becomes:
79+
80+``` pascal
81+ theRoutine:= NewAEEventHandlerUPP(HandleODOC); // FPC Apple Event Handler
82+```
83+
84+## Four Character Codes (FCCs)
85+
86+While FPC supports implicitly converting FCCs to integer constants to some extent, it does not do so in all cases. You can however always add an explicit typecast to OSType/Cardinal/LongWord around an FCC to ensure it is interpreted as an integer constant. This construct is endian-safe. For example: Cardinal('APPL').
87+
88+## *Result* function result alias
89+
90+FPC adds an implicit alias for the function result called *result*. This means that you cannot use local variables with the same name, as they will conflict with this identifier. The main reason is that using the function name itself is ambiguous in some situations (e.g., when passing it as a parameter to another routine it usually refers to the function result, but it can also mean the function itself as procedural variable).
91+
92+## See also
93+
94+- [[macOS Programming Tips|Converting legacy Pascal code]].
95+- [[Mode MacPas]]
96+
97+---
98+*From the Free Pascal wiki, page "Porting from Mac Pascal", as it stood on 2022-01-28 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
99+Category: Historical
100+Category: Mac OS Classic

Back to history