2
0

objpash.inc 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2005 by the Free Pascal development team
  4. This unit makes Free Pascal as much as possible Delphi compatible,
  5. defining several internal structures for classes, interfaces, and
  6. resource strings.
  7. Additionally this file defines the interface of TObject, providing
  8. their basic implementation in the corresponding objpas.inc file.
  9. WARNING: IF YOU CHANGE SOME OF THESE INTERNAL RECORDS, MAKE SURE
  10. TO MODIFY THE COMPILER AND OBJPAS.INC ACCORDINGLY, OTHERWISE
  11. THIS WILL LEAD TO CRASHES IN THE RESULTING COMPILER AND/OR RTL.
  12. IN PARTICULAR, THE IMPLEMENTATION PART OF THIS INCLUDE FILE,
  13. OBJPAS.INC, USES SOME HARDCODED RECORD MEMBER OFFSETS.
  14. See the file COPYING.FPC, included in this distribution,
  15. for details about the copyright.
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  19. **********************************************************************}
  20. {*****************************************************************************
  21. Basic Types/constants
  22. *****************************************************************************}
  23. type
  24. TextFile = Text;
  25. PGuid = ^TGuid;
  26. { TGuid }
  27. TGuid = packed record
  28. Public
  29. class operator =(const aLeft, aRight: TGUID): Boolean;
  30. class operator <>(const aLeft, aRight: TGUID): Boolean; inline;
  31. class function Empty: TGUID; static;
  32. class function Create(const aData; aBigEndian: Boolean = False): TGUID; overload; static;
  33. class function Create(const aData: array of Byte; aStartIndex: Cardinal; aBigEndian: Boolean = False): TGUID; overload; static;
  34. class function Create(const aData : PByte; aBigEndian: Boolean = False): TGUID; overload; static;
  35. function IsEmpty: Boolean;
  36. Public
  37. case integer of
  38. 1 : (
  39. Data1 : DWord;
  40. Data2 : word;
  41. Data3 : word;
  42. Data4 : array[0..7] of byte;
  43. );
  44. 2 : (
  45. D1 : DWord;
  46. D2 : word;
  47. D3 : word;
  48. D4 : array[0..7] of byte;
  49. );
  50. 3 : ( { uuid fields according to RFC4122 }
  51. time_low : dword; // The low field of the timestamp
  52. time_mid : word; // The middle field of the timestamp
  53. time_hi_and_version : word; // The high field of the timestamp multiplexed with the version number
  54. clock_seq_hi_and_reserved : byte; // The high field of the clock sequence multiplexed with the variant
  55. clock_seq_low : byte; // The low field of the clock sequence
  56. node : array[0..5] of byte; // The spatially unique node identifier
  57. );
  58. end;
  59. {$ifdef FPC_HAS_FEATURE_CLASSES}
  60. const
  61. vmtInstanceSize = 0;
  62. vmtParent = sizeof(SizeInt)*2;
  63. { These were negative value's, but are now positive, else classes
  64. couldn't be used with shared linking which copies only all data from
  65. the .global directive and not the data before the directive (PFV) }
  66. vmtClassName = vmtParent+sizeof(pointer);
  67. vmtDynamicTable = vmtParent+sizeof(pointer)*2;
  68. vmtMethodTable = vmtParent+sizeof(pointer)*3;
  69. vmtFieldTable = vmtParent+sizeof(pointer)*4;
  70. vmtTypeInfo = vmtParent+sizeof(pointer)*5;
  71. vmtInitTable = vmtParent+sizeof(pointer)*6;
  72. vmtAutoTable = vmtParent+sizeof(pointer)*7;
  73. vmtIntfTable = vmtParent+sizeof(pointer)*8;
  74. vmtMsgStrPtr = vmtParent+sizeof(pointer)*9;
  75. { methods }
  76. vmtMethodStart = vmtParent+sizeof(pointer)*10;
  77. vmtDestroy = vmtMethodStart;
  78. vmtNewInstance = vmtMethodStart+sizeof(codepointer);
  79. vmtFreeInstance = vmtMethodStart+sizeof(codepointer)*2;
  80. vmtSafeCallException = vmtMethodStart+sizeof(codepointer)*3;
  81. vmtDefaultHandler = vmtMethodStart+sizeof(codepointer)*4;
  82. vmtAfterConstruction = vmtMethodStart+sizeof(codepointer)*5;
  83. vmtBeforeDestruction = vmtMethodStart+sizeof(codepointer)*6;
  84. vmtDefaultHandlerStr = vmtMethodStart+sizeof(codepointer)*7;
  85. vmtDispatch = vmtMethodStart+sizeof(codepointer)*8;
  86. vmtDispatchStr = vmtMethodStart+sizeof(codepointer)*9;
  87. vmtEquals = vmtMethodStart+sizeof(codepointer)*10;
  88. vmtGetHashCode = vmtMethodStart+sizeof(codepointer)*11;
  89. vmtToString = vmtMethodStart+sizeof(codepointer)*12;
  90. { IInterface }
  91. S_OK = 0;
  92. S_FALSE = 1;
  93. E_NOINTERFACE = hresult($80004002);
  94. E_UNEXPECTED = hresult($8000FFFF);
  95. E_NOTIMPL = hresult($80004001);
  96. type
  97. { now the let's declare the base classes for the class object
  98. model. The compiler expects TObject and IUnknown to be defined
  99. first as forward classes }
  100. TObject = class;
  101. IUnknown = interface;
  102. TClass = class of tobject;
  103. PClass = ^tclass;
  104. { to access the message table from outside }
  105. TMsgStrTable = record
  106. name : pshortstring;
  107. method : codepointer;
  108. end;
  109. PMsgStrTable = ^TMsgStrTable;
  110. TStringMessageTable = record
  111. count : longint;
  112. msgstrtable : array[0..0] of tmsgstrtable;
  113. end;
  114. pstringmessagetable = ^tstringmessagetable;
  115. pinterfacetable = ^tinterfacetable;
  116. PVmt = ^TVmt;
  117. PPVmt = ^PVmt;
  118. TVmt = record
  119. vInstanceSize: SizeInt;
  120. vInstanceSize2: SizeInt;
  121. vParentRef: {$ifdef VER3_0}PVmt{$else}PPVmt{$endif};
  122. vClassName: PShortString;
  123. vDynamicTable: Pointer;
  124. vMethodTable: Pointer;
  125. vFieldTable: Pointer;
  126. vTypeInfo: Pointer;
  127. vInitTable: Pointer;
  128. vAutoTable: Pointer;
  129. vIntfTable: PInterfaceTable;
  130. vMsgStrPtr: pstringmessagetable;
  131. vDestroy: CodePointer;
  132. vNewInstance: CodePointer;
  133. vFreeInstance: CodePointer;
  134. vSafeCallException: CodePointer;
  135. vDefaultHandler: CodePointer;
  136. vAfterConstruction: CodePointer;
  137. vBeforeDestruction: CodePointer;
  138. vDefaultHandlerStr: CodePointer;
  139. vDispatch: CodePointer;
  140. vDispatchStr: CodePointer;
  141. vEquals: CodePointer;
  142. vGetHashCode: CodePointer;
  143. vToString: CodePointer;
  144. private
  145. function GetvParent: PVmt; inline;
  146. public
  147. property vParent: PVmt read GetvParent;
  148. end;
  149. // This enumerate is found both in the rtl and compiler. Do not change the order of the fields.
  150. tinterfaceentrytype = (etStandard,
  151. etVirtualMethodResult,
  152. etStaticMethodResult,
  153. etFieldValue,
  154. etVirtualMethodClass,
  155. etStaticMethodClass,
  156. etFieldValueClass
  157. );
  158. pinterfaceentry = ^tinterfaceentry;
  159. tinterfaceentry = record
  160. private
  161. function GetIID: pguid; inline;
  162. function GetIIDStr: pshortstring; inline;
  163. public
  164. property IID: pguid read GetIID;
  165. property IIDStr: pshortstring read GetIIDStr;
  166. public
  167. IIDRef : {$IFNDEF VER3_0}^{$ENDIF}pguid; { if assigned(IID) then Com else Corba}
  168. VTable : Pointer;
  169. case integer of
  170. 1 : (
  171. IOffset: sizeuint;
  172. );
  173. 2 : (
  174. IOffsetAsCodePtr: CodePointer;
  175. IIDStrRef : {$IFNDEF VER3_0}^{$ENDIF}pshortstring; { never nil. Com: upper(GuidToString(IID^)) }
  176. IType : tinterfaceentrytype;
  177. );
  178. end;
  179. tinterfacetable = record
  180. EntryCount : sizeuint;
  181. Entries : array[0..0] of tinterfaceentry;
  182. end;
  183. PMethod = ^TMethod;
  184. TMethod = record
  185. Code : CodePointer;
  186. Data : Pointer;
  187. public
  188. class operator =(const aLeft, aRight: TMethod): Boolean; inline;
  189. class operator <>(const aLeft, aRight: TMethod): Boolean; inline;
  190. class operator >(const aLeft, aRight: TMethod): Boolean; inline;
  191. class operator >=(const aLeft, aRight: TMethod): Boolean; inline;
  192. class operator <(const aLeft, aRight: TMethod): Boolean; inline;
  193. class operator <=(const aLeft, aRight: TMethod): Boolean; inline;
  194. end;
  195. // "Maximum" available stringtype : Shortstring, AnsiString or WideString
  196. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  197. {$IFNDEF UNICODERTL}
  198. RTLString = ansistring;
  199. {$ELSE UNICODERTL}
  200. RTLString = unicodestring;
  201. {$ENDIF UNICODERTL}
  202. {$else FPC_HAS_FEATURE_ANSISTRINGS}
  203. RTLString = shortstring;
  204. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  205. // Dispatch needs a DWord as the first 4 bytes in its untyped parameter.
  206. // Note that this is different from Delphi, which uses only a word.
  207. TDispatchMessage = record
  208. MsgID: DWord;
  209. end;
  210. TObject = class
  211. {$IFDEF SYSTEM_HAS_FEATURE_MONITOR}
  212. strict private
  213. _MonitorData : Pointer;
  214. private
  215. function SetMonitorData(aData,aCheckOld : Pointer) : Pointer; inline;
  216. function GetMonitorData: Pointer; inline;
  217. {$ENDIF}
  218. protected
  219. function GetDisposed : Boolean; inline;
  220. public
  221. { please don't change the order of virtual methods, because
  222. their vmt offsets are used by some assembler code which uses
  223. hard coded addresses (FK) }
  224. constructor Create;
  225. { the virtual procedures must be in THAT order }
  226. destructor Destroy;virtual;
  227. class function newinstance : tobject;virtual;
  228. procedure FreeInstance;virtual;
  229. function SafeCallException(exceptobject : tobject;
  230. exceptaddr : codepointer) : HResult;virtual;
  231. procedure DefaultHandler(var message);virtual;
  232. procedure Free;
  233. class function InitInstance(instance : pointer) : tobject;
  234. procedure CleanupInstance;
  235. class function ClassType : tclass;{$ifdef SYSTEMINLINE}inline;{$endif}
  236. class function ClassInfo : pointer;
  237. class function ClassName : shortstring;
  238. class function ClassNameIs(const name : RTLString) : boolean;
  239. class function ClassParent : tclass;{$ifdef SYSTEMINLINE}inline;{$endif}
  240. class function InstanceSize : SizeInt;// {$ifdef SYSTEMINLINE}inline;{$endif}
  241. class function InheritsFrom(aclass : tclass) : boolean;
  242. class function StringMessageTable : pstringmessagetable;
  243. class function MethodAddress(const name : shortstring) : codepointer;
  244. class function MethodName(address : codepointer) : shortstring;
  245. function FieldAddress(const name : shortstring) : pointer;
  246. { new since Delphi 4 }
  247. procedure AfterConstruction;virtual;
  248. procedure BeforeDestruction;virtual;
  249. { new for gtk, default handler for text based messages }
  250. procedure DefaultHandlerStr(var message);virtual;
  251. { message handling routines }
  252. procedure Dispatch(var message);virtual;
  253. procedure DispatchStr(var message);virtual;
  254. { interface functions }
  255. function GetInterface(const iid : tguid; out obj) : boolean;
  256. function GetInterface(const iidstr : shortstring;out obj) : boolean;
  257. function GetInterfaceByStr(const iidstr : shortstring; out obj) : boolean;
  258. function GetInterfaceWeak(const iid : tguid; out obj) : boolean; // equal to GetInterface but the interface returned is not referenced
  259. class function GetInterfaceEntry(const iid : tguid) : pinterfaceentry;
  260. class function GetInterfaceEntryByStr(const iidstr : shortstring) : pinterfaceentry;
  261. class function GetInterfaceTable : pinterfacetable;
  262. { new since Delphi 2009 }
  263. class function UnitName : RTLString;
  264. class function QualifiedClassName: RTLString;
  265. Procedure DisposeOf; inline;
  266. Procedure CheckDisposed; inline;
  267. Property Disposed : Boolean Read GetDisposed;
  268. function Equals(Obj: TObject) : boolean;virtual;
  269. function GetHashCode: PtrInt;virtual;
  270. function ToString: RTLString; virtual;
  271. end;
  272. IUnknown = interface
  273. ['{00000000-0000-0000-C000-000000000046}']
  274. function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  275. function _AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  276. function _Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  277. end;
  278. IInterface = IUnknown;
  279. {$M+}
  280. IInvokable = interface(IInterface)
  281. end;
  282. {$M-}
  283. { enumerator support }
  284. IEnumerator = interface(IInterface)
  285. function GetCurrent: TObject;
  286. function MoveNext: Boolean;
  287. procedure Reset;
  288. property Current: TObject read GetCurrent;
  289. end;
  290. IEnumerable = interface(IInterface)
  291. function GetEnumerator: IEnumerator;
  292. end;
  293. { for native dispinterface support }
  294. IDispatch = interface(IUnknown)
  295. ['{00020400-0000-0000-C000-000000000046}']
  296. function GetTypeInfoCount(out count : longint) : HResult;stdcall;
  297. function GetTypeInfo(Index,LocaleID : longint;
  298. out TypeInfo): HResult;stdcall;
  299. function GetIDsOfNames(const iid: TGUID; names: Pointer;
  300. NameCount, LocaleID: LongInt; DispIDs: Pointer) : HResult;stdcall;
  301. function Invoke(DispID: LongInt;const iid : TGUID;
  302. LocaleID : longint; Flags: Word;var params;
  303. VarResult,ExcepInfo,ArgErr : pointer) : HResult;stdcall;
  304. end;
  305. { TInterfacedObject }
  306. TInterfacedObject = class(TObject,IUnknown)
  307. protected
  308. FRefCount : longint;
  309. FDestroyCount : longint;
  310. { implement methods of IUnknown }
  311. function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  312. function _AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  313. function _Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  314. public
  315. destructor Destroy; override;
  316. procedure AfterConstruction;override;
  317. procedure BeforeDestruction;override;
  318. class function NewInstance : TObject;override;
  319. property RefCount : longint read FRefCount;
  320. end;
  321. TInterfacedClass = class of TInterfacedObject;
  322. TAggregatedObject = class(TObject)
  323. private
  324. fcontroller: Pointer;
  325. function GetController: IUnknown;
  326. protected
  327. { implement methods of IUnknown }
  328. function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  329. function _AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  330. function _Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  331. public
  332. constructor Create(const aController: IUnknown);
  333. property Controller : IUnknown read GetController;
  334. end;
  335. TContainedObject = class(TAggregatedObject,IInterface)
  336. protected
  337. function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;virtual; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  338. end;
  339. TNoRefCountObject = class(TObject, IInterface)
  340. protected
  341. function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  342. function _AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  343. function _Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  344. end;
  345. TInterfaceThunk = Class(TInterfacedObject)
  346. Public
  347. Type
  348. TArgData = record
  349. addr : pointer; // Location
  350. info : pointer; // type info (if available: nil for untyped args)
  351. idx : smallint; // param index in rtti
  352. ahigh : sizeint; // For open arrays, high()
  353. end;
  354. PArgData = ^TargData;
  355. TThunkCallBack = Procedure(aInstance: Pointer; aMethod,aCount : Longint; aData : PArgData) of object;
  356. Private
  357. FCallback : TThunkCallback;
  358. Protected
  359. function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  360. Procedure Thunk(aMethod: Longint; aCount : Longint; aData : PArgData); virtual;
  361. Public
  362. constructor create(aCallBack : TThunkCallback);
  363. function InterfaceVMTOffset : word; virtual;
  364. end;
  365. TInterfaceThunkClass = class of TInterfaceThunk;
  366. { some pointer definitions }
  367. PUnknown = ^IUnknown;
  368. PPUnknown = ^PUnknown;
  369. PDispatch = ^IDispatch;
  370. PPDispatch = ^PDispatch;
  371. PInterface = PUnknown;
  372. {*****************************************************************************
  373. Exception support
  374. *****************************************************************************}
  375. {$ifdef FPC_USE_PSABIEH}
  376. {$if (defined(CPUARMEL) or defined(CPUARMHF)) and not defined(darwin)}
  377. {$define __ARM_EABI_UNWINDER__}
  378. {$endif}
  379. { needed here for TExceptObject (rest is in psabiehh.inc) }
  380. FPC_Unwind_Reason_Code = longint; {cint}
  381. FPC_Unwind_Action = longint; {cint}
  382. {$ifdef __ARM_EABI_UNWINDER__}
  383. FPC_Unwind_State = longint; {cint}
  384. {$endif}
  385. PFPC_Unwind_Exception = ^FPC_Unwind_Exception;
  386. FPC_Unwind_Exception_Cleanup_Fn =
  387. procedure(reason: FPC_Unwind_Reason_Code; exc: PFPC_Unwind_Exception); cdecl;
  388. FPC_Unwind_Exception = record
  389. { qword instead of array of AnsiChar to ensure proper alignment and
  390. padding, and also easier to compare }
  391. exception_class: qword;
  392. exception_cleanup: FPC_Unwind_Exception_Cleanup_Fn;
  393. {$ifdef __ARM_EABI_UNWINDER__}
  394. { rest of UCB }
  395. // Unwinder cache, private fields for the unwinder's use
  396. unwinder_cache: record
  397. reserved1, // init reserved1 to 0, then don't touch
  398. reserved2,
  399. reserved3,
  400. reserved4,
  401. reserved5: UInt32;
  402. end;
  403. // Propagation barrier cache (valid after phase 1):
  404. barrier_cache: record
  405. sp: PtrUInt;
  406. bitpattern: array[0..4] of UInt32;
  407. end;
  408. // Cleanup cache (preserved over cleanup):
  409. cleanup_cache: record
  410. bitpattern: array[0..3] of UInt32;
  411. end;
  412. // Pr cache (for pr's benefit):
  413. pr_cache: record
  414. fnstart: UInt32; // function start address
  415. ehtp: pointer; // pointer to EHT entry header word
  416. additional: UInt32; // additional data
  417. reserved1: UInt32;
  418. end;
  419. {$else}
  420. private_1: ptruint;
  421. private_2: ptruint;
  422. private_3: ptruint;
  423. private_4: ptruint;
  424. private_5: ptruint;
  425. private_6: ptruint;
  426. {$endif}
  427. end;
  428. {$endif FPC_USE_PSABIEH}
  429. TExceptProc = Procedure (Obj : TObject; Addr : CodePointer; FrameCount:Longint; Frame: PCodePointer);
  430. { Exception object stack }
  431. PExceptObject = ^TExceptObject;
  432. TExceptObject = record
  433. FObject : TObject;
  434. Addr : codepointer;
  435. Next : PExceptObject;
  436. refcount : Longint;
  437. Framecount : Longint;
  438. Frames : PCodePointer;
  439. {$ifdef FPC_USE_WIN32_SEH}
  440. SEHFrame : Pointer;
  441. ExceptRec : Pointer;
  442. ReraiseBuf : jmp_buf;
  443. {$endif FPC_USE_WIN32_SEH}
  444. {$ifdef FPC_USE_PSABIEH}
  445. {$ifndef __ARM_EABI_UNWINDER__}
  446. { cached info from unwind phase for action phase }
  447. handler_switch_value: longint;
  448. language_specific_data: PByte;
  449. landing_pad: PtrUInt;
  450. {$endif __ARM_EABI_UNWINDER__}
  451. { libunwind exception handling data (must be last!) }
  452. unwind_exception: FPC_Unwind_Exception;
  453. {$endif FPC_USE_PSABIEH}
  454. end;
  455. Const
  456. ExceptProc : TExceptProc = Nil;
  457. RaiseProc : TExceptProc = Nil;
  458. RaiseMaxFrameCount : Longint = 16;
  459. Function RaiseList : PExceptObject;
  460. { @abstract(increase exception reference count)
  461. When leaving an except block, the exception object is normally
  462. freed automatically. To avoid this, call this function.
  463. If within the exception object you decide that you don't need
  464. the exception after all, call @link(ReleaseExceptionObject).
  465. Otherwise, if the reference count is > 0, the exception object
  466. goes into your "property" and you need to free it manually.
  467. The effect of this function is countered by re-raising an exception
  468. via "raise;", this zeroes the reference count again.
  469. Calling this method is only valid within an except block.
  470. @return(pointer to the exception object) }
  471. function AcquireExceptionObject: Pointer;
  472. { @abstract(decrease exception reference count)
  473. After calling @link(AcquireExceptionObject) you can call this method
  474. to decrease the exception reference count again.
  475. If the reference count is > 0, the exception object
  476. goes into your "property" and you need to free it manually.
  477. Calling this method is only valid within an except block. }
  478. procedure ReleaseExceptionObject;
  479. const
  480. { for safe as operator support }
  481. IObjectInstance: TGuid = '{D91C9AF4-3C93-420F-A303-BF5BA82BFD23}';
  482. {*****************************************************************************
  483. Attribute support
  484. *****************************************************************************}
  485. Type
  486. {$PUSH}
  487. { disable the warning that the constructor should be public }
  488. {$WARN 3018 OFF}
  489. TCustomAttribute = class(TObject)
  490. private
  491. { if the user wants to use a parameterless constructor they need to
  492. explicitely declare it in their type }
  493. constructor Create;
  494. end;
  495. {$POP}
  496. TUnimplementedAttribute = class(TCustomAttribute)
  497. public
  498. constructor Create; unimplemented;
  499. end;
  500. WeakAttribute = class(TUnimplementedAttribute);
  501. UnsafeAttribute = class(TUnimplementedAttribute);
  502. RefAttribute = class(TUnimplementedAttribute);
  503. VolatileAttribute = class(TUnimplementedAttribute);
  504. StoredAttribute = Class(TCustomAttribute)
  505. Private
  506. FFlag : Boolean;
  507. FName : ShortString;
  508. Public
  509. Constructor Create;
  510. Constructor Create(Const aFlag : Boolean);
  511. Constructor Create(Const aName : ShortString);
  512. Property Flag : Boolean Read FFlag;
  513. Property Name : ShortString Read FName;
  514. end;
  515. {*****************************************************************************
  516. TMonitor support
  517. *****************************************************************************}
  518. {$IFDEF SYSTEM_HAS_FEATURE_MONITOR}
  519. Type
  520. PPMonitor = ^PMonitor;
  521. PMonitor = ^TMonitor;
  522. TMonitor = record
  523. Private
  524. class procedure FreeMonitorData(aData : Pointer); static;
  525. public
  526. class procedure SetDefaultSpinCount(const aSpinCount: Longint); static;
  527. class function GetDefaultSpinCount : Longint; static;
  528. class procedure Enter(Const aObject: TObject); overload; static; inline;
  529. class function Enter(Const aObject: TObject; aTimeout: Cardinal): Boolean; overload; static;
  530. class procedure Exit(Const aObject: TObject); overload; static;
  531. class function TryEnter(Const aObject: TObject): Boolean; overload; static;
  532. class function Wait(Const aObject: TObject; aTimeout: Cardinal): Boolean; overload; static;
  533. class function Wait(Const aObject, aLock: TObject; aTimeout: Cardinal): Boolean; overload; static;
  534. class procedure Pulse(Const aObject: TObject); overload; static;
  535. class procedure PulseAll(Const aObject: TObject); overload; static;
  536. class property DefaultSpinCount: Longint read GetDefaultSpinCount write SetDefaultSpinCount;
  537. end;
  538. TMonitorManager = record
  539. Public type
  540. TMonitorSetSpinCountProc = Procedure(const aSpinCount : LongInt);
  541. TMonitorGetSpinCountProc = Function : LongInt;
  542. TMonitorProc = Procedure(const aObject : TObject);
  543. TMonitorFunc = function(const aObject : TObject) : Boolean;
  544. TMonitorTimeoutFunc = function(const aObject : TObject; aTimeout : Cardinal) : Boolean;
  545. TMonitorLockTimeoutFunc = function(const aObject,aLock : TObject; aTimeout : Cardinal) : Boolean;
  546. TMonitorSetObjectDataProc = function (const aObject : TObject; aData,aComparand : Pointer) : Pointer;
  547. TMonitorGetObjectDataFunc = function (const aObject : TObject): Pointer;
  548. TMonitorFreeDataProc = procedure (aData : Pointer);
  549. Public
  550. DoSetDefaultSpinCount : TMonitorSetSpinCountProc;
  551. DoGetDefaultSpinCount : TMonitorGetSpinCountProc;
  552. DoEnter : TMonitorProc;
  553. DoEnterTimeout : TMonitorTimeoutFunc;
  554. DoExit : TMonitorProc;
  555. DoTryEnter : TMonitorFunc;
  556. DoWait : TMonitorTimeoutFunc;
  557. DoWaitLock : TMonitorLockTimeoutFunc;
  558. DoPulse : TMonitorProc;
  559. DoPulseAll : TMonitorProc;
  560. DoFreeMonitorData : TMonitorFreeDataProc;
  561. // Will be set by SetMonitorManager
  562. DoGetMonitorObjectData : TMonitorGetObjectDataFunc;
  563. DoSetMonitorObjectData : TMonitorSetObjectDataProc;
  564. end;
  565. const
  566. INFINITE = CARDINAL($FFFFFFFF);
  567. function MonitorEnter(Const aObject: TObject; aTimeout: Cardinal = INFINITE): Boolean; inline;
  568. function MonitorTryEnter(Const aObject: TObject): Boolean; inline;
  569. procedure MonitorExit(Const aObject: TObject); inline;
  570. function MonitorWait(Const aObject: TObject; aTimeout: Cardinal): Boolean; inline; overload;
  571. function MonitorWait(Const aObject, ALock: TObject; aTimeout: Cardinal): Boolean; inline; overload;
  572. procedure MonitorPulse(Const aObject: TObject); inline;
  573. procedure MonitorPulseAll(Const aObject: TObject); inline;
  574. // Will set Do(S|G)etMonitorObjectData fields on aNew, and returns the old manager
  575. function SetMonitorManager (var aNew : TMonitorManager) : TMonitorManager;
  576. function GetMonitorManager : TMonitorManager;
  577. {$ENDIF}
  578. {$endif FPC_HAS_FEATURE_CLASSES}
  579. {*****************************************************************************
  580. Array of const support
  581. *****************************************************************************}
  582. const
  583. vtInteger = 0;
  584. vtBoolean = 1;
  585. vtChar = 2;
  586. {$ifndef FPUNONE}
  587. vtExtended = 3;
  588. {$endif}
  589. vtString = 4;
  590. vtPointer = 5;
  591. vtPChar = 6;
  592. vtObject = 7;
  593. vtClass = 8;
  594. vtWideChar = 9;
  595. vtPWideChar = 10;
  596. vtAnsiString = 11;
  597. vtCurrency = 12;
  598. vtVariant = 13;
  599. vtInterface = 14;
  600. vtWideString = 15;
  601. vtInt64 = 16;
  602. vtQWord = 17;
  603. vtUnicodeString = 18;
  604. type
  605. PVarRec = ^TVarRec;
  606. TVarRec = record
  607. case VType : sizeint of
  608. {$ifdef ENDIAN_BIG}
  609. vtInteger : ({$IFDEF CPU64}integerdummy1 : Longint;{$ENDIF CPU64}VInteger: Longint);
  610. vtBoolean : ({$IFDEF CPU64}booldummy : Longint;{$ENDIF CPU64}booldummy1,booldummy2,booldummy3: byte; VBoolean: Boolean);
  611. vtChar : ({$IFDEF CPU64}chardummy : Longint;{$ENDIF CPU64}chardummy1,chardummy2,chardummy3: byte; VChar: AnsiChar);
  612. vtWideChar : ({$IFDEF CPU64}widechardummy : Longint;{$ENDIF CPU64}wchardummy1,VWideChar: WideChar);
  613. {$else ENDIAN_BIG}
  614. vtInteger : (VInteger: Longint);
  615. vtBoolean : (VBoolean: Boolean);
  616. vtChar : (VChar: AnsiChar);
  617. vtWideChar : (VWideChar: WideChar);
  618. {$endif ENDIAN_BIG}
  619. {$ifndef FPUNONE}
  620. vtExtended : (VExtended: PExtended);
  621. {$endif}
  622. vtString : (VString: PShortString);
  623. vtPointer : (VPointer: Pointer);
  624. vtPChar : (VPChar: PAnsiChar);
  625. {$ifdef FPC_HAS_FEATURE_CLASSES}
  626. vtObject : (VObject: TObject);
  627. vtClass : (VClass: TClass);
  628. {$endif FPC_HAS_FEATURE_CLASSES}
  629. vtPWideChar : (VPWideChar: PWideChar);
  630. vtAnsiString : (VAnsiString: Pointer);
  631. vtCurrency : (VCurrency: PCurrency);
  632. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  633. vtVariant : (VVariant: PVariant);
  634. {$endif FPC_HAS_FEATURE_VARIANTS}
  635. vtInterface : (VInterface: Pointer);
  636. vtWideString : (VWideString: Pointer);
  637. vtInt64 : (VInt64: PInt64);
  638. vtUnicodeString : (VUnicodeString: Pointer);
  639. vtQWord : (VQWord: PQWord);
  640. end;
  641. var
  642. DispCallByIDProc : codepointer;
  643. {*****************************************************************************
  644. Resourcestring support
  645. *****************************************************************************}
  646. {$ifdef FPC_HAS_FEATURE_RESOURCES}
  647. type
  648. PResourceStringRecord = ^TResourceStringRecord;
  649. TResourceStringRecord = Record
  650. Name : AnsiString;
  651. CurrentValue,
  652. DefaultValue : RTLString;
  653. HashValue : LongWord;
  654. end;
  655. {$endif FPC_HAS_FEATURE_RESOURCES}
  656. {*****************************************************************************
  657. Various Delphi elements
  658. *****************************************************************************}
  659. Type
  660. TPtrWrapper = record
  661. private
  662. FValue: Pointer;
  663. class function GetNilValue: TPtrWrapper; inline; static;
  664. public
  665. constructor Create(AValue: PtrInt); overload;
  666. constructor Create(AValue: Pointer); overload;
  667. function ToPointer: Pointer; inline;
  668. function ToInteger: PtrInt; inline;
  669. class property NilValue: TPtrWrapper read GetNilValue;
  670. class operator =(Left, Right: TPtrWrapper): Boolean; inline;
  671. { ...to allow convenient and direct reading without relying on inline... and convenient writing from SysUtils until TMarshal is moved here... }
  672. property Value: Pointer read FValue write FValue;
  673. end;
  674. TPtrWrapperArray = Array of TPtrWrapper;
  675. { Generic array type.
  676. Slightly Less useful in FPC, since dyn array compatibility is at the element level.
  677. But still useful for generic methods and of course Delphi compatibility}
  678. generic TArray<T> = array of T;
  679. TMarshal = class sealed
  680. public
  681. Type
  682. TUnicodeCharArray = Array of UnicodeChar;
  683. Tint8Array = Array of int8;
  684. Tint16Array = Array of int16;
  685. Tint32Array = Array of int32;
  686. Tint64Array = Array of int64;
  687. TUint8Array = Array of Uint8;
  688. TUint16Array = Array of Uint16;
  689. TUint32Array = Array of Uint32;
  690. TUint64Array = Array of Uint64;
  691. Public
  692. constructor Create;
  693. class function AllocMem(Size: SizeInt): TPtrWrapper; static; inline;
  694. class function ReallocMem(OldPtr: TPtrWrapper; NewSize: SizeInt): TPtrWrapper; static; inline;
  695. class procedure FreeMem(Ptr: TPtrWrapper); static; inline;
  696. class procedure Move(Src, Dest: TPtrWrapper; Count: SizeInt); static; inline;
  697. class function UnsafeAddrOf(var Value): TPtrWrapper; static; inline;
  698. {$IFDEF FPC_HAS_FEATURE_UNICODESTRINGS}
  699. class function AsAnsi(const S: UnicodeString): AnsiString; static; inline;
  700. class function AsAnsi(S: PUnicodeChar): AnsiString; static; inline;
  701. class function InOutString(const S: UnicodeString): PUnicodeChar; static; inline;
  702. class function InString(const S: UnicodeString): PUnicodeChar; static; inline;
  703. class function OutString(const S: UnicodeString): PUnicodeChar; static; inline;
  704. class function AllocStringAsAnsi(const Str: UnicodeString): TPtrWrapper; static; inline;
  705. class function AllocStringAsAnsi(const Str: UnicodeString; CodePage: Word): TPtrWrapper; static; inline;
  706. class function AllocStringAsAnsi(S: PUnicodeChar): TPtrWrapper; static; inline;
  707. class function AllocStringAsAnsi(S: PUnicodeChar; CodePage: Word): TPtrWrapper; static; inline;
  708. class function AllocStringAsUnicode(const Str: UnicodeString): TPtrWrapper; static;
  709. class function AllocStringAsUtf8(const Str: UnicodeString): TPtrWrapper; static; inline;
  710. class function AllocStringAsUtf8(S: PUnicodeChar): TPtrWrapper; static; inline;
  711. { Generalization of all AllocStringAsAnsi* above, public because used in TMarshaller. }
  712. class function AllocStringAsAnsi(S: PUnicodeChar; Len: SizeInt; CodePage: Word): TPtrWrapper; static;
  713. class procedure Copy(const Src: TUnicodeCharArray; StartIndex: SizeInt; Dest: TPtrWrapper; Count: SizeInt); static; inline;
  714. class function FixString(var Str: UnicodeString): TPtrWrapper; static;
  715. class function UnsafeFixString(const Str: UnicodeString): TPtrWrapper; static;
  716. class procedure UnfixString(Ptr: TPtrWrapper); static;
  717. class function ReadStringAsAnsi(Ptr: TPtrWrapper; Len: SizeInt = -1): UnicodeString; static; inline;
  718. class function ReadStringAsAnsi(CodePage: Word; Ptr: TPtrWrapper; Len: SizeInt = -1): UnicodeString; static;
  719. class function ReadStringAsAnsiUpTo(CodePage: Word; Ptr: TPtrWrapper; MaxLen: SizeInt): UnicodeString; static;
  720. class procedure WriteStringAsAnsi(Ptr: TPtrWrapper; const Value: UnicodeString; MaxCharsIncNull: SizeInt); static; inline;
  721. class procedure WriteStringAsAnsi(Ptr: TPtrWrapper; const Value: UnicodeString; MaxCharsIncNull: SizeInt; CodePage: Word); static; inline;
  722. class procedure WriteStringAsAnsi(Ptr: TPtrWrapper; Ofs: SizeInt; const Value: UnicodeString; MaxCharsIncNull: SizeInt); static; inline;
  723. class procedure WriteStringAsAnsi(Ptr: TPtrWrapper; Ofs: SizeInt; const Value: UnicodeString; MaxCharsIncNull: SizeInt; CodePage: Word); static;
  724. class function ReadStringAsUnicode(Ptr: TPtrWrapper; Len: SizeInt = -1): UnicodeString; static;
  725. class function ReadStringAsUnicodeUpTo(Ptr: TPtrWrapper; MaxLen: SizeInt): UnicodeString; static;
  726. class procedure WriteStringAsUnicode(Ptr: TPtrWrapper; const Value: UnicodeString; MaxCharsIncNull: SizeInt); static;
  727. class procedure WriteStringAsUnicode(Ptr: TPtrWrapper; Ofs: SizeInt; const Value: UnicodeString; MaxCharsIncNull: SizeInt); static;
  728. class function ReadStringAsUtf8(Ptr: TPtrWrapper; Len: SizeInt = -1): UnicodeString; static; inline;
  729. class function ReadStringAsUtf8UpTo(Ptr: TPtrWrapper; MaxLen: SizeInt): UnicodeString; static; inline;
  730. class procedure WriteStringAsUtf8(Ptr: TPtrWrapper; const Value: UnicodeString; MaxCharsIncNull: SizeInt); static; inline;
  731. class procedure WriteStringAsUtf8(Ptr: TPtrWrapper; Ofs: SizeInt; const Value: UnicodeString; MaxCharsIncNull: SizeInt); static; inline;
  732. class procedure Copy(Src: TPtrWrapper; var Dest: TUnicodeCharArray; StartIndex: SizeInt; Count: SizeInt); static; inline;
  733. {$ENDIF}
  734. class procedure Copy(const Src: TUint8Array; StartIndex: SizeInt; Dest: TPtrWrapper; Count: SizeInt); static; inline;
  735. class procedure Copy(Src: TPtrWrapper; var Dest: TUint8Array; StartIndex: SizeInt; Count: SizeInt); static; inline;
  736. class procedure Copy(const Src: TInt8Array; StartIndex: SizeInt; Dest: TPtrWrapper; Count: SizeInt); static; inline;
  737. class procedure Copy(Src: TPtrWrapper; var Dest: TInt8Array; StartIndex: SizeInt; Count: SizeInt); static; inline;
  738. class procedure Copy(const Src: TUInt16Array; StartIndex: SizeInt; Dest: TPtrWrapper; Count: SizeInt); static; inline;
  739. class procedure Copy(Src: TPtrWrapper; var Dest: TUInt16Array; StartIndex: SizeInt; Count: SizeInt); static; inline;
  740. class procedure Copy(const Src: TInt16Array; StartIndex: SizeInt; Dest: TPtrWrapper; Count: SizeInt); static; inline;
  741. class procedure Copy(Src: TPtrWrapper; var Dest: TInt16Array; StartIndex: SizeInt; Count: SizeInt); static; inline;
  742. class procedure Copy(const Src: TInt32Array; StartIndex: SizeInt; Dest: TPtrWrapper; Count: SizeInt); static; inline;
  743. class procedure Copy(Src: TPtrWrapper; var Dest: TInt32Array; StartIndex: SizeInt; Count: SizeInt); static; inline;
  744. class procedure Copy(const Src: TInt64Array; StartIndex: SizeInt; Dest: TPtrWrapper; Count: SizeInt); static; inline;
  745. class procedure Copy(Src: TPtrWrapper; var Dest: TInt64Array; StartIndex: SizeInt; Count: SizeInt); static; inline;
  746. class procedure Copy(const Src: TPtrWrapperArray; StartIndex: SizeInt; Dest: TPtrWrapper; Count: SizeInt); static; inline;
  747. class procedure Copy(Src: TPtrWrapper; var Dest: TPtrWrapperArray; StartIndex: SizeInt; Count: SizeInt); static; inline;
  748. generic class function FixArray<T>(const Arr: specialize TArray<T>): TPtrWrapper; static;
  749. generic class procedure UnfixArray<T>(ArrPtr: TPtrWrapper); static;
  750. class function ReadByte(Ptr: TPtrWrapper; Ofs: SizeInt = 0): Byte; static; inline;
  751. class procedure WriteByte(Ptr: TPtrWrapper; Ofs: SizeInt; Value: Byte); static; inline;
  752. class procedure WriteByte(Ptr: TPtrWrapper; Value: Byte); static; inline;
  753. class function ReadInt16(Ptr: TPtrWrapper; Ofs: SizeInt = 0): Int16; static; inline;
  754. class procedure WriteInt16(Ptr: TPtrWrapper; Ofs: SizeInt; Value: Int16); static; inline;
  755. class procedure WriteInt16(Ptr: TPtrWrapper; Value: Int16); static; inline;
  756. class function ReadInt32(Ptr: TPtrWrapper; Ofs: SizeInt = 0): Int32; static; inline;
  757. class procedure WriteInt32(Ptr: TPtrWrapper; Ofs: SizeInt; Value: Int32); static; inline;
  758. class procedure WriteInt32(Ptr: TPtrWrapper; Value: Int32); static; inline;
  759. class function ReadInt64(Ptr: TPtrWrapper; Ofs: SizeInt = 0): Int64; static; inline;
  760. class procedure WriteInt64(Ptr: TPtrWrapper; Ofs: SizeInt; Value: Int64); static; inline;
  761. class procedure WriteInt64(Ptr: TPtrWrapper; Value: Int64); static; inline;
  762. class function ReadPtr(Ptr: TPtrWrapper; Ofs: SizeInt = 0): TPtrWrapper; static; inline;
  763. class procedure WritePtr(Ptr: TPtrWrapper; Ofs: SizeInt; Value: TPtrWrapper); static; inline;
  764. class procedure WritePtr(Ptr, Value: TPtrWrapper); static; inline;
  765. end;
  766. Const
  767. // In Delphi System.SysInit
  768. PtrToNil: Pointer = nil;