nobj.pas 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633
  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,
  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. next : pprocdefcoll;
  44. end;
  45. psymcoll = ^tsymcoll;
  46. tsymcoll = record
  47. speedvalue : cardinal;
  48. name : pstring;
  49. data : pprocdefcoll;
  50. next : psymcoll;
  51. end;
  52. tclassheader=class
  53. private
  54. _Class : tobjectdef;
  55. count : integer;
  56. private
  57. { message tables }
  58. root : pprocdeftree;
  59. procedure disposeprocdeftree(p : pprocdeftree);
  60. procedure insertmsgint(p : tnamedindexitem;arg:pointer);
  61. procedure insertmsgstr(p : tnamedindexitem;arg:pointer);
  62. procedure insertint(p : pprocdeftree;var at : pprocdeftree);
  63. procedure insertstr(p : pprocdeftree;var at : pprocdeftree);
  64. procedure writenames(p : pprocdeftree);
  65. procedure writeintentry(p : pprocdeftree);
  66. procedure writestrentry(p : pprocdeftree);
  67. {$ifdef WITHDMT}
  68. private
  69. { dmt }
  70. procedure insertdmtentry(p : tnamedindexitem;arg:pointer);
  71. procedure writedmtindexentry(p : pprocdeftree);
  72. procedure writedmtaddressentry(p : pprocdeftree);
  73. {$endif}
  74. private
  75. { published methods }
  76. procedure do_count(p : tnamedindexitem;arg:pointer);
  77. procedure genpubmethodtableentry(p : tnamedindexitem;arg:pointer);
  78. private
  79. { vmt }
  80. wurzel : psymcoll;
  81. nextvirtnumber : integer;
  82. has_constructor,
  83. has_virtual_method : boolean;
  84. procedure eachsym(sym : tnamedindexitem;arg:pointer);
  85. procedure disposevmttree;
  86. procedure writevirtualmethods(List:TAAsmoutput);
  87. private
  88. { interface tables }
  89. function gintfgetvtbllabelname(intfindex: integer): string;
  90. procedure gintfcreatevtbl(intfindex: integer; rawdata,rawcode: TAAsmoutput);
  91. procedure gintfgenentry(intfindex, contintfindex: integer; rawdata: TAAsmoutput);
  92. procedure gintfoptimizevtbls(implvtbl : plongintarray);
  93. procedure gintfwritedata;
  94. function gintfgetcprocdef(proc: tprocdef;const name: string): tprocdef;
  95. procedure gintfdoonintf(intf: tobjectdef; intfindex: longint);
  96. procedure gintfwalkdowninterface(intf: tobjectdef; intfindex: longint);
  97. protected
  98. { adjusts the self value with ioffset when casting a interface
  99. to a class
  100. }
  101. procedure adjustselfvalue(procdef: tprocdef;ioffset: aword);virtual;
  102. { generates the wrapper for a call to a method via an interface }
  103. procedure cgintfwrapper(asmlist: TAAsmoutput; procdef: tprocdef; const labelname: string; ioffset: longint);virtual;abstract;
  104. public
  105. constructor create(c:tobjectdef);
  106. destructor destroy;override;
  107. { generates the message tables for a class }
  108. function genstrmsgtab : tasmlabel;
  109. function genintmsgtab : tasmlabel;
  110. function genpublishedmethodstable : tasmlabel;
  111. { generates a VMT entries }
  112. procedure genvmt;
  113. {$ifdef WITHDMT}
  114. { generates a DMT for _class }
  115. function gendmt : tasmlabel;
  116. {$endif WITHDMT}
  117. { interfaces }
  118. function genintftable: tasmlabel;
  119. { write the VMT to datasegment }
  120. procedure writevmt;
  121. procedure writeinterfaceids;
  122. end;
  123. tclassheaderclass=class of tclassheader;
  124. var
  125. cclassheader : tclassheaderclass;
  126. implementation
  127. uses
  128. {$ifdef delphi}
  129. sysutils,
  130. {$else}
  131. strings,
  132. {$endif}
  133. globals,verbose,
  134. symtable,symconst,symtype,symsym,defcmp,paramgr,
  135. {$ifdef GDB}
  136. gdb,
  137. {$endif GDB}
  138. aasmcpu,
  139. cpubase,cgbase,
  140. cgutils,cgobj
  141. ;
  142. {*****************************************************************************
  143. TClassHeader
  144. *****************************************************************************}
  145. constructor tclassheader.create(c:tobjectdef);
  146. begin
  147. inherited Create;
  148. _Class:=c;
  149. end;
  150. destructor tclassheader.destroy;
  151. begin
  152. disposevmttree;
  153. end;
  154. {**************************************
  155. Message Tables
  156. **************************************}
  157. procedure tclassheader.disposeprocdeftree(p : pprocdeftree);
  158. begin
  159. if assigned(p^.l) then
  160. disposeprocdeftree(p^.l);
  161. if assigned(p^.r) then
  162. disposeprocdeftree(p^.r);
  163. dispose(p);
  164. end;
  165. procedure tclassheader.insertint(p : pprocdeftree;var at : pprocdeftree);
  166. begin
  167. if at=nil then
  168. begin
  169. at:=p;
  170. inc(count);
  171. end
  172. else
  173. begin
  174. if p^.data.messageinf.i<at^.data.messageinf.i then
  175. insertint(p,at^.l)
  176. else if p^.data.messageinf.i>at^.data.messageinf.i then
  177. insertint(p,at^.r)
  178. else
  179. Message1(parser_e_duplicate_message_label,tostr(p^.data.messageinf.i));
  180. end;
  181. end;
  182. procedure tclassheader.insertstr(p : pprocdeftree;var at : pprocdeftree);
  183. var
  184. i : integer;
  185. begin
  186. if at=nil then
  187. begin
  188. at:=p;
  189. inc(count);
  190. end
  191. else
  192. begin
  193. i:=strcomp(p^.data.messageinf.str,at^.data.messageinf.str);
  194. if i<0 then
  195. insertstr(p,at^.l)
  196. else if i>0 then
  197. insertstr(p,at^.r)
  198. else
  199. Message1(parser_e_duplicate_message_label,strpas(p^.data.messageinf.str));
  200. end;
  201. end;
  202. procedure tclassheader.insertmsgint(p : tnamedindexitem;arg:pointer);
  203. var
  204. i : cardinal;
  205. def: Tprocdef;
  206. pt : pprocdeftree;
  207. begin
  208. if tsym(p).typ=procsym then
  209. for i:=1 to Tprocsym(p).procdef_count do
  210. begin
  211. def:=Tprocsym(p).procdef[i];
  212. if po_msgint in def.procoptions then
  213. begin
  214. new(pt);
  215. pt^.data:=def;
  216. pt^.l:=nil;
  217. pt^.r:=nil;
  218. insertint(pt,root);
  219. end;
  220. end;
  221. end;
  222. procedure tclassheader.insertmsgstr(p : tnamedindexitem;arg:pointer);
  223. var
  224. i : cardinal;
  225. def: Tprocdef;
  226. pt : pprocdeftree;
  227. begin
  228. if tsym(p).typ=procsym then
  229. for i:=1 to Tprocsym(p).procdef_count do
  230. begin
  231. def:=Tprocsym(p).procdef[i];
  232. if po_msgstr in def.procoptions then
  233. begin
  234. new(pt);
  235. pt^.data:=def;
  236. pt^.l:=nil;
  237. pt^.r:=nil;
  238. insertstr(pt,root);
  239. end;
  240. end;
  241. end;
  242. procedure tclassheader.writenames(p : pprocdeftree);
  243. var
  244. ca : pchar;
  245. len : longint;
  246. begin
  247. objectlibrary.getdatalabel(p^.nl);
  248. if assigned(p^.l) then
  249. writenames(p^.l);
  250. datasegment.concat(tai_align.create(const_align(POINTER_SIZE)));
  251. dataSegment.concat(Tai_label.Create(p^.nl));
  252. len:=strlen(p^.data.messageinf.str);
  253. datasegment.concat(tai_const.create_8bit(len));
  254. getmem(ca,len+1);
  255. move(p^.data.messageinf.str^,ca^,len+1);
  256. dataSegment.concat(Tai_string.Create_pchar(ca));
  257. if assigned(p^.r) then
  258. writenames(p^.r);
  259. end;
  260. procedure tclassheader.writestrentry(p : pprocdeftree);
  261. begin
  262. if assigned(p^.l) then
  263. writestrentry(p^.l);
  264. { write name label }
  265. dataSegment.concat(Tai_const_symbol.Create(p^.nl));
  266. dataSegment.concat(Tai_const_symbol.Createname(p^.data.mangledname));
  267. if assigned(p^.r) then
  268. writestrentry(p^.r);
  269. end;
  270. function tclassheader.genstrmsgtab : tasmlabel;
  271. var
  272. r : tasmlabel;
  273. begin
  274. root:=nil;
  275. count:=0;
  276. { insert all message handlers into a tree, sorted by name }
  277. _class.symtable.foreach({$ifdef FPCPROCVAR}@{$endif}insertmsgstr,nil);
  278. { write all names }
  279. if assigned(root) then
  280. writenames(root);
  281. { now start writing of the message string table }
  282. objectlibrary.getdatalabel(r);
  283. datasegment.concat(tai_align.create(const_align(POINTER_SIZE)));
  284. dataSegment.concat(Tai_label.Create(r));
  285. genstrmsgtab:=r;
  286. dataSegment.concat(Tai_const.Create_32bit(count));
  287. if assigned(root) then
  288. begin
  289. writestrentry(root);
  290. disposeprocdeftree(root);
  291. end;
  292. end;
  293. procedure tclassheader.writeintentry(p : pprocdeftree);
  294. begin
  295. if assigned(p^.l) then
  296. writeintentry(p^.l);
  297. { write name label }
  298. dataSegment.concat(Tai_const.Create_32bit(p^.data.messageinf.i));
  299. dataSegment.concat(Tai_const_symbol.Createname(p^.data.mangledname));
  300. if assigned(p^.r) then
  301. writeintentry(p^.r);
  302. end;
  303. function tclassheader.genintmsgtab : tasmlabel;
  304. var
  305. r : tasmlabel;
  306. begin
  307. root:=nil;
  308. count:=0;
  309. { insert all message handlers into a tree, sorted by name }
  310. _class.symtable.foreach({$ifdef FPCPROCVAR}@{$endif}insertmsgint,nil);
  311. { now start writing of the message string table }
  312. objectlibrary.getdatalabel(r);
  313. datasegment.concat(tai_align.create(const_align(POINTER_SIZE)));
  314. dataSegment.concat(Tai_label.Create(r));
  315. genintmsgtab:=r;
  316. dataSegment.concat(Tai_const.Create_32bit(count));
  317. if assigned(root) then
  318. begin
  319. writeintentry(root);
  320. disposeprocdeftree(root);
  321. end;
  322. end;
  323. {$ifdef WITHDMT}
  324. {**************************************
  325. DMT
  326. **************************************}
  327. procedure tclassheader.insertdmtentry(p : tnamedindexitem;arg:pointer);
  328. var
  329. hp : tprocdef;
  330. pt : pprocdeftree;
  331. begin
  332. if tsym(p).typ=procsym then
  333. begin
  334. hp:=tprocsym(p).definition;
  335. while assigned(hp) do
  336. begin
  337. if (po_msgint in hp.procoptions) then
  338. begin
  339. new(pt);
  340. pt^.p:=hp;
  341. pt^.l:=nil;
  342. pt^.r:=nil;
  343. insertint(pt,root);
  344. end;
  345. hp:=hp.nextoverloaded;
  346. end;
  347. end;
  348. end;
  349. procedure tclassheader.writedmtindexentry(p : pprocdeftree);
  350. begin
  351. if assigned(p^.l) then
  352. writedmtindexentry(p^.l);
  353. dataSegment.concat(Tai_const.Create_32bit(p^.data.messageinf.i));
  354. if assigned(p^.r) then
  355. writedmtindexentry(p^.r);
  356. end;
  357. procedure tclassheader.writedmtaddressentry(p : pprocdeftree);
  358. begin
  359. if assigned(p^.l) then
  360. writedmtaddressentry(p^.l);
  361. dataSegment.concat(Tai_const_symbol.Createname(p^.data.mangledname));
  362. if assigned(p^.r) then
  363. writedmtaddressentry(p^.r);
  364. end;
  365. function tclassheader.gendmt : tasmlabel;
  366. var
  367. r : tasmlabel;
  368. begin
  369. root:=nil;
  370. count:=0;
  371. gendmt:=nil;
  372. { insert all message handlers into a tree, sorted by number }
  373. _class.symtable.foreach({$ifdef FPCPROCVAR}@{$endif}insertdmtentry);
  374. if count>0 then
  375. begin
  376. objectlibrary.getdatalabel(r);
  377. gendmt:=r;
  378. datasegment.concat(tai_align.create(const_align(POINTER_SIZE)));
  379. dataSegment.concat(Tai_label.Create(r));
  380. { entries for caching }
  381. dataSegment.concat(Tai_const.Create_ptr(0));
  382. dataSegment.concat(Tai_const.Create_ptr(0));
  383. dataSegment.concat(Tai_const.Create_32bit(count));
  384. if assigned(root) then
  385. begin
  386. writedmtindexentry(root);
  387. writedmtaddressentry(root);
  388. disposeprocdeftree(root);
  389. end;
  390. end;
  391. end;
  392. {$endif WITHDMT}
  393. {**************************************
  394. Published Methods
  395. **************************************}
  396. procedure tclassheader.do_count(p : tnamedindexitem;arg:pointer);
  397. begin
  398. if (tsym(p).typ=procsym) and (sp_published in tsym(p).symoptions) then
  399. inc(count);
  400. end;
  401. procedure tclassheader.genpubmethodtableentry(p : tnamedindexitem;arg:pointer);
  402. var
  403. hp : tprocdef;
  404. l : tasmlabel;
  405. begin
  406. if (tsym(p).typ=procsym) and (sp_published in tsym(p).symoptions) then
  407. begin
  408. if Tprocsym(p).procdef_count>1 then
  409. internalerror(1209992);
  410. hp:=tprocsym(p).first_procdef;
  411. objectlibrary.getdatalabel(l);
  412. consts.concat(tai_align.create(const_align(POINTER_SIZE)));
  413. Consts.concat(Tai_label.Create(l));
  414. Consts.concat(Tai_const.Create_8bit(length(p.name)));
  415. Consts.concat(Tai_string.Create(p.name));
  416. dataSegment.concat(Tai_const_symbol.Create(l));
  417. dataSegment.concat(Tai_const_symbol.Createname(hp.mangledname));
  418. end;
  419. end;
  420. function tclassheader.genpublishedmethodstable : tasmlabel;
  421. var
  422. l : tasmlabel;
  423. begin
  424. count:=0;
  425. _class.symtable.foreach({$ifdef FPCPROCVAR}@{$endif}do_count,nil);
  426. if count>0 then
  427. begin
  428. objectlibrary.getdatalabel(l);
  429. datasegment.concat(tai_align.create(const_align(POINTER_SIZE)));
  430. dataSegment.concat(Tai_label.Create(l));
  431. dataSegment.concat(Tai_const.Create_32bit(count));
  432. _class.symtable.foreach({$ifdef FPCPROCVAR}@{$endif}genpubmethodtableentry,nil);
  433. genpublishedmethodstable:=l;
  434. end
  435. else
  436. genpublishedmethodstable:=nil;
  437. end;
  438. {**************************************
  439. VMT
  440. **************************************}
  441. procedure tclassheader.eachsym(sym : tnamedindexitem;arg:pointer);
  442. var
  443. procdefcoll : pprocdefcoll;
  444. symcoll : psymcoll;
  445. _name : string;
  446. _speed : cardinal;
  447. procedure newdefentry(pd:tprocdef);
  448. begin
  449. new(procdefcoll);
  450. procdefcoll^.data:=pd;
  451. procdefcoll^.hidden:=false;
  452. procdefcoll^.next:=symcoll^.data;
  453. symcoll^.data:=procdefcoll;
  454. { if it's a virtual method }
  455. if (po_virtualmethod in pd.procoptions) then
  456. begin
  457. { then it gets a number ... }
  458. pd.extnumber:=nextvirtnumber;
  459. { and we inc the number }
  460. inc(nextvirtnumber);
  461. has_virtual_method:=true;
  462. end;
  463. if (pd.proctypeoption=potype_constructor) then
  464. has_constructor:=true;
  465. { check, if a method should be overridden }
  466. if (pd._class=_class) and
  467. (po_overridingmethod in pd.procoptions) then
  468. MessagePos1(pd.fileinfo,parser_e_nothing_to_be_overridden,pd.fullprocname(false));
  469. end;
  470. { creates a new entry in the procsym list }
  471. procedure newentry;
  472. var i:cardinal;
  473. begin
  474. { if not, generate a new symbol item }
  475. new(symcoll);
  476. symcoll^.speedvalue:=sym.speedvalue;
  477. symcoll^.name:=stringdup(sym.name);
  478. symcoll^.next:=wurzel;
  479. symcoll^.data:=nil;
  480. wurzel:=symcoll;
  481. { inserts all definitions }
  482. for i:=1 to Tprocsym(sym).procdef_count do
  483. newdefentry(Tprocsym(sym).procdef[i]);
  484. end;
  485. label
  486. handlenextdef;
  487. var
  488. pd : tprocdef;
  489. i : cardinal;
  490. is_visible,
  491. hasoverloads,
  492. pdoverload : boolean;
  493. begin
  494. { put only sub routines into the VMT, and routines
  495. that are visible to the current class. Skip private
  496. methods in other classes }
  497. if (tsym(sym).typ=procsym) then
  498. begin
  499. { is this symbol visible from the class that we are
  500. generating. This will be used to hide the other procdefs.
  501. When the symbol is not visible we don't hide the other
  502. procdefs, because they can be reused in the next class.
  503. The check to skip the invisible methods that are in the
  504. list is futher down in the code }
  505. is_visible:=tprocsym(sym).is_visible_for_object(_class);
  506. { check the current list of symbols }
  507. _name:=sym.name;
  508. _speed:=sym.speedvalue;
  509. symcoll:=wurzel;
  510. while assigned(symcoll) do
  511. begin
  512. { does the symbol already exist in the list? First
  513. compare speedvalue before doing the string compare to
  514. speed it up a little }
  515. if (_speed=symcoll^.speedvalue) and
  516. (_name=symcoll^.name^) then
  517. begin
  518. hasoverloads:=(Tprocsym(sym).procdef_count>1);
  519. { walk through all defs of the symbol }
  520. for i:=1 to Tprocsym(sym).procdef_count do
  521. begin
  522. pd:=Tprocsym(sym).procdef[i];
  523. if pd.procsym=sym then
  524. begin
  525. pdoverload:=(po_overload in pd.procoptions);
  526. { compare with all stored definitions }
  527. procdefcoll:=symcoll^.data;
  528. while assigned(procdefcoll) do
  529. begin
  530. { compare only if the definition is not hidden }
  531. if not procdefcoll^.hidden then
  532. begin
  533. { check that all methods have overload directive }
  534. if not(m_fpc in aktmodeswitches) and
  535. (_class=pd._class) and
  536. (procdefcoll^.data._class=pd._class) and
  537. ((po_overload in pd.procoptions)<>(po_overload in procdefcoll^.data.procoptions)) then
  538. begin
  539. MessagePos1(pd.fileinfo,parser_e_no_overload_for_all_procs,pd.procsym.realname);
  540. { recover }
  541. include(procdefcoll^.data.procoptions,po_overload);
  542. include(pd.procoptions,po_overload);
  543. end;
  544. { check if one of the two methods has virtual }
  545. if (po_virtualmethod in procdefcoll^.data.procoptions) or
  546. (po_virtualmethod in pd.procoptions) then
  547. begin
  548. { if the current definition has no virtual then hide the
  549. old virtual if the new definition has the same arguments or
  550. when it has no overload directive and no overloads }
  551. if not(po_virtualmethod in pd.procoptions) then
  552. begin
  553. if tstoredsym(procdefcoll^.data.procsym).is_visible_for_object(pd._class) and
  554. (not(pdoverload or hasoverloads) or
  555. (compare_paras(procdefcoll^.data.para,pd.para,cp_value_equal_const,[])>=te_equal)) then
  556. begin
  557. if is_visible then
  558. procdefcoll^.hidden:=true;
  559. if (_class=pd._class) and not(po_reintroduce in pd.procoptions) then
  560. MessagePos1(pd.fileinfo,parser_w_should_use_override,pd.fullprocname(false));
  561. end;
  562. end
  563. { if both are virtual we check the header }
  564. else if (po_virtualmethod in pd.procoptions) and
  565. (po_virtualmethod in procdefcoll^.data.procoptions) then
  566. begin
  567. { new one has not override }
  568. if is_class(_class) and
  569. not(po_overridingmethod in pd.procoptions) then
  570. begin
  571. { we start a new virtual tree, hide the old }
  572. if (not(pdoverload or hasoverloads) or
  573. (compare_paras(procdefcoll^.data.para,pd.para,cp_value_equal_const,[])>=te_equal)) and
  574. (tstoredsym(procdefcoll^.data.procsym).is_visible_for_object(pd._class)) then
  575. begin
  576. if is_visible then
  577. procdefcoll^.hidden:=true;
  578. if (_class=pd._class) and not(po_reintroduce in pd.procoptions) then
  579. MessagePos1(pd.fileinfo,parser_w_should_use_override,pd.fullprocname(false));
  580. end;
  581. end
  582. { check if the method to override is visible }
  583. else if (po_overridingmethod in pd.procoptions) and
  584. (not tstoredsym(procdefcoll^.data.procsym).is_visible_for_object(pd._class)) then
  585. begin
  586. { do nothing, the error will follow when adding the entry }
  587. end
  588. { same parameters }
  589. else if (compare_paras(procdefcoll^.data.para,pd.para,cp_value_equal_const,[])>=te_equal) then
  590. begin
  591. { overload is inherited }
  592. if (po_overload in procdefcoll^.data.procoptions) then
  593. include(pd.procoptions,po_overload);
  594. { inherite calling convention when it was force and the
  595. current definition has none force }
  596. if (po_hascallingconvention in procdefcoll^.data.procoptions) and
  597. not(po_hascallingconvention in pd.procoptions) then
  598. begin
  599. pd.proccalloption:=procdefcoll^.data.proccalloption;
  600. include(pd.procoptions,po_hascallingconvention);
  601. end;
  602. { the flags have to match except abstract and override }
  603. { only if both are virtual !! }
  604. if (procdefcoll^.data.proccalloption<>pd.proccalloption) or
  605. (procdefcoll^.data.proctypeoption<>pd.proctypeoption) or
  606. ((procdefcoll^.data.procoptions-
  607. [po_abstractmethod,po_overridingmethod,po_assembler,po_overload,po_public,po_reintroduce])<>
  608. (pd.procoptions-[po_abstractmethod,po_overridingmethod,po_assembler,po_overload,po_public,po_reintroduce])) then
  609. begin
  610. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,pd.fullprocname(false));
  611. tprocsym(procdefcoll^.data.procsym).write_parameter_lists(pd);
  612. end;
  613. { error, if the return types aren't equal }
  614. if not(equal_defs(procdefcoll^.data.rettype.def,pd.rettype.def)) and
  615. not((procdefcoll^.data.rettype.def.deftype=objectdef) and
  616. (pd.rettype.def.deftype=objectdef) and
  617. is_class(procdefcoll^.data.rettype.def) and
  618. is_class(pd.rettype.def) and
  619. (tobjectdef(pd.rettype.def).is_related(
  620. tobjectdef(procdefcoll^.data.rettype.def)))) then
  621. Message2(parser_e_overridden_methods_not_same_ret,pd.fullprocname(false),
  622. procdefcoll^.data.fullprocname(false));
  623. { now set the number }
  624. pd.extnumber:=procdefcoll^.data.extnumber;
  625. { and exchange }
  626. procdefcoll^.data:=pd;
  627. goto handlenextdef;
  628. end
  629. { different parameters }
  630. else
  631. begin
  632. { when we got an override directive then can search futher for
  633. the procedure to override.
  634. If we are starting a new virtual tree then hide the old tree }
  635. if not(po_overridingmethod in pd.procoptions) and
  636. not pdoverload then
  637. begin
  638. if is_visible then
  639. procdefcoll^.hidden:=true;
  640. if (_class=pd._class) and not(po_reintroduce in pd.procoptions) then
  641. MessagePos1(pd.fileinfo,parser_w_should_use_override,pd.fullprocname(false));
  642. end;
  643. end;
  644. end
  645. else
  646. begin
  647. { the new definition is virtual and the old static, we hide the old one
  648. if the new defintion has not the overload directive }
  649. if is_visible and
  650. ((not(pdoverload or hasoverloads)) or
  651. (compare_paras(procdefcoll^.data.para,pd.para,cp_value_equal_const,[])>=te_equal)) then
  652. procdefcoll^.hidden:=true;
  653. end;
  654. end
  655. else
  656. begin
  657. { both are static, we hide the old one if the new defintion
  658. has not the overload directive }
  659. if is_visible and
  660. ((not pdoverload) or
  661. (compare_paras(procdefcoll^.data.para,pd.para,cp_value_equal_const,[])>=te_equal)) then
  662. procdefcoll^.hidden:=true;
  663. end;
  664. end; { not hidden }
  665. procdefcoll:=procdefcoll^.next;
  666. end;
  667. { if it isn't saved in the list we create a new entry }
  668. newdefentry(pd);
  669. end;
  670. handlenextdef:
  671. end;
  672. exit;
  673. end;
  674. symcoll:=symcoll^.next;
  675. end;
  676. newentry;
  677. end;
  678. end;
  679. procedure tclassheader.disposevmttree;
  680. var
  681. symcoll : psymcoll;
  682. procdefcoll : pprocdefcoll;
  683. begin
  684. { disposes the above generated tree }
  685. symcoll:=wurzel;
  686. while assigned(symcoll) do
  687. begin
  688. wurzel:=symcoll^.next;
  689. stringdispose(symcoll^.name);
  690. procdefcoll:=symcoll^.data;
  691. while assigned(procdefcoll) do
  692. begin
  693. symcoll^.data:=procdefcoll^.next;
  694. dispose(procdefcoll);
  695. procdefcoll:=symcoll^.data;
  696. end;
  697. dispose(symcoll);
  698. symcoll:=wurzel;
  699. end;
  700. end;
  701. procedure tclassheader.genvmt;
  702. procedure do_genvmt(p : tobjectdef);
  703. begin
  704. { start with the base class }
  705. if assigned(p.childof) then
  706. do_genvmt(p.childof);
  707. { walk through all public syms }
  708. p.symtable.foreach({$ifdef FPCPROCVAR}@{$endif}eachsym,nil);
  709. end;
  710. begin
  711. wurzel:=nil;
  712. nextvirtnumber:=0;
  713. has_constructor:=false;
  714. has_virtual_method:=false;
  715. { generates a tree of all used methods }
  716. do_genvmt(_class);
  717. if not(is_interface(_class)) and
  718. has_virtual_method and
  719. not(has_constructor) then
  720. Message1(parser_w_virtual_without_constructor,_class.objrealname^);
  721. end;
  722. {**************************************
  723. Interface tables
  724. **************************************}
  725. function tclassheader.gintfgetvtbllabelname(intfindex: integer): string;
  726. begin
  727. gintfgetvtbllabelname:=make_mangledname('VTBL',_class.owner,_class.objname^+
  728. '_$_'+_class.implementedinterfaces.interfaces(intfindex).objname^);
  729. end;
  730. procedure tclassheader.gintfcreatevtbl(intfindex: integer; rawdata,rawcode: TAAsmoutput);
  731. var
  732. implintf: timplementedinterfaces;
  733. curintf: tobjectdef;
  734. proccount: integer;
  735. tmps: string;
  736. i: longint;
  737. begin
  738. implintf:=_class.implementedinterfaces;
  739. curintf:=implintf.interfaces(intfindex);
  740. if (cs_create_smart in aktmoduleswitches) then
  741. rawdata.concat(Tai_symbol.Createname_global(gintfgetvtbllabelname(intfindex),0))
  742. else
  743. rawdata.concat(Tai_symbol.Createname(gintfgetvtbllabelname(intfindex),0));
  744. proccount:=implintf.implproccount(intfindex);
  745. for i:=1 to proccount do
  746. begin
  747. tmps:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+curintf.objname^+'_$_'+
  748. tostr(i)+'_$_'+
  749. implintf.implprocs(intfindex,i).mangledname);
  750. { create wrapper code }
  751. cgintfwrapper(rawcode,implintf.implprocs(intfindex,i),tmps,implintf.ioffsets(intfindex)^);
  752. { create reference }
  753. rawdata.concat(Tai_const_symbol.Createname(tmps));
  754. end;
  755. end;
  756. procedure tclassheader.gintfgenentry(intfindex, contintfindex: integer; rawdata: TAAsmoutput);
  757. var
  758. implintf: timplementedinterfaces;
  759. curintf: tobjectdef;
  760. tmplabel: tasmlabel;
  761. i: longint;
  762. begin
  763. implintf:=_class.implementedinterfaces;
  764. curintf:=implintf.interfaces(intfindex);
  765. { GUID }
  766. if curintf.objecttype in [odt_interfacecom] then
  767. begin
  768. { label for GUID }
  769. objectlibrary.getdatalabel(tmplabel);
  770. rawdata.concat(tai_align.create(const_align(pointer_size)));
  771. rawdata.concat(Tai_label.Create(tmplabel));
  772. rawdata.concat(Tai_const.Create_32bit(longint(curintf.iidguid^.D1)));
  773. rawdata.concat(Tai_const.Create_16bit(curintf.iidguid^.D2));
  774. rawdata.concat(Tai_const.Create_16bit(curintf.iidguid^.D3));
  775. for i:=Low(curintf.iidguid^.D4) to High(curintf.iidguid^.D4) do
  776. rawdata.concat(Tai_const.Create_8bit(curintf.iidguid^.D4[i]));
  777. dataSegment.concat(Tai_const_symbol.Create(tmplabel));
  778. end
  779. else
  780. begin
  781. { nil for Corba interfaces }
  782. dataSegment.concat(Tai_const.Create_ptr(0)); { nil }
  783. end;
  784. { VTable }
  785. dataSegment.concat(Tai_const_symbol.Createname(gintfgetvtbllabelname(contintfindex)));
  786. { IOffset field }
  787. dataSegment.concat(Tai_const.Create_32bit(implintf.ioffsets(contintfindex)^));
  788. { IIDStr }
  789. objectlibrary.getdatalabel(tmplabel);
  790. rawdata.concat(tai_align.create(const_align(pointer_size)));
  791. rawdata.concat(Tai_label.Create(tmplabel));
  792. rawdata.concat(Tai_const.Create_8bit(length(curintf.iidstr^)));
  793. if curintf.objecttype=odt_interfacecom then
  794. rawdata.concat(Tai_string.Create(upper(curintf.iidstr^)))
  795. else
  796. rawdata.concat(Tai_string.Create(curintf.iidstr^));
  797. dataSegment.concat(Tai_const_symbol.Create(tmplabel));
  798. end;
  799. procedure tclassheader.gintfoptimizevtbls(implvtbl : plongintarray);
  800. type
  801. tcompintfentry = record
  802. weight: longint;
  803. compintf: longint;
  804. end;
  805. { Max 1000 interface in the class header interfaces it's enough imho }
  806. tcompintfs = packed array[1..1000] of tcompintfentry;
  807. pcompintfs = ^tcompintfs;
  808. tequals = packed array[1..1000] of longint;
  809. pequals = ^tequals;
  810. var
  811. max: longint;
  812. equals: pequals;
  813. compats: pcompintfs;
  814. i: longint;
  815. j: longint;
  816. w: longint;
  817. cij: boolean;
  818. cji: boolean;
  819. begin
  820. max:=_class.implementedinterfaces.count;
  821. if max>High(tequals) then
  822. Internalerror(200006135);
  823. getmem(compats,sizeof(tcompintfentry)*max);
  824. getmem(equals,sizeof(longint)*max);
  825. fillchar(compats^,sizeof(tcompintfentry)*max,0);
  826. fillchar(equals^,sizeof(longint)*max,0);
  827. { ismergepossible is a containing relation
  828. meaning of ismergepossible(a,b,w) =
  829. if implementorfunction map of a is contained implementorfunction map of b
  830. imp(a,b) and imp(b,c) => imp(a,c) ; imp(a,b) and imp(b,a) => a == b
  831. }
  832. { the order is very important for correct allocation }
  833. for i:=1 to max do
  834. begin
  835. for j:=i+1 to max do
  836. begin
  837. cij:=_class.implementedinterfaces.isimplmergepossible(i,j,w);
  838. cji:=_class.implementedinterfaces.isimplmergepossible(j,i,w);
  839. if cij and cji then { i equal j }
  840. begin
  841. { get minimum index of equal }
  842. if equals^[j]=0 then
  843. equals^[j]:=i;
  844. end
  845. else if cij then
  846. begin
  847. { get minimum index of maximum weight }
  848. if compats^[i].weight<w then
  849. begin
  850. compats^[i].weight:=w;
  851. compats^[i].compintf:=j;
  852. end;
  853. end
  854. else if cji then
  855. begin
  856. { get minimum index of maximum weight }
  857. if (compats^[j].weight<w) then
  858. begin
  859. compats^[j].weight:=w;
  860. compats^[j].compintf:=i;
  861. end;
  862. end;
  863. end;
  864. end;
  865. for i:=1 to max do
  866. begin
  867. if compats^[i].compintf<>0 then
  868. implvtbl[i]:=compats^[i].compintf
  869. else if equals^[i]<>0 then
  870. implvtbl[i]:=equals^[i]
  871. else
  872. implvtbl[i]:=i;
  873. end;
  874. freemem(compats,sizeof(tcompintfentry)*max);
  875. freemem(equals,sizeof(longint)*max);
  876. end;
  877. procedure tclassheader.gintfwritedata;
  878. var
  879. rawdata,rawcode: taasmoutput;
  880. impintfindexes: plongintarray;
  881. max: longint;
  882. i: longint;
  883. begin
  884. max:=_class.implementedinterfaces.count;
  885. getmem(impintfindexes,(max+1)*sizeof(longint));
  886. gintfoptimizevtbls(impintfindexes);
  887. rawdata:=TAAsmOutput.Create;
  888. rawcode:=TAAsmOutput.Create;
  889. dataSegment.concat(Tai_const.Create_16bit(max));
  890. { Two pass, one for allocation and vtbl creation }
  891. for i:=1 to max do
  892. begin
  893. if impintfindexes[i]=i then { if implement itself }
  894. begin
  895. { allocate a pointer in the object memory }
  896. with tobjectsymtable(_class.symtable) do
  897. begin
  898. datasize:=align(datasize,min(POINTER_SIZE,fieldalignment));
  899. _class.implementedinterfaces.ioffsets(i)^:=datasize;
  900. inc(datasize,POINTER_SIZE);
  901. end;
  902. { write vtbl }
  903. gintfcreatevtbl(i,rawdata,rawcode);
  904. end;
  905. end;
  906. { second pass: for fill interfacetable and remained ioffsets }
  907. for i:=1 to max do
  908. begin
  909. if i<>impintfindexes[i] then { why execute x:=x ? }
  910. with _class.implementedinterfaces do
  911. ioffsets(i)^:=ioffsets(impintfindexes[i])^;
  912. gintfgenentry(i,impintfindexes[i],rawdata);
  913. end;
  914. dataSegment.concatlist(rawdata);
  915. rawdata.free;
  916. codeSegment.concatlist(rawcode);
  917. rawcode.free;
  918. freemem(impintfindexes,(max+1)*sizeof(longint));
  919. end;
  920. function tclassheader.gintfgetcprocdef(proc: tprocdef;const name: string): tprocdef;
  921. var
  922. sym: tsym;
  923. implprocdef : Tprocdef;
  924. i: cardinal;
  925. begin
  926. gintfgetcprocdef:=nil;
  927. sym:=tsym(search_class_member(_class,name));
  928. if assigned(sym) and
  929. (sym.typ=procsym) then
  930. begin
  931. { when the definition has overload directive set, we search for
  932. overloaded definitions in the class, this only needs to be done once
  933. for class entries as the tree keeps always the same }
  934. if (not tprocsym(sym).overloadchecked) and
  935. (po_overload in tprocsym(sym).first_procdef.procoptions) and
  936. (tprocsym(sym).owner.symtabletype=objectsymtable) then
  937. search_class_overloads(tprocsym(sym));
  938. for i:=1 to tprocsym(sym).procdef_count do
  939. begin
  940. implprocdef:=tprocsym(sym).procdef[i];
  941. if (compare_paras(proc.para,implprocdef.para,cp_none,[])>=te_equal) and
  942. (proc.proccalloption=implprocdef.proccalloption) then
  943. begin
  944. gintfgetcprocdef:=implprocdef;
  945. exit;
  946. end;
  947. end;
  948. end;
  949. end;
  950. procedure tclassheader.gintfdoonintf(intf: tobjectdef; intfindex: longint);
  951. var
  952. def: tdef;
  953. procname: string; { for error }
  954. mappedname: string;
  955. nextexist: pointer;
  956. implprocdef: tprocdef;
  957. begin
  958. def:=tdef(intf.symtable.defindex.first);
  959. while assigned(def) do
  960. begin
  961. if def.deftype=procdef then
  962. begin
  963. procname:='';
  964. implprocdef:=nil;
  965. nextexist:=nil;
  966. repeat
  967. mappedname:=_class.implementedinterfaces.getmappings(intfindex,tprocdef(def).procsym.name,nextexist);
  968. if procname='' then
  969. procname:=tprocdef(def).procsym.name;
  970. //mappedname; { for error messages }
  971. if mappedname<>'' then
  972. implprocdef:=gintfgetcprocdef(tprocdef(def),mappedname);
  973. until assigned(implprocdef) or not assigned(nextexist);
  974. if not assigned(implprocdef) then
  975. implprocdef:=gintfgetcprocdef(tprocdef(def),tprocdef(def).procsym.name);
  976. if procname='' then
  977. procname:=tprocdef(def).procsym.name;
  978. if assigned(implprocdef) then
  979. _class.implementedinterfaces.addimplproc(intfindex,implprocdef)
  980. else
  981. Message1(sym_e_no_matching_implementation_found,tprocdef(def).fullprocname(false));
  982. end;
  983. def:=tdef(def.indexnext);
  984. end;
  985. end;
  986. procedure tclassheader.gintfwalkdowninterface(intf: tobjectdef; intfindex: longint);
  987. begin
  988. if assigned(intf.childof) then
  989. gintfwalkdowninterface(intf.childof,intfindex);
  990. gintfdoonintf(intf,intfindex);
  991. end;
  992. function tclassheader.genintftable: tasmlabel;
  993. var
  994. intfindex: longint;
  995. curintf: tobjectdef;
  996. intftable: tasmlabel;
  997. begin
  998. { 1. step collect implementor functions into the implementedinterfaces.implprocs }
  999. for intfindex:=1 to _class.implementedinterfaces.count do
  1000. begin
  1001. curintf:=_class.implementedinterfaces.interfaces(intfindex);
  1002. gintfwalkdowninterface(curintf,intfindex);
  1003. end;
  1004. { 2. step calc required fieldcount and their offsets in the object memory map
  1005. and write data }
  1006. objectlibrary.getdatalabel(intftable);
  1007. dataSegment.concat(tai_align.create(const_align(POINTER_SIZE)));
  1008. dataSegment.concat(Tai_label.Create(intftable));
  1009. gintfwritedata;
  1010. _class.implementedinterfaces.clearimplprocs; { release temporary information }
  1011. genintftable:=intftable;
  1012. end;
  1013. { Write interface identifiers to the data section }
  1014. procedure tclassheader.writeinterfaceids;
  1015. var
  1016. i: longint;
  1017. begin
  1018. if assigned(_class.iidguid) then
  1019. begin
  1020. if (cs_create_smart in aktmoduleswitches) then
  1021. dataSegment.concat(Tai_cut.Create);
  1022. dataSegment.concat(Tai_symbol.Createname_global(make_mangledname('IID',_class.owner,_class.objname^),0));
  1023. dataSegment.concat(Tai_const.Create_32bit(_class.iidguid^.D1));
  1024. dataSegment.concat(Tai_const.Create_16bit(_class.iidguid^.D2));
  1025. dataSegment.concat(Tai_const.Create_16bit(_class.iidguid^.D3));
  1026. for i:=Low(_class.iidguid^.D4) to High(_class.iidguid^.D4) do
  1027. dataSegment.concat(Tai_const.Create_8bit(_class.iidguid^.D4[i]));
  1028. end;
  1029. if (cs_create_smart in aktmoduleswitches) then
  1030. dataSegment.concat(Tai_cut.Create);
  1031. dataSegment.concat(Tai_symbol.Createname_global(make_mangledname('IIDSTR',_class.owner,_class.objname^),0));
  1032. dataSegment.concat(Tai_const.Create_8bit(length(_class.iidstr^)));
  1033. dataSegment.concat(Tai_string.Create(_class.iidstr^));
  1034. end;
  1035. procedure tclassheader.writevirtualmethods(List:TAAsmoutput);
  1036. var
  1037. symcoll : psymcoll;
  1038. procdefcoll : pprocdefcoll;
  1039. i : longint;
  1040. begin
  1041. { walk trough all numbers for virtual methods and search }
  1042. { the method }
  1043. for i:=0 to nextvirtnumber-1 do
  1044. begin
  1045. symcoll:=wurzel;
  1046. { walk trough all symbols }
  1047. while assigned(symcoll) do
  1048. begin
  1049. { walk trough all methods }
  1050. procdefcoll:=symcoll^.data;
  1051. while assigned(procdefcoll) do
  1052. begin
  1053. { writes the addresses to the VMT }
  1054. { but only this which are declared as virtual }
  1055. if procdefcoll^.data.extnumber=i then
  1056. begin
  1057. if (po_virtualmethod in procdefcoll^.data.procoptions) then
  1058. begin
  1059. { if a method is abstract, then is also the }
  1060. { class abstract and it's not allow to }
  1061. { generates an instance }
  1062. if (po_abstractmethod in procdefcoll^.data.procoptions) then
  1063. List.concat(Tai_const_symbol.Createname('FPC_ABSTRACTERROR'))
  1064. else
  1065. List.concat(Tai_const_symbol.createname(procdefcoll^.data.mangledname));
  1066. end;
  1067. end;
  1068. procdefcoll:=procdefcoll^.next;
  1069. end;
  1070. symcoll:=symcoll^.next;
  1071. end;
  1072. end;
  1073. end;
  1074. { generates the vmt for classes as well as for objects }
  1075. procedure tclassheader.writevmt;
  1076. var
  1077. methodnametable,intmessagetable,
  1078. strmessagetable,classnamelabel,
  1079. fieldtablelabel : tasmlabel;
  1080. {$ifdef WITHDMT}
  1081. dmtlabel : tasmlabel;
  1082. {$endif WITHDMT}
  1083. interfacetable : tasmlabel;
  1084. begin
  1085. {$ifdef WITHDMT}
  1086. dmtlabel:=gendmt;
  1087. {$endif WITHDMT}
  1088. if (cs_create_smart in aktmoduleswitches) then
  1089. dataSegment.concat(Tai_cut.Create);
  1090. { write tables for classes, this must be done before the actual
  1091. class is written, because we need the labels defined }
  1092. if is_class(_class) then
  1093. begin
  1094. { interface table }
  1095. if _class.implementedinterfaces.count>0 then
  1096. begin
  1097. if (cs_create_smart in aktmoduleswitches) then
  1098. codeSegment.concat(Tai_cut.Create);
  1099. interfacetable:=genintftable;
  1100. end;
  1101. methodnametable:=genpublishedmethodstable;
  1102. fieldtablelabel:=_class.generate_field_table;
  1103. { write class name }
  1104. objectlibrary.getdatalabel(classnamelabel);
  1105. dataSegment.concat(tai_align.create(const_align(POINTER_SIZE)));
  1106. dataSegment.concat(Tai_label.Create(classnamelabel));
  1107. dataSegment.concat(Tai_const.Create_8bit(length(_class.objrealname^)));
  1108. dataSegment.concat(Tai_string.Create(_class.objrealname^));
  1109. { generate message and dynamic tables }
  1110. if (oo_has_msgstr in _class.objectoptions) then
  1111. strmessagetable:=genstrmsgtab;
  1112. if (oo_has_msgint in _class.objectoptions) then
  1113. intmessagetable:=genintmsgtab;
  1114. end;
  1115. { write debug info }
  1116. {$ifdef GDB}
  1117. if (cs_debuginfo in aktmoduleswitches) then
  1118. begin
  1119. do_count_dbx:=true;
  1120. if assigned(_class.owner) and assigned(_class.owner.name) then
  1121. dataSegment.concat(Tai_stabs.Create(strpnew('"vmt_'+_class.owner.name^+_class.name+':S'+
  1122. typeglobalnumber('__vtbl_ptr_type')+'",'+tostr(N_STSYM)+',0,0,'+_class.vmt_mangledname)));
  1123. end;
  1124. {$endif GDB}
  1125. dataSegment.concat(tai_align.create(const_align(POINTER_SIZE)));
  1126. dataSegment.concat(Tai_symbol.Createdataname_global(_class.vmt_mangledname,0));
  1127. { determine the size with symtable.datasize, because }
  1128. { size gives back 4 for classes }
  1129. dataSegment.concat(Tai_const.Create_32bit(tobjectsymtable(_class.symtable).datasize));
  1130. dataSegment.concat(Tai_const.Create_32bit(Cardinal(-tobjectsymtable(_class.symtable).datasize)));
  1131. {$ifdef WITHDMT}
  1132. if _class.classtype=ct_object then
  1133. begin
  1134. if assigned(dmtlabel) then
  1135. dataSegment.concat(Tai_const_symbol.Create(dmtlabel)))
  1136. else
  1137. dataSegment.concat(Tai_const.Create_ptr(0));
  1138. end;
  1139. {$endif WITHDMT}
  1140. { write pointer to parent VMT, this isn't implemented in TP }
  1141. { but this is not used in FPC ? (PM) }
  1142. { it's not used yet, but the delphi-operators as and is need it (FK) }
  1143. { it is not written for parents that don't have any vmt !! }
  1144. if assigned(_class.childof) and
  1145. (oo_has_vmt in _class.childof.objectoptions) then
  1146. dataSegment.concat(Tai_const_symbol.Createname(_class.childof.vmt_mangledname))
  1147. else
  1148. dataSegment.concat(Tai_const.Create_ptr(0));
  1149. { write extended info for classes, for the order see rtl/inc/objpash.inc }
  1150. if is_class(_class) then
  1151. begin
  1152. { pointer to class name string }
  1153. dataSegment.concat(Tai_const_symbol.Create(classnamelabel));
  1154. { pointer to dynamic table }
  1155. if (oo_has_msgint in _class.objectoptions) then
  1156. dataSegment.concat(Tai_const_symbol.Create(intmessagetable))
  1157. else
  1158. dataSegment.concat(Tai_const.Create_ptr(0));
  1159. { pointer to method table }
  1160. if assigned(methodnametable) then
  1161. dataSegment.concat(Tai_const_symbol.Create(methodnametable))
  1162. else
  1163. dataSegment.concat(Tai_const.Create_ptr(0));
  1164. { pointer to field table }
  1165. dataSegment.concat(Tai_const_symbol.Create(fieldtablelabel));
  1166. { pointer to type info of published section }
  1167. if (oo_can_have_published in _class.objectoptions) then
  1168. dataSegment.concat(Tai_const_symbol.Create(_class.get_rtti_label(fullrtti)))
  1169. else
  1170. dataSegment.concat(Tai_const.Create_ptr(0));
  1171. { inittable for con-/destruction }
  1172. if _class.members_need_inittable then
  1173. dataSegment.concat(Tai_const_symbol.Create(_class.get_rtti_label(initrtti)))
  1174. else
  1175. dataSegment.concat(Tai_const.Create_ptr(0));
  1176. { auto table }
  1177. dataSegment.concat(Tai_const.Create_ptr(0));
  1178. { interface table }
  1179. if _class.implementedinterfaces.count>0 then
  1180. dataSegment.concat(Tai_const_symbol.Create(interfacetable))
  1181. else
  1182. dataSegment.concat(Tai_const.Create_ptr(0));
  1183. { table for string messages }
  1184. if (oo_has_msgstr in _class.objectoptions) then
  1185. dataSegment.concat(Tai_const_symbol.Create(strmessagetable))
  1186. else
  1187. dataSegment.concat(Tai_const.Create_ptr(0));
  1188. end;
  1189. { write virtual methods }
  1190. writevirtualmethods(dataSegment);
  1191. { write the size of the VMT }
  1192. dataSegment.concat(Tai_symbol_end.Createname(_class.vmt_mangledname));
  1193. end;
  1194. procedure tclassheader.adjustselfvalue(procdef: tprocdef;ioffset: aword);
  1195. var
  1196. hsym : tsym;
  1197. href : treference;
  1198. locpara : tparalocation;
  1199. begin
  1200. { calculate the parameter info for the procdef }
  1201. if not procdef.has_paraloc_info then
  1202. begin
  1203. procdef.requiredargarea:=paramanager.create_paraloc_info(procdef,callerside);
  1204. procdef.has_paraloc_info:=true;
  1205. end;
  1206. hsym:=tsym(procdef.parast.search('self'));
  1207. if not(assigned(hsym) and
  1208. (hsym.typ=varsym) and
  1209. assigned(tvarsym(hsym).paraitem)) then
  1210. internalerror(200305251);
  1211. locpara:=tvarsym(hsym).paraitem.paraloc[callerside];
  1212. case locpara.loc of
  1213. LOC_REGISTER:
  1214. cg.a_op_const_reg(exprasmlist,OP_SUB,locpara.size,ioffset,locpara.register);
  1215. LOC_REFERENCE:
  1216. begin
  1217. { offset in the wrapper needs to be adjusted for the stored
  1218. return address }
  1219. reference_reset_base(href,locpara.reference.index,locpara.reference.offset+POINTER_SIZE);
  1220. cg.a_op_const_ref(exprasmlist,OP_SUB,locpara.size,ioffset,href);
  1221. end
  1222. else
  1223. internalerror(200309189);
  1224. end;
  1225. end;
  1226. initialization
  1227. cclassheader:=tclassheader;
  1228. end.
  1229. {
  1230. $Log$
  1231. Revision 1.64 2004-02-27 10:21:05 florian
  1232. * top_symbol killed
  1233. + refaddr to treference added
  1234. + refsymbol to treference added
  1235. * top_local stuff moved to an extra record to save memory
  1236. + aint introduced
  1237. * tppufile.get/putint64/aint implemented
  1238. Revision 1.63 2004/02/26 16:16:38 peter
  1239. * tai_const.create_ptr added
  1240. Revision 1.62 2004/02/19 17:07:42 florian
  1241. * fixed arg. area calculation
  1242. Revision 1.61 2004/02/13 15:41:24 peter
  1243. * overload directive checking for methods is now done
  1244. when the vmt is generated
  1245. Revision 1.60 2004/02/08 23:30:43 florian
  1246. * web bug 2942 fixed: reintroduce isn't necessary in methods of child classes of course
  1247. Revision 1.59 2004/01/28 20:30:18 peter
  1248. * record alignment splitted in fieldalignment and recordalignment,
  1249. the latter is used when this record is inserted in another record.
  1250. Revision 1.58 2004/01/21 14:22:00 florian
  1251. + reintroduce implemented
  1252. Revision 1.57 2003/12/08 22:34:24 peter
  1253. * tai_const.create_32bit changed to cardinal
  1254. Revision 1.56 2003/11/28 17:24:22 peter
  1255. * reversed offset calculation for caller side so it works
  1256. correctly for interfaces
  1257. Revision 1.55 2003/10/30 16:23:13 peter
  1258. * don't search for overloads in parents for constructors
  1259. Revision 1.54 2003/10/29 19:48:50 peter
  1260. * renamed mangeldname_prefix to make_mangledname and made it more
  1261. generic
  1262. * make_mangledname is now also used for internal threadvar/resstring
  1263. lists
  1264. * Add P$ in front of program modulename to prevent duplicated symbols
  1265. at assembler level, because the main program can have the same name
  1266. as a unit, see webtbs/tw1251b
  1267. Revision 1.53 2003/10/13 14:05:12 peter
  1268. * removed is_visible_for_proc
  1269. * search also for class overloads when finding interface
  1270. implementations
  1271. Revision 1.52 2003/10/10 17:48:13 peter
  1272. * old trgobj moved to x86/rgcpu and renamed to trgx86fpu
  1273. * tregisteralloctor renamed to trgobj
  1274. * removed rgobj from a lot of units
  1275. * moved location_* and reference_* to cgobj
  1276. * first things for mmx register allocation
  1277. Revision 1.51 2003/10/07 21:14:32 peter
  1278. * compare_paras() has a parameter to ignore hidden parameters
  1279. * cross unit overload searching ignores hidden parameters when
  1280. comparing parameter lists. Now function(string):string is
  1281. not overriden with procedure(string) which has the same visible
  1282. parameter list
  1283. Revision 1.50 2003/10/07 20:44:22 peter
  1284. * inherited forced calling convention
  1285. * show hints when forward doesn't match
  1286. Revision 1.49 2003/10/01 20:34:49 peter
  1287. * procinfo unit contains tprocinfo
  1288. * cginfo renamed to cgbase
  1289. * moved cgmessage to verbose
  1290. * fixed ppc and sparc compiles
  1291. Revision 1.48 2003/09/23 17:56:05 peter
  1292. * locals and paras are allocated in the code generation
  1293. * tvarsym.localloc contains the location of para/local when
  1294. generating code for the current procedure
  1295. Revision 1.47 2003/08/21 14:47:41 peter
  1296. * remove convert_registers
  1297. Revision 1.46 2003/08/20 09:07:00 daniel
  1298. * New register coding now mandatory, some more convert_registers calls
  1299. removed.
  1300. Revision 1.45 2003/08/10 17:25:23 peter
  1301. * fixed some reported bugs
  1302. Revision 1.44 2003/06/01 21:38:06 peter
  1303. * getregisterfpu size parameter added
  1304. * op_const_reg size parameter added
  1305. * sparc updates
  1306. Revision 1.43 2003/05/23 14:27:35 peter
  1307. * remove some unit dependencies
  1308. * current_procinfo changes to store more info
  1309. Revision 1.42 2003/04/25 20:59:33 peter
  1310. * removed funcretn,funcretsym, function result is now in varsym
  1311. and aliases for result and function name are added using absolutesym
  1312. * vs_hidden parameter for funcret passed in parameter
  1313. * vs_hidden fixes
  1314. * writenode changed to printnode and released from extdebug
  1315. * -vp option added to generate a tree.log with the nodetree
  1316. * nicer printnode for statements, callnode
  1317. Revision 1.41 2003/04/23 10:11:22 peter
  1318. * range check error for GUID fixed
  1319. Revision 1.40 2003/01/13 14:54:34 daniel
  1320. * Further work to convert codegenerator register convention;
  1321. internalerror bug fixed.
  1322. Revision 1.39 2003/01/09 21:52:37 peter
  1323. * merged some verbosity options.
  1324. * V_LineInfo is a verbosity flag to include line info
  1325. Revision 1.38 2002/11/25 17:43:20 peter
  1326. * splitted defbase in defutil,symutil,defcmp
  1327. * merged isconvertable and is_equal into compare_defs(_ext)
  1328. * made operator search faster by walking the list only once
  1329. Revision 1.37 2002/11/17 16:31:56 carl
  1330. * memory optimization (3-4%) : cleanup of tai fields,
  1331. cleanup of tdef and tsym fields.
  1332. * make it work for m68k
  1333. Revision 1.36 2002/11/15 01:58:52 peter
  1334. * merged changes from 1.0.7 up to 04-11
  1335. - -V option for generating bug report tracing
  1336. - more tracing for option parsing
  1337. - errors for cdecl and high()
  1338. - win32 import stabs
  1339. - win32 records<=8 are returned in eax:edx (turned off by default)
  1340. - heaptrc update
  1341. - more info for temp management in .s file with EXTDEBUG
  1342. Revision 1.35 2002/11/09 16:19:43 carl
  1343. - remove superfluous data in classname
  1344. Revision 1.34 2002/11/09 15:35:35 carl
  1345. * major alignment updates for objects/class tables
  1346. Revision 1.33 2002/10/20 15:33:36 peter
  1347. * having overloads is the same as overload directive for hiding of
  1348. parent methods. This is required becuase it can be possible that a
  1349. method will then hide a method in the parent that an overloaded
  1350. method requires. See webbug tw2185
  1351. Revision 1.32 2002/10/19 15:09:24 peter
  1352. + tobjectdef.members_need_inittable that is used to generate only the
  1353. inittable when it is really used. This saves a lot of useless calls
  1354. to fpc_finalize when destroying classes
  1355. Revision 1.31 2002/10/15 19:00:42 peter
  1356. * small tweak to use speedvalue before comparing strings
  1357. Revision 1.30 2002/10/06 16:40:25 florian
  1358. * interface wrapper name mangling improved
  1359. Revision 1.29 2002/10/05 12:43:25 carl
  1360. * fixes for Delphi 6 compilation
  1361. (warning : Some features do not work under Delphi)
  1362. Revision 1.28 2002/09/16 14:11:13 peter
  1363. * add argument to equal_paras() to support default values or not
  1364. Revision 1.27 2002/09/03 16:26:26 daniel
  1365. * Make Tprocdef.defs protected
  1366. Revision 1.26 2002/09/03 15:44:44 peter
  1367. * fixed private methods hiding public virtual methods
  1368. Revision 1.25 2002/08/11 14:32:27 peter
  1369. * renamed current_library to objectlibrary
  1370. Revision 1.24 2002/08/11 13:24:12 peter
  1371. * saving of asmsymbols in ppu supported
  1372. * asmsymbollist global is removed and moved into a new class
  1373. tasmlibrarydata that will hold the info of a .a file which
  1374. corresponds with a single module. Added librarydata to tmodule
  1375. to keep the library info stored for the module. In the future the
  1376. objectfiles will also be stored to the tasmlibrarydata class
  1377. * all getlabel/newasmsymbol and friends are moved to the new class
  1378. Revision 1.23 2002/08/09 07:33:01 florian
  1379. * a couple of interface related fixes
  1380. Revision 1.22 2002/07/20 11:57:55 florian
  1381. * types.pas renamed to defbase.pas because D6 contains a types
  1382. unit so this would conflicts if D6 programms are compiled
  1383. + Willamette/SSE2 instructions to assembler added
  1384. Revision 1.21 2002/07/01 18:46:23 peter
  1385. * internal linker
  1386. * reorganized aasm layer
  1387. Revision 1.20 2002/05/18 13:34:10 peter
  1388. * readded missing revisions
  1389. Revision 1.19 2002/05/16 19:46:39 carl
  1390. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1391. + try to fix temp allocation (still in ifdef)
  1392. + generic constructor calls
  1393. + start of tassembler / tmodulebase class cleanup
  1394. Revision 1.17 2002/05/12 16:53:08 peter
  1395. * moved entry and exitcode to ncgutil and cgobj
  1396. * foreach gets extra argument for passing local data to the
  1397. iterator function
  1398. * -CR checks also class typecasts at runtime by changing them
  1399. into as
  1400. * fixed compiler to cycle with the -CR option
  1401. * fixed stabs with elf writer, finally the global variables can
  1402. be watched
  1403. * removed a lot of routines from cga unit and replaced them by
  1404. calls to cgobj
  1405. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1406. u32bit then the other is typecasted also to u32bit without giving
  1407. a rangecheck warning/error.
  1408. * fixed pascal calling method with reversing also the high tree in
  1409. the parast, detected by tcalcst3 test
  1410. Revision 1.16 2002/04/20 21:32:24 carl
  1411. + generic FPC_CHECKPOINTER
  1412. + first parameter offset in stack now portable
  1413. * rename some constants
  1414. + move some cpu stuff to other units
  1415. - remove unused constents
  1416. * fix stacksize for some targets
  1417. * fix generic size problems which depend now on EXTEND_SIZE constant
  1418. Revision 1.15 2002/04/19 15:46:01 peter
  1419. * mangledname rewrite, tprocdef.mangledname is now created dynamicly
  1420. in most cases and not written to the ppu
  1421. * add mangeledname_prefix() routine to generate the prefix of
  1422. manglednames depending on the current procedure, object and module
  1423. * removed static procprefix since the mangledname is now build only
  1424. on demand from tprocdef.mangledname
  1425. Revision 1.14 2002/04/15 18:59:07 carl
  1426. + target_info.size_of_pointer -> pointer_Size
  1427. Revision 1.13 2002/02/11 18:51:35 peter
  1428. * fixed vmt generation for private procedures that were skipped after
  1429. my previous changes
  1430. }