objpash.inc 14 KB

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