nobj.pas 55 KB

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