hcgdata.pas 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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 hcgdata;
  20. {$i defines.inc}
  21. interface
  22. uses
  23. symdef,aasm;
  24. { generates the message tables for a class }
  25. function genstrmsgtab(_class : pobjectdef) : pasmlabel;
  26. function genintmsgtab(_class : pobjectdef) : pasmlabel;
  27. { generates the method name table }
  28. function genpublishedmethodstable(_class : pobjectdef) : pasmlabel;
  29. { generates a VMT for _class }
  30. procedure genvmt(list : paasmoutput;_class : pobjectdef);
  31. {$ifdef WITHDMT}
  32. { generates a DMT for _class }
  33. function gendmt(_class : pobjectdef) : pasmlabel;
  34. {$endif WITHDMT}
  35. function genintftable(_class: pobjectdef): pasmlabel;
  36. procedure writeinterfaceids(c : pobjectdef);
  37. implementation
  38. uses
  39. {$ifdef delphi}
  40. sysutils,
  41. {$else}
  42. strings,
  43. {$endif}
  44. cutils,cobjects,
  45. globtype,globals,verbose,
  46. symtable,symconst,symtype,symsym,types,
  47. hcodegen, systems,fmodule
  48. {$ifdef i386}
  49. ,n386ic
  50. {$endif}
  51. ;
  52. {*****************************************************************************
  53. Message
  54. *****************************************************************************}
  55. type
  56. pprocdeftree = ^tprocdeftree;
  57. tprocdeftree = record
  58. p : pprocdef;
  59. nl : pasmlabel;
  60. l,r : pprocdeftree;
  61. end;
  62. var
  63. root : pprocdeftree;
  64. count : longint;
  65. procedure insertstr(p : pprocdeftree;var at : pprocdeftree);
  66. var
  67. i : longint;
  68. begin
  69. if at=nil then
  70. begin
  71. at:=p;
  72. inc(count);
  73. end
  74. else
  75. begin
  76. i:=strcomp(p^.p^.messageinf.str,at^.p^.messageinf.str);
  77. if i<0 then
  78. insertstr(p,at^.l)
  79. else if i>0 then
  80. insertstr(p,at^.r)
  81. else
  82. Message1(parser_e_duplicate_message_label,strpas(p^.p^.messageinf.str));
  83. end;
  84. end;
  85. procedure disposeprocdeftree(p : pprocdeftree);
  86. begin
  87. if assigned(p^.l) then
  88. disposeprocdeftree(p^.l);
  89. if assigned(p^.r) then
  90. disposeprocdeftree(p^.r);
  91. dispose(p);
  92. end;
  93. procedure insertmsgstr(p : pnamedindexobject);
  94. var
  95. hp : pprocdef;
  96. pt : pprocdeftree;
  97. begin
  98. if psym(p)^.typ=procsym then
  99. begin
  100. hp:=pprocsym(p)^.definition;
  101. while assigned(hp) do
  102. begin
  103. if (po_msgstr in hp^.procoptions) then
  104. begin
  105. new(pt);
  106. pt^.p:=hp;
  107. pt^.l:=nil;
  108. pt^.r:=nil;
  109. insertstr(pt,root);
  110. end;
  111. hp:=hp^.nextoverloaded;
  112. end;
  113. end;
  114. end;
  115. procedure insertint(p : pprocdeftree;var at : pprocdeftree);
  116. begin
  117. if at=nil then
  118. begin
  119. at:=p;
  120. inc(count);
  121. end
  122. else
  123. begin
  124. if p^.p^.messageinf.i<at^.p^.messageinf.i then
  125. insertint(p,at^.l)
  126. else if p^.p^.messageinf.i>at^.p^.messageinf.i then
  127. insertint(p,at^.r)
  128. else
  129. Message1(parser_e_duplicate_message_label,tostr(p^.p^.messageinf.i));
  130. end;
  131. end;
  132. procedure insertmsgint(p : pnamedindexobject);
  133. var
  134. hp : pprocdef;
  135. pt : pprocdeftree;
  136. begin
  137. if psym(p)^.typ=procsym then
  138. begin
  139. hp:=pprocsym(p)^.definition;
  140. while assigned(hp) do
  141. begin
  142. if (po_msgint in hp^.procoptions) then
  143. begin
  144. new(pt);
  145. pt^.p:=hp;
  146. pt^.l:=nil;
  147. pt^.r:=nil;
  148. insertint(pt,root);
  149. end;
  150. hp:=hp^.nextoverloaded;
  151. end;
  152. end;
  153. end;
  154. procedure writenames(p : pprocdeftree);
  155. begin
  156. getdatalabel(p^.nl);
  157. if assigned(p^.l) then
  158. writenames(p^.l);
  159. datasegment^.concat(new(pai_label,init(p^.nl)));
  160. datasegment^.concat(new(pai_const,init_8bit(strlen(p^.p^.messageinf.str))));
  161. datasegment^.concat(new(pai_string,init_pchar(p^.p^.messageinf.str)));
  162. if assigned(p^.r) then
  163. writenames(p^.r);
  164. end;
  165. procedure writestrentry(p : pprocdeftree);
  166. begin
  167. if assigned(p^.l) then
  168. writestrentry(p^.l);
  169. { write name label }
  170. datasegment^.concat(new(pai_const_symbol,init(p^.nl)));
  171. datasegment^.concat(new(pai_const_symbol,initname(p^.p^.mangledname)));
  172. if assigned(p^.r) then
  173. writestrentry(p^.r);
  174. end;
  175. function genstrmsgtab(_class : pobjectdef) : pasmlabel;
  176. var
  177. r : pasmlabel;
  178. begin
  179. root:=nil;
  180. count:=0;
  181. { insert all message handlers into a tree, sorted by name }
  182. _class^.symtable^.foreach({$ifdef FPCPROCVAR}@{$endif}insertmsgstr);
  183. { write all names }
  184. if assigned(root) then
  185. writenames(root);
  186. { now start writing of the message string table }
  187. getdatalabel(r);
  188. datasegment^.concat(new(pai_label,init(r)));
  189. genstrmsgtab:=r;
  190. datasegment^.concat(new(pai_const,init_32bit(count)));
  191. if assigned(root) then
  192. begin
  193. writestrentry(root);
  194. disposeprocdeftree(root);
  195. end;
  196. end;
  197. procedure writeintentry(p : pprocdeftree);
  198. begin
  199. if assigned(p^.l) then
  200. writeintentry(p^.l);
  201. { write name label }
  202. datasegment^.concat(new(pai_const,init_32bit(p^.p^.messageinf.i)));
  203. datasegment^.concat(new(pai_const_symbol,initname(p^.p^.mangledname)));
  204. if assigned(p^.r) then
  205. writeintentry(p^.r);
  206. end;
  207. function genintmsgtab(_class : pobjectdef) : pasmlabel;
  208. var
  209. r : pasmlabel;
  210. begin
  211. root:=nil;
  212. count:=0;
  213. { insert all message handlers into a tree, sorted by name }
  214. _class^.symtable^.foreach({$ifdef FPCPROCVAR}@{$endif}insertmsgint);
  215. { now start writing of the message string table }
  216. getdatalabel(r);
  217. datasegment^.concat(new(pai_label,init(r)));
  218. genintmsgtab:=r;
  219. datasegment^.concat(new(pai_const,init_32bit(count)));
  220. if assigned(root) then
  221. begin
  222. writeintentry(root);
  223. disposeprocdeftree(root);
  224. end;
  225. end;
  226. {$ifdef WITHDMT}
  227. procedure insertdmtentry(p : pnamedindexobject);
  228. var
  229. hp : pprocdef;
  230. pt : pprocdeftree;
  231. begin
  232. if psym(p)^.typ=procsym then
  233. begin
  234. hp:=pprocsym(p)^.definition;
  235. while assigned(hp) do
  236. begin
  237. if (po_msgint in hp^.procoptions) then
  238. begin
  239. new(pt);
  240. pt^.p:=hp;
  241. pt^.l:=nil;
  242. pt^.r:=nil;
  243. insertint(pt,root);
  244. end;
  245. hp:=hp^.nextoverloaded;
  246. end;
  247. end;
  248. end;
  249. procedure writedmtindexentry(p : pprocdeftree);
  250. begin
  251. if assigned(p^.l) then
  252. writedmtindexentry(p^.l);
  253. datasegment^.concat(new(pai_const,init_32bit(p^.p^.messageinf.i)));
  254. if assigned(p^.r) then
  255. writedmtindexentry(p^.r);
  256. end;
  257. procedure writedmtaddressentry(p : pprocdeftree);
  258. begin
  259. if assigned(p^.l) then
  260. writedmtaddressentry(p^.l);
  261. datasegment^.concat(new(pai_const_symbol,initname(p^.p^.mangledname)));
  262. if assigned(p^.r) then
  263. writedmtaddressentry(p^.r);
  264. end;
  265. function gendmt(_class : pobjectdef) : pasmlabel;
  266. var
  267. r : pasmlabel;
  268. begin
  269. root:=nil;
  270. count:=0;
  271. gendmt:=nil;
  272. { insert all message handlers into a tree, sorted by number }
  273. _class^.symtable^.foreach({$ifdef FPCPROCVAR}@{$endif}insertdmtentry);
  274. if count>0 then
  275. begin
  276. getdatalabel(r);
  277. gendmt:=r;
  278. datasegment^.concat(new(pai_label,init(r)));
  279. { entries for caching }
  280. datasegment^.concat(new(pai_const,init_32bit(0)));
  281. datasegment^.concat(new(pai_const,init_32bit(0)));
  282. datasegment^.concat(new(pai_const,init_32bit(count)));
  283. if assigned(root) then
  284. begin
  285. writedmtindexentry(root);
  286. writedmtaddressentry(root);
  287. disposeprocdeftree(root);
  288. end;
  289. end;
  290. end;
  291. {$endif WITHDMT}
  292. procedure do_count(p : pnamedindexobject);
  293. begin
  294. if (psym(p)^.typ=procsym) and (sp_published in psym(p)^.symoptions) then
  295. inc(count);
  296. end;
  297. procedure genpubmethodtableentry(p : pnamedindexobject);
  298. var
  299. hp : pprocdef;
  300. l : pasmlabel;
  301. begin
  302. if (psym(p)^.typ=procsym) and (sp_published in psym(p)^.symoptions) then
  303. begin
  304. hp:=pprocsym(p)^.definition;
  305. if assigned(hp^.nextoverloaded) then
  306. internalerror(1209992);
  307. getdatalabel(l);
  308. consts^.concat(new(pai_label,init(l)));
  309. consts^.concat(new(pai_const,init_8bit(length(p^.name))));
  310. consts^.concat(new(pai_string,init(p^.name)));
  311. datasegment^.concat(new(pai_const_symbol,init(l)));
  312. datasegment^.concat(new(pai_const_symbol,initname(hp^.mangledname)));
  313. end;
  314. end;
  315. function genpublishedmethodstable(_class : pobjectdef) : pasmlabel;
  316. var
  317. l : pasmlabel;
  318. begin
  319. count:=0;
  320. _class^.symtable^.foreach({$ifdef FPCPROCVAR}@{$endif}do_count);
  321. if count>0 then
  322. begin
  323. getdatalabel(l);
  324. datasegment^.concat(new(pai_label,init(l)));
  325. datasegment^.concat(new(pai_const,init_32bit(count)));
  326. _class^.symtable^.foreach({$ifdef FPCPROCVAR}@{$endif}genpubmethodtableentry);
  327. genpublishedmethodstable:=l;
  328. end
  329. else
  330. genpublishedmethodstable:=nil;
  331. end;
  332. {*****************************************************************************
  333. VMT
  334. *****************************************************************************}
  335. type
  336. pprocdefcoll = ^tprocdefcoll;
  337. tprocdefcoll = record
  338. next : pprocdefcoll;
  339. data : pprocdef;
  340. end;
  341. psymcoll = ^tsymcoll;
  342. tsymcoll = record
  343. next : psymcoll;
  344. name : pstring;
  345. data : pprocdefcoll;
  346. end;
  347. var
  348. wurzel : psymcoll;
  349. nextvirtnumber : longint;
  350. _c : pobjectdef;
  351. has_constructor,has_virtual_method : boolean;
  352. procedure eachsym(sym : pnamedindexobject);
  353. var
  354. procdefcoll : pprocdefcoll;
  355. hp : pprocdef;
  356. symcoll : psymcoll;
  357. _name : string;
  358. stored : boolean;
  359. { creates a new entry in the procsym list }
  360. procedure newentry;
  361. begin
  362. { if not, generate a new symbol item }
  363. new(symcoll);
  364. symcoll^.name:=stringdup(sym^.name);
  365. symcoll^.next:=wurzel;
  366. symcoll^.data:=nil;
  367. wurzel:=symcoll;
  368. hp:=pprocsym(sym)^.definition;
  369. { inserts all definitions }
  370. while assigned(hp) do
  371. begin
  372. new(procdefcoll);
  373. procdefcoll^.data:=hp;
  374. procdefcoll^.next:=symcoll^.data;
  375. symcoll^.data:=procdefcoll;
  376. { if it's a virtual method }
  377. if (po_virtualmethod in hp^.procoptions) then
  378. begin
  379. { then it gets a number ... }
  380. hp^.extnumber:=nextvirtnumber;
  381. { and we inc the number }
  382. inc(nextvirtnumber);
  383. has_virtual_method:=true;
  384. end;
  385. if (hp^.proctypeoption=potype_constructor) then
  386. has_constructor:=true;
  387. { check, if a method should be overridden }
  388. if (po_overridingmethod in hp^.procoptions) then
  389. MessagePos1(hp^.fileinfo,parser_e_nothing_to_be_overridden,_c^.objname^+'.'+_name+hp^.demangled_paras);
  390. { next overloaded method }
  391. hp:=hp^.nextoverloaded;
  392. end;
  393. end;
  394. procedure newdefentry;
  395. begin
  396. new(procdefcoll);
  397. procdefcoll^.data:=hp;
  398. procdefcoll^.next:=symcoll^.data;
  399. symcoll^.data:=procdefcoll;
  400. { if it's a virtual method }
  401. if (po_virtualmethod in hp^.procoptions) then
  402. begin
  403. { then it gets a number ... }
  404. hp^.extnumber:=nextvirtnumber;
  405. { and we inc the number }
  406. inc(nextvirtnumber);
  407. has_virtual_method:=true;
  408. end;
  409. if (hp^.proctypeoption=potype_constructor) then
  410. has_constructor:=true;
  411. { check, if a method should be overridden }
  412. if (po_overridingmethod in hp^.procoptions) then
  413. MessagePos1(hp^.fileinfo,parser_e_nothing_to_be_overridden,_c^.objname^+'.'+_name+hp^.demangled_paras);
  414. end;
  415. label
  416. handlenextdef;
  417. begin
  418. { put only sub routines into the VMT }
  419. if psym(sym)^.typ=procsym then
  420. begin
  421. _name:=sym^.name;
  422. symcoll:=wurzel;
  423. while assigned(symcoll) do
  424. begin
  425. { does the symbol already exist in the list ? }
  426. if _name=symcoll^.name^ then
  427. begin
  428. { walk through all defs of the symbol }
  429. hp:=pprocsym(sym)^.definition;
  430. while assigned(hp) do
  431. begin
  432. { compare with all stored definitions }
  433. procdefcoll:=symcoll^.data;
  434. stored:=false;
  435. while assigned(procdefcoll) do
  436. begin
  437. { compare parameters }
  438. if equal_paras(procdefcoll^.data^.para,hp^.para,cp_all) and
  439. (
  440. (po_virtualmethod in procdefcoll^.data^.procoptions) or
  441. (po_virtualmethod in hp^.procoptions)
  442. ) then
  443. begin { same parameters }
  444. { wenn sie gleich sind }
  445. { und eine davon virtual deklariert ist }
  446. { Fehler falls nur eine VIRTUAL }
  447. if (po_virtualmethod in procdefcoll^.data^.procoptions)<>
  448. (po_virtualmethod in hp^.procoptions) then
  449. begin
  450. { in classes, we hide the old method }
  451. if is_class(_c) then
  452. begin
  453. { warn only if it is the first time,
  454. we hide the method }
  455. if _c=hp^._class then
  456. Message1(parser_w_should_use_override,hp^.fullprocname);
  457. end
  458. else
  459. if _c=hp^._class then
  460. begin
  461. if (po_virtualmethod in procdefcoll^.data^.procoptions) then
  462. Message1(parser_w_overloaded_are_not_both_virtual,
  463. hp^.fullprocname)
  464. else
  465. Message1(parser_w_overloaded_are_not_both_non_virtual,
  466. hp^.fullprocname);
  467. end;
  468. { was newentry; exit; (FK) }
  469. newdefentry;
  470. goto handlenextdef;
  471. end
  472. else
  473. { the flags have to match }
  474. { except abstract and override }
  475. { only if both are virtual !! }
  476. if (procdefcoll^.data^.proccalloptions<>hp^.proccalloptions) or
  477. (procdefcoll^.data^.proctypeoption<>hp^.proctypeoption) or
  478. ((procdefcoll^.data^.procoptions-
  479. [po_abstractmethod,po_overridingmethod,po_assembler])<>
  480. (hp^.procoptions-[po_abstractmethod,po_overridingmethod,po_assembler])) then
  481. Message1(parser_e_header_dont_match_forward,hp^.fullprocname);
  482. { check, if the overridden directive is set }
  483. { (povirtualmethod is set! }
  484. { class ? }
  485. if is_class(_c) and
  486. not(po_overridingmethod in hp^.procoptions) then
  487. begin
  488. { warn only if it is the first time,
  489. we hide the method }
  490. if _c=hp^._class then
  491. Message1(parser_w_should_use_override,hp^.fullprocname);
  492. { was newentry; (FK) }
  493. newdefentry;
  494. exit;
  495. end;
  496. { error, if the return types aren't equal }
  497. if not(is_equal(procdefcoll^.data^.rettype.def,hp^.rettype.def)) and
  498. not((procdefcoll^.data^.rettype.def^.deftype=objectdef) and
  499. (hp^.rettype.def^.deftype=objectdef) and
  500. is_class(procdefcoll^.data^.rettype.def) and
  501. is_class(hp^.rettype.def) and
  502. (pobjectdef(hp^.rettype.def)^.is_related(
  503. pobjectdef(procdefcoll^.data^.rettype.def)))) then
  504. Message1(parser_e_overloaded_methodes_not_same_ret,hp^.fullprocname);
  505. { now set the number }
  506. hp^.extnumber:=procdefcoll^.data^.extnumber;
  507. { and exchange }
  508. procdefcoll^.data:=hp;
  509. stored:=true;
  510. goto handlenextdef;
  511. end; { same parameters }
  512. procdefcoll:=procdefcoll^.next;
  513. end;
  514. { if it isn't saved in the list }
  515. { we create a new entry }
  516. if not(stored) then
  517. begin
  518. new(procdefcoll);
  519. procdefcoll^.data:=hp;
  520. procdefcoll^.next:=symcoll^.data;
  521. symcoll^.data:=procdefcoll;
  522. { if the method is virtual ... }
  523. if (po_virtualmethod in hp^.procoptions) then
  524. begin
  525. { ... it will get a number }
  526. hp^.extnumber:=nextvirtnumber;
  527. inc(nextvirtnumber);
  528. end;
  529. { check, if a method should be overridden }
  530. if (po_overridingmethod in hp^.procoptions) then
  531. MessagePos1(hp^.fileinfo,parser_e_nothing_to_be_overridden,
  532. hp^.fullprocname);
  533. end;
  534. handlenextdef:
  535. hp:=hp^.nextoverloaded;
  536. end;
  537. exit;
  538. end;
  539. symcoll:=symcoll^.next;
  540. end;
  541. newentry;
  542. end;
  543. end;
  544. procedure disposevmttree;
  545. var
  546. symcoll : psymcoll;
  547. procdefcoll : pprocdefcoll;
  548. begin
  549. { disposes the above generated tree }
  550. symcoll:=wurzel;
  551. while assigned(symcoll) do
  552. begin
  553. wurzel:=symcoll^.next;
  554. stringdispose(symcoll^.name);
  555. procdefcoll:=symcoll^.data;
  556. while assigned(procdefcoll) do
  557. begin
  558. symcoll^.data:=procdefcoll^.next;
  559. dispose(procdefcoll);
  560. procdefcoll:=symcoll^.data;
  561. end;
  562. dispose(symcoll);
  563. symcoll:=wurzel;
  564. end;
  565. end;
  566. procedure genvmt(list : paasmoutput;_class : pobjectdef);
  567. procedure do_genvmt(p : pobjectdef);
  568. begin
  569. { start with the base class }
  570. if assigned(p^.childof) then
  571. do_genvmt(p^.childof);
  572. { walk through all public syms }
  573. { I had to change that to solve bug0260 (PM)}
  574. { _c:=p; }
  575. _c:=_class;
  576. { Florian, please check if you agree (PM) }
  577. { no it wasn't correct, but I fixed it at }
  578. { another place: your fix hides only a bug }
  579. { _c is only used to give correct warnings }
  580. p^.symtable^.foreach({$ifdef FPCPROCVAR}@{$endif}eachsym);
  581. end;
  582. var
  583. symcoll : psymcoll;
  584. procdefcoll : pprocdefcoll;
  585. i : longint;
  586. begin
  587. wurzel:=nil;
  588. nextvirtnumber:=0;
  589. has_constructor:=false;
  590. has_virtual_method:=false;
  591. { generates a tree of all used methods }
  592. do_genvmt(_class);
  593. if has_virtual_method and not(has_constructor) then
  594. Message1(parser_w_virtual_without_constructor,_class^.objname^);
  595. { generates the VMT }
  596. { walk trough all numbers for virtual methods and search }
  597. { the method }
  598. for i:=0 to nextvirtnumber-1 do
  599. begin
  600. symcoll:=wurzel;
  601. { walk trough all symbols }
  602. while assigned(symcoll) do
  603. begin
  604. { walk trough all methods }
  605. procdefcoll:=symcoll^.data;
  606. while assigned(procdefcoll) do
  607. begin
  608. { writes the addresses to the VMT }
  609. { but only this which are declared as virtual }
  610. if procdefcoll^.data^.extnumber=i then
  611. begin
  612. if (po_virtualmethod in procdefcoll^.data^.procoptions) then
  613. begin
  614. { if a method is abstract, then is also the }
  615. { class abstract and it's not allow to }
  616. { generates an instance }
  617. if (po_abstractmethod in procdefcoll^.data^.procoptions) then
  618. begin
  619. include(_class^.objectoptions,oo_has_abstract);
  620. list^.concat(new(pai_const_symbol,initname('FPC_ABSTRACTERROR')));
  621. end
  622. else
  623. begin
  624. list^.concat(new(pai_const_symbol,
  625. initname(procdefcoll^.data^.mangledname)));
  626. end;
  627. end;
  628. end;
  629. procdefcoll:=procdefcoll^.next;
  630. end;
  631. symcoll:=symcoll^.next;
  632. end;
  633. end;
  634. disposevmttree;
  635. end;
  636. function gintfgetvtbllabelname(_class: pobjectdef; intfindex: integer): string;
  637. begin
  638. gintfgetvtbllabelname:='_$$_'+upper(_class^.objname^)+'_$$_'+
  639. upper(_class^.implementedinterfaces^.interfaces(intfindex)^.objname^)+'_$$_VTBL';
  640. end;
  641. procedure gintfcreatevtbl(_class: pobjectdef; intfindex: integer; rawdata,rawcode: paasmoutput);
  642. var
  643. implintf: pimplementedinterfaces;
  644. curintf: pobjectdef;
  645. count: integer;
  646. tmps: string;
  647. i: longint;
  648. begin
  649. implintf:=_class^.implementedinterfaces;
  650. curintf:=implintf^.interfaces(intfindex);
  651. rawdata^.concat(new(pai_symbol,initname(gintfgetvtbllabelname(_class,intfindex),0)));
  652. count:=implintf^.implproccount(intfindex);
  653. for i:=1 to count do
  654. begin
  655. tmps:=implintf^.implprocs(intfindex,i)^.mangledname+'_$$_'+upper(curintf^.objname^);
  656. { create wrapper code }
  657. cgintfwrapper(rawcode,implintf^.implprocs(intfindex,i),tmps,implintf^.ioffsets(intfindex)^);
  658. { create reference }
  659. rawdata^.concat(new(pai_const_symbol,initname(tmps)));
  660. end;
  661. end;
  662. procedure gintfgenentry(_class: pobjectdef; intfindex, contintfindex: integer; rawdata: paasmoutput);
  663. var
  664. implintf: pimplementedinterfaces;
  665. curintf: pobjectdef;
  666. tmplabel: pasmlabel;
  667. i: longint;
  668. begin
  669. implintf:=_class^.implementedinterfaces;
  670. curintf:=implintf^.interfaces(intfindex);
  671. { GUID }
  672. if curintf^.objecttype in [odt_interfacecom] then
  673. begin
  674. { label for GUID }
  675. getdatalabel(tmplabel);
  676. rawdata^.concat(new(pai_label,init(tmplabel)));
  677. rawdata^.concat(new(pai_const,init_32bit(curintf^.iidguid.D1)));
  678. rawdata^.concat(new(pai_const,init_16bit(curintf^.iidguid.D2)));
  679. rawdata^.concat(new(pai_const,init_16bit(curintf^.iidguid.D3)));
  680. for i:=Low(curintf^.iidguid.D4) to High(curintf^.iidguid.D4) do
  681. rawdata^.concat(new(pai_const,init_8bit(curintf^.iidguid.D4[i])));
  682. datasegment^.concat(new(pai_const_symbol,init(tmplabel)));
  683. end
  684. else
  685. begin
  686. { nil for Corba interfaces }
  687. datasegment^.concat(new(pai_const,init_32bit(0))); { nil }
  688. end;
  689. { VTable }
  690. datasegment^.concat(new(pai_const_symbol,initname(gintfgetvtbllabelname(_class,contintfindex))));
  691. { IOffset field }
  692. datasegment^.concat(new(pai_const,init_32bit(implintf^.ioffsets(contintfindex)^)));
  693. { IIDStr }
  694. getdatalabel(tmplabel);
  695. rawdata^.concat(new(pai_label,init(tmplabel)));
  696. rawdata^.concat(new(pai_const,init_8bit(length(curintf^.iidstr^))));
  697. if curintf^.objecttype=odt_interfacecom then
  698. rawdata^.concat(new(pai_string,init(upper(curintf^.iidstr^))))
  699. else
  700. rawdata^.concat(new(pai_string,init(curintf^.iidstr^)));
  701. datasegment^.concat(new(pai_const_symbol,init(tmplabel)));
  702. end;
  703. procedure gintfoptimizevtbls(_class: pobjectdef; implvtbl : plongint);
  704. type
  705. tcompintfentry = record
  706. weight: longint;
  707. compintf: longint;
  708. end;
  709. { Max 1000 interface in the class header interfaces it's enough imho }
  710. tcompintfs = {$ifndef tp} packed {$endif} array[1..1000] of tcompintfentry;
  711. pcompintfs = ^tcompintfs;
  712. tequals = {$ifndef tp} packed {$endif} array[1..1000] of longint;
  713. pequals = ^tequals;
  714. var
  715. max: longint;
  716. equals: pequals;
  717. compats: pcompintfs;
  718. i: longint;
  719. j: longint;
  720. w: longint;
  721. cij: boolean;
  722. cji: boolean;
  723. begin
  724. max:=_class^.implementedinterfaces^.count;
  725. if max>High(tequals) then
  726. Internalerror(200006135);
  727. getmem(compats,sizeof(tcompintfentry)*max);
  728. getmem(equals,sizeof(longint)*max);
  729. fillchar(compats^,sizeof(tcompintfentry)*max,0);
  730. fillchar(equals^,sizeof(longint)*max,0);
  731. { ismergepossible is a containing relation
  732. meaning of ismergepossible(a,b,w) =
  733. if implementorfunction map of a is contained implementorfunction map of b
  734. imp(a,b) and imp(b,c) => imp(a,c) ; imp(a,b) and imp(b,a) => a == b
  735. }
  736. { the order is very important for correct allocation }
  737. for i:=1 to max do
  738. begin
  739. for j:=i+1 to max do
  740. begin
  741. cij:=_class^.implementedinterfaces^.isimplmergepossible(i,j,w);
  742. cji:=_class^.implementedinterfaces^.isimplmergepossible(j,i,w);
  743. if cij and cji then { i equal j }
  744. begin
  745. { get minimum index of equal }
  746. if equals^[j]=0 then
  747. equals^[j]:=i;
  748. end
  749. else if cij then
  750. begin
  751. { get minimum index of maximum weight }
  752. if compats^[i].weight<w then
  753. begin
  754. compats^[i].weight:=w;
  755. compats^[i].compintf:=j;
  756. end;
  757. end
  758. else if cji then
  759. begin
  760. { get minimum index of maximum weight }
  761. if (compats^[j].weight<w) then
  762. begin
  763. compats^[j].weight:=w;
  764. compats^[j].compintf:=i;
  765. end;
  766. end;
  767. end;
  768. end;
  769. for i:=1 to max do
  770. begin
  771. if compats^[i].compintf<>0 then
  772. implvtbl[i]:=compats^[i].compintf
  773. else if equals^[i]<>0 then
  774. implvtbl[i]:=equals^[i]
  775. else
  776. implvtbl[i]:=i;
  777. end;
  778. freemem(compats,sizeof(tcompintfentry)*max);
  779. freemem(equals,sizeof(longint)*max);
  780. end;
  781. procedure gintfwritedata(_class: pobjectdef);
  782. var
  783. rawdata,rawcode: taasmoutput;
  784. impintfindexes: plongint;
  785. max: longint;
  786. i: longint;
  787. begin
  788. max:=_class^.implementedinterfaces^.count;
  789. getmem(impintfindexes,(max+1)*sizeof(longint));
  790. gintfoptimizevtbls(_class,impintfindexes);
  791. rawdata.init;
  792. rawcode.init;
  793. datasegment^.concat(new(pai_const,init_16bit(max)));
  794. { Two pass, one for allocation and vtbl creation }
  795. for i:=1 to max do
  796. begin
  797. if impintfindexes[i]=i then { if implement itself }
  798. begin
  799. { allocate a pointer in the object memory }
  800. with pstoredsymtable(_class^.symtable)^ do
  801. begin
  802. if (dataalignment>=target_os.size_of_pointer) then
  803. datasize:=align(datasize,dataalignment)
  804. else
  805. datasize:=align(datasize,target_os.size_of_pointer);
  806. _class^.implementedinterfaces^.ioffsets(i)^:=datasize;
  807. datasize:=datasize+target_os.size_of_pointer;
  808. end;
  809. { write vtbl }
  810. gintfcreatevtbl(_class,i,@rawdata,@rawcode);
  811. end;
  812. end;
  813. { second pass: for fill interfacetable and remained ioffsets }
  814. for i:=1 to max do
  815. begin
  816. if i<>impintfindexes[i] then { why execute x:=x ? }
  817. with _class^.implementedinterfaces^ do
  818. ioffsets(i)^:=ioffsets(impintfindexes[i])^;
  819. gintfgenentry(_class,i,impintfindexes[i],@rawdata);
  820. end;
  821. datasegment^.insertlist(@rawdata);
  822. rawdata.done;
  823. if (cs_create_smart in aktmoduleswitches) then
  824. rawcode.insert(new(pai_cut,init));
  825. codesegment^.insertlist(@rawcode);
  826. rawcode.done;
  827. freemem(impintfindexes,(max+1)*sizeof(longint));
  828. end;
  829. function gintfgetcprocdef(_class: pobjectdef; proc: pprocdef;const name: string): pprocdef;
  830. var
  831. sym: pprocsym;
  832. implprocdef: pprocdef;
  833. begin
  834. implprocdef:=nil;
  835. sym:=pprocsym(search_class_member(_class,name));
  836. if assigned(sym) and (sym^.typ=procsym) and not (sp_private in sym^.symoptions) then
  837. begin
  838. implprocdef:=sym^.definition;
  839. while assigned(implprocdef) and not equal_paras(proc^.para,implprocdef^.para,cp_none) and
  840. (proc^.proccalloptions<>implprocdef^.proccalloptions) do
  841. implprocdef:=implprocdef^.nextoverloaded;
  842. end;
  843. gintfgetcprocdef:=implprocdef;
  844. end;
  845. procedure gintfdoonintf(intf, _class: pobjectdef; intfindex: longint);
  846. var
  847. i: longint;
  848. proc: pprocdef;
  849. procname: string; { for error }
  850. mappedname: string;
  851. nextexist: pointer;
  852. implprocdef: pprocdef;
  853. begin
  854. for i:=1 to intf^.symtable^.defindex^.count do
  855. begin
  856. proc:=pprocdef(intf^.symtable^.defindex^.search(i));
  857. if proc^.deftype=procdef then
  858. begin
  859. procname:='';
  860. implprocdef:=nil;
  861. nextexist:=nil;
  862. repeat
  863. mappedname:=_class^.implementedinterfaces^.getmappings(intfindex,proc^.procsym^.name,nextexist);
  864. if procname='' then
  865. procname:=mappedname; { for error messages }
  866. if mappedname<>'' then
  867. implprocdef:=gintfgetcprocdef(_class,proc,mappedname);
  868. until assigned(implprocdef) or not assigned(nextexist);
  869. if not assigned(implprocdef) then
  870. implprocdef:=gintfgetcprocdef(_class,proc,proc^.procsym^.name);
  871. if procname='' then
  872. procname:=proc^.procsym^.name;
  873. if assigned(implprocdef) then
  874. _class^.implementedinterfaces^.addimplproc(intfindex,implprocdef)
  875. else
  876. Message1(sym_e_id_not_found,procname);
  877. end;
  878. end;
  879. end;
  880. procedure gintfwalkdowninterface(intf, _class: pobjectdef; intfindex: longint);
  881. begin
  882. if assigned(intf^.childof) then
  883. gintfwalkdowninterface(intf^.childof,_class,intfindex);
  884. gintfdoonintf(intf,_class,intfindex);
  885. end;
  886. function genintftable(_class: pobjectdef): pasmlabel;
  887. var
  888. intfindex: longint;
  889. curintf: pobjectdef;
  890. intftable: pasmlabel;
  891. begin
  892. { 1. step collect implementor functions into the implementedinterfaces^.implprocs }
  893. for intfindex:=1 to _class^.implementedinterfaces^.count do
  894. begin
  895. curintf:=_class^.implementedinterfaces^.interfaces(intfindex);
  896. gintfwalkdowninterface(curintf,_class,intfindex);
  897. end;
  898. { 2. step calc required fieldcount and their offsets in the object memory map
  899. and write data }
  900. getdatalabel(intftable);
  901. datasegment^.concat(new(pai_label,init(intftable)));
  902. gintfwritedata(_class);
  903. _class^.implementedinterfaces^.clearimplprocs; { release temporary information }
  904. genintftable:=intftable;
  905. end;
  906. { Write interface identifiers to the data section }
  907. procedure writeinterfaceids(c : pobjectdef);
  908. var
  909. i: longint;
  910. s1,s2 : string;
  911. begin
  912. if c^.owner^.name=nil then
  913. s1:=''
  914. else
  915. s1:=upper(c^.owner^.name^);
  916. if c^.objname=nil then
  917. s2:=''
  918. else
  919. s2:=upper(c^.objname^);
  920. s1:=s1+'$_'+s2;
  921. if c^.isiidguidvalid then
  922. begin
  923. if (cs_create_smart in aktmoduleswitches) then
  924. datasegment^.concat(new(pai_cut,init));
  925. datasegment^.concat(new(pai_symbol,initname_global('IID$_'+s1,0)));
  926. datasegment^.concat(new(pai_const,init_32bit(c^.iidguid.D1)));
  927. datasegment^.concat(new(pai_const,init_16bit(c^.iidguid.D2)));
  928. datasegment^.concat(new(pai_const,init_16bit(c^.iidguid.D3)));
  929. for i:=Low(c^.iidguid.D4) to High(c^.iidguid.D4) do
  930. datasegment^.concat(new(pai_const,init_8bit(c^.iidguid.D4[i])));
  931. end;
  932. if (cs_create_smart in aktmoduleswitches) then
  933. datasegment^.concat(new(pai_cut,init));
  934. datasegment^.concat(new(pai_symbol,initname_global('IIDSTR$_'+s1,0)));
  935. datasegment^.concat(new(pai_const,init_8bit(length(c^.iidstr^))));
  936. datasegment^.concat(new(pai_string,init(c^.iidstr^)));
  937. end;
  938. end.
  939. {
  940. $Log$
  941. Revision 1.13 2000-11-08 00:07:40 florian
  942. * potential range check error fixed
  943. Revision 1.12 2000/11/06 23:13:53 peter
  944. * uppercase manglednames
  945. Revision 1.11 2000/11/04 17:31:00 florian
  946. * fixed some problems of previous commit
  947. Revision 1.10 2000/11/04 14:25:19 florian
  948. + merged Attila's changes for interfaces, not tested yet
  949. Revision 1.9 2000/11/01 23:04:37 peter
  950. * tprocdef.fullprocname added for better casesensitve writing of
  951. procedures
  952. Revision 1.8 2000/10/31 22:02:47 peter
  953. * symtable splitted, no real code changes
  954. Revision 1.7 2000/10/14 10:14:47 peter
  955. * moehrendorf oct 2000 rewrite
  956. Revision 1.6 2000/09/24 21:19:50 peter
  957. * delphi compile fixes
  958. Revision 1.5 2000/09/24 15:06:17 peter
  959. * use defines.inc
  960. Revision 1.4 2000/08/27 16:11:51 peter
  961. * moved some util functions from globals,cobjects to cutils
  962. * splitted files into finput,fmodule
  963. Revision 1.3 2000/07/13 12:08:26 michael
  964. + patched to 1.1.0 with former 1.09patch from peter
  965. Revision 1.2 2000/07/13 11:32:41 michael
  966. + removed logs
  967. }