nobj.pas 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Routines for the code generation of data structures
  4. like VMT, Messages, VTables, Interfaces descs
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit nobj;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cutils,cclasses,
  23. globtype,
  24. symdef,symsym,
  25. aasmbase,aasmtai,aasmdata
  26. ;
  27. type
  28. TVMTBuilder=class
  29. private
  30. _Class : tobjectdef;
  31. handledprotocols: tfpobjectlist;
  32. function is_new_vmt_entry(pd:tprocdef; out overridesclasshelper: boolean):boolean;
  33. procedure add_new_vmt_entry(pd:tprocdef; allowoverridingmethod: boolean);
  34. function check_msg_str(vmtpd, pd: tprocdef):boolean;
  35. function intf_search_procdef_by_name(proc: tprocdef;const name: string): tprocdef;
  36. procedure intf_get_procdefs(ImplIntf:TImplementedInterface;IntfDef:TObjectDef);
  37. procedure intf_get_procdefs_recursive(ImplIntf:TImplementedInterface;IntfDef:TObjectDef);
  38. procedure prot_get_procdefs_recursive(ImplProt:TImplementedInterface;ProtDef:TObjectDef);
  39. procedure intf_optimize_vtbls;
  40. procedure intf_allocate_vtbls;
  41. public
  42. constructor create(c:tobjectdef);
  43. destructor destroy;override;
  44. procedure generate_vmt;
  45. procedure build_interface_mappings;
  46. end;
  47. type
  48. pprocdeftree = ^tprocdeftree;
  49. tprocdeftree = record
  50. data : tprocdef;
  51. nl : tasmlabel;
  52. l,r : pprocdeftree;
  53. end;
  54. TVMTWriter=class
  55. private
  56. _Class : tobjectdef;
  57. { message tables }
  58. root : pprocdeftree;
  59. procedure disposeprocdeftree(p : pprocdeftree);
  60. procedure insertmsgint(p:TObject;arg:pointer);
  61. procedure insertmsgstr(p:TObject;arg:pointer);
  62. procedure insertint(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  63. procedure insertstr(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  64. procedure writenames(p : pprocdeftree);
  65. procedure writeintentry(p : pprocdeftree);
  66. procedure writestrentry(p : pprocdeftree);
  67. {$ifdef WITHDMT}
  68. { dmt }
  69. procedure insertdmtentry(p:TObject;arg:pointer);
  70. procedure writedmtindexentry(p : pprocdeftree);
  71. procedure writedmtaddressentry(p : pprocdeftree);
  72. {$endif}
  73. { published methods }
  74. procedure do_count_published_methods(p:TObject;arg:pointer);
  75. procedure do_gen_published_methods(p:TObject;arg:pointer);
  76. { virtual methods }
  77. procedure writevirtualmethods(List:TAsmList);
  78. { interface tables }
  79. function intf_get_vtbl_name(AImplIntf:TImplementedInterface): string;
  80. procedure intf_create_vtbl(rawdata: TAsmList;AImplIntf:TImplementedInterface);
  81. procedure intf_gen_intf_ref(rawdata: TAsmList;AImplIntf:TImplementedInterface);
  82. function intf_write_table:TAsmLabel;
  83. { generates the message tables for a class }
  84. function genstrmsgtab : tasmlabel;
  85. function genintmsgtab : tasmlabel;
  86. function genpublishedmethodstable : tasmlabel;
  87. function generate_field_table : tasmlabel;
  88. {$ifdef WITHDMT}
  89. { generates a DMT for _class }
  90. function gendmt : tasmlabel;
  91. {$endif WITHDMT}
  92. public
  93. constructor create(c:tobjectdef);
  94. destructor destroy;override;
  95. { write the VMT to al_globals }
  96. procedure writevmt;
  97. procedure writeinterfaceids;
  98. end;
  99. implementation
  100. uses
  101. SysUtils,
  102. globals,verbose,systems,
  103. node,
  104. symbase,symtable,symconst,symtype,defcmp,
  105. dbgbase,
  106. ncgrtti,
  107. wpobase
  108. ;
  109. {*****************************************************************************
  110. TVMTBuilder
  111. *****************************************************************************}
  112. constructor TVMTBuilder.create(c:tobjectdef);
  113. begin
  114. inherited Create;
  115. _Class:=c;
  116. end;
  117. destructor TVMTBuilder.destroy;
  118. begin
  119. end;
  120. procedure TVMTBuilder.add_new_vmt_entry(pd:tprocdef; allowoverridingmethod: boolean);
  121. var
  122. i : longint;
  123. vmtentry : pvmtentry;
  124. vmtpd : tprocdef;
  125. begin
  126. { new entry is needed, override was not possible }
  127. { Allowed when overriding a category method for a parent class in a
  128. descendent Objective-C class }
  129. if not allowoverridingmethod and
  130. (po_overridingmethod in pd.procoptions) then
  131. MessagePos1(pd.fileinfo,parser_e_nothing_to_be_overridden,pd.fullprocname(false));
  132. { check that all methods have overload directive }
  133. if not(m_fpc in current_settings.modeswitches) then
  134. begin
  135. for i:=0 to _class.vmtentries.count-1 do
  136. begin
  137. vmtentry:=pvmtentry(_class.vmtentries[i]);
  138. vmtpd:=tprocdef(vmtentry^.procdef);
  139. if (vmtpd.procsym=pd.procsym) and
  140. (not(po_overload in pd.procoptions) or
  141. not(po_overload in vmtpd.procoptions)) then
  142. begin
  143. MessagePos1(pd.fileinfo,parser_e_no_overload_for_all_procs,pd.procsym.realname);
  144. { recover }
  145. include(vmtpd.procoptions,po_overload);
  146. include(pd.procoptions,po_overload);
  147. end;
  148. end;
  149. end;
  150. { Register virtual method and give it a number }
  151. if (po_virtualmethod in pd.procoptions) then
  152. begin
  153. { store vmt entry number in procdef }
  154. if (pd.extnumber<>$ffff) and
  155. (pd.extnumber<>_class.VMTEntries.Count) then
  156. internalerror(200810283);
  157. pd.extnumber:=_class.VMTEntries.Count;
  158. new(vmtentry);
  159. vmtentry^.procdef:=pd;
  160. vmtentry^.procdefderef.reset;
  161. vmtentry^.visibility:=pd.visibility;
  162. _class.VMTEntries.Add(vmtentry);
  163. end;
  164. end;
  165. function TVMTBuilder.check_msg_str(vmtpd, pd: tprocdef): boolean;
  166. begin
  167. result:=true;
  168. if not(is_objc_class_or_protocol(_class)) then
  169. begin
  170. { the only requirement for normal methods is that both either
  171. have a message string or not (the value is irrelevant) }
  172. if ((pd.procoptions * [po_msgstr]) <> (vmtpd.procoptions * [po_msgstr])) then
  173. begin
  174. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,pd.fullprocname(false));
  175. tprocsym(vmtpd.procsym).write_parameter_lists(pd);
  176. result:=false;
  177. end
  178. end
  179. else
  180. begin
  181. { the compiler should have ensured that the protocol or parent
  182. class method has a message name specified }
  183. if not(po_msgstr in vmtpd.procoptions) then
  184. internalerror(2009070601);
  185. if not(po_msgstr in pd.procoptions) then
  186. begin
  187. { copy the protocol's/parent class' message name to the one in
  188. the class if none has been specified there }
  189. include(pd.procoptions,po_msgstr);
  190. pd.messageinf.str:=stringdup(vmtpd.messageinf.str^);
  191. end
  192. else
  193. begin
  194. { if both have a message name, make sure they are equal }
  195. if (vmtpd.messageinf.str^<>pd.messageinf.str^) then
  196. begin
  197. MessagePos2(pd.fileinfo,parser_e_objc_message_name_changed,vmtpd.messageinf.str^,pd.messageinf.str^);
  198. result:=false;
  199. end;
  200. end;
  201. end;
  202. end;
  203. function TVMTBuilder.is_new_vmt_entry(pd:tprocdef; out overridesclasshelper: boolean):boolean;
  204. const
  205. po_comp = [po_classmethod,po_virtualmethod,po_staticmethod,po_interrupt,po_iocheck,po_msgint,
  206. po_exports,po_varargs,po_explicitparaloc,po_nostackframe];
  207. var
  208. i : longint;
  209. hasequalpara,
  210. hasoverloads,
  211. pdoverload : boolean;
  212. srsym : tsym;
  213. st : tsymtable;
  214. // returns true if we can stop checking, false if we have to continue
  215. function found_entry(var vmtpd: tprocdef; var vmtentryvis: tvisibility; updatevalues: boolean): boolean;
  216. begin
  217. result:=false;
  218. { ignore hidden entries (e.g. virtual overridden by a static) that are not visible anymore }
  219. if vmtentryvis=vis_hidden then
  220. exit;
  221. { ignore different names }
  222. if vmtpd.procsym.name<>pd.procsym.name then
  223. exit;
  224. { hide private methods that are not visible anymore. For this check we
  225. must override the visibility with the highest value in the override chain.
  226. This is required for case (see tw3292) with protected-private-protected where the
  227. same vmtentry is used (PFV) }
  228. if not is_visible_for_object(vmtpd.owner,vmtentryvis,_class) then
  229. exit;
  230. { inherit overload }
  231. if (po_overload in vmtpd.procoptions) then
  232. begin
  233. include(pd.procoptions,po_overload);
  234. pdoverload:=true;
  235. end;
  236. { compare parameter types only, no specifiers yet }
  237. hasequalpara:=(compare_paras(vmtpd.paras,pd.paras,cp_none,[cpo_ignoreuniv,cpo_ignorehidden])>=te_equal);
  238. { check that we are not trying to override a final method }
  239. if (po_finalmethod in vmtpd.procoptions) and
  240. hasequalpara and (po_overridingmethod in pd.procoptions) and
  241. (is_class(_class) or is_objectpascal_helper(_class)) then
  242. MessagePos1(pd.fileinfo,parser_e_final_can_no_be_overridden,pd.fullprocname(false))
  243. else
  244. { old definition has virtual
  245. new definition has no virtual or override }
  246. if (po_virtualmethod in vmtpd.procoptions) and
  247. (
  248. not(po_virtualmethod in pd.procoptions) or
  249. (
  250. { new one does not have reintroduce in case of an objccategory }
  251. (is_objccategory(_class) and
  252. not(po_reintroduce in pd.procoptions)) or
  253. { new one does not have override in case of objpas/objc/java class/intf/proto }
  254. ((is_class_or_interface_or_objc_or_java(_class) or is_objectpascal_helper(_class)) and
  255. not is_objccategory(_class) and
  256. not(po_overridingmethod in pd.procoptions)
  257. )
  258. )
  259. ) then
  260. begin
  261. if (
  262. not(pdoverload or hasoverloads) or
  263. hasequalpara
  264. ) then
  265. begin
  266. if not(po_reintroduce in pd.procoptions) then
  267. if not(is_objc_class_or_protocol(_class)) and
  268. not(is_java_class_or_interface(_class)) then
  269. MessagePos1(pd.fileinfo,parser_w_should_use_override,pd.fullprocname(false))
  270. else
  271. begin
  272. { In Objective-C, you cannot create a new VMT entry to
  273. start a new inheritance tree. We therefore give an
  274. error when the class is implemented in Pascal, to
  275. avoid confusion due to things working differently
  276. with Object Pascal classes.
  277. In case of external classes, we only give a hint,
  278. because requiring override everywhere may make
  279. automated header translation tools too complex.
  280. The same goes for Java. }
  281. if not(oo_is_external in _class.objectoptions) then
  282. if not is_objccategory(_class) then
  283. MessagePos1(pd.fileinfo,parser_e_must_use_override,FullTypeName(tdef(vmtpd.owner.defowner),nil))
  284. else
  285. MessagePos1(pd.fileinfo,parser_e_must_use_reintroduce_objc,FullTypeName(tdef(vmtpd.owner.defowner),nil))
  286. { there may be a lot of these in auto-translated
  287. headers, so only calculate the fulltypename if
  288. the hint will be shown }
  289. else if CheckVerbosity(V_Hint) then
  290. if not is_objccategory(_class) then
  291. MessagePos1(pd.fileinfo,parser_h_should_use_override,FullTypeName(tdef(vmtpd.owner.defowner),nil))
  292. else
  293. MessagePos1(pd.fileinfo,parser_h_should_use_reintroduce_objc,FullTypeName(tdef(vmtpd.owner.defowner),nil));
  294. { no new entry, but copy the message name if any from
  295. the procdef in the parent class }
  296. check_msg_str(vmtpd,pd);
  297. { in case of Java, copy the real name from the parent,
  298. since overriding "Destroy" with "destroy" is not
  299. going to work very well }
  300. if is_java_class_or_interface(_class) and
  301. (pd.procsym.realname<>vmtpd.procsym.realname) then
  302. pd.procsym.realname:=vmtpd.procsym.realname;
  303. { in case we are overriding an abstract method,
  304. decrease the number of abstract methods in this class }
  305. if (po_abstractmethod in vmtpd.procoptions) then
  306. dec(tobjectdef(pd.owner.defowner).abstractcnt);
  307. result:=true;
  308. exit;
  309. end;
  310. { disable/hide old VMT entry }
  311. if updatevalues then
  312. vmtentryvis:=vis_hidden;
  313. end;
  314. end
  315. { both are virtual? }
  316. else if (po_virtualmethod in pd.procoptions) and
  317. (po_virtualmethod in vmtpd.procoptions) then
  318. begin
  319. { same parameter and return types (parameter specifiers will be checked below) }
  320. if hasequalpara and
  321. compatible_childmethod_resultdef(vmtpd.returndef,pd.returndef) then
  322. begin
  323. { inherite calling convention when it was explicit and the
  324. current definition has none explicit set }
  325. if (po_hascallingconvention in vmtpd.procoptions) and
  326. not(po_hascallingconvention in pd.procoptions) then
  327. begin
  328. pd.proccalloption:=vmtpd.proccalloption;
  329. include(pd.procoptions,po_hascallingconvention);
  330. end;
  331. { All parameter specifiers and some procedure the flags have to match
  332. except abstract and override }
  333. if (compare_paras(vmtpd.paras,pd.paras,cp_all,[cpo_ignoreuniv,cpo_ignorehidden])<te_equal) or
  334. (vmtpd.proccalloption<>pd.proccalloption) or
  335. (vmtpd.proctypeoption<>pd.proctypeoption) or
  336. ((vmtpd.procoptions*po_comp)<>(pd.procoptions*po_comp)) then
  337. begin
  338. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,pd.fullprocname(false));
  339. tprocsym(vmtpd.procsym).write_parameter_lists(pd);
  340. end;
  341. check_msg_str(vmtpd,pd);
  342. { Give a note if the new visibility is lower. For a higher
  343. visibility update the vmt info }
  344. if vmtentryvis>pd.visibility then
  345. MessagePos4(pd.fileinfo,parser_n_ignore_lower_visibility,pd.fullprocname(false),
  346. visibilityname[pd.visibility],tobjectdef(vmtpd.owner.defowner).objrealname^,visibilityname[vmtentryvis])
  347. else if pd.visibility>vmtentryvis then
  348. begin
  349. if updatevalues then
  350. vmtentryvis:=pd.visibility;
  351. end;
  352. { in case we are overriding an abstract method,
  353. decrease the number of abstract methods in this class }
  354. if (po_overridingmethod in pd.procoptions) and
  355. (po_abstractmethod in vmtpd.procoptions) then
  356. dec(tobjectdef(pd.owner.defowner).abstractcnt);
  357. { override old virtual method in VMT }
  358. if updatevalues then
  359. begin
  360. if (vmtpd.extnumber<>i) then
  361. internalerror(200611084);
  362. pd.extnumber:=vmtpd.extnumber;
  363. { in case of Java, copy the real name from the parent,
  364. since overriding "Destroy" with "destroy" is not
  365. going to work very well }
  366. if is_java_class_or_interface(_class) and
  367. (pd.procsym.realname<>vmtpd.procsym.realname) then
  368. pd.procsym.realname:=vmtpd.procsym.realname;
  369. vmtpd:=pd;
  370. end;
  371. result:=true;
  372. exit;
  373. end
  374. { different parameters }
  375. else
  376. begin
  377. { when we got an override directive then can search futher for
  378. the procedure to override.
  379. If we are starting a new virtual tree then hide the old tree }
  380. if not(po_overridingmethod in pd.procoptions) and
  381. not(pdoverload or hasoverloads) then
  382. begin
  383. if not(po_reintroduce in pd.procoptions) then
  384. begin
  385. if not is_object(_class) and
  386. not is_objc_class_or_protocol(_class) and
  387. not is_java_class_or_interface(_class) then
  388. MessagePos1(pd.fileinfo,parser_w_should_use_override,pd.fullprocname(false))
  389. else
  390. { objects don't allow starting a new virtual tree
  391. and neither do Objective-C or Java }
  392. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,vmtpd.fullprocname(false));
  393. end;
  394. { disable/hide old VMT entry }
  395. if updatevalues then
  396. vmtentryvis:=vis_hidden;
  397. end;
  398. end;
  399. end;
  400. end;
  401. function found_category_method(st: tsymtable): boolean;
  402. var
  403. entrycount: longint;
  404. cat: tobjectdef;
  405. vmtpd: tprocdef;
  406. vmtvis: tvisibility;
  407. begin
  408. result:=false;
  409. if is_objccategory(tdef(st.defowner)) then
  410. begin
  411. cat:=tobjectdef(st.defowner);
  412. { go through all of the category's methods to find the
  413. vmtentry corresponding to the procdef we are handling }
  414. for entrycount:=0 to cat.vmtentries.Count-1 do
  415. begin
  416. vmtpd:=pvmtentry(cat.vmtentries[entrycount])^.procdef;
  417. vmtvis:=pvmtentry(cat.vmtentries[entrycount])^.visibility;
  418. { don't change the vmtentry of the category }
  419. if found_entry(vmtpd,vmtvis,false) then
  420. begin
  421. result:=true;
  422. exit;
  423. end;
  424. end;
  425. end;
  426. end;
  427. begin
  428. result:=false;
  429. overridesclasshelper:=false;
  430. { Load other values for easier readability }
  431. hasoverloads:=(tprocsym(pd.procsym).ProcdefList.Count>1);
  432. pdoverload:=(po_overload in pd.procoptions);
  433. { compare with all stored definitions }
  434. for i:=0 to _class.vmtentries.Count-1 do
  435. begin
  436. if found_entry(pvmtentry(_class.vmtentries[i])^.procdef, pvmtentry(_class.vmtentries[i])^.visibility,true) then
  437. exit;
  438. end;
  439. { in case of Objective-C, also check the categories that apply to this
  440. class' *parent* for methods to override (don't allow class X to
  441. "override" a method added by a category to class X itself, since in
  442. that case the category method will in fact replace class X'
  443. "overriding" method }
  444. if is_objcclass(_class) and
  445. assigned(_class.childof) and
  446. search_objc_helper(_class.childof,pd.procsym.name,srsym,st) then
  447. begin
  448. overridesclasshelper:=found_category_method(st);
  449. end;
  450. { No entry found, we need to create a new entry }
  451. result:=true;
  452. end;
  453. function TVMTBuilder.intf_search_procdef_by_name(proc: tprocdef;const name: string): tprocdef;
  454. const
  455. po_comp = [po_classmethod,po_staticmethod,po_interrupt,po_iocheck,po_msgint,
  456. po_exports,po_varargs,po_explicitparaloc,po_nostackframe];
  457. var
  458. implprocdef : Tprocdef;
  459. i: cardinal;
  460. hclass : tobjectdef;
  461. hashedid : THashedIDString;
  462. srsym : tsym;
  463. begin
  464. result:=nil;
  465. hashedid.id:=name;
  466. hclass:=_class;
  467. while assigned(hclass) do
  468. begin
  469. srsym:=tsym(hclass.symtable.FindWithHash(hashedid));
  470. if assigned(srsym) and
  471. (srsym.typ=procsym) then
  472. begin
  473. for i:=0 to Tprocsym(srsym).ProcdefList.Count-1 do
  474. begin
  475. implprocdef:=tprocdef(tprocsym(srsym).ProcdefList[i]);
  476. if (implprocdef.procsym=tprocsym(srsym)) and
  477. (compare_paras(proc.paras,implprocdef.paras,cp_all,[cpo_ignorehidden,cpo_comparedefaultvalue,cpo_ignoreuniv])>=te_equal) and
  478. (compare_defs(proc.returndef,implprocdef.returndef,nothingn)>=te_equal) and
  479. (proc.proccalloption=implprocdef.proccalloption) and
  480. (proc.proctypeoption=implprocdef.proctypeoption) and
  481. ((proc.procoptions*po_comp)=((implprocdef.procoptions+[po_virtualmethod])*po_comp)) and
  482. check_msg_str(proc,implprocdef) then
  483. begin
  484. result:=implprocdef;
  485. exit;
  486. end;
  487. end;
  488. end;
  489. hclass:=hclass.childof;
  490. end;
  491. end;
  492. procedure TVMTBuilder.intf_get_procdefs(ImplIntf:TImplementedInterface;IntfDef:TObjectDef);
  493. var
  494. i : longint;
  495. def : tdef;
  496. hs,
  497. prefix,
  498. mappedname: string;
  499. implprocdef: tprocdef;
  500. begin
  501. prefix:=ImplIntf.IntfDef.symtable.name^+'.';
  502. for i:=0 to IntfDef.symtable.DefList.Count-1 do
  503. begin
  504. def:=tdef(IntfDef.symtable.DefList[i]);
  505. if assigned(def) and
  506. (def.typ=procdef) then
  507. begin
  508. { Find implementing procdef
  509. 1. Check for mapped name
  510. 2. Use symbol name, but only if there's no mapping,
  511. or we're processing ancestor of interface.
  512. When modifying this code, ensure that webtbs/tw11862, webtbs/tw4950
  513. and webtbf/tw19591 stay correct. }
  514. implprocdef:=nil;
  515. hs:=prefix+tprocdef(def).procsym.name;
  516. mappedname:=ImplIntf.GetMapping(hs);
  517. if mappedname<>'' then
  518. implprocdef:=intf_search_procdef_by_name(tprocdef(def),mappedname);
  519. if not assigned(implprocdef) then
  520. if (mappedname='') or (ImplIntf.IntfDef<>IntfDef) then
  521. implprocdef:=intf_search_procdef_by_name(tprocdef(def),tprocdef(def).procsym.name);
  522. { Add procdef to the implemented interface }
  523. if assigned(implprocdef) then
  524. begin
  525. if (tobjectdef(implprocdef.struct).objecttype<>odt_objcclass) then
  526. ImplIntf.AddImplProc(implprocdef)
  527. else
  528. begin
  529. { If no message name has been specified for the method
  530. in the objcclass, copy it from the protocol
  531. definition. }
  532. if not(po_msgstr in tprocdef(def).procoptions) then
  533. begin
  534. include(tprocdef(def).procoptions,po_msgstr);
  535. implprocdef.messageinf.str:=stringdup(tprocdef(def).messageinf.str^);
  536. end
  537. else
  538. begin
  539. { If a message name has been specified in the
  540. objcclass, it has to match the message name in the
  541. protocol definition. }
  542. if (implprocdef.messageinf.str^<>tprocdef(def).messageinf.str^) then
  543. MessagePos2(implprocdef.fileinfo,parser_e_objc_message_name_changed,tprocdef(def).messageinf.str^,implprocdef.messageinf.str^);
  544. end;
  545. end;
  546. end
  547. else
  548. if (ImplIntf.IType=etStandard) and
  549. not(po_optional in tprocdef(def).procoptions) then
  550. MessagePos1(_Class.typesym.fileinfo,sym_e_no_matching_implementation_found,tprocdef(def).fullprocname(false));
  551. end;
  552. end;
  553. end;
  554. procedure TVMTBuilder.intf_get_procdefs_recursive(ImplIntf:TImplementedInterface;IntfDef:TObjectDef);
  555. begin
  556. if assigned(IntfDef.childof) then
  557. intf_get_procdefs_recursive(ImplIntf,IntfDef.childof);
  558. intf_get_procdefs(ImplIntf,IntfDef);
  559. end;
  560. procedure TVMTBuilder.prot_get_procdefs_recursive(ImplProt:TImplementedInterface;ProtDef:TObjectDef);
  561. var
  562. i: longint;
  563. begin
  564. { don't check the same protocol twice }
  565. if handledprotocols.IndexOf(ProtDef)<>-1 then
  566. exit;
  567. handledprotocols.add(ProtDef);
  568. for i:=0 to ProtDef.ImplementedInterfaces.count-1 do
  569. prot_get_procdefs_recursive(ImplProt,TImplementedInterface(ProtDef.ImplementedInterfaces[i]).intfdef);
  570. intf_get_procdefs(ImplProt,ProtDef);
  571. end;
  572. procedure TVMTBuilder.intf_optimize_vtbls;
  573. type
  574. tcompintfentry = record
  575. weight: longint;
  576. compintf: longint;
  577. end;
  578. { Max 1000 interface in the class header interfaces it's enough imho }
  579. tcompintfs = array[0..1000] of tcompintfentry;
  580. pcompintfs = ^tcompintfs;
  581. tequals = array[0..1000] of longint;
  582. pequals = ^tequals;
  583. timpls = array[0..1000] of longint;
  584. pimpls = ^timpls;
  585. var
  586. aequals: pequals;
  587. compats: pcompintfs;
  588. impls: pimpls;
  589. ImplIntfCount,
  590. w,i,j,k: longint;
  591. ImplIntfI,
  592. ImplIntfJ : TImplementedInterface;
  593. cij: boolean;
  594. cji: boolean;
  595. begin
  596. ImplIntfCount:=_class.ImplementedInterfaces.count;
  597. if ImplIntfCount>=High(tequals) then
  598. Internalerror(200006135);
  599. getmem(compats,sizeof(tcompintfentry)*ImplIntfCount);
  600. getmem(aequals,sizeof(longint)*ImplIntfCount);
  601. getmem(impls,sizeof(longint)*ImplIntfCount);
  602. filldword(compats^,(sizeof(tcompintfentry) div sizeof(dword))*ImplIntfCount,dword(-1));
  603. filldword(aequals^,ImplIntfCount,dword(-1));
  604. filldword(impls^,ImplIntfCount,dword(-1));
  605. { ismergepossible is a containing relation
  606. meaning of ismergepossible(a,b,w) =
  607. if implementorfunction map of a is contained implementorfunction map of b
  608. imp(a,b) and imp(b,c) => imp(a,c) ; imp(a,b) and imp(b,a) => a == b
  609. }
  610. { the order is very important for correct allocation }
  611. for i:=0 to ImplIntfCount-1 do
  612. begin
  613. for j:=i+1 to ImplIntfCount-1 do
  614. begin
  615. ImplIntfI:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  616. ImplIntfJ:=TImplementedInterface(_class.ImplementedInterfaces[j]);
  617. cij:=ImplIntfI.IsImplMergePossible(ImplIntfJ,w);
  618. cji:=ImplIntfJ.IsImplMergePossible(ImplIntfI,w);
  619. if cij and cji then { i equal j }
  620. begin
  621. { get minimum index of equal }
  622. if aequals^[j]=-1 then
  623. aequals^[j]:=i;
  624. end
  625. else if cij then
  626. begin
  627. { get minimum index of maximum weight }
  628. if compats^[i].weight<w then
  629. begin
  630. compats^[i].weight:=w;
  631. compats^[i].compintf:=j;
  632. end;
  633. end
  634. else if cji then
  635. begin
  636. { get minimum index of maximum weight }
  637. if (compats^[j].weight<w) then
  638. begin
  639. compats^[j].weight:=w;
  640. compats^[j].compintf:=i;
  641. end;
  642. end;
  643. end;
  644. end;
  645. { Reset, no replacements by default }
  646. for i:=0 to ImplIntfCount-1 do
  647. impls^[i]:=i;
  648. { Replace vtbls when equal or compat, repeat
  649. until there are no replacements possible anymore. This is
  650. needed for the cases like:
  651. First loop: 2->3, 3->1
  652. Second loop: 2->1 (because 3 was replaced with 1)
  653. }
  654. repeat
  655. k:=0;
  656. for i:=0 to ImplIntfCount-1 do
  657. begin
  658. if compats^[impls^[i]].compintf<>-1 then
  659. impls^[i]:=compats^[impls^[i]].compintf
  660. else if aequals^[impls^[i]]<>-1 then
  661. impls^[i]:=aequals^[impls^[i]]
  662. else
  663. inc(k);
  664. end;
  665. until k=ImplIntfCount;
  666. { Update the VtblImplIntf }
  667. for i:=0 to ImplIntfCount-1 do
  668. begin
  669. ImplIntfI:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  670. ImplIntfI.VtblImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[impls^[i]]);
  671. end;
  672. freemem(compats);
  673. freemem(aequals);
  674. freemem(impls);
  675. end;
  676. procedure TVMTBuilder.intf_allocate_vtbls;
  677. var
  678. i : longint;
  679. ImplIntf : TImplementedInterface;
  680. begin
  681. { Allocation vtbl space }
  682. for i:=0 to _class.ImplementedInterfaces.count-1 do
  683. begin
  684. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  685. { if it implements itself and if it's not implemented by delegation }
  686. if (ImplIntf.VtblImplIntf=ImplIntf) and (ImplIntf.IType=etStandard) then
  687. begin
  688. { allocate a pointer in the object memory }
  689. with tObjectSymtable(_class.symtable) do
  690. begin
  691. datasize:=align(datasize,sizeof(pint));
  692. ImplIntf.Ioffset:=datasize;
  693. datasize:=datasize+sizeof(pint);
  694. end;
  695. end;
  696. end;
  697. { Update ioffset of current interface with the ioffset from
  698. the interface that is reused to implements this interface }
  699. for i:=0 to _class.ImplementedInterfaces.count-1 do
  700. begin
  701. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  702. if ImplIntf.VtblImplIntf<>ImplIntf then
  703. ImplIntf.IOffset:=ImplIntf.VtblImplIntf.IOffset;
  704. end;
  705. end;
  706. procedure TVMTBuilder.generate_vmt;
  707. var
  708. i : longint;
  709. def : tdef;
  710. old_current_structdef : tabstractrecorddef;
  711. overridesclasshelper : boolean;
  712. begin
  713. old_current_structdef:=current_structdef;
  714. current_structdef:=_class;
  715. _class.resetvmtentries;
  716. { inherit (copy) VMT from parent object }
  717. if assigned(_class.childof) then
  718. begin
  719. if not assigned(_class.childof.vmtentries) then
  720. internalerror(200810281);
  721. _class.copyvmtentries(_class.childof);
  722. end;
  723. { process all procdefs, we must process the defs to
  724. keep the same order as that is written in the source
  725. to be compatible with the indexes in the interface vtable (PFV) }
  726. for i:=0 to _class.symtable.DefList.Count-1 do
  727. begin
  728. def:=tdef(_class.symtable.DefList[i]);
  729. if def.typ=procdef then
  730. begin
  731. { VMT entry }
  732. if is_new_vmt_entry(tprocdef(def),overridesclasshelper) then
  733. add_new_vmt_entry(tprocdef(def),overridesclasshelper);
  734. end;
  735. end;
  736. build_interface_mappings;
  737. if assigned(_class.ImplementedInterfaces) and
  738. not(is_objc_class_or_protocol(_class)) and
  739. not(is_java_class_or_interface(_class)) then
  740. begin
  741. { Optimize interface tables to reuse wrappers }
  742. intf_optimize_vtbls;
  743. { Allocate interface tables }
  744. intf_allocate_vtbls;
  745. end;
  746. current_structdef:=old_current_structdef;
  747. end;
  748. procedure TVMTBuilder.build_interface_mappings;
  749. var
  750. ImplIntf : TImplementedInterface;
  751. i: longint;
  752. begin
  753. { Find Procdefs implementing the interfaces (both Objective-C protocols
  754. and Java interfaces can have multiple parent interfaces, but in that
  755. case obviously no implementations are required) }
  756. if assigned(_class.ImplementedInterfaces) and
  757. not(_class.objecttype in [odt_objcprotocol,odt_interfacejava]) and
  758. // abstract java classes do not have to implement all interface
  759. // methods. todo: check that non-abstract descendents do!
  760. not((_class.objecttype=odt_javaclass) and (oo_is_abstract in _class.objectoptions)) then
  761. begin
  762. { Collect implementor functions into the tImplementedInterface.procdefs }
  763. case _class.objecttype of
  764. odt_class:
  765. begin
  766. for i:=0 to _class.ImplementedInterfaces.count-1 do
  767. begin
  768. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  769. intf_get_procdefs_recursive(ImplIntf,ImplIntf.IntfDef)
  770. end;
  771. end;
  772. odt_objcclass,
  773. odt_javaclass:
  774. begin
  775. { Object Pascal interfaces are afterwards optimized via the
  776. intf_optimize_vtbls() method, but we can't do this for
  777. protocols/Java interfaces -> check for duplicates here
  778. already. }
  779. handledprotocols:=tfpobjectlist.create(false);
  780. for i:=0 to _class.ImplementedInterfaces.count-1 do
  781. begin
  782. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  783. prot_get_procdefs_recursive(ImplIntf,ImplIntf.IntfDef);
  784. end;
  785. handledprotocols.free;
  786. end
  787. else
  788. internalerror(2009091801);
  789. end
  790. end;
  791. end;
  792. {*****************************************************************************
  793. TVMTWriter
  794. *****************************************************************************}
  795. constructor TVMTWriter.create(c:tobjectdef);
  796. begin
  797. inherited Create;
  798. _Class:=c;
  799. end;
  800. destructor TVMTWriter.destroy;
  801. begin
  802. end;
  803. {**************************************
  804. Message Tables
  805. **************************************}
  806. procedure TVMTWriter.disposeprocdeftree(p : pprocdeftree);
  807. begin
  808. if assigned(p^.l) then
  809. disposeprocdeftree(p^.l);
  810. if assigned(p^.r) then
  811. disposeprocdeftree(p^.r);
  812. dispose(p);
  813. end;
  814. procedure TVMTWriter.insertint(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  815. begin
  816. if at=nil then
  817. begin
  818. at:=p;
  819. inc(count);
  820. end
  821. else
  822. begin
  823. if p^.data.messageinf.i<at^.data.messageinf.i then
  824. insertint(p,at^.l,count)
  825. else if p^.data.messageinf.i>at^.data.messageinf.i then
  826. insertint(p,at^.r,count)
  827. else
  828. Message1(parser_e_duplicate_message_label,tostr(p^.data.messageinf.i));
  829. end;
  830. end;
  831. procedure TVMTWriter.insertstr(p : pprocdeftree;var at : pprocdeftree;var count:longint);
  832. var
  833. i : integer;
  834. begin
  835. if at=nil then
  836. begin
  837. at:=p;
  838. inc(count);
  839. end
  840. else
  841. begin
  842. i:=CompareStr(p^.data.messageinf.str^,at^.data.messageinf.str^);
  843. if i<0 then
  844. insertstr(p,at^.l,count)
  845. else if i>0 then
  846. insertstr(p,at^.r,count)
  847. else
  848. Message1(parser_e_duplicate_message_label,p^.data.messageinf.str^);
  849. end;
  850. end;
  851. procedure TVMTWriter.insertmsgint(p:TObject;arg:pointer);
  852. var
  853. i : longint;
  854. pd : Tprocdef;
  855. pt : pprocdeftree;
  856. begin
  857. if tsym(p).typ<>procsym then
  858. exit;
  859. for i:=0 to Tprocsym(p).ProcdefList.Count-1 do
  860. begin
  861. pd:=tprocdef(Tprocsym(p).ProcdefList[i]);
  862. if po_msgint in pd.procoptions then
  863. begin
  864. new(pt);
  865. pt^.data:=pd;
  866. pt^.l:=nil;
  867. pt^.r:=nil;
  868. insertint(pt,root,plongint(arg)^);
  869. end;
  870. end;
  871. end;
  872. procedure TVMTWriter.insertmsgstr(p:TObject;arg:pointer);
  873. var
  874. i : longint;
  875. pd : Tprocdef;
  876. pt : pprocdeftree;
  877. begin
  878. if tsym(p).typ<>procsym then
  879. exit;
  880. for i:=0 to Tprocsym(p).ProcdefList.Count-1 do
  881. begin
  882. pd:=tprocdef(Tprocsym(p).ProcdefList[i]);
  883. if po_msgstr in pd.procoptions then
  884. begin
  885. new(pt);
  886. pt^.data:=pd;
  887. pt^.l:=nil;
  888. pt^.r:=nil;
  889. insertstr(pt,root,plongint(arg)^);
  890. end;
  891. end;
  892. end;
  893. procedure TVMTWriter.writenames(p : pprocdeftree);
  894. var
  895. ca : pchar;
  896. len : byte;
  897. begin
  898. current_asmdata.getdatalabel(p^.nl);
  899. if assigned(p^.l) then
  900. writenames(p^.l);
  901. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(pint))));
  902. current_asmdata.asmlists[al_globals].concat(Tai_label.Create(p^.nl));
  903. len:=length(p^.data.messageinf.str^);
  904. current_asmdata.asmlists[al_globals].concat(tai_const.create_8bit(len));
  905. getmem(ca,len+1);
  906. move(p^.data.messageinf.str^[1],ca^,len);
  907. ca[len]:=#0;
  908. current_asmdata.asmlists[al_globals].concat(Tai_string.Create_pchar(ca,len));
  909. if assigned(p^.r) then
  910. writenames(p^.r);
  911. end;
  912. procedure TVMTWriter.writestrentry(p : pprocdeftree);
  913. begin
  914. if assigned(p^.l) then
  915. writestrentry(p^.l);
  916. { write name label }
  917. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(pint))));
  918. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(p^.nl));
  919. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(pint))));
  920. current_asmdata.asmlists[al_globals].concat(Tai_const.Createname(p^.data.mangledname,0));
  921. if assigned(p^.r) then
  922. writestrentry(p^.r);
  923. end;
  924. function TVMTWriter.genstrmsgtab : tasmlabel;
  925. var
  926. count : longint;
  927. begin
  928. root:=nil;
  929. count:=0;
  930. { insert all message handlers into a tree, sorted by name }
  931. _class.symtable.SymList.ForEachCall(@insertmsgstr,@count);
  932. { write all names }
  933. if assigned(root) then
  934. writenames(root);
  935. { now start writing of the message string table }
  936. current_asmdata.getdatalabel(result);
  937. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(pint))));
  938. current_asmdata.asmlists[al_globals].concat(Tai_label.Create(result));
  939. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(longint))));
  940. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_32bit(count));
  941. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(pint))));
  942. if assigned(root) then
  943. begin
  944. writestrentry(root);
  945. disposeprocdeftree(root);
  946. end;
  947. end;
  948. procedure TVMTWriter.writeintentry(p : pprocdeftree);
  949. begin
  950. if assigned(p^.l) then
  951. writeintentry(p^.l);
  952. { write name label }
  953. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(longint))));
  954. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_32bit(p^.data.messageinf.i));
  955. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(pint))));
  956. current_asmdata.asmlists[al_globals].concat(Tai_const.Createname(p^.data.mangledname,0));
  957. if assigned(p^.r) then
  958. writeintentry(p^.r);
  959. end;
  960. function TVMTWriter.genintmsgtab : tasmlabel;
  961. var
  962. r : tasmlabel;
  963. count : longint;
  964. begin
  965. root:=nil;
  966. count:=0;
  967. { insert all message handlers into a tree, sorted by name }
  968. _class.symtable.SymList.ForEachCall(@insertmsgint,@count);
  969. { now start writing of the message string table }
  970. current_asmdata.getdatalabel(r);
  971. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(pint))));
  972. current_asmdata.asmlists[al_globals].concat(Tai_label.Create(r));
  973. genintmsgtab:=r;
  974. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(longint))));
  975. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_32bit(count));
  976. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(pint))));
  977. if assigned(root) then
  978. begin
  979. writeintentry(root);
  980. disposeprocdeftree(root);
  981. end;
  982. end;
  983. {$ifdef WITHDMT}
  984. {**************************************
  985. DMT
  986. **************************************}
  987. procedure TVMTWriter.insertdmtentry(p:TObject;arg:pointer);
  988. var
  989. hp : tprocdef;
  990. pt : pprocdeftree;
  991. begin
  992. if tsym(p).typ=procsym then
  993. begin
  994. hp:=tprocsym(p).definition;
  995. while assigned(hp) do
  996. begin
  997. if (po_msgint in hp.procoptions) then
  998. begin
  999. new(pt);
  1000. pt^.p:=hp;
  1001. pt^.l:=nil;
  1002. pt^.r:=nil;
  1003. insertint(pt,root);
  1004. end;
  1005. hp:=hp.nextoverloaded;
  1006. end;
  1007. end;
  1008. end;
  1009. procedure TVMTWriter.writedmtindexentry(p : pprocdeftree);
  1010. begin
  1011. if assigned(p^.l) then
  1012. writedmtindexentry(p^.l);
  1013. al_globals.concat(Tai_const.Create_32bit(p^.data.messageinf.i));
  1014. if assigned(p^.r) then
  1015. writedmtindexentry(p^.r);
  1016. end;
  1017. procedure TVMTWriter.writedmtaddressentry(p : pprocdeftree);
  1018. begin
  1019. if assigned(p^.l) then
  1020. writedmtaddressentry(p^.l);
  1021. al_globals.concat(Tai_const_symbol.Createname(p^.data.mangledname,0));
  1022. if assigned(p^.r) then
  1023. writedmtaddressentry(p^.r);
  1024. end;
  1025. function TVMTWriter.gendmt : tasmlabel;
  1026. var
  1027. r : tasmlabel;
  1028. begin
  1029. root:=nil;
  1030. count:=0;
  1031. gendmt:=nil;
  1032. { insert all message handlers into a tree, sorted by number }
  1033. _class.symtable.SymList.ForEachCall(insertdmtentry);
  1034. if count>0 then
  1035. begin
  1036. current_asmdata.getdatalabel(r);
  1037. gendmt:=r;
  1038. al_globals.concat(cai_align.create(const_align(sizeof(pint))));
  1039. al_globals.concat(Tai_label.Create(r));
  1040. { entries for caching }
  1041. al_globals.concat(Tai_const.Create_ptr(0));
  1042. al_globals.concat(Tai_const.Create_ptr(0));
  1043. al_globals.concat(Tai_const.Create_32bit(count));
  1044. if assigned(root) then
  1045. begin
  1046. writedmtindexentry(root);
  1047. writedmtaddressentry(root);
  1048. disposeprocdeftree(root);
  1049. end;
  1050. end;
  1051. end;
  1052. {$endif WITHDMT}
  1053. {**************************************
  1054. Published Methods
  1055. **************************************}
  1056. procedure TVMTWriter.do_count_published_methods(p:TObject;arg:pointer);
  1057. var
  1058. i : longint;
  1059. pd : tprocdef;
  1060. begin
  1061. if (tsym(p).typ<>procsym) then
  1062. exit;
  1063. for i:=0 to Tprocsym(p).ProcdefList.Count-1 do
  1064. begin
  1065. pd:=tprocdef(Tprocsym(p).ProcdefList[i]);
  1066. if (pd.procsym=tsym(p)) and
  1067. (pd.visibility=vis_published) then
  1068. inc(plongint(arg)^);
  1069. end;
  1070. end;
  1071. procedure TVMTWriter.do_gen_published_methods(p:TObject;arg:pointer);
  1072. var
  1073. i : longint;
  1074. l : tasmlabel;
  1075. pd : tprocdef;
  1076. begin
  1077. if (tsym(p).typ<>procsym) then
  1078. exit;
  1079. for i:=0 to Tprocsym(p).ProcdefList.Count-1 do
  1080. begin
  1081. pd:=tprocdef(Tprocsym(p).ProcdefList[i]);
  1082. if (pd.procsym=tsym(p)) and
  1083. (pd.visibility=vis_published) then
  1084. begin
  1085. current_asmdata.getdatalabel(l);
  1086. new_section(current_asmdata.asmlists[al_typedconsts],sec_rodata_norel,l.name,const_align(sizeof(pint)));
  1087. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(l));
  1088. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_8bit(length(tsym(p).realname)));
  1089. current_asmdata.asmlists[al_typedconsts].concat(Tai_string.Create(tsym(p).realname));
  1090. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(l));
  1091. if po_abstractmethod in pd.procoptions then
  1092. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil))
  1093. else
  1094. current_asmdata.asmlists[al_globals].concat(Tai_const.Createname(pd.mangledname,0));
  1095. end;
  1096. end;
  1097. end;
  1098. function TVMTWriter.genpublishedmethodstable : tasmlabel;
  1099. var
  1100. l : tasmlabel;
  1101. count : longint;
  1102. begin
  1103. count:=0;
  1104. _class.symtable.SymList.ForEachCall(@do_count_published_methods,@count);
  1105. if count>0 then
  1106. begin
  1107. current_asmdata.getdatalabel(l);
  1108. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(pint))));
  1109. current_asmdata.asmlists[al_globals].concat(Tai_label.Create(l));
  1110. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_32bit(count));
  1111. _class.symtable.SymList.ForEachCall(@do_gen_published_methods,nil);
  1112. genpublishedmethodstable:=l;
  1113. end
  1114. else
  1115. genpublishedmethodstable:=nil;
  1116. end;
  1117. function TVMTWriter.generate_field_table : tasmlabel;
  1118. var
  1119. i : longint;
  1120. sym : tsym;
  1121. fieldtable,
  1122. classtable : tasmlabel;
  1123. classindex,
  1124. fieldcount : longint;
  1125. classtablelist : TFPList;
  1126. begin
  1127. classtablelist:=TFPList.Create;
  1128. current_asmdata.getdatalabel(fieldtable);
  1129. current_asmdata.getdatalabel(classtable);
  1130. maybe_new_object_file(current_asmdata.asmlists[al_rtti]);
  1131. new_section(current_asmdata.asmlists[al_rtti],sec_rodata,classtable.name,const_align(sizeof(pint)));
  1132. { retrieve field info fields }
  1133. fieldcount:=0;
  1134. for i:=0 to _class.symtable.SymList.Count-1 do
  1135. begin
  1136. sym:=tsym(_class.symtable.SymList[i]);
  1137. if (sym.typ=fieldvarsym) and
  1138. (sym.visibility=vis_published) then
  1139. begin
  1140. if tfieldvarsym(sym).vardef.typ<>objectdef then
  1141. internalerror(200611032);
  1142. classindex:=classtablelist.IndexOf(tfieldvarsym(sym).vardef);
  1143. if classindex=-1 then
  1144. classtablelist.Add(tfieldvarsym(sym).vardef);
  1145. inc(fieldcount);
  1146. end;
  1147. end;
  1148. { write fields }
  1149. current_asmdata.asmlists[al_rtti].concat(Tai_label.Create(fieldtable));
  1150. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(fieldcount));
  1151. if (tf_requires_proper_alignment in target_info.flags) then
  1152. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  1153. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(classtable));
  1154. for i:=0 to _class.symtable.SymList.Count-1 do
  1155. begin
  1156. sym:=tsym(_class.symtable.SymList[i]);
  1157. if (sym.typ=fieldvarsym) and
  1158. (sym.visibility=vis_published) then
  1159. begin
  1160. if (tf_requires_proper_alignment in target_info.flags) then
  1161. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(pint)));
  1162. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_pint(tfieldvarsym(sym).fieldoffset));
  1163. classindex:=classtablelist.IndexOf(tfieldvarsym(sym).vardef);
  1164. if classindex=-1 then
  1165. internalerror(200611033);
  1166. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(classindex+1));
  1167. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(tfieldvarsym(sym).realname)));
  1168. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(tfieldvarsym(sym).realname));
  1169. end;
  1170. end;
  1171. { generate the class table }
  1172. current_asmdata.asmlists[al_rtti].concat(cai_align.create(const_align(sizeof(pint))));
  1173. current_asmdata.asmlists[al_rtti].concat(Tai_label.Create(classtable));
  1174. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(classtablelist.count));
  1175. if (tf_requires_proper_alignment in target_info.flags) then
  1176. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  1177. for i:=0 to classtablelist.Count-1 do
  1178. current_asmdata.asmlists[al_rtti].concat(Tai_const.Createname(tobjectdef(classtablelist[i]).vmt_mangledname,0));
  1179. classtablelist.free;
  1180. result:=fieldtable;
  1181. end;
  1182. {**************************************
  1183. Interface tables
  1184. **************************************}
  1185. function TVMTWriter.intf_get_vtbl_name(AImplIntf:TImplementedInterface): string;
  1186. begin
  1187. result:=make_mangledname('VTBL',_class.owner,_class.objname^+'_$_'+AImplIntf.IntfDef.objname^);
  1188. end;
  1189. procedure TVMTWriter.intf_create_vtbl(rawdata: TAsmList;AImplIntf:TImplementedInterface);
  1190. var
  1191. pd : tprocdef;
  1192. vtblstr,
  1193. hs : string;
  1194. i : longint;
  1195. begin
  1196. vtblstr:=intf_get_vtbl_name(AImplIntf);
  1197. section_symbol_start(rawdata,vtblstr,AT_DATA,true,sec_data,const_align(sizeof(pint)));
  1198. if assigned(AImplIntf.procdefs) then
  1199. begin
  1200. for i:=0 to AImplIntf.procdefs.count-1 do
  1201. begin
  1202. pd:=tprocdef(AImplIntf.procdefs[i]);
  1203. hs:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+AImplIntf.IntfDef.objname^+'_$_'+
  1204. tostr(i)+'_$_'+pd.mangledname);
  1205. { create reference }
  1206. rawdata.concat(Tai_const.Createname(hs,0));
  1207. end;
  1208. end;
  1209. section_symbol_end(rawdata,vtblstr);
  1210. end;
  1211. procedure TVMTWriter.intf_gen_intf_ref(rawdata: TAsmList;AImplIntf:TImplementedInterface);
  1212. var
  1213. iidlabel,
  1214. guidlabel : tasmlabel;
  1215. i: longint;
  1216. pd: tprocdef;
  1217. begin
  1218. { GUID }
  1219. if AImplIntf.IntfDef.objecttype in [odt_interfacecom] then
  1220. begin
  1221. { label for GUID }
  1222. current_asmdata.getdatalabel(guidlabel);
  1223. rawdata.concat(cai_align.create(const_align(sizeof(pint))));
  1224. rawdata.concat(Tai_label.Create(guidlabel));
  1225. with AImplIntf.IntfDef.iidguid^ do
  1226. begin
  1227. rawdata.concat(Tai_const.Create_32bit(longint(D1)));
  1228. rawdata.concat(Tai_const.Create_16bit(D2));
  1229. rawdata.concat(Tai_const.Create_16bit(D3));
  1230. for i:=Low(D4) to High(D4) do
  1231. rawdata.concat(Tai_const.Create_8bit(D4[i]));
  1232. end;
  1233. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(guidlabel));
  1234. end
  1235. else
  1236. begin
  1237. { nil for Corba interfaces }
  1238. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  1239. end;
  1240. { VTable }
  1241. current_asmdata.asmlists[al_globals].concat(Tai_const.Createname(intf_get_vtbl_name(AImplIntf.VtblImplIntf),0));
  1242. { IOffset field }
  1243. case AImplIntf.VtblImplIntf.IType of
  1244. etFieldValue, etFieldValueClass,
  1245. etStandard:
  1246. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_pint(AImplIntf.VtblImplIntf.IOffset));
  1247. etStaticMethodResult, etStaticMethodClass:
  1248. current_asmdata.asmlists[al_globals].concat(Tai_const.Createname(
  1249. tprocdef(tpropertysym(AImplIntf.ImplementsGetter).propaccesslist[palt_read].procdef).mangledname,
  1250. 0
  1251. ));
  1252. etVirtualMethodResult, etVirtualMethodClass:
  1253. begin
  1254. pd := tprocdef(tpropertysym(AImplIntf.ImplementsGetter).propaccesslist[palt_read].procdef);
  1255. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_pint(tobjectdef(pd.struct).vmtmethodoffset(pd.extnumber)));
  1256. end;
  1257. else
  1258. internalerror(200802162);
  1259. end;
  1260. { IIDStr }
  1261. current_asmdata.getdatalabel(iidlabel);
  1262. rawdata.concat(cai_align.create(const_align(sizeof(pint))));
  1263. rawdata.concat(Tai_label.Create(iidlabel));
  1264. rawdata.concat(Tai_const.Create_8bit(length(AImplIntf.IntfDef.iidstr^)));
  1265. if AImplIntf.IntfDef.objecttype=odt_interfacecom then
  1266. rawdata.concat(Tai_string.Create(upper(AImplIntf.IntfDef.iidstr^)))
  1267. else
  1268. rawdata.concat(Tai_string.Create(AImplIntf.IntfDef.iidstr^));
  1269. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(iidlabel));
  1270. { IType }
  1271. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_pint(aint(AImplIntf.VtblImplIntf.IType)));
  1272. end;
  1273. function TVMTWriter.intf_write_table:TAsmLabel;
  1274. var
  1275. rawdata : TAsmList;
  1276. i : longint;
  1277. ImplIntf : TImplementedInterface;
  1278. intftablelab : tasmlabel;
  1279. begin
  1280. current_asmdata.getdatalabel(intftablelab);
  1281. current_asmdata.asmlists[al_globals].concat(cai_align.create(const_align(sizeof(pint))));
  1282. current_asmdata.asmlists[al_globals].concat(Tai_label.Create(intftablelab));
  1283. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_pint(_class.ImplementedInterfaces.count));
  1284. rawdata:=TAsmList.Create;
  1285. { Write vtbls }
  1286. for i:=0 to _class.ImplementedInterfaces.count-1 do
  1287. begin
  1288. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  1289. if ImplIntf.VtblImplIntf=ImplIntf then
  1290. intf_create_vtbl(rawdata,ImplIntf);
  1291. end;
  1292. { Write vtbl references }
  1293. for i:=0 to _class.ImplementedInterfaces.count-1 do
  1294. begin
  1295. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  1296. intf_gen_intf_ref(rawdata,ImplIntf);
  1297. end;
  1298. { Write interface table }
  1299. current_asmdata.asmlists[al_globals].concatlist(rawdata);
  1300. rawdata.free;
  1301. result:=intftablelab;
  1302. end;
  1303. { Write interface identifiers to the data section }
  1304. procedure TVMTWriter.writeinterfaceids;
  1305. var
  1306. i : longint;
  1307. s : string;
  1308. begin
  1309. if assigned(_class.iidguid) then
  1310. begin
  1311. s:=make_mangledname('IID',_class.owner,_class.objname^);
  1312. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  1313. new_section(current_asmdata.asmlists[al_globals],sec_rodata,s,const_align(sizeof(pint)));
  1314. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  1315. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_32bit(longint(_class.iidguid^.D1)));
  1316. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_16bit(_class.iidguid^.D2));
  1317. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_16bit(_class.iidguid^.D3));
  1318. for i:=Low(_class.iidguid^.D4) to High(_class.iidguid^.D4) do
  1319. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_8bit(_class.iidguid^.D4[i]));
  1320. end;
  1321. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  1322. s:=make_mangledname('IIDSTR',_class.owner,_class.objname^);
  1323. new_section(current_asmdata.asmlists[al_globals],sec_rodata,s,0);
  1324. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  1325. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_8bit(length(_class.iidstr^)));
  1326. current_asmdata.asmlists[al_globals].concat(Tai_string.Create(_class.iidstr^));
  1327. end;
  1328. procedure TVMTWriter.writevirtualmethods(List:TAsmList);
  1329. var
  1330. vmtpd : tprocdef;
  1331. vmtentry : pvmtentry;
  1332. i : longint;
  1333. procname : string;
  1334. {$ifdef vtentry}
  1335. hs : string;
  1336. {$endif vtentry}
  1337. begin
  1338. if not assigned(_class.VMTEntries) then
  1339. exit;
  1340. for i:=0 to _class.VMTEntries.Count-1 do
  1341. begin
  1342. vmtentry:=pvmtentry(_class.vmtentries[i]);
  1343. vmtpd:=vmtentry^.procdef;
  1344. { safety checks }
  1345. if not(po_virtualmethod in vmtpd.procoptions) then
  1346. internalerror(200611082);
  1347. if vmtpd.extnumber<>i then
  1348. internalerror(200611083);
  1349. if (po_abstractmethod in vmtpd.procoptions) then
  1350. procname:='FPC_ABSTRACTERROR'
  1351. else if not wpoinfomanager.optimized_name_for_vmt(_class,vmtpd,procname) then
  1352. procname:=vmtpd.mangledname;
  1353. List.concat(Tai_const.createname(procname,0));
  1354. {$ifdef vtentry}
  1355. hs:='VTENTRY'+'_'+_class.vmt_mangledname+'$$'+tostr(_class.vmtmethodoffset(i) div sizeof(pint));
  1356. current_asmdata.asmlists[al_globals].concat(tai_symbol.CreateName(hs,AT_DATA,0));
  1357. {$endif vtentry}
  1358. end;
  1359. end;
  1360. procedure TVMTWriter.writevmt;
  1361. var
  1362. methodnametable,intmessagetable,
  1363. strmessagetable,classnamelabel,
  1364. fieldtablelabel : tasmlabel;
  1365. hs: string;
  1366. {$ifdef WITHDMT}
  1367. dmtlabel : tasmlabel;
  1368. {$endif WITHDMT}
  1369. interfacetable : tasmlabel;
  1370. begin
  1371. {$ifdef WITHDMT}
  1372. dmtlabel:=gendmt;
  1373. {$endif WITHDMT}
  1374. { write tables for classes, this must be done before the actual
  1375. class is written, because we need the labels defined }
  1376. if is_class(_class) then
  1377. begin
  1378. current_asmdata.getdatalabel(classnamelabel);
  1379. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  1380. new_section(current_asmdata.asmlists[al_globals],sec_rodata,classnamelabel.name,const_align(sizeof(pint)));
  1381. { interface table }
  1382. if _class.ImplementedInterfaces.count>0 then
  1383. interfacetable:=intf_write_table;
  1384. methodnametable:=genpublishedmethodstable;
  1385. fieldtablelabel:=generate_field_table;
  1386. { write class name }
  1387. current_asmdata.asmlists[al_globals].concat(Tai_label.Create(classnamelabel));
  1388. hs:=_class.RttiName;
  1389. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_8bit(length(hs)));
  1390. current_asmdata.asmlists[al_globals].concat(Tai_string.Create(hs));
  1391. { generate message and dynamic tables }
  1392. if (oo_has_msgstr in _class.objectoptions) then
  1393. strmessagetable:=genstrmsgtab;
  1394. if (oo_has_msgint in _class.objectoptions) then
  1395. intmessagetable:=genintmsgtab;
  1396. end;
  1397. { write debug info }
  1398. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  1399. new_section(current_asmdata.asmlists[al_globals],sec_rodata,_class.vmt_mangledname,const_align(sizeof(pint)));
  1400. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global(_class.vmt_mangledname,AT_DATA,0));
  1401. { determine the size with symtable.datasize, because }
  1402. { size gives back 4 for classes }
  1403. current_asmdata.asmlists[al_globals].concat(Tai_const.Create(aitconst_ptr,tObjectSymtable(_class.symtable).datasize));
  1404. current_asmdata.asmlists[al_globals].concat(Tai_const.Create(aitconst_ptr,-int64(tObjectSymtable(_class.symtable).datasize)));
  1405. {$ifdef WITHDMT}
  1406. if _class.classtype=ct_object then
  1407. begin
  1408. if assigned(dmtlabel) then
  1409. current_asmdata.asmlists[al_globals].concat(Tai_const_symbol.Create(dmtlabel)))
  1410. else
  1411. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_ptr(0));
  1412. end;
  1413. {$endif WITHDMT}
  1414. { write pointer to parent VMT, this isn't implemented in TP }
  1415. { but this is not used in FPC ? (PM) }
  1416. { it's not used yet, but the delphi-operators as and is need it (FK) }
  1417. { it is not written for parents that don't have any vmt !! }
  1418. if assigned(_class.childof) and
  1419. (oo_has_vmt in _class.childof.objectoptions) then
  1420. current_asmdata.asmlists[al_globals].concat(Tai_const.Createname(_class.childof.vmt_mangledname,0))
  1421. else
  1422. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  1423. { write extended info for classes, for the order see rtl/inc/objpash.inc }
  1424. if is_class(_class) then
  1425. begin
  1426. { pointer to class name string }
  1427. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(classnamelabel));
  1428. { pointer to dynamic table or nil }
  1429. if (oo_has_msgint in _class.objectoptions) then
  1430. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(intmessagetable))
  1431. else
  1432. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  1433. { pointer to method table or nil }
  1434. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(methodnametable));
  1435. { pointer to field table }
  1436. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(fieldtablelabel));
  1437. { pointer to type info of published section }
  1438. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(RTTIWriter.get_rtti_label(_class,fullrtti)));
  1439. { inittable for con-/destruction }
  1440. if _class.members_need_inittable then
  1441. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(RTTIWriter.get_rtti_label(_class,initrtti)))
  1442. else
  1443. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  1444. { auto table }
  1445. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  1446. { interface table }
  1447. if _class.ImplementedInterfaces.count>0 then
  1448. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(interfacetable))
  1449. else if _class.implements_any_interfaces then
  1450. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil))
  1451. else
  1452. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(current_asmdata.RefAsmSymbol('FPC_EMPTYINTF')));
  1453. { table for string messages }
  1454. if (oo_has_msgstr in _class.objectoptions) then
  1455. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(strmessagetable))
  1456. else
  1457. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  1458. end;
  1459. { write virtual methods }
  1460. writevirtualmethods(current_asmdata.asmlists[al_globals]);
  1461. current_asmdata.asmlists[al_globals].concat(Tai_const.create(aitconst_ptr,0));
  1462. { write the size of the VMT }
  1463. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname(_class.vmt_mangledname));
  1464. {$ifdef vtentry}
  1465. { write vtinherit symbol to notify the linker of the class inheritance tree }
  1466. hs:='VTINHERIT'+'_'+_class.vmt_mangledname+'$$';
  1467. if assigned(_class.childof) then
  1468. hs:=hs+_class.childof.vmt_mangledname
  1469. else
  1470. hs:=hs+_class.vmt_mangledname;
  1471. current_asmdata.asmlists[al_globals].concat(tai_symbol.CreateName(hs,AT_DATA,0));
  1472. {$endif vtentry}
  1473. end;
  1474. end.