objpas.inc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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. {****************************************************************************
  12. Internal Routines called from the Compiler
  13. ****************************************************************************}
  14. { the reverse order of the parameters make code generation easier }
  15. function fpc_do_is(aclass : tclass;aobject : tobject) : boolean;[public,alias: 'FPC_DO_IS']; compilerproc;
  16. begin
  17. fpc_do_is:=assigned(aobject) and assigned(aclass) and
  18. aobject.inheritsfrom(aclass);
  19. end;
  20. { the reverse order of the parameters make code generation easier }
  21. function fpc_do_as(aclass : tclass;aobject : tobject): tobject;[public,alias: 'FPC_DO_AS']; compilerproc;
  22. begin
  23. if assigned(aobject) and not(aobject.inheritsfrom(aclass)) then
  24. handleerrorframe(219,get_frame);
  25. result := aobject;
  26. end;
  27. { interface helpers }
  28. procedure fpc_intf_decr_ref(var i: pointer);[public,alias: 'FPC_INTF_DECR_REF']; compilerproc;
  29. begin
  30. if assigned(i) then
  31. IUnknown(i)._Release;
  32. i:=nil;
  33. end;
  34. { local declaration for intf_decr_ref for local access }
  35. procedure intf_decr_ref(var i: pointer); [external name 'FPC_INTF_DECR_REF'];
  36. procedure fpc_intf_incr_ref(i: pointer);[public,alias: 'FPC_INTF_INCR_REF']; compilerproc;
  37. begin
  38. if assigned(i) then
  39. IUnknown(i)._AddRef;
  40. end;
  41. { local declaration of intf_incr_ref for local access }
  42. procedure intf_incr_ref(i: pointer); [external name 'FPC_INTF_INCR_REF'];
  43. procedure fpc_intf_assign(var D: pointer; const S: pointer);[public,alias: 'FPC_INTF_ASSIGN']; compilerproc;
  44. begin
  45. if assigned(S) then
  46. IUnknown(S)._AddRef;
  47. if assigned(D) then
  48. IUnknown(D)._Release;
  49. D:=S;
  50. end;
  51. function fpc_intf_as(const S: pointer; const iid: TGUID): pointer;[public,alias: 'FPC_INTF_AS']; compilerproc;
  52. var
  53. tmpi: pointer; // _AddRef before _Release
  54. begin
  55. if assigned(S) then
  56. begin
  57. if IUnknown(S).QueryInterface(iid,tmpi)<>S_OK then
  58. handleerror(219);
  59. fpc_intf_as:=tmpi;
  60. end
  61. else
  62. fpc_intf_as:=nil;
  63. end;
  64. function fpc_class_as_intf(const S: pointer; const iid: TGUID): pointer;[public,alias: 'FPC_CLASS_AS_INTF']; compilerproc;
  65. var
  66. tmpi: pointer; // _AddRef before _Release
  67. begin
  68. if assigned(S) then
  69. begin
  70. if not TObject(S).GetInterface(iid,tmpi) then
  71. handleerror(219);
  72. fpc_class_as_intf:=tmpi;
  73. end
  74. else
  75. fpc_class_as_intf:=nil;
  76. end;
  77. {****************************************************************************
  78. TOBJECT
  79. ****************************************************************************}
  80. constructor TObject.Create;
  81. begin
  82. end;
  83. destructor TObject.Destroy;
  84. begin
  85. end;
  86. procedure TObject.Free;
  87. begin
  88. // the call via self avoids a warning
  89. if self<>nil then
  90. self.destroy;
  91. end;
  92. class function TObject.InstanceSize : SizeInt;
  93. begin
  94. InstanceSize:=pSizeInt(pointer(self)+vmtInstanceSize)^;
  95. end;
  96. procedure InitInterfacePointers(objclass: tclass;instance : pointer);
  97. var
  98. intftable : pinterfacetable;
  99. i : longint;
  100. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  101. IOffset : longint;
  102. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  103. begin
  104. while assigned(objclass) do
  105. begin
  106. intftable:=pinterfacetable((pointer(objclass)+vmtIntfTable)^);
  107. if assigned(intftable) then
  108. for i:=0 to intftable^.EntryCount-1 do
  109. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  110. begin
  111. move(intftable^.Entries[i].IOffset,IOffset,sizeof(longint));
  112. move(pointer(intftable^.Entries[i].VTable),ppointer(@(PChar(instance)[IOffset]))^,sizeof(pointer));
  113. end;
  114. {$else FPC_REQUIRES_PROPER_ALIGNMENT}
  115. ppointer(@(PChar(instance)[intftable^.Entries[i].IOffset]))^:=
  116. pointer(intftable^.Entries[i].VTable);
  117. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  118. objclass:=pclass(pointer(objclass)+vmtParent)^;
  119. end;
  120. end;
  121. class function TObject.InitInstance(instance : pointer) : tobject;
  122. begin
  123. { the size is saved at offset 0 }
  124. fillchar(instance^, InstanceSize, 0);
  125. { insert VMT pointer into the new created memory area }
  126. { (in class methods self contains the VMT!) }
  127. ppointer(instance)^:=pointer(self);
  128. InitInterfacePointers(self,instance);
  129. InitInstance:=TObject(Instance);
  130. end;
  131. class function TObject.ClassParent : tclass;
  132. begin
  133. { type of self is class of tobject => it points to the vmt }
  134. { the parent vmt is saved at offset vmtParent }
  135. classparent:=pclass(pointer(self)+vmtParent)^;
  136. end;
  137. class function TObject.NewInstance : tobject;
  138. var
  139. p : pointer;
  140. begin
  141. getmem(p, InstanceSize);
  142. if p <> nil then
  143. InitInstance(p);
  144. NewInstance:=TObject(p);
  145. end;
  146. procedure TObject.FreeInstance;
  147. begin
  148. CleanupInstance;
  149. FreeMem(Pointer(Self));
  150. end;
  151. class function TObject.ClassType : TClass;
  152. begin
  153. ClassType:=TClass(Pointer(Self))
  154. end;
  155. type
  156. tmethodnamerec = packed record
  157. name : pshortstring;
  158. addr : pointer;
  159. end;
  160. tmethodnametable = packed record
  161. count : dword;
  162. entries : packed array[0..0] of tmethodnamerec;
  163. end;
  164. pmethodnametable = ^tmethodnametable;
  165. class function TObject.MethodAddress(const name : shortstring) : pointer;
  166. var
  167. UName : ShortString;
  168. methodtable : pmethodnametable;
  169. i : dword;
  170. vmt : tclass;
  171. begin
  172. UName := UpCase(name);
  173. vmt:=self;
  174. while assigned(vmt) do
  175. begin
  176. methodtable:=pmethodnametable((Pointer(vmt)+vmtMethodTable)^);
  177. if assigned(methodtable) then
  178. begin
  179. for i:=0 to methodtable^.count-1 do
  180. if UpCase(methodtable^.entries[i].name^)=UName then
  181. begin
  182. MethodAddress:=methodtable^.entries[i].addr;
  183. exit;
  184. end;
  185. end;
  186. vmt:=pclass(pointer(vmt)+vmtParent)^;
  187. end;
  188. MethodAddress:=nil;
  189. end;
  190. class function TObject.MethodName(address : pointer) : shortstring;
  191. var
  192. methodtable : pmethodnametable;
  193. i : dword;
  194. vmt : tclass;
  195. begin
  196. vmt:=self;
  197. while assigned(vmt) do
  198. begin
  199. methodtable:=pmethodnametable((Pointer(vmt)+vmtMethodTable)^);
  200. if assigned(methodtable) then
  201. begin
  202. for i:=0 to methodtable^.count-1 do
  203. if methodtable^.entries[i].addr=address then
  204. begin
  205. MethodName:=methodtable^.entries[i].name^;
  206. exit;
  207. end;
  208. end;
  209. vmt:=pclass(pointer(vmt)+vmtParent)^;
  210. end;
  211. MethodName:='';
  212. end;
  213. function TObject.FieldAddress(const name : shortstring) : pointer;
  214. type
  215. PFieldInfo = ^TFieldInfo;
  216. TFieldInfo =
  217. {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
  218. packed
  219. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  220. record
  221. FieldOffset: PtrUInt;
  222. ClassTypeIndex: Word;
  223. Name: ShortString;
  224. end;
  225. PFieldTable = ^TFieldTable;
  226. TFieldTable =
  227. {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
  228. packed
  229. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  230. record
  231. FieldCount: Word;
  232. ClassTable: Pointer;
  233. { should be array[Word] of TFieldInfo; but
  234. Elements have variant size! force at least proper alignment }
  235. Fields: array[0..0] of TFieldInfo
  236. end;
  237. var
  238. UName: ShortString;
  239. CurClassType: TClass;
  240. FieldTable: PFieldTable;
  241. FieldInfo: PFieldInfo;
  242. i: Integer;
  243. begin
  244. if Length(name) > 0 then
  245. begin
  246. UName := UpCase(name);
  247. CurClassType := ClassType;
  248. while CurClassType <> nil do
  249. begin
  250. FieldTable := PFieldTable((Pointer(CurClassType) + vmtFieldTable)^);
  251. if FieldTable <> nil then
  252. begin
  253. FieldInfo := @FieldTable^.Fields;
  254. for i := 0 to FieldTable^.FieldCount - 1 do
  255. begin
  256. if UpCase(FieldInfo^.Name) = UName then
  257. begin
  258. fieldaddress := Pointer(Self) + FieldInfo^.FieldOffset;
  259. exit;
  260. end;
  261. FieldInfo := @FieldInfo^.Name + 1 + Length(FieldInfo^.Name);
  262. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  263. { align to largest field of TFieldInfo }
  264. FieldInfo := Align(FieldInfo, SizeOf(PtrUInt));
  265. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  266. end;
  267. end;
  268. { Try again with the parent class type }
  269. CurClassType:=pclass(pointer(CurClassType)+vmtParent)^;
  270. end;
  271. end;
  272. fieldaddress:=nil;
  273. end;
  274. function TObject.SafeCallException(exceptobject : tobject;
  275. exceptaddr : pointer) : longint;
  276. begin
  277. safecallexception:=0;
  278. end;
  279. class function TObject.ClassInfo : pointer;
  280. begin
  281. ClassInfo:=ppointer(Pointer(self)+vmtTypeInfo)^;
  282. end;
  283. class function TObject.ClassName : ShortString;
  284. begin
  285. ClassName:=PShortString((Pointer(Self)+vmtClassName)^)^;
  286. end;
  287. class function TObject.ClassNameIs(const name : string) : boolean;
  288. begin
  289. ClassNameIs:=Upcase(ClassName)=Upcase(name);
  290. end;
  291. class function TObject.InheritsFrom(aclass : TClass) : Boolean;
  292. var
  293. vmt : tclass;
  294. begin
  295. vmt:=self;
  296. while assigned(vmt) do
  297. begin
  298. if vmt=aclass then
  299. begin
  300. InheritsFrom:=true;
  301. exit;
  302. end;
  303. vmt:=pclass(pointer(vmt)+vmtParent)^;
  304. end;
  305. InheritsFrom:=false;
  306. end;
  307. class function TObject.stringmessagetable : pstringmessagetable;
  308. begin
  309. stringmessagetable:=pstringmessagetable((pointer(Self)+vmtMsgStrPtr)^);
  310. end;
  311. type
  312. tmessagehandler = procedure(var msg) of object;
  313. tmessagehandlerrec = packed record
  314. proc : pointer;
  315. obj : pointer;
  316. end;
  317. procedure TObject.Dispatch(var message);
  318. type
  319. tmsgtable = packed record
  320. index : dword;
  321. method : pointer;
  322. end;
  323. pmsgtable = ^tmsgtable;
  324. var
  325. index : dword;
  326. count,i : longint;
  327. msgtable : pmsgtable;
  328. p : pointer;
  329. vmt : tclass;
  330. msghandler : tmessagehandler;
  331. begin
  332. index:=dword(message);
  333. vmt:=ClassType;
  334. while assigned(vmt) do
  335. begin
  336. // See if we have messages at all in this class.
  337. p:=pointer(vmt)+vmtDynamicTable;
  338. If assigned(PPointer(p)^) then
  339. begin
  340. msgtable:=pmsgtable(Pointer(p^)+4);
  341. count:=pdword(p^)^;
  342. end
  343. else
  344. Count:=0;
  345. { later, we can implement a binary search here }
  346. for i:=0 to count-1 do
  347. begin
  348. if index=msgtable[i].index then
  349. begin
  350. p:=msgtable[i].method;
  351. tmessagehandlerrec(msghandler).proc:=p;
  352. tmessagehandlerrec(msghandler).obj:=self;
  353. msghandler(message);
  354. exit;
  355. end;
  356. end;
  357. vmt:=pclass(pointer(vmt)+vmtParent)^;
  358. end;
  359. DefaultHandler(message);
  360. end;
  361. procedure TObject.DispatchStr(var message);
  362. type
  363. PSizeUInt = ^SizeUInt;
  364. var
  365. name : shortstring;
  366. count,i : longint;
  367. msgstrtable : pmsgstrtable;
  368. p : pointer;
  369. vmt : tclass;
  370. msghandler : tmessagehandler;
  371. begin
  372. name:=pshortstring(@message)^;
  373. vmt:=ClassType;
  374. while assigned(vmt) do
  375. begin
  376. p:=(pointer(vmt)+vmtMsgStrPtr);
  377. If (P<>Nil) and (PDWord(P)^<>0) then
  378. begin
  379. count:=pdword(PSizeUInt(p)^)^;
  380. msgstrtable:=pmsgstrtable(PSizeUInt(P)^+4);
  381. end
  382. else
  383. Count:=0;
  384. { later, we can implement a binary search here }
  385. for i:=0 to count-1 do
  386. begin
  387. if name=msgstrtable[i].name^ then
  388. begin
  389. p:=msgstrtable[i].method;
  390. tmessagehandlerrec(msghandler).proc:=p;
  391. tmessagehandlerrec(msghandler).obj:=self;
  392. msghandler(message);
  393. exit;
  394. end;
  395. end;
  396. vmt:=pclass(pointer(vmt)+vmtParent)^;
  397. end;
  398. DefaultHandlerStr(message);
  399. end;
  400. procedure TObject.DefaultHandler(var message);
  401. begin
  402. end;
  403. procedure TObject.DefaultHandlerStr(var message);
  404. begin
  405. end;
  406. procedure TObject.CleanupInstance;
  407. Type
  408. TRecElem = packed Record
  409. Info : Pointer;
  410. Offset : Longint;
  411. end;
  412. TRecElemArray = packed array[1..Maxint] of TRecElem;
  413. PRecRec = ^TRecRec;
  414. TRecRec = record
  415. Size,Count : Longint;
  416. Elements : TRecElemArray;
  417. end;
  418. var
  419. vmt : tclass;
  420. temp : pbyte;
  421. count,
  422. i : longint;
  423. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  424. recelem : TRecElem;
  425. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  426. begin
  427. vmt:=ClassType;
  428. while vmt<>nil do
  429. begin
  430. { This need to be included here, because Finalize()
  431. has should support for tkClass }
  432. Temp:=Pointer((Pointer(vmt)+vmtInitTable)^);
  433. if Assigned(Temp) then
  434. begin
  435. inc(Temp);
  436. I:=Temp^;
  437. inc(temp,I+1); // skip name string;
  438. temp:=aligntoptr(temp);
  439. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  440. move(PRecRec(Temp)^.Count,Count,sizeof(Count));
  441. {$else FPC_REQUIRES_PROPER_ALIGNMENT}
  442. Count:=PRecRec(Temp)^.Count; // get element Count
  443. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  444. For I:=1 to count do
  445. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  446. begin
  447. move(PRecRec(Temp)^.elements[I],RecElem,sizeof(RecElem));
  448. With RecElem do
  449. int_Finalize (pointer(self)+Offset,Info);
  450. end;
  451. {$else FPC_REQUIRES_PROPER_ALIGNMENT}
  452. With PRecRec(Temp)^.elements[I] do
  453. int_Finalize (pointer(self)+Offset,Info);
  454. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  455. end;
  456. vmt:=pclass(pointer(vmt)+vmtParent)^;
  457. end;
  458. end;
  459. procedure TObject.AfterConstruction;
  460. begin
  461. end;
  462. procedure TObject.BeforeDestruction;
  463. begin
  464. end;
  465. function IsGUIDEqual(const guid1, guid2: tguid): boolean;
  466. begin
  467. IsGUIDEqual:=
  468. (guid1.D1=guid2.D1) and
  469. (PDWORD(@guid1.D2)^=PDWORD(@guid2.D2)^) and
  470. (PDWORD(@guid1.D4[0])^=PDWORD(@guid2.D4[0])^) and
  471. (PDWORD(@guid1.D4[4])^=PDWORD(@guid2.D4[4])^);
  472. end;
  473. function TObject.getinterface(const iid : tguid;out obj) : boolean;
  474. var
  475. IEntry: pinterfaceentry;
  476. begin
  477. IEntry:=getinterfaceentry(iid);
  478. if Assigned(IEntry) then
  479. begin
  480. Pointer(obj):=Pointer(Self)+IEntry^.IOffset;
  481. if assigned(pointer(obj)) then
  482. iinterface(obj)._AddRef;
  483. getinterface:=True;
  484. end
  485. else
  486. begin
  487. PPointer(@Obj)^:=nil;
  488. getinterface:=False;
  489. end;
  490. end;
  491. function TObject.getinterfacebystr(const iidstr : string;out obj) : boolean;
  492. var
  493. IEntry: pinterfaceentry;
  494. begin
  495. IEntry:=getinterfaceentrybystr(iidstr);
  496. if Assigned(IEntry) then
  497. begin
  498. Pointer(obj):=Pointer(Self)+IEntry^.IOffset;
  499. if assigned(pointer(obj)) then
  500. iinterface(obj)._AddRef;
  501. getinterfacebystr:=True;
  502. end
  503. else
  504. begin
  505. PPointer(@Obj)^:=nil;
  506. getinterfacebystr:=False;
  507. end;
  508. end;
  509. class function TObject.getinterfaceentry(const iid : tguid) : pinterfaceentry;
  510. var
  511. i: integer;
  512. intftable: pinterfacetable;
  513. Res: pinterfaceentry;
  514. begin
  515. getinterfaceentry:=nil;
  516. intftable:=pinterfacetable((pointer(Self)+vmtIntfTable)^);
  517. if assigned(intftable) then begin
  518. i:=intftable^.EntryCount;
  519. Res:=@intftable^.Entries[0];
  520. while (i>0) and
  521. not (assigned(Res^.iid) and IsGUIDEqual(Res^.iid^,iid)) do begin
  522. inc(Res);
  523. dec(i);
  524. end;
  525. if (i>0) then
  526. getinterfaceentry:=Res;
  527. end;
  528. if (getinterfaceentry=nil)and not(classparent=nil) then
  529. getinterfaceentry:=classparent.getinterfaceentry(iid)
  530. end;
  531. class function TObject.getinterfaceentrybystr(const iidstr : string) : pinterfaceentry;
  532. var
  533. i: integer;
  534. intftable: pinterfacetable;
  535. Res: pinterfaceentry;
  536. begin
  537. getinterfaceentrybystr:=nil;
  538. intftable:=getinterfacetable;
  539. if assigned(intftable) then begin
  540. i:=intftable^.EntryCount;
  541. Res:=@intftable^.Entries[0];
  542. while (i>0) and (Res^.iidstr^<>iidstr) do begin
  543. inc(Res);
  544. dec(i);
  545. end;
  546. if (i>0) then
  547. getinterfaceentrybystr:=Res;
  548. end;
  549. if (getinterfaceentrybystr=nil)and not(classparent=nil) then
  550. getinterfaceentrybystr:=classparent.getinterfaceentrybystr(iidstr)
  551. end;
  552. class function TObject.getinterfacetable : pinterfacetable;
  553. begin
  554. getinterfacetable:=pinterfacetable((pointer(Self)+vmtIntfTable)^);
  555. end;
  556. {****************************************************************************
  557. TINTERFACEDOBJECT
  558. ****************************************************************************}
  559. function TInterfacedObject.QueryInterface(
  560. const iid : tguid;out obj) : longint;stdcall;
  561. begin
  562. if getinterface(iid,obj) then
  563. result:=0
  564. else
  565. result:=longint($80004002);
  566. end;
  567. function TInterfacedObject._AddRef : longint;stdcall;
  568. begin
  569. inclocked(frefcount);
  570. _addref:=frefcount;
  571. end;
  572. function TInterfacedObject._Release : longint;stdcall;
  573. begin
  574. if declocked(frefcount) then
  575. begin
  576. self.destroy;
  577. _Release:=0;
  578. end
  579. else
  580. _Release:=frefcount;
  581. end;
  582. procedure TInterfacedObject.AfterConstruction;
  583. begin
  584. { we need to fix the refcount we forced in newinstance }
  585. { further, it must be done in a thread safe way }
  586. declocked(frefcount);
  587. end;
  588. procedure TInterfacedObject.BeforeDestruction;
  589. begin
  590. if frefcount<>0 then
  591. HandleError(204);
  592. end;
  593. class function TInterfacedObject.NewInstance : TObject;
  594. begin
  595. NewInstance:=inherited NewInstance;
  596. TInterfacedObject(NewInstance).frefcount:=1;
  597. end;
  598. {****************************************************************************
  599. Exception Support
  600. ****************************************************************************}
  601. {$i except.inc}
  602. {****************************************************************************
  603. Initialize
  604. ****************************************************************************}