objpas.pp 15 KB

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