objpash.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. const
  24. vmtInstanceSize = 0;
  25. vmtParent = sizeof(SizeInt)*2;
  26. { These were negative value's, but are now positive, else classes
  27. couldn't be used with shared linking which copies only all data from
  28. the .global directive and not the data before the directive (PFV) }
  29. vmtClassName = vmtParent+sizeof(pointer);
  30. vmtDynamicTable = vmtParent+sizeof(pointer)*2;
  31. vmtMethodTable = vmtParent+sizeof(pointer)*3;
  32. vmtFieldTable = vmtParent+sizeof(pointer)*4;
  33. vmtTypeInfo = vmtParent+sizeof(pointer)*5;
  34. vmtInitTable = vmtParent+sizeof(pointer)*6;
  35. vmtAutoTable = vmtParent+sizeof(pointer)*7;
  36. vmtIntfTable = vmtParent+sizeof(pointer)*8;
  37. vmtMsgStrPtr = vmtParent+sizeof(pointer)*9;
  38. { methods }
  39. vmtMethodStart = vmtParent+sizeof(pointer)*10;
  40. vmtDestroy = vmtMethodStart;
  41. vmtNewInstance = vmtMethodStart+sizeof(codepointer);
  42. vmtFreeInstance = vmtMethodStart+sizeof(codepointer)*2;
  43. vmtSafeCallException = vmtMethodStart+sizeof(codepointer)*3;
  44. vmtDefaultHandler = vmtMethodStart+sizeof(codepointer)*4;
  45. vmtAfterConstruction = vmtMethodStart+sizeof(codepointer)*5;
  46. vmtBeforeDestruction = vmtMethodStart+sizeof(codepointer)*6;
  47. vmtDefaultHandlerStr = vmtMethodStart+sizeof(codepointer)*7;
  48. vmtDispatch = vmtMethodStart+sizeof(codepointer)*8;
  49. vmtDispatchStr = vmtMethodStart+sizeof(codepointer)*9;
  50. vmtEquals = vmtMethodStart+sizeof(codepointer)*10;
  51. vmtGetHashCode = vmtMethodStart+sizeof(codepointer)*11;
  52. vmtToString = vmtMethodStart+sizeof(codepointer)*12;
  53. { IInterface }
  54. S_OK = 0;
  55. S_FALSE = 1;
  56. E_NOINTERFACE = hresult($80004002);
  57. E_UNEXPECTED = hresult($8000FFFF);
  58. E_NOTIMPL = hresult($80004001);
  59. type
  60. TextFile = Text;
  61. { now the let's declare the base classes for the class object
  62. model. The compiler expects TObject and IUnknown to be defined
  63. first as forward classes }
  64. TObject = class;
  65. IUnknown = interface;
  66. TClass = class of tobject;
  67. PClass = ^tclass;
  68. { to access the message table from outside }
  69. TMsgStrTable = record
  70. name : pshortstring;
  71. method : codepointer;
  72. end;
  73. PMsgStrTable = ^TMsgStrTable;
  74. TStringMessageTable = record
  75. count : longint;
  76. msgstrtable : array[0..0] of tmsgstrtable;
  77. end;
  78. pstringmessagetable = ^tstringmessagetable;
  79. pinterfacetable = ^tinterfacetable;
  80. PVmt = ^TVmt;
  81. PPVmt = ^PVmt;
  82. TVmt = record
  83. vInstanceSize: SizeInt;
  84. vInstanceSize2: SizeInt;
  85. vParentRef: {$ifdef VER3_0}PVmt{$else}PPVmt{$endif};
  86. vClassName: PShortString;
  87. vDynamicTable: Pointer;
  88. vMethodTable: Pointer;
  89. vFieldTable: Pointer;
  90. vTypeInfo: Pointer;
  91. vInitTable: Pointer;
  92. vAutoTable: Pointer;
  93. vIntfTable: PInterfaceTable;
  94. vMsgStrPtr: pstringmessagetable;
  95. vDestroy: CodePointer;
  96. vNewInstance: CodePointer;
  97. vFreeInstance: CodePointer;
  98. vSafeCallException: CodePointer;
  99. vDefaultHandler: CodePointer;
  100. vAfterConstruction: CodePointer;
  101. vBeforeDestruction: CodePointer;
  102. vDefaultHandlerStr: CodePointer;
  103. vDispatch: CodePointer;
  104. vDispatchStr: CodePointer;
  105. vEquals: CodePointer;
  106. vGetHashCode: CodePointer;
  107. vToString: CodePointer;
  108. private
  109. function GetvParent: PVmt; inline;
  110. public
  111. property vParent: PVmt read GetvParent;
  112. end;
  113. PGuid = ^TGuid;
  114. TGuid = packed record
  115. case integer of
  116. 1 : (
  117. Data1 : DWord;
  118. Data2 : word;
  119. Data3 : word;
  120. Data4 : array[0..7] of byte;
  121. );
  122. 2 : (
  123. D1 : DWord;
  124. D2 : word;
  125. D3 : word;
  126. D4 : array[0..7] of byte;
  127. );
  128. 3 : ( { uuid fields according to RFC4122 }
  129. time_low : dword; // The low field of the timestamp
  130. time_mid : word; // The middle field of the timestamp
  131. time_hi_and_version : word; // The high field of the timestamp multiplexed with the version number
  132. clock_seq_hi_and_reserved : byte; // The high field of the clock sequence multiplexed with the variant
  133. clock_seq_low : byte; // The low field of the clock sequence
  134. node : array[0..5] of byte; // The spatially unique node identifier
  135. );
  136. end;
  137. // This enumerate is found both in the rtl and compiler. Do not change the order of the fields.
  138. tinterfaceentrytype = (etStandard,
  139. etVirtualMethodResult,
  140. etStaticMethodResult,
  141. etFieldValue,
  142. etVirtualMethodClass,
  143. etStaticMethodClass,
  144. etFieldValueClass
  145. );
  146. pinterfaceentry = ^tinterfaceentry;
  147. tinterfaceentry = record
  148. private
  149. function GetIID: pguid; inline;
  150. function GetIIDStr: pshortstring; inline;
  151. public
  152. property IID: pguid read GetIID;
  153. property IIDStr: pshortstring read GetIIDStr;
  154. public
  155. IIDRef : {$IFNDEF VER3_0}^{$ENDIF}pguid; { if assigned(IID) then Com else Corba}
  156. VTable : Pointer;
  157. case integer of
  158. 1 : (
  159. IOffset: sizeuint;
  160. );
  161. 2 : (
  162. IOffsetAsCodePtr: CodePointer;
  163. IIDStrRef : {$IFNDEF VER3_0}^{$ENDIF}pshortstring; { never nil. Com: upper(GuidToString(IID^)) }
  164. IType : tinterfaceentrytype;
  165. );
  166. end;
  167. tinterfacetable = record
  168. EntryCount : sizeuint;
  169. Entries : array[0..0] of tinterfaceentry;
  170. end;
  171. PMethod = ^TMethod;
  172. TMethod = record
  173. Code : CodePointer;
  174. Data : Pointer;
  175. end;
  176. TObject = class
  177. public
  178. { please don't change the order of virtual methods, because
  179. their vmt offsets are used by some assembler code which uses
  180. hard coded addresses (FK) }
  181. constructor Create;
  182. { the virtual procedures must be in THAT order }
  183. destructor Destroy;virtual;
  184. class function newinstance : tobject;virtual;
  185. procedure FreeInstance;virtual;
  186. function SafeCallException(exceptobject : tobject;
  187. exceptaddr : codepointer) : HResult;virtual;
  188. procedure DefaultHandler(var message);virtual;
  189. procedure Free;
  190. class function InitInstance(instance : pointer) : tobject; {$ifdef SYSTEMINLINE} inline; {$endif}
  191. procedure CleanupInstance;
  192. class function ClassType : tclass;{$ifdef SYSTEMINLINE}inline;{$endif}
  193. class function ClassInfo : pointer;
  194. class function ClassName : shortstring;
  195. class function ClassNameIs(const name : string) : boolean;
  196. class function ClassParent : tclass;{$ifdef SYSTEMINLINE}inline;{$endif}
  197. class function InstanceSize : SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  198. class function InheritsFrom(aclass : tclass) : boolean;
  199. class function StringMessageTable : pstringmessagetable;
  200. class function MethodAddress(const name : shortstring) : codepointer;
  201. class function MethodName(address : codepointer) : shortstring;
  202. function FieldAddress(const name : shortstring) : pointer;
  203. { new since Delphi 4 }
  204. procedure AfterConstruction;virtual;
  205. procedure BeforeDestruction;virtual;
  206. { new for gtk, default handler for text based messages }
  207. procedure DefaultHandlerStr(var message);virtual;
  208. { message handling routines }
  209. procedure Dispatch(var message);virtual;
  210. procedure DispatchStr(var message);virtual;
  211. { interface functions }
  212. function GetInterface(const iid : tguid; out obj) : boolean;
  213. function GetInterface(const iidstr : shortstring;out obj) : boolean;
  214. function GetInterfaceByStr(const iidstr : shortstring; out obj) : boolean;
  215. function GetInterfaceWeak(const iid : tguid; out obj) : boolean; // equal to GetInterface but the interface returned is not referenced
  216. class function GetInterfaceEntry(const iid : tguid) : pinterfaceentry;
  217. class function GetInterfaceEntryByStr(const iidstr : shortstring) : pinterfaceentry;
  218. class function GetInterfaceTable : pinterfacetable;
  219. { new since Delphi 2009 }
  220. class function UnitName : {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}ansistring{$else FPC_HAS_FEATURE_ANSISTRINGS}shortstring{$endif FPC_HAS_FEATURE_ANSISTRINGS};
  221. class function QualifiedClassName: {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}ansistring{$else FPC_HAS_FEATURE_ANSISTRINGS}shortstring{$endif FPC_HAS_FEATURE_ANSISTRINGS};
  222. function Equals(Obj: TObject) : boolean;virtual;
  223. function GetHashCode: PtrInt;virtual;
  224. function ToString: {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}ansistring{$else FPC_HAS_FEATURE_ANSISTRINGS}shortstring{$endif FPC_HAS_FEATURE_ANSISTRINGS};virtual;
  225. end;
  226. IUnknown = interface
  227. ['{00000000-0000-0000-C000-000000000046}']
  228. function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  229. function _AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  230. function _Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  231. end;
  232. IInterface = IUnknown;
  233. {$M+}
  234. IInvokable = interface(IInterface)
  235. end;
  236. {$M-}
  237. { enumerator support }
  238. IEnumerator = interface(IInterface)
  239. function GetCurrent: TObject;
  240. function MoveNext: Boolean;
  241. procedure Reset;
  242. property Current: TObject read GetCurrent;
  243. end;
  244. IEnumerable = interface(IInterface)
  245. function GetEnumerator: IEnumerator;
  246. end;
  247. { for native dispinterface support }
  248. IDispatch = interface(IUnknown)
  249. ['{00020400-0000-0000-C000-000000000046}']
  250. function GetTypeInfoCount(out count : longint) : HResult;stdcall;
  251. function GetTypeInfo(Index,LocaleID : longint;
  252. out TypeInfo): HResult;stdcall;
  253. function GetIDsOfNames(const iid: TGUID; names: Pointer;
  254. NameCount, LocaleID: LongInt; DispIDs: Pointer) : HResult;stdcall;
  255. function Invoke(DispID: LongInt;const iid : TGUID;
  256. LocaleID : longint; Flags: Word;var params;
  257. VarResult,ExcepInfo,ArgErr : pointer) : HResult;stdcall;
  258. end;
  259. { TInterfacedObject }
  260. TInterfacedObject = class(TObject,IUnknown)
  261. protected
  262. frefcount : longint;
  263. FDestroyCount : longint;
  264. { implement methods of IUnknown }
  265. function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  266. function _AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  267. function _Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  268. public
  269. destructor destroy; override;
  270. procedure AfterConstruction;override;
  271. procedure BeforeDestruction;override;
  272. class function NewInstance : TObject;override;
  273. property RefCount : longint read frefcount;
  274. end;
  275. TInterfacedClass = class of TInterfacedObject;
  276. TAggregatedObject = class(TObject)
  277. private
  278. fcontroller: Pointer;
  279. function GetController: IUnknown;
  280. protected
  281. { implement methods of IUnknown }
  282. function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  283. function _AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  284. function _Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  285. public
  286. constructor Create(const aController: IUnknown);
  287. property Controller : IUnknown read GetController;
  288. end;
  289. TContainedObject = class(TAggregatedObject,IInterface)
  290. protected
  291. function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;virtual; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  292. end;
  293. { some pointer definitions }
  294. PUnknown = ^IUnknown;
  295. PPUnknown = ^PUnknown;
  296. PDispatch = ^IDispatch;
  297. PPDispatch = ^PDispatch;
  298. PInterface = PUnknown;
  299. TExceptProc = Procedure (Obj : TObject; Addr : CodePointer; FrameCount:Longint; Frame: PCodePointer);
  300. { Exception object stack }
  301. PExceptObject = ^TExceptObject;
  302. TExceptObject = record
  303. FObject : TObject;
  304. Addr : codepointer;
  305. Next : PExceptObject;
  306. refcount : Longint;
  307. Framecount : Longint;
  308. Frames : PCodePointer;
  309. {$ifdef FPC_USE_WIN32_SEH}
  310. SEHFrame : Pointer;
  311. ExceptRec : Pointer;
  312. ReraiseBuf : jmp_buf;
  313. {$endif FPC_USE_WIN32_SEH}
  314. end;
  315. Const
  316. ExceptProc : TExceptProc = Nil;
  317. RaiseProc : TExceptProc = Nil;
  318. RaiseMaxFrameCount : Longint = 16;
  319. Function RaiseList : PExceptObject;
  320. { @abstract(increase exception reference count)
  321. When leaving an except block, the exception object is normally
  322. freed automatically. To avoid this, call this function.
  323. If within the exception object you decide that you don't need
  324. the exception after all, call @link(ReleaseExceptionObject).
  325. Otherwise, if the reference count is > 0, the exception object
  326. goes into your "property" and you need to free it manually.
  327. The effect of this function is countered by re-raising an exception
  328. via "raise;", this zeroes the reference count again.
  329. Calling this method is only valid within an except block.
  330. @return(pointer to the exception object) }
  331. function AcquireExceptionObject: Pointer;
  332. { @abstract(decrease exception reference count)
  333. After calling @link(AcquireExceptionObject) you can call this method
  334. to decrease the exception reference count again.
  335. If the reference count is > 0, the exception object
  336. goes into your "property" and you need to free it manually.
  337. Calling this method is only valid within an except block. }
  338. procedure ReleaseExceptionObject;
  339. {*****************************************************************************
  340. Array of const support
  341. *****************************************************************************}
  342. const
  343. vtInteger = 0;
  344. vtBoolean = 1;
  345. vtChar = 2;
  346. {$ifndef FPUNONE}
  347. vtExtended = 3;
  348. {$endif}
  349. vtString = 4;
  350. vtPointer = 5;
  351. vtPChar = 6;
  352. vtObject = 7;
  353. vtClass = 8;
  354. vtWideChar = 9;
  355. vtPWideChar = 10;
  356. vtAnsiString = 11;
  357. vtCurrency = 12;
  358. vtVariant = 13;
  359. vtInterface = 14;
  360. vtWideString = 15;
  361. vtInt64 = 16;
  362. vtQWord = 17;
  363. vtUnicodeString = 18;
  364. type
  365. PVarRec = ^TVarRec;
  366. TVarRec = record
  367. case VType : sizeint of
  368. {$ifdef ENDIAN_BIG}
  369. vtInteger : ({$IFDEF CPU64}integerdummy1 : Longint;{$ENDIF CPU64}VInteger: Longint);
  370. vtBoolean : ({$IFDEF CPU64}booldummy : Longint;{$ENDIF CPU64}booldummy1,booldummy2,booldummy3: byte; VBoolean: Boolean);
  371. vtChar : ({$IFDEF CPU64}chardummy : Longint;{$ENDIF CPU64}chardummy1,chardummy2,chardummy3: byte; VChar: Char);
  372. vtWideChar : ({$IFDEF CPU64}widechardummy : Longint;{$ENDIF CPU64}wchardummy1,VWideChar: WideChar);
  373. {$else ENDIAN_BIG}
  374. vtInteger : (VInteger: Longint);
  375. vtBoolean : (VBoolean: Boolean);
  376. vtChar : (VChar: Char);
  377. vtWideChar : (VWideChar: WideChar);
  378. {$endif ENDIAN_BIG}
  379. {$ifndef FPUNONE}
  380. vtExtended : (VExtended: PExtended);
  381. {$endif}
  382. vtString : (VString: PShortString);
  383. vtPointer : (VPointer: Pointer);
  384. vtPChar : (VPChar: PAnsiChar);
  385. vtObject : (VObject: TObject);
  386. vtClass : (VClass: TClass);
  387. vtPWideChar : (VPWideChar: PWideChar);
  388. vtAnsiString : (VAnsiString: Pointer);
  389. vtCurrency : (VCurrency: PCurrency);
  390. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  391. vtVariant : (VVariant: PVariant);
  392. {$endif FPC_HAS_FEATURE_VARIANTS}
  393. vtInterface : (VInterface: Pointer);
  394. vtWideString : (VWideString: Pointer);
  395. vtInt64 : (VInt64: PInt64);
  396. vtUnicodeString : (VUnicodeString: Pointer);
  397. vtQWord : (VQWord: PQWord);
  398. end;
  399. var
  400. DispCallByIDProc : codepointer;
  401. const
  402. { for safe as operator support }
  403. IObjectInstance: TGuid = '{D91C9AF4-3C93-420F-A303-BF5BA82BFD23}';
  404. {*****************************************************************************
  405. Resourcestring support
  406. *****************************************************************************}
  407. {$ifdef FPC_HAS_FEATURE_RESOURCES}
  408. type
  409. PResourceStringRecord = ^TResourceStringRecord;
  410. TResourceStringRecord = Record
  411. Name,
  412. CurrentValue,
  413. DefaultValue : AnsiString;
  414. HashValue : LongWord;
  415. end;
  416. {$endif FPC_HAS_FEATURE_RESOURCES}