objpash.inc 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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. {$IFDEF ENABLE_OBJECTHOOK}
  223. public
  224. Type
  225. TObjectHook = procedure(aObject : TObject);
  226. class var
  227. AllocateObjectHook : TObjectHook;
  228. DestroyObjectHook : TObjectHook;
  229. {$ENDIF}
  230. public
  231. { please don't change the order of virtual methods, because
  232. their vmt offsets are used by some assembler code which uses
  233. hard coded addresses (FK) }
  234. constructor Create;
  235. { the virtual procedures must be in THAT order }
  236. destructor Destroy;virtual;
  237. class function newinstance : tobject;virtual;
  238. procedure FreeInstance;virtual;
  239. function SafeCallException(exceptobject : tobject;
  240. exceptaddr : codepointer) : HResult;virtual;
  241. procedure DefaultHandler(var message);virtual;
  242. procedure Free;
  243. class function InitInstance(instance : pointer) : tobject;
  244. procedure CleanupInstance;
  245. class function ClassType : tclass;{$ifdef SYSTEMINLINE}inline;{$endif}
  246. class function ClassInfo : pointer;
  247. class function ClassName : shortstring;
  248. {$IF SIZEOF(CHAR)=2}
  249. class function ClassNameIs(const name : AnsiString) : boolean; overload; {$ifdef SYSTEMINLINE}inline;{$endif}
  250. {$ENDIF}
  251. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  252. class function ClassNameIs(const name : RTLString) : boolean; overload; {$ifdef SYSTEMINLINE}inline;{$endif}
  253. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  254. class function ClassNameIs(const name : ShortString) : boolean;
  255. class function ClassParent : tclass;{$ifdef SYSTEMINLINE}inline;{$endif}
  256. class function InstanceSize : SizeInt;// {$ifdef SYSTEMINLINE}inline;{$endif}
  257. class function InheritsFrom(aclass : tclass) : boolean;
  258. class function StringMessageTable : pstringmessagetable;
  259. class function MethodAddress(const name : shortstring) : codepointer;
  260. class function MethodName(address : codepointer) : shortstring;
  261. class procedure GetLastCastErrorInfo(out aFrom,aTo : shortstring); static;
  262. function FieldAddress(const name : shortstring) : pointer;
  263. { new since Delphi 4 }
  264. procedure AfterConstruction;virtual;
  265. procedure BeforeDestruction;virtual;
  266. { new for gtk, default handler for text based messages }
  267. procedure DefaultHandlerStr(var message);virtual;
  268. { message handling routines }
  269. procedure Dispatch(var message);virtual;
  270. procedure DispatchStr(var message);virtual;
  271. { interface functions }
  272. function GetInterface(const iid : tguid; out obj) : boolean;
  273. function GetInterface(const iidstr : shortstring;out obj) : boolean;
  274. function GetInterfaceByStr(const iidstr : shortstring; out obj) : boolean;
  275. function GetInterfaceWeak(const iid : tguid; out obj) : boolean; // equal to GetInterface but the interface returned is not referenced
  276. class function GetInterfaceEntry(const iid : tguid) : pinterfaceentry;
  277. class function GetInterfaceEntryByStr(const iidstr : shortstring) : pinterfaceentry;
  278. class function GetInterfaceTable : pinterfacetable;
  279. { new since Delphi 2009 }
  280. class function UnitName : RTLString;
  281. class function QualifiedClassName: RTLString;
  282. Procedure DisposeOf; inline;
  283. Procedure CheckDisposed; inline;
  284. function Equals(Obj: TObject) : boolean;virtual;
  285. function GetHashCode: PtrInt;virtual;
  286. function ToString: RTLString; virtual;
  287. end;
  288. IUnknown = interface
  289. ['{00000000-0000-0000-C000-000000000046}']
  290. function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  291. function _AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  292. function _Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  293. end;
  294. IInterface = IUnknown;
  295. {$M+}
  296. IInvokable = interface(IInterface)
  297. end;
  298. {$M-}
  299. { enumerator support }
  300. IEnumerator = interface(IInterface)
  301. function GetCurrent: TObject;
  302. function MoveNext: Boolean;
  303. procedure Reset;
  304. property Current: TObject read GetCurrent;
  305. end;
  306. IEnumerable = interface(IInterface)
  307. function GetEnumerator: IEnumerator;
  308. end;
  309. { for native dispinterface support }
  310. IDispatch = interface(IUnknown)
  311. ['{00020400-0000-0000-C000-000000000046}']
  312. function GetTypeInfoCount(out count : longint) : HResult;stdcall;
  313. function GetTypeInfo(Index,LocaleID : longint;
  314. out TypeInfo): HResult;stdcall;
  315. function GetIDsOfNames(const iid: TGUID; names: Pointer;
  316. NameCount, LocaleID: LongInt; DispIDs: Pointer) : HResult;stdcall;
  317. function Invoke(DispID: LongInt;const iid : TGUID;
  318. LocaleID : longint; Flags: Word;var params;
  319. VarResult,ExcepInfo,ArgErr : pointer) : HResult;stdcall;
  320. end;
  321. { TInterfacedObject }
  322. TInterfacedObject = class(TObject,IUnknown)
  323. protected
  324. FRefCount : longint; { -1 = destroying. }
  325. { implement methods of IUnknown }
  326. function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  327. function _AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  328. function _Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  329. public
  330. destructor Destroy; override;
  331. procedure AfterConstruction;override;
  332. procedure BeforeDestruction;override;
  333. class function NewInstance : TObject;override;
  334. property RefCount : longint read FRefCount;
  335. end;
  336. TInterfacedClass = class of TInterfacedObject;
  337. TAggregatedObject = class(TObject)
  338. private
  339. fcontroller: Pointer;
  340. function GetController: IUnknown;
  341. protected
  342. { implement methods of IUnknown }
  343. function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  344. function _AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  345. function _Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  346. public
  347. constructor Create(const aController: IUnknown);
  348. property Controller : IUnknown read GetController;
  349. end;
  350. TContainedObject = class(TAggregatedObject,IInterface)
  351. protected
  352. function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;virtual; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  353. end;
  354. TNoRefCountObject = class(TObject, IInterface)
  355. protected
  356. function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  357. function _AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  358. function _Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  359. end;
  360. TInterfaceThunk = Class(TInterfacedObject)
  361. Public
  362. Type
  363. TArgData = record
  364. addr : pointer; // Location
  365. info : pointer; // type info (if available: nil for untyped args)
  366. idx : smallint; // param index in rtti
  367. ahigh : sizeint; // For open arrays, high()
  368. end;
  369. PArgData = ^TargData;
  370. TThunkCallBack = Procedure(aInstance: Pointer; aMethod,aCount : Longint; aData : PArgData) of object;
  371. TQueryInterfaceCallback = procedure(iid : tguid;out Result : longint;out aIntf) of object;
  372. Private
  373. FOnQueryInterface : TQueryInterfaceCallback;
  374. FCallback : TThunkCallback;
  375. Protected
  376. function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  377. Procedure Thunk(aMethod: Longint; aCount : Longint; aData : PArgData); virtual;
  378. Public
  379. constructor create(aCallBack : TThunkCallback);
  380. function InterfaceVMTOffset : word; virtual;
  381. property OnQueryInterface : TQueryInterfaceCallback read FOnQueryInterface Write FOnQueryInterface;
  382. end;
  383. TInterfaceThunkClass = class of TInterfaceThunk;
  384. { some pointer definitions }
  385. PUnknown = ^IUnknown;
  386. PPUnknown = ^PUnknown;
  387. PDispatch = ^IDispatch;
  388. PPDispatch = ^PDispatch;
  389. PInterface = PUnknown;
  390. {*****************************************************************************
  391. Exception support
  392. *****************************************************************************}
  393. {$ifdef FPC_USE_PSABIEH}
  394. {$if (defined(CPUARMEL) or defined(CPUARMHF)) and not defined(darwin)}
  395. {$define __ARM_EABI_UNWINDER__}
  396. {$endif}
  397. { needed here for TExceptObject (rest is in psabiehh.inc) }
  398. FPC_Unwind_Reason_Code = longint; {cint}
  399. FPC_Unwind_Action = longint; {cint}
  400. {$ifdef __ARM_EABI_UNWINDER__}
  401. FPC_Unwind_State = longint; {cint}
  402. {$endif}
  403. PFPC_Unwind_Exception = ^FPC_Unwind_Exception;
  404. FPC_Unwind_Exception_Cleanup_Fn =
  405. procedure(reason: FPC_Unwind_Reason_Code; exc: PFPC_Unwind_Exception); cdecl;
  406. FPC_Unwind_Exception = record
  407. { qword instead of array of AnsiChar to ensure proper alignment and
  408. padding, and also easier to compare }
  409. exception_class: qword;
  410. exception_cleanup: FPC_Unwind_Exception_Cleanup_Fn;
  411. {$ifdef __ARM_EABI_UNWINDER__}
  412. { rest of UCB }
  413. // Unwinder cache, private fields for the unwinder's use
  414. unwinder_cache: record
  415. reserved1, // init reserved1 to 0, then don't touch
  416. reserved2,
  417. reserved3,
  418. reserved4,
  419. reserved5: UInt32;
  420. end;
  421. // Propagation barrier cache (valid after phase 1):
  422. barrier_cache: record
  423. sp: PtrUInt;
  424. bitpattern: array[0..4] of UInt32;
  425. end;
  426. // Cleanup cache (preserved over cleanup):
  427. cleanup_cache: record
  428. bitpattern: array[0..3] of UInt32;
  429. end;
  430. // Pr cache (for pr's benefit):
  431. pr_cache: record
  432. fnstart: UInt32; // function start address
  433. ehtp: pointer; // pointer to EHT entry header word
  434. additional: UInt32; // additional data
  435. reserved1: UInt32;
  436. end;
  437. {$else}
  438. private_1: ptruint;
  439. private_2: ptruint;
  440. private_3: ptruint;
  441. private_4: ptruint;
  442. private_5: ptruint;
  443. private_6: ptruint;
  444. {$endif}
  445. end;
  446. {$endif FPC_USE_PSABIEH}
  447. TExceptProc = Procedure (Obj : TObject; Addr : CodePointer; FrameCount:Longint; Frame: PCodePointer);
  448. { Exception object stack }
  449. PExceptObject = ^TExceptObject;
  450. TExceptObject = record
  451. FObject : TObject;
  452. Addr : codepointer;
  453. Next : PExceptObject;
  454. refcount : Longint;
  455. Framecount : Longint;
  456. Frames : PCodePointer;
  457. {$ifdef FPC_USE_WIN32_SEH}
  458. SEHFrame : Pointer;
  459. ExceptRec : Pointer;
  460. ReraiseBuf : jmp_buf;
  461. {$endif FPC_USE_WIN32_SEH}
  462. {$ifdef FPC_USE_PSABIEH}
  463. {$ifndef __ARM_EABI_UNWINDER__}
  464. { cached info from unwind phase for action phase }
  465. handler_switch_value: longint;
  466. language_specific_data: PByte;
  467. landing_pad: PtrUInt;
  468. {$endif __ARM_EABI_UNWINDER__}
  469. { libunwind exception handling data (must be last!) }
  470. unwind_exception: FPC_Unwind_Exception;
  471. {$endif FPC_USE_PSABIEH}
  472. end;
  473. Const
  474. ExceptProc : TExceptProc = Nil;
  475. RaiseProc : TExceptProc = Nil;
  476. RaiseMaxFrameCount : Longint = 16;
  477. Function RaiseList : PExceptObject;
  478. { @abstract(increase exception reference count)
  479. When leaving an except block, the exception object is normally
  480. freed automatically. To avoid this, call this function.
  481. If within the exception object you decide that you don't need
  482. the exception after all, call @link(ReleaseExceptionObject).
  483. Otherwise, if the reference count is > 0, the exception object
  484. goes into your "property" and you need to free it manually.
  485. The effect of this function is countered by re-raising an exception
  486. via "raise;", this zeroes the reference count again.
  487. Calling this method is only valid within an except block.
  488. @return(pointer to the exception object) }
  489. function AcquireExceptionObject: Pointer;
  490. { @abstract(decrease exception reference count)
  491. After calling @link(AcquireExceptionObject) you can call this method
  492. to decrease the exception reference count again.
  493. If the reference count is > 0, the exception object
  494. goes into your "property" and you need to free it manually.
  495. Calling this method is only valid within an except block. }
  496. procedure ReleaseExceptionObject;
  497. const
  498. { for safe as operator support }
  499. IObjectInstance: TGuid = '{D91C9AF4-3C93-420F-A303-BF5BA82BFD23}';
  500. {*****************************************************************************
  501. Attribute support
  502. *****************************************************************************}
  503. Type
  504. {$PUSH}
  505. { disable the warning that the constructor should be public }
  506. {$WARN 3018 OFF}
  507. TCustomAttribute = class(TObject)
  508. private
  509. { if the user wants to use a parameterless constructor they need to
  510. explicitely declare it in their type }
  511. constructor Create;
  512. end;
  513. {$POP}
  514. TUnimplementedAttribute = class(TCustomAttribute)
  515. public
  516. constructor Create; unimplemented;
  517. end;
  518. WeakAttribute = class(TUnimplementedAttribute);
  519. UnsafeAttribute = class(TUnimplementedAttribute);
  520. RefAttribute = class(TUnimplementedAttribute);
  521. VolatileAttribute = class(TUnimplementedAttribute);
  522. StoredAttribute = Class(TCustomAttribute)
  523. Private
  524. FFlag : Boolean;
  525. FName : ShortString;
  526. Public
  527. Constructor Create;
  528. Constructor Create(Const aFlag : Boolean);
  529. Constructor Create(Const aName : ShortString);
  530. Property Flag : Boolean Read FFlag;
  531. Property Name : ShortString Read FName;
  532. end;
  533. {*****************************************************************************
  534. TMonitor support
  535. *****************************************************************************}
  536. {$IFDEF SYSTEM_HAS_FEATURE_MONITOR}
  537. Type
  538. PPMonitor = ^PMonitor;
  539. PMonitor = ^TMonitor;
  540. TMonitor = record
  541. Private
  542. class procedure FreeMonitorData(aData : Pointer); static;
  543. public
  544. class procedure SetDefaultSpinCount(const aSpinCount: Longint); static;
  545. class function GetDefaultSpinCount : Longint; static;
  546. class procedure Enter(Const aObject: TObject); overload; static; inline;
  547. class function Enter(Const aObject: TObject; aTimeout: Cardinal): Boolean; overload; static;
  548. class procedure Exit(Const aObject: TObject); overload; static;
  549. class function TryEnter(Const aObject: TObject): Boolean; overload; static;
  550. class function Wait(Const aObject: TObject; aTimeout: Cardinal): Boolean; overload; static;
  551. class function Wait(Const aObject, aLock: TObject; aTimeout: Cardinal): Boolean; overload; static;
  552. class procedure Pulse(Const aObject: TObject); overload; static;
  553. class procedure PulseAll(Const aObject: TObject); overload; static;
  554. class property DefaultSpinCount: Longint read GetDefaultSpinCount write SetDefaultSpinCount;
  555. end;
  556. TMonitorManager = record
  557. Public type
  558. TMonitorSetSpinCountProc = Procedure(const aSpinCount : LongInt);
  559. TMonitorGetSpinCountProc = Function : LongInt;
  560. TMonitorProc = Procedure(const aObject : TObject);
  561. TMonitorFunc = function(const aObject : TObject) : Boolean;
  562. TMonitorTimeoutFunc = function(const aObject : TObject; aTimeout : Cardinal) : Boolean;
  563. TMonitorLockTimeoutFunc = function(const aObject,aLock : TObject; aTimeout : Cardinal) : Boolean;
  564. TMonitorSetObjectDataProc = function (const aObject : TObject; aData,aComparand : Pointer) : Pointer;
  565. TMonitorGetObjectDataFunc = function (const aObject : TObject): Pointer;
  566. TMonitorFreeDataProc = procedure (aData : Pointer);
  567. Public
  568. DoSetDefaultSpinCount : TMonitorSetSpinCountProc;
  569. DoGetDefaultSpinCount : TMonitorGetSpinCountProc;
  570. DoEnter : TMonitorProc;
  571. DoEnterTimeout : TMonitorTimeoutFunc;
  572. DoExit : TMonitorProc;
  573. DoTryEnter : TMonitorFunc;
  574. DoWait : TMonitorTimeoutFunc;
  575. DoWaitLock : TMonitorLockTimeoutFunc;
  576. DoPulse : TMonitorProc;
  577. DoPulseAll : TMonitorProc;
  578. DoFreeMonitorData : TMonitorFreeDataProc;
  579. // Will be set by SetMonitorManager
  580. DoGetMonitorObjectData : TMonitorGetObjectDataFunc;
  581. DoSetMonitorObjectData : TMonitorSetObjectDataProc;
  582. end;
  583. const
  584. INFINITE = CARDINAL($FFFFFFFF);
  585. function MonitorEnter(Const aObject: TObject; aTimeout: Cardinal = INFINITE): Boolean; inline;
  586. function MonitorTryEnter(Const aObject: TObject): Boolean; inline;
  587. procedure MonitorExit(Const aObject: TObject); inline;
  588. function MonitorWait(Const aObject: TObject; aTimeout: Cardinal): Boolean; inline; overload;
  589. function MonitorWait(Const aObject, ALock: TObject; aTimeout: Cardinal): Boolean; inline; overload;
  590. procedure MonitorPulse(Const aObject: TObject); inline;
  591. procedure MonitorPulseAll(Const aObject: TObject); inline;
  592. // Will set Do(S|G)etMonitorObjectData fields on aNew, and returns the old manager
  593. function SetMonitorManager (var aNew : TMonitorManager) : TMonitorManager;
  594. function GetMonitorManager : TMonitorManager;
  595. {$ENDIF}
  596. {$endif FPC_HAS_FEATURE_CLASSES}
  597. {*****************************************************************************
  598. Array of const support
  599. *****************************************************************************}
  600. const
  601. vtInteger = 0;
  602. vtBoolean = 1;
  603. vtChar = 2;
  604. {$ifndef FPUNONE}
  605. vtExtended = 3;
  606. {$endif}
  607. vtString = 4;
  608. vtPointer = 5;
  609. vtPChar = 6;
  610. vtObject = 7;
  611. vtClass = 8;
  612. vtWideChar = 9;
  613. vtPWideChar = 10;
  614. vtAnsiString = 11;
  615. vtCurrency = 12;
  616. vtVariant = 13;
  617. vtInterface = 14;
  618. vtWideString = 15;
  619. vtInt64 = 16;
  620. vtQWord = 17;
  621. vtUnicodeString = 18;
  622. type
  623. PVarRec = ^TVarRec;
  624. TVarRec = record
  625. case VType : sizeint of
  626. {$ifdef ENDIAN_BIG}
  627. vtInteger : ({$IFDEF CPU64}integerdummy1 : Longint;{$ENDIF CPU64}VInteger: Longint);
  628. vtBoolean : ({$IFDEF CPU64}booldummy : Longint;{$ENDIF CPU64}booldummy1,booldummy2,booldummy3: byte; VBoolean: Boolean);
  629. vtChar : ({$IFDEF CPU64}chardummy : Longint;{$ENDIF CPU64}chardummy1,chardummy2,chardummy3: byte; VChar: AnsiChar);
  630. vtWideChar : ({$IFDEF CPU64}widechardummy : Longint;{$ENDIF CPU64}wchardummy1,VWideChar: WideChar);
  631. {$else ENDIAN_BIG}
  632. vtInteger : (VInteger: Longint);
  633. vtBoolean : (VBoolean: Boolean);
  634. vtChar : (VChar: AnsiChar);
  635. vtWideChar : (VWideChar: WideChar);
  636. {$endif ENDIAN_BIG}
  637. {$ifndef FPUNONE}
  638. vtExtended : (VExtended: PExtended);
  639. {$endif}
  640. vtString : (VString: PShortString);
  641. vtPointer : (VPointer: Pointer);
  642. vtPChar : (VPChar: PAnsiChar);
  643. {$ifdef FPC_HAS_FEATURE_CLASSES}
  644. vtObject : (VObject: TObject);
  645. vtClass : (VClass: TClass);
  646. {$endif FPC_HAS_FEATURE_CLASSES}
  647. vtPWideChar : (VPWideChar: PWideChar);
  648. vtAnsiString : (VAnsiString: Pointer);
  649. vtCurrency : (VCurrency: PCurrency);
  650. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  651. vtVariant : (VVariant: PVariant);
  652. {$endif FPC_HAS_FEATURE_VARIANTS}
  653. vtInterface : (VInterface: Pointer);
  654. vtWideString : (VWideString: Pointer);
  655. vtInt64 : (VInt64: PInt64);
  656. vtUnicodeString : (VUnicodeString: Pointer);
  657. vtQWord : (VQWord: PQWord);
  658. end;
  659. var
  660. DispCallByIDProc : codepointer;
  661. {*****************************************************************************
  662. Resourcestring support
  663. *****************************************************************************}
  664. {$ifdef FPC_HAS_FEATURE_RESOURCES}
  665. type
  666. PResourceStringRecord = ^TResourceStringRecord;
  667. TResourceStringRecord = Record
  668. Name : AnsiString;
  669. CurrentValue,
  670. DefaultValue : RTLString;
  671. HashValue : LongWord;
  672. end;
  673. {$endif FPC_HAS_FEATURE_RESOURCES}
  674. {*****************************************************************************
  675. Various Delphi elements
  676. *****************************************************************************}
  677. Type
  678. TPtrWrapper = record
  679. private
  680. FValue: Pointer;
  681. class function GetNilValue: TPtrWrapper; inline; static;
  682. public
  683. constructor Create(AValue: PtrInt); overload;
  684. constructor Create(AValue: Pointer); overload;
  685. function ToPointer: Pointer; inline;
  686. function ToInteger: PtrInt; inline;
  687. class property NilValue: TPtrWrapper read GetNilValue;
  688. class operator =(Left, Right: TPtrWrapper): Boolean; inline;
  689. { ...to allow convenient and direct reading without relying on inline... and convenient writing from SysUtils until TMarshal is moved here... }
  690. property Value: Pointer read FValue write FValue;
  691. end;
  692. TPtrWrapperArray = Array of TPtrWrapper;
  693. { Generic array type.
  694. Slightly Less useful in FPC, since dyn array compatibility is at the element level.
  695. But still useful for generic methods and of course Delphi compatibility}
  696. generic TArray<T> = array of T;
  697. TMarshal = class sealed
  698. public
  699. Type
  700. TUnicodeCharArray = Array of UnicodeChar;
  701. Tint8Array = Array of int8;
  702. Tint16Array = Array of int16;
  703. Tint32Array = Array of int32;
  704. Tint64Array = Array of int64;
  705. TUint8Array = Array of Uint8;
  706. TUint16Array = Array of Uint16;
  707. TUint32Array = Array of Uint32;
  708. TUint64Array = Array of Uint64;
  709. Public
  710. constructor Create;
  711. class function AllocMem(Size: SizeInt): TPtrWrapper; static; inline;
  712. class function ReallocMem(OldPtr: TPtrWrapper; NewSize: SizeInt): TPtrWrapper; static; inline;
  713. class procedure FreeMem(Ptr: TPtrWrapper); static; inline;
  714. class procedure Move(Src, Dest: TPtrWrapper; Count: SizeInt); static; inline;
  715. class function UnsafeAddrOf(var Value): TPtrWrapper; static; inline;
  716. {$IFDEF FPC_HAS_FEATURE_UNICODESTRINGS}
  717. class function AsAnsi(const S: UnicodeString): AnsiString; static; inline;
  718. class function AsAnsi(S: PUnicodeChar): AnsiString; static; inline;
  719. class function InOutString(const S: UnicodeString): PUnicodeChar; static; inline;
  720. class function InString(const S: UnicodeString): PUnicodeChar; static; inline;
  721. class function OutString(const S: UnicodeString): PUnicodeChar; static; inline;
  722. class function AllocStringAsAnsi(const Str: UnicodeString): TPtrWrapper; static; inline;
  723. class function AllocStringAsAnsi(const Str: UnicodeString; CodePage: Word): TPtrWrapper; static; inline;
  724. class function AllocStringAsAnsi(S: PUnicodeChar): TPtrWrapper; static; inline;
  725. class function AllocStringAsAnsi(S: PUnicodeChar; CodePage: Word): TPtrWrapper; static; inline;
  726. class function AllocStringAsUnicode(const Str: UnicodeString): TPtrWrapper; static;
  727. class function AllocStringAsUtf8(const Str: UnicodeString): TPtrWrapper; static; inline;
  728. class function AllocStringAsUtf8(S: PUnicodeChar): TPtrWrapper; static; inline;
  729. { Generalization of all AllocStringAsAnsi* above, public because used in TMarshaller. }
  730. class function AllocStringAsAnsi(S: PUnicodeChar; Len: SizeInt; CodePage: Word): TPtrWrapper; static;
  731. class procedure Copy(const Src: TUnicodeCharArray; StartIndex: SizeInt; Dest: TPtrWrapper; Count: SizeInt); static; inline;
  732. class function FixString(var Str: UnicodeString): TPtrWrapper; static;
  733. class function UnsafeFixString(const Str: UnicodeString): TPtrWrapper; static;
  734. class procedure UnfixString(Ptr: TPtrWrapper); static;
  735. class function ReadStringAsAnsi(Ptr: TPtrWrapper; Len: SizeInt = -1): UnicodeString; static; inline;
  736. class function ReadStringAsAnsi(CodePage: Word; Ptr: TPtrWrapper; Len: SizeInt = -1): UnicodeString; static;
  737. class function ReadStringAsAnsiUpTo(CodePage: Word; Ptr: TPtrWrapper; MaxLen: SizeInt): UnicodeString; static;
  738. class procedure WriteStringAsAnsi(Ptr: TPtrWrapper; const Value: UnicodeString; MaxCharsIncNull: SizeInt); static; inline;
  739. class procedure WriteStringAsAnsi(Ptr: TPtrWrapper; const Value: UnicodeString; MaxCharsIncNull: SizeInt; CodePage: Word); static; inline;
  740. class procedure WriteStringAsAnsi(Ptr: TPtrWrapper; Ofs: SizeInt; const Value: UnicodeString; MaxCharsIncNull: SizeInt); static; inline;
  741. class procedure WriteStringAsAnsi(Ptr: TPtrWrapper; Ofs: SizeInt; const Value: UnicodeString; MaxCharsIncNull: SizeInt; CodePage: Word); static;
  742. class function ReadStringAsUnicode(Ptr: TPtrWrapper; Len: SizeInt = -1): UnicodeString; static;
  743. class function ReadStringAsUnicodeUpTo(Ptr: TPtrWrapper; MaxLen: SizeInt): UnicodeString; static;
  744. class procedure WriteStringAsUnicode(Ptr: TPtrWrapper; const Value: UnicodeString; MaxCharsIncNull: SizeInt); static;
  745. class procedure WriteStringAsUnicode(Ptr: TPtrWrapper; Ofs: SizeInt; const Value: UnicodeString; MaxCharsIncNull: SizeInt); static;
  746. class function ReadStringAsUtf8(Ptr: TPtrWrapper; Len: SizeInt = -1): UnicodeString; static; inline;
  747. class function ReadStringAsUtf8UpTo(Ptr: TPtrWrapper; MaxLen: SizeInt): UnicodeString; static; inline;
  748. class procedure WriteStringAsUtf8(Ptr: TPtrWrapper; const Value: UnicodeString; MaxCharsIncNull: SizeInt); static; inline;
  749. class procedure WriteStringAsUtf8(Ptr: TPtrWrapper; Ofs: SizeInt; const Value: UnicodeString; MaxCharsIncNull: SizeInt); static; inline;
  750. class procedure Copy(Src: TPtrWrapper; var Dest: TUnicodeCharArray; StartIndex: SizeInt; Count: SizeInt); static; inline;
  751. {$ENDIF}
  752. class procedure Copy(const Src: TUint8Array; StartIndex: SizeInt; Dest: TPtrWrapper; Count: SizeInt); static; inline;
  753. class procedure Copy(Src: TPtrWrapper; var Dest: TUint8Array; StartIndex: SizeInt; Count: SizeInt); static; inline;
  754. class procedure Copy(const Src: TInt8Array; StartIndex: SizeInt; Dest: TPtrWrapper; Count: SizeInt); static; inline;
  755. class procedure Copy(Src: TPtrWrapper; var Dest: TInt8Array; StartIndex: SizeInt; Count: SizeInt); static; inline;
  756. class procedure Copy(const Src: TUInt16Array; StartIndex: SizeInt; Dest: TPtrWrapper; Count: SizeInt); static; inline;
  757. class procedure Copy(Src: TPtrWrapper; var Dest: TUInt16Array; StartIndex: SizeInt; Count: SizeInt); static; inline;
  758. class procedure Copy(const Src: TInt16Array; StartIndex: SizeInt; Dest: TPtrWrapper; Count: SizeInt); static; inline;
  759. class procedure Copy(Src: TPtrWrapper; var Dest: TInt16Array; StartIndex: SizeInt; Count: SizeInt); static; inline;
  760. class procedure Copy(const Src: TInt32Array; StartIndex: SizeInt; Dest: TPtrWrapper; Count: SizeInt); static; inline;
  761. class procedure Copy(Src: TPtrWrapper; var Dest: TInt32Array; StartIndex: SizeInt; Count: SizeInt); static; inline;
  762. class procedure Copy(const Src: TInt64Array; StartIndex: SizeInt; Dest: TPtrWrapper; Count: SizeInt); static; inline;
  763. class procedure Copy(Src: TPtrWrapper; var Dest: TInt64Array; StartIndex: SizeInt; Count: SizeInt); static; inline;
  764. class procedure Copy(const Src: TPtrWrapperArray; StartIndex: SizeInt; Dest: TPtrWrapper; Count: SizeInt); static; inline;
  765. class procedure Copy(Src: TPtrWrapper; var Dest: TPtrWrapperArray; StartIndex: SizeInt; Count: SizeInt); static; inline;
  766. generic class function FixArray<T>(const Arr: specialize TArray<T>): TPtrWrapper; static;
  767. generic class procedure UnfixArray<T>(ArrPtr: TPtrWrapper); static;
  768. class function ReadByte(Ptr: TPtrWrapper; Ofs: SizeInt = 0): Byte; static; inline;
  769. class procedure WriteByte(Ptr: TPtrWrapper; Ofs: SizeInt; Value: Byte); static; inline;
  770. class procedure WriteByte(Ptr: TPtrWrapper; Value: Byte); static; inline;
  771. class function ReadInt16(Ptr: TPtrWrapper; Ofs: SizeInt = 0): Int16; static; inline;
  772. class procedure WriteInt16(Ptr: TPtrWrapper; Ofs: SizeInt; Value: Int16); static; inline;
  773. class procedure WriteInt16(Ptr: TPtrWrapper; Value: Int16); static; inline;
  774. class function ReadInt32(Ptr: TPtrWrapper; Ofs: SizeInt = 0): Int32; static; inline;
  775. class procedure WriteInt32(Ptr: TPtrWrapper; Ofs: SizeInt; Value: Int32); static; inline;
  776. class procedure WriteInt32(Ptr: TPtrWrapper; Value: Int32); static; inline;
  777. class function ReadInt64(Ptr: TPtrWrapper; Ofs: SizeInt = 0): Int64; static; inline;
  778. class procedure WriteInt64(Ptr: TPtrWrapper; Ofs: SizeInt; Value: Int64); static; inline;
  779. class procedure WriteInt64(Ptr: TPtrWrapper; Value: Int64); static; inline;
  780. class function ReadPtr(Ptr: TPtrWrapper; Ofs: SizeInt = 0): TPtrWrapper; static; inline;
  781. class procedure WritePtr(Ptr: TPtrWrapper; Ofs: SizeInt; Value: TPtrWrapper); static; inline;
  782. class procedure WritePtr(Ptr, Value: TPtrWrapper); static; inline;
  783. end;
  784. Const
  785. // In Delphi System.SysInit
  786. PtrToNil: Pointer = nil;