symcreat.pas 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273
  1. {
  2. Copyright (c) 2011 by Jonas Maebe
  3. This unit provides helpers for creating new syms/defs based on string
  4. representations.
  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. {$i fpcdefs.inc}
  19. unit symcreat;
  20. interface
  21. uses
  22. finput,tokens,scanner,globtype,cclasses,
  23. aasmdata,
  24. symconst,symbase,symtype,symdef,symsym,
  25. node;
  26. type
  27. tscannerstate = record
  28. new_scanner: tscannerfile;
  29. old_scanner: tscannerfile;
  30. old_filepos: tfileposinfo;
  31. old_token: ttoken;
  32. old_c: char;
  33. old_orgpattern: string;
  34. old_modeswitches: tmodeswitches;
  35. old_idtoken: ttoken;
  36. valid: boolean;
  37. end;
  38. { save/restore the scanner state before/after injecting }
  39. procedure replace_scanner(const tempname: string; out sstate: tscannerstate);
  40. procedure restore_scanner(const sstate: tscannerstate);
  41. { parses a (class or regular) method/constructor/destructor declaration from
  42. str, as if it were declared in astruct's declaration body
  43. WARNING: save the scanner state before calling this routine, and restore
  44. when done. }
  45. function str_parse_method_dec(str: ansistring; potype: tproctypeoption; is_classdef: boolean; astruct: tabstractrecorddef; out pd: tprocdef): boolean;
  46. { parses a (class or regular) method/constructor/destructor implementation
  47. from str, as if it appeared in the current unit's implementation section
  48. WARNINGS:
  49. * save the scanner state before calling this routine, and restore when done.
  50. * the code *must* be written in objfpc style
  51. }
  52. function str_parse_method_impl(const str: ansistring; usefwpd: tprocdef; is_classdef: boolean):boolean;
  53. { parses a typed constant assignment to ssym
  54. WARNINGS:
  55. * save the scanner state before calling this routine, and restore when done.
  56. * the code *must* be written in objfpc style
  57. }
  58. procedure str_parse_typedconst(list: TAsmList; str: ansistring; ssym: tstaticvarsym);
  59. { in the JVM, constructors are not automatically inherited (so you can hide
  60. them). To emulate the Pascal behaviour, we have to automatically add
  61. all parent constructors to the current class as well. We also have to do
  62. the same for the (emulated) virtual class methods }
  63. procedure add_missing_parent_constructors_intf(obj: tobjectdef; addvirtclassmeth: boolean; forcevis: tvisibility);
  64. { goes through all defs in st to add implementations for synthetic methods
  65. added earlier }
  66. procedure add_synthetic_method_implementations(st: tsymtable);
  67. { create an alias for a procdef with Pascal name "newrealname",
  68. mangledname "newmangledname", in symtable newparentst, part of the
  69. record/class/.. "newstruct" (nil if none), and with synthetickind "sk" and
  70. synthetic kind para "skpara" to create the implementation (tsk_none and nil
  71. in case not necessary). Returns the new procdef; finish_copied_procdef() is
  72. not required/must not be called for the result. }
  73. function create_procdef_alias(pd: tprocdef; const newrealname: string; const newmangledname: TSymStr; newparentst: tsymtable; newstruct: tabstractrecorddef; sk: tsynthetickind; skpara: pointer): tprocdef;
  74. { finalize a procdef that has been copied with
  75. tprocdef.getcopyas(procdef,pc_bareproc) }
  76. procedure finish_copied_procdef(var pd: tprocdef; const realname: string; newparentst: tsymtable; newstruct: tabstractrecorddef);
  77. { checks whether sym (a local or para of pd) already has a counterpart in
  78. pd's parentfpstruct, and if not adds a new field to the struct with type
  79. "vardef" (can be different from sym's type in case it's a call-by-reference
  80. parameter, which is indicated by addrparam). If it already has a field in
  81. the parentfpstruct, this field is returned. }
  82. function maybe_add_sym_to_parentfpstruct(pd: tprocdef; sym: tsym; vardef: tdef; addrparam: boolean): tsym;
  83. { given a localvarsym or paravarsym of pd, returns the field of the
  84. parentfpstruct corresponding to this sym }
  85. function find_sym_in_parentfpstruct(pd: tprocdef; sym: tsym): tsym;
  86. { replaces all local and paravarsyms that have been mirrored in the
  87. parentfpstruct with aliasvarsyms that redirect to these fields (used to
  88. make sure that references to these syms in the owning procdef itself also
  89. use the ones in the parentfpstructs) }
  90. procedure redirect_parentfpstruct_local_syms(pd: tprocdef);
  91. { finalises the parentfpstruct (alignment padding, ...) }
  92. procedure finish_parentfpstruct(pd: tprocdef);
  93. { turns a fieldvarsym into a class/static field definition, and returns the
  94. created staticvarsym that is responsible for allocating the global storage }
  95. function make_field_static(recst: tsymtable; fieldvs: tfieldvarsym): tstaticvarsym;
  96. { create a new procdef with the signature of orgpd and (mangled) name
  97. newname, and change the implementation of orgpd so that it calls through
  98. to this new procedure }
  99. procedure call_through_new_name(orgpd: tprocdef; const newname: TSymStr);
  100. function generate_pkg_stub(pd:tprocdef):tnode;
  101. procedure generate_attr_constrs(attrs:tfpobjectlist);
  102. { Generate the hidden thunk class for interfaces,
  103. so we can use them in TVirtualInterface on platforms that do not allow
  104. generating executable code in memory at runtime.}
  105. procedure add_synthetic_interface_classes_for_st(st : tsymtable; gen_intf, gen_impl : boolean);
  106. implementation
  107. uses
  108. cutils,globals,verbose,systems,comphook,fmodule,constexp,
  109. symtable,defutil,symutil,procinfo,
  110. pbase,pdecl, pdecobj,pdecsub,psub,ptconst,pparautl,
  111. {$ifdef jvm}
  112. pjvm,jvmdef,
  113. {$endif jvm}
  114. aasmcpu,symcpu,
  115. nbas,nld,nmem,ncon,
  116. defcmp,
  117. paramgr;
  118. procedure replace_scanner(const tempname: string; out sstate: tscannerstate);
  119. var
  120. old_block_type: tblock_type;
  121. begin
  122. { would require saving of cstringpattern, patternw }
  123. if (token=_CSTRING) or
  124. (token=_CWCHAR) or
  125. (token=_CWSTRING) then
  126. internalerror(2011032201);
  127. sstate.old_scanner:=current_scanner;
  128. sstate.old_filepos:=current_filepos;
  129. sstate.old_token:=token;
  130. sstate.old_c:=c;
  131. sstate.old_orgpattern:=orgpattern;
  132. sstate.old_modeswitches:=current_settings.modeswitches;
  133. sstate.old_idtoken:=idtoken;
  134. sstate.valid:=true;
  135. { creating a new scanner resets the block type, while we want to continue
  136. in the current one }
  137. old_block_type:=block_type;
  138. sstate.new_scanner:=tscannerfile.Create('_Macro_.'+tempname,true);
  139. set_current_scanner(sstate.new_scanner);
  140. block_type:=old_block_type;
  141. { required for e.g. FpcDeepCopy record method (uses "out" parameter; field
  142. names are escaped via &, so should not cause conflicts }
  143. current_settings.modeswitches:=objfpcmodeswitches;
  144. end;
  145. procedure restore_scanner(const sstate: tscannerstate);
  146. begin
  147. if sstate.valid then
  148. begin
  149. sstate.new_scanner.free;
  150. set_current_scanner(sstate.old_scanner);
  151. current_filepos:=sstate.old_filepos;
  152. token:=sstate.old_token;
  153. current_settings.modeswitches:=sstate.old_modeswitches;
  154. c:=sstate.old_c;
  155. orgpattern:=sstate.old_orgpattern;
  156. pattern:=upper(sstate.old_orgpattern);
  157. idtoken:=sstate.old_idtoken;
  158. end;
  159. end;
  160. function str_parse_method_dec(str: ansistring; potype: tproctypeoption; is_classdef: boolean; astruct: tabstractrecorddef; out pd: tprocdef): boolean;
  161. var
  162. oldparse_only: boolean;
  163. begin
  164. Message1(parser_d_internal_parser_string,str);
  165. oldparse_only:=parse_only;
  166. parse_only:=true;
  167. result:=false;
  168. { in case multiple strings are injected, make sure to always close the
  169. previous macro inputfile to prevent memory leaks }
  170. if assigned(current_scanner.inputfile) and
  171. not(current_scanner.inputfile.closed) then
  172. current_scanner.closeinputfile;
  173. { inject the string in the scanner }
  174. str:=str+'end;';
  175. current_scanner.substitutemacro('meth_head_macro',@str[1],length(str),current_scanner.line_no,current_scanner.inputfile.ref_index,true);
  176. current_scanner.readtoken(false);
  177. { and parse it... }
  178. case potype of
  179. potype_class_constructor:
  180. pd:=class_constructor_head(astruct);
  181. potype_class_destructor:
  182. pd:=class_destructor_head(astruct);
  183. potype_constructor:
  184. pd:=constructor_head;
  185. potype_destructor:
  186. pd:=destructor_head;
  187. else if assigned(astruct) and
  188. (astruct.typ=recorddef) then
  189. pd:=parse_record_method_dec(astruct,is_classdef,false)
  190. else
  191. pd:=method_dec(astruct,is_classdef,false);
  192. end;
  193. if assigned(pd) then
  194. result:=true;
  195. parse_only:=oldparse_only;
  196. { remove the temporary macro input file again }
  197. current_scanner.closeinputfile;
  198. current_scanner.nextfile;
  199. current_scanner.tempopeninputfile;
  200. end;
  201. function str_parse_method_impl_with_fileinfo(str: ansistring; usefwpd: tprocdef; fileno, lineno: longint; is_classdef: boolean; out result_procdef: tprocdef):boolean;
  202. var
  203. oldparse_only: boolean;
  204. tmpstr: ansistring;
  205. flags : tread_proc_flags;
  206. begin
  207. if ((status.verbosity and v_debug)<>0) then
  208. begin
  209. if assigned(usefwpd) then
  210. Message1(parser_d_internal_parser_string,usefwpd.customprocname([pno_proctypeoption,pno_paranames,pno_ownername,pno_noclassmarker,pno_noleadingdollar])+str)
  211. else
  212. begin
  213. if is_classdef then
  214. tmpstr:='class '
  215. else
  216. tmpstr:='';
  217. Message1(parser_d_internal_parser_string,tmpstr+str);
  218. end;
  219. end;
  220. oldparse_only:=parse_only;
  221. parse_only:=false;
  222. result:=false;
  223. { "const" starts a new kind of block and hence makes the scanner return }
  224. str:=str+'const;';
  225. { inject the string in the scanner }
  226. current_scanner.substitutemacro('meth_impl_macro',@str[1],length(str),lineno,fileno,true);
  227. current_scanner.readtoken(false);
  228. { and parse it... }
  229. flags:=[];
  230. if is_classdef then
  231. include(flags,rpf_classmethod);
  232. result_procdef:=read_proc(flags,usefwpd);
  233. parse_only:=oldparse_only;
  234. { remove the temporary macro input file again }
  235. current_scanner.closeinputfile;
  236. current_scanner.nextfile;
  237. current_scanner.tempopeninputfile;
  238. result:=true;
  239. end;
  240. function str_parse_method_impl(const str: ansistring; usefwpd: tprocdef; is_classdef: boolean):boolean;
  241. var
  242. tmpproc: tprocdef;
  243. begin
  244. result:=str_parse_method_impl_with_fileinfo(str, usefwpd, current_scanner.inputfile.ref_index, current_scanner.line_no, is_classdef, tmpproc);
  245. end;
  246. function str_parse_method_impl(const str: ansistring; usefwpd: tprocdef; is_classdef: boolean; out result_procdef: tprocdef):boolean;
  247. begin
  248. result:=str_parse_method_impl_with_fileinfo(str, usefwpd, current_scanner.inputfile.ref_index, current_scanner.line_no, is_classdef, result_procdef);
  249. end;
  250. procedure str_parse_typedconst(list: TAsmList; str: ansistring; ssym: tstaticvarsym);
  251. var
  252. old_block_type: tblock_type;
  253. old_parse_only: boolean;
  254. begin
  255. Message1(parser_d_internal_parser_string,str);
  256. { a string that will be interpreted as the start of a new section ->
  257. typed constant parsing will stop }
  258. str:=str+'type ';
  259. old_parse_only:=parse_only;
  260. old_block_type:=block_type;
  261. parse_only:=true;
  262. block_type:=bt_const;
  263. current_scanner.substitutemacro('typed_const_macro',@str[1],length(str),current_scanner.line_no,current_scanner.inputfile.ref_index,true);
  264. current_scanner.readtoken(false);
  265. read_typed_const(list,ssym,ssym.owner.symtabletype in [recordsymtable,objectsymtable]);
  266. parse_only:=old_parse_only;
  267. block_type:=old_block_type;
  268. { remove the temporary macro input file again }
  269. current_scanner.closeinputfile;
  270. current_scanner.nextfile;
  271. current_scanner.tempopeninputfile;
  272. end;
  273. function str_parse_objecttypedef(typename : shortstring;str: ansistring): tobjectdef;
  274. var
  275. b,oldparse_only: boolean;
  276. i : integer;
  277. tmpstr: ansistring;
  278. flags : tread_proc_flags;
  279. o : TObject;
  280. begin
  281. result:=nil;
  282. Message1(parser_d_internal_parser_string,str);
  283. oldparse_only:=parse_only;
  284. parse_only:=true;
  285. { "const" starts a new kind of block and hence makes the scanner return }
  286. str:=str+'const;';
  287. block_type:=bt_type;
  288. { inject the string in the scanner }
  289. current_scanner.substitutemacro('hidden_interface_class_macro',@str[1],length(str),current_scanner.line_no,current_scanner.inputfile.ref_index,true);
  290. current_scanner.readtoken(false);
  291. type_dec(b);
  292. // In the interface part, the object def is not necessarily the last one, the methods also generate defs.
  293. i:=current_module.DefList.count-1;
  294. While (result=nil) and (i>=0) do
  295. begin
  296. O:=current_module.DefList[i];
  297. if (o is tobjectdef) then
  298. if (tobjectdef(o).GetTypeName=typename) then
  299. result:=tobjectdef(o);
  300. dec(i);
  301. end;
  302. if result=nil then
  303. internalerror(2024050401);
  304. parse_only:=oldparse_only;
  305. { remove the temporary macro input file again }
  306. current_scanner.closeinputfile;
  307. current_scanner.nextfile;
  308. current_scanner.tempopeninputfile;
  309. end;
  310. function def_unit_name_prefix_if_toplevel(def: tdef): TSymStr;
  311. begin
  312. result:='';
  313. { if the routine is a global routine in a unit, explicitly use this unit
  314. name to avoid accidentally calling other same-named routines that may be
  315. in scope }
  316. if not assigned(def.owner.defowner) and
  317. assigned(def.owner.realname) and
  318. (def.owner.moduleid<>0) then
  319. result:=internal_macro_escape_unit_namespace_name+def.owner.realname^+'.';
  320. end;
  321. procedure add_missing_parent_constructors_intf(obj: tobjectdef; addvirtclassmeth: boolean; forcevis: tvisibility);
  322. var
  323. parent: tobjectdef;
  324. def: tdef;
  325. parentpd,
  326. childpd: tprocdef;
  327. i: longint;
  328. srsym: tsym;
  329. srsymtable: tsymtable;
  330. begin
  331. if (oo_is_external in obj.objectoptions) or
  332. not assigned(obj.childof) then
  333. exit;
  334. parent:=obj.childof;
  335. { find all constructor in the parent }
  336. for i:=0 to tobjectsymtable(parent.symtable).deflist.count-1 do
  337. begin
  338. def:=tdef(tobjectsymtable(parent.symtable).deflist[i]);
  339. if (def.typ<>procdef) or
  340. ((tprocdef(def).proctypeoption<>potype_constructor) and
  341. (not addvirtclassmeth or
  342. not([po_classmethod,po_virtualmethod]<=tprocdef(def).procoptions))) or
  343. not is_visible_for_object(tprocdef(def),obj) then
  344. continue;
  345. parentpd:=tprocdef(def);
  346. { do we have this constructor too? (don't use
  347. search_struct_member/searchsym_in_class, since those will
  348. search parents too) }
  349. if searchsym_in_record(obj,parentpd.procsym.name,srsym,srsymtable) then
  350. begin
  351. { there's a symbol with the same name, is it a routine of the
  352. same type with the same parameters? }
  353. if srsym.typ=procsym then
  354. begin
  355. childpd:=tprocsym(srsym).find_procdef_bytype_and_para(
  356. tprocdef(def).proctypeoption,parentpd.paras,nil,
  357. [cpo_ignorehidden,cpo_ignoreuniv,cpo_openequalisexact]);
  358. if assigned(childpd) then
  359. continue;
  360. end;
  361. end;
  362. { if we get here, we did not find it in the current objectdef ->
  363. add }
  364. childpd:=tprocdef(parentpd.getcopyas(procdef,pc_normal_no_hidden,'',true));
  365. { get rid of the import name for inherited virtual class methods,
  366. it has to be regenerated rather than amended }
  367. if [po_classmethod,po_virtualmethod]<=childpd.procoptions then
  368. begin
  369. stringdispose(childpd.import_name);
  370. exclude(childpd.procoptions,po_has_importname);
  371. end;
  372. if forcevis<>vis_none then
  373. childpd.visibility:=forcevis;
  374. if po_virtualmethod in childpd.procoptions then
  375. include(childpd.procoptions,po_overridingmethod);
  376. { ignore this artificially added procdef when looking for overloads }
  377. include(childpd.procoptions,po_ignore_for_overload_resolution);
  378. finish_copied_procdef(childpd,parentpd.procsym.realname,obj.symtable,obj);
  379. exclude(childpd.procoptions,po_external);
  380. childpd.synthetickind:=tsk_anon_inherited;
  381. include(obj.objectoptions,oo_has_constructor);
  382. end;
  383. end;
  384. procedure implement_anon_inherited(pd: tprocdef);
  385. var
  386. str: ansistring;
  387. isclassmethod: boolean;
  388. begin
  389. isclassmethod:=
  390. (po_classmethod in pd.procoptions) and
  391. not(pd.proctypeoption in [potype_constructor,potype_destructor]);
  392. str:='begin ';
  393. if (pd.proctypeoption<>potype_constructor) and
  394. not is_void(pd.returndef) then
  395. str:=str+'result:=';
  396. str:=str+'inherited end;';
  397. str_parse_method_impl(str,pd,isclassmethod);
  398. end;
  399. procedure implement_jvm_clone(pd: tprocdef);
  400. var
  401. struct: tabstractrecorddef;
  402. str: ansistring;
  403. i: longint;
  404. sym: tsym;
  405. fsym: tfieldvarsym;
  406. begin
  407. if not(pd.struct.typ in [recorddef,objectdef]) then
  408. internalerror(2011032802);
  409. struct:=pd.struct;
  410. { anonymous record types must get an artificial name, so we can generate
  411. a typecast at the scanner level }
  412. if (struct.typ=recorddef) and
  413. not assigned(struct.typesym) then
  414. internalerror(2011032812);
  415. { We cannot easily use the inherited clone in case we have to create a
  416. deep copy of certain fields. The reason is that e.g. sets are pointers
  417. at the JVM level, but not in Pascal. So the JVM clone routine will copy
  418. the pointer to the set from the old record (= class instance) to the new
  419. one, but we have no way to change this pointer itself from inside Pascal
  420. code.
  421. We solve this by relying on the fact that the JVM is garbage collected:
  422. we simply declare a temporary instance on the stack, which will be
  423. allocated/initialized by the temp generator. We return its address as
  424. the result of the clone routine, so it remains live. }
  425. str:='var __fpc_newcopy:'+ struct.typesym.realname+'; begin clone:=JLObject(@__fpc_newcopy);';
  426. { copy all field contents }
  427. for i:=0 to struct.symtable.symlist.count-1 do
  428. begin
  429. sym:=tsym(struct.symtable.symlist[i]);
  430. if (sym.typ=fieldvarsym) then
  431. begin
  432. fsym:=tfieldvarsym(sym);
  433. str:=str+'__fpc_newcopy.&'+fsym.realname+':=&'+fsym.realname+';';
  434. end;
  435. end;
  436. str:=str+'end;';
  437. str_parse_method_impl(str,pd,false);
  438. end;
  439. procedure implement_record_deepcopy(pd: tprocdef);
  440. var
  441. struct: tabstractrecorddef;
  442. str: ansistring;
  443. i: longint;
  444. sym: tsym;
  445. fsym: tfieldvarsym;
  446. begin
  447. if not(pd.struct.typ in [recorddef,objectdef]) then
  448. internalerror(2011032810);
  449. struct:=pd.struct;
  450. { anonymous record types must get an artificial name, so we can generate
  451. a typecast at the scanner level }
  452. if (struct.typ=recorddef) and
  453. not assigned(struct.typesym) then
  454. internalerror(2011032811);
  455. { copy all fields }
  456. str:='type _fpc_ptrt = ^'+struct.typesym.realname+'; var res: _fpc_ptrt; begin res:=_fpc_ptrt(result);';
  457. for i:=0 to struct.symtable.symlist.count-1 do
  458. begin
  459. sym:=tsym(struct.symtable.symlist[i]);
  460. if (sym.typ=fieldvarsym) then
  461. begin
  462. fsym:=tfieldvarsym(sym);
  463. str:=str+'res^.&'+fsym.realname+':=&'+fsym.realname+';';
  464. end;
  465. end;
  466. str:=str+'end;';
  467. str_parse_method_impl(str,pd,false);
  468. end;
  469. procedure implement_record_initialize(pd: tprocdef);
  470. var
  471. struct: tabstractrecorddef;
  472. str: ansistring;
  473. i: longint;
  474. sym: tsym;
  475. fsym: tfieldvarsym;
  476. begin
  477. if not(pd.struct.typ in [recorddef,objectdef]) then
  478. internalerror(2011071710);
  479. struct:=pd.struct;
  480. { anonymous record types must get an artificial name, so we can generate
  481. a typecast at the scanner level }
  482. if (struct.typ=recorddef) and
  483. not assigned(struct.typesym) then
  484. internalerror(2011032804);
  485. { walk over all fields that need initialization }
  486. str:='begin ';
  487. for i:=0 to struct.symtable.symlist.count-1 do
  488. begin
  489. sym:=tsym(struct.symtable.symlist[i]);
  490. if (sym.typ=fieldvarsym) then
  491. begin
  492. fsym:=tfieldvarsym(sym);
  493. if fsym.vardef.needs_inittable then
  494. str:=str+(internal_macro_escape_unit_namespace_name+'system.initialize(&')+fsym.realname+');';
  495. end;
  496. end;
  497. str:=str+'end;';
  498. str_parse_method_impl(str,pd,false);
  499. end;
  500. procedure implement_empty(pd: tprocdef);
  501. var
  502. str: ansistring;
  503. isclassmethod: boolean;
  504. begin
  505. isclassmethod:=
  506. (po_classmethod in pd.procoptions) and
  507. not(pd.proctypeoption in [potype_constructor,potype_destructor]);
  508. str:='begin end;';
  509. str_parse_method_impl(str,pd,isclassmethod);
  510. end;
  511. procedure addvisibleparameters(var str: ansistring; pd: tprocdef);
  512. var
  513. currpara: tparavarsym;
  514. i: longint;
  515. firstpara: boolean;
  516. begin
  517. firstpara:=true;
  518. for i:=0 to pd.paras.count-1 do
  519. begin
  520. currpara:=tparavarsym(pd.paras[i]);
  521. if not(vo_is_hidden_para in currpara.varoptions) then
  522. begin
  523. if not firstpara then
  524. str:=str+',';
  525. firstpara:=false;
  526. str:=str+'&'+currpara.realname;
  527. end;
  528. end;
  529. end;
  530. procedure implement_callthrough(pd: tprocdef);
  531. var
  532. str: ansistring;
  533. callpd: tprocdef;
  534. isclassmethod: boolean;
  535. begin
  536. isclassmethod:=
  537. (po_classmethod in pd.procoptions) and
  538. not(pd.proctypeoption in [potype_constructor,potype_destructor]);
  539. callpd:=tprocdef(pd.skpara);
  540. str:='begin ';
  541. if pd.returndef<>voidtype then
  542. str:=str+'result:=';
  543. { if the routine is a global routine in a unit/program, explicitly
  544. mnetion this program/unit name to avoid accidentally calling other
  545. same-named routines that may be in scope }
  546. str:=str+def_unit_name_prefix_if_toplevel(callpd)+callpd.procsym.realname+'(';
  547. addvisibleparameters(str,pd);
  548. str:=str+') end;';
  549. str_parse_method_impl(str,pd,isclassmethod);
  550. end;
  551. {$ifdef jvm}
  552. procedure implement_jvm_enum_values(pd: tprocdef);
  553. begin
  554. str_parse_method_impl('begin result:=__fpc_FVALUES end;',pd,true);
  555. end;
  556. procedure implement_jvm_enum_valuof(pd: tprocdef);
  557. begin
  558. str_parse_method_impl('begin result:=__FPC_TEnumClassAlias(inherited valueOf(JLClass(__FPC_TEnumClassAlias),__fpc_str)) end;',pd,true);
  559. end;
  560. procedure implement_jvm_enum_jumps_constr(pd: tprocdef);
  561. begin
  562. str_parse_method_impl('begin inherited create(__fpc_name,__fpc_ord); __fpc_fenumval:=__fpc_initenumval end;',pd,false);
  563. end;
  564. procedure implement_jvm_enum_fpcordinal(pd: tprocdef);
  565. var
  566. enumclass: tobjectdef;
  567. enumdef: tenumdef;
  568. begin
  569. enumclass:=tobjectdef(pd.owner.defowner);
  570. enumdef:=tenumdef(ttypesym(search_struct_member(enumclass,'__FPC_TENUMALIAS')).typedef);
  571. if not enumdef.has_jumps then
  572. str_parse_method_impl('begin result:=ordinal end;',pd,false)
  573. else
  574. str_parse_method_impl('begin result:=__fpc_fenumval end;',pd,false);
  575. end;
  576. procedure implement_jvm_enum_fpcvalueof(pd: tprocdef);
  577. var
  578. enumclass: tobjectdef;
  579. enumdef: tenumdef;
  580. isclassmethod: boolean;
  581. begin
  582. isclassmethod:=
  583. (po_classmethod in pd.procoptions) and
  584. not(pd.proctypeoption in [potype_constructor,potype_destructor]);
  585. enumclass:=tobjectdef(pd.owner.defowner);
  586. enumdef:=tenumdef(ttypesym(search_struct_member(enumclass,'__FPC_TENUMALIAS')).typedef);
  587. { convert integer to corresponding enum instance: in case of no jumps
  588. get it from the $VALUES array, otherwise from the __fpc_ord2enum
  589. hashmap }
  590. if not enumdef.has_jumps then
  591. str_parse_method_impl('begin result:=__fpc_FVALUES[__fpc_int] end;',pd,isclassmethod)
  592. else
  593. str_parse_method_impl('begin result:=__FPC_TEnumClassAlias(__fpc_ord2enum.get(JLInteger.valueOf(__fpc_int))) end;',pd,isclassmethod);
  594. end;
  595. function CompareEnumSyms(Item1, Item2: Pointer): Integer;
  596. var
  597. I1 : tenumsym absolute Item1;
  598. I2 : tenumsym absolute Item2;
  599. begin
  600. Result:=I1.value-I2.value;
  601. end;
  602. procedure implement_jvm_enum_classconstr(pd: tprocdef);
  603. var
  604. enumclass: tobjectdef;
  605. enumdef: tenumdef;
  606. enumname,
  607. str: ansistring;
  608. i: longint;
  609. enumsym: tenumsym;
  610. orderedenums: tfpobjectlist;
  611. begin
  612. enumclass:=tobjectdef(pd.owner.defowner);
  613. enumdef:=tenumdef(ttypesym(search_struct_member(enumclass,'__FPC_TENUMALIAS')).typedef);
  614. if not assigned(enumdef) then
  615. internalerror(2011062305);
  616. str:='begin ';
  617. if enumdef.has_jumps then
  618. { init hashmap for ordinal -> enum instance mapping; don't let it grow,
  619. and set the capacity to the next prime following the total number of
  620. enum elements to minimise the number of collisions }
  621. str:=str+'__fpc_ord2enum:=JUHashMap.Create('+tostr(next_prime(enumdef.symtable.symlist.count))+',1.0);';
  622. { iterate over all enum elements and initialise the class fields, and
  623. store them in the values array. Since the java.lang.Enum doCompare
  624. method is final and hardcoded to compare based on declaration order
  625. (= java.lang.Enum.ordinal() value), we have to create them in order of
  626. ascending FPC ordinal values (which may not be the same as the FPC
  627. declaration order in case of jumps }
  628. orderedenums:=tfpobjectlist.create(false);
  629. for i:=0 to enumdef.symtable.symlist.count-1 do
  630. orderedenums.add(enumdef.symtable.symlist[i]);
  631. if enumdef.has_jumps then
  632. orderedenums.sort(@CompareEnumSyms);
  633. for i:=0 to orderedenums.count-1 do
  634. begin
  635. enumsym:=tenumsym(orderedenums[i]);
  636. enumname:=enumsym.realname;
  637. str:=str+enumsym.name+':=__FPC_TEnumClassAlias.Create('''+enumname+''','+tostr(i);
  638. if enumdef.has_jumps then
  639. str:=str+','+tostr(enumsym.value);
  640. str:=str+');';
  641. { alias for $VALUES array used internally by the JDK, and also by FPC
  642. in case of no jumps }
  643. str:=str+'__fpc_FVALUES['+tostr(i)+']:='+enumname+';';
  644. if enumdef.has_jumps then
  645. str:=str+'__fpc_ord2enum.put(JLInteger.valueOf('+tostr(enumsym.value)+'),'+enumname+');';
  646. end;
  647. orderedenums.free;
  648. str:=str+' end;';
  649. str_parse_method_impl(str,pd,true);
  650. end;
  651. procedure implement_jvm_enum_long2set(pd: tprocdef);
  652. begin
  653. str_parse_method_impl(
  654. 'var '+
  655. 'i, setval: jint;'+
  656. 'begin '+
  657. 'result:=JUEnumSet.noneOf(JLClass(__FPC_TEnumClassAlias));'+
  658. 'if __val<>0 then '+
  659. 'begin '+
  660. '__setsize:=__setsize*8;'+
  661. 'for i:=0 to __setsize-1 do '+
  662. // setsize-i because JVM = big endian
  663. 'if (__val and (jlong(1) shl (__setsize-i)))<>0 then '+
  664. 'result.add(fpcValueOf(i+__setbase));'+
  665. 'end '+
  666. 'end;',
  667. pd,true);
  668. end;
  669. procedure implement_jvm_enum_bitset2set(pd: tprocdef);
  670. begin
  671. str_parse_method_impl(
  672. 'var '+
  673. 'i, setval: jint;'+
  674. 'begin '+
  675. 'result:=JUEnumSet.noneOf(JLClass(__FPC_TEnumClassAlias));'+
  676. 'i:=__val.nextSetBit(0);'+
  677. 'while i>=0 do '+
  678. 'begin '+
  679. 'setval:=-__fromsetbase;'+
  680. 'result.add(fpcValueOf(setval+__tosetbase));'+
  681. 'i:=__val.nextSetBit(i+1);'+
  682. 'end '+
  683. 'end;',
  684. pd,true);
  685. end;
  686. procedure implement_jvm_enum_set2set(pd: tprocdef);
  687. begin
  688. str_parse_method_impl(
  689. 'var '+
  690. 'it: JUIterator;'+
  691. 'ele: FpcEnumValueObtainable;'+
  692. 'i: longint;'+
  693. 'begin '+
  694. 'result:=JUEnumSet.noneOf(JLClass(__FPC_TEnumClassAlias));'+
  695. 'it:=__val.iterator;'+
  696. 'while it.hasNext do '+
  697. 'begin '+
  698. 'ele:=FpcEnumValueObtainable(it.next);'+
  699. 'i:=ele.fpcOrdinal-__fromsetbase;'+
  700. 'result.add(fpcValueOf(i+__tosetbase));'+
  701. 'end '+
  702. 'end;',
  703. pd,true);
  704. end;
  705. procedure implement_jvm_procvar_invoke(pd: tprocdef);
  706. var
  707. pvclass: tobjectdef;
  708. procvar: tprocvardef;
  709. paraname,str,endstr: ansistring;
  710. pvs: tparavarsym;
  711. paradef,boxdef,boxargdef: tdef;
  712. i: longint;
  713. firstpara: boolean;
  714. begin
  715. pvclass:=tobjectdef(pd.owner.defowner);
  716. procvar:=tprocvardef(ttypesym(search_struct_member(pvclass,'__FPC_PROCVARALIAS')).typedef);
  717. { the procvar wrapper class has a tmethod member called "method", whose
  718. "code" field is a JLRMethod, and whose "data" field is the self pointer
  719. if any (if none is required, it's ignored by the JVM, so there's no
  720. problem with always passing it) }
  721. { force extended syntax to allow calling invokeObjectFunc() without using
  722. its result }
  723. str:='';
  724. endstr:='';
  725. { create local pointer to result type for typecasting in case of an
  726. implicit pointer type }
  727. if jvmimplicitpointertype(procvar.returndef) then
  728. str:=str+'type __FPC_returnptrtype = ^'+procvar.returndef.typename+';';
  729. str:=str+'begin ';
  730. { result handling (skip for generic definitions, we'll generate a new
  731. version for the specialized definition) ) }
  732. if not is_void(procvar.returndef) and
  733. (procvar.returndef.typ<>undefineddef) then
  734. begin
  735. str:=str+'invoke:=';
  736. if procvar.returndef.typ in [orddef,floatdef] then
  737. begin
  738. { primitivetype(boxtype(..).unboxmethod) }
  739. jvmgetboxtype(procvar.returndef,boxdef,boxargdef,false);
  740. str:=str+procvar.returndef.typename+'('+boxdef.typename+'(';
  741. endstr:=').'+jvmgetunboxmethod(procvar.returndef)+')';
  742. end
  743. else if jvmimplicitpointertype(procvar.returndef) then
  744. begin
  745. str:=str+'__FPC_returnptrtype(';
  746. { dereference }
  747. endstr:=')^';
  748. end
  749. else
  750. begin
  751. str:=str+procvar.returndef.typename+'(';
  752. endstr:=')';
  753. end;
  754. end;
  755. str:=str+'invokeObjectFunc([';
  756. { parameters are a constant array of jlobject }
  757. firstpara:=true;
  758. for i:=0 to procvar.paras.count-1 do
  759. begin
  760. { skip self/vmt/parentfp, passed separately }
  761. pvs:=tparavarsym(procvar.paras[i]);
  762. if ([vo_is_self,vo_is_vmt,vo_is_parentfp]*pvs.varoptions)<>[] then
  763. continue;
  764. if not firstpara then
  765. str:=str+',';
  766. firstpara:=false;
  767. paraname:=pvs.realname;
  768. paradef:=pvs.vardef;
  769. { Pascalize hidden high parameter }
  770. if vo_is_high_para in pvs.varoptions then
  771. paraname:='high('+tparavarsym(procvar.paras[i-1]).realname+')'
  772. else if vo_is_hidden_para in pvs.varoptions then
  773. begin
  774. if ([vo_is_range_check,vo_is_overflow_check]*pvs.varoptions)<>[] then
  775. { ok, simple boolean parameters }
  776. else
  777. internalerror(2011072403);
  778. end;
  779. { var/out/constref parameters -> pass address through (same for
  780. implicit pointer types) }
  781. if paramanager.push_copyout_param(pvs.varspez,paradef,procvar.proccalloption) or
  782. jvmimplicitpointertype(paradef) then
  783. begin
  784. paraname:='@'+paraname;
  785. paradef:=java_jlobject;
  786. end;
  787. if paradef.typ in [orddef,floatdef] then
  788. begin
  789. { box primitive types; use valueOf() rather than create because it
  790. can give better performance }
  791. jvmgetboxtype(paradef,boxdef,boxargdef,false);
  792. str:=str+boxdef.typename+'.valueOf('+boxargdef.typename+'('+paraname+'))'
  793. end
  794. else
  795. str:=str+'JLObject('+paraname+')';
  796. end;
  797. str:=str+'])'+endstr+' end;';
  798. str_parse_method_impl(str,pd,false)
  799. end;
  800. procedure implement_jvm_procvar_intconstr(pd: tprocdef);
  801. var
  802. pvdef: tprocvardef;
  803. begin
  804. { ideal, and most performant, would be to keep the interface instance
  805. passed to the constructor around and always call its method directly
  806. rather than working via reflection. Unfortunately, the procvar semantics
  807. that allow directly modifying the procvar via typecasting it to a
  808. tmethod make this very hard.
  809. So for now we simply take the address of the interface instance's
  810. method and assign it to the tmethod of this procvar }
  811. pvdef:=tprocvardef(pd.skpara);
  812. str_parse_method_impl('begin method:=System.TMethod(@__intf.'+pvdef.typesym.RealName+'Callback) end;',pd,false);
  813. end;
  814. procedure implement_jvm_virtual_clmethod(pd: tprocdef);
  815. var
  816. str: ansistring;
  817. callpd: tprocdef;
  818. begin
  819. callpd:=tprocdef(pd.skpara);
  820. str:='var pv: __fpc_virtualclassmethod_pv_t'+pd.unique_id_str+'; begin '
  821. + 'pv:=@'+callpd.procsym.RealName+';';
  822. if (pd.proctypeoption<>potype_constructor) and
  823. not is_void(pd.returndef) then
  824. str:=str+'result:=';
  825. str:=str+'pv(';
  826. addvisibleparameters(str,pd);
  827. str:=str+') end;';
  828. str_parse_method_impl(str,pd,true)
  829. end;
  830. {$endif jvm}
  831. {$ifdef wasm}
  832. procedure addvisibleparameterdeclarations(var str: ansistring; pd: tprocdef);
  833. var
  834. currpara: tparavarsym;
  835. i: longint;
  836. firstpara: boolean;
  837. begin
  838. firstpara:=true;
  839. for i:=0 to pd.paras.count-1 do
  840. begin
  841. currpara:=tparavarsym(pd.paras[i]);
  842. if not(vo_is_hidden_para in currpara.varoptions) then
  843. begin
  844. if not firstpara then
  845. str:=str+';';
  846. firstpara:=false;
  847. case currpara.varspez of
  848. vs_constref:
  849. str:=str+'constref ';
  850. vs_out:
  851. str:=str+'out ';
  852. vs_var:
  853. str:=str+'var ';
  854. vs_const:
  855. str:=str+'const ';
  856. vs_value:
  857. ;
  858. else
  859. internalerror(2023061108);
  860. end;
  861. str:=str+currpara.realname;
  862. if currpara.vardef.typ<>formaldef then
  863. str:=str+':'+currpara.vardef.fulltypename;
  864. end;
  865. end;
  866. end;
  867. procedure implement_wasm_suspending(pd: tcpuprocdef; last: Boolean);
  868. var
  869. str: ansistring;
  870. wrapper_name: ansistring;
  871. begin
  872. wrapper_name:=pd.suspending_wrapper_name;
  873. if is_void(pd.returndef) then
  874. str:='procedure '
  875. else
  876. str:='function ';
  877. str:=str+wrapper_name+'(';
  878. if last then
  879. begin
  880. addvisibleparameterdeclarations(str,pd);
  881. if str[Length(str)]<>'(' then
  882. str:=str+';';
  883. str:=str+'__fpc_wasm_susp: WasmExternRef';
  884. end
  885. else
  886. begin
  887. str:=str+'__fpc_wasm_susp: WasmExternRef;';
  888. addvisibleparameterdeclarations(str,pd);
  889. if str[Length(str)]=';' then
  890. delete(str,Length(str),1);
  891. end;
  892. str:=str+')';
  893. if not is_void(pd.returndef) then
  894. str:=str+': '+pd.returndef.fulltypename;
  895. str:=str+'; external '''+pd.import_dll^+ ''' name '''+pd.import_name^+''';';
  896. str_parse_method_impl(str,nil,false);
  897. str:='var __fpc_wasm_suspender_copy:WasmExternRef; begin __fpc_wasm_suspender_copy:=__fpc_wasm_suspender; ';
  898. if not is_void(pd.returndef) then
  899. str:=str+' result:=';
  900. str:=str+wrapper_name+'(__fpc_wasm_suspender_copy,';
  901. addvisibleparameters(str,pd);
  902. if str[Length(str)]=',' then
  903. delete(str,Length(str),1);
  904. str:=str+');';
  905. str:=str+' __fpc_wasm_suspender:=__fpc_wasm_suspender_copy;';
  906. str:=str+' end;';
  907. str_parse_method_impl(str,pd,false);
  908. exclude(pd.procoptions,po_external);
  909. end;
  910. function implement_wasm_promising_wrapper(pd: tcpuprocdef;last:boolean):tprocdef;
  911. var
  912. str: ansistring;
  913. wrapper_name: ansistring;
  914. begin
  915. wrapper_name:=pd.promising_wrapper_name(last);
  916. if is_void(pd.returndef) then
  917. str:='procedure '
  918. else
  919. str:='function ';
  920. str:=str+wrapper_name+'(';
  921. if last then
  922. begin
  923. addvisibleparameterdeclarations(str,pd);
  924. if str[Length(str)]<>'(' then
  925. str:=str+';';
  926. str:=str+'__fpc_wasm_susp: WasmExternRef';
  927. end
  928. else
  929. begin
  930. str:=str+'__fpc_wasm_susp: WasmExternRef;';
  931. addvisibleparameterdeclarations(str,pd);
  932. if str[Length(str)]=';' then
  933. delete(str,Length(str),1);
  934. end;
  935. str:=str+')';
  936. if not is_void(pd.returndef) then
  937. str:=str+': '+pd.returndef.fulltypename;
  938. str:=str+'; begin __fpc_wasm_suspender:=__fpc_wasm_susp;';
  939. if not is_void(pd.returndef) then
  940. str:=str+' result:=';
  941. str:=str+pd.procsym.RealName+'(';
  942. addvisibleparameters(str,pd);
  943. if str[Length(str)]=',' then
  944. delete(str,Length(str),1);
  945. str:=str+'); end;';
  946. str_parse_method_impl(str,nil,false,result);
  947. end;
  948. procedure implement_wasm_promising(pd: tcpuprocdef);
  949. var
  950. new_wrapper_pd: tprocdef;
  951. begin
  952. if pd.promising_first_export_name<>'' then
  953. begin
  954. new_wrapper_pd:=implement_wasm_promising_wrapper(pd,false);
  955. current_asmdata.asmlists[al_exports].Concat(tai_export_name.create(pd.promising_first_export_name,new_wrapper_pd.mangledname,ie_Func));
  956. end;
  957. if pd.promising_last_export_name<>'' then
  958. begin
  959. new_wrapper_pd:=implement_wasm_promising_wrapper(pd,true);
  960. current_asmdata.asmlists[al_exports].Concat(tai_export_name.create(pd.promising_last_export_name,new_wrapper_pd.mangledname,ie_Func));
  961. end;
  962. end;
  963. {$endif wasm}
  964. procedure implement_field_getter(pd: tprocdef);
  965. var
  966. i: longint;
  967. pvs: tparavarsym;
  968. str: ansistring;
  969. callthroughprop: tpropertysym;
  970. propaccesslist: tpropaccesslist;
  971. lastparanr: longint;
  972. firstpara: boolean;
  973. begin
  974. callthroughprop:=tpropertysym(pd.skpara);
  975. str:='begin result:='+callthroughprop.realname;
  976. if ppo_hasparameters in callthroughprop.propoptions then
  977. begin
  978. if not callthroughprop.getpropaccesslist(palt_read,propaccesslist) then
  979. internalerror(2012100701);
  980. str:=str+'[';
  981. firstpara:=true;
  982. lastparanr:=tprocdef(propaccesslist.procdef).paras.count-1;
  983. if ppo_indexed in callthroughprop.propoptions then
  984. dec(lastparanr);
  985. for i:=0 to lastparanr do
  986. begin
  987. { skip self/vmt/parentfp, passed implicitly }
  988. pvs:=tparavarsym(tprocdef(propaccesslist.procdef).paras[i]);
  989. if ([vo_is_self,vo_is_vmt,vo_is_parentfp]*pvs.varoptions)<>[] then
  990. continue;
  991. if not firstpara then
  992. str:=str+',';
  993. firstpara:=false;
  994. str:=str+pvs.realname;
  995. end;
  996. str:=str+']';
  997. end;
  998. str:=str+'; end;';
  999. str_parse_method_impl(str,pd,po_classmethod in pd.procoptions)
  1000. end;
  1001. procedure implement_field_setter(pd: tprocdef);
  1002. var
  1003. i, lastparaindex: longint;
  1004. pvs: tparavarsym;
  1005. paraname, str: ansistring;
  1006. callthroughprop: tpropertysym;
  1007. propaccesslist: tpropaccesslist;
  1008. firstpara: boolean;
  1009. begin
  1010. callthroughprop:=tpropertysym(pd.skpara);
  1011. str:='begin '+callthroughprop.realname;
  1012. if not callthroughprop.getpropaccesslist(palt_write,propaccesslist) then
  1013. internalerror(2012100702);
  1014. if ppo_hasparameters in callthroughprop.propoptions then
  1015. begin
  1016. str:=str+'[';
  1017. firstpara:=true;
  1018. { last parameter is the value to be set, skip (only add index
  1019. parameters here) }
  1020. lastparaindex:=tprocdef(propaccesslist.procdef).paras.count-2;
  1021. if ppo_indexed in callthroughprop.propoptions then
  1022. dec(lastparaindex);
  1023. for i:=0 to lastparaindex do
  1024. begin
  1025. { skip self/vmt/parentfp/index, passed implicitly }
  1026. pvs:=tparavarsym(tprocdef(propaccesslist.procdef).paras[i]);
  1027. if ([vo_is_self,vo_is_vmt,vo_is_parentfp]*pvs.varoptions)<>[] then
  1028. continue;
  1029. if not firstpara then
  1030. str:=str+',';
  1031. firstpara:=false;
  1032. str:=str+pvs.realname;
  1033. end;
  1034. str:=str+']';
  1035. end;
  1036. { the value-to-be-set }
  1037. if assigned(propaccesslist.procdef) then
  1038. begin
  1039. pvs:=tparavarsym(tprocdef(propaccesslist.procdef).paras[tprocdef(propaccesslist.procdef).paras.count-1]);
  1040. paraname:=pvs.realname;
  1041. end
  1042. else
  1043. paraname:='__fpc_newval__';
  1044. str:=str+':='+paraname+'; end;';
  1045. str_parse_method_impl(str,pd,po_classmethod in pd.procoptions)
  1046. end;
  1047. procedure implement_block_invoke_procvar(pd: tprocdef);
  1048. var
  1049. str: ansistring;
  1050. begin
  1051. str:='';
  1052. str:='begin ';
  1053. if pd.returndef<>voidtype then
  1054. str:=str+'result:=';
  1055. str:=str+'__FPC_BLOCK_INVOKE_PV_TYPE(PFPC_Block_literal_complex_procvar(FPC_Block_Self)^.pv)(';
  1056. addvisibleparameters(str,pd);
  1057. str:=str+') end;';
  1058. str_parse_method_impl(str,pd,false);
  1059. end;
  1060. procedure implement_interface_wrapper(pd: tprocdef);
  1061. var
  1062. wrapperinfo: pskpara_interface_wrapper;
  1063. callthroughpd, tmpproc: tprocdef;
  1064. str: ansistring;
  1065. fileinfo: tfileposinfo;
  1066. begin
  1067. wrapperinfo:=pskpara_interface_wrapper(pd.skpara);
  1068. if not assigned(wrapperinfo) then
  1069. internalerror(2015090801);
  1070. callthroughpd:=tprocdef(wrapperinfo^.pd);
  1071. str:='begin ';
  1072. { self right now points to the VMT of interface inside the instance ->
  1073. adjust so it points to the start of the instance }
  1074. str:=str+'pointer(self):=pointer(self) - '+tostr(wrapperinfo^.offset)+';';
  1075. { now call through to the actual method }
  1076. if pd.returndef<>voidtype then
  1077. str:=str+'result:=';
  1078. str:=str+'&'+callthroughpd.procsym.realname+'(';
  1079. addvisibleparameters(str,pd);
  1080. str:=str+') end;';
  1081. { add dummy file info so we can step in/through it }
  1082. if pd.owner.iscurrentunit then
  1083. fileinfo:=pd.fileinfo
  1084. else
  1085. begin
  1086. fileinfo.moduleindex:=current_module.moduleid;
  1087. fileinfo.fileindex:=1;
  1088. fileinfo.line:=1;
  1089. fileinfo.column:=1;
  1090. end;
  1091. str_parse_method_impl_with_fileinfo(str,pd,fileinfo.fileindex,fileinfo.line,false,tmpproc);
  1092. dispose(wrapperinfo);
  1093. pd.skpara:=nil;
  1094. end;
  1095. procedure implement_call_no_parameters(pd: tprocdef);
  1096. var
  1097. callpd: tprocdef;
  1098. str: ansistring;
  1099. warningson,
  1100. isclassmethod: boolean;
  1101. begin
  1102. { avoid warnings about unset function results in these abstract wrappers }
  1103. warningson:=(status.verbosity and V_Warning)<>0;
  1104. setverbosity('W-');
  1105. str:='begin ';
  1106. callpd:=tprocdef(pd.skpara);
  1107. str:=str+def_unit_name_prefix_if_toplevel(callpd)+callpd.procsym.realname+'; end;';
  1108. isclassmethod:=
  1109. (po_classmethod in pd.procoptions) and
  1110. not(pd.proctypeoption in [potype_constructor,potype_destructor]);
  1111. str_parse_method_impl(str,pd,isclassmethod);
  1112. if warningson then
  1113. setverbosity('W+');
  1114. end;
  1115. function get_method_paramtype(vardef : Tdef; asPointer : Boolean; out isAnonymousArrayDef : Boolean) : ansistring; forward;
  1116. function str_parse_method(str: ansistring; allowgenericid : boolean): tprocdef; forward;
  1117. function str_parse_method(str: ansistring): tprocdef;
  1118. begin
  1119. Result:=str_parse_method(str,false);
  1120. end;
  1121. procedure implement_invoke_helper(cn : string;pd: tprocdef; idx : integer);
  1122. var
  1123. sarg,str : ansistring;
  1124. pt, pn,d : shortstring;
  1125. sym : tsym;
  1126. aArg,argcount,i : integer;
  1127. isarray,haveresult : boolean;
  1128. para : tparavarsym;
  1129. hasopenarray, washigh: Boolean;
  1130. begin
  1131. str:='procedure __invoke_helper__';
  1132. pn:=pd.procsym.realname;
  1133. str:=str+cn+'__'+pn+'_'+tostr(idx);
  1134. for I:=1 to length(str) do
  1135. if str[i]='.' then
  1136. str[i]:='_';
  1137. str:=str+'(Instance : Pointer; Args : PPointer);'#10;
  1138. argCount:=0;
  1139. for i:=0 to pd.paras.Count-1 do
  1140. begin
  1141. para:=tparavarsym(pd.paras[i]);
  1142. if vo_is_hidden_para in para.varoptions then
  1143. continue;
  1144. inc(argCount);
  1145. if argCount=1 then
  1146. str:=str+'Type'#10;
  1147. pt:=get_method_paramtype(para.vardef,true,isArray);
  1148. if isArray then
  1149. begin
  1150. str:=str+' tpa'+tostr(argcount)+' = '+pt+';'#10;
  1151. pt:='^tpa'+tostr(argcount);
  1152. end;
  1153. str:=str+' tp'+tostr(argcount)+' = '+pt+';'#10;
  1154. end;
  1155. haveresult:=pd.returndef<>voidtype;
  1156. if haveresult then
  1157. begin
  1158. if argCount=0 then
  1159. str:=str+'Type'#10;
  1160. pt:=get_method_paramtype(pd.returndef ,true,isArray);
  1161. if isArray then
  1162. begin
  1163. str:=str+' tra'+tostr(argcount)+' = '+pt+';'#10;
  1164. pt:='^tra';
  1165. end;
  1166. str:=str+' tr = '+pt+';'#10;
  1167. end;
  1168. str:=str+'begin'#10' ';
  1169. if haveResult then
  1170. str:=str+'TR(args[0])^:=';
  1171. str:=str+cn+'(Instance).'+pn+'(';
  1172. argCount:=0;
  1173. for i:=0 to pd.paras.Count-1 do
  1174. begin
  1175. para:=tparavarsym(pd.paras[i]);
  1176. if vo_is_hidden_para in para.varoptions then
  1177. continue;
  1178. inc(argCount);
  1179. sarg:=tostr(argcount);
  1180. if argCount>1 then
  1181. str:=str+',';
  1182. str:=str+'tp'+sarg+'(Args['+sarg+'])^';
  1183. end;
  1184. str:=str+');'#10;
  1185. str:=str+'end;'#10;
  1186. pd.invoke_helper:=str_parse_method(str,true);
  1187. end;
  1188. procedure add_synthetic_method_implementations_for_st(st: tsymtable);
  1189. var
  1190. i : longint;
  1191. def : tdef;
  1192. pd : tprocdef;
  1193. cn : shortstring;
  1194. begin
  1195. for i:=0 to st.deflist.count-1 do
  1196. begin
  1197. def:=tdef(st.deflist[i]);
  1198. if (def.typ<>procdef) then
  1199. continue;
  1200. { skip methods when processing unit symtable }
  1201. if def.owner<>st then
  1202. continue;
  1203. pd:=tprocdef(def);
  1204. case pd.synthetickind of
  1205. tsk_none:
  1206. ;
  1207. tsk_anon_inherited:
  1208. implement_anon_inherited(pd);
  1209. tsk_jvm_clone:
  1210. implement_jvm_clone(pd);
  1211. tsk_record_deepcopy:
  1212. implement_record_deepcopy(pd);
  1213. tsk_record_initialize:
  1214. implement_record_initialize(pd);
  1215. tsk_empty,
  1216. { special handling for this one is done in tnodeutils.wrap_proc_body }
  1217. tsk_tcinit:
  1218. implement_empty(pd);
  1219. tsk_callthrough:
  1220. implement_callthrough(pd);
  1221. tsk_callthrough_nonabstract:
  1222. begin
  1223. if (pd.owner.defowner.typ<>objectdef) or
  1224. (tobjectdef(pd.owner.defowner).abstractcnt=0) then
  1225. implement_callthrough(pd)
  1226. else
  1227. implement_empty(pd);
  1228. end;
  1229. {$ifdef jvm}
  1230. tsk_jvm_enum_values:
  1231. implement_jvm_enum_values(pd);
  1232. tsk_jvm_enum_valueof:
  1233. implement_jvm_enum_valuof(pd);
  1234. tsk_jvm_enum_classconstr:
  1235. implement_jvm_enum_classconstr(pd);
  1236. tsk_jvm_enum_jumps_constr:
  1237. implement_jvm_enum_jumps_constr(pd);
  1238. tsk_jvm_enum_fpcordinal:
  1239. implement_jvm_enum_fpcordinal(pd);
  1240. tsk_jvm_enum_fpcvalueof:
  1241. implement_jvm_enum_fpcvalueof(pd);
  1242. tsk_jvm_enum_long2set:
  1243. implement_jvm_enum_long2set(pd);
  1244. tsk_jvm_enum_bitset2set:
  1245. implement_jvm_enum_bitset2set(pd);
  1246. tsk_jvm_enum_set2set:
  1247. implement_jvm_enum_set2set(pd);
  1248. tsk_jvm_procvar_invoke:
  1249. implement_jvm_procvar_invoke(pd);
  1250. tsk_jvm_procvar_intconstr:
  1251. implement_jvm_procvar_intconstr(pd);
  1252. tsk_jvm_virtual_clmethod:
  1253. implement_jvm_virtual_clmethod(pd);
  1254. {$else}
  1255. tsk_jvm_enum_values,
  1256. tsk_jvm_enum_valueof,
  1257. tsk_jvm_enum_classconstr,
  1258. tsk_jvm_enum_jumps_constr,
  1259. tsk_jvm_enum_fpcordinal,
  1260. tsk_jvm_enum_fpcvalueof,
  1261. tsk_jvm_enum_long2set,
  1262. tsk_jvm_enum_bitset2set,
  1263. tsk_jvm_enum_set2set,
  1264. tsk_jvm_procvar_invoke,
  1265. tsk_jvm_procvar_intconstr,
  1266. tsk_jvm_virtual_clmethod:
  1267. internalerror(2011032801);
  1268. {$endif jvm}
  1269. {$ifdef wasm}
  1270. tsk_wasm_suspending_first:
  1271. implement_wasm_suspending(tcpuprocdef(pd),false);
  1272. tsk_wasm_suspending_last:
  1273. implement_wasm_suspending(tcpuprocdef(pd),true);
  1274. tsk_wasm_promising:
  1275. implement_wasm_promising(tcpuprocdef(pd));
  1276. {$else wasm}
  1277. tsk_wasm_suspending_first,
  1278. tsk_wasm_suspending_last,
  1279. tsk_wasm_promising:
  1280. internalerror(2023061107);
  1281. {$endif wasm}
  1282. tsk_field_getter:
  1283. implement_field_getter(pd);
  1284. tsk_field_setter:
  1285. implement_field_setter(pd);
  1286. tsk_block_invoke_procvar:
  1287. implement_block_invoke_procvar(pd);
  1288. tsk_interface_wrapper:
  1289. implement_interface_wrapper(pd);
  1290. tsk_call_no_parameters:
  1291. implement_call_no_parameters(pd);
  1292. tsk_invoke_helper:
  1293. begin
  1294. if (pd.owner.defowner) is tobjectdef then
  1295. cn:=tobjectdef(def.owner.defowner).GetTypeName
  1296. else
  1297. internalerror(2023061107);
  1298. implement_invoke_helper(cn,pd,i);
  1299. end;
  1300. end;
  1301. end;
  1302. end;
  1303. function get_method_paramtype(vardef : Tdef; asPointer : Boolean; out isAnonymousArrayDef : Boolean) : ansistring;
  1304. var
  1305. p : integer;
  1306. arrdef : tarraydef absolute vardef;
  1307. begin
  1308. {
  1309. None of the existing routines fulltypename,OwnerHierarchyName,FullOwnerHierarchyName,typename
  1310. results in a workable definition for open array parameters.
  1311. }
  1312. isAnonymousArrayDef:=false;
  1313. if asPointer and (vardef.typ=formaldef) then
  1314. exit('pointer');
  1315. if (vardef is tprocvardef) then
  1316. begin
  1317. result:=vardef.fullownerhierarchyname(false);
  1318. if Assigned(vardef.typesym) then
  1319. Result:=Result+(vardef.typesym.Name);
  1320. end
  1321. else if not (vardef is tarraydef) then
  1322. result:=vardef.fulltypename
  1323. else
  1324. begin
  1325. if (ado_isarrayofconst in arrdef.arrayoptions) then
  1326. begin
  1327. if asPointer then
  1328. Result:='Array of TVarRec'
  1329. else
  1330. result:='Array Of Const';
  1331. asPointer:=False;
  1332. isAnonymousArrayDef:=true;
  1333. end
  1334. else if (ado_OpenArray in arrdef.arrayoptions) then
  1335. begin
  1336. result:='Array of '+arrdef.elementdef.fulltypename;
  1337. asPointer:=False;
  1338. isAnonymousArrayDef:=true;
  1339. end
  1340. else
  1341. begin
  1342. result:=vardef.fulltypename;
  1343. end;
  1344. end;
  1345. // ansistring(0) -> ansistring
  1346. p:=pos('(',result);
  1347. if p=0 then
  1348. p:=pos('[',result);
  1349. if p>0 then
  1350. result:=copy(result,1,p-1);
  1351. if asPointer then
  1352. Result:='^'+Result;
  1353. end;
  1354. function get_method_paramtype(vardef : Tdef; asPointer : Boolean) : ansistring;
  1355. var
  1356. ad : boolean;
  1357. begin
  1358. result:=get_method_paramtype(vardef,aspointer,ad);
  1359. end;
  1360. function create_intf_method_args(p : tprocdef; out argcount: integer) : ansistring;
  1361. const
  1362. varspezprefixes : array[tvarspez] of shortstring =
  1363. ('','const','var','out','constref','final');
  1364. var
  1365. i : integer;
  1366. s : string;
  1367. para : tparavarsym;
  1368. begin
  1369. result:='';
  1370. argCount:=0;
  1371. for i:=0 to p.paras.Count-1 do
  1372. begin
  1373. para:=tparavarsym(p.paras[i]);
  1374. if vo_is_hidden_para in para.varoptions then
  1375. continue;
  1376. if Result<>'' then
  1377. Result:=Result+';';
  1378. inc(argCount);
  1379. result:=result+varspezprefixes[para.varspez]+' p'+tostr(argcount);
  1380. if Assigned(para.vardef) and not (para.vardef is tformaldef) then
  1381. result:=Result+' : '+get_method_paramtype(para.vardef,false);
  1382. end;
  1383. if Result<>'' then
  1384. Result:='('+Result+')';
  1385. end;
  1386. function generate_thunkclass_name(acount: Integer; objdef : tobjectdef) : shortstring;
  1387. var
  1388. cn : shortstring;
  1389. i : integer;
  1390. begin
  1391. cn:=ObjDef.GetTypeName;
  1392. for i:=0 to Length(cn) do
  1393. begin
  1394. if cn[i]='.' then
  1395. cn[i]:='_'
  1396. else if (cn[i]='<') or (cn[i]='>') then
  1397. cn[i]:='_';
  1398. end;
  1399. result:='_t_hidden'+tostr(acount)+cn;
  1400. end;
  1401. function get_thunkclass_interface_vmtoffset(objdef : tobjectdef) : integer;
  1402. var
  1403. i,j,offs : integer;
  1404. sym : tsym;
  1405. proc : tprocsym absolute sym;
  1406. pd : tprocdef;
  1407. begin
  1408. offs:=maxint;
  1409. for I:=0 to objdef.symtable.symList.Count-1 do
  1410. begin
  1411. sym:=tsym(objdef.symtable.symList[i]);
  1412. if Not assigned(sym) then
  1413. continue;
  1414. if (Sym.typ<>procsym) then
  1415. continue;
  1416. for j:=0 to proc.ProcdefList.Count-1 do
  1417. begin
  1418. pd:=tprocdef(proc.ProcdefList[j]);
  1419. if pd.extnumber<offs then
  1420. offs:=pd.extnumber;
  1421. end;
  1422. end;
  1423. if offs=maxint then
  1424. offs:=0;
  1425. result:=offs;
  1426. end;
  1427. // return parent Interface def, but skip iunknown.
  1428. function getparent_interface_def(odef : tobjectdef) : tobjectdef;
  1429. begin
  1430. if (odef.getparentdef is tobjectdef) then
  1431. begin
  1432. result:=odef.getparentdef as tobjectdef;
  1433. if result=interface_iunknown then
  1434. result:=Nil;
  1435. end
  1436. else
  1437. result:=nil;
  1438. end;
  1439. procedure implement_interface_thunkclass_decl(cn : shortstring; objdef : tobjectdef);
  1440. var
  1441. parentname,str : ansistring;
  1442. sym : tsym;
  1443. proc : tprocsym absolute sym;
  1444. pd : tprocdef;
  1445. odef,def : tobjectdef;
  1446. offs,argcount,i,j : integer;
  1447. intfDef : tobjectdef;
  1448. begin
  1449. str:='type '#10;
  1450. odef:=getparent_interface_def(objdef);
  1451. if (oDef=Nil) or (oDef.hiddenclassdef=Nil) then
  1452. parentname:='TInterfaceThunk'
  1453. else
  1454. parentname:=odef.hiddenclassdef.GetTypeName;
  1455. str:=str+cn+' = class('+parentname+','+objdef.GetTypeName+')'#10;
  1456. str:=str+' protected '#10;
  1457. Intfdef:=objdef;
  1458. Repeat
  1459. if not IntfDef.is_generic then
  1460. for I:=0 to intfdef.symtable.symList.Count-1 do
  1461. begin
  1462. sym:=tsym(intfdef.symtable.symList[i]);
  1463. if Not assigned(sym) then
  1464. continue;
  1465. if (Sym.typ<>procsym) then
  1466. continue;
  1467. for j:=0 to proc.ProcdefList.Count-1 do
  1468. begin
  1469. pd:=tprocdef(proc.ProcdefList[j]);
  1470. if pd.returndef<>voidtype then
  1471. str:=str+'function '
  1472. else
  1473. str:=str+'procedure ';
  1474. str:=str+proc.RealName;
  1475. str:=str+create_intf_method_args(pd,argcount);
  1476. if pd.returndef<>voidtype then
  1477. str:=str+' : '+get_method_paramtype(pd.returndef,false);
  1478. str:=str+';'#10;
  1479. end;
  1480. end;
  1481. // Check parent class
  1482. intfdef:=getparent_interface_def(intfdef);
  1483. // If we already have a hidden class def for it, no need to continue
  1484. if (IntfDef<>nil) and (IntfDef.hiddenclassdef<>nil) then
  1485. IntfDef:=Nil;
  1486. until intfdef=nil;
  1487. offs:=get_thunkclass_interface_vmtoffset(objdef);
  1488. if offs>0 then
  1489. begin
  1490. str:=str+'public '#10;
  1491. str:=str+' function InterfaceVMTOffset : word; override;'#10;
  1492. end;
  1493. str:=str+' end;'#10;
  1494. def:=str_parse_objecttypedef(cn,str);
  1495. if assigned(def) then
  1496. begin
  1497. def.created_in_current_module:=true;
  1498. if not def.typesym.is_registered then
  1499. def.typesym.register_sym;
  1500. def.buildderef;
  1501. include(def.objectoptions,oo_can_have_published);
  1502. end;
  1503. objdef.hiddenclassdef:=def;
  1504. end;
  1505. function str_parse_method(str: ansistring; allowgenericid : boolean): tprocdef;
  1506. var
  1507. oldparse_only: boolean;
  1508. tmpstr: ansistring;
  1509. flags : tread_proc_flags;
  1510. oldallow : boolean;
  1511. begin
  1512. Message1(parser_d_internal_parser_string,str);
  1513. oldparse_only:=parse_only;
  1514. parse_only:=false;
  1515. { "const" starts a new kind of block and hence makes the scanner return }
  1516. str:=str+'const;';
  1517. block_type:=bt_none;
  1518. { inject the string in the scanner }
  1519. oldallow:=current_scanner.allowgenericid;
  1520. current_scanner.allowgenericid:=allowgenericid;
  1521. current_scanner.substitutemacro('hidden_interface_method',@str[1],length(str),current_scanner.line_no,current_scanner.inputfile.ref_index,true);
  1522. current_scanner.readtoken(false);
  1523. Result:=read_proc([],Nil);
  1524. parse_only:=oldparse_only;
  1525. { remove the temporary macro input file again }
  1526. current_scanner.closeinputfile;
  1527. current_scanner.nextfile;
  1528. current_scanner.tempopeninputfile;
  1529. current_scanner.allowgenericid:=oldallow;
  1530. end;
  1531. procedure implement_interface_thunkclass_impl_method(cn : shortstring; objdef : tobjectdef; proc : tprocsym; pd : tprocdef);
  1532. var
  1533. rest,str : ansistring;
  1534. pn,d : shortstring;
  1535. sym : tsym;
  1536. aArg,argcount,i : integer;
  1537. haveresult : boolean;
  1538. para : tparavarsym;
  1539. hasopenarray, washigh: Boolean;
  1540. begin
  1541. rest:='';
  1542. str:='';
  1543. if pd.returndef<>voidtype then
  1544. str:=str+'function '
  1545. else
  1546. str:=str+'procedure ';
  1547. pn:=proc.RealName;
  1548. str:=str+cn+'.'+pn;
  1549. str:=str+create_intf_method_args(pd,argcount);
  1550. haveresult:=pd.returndef<>voidtype;
  1551. if haveresult then
  1552. begin
  1553. rest:=get_method_paramtype(pd.returndef,false);
  1554. str:=str+' : '+rest;
  1555. end;
  1556. str:=str+';'#10;
  1557. str:=str+'var '#10;
  1558. str:=str+' data : array[0..'+tostr(argcount)+'] of System.TInterfaceThunk.TArgData;'#10;
  1559. if haveresult then
  1560. str:=str+' res : '+rest+';'#10;
  1561. str:=str+'begin'#10;
  1562. // initialize result.
  1563. if HaveResult then
  1564. begin
  1565. str:=Str+' data[0].addr:=@Res;'#10;
  1566. str:=Str+' data[0].info:=TypeInfo(Res);'#10;
  1567. end
  1568. else
  1569. begin
  1570. str:=Str+' data[0].addr:=nil;'#10;
  1571. str:=Str+' data[0].idx:=-1;'#10;
  1572. end;
  1573. str:=Str+' data[0].idx:=-1;'#10;
  1574. str:=Str+' data[0].ahigh:=-1;'#10;
  1575. // Fill rest of data
  1576. aArg:=0;
  1577. washigh:=false;
  1578. d:='0';
  1579. for i:=0 to pd.paras.Count-1 do
  1580. begin
  1581. para:=tparavarsym(pd.paras[i]);
  1582. // previous was open array. Record high
  1583. if (i>1) then
  1584. begin
  1585. WasHigh:=(vo_is_high_para in para.varoptions);
  1586. if Washigh then
  1587. // D is still value of previous (real) parameter
  1588. str:=str+' data['+d+'].ahigh:=High(p'+d+');'#10
  1589. else
  1590. str:=str+' data['+d+'].ahigh:=-1;'#10;
  1591. end;
  1592. if vo_is_hidden_para in para.varoptions then
  1593. continue;
  1594. inc(aArg);
  1595. d:=tostr(aArg);
  1596. Str:=Str+' data['+d+'].addr:=@p'+d+';'#10;
  1597. Str:=Str+' data['+d+'].idx:='+tostr(i)+';'#10;
  1598. if Assigned(para.vardef) and not (para.vardef is tformaldef) then
  1599. Str:=Str+' data['+d+'].info:=TypeInfo(p'+d+');'#10
  1600. else
  1601. Str:=Str+' data['+d+'].info:=Nil;'#10
  1602. end;
  1603. // if last was not high, set to sentinel.
  1604. if not WasHigh then
  1605. str:=str+' data['+d+'].ahigh:=-1;'#10;
  1606. str:=str+' Thunk('+tostr(pd.extnumber)+','+tostr(argcount)+',@Data);'#10;
  1607. if HaveResult then
  1608. str:=str+' Result:=res;'#10;
  1609. str:=str+'end;'#10;
  1610. pd:=str_parse_method(str,true);
  1611. end;
  1612. procedure implement_thunkclass_interfacevmtoffset(cn : shortstring; objdef : tobjectdef; offs : integer);
  1613. var
  1614. str : ansistring;
  1615. begin
  1616. str:='function '+cn+'.InterfaceVMTOffset : word;'#10;
  1617. str:=str+'begin'#10;
  1618. str:=str+' result:='+toStr(offs)+';'#10;
  1619. str:=str+'end;'#10;
  1620. str_parse_method(str);
  1621. end;
  1622. procedure implement_interface_thunkclass_impl(cn: shortstring; objdef : tobjectdef);
  1623. var
  1624. str : ansistring;
  1625. sym : tsym;
  1626. proc : tprocsym absolute sym;
  1627. pd : tprocdef;
  1628. offs,i,j : integer;
  1629. intfDef : tobjectdef;
  1630. begin
  1631. offs:=get_thunkclass_interface_vmtoffset(objdef);
  1632. if offs>0 then
  1633. implement_thunkclass_interfacevmtoffset(cn,objdef,offs);
  1634. intfDef:=objdef;
  1635. repeat
  1636. for I:=0 to intfdef.symtable.symList.Count-1 do
  1637. begin
  1638. sym:=tsym(intfdef.symtable.symList[i]);
  1639. if Not assigned(sym) then
  1640. continue;
  1641. if (Sym.typ<>procsym) then
  1642. continue;
  1643. for j:=0 to proc.ProcdefList.Count-1 do
  1644. begin
  1645. pd:=tprocdef(proc.ProcdefList[j]);
  1646. implement_interface_thunkclass_impl_method(cn,intfdef,proc,pd);
  1647. end;
  1648. end;
  1649. // Check parent class.
  1650. intfdef:=getparent_interface_def(intfdef);
  1651. // If we already have a hidden class def for it, no need to continue
  1652. if (intfdef<>Nil) and (IntfDef.hiddenclassdef<>nil) then
  1653. IntfDef:=Nil;
  1654. until (intfdef=Nil);
  1655. end;
  1656. procedure add_synthetic_interface_classes_for_st(st : tsymtable; gen_intf, gen_impl : boolean);
  1657. var
  1658. i : longint;
  1659. def : tdef;
  1660. objdef : tobjectdef absolute def;
  1661. recdef : trecorddef absolute def;
  1662. sstate: tscannerstate;
  1663. cn : shortstring;
  1664. isDelphiMode : boolean;
  1665. oldsettings: tmodeswitches;
  1666. begin
  1667. { skip if any errors have occurred, since then this can only cause more
  1668. errors }
  1669. if ErrorCount<>0 then
  1670. exit;
  1671. isDelphiMode:=(m_delphi in current_settings.modeswitches);
  1672. replace_scanner('hiddenclass_impl',sstate);
  1673. sstate.new_scanner.allowgenericid:=true;
  1674. if isDelphiMode then
  1675. begin
  1676. oldsettings:=current_settings.modeswitches;
  1677. current_settings.modeswitches:=current_settings.modeswitches+[m_delphi]-[m_objfpc];
  1678. end;
  1679. for i:=0 to st.deflist.count-1 do
  1680. begin
  1681. def:=tdef(st.deflist[i]);
  1682. if (def.typ<>objectdef) then
  1683. continue;
  1684. if not (objdef.objecttype in objecttypes_with_thunk) then
  1685. continue;
  1686. if not (oo_can_have_published in objdef.objectoptions) then
  1687. continue;
  1688. // need to add here extended rtti check when it is available
  1689. cn:=generate_thunkclass_name(i,objdef);
  1690. if gen_intf then
  1691. implement_interface_thunkclass_decl(cn,objdef);
  1692. if gen_impl then
  1693. implement_interface_thunkclass_impl(cn,objdef);
  1694. end;
  1695. restore_scanner(sstate);
  1696. // Recurse for interfaces defined in a type section of a class/record.
  1697. for i:=0 to st.deflist.count-1 do
  1698. begin
  1699. def:=tdef(st.deflist[i]);
  1700. if (def.typ=objectdef) and (objdef.objecttype=odt_class) then
  1701. add_synthetic_interface_classes_for_st(objdef.symtable,gen_intf,gen_impl)
  1702. else if (def.typ=recorddef) and (m_advanced_records in current_settings.modeswitches) then
  1703. add_synthetic_interface_classes_for_st(recdef.symtable,gen_intf,gen_impl);
  1704. end;
  1705. end;
  1706. procedure add_synthetic_method_implementations(st: tsymtable);
  1707. var
  1708. i: longint;
  1709. def: tdef;
  1710. sstate: tscannerstate;
  1711. begin
  1712. { skip if any errors have occurred, since then this can only cause more
  1713. errors }
  1714. if ErrorCount<>0 then
  1715. exit;
  1716. replace_scanner('synthetic_impl',sstate);
  1717. add_synthetic_method_implementations_for_st(st);
  1718. for i:=0 to st.deflist.count-1 do
  1719. begin
  1720. def:=tdef(st.deflist[i]);
  1721. if (def.typ=procdef) and
  1722. assigned(tprocdef(def).localst) and
  1723. { not true for the "main" procedure, whose localsymtable is the staticsymtable }
  1724. (tprocdef(def).localst.symtabletype=localsymtable) then
  1725. add_synthetic_method_implementations(tprocdef(def).localst)
  1726. else if ((def.typ=objectdef) and
  1727. not(oo_is_external in tobjectdef(def).objectoptions)) or
  1728. (def.typ=recorddef) then
  1729. begin
  1730. { also complete nested types }
  1731. add_synthetic_method_implementations(tabstractrecorddef(def).symtable);
  1732. end;
  1733. end;
  1734. restore_scanner(sstate);
  1735. end;
  1736. function create_procdef_alias(pd: tprocdef; const newrealname: string; const newmangledname: TSymStr; newparentst: tsymtable; newstruct: tabstractrecorddef;
  1737. sk: tsynthetickind; skpara: pointer): tprocdef;
  1738. begin
  1739. { bare copy so we don't copy the aliasnames (specify prefix for
  1740. parameter names so we don't get issues in the body in case
  1741. we e.g. reference system.initialize and one of the parameters
  1742. is called "system") }
  1743. result:=tprocdef(pd.getcopyas(procdef,pc_bareproc,'__FPCW_',true));
  1744. { set the mangled name to the wrapper name }
  1745. result.setmangledname(newmangledname);
  1746. { finish creating the copy }
  1747. finish_copied_procdef(result,newrealname,newparentst,newstruct);
  1748. { insert hidden high parameters }
  1749. result.parast.SymList.ForEachCall(@insert_hidden_para,result);
  1750. { now insert self/vmt }
  1751. insert_self_and_vmt_para(result);
  1752. { and the function result }
  1753. insert_funcret_para(result);
  1754. { recalculate the parameters now that we've added the missing ones }
  1755. result.calcparas;
  1756. { set the info required to generate the implementation }
  1757. result.synthetickind:=sk;
  1758. result.skpara:=skpara;
  1759. end;
  1760. procedure finish_copied_procdef(var pd: tprocdef; const realname: string; newparentst: tsymtable; newstruct: tabstractrecorddef);
  1761. var
  1762. sym: tsym;
  1763. parasym: tparavarsym;
  1764. ps: tprocsym;
  1765. stname: string;
  1766. i: longint;
  1767. begin
  1768. { add generic flag if required }
  1769. if assigned(newstruct) and
  1770. (df_generic in newstruct.defoptions) then
  1771. include(pd.defoptions,df_generic);
  1772. { associate the procdef with a procsym in the owner }
  1773. if not(pd.proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  1774. stname:=upper(realname)
  1775. else
  1776. stname:=lower(realname);
  1777. sym:=tsym(newparentst.find(stname));
  1778. if assigned(sym) then
  1779. begin
  1780. if sym.typ<>procsym then
  1781. internalerror(2011040601);
  1782. ps:=tprocsym(sym);
  1783. end
  1784. else
  1785. begin
  1786. ps:=cprocsym.create(realname);
  1787. newparentst.insertsym(ps);
  1788. end;
  1789. pd.procsym:=ps;
  1790. pd.struct:=newstruct;
  1791. { in case of methods, replace the special parameter types with new ones }
  1792. if assigned(newstruct) then
  1793. begin
  1794. symtablestack.push(pd.parast);
  1795. { may not be assigned in case we converted a procvar into a procdef }
  1796. if assigned(pd.paras) then
  1797. begin
  1798. for i:=0 to pd.paras.count-1 do
  1799. begin
  1800. parasym:=tparavarsym(pd.paras[i]);
  1801. if vo_is_self in parasym.varoptions then
  1802. begin
  1803. if parasym.vardef.typ=classrefdef then
  1804. parasym.vardef:=cclassrefdef.create(newstruct)
  1805. else
  1806. parasym.vardef:=newstruct;
  1807. end
  1808. end;
  1809. end;
  1810. { also fix returndef in case of a constructor }
  1811. if pd.proctypeoption=potype_constructor then
  1812. pd.returndef:=newstruct;
  1813. symtablestack.pop(pd.parast);
  1814. end;
  1815. pd.calcparas;
  1816. proc_add_definition(pd);
  1817. end;
  1818. function maybe_add_sym_to_parentfpstruct(pd: tprocdef; sym: tsym; vardef: tdef; addrparam: boolean): tsym;
  1819. var
  1820. fieldvardef,
  1821. nestedvarsdef: tdef;
  1822. nestedvarsst: tsymtable;
  1823. initcode: tnode;
  1824. old_filepos: tfileposinfo;
  1825. symname,
  1826. symrealname: TSymStr;
  1827. highsym: tabstractvarsym;
  1828. begin
  1829. nestedvarsdef:=tlocalvarsym(pd.parentfpstruct).vardef;
  1830. { redirect all aliases for the function result also to the function
  1831. result }
  1832. if vo_is_funcret in tabstractvarsym(sym).varoptions then
  1833. begin
  1834. symname:='result';
  1835. symrealname:='$result'
  1836. end
  1837. else
  1838. begin
  1839. symname:=sym.name;
  1840. symrealname:=sym.EscapedRealName;
  1841. end;
  1842. result:=search_struct_member(trecorddef(nestedvarsdef),symname);
  1843. if not assigned(result) then
  1844. begin
  1845. { mark that this symbol is mirrored in the parentfpstruct }
  1846. tabstractnormalvarsym(sym).inparentfpstruct:=true;
  1847. { add field to the struct holding all locals accessed
  1848. by nested routines }
  1849. nestedvarsst:=trecorddef(nestedvarsdef).symtable;
  1850. { indicate whether or not this is a var/out/constref/... parameter }
  1851. if addrparam then
  1852. fieldvardef:=cpointerdef.getreusable(vardef)
  1853. else
  1854. fieldvardef:=vardef;
  1855. result:=cfieldvarsym.create(symrealname,vs_value,fieldvardef,[]);
  1856. nestedvarsst.insertsym(result);
  1857. trecordsymtable(nestedvarsst).addfield(tfieldvarsym(result),vis_public);
  1858. { add initialization with original value if it's a parameter }
  1859. if (sym.typ=paravarsym) then
  1860. begin
  1861. old_filepos:=current_filepos;
  1862. fillchar(current_filepos,sizeof(current_filepos),0);
  1863. initcode:=cloadnode.create(sym,sym.owner);
  1864. { indicate that this load should not be transformed into a load
  1865. from the parentfpstruct, but instead should load the original
  1866. value }
  1867. include(initcode.flags,nf_internal);
  1868. { in case it's a var/out/constref parameter, store the address of the
  1869. parameter in the struct }
  1870. if addrparam then
  1871. begin
  1872. initcode:=caddrnode.create_internal(initcode);
  1873. include(taddrnode(initcode).addrnodeflags,anf_typedaddr);
  1874. end;
  1875. initcode:=cassignmentnode.create(
  1876. csubscriptnode.create(result,cloadnode.create(pd.parentfpstruct,pd.parentfpstruct.owner)),
  1877. initcode);
  1878. tblocknode(pd.parentfpinitblock).left:=cstatementnode.create
  1879. (initcode,tblocknode(pd.parentfpinitblock).left);
  1880. current_filepos:=old_filepos;
  1881. { also add the associated high para, if any. It may not be accessed
  1882. during code generation, and we need to catch 'em all (TM) during
  1883. the typecheck/firstpass }
  1884. highsym:=get_high_value_sym(tparavarsym(sym));
  1885. if assigned(highsym) then
  1886. maybe_add_sym_to_parentfpstruct(pd, highsym, highsym.vardef, false);
  1887. end;
  1888. end;
  1889. end;
  1890. procedure redirect_parentfpstruct_local_syms(pd: tprocdef);
  1891. var
  1892. nestedvarsdef: trecorddef;
  1893. sl: tpropaccesslist;
  1894. fsym,
  1895. lsym,
  1896. aliassym: tsym;
  1897. i: longint;
  1898. begin
  1899. nestedvarsdef:=trecorddef(tlocalvarsym(pd.parentfpstruct).vardef);
  1900. for i:=0 to nestedvarsdef.symtable.symlist.count-1 do
  1901. begin
  1902. fsym:=tsym(nestedvarsdef.symtable.symlist[i]);
  1903. if fsym.typ<>fieldvarsym then
  1904. continue;
  1905. lsym:=tsym(pd.localst.find(fsym.name));
  1906. if not assigned(lsym) then
  1907. lsym:=tsym(pd.parast.find(fsym.name));
  1908. if not assigned(lsym) then
  1909. internalerror(2011060408);
  1910. { add an absolute variable that redirects to the field }
  1911. sl:=tpropaccesslist.create;
  1912. sl.addsym(sl_load,pd.parentfpstruct);
  1913. sl.addsym(sl_subscript,tfieldvarsym(fsym));
  1914. aliassym:=cabsolutevarsym.create_ref(lsym.EscapedRealName,tfieldvarsym(fsym).vardef,sl);
  1915. { hide the original variable (can't delete, because there
  1916. may be other loadnodes that reference it)
  1917. -- only for locals; hiding parameters changes the
  1918. function signature }
  1919. if lsym.typ<>paravarsym then
  1920. hidesym(lsym);
  1921. { insert the absolute variable in the localst of the
  1922. routine; ignore duplicates, because this will also check the
  1923. parasymtable and we want to override parameters with our local
  1924. versions }
  1925. pd.localst.insertsym(aliassym,false);
  1926. end;
  1927. end;
  1928. function find_sym_in_parentfpstruct(pd: tprocdef; sym: tsym): tsym;
  1929. var
  1930. nestedvarsdef: tdef;
  1931. begin
  1932. nestedvarsdef:=tlocalvarsym(pd.parentfpstruct).vardef;
  1933. result:=search_struct_member(trecorddef(nestedvarsdef),sym.name);
  1934. end;
  1935. procedure finish_parentfpstruct(pd: tprocdef);
  1936. begin
  1937. trecordsymtable(trecorddef(tlocalvarsym(pd.parentfpstruct).vardef).symtable).addalignmentpadding;
  1938. end;
  1939. function make_field_static(recst: tsymtable; fieldvs: tfieldvarsym): tstaticvarsym;
  1940. var
  1941. static_name: string;
  1942. hstaticvs: tstaticvarsym;
  1943. tmp: tabsolutevarsym;
  1944. sl: tpropaccesslist;
  1945. begin
  1946. include(fieldvs.symoptions,sp_static);
  1947. { generate the symbol which reserves the space }
  1948. static_name:=lower(generate_nested_name(recst,'_'))+'_'+fieldvs.name;
  1949. hstaticvs:=cstaticvarsym.create_from_fieldvar(static_name,fieldvs);
  1950. {$ifdef jvm}
  1951. { for the JVM, static field accesses are name-based and
  1952. hence we have to keep the original name of the field.
  1953. Create a staticvarsym instead of a fieldvarsym so we can
  1954. nevertheless use a loadn instead of a subscriptn though,
  1955. since a subscriptn requires something to subscript and
  1956. there is nothing in this case (class+field name will be
  1957. encoded in the mangled symbol name) }
  1958. recst.insertsym(hstaticvs);
  1959. { only set the staticvarsym's basename (= field name, without any
  1960. mangling), because generating the fully mangled name right now can
  1961. result in a wrong string in case the field's type is a forward
  1962. declared class whose external name will change when the actual
  1963. definition is parsed }
  1964. if (vo_has_mangledname in fieldvs.varoptions) then
  1965. hstaticvs.set_mangledbasename(fieldvs.externalname^)
  1966. else
  1967. hstaticvs.set_mangledbasename(fieldvs.realname);
  1968. { for definition in class file }
  1969. hstaticvs.visibility:=fieldvs.visibility;
  1970. {$else jvm}
  1971. include(hstaticvs.symoptions,sp_internal);
  1972. if df_generic in tdef(recst.defowner).defoptions then
  1973. tabstractrecordsymtable(recst).insertsym(hstaticvs)
  1974. else
  1975. tdef(tabstractrecordsymtable(recst).defowner).get_top_level_symtable(false).insertsym(hstaticvs);
  1976. {$endif jvm}
  1977. { generate the symbol for the access }
  1978. sl:=tpropaccesslist.create;
  1979. sl.addsym(sl_load,hstaticvs);
  1980. { do *not* change the visibility of this absolutevarsym from vis_public
  1981. to anything else, because its visibility is used by visibility checks
  1982. after turning a class property referring to a class variable into a
  1983. load node (handle_staticfield_access -> searchsym_in_class ->
  1984. is_visible_for_object), which means that the load will fail if this
  1985. symbol is e.g. "strict private" while the property is public }
  1986. tmp:=cabsolutevarsym.create_ref('$'+static_name,fieldvs.vardef,sl);
  1987. recst.insertsym(tmp);
  1988. result:=hstaticvs;
  1989. end;
  1990. procedure call_through_new_name(orgpd: tprocdef; const newname: TSymStr);
  1991. var
  1992. newpd: tprocdef;
  1993. begin
  1994. { we have a forward declaration like
  1995. procedure test; (in the unit interface or "forward")
  1996. and then an implementation like
  1997. procedure test; external name 'something';
  1998. To solve this, we create a new external procdef for the
  1999. implementation, and then generate a procedure body for the original
  2000. one that calls through to the external procdef. This is necessary
  2001. because there may already be references to the mangled name for the
  2002. non-external "test".
  2003. }
  2004. { prefixing the parameters here is useless, because the new procdef will
  2005. just be an external declaration without a body }
  2006. newpd:=tprocdef(orgpd.getcopyas(procdef,pc_bareproc,'',true));
  2007. insert_funcret_para(newpd);
  2008. newpd.procoptions:=newpd.procoptions+orgpd.procoptions*[po_external,po_has_importname,po_has_importdll];
  2009. stringdispose(orgpd.import_name);
  2010. stringdispose(orgpd.import_dll);
  2011. orgpd.import_nr:=0;
  2012. newpd.setmangledname(newname);
  2013. finish_copied_procdef(newpd,'__FPC_IMPL_EXTERNAL_REDIRECT_'+newname,current_module.localsymtable,nil);
  2014. newpd.forwarddef:=false;
  2015. { ideally we would prefix the parameters of the original routine here, but since it
  2016. can be an interface definition, we cannot do that without risking to change the
  2017. interface crc }
  2018. orgpd.skpara:=newpd;
  2019. orgpd.synthetickind:=tsk_callthrough;
  2020. orgpd.procoptions:=orgpd.procoptions-[po_external,po_has_importname,po_has_importdll];
  2021. orgpd.forwarddef:=true;
  2022. end;
  2023. function generate_pkg_stub(pd:tprocdef):tnode;
  2024. begin
  2025. if target_info.system in systems_all_windows+systems_nativent then
  2026. begin
  2027. insert_funcret_local(pd);
  2028. result:=cassignmentnode.create(
  2029. cloadnode.create(pd.funcretsym,pd.localst),
  2030. cordconstnode.create(1,bool32type,false)
  2031. );
  2032. end
  2033. else
  2034. result:=cnothingnode.create;
  2035. end;
  2036. procedure generate_attr_constrs(attrs:tfpobjectlist);
  2037. var
  2038. ps : tprocsym;
  2039. pd : tprocdef;
  2040. pi : tcgprocinfo;
  2041. i : sizeint;
  2042. attr : trtti_attribute;
  2043. begin
  2044. if attrs.count=0 then
  2045. exit;
  2046. { if this isn't set then this unit shouldn't have any attributes }
  2047. if not assigned(class_tcustomattribute) then
  2048. internalerror(2019071003);
  2049. for i:=0 to attrs.count-1 do
  2050. begin
  2051. attr:=trtti_attribute(attrs[i]);
  2052. {Generate a procsym for main}
  2053. ps:=cprocsym.create('$rttiattrconstr$'+tostr(i));
  2054. { always register the symbol }
  2055. ps.register_sym;
  2056. { the RTTI always references this symbol }
  2057. inc(ps.refs);
  2058. current_module.localsymtable.insertsym(ps);
  2059. pd:=cprocdef.create(normal_function_level,true);
  2060. { always register the def }
  2061. pd.register_def;
  2062. pd.procsym:=ps;
  2063. ps.ProcdefList.Add(pd);
  2064. { set procdef options }
  2065. pd.proctypeoption:=potype_function;
  2066. pd.proccalloption:=pocall_default;
  2067. include(pd.procoptions,po_hascallingconvention);
  2068. pd.returndef:=class_tcustomattribute;
  2069. insert_funcret_para(pd);
  2070. pd.calcparas;
  2071. pd.forwarddef:=false;
  2072. pd.aliasnames.insert(pd.mangledname);
  2073. handle_calling_convention(pd,hcc_default_actions_impl);
  2074. { set procinfo and current_procinfo.procdef }
  2075. pi:=tcgprocinfo(cprocinfo.create(nil));
  2076. pi.procdef:=pd;
  2077. { we always do a call, namely to the constructor }
  2078. include(pi.flags,pi_do_call);
  2079. insert_funcret_local(pd);
  2080. pi.code:=cassignmentnode.create(
  2081. cloadnode.create(pd.funcretsym,pd.localst),
  2082. attr.constructorcall.getcopy
  2083. );
  2084. pi.generate_code;
  2085. attr.constructorpd:=pd;
  2086. end;
  2087. end;
  2088. end.