hcgdata.pas 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  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:='_$$_'+_class^.objname^+'_$$_'+
  639. _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+'_$$_'+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. type
  704. tlongintarr = array[0..0] of longint;
  705. plongintarr = ^tlongintarr;
  706. procedure gintfoptimizevtbls(_class: pobjectdef; var implvtbl: tlongintarr);
  707. type
  708. tcompintfentry = record
  709. weight: longint;
  710. compintf: longint;
  711. end;
  712. { Max 1000 interface in the class header interfaces it's enough imho }
  713. tcompintfs = {$ifndef tp} packed {$endif} array[1..1000] of tcompintfentry;
  714. pcompintfs = ^tcompintfs;
  715. tequals = {$ifndef tp} packed {$endif} array[1..1000] of longint;
  716. pequals = ^tequals;
  717. var
  718. max: longint;
  719. equals: pequals;
  720. compats: pcompintfs;
  721. i: longint;
  722. j: longint;
  723. w: longint;
  724. cij: boolean;
  725. cji: boolean;
  726. begin
  727. max:=_class^.implementedinterfaces^.count;
  728. if max>High(tequals) then
  729. Internalerror(200006135);
  730. getmem(compats,sizeof(tcompintfentry)*max);
  731. getmem(equals,sizeof(longint)*max);
  732. fillchar(compats^,sizeof(tcompintfentry)*max,0);
  733. fillchar(equals^,sizeof(longint)*max,0);
  734. { ismergepossible is a containing relation
  735. meaning of ismergepossible(a,b,w) =
  736. if implementorfunction map of a is contained implementorfunction map of b
  737. imp(a,b) and imp(b,c) => imp(a,c) ; imp(a,b) and imp(b,a) => a == b
  738. }
  739. { the order is very important for correct allocation }
  740. for i:=1 to max do
  741. begin
  742. for j:=i+1 to max do
  743. begin
  744. cij:=_class^.implementedinterfaces^.isimplmergepossible(i,j,w);
  745. cji:=_class^.implementedinterfaces^.isimplmergepossible(j,i,w);
  746. if cij and cji then { i equal j }
  747. begin
  748. { get minimum index of equal }
  749. if equals^[j]=0 then
  750. equals^[j]:=i;
  751. end
  752. else if cij then
  753. begin
  754. { get minimum index of maximum weight }
  755. if compats^[i].weight<w then
  756. begin
  757. compats^[i].weight:=w;
  758. compats^[i].compintf:=j;
  759. end;
  760. end
  761. else if cji then
  762. begin
  763. { get minimum index of maximum weight }
  764. if (compats^[j].weight<w) then
  765. begin
  766. compats^[j].weight:=w;
  767. compats^[j].compintf:=i;
  768. end;
  769. end;
  770. end;
  771. end;
  772. for i:=1 to max do
  773. begin
  774. if compats^[i].compintf<>0 then
  775. implvtbl[i]:=compats^[i].compintf
  776. else if equals^[i]<>0 then
  777. implvtbl[i]:=equals^[i]
  778. else
  779. implvtbl[i]:=i;
  780. end;
  781. freemem(compats,sizeof(tcompintfentry)*max);
  782. freemem(equals,sizeof(longint)*max);
  783. end;
  784. procedure gintfwritedata(_class: pobjectdef);
  785. var
  786. rawdata,rawcode: taasmoutput;
  787. impintfindexes: plongintarr;
  788. max: longint;
  789. i: longint;
  790. begin
  791. max:=_class^.implementedinterfaces^.count;
  792. getmem(impintfindexes,(max+1)*sizeof(longint));
  793. gintfoptimizevtbls(_class,impintfindexes^);
  794. rawdata.init;
  795. rawcode.init;
  796. datasegment^.concat(new(pai_const,init_16bit(max)));
  797. { Two pass, one for allocation and vtbl creation }
  798. for i:=1 to max do
  799. begin
  800. if impintfindexes^[i]=i then { if implement itself }
  801. begin
  802. { allocate a pointer in the object memory }
  803. with pstoredsymtable(_class^.symtable)^ do
  804. begin
  805. if (dataalignment>=target_os.size_of_pointer) then
  806. datasize:=align(datasize,dataalignment)
  807. else
  808. datasize:=align(datasize,target_os.size_of_pointer);
  809. _class^.implementedinterfaces^.ioffsets(i)^:=datasize;
  810. datasize:=datasize+target_os.size_of_pointer;
  811. end;
  812. { write vtbl }
  813. gintfcreatevtbl(_class,i,@rawdata,@rawcode);
  814. end;
  815. end;
  816. { second pass: for fill interfacetable and remained ioffsets }
  817. for i:=1 to max do
  818. begin
  819. if i<>impintfindexes^[i] then { why execute x:=x ? }
  820. with _class^.implementedinterfaces^ do ioffsets(i)^:=ioffsets(impintfindexes^[i])^;
  821. gintfgenentry(_class,i,impintfindexes^[i],@rawdata);
  822. end;
  823. datasegment^.insertlist(@rawdata);
  824. rawdata.done;
  825. if (cs_create_smart in aktmoduleswitches) then
  826. rawcode.insert(new(pai_cut,init));
  827. codesegment^.insertlist(@rawcode);
  828. rawcode.done;
  829. freemem(impintfindexes,(max+1)*sizeof(longint));
  830. end;
  831. function gintfgetcprocdef(_class: pobjectdef; proc: pprocdef;const name: string): pprocdef;
  832. var
  833. sym: pprocsym;
  834. implprocdef: pprocdef;
  835. begin
  836. implprocdef:=nil;
  837. sym:=pprocsym(search_class_member(_class,name));
  838. if assigned(sym) and (sym^.typ=procsym) and not (sp_private in sym^.symoptions) then
  839. begin
  840. implprocdef:=sym^.definition;
  841. while assigned(implprocdef) and not equal_paras(proc^.para,implprocdef^.para,cp_none) and
  842. (proc^.proccalloptions<>implprocdef^.proccalloptions) do
  843. implprocdef:=implprocdef^.nextoverloaded;
  844. end;
  845. gintfgetcprocdef:=implprocdef;
  846. end;
  847. procedure gintfdoonintf(intf, _class: pobjectdef; intfindex: longint);
  848. var
  849. i: longint;
  850. proc: pprocdef;
  851. procname: string; { for error }
  852. mappedname: string;
  853. nextexist: pointer;
  854. implprocdef: pprocdef;
  855. begin
  856. for i:=1 to intf^.symtable^.defindex^.count do
  857. begin
  858. proc:=pprocdef(intf^.symtable^.defindex^.search(i));
  859. if proc^.deftype=procdef then
  860. begin
  861. procname:='';
  862. implprocdef:=nil;
  863. nextexist:=nil;
  864. repeat
  865. mappedname:=_class^.implementedinterfaces^.getmappings(intfindex,proc^.procsym^.name,nextexist);
  866. if procname='' then
  867. procname:=mappedname; { for error messages }
  868. if mappedname<>'' then
  869. implprocdef:=gintfgetcprocdef(_class,proc,mappedname);
  870. until assigned(implprocdef) or not assigned(nextexist);
  871. if not assigned(implprocdef) then
  872. implprocdef:=gintfgetcprocdef(_class,proc,proc^.procsym^.name);
  873. if procname='' then
  874. procname:=proc^.procsym^.name;
  875. if assigned(implprocdef) then
  876. _class^.implementedinterfaces^.addimplproc(intfindex,implprocdef)
  877. else
  878. Message1(sym_e_id_not_found,procname);
  879. end;
  880. end;
  881. end;
  882. procedure gintfwalkdowninterface(intf, _class: pobjectdef; intfindex: longint);
  883. begin
  884. if assigned(intf^.childof) then
  885. gintfwalkdowninterface(intf^.childof,_class,intfindex);
  886. gintfdoonintf(intf,_class,intfindex);
  887. end;
  888. function genintftable(_class: pobjectdef): pasmlabel;
  889. var
  890. intfindex: longint;
  891. curintf: pobjectdef;
  892. intftable: pasmlabel;
  893. begin
  894. { 1. step collect implementor functions into the implementedinterfaces^.implprocs }
  895. for intfindex:=1 to _class^.implementedinterfaces^.count do
  896. begin
  897. curintf:=_class^.implementedinterfaces^.interfaces(intfindex);
  898. gintfwalkdowninterface(curintf,_class,intfindex);
  899. end;
  900. { 2. step calc required fieldcount and their offsets in the object memory map
  901. and write data }
  902. getdatalabel(intftable);
  903. datasegment^.concat(new(pai_label,init(intftable)));
  904. gintfwritedata(_class);
  905. _class^.implementedinterfaces^.clearimplprocs; { release temporary information }
  906. genintftable:=intftable;
  907. end;
  908. { Write interface identifiers to the data section }
  909. procedure writeinterfaceids(c : pobjectdef);
  910. var
  911. i: longint;
  912. begin
  913. if c^.isiidguidvalid then
  914. begin
  915. if (cs_create_smart in aktmoduleswitches) then
  916. datasegment^.concat(new(pai_cut,init));
  917. datasegment^.concat(new(pai_symbol,initname_global(c^.vmt_mangledname+'$_IID',0)));
  918. datasegment^.concat(new(pai_const,init_32bit(c^.iidguid.D1)));
  919. datasegment^.concat(new(pai_const,init_16bit(c^.iidguid.D2)));
  920. datasegment^.concat(new(pai_const,init_16bit(c^.iidguid.D3)));
  921. for i:=Low(c^.iidguid.D4) to High(c^.iidguid.D4) do
  922. datasegment^.concat(new(pai_const,init_8bit(c^.iidguid.D4[i])));
  923. end;
  924. if (cs_create_smart in aktmoduleswitches) then
  925. datasegment^.concat(new(pai_cut,init));
  926. datasegment^.concat(new(pai_symbol,initname_global(c^.vmt_mangledname+'$_IIDSTR',0)));
  927. datasegment^.concat(new(pai_const,init_8bit(length(c^.iidstr^))));
  928. datasegment^.concat(new(pai_string,init(c^.iidstr^)));
  929. end;
  930. end.
  931. {
  932. $Log$
  933. Revision 1.10 2000-11-04 14:25:19 florian
  934. + merged Attila's changes for interfaces, not tested yet
  935. Revision 1.9 2000/11/01 23:04:37 peter
  936. * tprocdef.fullprocname added for better casesensitve writing of
  937. procedures
  938. Revision 1.8 2000/10/31 22:02:47 peter
  939. * symtable splitted, no real code changes
  940. Revision 1.7 2000/10/14 10:14:47 peter
  941. * moehrendorf oct 2000 rewrite
  942. Revision 1.6 2000/09/24 21:19:50 peter
  943. * delphi compile fixes
  944. Revision 1.5 2000/09/24 15:06:17 peter
  945. * use defines.inc
  946. Revision 1.4 2000/08/27 16:11:51 peter
  947. * moved some util functions from globals,cobjects to cutils
  948. * splitted files into finput,fmodule
  949. Revision 1.3 2000/07/13 12:08:26 michael
  950. + patched to 1.1.0 with former 1.09patch from peter
  951. Revision 1.2 2000/07/13 11:32:41 michael
  952. + removed logs
  953. }