objpas.inc 36 KB

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