objpash.inc 18 KB

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