objpas.pp 14 KB

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