nobj.pas 67 KB

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