nobj.pas 66 KB

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