nobj.pas 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476
  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. ;
  28. type
  29. pprocdeftree = ^tprocdeftree;
  30. tprocdeftree = record
  31. data : tprocdef;
  32. nl : tasmlabel;
  33. l,r : pprocdeftree;
  34. end;
  35. pprocdefcoll = ^tprocdefcoll;
  36. tprocdefcoll = record
  37. data : tprocdef;
  38. hidden : boolean;
  39. visible : boolean;
  40. next : pprocdefcoll;
  41. end;
  42. pvmtentry = ^tvmtentry;
  43. tvmtentry = record
  44. speedvalue : cardinal;
  45. name : pstring;
  46. firstprocdef : pprocdefcoll;
  47. next : pvmtentry;
  48. end;
  49. tclassheader=class
  50. private
  51. _Class : tobjectdef;
  52. private
  53. { message tables }
  54. root : pprocdeftree;
  55. procedure disposeprocdeftree(p : pprocdeftree);
  56. procedure insertmsgint(p : tnamedindexitem;arg:pointer);
  57. procedure insertmsgstr(p : tnamedindexitem;arg:pointer);
  58. procedure insertint(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  59. procedure insertstr(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  60. procedure writenames(p : pprocdeftree);
  61. procedure writeintentry(p : pprocdeftree);
  62. procedure writestrentry(p : pprocdeftree);
  63. {$ifdef WITHDMT}
  64. private
  65. { dmt }
  66. procedure insertdmtentry(p : tnamedindexitem;arg:pointer);
  67. procedure writedmtindexentry(p : pprocdeftree);
  68. procedure writedmtaddressentry(p : pprocdeftree);
  69. {$endif}
  70. private
  71. { published methods }
  72. procedure do_count_published_methods(p : tnamedindexitem;arg:pointer);
  73. procedure do_gen_published_methods(p : tnamedindexitem;arg:pointer);
  74. private
  75. { vmt }
  76. firstvmtentry : pvmtentry;
  77. nextvirtnumber : integer;
  78. has_constructor,
  79. has_virtual_method : boolean;
  80. procedure newdefentry(vmtentry:pvmtentry;pd:tprocdef;is_visible:boolean);
  81. function newvmtentry(sym:tprocsym):pvmtentry;
  82. procedure eachsym(sym : tnamedindexitem;arg:pointer);
  83. procedure disposevmttree;
  84. procedure writevirtualmethods(List:TAAsmoutput);
  85. private
  86. { interface tables }
  87. function gintfgetvtbllabelname(intfindex: integer): string;
  88. procedure gintfcreatevtbl(intfindex: integer; rawdata,rawcode: TAAsmoutput);
  89. procedure gintfgenentry(intfindex, contintfindex: integer; rawdata: TAAsmoutput);
  90. procedure gintfoptimizevtbls(implvtbl : plongintarray);
  91. procedure gintfwritedata;
  92. function gintfgetcprocdef(proc: tprocdef;const name: string): tprocdef;
  93. procedure gintfdoonintf(intf: tobjectdef; intfindex: longint);
  94. procedure gintfwalkdowninterface(intf: tobjectdef; intfindex: longint);
  95. protected
  96. { adjusts the self value with ioffset when casting a interface
  97. to a class
  98. }
  99. procedure adjustselfvalue(procdef: tprocdef;ioffset: aint);virtual;
  100. { generates the wrapper for a call to a method via an interface }
  101. procedure cgintfwrapper(asmlist: TAAsmoutput; procdef: tprocdef; const labelname: string; ioffset: longint);virtual;abstract;
  102. public
  103. constructor create(c:tobjectdef);
  104. destructor destroy;override;
  105. { generates the message tables for a class }
  106. function genstrmsgtab : tasmlabel;
  107. function genintmsgtab : tasmlabel;
  108. function genpublishedmethodstable : tasmlabel;
  109. { generates a VMT entries }
  110. procedure genvmt;
  111. {$ifdef WITHDMT}
  112. { generates a DMT for _class }
  113. function gendmt : tasmlabel;
  114. {$endif WITHDMT}
  115. { interfaces }
  116. function genintftable: tasmlabel;
  117. { write the VMT to datasegment }
  118. procedure writevmt;
  119. procedure writeinterfaceids;
  120. end;
  121. tclassheaderclass=class of tclassheader;
  122. var
  123. cclassheader : tclassheaderclass;
  124. implementation
  125. uses
  126. strings,
  127. globals,verbose,systems,
  128. symtable,symconst,symtype,defcmp,paramgr,
  129. {$ifdef GDB}
  130. gdb,
  131. {$endif GDB}
  132. aasmcpu,
  133. cgbase,parabase,
  134. cgutils,cgobj
  135. ;
  136. {*****************************************************************************
  137. TClassHeader
  138. *****************************************************************************}
  139. constructor tclassheader.create(c:tobjectdef);
  140. begin
  141. inherited Create;
  142. _Class:=c;
  143. end;
  144. destructor tclassheader.destroy;
  145. begin
  146. disposevmttree;
  147. end;
  148. {**************************************
  149. Message Tables
  150. **************************************}
  151. procedure tclassheader.disposeprocdeftree(p : pprocdeftree);
  152. begin
  153. if assigned(p^.l) then
  154. disposeprocdeftree(p^.l);
  155. if assigned(p^.r) then
  156. disposeprocdeftree(p^.r);
  157. dispose(p);
  158. end;
  159. procedure tclassheader.insertint(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  160. begin
  161. if at=nil then
  162. begin
  163. at:=p;
  164. inc(count);
  165. end
  166. else
  167. begin
  168. if p^.data.messageinf.i<at^.data.messageinf.i then
  169. insertint(p,at^.l,count)
  170. else if p^.data.messageinf.i>at^.data.messageinf.i then
  171. insertint(p,at^.r,count)
  172. else
  173. Message1(parser_e_duplicate_message_label,tostr(p^.data.messageinf.i));
  174. end;
  175. end;
  176. procedure tclassheader.insertstr(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  177. var
  178. i : integer;
  179. begin
  180. if at=nil then
  181. begin
  182. at:=p;
  183. inc(count);
  184. end
  185. else
  186. begin
  187. i:=strcomp(p^.data.messageinf.str,at^.data.messageinf.str);
  188. if i<0 then
  189. insertstr(p,at^.l,count)
  190. else if i>0 then
  191. insertstr(p,at^.r,count)
  192. else
  193. Message1(parser_e_duplicate_message_label,strpas(p^.data.messageinf.str));
  194. end;
  195. end;
  196. procedure tclassheader.insertmsgint(p : tnamedindexitem;arg:pointer);
  197. var
  198. i : cardinal;
  199. def: Tprocdef;
  200. pt : pprocdeftree;
  201. begin
  202. if tsym(p).typ=procsym then
  203. for i:=1 to Tprocsym(p).procdef_count do
  204. begin
  205. def:=Tprocsym(p).procdef[i];
  206. if po_msgint in def.procoptions then
  207. begin
  208. new(pt);
  209. pt^.data:=def;
  210. pt^.l:=nil;
  211. pt^.r:=nil;
  212. insertint(pt,root,plongint(arg)^);
  213. end;
  214. end;
  215. end;
  216. procedure tclassheader.insertmsgstr(p : tnamedindexitem;arg:pointer);
  217. var
  218. i : cardinal;
  219. def: Tprocdef;
  220. pt : pprocdeftree;
  221. begin
  222. if tsym(p).typ=procsym then
  223. for i:=1 to Tprocsym(p).procdef_count do
  224. begin
  225. def:=Tprocsym(p).procdef[i];
  226. if po_msgstr in def.procoptions then
  227. begin
  228. new(pt);
  229. pt^.data:=def;
  230. pt^.l:=nil;
  231. pt^.r:=nil;
  232. insertstr(pt,root,plongint(arg)^);
  233. end;
  234. end;
  235. end;
  236. procedure tclassheader.writenames(p : pprocdeftree);
  237. var
  238. ca : pchar;
  239. len : longint;
  240. begin
  241. objectlibrary.getdatalabel(p^.nl);
  242. if assigned(p^.l) then
  243. writenames(p^.l);
  244. datasegment.concat(tai_align.create(const_align(sizeof(aint))));
  245. dataSegment.concat(Tai_label.Create(p^.nl));
  246. len:=strlen(p^.data.messageinf.str);
  247. datasegment.concat(tai_const.create_8bit(len));
  248. getmem(ca,len+1);
  249. move(p^.data.messageinf.str^,ca^,len+1);
  250. dataSegment.concat(Tai_string.Create_pchar(ca));
  251. if assigned(p^.r) then
  252. writenames(p^.r);
  253. end;
  254. procedure tclassheader.writestrentry(p : pprocdeftree);
  255. begin
  256. if assigned(p^.l) then
  257. writestrentry(p^.l);
  258. { write name label }
  259. dataSegment.concat(Tai_const.Create_sym(p^.nl));
  260. dataSegment.concat(Tai_const.Createname(p^.data.mangledname,AT_FUNCTION,0));
  261. if assigned(p^.r) then
  262. writestrentry(p^.r);
  263. end;
  264. function tclassheader.genstrmsgtab : tasmlabel;
  265. var
  266. r : tasmlabel;
  267. count : longint;
  268. begin
  269. root:=nil;
  270. count:=0;
  271. { insert all message handlers into a tree, sorted by name }
  272. _class.symtable.foreach(@insertmsgstr,@count);
  273. { write all names }
  274. if assigned(root) then
  275. writenames(root);
  276. { now start writing of the message string table }
  277. objectlibrary.getdatalabel(r);
  278. datasegment.concat(tai_align.create(const_align(sizeof(aint))));
  279. dataSegment.concat(Tai_label.Create(r));
  280. genstrmsgtab:=r;
  281. dataSegment.concat(Tai_const.Create_32bit(count));
  282. if assigned(root) then
  283. begin
  284. writestrentry(root);
  285. disposeprocdeftree(root);
  286. end;
  287. end;
  288. procedure tclassheader.writeintentry(p : pprocdeftree);
  289. begin
  290. if assigned(p^.l) then
  291. writeintentry(p^.l);
  292. { write name label }
  293. dataSegment.concat(Tai_const.Create_32bit(p^.data.messageinf.i));
  294. dataSegment.concat(Tai_const.Createname(p^.data.mangledname,AT_FUNCTION,0));
  295. if assigned(p^.r) then
  296. writeintentry(p^.r);
  297. end;
  298. function tclassheader.genintmsgtab : tasmlabel;
  299. var
  300. r : tasmlabel;
  301. count : longint;
  302. begin
  303. root:=nil;
  304. count:=0;
  305. { insert all message handlers into a tree, sorted by name }
  306. _class.symtable.foreach(@insertmsgint,@count);
  307. { now start writing of the message string table }
  308. objectlibrary.getdatalabel(r);
  309. datasegment.concat(tai_align.create(const_align(sizeof(aint))));
  310. dataSegment.concat(Tai_label.Create(r));
  311. genintmsgtab:=r;
  312. dataSegment.concat(Tai_const.Create_32bit(count));
  313. if assigned(root) then
  314. begin
  315. writeintentry(root);
  316. disposeprocdeftree(root);
  317. end;
  318. end;
  319. {$ifdef WITHDMT}
  320. {**************************************
  321. DMT
  322. **************************************}
  323. procedure tclassheader.insertdmtentry(p : tnamedindexitem;arg:pointer);
  324. var
  325. hp : tprocdef;
  326. pt : pprocdeftree;
  327. begin
  328. if tsym(p).typ=procsym then
  329. begin
  330. hp:=tprocsym(p).definition;
  331. while assigned(hp) do
  332. begin
  333. if (po_msgint in hp.procoptions) then
  334. begin
  335. new(pt);
  336. pt^.p:=hp;
  337. pt^.l:=nil;
  338. pt^.r:=nil;
  339. insertint(pt,root);
  340. end;
  341. hp:=hp.nextoverloaded;
  342. end;
  343. end;
  344. end;
  345. procedure tclassheader.writedmtindexentry(p : pprocdeftree);
  346. begin
  347. if assigned(p^.l) then
  348. writedmtindexentry(p^.l);
  349. dataSegment.concat(Tai_const.Create_32bit(p^.data.messageinf.i));
  350. if assigned(p^.r) then
  351. writedmtindexentry(p^.r);
  352. end;
  353. procedure tclassheader.writedmtaddressentry(p : pprocdeftree);
  354. begin
  355. if assigned(p^.l) then
  356. writedmtaddressentry(p^.l);
  357. dataSegment.concat(Tai_const_symbol.Createname(p^.data.mangledname,AT_FUNCTION,0));
  358. if assigned(p^.r) then
  359. writedmtaddressentry(p^.r);
  360. end;
  361. function tclassheader.gendmt : tasmlabel;
  362. var
  363. r : tasmlabel;
  364. begin
  365. root:=nil;
  366. count:=0;
  367. gendmt:=nil;
  368. { insert all message handlers into a tree, sorted by number }
  369. _class.symtable.foreach(insertdmtentry);
  370. if count>0 then
  371. begin
  372. objectlibrary.getdatalabel(r);
  373. gendmt:=r;
  374. datasegment.concat(tai_align.create(const_align(sizeof(aint))));
  375. dataSegment.concat(Tai_label.Create(r));
  376. { entries for caching }
  377. dataSegment.concat(Tai_const.Create_ptr(0));
  378. dataSegment.concat(Tai_const.Create_ptr(0));
  379. dataSegment.concat(Tai_const.Create_32bit(count));
  380. if assigned(root) then
  381. begin
  382. writedmtindexentry(root);
  383. writedmtaddressentry(root);
  384. disposeprocdeftree(root);
  385. end;
  386. end;
  387. end;
  388. {$endif WITHDMT}
  389. {**************************************
  390. Published Methods
  391. **************************************}
  392. procedure tclassheader.do_count_published_methods(p : tnamedindexitem;arg:pointer);
  393. var
  394. i : longint;
  395. pd : tprocdef;
  396. begin
  397. if (tsym(p).typ=procsym) then
  398. begin
  399. for i:=1 to tprocsym(p).procdef_count do
  400. begin
  401. pd:=tprocsym(p).procdef[i];
  402. if (pd.procsym=tsym(p)) and
  403. (sp_published in pd.symoptions) then
  404. inc(plongint(arg)^);
  405. end;
  406. end;
  407. end;
  408. procedure tclassheader.do_gen_published_methods(p : tnamedindexitem;arg:pointer);
  409. var
  410. i : longint;
  411. l : tasmlabel;
  412. pd : tprocdef;
  413. begin
  414. if (tsym(p).typ=procsym) then
  415. begin
  416. for i:=1 to tprocsym(p).procdef_count do
  417. begin
  418. pd:=tprocsym(p).procdef[i];
  419. if (pd.procsym=tsym(p)) and
  420. (sp_published in pd.symoptions) then
  421. begin
  422. objectlibrary.getdatalabel(l);
  423. consts.concat(tai_align.create(const_align(sizeof(aint))));
  424. Consts.concat(Tai_label.Create(l));
  425. Consts.concat(Tai_const.Create_8bit(length(p.name)));
  426. Consts.concat(Tai_string.Create(p.name));
  427. dataSegment.concat(Tai_const.Create_sym(l));
  428. dataSegment.concat(Tai_const.Createname(pd.mangledname,AT_FUNCTION,0));
  429. end;
  430. end;
  431. end;
  432. end;
  433. function tclassheader.genpublishedmethodstable : tasmlabel;
  434. var
  435. l : tasmlabel;
  436. count : longint;
  437. begin
  438. count:=0;
  439. _class.symtable.foreach(@do_count_published_methods,@count);
  440. if count>0 then
  441. begin
  442. objectlibrary.getdatalabel(l);
  443. datasegment.concat(tai_align.create(const_align(sizeof(aint))));
  444. dataSegment.concat(Tai_label.Create(l));
  445. dataSegment.concat(Tai_const.Create_32bit(count));
  446. _class.symtable.foreach(@do_gen_published_methods,nil);
  447. genpublishedmethodstable:=l;
  448. end
  449. else
  450. genpublishedmethodstable:=nil;
  451. end;
  452. {**************************************
  453. VMT
  454. **************************************}
  455. procedure tclassheader.newdefentry(vmtentry:pvmtentry;pd:tprocdef;is_visible:boolean);
  456. var
  457. procdefcoll : pprocdefcoll;
  458. begin
  459. { generate new entry }
  460. new(procdefcoll);
  461. procdefcoll^.data:=pd;
  462. procdefcoll^.hidden:=false;
  463. procdefcoll^.visible:=is_visible;
  464. procdefcoll^.next:=vmtentry^.firstprocdef;
  465. vmtentry^.firstprocdef:=procdefcoll;
  466. { give virtual method a number }
  467. if (po_virtualmethod in pd.procoptions) then
  468. begin
  469. pd.extnumber:=nextvirtnumber;
  470. inc(nextvirtnumber);
  471. has_virtual_method:=true;
  472. end;
  473. if (pd.proctypeoption=potype_constructor) then
  474. has_constructor:=true;
  475. end;
  476. function tclassheader.newvmtentry(sym:tprocsym):pvmtentry;
  477. begin
  478. { generate new vmtentry }
  479. new(result);
  480. result^.speedvalue:=sym.speedvalue;
  481. result^.name:=stringdup(sym.name);
  482. result^.next:=firstvmtentry;
  483. result^.firstprocdef:=nil;
  484. firstvmtentry:=result;
  485. end;
  486. procedure tclassheader.eachsym(sym : tnamedindexitem;arg:pointer);
  487. const
  488. po_comp = [po_classmethod,po_virtualmethod,po_staticmethod,po_interrupt,po_iocheck,po_msgstr,po_msgint,
  489. po_exports,po_varargs,po_explicitparaloc,po_nostackframe];
  490. label
  491. handlenextdef;
  492. var
  493. pd : tprocdef;
  494. i : cardinal;
  495. is_visible,
  496. hasoverloads,
  497. pdoverload : boolean;
  498. procdefcoll : pprocdefcoll;
  499. vmtentry : pvmtentry;
  500. _name : string;
  501. _speed : cardinal;
  502. begin
  503. if (tsym(sym).typ<>procsym) then
  504. exit;
  505. { check the current list of symbols }
  506. _name:=sym.name;
  507. _speed:=sym.speedvalue;
  508. vmtentry:=firstvmtentry;
  509. while assigned(vmtentry) do
  510. begin
  511. { does the symbol already exist in the list? First
  512. compare speedvalue before doing the string compare to
  513. speed it up a little }
  514. if (_speed=vmtentry^.speedvalue) and
  515. (_name=vmtentry^.name^) then
  516. begin
  517. hasoverloads:=(Tprocsym(sym).procdef_count>1);
  518. { walk through all defs of the symbol }
  519. for i:=1 to Tprocsym(sym).procdef_count do
  520. begin
  521. pd:=Tprocsym(sym).procdef[i];
  522. { is this procdef visible from the class that we are
  523. generating. This will be used to hide the other procdefs.
  524. When the symbol is not visible we don't hide the other
  525. procdefs, because they can be reused in the next class.
  526. The check to skip the invisible methods that are in the
  527. list is futher down in the code }
  528. is_visible:=pd.is_visible_for_object(_class);
  529. if pd.procsym=sym then
  530. begin
  531. pdoverload:=(po_overload in pd.procoptions);
  532. { compare with all stored definitions }
  533. procdefcoll:=vmtentry^.firstprocdef;
  534. while assigned(procdefcoll) do
  535. begin
  536. { compare only if the definition is not hidden }
  537. if not procdefcoll^.hidden then
  538. begin
  539. { check that all methods have overload directive }
  540. if not(m_fpc in aktmodeswitches) and
  541. (_class=pd._class) and
  542. (procdefcoll^.data._class=pd._class) and
  543. ((po_overload in pd.procoptions)<>(po_overload in procdefcoll^.data.procoptions)) then
  544. begin
  545. MessagePos1(pd.fileinfo,parser_e_no_overload_for_all_procs,pd.procsym.realname);
  546. { recover }
  547. include(procdefcoll^.data.procoptions,po_overload);
  548. include(pd.procoptions,po_overload);
  549. end;
  550. { check if one of the two methods has virtual }
  551. if (po_virtualmethod in procdefcoll^.data.procoptions) or
  552. (po_virtualmethod in pd.procoptions) then
  553. begin
  554. { if the current definition has no virtual then hide the
  555. old virtual if the new definition has the same arguments or
  556. when it has no overload directive and no overloads }
  557. if not(po_virtualmethod in pd.procoptions) then
  558. begin
  559. if procdefcoll^.visible and
  560. (not(pdoverload or hasoverloads) or
  561. (compare_paras(procdefcoll^.data.paras,pd.paras,cp_all,[])>=te_equal)) then
  562. begin
  563. if is_visible then
  564. procdefcoll^.hidden:=true;
  565. if (_class=pd._class) and not(po_reintroduce in pd.procoptions) then
  566. MessagePos1(pd.fileinfo,parser_w_should_use_override,pd.fullprocname(false));
  567. end;
  568. end
  569. { if both are virtual we check the header }
  570. else if (po_virtualmethod in pd.procoptions) and
  571. (po_virtualmethod in procdefcoll^.data.procoptions) then
  572. begin
  573. { new one has not override }
  574. if is_class(_class) and
  575. not(po_overridingmethod in pd.procoptions) then
  576. begin
  577. { we start a new virtual tree, hide the old }
  578. if (not(pdoverload or hasoverloads) or
  579. (compare_paras(procdefcoll^.data.paras,pd.paras,cp_all,[])>=te_equal)) and
  580. (procdefcoll^.visible) then
  581. begin
  582. if is_visible then
  583. procdefcoll^.hidden:=true;
  584. if (_class=pd._class) and not(po_reintroduce in pd.procoptions) then
  585. MessagePos1(pd.fileinfo,parser_w_should_use_override,pd.fullprocname(false));
  586. end;
  587. end
  588. { same parameters }
  589. else if (compare_paras(procdefcoll^.data.paras,pd.paras,cp_all,[])>=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*po_comp)<>(pd.procoptions*po_comp)) 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.paras,pd.paras,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.paras,pd.paras,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. const
  944. po_comp = [po_classmethod,po_staticmethod,po_interrupt,po_iocheck,po_msgstr,po_msgint,
  945. po_exports,po_varargs,po_explicitparaloc,po_nostackframe];
  946. var
  947. sym: tsym;
  948. implprocdef : Tprocdef;
  949. i: cardinal;
  950. begin
  951. gintfgetcprocdef:=nil;
  952. sym:=tsym(search_class_member(_class,name));
  953. if assigned(sym) and
  954. (sym.typ=procsym) then
  955. begin
  956. { when the definition has overload directive set, we search for
  957. overloaded definitions in the class, this only needs to be done once
  958. for class entries as the tree keeps always the same }
  959. if (not tprocsym(sym).overloadchecked) and
  960. (po_overload in tprocsym(sym).first_procdef.procoptions) and
  961. (tprocsym(sym).owner.symtabletype=objectsymtable) then
  962. search_class_overloads(tprocsym(sym));
  963. for i:=1 to tprocsym(sym).procdef_count do
  964. begin
  965. implprocdef:=tprocsym(sym).procdef[i];
  966. if (compare_paras(proc.paras,implprocdef.paras,cp_none,[])>=te_equal) and
  967. (proc.proccalloption=implprocdef.proccalloption) and
  968. (proc.proctypeoption=implprocdef.proctypeoption) and
  969. ((proc.procoptions*po_comp)=(implprocdef.procoptions*po_comp)) then
  970. begin
  971. gintfgetcprocdef:=implprocdef;
  972. exit;
  973. end;
  974. end;
  975. end;
  976. end;
  977. procedure tclassheader.gintfdoonintf(intf: tobjectdef; intfindex: longint);
  978. var
  979. def: tdef;
  980. procname: string; { for error }
  981. mappedname: string;
  982. nextexist: pointer;
  983. implprocdef: tprocdef;
  984. begin
  985. def:=tdef(intf.symtable.defindex.first);
  986. while assigned(def) do
  987. begin
  988. if def.deftype=procdef then
  989. begin
  990. procname:='';
  991. implprocdef:=nil;
  992. nextexist:=nil;
  993. repeat
  994. mappedname:=_class.implementedinterfaces.getmappings(intfindex,tprocdef(def).procsym.name,nextexist);
  995. if procname='' then
  996. procname:=tprocdef(def).procsym.name;
  997. //mappedname; { for error messages }
  998. if mappedname<>'' then
  999. implprocdef:=gintfgetcprocdef(tprocdef(def),mappedname);
  1000. until assigned(implprocdef) or not assigned(nextexist);
  1001. if not assigned(implprocdef) then
  1002. implprocdef:=gintfgetcprocdef(tprocdef(def),tprocdef(def).procsym.name);
  1003. if procname='' then
  1004. procname:=tprocdef(def).procsym.name;
  1005. if assigned(implprocdef) then
  1006. _class.implementedinterfaces.addimplproc(intfindex,implprocdef)
  1007. else
  1008. Message1(sym_e_no_matching_implementation_found,tprocdef(def).fullprocname(false));
  1009. end;
  1010. def:=tdef(def.indexnext);
  1011. end;
  1012. end;
  1013. procedure tclassheader.gintfwalkdowninterface(intf: tobjectdef; intfindex: longint);
  1014. begin
  1015. if assigned(intf.childof) then
  1016. gintfwalkdowninterface(intf.childof,intfindex);
  1017. gintfdoonintf(intf,intfindex);
  1018. end;
  1019. function tclassheader.genintftable: tasmlabel;
  1020. var
  1021. intfindex: longint;
  1022. curintf: tobjectdef;
  1023. intftable: tasmlabel;
  1024. begin
  1025. { 1. step collect implementor functions into the implementedinterfaces.implprocs }
  1026. for intfindex:=1 to _class.implementedinterfaces.count do
  1027. begin
  1028. curintf:=_class.implementedinterfaces.interfaces(intfindex);
  1029. gintfwalkdowninterface(curintf,intfindex);
  1030. end;
  1031. { 2. step calc required fieldcount and their offsets in the object memory map
  1032. and write data }
  1033. objectlibrary.getdatalabel(intftable);
  1034. dataSegment.concat(tai_align.create(const_align(sizeof(aint))));
  1035. dataSegment.concat(Tai_label.Create(intftable));
  1036. gintfwritedata;
  1037. _class.implementedinterfaces.clearimplprocs; { release temporary information }
  1038. genintftable:=intftable;
  1039. end;
  1040. { Write interface identifiers to the data section }
  1041. procedure tclassheader.writeinterfaceids;
  1042. var
  1043. i : longint;
  1044. s : string;
  1045. begin
  1046. if assigned(_class.iidguid) then
  1047. begin
  1048. s:=make_mangledname('IID',_class.owner,_class.objname^);
  1049. maybe_new_object_file(dataSegment);
  1050. new_section(dataSegment,sec_rodata,s,const_align(sizeof(aint)));
  1051. dataSegment.concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  1052. dataSegment.concat(Tai_const.Create_32bit(longint(_class.iidguid^.D1)));
  1053. dataSegment.concat(Tai_const.Create_16bit(_class.iidguid^.D2));
  1054. dataSegment.concat(Tai_const.Create_16bit(_class.iidguid^.D3));
  1055. for i:=Low(_class.iidguid^.D4) to High(_class.iidguid^.D4) do
  1056. dataSegment.concat(Tai_const.Create_8bit(_class.iidguid^.D4[i]));
  1057. end;
  1058. maybe_new_object_file(dataSegment);
  1059. s:=make_mangledname('IIDSTR',_class.owner,_class.objname^);
  1060. new_section(dataSegment,sec_rodata,s,0);
  1061. dataSegment.concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  1062. dataSegment.concat(Tai_const.Create_8bit(length(_class.iidstr^)));
  1063. dataSegment.concat(Tai_string.Create(_class.iidstr^));
  1064. end;
  1065. procedure tclassheader.writevirtualmethods(List:TAAsmoutput);
  1066. var
  1067. vmtentry : pvmtentry;
  1068. procdefcoll : pprocdefcoll;
  1069. i : longint;
  1070. begin
  1071. { walk trough all numbers for virtual methods and search }
  1072. { the method }
  1073. for i:=0 to nextvirtnumber-1 do
  1074. begin
  1075. { walk trough all symbols }
  1076. vmtentry:=firstvmtentry;
  1077. while assigned(vmtentry) do
  1078. begin
  1079. { walk trough all methods }
  1080. procdefcoll:=vmtentry^.firstprocdef;
  1081. while assigned(procdefcoll) do
  1082. begin
  1083. { writes the addresses to the VMT }
  1084. { but only this which are declared as virtual }
  1085. if procdefcoll^.data.extnumber=i then
  1086. begin
  1087. if (po_virtualmethod in procdefcoll^.data.procoptions) then
  1088. begin
  1089. { if a method is abstract, then is also the }
  1090. { class abstract and it's not allow to }
  1091. { generates an instance }
  1092. if (po_abstractmethod in procdefcoll^.data.procoptions) then
  1093. List.concat(Tai_const.Createname('FPC_ABSTRACTERROR',AT_FUNCTION,0))
  1094. else
  1095. List.concat(Tai_const.createname(procdefcoll^.data.mangledname,AT_FUNCTION,0));
  1096. end;
  1097. end;
  1098. procdefcoll:=procdefcoll^.next;
  1099. end;
  1100. vmtentry:=vmtentry^.next;
  1101. end;
  1102. end;
  1103. end;
  1104. { generates the vmt for classes as well as for objects }
  1105. procedure tclassheader.writevmt;
  1106. var
  1107. methodnametable,intmessagetable,
  1108. strmessagetable,classnamelabel,
  1109. fieldtablelabel : tasmlabel;
  1110. {$ifdef WITHDMT}
  1111. dmtlabel : tasmlabel;
  1112. {$endif WITHDMT}
  1113. interfacetable : tasmlabel;
  1114. begin
  1115. {$ifdef WITHDMT}
  1116. dmtlabel:=gendmt;
  1117. {$endif WITHDMT}
  1118. { write tables for classes, this must be done before the actual
  1119. class is written, because we need the labels defined }
  1120. if is_class(_class) then
  1121. begin
  1122. objectlibrary.getdatalabel(classnamelabel);
  1123. maybe_new_object_file(dataSegment);
  1124. new_section(dataSegment,sec_rodata,classnamelabel.name,const_align(sizeof(aint)));
  1125. { interface table }
  1126. if _class.implementedinterfaces.count>0 then
  1127. interfacetable:=genintftable;
  1128. methodnametable:=genpublishedmethodstable;
  1129. fieldtablelabel:=_class.generate_field_table;
  1130. { write class name }
  1131. dataSegment.concat(Tai_label.Create(classnamelabel));
  1132. dataSegment.concat(Tai_const.Create_8bit(length(_class.objrealname^)));
  1133. dataSegment.concat(Tai_string.Create(_class.objrealname^));
  1134. { generate message and dynamic tables }
  1135. if (oo_has_msgstr in _class.objectoptions) then
  1136. strmessagetable:=genstrmsgtab;
  1137. if (oo_has_msgint in _class.objectoptions) then
  1138. intmessagetable:=genintmsgtab;
  1139. end;
  1140. { write debug info }
  1141. maybe_new_object_file(dataSegment);
  1142. new_section(dataSegment,sec_rodata,_class.vmt_mangledname,const_align(sizeof(aint)));
  1143. {$ifdef GDB}
  1144. if (cs_debuginfo in aktmoduleswitches) then
  1145. begin
  1146. do_count_dbx:=true;
  1147. if assigned(_class.owner) and assigned(_class.owner.name) then
  1148. dataSegment.concat(Tai_stabs.Create(strpnew('"vmt_'+_class.owner.name^+_class.name+':S'+
  1149. tstoreddef(vmttype.def).numberstring+'",'+tostr(N_STSYM)+',0,0,'+_class.vmt_mangledname)));
  1150. end;
  1151. {$endif GDB}
  1152. dataSegment.concat(Tai_symbol.Createname_global(_class.vmt_mangledname,AT_DATA,0));
  1153. { determine the size with symtable.datasize, because }
  1154. { size gives back 4 for classes }
  1155. dataSegment.concat(Tai_const.Create(ait_const_ptr,tobjectsymtable(_class.symtable).datasize));
  1156. dataSegment.concat(Tai_const.Create(ait_const_ptr,-int64(tobjectsymtable(_class.symtable).datasize)));
  1157. {$ifdef WITHDMT}
  1158. if _class.classtype=ct_object then
  1159. begin
  1160. if assigned(dmtlabel) then
  1161. dataSegment.concat(Tai_const_symbol.Create(dmtlabel)))
  1162. else
  1163. dataSegment.concat(Tai_const.Create_ptr(0));
  1164. end;
  1165. {$endif WITHDMT}
  1166. { write pointer to parent VMT, this isn't implemented in TP }
  1167. { but this is not used in FPC ? (PM) }
  1168. { it's not used yet, but the delphi-operators as and is need it (FK) }
  1169. { it is not written for parents that don't have any vmt !! }
  1170. if assigned(_class.childof) and
  1171. (oo_has_vmt in _class.childof.objectoptions) then
  1172. dataSegment.concat(Tai_const.Createname(_class.childof.vmt_mangledname,AT_DATA,0))
  1173. else
  1174. dataSegment.concat(Tai_const.Create_sym(nil));
  1175. { write extended info for classes, for the order see rtl/inc/objpash.inc }
  1176. if is_class(_class) then
  1177. begin
  1178. { pointer to class name string }
  1179. dataSegment.concat(Tai_const.Create_sym(classnamelabel));
  1180. { pointer to dynamic table or nil }
  1181. if (oo_has_msgint in _class.objectoptions) then
  1182. dataSegment.concat(Tai_const.Create_sym(intmessagetable))
  1183. else
  1184. dataSegment.concat(Tai_const.Create_sym(nil));
  1185. { pointer to method table or nil }
  1186. dataSegment.concat(Tai_const.Create_sym(methodnametable));
  1187. { pointer to field table }
  1188. dataSegment.concat(Tai_const.Create_sym(fieldtablelabel));
  1189. { pointer to type info of published section }
  1190. if (oo_can_have_published in _class.objectoptions) then
  1191. dataSegment.concat(Tai_const.Create_sym(_class.get_rtti_label(fullrtti)))
  1192. else
  1193. dataSegment.concat(Tai_const.Create_sym(nil));
  1194. { inittable for con-/destruction }
  1195. if _class.members_need_inittable then
  1196. dataSegment.concat(Tai_const.Create_sym(_class.get_rtti_label(initrtti)))
  1197. else
  1198. dataSegment.concat(Tai_const.Create_sym(nil));
  1199. { auto table }
  1200. dataSegment.concat(Tai_const.Create_sym(nil));
  1201. { interface table }
  1202. if _class.implementedinterfaces.count>0 then
  1203. dataSegment.concat(Tai_const.Create_sym(interfacetable))
  1204. else
  1205. dataSegment.concat(Tai_const.Create_sym(nil));
  1206. { table for string messages }
  1207. if (oo_has_msgstr in _class.objectoptions) then
  1208. dataSegment.concat(Tai_const.Create_sym(strmessagetable))
  1209. else
  1210. dataSegment.concat(Tai_const.Create_sym(nil));
  1211. end;
  1212. { write virtual methods }
  1213. writevirtualmethods(dataSegment);
  1214. datasegment.concat(Tai_const.create(ait_const_ptr,0));
  1215. { write the size of the VMT }
  1216. dataSegment.concat(Tai_symbol_end.Createname(_class.vmt_mangledname));
  1217. end;
  1218. procedure tclassheader.adjustselfvalue(procdef: tprocdef;ioffset: aint);
  1219. var
  1220. hsym : tsym;
  1221. href : treference;
  1222. paraloc : tcgparalocation;
  1223. begin
  1224. { calculate the parameter info for the procdef }
  1225. if not procdef.has_paraloc_info then
  1226. begin
  1227. procdef.requiredargarea:=paramanager.create_paraloc_info(procdef,callerside);
  1228. procdef.has_paraloc_info:=true;
  1229. end;
  1230. hsym:=tsym(procdef.parast.search('self'));
  1231. if not(assigned(hsym) and
  1232. (hsym.typ=paravarsym)) then
  1233. internalerror(200305251);
  1234. paraloc:=tparavarsym(hsym).paraloc[callerside].location^;
  1235. case paraloc.loc of
  1236. LOC_REGISTER:
  1237. cg.a_op_const_reg(exprasmlist,OP_SUB,paraloc.size,ioffset,paraloc.register);
  1238. LOC_REFERENCE:
  1239. begin
  1240. { offset in the wrapper needs to be adjusted for the stored
  1241. return address }
  1242. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset+sizeof(aint));
  1243. cg.a_op_const_ref(exprasmlist,OP_SUB,paraloc.size,ioffset,href);
  1244. end
  1245. else
  1246. internalerror(200309189);
  1247. end;
  1248. end;
  1249. initialization
  1250. cclassheader:=tclassheader;
  1251. end.
  1252. {
  1253. $Log$
  1254. Revision 1.84 2004-12-26 20:16:44 peter
  1255. * also compare procoptions and proctype when searching interface
  1256. implementations
  1257. Revision 1.83 2004/11/20 14:39:27 florian
  1258. * write nil entry after last entry to vmt table so the size of the vmt can be determined
  1259. Revision 1.82 2004/11/17 22:21:35 peter
  1260. mangledname setting moved to place after the complete proc declaration is read
  1261. import generation moved to place where body is also parsed (still gives problems with win32)
  1262. Revision 1.81 2004/11/15 23:35:31 peter
  1263. * tparaitem removed, use tparavarsym instead
  1264. * parameter order is now calculated from paranr value in tparavarsym
  1265. Revision 1.80 2004/11/08 22:09:59 peter
  1266. * tvarsym splitted
  1267. Revision 1.79 2004/10/24 13:35:39 peter
  1268. * fixed writing of methodtable
  1269. Revision 1.78 2004/10/15 09:14:17 mazen
  1270. - remove $IFDEF DELPHI and related code
  1271. - remove $IFDEF FPCPROCVAR and related code
  1272. Revision 1.77 2004/10/12 14:34:49 peter
  1273. * fixed visibility for procsyms
  1274. * fixed override check when there was no entry yet
  1275. Revision 1.76 2004/09/21 17:25:12 peter
  1276. * paraloc branch merged
  1277. Revision 1.75 2004/09/13 20:31:07 peter
  1278. * fixed and cleanup of overriding non-visible methods
  1279. Revision 1.74.4.1 2004/08/31 20:43:06 peter
  1280. * paraloc patch
  1281. Revision 1.74 2004/07/09 22:17:32 peter
  1282. * revert has_localst patch
  1283. * replace aktstaticsymtable/aktglobalsymtable with current_module
  1284. Revision 1.73 2004/07/06 20:58:50 peter
  1285. * ignore po_haslocalst
  1286. Revision 1.72 2004/06/29 20:58:46 peter
  1287. * fix writing of private virtual/overriden methods that aren't
  1288. visibile in the current class, bug 3184
  1289. Revision 1.71 2004/06/20 08:55:29 florian
  1290. * logs truncated
  1291. Revision 1.70 2004/06/16 20:07:09 florian
  1292. * dwarf branch merged
  1293. Revision 1.69.2.8 2004/05/10 21:28:34 peter
  1294. * section_smartlink enabled for gas under linux
  1295. Revision 1.69.2.7 2004/05/01 16:02:09 peter
  1296. * POINTER_SIZE replaced with sizeof(aint)
  1297. * aint,aword,tconst*int moved to globtype
  1298. Revision 1.69.2.6 2004/04/28 20:36:13 florian
  1299. * fixed writing of sizes in classes/object vmts
  1300. Revision 1.69.2.5 2004/04/27 18:18:26 peter
  1301. * aword -> aint
  1302. }