objpas.inc 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by the Free Pascal development team
  4. This unit makes Free Pascal as much as possible Delphi compatible
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  12. procedure DoDispCallByIDError(res : Pointer; const disp : IDispatch;desc : PDispDesc; params : Pointer);
  13. begin
  14. handleerroraddrframeind(RuntimeErrorExitCodes[reVarDispatch],
  15. get_pc_addr,get_frame);
  16. end;
  17. procedure fpc_dispatch_by_id(Result: Pointer; const Dispatch: pointer;
  18. DispDesc: Pointer; Params: Pointer); compilerproc;
  19. type
  20. TDispProc = procedure(res : Pointer; const disp : IDispatch;desc : PDispDesc; params : Pointer);
  21. begin
  22. TDispProc(DispCallByIDProc)(Result,IDispatch(Dispatch),DispDesc,Params);
  23. end;
  24. {$endif FPC_HAS_FEATURE_VARIANTS}
  25. {****************************************************************************
  26. Internal Routines called from the Compiler
  27. ****************************************************************************}
  28. { the reverse order of the parameters make code generation easier }
  29. function fpc_do_is(aclass : tclass;aobject : tobject) : boolean;[public,alias: 'FPC_DO_IS']; compilerproc;
  30. begin
  31. fpc_do_is:=assigned(aobject) and assigned(aclass) and
  32. aobject.inheritsfrom(aclass);
  33. end;
  34. { the reverse order of the parameters make code generation easier }
  35. function fpc_do_as(aclass : tclass;aobject : tobject): tobject;[public,alias: 'FPC_DO_AS']; compilerproc;
  36. begin
  37. if assigned(aobject) and not(aobject.inheritsfrom(aclass)) then
  38. handleerroraddrframeInd(219,get_pc_addr,get_frame);
  39. result := aobject;
  40. end;
  41. { interface helpers }
  42. procedure fpc_intf_decr_ref(var i: pointer);[public,alias: 'FPC_INTF_DECR_REF']; compilerproc;
  43. begin
  44. if assigned(i) then
  45. begin
  46. IUnknown(i)._Release;
  47. i:=nil;
  48. end;
  49. end;
  50. { local declaration for intf_decr_ref for local access }
  51. procedure intf_decr_ref(var i: pointer); [external name 'FPC_INTF_DECR_REF'];
  52. procedure fpc_intf_incr_ref(i: pointer);[public,alias: 'FPC_INTF_INCR_REF']; compilerproc;
  53. begin
  54. if assigned(i) then
  55. IUnknown(i)._AddRef;
  56. end;
  57. { local declaration of intf_incr_ref for local access }
  58. procedure intf_incr_ref(i: pointer); [external name 'FPC_INTF_INCR_REF'];
  59. procedure fpc_intf_assign(var D: pointer; const S: pointer);[public,alias: 'FPC_INTF_ASSIGN']; compilerproc;
  60. begin
  61. if assigned(S) then
  62. IUnknown(S)._AddRef;
  63. if assigned(D) then
  64. IUnknown(D)._Release;
  65. D:=S;
  66. end;
  67. procedure fpc_intf_assign(var D: pointer; const s: pointer); [external name 'FPC_INTF_ASSIGN'];
  68. {procedure fpc_intf_assign_by_iid(var D: pointer; const S: pointer; const iid: TGUID);[public,alias: 'FPC_INTF_ASSIGN2']; compilerproc;
  69. var
  70. tmp : pointer;
  71. begin
  72. if assigned(S) then
  73. begin
  74. tmp:=nil;
  75. if IUnknown(S).QueryInterface(iid,tmp)<>S_OK then
  76. handleerror(219);
  77. if assigned(D) then
  78. IUnknown(D)._Release;
  79. D:=tmp;
  80. end
  81. else
  82. begin
  83. if assigned(D) then
  84. IUnknown(D)._Release;
  85. D:=nil;
  86. end;
  87. end;}
  88. function fpc_intf_is(const S: pointer; const iid: TGUID): Boolean;[public,alias: 'FPC_INTF_IS']; compilerproc;
  89. var
  90. tmpi: pointer;
  91. begin
  92. tmpi:=nil;
  93. fpc_intf_is:=Assigned(S) and (IUnknown(S).QueryInterface(iid,tmpi)=S_OK);
  94. if Assigned(tmpi) then
  95. IUnknown(tmpi)._Release;
  96. end;
  97. function fpc_intf_is_class(const S: pointer; const aclass: tclass): Boolean;[public,alias: 'FPC_INTF_IS_CLASS']; compilerproc;
  98. var
  99. tmpo: tobject;
  100. begin
  101. fpc_intf_is_class:=Assigned(S) and (IUnknown(S).QueryInterface(IObjectInstance,tmpo)=S_OK) and tmpo.InheritsFrom(aclass);
  102. end;
  103. function fpc_class_is_intf(const S: pointer; const iid: TGUID): Boolean;[public,alias: 'FPC_CLASS_IS_INTF']; compilerproc;
  104. var
  105. tmpi: pointer;
  106. tmpi2: pointer; // weak!
  107. begin
  108. tmpi:=nil;
  109. tmpi2:=nil;
  110. fpc_class_is_intf:=Assigned(S) and ((TObject(S).GetInterfaceWeak(IUnknown,tmpi2) and (IUnknown(tmpi2).QueryInterface(IID,tmpi)=S_OK)) or
  111. TObject(S).GetInterface(IID,tmpi));
  112. if Assigned(tmpi) then
  113. IUnknown(tmpi)._Release;
  114. end;
  115. function fpc_class_is_corbaintf(const S: pointer; const iid: Shortstring): Boolean;[public,alias: 'FPC_CLASS_IS_CORBAINTF']; compilerproc;
  116. begin
  117. fpc_class_is_corbaintf:=Assigned(S) and Assigned(TObject(S).GetInterfaceEntryByStr(iid));
  118. end;
  119. function fpc_intf_cast(const S: pointer; const iid: TGUID): IInterface;[public,alias: 'FPC_INTF_CAST']; compilerproc;
  120. var
  121. tmpi: pointer;
  122. begin
  123. tmpi:=nil;
  124. if Assigned(S) and (IUnknown(S).QueryInterface(iid,tmpi)=S_OK) then
  125. pointer(fpc_intf_cast):=tmpi
  126. else
  127. fpc_intf_cast:= nil;
  128. end;
  129. function fpc_intf_cast_class(const S: pointer; const aclass: tclass): pointer;[public,alias: 'FPC_INTF_CAST_CLASS']; compilerproc;
  130. var
  131. tmpo: tobject;
  132. begin
  133. if Assigned(S) and (IUnknown(S).QueryInterface(IObjectInstance,tmpo)=S_OK) and tmpo.InheritsFrom(aclass) then
  134. fpc_intf_cast_class:=tmpo
  135. else
  136. fpc_intf_cast_class:=nil;
  137. end;
  138. function fpc_class_cast_intf(const S: pointer; const iid: TGUID): IInterface;[public,alias: 'FPC_CLASS_CAST_INTF']; compilerproc;
  139. var
  140. tmpi: pointer;
  141. tmpi2: pointer; // weak!
  142. begin
  143. tmpi:=nil;
  144. tmpi2:=nil;
  145. if Assigned(S) and ((TObject(S).GetInterfaceWeak(IUnknown,tmpi2) and (IUnknown(tmpi2).QueryInterface(IID,tmpi)=S_OK)) or
  146. TObject(S).GetInterface(IID,tmpi)) then
  147. begin
  148. // decrease reference count
  149. fpc_class_cast_intf:=nil;
  150. pointer(fpc_class_cast_intf):=tmpi
  151. end
  152. else
  153. fpc_class_cast_intf:=nil;
  154. end;
  155. function fpc_class_cast_corbaintf(const S: pointer; const iid: Shortstring): Pointer;[public,alias: 'FPC_CLASS_CAST_CORBAINTF']; compilerproc;
  156. var
  157. tmpi: pointer;
  158. begin
  159. if Assigned(S) and TObject(S).GetInterface(iid,tmpi) then
  160. fpc_class_cast_corbaintf:=tmpi
  161. else
  162. fpc_class_cast_corbaintf:=nil;
  163. end;
  164. function fpc_intf_as(const S: pointer; const iid: TGUID): IInterface;[public,alias: 'FPC_INTF_AS']; compilerproc;
  165. var
  166. tmpi: pointer; // _AddRef before _Release
  167. begin
  168. if assigned(S) then
  169. begin
  170. tmpi:=nil;
  171. if IUnknown(S).QueryInterface(iid,tmpi)<>S_OK then
  172. handleerror(219);
  173. // decrease reference count
  174. fpc_intf_as:=nil;
  175. pointer(fpc_intf_as):=tmpi;
  176. end
  177. else
  178. fpc_intf_as:=nil;
  179. end;
  180. function fpc_intf_as_class(const S: pointer; const aclass: tclass): pointer;[public,alias: 'FPC_INTF_AS_CLASS']; compilerproc;
  181. var
  182. tmpo: tobject;
  183. begin
  184. if assigned(S) then
  185. begin
  186. if not ((IUnknown(S).QueryInterface(IObjectInstance,tmpo)=S_OK) and tmpo.inheritsfrom(aclass)) then
  187. handleerror(219);
  188. fpc_intf_as_class:=tmpo;
  189. end
  190. else
  191. fpc_intf_as_class:=nil;
  192. end;
  193. function fpc_class_as_intf(const S: pointer; const iid: TGUID): IInterface;[public,alias: 'FPC_CLASS_AS_INTF']; compilerproc;
  194. var
  195. tmpi: pointer; // _AddRef before _Release
  196. tmpi2: pointer; // weak!
  197. begin
  198. if assigned(S) then
  199. begin
  200. tmpi:=nil;
  201. tmpi2:=nil;
  202. if not ((TObject(S).GetInterfaceWeak(IUnknown,tmpi2) and (IUnknown(tmpi2).QueryInterface(IID,tmpi)=S_OK)) or TObject(S).GetInterface(IID,tmpi)) then
  203. handleerror(219);
  204. // decrease reference count
  205. fpc_class_as_intf:=nil;
  206. pointer(fpc_class_as_intf):=tmpi;
  207. end
  208. else
  209. fpc_class_as_intf:=nil;
  210. end;
  211. function fpc_class_as_corbaintf(const S: pointer; const iid: Shortstring): Pointer;[public,alias: 'FPC_CLASS_AS_CORBAINTF']; compilerproc;
  212. var
  213. tmpi: pointer; // _AddRef before _Release
  214. begin
  215. if assigned(S) then
  216. begin
  217. tmpi:=nil;
  218. if not TObject(S).GetInterface(iid,tmpi) then
  219. handleerror(219);
  220. fpc_class_as_corbaintf:=tmpi;
  221. end
  222. else
  223. fpc_class_as_corbaintf:=nil;
  224. end;
  225. {****************************************************************************
  226. TVMT
  227. ****************************************************************************}
  228. function TVmt.GetvParent: PVmt;
  229. begin
  230. {$ifdef VER3_0}
  231. GetvParent:=vParentRef;
  232. {$else VER3_0}
  233. if Assigned(vParentRef) then
  234. GetvParent:=vParentRef^
  235. else
  236. GetvParent:=Nil;
  237. {$endif VER3_0}
  238. end;
  239. {****************************************************************************
  240. TINTERFACEENTRY
  241. ****************************************************************************}
  242. function tinterfaceentry.GetIID: pguid;
  243. begin
  244. {$ifdef VER3_0}
  245. GetIID:=IIDRef;
  246. {$else VER3_0}
  247. if Assigned(IIDRef) then
  248. GetIID:=IIDRef^
  249. else
  250. GetIID:=Nil;
  251. {$endif VER3_0}
  252. end;
  253. function tinterfaceentry.GetIIDStr: pshortstring;
  254. begin
  255. {$ifdef VER3_0}
  256. GetIIDStr:=IIDStrRef;
  257. {$else VER3_0}
  258. if Assigned(IIDStrRef) then
  259. GetIIDStr:=IIDStrRef^
  260. else
  261. GetIIDStr:=Nil;
  262. {$endif VER3_0}
  263. end;
  264. {****************************************************************************
  265. TOBJECT
  266. ****************************************************************************}
  267. constructor TObject.Create;
  268. begin
  269. end;
  270. destructor TObject.Destroy;
  271. begin
  272. end;
  273. procedure TObject.Free;
  274. begin
  275. // the call via self avoids a warning
  276. if self<>nil then
  277. self.destroy;
  278. end;
  279. class function TObject.InstanceSize : SizeInt;
  280. begin
  281. InstanceSize := PVmt(Self)^.vInstanceSize;
  282. end;
  283. {$ifdef VER3_0}
  284. var
  285. emptyintf: ptruint; public name 'FPC_EMPTYINTF';
  286. {$endif VER3_0}
  287. procedure InitInterfacePointers(objclass: tclass;instance : pointer);
  288. var
  289. ovmt: PVmt;
  290. i: longint;
  291. intftable: pinterfacetable;
  292. Res: pinterfaceentry;
  293. begin
  294. ovmt := PVmt(objclass);
  295. while assigned(ovmt) and {$ifdef VER3_0}(ovmt^.vIntfTable <> @emptyintf){$else}assigned(ovmt^.vIntfTable){$endif} do
  296. begin
  297. intftable:=ovmt^.vIntfTable;
  298. {$ifdef VER3_0}
  299. if assigned(intftable) then
  300. {$endif VER3_0}
  301. begin
  302. i:=intftable^.EntryCount;
  303. Res:=@intftable^.Entries[0];
  304. while i>0 do begin
  305. if Res^.IType = etStandard then
  306. ppointer(@(pbyte(instance)[Res^.IOffset]))^:=
  307. pointer(Res^.VTable);
  308. inc(Res);
  309. dec(i);
  310. end;
  311. end;
  312. ovmt:=ovmt^.vParent;
  313. end;
  314. end;
  315. class function TObject.InitInstance(instance : pointer) : tobject; {$ifdef SYSTEMINLINE} inline; {$ENDIF}
  316. {$ifndef VER3_0}
  317. var
  318. vmt : PVmt;
  319. inittable : pointer;
  320. mopinittable : PRTTIRecordOpOffsetTable;
  321. i : longint;
  322. {$endif VER3_0}
  323. begin
  324. { the size is saved at offset 0 }
  325. fillchar(instance^, InstanceSize, 0);
  326. { insert VMT pointer into the new created memory area }
  327. { (in class methods self contains the VMT!) }
  328. ppointer(instance)^:=pointer(self);
  329. if {$ifdef VER3_0}PVmt(self)^.vIntfTable <> @emptyintf{$else}assigned(PVmt(self)^.vIntfTable){$endif} then
  330. InitInterfacePointers(self,instance);
  331. {$ifndef VER3_0}
  332. { for management operators like initialize call int_initialize }
  333. vmt := PVmt(self);
  334. if assigned(vmt) then
  335. begin
  336. inittable:=vmt^.vInitTable;
  337. if assigned(inittable) then
  338. begin
  339. mopinittable:=RTTIRecordMopInitTable(inittable);
  340. if assigned(mopinittable) then
  341. begin
  342. {$push}
  343. { ensure that no range check errors pop up with the [0..0] array }
  344. {$R-}
  345. for i:=0 to mopinittable^.Count-1 do
  346. TRTTIRecVarOp(mopinittable^.Entries[i].ManagmentOperator)(PByte(Instance)+mopinittable^.Entries[i].FieldOffset);
  347. {$pop}
  348. end;
  349. end;
  350. end;
  351. {$endif VER3_0}
  352. InitInstance:=TObject(Instance);
  353. end;
  354. class function TObject.ClassParent : tclass;
  355. begin
  356. { type of self is class of tobject => it points to the vmt }
  357. { the parent vmt is saved at offset vmtParent }
  358. classparent:=tclass(PVmt(Self)^.vParent);
  359. end;
  360. class function TObject.NewInstance : tobject;
  361. var
  362. p : pointer;
  363. begin
  364. getmem(p, InstanceSize);
  365. if p <> nil then
  366. InitInstance(p);
  367. NewInstance:=TObject(p);
  368. end;
  369. procedure TObject.FreeInstance;
  370. begin
  371. CleanupInstance;
  372. FreeMem(Pointer(Self));
  373. end;
  374. class function TObject.ClassType : TClass;
  375. begin
  376. ClassType:=TClass(Pointer(Self))
  377. end;
  378. type
  379. tmethodnamerec = packed record
  380. name : pshortstring;
  381. addr : codepointer;
  382. end;
  383. tmethodnametable = packed record
  384. count : dword;
  385. entries : packed array[0..0] of tmethodnamerec;
  386. end;
  387. pmethodnametable = ^tmethodnametable;
  388. class function TObject.MethodAddress(const name : shortstring) : codepointer;
  389. var
  390. methodtable : pmethodnametable;
  391. i : dword;
  392. ovmt : PVmt;
  393. begin
  394. ovmt:=PVmt(self);
  395. while assigned(ovmt) do
  396. begin
  397. methodtable:=pmethodnametable(ovmt^.vMethodTable);
  398. if assigned(methodtable) then
  399. begin
  400. for i:=0 to methodtable^.count-1 do
  401. if ShortCompareText(methodtable^.entries[i].name^, name)=0 then
  402. begin
  403. MethodAddress:=methodtable^.entries[i].addr;
  404. exit;
  405. end;
  406. end;
  407. ovmt := ovmt^.vParent;
  408. end;
  409. MethodAddress:=nil;
  410. end;
  411. class function TObject.MethodName(address : codepointer) : shortstring;
  412. var
  413. methodtable : pmethodnametable;
  414. i : dword;
  415. ovmt : PVmt;
  416. begin
  417. ovmt:=PVmt(self);
  418. while assigned(ovmt) do
  419. begin
  420. methodtable:=pmethodnametable(ovmt^.vMethodTable);
  421. if assigned(methodtable) then
  422. begin
  423. for i:=0 to methodtable^.count-1 do
  424. if methodtable^.entries[i].addr=address then
  425. begin
  426. MethodName:=methodtable^.entries[i].name^;
  427. exit;
  428. end;
  429. end;
  430. ovmt := ovmt^.vParent;
  431. end;
  432. MethodName:='';
  433. end;
  434. function TObject.FieldAddress(const name : shortstring) : pointer;
  435. type
  436. PFieldInfo = ^TFieldInfo;
  437. TFieldInfo =
  438. {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
  439. packed
  440. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  441. record
  442. FieldOffset: SizeUInt;
  443. ClassTypeIndex: Word;
  444. Name: ShortString;
  445. end;
  446. PFieldTable = ^TFieldTable;
  447. TFieldTable =
  448. {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
  449. packed
  450. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  451. record
  452. FieldCount: Word;
  453. ClassTable: Pointer;
  454. { should be array[Word] of TFieldInfo; but
  455. Elements have variant size! force at least proper alignment }
  456. Fields: array[0..0] of TFieldInfo
  457. end;
  458. var
  459. ovmt: PVmt;
  460. FieldTable: PFieldTable;
  461. FieldInfo: PFieldInfo;
  462. i: longint;
  463. begin
  464. if Length(name) > 0 then
  465. begin
  466. ovmt := PVmt(ClassType);
  467. while ovmt <> nil do
  468. begin
  469. FieldTable := PFieldTable(ovmt^.vFieldTable);
  470. if FieldTable <> nil then
  471. begin
  472. FieldInfo := @FieldTable^.Fields[0];
  473. for i := 0 to FieldTable^.FieldCount - 1 do
  474. begin
  475. if ShortCompareText(FieldInfo^.Name, name) = 0 then
  476. begin
  477. fieldaddress := Pointer(Self) + FieldInfo^.FieldOffset;
  478. exit;
  479. end;
  480. FieldInfo := PFieldInfo(PByte(@FieldInfo^.Name) + 1 + Length(FieldInfo^.Name));
  481. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  482. { align to largest field of TFieldInfo }
  483. FieldInfo := Align(FieldInfo, SizeOf(PtrUInt));
  484. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  485. end;
  486. end;
  487. { Try again with the parent class type }
  488. ovmt:=ovmt^.vParent;
  489. end;
  490. end;
  491. fieldaddress:=nil;
  492. end;
  493. function TObject.SafeCallException(exceptobject : tobject;
  494. exceptaddr : codepointer) : HResult;
  495. begin
  496. safecallexception:=E_UNEXPECTED;
  497. end;
  498. class function TObject.ClassInfo : pointer;
  499. begin
  500. ClassInfo := PVmt(Self)^.vTypeInfo;
  501. end;
  502. class function TObject.ClassName : ShortString;
  503. begin
  504. ClassName := PVmt(Self)^.vClassName^;
  505. end;
  506. class function TObject.ClassNameIs(const name : string) : boolean;
  507. begin
  508. // call to ClassName inlined here, this eliminates stack and string copying.
  509. ClassNameIs:=ShortCompareText(PVmt(Self)^.vClassName^, name) = 0;
  510. end;
  511. class function TObject.InheritsFrom(aclass : TClass) : Boolean;
  512. var
  513. vmt: PVmt;
  514. begin
  515. if assigned(aclass) then
  516. begin
  517. vmt:=PVmt(self);
  518. while assigned(vmt) and (vmt <> PVmt(aclass)) do
  519. vmt := vmt^.vParent;
  520. InheritsFrom := (vmt = PVmt(aclass));
  521. end
  522. else
  523. inheritsFrom := False;
  524. end;
  525. class function TObject.stringmessagetable : pstringmessagetable;
  526. begin
  527. stringmessagetable:=PVmt(Self)^.vMsgStrPtr;
  528. end;
  529. type
  530. tmessagehandler = procedure(var msg) of object;
  531. procedure TObject.Dispatch(var message);
  532. type
  533. {$PUSH}
  534. {$PACKRECORDS NORMAL}
  535. PMsgIntTable = ^TMsgIntTable;
  536. TMsgIntTable = record
  537. index : dword;
  538. method : codepointer;
  539. end;
  540. PMsgInt = ^TMsgInt;
  541. TMsgInt = record
  542. count : longint;
  543. msgs : array[0..0] of TMsgIntTable;
  544. end;
  545. {$POP}
  546. var
  547. index : dword;
  548. count,i : longint;
  549. msgtable : PMsgIntTable;
  550. p : PMsgInt;
  551. ovmt : PVmt;
  552. msghandler : tmessagehandler;
  553. begin
  554. index:=dword(message);
  555. ovmt := PVmt(ClassType);
  556. while assigned(ovmt) do
  557. begin
  558. // See if we have messages at all in this class.
  559. p:=PMsgInt(ovmt^.vDynamicTable);
  560. If Assigned(p) then
  561. begin
  562. msgtable:=@p^.msgs;
  563. count:=p^.count;
  564. end
  565. else
  566. Count:=0;
  567. { later, we can implement a binary search here }
  568. for i:=0 to count-1 do
  569. begin
  570. if index=msgtable[i].index then
  571. begin
  572. TMethod(msghandler).Code:=msgtable[i].method;
  573. TMethod(msghandler).Data:=self;
  574. msghandler(message);
  575. exit;
  576. end;
  577. end;
  578. ovmt:=ovmt^.vParent;
  579. end;
  580. DefaultHandler(message);
  581. end;
  582. procedure TObject.DispatchStr(var message);
  583. var
  584. name : shortstring;
  585. count,i : longint;
  586. msgstrtable : pmsgstrtable;
  587. p: pstringmessagetable;
  588. ovmt : PVmt;
  589. msghandler : tmessagehandler;
  590. begin
  591. name:=pshortstring(@message)^;
  592. ovmt:=PVmt(ClassType);
  593. while assigned(ovmt) do
  594. begin
  595. p := ovmt^.vMsgStrPtr;
  596. if (P<>Nil) and (p^.count<>0) then
  597. begin
  598. count:=p^.count;
  599. msgstrtable:=@p^.msgstrtable;
  600. end
  601. else
  602. Count:=0;
  603. { later, we can implement a binary search here }
  604. for i:=0 to count-1 do
  605. begin
  606. if name=msgstrtable[i].name^ then
  607. begin
  608. TMethod(msghandler).Code:=msgstrtable[i].method;
  609. TMethod(msghandler).Data:=self;
  610. msghandler(message);
  611. exit;
  612. end;
  613. end;
  614. ovmt:=ovmt^.vParent;
  615. end;
  616. DefaultHandlerStr(message);
  617. end;
  618. procedure TObject.DefaultHandler(var message);
  619. begin
  620. end;
  621. procedure TObject.DefaultHandlerStr(var message);
  622. begin
  623. end;
  624. procedure TObject.CleanupInstance;
  625. var
  626. vmt : PVmt;
  627. temp : pointer;
  628. begin
  629. vmt := PVmt(ClassType);
  630. while vmt<>nil do
  631. begin
  632. Temp:= vmt^.vInitTable;
  633. { The RTTI format matches one for records, except the type is tkClass.
  634. Since RecordRTTI does not check the type, calling it yields the desired result. }
  635. if Assigned(Temp) then
  636. RecordRTTI(Self,Temp,@int_finalize);
  637. vmt:= vmt^.vParent;
  638. end;
  639. end;
  640. procedure TObject.AfterConstruction;
  641. begin
  642. end;
  643. procedure TObject.BeforeDestruction;
  644. begin
  645. end;
  646. function IsGUIDEqual(const guid1, guid2: tguid): boolean;
  647. begin
  648. IsGUIDEqual:=
  649. (guid1.D1=guid2.D1) and
  650. (PDWORD(@guid1.D2)^=PDWORD(@guid2.D2)^) and
  651. (PDWORD(@guid1.D4[0])^=PDWORD(@guid2.D4[0])^) and
  652. (PDWORD(@guid1.D4[4])^=PDWORD(@guid2.D4[4])^);
  653. end;
  654. // Use of managed types should be avoided here; implicit _Addref/_Release
  655. // will end up in unpredictable behaviour if called on CORBA interfaces.
  656. type
  657. TInterfaceGetter = procedure(out Obj) of object;
  658. TClassGetter = function: TObject of object;
  659. function GetInterfaceByEntry(Instance: pointer; IEntry: pinterfaceentry; out obj): boolean;
  660. var
  661. Getter: TMethod;
  662. begin
  663. Pointer(Obj) := nil;
  664. Getter.Data := Instance;
  665. if Assigned(IEntry) and Assigned(Instance) then
  666. begin
  667. case IEntry^.IType of
  668. etStandard:
  669. Pointer(Obj) := PByte(instance)+IEntry^.IOffset;
  670. etFieldValue, etFieldValueClass:
  671. Pointer(obj) := PPointer(PByte(Instance)+IEntry^.IOffset)^;
  672. etVirtualMethodResult:
  673. begin
  674. // IOffset is relative to the VMT, not to instance.
  675. Getter.code := PCodePointer(PByte(PPointer(Instance)^) + IEntry^.IOffset)^;
  676. TInterfaceGetter(Getter)(obj);
  677. end;
  678. etVirtualMethodClass:
  679. begin
  680. // IOffset is relative to the VMT, not to instance.
  681. Getter.code := PCodePointer(PByte(PPointer(Instance)^) + IEntry^.IOffset)^;
  682. TObject(obj) := TClassGetter(Getter)();
  683. end;
  684. etStaticMethodResult:
  685. begin
  686. Getter.code := IEntry^.IOffsetAsCodePtr;
  687. TInterfaceGetter(Getter)(obj);
  688. end;
  689. etStaticMethodClass:
  690. begin
  691. Getter.code := IEntry^.IOffsetAsCodePtr;
  692. TObject(obj) := TClassGetter(Getter)();
  693. end;
  694. end;
  695. end;
  696. result := assigned(pointer(obj));
  697. end;
  698. function TObject.GetInterface(const iid : tguid;out obj) : boolean;
  699. var
  700. IEntry: PInterfaceEntry;
  701. Instance: TObject;
  702. begin
  703. if IsGUIDEqual(IObjectInstance,iid) then
  704. begin
  705. TObject(Obj) := Self;
  706. Result := True;
  707. Exit;
  708. end;
  709. Instance := self;
  710. repeat
  711. IEntry := Instance.GetInterfaceEntry(iid);
  712. result := GetInterfaceByEntry(Instance, IEntry, obj);
  713. if (not result) or
  714. (IEntry^.IType in [etStandard, etFieldValue,
  715. etStaticMethodResult, etVirtualMethodResult]) then
  716. Break;
  717. { if interface is implemented by a class-type property or field,
  718. continue search }
  719. Instance := TObject(obj);
  720. until False;
  721. { Getter function will normally AddRef, so adding another reference here
  722. will cause memleak. }
  723. if result and (IEntry^.IType in [etStandard, etFieldValue]) then
  724. IInterface(obj)._AddRef;
  725. end;
  726. function TObject.GetInterfaceWeak(const iid : tguid; out obj) : boolean;
  727. var
  728. IEntry: PInterfaceEntry;
  729. Instance: TObject;
  730. begin
  731. if IsGUIDEqual(IObjectInstance,iid) then
  732. begin
  733. TObject(Obj) := Self;
  734. Result := True;
  735. Exit;
  736. end;
  737. Instance := self;
  738. repeat
  739. IEntry := Instance.GetInterfaceEntry(iid);
  740. result := GetInterfaceByEntry(Instance, IEntry, obj);
  741. if (not result) or
  742. (IEntry^.IType in [etStandard, etFieldValue,
  743. etStaticMethodResult, etVirtualMethodResult]) then
  744. Break;
  745. { if interface is implemented by a class-type property or field,
  746. continue search }
  747. Instance := TObject(obj);
  748. until False;
  749. { Getter function will normally AddRef, so we have to release it,
  750. else the ref is not weak. }
  751. if result and not (IEntry^.IType in [etStandard, etFieldValue]) then
  752. IInterface(obj)._Release;
  753. end;
  754. function TObject.GetInterfaceByStr(const iidstr : shortstring;out obj) : boolean;
  755. var
  756. IEntry: PInterfaceEntry;
  757. Instance: TObject;
  758. begin
  759. Instance := self;
  760. repeat
  761. IEntry := Instance.GetInterfaceEntryByStr(iidstr);
  762. result := GetInterfaceByEntry(Instance, IEntry, obj);
  763. if (not result) or
  764. (IEntry^.IType in [etStandard, etFieldValue,
  765. etStaticMethodResult, etVirtualMethodResult]) then
  766. Break;
  767. { if interface is implemented by a class-type property or field,
  768. continue search }
  769. Instance := TObject(obj);
  770. until False;
  771. { Getter function will normally AddRef, so adding another reference here
  772. will cause memleak. (com interfaces only!) }
  773. if result and Assigned(IEntry^.IID) and (IEntry^.IType in [etStandard, etFieldValue]) then
  774. IInterface(obj)._AddRef;
  775. end;
  776. function TObject.GetInterface(const iidstr : shortstring;out obj) : boolean;
  777. begin
  778. Result := GetInterfaceByStr(iidstr,obj);
  779. end;
  780. class function TObject.GetInterfaceEntry(const iid : tguid) : pinterfaceentry;
  781. var
  782. i: longint;
  783. intftable: pinterfacetable;
  784. ovmt: PVmt;
  785. begin
  786. ovmt := PVmt(Self);
  787. while Assigned(ovmt) and {$ifdef VER3_0}(ovmt^.vIntfTable <> @emptyintf){$else}Assigned(ovmt^.vIntftable){$endif} do
  788. begin
  789. intftable:=ovmt^.vIntfTable;
  790. {$ifdef VER3_0}
  791. if assigned(intftable) then
  792. {$endif VER3_0}
  793. begin
  794. for i:=0 to intftable^.EntryCount-1 do
  795. begin
  796. result:=@intftable^.Entries[i];
  797. if assigned(Result^.iid) and IsGUIDEqual(Result^.iid^,iid) then
  798. Exit;
  799. end;
  800. end;
  801. ovmt := ovmt^.vParent;
  802. end;
  803. result := nil;
  804. end;
  805. class function TObject.GetInterfaceEntryByStr(const iidstr : shortstring) : pinterfaceentry;
  806. var
  807. i: longint;
  808. intftable: pinterfacetable;
  809. ovmt: PVmt;
  810. begin
  811. ovmt := PVmt(Self);
  812. while Assigned(ovmt) and {$ifdef VER3_0}(ovmt^.vIntfTable <> @emptyintf){$else}Assigned(ovmt^.vIntfTable){$endif} do
  813. begin
  814. intftable:=ovmt^.vIntfTable;
  815. {$ifdef VER3_0}
  816. if assigned(intftable) then
  817. {$endif VER3_0}
  818. begin
  819. for i:=0 to intftable^.EntryCount-1 do
  820. begin
  821. result:=@intftable^.Entries[i];
  822. if assigned(result^.iidstr) and (result^.iidstr^ = iidstr) then
  823. Exit;
  824. end;
  825. end;
  826. ovmt := ovmt^.vParent;
  827. end;
  828. result:=nil;
  829. end;
  830. class function TObject.GetInterfaceTable : pinterfacetable;
  831. begin
  832. getinterfacetable:=PVmt(Self)^.vIntfTable;
  833. end;
  834. class function TObject.UnitName : {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}ansistring{$else FPC_HAS_FEATURE_ANSISTRINGS}shortstring{$endif FPC_HAS_FEATURE_ANSISTRINGS};
  835. type
  836. // from the typinfo unit
  837. TClassTypeInfo = {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}packed{$endif}record
  838. ClassType: TClass;
  839. ParentInfo: Pointer;
  840. PropCount: SmallInt;
  841. UnitName: ShortString;
  842. end;
  843. PClassTypeInfo = ^TClassTypeInfo;
  844. var
  845. classtypeinfo: PClassTypeInfo;
  846. begin
  847. classtypeinfo:=ClassInfo;
  848. if Assigned(classtypeinfo) then
  849. begin
  850. // offset PTypeInfo by Length(Name) + 2 (ShortString length byte + SizeOf(Kind))
  851. inc(Pointer(classtypeinfo), PByte(Pointer(classtypeinfo)+1)^ + 2);
  852. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  853. classtypeinfo:=aligntoqword(classtypeinfo);
  854. {$endif}
  855. result:=classtypeinfo^.UnitName;
  856. end
  857. else
  858. result:='';
  859. end;
  860. class function TObject.QualifiedClassName: {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}ansistring{$else FPC_HAS_FEATURE_ANSISTRINGS}shortstring{$endif FPC_HAS_FEATURE_ANSISTRINGS};
  861. var
  862. uname: {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}ansistring{$else FPC_HAS_FEATURE_ANSISTRINGS}shortstring{$endif FPC_HAS_FEATURE_ANSISTRINGS};
  863. begin
  864. uname := UnitName; //TODO: change 'UnitName' to 'UnitScope' as soon as RTL implement it
  865. if uname='' then
  866. result:=ClassName
  867. else
  868. result:=Concat(uname, '.', ClassName);
  869. end;
  870. function TObject.Equals(Obj: TObject) : boolean;
  871. begin
  872. result:=Obj=Self;
  873. end;
  874. function TObject.GetHashCode: PtrInt;
  875. begin
  876. result:=PtrInt(Self);
  877. end;
  878. function TObject.ToString: {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}ansistring{$else FPC_HAS_FEATURE_ANSISTRINGS}shortstring{$endif FPC_HAS_FEATURE_ANSISTRINGS};
  879. begin
  880. result:=ClassName;
  881. end;
  882. {****************************************************************************
  883. TINTERFACEDOBJECT
  884. ****************************************************************************}
  885. function TInterfacedObject.QueryInterface(
  886. {$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  887. begin
  888. if getinterface(iid,obj) then
  889. result:=S_OK
  890. else
  891. result:=longint(E_NOINTERFACE);
  892. end;
  893. function TInterfacedObject._AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  894. begin
  895. _addref:=interlockedincrement(frefcount);
  896. end;
  897. function TInterfacedObject._Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  898. begin
  899. _Release:=interlockeddecrement(frefcount);
  900. if _Release=0 then
  901. begin
  902. if interlockedincrement(fdestroycount)=1 then
  903. self.destroy;
  904. end;
  905. end;
  906. destructor TInterfacedObject.destroy;
  907. begin
  908. // We must explicitly reset. Bug ID 32353
  909. FRefCount:=0;
  910. FDestroyCount:=0;
  911. inherited destroy;
  912. end;
  913. procedure TInterfacedObject.AfterConstruction;
  914. begin
  915. { we need to fix the refcount we forced in newinstance }
  916. { further, it must be done in a thread safe way }
  917. declocked(frefcount);
  918. end;
  919. procedure TInterfacedObject.BeforeDestruction;
  920. begin
  921. if frefcount<>0 then
  922. HandleError(204);
  923. end;
  924. class function TInterfacedObject.NewInstance : TObject;
  925. begin
  926. NewInstance:=inherited NewInstance;
  927. if NewInstance<>nil then
  928. TInterfacedObject(NewInstance).frefcount:=1;
  929. end;
  930. {****************************************************************************
  931. TAGGREGATEDOBJECT
  932. ****************************************************************************}
  933. constructor TAggregatedObject.Create(const aController: IUnknown);
  934. begin
  935. inherited Create;
  936. { do not keep a counted reference to the controller! }
  937. fcontroller := Pointer(aController);
  938. end;
  939. function TAggregatedObject.QueryInterface(
  940. {$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  941. begin
  942. Result := IUnknown(fcontroller).QueryInterface(iid, obj);
  943. end;
  944. function TAggregatedObject._AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  945. begin
  946. Result := IUnknown(fcontroller)._AddRef;
  947. end;
  948. function TAggregatedObject._Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  949. begin
  950. Result := IUnknown(fcontroller)._Release;
  951. end;
  952. function TAggregatedObject.GetController : IUnknown;
  953. begin
  954. Result := IUnknown(fcontroller);
  955. end;
  956. {****************************************************************************
  957. TContainedOBJECT
  958. ****************************************************************************}
  959. function TContainedObject.QueryInterface(
  960. {$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  961. begin
  962. if getinterface(iid,obj) then
  963. result:=S_OK
  964. else
  965. result:=longint(E_NOINTERFACE);
  966. end;
  967. {****************************************************************************
  968. Exception Support
  969. ****************************************************************************}
  970. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  971. {$i except.inc}
  972. {$endif FPC_HAS_FEATURE_EXCEPTIONS}