hcgdata.pas 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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. 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 : paasmoutput;_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(new(pai_const_symbol,initname('FPC_ABSTRACTERROR')));
  622. end
  623. else
  624. begin
  625. list^.concat(new(pai_const_symbol,
  626. initname(procdefcoll^.data^.mangledname)));
  627. end;
  628. end;
  629. end;
  630. procdefcoll:=procdefcoll^.next;
  631. end;
  632. symcoll:=symcoll^.next;
  633. end;
  634. end;
  635. disposevmttree;
  636. end;
  637. function gintfgetvtbllabelname(_class: pobjectdef; intfindex: integer): string;
  638. begin
  639. gintfgetvtbllabelname:='_$$_'+upper(_class^.objname^)+'_$$_'+
  640. upper(_class^.implementedinterfaces^.interfaces(intfindex)^.objname^)+'_$$_VTBL';
  641. end;
  642. procedure gintfcreatevtbl(_class: pobjectdef; intfindex: integer; rawdata,rawcode: paasmoutput);
  643. var
  644. implintf: pimplementedinterfaces;
  645. curintf: pobjectdef;
  646. count: integer;
  647. tmps: string;
  648. i: longint;
  649. begin
  650. implintf:=_class^.implementedinterfaces;
  651. curintf:=implintf^.interfaces(intfindex);
  652. rawdata^.concat(new(pai_symbol,initname(gintfgetvtbllabelname(_class,intfindex),0)));
  653. count:=implintf^.implproccount(intfindex);
  654. for i:=1 to count do
  655. begin
  656. tmps:=implintf^.implprocs(intfindex,i)^.mangledname+'_$$_'+upper(curintf^.objname^);
  657. { create wrapper code }
  658. cgintfwrapper(rawcode,implintf^.implprocs(intfindex,i),tmps,implintf^.ioffsets(intfindex)^);
  659. { create reference }
  660. rawdata^.concat(new(pai_const_symbol,initname(tmps)));
  661. end;
  662. end;
  663. procedure gintfgenentry(_class: pobjectdef; intfindex, contintfindex: integer; rawdata: paasmoutput);
  664. var
  665. implintf: pimplementedinterfaces;
  666. curintf: pobjectdef;
  667. tmplabel: pasmlabel;
  668. i: longint;
  669. begin
  670. implintf:=_class^.implementedinterfaces;
  671. curintf:=implintf^.interfaces(intfindex);
  672. { GUID }
  673. if curintf^.objecttype in [odt_interfacecom] then
  674. begin
  675. { label for GUID }
  676. getdatalabel(tmplabel);
  677. rawdata^.concat(new(pai_label,init(tmplabel)));
  678. rawdata^.concat(new(pai_const,init_32bit(curintf^.iidguid.D1)));
  679. rawdata^.concat(new(pai_const,init_16bit(curintf^.iidguid.D2)));
  680. rawdata^.concat(new(pai_const,init_16bit(curintf^.iidguid.D3)));
  681. for i:=Low(curintf^.iidguid.D4) to High(curintf^.iidguid.D4) do
  682. rawdata^.concat(new(pai_const,init_8bit(curintf^.iidguid.D4[i])));
  683. datasegment^.concat(new(pai_const_symbol,init(tmplabel)));
  684. end
  685. else
  686. begin
  687. { nil for Corba interfaces }
  688. datasegment^.concat(new(pai_const,init_32bit(0))); { nil }
  689. end;
  690. { VTable }
  691. datasegment^.concat(new(pai_const_symbol,initname(gintfgetvtbllabelname(_class,contintfindex))));
  692. { IOffset field }
  693. datasegment^.concat(new(pai_const,init_32bit(implintf^.ioffsets(contintfindex)^)));
  694. { IIDStr }
  695. getdatalabel(tmplabel);
  696. rawdata^.concat(new(pai_label,init(tmplabel)));
  697. rawdata^.concat(new(pai_const,init_8bit(length(curintf^.iidstr^))));
  698. if curintf^.objecttype=odt_interfacecom then
  699. rawdata^.concat(new(pai_string,init(upper(curintf^.iidstr^))))
  700. else
  701. rawdata^.concat(new(pai_string,init(curintf^.iidstr^)));
  702. datasegment^.concat(new(pai_const_symbol,init(tmplabel)));
  703. end;
  704. procedure gintfoptimizevtbls(_class: pobjectdef; implvtbl : plongint);
  705. type
  706. tcompintfentry = record
  707. weight: longint;
  708. compintf: longint;
  709. end;
  710. { Max 1000 interface in the class header interfaces it's enough imho }
  711. tcompintfs = {$ifndef tp} packed {$endif} array[1..1000] of tcompintfentry;
  712. pcompintfs = ^tcompintfs;
  713. tequals = {$ifndef tp} packed {$endif} array[1..1000] of longint;
  714. pequals = ^tequals;
  715. var
  716. max: longint;
  717. equals: pequals;
  718. compats: pcompintfs;
  719. i: longint;
  720. j: longint;
  721. w: longint;
  722. cij: boolean;
  723. cji: boolean;
  724. begin
  725. max:=_class^.implementedinterfaces^.count;
  726. if max>High(tequals) then
  727. Internalerror(200006135);
  728. getmem(compats,sizeof(tcompintfentry)*max);
  729. getmem(equals,sizeof(longint)*max);
  730. fillchar(compats^,sizeof(tcompintfentry)*max,0);
  731. fillchar(equals^,sizeof(longint)*max,0);
  732. { ismergepossible is a containing relation
  733. meaning of ismergepossible(a,b,w) =
  734. if implementorfunction map of a is contained implementorfunction map of b
  735. imp(a,b) and imp(b,c) => imp(a,c) ; imp(a,b) and imp(b,a) => a == b
  736. }
  737. { the order is very important for correct allocation }
  738. for i:=1 to max do
  739. begin
  740. for j:=i+1 to max do
  741. begin
  742. cij:=_class^.implementedinterfaces^.isimplmergepossible(i,j,w);
  743. cji:=_class^.implementedinterfaces^.isimplmergepossible(j,i,w);
  744. if cij and cji then { i equal j }
  745. begin
  746. { get minimum index of equal }
  747. if equals^[j]=0 then
  748. equals^[j]:=i;
  749. end
  750. else if cij then
  751. begin
  752. { get minimum index of maximum weight }
  753. if compats^[i].weight<w then
  754. begin
  755. compats^[i].weight:=w;
  756. compats^[i].compintf:=j;
  757. end;
  758. end
  759. else if cji then
  760. begin
  761. { get minimum index of maximum weight }
  762. if (compats^[j].weight<w) then
  763. begin
  764. compats^[j].weight:=w;
  765. compats^[j].compintf:=i;
  766. end;
  767. end;
  768. end;
  769. end;
  770. for i:=1 to max do
  771. begin
  772. if compats^[i].compintf<>0 then
  773. implvtbl[i]:=compats^[i].compintf
  774. else if equals^[i]<>0 then
  775. implvtbl[i]:=equals^[i]
  776. else
  777. implvtbl[i]:=i;
  778. end;
  779. freemem(compats,sizeof(tcompintfentry)*max);
  780. freemem(equals,sizeof(longint)*max);
  781. end;
  782. procedure gintfwritedata(_class: pobjectdef);
  783. var
  784. rawdata,rawcode: taasmoutput;
  785. impintfindexes: plongint;
  786. max: longint;
  787. i: longint;
  788. begin
  789. max:=_class^.implementedinterfaces^.count;
  790. getmem(impintfindexes,(max+1)*sizeof(longint));
  791. gintfoptimizevtbls(_class,impintfindexes);
  792. rawdata.init;
  793. rawcode.init;
  794. datasegment^.concat(new(pai_const,init_16bit(max)));
  795. { Two pass, one for allocation and vtbl creation }
  796. for i:=1 to max do
  797. begin
  798. if impintfindexes[i]=i then { if implement itself }
  799. begin
  800. { allocate a pointer in the object memory }
  801. with pstoredsymtable(_class^.symtable)^ do
  802. begin
  803. if (dataalignment>=target_os.size_of_pointer) then
  804. datasize:=align(datasize,dataalignment)
  805. else
  806. datasize:=align(datasize,target_os.size_of_pointer);
  807. _class^.implementedinterfaces^.ioffsets(i)^:=datasize;
  808. datasize:=datasize+target_os.size_of_pointer;
  809. end;
  810. { write vtbl }
  811. gintfcreatevtbl(_class,i,@rawdata,@rawcode);
  812. end;
  813. end;
  814. { second pass: for fill interfacetable and remained ioffsets }
  815. for i:=1 to max do
  816. begin
  817. if i<>impintfindexes[i] then { why execute x:=x ? }
  818. with _class^.implementedinterfaces^ do
  819. ioffsets(i)^:=ioffsets(impintfindexes[i])^;
  820. gintfgenentry(_class,i,impintfindexes[i],@rawdata);
  821. end;
  822. datasegment^.insertlist(@rawdata);
  823. rawdata.done;
  824. if (cs_create_smart in aktmoduleswitches) then
  825. rawcode.insert(new(pai_cut,init));
  826. codesegment^.insertlist(@rawcode);
  827. rawcode.done;
  828. freemem(impintfindexes,(max+1)*sizeof(longint));
  829. end;
  830. function gintfgetcprocdef(_class: pobjectdef; proc: pprocdef;const name: string): pprocdef;
  831. var
  832. sym: pprocsym;
  833. implprocdef: pprocdef;
  834. begin
  835. implprocdef:=nil;
  836. sym:=pprocsym(search_class_member(_class,name));
  837. if assigned(sym) and (sym^.typ=procsym) and not (sp_private in sym^.symoptions) then
  838. begin
  839. implprocdef:=sym^.definition;
  840. while assigned(implprocdef) and not equal_paras(proc^.para,implprocdef^.para,cp_none) and
  841. (proc^.proccalloptions<>implprocdef^.proccalloptions) do
  842. implprocdef:=implprocdef^.nextoverloaded;
  843. end;
  844. gintfgetcprocdef:=implprocdef;
  845. end;
  846. procedure gintfdoonintf(intf, _class: pobjectdef; intfindex: longint);
  847. var
  848. i: longint;
  849. proc: pprocdef;
  850. procname: string; { for error }
  851. mappedname: string;
  852. nextexist: pointer;
  853. implprocdef: pprocdef;
  854. begin
  855. for i:=1 to intf^.symtable^.defindex^.count do
  856. begin
  857. proc:=pprocdef(intf^.symtable^.defindex^.search(i));
  858. if proc^.deftype=procdef then
  859. begin
  860. procname:='';
  861. implprocdef:=nil;
  862. nextexist:=nil;
  863. repeat
  864. mappedname:=_class^.implementedinterfaces^.getmappings(intfindex,proc^.procsym^.name,nextexist);
  865. if procname='' then
  866. procname:=mappedname; { for error messages }
  867. if mappedname<>'' then
  868. implprocdef:=gintfgetcprocdef(_class,proc,mappedname);
  869. until assigned(implprocdef) or not assigned(nextexist);
  870. if not assigned(implprocdef) then
  871. implprocdef:=gintfgetcprocdef(_class,proc,proc^.procsym^.name);
  872. if procname='' then
  873. procname:=proc^.procsym^.name;
  874. if assigned(implprocdef) then
  875. _class^.implementedinterfaces^.addimplproc(intfindex,implprocdef)
  876. else
  877. Message1(sym_e_id_not_found,procname);
  878. end;
  879. end;
  880. end;
  881. procedure gintfwalkdowninterface(intf, _class: pobjectdef; intfindex: longint);
  882. begin
  883. if assigned(intf^.childof) then
  884. gintfwalkdowninterface(intf^.childof,_class,intfindex);
  885. gintfdoonintf(intf,_class,intfindex);
  886. end;
  887. function genintftable(_class: pobjectdef): pasmlabel;
  888. var
  889. intfindex: longint;
  890. curintf: pobjectdef;
  891. intftable: pasmlabel;
  892. begin
  893. { 1. step collect implementor functions into the implementedinterfaces^.implprocs }
  894. for intfindex:=1 to _class^.implementedinterfaces^.count do
  895. begin
  896. curintf:=_class^.implementedinterfaces^.interfaces(intfindex);
  897. gintfwalkdowninterface(curintf,_class,intfindex);
  898. end;
  899. { 2. step calc required fieldcount and their offsets in the object memory map
  900. and write data }
  901. getdatalabel(intftable);
  902. datasegment^.concat(new(pai_label,init(intftable)));
  903. gintfwritedata(_class);
  904. _class^.implementedinterfaces^.clearimplprocs; { release temporary information }
  905. genintftable:=intftable;
  906. end;
  907. { Write interface identifiers to the data section }
  908. procedure writeinterfaceids(c : pobjectdef);
  909. var
  910. i: longint;
  911. s1,s2 : string;
  912. begin
  913. if c^.owner^.name=nil then
  914. s1:=''
  915. else
  916. s1:=upper(c^.owner^.name^);
  917. if c^.objname=nil then
  918. s2:=''
  919. else
  920. s2:=upper(c^.objname^);
  921. s1:=s1+'$_'+s2;
  922. if c^.isiidguidvalid then
  923. begin
  924. if (cs_create_smart in aktmoduleswitches) then
  925. datasegment^.concat(new(pai_cut,init));
  926. datasegment^.concat(new(pai_symbol,initname_global('IID$_'+s1,0)));
  927. datasegment^.concat(new(pai_const,init_32bit(c^.iidguid.D1)));
  928. datasegment^.concat(new(pai_const,init_16bit(c^.iidguid.D2)));
  929. datasegment^.concat(new(pai_const,init_16bit(c^.iidguid.D3)));
  930. for i:=Low(c^.iidguid.D4) to High(c^.iidguid.D4) do
  931. datasegment^.concat(new(pai_const,init_8bit(c^.iidguid.D4[i])));
  932. end;
  933. if (cs_create_smart in aktmoduleswitches) then
  934. datasegment^.concat(new(pai_cut,init));
  935. datasegment^.concat(new(pai_symbol,initname_global('IIDSTR$_'+s1,0)));
  936. datasegment^.concat(new(pai_const,init_8bit(length(c^.iidstr^))));
  937. datasegment^.concat(new(pai_string,init(c^.iidstr^)));
  938. end;
  939. end.
  940. {
  941. $Log$
  942. Revision 1.15 2000-11-19 16:23:35 florian
  943. *** empty log message ***
  944. Revision 1.14 2000/11/12 23:24:10 florian
  945. * interfaces are basically running
  946. Revision 1.13 2000/11/08 00:07:40 florian
  947. * potential range check error fixed
  948. Revision 1.12 2000/11/06 23:13:53 peter
  949. * uppercase manglednames
  950. Revision 1.11 2000/11/04 17:31:00 florian
  951. * fixed some problems of previous commit
  952. Revision 1.10 2000/11/04 14:25:19 florian
  953. + merged Attila's changes for interfaces, not tested yet
  954. Revision 1.9 2000/11/01 23:04:37 peter
  955. * tprocdef.fullprocname added for better casesensitve writing of
  956. procedures
  957. Revision 1.8 2000/10/31 22:02:47 peter
  958. * symtable splitted, no real code changes
  959. Revision 1.7 2000/10/14 10:14:47 peter
  960. * moehrendorf oct 2000 rewrite
  961. Revision 1.6 2000/09/24 21:19:50 peter
  962. * delphi compile fixes
  963. Revision 1.5 2000/09/24 15:06:17 peter
  964. * use defines.inc
  965. Revision 1.4 2000/08/27 16:11:51 peter
  966. * moved some util functions from globals,cobjects to cutils
  967. * splitted files into finput,fmodule
  968. Revision 1.3 2000/07/13 12:08:26 michael
  969. + patched to 1.1.0 with former 1.09patch from peter
  970. Revision 1.2 2000/07/13 11:32:41 michael
  971. + removed logs
  972. }