objpash.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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(ptruint)*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 : ptruint;
  146. IIDStr : pshortstring; { never nil. Com: upper(GuidToString(IID^)) }
  147. case boolean of
  148. {$ifdef ENDIAN_BIG}
  149. true : ({$IFDEF CPU64}__pad: longint;{$ENDIF}IType : tinterfaceentrytype);
  150. {$else ENDIAN_BIG}
  151. true : (IType : tinterfaceentrytype);
  152. {$endif ENDIAN_BIG}
  153. false : (__pad_dummy : pointer);
  154. end;
  155. tinterfacetable = record
  156. EntryCount : ptruint;
  157. Entries : array[0..0] of tinterfaceentry;
  158. end;
  159. TMethod = record
  160. Code : CodePointer;
  161. Data : Pointer;
  162. end;
  163. TObject = class
  164. public
  165. { please don't change the order of virtual methods, because
  166. their vmt offsets are used by some assembler code which uses
  167. hard coded addresses (FK) }
  168. constructor Create;
  169. { the virtual procedures must be in THAT order }
  170. destructor Destroy;virtual;
  171. class function newinstance : tobject;virtual;
  172. procedure FreeInstance;virtual;
  173. function SafeCallException(exceptobject : tobject;
  174. exceptaddr : codepointer) : HResult;virtual;
  175. procedure DefaultHandler(var message);virtual;
  176. procedure Free;
  177. class function InitInstance(instance : pointer) : tobject; {$ifdef SYSTEMINLINE} inline; {$endif}
  178. procedure CleanupInstance;
  179. class function ClassType : tclass;{$ifdef SYSTEMINLINE}inline;{$endif}
  180. class function ClassInfo : pointer;
  181. class function ClassName : shortstring;
  182. class function ClassNameIs(const name : string) : boolean;
  183. class function ClassParent : tclass;{$ifdef SYSTEMINLINE}inline;{$endif}
  184. class function InstanceSize : SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  185. class function InheritsFrom(aclass : tclass) : boolean;
  186. class function StringMessageTable : pstringmessagetable;
  187. class function MethodAddress(const name : shortstring) : codepointer;
  188. class function MethodName(address : codepointer) : shortstring;
  189. function FieldAddress(const name : shortstring) : pointer;
  190. { new since Delphi 4 }
  191. procedure AfterConstruction;virtual;
  192. procedure BeforeDestruction;virtual;
  193. { new for gtk, default handler for text based messages }
  194. procedure DefaultHandlerStr(var message);virtual;
  195. { message handling routines }
  196. procedure Dispatch(var message);virtual;
  197. procedure DispatchStr(var message);virtual;
  198. { interface functions }
  199. function GetInterface(const iid : tguid; out obj) : boolean;
  200. function GetInterface(const iidstr : shortstring;out obj) : boolean;
  201. function GetInterfaceByStr(const iidstr : shortstring; out obj) : boolean;
  202. function GetInterfaceWeak(const iid : tguid; out obj) : boolean; // equal to GetInterface but the interface returned is not referenced
  203. class function GetInterfaceEntry(const iid : tguid) : pinterfaceentry;
  204. class function GetInterfaceEntryByStr(const iidstr : shortstring) : pinterfaceentry;
  205. class function GetInterfaceTable : pinterfacetable;
  206. { new since Delphi 2009 }
  207. class function UnitName : ansistring;
  208. function Equals(Obj: TObject) : boolean;virtual;
  209. function GetHashCode: PtrInt;virtual;
  210. function ToString: ansistring;virtual;
  211. end;
  212. IUnknown = interface
  213. ['{00000000-0000-0000-C000-000000000046}']
  214. function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  215. function _AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  216. function _Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  217. end;
  218. IInterface = IUnknown;
  219. {$M+}
  220. IInvokable = interface(IInterface)
  221. end;
  222. {$M-}
  223. { enumerator support }
  224. IEnumerator = interface(IInterface)
  225. function GetCurrent: TObject;
  226. function MoveNext: Boolean;
  227. procedure Reset;
  228. property Current: TObject read GetCurrent;
  229. end;
  230. IEnumerable = interface(IInterface)
  231. function GetEnumerator: IEnumerator;
  232. end;
  233. { for native dispinterface support }
  234. IDispatch = interface(IUnknown)
  235. ['{00020400-0000-0000-C000-000000000046}']
  236. function GetTypeInfoCount(out count : longint) : HResult;stdcall;
  237. function GetTypeInfo(Index,LocaleID : longint;
  238. out TypeInfo): HResult;stdcall;
  239. function GetIDsOfNames(const iid: TGUID; names: Pointer;
  240. NameCount, LocaleID: LongInt; DispIDs: Pointer) : HResult;stdcall;
  241. function Invoke(DispID: LongInt;const iid : TGUID;
  242. LocaleID : longint; Flags: Word;var params;
  243. VarResult,ExcepInfo,ArgErr : pointer) : HResult;stdcall;
  244. end;
  245. TInterfacedObject = class(TObject,IUnknown)
  246. protected
  247. frefcount : longint;
  248. { implement methods of IUnknown }
  249. function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  250. function _AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  251. function _Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  252. public
  253. procedure AfterConstruction;override;
  254. procedure BeforeDestruction;override;
  255. class function NewInstance : TObject;override;
  256. property RefCount : longint read frefcount;
  257. end;
  258. TInterfacedClass = class of TInterfacedObject;
  259. TAggregatedObject = class(TObject)
  260. private
  261. fcontroller: Pointer;
  262. function GetController: IUnknown;
  263. protected
  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. constructor Create(const aController: IUnknown);
  270. property Controller : IUnknown read GetController;
  271. end;
  272. TContainedObject = class(TAggregatedObject,IInterface)
  273. protected
  274. function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;virtual; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  275. end;
  276. { some pointer definitions }
  277. PUnknown = ^IUnknown;
  278. PPUnknown = ^PUnknown;
  279. PDispatch = ^IDispatch;
  280. PPDispatch = ^PDispatch;
  281. PInterface = PUnknown;
  282. TExceptProc = Procedure (Obj : TObject; Addr : CodePointer; FrameCount:Longint; Frame: PCodePointer);
  283. { Exception object stack }
  284. PExceptObject = ^TExceptObject;
  285. TExceptObject = record
  286. FObject : TObject;
  287. Addr : codepointer;
  288. Next : PExceptObject;
  289. refcount : Longint;
  290. Framecount : Longint;
  291. Frames : PCodePointer;
  292. end;
  293. Const
  294. ExceptProc : TExceptProc = Nil;
  295. RaiseProc : TExceptProc = Nil;
  296. RaiseMaxFrameCount : Longint = 16;
  297. Function RaiseList : PExceptObject;
  298. { @abstract(increase exception reference count)
  299. When leaving an except block, the exception object is normally
  300. freed automatically. To avoid this, call this function.
  301. If within the exception object you decide that you don't need
  302. the exception after all, call @link(ReleaseExceptionObject).
  303. Otherwise, if the reference count is > 0, the exception object
  304. goes into your "property" and you need to free it manually.
  305. The effect of this function is countered by re-raising an exception
  306. via "raise;", this zeroes the reference count again.
  307. Calling this method is only valid within an except block.
  308. @return(pointer to the exception object) }
  309. function AcquireExceptionObject: Pointer;
  310. { @abstract(decrease exception reference count)
  311. After calling @link(AcquireExceptionObject) you can call this method
  312. to decrease the exception reference count again.
  313. If the reference count is > 0, the exception object
  314. goes into your "property" and you need to free it manually.
  315. Calling this method is only valid within an except block. }
  316. procedure ReleaseExceptionObject;
  317. {*****************************************************************************
  318. Array of const support
  319. *****************************************************************************}
  320. const
  321. vtInteger = 0;
  322. vtBoolean = 1;
  323. vtChar = 2;
  324. {$ifndef FPUNONE}
  325. vtExtended = 3;
  326. {$endif}
  327. vtString = 4;
  328. vtPointer = 5;
  329. vtPChar = 6;
  330. vtObject = 7;
  331. vtClass = 8;
  332. vtWideChar = 9;
  333. vtPWideChar = 10;
  334. vtAnsiString = 11;
  335. vtCurrency = 12;
  336. vtVariant = 13;
  337. vtInterface = 14;
  338. vtWideString = 15;
  339. vtInt64 = 16;
  340. vtQWord = 17;
  341. vtUnicodeString = 18;
  342. type
  343. PVarRec = ^TVarRec;
  344. TVarRec = record
  345. case VType : sizeint of
  346. {$ifdef ENDIAN_BIG}
  347. vtInteger : ({$IFDEF CPU64}integerdummy1 : Longint;{$ENDIF CPU64}VInteger: Longint);
  348. vtBoolean : ({$IFDEF CPU64}booldummy : Longint;{$ENDIF CPU64}booldummy1,booldummy2,booldummy3: byte; VBoolean: Boolean);
  349. vtChar : ({$IFDEF CPU64}chardummy : Longint;{$ENDIF CPU64}chardummy1,chardummy2,chardummy3: byte; VChar: Char);
  350. vtWideChar : ({$IFDEF CPU64}widechardummy : Longint;{$ENDIF CPU64}wchardummy1,VWideChar: WideChar);
  351. {$else ENDIAN_BIG}
  352. vtInteger : (VInteger: Longint);
  353. vtBoolean : (VBoolean: Boolean);
  354. vtChar : (VChar: Char);
  355. vtWideChar : (VWideChar: WideChar);
  356. {$endif ENDIAN_BIG}
  357. {$ifndef FPUNONE}
  358. vtExtended : (VExtended: PExtended);
  359. {$endif}
  360. vtString : (VString: PShortString);
  361. vtPointer : (VPointer: Pointer);
  362. vtPChar : (VPChar: PChar);
  363. vtObject : (VObject: TObject);
  364. vtClass : (VClass: TClass);
  365. vtPWideChar : (VPWideChar: PWideChar);
  366. vtAnsiString : (VAnsiString: Pointer);
  367. vtCurrency : (VCurrency: PCurrency);
  368. vtVariant : (VVariant: PVariant);
  369. vtInterface : (VInterface: Pointer);
  370. vtWideString : (VWideString: Pointer);
  371. vtInt64 : (VInt64: PInt64);
  372. vtUnicodeString : (VUnicodeString: Pointer);
  373. vtQWord : (VQWord: PQWord);
  374. end;
  375. var
  376. DispCallByIDProc : codepointer;
  377. const
  378. { for safe as operator support }
  379. IObjectInstance: TGuid = '{D91C9AF4-3C93-420F-A303-BF5BA82BFD23}';