objpas.pp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by the Free Pascal development team
  4. This unit makes Free Pascal as much as possible Delphi compatible
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$Mode ObjFpc}
  12. {$I-}
  13. {$ifndef Unix}
  14. {$S-}
  15. {$endif}
  16. unit objpas;
  17. interface
  18. { first, in object pascal, the integer type must be redefined }
  19. {$ifdef CPU16}
  20. const
  21. MaxInt = MaxSmallint;
  22. type
  23. Integer = smallint;
  24. PInteger = ^Integer;
  25. {$else CPU16}
  26. const
  27. MaxInt = MaxLongint;
  28. type
  29. Integer = longint;
  30. PInteger = ^Integer;
  31. {$endif CPU16}
  32. { Ansistring are the default }
  33. PString = PAnsiString;
  34. { array types }
  35. {$ifdef CPU16}
  36. IntegerArray = array[0..(32768 div SizeOf(Integer))-2] of Integer;
  37. {$else CPU16}
  38. IntegerArray = array[0..$effffff] of Integer;
  39. {$endif CPU16}
  40. TIntegerArray = IntegerArray;
  41. PIntegerArray = ^IntegerArray;
  42. {$ifdef CPU16}
  43. PointerArray = array [0..(32768 div SizeOf(Pointer))-2] of Pointer;
  44. {$else CPU16}
  45. PointerArray = array [0..512*1024*1024-2] of Pointer;
  46. {$endif CPU16}
  47. TPointerArray = PointerArray;
  48. PPointerArray = ^PointerArray;
  49. // Delphi Berlin compatibility
  50. FixedInt = Int32;
  51. FixedUInt = UInt32;
  52. {$if FPC_FULLVERSION >= 20701}
  53. { Generic array type.
  54. Slightly Less useful in FPC, since dyn array compatibility is at the element level.
  55. But still useful for generic methods and of course Delphi compatibility}
  56. Generic TArray<T> = Array of T;
  57. { Generic support for enumerator interfaces. These are added here, because
  58. mode (Obj)FPC does currently not allow the overloading of types with
  59. generic types (this will need a modeswitch...) }
  60. { Note: In Delphi these two generic types inherit from the two interfaces
  61. above, but in FPC as well as in Delphi(!) this leads to problems,
  62. because of method hiding and method implementation. E.g.
  63. consider a class which enumerates integers one needs to implement
  64. a GetCurrent for TObject as well... }
  65. generic IEnumerator<T> = interface
  66. function GetCurrent: T;
  67. function MoveNext: Boolean;
  68. procedure Reset;
  69. property Current: T read GetCurrent;
  70. end;
  71. generic IEnumerable<T> = interface
  72. function GetEnumerator: specialize IEnumerator<T>;
  73. end;
  74. {$endif}
  75. {$SCOPEDENUMS ON}
  76. TEndian = (Little,Big);
  77. {$SCOPEDENUMS OFF}
  78. {$ifdef FPC_HAS_FEATURE_CLASSES}
  79. Var
  80. ExceptionClass: TClass; { Exception base class (must actually be Exception, defined in sysutils ) }
  81. {$endif FPC_HAS_FEATURE_CLASSES}
  82. {****************************************************************************
  83. Compatibility routines.
  84. ****************************************************************************}
  85. {$ifdef FPC_HAS_FEATURE_FILEIO}
  86. { Untyped file support }
  87. Procedure AssignFile(out f:File;p:pchar);
  88. Procedure AssignFile(out f:File;c:char);
  89. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  90. Procedure AssignFile(out f:File;const Name:UnicodeString);
  91. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  92. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  93. Procedure AssignFile(out f:File;const Name:RawByteString);
  94. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  95. Procedure CloseFile(var f:File);
  96. {$endif FPC_HAS_FEATURE_FILEIO}
  97. {$ifdef FPC_HAS_FEATURE_TEXTIO}
  98. { Text file support }
  99. Procedure AssignFile(out t:Text;p:pchar);
  100. Procedure AssignFile(out t:Text;c:char);
  101. Procedure AssignFile(out t:Text;p:pchar; aCodePage : TSystemCodePage);
  102. Procedure AssignFile(out t:Text;c:char; aCodePage : TSystemCodePage);
  103. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  104. Procedure AssignFile(out t:Text;const Name:UnicodeString);
  105. Procedure AssignFile(out t:Text;const Name:UnicodeString; aCodePage : TSystemCodePage);
  106. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  107. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  108. Procedure AssignFile(out t:Text;const Name:RawByteString);
  109. Procedure AssignFile(out t:Text;const Name:RawByteString; aCodePage : TSystemCodePage);
  110. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  111. Procedure CloseFile(Var t:Text);
  112. {$endif FPC_HAS_FEATURE_TEXTIO}
  113. {$ifdef FPC_HAS_FEATURE_FILEIO}
  114. { Typed file supoort }
  115. Procedure AssignFile(out f:TypedFile;p:pchar);
  116. Procedure AssignFile(out f:TypedFile;c:char);
  117. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  118. Procedure AssignFile(out f:TypedFile;const Name:UnicodeString);
  119. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  120. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  121. Procedure AssignFile(out f:TypedFile;const Name:RawByteString);
  122. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  123. {$endif FPC_HAS_FEATURE_FILEIO}
  124. {$ifdef FPC_HAS_FEATURE_COMMANDARGS}
  125. {$ifdef MSWINDOWS}
  126. {$define HAS_PARAMSTRA}
  127. {$undef FPC_HAS_FEATURE_COMMANDARGS} // Skip the implementation of ParamStr()
  128. {$endif MSWINDOWS}
  129. { ParamStr should return also an ansistring }
  130. Function ParamStr(Param : Integer) : Ansistring;
  131. {$ifdef HAS_PARAMSTRA} external name '_FPC_ParamStrA'; {$endif}
  132. {$endif FPC_HAS_FEATURE_COMMANDARGS}
  133. {****************************************************************************
  134. Resource strings.
  135. ****************************************************************************}
  136. {$ifdef FPC_HAS_FEATURE_RESOURCES}
  137. type
  138. TResourceIterator = Function (Name,Value : AnsiString; Hash : Longint; arg:pointer) : AnsiString;
  139. Function Hash(S : AnsiString) : LongWord;
  140. Procedure ResetResourceTables;
  141. Procedure FinalizeResourceTables;
  142. Procedure SetResourceStrings (SetFunction : TResourceIterator;arg:pointer);
  143. Procedure SetUnitResourceStrings (const UnitName:string;SetFunction : TResourceIterator;arg:pointer);
  144. { Delphi compatibility }
  145. type
  146. PResStringRec=^AnsiString;
  147. TResStringRec=AnsiString;
  148. Function LoadResString(p:PResStringRec):AnsiString;
  149. {$endif FPC_HAS_FEATURE_RESOURCES}
  150. implementation
  151. {****************************************************************************
  152. Compatibility routines.
  153. ****************************************************************************}
  154. {$ifdef FPC_HAS_FEATURE_FILEIO}
  155. { Untyped file support }
  156. Procedure AssignFile(out f:File;p:pchar);
  157. begin
  158. System.Assign (F,p);
  159. end;
  160. Procedure AssignFile(out f:File;c:char);
  161. begin
  162. System.Assign (F,c);
  163. end;
  164. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  165. Procedure AssignFile(out f:File;const Name:RawBytestring);
  166. begin
  167. System.Assign (F,Name);
  168. end;
  169. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  170. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  171. Procedure AssignFile(out f:File;const Name:UnicodeString);
  172. begin
  173. System.Assign (F,Name);
  174. end;
  175. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  176. Procedure CloseFile(Var f:File); [IOCheck];
  177. begin
  178. { Catch Runtime error/Exception }
  179. System.Close(f);
  180. end;
  181. {$endif FPC_HAS_FEATURE_FILEIO}
  182. {$ifdef FPC_HAS_FEATURE_TEXTIO}
  183. { Text file support }
  184. Procedure AssignFile(out t:Text;p:pchar);
  185. begin
  186. System.Assign (T,p);
  187. end;
  188. Procedure AssignFile(out t:Text;p:pchar; aCodePage : TSystemCodePage);
  189. begin
  190. System.Assign (T,p);
  191. SetTextCodePage(T,aCodePage);
  192. end;
  193. Procedure AssignFile(out t:Text;c:char);
  194. begin
  195. System.Assign (T,c);
  196. end;
  197. Procedure AssignFile(out t:Text;c:char; aCodePage : TSystemCodePage);
  198. begin
  199. System.Assign (T,c);
  200. SetTextCodePage(T,aCodePage);
  201. end;
  202. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  203. Procedure AssignFile(out t:Text;const Name:RawBytestring; aCodePage : TSystemCodePage);
  204. begin
  205. System.Assign (T,Name);
  206. SetTextCodePage(T,aCodePage);
  207. end;
  208. Procedure AssignFile(out t:Text;const Name:RawBytestring);
  209. begin
  210. System.Assign (T,Name);
  211. end;
  212. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  213. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  214. Procedure AssignFile(out t:Text;const Name:UnicodeString; aCodePage : TSystemCodePage);
  215. begin
  216. System.Assign (T,Name);
  217. SetTextCodePage(T,aCodePage);
  218. end;
  219. Procedure AssignFile(out t:Text;const Name:UnicodeString);
  220. begin
  221. System.Assign (T,Name);
  222. end;
  223. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  224. Procedure CloseFile(Var t:Text); [IOCheck];
  225. begin
  226. { Catch Runtime error/Exception }
  227. System.Close(T);
  228. end;
  229. {$endif FPC_HAS_FEATURE_TEXTIO}
  230. {$ifdef FPC_HAS_FEATURE_FILEIO}
  231. { Typed file support }
  232. Procedure AssignFile(out f:TypedFile;p:pchar);
  233. begin
  234. System.Assign (F,p);
  235. end;
  236. Procedure AssignFile(out f:TypedFile;c:char);
  237. begin
  238. System.Assign (F,c);
  239. end;
  240. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  241. Procedure AssignFile(out f:TypedFile;const Name:RawBytestring);
  242. begin
  243. System.Assign (F,Name);
  244. end;
  245. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  246. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  247. Procedure AssignFile(out f:TypedFile;const Name:UnicodeString);
  248. begin
  249. System.Assign (F,Name);
  250. end;
  251. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  252. {$endif FPC_HAS_FEATURE_FILEIO}
  253. {$ifdef FPC_HAS_FEATURE_COMMANDARGS}
  254. Function ParamStr(Param : Integer) : ansistring;
  255. begin
  256. {
  257. Paramstr(0) should return the name of the binary.
  258. Since this functionality is included in the system unit,
  259. we fetch it from there.
  260. Normally, pathnames are less than 255 chars anyway,
  261. so this will work correct in 99% of all cases.
  262. In time, the system unit should get a GetExeName call.
  263. }
  264. if (Param=0) then
  265. Result:=System.Paramstr(0)
  266. else if (Param>0) and (Param<argc) then
  267. Result:=Argv[Param]
  268. else
  269. Result:='';
  270. end;
  271. {$endif FPC_HAS_FEATURE_COMMANDARGS}
  272. {$ifdef FPC_HAS_FEATURE_RESOURCES}
  273. { ---------------------------------------------------------------------
  274. ResourceString support
  275. ---------------------------------------------------------------------}
  276. Function Hash(S : AnsiString) : LongWord;
  277. Var
  278. thehash,g,I : LongWord;
  279. begin
  280. thehash:=0;
  281. For I:=1 to Length(S) do { 0 terminated }
  282. begin
  283. thehash:=thehash shl 4;
  284. {$push}
  285. {$R-}{$Q-}
  286. inc(theHash,Ord(S[i]));
  287. {$pop}
  288. g:=thehash and LongWord($f shl 28);
  289. if g<>0 then
  290. begin
  291. thehash:=thehash xor (g shr 24);
  292. thehash:=thehash xor g;
  293. end;
  294. end;
  295. If theHash=0 then
  296. Hash:=$ffffffff
  297. else
  298. Hash:=TheHash;
  299. end;
  300. Type
  301. PPResourceStringRecord = ^PResourceStringRecord;
  302. TResourceStringTableList = Packed Record
  303. Count : sizeint;
  304. Tables : Array[{$ifdef cpu16}Byte{$else cpu16}Word{$endif cpu16}] of record
  305. TableStart,
  306. TableEnd : {$ifdef ver3_0}PResourceStringRecord{$else}PPResourceStringRecord{$endif};
  307. end;
  308. end;
  309. PResourceStringTableList = ^TResourceStringTableList;
  310. { Support for string constants initialized with resourcestrings }
  311. {$ifdef FPC_HAS_RESSTRINITS}
  312. PResStrInitEntry = ^TResStrInitEntry;
  313. TResStrInitEntry = record
  314. Addr: PPointer;
  315. Data: PResourceStringRecord;
  316. end;
  317. TResStrInitTable = packed record
  318. Count: sizeint;
  319. Tables: packed array[1..{$ifdef cpu16}8191{$else cpu16}32767{$endif cpu16}] of PResStrInitEntry;
  320. end;
  321. PResStrInitTable = ^TResStrInitTable;
  322. var
  323. ResStrInitTable : PResStrInitTable; external name '_FPC_ResStrInitTables';
  324. procedure UpdateResourceStringRefs;
  325. var
  326. i: integer;
  327. ptable: PResStrInitEntry;
  328. begin
  329. for i:=1 to ResStrInitTable^.Count do
  330. begin
  331. ptable:=ResStrInitTable^.Tables[i];
  332. while Assigned(ptable^.Addr) do
  333. begin
  334. AnsiString(ptable^.Addr^):=ptable^.Data^.CurrentValue;
  335. Inc(ptable);
  336. end;
  337. end;
  338. end;
  339. {$endif FPC_HAS_RESSTRINITS}
  340. Var
  341. ResourceStringTable : PResourceStringTableList; External Name '_FPC_ResourceStringTables';
  342. Procedure SetResourceStrings (SetFunction : TResourceIterator;arg:pointer);
  343. Var
  344. ResStr : PResourceStringRecord;
  345. i : integer;
  346. s : AnsiString;
  347. begin
  348. With ResourceStringTable^ do
  349. begin
  350. For i:=0 to Count-1 do
  351. begin
  352. ResStr:=Tables[I].TableStart{$ifndef VER3_0}^{$endif};
  353. { Skip first entry (name of the Unit) }
  354. inc(ResStr);
  355. while ResStr<Tables[I].TableEnd{$ifndef VER3_0}^{$endif} do
  356. begin
  357. s:=SetFunction(ResStr^.Name,ResStr^.DefaultValue,Longint(ResStr^.HashValue),arg);
  358. if s<>'' then
  359. ResStr^.CurrentValue:=s;
  360. inc(ResStr);
  361. end;
  362. end;
  363. end;
  364. {$ifdef FPC_HAS_RESSTRINITS}
  365. UpdateResourceStringRefs;
  366. {$endif FPC_HAS_RESSTRINITS}
  367. end;
  368. Procedure SetUnitResourceStrings (const UnitName:string;SetFunction : TResourceIterator;arg:pointer);
  369. Var
  370. ResStr : PResourceStringRecord;
  371. i : integer;
  372. s,
  373. UpUnitName : AnsiString;
  374. begin
  375. With ResourceStringTable^ do
  376. begin
  377. UpUnitName:=UpCase(UnitName);
  378. For i:=0 to Count-1 do
  379. begin
  380. ResStr:=Tables[I].TableStart{$ifndef VER3_0}^{$endif};
  381. { Check name of the Unit }
  382. if ResStr^.Name<>UpUnitName then
  383. continue;
  384. inc(ResStr);
  385. while ResStr<Tables[I].TableEnd{$ifndef VER3_0}^{$endif} do
  386. begin
  387. s:=SetFunction(ResStr^.Name,ResStr^.DefaultValue,Longint(ResStr^.HashValue),arg);
  388. if s<>'' then
  389. ResStr^.CurrentValue:=s;
  390. inc(ResStr);
  391. end;
  392. end;
  393. end;
  394. {$ifdef FPC_HAS_RESSTRINITS}
  395. { Resourcestrings of one unit may be referenced from other units,
  396. so updating everything is the only option. }
  397. UpdateResourceStringRefs;
  398. {$endif FPC_HAS_RESSTRINITS}
  399. end;
  400. Procedure ResetResourceTables;
  401. Var
  402. ResStr : PResourceStringRecord;
  403. i : integer;
  404. begin
  405. With ResourceStringTable^ do
  406. begin
  407. For i:=0 to Count-1 do
  408. begin
  409. ResStr:=Tables[I].TableStart{$ifndef VER3_0}^{$endif};
  410. { Skip first entry (name of the Unit) }
  411. inc(ResStr);
  412. while ResStr<Tables[I].TableEnd{$ifndef VER3_0}^{$endif} do
  413. begin
  414. ResStr^.CurrentValue:=ResStr^.DefaultValue;
  415. inc(ResStr);
  416. end;
  417. end;
  418. end;
  419. end;
  420. Procedure FinalizeResourceTables;
  421. Var
  422. ResStr : PResourceStringRecord;
  423. i : integer;
  424. begin
  425. With ResourceStringTable^ do
  426. begin
  427. For i:=0 to Count-1 do
  428. begin
  429. ResStr:=Tables[I].TableStart{$ifndef VER3_0}^{$endif};
  430. { Skip first entry (name of the Unit) }
  431. inc(ResStr);
  432. while ResStr<Tables[I].TableEnd{$ifndef VER3_0}^{$endif} do
  433. begin
  434. ResStr^.CurrentValue:='';
  435. inc(ResStr);
  436. end;
  437. end;
  438. end;
  439. end;
  440. Function LoadResString(p:PResStringRec):AnsiString;
  441. begin
  442. Result:=p^;
  443. end;
  444. {$endif FPC_HAS_FEATURE_RESOURCES}
  445. {$ifdef FPC_HAS_FEATURE_RESOURCES}
  446. Initialization
  447. { ResetResourceTables;}
  448. finalization
  449. FinalizeResourceTables;
  450. {$endif FPC_HAS_FEATURE_RESOURCES}
  451. end.