nobj.pas 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Routines for the code generation of data structures
  5. like VMT, Messages, VTables, Interfaces descs
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit nobj;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. {$ifdef Delphi}
  24. dmisc,
  25. {$endif}
  26. cutils,cclasses,
  27. globtype,
  28. symdef,symsym,
  29. aasmbase,aasmtai,
  30. cpuinfo
  31. ;
  32. type
  33. pprocdeftree = ^tprocdeftree;
  34. tprocdeftree = record
  35. data : tprocdef;
  36. nl : tasmlabel;
  37. l,r : pprocdeftree;
  38. end;
  39. pprocdefcoll = ^tprocdefcoll;
  40. tprocdefcoll = record
  41. data : tprocdef;
  42. hidden : boolean;
  43. visible : boolean;
  44. next : pprocdefcoll;
  45. end;
  46. pvmtentry = ^tvmtentry;
  47. tvmtentry = record
  48. speedvalue : cardinal;
  49. name : pstring;
  50. firstprocdef : pprocdefcoll;
  51. next : pvmtentry;
  52. end;
  53. tclassheader=class
  54. private
  55. _Class : tobjectdef;
  56. count : integer;
  57. private
  58. { message tables }
  59. root : pprocdeftree;
  60. procedure disposeprocdeftree(p : pprocdeftree);
  61. procedure insertmsgint(p : tnamedindexitem;arg:pointer);
  62. procedure insertmsgstr(p : tnamedindexitem;arg:pointer);
  63. procedure insertint(p : pprocdeftree;var at : pprocdeftree);
  64. procedure insertstr(p : pprocdeftree;var at : pprocdeftree);
  65. procedure writenames(p : pprocdeftree);
  66. procedure writeintentry(p : pprocdeftree);
  67. procedure writestrentry(p : pprocdeftree);
  68. {$ifdef WITHDMT}
  69. private
  70. { dmt }
  71. procedure insertdmtentry(p : tnamedindexitem;arg:pointer);
  72. procedure writedmtindexentry(p : pprocdeftree);
  73. procedure writedmtaddressentry(p : pprocdeftree);
  74. {$endif}
  75. private
  76. { published methods }
  77. procedure do_count(p : tnamedindexitem;arg:pointer);
  78. procedure genpubmethodtableentry(p : tnamedindexitem;arg:pointer);
  79. private
  80. { vmt }
  81. firstvmtentry : pvmtentry;
  82. nextvirtnumber : integer;
  83. has_constructor,
  84. has_virtual_method : boolean;
  85. procedure newdefentry(vmtentry:pvmtentry;pd:tprocdef;is_visible:boolean);
  86. function newvmtentry(sym:tprocsym):pvmtentry;
  87. procedure eachsym(sym : tnamedindexitem;arg:pointer);
  88. procedure disposevmttree;
  89. procedure writevirtualmethods(List:TAAsmoutput);
  90. private
  91. { interface tables }
  92. function gintfgetvtbllabelname(intfindex: integer): string;
  93. procedure gintfcreatevtbl(intfindex: integer; rawdata,rawcode: TAAsmoutput);
  94. procedure gintfgenentry(intfindex, contintfindex: integer; rawdata: TAAsmoutput);
  95. procedure gintfoptimizevtbls(implvtbl : plongintarray);
  96. procedure gintfwritedata;
  97. function gintfgetcprocdef(proc: tprocdef;const name: string): tprocdef;
  98. procedure gintfdoonintf(intf: tobjectdef; intfindex: longint);
  99. procedure gintfwalkdowninterface(intf: tobjectdef; intfindex: longint);
  100. protected
  101. { adjusts the self value with ioffset when casting a interface
  102. to a class
  103. }
  104. procedure adjustselfvalue(procdef: tprocdef;ioffset: aint);virtual;
  105. { generates the wrapper for a call to a method via an interface }
  106. procedure cgintfwrapper(asmlist: TAAsmoutput; procdef: tprocdef; const labelname: string; ioffset: longint);virtual;abstract;
  107. public
  108. constructor create(c:tobjectdef);
  109. destructor destroy;override;
  110. { generates the message tables for a class }
  111. function genstrmsgtab : tasmlabel;
  112. function genintmsgtab : tasmlabel;
  113. function genpublishedmethodstable : tasmlabel;
  114. { generates a VMT entries }
  115. procedure genvmt;
  116. {$ifdef WITHDMT}
  117. { generates a DMT for _class }
  118. function gendmt : tasmlabel;
  119. {$endif WITHDMT}
  120. { interfaces }
  121. function genintftable: tasmlabel;
  122. { write the VMT to datasegment }
  123. procedure writevmt;
  124. procedure writeinterfaceids;
  125. end;
  126. tclassheaderclass=class of tclassheader;
  127. var
  128. cclassheader : tclassheaderclass;
  129. implementation
  130. uses
  131. {$ifdef delphi}
  132. sysutils,
  133. {$else}
  134. strings,
  135. {$endif}
  136. globals,verbose,systems,
  137. symtable,symconst,symtype,defcmp,paramgr,
  138. {$ifdef GDB}
  139. gdb,
  140. {$endif GDB}
  141. aasmcpu,
  142. cpubase,cgbase,parabase,
  143. cgutils,cgobj
  144. ;
  145. {*****************************************************************************
  146. TClassHeader
  147. *****************************************************************************}
  148. constructor tclassheader.create(c:tobjectdef);
  149. begin
  150. inherited Create;
  151. _Class:=c;
  152. end;
  153. destructor tclassheader.destroy;
  154. begin
  155. disposevmttree;
  156. end;
  157. {**************************************
  158. Message Tables
  159. **************************************}
  160. procedure tclassheader.disposeprocdeftree(p : pprocdeftree);
  161. begin
  162. if assigned(p^.l) then
  163. disposeprocdeftree(p^.l);
  164. if assigned(p^.r) then
  165. disposeprocdeftree(p^.r);
  166. dispose(p);
  167. end;
  168. procedure tclassheader.insertint(p : pprocdeftree;var at : pprocdeftree);
  169. begin
  170. if at=nil then
  171. begin
  172. at:=p;
  173. inc(count);
  174. end
  175. else
  176. begin
  177. if p^.data.messageinf.i<at^.data.messageinf.i then
  178. insertint(p,at^.l)
  179. else if p^.data.messageinf.i>at^.data.messageinf.i then
  180. insertint(p,at^.r)
  181. else
  182. Message1(parser_e_duplicate_message_label,tostr(p^.data.messageinf.i));
  183. end;
  184. end;
  185. procedure tclassheader.insertstr(p : pprocdeftree;var at : pprocdeftree);
  186. var
  187. i : integer;
  188. begin
  189. if at=nil then
  190. begin
  191. at:=p;
  192. inc(count);
  193. end
  194. else
  195. begin
  196. i:=strcomp(p^.data.messageinf.str,at^.data.messageinf.str);
  197. if i<0 then
  198. insertstr(p,at^.l)
  199. else if i>0 then
  200. insertstr(p,at^.r)
  201. else
  202. Message1(parser_e_duplicate_message_label,strpas(p^.data.messageinf.str));
  203. end;
  204. end;
  205. procedure tclassheader.insertmsgint(p : tnamedindexitem;arg:pointer);
  206. var
  207. i : cardinal;
  208. def: Tprocdef;
  209. pt : pprocdeftree;
  210. begin
  211. if tsym(p).typ=procsym then
  212. for i:=1 to Tprocsym(p).procdef_count do
  213. begin
  214. def:=Tprocsym(p).procdef[i];
  215. if po_msgint in def.procoptions then
  216. begin
  217. new(pt);
  218. pt^.data:=def;
  219. pt^.l:=nil;
  220. pt^.r:=nil;
  221. insertint(pt,root);
  222. end;
  223. end;
  224. end;
  225. procedure tclassheader.insertmsgstr(p : tnamedindexitem;arg:pointer);
  226. var
  227. i : cardinal;
  228. def: Tprocdef;
  229. pt : pprocdeftree;
  230. begin
  231. if tsym(p).typ=procsym then
  232. for i:=1 to Tprocsym(p).procdef_count do
  233. begin
  234. def:=Tprocsym(p).procdef[i];
  235. if po_msgstr in def.procoptions then
  236. begin
  237. new(pt);
  238. pt^.data:=def;
  239. pt^.l:=nil;
  240. pt^.r:=nil;
  241. insertstr(pt,root);
  242. end;
  243. end;
  244. end;
  245. procedure tclassheader.writenames(p : pprocdeftree);
  246. var
  247. ca : pchar;
  248. len : longint;
  249. begin
  250. objectlibrary.getdatalabel(p^.nl);
  251. if assigned(p^.l) then
  252. writenames(p^.l);
  253. datasegment.concat(tai_align.create(const_align(sizeof(aint))));
  254. dataSegment.concat(Tai_label.Create(p^.nl));
  255. len:=strlen(p^.data.messageinf.str);
  256. datasegment.concat(tai_const.create_8bit(len));
  257. getmem(ca,len+1);
  258. move(p^.data.messageinf.str^,ca^,len+1);
  259. dataSegment.concat(Tai_string.Create_pchar(ca));
  260. if assigned(p^.r) then
  261. writenames(p^.r);
  262. end;
  263. procedure tclassheader.writestrentry(p : pprocdeftree);
  264. begin
  265. if assigned(p^.l) then
  266. writestrentry(p^.l);
  267. { write name label }
  268. dataSegment.concat(Tai_const.Create_sym(p^.nl));
  269. dataSegment.concat(Tai_const.Createname(p^.data.mangledname,AT_FUNCTION,0));
  270. if assigned(p^.r) then
  271. writestrentry(p^.r);
  272. end;
  273. function tclassheader.genstrmsgtab : tasmlabel;
  274. var
  275. r : tasmlabel;
  276. begin
  277. root:=nil;
  278. count:=0;
  279. { insert all message handlers into a tree, sorted by name }
  280. _class.symtable.foreach({$ifdef FPCPROCVAR}@{$endif}insertmsgstr,nil);
  281. { write all names }
  282. if assigned(root) then
  283. writenames(root);
  284. { now start writing of the message string table }
  285. objectlibrary.getdatalabel(r);
  286. datasegment.concat(tai_align.create(const_align(sizeof(aint))));
  287. dataSegment.concat(Tai_label.Create(r));
  288. genstrmsgtab:=r;
  289. dataSegment.concat(Tai_const.Create_32bit(count));
  290. if assigned(root) then
  291. begin
  292. writestrentry(root);
  293. disposeprocdeftree(root);
  294. end;
  295. end;
  296. procedure tclassheader.writeintentry(p : pprocdeftree);
  297. begin
  298. if assigned(p^.l) then
  299. writeintentry(p^.l);
  300. { write name label }
  301. dataSegment.concat(Tai_const.Create_32bit(p^.data.messageinf.i));
  302. dataSegment.concat(Tai_const.Createname(p^.data.mangledname,AT_FUNCTION,0));
  303. if assigned(p^.r) then
  304. writeintentry(p^.r);
  305. end;
  306. function tclassheader.genintmsgtab : tasmlabel;
  307. var
  308. r : tasmlabel;
  309. begin
  310. root:=nil;
  311. count:=0;
  312. { insert all message handlers into a tree, sorted by name }
  313. _class.symtable.foreach({$ifdef FPCPROCVAR}@{$endif}insertmsgint,nil);
  314. { now start writing of the message string table }
  315. objectlibrary.getdatalabel(r);
  316. datasegment.concat(tai_align.create(const_align(sizeof(aint))));
  317. dataSegment.concat(Tai_label.Create(r));
  318. genintmsgtab:=r;
  319. dataSegment.concat(Tai_const.Create_32bit(count));
  320. if assigned(root) then
  321. begin
  322. writeintentry(root);
  323. disposeprocdeftree(root);
  324. end;
  325. end;
  326. {$ifdef WITHDMT}
  327. {**************************************
  328. DMT
  329. **************************************}
  330. procedure tclassheader.insertdmtentry(p : tnamedindexitem;arg:pointer);
  331. var
  332. hp : tprocdef;
  333. pt : pprocdeftree;
  334. begin
  335. if tsym(p).typ=procsym then
  336. begin
  337. hp:=tprocsym(p).definition;
  338. while assigned(hp) do
  339. begin
  340. if (po_msgint in hp.procoptions) then
  341. begin
  342. new(pt);
  343. pt^.p:=hp;
  344. pt^.l:=nil;
  345. pt^.r:=nil;
  346. insertint(pt,root);
  347. end;
  348. hp:=hp.nextoverloaded;
  349. end;
  350. end;
  351. end;
  352. procedure tclassheader.writedmtindexentry(p : pprocdeftree);
  353. begin
  354. if assigned(p^.l) then
  355. writedmtindexentry(p^.l);
  356. dataSegment.concat(Tai_const.Create_32bit(p^.data.messageinf.i));
  357. if assigned(p^.r) then
  358. writedmtindexentry(p^.r);
  359. end;
  360. procedure tclassheader.writedmtaddressentry(p : pprocdeftree);
  361. begin
  362. if assigned(p^.l) then
  363. writedmtaddressentry(p^.l);
  364. dataSegment.concat(Tai_const_symbol.Createname(p^.data.mangledname,AT_FUNCTION,0));
  365. if assigned(p^.r) then
  366. writedmtaddressentry(p^.r);
  367. end;
  368. function tclassheader.gendmt : tasmlabel;
  369. var
  370. r : tasmlabel;
  371. begin
  372. root:=nil;
  373. count:=0;
  374. gendmt:=nil;
  375. { insert all message handlers into a tree, sorted by number }
  376. _class.symtable.foreach({$ifdef FPCPROCVAR}@{$endif}insertdmtentry);
  377. if count>0 then
  378. begin
  379. objectlibrary.getdatalabel(r);
  380. gendmt:=r;
  381. datasegment.concat(tai_align.create(const_align(sizeof(aint))));
  382. dataSegment.concat(Tai_label.Create(r));
  383. { entries for caching }
  384. dataSegment.concat(Tai_const.Create_ptr(0));
  385. dataSegment.concat(Tai_const.Create_ptr(0));
  386. dataSegment.concat(Tai_const.Create_32bit(count));
  387. if assigned(root) then
  388. begin
  389. writedmtindexentry(root);
  390. writedmtaddressentry(root);
  391. disposeprocdeftree(root);
  392. end;
  393. end;
  394. end;
  395. {$endif WITHDMT}
  396. {**************************************
  397. Published Methods
  398. **************************************}
  399. procedure tclassheader.do_count(p : tnamedindexitem;arg:pointer);
  400. begin
  401. if (tsym(p).typ=procsym) and (sp_published in tsym(p).symoptions) then
  402. inc(count);
  403. end;
  404. procedure tclassheader.genpubmethodtableentry(p : tnamedindexitem;arg:pointer);
  405. var
  406. hp : tprocdef;
  407. l : tasmlabel;
  408. begin
  409. if (tsym(p).typ=procsym) and (sp_published in tsym(p).symoptions) then
  410. begin
  411. if Tprocsym(p).procdef_count>1 then
  412. internalerror(1209992);
  413. hp:=tprocsym(p).first_procdef;
  414. objectlibrary.getdatalabel(l);
  415. consts.concat(tai_align.create(const_align(sizeof(aint))));
  416. Consts.concat(Tai_label.Create(l));
  417. Consts.concat(Tai_const.Create_8bit(length(p.name)));
  418. Consts.concat(Tai_string.Create(p.name));
  419. dataSegment.concat(Tai_const.Create_sym(l));
  420. dataSegment.concat(Tai_const.Createname(hp.mangledname,AT_FUNCTION,0));
  421. end;
  422. end;
  423. function tclassheader.genpublishedmethodstable : tasmlabel;
  424. var
  425. l : tasmlabel;
  426. begin
  427. count:=0;
  428. _class.symtable.foreach({$ifdef FPCPROCVAR}@{$endif}do_count,nil);
  429. if count>0 then
  430. begin
  431. objectlibrary.getdatalabel(l);
  432. datasegment.concat(tai_align.create(const_align(sizeof(aint))));
  433. dataSegment.concat(Tai_label.Create(l));
  434. dataSegment.concat(Tai_const.Create_32bit(count));
  435. _class.symtable.foreach({$ifdef FPCPROCVAR}@{$endif}genpubmethodtableentry,nil);
  436. genpublishedmethodstable:=l;
  437. end
  438. else
  439. genpublishedmethodstable:=nil;
  440. end;
  441. {**************************************
  442. VMT
  443. **************************************}
  444. procedure tclassheader.newdefentry(vmtentry:pvmtentry;pd:tprocdef;is_visible:boolean);
  445. var
  446. procdefcoll : pprocdefcoll;
  447. begin
  448. { generate new entry }
  449. new(procdefcoll);
  450. procdefcoll^.data:=pd;
  451. procdefcoll^.hidden:=false;
  452. procdefcoll^.visible:=is_visible;
  453. procdefcoll^.next:=vmtentry^.firstprocdef;
  454. vmtentry^.firstprocdef:=procdefcoll;
  455. { give virtual method a number }
  456. if (po_virtualmethod in pd.procoptions) then
  457. begin
  458. pd.extnumber:=nextvirtnumber;
  459. inc(nextvirtnumber);
  460. has_virtual_method:=true;
  461. end;
  462. if (pd.proctypeoption=potype_constructor) then
  463. has_constructor:=true;
  464. end;
  465. function tclassheader.newvmtentry(sym:tprocsym):pvmtentry;
  466. begin
  467. { generate new vmtentry }
  468. new(result);
  469. result^.speedvalue:=sym.speedvalue;
  470. result^.name:=stringdup(sym.name);
  471. result^.next:=firstvmtentry;
  472. result^.firstprocdef:=nil;
  473. firstvmtentry:=result;
  474. end;
  475. procedure tclassheader.eachsym(sym : tnamedindexitem;arg:pointer);
  476. label
  477. handlenextdef;
  478. var
  479. pd : tprocdef;
  480. i : cardinal;
  481. is_visible,
  482. hasoverloads,
  483. pdoverload : boolean;
  484. procdefcoll : pprocdefcoll;
  485. vmtentry : pvmtentry;
  486. _name : string;
  487. _speed : cardinal;
  488. begin
  489. if (tsym(sym).typ<>procsym) then
  490. exit;
  491. { check the current list of symbols }
  492. _name:=sym.name;
  493. _speed:=sym.speedvalue;
  494. vmtentry:=firstvmtentry;
  495. while assigned(vmtentry) do
  496. begin
  497. { does the symbol already exist in the list? First
  498. compare speedvalue before doing the string compare to
  499. speed it up a little }
  500. if (_speed=vmtentry^.speedvalue) and
  501. (_name=vmtentry^.name^) then
  502. begin
  503. hasoverloads:=(Tprocsym(sym).procdef_count>1);
  504. { walk through all defs of the symbol }
  505. for i:=1 to Tprocsym(sym).procdef_count do
  506. begin
  507. pd:=Tprocsym(sym).procdef[i];
  508. { is this procdef visible from the class that we are
  509. generating. This will be used to hide the other procdefs.
  510. When the symbol is not visible we don't hide the other
  511. procdefs, because they can be reused in the next class.
  512. The check to skip the invisible methods that are in the
  513. list is futher down in the code }
  514. is_visible:=pd.is_visible_for_object(_class);
  515. if pd.procsym=sym then
  516. begin
  517. pdoverload:=(po_overload in pd.procoptions);
  518. { compare with all stored definitions }
  519. procdefcoll:=vmtentry^.firstprocdef;
  520. while assigned(procdefcoll) do
  521. begin
  522. { compare only if the definition is not hidden }
  523. if not procdefcoll^.hidden then
  524. begin
  525. { check that all methods have overload directive }
  526. if not(m_fpc in aktmodeswitches) and
  527. (_class=pd._class) and
  528. (procdefcoll^.data._class=pd._class) and
  529. ((po_overload in pd.procoptions)<>(po_overload in procdefcoll^.data.procoptions)) then
  530. begin
  531. MessagePos1(pd.fileinfo,parser_e_no_overload_for_all_procs,pd.procsym.realname);
  532. { recover }
  533. include(procdefcoll^.data.procoptions,po_overload);
  534. include(pd.procoptions,po_overload);
  535. end;
  536. { check if one of the two methods has virtual }
  537. if (po_virtualmethod in procdefcoll^.data.procoptions) or
  538. (po_virtualmethod in pd.procoptions) then
  539. begin
  540. { if the current definition has no virtual then hide the
  541. old virtual if the new definition has the same arguments or
  542. when it has no overload directive and no overloads }
  543. if not(po_virtualmethod in pd.procoptions) then
  544. begin
  545. if procdefcoll^.visible and
  546. (not(pdoverload or hasoverloads) or
  547. (compare_paras(procdefcoll^.data.para,pd.para,cp_all,[])>=te_equal)) then
  548. begin
  549. if is_visible then
  550. procdefcoll^.hidden:=true;
  551. if (_class=pd._class) and not(po_reintroduce in pd.procoptions) then
  552. MessagePos1(pd.fileinfo,parser_w_should_use_override,pd.fullprocname(false));
  553. end;
  554. end
  555. { if both are virtual we check the header }
  556. else if (po_virtualmethod in pd.procoptions) and
  557. (po_virtualmethod in procdefcoll^.data.procoptions) then
  558. begin
  559. { new one has not override }
  560. if is_class(_class) and
  561. not(po_overridingmethod in pd.procoptions) then
  562. begin
  563. { we start a new virtual tree, hide the old }
  564. if (not(pdoverload or hasoverloads) or
  565. (compare_paras(procdefcoll^.data.para,pd.para,cp_all,[])>=te_equal)) and
  566. (procdefcoll^.visible) then
  567. begin
  568. if is_visible then
  569. procdefcoll^.hidden:=true;
  570. if (_class=pd._class) and not(po_reintroduce in pd.procoptions) then
  571. MessagePos1(pd.fileinfo,parser_w_should_use_override,pd.fullprocname(false));
  572. end;
  573. end
  574. { same parameters }
  575. else if (compare_paras(procdefcoll^.data.para,pd.para,cp_all,[])>=te_equal) then
  576. begin
  577. { overload is inherited }
  578. if (po_overload in procdefcoll^.data.procoptions) then
  579. include(pd.procoptions,po_overload);
  580. { inherite calling convention when it was force and the
  581. current definition has none force }
  582. if (po_hascallingconvention in procdefcoll^.data.procoptions) and
  583. not(po_hascallingconvention in pd.procoptions) then
  584. begin
  585. pd.proccalloption:=procdefcoll^.data.proccalloption;
  586. include(pd.procoptions,po_hascallingconvention);
  587. end;
  588. { the flags have to match except abstract and override }
  589. { only if both are virtual !! }
  590. if (procdefcoll^.data.proccalloption<>pd.proccalloption) or
  591. (procdefcoll^.data.proctypeoption<>pd.proctypeoption) or
  592. ((procdefcoll^.data.procoptions-
  593. [po_abstractmethod,po_overridingmethod,po_assembler,po_overload,po_public,po_reintroduce])<>
  594. (pd.procoptions-[po_abstractmethod,po_overridingmethod,po_assembler,po_overload,po_public,po_reintroduce])) then
  595. begin
  596. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,pd.fullprocname(false));
  597. tprocsym(procdefcoll^.data.procsym).write_parameter_lists(pd);
  598. end;
  599. { error, if the return types aren't equal }
  600. if not(equal_defs(procdefcoll^.data.rettype.def,pd.rettype.def)) and
  601. not((procdefcoll^.data.rettype.def.deftype=objectdef) and
  602. (pd.rettype.def.deftype=objectdef) and
  603. is_class(procdefcoll^.data.rettype.def) and
  604. is_class(pd.rettype.def) and
  605. (tobjectdef(pd.rettype.def).is_related(
  606. tobjectdef(procdefcoll^.data.rettype.def)))) then
  607. Message2(parser_e_overridden_methods_not_same_ret,pd.fullprocname(false),
  608. procdefcoll^.data.fullprocname(false));
  609. { check if the method to override is visible, check is only needed
  610. for the current parsed class. Parent classes are already validated and
  611. need to include all virtual methods including the ones not visible in the
  612. current class }
  613. if (_class=pd._class) and
  614. (po_overridingmethod in pd.procoptions) and
  615. (not procdefcoll^.visible) then
  616. MessagePos1(pd.fileinfo,parser_e_nothing_to_be_overridden,pd.fullprocname(false));
  617. { override old virtual method in VMT }
  618. pd.extnumber:=procdefcoll^.data.extnumber;
  619. procdefcoll^.data:=pd;
  620. if is_visible then
  621. procdefcoll^.visible:=true;
  622. goto handlenextdef;
  623. end
  624. { different parameters }
  625. else
  626. begin
  627. { when we got an override directive then can search futher for
  628. the procedure to override.
  629. If we are starting a new virtual tree then hide the old tree }
  630. if not(po_overridingmethod in pd.procoptions) and
  631. not pdoverload then
  632. begin
  633. if is_visible then
  634. procdefcoll^.hidden:=true;
  635. if (_class=pd._class) and not(po_reintroduce in pd.procoptions) then
  636. MessagePos1(pd.fileinfo,parser_w_should_use_override,pd.fullprocname(false));
  637. end;
  638. end;
  639. end
  640. else
  641. begin
  642. { the new definition is virtual and the old static, we hide the old one
  643. if the new defintion has not the overload directive }
  644. if is_visible and
  645. ((not(pdoverload or hasoverloads)) or
  646. (compare_paras(procdefcoll^.data.para,pd.para,cp_all,[])>=te_equal)) then
  647. procdefcoll^.hidden:=true;
  648. end;
  649. end
  650. else
  651. begin
  652. { both are static, we hide the old one if the new defintion
  653. has not the overload directive }
  654. if is_visible and
  655. ((not pdoverload) or
  656. (compare_paras(procdefcoll^.data.para,pd.para,cp_all,[])>=te_equal)) then
  657. procdefcoll^.hidden:=true;
  658. end;
  659. end; { not hidden }
  660. procdefcoll:=procdefcoll^.next;
  661. end;
  662. { new entry is needed, override was not possible }
  663. if (_class=pd._class) and
  664. (po_overridingmethod in pd.procoptions) then
  665. MessagePos1(pd.fileinfo,parser_e_nothing_to_be_overridden,pd.fullprocname(false));
  666. { if it isn't saved in the list we create a new entry }
  667. newdefentry(vmtentry,pd,is_visible);
  668. end;
  669. handlenextdef:
  670. end;
  671. exit;
  672. end;
  673. vmtentry:=vmtentry^.next;
  674. end;
  675. { Generate new procsym entry in vmt }
  676. vmtentry:=newvmtentry(tprocsym(sym));
  677. { Add procdefs }
  678. for i:=1 to Tprocsym(sym).procdef_count do
  679. begin
  680. pd:=Tprocsym(sym).procdef[i];
  681. { new entry is needed, override was not possible }
  682. if (_class=pd._class) and
  683. (po_overridingmethod in pd.procoptions) then
  684. MessagePos1(pd.fileinfo,parser_e_nothing_to_be_overridden,pd.fullprocname(false));
  685. newdefentry(vmtentry,pd,pd.is_visible_for_object(_class));
  686. end;
  687. end;
  688. procedure tclassheader.disposevmttree;
  689. var
  690. vmtentry : pvmtentry;
  691. procdefcoll : pprocdefcoll;
  692. begin
  693. { disposes the above generated tree }
  694. vmtentry:=firstvmtentry;
  695. while assigned(vmtentry) do
  696. begin
  697. firstvmtentry:=vmtentry^.next;
  698. stringdispose(vmtentry^.name);
  699. procdefcoll:=vmtentry^.firstprocdef;
  700. while assigned(procdefcoll) do
  701. begin
  702. vmtentry^.firstprocdef:=procdefcoll^.next;
  703. dispose(procdefcoll);
  704. procdefcoll:=vmtentry^.firstprocdef;
  705. end;
  706. dispose(vmtentry);
  707. vmtentry:=firstvmtentry;
  708. end;
  709. end;
  710. procedure tclassheader.genvmt;
  711. procedure do_genvmt(p : tobjectdef);
  712. begin
  713. { start with the base class }
  714. if assigned(p.childof) then
  715. do_genvmt(p.childof);
  716. { walk through all public syms }
  717. p.symtable.foreach({$ifdef FPCPROCVAR}@{$endif}eachsym,nil);
  718. end;
  719. begin
  720. firstvmtentry:=nil;
  721. nextvirtnumber:=0;
  722. has_constructor:=false;
  723. has_virtual_method:=false;
  724. { generates a tree of all used methods }
  725. do_genvmt(_class);
  726. if not(is_interface(_class)) and
  727. has_virtual_method and
  728. not(has_constructor) then
  729. Message1(parser_w_virtual_without_constructor,_class.objrealname^);
  730. end;
  731. {**************************************
  732. Interface tables
  733. **************************************}
  734. function tclassheader.gintfgetvtbllabelname(intfindex: integer): string;
  735. begin
  736. gintfgetvtbllabelname:=make_mangledname('VTBL',_class.owner,_class.objname^+
  737. '_$_'+_class.implementedinterfaces.interfaces(intfindex).objname^);
  738. end;
  739. procedure tclassheader.gintfcreatevtbl(intfindex: integer; rawdata,rawcode: TAAsmoutput);
  740. var
  741. implintf: timplementedinterfaces;
  742. curintf: tobjectdef;
  743. proccount: integer;
  744. tmps: string;
  745. i: longint;
  746. begin
  747. implintf:=_class.implementedinterfaces;
  748. curintf:=implintf.interfaces(intfindex);
  749. rawdata.concat(tai_align.create(const_align(sizeof(aint))));
  750. if maybe_smartlink_symbol then
  751. rawdata.concat(Tai_symbol.Createname_global(gintfgetvtbllabelname(intfindex),AT_DATA ,0))
  752. else
  753. rawdata.concat(Tai_symbol.Createname(gintfgetvtbllabelname(intfindex),AT_DATA,0));
  754. proccount:=implintf.implproccount(intfindex);
  755. for i:=1 to proccount do
  756. begin
  757. tmps:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+curintf.objname^+'_$_'+
  758. tostr(i)+'_$_'+
  759. implintf.implprocs(intfindex,i).mangledname);
  760. { create wrapper code }
  761. cgintfwrapper(rawcode,implintf.implprocs(intfindex,i),tmps,implintf.ioffsets(intfindex)^);
  762. { create reference }
  763. rawdata.concat(Tai_const.Createname(tmps,AT_FUNCTION,0));
  764. end;
  765. end;
  766. procedure tclassheader.gintfgenentry(intfindex, contintfindex: integer; rawdata: TAAsmoutput);
  767. var
  768. implintf: timplementedinterfaces;
  769. curintf: tobjectdef;
  770. tmplabel: tasmlabel;
  771. i: longint;
  772. begin
  773. implintf:=_class.implementedinterfaces;
  774. curintf:=implintf.interfaces(intfindex);
  775. { GUID }
  776. if curintf.objecttype in [odt_interfacecom] then
  777. begin
  778. { label for GUID }
  779. objectlibrary.getdatalabel(tmplabel);
  780. rawdata.concat(tai_align.create(const_align(sizeof(aint))));
  781. rawdata.concat(Tai_label.Create(tmplabel));
  782. rawdata.concat(Tai_const.Create_32bit(longint(curintf.iidguid^.D1)));
  783. rawdata.concat(Tai_const.Create_16bit(curintf.iidguid^.D2));
  784. rawdata.concat(Tai_const.Create_16bit(curintf.iidguid^.D3));
  785. for i:=Low(curintf.iidguid^.D4) to High(curintf.iidguid^.D4) do
  786. rawdata.concat(Tai_const.Create_8bit(curintf.iidguid^.D4[i]));
  787. dataSegment.concat(Tai_const.Create_sym(tmplabel));
  788. end
  789. else
  790. begin
  791. { nil for Corba interfaces }
  792. dataSegment.concat(Tai_const.Create_sym(nil));
  793. end;
  794. { VTable }
  795. dataSegment.concat(Tai_const.Createname(gintfgetvtbllabelname(contintfindex),AT_DATA,0));
  796. { IOffset field }
  797. dataSegment.concat(Tai_const.Create_32bit(implintf.ioffsets(contintfindex)^));
  798. { IIDStr }
  799. objectlibrary.getdatalabel(tmplabel);
  800. rawdata.concat(tai_align.create(const_align(sizeof(aint))));
  801. rawdata.concat(Tai_label.Create(tmplabel));
  802. rawdata.concat(Tai_const.Create_8bit(length(curintf.iidstr^)));
  803. if curintf.objecttype=odt_interfacecom then
  804. rawdata.concat(Tai_string.Create(upper(curintf.iidstr^)))
  805. else
  806. rawdata.concat(Tai_string.Create(curintf.iidstr^));
  807. dataSegment.concat(Tai_const.Create_sym(tmplabel));
  808. end;
  809. procedure tclassheader.gintfoptimizevtbls(implvtbl : plongintarray);
  810. type
  811. tcompintfentry = record
  812. weight: longint;
  813. compintf: longint;
  814. end;
  815. { Max 1000 interface in the class header interfaces it's enough imho }
  816. tcompintfs = packed array[1..1000] of tcompintfentry;
  817. pcompintfs = ^tcompintfs;
  818. tequals = packed array[1..1000] of longint;
  819. pequals = ^tequals;
  820. var
  821. max: longint;
  822. equals: pequals;
  823. compats: pcompintfs;
  824. i: longint;
  825. j: longint;
  826. w: longint;
  827. cij: boolean;
  828. cji: boolean;
  829. begin
  830. max:=_class.implementedinterfaces.count;
  831. if max>High(tequals) then
  832. Internalerror(200006135);
  833. getmem(compats,sizeof(tcompintfentry)*max);
  834. getmem(equals,sizeof(longint)*max);
  835. fillchar(compats^,sizeof(tcompintfentry)*max,0);
  836. fillchar(equals^,sizeof(longint)*max,0);
  837. { ismergepossible is a containing relation
  838. meaning of ismergepossible(a,b,w) =
  839. if implementorfunction map of a is contained implementorfunction map of b
  840. imp(a,b) and imp(b,c) => imp(a,c) ; imp(a,b) and imp(b,a) => a == b
  841. }
  842. { the order is very important for correct allocation }
  843. for i:=1 to max do
  844. begin
  845. for j:=i+1 to max do
  846. begin
  847. cij:=_class.implementedinterfaces.isimplmergepossible(i,j,w);
  848. cji:=_class.implementedinterfaces.isimplmergepossible(j,i,w);
  849. if cij and cji then { i equal j }
  850. begin
  851. { get minimum index of equal }
  852. if equals^[j]=0 then
  853. equals^[j]:=i;
  854. end
  855. else if cij then
  856. begin
  857. { get minimum index of maximum weight }
  858. if compats^[i].weight<w then
  859. begin
  860. compats^[i].weight:=w;
  861. compats^[i].compintf:=j;
  862. end;
  863. end
  864. else if cji then
  865. begin
  866. { get minimum index of maximum weight }
  867. if (compats^[j].weight<w) then
  868. begin
  869. compats^[j].weight:=w;
  870. compats^[j].compintf:=i;
  871. end;
  872. end;
  873. end;
  874. end;
  875. for i:=1 to max do
  876. begin
  877. if compats^[i].compintf<>0 then
  878. implvtbl[i]:=compats^[i].compintf
  879. else if equals^[i]<>0 then
  880. implvtbl[i]:=equals^[i]
  881. else
  882. implvtbl[i]:=i;
  883. end;
  884. freemem(compats,sizeof(tcompintfentry)*max);
  885. freemem(equals,sizeof(longint)*max);
  886. end;
  887. procedure tclassheader.gintfwritedata;
  888. var
  889. rawdata,rawcode: taasmoutput;
  890. impintfindexes: plongintarray;
  891. max: longint;
  892. i: longint;
  893. begin
  894. max:=_class.implementedinterfaces.count;
  895. getmem(impintfindexes,(max+1)*sizeof(longint));
  896. gintfoptimizevtbls(impintfindexes);
  897. rawdata:=TAAsmOutput.Create;
  898. rawcode:=TAAsmOutput.Create;
  899. dataSegment.concat(Tai_const.Create_16bit(max));
  900. { Two pass, one for allocation and vtbl creation }
  901. for i:=1 to max do
  902. begin
  903. if impintfindexes[i]=i then { if implement itself }
  904. begin
  905. { allocate a pointer in the object memory }
  906. with tobjectsymtable(_class.symtable) do
  907. begin
  908. datasize:=align(datasize,min(sizeof(aint),fieldalignment));
  909. _class.implementedinterfaces.ioffsets(i)^:=datasize;
  910. inc(datasize,sizeof(aint));
  911. end;
  912. { write vtbl }
  913. gintfcreatevtbl(i,rawdata,rawcode);
  914. end;
  915. end;
  916. { second pass: for fill interfacetable and remained ioffsets }
  917. for i:=1 to max do
  918. begin
  919. if i<>impintfindexes[i] then { why execute x:=x ? }
  920. with _class.implementedinterfaces do
  921. ioffsets(i)^:=ioffsets(impintfindexes[i])^;
  922. gintfgenentry(i,impintfindexes[i],rawdata);
  923. end;
  924. dataSegment.concatlist(rawdata);
  925. rawdata.free;
  926. codeSegment.concatlist(rawcode);
  927. rawcode.free;
  928. freemem(impintfindexes,(max+1)*sizeof(longint));
  929. end;
  930. function tclassheader.gintfgetcprocdef(proc: tprocdef;const name: string): tprocdef;
  931. var
  932. sym: tsym;
  933. implprocdef : Tprocdef;
  934. i: cardinal;
  935. begin
  936. gintfgetcprocdef:=nil;
  937. sym:=tsym(search_class_member(_class,name));
  938. if assigned(sym) and
  939. (sym.typ=procsym) then
  940. begin
  941. { when the definition has overload directive set, we search for
  942. overloaded definitions in the class, this only needs to be done once
  943. for class entries as the tree keeps always the same }
  944. if (not tprocsym(sym).overloadchecked) and
  945. (po_overload in tprocsym(sym).first_procdef.procoptions) and
  946. (tprocsym(sym).owner.symtabletype=objectsymtable) then
  947. search_class_overloads(tprocsym(sym));
  948. for i:=1 to tprocsym(sym).procdef_count do
  949. begin
  950. implprocdef:=tprocsym(sym).procdef[i];
  951. if (compare_paras(proc.para,implprocdef.para,cp_none,[])>=te_equal) and
  952. (proc.proccalloption=implprocdef.proccalloption) then
  953. begin
  954. gintfgetcprocdef:=implprocdef;
  955. exit;
  956. end;
  957. end;
  958. end;
  959. end;
  960. procedure tclassheader.gintfdoonintf(intf: tobjectdef; intfindex: longint);
  961. var
  962. def: tdef;
  963. procname: string; { for error }
  964. mappedname: string;
  965. nextexist: pointer;
  966. implprocdef: tprocdef;
  967. begin
  968. def:=tdef(intf.symtable.defindex.first);
  969. while assigned(def) do
  970. begin
  971. if def.deftype=procdef then
  972. begin
  973. procname:='';
  974. implprocdef:=nil;
  975. nextexist:=nil;
  976. repeat
  977. mappedname:=_class.implementedinterfaces.getmappings(intfindex,tprocdef(def).procsym.name,nextexist);
  978. if procname='' then
  979. procname:=tprocdef(def).procsym.name;
  980. //mappedname; { for error messages }
  981. if mappedname<>'' then
  982. implprocdef:=gintfgetcprocdef(tprocdef(def),mappedname);
  983. until assigned(implprocdef) or not assigned(nextexist);
  984. if not assigned(implprocdef) then
  985. implprocdef:=gintfgetcprocdef(tprocdef(def),tprocdef(def).procsym.name);
  986. if procname='' then
  987. procname:=tprocdef(def).procsym.name;
  988. if assigned(implprocdef) then
  989. _class.implementedinterfaces.addimplproc(intfindex,implprocdef)
  990. else
  991. Message1(sym_e_no_matching_implementation_found,tprocdef(def).fullprocname(false));
  992. end;
  993. def:=tdef(def.indexnext);
  994. end;
  995. end;
  996. procedure tclassheader.gintfwalkdowninterface(intf: tobjectdef; intfindex: longint);
  997. begin
  998. if assigned(intf.childof) then
  999. gintfwalkdowninterface(intf.childof,intfindex);
  1000. gintfdoonintf(intf,intfindex);
  1001. end;
  1002. function tclassheader.genintftable: tasmlabel;
  1003. var
  1004. intfindex: longint;
  1005. curintf: tobjectdef;
  1006. intftable: tasmlabel;
  1007. begin
  1008. { 1. step collect implementor functions into the implementedinterfaces.implprocs }
  1009. for intfindex:=1 to _class.implementedinterfaces.count do
  1010. begin
  1011. curintf:=_class.implementedinterfaces.interfaces(intfindex);
  1012. gintfwalkdowninterface(curintf,intfindex);
  1013. end;
  1014. { 2. step calc required fieldcount and their offsets in the object memory map
  1015. and write data }
  1016. objectlibrary.getdatalabel(intftable);
  1017. dataSegment.concat(tai_align.create(const_align(sizeof(aint))));
  1018. dataSegment.concat(Tai_label.Create(intftable));
  1019. gintfwritedata;
  1020. _class.implementedinterfaces.clearimplprocs; { release temporary information }
  1021. genintftable:=intftable;
  1022. end;
  1023. { Write interface identifiers to the data section }
  1024. procedure tclassheader.writeinterfaceids;
  1025. var
  1026. i : longint;
  1027. s : string;
  1028. begin
  1029. if assigned(_class.iidguid) then
  1030. begin
  1031. s:=make_mangledname('IID',_class.owner,_class.objname^);
  1032. maybe_new_object_file(dataSegment);
  1033. new_section(dataSegment,sec_rodata,s,const_align(sizeof(aint)));
  1034. dataSegment.concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  1035. dataSegment.concat(Tai_const.Create_32bit(longint(_class.iidguid^.D1)));
  1036. dataSegment.concat(Tai_const.Create_16bit(_class.iidguid^.D2));
  1037. dataSegment.concat(Tai_const.Create_16bit(_class.iidguid^.D3));
  1038. for i:=Low(_class.iidguid^.D4) to High(_class.iidguid^.D4) do
  1039. dataSegment.concat(Tai_const.Create_8bit(_class.iidguid^.D4[i]));
  1040. end;
  1041. maybe_new_object_file(dataSegment);
  1042. s:=make_mangledname('IIDSTR',_class.owner,_class.objname^);
  1043. new_section(dataSegment,sec_rodata,s,0);
  1044. dataSegment.concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  1045. dataSegment.concat(Tai_const.Create_8bit(length(_class.iidstr^)));
  1046. dataSegment.concat(Tai_string.Create(_class.iidstr^));
  1047. end;
  1048. procedure tclassheader.writevirtualmethods(List:TAAsmoutput);
  1049. var
  1050. vmtentry : pvmtentry;
  1051. procdefcoll : pprocdefcoll;
  1052. i : longint;
  1053. begin
  1054. { walk trough all numbers for virtual methods and search }
  1055. { the method }
  1056. for i:=0 to nextvirtnumber-1 do
  1057. begin
  1058. { walk trough all symbols }
  1059. vmtentry:=firstvmtentry;
  1060. while assigned(vmtentry) do
  1061. begin
  1062. { walk trough all methods }
  1063. procdefcoll:=vmtentry^.firstprocdef;
  1064. while assigned(procdefcoll) do
  1065. begin
  1066. { writes the addresses to the VMT }
  1067. { but only this which are declared as virtual }
  1068. if procdefcoll^.data.extnumber=i then
  1069. begin
  1070. if (po_virtualmethod in procdefcoll^.data.procoptions) then
  1071. begin
  1072. { if a method is abstract, then is also the }
  1073. { class abstract and it's not allow to }
  1074. { generates an instance }
  1075. if (po_abstractmethod in procdefcoll^.data.procoptions) then
  1076. List.concat(Tai_const.Createname('FPC_ABSTRACTERROR',AT_FUNCTION,0))
  1077. else
  1078. List.concat(Tai_const.createname(procdefcoll^.data.mangledname,AT_FUNCTION,0));
  1079. end;
  1080. end;
  1081. procdefcoll:=procdefcoll^.next;
  1082. end;
  1083. vmtentry:=vmtentry^.next;
  1084. end;
  1085. end;
  1086. end;
  1087. { generates the vmt for classes as well as for objects }
  1088. procedure tclassheader.writevmt;
  1089. var
  1090. methodnametable,intmessagetable,
  1091. strmessagetable,classnamelabel,
  1092. fieldtablelabel : tasmlabel;
  1093. {$ifdef WITHDMT}
  1094. dmtlabel : tasmlabel;
  1095. {$endif WITHDMT}
  1096. interfacetable : tasmlabel;
  1097. begin
  1098. {$ifdef WITHDMT}
  1099. dmtlabel:=gendmt;
  1100. {$endif WITHDMT}
  1101. { write tables for classes, this must be done before the actual
  1102. class is written, because we need the labels defined }
  1103. if is_class(_class) then
  1104. begin
  1105. objectlibrary.getdatalabel(classnamelabel);
  1106. maybe_new_object_file(dataSegment);
  1107. new_section(dataSegment,sec_rodata,classnamelabel.name,const_align(sizeof(aint)));
  1108. { interface table }
  1109. if _class.implementedinterfaces.count>0 then
  1110. interfacetable:=genintftable;
  1111. methodnametable:=genpublishedmethodstable;
  1112. fieldtablelabel:=_class.generate_field_table;
  1113. { write class name }
  1114. dataSegment.concat(Tai_label.Create(classnamelabel));
  1115. dataSegment.concat(Tai_const.Create_8bit(length(_class.objrealname^)));
  1116. dataSegment.concat(Tai_string.Create(_class.objrealname^));
  1117. { generate message and dynamic tables }
  1118. if (oo_has_msgstr in _class.objectoptions) then
  1119. strmessagetable:=genstrmsgtab;
  1120. if (oo_has_msgint in _class.objectoptions) then
  1121. intmessagetable:=genintmsgtab;
  1122. end;
  1123. { write debug info }
  1124. maybe_new_object_file(dataSegment);
  1125. new_section(dataSegment,sec_rodata,_class.vmt_mangledname,const_align(sizeof(aint)));
  1126. {$ifdef GDB}
  1127. if (cs_debuginfo in aktmoduleswitches) then
  1128. begin
  1129. do_count_dbx:=true;
  1130. if assigned(_class.owner) and assigned(_class.owner.name) then
  1131. dataSegment.concat(Tai_stabs.Create(strpnew('"vmt_'+_class.owner.name^+_class.name+':S'+
  1132. tstoreddef(vmttype.def).numberstring+'",'+tostr(N_STSYM)+',0,0,'+_class.vmt_mangledname)));
  1133. end;
  1134. {$endif GDB}
  1135. dataSegment.concat(Tai_symbol.Createname_global(_class.vmt_mangledname,AT_DATA,0));
  1136. { determine the size with symtable.datasize, because }
  1137. { size gives back 4 for classes }
  1138. dataSegment.concat(Tai_const.Create(ait_const_ptr,tobjectsymtable(_class.symtable).datasize));
  1139. dataSegment.concat(Tai_const.Create(ait_const_ptr,-int64(tobjectsymtable(_class.symtable).datasize)));
  1140. {$ifdef WITHDMT}
  1141. if _class.classtype=ct_object then
  1142. begin
  1143. if assigned(dmtlabel) then
  1144. dataSegment.concat(Tai_const_symbol.Create(dmtlabel)))
  1145. else
  1146. dataSegment.concat(Tai_const.Create_ptr(0));
  1147. end;
  1148. {$endif WITHDMT}
  1149. { write pointer to parent VMT, this isn't implemented in TP }
  1150. { but this is not used in FPC ? (PM) }
  1151. { it's not used yet, but the delphi-operators as and is need it (FK) }
  1152. { it is not written for parents that don't have any vmt !! }
  1153. if assigned(_class.childof) and
  1154. (oo_has_vmt in _class.childof.objectoptions) then
  1155. dataSegment.concat(Tai_const.Createname(_class.childof.vmt_mangledname,AT_DATA,0))
  1156. else
  1157. dataSegment.concat(Tai_const.Create_sym(nil));
  1158. { write extended info for classes, for the order see rtl/inc/objpash.inc }
  1159. if is_class(_class) then
  1160. begin
  1161. { pointer to class name string }
  1162. dataSegment.concat(Tai_const.Create_sym(classnamelabel));
  1163. { pointer to dynamic table or nil }
  1164. if (oo_has_msgint in _class.objectoptions) then
  1165. dataSegment.concat(Tai_const.Create_sym(intmessagetable))
  1166. else
  1167. dataSegment.concat(Tai_const.Create_sym(nil));
  1168. { pointer to method table or nil }
  1169. dataSegment.concat(Tai_const.Create_sym(methodnametable));
  1170. { pointer to field table }
  1171. dataSegment.concat(Tai_const.Create_sym(fieldtablelabel));
  1172. { pointer to type info of published section }
  1173. if (oo_can_have_published in _class.objectoptions) then
  1174. dataSegment.concat(Tai_const.Create_sym(_class.get_rtti_label(fullrtti)))
  1175. else
  1176. dataSegment.concat(Tai_const.Create_sym(nil));
  1177. { inittable for con-/destruction }
  1178. if _class.members_need_inittable then
  1179. dataSegment.concat(Tai_const.Create_sym(_class.get_rtti_label(initrtti)))
  1180. else
  1181. dataSegment.concat(Tai_const.Create_sym(nil));
  1182. { auto table }
  1183. dataSegment.concat(Tai_const.Create_sym(nil));
  1184. { interface table }
  1185. if _class.implementedinterfaces.count>0 then
  1186. dataSegment.concat(Tai_const.Create_sym(interfacetable))
  1187. else
  1188. dataSegment.concat(Tai_const.Create_sym(nil));
  1189. { table for string messages }
  1190. if (oo_has_msgstr in _class.objectoptions) then
  1191. dataSegment.concat(Tai_const.Create_sym(strmessagetable))
  1192. else
  1193. dataSegment.concat(Tai_const.Create_sym(nil));
  1194. end;
  1195. { write virtual methods }
  1196. writevirtualmethods(dataSegment);
  1197. { write the size of the VMT }
  1198. dataSegment.concat(Tai_symbol_end.Createname(_class.vmt_mangledname));
  1199. end;
  1200. procedure tclassheader.adjustselfvalue(procdef: tprocdef;ioffset: aint);
  1201. var
  1202. hsym : tsym;
  1203. href : treference;
  1204. paraloc : tcgparalocation;
  1205. begin
  1206. { calculate the parameter info for the procdef }
  1207. if not procdef.has_paraloc_info then
  1208. begin
  1209. procdef.requiredargarea:=paramanager.create_paraloc_info(procdef,callerside);
  1210. procdef.has_paraloc_info:=true;
  1211. end;
  1212. hsym:=tsym(procdef.parast.search('self'));
  1213. if not(assigned(hsym) and
  1214. (hsym.typ=varsym) and
  1215. assigned(tvarsym(hsym).paraitem)) then
  1216. internalerror(200305251);
  1217. paraloc:=tvarsym(hsym).paraitem.paraloc[callerside].location^;
  1218. case paraloc.loc of
  1219. LOC_REGISTER:
  1220. cg.a_op_const_reg(exprasmlist,OP_SUB,paraloc.size,ioffset,paraloc.register);
  1221. LOC_REFERENCE:
  1222. begin
  1223. { offset in the wrapper needs to be adjusted for the stored
  1224. return address }
  1225. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset+sizeof(aint));
  1226. cg.a_op_const_ref(exprasmlist,OP_SUB,paraloc.size,ioffset,href);
  1227. end
  1228. else
  1229. internalerror(200309189);
  1230. end;
  1231. end;
  1232. initialization
  1233. cclassheader:=tclassheader;
  1234. end.
  1235. {
  1236. $Log$
  1237. Revision 1.77 2004-10-12 14:34:49 peter
  1238. * fixed visibility for procsyms
  1239. * fixed override check when there was no entry yet
  1240. Revision 1.76 2004/09/21 17:25:12 peter
  1241. * paraloc branch merged
  1242. Revision 1.75 2004/09/13 20:31:07 peter
  1243. * fixed and cleanup of overriding non-visible methods
  1244. Revision 1.74.4.1 2004/08/31 20:43:06 peter
  1245. * paraloc patch
  1246. Revision 1.74 2004/07/09 22:17:32 peter
  1247. * revert has_localst patch
  1248. * replace aktstaticsymtable/aktglobalsymtable with current_module
  1249. Revision 1.73 2004/07/06 20:58:50 peter
  1250. * ignore po_haslocalst
  1251. Revision 1.72 2004/06/29 20:58:46 peter
  1252. * fix writing of private virtual/overriden methods that aren't
  1253. visibile in the current class, bug 3184
  1254. Revision 1.71 2004/06/20 08:55:29 florian
  1255. * logs truncated
  1256. Revision 1.70 2004/06/16 20:07:09 florian
  1257. * dwarf branch merged
  1258. Revision 1.69.2.8 2004/05/10 21:28:34 peter
  1259. * section_smartlink enabled for gas under linux
  1260. Revision 1.69.2.7 2004/05/01 16:02:09 peter
  1261. * POINTER_SIZE replaced with sizeof(aint)
  1262. * aint,aword,tconst*int moved to globtype
  1263. Revision 1.69.2.6 2004/04/28 20:36:13 florian
  1264. * fixed writing of sizes in classes/object vmts
  1265. Revision 1.69.2.5 2004/04/27 18:18:26 peter
  1266. * aword -> aint
  1267. }