objpas.inc 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  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. {$ifdef FPC_HAS_FEATURE_RTTI}
  321. mopinittable : PRTTIRecordOpOffsetTable;
  322. {$endif def FPC_HAS_FEATURE_RTTI}
  323. i : longint;
  324. {$endif VER3_0}
  325. begin
  326. { the size is saved at offset 0 }
  327. fillchar(instance^, InstanceSize, 0);
  328. { insert VMT pointer into the new created memory area }
  329. { (in class methods self contains the VMT!) }
  330. ppointer(instance)^:=pointer(self);
  331. if {$ifdef VER3_0}PVmt(self)^.vIntfTable <> @emptyintf{$else}assigned(PVmt(self)^.vIntfTable){$endif} then
  332. InitInterfacePointers(self,instance);
  333. {$ifndef VER3_0}
  334. {$ifdef FPC_HAS_FEATURE_RTTI}
  335. { for management operators like initialize call int_initialize }
  336. vmt := PVmt(self);
  337. if assigned(vmt) then
  338. begin
  339. inittable:=vmt^.vInitTable;
  340. if assigned(inittable) then
  341. begin
  342. mopinittable:=RTTIRecordMopInitTable(inittable);
  343. if assigned(mopinittable) then
  344. begin
  345. {$push}
  346. { ensure that no range check errors pop up with the [0..0] array }
  347. {$R-}
  348. for i:=0 to mopinittable^.Count-1 do
  349. TRTTIRecVarOp(mopinittable^.Entries[i].ManagmentOperator)(PByte(Instance)+mopinittable^.Entries[i].FieldOffset);
  350. {$pop}
  351. end;
  352. end;
  353. end;
  354. {$endif def FPC_HAS_FEATURE_RTTI}
  355. {$endif VER3_0}
  356. InitInstance:=TObject(Instance);
  357. end;
  358. class function TObject.ClassParent : tclass;
  359. begin
  360. { type of self is class of tobject => it points to the vmt }
  361. { the parent vmt is saved at offset vmtParent }
  362. classparent:=tclass(PVmt(Self)^.vParent);
  363. end;
  364. class function TObject.NewInstance : tobject;
  365. var
  366. p : pointer;
  367. begin
  368. getmem(p, InstanceSize);
  369. if p <> nil then
  370. InitInstance(p);
  371. NewInstance:=TObject(p);
  372. end;
  373. procedure TObject.FreeInstance;
  374. begin
  375. CleanupInstance;
  376. FreeMem(Pointer(Self));
  377. end;
  378. class function TObject.ClassType : TClass;
  379. begin
  380. ClassType:=TClass(Pointer(Self))
  381. end;
  382. type
  383. tmethodnamerec = packed record
  384. name : pshortstring;
  385. addr : codepointer;
  386. end;
  387. tmethodnametable = packed record
  388. count : dword;
  389. entries : packed array[0..0] of tmethodnamerec;
  390. end;
  391. pmethodnametable = ^tmethodnametable;
  392. class function TObject.MethodAddress(const name : shortstring) : codepointer;
  393. var
  394. methodtable : pmethodnametable;
  395. i : dword;
  396. ovmt : PVmt;
  397. begin
  398. ovmt:=PVmt(self);
  399. while assigned(ovmt) do
  400. begin
  401. methodtable:=pmethodnametable(ovmt^.vMethodTable);
  402. if assigned(methodtable) then
  403. begin
  404. for i:=0 to methodtable^.count-1 do
  405. if ShortCompareText(methodtable^.entries[i].name^, name)=0 then
  406. begin
  407. MethodAddress:=methodtable^.entries[i].addr;
  408. exit;
  409. end;
  410. end;
  411. ovmt := ovmt^.vParent;
  412. end;
  413. MethodAddress:=nil;
  414. end;
  415. class function TObject.MethodName(address : codepointer) : shortstring;
  416. var
  417. methodtable : pmethodnametable;
  418. i : dword;
  419. ovmt : PVmt;
  420. begin
  421. ovmt:=PVmt(self);
  422. while assigned(ovmt) do
  423. begin
  424. methodtable:=pmethodnametable(ovmt^.vMethodTable);
  425. if assigned(methodtable) then
  426. begin
  427. for i:=0 to methodtable^.count-1 do
  428. if methodtable^.entries[i].addr=address then
  429. begin
  430. MethodName:=methodtable^.entries[i].name^;
  431. exit;
  432. end;
  433. end;
  434. ovmt := ovmt^.vParent;
  435. end;
  436. MethodName:='';
  437. end;
  438. function TObject.FieldAddress(const name : shortstring) : pointer;
  439. type
  440. PFieldInfo = ^TFieldInfo;
  441. TFieldInfo =
  442. {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
  443. packed
  444. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  445. record
  446. FieldOffset: SizeUInt;
  447. ClassTypeIndex: Word;
  448. Name: ShortString;
  449. end;
  450. PFieldTable = ^TFieldTable;
  451. TFieldTable =
  452. {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
  453. packed
  454. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  455. record
  456. FieldCount: Word;
  457. ClassTable: Pointer;
  458. { should be array[Word] of TFieldInfo; but
  459. Elements have variant size! force at least proper alignment }
  460. Fields: array[0..0] of TFieldInfo
  461. end;
  462. var
  463. ovmt: PVmt;
  464. FieldTable: PFieldTable;
  465. FieldInfo: PFieldInfo;
  466. i: longint;
  467. begin
  468. if Length(name) > 0 then
  469. begin
  470. ovmt := PVmt(ClassType);
  471. while ovmt <> nil do
  472. begin
  473. FieldTable := PFieldTable(ovmt^.vFieldTable);
  474. if FieldTable <> nil then
  475. begin
  476. FieldInfo := @FieldTable^.Fields[0];
  477. for i := 0 to FieldTable^.FieldCount - 1 do
  478. begin
  479. if ShortCompareText(FieldInfo^.Name, name) = 0 then
  480. begin
  481. fieldaddress := Pointer(Self) + FieldInfo^.FieldOffset;
  482. exit;
  483. end;
  484. FieldInfo := PFieldInfo(PByte(@FieldInfo^.Name) + 1 + Length(FieldInfo^.Name));
  485. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  486. { align to largest field of TFieldInfo }
  487. FieldInfo := Align(FieldInfo, SizeOf(PtrUInt));
  488. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  489. end;
  490. end;
  491. { Try again with the parent class type }
  492. ovmt:=ovmt^.vParent;
  493. end;
  494. end;
  495. fieldaddress:=nil;
  496. end;
  497. function TObject.SafeCallException(exceptobject : tobject;
  498. exceptaddr : codepointer) : HResult;
  499. begin
  500. safecallexception:=E_UNEXPECTED;
  501. end;
  502. class function TObject.ClassInfo : pointer;
  503. begin
  504. ClassInfo := PVmt(Self)^.vTypeInfo;
  505. end;
  506. class function TObject.ClassName : ShortString;
  507. begin
  508. ClassName := PVmt(Self)^.vClassName^;
  509. end;
  510. class function TObject.ClassNameIs(const name : string) : boolean;
  511. begin
  512. // call to ClassName inlined here, this eliminates stack and string copying.
  513. ClassNameIs:=ShortCompareText(PVmt(Self)^.vClassName^, name) = 0;
  514. end;
  515. class function TObject.InheritsFrom(aclass : TClass) : Boolean;
  516. var
  517. vmt: PVmt;
  518. begin
  519. if assigned(aclass) then
  520. begin
  521. vmt:=PVmt(self);
  522. while assigned(vmt) and (vmt <> PVmt(aclass)) do
  523. vmt := vmt^.vParent;
  524. InheritsFrom := (vmt = PVmt(aclass));
  525. end
  526. else
  527. inheritsFrom := False;
  528. end;
  529. class function TObject.stringmessagetable : pstringmessagetable;
  530. begin
  531. stringmessagetable:=PVmt(Self)^.vMsgStrPtr;
  532. end;
  533. type
  534. tmessagehandler = procedure(var msg) of object;
  535. procedure TObject.Dispatch(var message);
  536. type
  537. {$PUSH}
  538. {$PACKRECORDS NORMAL}
  539. PMsgIntTable = ^TMsgIntTable;
  540. TMsgIntTable = record
  541. index : dword;
  542. method : codepointer;
  543. end;
  544. PMsgInt = ^TMsgInt;
  545. TMsgInt = record
  546. count : longint;
  547. msgs : array[0..0] of TMsgIntTable;
  548. end;
  549. {$POP}
  550. var
  551. index : dword;
  552. count,i : longint;
  553. msgtable : PMsgIntTable;
  554. p : PMsgInt;
  555. ovmt : PVmt;
  556. msghandler : tmessagehandler;
  557. begin
  558. index:=dword(message);
  559. ovmt := PVmt(ClassType);
  560. while assigned(ovmt) do
  561. begin
  562. // See if we have messages at all in this class.
  563. p:=PMsgInt(ovmt^.vDynamicTable);
  564. If Assigned(p) then
  565. begin
  566. msgtable:=@p^.msgs;
  567. count:=p^.count;
  568. end
  569. else
  570. Count:=0;
  571. { later, we can implement a binary search here }
  572. for i:=0 to count-1 do
  573. begin
  574. if index=msgtable[i].index then
  575. begin
  576. TMethod(msghandler).Code:=msgtable[i].method;
  577. TMethod(msghandler).Data:=self;
  578. msghandler(message);
  579. exit;
  580. end;
  581. end;
  582. ovmt:=ovmt^.vParent;
  583. end;
  584. DefaultHandler(message);
  585. end;
  586. procedure TObject.DispatchStr(var message);
  587. var
  588. name : shortstring;
  589. count,i : longint;
  590. msgstrtable : pmsgstrtable;
  591. p: pstringmessagetable;
  592. ovmt : PVmt;
  593. msghandler : tmessagehandler;
  594. begin
  595. name:=pshortstring(@message)^;
  596. ovmt:=PVmt(ClassType);
  597. while assigned(ovmt) do
  598. begin
  599. p := ovmt^.vMsgStrPtr;
  600. if (P<>Nil) and (p^.count<>0) then
  601. begin
  602. count:=p^.count;
  603. msgstrtable:=@p^.msgstrtable;
  604. end
  605. else
  606. Count:=0;
  607. { later, we can implement a binary search here }
  608. for i:=0 to count-1 do
  609. begin
  610. if name=msgstrtable[i].name^ then
  611. begin
  612. TMethod(msghandler).Code:=msgstrtable[i].method;
  613. TMethod(msghandler).Data:=self;
  614. msghandler(message);
  615. exit;
  616. end;
  617. end;
  618. ovmt:=ovmt^.vParent;
  619. end;
  620. DefaultHandlerStr(message);
  621. end;
  622. procedure TObject.DefaultHandler(var message);
  623. begin
  624. end;
  625. procedure TObject.DefaultHandlerStr(var message);
  626. begin
  627. end;
  628. procedure TObject.CleanupInstance;
  629. var
  630. vmt : PVmt;
  631. temp : pointer;
  632. begin
  633. vmt := PVmt(ClassType);
  634. while vmt<>nil do
  635. begin
  636. Temp:= vmt^.vInitTable;
  637. {$ifdef FPC_HAS_FEATURE_RTTI}
  638. { The RTTI format matches one for records, except the type is tkClass.
  639. Since RecordRTTI does not check the type, calling it yields the desired result. }
  640. if Assigned(Temp) then
  641. RecordRTTI(Self,Temp,@int_finalize);
  642. {$endif def FPC_HAS_FEATURE_RTTI}
  643. vmt:= vmt^.vParent;
  644. end;
  645. end;
  646. procedure TObject.AfterConstruction;
  647. begin
  648. end;
  649. procedure TObject.BeforeDestruction;
  650. begin
  651. end;
  652. function IsGUIDEqual(const guid1, guid2: tguid): boolean;
  653. begin
  654. IsGUIDEqual:=
  655. (guid1.D1=guid2.D1) and
  656. (PDWORD(@guid1.D2)^=PDWORD(@guid2.D2)^) and
  657. (PDWORD(@guid1.D4[0])^=PDWORD(@guid2.D4[0])^) and
  658. (PDWORD(@guid1.D4[4])^=PDWORD(@guid2.D4[4])^);
  659. end;
  660. // Use of managed types should be avoided here; implicit _Addref/_Release
  661. // will end up in unpredictable behaviour if called on CORBA interfaces.
  662. type
  663. TInterfaceGetter = procedure(out Obj) of object;
  664. TClassGetter = function: TObject of object;
  665. function GetInterfaceByEntry(Instance: pointer; IEntry: pinterfaceentry; out obj): boolean;
  666. var
  667. Getter: TMethod;
  668. begin
  669. Pointer(Obj) := nil;
  670. Getter.Data := Instance;
  671. if Assigned(IEntry) and Assigned(Instance) then
  672. begin
  673. case IEntry^.IType of
  674. etStandard:
  675. Pointer(Obj) := PByte(instance)+IEntry^.IOffset;
  676. etFieldValue, etFieldValueClass:
  677. Pointer(obj) := PPointer(PByte(Instance)+IEntry^.IOffset)^;
  678. etVirtualMethodResult:
  679. begin
  680. // IOffset is relative to the VMT, not to instance.
  681. Getter.code := PCodePointer(PByte(PPointer(Instance)^) + IEntry^.IOffset)^;
  682. TInterfaceGetter(Getter)(obj);
  683. end;
  684. etVirtualMethodClass:
  685. begin
  686. // IOffset is relative to the VMT, not to instance.
  687. Getter.code := PCodePointer(PByte(PPointer(Instance)^) + IEntry^.IOffset)^;
  688. TObject(obj) := TClassGetter(Getter)();
  689. end;
  690. etStaticMethodResult:
  691. begin
  692. Getter.code := IEntry^.IOffsetAsCodePtr;
  693. TInterfaceGetter(Getter)(obj);
  694. end;
  695. etStaticMethodClass:
  696. begin
  697. Getter.code := IEntry^.IOffsetAsCodePtr;
  698. TObject(obj) := TClassGetter(Getter)();
  699. end;
  700. end;
  701. end;
  702. result := assigned(pointer(obj));
  703. end;
  704. function TObject.GetInterface(const iid : tguid;out obj) : boolean;
  705. var
  706. IEntry: PInterfaceEntry;
  707. Instance: TObject;
  708. begin
  709. if IsGUIDEqual(IObjectInstance,iid) then
  710. begin
  711. TObject(Obj) := Self;
  712. Result := True;
  713. Exit;
  714. end;
  715. Instance := self;
  716. repeat
  717. IEntry := Instance.GetInterfaceEntry(iid);
  718. result := GetInterfaceByEntry(Instance, IEntry, obj);
  719. if (not result) or
  720. (IEntry^.IType in [etStandard, etFieldValue,
  721. etStaticMethodResult, etVirtualMethodResult]) then
  722. Break;
  723. { if interface is implemented by a class-type property or field,
  724. continue search }
  725. Instance := TObject(obj);
  726. until False;
  727. { Getter function will normally AddRef, so adding another reference here
  728. will cause memleak. }
  729. if result and (IEntry^.IType in [etStandard, etFieldValue]) then
  730. IInterface(obj)._AddRef;
  731. end;
  732. function TObject.GetInterfaceWeak(const iid : tguid; out obj) : boolean;
  733. var
  734. IEntry: PInterfaceEntry;
  735. Instance: TObject;
  736. begin
  737. if IsGUIDEqual(IObjectInstance,iid) then
  738. begin
  739. TObject(Obj) := Self;
  740. Result := True;
  741. Exit;
  742. end;
  743. Instance := self;
  744. repeat
  745. IEntry := Instance.GetInterfaceEntry(iid);
  746. result := GetInterfaceByEntry(Instance, IEntry, obj);
  747. if (not result) or
  748. (IEntry^.IType in [etStandard, etFieldValue,
  749. etStaticMethodResult, etVirtualMethodResult]) then
  750. Break;
  751. { if interface is implemented by a class-type property or field,
  752. continue search }
  753. Instance := TObject(obj);
  754. until False;
  755. { Getter function will normally AddRef, so we have to release it,
  756. else the ref is not weak. }
  757. if result and not (IEntry^.IType in [etStandard, etFieldValue]) then
  758. IInterface(obj)._Release;
  759. end;
  760. function TObject.GetInterfaceByStr(const iidstr : shortstring;out obj) : boolean;
  761. var
  762. IEntry: PInterfaceEntry;
  763. Instance: TObject;
  764. begin
  765. Instance := self;
  766. repeat
  767. IEntry := Instance.GetInterfaceEntryByStr(iidstr);
  768. result := GetInterfaceByEntry(Instance, IEntry, obj);
  769. if (not result) or
  770. (IEntry^.IType in [etStandard, etFieldValue,
  771. etStaticMethodResult, etVirtualMethodResult]) then
  772. Break;
  773. { if interface is implemented by a class-type property or field,
  774. continue search }
  775. Instance := TObject(obj);
  776. until False;
  777. { Getter function will normally AddRef, so adding another reference here
  778. will cause memleak. (com interfaces only!) }
  779. if result and Assigned(IEntry^.IID) and (IEntry^.IType in [etStandard, etFieldValue]) then
  780. IInterface(obj)._AddRef;
  781. end;
  782. function TObject.GetInterface(const iidstr : shortstring;out obj) : boolean;
  783. begin
  784. Result := GetInterfaceByStr(iidstr,obj);
  785. end;
  786. class function TObject.GetInterfaceEntry(const iid : tguid) : pinterfaceentry;
  787. var
  788. i: longint;
  789. intftable: pinterfacetable;
  790. ovmt: PVmt;
  791. begin
  792. ovmt := PVmt(Self);
  793. while Assigned(ovmt) and {$ifdef VER3_0}(ovmt^.vIntfTable <> @emptyintf){$else}Assigned(ovmt^.vIntftable){$endif} do
  794. begin
  795. intftable:=ovmt^.vIntfTable;
  796. {$ifdef VER3_0}
  797. if assigned(intftable) then
  798. {$endif VER3_0}
  799. begin
  800. for i:=0 to intftable^.EntryCount-1 do
  801. begin
  802. result:=@intftable^.Entries[i];
  803. if assigned(Result^.iid) and IsGUIDEqual(Result^.iid^,iid) then
  804. Exit;
  805. end;
  806. end;
  807. ovmt := ovmt^.vParent;
  808. end;
  809. result := nil;
  810. end;
  811. class function TObject.GetInterfaceEntryByStr(const iidstr : shortstring) : pinterfaceentry;
  812. var
  813. i: longint;
  814. intftable: pinterfacetable;
  815. ovmt: PVmt;
  816. begin
  817. ovmt := PVmt(Self);
  818. while Assigned(ovmt) and {$ifdef VER3_0}(ovmt^.vIntfTable <> @emptyintf){$else}Assigned(ovmt^.vIntfTable){$endif} do
  819. begin
  820. intftable:=ovmt^.vIntfTable;
  821. {$ifdef VER3_0}
  822. if assigned(intftable) then
  823. {$endif VER3_0}
  824. begin
  825. for i:=0 to intftable^.EntryCount-1 do
  826. begin
  827. result:=@intftable^.Entries[i];
  828. if assigned(result^.iidstr) and (result^.iidstr^ = iidstr) then
  829. Exit;
  830. end;
  831. end;
  832. ovmt := ovmt^.vParent;
  833. end;
  834. result:=nil;
  835. end;
  836. class function TObject.GetInterfaceTable : pinterfacetable;
  837. begin
  838. getinterfacetable:=PVmt(Self)^.vIntfTable;
  839. end;
  840. class function TObject.UnitName : {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}ansistring{$else FPC_HAS_FEATURE_ANSISTRINGS}shortstring{$endif FPC_HAS_FEATURE_ANSISTRINGS};
  841. {$ifdef FPC_HAS_FEATURE_RTTI}
  842. type
  843. TClassTypeInfo = {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}packed{$endif}record
  844. Attributes: Pointer;
  845. case TTypeKind of
  846. tkClass: (
  847. ClassType: TClass;
  848. ParentInfo: Pointer;
  849. PropCount: SmallInt;
  850. UnitName: ShortString;
  851. );
  852. { include for proper alignment }
  853. tkInt64: (
  854. Dummy: Int64;
  855. );
  856. end;
  857. PClassTypeInfo = ^TClassTypeInfo;
  858. var
  859. classtypeinfo: PClassTypeInfo;
  860. begin
  861. classtypeinfo:=ClassInfo;
  862. if Assigned(classtypeinfo) then
  863. begin
  864. // offset PTypeInfo by Length(Name) + 2 (ShortString length byte + SizeOf(Kind))
  865. inc(Pointer(classtypeinfo), PByte(Pointer(classtypeinfo)+1)^ + 2);
  866. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  867. classtypeinfo:=aligntoqword(classtypeinfo);
  868. {$endif}
  869. result:=classtypeinfo^.UnitName;
  870. end
  871. else
  872. result:='';
  873. end;
  874. {$else not FPC_HAS_FEATURE_RTTI}
  875. begin
  876. result:='';
  877. end;
  878. {$endif ndef FPC_HAS_FEATURE_RTTI}
  879. class function TObject.QualifiedClassName: {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}ansistring{$else FPC_HAS_FEATURE_ANSISTRINGS}shortstring{$endif FPC_HAS_FEATURE_ANSISTRINGS};
  880. var
  881. uname: {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}ansistring{$else FPC_HAS_FEATURE_ANSISTRINGS}shortstring{$endif FPC_HAS_FEATURE_ANSISTRINGS};
  882. begin
  883. uname := UnitName; //TODO: change 'UnitName' to 'UnitScope' as soon as RTL implement it
  884. if uname='' then
  885. result:=ClassName
  886. else
  887. result:=Concat(uname, '.', ClassName);
  888. end;
  889. function TObject.Equals(Obj: TObject) : boolean;
  890. begin
  891. result:=Obj=Self;
  892. end;
  893. function TObject.GetHashCode: PtrInt;
  894. begin
  895. result:=PtrInt(Self);
  896. end;
  897. function TObject.ToString: {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}ansistring{$else FPC_HAS_FEATURE_ANSISTRINGS}shortstring{$endif FPC_HAS_FEATURE_ANSISTRINGS};
  898. begin
  899. result:=ClassName;
  900. end;
  901. {****************************************************************************
  902. TINTERFACEDOBJECT
  903. ****************************************************************************}
  904. function TInterfacedObject.QueryInterface(
  905. {$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  906. begin
  907. if getinterface(iid,obj) then
  908. result:=S_OK
  909. else
  910. result:=longint(E_NOINTERFACE);
  911. end;
  912. function TInterfacedObject._AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  913. begin
  914. _addref:=interlockedincrement(frefcount);
  915. end;
  916. function TInterfacedObject._Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  917. begin
  918. _Release:=interlockeddecrement(frefcount);
  919. if _Release=0 then
  920. begin
  921. if interlockedincrement(fdestroycount)=1 then
  922. self.destroy;
  923. end;
  924. end;
  925. destructor TInterfacedObject.Destroy;
  926. begin
  927. // We must explicitly reset. Bug ID 32353
  928. FRefCount:=0;
  929. FDestroyCount:=0;
  930. inherited Destroy;
  931. end;
  932. procedure TInterfacedObject.AfterConstruction;
  933. begin
  934. { we need to fix the refcount we forced in newinstance }
  935. { further, it must be done in a thread safe way }
  936. declocked(frefcount);
  937. end;
  938. procedure TInterfacedObject.BeforeDestruction;
  939. begin
  940. if frefcount<>0 then
  941. HandleError(204);
  942. end;
  943. class function TInterfacedObject.NewInstance : TObject;
  944. begin
  945. NewInstance:=inherited NewInstance;
  946. if NewInstance<>nil then
  947. TInterfacedObject(NewInstance).frefcount:=1;
  948. end;
  949. {****************************************************************************
  950. TAGGREGATEDOBJECT
  951. ****************************************************************************}
  952. constructor TAggregatedObject.Create(const aController: IUnknown);
  953. begin
  954. inherited Create;
  955. { do not keep a counted reference to the controller! }
  956. fcontroller := Pointer(aController);
  957. end;
  958. function TAggregatedObject.QueryInterface(
  959. {$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  960. begin
  961. Result := IUnknown(fcontroller).QueryInterface(iid, obj);
  962. end;
  963. function TAggregatedObject._AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  964. begin
  965. Result := IUnknown(fcontroller)._AddRef;
  966. end;
  967. function TAggregatedObject._Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  968. begin
  969. Result := IUnknown(fcontroller)._Release;
  970. end;
  971. function TAggregatedObject.GetController : IUnknown;
  972. begin
  973. Result := IUnknown(fcontroller);
  974. end;
  975. {****************************************************************************
  976. TContainedOBJECT
  977. ****************************************************************************}
  978. function TContainedObject.QueryInterface(
  979. {$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  980. begin
  981. if getinterface(iid,obj) then
  982. result:=S_OK
  983. else
  984. result:=longint(E_NOINTERFACE);
  985. end;
  986. {****************************************************************************
  987. TCustomAttribute
  988. ****************************************************************************}
  989. constructor TCustomAttribute.Create;
  990. begin
  991. inherited;
  992. end;
  993. {****************************************************************************
  994. TCustomStoredAttribute
  995. ****************************************************************************}
  996. constructor StoredAttribute.Create;
  997. begin
  998. end;
  999. constructor StoredAttribute.Create(Const aFlag : Boolean);
  1000. begin
  1001. FFlag:=aFlag;
  1002. end;
  1003. constructor StoredAttribute.Create(Const aName : string);
  1004. begin
  1005. FName:=aName;
  1006. end;
  1007. {****************************************************************************
  1008. Exception Support
  1009. ****************************************************************************}
  1010. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  1011. {$i except.inc}
  1012. {$endif FPC_HAS_FEATURE_EXCEPTIONS}