nobj.pas 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Routines for the code generation of data structures
  5. like VMT, Messages, VTables, Interfaces descs
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit nobj;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. {$ifdef Delphi}
  24. dmisc,
  25. {$endif}
  26. cutils,cclasses,
  27. globtype,
  28. symdef,
  29. aasmbase,aasmtai,
  30. cpuinfo
  31. ;
  32. type
  33. pprocdeftree = ^tprocdeftree;
  34. tprocdeftree = record
  35. data : tprocdef;
  36. nl : tasmlabel;
  37. l,r : pprocdeftree;
  38. end;
  39. pprocdefcoll = ^tprocdefcoll;
  40. tprocdefcoll = record
  41. data : tprocdef;
  42. hidden : boolean;
  43. next : pprocdefcoll;
  44. end;
  45. psymcoll = ^tsymcoll;
  46. tsymcoll = record
  47. speedvalue : cardinal;
  48. name : pstring;
  49. data : pprocdefcoll;
  50. next : psymcoll;
  51. end;
  52. tclassheader=class
  53. private
  54. _Class : tobjectdef;
  55. count : integer;
  56. private
  57. { message tables }
  58. root : pprocdeftree;
  59. procedure disposeprocdeftree(p : pprocdeftree);
  60. procedure insertmsgint(p : tnamedindexitem;arg:pointer);
  61. procedure insertmsgstr(p : tnamedindexitem;arg:pointer);
  62. procedure insertint(p : pprocdeftree;var at : pprocdeftree);
  63. procedure insertstr(p : pprocdeftree;var at : pprocdeftree);
  64. procedure writenames(p : pprocdeftree);
  65. procedure writeintentry(p : pprocdeftree);
  66. procedure writestrentry(p : pprocdeftree);
  67. {$ifdef WITHDMT}
  68. private
  69. { dmt }
  70. procedure insertdmtentry(p : tnamedindexitem;arg:pointer);
  71. procedure writedmtindexentry(p : pprocdeftree);
  72. procedure writedmtaddressentry(p : pprocdeftree);
  73. {$endif}
  74. private
  75. { published methods }
  76. procedure do_count(p : tnamedindexitem;arg:pointer);
  77. procedure genpubmethodtableentry(p : tnamedindexitem;arg:pointer);
  78. private
  79. { vmt }
  80. wurzel : psymcoll;
  81. nextvirtnumber : integer;
  82. has_constructor,
  83. has_virtual_method : boolean;
  84. procedure eachsym(sym : tnamedindexitem;arg:pointer);
  85. procedure disposevmttree;
  86. procedure writevirtualmethods(List:TAAsmoutput);
  87. private
  88. { interface tables }
  89. function gintfgetvtbllabelname(intfindex: integer): string;
  90. procedure gintfcreatevtbl(intfindex: integer; rawdata,rawcode: TAAsmoutput);
  91. procedure gintfgenentry(intfindex, contintfindex: integer; rawdata: TAAsmoutput);
  92. procedure gintfoptimizevtbls(implvtbl : plongintarray);
  93. procedure gintfwritedata;
  94. function gintfgetcprocdef(proc: tprocdef;const name: string): tprocdef;
  95. procedure gintfdoonintf(intf: tobjectdef; intfindex: longint);
  96. procedure gintfwalkdowninterface(intf: tobjectdef; intfindex: longint);
  97. protected
  98. { adjusts the self value with ioffset when casting a interface
  99. to a class
  100. }
  101. procedure adjustselfvalue(procdef: tprocdef;ioffset: aword);virtual;
  102. { generates the wrapper for a call to a method via an interface }
  103. procedure cgintfwrapper(asmlist: TAAsmoutput; procdef: tprocdef; const labelname: string; ioffset: longint);virtual;abstract;
  104. public
  105. constructor create(c:tobjectdef);
  106. destructor destroy;override;
  107. { generates the message tables for a class }
  108. function genstrmsgtab : tasmlabel;
  109. function genintmsgtab : tasmlabel;
  110. function genpublishedmethodstable : tasmlabel;
  111. { generates a VMT entries }
  112. procedure genvmt;
  113. {$ifdef WITHDMT}
  114. { generates a DMT for _class }
  115. function gendmt : tasmlabel;
  116. {$endif WITHDMT}
  117. { interfaces }
  118. function genintftable: tasmlabel;
  119. { write the VMT to datasegment }
  120. procedure writevmt;
  121. procedure writeinterfaceids;
  122. end;
  123. tclassheaderclass=class of tclassheader;
  124. var
  125. cclassheader : tclassheaderclass;
  126. implementation
  127. uses
  128. {$ifdef delphi}
  129. sysutils,
  130. {$else}
  131. strings,
  132. {$endif}
  133. globals,verbose,
  134. symtable,symconst,symtype,symsym,defcmp,paramgr,
  135. {$ifdef GDB}
  136. gdb,
  137. {$endif GDB}
  138. aasmcpu,
  139. cpubase,cgbase,
  140. cgobj
  141. ;
  142. {*****************************************************************************
  143. TClassHeader
  144. *****************************************************************************}
  145. constructor tclassheader.create(c:tobjectdef);
  146. begin
  147. inherited Create;
  148. _Class:=c;
  149. end;
  150. destructor tclassheader.destroy;
  151. begin
  152. disposevmttree;
  153. end;
  154. {**************************************
  155. Message Tables
  156. **************************************}
  157. procedure tclassheader.disposeprocdeftree(p : pprocdeftree);
  158. begin
  159. if assigned(p^.l) then
  160. disposeprocdeftree(p^.l);
  161. if assigned(p^.r) then
  162. disposeprocdeftree(p^.r);
  163. dispose(p);
  164. end;
  165. procedure tclassheader.insertint(p : pprocdeftree;var at : pprocdeftree);
  166. begin
  167. if at=nil then
  168. begin
  169. at:=p;
  170. inc(count);
  171. end
  172. else
  173. begin
  174. if p^.data.messageinf.i<at^.data.messageinf.i then
  175. insertint(p,at^.l)
  176. else if p^.data.messageinf.i>at^.data.messageinf.i then
  177. insertint(p,at^.r)
  178. else
  179. Message1(parser_e_duplicate_message_label,tostr(p^.data.messageinf.i));
  180. end;
  181. end;
  182. procedure tclassheader.insertstr(p : pprocdeftree;var at : pprocdeftree);
  183. var
  184. i : integer;
  185. begin
  186. if at=nil then
  187. begin
  188. at:=p;
  189. inc(count);
  190. end
  191. else
  192. begin
  193. i:=strcomp(p^.data.messageinf.str,at^.data.messageinf.str);
  194. if i<0 then
  195. insertstr(p,at^.l)
  196. else if i>0 then
  197. insertstr(p,at^.r)
  198. else
  199. Message1(parser_e_duplicate_message_label,strpas(p^.data.messageinf.str));
  200. end;
  201. end;
  202. procedure tclassheader.insertmsgint(p : tnamedindexitem;arg:pointer);
  203. var
  204. i : cardinal;
  205. def: Tprocdef;
  206. pt : pprocdeftree;
  207. begin
  208. if tsym(p).typ=procsym then
  209. for i:=1 to Tprocsym(p).procdef_count do
  210. begin
  211. def:=Tprocsym(p).procdef[i];
  212. if po_msgint in def.procoptions then
  213. begin
  214. new(pt);
  215. pt^.data:=def;
  216. pt^.l:=nil;
  217. pt^.r:=nil;
  218. insertint(pt,root);
  219. end;
  220. end;
  221. end;
  222. procedure tclassheader.insertmsgstr(p : tnamedindexitem;arg:pointer);
  223. var
  224. i : cardinal;
  225. def: Tprocdef;
  226. pt : pprocdeftree;
  227. begin
  228. if tsym(p).typ=procsym then
  229. for i:=1 to Tprocsym(p).procdef_count do
  230. begin
  231. def:=Tprocsym(p).procdef[i];
  232. if po_msgstr in def.procoptions then
  233. begin
  234. new(pt);
  235. pt^.data:=def;
  236. pt^.l:=nil;
  237. pt^.r:=nil;
  238. insertstr(pt,root);
  239. end;
  240. end;
  241. end;
  242. procedure tclassheader.writenames(p : pprocdeftree);
  243. var
  244. ca : pchar;
  245. len : longint;
  246. begin
  247. objectlibrary.getdatalabel(p^.nl);
  248. if assigned(p^.l) then
  249. writenames(p^.l);
  250. datasegment.concat(tai_align.create(const_align(POINTER_SIZE)));
  251. dataSegment.concat(Tai_label.Create(p^.nl));
  252. len:=strlen(p^.data.messageinf.str);
  253. datasegment.concat(tai_const.create_8bit(len));
  254. getmem(ca,len+1);
  255. move(p^.data.messageinf.str^,ca^,len+1);
  256. dataSegment.concat(Tai_string.Create_pchar(ca));
  257. if assigned(p^.r) then
  258. writenames(p^.r);
  259. end;
  260. procedure tclassheader.writestrentry(p : pprocdeftree);
  261. begin
  262. if assigned(p^.l) then
  263. writestrentry(p^.l);
  264. { write name label }
  265. dataSegment.concat(Tai_const_symbol.Create(p^.nl));
  266. dataSegment.concat(Tai_const_symbol.Createname(p^.data.mangledname));
  267. if assigned(p^.r) then
  268. writestrentry(p^.r);
  269. end;
  270. function tclassheader.genstrmsgtab : tasmlabel;
  271. var
  272. r : tasmlabel;
  273. begin
  274. root:=nil;
  275. count:=0;
  276. { insert all message handlers into a tree, sorted by name }
  277. _class.symtable.foreach({$ifdef FPCPROCVAR}@{$endif}insertmsgstr,nil);
  278. { write all names }
  279. if assigned(root) then
  280. writenames(root);
  281. { now start writing of the message string table }
  282. objectlibrary.getdatalabel(r);
  283. datasegment.concat(tai_align.create(const_align(POINTER_SIZE)));
  284. dataSegment.concat(Tai_label.Create(r));
  285. genstrmsgtab:=r;
  286. dataSegment.concat(Tai_const.Create_32bit(count));
  287. if assigned(root) then
  288. begin
  289. writestrentry(root);
  290. disposeprocdeftree(root);
  291. end;
  292. end;
  293. procedure tclassheader.writeintentry(p : pprocdeftree);
  294. begin
  295. if assigned(p^.l) then
  296. writeintentry(p^.l);
  297. { write name label }
  298. dataSegment.concat(Tai_const.Create_32bit(p^.data.messageinf.i));
  299. dataSegment.concat(Tai_const_symbol.Createname(p^.data.mangledname));
  300. if assigned(p^.r) then
  301. writeintentry(p^.r);
  302. end;
  303. function tclassheader.genintmsgtab : tasmlabel;
  304. var
  305. r : tasmlabel;
  306. begin
  307. root:=nil;
  308. count:=0;
  309. { insert all message handlers into a tree, sorted by name }
  310. _class.symtable.foreach({$ifdef FPCPROCVAR}@{$endif}insertmsgint,nil);
  311. { now start writing of the message string table }
  312. objectlibrary.getdatalabel(r);
  313. datasegment.concat(tai_align.create(const_align(POINTER_SIZE)));
  314. dataSegment.concat(Tai_label.Create(r));
  315. genintmsgtab:=r;
  316. dataSegment.concat(Tai_const.Create_32bit(count));
  317. if assigned(root) then
  318. begin
  319. writeintentry(root);
  320. disposeprocdeftree(root);
  321. end;
  322. end;
  323. {$ifdef WITHDMT}
  324. {**************************************
  325. DMT
  326. **************************************}
  327. procedure tclassheader.insertdmtentry(p : tnamedindexitem;arg:pointer);
  328. var
  329. hp : tprocdef;
  330. pt : pprocdeftree;
  331. begin
  332. if tsym(p).typ=procsym then
  333. begin
  334. hp:=tprocsym(p).definition;
  335. while assigned(hp) do
  336. begin
  337. if (po_msgint in hp.procoptions) then
  338. begin
  339. new(pt);
  340. pt^.p:=hp;
  341. pt^.l:=nil;
  342. pt^.r:=nil;
  343. insertint(pt,root);
  344. end;
  345. hp:=hp.nextoverloaded;
  346. end;
  347. end;
  348. end;
  349. procedure tclassheader.writedmtindexentry(p : pprocdeftree);
  350. begin
  351. if assigned(p^.l) then
  352. writedmtindexentry(p^.l);
  353. dataSegment.concat(Tai_const.Create_32bit(p^.data.messageinf.i));
  354. if assigned(p^.r) then
  355. writedmtindexentry(p^.r);
  356. end;
  357. procedure tclassheader.writedmtaddressentry(p : pprocdeftree);
  358. begin
  359. if assigned(p^.l) then
  360. writedmtaddressentry(p^.l);
  361. dataSegment.concat(Tai_const_symbol.Createname(p^.data.mangledname));
  362. if assigned(p^.r) then
  363. writedmtaddressentry(p^.r);
  364. end;
  365. function tclassheader.gendmt : tasmlabel;
  366. var
  367. r : tasmlabel;
  368. begin
  369. root:=nil;
  370. count:=0;
  371. gendmt:=nil;
  372. { insert all message handlers into a tree, sorted by number }
  373. _class.symtable.foreach({$ifdef FPCPROCVAR}@{$endif}insertdmtentry);
  374. if count>0 then
  375. begin
  376. objectlibrary.getdatalabel(r);
  377. gendmt:=r;
  378. datasegment.concat(tai_align.create(const_align(POINTER_SIZE)));
  379. dataSegment.concat(Tai_label.Create(r));
  380. { entries for caching }
  381. dataSegment.concat(Tai_const.Create_32bit(0));
  382. dataSegment.concat(Tai_const.Create_32bit(0));
  383. dataSegment.concat(Tai_const.Create_32bit(count));
  384. if assigned(root) then
  385. begin
  386. writedmtindexentry(root);
  387. writedmtaddressentry(root);
  388. disposeprocdeftree(root);
  389. end;
  390. end;
  391. end;
  392. {$endif WITHDMT}
  393. {**************************************
  394. Published Methods
  395. **************************************}
  396. procedure tclassheader.do_count(p : tnamedindexitem;arg:pointer);
  397. begin
  398. if (tsym(p).typ=procsym) and (sp_published in tsym(p).symoptions) then
  399. inc(count);
  400. end;
  401. procedure tclassheader.genpubmethodtableentry(p : tnamedindexitem;arg:pointer);
  402. var
  403. hp : tprocdef;
  404. l : tasmlabel;
  405. begin
  406. if (tsym(p).typ=procsym) and (sp_published in tsym(p).symoptions) then
  407. begin
  408. if Tprocsym(p).procdef_count>1 then
  409. internalerror(1209992);
  410. hp:=tprocsym(p).first_procdef;
  411. objectlibrary.getdatalabel(l);
  412. consts.concat(tai_align.create(const_align(POINTER_SIZE)));
  413. Consts.concat(Tai_label.Create(l));
  414. Consts.concat(Tai_const.Create_8bit(length(p.name)));
  415. Consts.concat(Tai_string.Create(p.name));
  416. dataSegment.concat(Tai_const_symbol.Create(l));
  417. dataSegment.concat(Tai_const_symbol.Createname(hp.mangledname));
  418. end;
  419. end;
  420. function tclassheader.genpublishedmethodstable : tasmlabel;
  421. var
  422. l : tasmlabel;
  423. begin
  424. count:=0;
  425. _class.symtable.foreach({$ifdef FPCPROCVAR}@{$endif}do_count,nil);
  426. if count>0 then
  427. begin
  428. objectlibrary.getdatalabel(l);
  429. datasegment.concat(tai_align.create(const_align(POINTER_SIZE)));
  430. dataSegment.concat(Tai_label.Create(l));
  431. dataSegment.concat(Tai_const.Create_32bit(count));
  432. _class.symtable.foreach({$ifdef FPCPROCVAR}@{$endif}genpubmethodtableentry,nil);
  433. genpublishedmethodstable:=l;
  434. end
  435. else
  436. genpublishedmethodstable:=nil;
  437. end;
  438. {**************************************
  439. VMT
  440. **************************************}
  441. procedure tclassheader.eachsym(sym : tnamedindexitem;arg:pointer);
  442. var
  443. procdefcoll : pprocdefcoll;
  444. symcoll : psymcoll;
  445. _name : string;
  446. _speed : cardinal;
  447. procedure newdefentry(pd:tprocdef);
  448. begin
  449. new(procdefcoll);
  450. procdefcoll^.data:=pd;
  451. procdefcoll^.hidden:=false;
  452. procdefcoll^.next:=symcoll^.data;
  453. symcoll^.data:=procdefcoll;
  454. { if it's a virtual method }
  455. if (po_virtualmethod in pd.procoptions) then
  456. begin
  457. { then it gets a number ... }
  458. pd.extnumber:=nextvirtnumber;
  459. { and we inc the number }
  460. inc(nextvirtnumber);
  461. has_virtual_method:=true;
  462. end;
  463. if (pd.proctypeoption=potype_constructor) then
  464. has_constructor:=true;
  465. { check, if a method should be overridden }
  466. if (pd._class=_class) and
  467. (po_overridingmethod in pd.procoptions) then
  468. MessagePos1(pd.fileinfo,parser_e_nothing_to_be_overridden,pd.fullprocname(false));
  469. end;
  470. { creates a new entry in the procsym list }
  471. procedure newentry;
  472. var i:cardinal;
  473. begin
  474. { if not, generate a new symbol item }
  475. new(symcoll);
  476. symcoll^.speedvalue:=sym.speedvalue;
  477. symcoll^.name:=stringdup(sym.name);
  478. symcoll^.next:=wurzel;
  479. symcoll^.data:=nil;
  480. wurzel:=symcoll;
  481. { inserts all definitions }
  482. for i:=1 to Tprocsym(sym).procdef_count do
  483. newdefentry(Tprocsym(sym).procdef[i]);
  484. end;
  485. label
  486. handlenextdef;
  487. var
  488. pd : tprocdef;
  489. i : cardinal;
  490. is_visible,
  491. hasoverloads,
  492. pdoverload : boolean;
  493. begin
  494. { put only sub routines into the VMT, and routines
  495. that are visible to the current class. Skip private
  496. methods in other classes }
  497. if (tsym(sym).typ=procsym) then
  498. begin
  499. { is this symbol visible from the class that we are
  500. generating. This will be used to hide the other procdefs.
  501. When the symbol is not visible we don't hide the other
  502. procdefs, because they can be reused in the next class.
  503. The check to skip the invisible methods that are in the
  504. list is futher down in the code }
  505. is_visible:=tprocsym(sym).is_visible_for_object(_class);
  506. { check the current list of symbols }
  507. _name:=sym.name;
  508. _speed:=sym.speedvalue;
  509. symcoll:=wurzel;
  510. while assigned(symcoll) do
  511. begin
  512. { does the symbol already exist in the list? First
  513. compare speedvalue before doing the string compare to
  514. speed it up a little }
  515. if (_speed=symcoll^.speedvalue) and
  516. (_name=symcoll^.name^) then
  517. begin
  518. hasoverloads:=(Tprocsym(sym).procdef_count>1);
  519. { walk through all defs of the symbol }
  520. for i:=1 to Tprocsym(sym).procdef_count do
  521. begin
  522. pd:=Tprocsym(sym).procdef[i];
  523. if pd.procsym=sym then
  524. begin
  525. pdoverload:=(po_overload in pd.procoptions);
  526. { compare with all stored definitions }
  527. procdefcoll:=symcoll^.data;
  528. while assigned(procdefcoll) do
  529. begin
  530. { compare only if the definition is not hidden }
  531. if not procdefcoll^.hidden then
  532. begin
  533. { check if one of the two methods has virtual }
  534. if (po_virtualmethod in procdefcoll^.data.procoptions) or
  535. (po_virtualmethod in pd.procoptions) then
  536. begin
  537. { if the current definition has no virtual then hide the
  538. old virtual if the new definition has the same arguments or
  539. when it has no overload directive and no overloads }
  540. if not(po_virtualmethod in pd.procoptions) then
  541. begin
  542. if tstoredsym(procdefcoll^.data.procsym).is_visible_for_object(pd._class) and
  543. (not(pdoverload or hasoverloads) or
  544. (compare_paras(procdefcoll^.data.para,pd.para,cp_value_equal_const,[])>=te_equal)) then
  545. begin
  546. if is_visible then
  547. procdefcoll^.hidden:=true;
  548. if _class=pd._class then
  549. MessagePos1(pd.fileinfo,parser_w_should_use_override,pd.fullprocname(false));
  550. end;
  551. end
  552. { if both are virtual we check the header }
  553. else if (po_virtualmethod in pd.procoptions) and
  554. (po_virtualmethod in procdefcoll^.data.procoptions) then
  555. begin
  556. { new one has not override }
  557. if is_class(_class) and
  558. not(po_overridingmethod in pd.procoptions) then
  559. begin
  560. { we start a new virtual tree, hide the old }
  561. if (not(pdoverload or hasoverloads) or
  562. (compare_paras(procdefcoll^.data.para,pd.para,cp_value_equal_const,[])>=te_equal)) and
  563. (tstoredsym(procdefcoll^.data.procsym).is_visible_for_object(pd._class)) then
  564. begin
  565. if is_visible then
  566. procdefcoll^.hidden:=true;
  567. if _class=pd._class then
  568. MessagePos1(pd.fileinfo,parser_w_should_use_override,pd.fullprocname(false));
  569. end;
  570. end
  571. { check if the method to override is visible }
  572. else if (po_overridingmethod in pd.procoptions) and
  573. (not tstoredsym(procdefcoll^.data.procsym).is_visible_for_object(pd._class)) then
  574. begin
  575. { do nothing, the error will follow when adding the entry }
  576. end
  577. { same parameters }
  578. else if (compare_paras(procdefcoll^.data.para,pd.para,cp_value_equal_const,[])>=te_equal) then
  579. begin
  580. { overload is inherited }
  581. if (po_overload in procdefcoll^.data.procoptions) then
  582. include(pd.procoptions,po_overload);
  583. { inherite calling convention when it was force and the
  584. current definition has none force }
  585. if (po_hascallingconvention in procdefcoll^.data.procoptions) and
  586. not(po_hascallingconvention in pd.procoptions) then
  587. begin
  588. pd.proccalloption:=procdefcoll^.data.proccalloption;
  589. include(pd.procoptions,po_hascallingconvention);
  590. end;
  591. { the flags have to match except abstract and override }
  592. { only if both are virtual !! }
  593. if (procdefcoll^.data.proccalloption<>pd.proccalloption) or
  594. (procdefcoll^.data.proctypeoption<>pd.proctypeoption) or
  595. ((procdefcoll^.data.procoptions-
  596. [po_abstractmethod,po_overridingmethod,po_assembler,po_overload,po_public])<>
  597. (pd.procoptions-[po_abstractmethod,po_overridingmethod,po_assembler,po_overload,po_public])) then
  598. begin
  599. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,pd.fullprocname(false));
  600. tprocsym(procdefcoll^.data.procsym).write_parameter_lists(pd);
  601. end;
  602. { error, if the return types aren't equal }
  603. if not(equal_defs(procdefcoll^.data.rettype.def,pd.rettype.def)) and
  604. not((procdefcoll^.data.rettype.def.deftype=objectdef) and
  605. (pd.rettype.def.deftype=objectdef) and
  606. is_class(procdefcoll^.data.rettype.def) and
  607. is_class(pd.rettype.def) and
  608. (tobjectdef(pd.rettype.def).is_related(
  609. tobjectdef(procdefcoll^.data.rettype.def)))) then
  610. Message2(parser_e_overridden_methods_not_same_ret,pd.fullprocname(false),
  611. procdefcoll^.data.fullprocname(false));
  612. { now set the number }
  613. pd.extnumber:=procdefcoll^.data.extnumber;
  614. { and exchange }
  615. procdefcoll^.data:=pd;
  616. goto handlenextdef;
  617. end
  618. { different parameters }
  619. else
  620. begin
  621. { when we got an override directive then can search futher for
  622. the procedure to override.
  623. If we are starting a new virtual tree then hide the old tree }
  624. if not(po_overridingmethod in pd.procoptions) and
  625. not pdoverload then
  626. begin
  627. if is_visible then
  628. procdefcoll^.hidden:=true;
  629. if _class=pd._class then
  630. MessagePos1(pd.fileinfo,parser_w_should_use_override,pd.fullprocname(false));
  631. end;
  632. end;
  633. end
  634. else
  635. begin
  636. { the new definition is virtual and the old static, we hide the old one
  637. if the new defintion has not the overload directive }
  638. if is_visible and
  639. ((not(pdoverload or hasoverloads)) or
  640. (compare_paras(procdefcoll^.data.para,pd.para,cp_value_equal_const,[])>=te_equal)) then
  641. procdefcoll^.hidden:=true;
  642. end;
  643. end
  644. else
  645. begin
  646. { both are static, we hide the old one if the new defintion
  647. has not the overload directive }
  648. if is_visible and
  649. ((not pdoverload) or
  650. (compare_paras(procdefcoll^.data.para,pd.para,cp_value_equal_const,[])>=te_equal)) then
  651. procdefcoll^.hidden:=true;
  652. end;
  653. end; { not hidden }
  654. procdefcoll:=procdefcoll^.next;
  655. end;
  656. { if it isn't saved in the list we create a new entry }
  657. newdefentry(pd);
  658. end;
  659. handlenextdef:
  660. end;
  661. exit;
  662. end;
  663. symcoll:=symcoll^.next;
  664. end;
  665. newentry;
  666. end;
  667. end;
  668. procedure tclassheader.disposevmttree;
  669. var
  670. symcoll : psymcoll;
  671. procdefcoll : pprocdefcoll;
  672. begin
  673. { disposes the above generated tree }
  674. symcoll:=wurzel;
  675. while assigned(symcoll) do
  676. begin
  677. wurzel:=symcoll^.next;
  678. stringdispose(symcoll^.name);
  679. procdefcoll:=symcoll^.data;
  680. while assigned(procdefcoll) do
  681. begin
  682. symcoll^.data:=procdefcoll^.next;
  683. dispose(procdefcoll);
  684. procdefcoll:=symcoll^.data;
  685. end;
  686. dispose(symcoll);
  687. symcoll:=wurzel;
  688. end;
  689. end;
  690. procedure tclassheader.genvmt;
  691. procedure do_genvmt(p : tobjectdef);
  692. begin
  693. { start with the base class }
  694. if assigned(p.childof) then
  695. do_genvmt(p.childof);
  696. { walk through all public syms }
  697. p.symtable.foreach({$ifdef FPCPROCVAR}@{$endif}eachsym,nil);
  698. end;
  699. begin
  700. wurzel:=nil;
  701. nextvirtnumber:=0;
  702. has_constructor:=false;
  703. has_virtual_method:=false;
  704. { generates a tree of all used methods }
  705. do_genvmt(_class);
  706. if not(is_interface(_class)) and
  707. has_virtual_method and
  708. not(has_constructor) then
  709. Message1(parser_w_virtual_without_constructor,_class.objrealname^);
  710. end;
  711. {**************************************
  712. Interface tables
  713. **************************************}
  714. function tclassheader.gintfgetvtbllabelname(intfindex: integer): string;
  715. begin
  716. gintfgetvtbllabelname:=make_mangledname('VTBL',_class.owner,_class.objname^+
  717. '_$_'+_class.implementedinterfaces.interfaces(intfindex).objname^);
  718. end;
  719. procedure tclassheader.gintfcreatevtbl(intfindex: integer; rawdata,rawcode: TAAsmoutput);
  720. var
  721. implintf: timplementedinterfaces;
  722. curintf: tobjectdef;
  723. proccount: integer;
  724. tmps: string;
  725. i: longint;
  726. begin
  727. implintf:=_class.implementedinterfaces;
  728. curintf:=implintf.interfaces(intfindex);
  729. if (cs_create_smart in aktmoduleswitches) then
  730. rawdata.concat(Tai_symbol.Createname_global(gintfgetvtbllabelname(intfindex),0))
  731. else
  732. rawdata.concat(Tai_symbol.Createname(gintfgetvtbllabelname(intfindex),0));
  733. proccount:=implintf.implproccount(intfindex);
  734. for i:=1 to proccount do
  735. begin
  736. tmps:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+curintf.objname^+'_$_'+
  737. tostr(i)+'_$_'+
  738. implintf.implprocs(intfindex,i).mangledname);
  739. { create wrapper code }
  740. cgintfwrapper(rawcode,implintf.implprocs(intfindex,i),tmps,implintf.ioffsets(intfindex)^);
  741. { create reference }
  742. rawdata.concat(Tai_const_symbol.Createname(tmps));
  743. end;
  744. end;
  745. procedure tclassheader.gintfgenentry(intfindex, contintfindex: integer; rawdata: TAAsmoutput);
  746. var
  747. implintf: timplementedinterfaces;
  748. curintf: tobjectdef;
  749. tmplabel: tasmlabel;
  750. i: longint;
  751. begin
  752. implintf:=_class.implementedinterfaces;
  753. curintf:=implintf.interfaces(intfindex);
  754. { GUID }
  755. if curintf.objecttype in [odt_interfacecom] then
  756. begin
  757. { label for GUID }
  758. objectlibrary.getdatalabel(tmplabel);
  759. rawdata.concat(tai_align.create(const_align(pointer_size)));
  760. rawdata.concat(Tai_label.Create(tmplabel));
  761. rawdata.concat(Tai_const.Create_32bit(longint(curintf.iidguid^.D1)));
  762. rawdata.concat(Tai_const.Create_16bit(curintf.iidguid^.D2));
  763. rawdata.concat(Tai_const.Create_16bit(curintf.iidguid^.D3));
  764. for i:=Low(curintf.iidguid^.D4) to High(curintf.iidguid^.D4) do
  765. rawdata.concat(Tai_const.Create_8bit(curintf.iidguid^.D4[i]));
  766. dataSegment.concat(Tai_const_symbol.Create(tmplabel));
  767. end
  768. else
  769. begin
  770. { nil for Corba interfaces }
  771. dataSegment.concat(Tai_const.Create_32bit(0)); { nil }
  772. end;
  773. { VTable }
  774. dataSegment.concat(Tai_const_symbol.Createname(gintfgetvtbllabelname(contintfindex)));
  775. { IOffset field }
  776. dataSegment.concat(Tai_const.Create_32bit(implintf.ioffsets(contintfindex)^));
  777. { IIDStr }
  778. objectlibrary.getdatalabel(tmplabel);
  779. rawdata.concat(tai_align.create(const_align(pointer_size)));
  780. rawdata.concat(Tai_label.Create(tmplabel));
  781. rawdata.concat(Tai_const.Create_8bit(length(curintf.iidstr^)));
  782. if curintf.objecttype=odt_interfacecom then
  783. rawdata.concat(Tai_string.Create(upper(curintf.iidstr^)))
  784. else
  785. rawdata.concat(Tai_string.Create(curintf.iidstr^));
  786. dataSegment.concat(Tai_const_symbol.Create(tmplabel));
  787. end;
  788. procedure tclassheader.gintfoptimizevtbls(implvtbl : plongintarray);
  789. type
  790. tcompintfentry = record
  791. weight: longint;
  792. compintf: longint;
  793. end;
  794. { Max 1000 interface in the class header interfaces it's enough imho }
  795. tcompintfs = packed array[1..1000] of tcompintfentry;
  796. pcompintfs = ^tcompintfs;
  797. tequals = packed array[1..1000] of longint;
  798. pequals = ^tequals;
  799. var
  800. max: longint;
  801. equals: pequals;
  802. compats: pcompintfs;
  803. i: longint;
  804. j: longint;
  805. w: longint;
  806. cij: boolean;
  807. cji: boolean;
  808. begin
  809. max:=_class.implementedinterfaces.count;
  810. if max>High(tequals) then
  811. Internalerror(200006135);
  812. getmem(compats,sizeof(tcompintfentry)*max);
  813. getmem(equals,sizeof(longint)*max);
  814. fillchar(compats^,sizeof(tcompintfentry)*max,0);
  815. fillchar(equals^,sizeof(longint)*max,0);
  816. { ismergepossible is a containing relation
  817. meaning of ismergepossible(a,b,w) =
  818. if implementorfunction map of a is contained implementorfunction map of b
  819. imp(a,b) and imp(b,c) => imp(a,c) ; imp(a,b) and imp(b,a) => a == b
  820. }
  821. { the order is very important for correct allocation }
  822. for i:=1 to max do
  823. begin
  824. for j:=i+1 to max do
  825. begin
  826. cij:=_class.implementedinterfaces.isimplmergepossible(i,j,w);
  827. cji:=_class.implementedinterfaces.isimplmergepossible(j,i,w);
  828. if cij and cji then { i equal j }
  829. begin
  830. { get minimum index of equal }
  831. if equals^[j]=0 then
  832. equals^[j]:=i;
  833. end
  834. else if cij then
  835. begin
  836. { get minimum index of maximum weight }
  837. if compats^[i].weight<w then
  838. begin
  839. compats^[i].weight:=w;
  840. compats^[i].compintf:=j;
  841. end;
  842. end
  843. else if cji then
  844. begin
  845. { get minimum index of maximum weight }
  846. if (compats^[j].weight<w) then
  847. begin
  848. compats^[j].weight:=w;
  849. compats^[j].compintf:=i;
  850. end;
  851. end;
  852. end;
  853. end;
  854. for i:=1 to max do
  855. begin
  856. if compats^[i].compintf<>0 then
  857. implvtbl[i]:=compats^[i].compintf
  858. else if equals^[i]<>0 then
  859. implvtbl[i]:=equals^[i]
  860. else
  861. implvtbl[i]:=i;
  862. end;
  863. freemem(compats,sizeof(tcompintfentry)*max);
  864. freemem(equals,sizeof(longint)*max);
  865. end;
  866. procedure tclassheader.gintfwritedata;
  867. var
  868. rawdata,rawcode: taasmoutput;
  869. impintfindexes: plongintarray;
  870. max: longint;
  871. i: longint;
  872. begin
  873. max:=_class.implementedinterfaces.count;
  874. getmem(impintfindexes,(max+1)*sizeof(longint));
  875. gintfoptimizevtbls(impintfindexes);
  876. rawdata:=TAAsmOutput.Create;
  877. rawcode:=TAAsmOutput.Create;
  878. dataSegment.concat(Tai_const.Create_16bit(max));
  879. { Two pass, one for allocation and vtbl creation }
  880. for i:=1 to max do
  881. begin
  882. if impintfindexes[i]=i then { if implement itself }
  883. begin
  884. { allocate a pointer in the object memory }
  885. with tobjectsymtable(_class.symtable) do
  886. begin
  887. if (dataalignment>=pointer_size) then
  888. datasize:=align(datasize,dataalignment)
  889. else
  890. datasize:=align(datasize,pointer_size);
  891. _class.implementedinterfaces.ioffsets(i)^:=datasize;
  892. datasize:=datasize+pointer_size;
  893. end;
  894. { write vtbl }
  895. gintfcreatevtbl(i,rawdata,rawcode);
  896. end;
  897. end;
  898. { second pass: for fill interfacetable and remained ioffsets }
  899. for i:=1 to max do
  900. begin
  901. if i<>impintfindexes[i] then { why execute x:=x ? }
  902. with _class.implementedinterfaces do
  903. ioffsets(i)^:=ioffsets(impintfindexes[i])^;
  904. gintfgenentry(i,impintfindexes[i],rawdata);
  905. end;
  906. dataSegment.concatlist(rawdata);
  907. rawdata.free;
  908. codeSegment.concatlist(rawcode);
  909. rawcode.free;
  910. freemem(impintfindexes,(max+1)*sizeof(longint));
  911. end;
  912. function tclassheader.gintfgetcprocdef(proc: tprocdef;const name: string): tprocdef;
  913. var
  914. sym: tsym;
  915. implprocdef : Tprocdef;
  916. i: cardinal;
  917. begin
  918. gintfgetcprocdef:=nil;
  919. sym:=tsym(search_class_member(_class,name));
  920. if assigned(sym) and
  921. (sym.typ=procsym) then
  922. begin
  923. { when the definition has overload directive set, we search for
  924. overloaded definitions in the class, this only needs to be done once
  925. for class entries as the tree keeps always the same }
  926. if (not tprocsym(sym).overloadchecked) and
  927. (po_overload in tprocsym(sym).first_procdef.procoptions) and
  928. (tprocsym(sym).owner.symtabletype=objectsymtable) then
  929. search_class_overloads(tprocsym(sym));
  930. for i:=1 to tprocsym(sym).procdef_count do
  931. begin
  932. implprocdef:=tprocsym(sym).procdef[i];
  933. if (compare_paras(proc.para,implprocdef.para,cp_none,[])>=te_equal) and
  934. (proc.proccalloption=implprocdef.proccalloption) then
  935. begin
  936. gintfgetcprocdef:=implprocdef;
  937. exit;
  938. end;
  939. end;
  940. end;
  941. end;
  942. procedure tclassheader.gintfdoonintf(intf: tobjectdef; intfindex: longint);
  943. var
  944. def: tdef;
  945. procname: string; { for error }
  946. mappedname: string;
  947. nextexist: pointer;
  948. implprocdef: tprocdef;
  949. begin
  950. def:=tdef(intf.symtable.defindex.first);
  951. while assigned(def) do
  952. begin
  953. if def.deftype=procdef then
  954. begin
  955. procname:='';
  956. implprocdef:=nil;
  957. nextexist:=nil;
  958. repeat
  959. mappedname:=_class.implementedinterfaces.getmappings(intfindex,tprocdef(def).procsym.name,nextexist);
  960. if procname='' then
  961. procname:=tprocdef(def).procsym.name;
  962. //mappedname; { for error messages }
  963. if mappedname<>'' then
  964. implprocdef:=gintfgetcprocdef(tprocdef(def),mappedname);
  965. until assigned(implprocdef) or not assigned(nextexist);
  966. if not assigned(implprocdef) then
  967. implprocdef:=gintfgetcprocdef(tprocdef(def),tprocdef(def).procsym.name);
  968. if procname='' then
  969. procname:=tprocdef(def).procsym.name;
  970. if assigned(implprocdef) then
  971. _class.implementedinterfaces.addimplproc(intfindex,implprocdef)
  972. else
  973. Message1(sym_e_no_matching_implementation_found,tprocdef(def).fullprocname(false));
  974. end;
  975. def:=tdef(def.indexnext);
  976. end;
  977. end;
  978. procedure tclassheader.gintfwalkdowninterface(intf: tobjectdef; intfindex: longint);
  979. begin
  980. if assigned(intf.childof) then
  981. gintfwalkdowninterface(intf.childof,intfindex);
  982. gintfdoonintf(intf,intfindex);
  983. end;
  984. function tclassheader.genintftable: tasmlabel;
  985. var
  986. intfindex: longint;
  987. curintf: tobjectdef;
  988. intftable: tasmlabel;
  989. begin
  990. { 1. step collect implementor functions into the implementedinterfaces.implprocs }
  991. for intfindex:=1 to _class.implementedinterfaces.count do
  992. begin
  993. curintf:=_class.implementedinterfaces.interfaces(intfindex);
  994. gintfwalkdowninterface(curintf,intfindex);
  995. end;
  996. { 2. step calc required fieldcount and their offsets in the object memory map
  997. and write data }
  998. objectlibrary.getdatalabel(intftable);
  999. dataSegment.concat(tai_align.create(const_align(POINTER_SIZE)));
  1000. dataSegment.concat(Tai_label.Create(intftable));
  1001. gintfwritedata;
  1002. _class.implementedinterfaces.clearimplprocs; { release temporary information }
  1003. genintftable:=intftable;
  1004. end;
  1005. { Write interface identifiers to the data section }
  1006. procedure tclassheader.writeinterfaceids;
  1007. var
  1008. i: longint;
  1009. begin
  1010. if assigned(_class.iidguid) then
  1011. begin
  1012. if (cs_create_smart in aktmoduleswitches) then
  1013. dataSegment.concat(Tai_cut.Create);
  1014. dataSegment.concat(Tai_symbol.Createname_global(make_mangledname('IID',_class.owner,_class.objname^),0));
  1015. dataSegment.concat(Tai_const.Create_32bit(longint(_class.iidguid^.D1)));
  1016. dataSegment.concat(Tai_const.Create_16bit(_class.iidguid^.D2));
  1017. dataSegment.concat(Tai_const.Create_16bit(_class.iidguid^.D3));
  1018. for i:=Low(_class.iidguid^.D4) to High(_class.iidguid^.D4) do
  1019. dataSegment.concat(Tai_const.Create_8bit(_class.iidguid^.D4[i]));
  1020. end;
  1021. if (cs_create_smart in aktmoduleswitches) then
  1022. dataSegment.concat(Tai_cut.Create);
  1023. dataSegment.concat(Tai_symbol.Createname_global(make_mangledname('IIDSTR',_class.owner,_class.objname^),0));
  1024. dataSegment.concat(Tai_const.Create_8bit(length(_class.iidstr^)));
  1025. dataSegment.concat(Tai_string.Create(_class.iidstr^));
  1026. end;
  1027. procedure tclassheader.writevirtualmethods(List:TAAsmoutput);
  1028. var
  1029. symcoll : psymcoll;
  1030. procdefcoll : pprocdefcoll;
  1031. i : longint;
  1032. begin
  1033. { walk trough all numbers for virtual methods and search }
  1034. { the method }
  1035. for i:=0 to nextvirtnumber-1 do
  1036. begin
  1037. symcoll:=wurzel;
  1038. { walk trough all symbols }
  1039. while assigned(symcoll) do
  1040. begin
  1041. { walk trough all methods }
  1042. procdefcoll:=symcoll^.data;
  1043. while assigned(procdefcoll) do
  1044. begin
  1045. { writes the addresses to the VMT }
  1046. { but only this which are declared as virtual }
  1047. if procdefcoll^.data.extnumber=i then
  1048. begin
  1049. if (po_virtualmethod in procdefcoll^.data.procoptions) then
  1050. begin
  1051. { if a method is abstract, then is also the }
  1052. { class abstract and it's not allow to }
  1053. { generates an instance }
  1054. if (po_abstractmethod in procdefcoll^.data.procoptions) then
  1055. List.concat(Tai_const_symbol.Createname('FPC_ABSTRACTERROR'))
  1056. else
  1057. List.concat(Tai_const_symbol.createname(procdefcoll^.data.mangledname));
  1058. end;
  1059. end;
  1060. procdefcoll:=procdefcoll^.next;
  1061. end;
  1062. symcoll:=symcoll^.next;
  1063. end;
  1064. end;
  1065. end;
  1066. { generates the vmt for classes as well as for objects }
  1067. procedure tclassheader.writevmt;
  1068. var
  1069. methodnametable,intmessagetable,
  1070. strmessagetable,classnamelabel,
  1071. fieldtablelabel : tasmlabel;
  1072. {$ifdef WITHDMT}
  1073. dmtlabel : tasmlabel;
  1074. {$endif WITHDMT}
  1075. interfacetable : tasmlabel;
  1076. begin
  1077. {$ifdef WITHDMT}
  1078. dmtlabel:=gendmt;
  1079. {$endif WITHDMT}
  1080. if (cs_create_smart in aktmoduleswitches) then
  1081. dataSegment.concat(Tai_cut.Create);
  1082. { write tables for classes, this must be done before the actual
  1083. class is written, because we need the labels defined }
  1084. if is_class(_class) then
  1085. begin
  1086. { interface table }
  1087. if _class.implementedinterfaces.count>0 then
  1088. begin
  1089. if (cs_create_smart in aktmoduleswitches) then
  1090. codeSegment.concat(Tai_cut.Create);
  1091. interfacetable:=genintftable;
  1092. end;
  1093. methodnametable:=genpublishedmethodstable;
  1094. fieldtablelabel:=_class.generate_field_table;
  1095. { write class name }
  1096. objectlibrary.getdatalabel(classnamelabel);
  1097. dataSegment.concat(tai_align.create(const_align(POINTER_SIZE)));
  1098. dataSegment.concat(Tai_label.Create(classnamelabel));
  1099. dataSegment.concat(Tai_const.Create_8bit(length(_class.objrealname^)));
  1100. dataSegment.concat(Tai_string.Create(_class.objrealname^));
  1101. { generate message and dynamic tables }
  1102. if (oo_has_msgstr in _class.objectoptions) then
  1103. strmessagetable:=genstrmsgtab;
  1104. if (oo_has_msgint in _class.objectoptions) then
  1105. intmessagetable:=genintmsgtab;
  1106. end;
  1107. { write debug info }
  1108. {$ifdef GDB}
  1109. if (cs_debuginfo in aktmoduleswitches) then
  1110. begin
  1111. do_count_dbx:=true;
  1112. if assigned(_class.owner) and assigned(_class.owner.name) then
  1113. dataSegment.concat(Tai_stabs.Create(strpnew('"vmt_'+_class.owner.name^+_class.name+':S'+
  1114. typeglobalnumber('__vtbl_ptr_type')+'",'+tostr(N_STSYM)+',0,0,'+_class.vmt_mangledname)));
  1115. end;
  1116. {$endif GDB}
  1117. dataSegment.concat(tai_align.create(const_align(POINTER_SIZE)));
  1118. dataSegment.concat(Tai_symbol.Createdataname_global(_class.vmt_mangledname,0));
  1119. { determine the size with symtable.datasize, because }
  1120. { size gives back 4 for classes }
  1121. dataSegment.concat(Tai_const.Create_32bit(tobjectsymtable(_class.symtable).datasize));
  1122. dataSegment.concat(Tai_const.Create_32bit(-tobjectsymtable(_class.symtable).datasize));
  1123. {$ifdef WITHDMT}
  1124. if _class.classtype=ct_object then
  1125. begin
  1126. if assigned(dmtlabel) then
  1127. dataSegment.concat(Tai_const_symbol.Create(dmtlabel)))
  1128. else
  1129. dataSegment.concat(Tai_const.Create_32bit(0));
  1130. end;
  1131. {$endif WITHDMT}
  1132. { write pointer to parent VMT, this isn't implemented in TP }
  1133. { but this is not used in FPC ? (PM) }
  1134. { it's not used yet, but the delphi-operators as and is need it (FK) }
  1135. { it is not written for parents that don't have any vmt !! }
  1136. if assigned(_class.childof) and
  1137. (oo_has_vmt in _class.childof.objectoptions) then
  1138. dataSegment.concat(Tai_const_symbol.Createname(_class.childof.vmt_mangledname))
  1139. else
  1140. dataSegment.concat(Tai_const.Create_32bit(0));
  1141. { write extended info for classes, for the order see rtl/inc/objpash.inc }
  1142. if is_class(_class) then
  1143. begin
  1144. { pointer to class name string }
  1145. dataSegment.concat(Tai_const_symbol.Create(classnamelabel));
  1146. { pointer to dynamic table }
  1147. if (oo_has_msgint in _class.objectoptions) then
  1148. dataSegment.concat(Tai_const_symbol.Create(intmessagetable))
  1149. else
  1150. dataSegment.concat(Tai_const.Create_32bit(0));
  1151. { pointer to method table }
  1152. if assigned(methodnametable) then
  1153. dataSegment.concat(Tai_const_symbol.Create(methodnametable))
  1154. else
  1155. dataSegment.concat(Tai_const.Create_32bit(0));
  1156. { pointer to field table }
  1157. dataSegment.concat(Tai_const_symbol.Create(fieldtablelabel));
  1158. { pointer to type info of published section }
  1159. if (oo_can_have_published in _class.objectoptions) then
  1160. dataSegment.concat(Tai_const_symbol.Create(_class.get_rtti_label(fullrtti)))
  1161. else
  1162. dataSegment.concat(Tai_const.Create_32bit(0));
  1163. { inittable for con-/destruction }
  1164. if _class.members_need_inittable then
  1165. dataSegment.concat(Tai_const_symbol.Create(_class.get_rtti_label(initrtti)))
  1166. else
  1167. dataSegment.concat(Tai_const.Create_32bit(0));
  1168. { auto table }
  1169. dataSegment.concat(Tai_const.Create_32bit(0));
  1170. { interface table }
  1171. if _class.implementedinterfaces.count>0 then
  1172. dataSegment.concat(Tai_const_symbol.Create(interfacetable))
  1173. else
  1174. dataSegment.concat(Tai_const.Create_32bit(0));
  1175. { table for string messages }
  1176. if (oo_has_msgstr in _class.objectoptions) then
  1177. dataSegment.concat(Tai_const_symbol.Create(strmessagetable))
  1178. else
  1179. dataSegment.concat(Tai_const.Create_32bit(0));
  1180. end;
  1181. { write virtual methods }
  1182. writevirtualmethods(dataSegment);
  1183. { write the size of the VMT }
  1184. dataSegment.concat(Tai_symbol_end.Createname(_class.vmt_mangledname));
  1185. end;
  1186. procedure tclassheader.adjustselfvalue(procdef: tprocdef;ioffset: aword);
  1187. var
  1188. hsym : tsym;
  1189. href : treference;
  1190. locpara : tparalocation;
  1191. begin
  1192. { calculate the parameter info for the procdef }
  1193. if not procdef.has_paraloc_info then
  1194. begin
  1195. paramanager.create_paraloc_info(procdef,callerside);
  1196. procdef.has_paraloc_info:=true;
  1197. end;
  1198. hsym:=tsym(procdef.parast.search('self'));
  1199. if not(assigned(hsym) and
  1200. (hsym.typ=varsym) and
  1201. assigned(tvarsym(hsym).paraitem)) then
  1202. internalerror(200305251);
  1203. locpara:=tvarsym(hsym).paraitem.paraloc[callerside];
  1204. case locpara.loc of
  1205. LOC_REGISTER:
  1206. cg.a_op_const_reg(exprasmlist,OP_SUB,locpara.size,ioffset,locpara.register);
  1207. LOC_REFERENCE:
  1208. begin
  1209. reference_reset_base(href,locpara.reference.index,locpara.reference.offset);
  1210. cg.a_op_const_ref(exprasmlist,OP_SUB,locpara.size,ioffset,href);
  1211. end
  1212. else
  1213. internalerror(200309189);
  1214. end;
  1215. end;
  1216. initialization
  1217. cclassheader:=tclassheader;
  1218. end.
  1219. {
  1220. $Log$
  1221. Revision 1.55 2003-10-30 16:23:13 peter
  1222. * don't search for overloads in parents for constructors
  1223. Revision 1.54 2003/10/29 19:48:50 peter
  1224. * renamed mangeldname_prefix to make_mangledname and made it more
  1225. generic
  1226. * make_mangledname is now also used for internal threadvar/resstring
  1227. lists
  1228. * Add P$ in front of program modulename to prevent duplicated symbols
  1229. at assembler level, because the main program can have the same name
  1230. as a unit, see webtbs/tw1251b
  1231. Revision 1.53 2003/10/13 14:05:12 peter
  1232. * removed is_visible_for_proc
  1233. * search also for class overloads when finding interface
  1234. implementations
  1235. Revision 1.52 2003/10/10 17:48:13 peter
  1236. * old trgobj moved to x86/rgcpu and renamed to trgx86fpu
  1237. * tregisteralloctor renamed to trgobj
  1238. * removed rgobj from a lot of units
  1239. * moved location_* and reference_* to cgobj
  1240. * first things for mmx register allocation
  1241. Revision 1.51 2003/10/07 21:14:32 peter
  1242. * compare_paras() has a parameter to ignore hidden parameters
  1243. * cross unit overload searching ignores hidden parameters when
  1244. comparing parameter lists. Now function(string):string is
  1245. not overriden with procedure(string) which has the same visible
  1246. parameter list
  1247. Revision 1.50 2003/10/07 20:44:22 peter
  1248. * inherited forced calling convention
  1249. * show hints when forward doesn't match
  1250. Revision 1.49 2003/10/01 20:34:49 peter
  1251. * procinfo unit contains tprocinfo
  1252. * cginfo renamed to cgbase
  1253. * moved cgmessage to verbose
  1254. * fixed ppc and sparc compiles
  1255. Revision 1.48 2003/09/23 17:56:05 peter
  1256. * locals and paras are allocated in the code generation
  1257. * tvarsym.localloc contains the location of para/local when
  1258. generating code for the current procedure
  1259. Revision 1.47 2003/08/21 14:47:41 peter
  1260. * remove convert_registers
  1261. Revision 1.46 2003/08/20 09:07:00 daniel
  1262. * New register coding now mandatory, some more convert_registers calls
  1263. removed.
  1264. Revision 1.45 2003/08/10 17:25:23 peter
  1265. * fixed some reported bugs
  1266. Revision 1.44 2003/06/01 21:38:06 peter
  1267. * getregisterfpu size parameter added
  1268. * op_const_reg size parameter added
  1269. * sparc updates
  1270. Revision 1.43 2003/05/23 14:27:35 peter
  1271. * remove some unit dependencies
  1272. * current_procinfo changes to store more info
  1273. Revision 1.42 2003/04/25 20:59:33 peter
  1274. * removed funcretn,funcretsym, function result is now in varsym
  1275. and aliases for result and function name are added using absolutesym
  1276. * vs_hidden parameter for funcret passed in parameter
  1277. * vs_hidden fixes
  1278. * writenode changed to printnode and released from extdebug
  1279. * -vp option added to generate a tree.log with the nodetree
  1280. * nicer printnode for statements, callnode
  1281. Revision 1.41 2003/04/23 10:11:22 peter
  1282. * range check error for GUID fixed
  1283. Revision 1.40 2003/01/13 14:54:34 daniel
  1284. * Further work to convert codegenerator register convention;
  1285. internalerror bug fixed.
  1286. Revision 1.39 2003/01/09 21:52:37 peter
  1287. * merged some verbosity options.
  1288. * V_LineInfo is a verbosity flag to include line info
  1289. Revision 1.38 2002/11/25 17:43:20 peter
  1290. * splitted defbase in defutil,symutil,defcmp
  1291. * merged isconvertable and is_equal into compare_defs(_ext)
  1292. * made operator search faster by walking the list only once
  1293. Revision 1.37 2002/11/17 16:31:56 carl
  1294. * memory optimization (3-4%) : cleanup of tai fields,
  1295. cleanup of tdef and tsym fields.
  1296. * make it work for m68k
  1297. Revision 1.36 2002/11/15 01:58:52 peter
  1298. * merged changes from 1.0.7 up to 04-11
  1299. - -V option for generating bug report tracing
  1300. - more tracing for option parsing
  1301. - errors for cdecl and high()
  1302. - win32 import stabs
  1303. - win32 records<=8 are returned in eax:edx (turned off by default)
  1304. - heaptrc update
  1305. - more info for temp management in .s file with EXTDEBUG
  1306. Revision 1.35 2002/11/09 16:19:43 carl
  1307. - remove superfluous data in classname
  1308. Revision 1.34 2002/11/09 15:35:35 carl
  1309. * major alignment updates for objects/class tables
  1310. Revision 1.33 2002/10/20 15:33:36 peter
  1311. * having overloads is the same as overload directive for hiding of
  1312. parent methods. This is required becuase it can be possible that a
  1313. method will then hide a method in the parent that an overloaded
  1314. method requires. See webbug tw2185
  1315. Revision 1.32 2002/10/19 15:09:24 peter
  1316. + tobjectdef.members_need_inittable that is used to generate only the
  1317. inittable when it is really used. This saves a lot of useless calls
  1318. to fpc_finalize when destroying classes
  1319. Revision 1.31 2002/10/15 19:00:42 peter
  1320. * small tweak to use speedvalue before comparing strings
  1321. Revision 1.30 2002/10/06 16:40:25 florian
  1322. * interface wrapper name mangling improved
  1323. Revision 1.29 2002/10/05 12:43:25 carl
  1324. * fixes for Delphi 6 compilation
  1325. (warning : Some features do not work under Delphi)
  1326. Revision 1.28 2002/09/16 14:11:13 peter
  1327. * add argument to equal_paras() to support default values or not
  1328. Revision 1.27 2002/09/03 16:26:26 daniel
  1329. * Make Tprocdef.defs protected
  1330. Revision 1.26 2002/09/03 15:44:44 peter
  1331. * fixed private methods hiding public virtual methods
  1332. Revision 1.25 2002/08/11 14:32:27 peter
  1333. * renamed current_library to objectlibrary
  1334. Revision 1.24 2002/08/11 13:24:12 peter
  1335. * saving of asmsymbols in ppu supported
  1336. * asmsymbollist global is removed and moved into a new class
  1337. tasmlibrarydata that will hold the info of a .a file which
  1338. corresponds with a single module. Added librarydata to tmodule
  1339. to keep the library info stored for the module. In the future the
  1340. objectfiles will also be stored to the tasmlibrarydata class
  1341. * all getlabel/newasmsymbol and friends are moved to the new class
  1342. Revision 1.23 2002/08/09 07:33:01 florian
  1343. * a couple of interface related fixes
  1344. Revision 1.22 2002/07/20 11:57:55 florian
  1345. * types.pas renamed to defbase.pas because D6 contains a types
  1346. unit so this would conflicts if D6 programms are compiled
  1347. + Willamette/SSE2 instructions to assembler added
  1348. Revision 1.21 2002/07/01 18:46:23 peter
  1349. * internal linker
  1350. * reorganized aasm layer
  1351. Revision 1.20 2002/05/18 13:34:10 peter
  1352. * readded missing revisions
  1353. Revision 1.19 2002/05/16 19:46:39 carl
  1354. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1355. + try to fix temp allocation (still in ifdef)
  1356. + generic constructor calls
  1357. + start of tassembler / tmodulebase class cleanup
  1358. Revision 1.17 2002/05/12 16:53:08 peter
  1359. * moved entry and exitcode to ncgutil and cgobj
  1360. * foreach gets extra argument for passing local data to the
  1361. iterator function
  1362. * -CR checks also class typecasts at runtime by changing them
  1363. into as
  1364. * fixed compiler to cycle with the -CR option
  1365. * fixed stabs with elf writer, finally the global variables can
  1366. be watched
  1367. * removed a lot of routines from cga unit and replaced them by
  1368. calls to cgobj
  1369. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1370. u32bit then the other is typecasted also to u32bit without giving
  1371. a rangecheck warning/error.
  1372. * fixed pascal calling method with reversing also the high tree in
  1373. the parast, detected by tcalcst3 test
  1374. Revision 1.16 2002/04/20 21:32:24 carl
  1375. + generic FPC_CHECKPOINTER
  1376. + first parameter offset in stack now portable
  1377. * rename some constants
  1378. + move some cpu stuff to other units
  1379. - remove unused constents
  1380. * fix stacksize for some targets
  1381. * fix generic size problems which depend now on EXTEND_SIZE constant
  1382. Revision 1.15 2002/04/19 15:46:01 peter
  1383. * mangledname rewrite, tprocdef.mangledname is now created dynamicly
  1384. in most cases and not written to the ppu
  1385. * add mangeledname_prefix() routine to generate the prefix of
  1386. manglednames depending on the current procedure, object and module
  1387. * removed static procprefix since the mangledname is now build only
  1388. on demand from tprocdef.mangledname
  1389. Revision 1.14 2002/04/15 18:59:07 carl
  1390. + target_info.size_of_pointer -> pointer_Size
  1391. Revision 1.13 2002/02/11 18:51:35 peter
  1392. * fixed vmt generation for private procedures that were skipped after
  1393. my previous changes
  1394. }