hcgdata.pas 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  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 : TAAsmoutput;_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,cclasses,cobjects,
  45. globtype,globals,verbose,
  46. symtable,symconst,symtype,symsym,types,
  47. systems
  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(Tai_label.Create(p^.nl));
  160. dataSegment.concat(Tai_const.Create_8bit(strlen(p^.p^.messageinf.str)));
  161. dataSegment.concat(Tai_string.Create_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(Tai_const_symbol.Create(p^.nl));
  171. dataSegment.concat(Tai_const_symbol.Createname(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(Tai_label.Create(r));
  189. genstrmsgtab:=r;
  190. dataSegment.concat(Tai_const.Create_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(Tai_const.Create_32bit(p^.p^.messageinf.i));
  203. dataSegment.concat(Tai_const_symbol.Createname(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(Tai_label.Create(r));
  218. genintmsgtab:=r;
  219. dataSegment.concat(Tai_const.Create_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(Tai_const.Create_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(Tai_const_symbol.Createname(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(Tai_label.Create(r));
  279. { entries for caching }
  280. dataSegment.concat(Tai_const.Create_32bit(0));
  281. dataSegment.concat(Tai_const.Create_32bit(0));
  282. dataSegment.concat(Tai_const.Create_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(Tai_label.Create(l));
  309. Consts.concat(Tai_const.Create_8bit(length(p^.name)));
  310. Consts.concat(Tai_string.Create(p^.name));
  311. dataSegment.concat(Tai_const_symbol.Create(l));
  312. dataSegment.concat(Tai_const_symbol.Createname(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(Tai_label.Create(l));
  325. dataSegment.concat(Tai_const.Create_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. Message2(parser_e_overridden_methods_not_same_ret,hp^.fullprocnamewithret,
  505. procdefcoll^.data^.fullprocnamewithret);
  506. { now set the number }
  507. hp^.extnumber:=procdefcoll^.data^.extnumber;
  508. { and exchange }
  509. procdefcoll^.data:=hp;
  510. stored:=true;
  511. goto handlenextdef;
  512. end; { same parameters }
  513. procdefcoll:=procdefcoll^.next;
  514. end;
  515. { if it isn't saved in the list }
  516. { we create a new entry }
  517. if not(stored) then
  518. begin
  519. new(procdefcoll);
  520. procdefcoll^.data:=hp;
  521. procdefcoll^.next:=symcoll^.data;
  522. symcoll^.data:=procdefcoll;
  523. { if the method is virtual ... }
  524. if (po_virtualmethod in hp^.procoptions) then
  525. begin
  526. { ... it will get a number }
  527. hp^.extnumber:=nextvirtnumber;
  528. inc(nextvirtnumber);
  529. end;
  530. { check, if a method should be overridden }
  531. if (po_overridingmethod in hp^.procoptions) then
  532. MessagePos1(hp^.fileinfo,parser_e_nothing_to_be_overridden,
  533. hp^.fullprocname);
  534. end;
  535. handlenextdef:
  536. hp:=hp^.nextoverloaded;
  537. end;
  538. exit;
  539. end;
  540. symcoll:=symcoll^.next;
  541. end;
  542. newentry;
  543. end;
  544. end;
  545. procedure disposevmttree;
  546. var
  547. symcoll : psymcoll;
  548. procdefcoll : pprocdefcoll;
  549. begin
  550. { disposes the above generated tree }
  551. symcoll:=wurzel;
  552. while assigned(symcoll) do
  553. begin
  554. wurzel:=symcoll^.next;
  555. stringdispose(symcoll^.name);
  556. procdefcoll:=symcoll^.data;
  557. while assigned(procdefcoll) do
  558. begin
  559. symcoll^.data:=procdefcoll^.next;
  560. dispose(procdefcoll);
  561. procdefcoll:=symcoll^.data;
  562. end;
  563. dispose(symcoll);
  564. symcoll:=wurzel;
  565. end;
  566. end;
  567. procedure genvmt(list : TAAsmoutput;_class : pobjectdef);
  568. procedure do_genvmt(p : pobjectdef);
  569. begin
  570. { start with the base class }
  571. if assigned(p^.childof) then
  572. do_genvmt(p^.childof);
  573. { walk through all public syms }
  574. { I had to change that to solve bug0260 (PM)}
  575. { _c:=p; }
  576. _c:=_class;
  577. { Florian, please check if you agree (PM) }
  578. { no it wasn't correct, but I fixed it at }
  579. { another place: your fix hides only a bug }
  580. { _c is only used to give correct warnings }
  581. p^.symtable^.foreach({$ifdef FPCPROCVAR}@{$endif}eachsym);
  582. end;
  583. var
  584. symcoll : psymcoll;
  585. procdefcoll : pprocdefcoll;
  586. i : longint;
  587. begin
  588. wurzel:=nil;
  589. nextvirtnumber:=0;
  590. has_constructor:=false;
  591. has_virtual_method:=false;
  592. { generates a tree of all used methods }
  593. do_genvmt(_class);
  594. if has_virtual_method and not(has_constructor) then
  595. Message1(parser_w_virtual_without_constructor,_class^.objname^);
  596. { generates the VMT }
  597. { walk trough all numbers for virtual methods and search }
  598. { the method }
  599. for i:=0 to nextvirtnumber-1 do
  600. begin
  601. symcoll:=wurzel;
  602. { walk trough all symbols }
  603. while assigned(symcoll) do
  604. begin
  605. { walk trough all methods }
  606. procdefcoll:=symcoll^.data;
  607. while assigned(procdefcoll) do
  608. begin
  609. { writes the addresses to the VMT }
  610. { but only this which are declared as virtual }
  611. if procdefcoll^.data^.extnumber=i then
  612. begin
  613. if (po_virtualmethod in procdefcoll^.data^.procoptions) then
  614. begin
  615. { if a method is abstract, then is also the }
  616. { class abstract and it's not allow to }
  617. { generates an instance }
  618. if (po_abstractmethod in procdefcoll^.data^.procoptions) then
  619. begin
  620. include(_class^.objectoptions,oo_has_abstract);
  621. List.concat(Tai_const_symbol.Createname('FPC_ABSTRACTERROR'));
  622. end
  623. else
  624. begin
  625. List.concat(Tai_const_symbol.createname(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: TAAsmoutput);
  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(Tai_symbol.Createname(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(Tai_const_symbol.Createname(tmps));
  660. end;
  661. end;
  662. procedure gintfgenentry(_class: pobjectdef; intfindex, contintfindex: integer; rawdata: TAAsmoutput);
  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(Tai_label.Create(tmplabel));
  677. rawdata.concat(Tai_const.Create_32bit(curintf^.iidguid.D1));
  678. rawdata.concat(Tai_const.Create_16bit(curintf^.iidguid.D2));
  679. rawdata.concat(Tai_const.Create_16bit(curintf^.iidguid.D3));
  680. for i:=Low(curintf^.iidguid.D4) to High(curintf^.iidguid.D4) do
  681. rawdata.concat(Tai_const.Create_8bit(curintf^.iidguid.D4[i]));
  682. dataSegment.concat(Tai_const_symbol.Create(tmplabel));
  683. end
  684. else
  685. begin
  686. { nil for Corba interfaces }
  687. dataSegment.concat(Tai_const.Create_32bit(0)); { nil }
  688. end;
  689. { VTable }
  690. dataSegment.concat(Tai_const_symbol.Createname(gintfgetvtbllabelname(_class,contintfindex)));
  691. { IOffset field }
  692. dataSegment.concat(Tai_const.Create_32bit(implintf^.ioffsets(contintfindex)^));
  693. { IIDStr }
  694. getdatalabel(tmplabel);
  695. rawdata.concat(Tai_label.Create(tmplabel));
  696. rawdata.concat(Tai_const.Create_8bit(length(curintf^.iidstr^)));
  697. if curintf^.objecttype=odt_interfacecom then
  698. rawdata.concat(Tai_string.Create(upper(curintf^.iidstr^)))
  699. else
  700. rawdata.concat(Tai_string.Create(curintf^.iidstr^));
  701. dataSegment.concat(Tai_const_symbol.Create(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:=TAAsmOutput.Create;
  792. rawcode:=TAAsmOutput.Create;
  793. dataSegment.concat(Tai_const.Create_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.free;
  823. if (cs_create_smart in aktmoduleswitches) then
  824. rawcode.insert(Tai_cut.Create);
  825. codeSegment.insertlist(rawcode);
  826. rawcode.free;
  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(Tai_label.Create(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(Tai_cut.Create);
  925. dataSegment.concat(Tai_symbol.Createname_global('IID$_'+s1,0));
  926. dataSegment.concat(Tai_const.Create_32bit(c^.iidguid.D1));
  927. dataSegment.concat(Tai_const.Create_16bit(c^.iidguid.D2));
  928. dataSegment.concat(Tai_const.Create_16bit(c^.iidguid.D3));
  929. for i:=Low(c^.iidguid.D4) to High(c^.iidguid.D4) do
  930. dataSegment.concat(Tai_const.Create_8bit(c^.iidguid.D4[i]));
  931. end;
  932. if (cs_create_smart in aktmoduleswitches) then
  933. dataSegment.concat(Tai_cut.Create);
  934. dataSegment.concat(Tai_symbol.Createname_global('IIDSTR$_'+s1,0));
  935. dataSegment.concat(Tai_const.Create_8bit(length(c^.iidstr^)));
  936. dataSegment.concat(Tai_string.Create(c^.iidstr^));
  937. end;
  938. end.
  939. {
  940. $Log$
  941. Revision 1.17 2000-12-25 00:07:26 peter
  942. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  943. tlinkedlist objects)
  944. Revision 1.16 2000/11/29 00:30:30 florian
  945. * unused units removed from uses clause
  946. * some changes for widestrings
  947. Revision 1.15 2000/11/19 16:23:35 florian
  948. *** empty log message ***
  949. Revision 1.14 2000/11/12 23:24:10 florian
  950. * interfaces are basically running
  951. Revision 1.13 2000/11/08 00:07:40 florian
  952. * potential range check error fixed
  953. Revision 1.12 2000/11/06 23:13:53 peter
  954. * uppercase manglednames
  955. Revision 1.11 2000/11/04 17:31:00 florian
  956. * fixed some problems of previous commit
  957. Revision 1.10 2000/11/04 14:25:19 florian
  958. + merged Attila's changes for interfaces, not tested yet
  959. Revision 1.9 2000/11/01 23:04:37 peter
  960. * tprocdef.fullprocname added for better casesensitve writing of
  961. procedures
  962. Revision 1.8 2000/10/31 22:02:47 peter
  963. * symtable splitted, no real code changes
  964. Revision 1.7 2000/10/14 10:14:47 peter
  965. * moehrendorf oct 2000 rewrite
  966. Revision 1.6 2000/09/24 21:19:50 peter
  967. * delphi compile fixes
  968. Revision 1.5 2000/09/24 15:06:17 peter
  969. * use defines.inc
  970. Revision 1.4 2000/08/27 16:11:51 peter
  971. * moved some util functions from globals,cobjects to cutils
  972. * splitted files into finput,fmodule
  973. Revision 1.3 2000/07/13 12:08:26 michael
  974. + patched to 1.1.0 with former 1.09patch from peter
  975. Revision 1.2 2000/07/13 11:32:41 michael
  976. + removed logs
  977. }