objpash.inc 38 KB

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