objpas.inc 34 KB

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