objpash.inc 16 KB

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