objpas.pp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  102. Procedure AssignFile(out t:Text;const Name:UnicodeString);
  103. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  104. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  105. Procedure AssignFile(out t:Text;const Name:RawByteString);
  106. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  107. Procedure CloseFile(Var t:Text);
  108. {$endif FPC_HAS_FEATURE_TEXTIO}
  109. {$ifdef FPC_HAS_FEATURE_FILEIO}
  110. { Typed file supoort }
  111. Procedure AssignFile(out f:TypedFile;p:pchar);
  112. Procedure AssignFile(out f:TypedFile;c:char);
  113. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  114. Procedure AssignFile(out f:TypedFile;const Name:UnicodeString);
  115. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  116. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  117. Procedure AssignFile(out f:TypedFile;const Name:RawByteString);
  118. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  119. {$endif FPC_HAS_FEATURE_FILEIO}
  120. {$ifdef FPC_HAS_FEATURE_COMMANDARGS}
  121. { ParamStr should return also an ansistring }
  122. Function ParamStr(Param : Integer) : Ansistring;
  123. {$endif FPC_HAS_FEATURE_COMMANDARGS}
  124. {****************************************************************************
  125. Resource strings.
  126. ****************************************************************************}
  127. {$ifdef FPC_HAS_FEATURE_RESOURCES}
  128. type
  129. TResourceIterator = Function (Name,Value : AnsiString; Hash : Longint; arg:pointer) : AnsiString;
  130. Function Hash(S : AnsiString) : LongWord;
  131. Procedure ResetResourceTables;
  132. Procedure FinalizeResourceTables;
  133. Procedure SetResourceStrings (SetFunction : TResourceIterator;arg:pointer);
  134. Procedure SetUnitResourceStrings (const UnitName:string;SetFunction : TResourceIterator;arg:pointer);
  135. { Delphi compatibility }
  136. type
  137. PResStringRec=^AnsiString;
  138. TResStringRec=AnsiString;
  139. Function LoadResString(p:PResStringRec):AnsiString;
  140. {$endif FPC_HAS_FEATURE_RESOURCES}
  141. implementation
  142. {****************************************************************************
  143. Compatibility routines.
  144. ****************************************************************************}
  145. {$ifdef FPC_HAS_FEATURE_FILEIO}
  146. { Untyped file support }
  147. Procedure AssignFile(out f:File;p:pchar);
  148. begin
  149. System.Assign (F,p);
  150. end;
  151. Procedure AssignFile(out f:File;c:char);
  152. begin
  153. System.Assign (F,c);
  154. end;
  155. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  156. Procedure AssignFile(out f:File;const Name:RawBytestring);
  157. begin
  158. System.Assign (F,Name);
  159. end;
  160. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  161. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  162. Procedure AssignFile(out f:File;const Name:UnicodeString);
  163. begin
  164. System.Assign (F,Name);
  165. end;
  166. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  167. Procedure CloseFile(Var f:File); [IOCheck];
  168. begin
  169. { Catch Runtime error/Exception }
  170. System.Close(f);
  171. end;
  172. {$endif FPC_HAS_FEATURE_FILEIO}
  173. {$ifdef FPC_HAS_FEATURE_TEXTIO}
  174. { Text file support }
  175. Procedure AssignFile(out t:Text;p:pchar);
  176. begin
  177. System.Assign (T,p);
  178. end;
  179. Procedure AssignFile(out t:Text;c:char);
  180. begin
  181. System.Assign (T,c);
  182. end;
  183. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  184. Procedure AssignFile(out t:Text;const Name:RawBytestring);
  185. begin
  186. System.Assign (T,Name);
  187. end;
  188. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  189. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  190. Procedure AssignFile(out t:Text;const Name:UnicodeString);
  191. begin
  192. System.Assign (T,Name);
  193. end;
  194. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  195. Procedure CloseFile(Var t:Text); [IOCheck];
  196. begin
  197. { Catch Runtime error/Exception }
  198. System.Close(T);
  199. end;
  200. {$endif FPC_HAS_FEATURE_TEXTIO}
  201. {$ifdef FPC_HAS_FEATURE_FILEIO}
  202. { Typed file support }
  203. Procedure AssignFile(out f:TypedFile;p:pchar);
  204. begin
  205. System.Assign (F,p);
  206. end;
  207. Procedure AssignFile(out f:TypedFile;c:char);
  208. begin
  209. System.Assign (F,c);
  210. end;
  211. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  212. Procedure AssignFile(out f:TypedFile;const Name:RawBytestring);
  213. begin
  214. System.Assign (F,Name);
  215. end;
  216. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  217. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  218. Procedure AssignFile(out f:TypedFile;const Name:UnicodeString);
  219. begin
  220. System.Assign (F,Name);
  221. end;
  222. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  223. {$endif FPC_HAS_FEATURE_FILEIO}
  224. {$ifdef FPC_HAS_FEATURE_COMMANDARGS}
  225. Function ParamStr(Param : Integer) : ansistring;
  226. begin
  227. {
  228. Paramstr(0) should return the name of the binary.
  229. Since this functionality is included in the system unit,
  230. we fetch it from there.
  231. Normally, pathnames are less than 255 chars anyway,
  232. so this will work correct in 99% of all cases.
  233. In time, the system unit should get a GetExeName call.
  234. }
  235. if (Param=0) then
  236. Result:=System.Paramstr(0)
  237. else if (Param>0) and (Param<argc) then
  238. Result:=Argv[Param]
  239. else
  240. Result:='';
  241. end;
  242. {$endif FPC_HAS_FEATURE_COMMANDARGS}
  243. {$ifdef FPC_HAS_FEATURE_RESOURCES}
  244. { ---------------------------------------------------------------------
  245. ResourceString support
  246. ---------------------------------------------------------------------}
  247. Function Hash(S : AnsiString) : LongWord;
  248. Var
  249. thehash,g,I : LongWord;
  250. begin
  251. thehash:=0;
  252. For I:=1 to Length(S) do { 0 terminated }
  253. begin
  254. thehash:=thehash shl 4;
  255. inc(theHash,Ord(S[i]));
  256. g:=thehash and LongWord($f shl 28);
  257. if g<>0 then
  258. begin
  259. thehash:=thehash xor (g shr 24);
  260. thehash:=thehash xor g;
  261. end;
  262. end;
  263. If theHash=0 then
  264. Hash:=$ffffffff
  265. else
  266. Hash:=TheHash;
  267. end;
  268. Type
  269. PPResourceStringRecord = ^PResourceStringRecord;
  270. TResourceStringTableList = Packed Record
  271. Count : sizeint;
  272. Tables : Array[{$ifdef cpu16}Byte{$else cpu16}Word{$endif cpu16}] of record
  273. TableStart,
  274. TableEnd : {$ifdef ver3_0}PResourceStringRecord{$else}PPResourceStringRecord{$endif};
  275. end;
  276. end;
  277. PResourceStringTableList = ^TResourceStringTableList;
  278. { Support for string constants initialized with resourcestrings }
  279. {$ifdef FPC_HAS_RESSTRINITS}
  280. PResStrInitEntry = ^TResStrInitEntry;
  281. TResStrInitEntry = record
  282. Addr: PPointer;
  283. Data: PResourceStringRecord;
  284. end;
  285. TResStrInitTable = packed record
  286. Count: {$ifdef VER2_6}longint{$else}sizeint{$endif};
  287. Tables: packed array[1..{$ifdef cpu16}8191{$else cpu16}32767{$endif cpu16}] of PResStrInitEntry;
  288. end;
  289. PResStrInitTable = ^TResStrInitTable;
  290. var
  291. ResStrInitTable : PResStrInitTable; external name '_FPC_ResStrInitTables';
  292. procedure UpdateResourceStringRefs;
  293. var
  294. i: integer;
  295. ptable: PResStrInitEntry;
  296. begin
  297. for i:=1 to ResStrInitTable^.Count do
  298. begin
  299. ptable:=ResStrInitTable^.Tables[i];
  300. while Assigned(ptable^.Addr) do
  301. begin
  302. AnsiString(ptable^.Addr^):=ptable^.Data^.CurrentValue;
  303. Inc(ptable);
  304. end;
  305. end;
  306. end;
  307. {$endif FPC_HAS_RESSTRINITS}
  308. Var
  309. ResourceStringTable : PResourceStringTableList; External Name '_FPC_ResourceStringTables';
  310. Procedure SetResourceStrings (SetFunction : TResourceIterator;arg:pointer);
  311. Var
  312. ResStr : PResourceStringRecord;
  313. i : integer;
  314. s : AnsiString;
  315. begin
  316. With ResourceStringTable^ do
  317. begin
  318. For i:=0 to Count-1 do
  319. begin
  320. ResStr:=Tables[I].TableStart{$ifndef VER3_0}^{$endif};
  321. { Skip first entry (name of the Unit) }
  322. inc(ResStr);
  323. while ResStr<Tables[I].TableEnd{$ifndef VER3_0}^{$endif} do
  324. begin
  325. s:=SetFunction(ResStr^.Name,ResStr^.DefaultValue,Longint(ResStr^.HashValue),arg);
  326. if s<>'' then
  327. ResStr^.CurrentValue:=s;
  328. inc(ResStr);
  329. end;
  330. end;
  331. end;
  332. {$ifdef FPC_HAS_RESSTRINITS}
  333. UpdateResourceStringRefs;
  334. {$endif FPC_HAS_RESSTRINITS}
  335. end;
  336. Procedure SetUnitResourceStrings (const UnitName:string;SetFunction : TResourceIterator;arg:pointer);
  337. Var
  338. ResStr : PResourceStringRecord;
  339. i : integer;
  340. s,
  341. UpUnitName : AnsiString;
  342. begin
  343. With ResourceStringTable^ do
  344. begin
  345. UpUnitName:=UpCase(UnitName);
  346. For i:=0 to Count-1 do
  347. begin
  348. ResStr:=Tables[I].TableStart{$ifndef VER3_0}^{$endif};
  349. { Check name of the Unit }
  350. if ResStr^.Name<>UpUnitName then
  351. continue;
  352. inc(ResStr);
  353. while ResStr<Tables[I].TableEnd{$ifndef VER3_0}^{$endif} do
  354. begin
  355. s:=SetFunction(ResStr^.Name,ResStr^.DefaultValue,Longint(ResStr^.HashValue),arg);
  356. if s<>'' then
  357. ResStr^.CurrentValue:=s;
  358. inc(ResStr);
  359. end;
  360. end;
  361. end;
  362. {$ifdef FPC_HAS_RESSTRINITS}
  363. { Resourcestrings of one unit may be referenced from other units,
  364. so updating everything is the only option. }
  365. UpdateResourceStringRefs;
  366. {$endif FPC_HAS_RESSTRINITS}
  367. end;
  368. Procedure ResetResourceTables;
  369. Var
  370. ResStr : PResourceStringRecord;
  371. i : integer;
  372. begin
  373. With ResourceStringTable^ do
  374. begin
  375. For i:=0 to Count-1 do
  376. begin
  377. ResStr:=Tables[I].TableStart{$ifndef VER3_0}^{$endif};
  378. { Skip first entry (name of the Unit) }
  379. inc(ResStr);
  380. while ResStr<Tables[I].TableEnd{$ifndef VER3_0}^{$endif} do
  381. begin
  382. ResStr^.CurrentValue:=ResStr^.DefaultValue;
  383. inc(ResStr);
  384. end;
  385. end;
  386. end;
  387. end;
  388. Procedure FinalizeResourceTables;
  389. Var
  390. ResStr : PResourceStringRecord;
  391. i : integer;
  392. begin
  393. With ResourceStringTable^ do
  394. begin
  395. For i:=0 to Count-1 do
  396. begin
  397. ResStr:=Tables[I].TableStart{$ifndef VER3_0}^{$endif};
  398. { Skip first entry (name of the Unit) }
  399. inc(ResStr);
  400. while ResStr<Tables[I].TableEnd{$ifndef VER3_0}^{$endif} do
  401. begin
  402. ResStr^.CurrentValue:='';
  403. inc(ResStr);
  404. end;
  405. end;
  406. end;
  407. end;
  408. Function LoadResString(p:PResStringRec):AnsiString;
  409. begin
  410. Result:=p^;
  411. end;
  412. {$endif FPC_HAS_FEATURE_RESOURCES}
  413. {$ifdef FPC_HAS_FEATURE_RESOURCES}
  414. Initialization
  415. { ResetResourceTables;}
  416. finalization
  417. FinalizeResourceTables;
  418. {$endif FPC_HAS_FEATURE_RESOURCES}
  419. end.