nobj.pas 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  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: aint);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,systems,
  134. symtable,symconst,symtype,symsym,defcmp,paramgr,
  135. {$ifdef GDB}
  136. gdb,
  137. {$endif GDB}
  138. aasmcpu,
  139. cpubase,cgbase,
  140. cgutils,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(sizeof(aint))));
  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.Create_sym(p^.nl));
  266. dataSegment.concat(Tai_const.Createname(p^.data.mangledname,AT_FUNCTION,0));
  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(sizeof(aint))));
  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.Createname(p^.data.mangledname,AT_FUNCTION,0));
  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(sizeof(aint))));
  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,AT_FUNCTION,0));
  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(sizeof(aint))));
  379. dataSegment.concat(Tai_label.Create(r));
  380. { entries for caching }
  381. dataSegment.concat(Tai_const.Create_ptr(0));
  382. dataSegment.concat(Tai_const.Create_ptr(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(sizeof(aint))));
  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.Create_sym(l));
  417. dataSegment.concat(Tai_const.Createname(hp.mangledname,AT_FUNCTION,0));
  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(sizeof(aint))));
  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 that all methods have overload directive }
  534. if not(m_fpc in aktmodeswitches) and
  535. (_class=pd._class) and
  536. (procdefcoll^.data._class=pd._class) and
  537. ((po_overload in pd.procoptions)<>(po_overload in procdefcoll^.data.procoptions)) then
  538. begin
  539. MessagePos1(pd.fileinfo,parser_e_no_overload_for_all_procs,pd.procsym.realname);
  540. { recover }
  541. include(procdefcoll^.data.procoptions,po_overload);
  542. include(pd.procoptions,po_overload);
  543. end;
  544. { check if one of the two methods has virtual }
  545. if (po_virtualmethod in procdefcoll^.data.procoptions) or
  546. (po_virtualmethod in pd.procoptions) then
  547. begin
  548. { if the current definition has no virtual then hide the
  549. old virtual if the new definition has the same arguments or
  550. when it has no overload directive and no overloads }
  551. if not(po_virtualmethod in pd.procoptions) then
  552. begin
  553. if tstoredsym(procdefcoll^.data.procsym).is_visible_for_object(pd._class) and
  554. (not(pdoverload or hasoverloads) or
  555. (compare_paras(procdefcoll^.data.para,pd.para,cp_all,[])>=te_equal)) then
  556. begin
  557. if is_visible then
  558. procdefcoll^.hidden:=true;
  559. if (_class=pd._class) and not(po_reintroduce in pd.procoptions) then
  560. MessagePos1(pd.fileinfo,parser_w_should_use_override,pd.fullprocname(false));
  561. end;
  562. end
  563. { if both are virtual we check the header }
  564. else if (po_virtualmethod in pd.procoptions) and
  565. (po_virtualmethod in procdefcoll^.data.procoptions) then
  566. begin
  567. { new one has not override }
  568. if is_class(_class) and
  569. not(po_overridingmethod in pd.procoptions) then
  570. begin
  571. { we start a new virtual tree, hide the old }
  572. if (not(pdoverload or hasoverloads) or
  573. (compare_paras(procdefcoll^.data.para,pd.para,cp_all,[])>=te_equal)) and
  574. (tstoredsym(procdefcoll^.data.procsym).is_visible_for_object(pd._class)) then
  575. begin
  576. if is_visible then
  577. procdefcoll^.hidden:=true;
  578. if (_class=pd._class) and not(po_reintroduce in pd.procoptions) then
  579. MessagePos1(pd.fileinfo,parser_w_should_use_override,pd.fullprocname(false));
  580. end;
  581. end
  582. { check if the method to override is visible }
  583. else if (po_overridingmethod in pd.procoptions) and
  584. (not tstoredsym(procdefcoll^.data.procsym).is_visible_for_object(pd._class)) then
  585. begin
  586. { do nothing, the error will follow when adding the entry }
  587. end
  588. { same parameters }
  589. else if (compare_paras(procdefcoll^.data.para,pd.para,cp_all,[])>=te_equal) then
  590. begin
  591. { overload is inherited }
  592. if (po_overload in procdefcoll^.data.procoptions) then
  593. include(pd.procoptions,po_overload);
  594. { inherite calling convention when it was force and the
  595. current definition has none force }
  596. if (po_hascallingconvention in procdefcoll^.data.procoptions) and
  597. not(po_hascallingconvention in pd.procoptions) then
  598. begin
  599. pd.proccalloption:=procdefcoll^.data.proccalloption;
  600. include(pd.procoptions,po_hascallingconvention);
  601. end;
  602. { the flags have to match except abstract and override }
  603. { only if both are virtual !! }
  604. if (procdefcoll^.data.proccalloption<>pd.proccalloption) or
  605. (procdefcoll^.data.proctypeoption<>pd.proctypeoption) or
  606. ((procdefcoll^.data.procoptions-
  607. [po_abstractmethod,po_overridingmethod,po_assembler,po_overload,po_public,po_reintroduce])<>
  608. (pd.procoptions-[po_abstractmethod,po_overridingmethod,po_assembler,po_overload,po_public,po_reintroduce])) then
  609. begin
  610. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,pd.fullprocname(false));
  611. tprocsym(procdefcoll^.data.procsym).write_parameter_lists(pd);
  612. end;
  613. { error, if the return types aren't equal }
  614. if not(equal_defs(procdefcoll^.data.rettype.def,pd.rettype.def)) and
  615. not((procdefcoll^.data.rettype.def.deftype=objectdef) and
  616. (pd.rettype.def.deftype=objectdef) and
  617. is_class(procdefcoll^.data.rettype.def) and
  618. is_class(pd.rettype.def) and
  619. (tobjectdef(pd.rettype.def).is_related(
  620. tobjectdef(procdefcoll^.data.rettype.def)))) then
  621. Message2(parser_e_overridden_methods_not_same_ret,pd.fullprocname(false),
  622. procdefcoll^.data.fullprocname(false));
  623. { now set the number }
  624. pd.extnumber:=procdefcoll^.data.extnumber;
  625. { and exchange }
  626. procdefcoll^.data:=pd;
  627. goto handlenextdef;
  628. end
  629. { different parameters }
  630. else
  631. begin
  632. { when we got an override directive then can search futher for
  633. the procedure to override.
  634. If we are starting a new virtual tree then hide the old tree }
  635. if not(po_overridingmethod in pd.procoptions) and
  636. not pdoverload then
  637. begin
  638. if is_visible then
  639. procdefcoll^.hidden:=true;
  640. if (_class=pd._class) and not(po_reintroduce in pd.procoptions) then
  641. MessagePos1(pd.fileinfo,parser_w_should_use_override,pd.fullprocname(false));
  642. end;
  643. end;
  644. end
  645. else
  646. begin
  647. { the new definition is virtual and the old static, we hide the old one
  648. if the new defintion has not the overload directive }
  649. if is_visible and
  650. ((not(pdoverload or hasoverloads)) or
  651. (compare_paras(procdefcoll^.data.para,pd.para,cp_all,[])>=te_equal)) then
  652. procdefcoll^.hidden:=true;
  653. end;
  654. end
  655. else
  656. begin
  657. { both are static, we hide the old one if the new defintion
  658. has not the overload directive }
  659. if is_visible and
  660. ((not pdoverload) or
  661. (compare_paras(procdefcoll^.data.para,pd.para,cp_all,[])>=te_equal)) then
  662. procdefcoll^.hidden:=true;
  663. end;
  664. end; { not hidden }
  665. procdefcoll:=procdefcoll^.next;
  666. end;
  667. { if it isn't saved in the list we create a new entry }
  668. newdefentry(pd);
  669. end;
  670. handlenextdef:
  671. end;
  672. exit;
  673. end;
  674. symcoll:=symcoll^.next;
  675. end;
  676. newentry;
  677. end;
  678. end;
  679. procedure tclassheader.disposevmttree;
  680. var
  681. symcoll : psymcoll;
  682. procdefcoll : pprocdefcoll;
  683. begin
  684. { disposes the above generated tree }
  685. symcoll:=wurzel;
  686. while assigned(symcoll) do
  687. begin
  688. wurzel:=symcoll^.next;
  689. stringdispose(symcoll^.name);
  690. procdefcoll:=symcoll^.data;
  691. while assigned(procdefcoll) do
  692. begin
  693. symcoll^.data:=procdefcoll^.next;
  694. dispose(procdefcoll);
  695. procdefcoll:=symcoll^.data;
  696. end;
  697. dispose(symcoll);
  698. symcoll:=wurzel;
  699. end;
  700. end;
  701. procedure tclassheader.genvmt;
  702. procedure do_genvmt(p : tobjectdef);
  703. begin
  704. { start with the base class }
  705. if assigned(p.childof) then
  706. do_genvmt(p.childof);
  707. { walk through all public syms }
  708. p.symtable.foreach({$ifdef FPCPROCVAR}@{$endif}eachsym,nil);
  709. end;
  710. begin
  711. wurzel:=nil;
  712. nextvirtnumber:=0;
  713. has_constructor:=false;
  714. has_virtual_method:=false;
  715. { generates a tree of all used methods }
  716. do_genvmt(_class);
  717. if not(is_interface(_class)) and
  718. has_virtual_method and
  719. not(has_constructor) then
  720. Message1(parser_w_virtual_without_constructor,_class.objrealname^);
  721. end;
  722. {**************************************
  723. Interface tables
  724. **************************************}
  725. function tclassheader.gintfgetvtbllabelname(intfindex: integer): string;
  726. begin
  727. gintfgetvtbllabelname:=make_mangledname('VTBL',_class.owner,_class.objname^+
  728. '_$_'+_class.implementedinterfaces.interfaces(intfindex).objname^);
  729. end;
  730. procedure tclassheader.gintfcreatevtbl(intfindex: integer; rawdata,rawcode: TAAsmoutput);
  731. var
  732. implintf: timplementedinterfaces;
  733. curintf: tobjectdef;
  734. proccount: integer;
  735. tmps: string;
  736. i: longint;
  737. begin
  738. implintf:=_class.implementedinterfaces;
  739. curintf:=implintf.interfaces(intfindex);
  740. rawdata.concat(tai_align.create(const_align(sizeof(aint))));
  741. if maybe_smartlink_symbol then
  742. rawdata.concat(Tai_symbol.Createname_global(gintfgetvtbllabelname(intfindex),AT_DATA ,0))
  743. else
  744. rawdata.concat(Tai_symbol.Createname(gintfgetvtbllabelname(intfindex),AT_DATA,0));
  745. proccount:=implintf.implproccount(intfindex);
  746. for i:=1 to proccount do
  747. begin
  748. tmps:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+curintf.objname^+'_$_'+
  749. tostr(i)+'_$_'+
  750. implintf.implprocs(intfindex,i).mangledname);
  751. { create wrapper code }
  752. cgintfwrapper(rawcode,implintf.implprocs(intfindex,i),tmps,implintf.ioffsets(intfindex)^);
  753. { create reference }
  754. rawdata.concat(Tai_const.Createname(tmps,AT_FUNCTION,0));
  755. end;
  756. end;
  757. procedure tclassheader.gintfgenentry(intfindex, contintfindex: integer; rawdata: TAAsmoutput);
  758. var
  759. implintf: timplementedinterfaces;
  760. curintf: tobjectdef;
  761. tmplabel: tasmlabel;
  762. i: longint;
  763. begin
  764. implintf:=_class.implementedinterfaces;
  765. curintf:=implintf.interfaces(intfindex);
  766. { GUID }
  767. if curintf.objecttype in [odt_interfacecom] then
  768. begin
  769. { label for GUID }
  770. objectlibrary.getdatalabel(tmplabel);
  771. rawdata.concat(tai_align.create(const_align(sizeof(aint))));
  772. rawdata.concat(Tai_label.Create(tmplabel));
  773. rawdata.concat(Tai_const.Create_32bit(longint(curintf.iidguid^.D1)));
  774. rawdata.concat(Tai_const.Create_16bit(curintf.iidguid^.D2));
  775. rawdata.concat(Tai_const.Create_16bit(curintf.iidguid^.D3));
  776. for i:=Low(curintf.iidguid^.D4) to High(curintf.iidguid^.D4) do
  777. rawdata.concat(Tai_const.Create_8bit(curintf.iidguid^.D4[i]));
  778. dataSegment.concat(Tai_const.Create_sym(tmplabel));
  779. end
  780. else
  781. begin
  782. { nil for Corba interfaces }
  783. dataSegment.concat(Tai_const.Create_sym(nil));
  784. end;
  785. { VTable }
  786. dataSegment.concat(Tai_const.Createname(gintfgetvtbllabelname(contintfindex),AT_DATA,0));
  787. { IOffset field }
  788. dataSegment.concat(Tai_const.Create_32bit(implintf.ioffsets(contintfindex)^));
  789. { IIDStr }
  790. objectlibrary.getdatalabel(tmplabel);
  791. rawdata.concat(tai_align.create(const_align(sizeof(aint))));
  792. rawdata.concat(Tai_label.Create(tmplabel));
  793. rawdata.concat(Tai_const.Create_8bit(length(curintf.iidstr^)));
  794. if curintf.objecttype=odt_interfacecom then
  795. rawdata.concat(Tai_string.Create(upper(curintf.iidstr^)))
  796. else
  797. rawdata.concat(Tai_string.Create(curintf.iidstr^));
  798. dataSegment.concat(Tai_const.Create_sym(tmplabel));
  799. end;
  800. procedure tclassheader.gintfoptimizevtbls(implvtbl : plongintarray);
  801. type
  802. tcompintfentry = record
  803. weight: longint;
  804. compintf: longint;
  805. end;
  806. { Max 1000 interface in the class header interfaces it's enough imho }
  807. tcompintfs = packed array[1..1000] of tcompintfentry;
  808. pcompintfs = ^tcompintfs;
  809. tequals = packed array[1..1000] of longint;
  810. pequals = ^tequals;
  811. var
  812. max: longint;
  813. equals: pequals;
  814. compats: pcompintfs;
  815. i: longint;
  816. j: longint;
  817. w: longint;
  818. cij: boolean;
  819. cji: boolean;
  820. begin
  821. max:=_class.implementedinterfaces.count;
  822. if max>High(tequals) then
  823. Internalerror(200006135);
  824. getmem(compats,sizeof(tcompintfentry)*max);
  825. getmem(equals,sizeof(longint)*max);
  826. fillchar(compats^,sizeof(tcompintfentry)*max,0);
  827. fillchar(equals^,sizeof(longint)*max,0);
  828. { ismergepossible is a containing relation
  829. meaning of ismergepossible(a,b,w) =
  830. if implementorfunction map of a is contained implementorfunction map of b
  831. imp(a,b) and imp(b,c) => imp(a,c) ; imp(a,b) and imp(b,a) => a == b
  832. }
  833. { the order is very important for correct allocation }
  834. for i:=1 to max do
  835. begin
  836. for j:=i+1 to max do
  837. begin
  838. cij:=_class.implementedinterfaces.isimplmergepossible(i,j,w);
  839. cji:=_class.implementedinterfaces.isimplmergepossible(j,i,w);
  840. if cij and cji then { i equal j }
  841. begin
  842. { get minimum index of equal }
  843. if equals^[j]=0 then
  844. equals^[j]:=i;
  845. end
  846. else if cij then
  847. begin
  848. { get minimum index of maximum weight }
  849. if compats^[i].weight<w then
  850. begin
  851. compats^[i].weight:=w;
  852. compats^[i].compintf:=j;
  853. end;
  854. end
  855. else if cji then
  856. begin
  857. { get minimum index of maximum weight }
  858. if (compats^[j].weight<w) then
  859. begin
  860. compats^[j].weight:=w;
  861. compats^[j].compintf:=i;
  862. end;
  863. end;
  864. end;
  865. end;
  866. for i:=1 to max do
  867. begin
  868. if compats^[i].compintf<>0 then
  869. implvtbl[i]:=compats^[i].compintf
  870. else if equals^[i]<>0 then
  871. implvtbl[i]:=equals^[i]
  872. else
  873. implvtbl[i]:=i;
  874. end;
  875. freemem(compats,sizeof(tcompintfentry)*max);
  876. freemem(equals,sizeof(longint)*max);
  877. end;
  878. procedure tclassheader.gintfwritedata;
  879. var
  880. rawdata,rawcode: taasmoutput;
  881. impintfindexes: plongintarray;
  882. max: longint;
  883. i: longint;
  884. begin
  885. max:=_class.implementedinterfaces.count;
  886. getmem(impintfindexes,(max+1)*sizeof(longint));
  887. gintfoptimizevtbls(impintfindexes);
  888. rawdata:=TAAsmOutput.Create;
  889. rawcode:=TAAsmOutput.Create;
  890. dataSegment.concat(Tai_const.Create_16bit(max));
  891. { Two pass, one for allocation and vtbl creation }
  892. for i:=1 to max do
  893. begin
  894. if impintfindexes[i]=i then { if implement itself }
  895. begin
  896. { allocate a pointer in the object memory }
  897. with tobjectsymtable(_class.symtable) do
  898. begin
  899. datasize:=align(datasize,min(sizeof(aint),fieldalignment));
  900. _class.implementedinterfaces.ioffsets(i)^:=datasize;
  901. inc(datasize,sizeof(aint));
  902. end;
  903. { write vtbl }
  904. gintfcreatevtbl(i,rawdata,rawcode);
  905. end;
  906. end;
  907. { second pass: for fill interfacetable and remained ioffsets }
  908. for i:=1 to max do
  909. begin
  910. if i<>impintfindexes[i] then { why execute x:=x ? }
  911. with _class.implementedinterfaces do
  912. ioffsets(i)^:=ioffsets(impintfindexes[i])^;
  913. gintfgenentry(i,impintfindexes[i],rawdata);
  914. end;
  915. dataSegment.concatlist(rawdata);
  916. rawdata.free;
  917. codeSegment.concatlist(rawcode);
  918. rawcode.free;
  919. freemem(impintfindexes,(max+1)*sizeof(longint));
  920. end;
  921. function tclassheader.gintfgetcprocdef(proc: tprocdef;const name: string): tprocdef;
  922. var
  923. sym: tsym;
  924. implprocdef : Tprocdef;
  925. i: cardinal;
  926. begin
  927. gintfgetcprocdef:=nil;
  928. sym:=tsym(search_class_member(_class,name));
  929. if assigned(sym) and
  930. (sym.typ=procsym) then
  931. begin
  932. { when the definition has overload directive set, we search for
  933. overloaded definitions in the class, this only needs to be done once
  934. for class entries as the tree keeps always the same }
  935. if (not tprocsym(sym).overloadchecked) and
  936. (po_overload in tprocsym(sym).first_procdef.procoptions) and
  937. (tprocsym(sym).owner.symtabletype=objectsymtable) then
  938. search_class_overloads(tprocsym(sym));
  939. for i:=1 to tprocsym(sym).procdef_count do
  940. begin
  941. implprocdef:=tprocsym(sym).procdef[i];
  942. if (compare_paras(proc.para,implprocdef.para,cp_none,[])>=te_equal) and
  943. (proc.proccalloption=implprocdef.proccalloption) then
  944. begin
  945. gintfgetcprocdef:=implprocdef;
  946. exit;
  947. end;
  948. end;
  949. end;
  950. end;
  951. procedure tclassheader.gintfdoonintf(intf: tobjectdef; intfindex: longint);
  952. var
  953. def: tdef;
  954. procname: string; { for error }
  955. mappedname: string;
  956. nextexist: pointer;
  957. implprocdef: tprocdef;
  958. begin
  959. def:=tdef(intf.symtable.defindex.first);
  960. while assigned(def) do
  961. begin
  962. if def.deftype=procdef then
  963. begin
  964. procname:='';
  965. implprocdef:=nil;
  966. nextexist:=nil;
  967. repeat
  968. mappedname:=_class.implementedinterfaces.getmappings(intfindex,tprocdef(def).procsym.name,nextexist);
  969. if procname='' then
  970. procname:=tprocdef(def).procsym.name;
  971. //mappedname; { for error messages }
  972. if mappedname<>'' then
  973. implprocdef:=gintfgetcprocdef(tprocdef(def),mappedname);
  974. until assigned(implprocdef) or not assigned(nextexist);
  975. if not assigned(implprocdef) then
  976. implprocdef:=gintfgetcprocdef(tprocdef(def),tprocdef(def).procsym.name);
  977. if procname='' then
  978. procname:=tprocdef(def).procsym.name;
  979. if assigned(implprocdef) then
  980. _class.implementedinterfaces.addimplproc(intfindex,implprocdef)
  981. else
  982. Message1(sym_e_no_matching_implementation_found,tprocdef(def).fullprocname(false));
  983. end;
  984. def:=tdef(def.indexnext);
  985. end;
  986. end;
  987. procedure tclassheader.gintfwalkdowninterface(intf: tobjectdef; intfindex: longint);
  988. begin
  989. if assigned(intf.childof) then
  990. gintfwalkdowninterface(intf.childof,intfindex);
  991. gintfdoonintf(intf,intfindex);
  992. end;
  993. function tclassheader.genintftable: tasmlabel;
  994. var
  995. intfindex: longint;
  996. curintf: tobjectdef;
  997. intftable: tasmlabel;
  998. begin
  999. { 1. step collect implementor functions into the implementedinterfaces.implprocs }
  1000. for intfindex:=1 to _class.implementedinterfaces.count do
  1001. begin
  1002. curintf:=_class.implementedinterfaces.interfaces(intfindex);
  1003. gintfwalkdowninterface(curintf,intfindex);
  1004. end;
  1005. { 2. step calc required fieldcount and their offsets in the object memory map
  1006. and write data }
  1007. objectlibrary.getdatalabel(intftable);
  1008. dataSegment.concat(tai_align.create(const_align(sizeof(aint))));
  1009. dataSegment.concat(Tai_label.Create(intftable));
  1010. gintfwritedata;
  1011. _class.implementedinterfaces.clearimplprocs; { release temporary information }
  1012. genintftable:=intftable;
  1013. end;
  1014. { Write interface identifiers to the data section }
  1015. procedure tclassheader.writeinterfaceids;
  1016. var
  1017. i : longint;
  1018. s : string;
  1019. begin
  1020. if assigned(_class.iidguid) then
  1021. begin
  1022. s:=make_mangledname('IID',_class.owner,_class.objname^);
  1023. maybe_new_object_file(dataSegment);
  1024. new_section(dataSegment,sec_rodata,s,const_align(sizeof(aint)));
  1025. dataSegment.concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  1026. dataSegment.concat(Tai_const.Create_32bit(longint(_class.iidguid^.D1)));
  1027. dataSegment.concat(Tai_const.Create_16bit(_class.iidguid^.D2));
  1028. dataSegment.concat(Tai_const.Create_16bit(_class.iidguid^.D3));
  1029. for i:=Low(_class.iidguid^.D4) to High(_class.iidguid^.D4) do
  1030. dataSegment.concat(Tai_const.Create_8bit(_class.iidguid^.D4[i]));
  1031. end;
  1032. maybe_new_object_file(dataSegment);
  1033. s:=make_mangledname('IIDSTR',_class.owner,_class.objname^);
  1034. new_section(dataSegment,sec_rodata,s,0);
  1035. dataSegment.concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  1036. dataSegment.concat(Tai_const.Create_8bit(length(_class.iidstr^)));
  1037. dataSegment.concat(Tai_string.Create(_class.iidstr^));
  1038. end;
  1039. procedure tclassheader.writevirtualmethods(List:TAAsmoutput);
  1040. var
  1041. symcoll : psymcoll;
  1042. procdefcoll : pprocdefcoll;
  1043. i : longint;
  1044. begin
  1045. { walk trough all numbers for virtual methods and search }
  1046. { the method }
  1047. for i:=0 to nextvirtnumber-1 do
  1048. begin
  1049. symcoll:=wurzel;
  1050. { walk trough all symbols }
  1051. while assigned(symcoll) do
  1052. begin
  1053. { walk trough all methods }
  1054. procdefcoll:=symcoll^.data;
  1055. while assigned(procdefcoll) do
  1056. begin
  1057. { writes the addresses to the VMT }
  1058. { but only this which are declared as virtual }
  1059. if procdefcoll^.data.extnumber=i then
  1060. begin
  1061. if (po_virtualmethod in procdefcoll^.data.procoptions) then
  1062. begin
  1063. { if a method is abstract, then is also the }
  1064. { class abstract and it's not allow to }
  1065. { generates an instance }
  1066. if (po_abstractmethod in procdefcoll^.data.procoptions) then
  1067. List.concat(Tai_const.Createname('FPC_ABSTRACTERROR',AT_FUNCTION,0))
  1068. else
  1069. List.concat(Tai_const.createname(procdefcoll^.data.mangledname,AT_FUNCTION,0));
  1070. end;
  1071. end;
  1072. procdefcoll:=procdefcoll^.next;
  1073. end;
  1074. symcoll:=symcoll^.next;
  1075. end;
  1076. end;
  1077. end;
  1078. { generates the vmt for classes as well as for objects }
  1079. procedure tclassheader.writevmt;
  1080. var
  1081. methodnametable,intmessagetable,
  1082. strmessagetable,classnamelabel,
  1083. fieldtablelabel : tasmlabel;
  1084. {$ifdef WITHDMT}
  1085. dmtlabel : tasmlabel;
  1086. {$endif WITHDMT}
  1087. interfacetable : tasmlabel;
  1088. begin
  1089. {$ifdef WITHDMT}
  1090. dmtlabel:=gendmt;
  1091. {$endif WITHDMT}
  1092. { write tables for classes, this must be done before the actual
  1093. class is written, because we need the labels defined }
  1094. if is_class(_class) then
  1095. begin
  1096. objectlibrary.getdatalabel(classnamelabel);
  1097. maybe_new_object_file(dataSegment);
  1098. new_section(dataSegment,sec_rodata,classnamelabel.name,const_align(sizeof(aint)));
  1099. { interface table }
  1100. if _class.implementedinterfaces.count>0 then
  1101. interfacetable:=genintftable;
  1102. methodnametable:=genpublishedmethodstable;
  1103. fieldtablelabel:=_class.generate_field_table;
  1104. { write class name }
  1105. dataSegment.concat(Tai_label.Create(classnamelabel));
  1106. dataSegment.concat(Tai_const.Create_8bit(length(_class.objrealname^)));
  1107. dataSegment.concat(Tai_string.Create(_class.objrealname^));
  1108. { generate message and dynamic tables }
  1109. if (oo_has_msgstr in _class.objectoptions) then
  1110. strmessagetable:=genstrmsgtab;
  1111. if (oo_has_msgint in _class.objectoptions) then
  1112. intmessagetable:=genintmsgtab;
  1113. end;
  1114. { write debug info }
  1115. maybe_new_object_file(dataSegment);
  1116. new_section(dataSegment,sec_rodata,_class.vmt_mangledname,const_align(sizeof(aint)));
  1117. {$ifdef GDB}
  1118. if (cs_debuginfo in aktmoduleswitches) then
  1119. begin
  1120. do_count_dbx:=true;
  1121. if assigned(_class.owner) and assigned(_class.owner.name) then
  1122. dataSegment.concat(Tai_stabs.Create(strpnew('"vmt_'+_class.owner.name^+_class.name+':S'+
  1123. tstoreddef(vmttype.def).numberstring+'",'+tostr(N_STSYM)+',0,0,'+_class.vmt_mangledname)));
  1124. end;
  1125. {$endif GDB}
  1126. dataSegment.concat(Tai_symbol.Createname_global(_class.vmt_mangledname,AT_DATA,0));
  1127. { determine the size with symtable.datasize, because }
  1128. { size gives back 4 for classes }
  1129. dataSegment.concat(Tai_const.Create(ait_const_ptr,tobjectsymtable(_class.symtable).datasize));
  1130. dataSegment.concat(Tai_const.Create(ait_const_ptr,-int64(tobjectsymtable(_class.symtable).datasize)));
  1131. {$ifdef WITHDMT}
  1132. if _class.classtype=ct_object then
  1133. begin
  1134. if assigned(dmtlabel) then
  1135. dataSegment.concat(Tai_const_symbol.Create(dmtlabel)))
  1136. else
  1137. dataSegment.concat(Tai_const.Create_ptr(0));
  1138. end;
  1139. {$endif WITHDMT}
  1140. { write pointer to parent VMT, this isn't implemented in TP }
  1141. { but this is not used in FPC ? (PM) }
  1142. { it's not used yet, but the delphi-operators as and is need it (FK) }
  1143. { it is not written for parents that don't have any vmt !! }
  1144. if assigned(_class.childof) and
  1145. (oo_has_vmt in _class.childof.objectoptions) then
  1146. dataSegment.concat(Tai_const.Createname(_class.childof.vmt_mangledname,AT_DATA,0))
  1147. else
  1148. dataSegment.concat(Tai_const.Create_sym(nil));
  1149. { write extended info for classes, for the order see rtl/inc/objpash.inc }
  1150. if is_class(_class) then
  1151. begin
  1152. { pointer to class name string }
  1153. dataSegment.concat(Tai_const.Create_sym(classnamelabel));
  1154. { pointer to dynamic table or nil }
  1155. if (oo_has_msgint in _class.objectoptions) then
  1156. dataSegment.concat(Tai_const.Create_sym(intmessagetable))
  1157. else
  1158. dataSegment.concat(Tai_const.Create_sym(nil));
  1159. { pointer to method table or nil }
  1160. dataSegment.concat(Tai_const.Create_sym(methodnametable));
  1161. { pointer to field table }
  1162. dataSegment.concat(Tai_const.Create_sym(fieldtablelabel));
  1163. { pointer to type info of published section }
  1164. if (oo_can_have_published in _class.objectoptions) then
  1165. dataSegment.concat(Tai_const.Create_sym(_class.get_rtti_label(fullrtti)))
  1166. else
  1167. dataSegment.concat(Tai_const.Create_sym(nil));
  1168. { inittable for con-/destruction }
  1169. if _class.members_need_inittable then
  1170. dataSegment.concat(Tai_const.Create_sym(_class.get_rtti_label(initrtti)))
  1171. else
  1172. dataSegment.concat(Tai_const.Create_sym(nil));
  1173. { auto table }
  1174. dataSegment.concat(Tai_const.Create_sym(nil));
  1175. { interface table }
  1176. if _class.implementedinterfaces.count>0 then
  1177. dataSegment.concat(Tai_const.Create_sym(interfacetable))
  1178. else
  1179. dataSegment.concat(Tai_const.Create_sym(nil));
  1180. { table for string messages }
  1181. if (oo_has_msgstr in _class.objectoptions) then
  1182. dataSegment.concat(Tai_const.Create_sym(strmessagetable))
  1183. else
  1184. dataSegment.concat(Tai_const.Create_sym(nil));
  1185. end;
  1186. { write virtual methods }
  1187. writevirtualmethods(dataSegment);
  1188. { write the size of the VMT }
  1189. dataSegment.concat(Tai_symbol_end.Createname(_class.vmt_mangledname));
  1190. end;
  1191. procedure tclassheader.adjustselfvalue(procdef: tprocdef;ioffset: aint);
  1192. var
  1193. hsym : tsym;
  1194. href : treference;
  1195. locpara : tparalocation;
  1196. begin
  1197. { calculate the parameter info for the procdef }
  1198. if not procdef.has_paraloc_info then
  1199. begin
  1200. procdef.requiredargarea:=paramanager.create_paraloc_info(procdef,callerside);
  1201. procdef.has_paraloc_info:=true;
  1202. end;
  1203. hsym:=tsym(procdef.parast.search('self'));
  1204. if not(assigned(hsym) and
  1205. (hsym.typ=varsym) and
  1206. assigned(tvarsym(hsym).paraitem)) then
  1207. internalerror(200305251);
  1208. locpara:=tvarsym(hsym).paraitem.paraloc[callerside];
  1209. case locpara.loc of
  1210. LOC_REGISTER:
  1211. cg.a_op_const_reg(exprasmlist,OP_SUB,locpara.size,ioffset,locpara.register);
  1212. LOC_REFERENCE:
  1213. begin
  1214. { offset in the wrapper needs to be adjusted for the stored
  1215. return address }
  1216. reference_reset_base(href,locpara.reference.index,locpara.reference.offset+sizeof(aint));
  1217. cg.a_op_const_ref(exprasmlist,OP_SUB,locpara.size,ioffset,href);
  1218. end
  1219. else
  1220. internalerror(200309189);
  1221. end;
  1222. end;
  1223. initialization
  1224. cclassheader:=tclassheader;
  1225. end.
  1226. {
  1227. $Log$
  1228. Revision 1.71 2004-06-20 08:55:29 florian
  1229. * logs truncated
  1230. Revision 1.70 2004/06/16 20:07:09 florian
  1231. * dwarf branch merged
  1232. Revision 1.69.2.8 2004/05/10 21:28:34 peter
  1233. * section_smartlink enabled for gas under linux
  1234. Revision 1.69.2.7 2004/05/01 16:02:09 peter
  1235. * POINTER_SIZE replaced with sizeof(aint)
  1236. * aint,aword,tconst*int moved to globtype
  1237. Revision 1.69.2.6 2004/04/28 20:36:13 florian
  1238. * fixed writing of sizes in classes/object vmts
  1239. Revision 1.69.2.5 2004/04/27 18:18:26 peter
  1240. * aword -> aint
  1241. }