symcreat.pas 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693
  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. old_scanner: tscannerfile;
  29. old_filepos: tfileposinfo;
  30. old_token: ttoken;
  31. old_c: char;
  32. old_orgpattern: string;
  33. old_modeswitches: tmodeswitches;
  34. old_idtoken: ttoken;
  35. valid: boolean;
  36. end;
  37. { save/restore the scanner state before/after injecting }
  38. procedure replace_scanner(const tempname: string; out sstate: tscannerstate);
  39. procedure restore_scanner(const sstate: tscannerstate);
  40. { parses a (class or regular) method/constructor/destructor declaration from
  41. str, as if it were declared in astruct's declaration body
  42. WARNING: save the scanner state before calling this routine, and restore
  43. when done. }
  44. function str_parse_method_dec(str: ansistring; potype: tproctypeoption; is_classdef: boolean; astruct: tabstractrecorddef; out pd: tprocdef): boolean;
  45. { parses a (class or regular) method/constructor/destructor implementation
  46. from str, as if it appeared in the current unit's implementation section
  47. WARNINGS:
  48. * save the scanner state before calling this routine, and restore when done.
  49. * the code *must* be written in objfpc style
  50. }
  51. function str_parse_method_impl(const str: ansistring; usefwpd: tprocdef; is_classdef: boolean):boolean;
  52. { parses a typed constant assignment to ssym
  53. WARNINGS:
  54. * save the scanner state before calling this routine, and restore when done.
  55. * the code *must* be written in objfpc style
  56. }
  57. procedure str_parse_typedconst(list: TAsmList; str: ansistring; ssym: tstaticvarsym);
  58. { in the JVM, constructors are not automatically inherited (so you can hide
  59. them). To emulate the Pascal behaviour, we have to automatically add
  60. all parent constructors to the current class as well. We also have to do
  61. the same for the (emulated) virtual class methods }
  62. procedure add_missing_parent_constructors_intf(obj: tobjectdef; addvirtclassmeth: boolean; forcevis: tvisibility);
  63. { goes through all defs in st to add implementations for synthetic methods
  64. added earlier }
  65. procedure add_synthetic_method_implementations(st: tsymtable);
  66. { create an alias for a procdef with Pascal name "newrealname",
  67. mangledname "newmangledname", in symtable newparentst, part of the
  68. record/class/.. "newstruct" (nil if none), and with synthetickind "sk" and
  69. synthetic kind para "skpara" to create the implementation (tsk_none and nil
  70. in case not necessary). Returns the new procdef; finish_copied_procdef() is
  71. not required/must not be called for the result. }
  72. function create_procdef_alias(pd: tprocdef; const newrealname: string; const newmangledname: TSymStr; newparentst: tsymtable; newstruct: tabstractrecorddef; sk: tsynthetickind; skpara: pointer): tprocdef;
  73. { finalize a procdef that has been copied with
  74. tprocdef.getcopyas(procdef,pc_bareproc) }
  75. procedure finish_copied_procdef(var pd: tprocdef; const realname: string; newparentst: tsymtable; newstruct: tabstractrecorddef);
  76. { checks whether sym (a local or para of pd) already has a counterpart in
  77. pd's parentfpstruct, and if not adds a new field to the struct with type
  78. "vardef" (can be different from sym's type in case it's a call-by-reference
  79. parameter, which is indicated by addrparam). If it already has a field in
  80. the parentfpstruct, this field is returned. }
  81. function maybe_add_sym_to_parentfpstruct(pd: tprocdef; sym: tsym; vardef: tdef; addrparam: boolean): tsym;
  82. { given a localvarsym or paravarsym of pd, returns the field of the
  83. parentfpstruct corresponding to this sym }
  84. function find_sym_in_parentfpstruct(pd: tprocdef; sym: tsym): tsym;
  85. { replaces all local and paravarsyms that have been mirrored in the
  86. parentfpstruct with aliasvarsyms that redirect to these fields (used to
  87. make sure that references to these syms in the owning procdef itself also
  88. use the ones in the parentfpstructs) }
  89. procedure redirect_parentfpstruct_local_syms(pd: tprocdef);
  90. { finalises the parentfpstruct (alignment padding, ...) }
  91. procedure finish_parentfpstruct(pd: tprocdef);
  92. { turns a fieldvarsym into a class/static field definition, and returns the
  93. created staticvarsym that is responsible for allocating the global storage }
  94. function make_field_static(recst: tsymtable; fieldvs: tfieldvarsym): tstaticvarsym;
  95. { create a new procdef with the signature of orgpd and (mangled) name
  96. newname, and change the implementation of orgpd so that it calls through
  97. to this new procedure }
  98. procedure call_through_new_name(orgpd: tprocdef; const newname: TSymStr);
  99. function generate_pkg_stub(pd:tprocdef):tnode;
  100. procedure generate_attr_constrs(attrs:tfpobjectlist);
  101. implementation
  102. uses
  103. cutils,globals,verbose,systems,comphook,fmodule,constexp,
  104. symtable,defutil,symutil,procinfo,
  105. pbase,pdecobj,pdecsub,psub,ptconst,pparautl,
  106. {$ifdef jvm}
  107. pjvm,jvmdef,
  108. {$endif jvm}
  109. aasmcpu,symcpu,
  110. nbas,nld,nmem,ncon,
  111. defcmp,
  112. paramgr;
  113. procedure replace_scanner(const tempname: string; out sstate: tscannerstate);
  114. var
  115. old_block_type: tblock_type;
  116. begin
  117. { would require saving of cstringpattern, patternw }
  118. if (token=_CSTRING) or
  119. (token=_CWCHAR) or
  120. (token=_CWSTRING) then
  121. internalerror(2011032201);
  122. sstate.old_scanner:=current_scanner;
  123. sstate.old_filepos:=current_filepos;
  124. sstate.old_token:=token;
  125. sstate.old_c:=c;
  126. sstate.old_orgpattern:=orgpattern;
  127. sstate.old_modeswitches:=current_settings.modeswitches;
  128. sstate.old_idtoken:=idtoken;
  129. sstate.valid:=true;
  130. { creating a new scanner resets the block type, while we want to continue
  131. in the current one }
  132. old_block_type:=block_type;
  133. current_scanner:=tscannerfile.Create('_Macro_.'+tempname,true);
  134. block_type:=old_block_type;
  135. { required for e.g. FpcDeepCopy record method (uses "out" parameter; field
  136. names are escaped via &, so should not cause conflicts }
  137. current_settings.modeswitches:=objfpcmodeswitches;
  138. end;
  139. procedure restore_scanner(const sstate: tscannerstate);
  140. begin
  141. if sstate.valid then
  142. begin
  143. current_scanner.free;
  144. current_scanner:=sstate.old_scanner;
  145. current_filepos:=sstate.old_filepos;
  146. token:=sstate.old_token;
  147. current_settings.modeswitches:=sstate.old_modeswitches;
  148. c:=sstate.old_c;
  149. orgpattern:=sstate.old_orgpattern;
  150. pattern:=upper(sstate.old_orgpattern);
  151. idtoken:=sstate.old_idtoken;
  152. end;
  153. end;
  154. function str_parse_method_dec(str: ansistring; potype: tproctypeoption; is_classdef: boolean; astruct: tabstractrecorddef; out pd: tprocdef): boolean;
  155. var
  156. oldparse_only: boolean;
  157. begin
  158. Message1(parser_d_internal_parser_string,str);
  159. oldparse_only:=parse_only;
  160. parse_only:=true;
  161. result:=false;
  162. { in case multiple strings are injected, make sure to always close the
  163. previous macro inputfile to prevent memory leaks }
  164. if assigned(current_scanner.inputfile) and
  165. not(current_scanner.inputfile.closed) then
  166. current_scanner.closeinputfile;
  167. { inject the string in the scanner }
  168. str:=str+'end;';
  169. current_scanner.substitutemacro('meth_head_macro',@str[1],length(str),current_scanner.line_no,current_scanner.inputfile.ref_index,true);
  170. current_scanner.readtoken(false);
  171. { and parse it... }
  172. case potype of
  173. potype_class_constructor:
  174. pd:=class_constructor_head(astruct);
  175. potype_class_destructor:
  176. pd:=class_destructor_head(astruct);
  177. potype_constructor:
  178. pd:=constructor_head;
  179. potype_destructor:
  180. pd:=destructor_head;
  181. else if assigned(astruct) and
  182. (astruct.typ=recorddef) then
  183. pd:=parse_record_method_dec(astruct,is_classdef,false)
  184. else
  185. pd:=method_dec(astruct,is_classdef,false);
  186. end;
  187. if assigned(pd) then
  188. result:=true;
  189. parse_only:=oldparse_only;
  190. { remove the temporary macro input file again }
  191. current_scanner.closeinputfile;
  192. current_scanner.nextfile;
  193. current_scanner.tempopeninputfile;
  194. end;
  195. function str_parse_method_impl_with_fileinfo(str: ansistring; usefwpd: tprocdef; fileno, lineno: longint; is_classdef: boolean; out result_procdef: tprocdef):boolean;
  196. var
  197. oldparse_only: boolean;
  198. tmpstr: ansistring;
  199. flags : tread_proc_flags;
  200. begin
  201. if ((status.verbosity and v_debug)<>0) then
  202. begin
  203. if assigned(usefwpd) then
  204. Message1(parser_d_internal_parser_string,usefwpd.customprocname([pno_proctypeoption,pno_paranames,pno_ownername,pno_noclassmarker,pno_noleadingdollar])+str)
  205. else
  206. begin
  207. if is_classdef then
  208. tmpstr:='class '
  209. else
  210. tmpstr:='';
  211. Message1(parser_d_internal_parser_string,tmpstr+str);
  212. end;
  213. end;
  214. oldparse_only:=parse_only;
  215. parse_only:=false;
  216. result:=false;
  217. { "const" starts a new kind of block and hence makes the scanner return }
  218. str:=str+'const;';
  219. { inject the string in the scanner }
  220. current_scanner.substitutemacro('meth_impl_macro',@str[1],length(str),lineno,fileno,true);
  221. current_scanner.readtoken(false);
  222. { and parse it... }
  223. flags:=[];
  224. if is_classdef then
  225. include(flags,rpf_classmethod);
  226. result_procdef:=read_proc(flags,usefwpd);
  227. parse_only:=oldparse_only;
  228. { remove the temporary macro input file again }
  229. current_scanner.closeinputfile;
  230. current_scanner.nextfile;
  231. current_scanner.tempopeninputfile;
  232. result:=true;
  233. end;
  234. function str_parse_method_impl(const str: ansistring; usefwpd: tprocdef; is_classdef: boolean):boolean;
  235. var
  236. tmpproc: tprocdef;
  237. begin
  238. result:=str_parse_method_impl_with_fileinfo(str, usefwpd, current_scanner.inputfile.ref_index, current_scanner.line_no, is_classdef, tmpproc);
  239. end;
  240. function str_parse_method_impl(const str: ansistring; usefwpd: tprocdef; is_classdef: boolean; out result_procdef: tprocdef):boolean;
  241. begin
  242. result:=str_parse_method_impl_with_fileinfo(str, usefwpd, current_scanner.inputfile.ref_index, current_scanner.line_no, is_classdef, result_procdef);
  243. end;
  244. procedure str_parse_typedconst(list: TAsmList; str: ansistring; ssym: tstaticvarsym);
  245. var
  246. old_block_type: tblock_type;
  247. old_parse_only: boolean;
  248. begin
  249. Message1(parser_d_internal_parser_string,str);
  250. { a string that will be interpreted as the start of a new section ->
  251. typed constant parsing will stop }
  252. str:=str+'type ';
  253. old_parse_only:=parse_only;
  254. old_block_type:=block_type;
  255. parse_only:=true;
  256. block_type:=bt_const;
  257. current_scanner.substitutemacro('typed_const_macro',@str[1],length(str),current_scanner.line_no,current_scanner.inputfile.ref_index,true);
  258. current_scanner.readtoken(false);
  259. read_typed_const(list,ssym,ssym.owner.symtabletype in [recordsymtable,objectsymtable]);
  260. parse_only:=old_parse_only;
  261. block_type:=old_block_type;
  262. { remove the temporary macro input file again }
  263. current_scanner.closeinputfile;
  264. current_scanner.nextfile;
  265. current_scanner.tempopeninputfile;
  266. end;
  267. function def_unit_name_prefix_if_toplevel(def: tdef): TSymStr;
  268. begin
  269. result:='';
  270. { if the routine is a global routine in a unit, explicitly use this unit
  271. name to avoid accidentally calling other same-named routines that may be
  272. in scope }
  273. if not assigned(def.owner.defowner) and
  274. assigned(def.owner.realname) and
  275. (def.owner.moduleid<>0) then
  276. result:=internal_macro_escape_unit_namespace_name+def.owner.realname^+'.';
  277. end;
  278. procedure add_missing_parent_constructors_intf(obj: tobjectdef; addvirtclassmeth: boolean; forcevis: tvisibility);
  279. var
  280. parent: tobjectdef;
  281. def: tdef;
  282. parentpd,
  283. childpd: tprocdef;
  284. i: longint;
  285. srsym: tsym;
  286. srsymtable: tsymtable;
  287. begin
  288. if (oo_is_external in obj.objectoptions) or
  289. not assigned(obj.childof) then
  290. exit;
  291. parent:=obj.childof;
  292. { find all constructor in the parent }
  293. for i:=0 to tobjectsymtable(parent.symtable).deflist.count-1 do
  294. begin
  295. def:=tdef(tobjectsymtable(parent.symtable).deflist[i]);
  296. if (def.typ<>procdef) or
  297. ((tprocdef(def).proctypeoption<>potype_constructor) and
  298. (not addvirtclassmeth or
  299. not([po_classmethod,po_virtualmethod]<=tprocdef(def).procoptions))) or
  300. not is_visible_for_object(tprocdef(def),obj) then
  301. continue;
  302. parentpd:=tprocdef(def);
  303. { do we have this constructor too? (don't use
  304. search_struct_member/searchsym_in_class, since those will
  305. search parents too) }
  306. if searchsym_in_record(obj,parentpd.procsym.name,srsym,srsymtable) then
  307. begin
  308. { there's a symbol with the same name, is it a routine of the
  309. same type with the same parameters? }
  310. if srsym.typ=procsym then
  311. begin
  312. childpd:=tprocsym(srsym).find_procdef_bytype_and_para(
  313. tprocdef(def).proctypeoption,parentpd.paras,nil,
  314. [cpo_ignorehidden,cpo_ignoreuniv,cpo_openequalisexact]);
  315. if assigned(childpd) then
  316. continue;
  317. end;
  318. end;
  319. { if we get here, we did not find it in the current objectdef ->
  320. add }
  321. childpd:=tprocdef(parentpd.getcopyas(procdef,pc_normal_no_hidden,'',true));
  322. { get rid of the import name for inherited virtual class methods,
  323. it has to be regenerated rather than amended }
  324. if [po_classmethod,po_virtualmethod]<=childpd.procoptions then
  325. begin
  326. stringdispose(childpd.import_name);
  327. exclude(childpd.procoptions,po_has_importname);
  328. end;
  329. if forcevis<>vis_none then
  330. childpd.visibility:=forcevis;
  331. if po_virtualmethod in childpd.procoptions then
  332. include(childpd.procoptions,po_overridingmethod);
  333. { ignore this artificially added procdef when looking for overloads }
  334. include(childpd.procoptions,po_ignore_for_overload_resolution);
  335. finish_copied_procdef(childpd,parentpd.procsym.realname,obj.symtable,obj);
  336. exclude(childpd.procoptions,po_external);
  337. childpd.synthetickind:=tsk_anon_inherited;
  338. include(obj.objectoptions,oo_has_constructor);
  339. end;
  340. end;
  341. procedure implement_anon_inherited(pd: tprocdef);
  342. var
  343. str: ansistring;
  344. isclassmethod: boolean;
  345. begin
  346. isclassmethod:=
  347. (po_classmethod in pd.procoptions) and
  348. not(pd.proctypeoption in [potype_constructor,potype_destructor]);
  349. str:='begin ';
  350. if (pd.proctypeoption<>potype_constructor) and
  351. not is_void(pd.returndef) then
  352. str:=str+'result:=';
  353. str:=str+'inherited end;';
  354. str_parse_method_impl(str,pd,isclassmethod);
  355. end;
  356. procedure implement_jvm_clone(pd: tprocdef);
  357. var
  358. struct: tabstractrecorddef;
  359. str: ansistring;
  360. i: longint;
  361. sym: tsym;
  362. fsym: tfieldvarsym;
  363. begin
  364. if not(pd.struct.typ in [recorddef,objectdef]) then
  365. internalerror(2011032802);
  366. struct:=pd.struct;
  367. { anonymous record types must get an artificial name, so we can generate
  368. a typecast at the scanner level }
  369. if (struct.typ=recorddef) and
  370. not assigned(struct.typesym) then
  371. internalerror(2011032812);
  372. { We cannot easily use the inherited clone in case we have to create a
  373. deep copy of certain fields. The reason is that e.g. sets are pointers
  374. at the JVM level, but not in Pascal. So the JVM clone routine will copy
  375. the pointer to the set from the old record (= class instance) to the new
  376. one, but we have no way to change this pointer itself from inside Pascal
  377. code.
  378. We solve this by relying on the fact that the JVM is garbage collected:
  379. we simply declare a temporary instance on the stack, which will be
  380. allocated/initialized by the temp generator. We return its address as
  381. the result of the clone routine, so it remains live. }
  382. str:='var __fpc_newcopy:'+ struct.typesym.realname+'; begin clone:=JLObject(@__fpc_newcopy);';
  383. { copy all field contents }
  384. for i:=0 to struct.symtable.symlist.count-1 do
  385. begin
  386. sym:=tsym(struct.symtable.symlist[i]);
  387. if (sym.typ=fieldvarsym) then
  388. begin
  389. fsym:=tfieldvarsym(sym);
  390. str:=str+'__fpc_newcopy.&'+fsym.realname+':=&'+fsym.realname+';';
  391. end;
  392. end;
  393. str:=str+'end;';
  394. str_parse_method_impl(str,pd,false);
  395. end;
  396. procedure implement_record_deepcopy(pd: tprocdef);
  397. var
  398. struct: tabstractrecorddef;
  399. str: ansistring;
  400. i: longint;
  401. sym: tsym;
  402. fsym: tfieldvarsym;
  403. begin
  404. if not(pd.struct.typ in [recorddef,objectdef]) then
  405. internalerror(2011032810);
  406. struct:=pd.struct;
  407. { anonymous record types must get an artificial name, so we can generate
  408. a typecast at the scanner level }
  409. if (struct.typ=recorddef) and
  410. not assigned(struct.typesym) then
  411. internalerror(2011032811);
  412. { copy all fields }
  413. str:='type _fpc_ptrt = ^'+struct.typesym.realname+'; var res: _fpc_ptrt; begin res:=_fpc_ptrt(result);';
  414. for i:=0 to struct.symtable.symlist.count-1 do
  415. begin
  416. sym:=tsym(struct.symtable.symlist[i]);
  417. if (sym.typ=fieldvarsym) then
  418. begin
  419. fsym:=tfieldvarsym(sym);
  420. str:=str+'res^.&'+fsym.realname+':=&'+fsym.realname+';';
  421. end;
  422. end;
  423. str:=str+'end;';
  424. str_parse_method_impl(str,pd,false);
  425. end;
  426. procedure implement_record_initialize(pd: tprocdef);
  427. var
  428. struct: tabstractrecorddef;
  429. str: ansistring;
  430. i: longint;
  431. sym: tsym;
  432. fsym: tfieldvarsym;
  433. begin
  434. if not(pd.struct.typ in [recorddef,objectdef]) then
  435. internalerror(2011071710);
  436. struct:=pd.struct;
  437. { anonymous record types must get an artificial name, so we can generate
  438. a typecast at the scanner level }
  439. if (struct.typ=recorddef) and
  440. not assigned(struct.typesym) then
  441. internalerror(2011032804);
  442. { walk over all fields that need initialization }
  443. str:='begin ';
  444. for i:=0 to struct.symtable.symlist.count-1 do
  445. begin
  446. sym:=tsym(struct.symtable.symlist[i]);
  447. if (sym.typ=fieldvarsym) then
  448. begin
  449. fsym:=tfieldvarsym(sym);
  450. if fsym.vardef.needs_inittable then
  451. str:=str+(internal_macro_escape_unit_namespace_name+'system.initialize(&')+fsym.realname+');';
  452. end;
  453. end;
  454. str:=str+'end;';
  455. str_parse_method_impl(str,pd,false);
  456. end;
  457. procedure implement_empty(pd: tprocdef);
  458. var
  459. str: ansistring;
  460. isclassmethod: boolean;
  461. begin
  462. isclassmethod:=
  463. (po_classmethod in pd.procoptions) and
  464. not(pd.proctypeoption in [potype_constructor,potype_destructor]);
  465. str:='begin end;';
  466. str_parse_method_impl(str,pd,isclassmethod);
  467. end;
  468. procedure addvisibleparameters(var str: ansistring; pd: tprocdef);
  469. var
  470. currpara: tparavarsym;
  471. i: longint;
  472. firstpara: boolean;
  473. begin
  474. firstpara:=true;
  475. for i:=0 to pd.paras.count-1 do
  476. begin
  477. currpara:=tparavarsym(pd.paras[i]);
  478. if not(vo_is_hidden_para in currpara.varoptions) then
  479. begin
  480. if not firstpara then
  481. str:=str+',';
  482. firstpara:=false;
  483. str:=str+'&'+currpara.realname;
  484. end;
  485. end;
  486. end;
  487. procedure implement_callthrough(pd: tprocdef);
  488. var
  489. str: ansistring;
  490. callpd: tprocdef;
  491. isclassmethod: boolean;
  492. begin
  493. isclassmethod:=
  494. (po_classmethod in pd.procoptions) and
  495. not(pd.proctypeoption in [potype_constructor,potype_destructor]);
  496. callpd:=tprocdef(pd.skpara);
  497. str:='begin ';
  498. if pd.returndef<>voidtype then
  499. str:=str+'result:=';
  500. { if the routine is a global routine in a unit/program, explicitly
  501. mnetion this program/unit name to avoid accidentally calling other
  502. same-named routines that may be in scope }
  503. str:=str+def_unit_name_prefix_if_toplevel(callpd)+callpd.procsym.realname+'(';
  504. addvisibleparameters(str,pd);
  505. str:=str+') end;';
  506. str_parse_method_impl(str,pd,isclassmethod);
  507. end;
  508. {$ifdef jvm}
  509. procedure implement_jvm_enum_values(pd: tprocdef);
  510. begin
  511. str_parse_method_impl('begin result:=__fpc_FVALUES end;',pd,true);
  512. end;
  513. procedure implement_jvm_enum_valuof(pd: tprocdef);
  514. begin
  515. str_parse_method_impl('begin result:=__FPC_TEnumClassAlias(inherited valueOf(JLClass(__FPC_TEnumClassAlias),__fpc_str)) end;',pd,true);
  516. end;
  517. procedure implement_jvm_enum_jumps_constr(pd: tprocdef);
  518. begin
  519. str_parse_method_impl('begin inherited create(__fpc_name,__fpc_ord); __fpc_fenumval:=__fpc_initenumval end;',pd,false);
  520. end;
  521. procedure implement_jvm_enum_fpcordinal(pd: tprocdef);
  522. var
  523. enumclass: tobjectdef;
  524. enumdef: tenumdef;
  525. begin
  526. enumclass:=tobjectdef(pd.owner.defowner);
  527. enumdef:=tenumdef(ttypesym(search_struct_member(enumclass,'__FPC_TENUMALIAS')).typedef);
  528. if not enumdef.has_jumps then
  529. str_parse_method_impl('begin result:=ordinal end;',pd,false)
  530. else
  531. str_parse_method_impl('begin result:=__fpc_fenumval end;',pd,false);
  532. end;
  533. procedure implement_jvm_enum_fpcvalueof(pd: tprocdef);
  534. var
  535. enumclass: tobjectdef;
  536. enumdef: tenumdef;
  537. isclassmethod: boolean;
  538. begin
  539. isclassmethod:=
  540. (po_classmethod in pd.procoptions) and
  541. not(pd.proctypeoption in [potype_constructor,potype_destructor]);
  542. enumclass:=tobjectdef(pd.owner.defowner);
  543. enumdef:=tenumdef(ttypesym(search_struct_member(enumclass,'__FPC_TENUMALIAS')).typedef);
  544. { convert integer to corresponding enum instance: in case of no jumps
  545. get it from the $VALUES array, otherwise from the __fpc_ord2enum
  546. hashmap }
  547. if not enumdef.has_jumps then
  548. str_parse_method_impl('begin result:=__fpc_FVALUES[__fpc_int] end;',pd,isclassmethod)
  549. else
  550. str_parse_method_impl('begin result:=__FPC_TEnumClassAlias(__fpc_ord2enum.get(JLInteger.valueOf(__fpc_int))) end;',pd,isclassmethod);
  551. end;
  552. function CompareEnumSyms(Item1, Item2: Pointer): Integer;
  553. var
  554. I1 : tenumsym absolute Item1;
  555. I2 : tenumsym absolute Item2;
  556. begin
  557. Result:=I1.value-I2.value;
  558. end;
  559. procedure implement_jvm_enum_classconstr(pd: tprocdef);
  560. var
  561. enumclass: tobjectdef;
  562. enumdef: tenumdef;
  563. enumname,
  564. str: ansistring;
  565. i: longint;
  566. enumsym: tenumsym;
  567. orderedenums: tfpobjectlist;
  568. begin
  569. enumclass:=tobjectdef(pd.owner.defowner);
  570. enumdef:=tenumdef(ttypesym(search_struct_member(enumclass,'__FPC_TENUMALIAS')).typedef);
  571. if not assigned(enumdef) then
  572. internalerror(2011062305);
  573. str:='begin ';
  574. if enumdef.has_jumps then
  575. { init hashmap for ordinal -> enum instance mapping; don't let it grow,
  576. and set the capacity to the next prime following the total number of
  577. enum elements to minimise the number of collisions }
  578. str:=str+'__fpc_ord2enum:=JUHashMap.Create('+tostr(next_prime(enumdef.symtable.symlist.count))+',1.0);';
  579. { iterate over all enum elements and initialise the class fields, and
  580. store them in the values array. Since the java.lang.Enum doCompare
  581. method is final and hardcoded to compare based on declaration order
  582. (= java.lang.Enum.ordinal() value), we have to create them in order of
  583. ascending FPC ordinal values (which may not be the same as the FPC
  584. declaration order in case of jumps }
  585. orderedenums:=tfpobjectlist.create(false);
  586. for i:=0 to enumdef.symtable.symlist.count-1 do
  587. orderedenums.add(enumdef.symtable.symlist[i]);
  588. if enumdef.has_jumps then
  589. orderedenums.sort(@CompareEnumSyms);
  590. for i:=0 to orderedenums.count-1 do
  591. begin
  592. enumsym:=tenumsym(orderedenums[i]);
  593. enumname:=enumsym.realname;
  594. str:=str+enumsym.name+':=__FPC_TEnumClassAlias.Create('''+enumname+''','+tostr(i);
  595. if enumdef.has_jumps then
  596. str:=str+','+tostr(enumsym.value);
  597. str:=str+');';
  598. { alias for $VALUES array used internally by the JDK, and also by FPC
  599. in case of no jumps }
  600. str:=str+'__fpc_FVALUES['+tostr(i)+']:='+enumname+';';
  601. if enumdef.has_jumps then
  602. str:=str+'__fpc_ord2enum.put(JLInteger.valueOf('+tostr(enumsym.value)+'),'+enumname+');';
  603. end;
  604. orderedenums.free;
  605. str:=str+' end;';
  606. str_parse_method_impl(str,pd,true);
  607. end;
  608. procedure implement_jvm_enum_long2set(pd: tprocdef);
  609. begin
  610. str_parse_method_impl(
  611. 'var '+
  612. 'i, setval: jint;'+
  613. 'begin '+
  614. 'result:=JUEnumSet.noneOf(JLClass(__FPC_TEnumClassAlias));'+
  615. 'if __val<>0 then '+
  616. 'begin '+
  617. '__setsize:=__setsize*8;'+
  618. 'for i:=0 to __setsize-1 do '+
  619. // setsize-i because JVM = big endian
  620. 'if (__val and (jlong(1) shl (__setsize-i)))<>0 then '+
  621. 'result.add(fpcValueOf(i+__setbase));'+
  622. 'end '+
  623. 'end;',
  624. pd,true);
  625. end;
  626. procedure implement_jvm_enum_bitset2set(pd: tprocdef);
  627. begin
  628. str_parse_method_impl(
  629. 'var '+
  630. 'i, setval: jint;'+
  631. 'begin '+
  632. 'result:=JUEnumSet.noneOf(JLClass(__FPC_TEnumClassAlias));'+
  633. 'i:=__val.nextSetBit(0);'+
  634. 'while i>=0 do '+
  635. 'begin '+
  636. 'setval:=-__fromsetbase;'+
  637. 'result.add(fpcValueOf(setval+__tosetbase));'+
  638. 'i:=__val.nextSetBit(i+1);'+
  639. 'end '+
  640. 'end;',
  641. pd,true);
  642. end;
  643. procedure implement_jvm_enum_set2set(pd: tprocdef);
  644. begin
  645. str_parse_method_impl(
  646. 'var '+
  647. 'it: JUIterator;'+
  648. 'ele: FpcEnumValueObtainable;'+
  649. 'i: longint;'+
  650. 'begin '+
  651. 'result:=JUEnumSet.noneOf(JLClass(__FPC_TEnumClassAlias));'+
  652. 'it:=__val.iterator;'+
  653. 'while it.hasNext do '+
  654. 'begin '+
  655. 'ele:=FpcEnumValueObtainable(it.next);'+
  656. 'i:=ele.fpcOrdinal-__fromsetbase;'+
  657. 'result.add(fpcValueOf(i+__tosetbase));'+
  658. 'end '+
  659. 'end;',
  660. pd,true);
  661. end;
  662. procedure implement_jvm_procvar_invoke(pd: tprocdef);
  663. var
  664. pvclass: tobjectdef;
  665. procvar: tprocvardef;
  666. paraname,str,endstr: ansistring;
  667. pvs: tparavarsym;
  668. paradef,boxdef,boxargdef: tdef;
  669. i: longint;
  670. firstpara: boolean;
  671. begin
  672. pvclass:=tobjectdef(pd.owner.defowner);
  673. procvar:=tprocvardef(ttypesym(search_struct_member(pvclass,'__FPC_PROCVARALIAS')).typedef);
  674. { the procvar wrapper class has a tmethod member called "method", whose
  675. "code" field is a JLRMethod, and whose "data" field is the self pointer
  676. if any (if none is required, it's ignored by the JVM, so there's no
  677. problem with always passing it) }
  678. { force extended syntax to allow calling invokeObjectFunc() without using
  679. its result }
  680. str:='';
  681. endstr:='';
  682. { create local pointer to result type for typecasting in case of an
  683. implicit pointer type }
  684. if jvmimplicitpointertype(procvar.returndef) then
  685. str:=str+'type __FPC_returnptrtype = ^'+procvar.returndef.typename+';';
  686. str:=str+'begin ';
  687. { result handling (skip for generic definitions, we'll generate a new
  688. version for the specialized definition) ) }
  689. if not is_void(procvar.returndef) and
  690. (procvar.returndef.typ<>undefineddef) then
  691. begin
  692. str:=str+'invoke:=';
  693. if procvar.returndef.typ in [orddef,floatdef] then
  694. begin
  695. { primitivetype(boxtype(..).unboxmethod) }
  696. jvmgetboxtype(procvar.returndef,boxdef,boxargdef,false);
  697. str:=str+procvar.returndef.typename+'('+boxdef.typename+'(';
  698. endstr:=').'+jvmgetunboxmethod(procvar.returndef)+')';
  699. end
  700. else if jvmimplicitpointertype(procvar.returndef) then
  701. begin
  702. str:=str+'__FPC_returnptrtype(';
  703. { dereference }
  704. endstr:=')^';
  705. end
  706. else
  707. begin
  708. str:=str+procvar.returndef.typename+'(';
  709. endstr:=')';
  710. end;
  711. end;
  712. str:=str+'invokeObjectFunc([';
  713. { parameters are a constant array of jlobject }
  714. firstpara:=true;
  715. for i:=0 to procvar.paras.count-1 do
  716. begin
  717. { skip self/vmt/parentfp, passed separately }
  718. pvs:=tparavarsym(procvar.paras[i]);
  719. if ([vo_is_self,vo_is_vmt,vo_is_parentfp]*pvs.varoptions)<>[] then
  720. continue;
  721. if not firstpara then
  722. str:=str+',';
  723. firstpara:=false;
  724. paraname:=pvs.realname;
  725. paradef:=pvs.vardef;
  726. { Pascalize hidden high parameter }
  727. if vo_is_high_para in pvs.varoptions then
  728. paraname:='high('+tparavarsym(procvar.paras[i-1]).realname+')'
  729. else if vo_is_hidden_para in pvs.varoptions then
  730. begin
  731. if ([vo_is_range_check,vo_is_overflow_check]*pvs.varoptions)<>[] then
  732. { ok, simple boolean parameters }
  733. else
  734. internalerror(2011072403);
  735. end;
  736. { var/out/constref parameters -> pass address through (same for
  737. implicit pointer types) }
  738. if paramanager.push_copyout_param(pvs.varspez,paradef,procvar.proccalloption) or
  739. jvmimplicitpointertype(paradef) then
  740. begin
  741. paraname:='@'+paraname;
  742. paradef:=java_jlobject;
  743. end;
  744. if paradef.typ in [orddef,floatdef] then
  745. begin
  746. { box primitive types; use valueOf() rather than create because it
  747. can give better performance }
  748. jvmgetboxtype(paradef,boxdef,boxargdef,false);
  749. str:=str+boxdef.typename+'.valueOf('+boxargdef.typename+'('+paraname+'))'
  750. end
  751. else
  752. str:=str+'JLObject('+paraname+')';
  753. end;
  754. str:=str+'])'+endstr+' end;';
  755. str_parse_method_impl(str,pd,false)
  756. end;
  757. procedure implement_jvm_procvar_intconstr(pd: tprocdef);
  758. var
  759. pvdef: tprocvardef;
  760. begin
  761. { ideal, and most performant, would be to keep the interface instance
  762. passed to the constructor around and always call its method directly
  763. rather than working via reflection. Unfortunately, the procvar semantics
  764. that allow directly modifying the procvar via typecasting it to a
  765. tmethod make this very hard.
  766. So for now we simply take the address of the interface instance's
  767. method and assign it to the tmethod of this procvar }
  768. pvdef:=tprocvardef(pd.skpara);
  769. str_parse_method_impl('begin method:=System.TMethod(@__intf.'+pvdef.typesym.RealName+'Callback) end;',pd,false);
  770. end;
  771. procedure implement_jvm_virtual_clmethod(pd: tprocdef);
  772. var
  773. str: ansistring;
  774. callpd: tprocdef;
  775. begin
  776. callpd:=tprocdef(pd.skpara);
  777. str:='var pv: __fpc_virtualclassmethod_pv_t'+pd.unique_id_str+'; begin '
  778. + 'pv:=@'+callpd.procsym.RealName+';';
  779. if (pd.proctypeoption<>potype_constructor) and
  780. not is_void(pd.returndef) then
  781. str:=str+'result:=';
  782. str:=str+'pv(';
  783. addvisibleparameters(str,pd);
  784. str:=str+') end;';
  785. str_parse_method_impl(str,pd,true)
  786. end;
  787. {$endif jvm}
  788. {$ifdef wasm}
  789. procedure addvisibleparameterdeclarations(var str: ansistring; pd: tprocdef);
  790. var
  791. currpara: tparavarsym;
  792. i: longint;
  793. firstpara: boolean;
  794. begin
  795. firstpara:=true;
  796. for i:=0 to pd.paras.count-1 do
  797. begin
  798. currpara:=tparavarsym(pd.paras[i]);
  799. if not(vo_is_hidden_para in currpara.varoptions) then
  800. begin
  801. if not firstpara then
  802. str:=str+';';
  803. firstpara:=false;
  804. case currpara.varspez of
  805. vs_constref:
  806. str:=str+'constref ';
  807. vs_out:
  808. str:=str+'out ';
  809. vs_var:
  810. str:=str+'var ';
  811. vs_const:
  812. str:=str+'const ';
  813. vs_value:
  814. ;
  815. else
  816. internalerror(2023061108);
  817. end;
  818. str:=str+currpara.realname;
  819. if currpara.vardef.typ<>formaldef then
  820. str:=str+':'+currpara.vardef.fulltypename;
  821. end;
  822. end;
  823. end;
  824. procedure implement_wasm_suspending(pd: tcpuprocdef; last: Boolean);
  825. var
  826. str: ansistring;
  827. wrapper_name: ansistring;
  828. begin
  829. wrapper_name:=pd.suspending_wrapper_name;
  830. if is_void(pd.returndef) then
  831. str:='procedure '
  832. else
  833. str:='function ';
  834. str:=str+wrapper_name+'(';
  835. if last then
  836. begin
  837. addvisibleparameterdeclarations(str,pd);
  838. if str[Length(str)]<>'(' then
  839. str:=str+';';
  840. str:=str+'__fpc_wasm_susp: WasmExternRef';
  841. end
  842. else
  843. begin
  844. str:=str+'__fpc_wasm_susp: WasmExternRef;';
  845. addvisibleparameterdeclarations(str,pd);
  846. if str[Length(str)]=';' then
  847. delete(str,Length(str),1);
  848. end;
  849. str:=str+')';
  850. if not is_void(pd.returndef) then
  851. str:=str+': '+pd.returndef.fulltypename;
  852. str:=str+'; external '''+pd.import_dll^+ ''' name '''+pd.import_name^+''';';
  853. str_parse_method_impl(str,nil,false);
  854. str:='var __fpc_wasm_suspender_copy:WasmExternRef; begin __fpc_wasm_suspender_copy:=__fpc_wasm_suspender; ';
  855. if not is_void(pd.returndef) then
  856. str:=str+' result:=';
  857. str:=str+wrapper_name+'(__fpc_wasm_suspender_copy,';
  858. addvisibleparameters(str,pd);
  859. if str[Length(str)]=',' then
  860. delete(str,Length(str),1);
  861. str:=str+');';
  862. str:=str+' __fpc_wasm_suspender:=__fpc_wasm_suspender_copy;';
  863. str:=str+' end;';
  864. str_parse_method_impl(str,pd,false);
  865. exclude(pd.procoptions,po_external);
  866. end;
  867. function implement_wasm_promising_wrapper(pd: tcpuprocdef;last:boolean):tprocdef;
  868. var
  869. str: ansistring;
  870. wrapper_name: ansistring;
  871. begin
  872. wrapper_name:=pd.promising_wrapper_name(last);
  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+'; begin __fpc_wasm_suspender:=__fpc_wasm_susp;';
  896. if not is_void(pd.returndef) then
  897. str:=str+' result:=';
  898. str:=str+pd.procsym.RealName+'(';
  899. addvisibleparameters(str,pd);
  900. if str[Length(str)]=',' then
  901. delete(str,Length(str),1);
  902. str:=str+'); end;';
  903. str_parse_method_impl(str,nil,false,result);
  904. end;
  905. procedure implement_wasm_promising(pd: tcpuprocdef);
  906. var
  907. new_wrapper_pd: tprocdef;
  908. begin
  909. if pd.promising_first_export_name<>'' then
  910. begin
  911. new_wrapper_pd:=implement_wasm_promising_wrapper(pd,false);
  912. current_asmdata.asmlists[al_exports].Concat(tai_export_name.create(pd.promising_first_export_name,new_wrapper_pd.mangledname,ie_Func));
  913. end;
  914. if pd.promising_last_export_name<>'' then
  915. begin
  916. new_wrapper_pd:=implement_wasm_promising_wrapper(pd,true);
  917. current_asmdata.asmlists[al_exports].Concat(tai_export_name.create(pd.promising_last_export_name,new_wrapper_pd.mangledname,ie_Func));
  918. end;
  919. end;
  920. {$endif wasm}
  921. procedure implement_field_getter(pd: tprocdef);
  922. var
  923. i: longint;
  924. pvs: tparavarsym;
  925. str: ansistring;
  926. callthroughprop: tpropertysym;
  927. propaccesslist: tpropaccesslist;
  928. lastparanr: longint;
  929. firstpara: boolean;
  930. begin
  931. callthroughprop:=tpropertysym(pd.skpara);
  932. str:='begin result:='+callthroughprop.realname;
  933. if ppo_hasparameters in callthroughprop.propoptions then
  934. begin
  935. if not callthroughprop.getpropaccesslist(palt_read,propaccesslist) then
  936. internalerror(2012100701);
  937. str:=str+'[';
  938. firstpara:=true;
  939. lastparanr:=tprocdef(propaccesslist.procdef).paras.count-1;
  940. if ppo_indexed in callthroughprop.propoptions then
  941. dec(lastparanr);
  942. for i:=0 to lastparanr do
  943. begin
  944. { skip self/vmt/parentfp, passed implicitly }
  945. pvs:=tparavarsym(tprocdef(propaccesslist.procdef).paras[i]);
  946. if ([vo_is_self,vo_is_vmt,vo_is_parentfp]*pvs.varoptions)<>[] then
  947. continue;
  948. if not firstpara then
  949. str:=str+',';
  950. firstpara:=false;
  951. str:=str+pvs.realname;
  952. end;
  953. str:=str+']';
  954. end;
  955. str:=str+'; end;';
  956. str_parse_method_impl(str,pd,po_classmethod in pd.procoptions)
  957. end;
  958. procedure implement_field_setter(pd: tprocdef);
  959. var
  960. i, lastparaindex: longint;
  961. pvs: tparavarsym;
  962. paraname, str: ansistring;
  963. callthroughprop: tpropertysym;
  964. propaccesslist: tpropaccesslist;
  965. firstpara: boolean;
  966. begin
  967. callthroughprop:=tpropertysym(pd.skpara);
  968. str:='begin '+callthroughprop.realname;
  969. if not callthroughprop.getpropaccesslist(palt_write,propaccesslist) then
  970. internalerror(2012100702);
  971. if ppo_hasparameters in callthroughprop.propoptions then
  972. begin
  973. str:=str+'[';
  974. firstpara:=true;
  975. { last parameter is the value to be set, skip (only add index
  976. parameters here) }
  977. lastparaindex:=tprocdef(propaccesslist.procdef).paras.count-2;
  978. if ppo_indexed in callthroughprop.propoptions then
  979. dec(lastparaindex);
  980. for i:=0 to lastparaindex do
  981. begin
  982. { skip self/vmt/parentfp/index, passed implicitly }
  983. pvs:=tparavarsym(tprocdef(propaccesslist.procdef).paras[i]);
  984. if ([vo_is_self,vo_is_vmt,vo_is_parentfp]*pvs.varoptions)<>[] then
  985. continue;
  986. if not firstpara then
  987. str:=str+',';
  988. firstpara:=false;
  989. str:=str+pvs.realname;
  990. end;
  991. str:=str+']';
  992. end;
  993. { the value-to-be-set }
  994. if assigned(propaccesslist.procdef) then
  995. begin
  996. pvs:=tparavarsym(tprocdef(propaccesslist.procdef).paras[tprocdef(propaccesslist.procdef).paras.count-1]);
  997. paraname:=pvs.realname;
  998. end
  999. else
  1000. paraname:='__fpc_newval__';
  1001. str:=str+':='+paraname+'; end;';
  1002. str_parse_method_impl(str,pd,po_classmethod in pd.procoptions)
  1003. end;
  1004. procedure implement_block_invoke_procvar(pd: tprocdef);
  1005. var
  1006. str: ansistring;
  1007. begin
  1008. str:='';
  1009. str:='begin ';
  1010. if pd.returndef<>voidtype then
  1011. str:=str+'result:=';
  1012. str:=str+'__FPC_BLOCK_INVOKE_PV_TYPE(PFPC_Block_literal_complex_procvar(FPC_Block_Self)^.pv)(';
  1013. addvisibleparameters(str,pd);
  1014. str:=str+') end;';
  1015. str_parse_method_impl(str,pd,false);
  1016. end;
  1017. procedure implement_interface_wrapper(pd: tprocdef);
  1018. var
  1019. wrapperinfo: pskpara_interface_wrapper;
  1020. callthroughpd, tmpproc: tprocdef;
  1021. str: ansistring;
  1022. fileinfo: tfileposinfo;
  1023. begin
  1024. wrapperinfo:=pskpara_interface_wrapper(pd.skpara);
  1025. if not assigned(wrapperinfo) then
  1026. internalerror(2015090801);
  1027. callthroughpd:=tprocdef(wrapperinfo^.pd);
  1028. str:='begin ';
  1029. { self right now points to the VMT of interface inside the instance ->
  1030. adjust so it points to the start of the instance }
  1031. str:=str+'pointer(self):=pointer(self) - '+tostr(wrapperinfo^.offset)+';';
  1032. { now call through to the actual method }
  1033. if pd.returndef<>voidtype then
  1034. str:=str+'result:=';
  1035. str:=str+'&'+callthroughpd.procsym.realname+'(';
  1036. addvisibleparameters(str,pd);
  1037. str:=str+') end;';
  1038. { add dummy file info so we can step in/through it }
  1039. if pd.owner.iscurrentunit then
  1040. fileinfo:=pd.fileinfo
  1041. else
  1042. begin
  1043. fileinfo.moduleindex:=current_module.moduleid;
  1044. fileinfo.fileindex:=1;
  1045. fileinfo.line:=1;
  1046. fileinfo.column:=1;
  1047. end;
  1048. str_parse_method_impl_with_fileinfo(str,pd,fileinfo.fileindex,fileinfo.line,false,tmpproc);
  1049. dispose(wrapperinfo);
  1050. pd.skpara:=nil;
  1051. end;
  1052. procedure implement_call_no_parameters(pd: tprocdef);
  1053. var
  1054. callpd: tprocdef;
  1055. str: ansistring;
  1056. warningson,
  1057. isclassmethod: boolean;
  1058. begin
  1059. { avoid warnings about unset function results in these abstract wrappers }
  1060. warningson:=(status.verbosity and V_Warning)<>0;
  1061. setverbosity('W-');
  1062. str:='begin ';
  1063. callpd:=tprocdef(pd.skpara);
  1064. str:=str+def_unit_name_prefix_if_toplevel(callpd)+callpd.procsym.realname+'; end;';
  1065. isclassmethod:=
  1066. (po_classmethod in pd.procoptions) and
  1067. not(pd.proctypeoption in [potype_constructor,potype_destructor]);
  1068. str_parse_method_impl(str,pd,isclassmethod);
  1069. if warningson then
  1070. setverbosity('W+');
  1071. end;
  1072. procedure add_synthetic_method_implementations_for_st(st: tsymtable);
  1073. var
  1074. i : longint;
  1075. def : tdef;
  1076. pd : tprocdef;
  1077. begin
  1078. for i:=0 to st.deflist.count-1 do
  1079. begin
  1080. def:=tdef(st.deflist[i]);
  1081. if (def.typ<>procdef) then
  1082. continue;
  1083. { skip methods when processing unit symtable }
  1084. if def.owner<>st then
  1085. continue;
  1086. pd:=tprocdef(def);
  1087. case pd.synthetickind of
  1088. tsk_none:
  1089. ;
  1090. tsk_anon_inherited:
  1091. implement_anon_inherited(pd);
  1092. tsk_jvm_clone:
  1093. implement_jvm_clone(pd);
  1094. tsk_record_deepcopy:
  1095. implement_record_deepcopy(pd);
  1096. tsk_record_initialize:
  1097. implement_record_initialize(pd);
  1098. tsk_empty,
  1099. { special handling for this one is done in tnodeutils.wrap_proc_body }
  1100. tsk_tcinit:
  1101. implement_empty(pd);
  1102. tsk_callthrough:
  1103. implement_callthrough(pd);
  1104. tsk_callthrough_nonabstract:
  1105. begin
  1106. if (pd.owner.defowner.typ<>objectdef) or
  1107. (tobjectdef(pd.owner.defowner).abstractcnt=0) then
  1108. implement_callthrough(pd)
  1109. else
  1110. implement_empty(pd);
  1111. end;
  1112. {$ifdef jvm}
  1113. tsk_jvm_enum_values:
  1114. implement_jvm_enum_values(pd);
  1115. tsk_jvm_enum_valueof:
  1116. implement_jvm_enum_valuof(pd);
  1117. tsk_jvm_enum_classconstr:
  1118. implement_jvm_enum_classconstr(pd);
  1119. tsk_jvm_enum_jumps_constr:
  1120. implement_jvm_enum_jumps_constr(pd);
  1121. tsk_jvm_enum_fpcordinal:
  1122. implement_jvm_enum_fpcordinal(pd);
  1123. tsk_jvm_enum_fpcvalueof:
  1124. implement_jvm_enum_fpcvalueof(pd);
  1125. tsk_jvm_enum_long2set:
  1126. implement_jvm_enum_long2set(pd);
  1127. tsk_jvm_enum_bitset2set:
  1128. implement_jvm_enum_bitset2set(pd);
  1129. tsk_jvm_enum_set2set:
  1130. implement_jvm_enum_set2set(pd);
  1131. tsk_jvm_procvar_invoke:
  1132. implement_jvm_procvar_invoke(pd);
  1133. tsk_jvm_procvar_intconstr:
  1134. implement_jvm_procvar_intconstr(pd);
  1135. tsk_jvm_virtual_clmethod:
  1136. implement_jvm_virtual_clmethod(pd);
  1137. {$else}
  1138. tsk_jvm_enum_values,
  1139. tsk_jvm_enum_valueof,
  1140. tsk_jvm_enum_classconstr,
  1141. tsk_jvm_enum_jumps_constr,
  1142. tsk_jvm_enum_fpcordinal,
  1143. tsk_jvm_enum_fpcvalueof,
  1144. tsk_jvm_enum_long2set,
  1145. tsk_jvm_enum_bitset2set,
  1146. tsk_jvm_enum_set2set,
  1147. tsk_jvm_procvar_invoke,
  1148. tsk_jvm_procvar_intconstr,
  1149. tsk_jvm_virtual_clmethod:
  1150. internalerror(2011032801);
  1151. {$endif jvm}
  1152. {$ifdef wasm}
  1153. tsk_wasm_suspending:
  1154. implement_wasm_suspending(tcpuprocdef(pd),false);
  1155. tsk_wasm_suspending_last:
  1156. implement_wasm_suspending(tcpuprocdef(pd),true);
  1157. tsk_wasm_promising:
  1158. implement_wasm_promising(tcpuprocdef(pd));
  1159. {$else wasm}
  1160. tsk_wasm_suspending,
  1161. tsk_wasm_suspending_last,
  1162. tsk_wasm_promising:
  1163. internalerror(2023061107);
  1164. {$endif wasm}
  1165. tsk_field_getter:
  1166. implement_field_getter(pd);
  1167. tsk_field_setter:
  1168. implement_field_setter(pd);
  1169. tsk_block_invoke_procvar:
  1170. implement_block_invoke_procvar(pd);
  1171. tsk_interface_wrapper:
  1172. implement_interface_wrapper(pd);
  1173. tsk_call_no_parameters:
  1174. implement_call_no_parameters(pd);
  1175. end;
  1176. end;
  1177. end;
  1178. procedure add_synthetic_method_implementations(st: tsymtable);
  1179. var
  1180. i: longint;
  1181. def: tdef;
  1182. sstate: tscannerstate;
  1183. begin
  1184. { skip if any errors have occurred, since then this can only cause more
  1185. errors }
  1186. if ErrorCount<>0 then
  1187. exit;
  1188. replace_scanner('synthetic_impl',sstate);
  1189. add_synthetic_method_implementations_for_st(st);
  1190. for i:=0 to st.deflist.count-1 do
  1191. begin
  1192. def:=tdef(st.deflist[i]);
  1193. if (def.typ=procdef) and
  1194. assigned(tprocdef(def).localst) and
  1195. { not true for the "main" procedure, whose localsymtable is the staticsymtable }
  1196. (tprocdef(def).localst.symtabletype=localsymtable) then
  1197. add_synthetic_method_implementations(tprocdef(def).localst)
  1198. else if ((def.typ=objectdef) and
  1199. not(oo_is_external in tobjectdef(def).objectoptions)) or
  1200. (def.typ=recorddef) then
  1201. begin
  1202. { also complete nested types }
  1203. add_synthetic_method_implementations(tabstractrecorddef(def).symtable);
  1204. end;
  1205. end;
  1206. restore_scanner(sstate);
  1207. end;
  1208. function create_procdef_alias(pd: tprocdef; const newrealname: string; const newmangledname: TSymStr; newparentst: tsymtable; newstruct: tabstractrecorddef;
  1209. sk: tsynthetickind; skpara: pointer): tprocdef;
  1210. begin
  1211. { bare copy so we don't copy the aliasnames (specify prefix for
  1212. parameter names so we don't get issues in the body in case
  1213. we e.g. reference system.initialize and one of the parameters
  1214. is called "system") }
  1215. result:=tprocdef(pd.getcopyas(procdef,pc_bareproc,'__FPCW_',true));
  1216. { set the mangled name to the wrapper name }
  1217. result.setmangledname(newmangledname);
  1218. { finish creating the copy }
  1219. finish_copied_procdef(result,newrealname,newparentst,newstruct);
  1220. { insert hidden high parameters }
  1221. result.parast.SymList.ForEachCall(@insert_hidden_para,result);
  1222. { now insert self/vmt }
  1223. insert_self_and_vmt_para(result);
  1224. { and the function result }
  1225. insert_funcret_para(result);
  1226. { recalculate the parameters now that we've added the missing ones }
  1227. result.calcparas;
  1228. { set the info required to generate the implementation }
  1229. result.synthetickind:=sk;
  1230. result.skpara:=skpara;
  1231. end;
  1232. procedure finish_copied_procdef(var pd: tprocdef; const realname: string; newparentst: tsymtable; newstruct: tabstractrecorddef);
  1233. var
  1234. sym: tsym;
  1235. parasym: tparavarsym;
  1236. ps: tprocsym;
  1237. stname: string;
  1238. i: longint;
  1239. begin
  1240. { add generic flag if required }
  1241. if assigned(newstruct) and
  1242. (df_generic in newstruct.defoptions) then
  1243. include(pd.defoptions,df_generic);
  1244. { associate the procdef with a procsym in the owner }
  1245. if not(pd.proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  1246. stname:=upper(realname)
  1247. else
  1248. stname:=lower(realname);
  1249. sym:=tsym(newparentst.find(stname));
  1250. if assigned(sym) then
  1251. begin
  1252. if sym.typ<>procsym then
  1253. internalerror(2011040601);
  1254. ps:=tprocsym(sym);
  1255. end
  1256. else
  1257. begin
  1258. ps:=cprocsym.create(realname);
  1259. newparentst.insertsym(ps);
  1260. end;
  1261. pd.procsym:=ps;
  1262. pd.struct:=newstruct;
  1263. { in case of methods, replace the special parameter types with new ones }
  1264. if assigned(newstruct) then
  1265. begin
  1266. symtablestack.push(pd.parast);
  1267. { may not be assigned in case we converted a procvar into a procdef }
  1268. if assigned(pd.paras) then
  1269. begin
  1270. for i:=0 to pd.paras.count-1 do
  1271. begin
  1272. parasym:=tparavarsym(pd.paras[i]);
  1273. if vo_is_self in parasym.varoptions then
  1274. begin
  1275. if parasym.vardef.typ=classrefdef then
  1276. parasym.vardef:=cclassrefdef.create(newstruct)
  1277. else
  1278. parasym.vardef:=newstruct;
  1279. end
  1280. end;
  1281. end;
  1282. { also fix returndef in case of a constructor }
  1283. if pd.proctypeoption=potype_constructor then
  1284. pd.returndef:=newstruct;
  1285. symtablestack.pop(pd.parast);
  1286. end;
  1287. pd.calcparas;
  1288. proc_add_definition(pd);
  1289. end;
  1290. function maybe_add_sym_to_parentfpstruct(pd: tprocdef; sym: tsym; vardef: tdef; addrparam: boolean): tsym;
  1291. var
  1292. fieldvardef,
  1293. nestedvarsdef: tdef;
  1294. nestedvarsst: tsymtable;
  1295. initcode: tnode;
  1296. old_filepos: tfileposinfo;
  1297. symname,
  1298. symrealname: TSymStr;
  1299. begin
  1300. nestedvarsdef:=tlocalvarsym(pd.parentfpstruct).vardef;
  1301. { redirect all aliases for the function result also to the function
  1302. result }
  1303. if vo_is_funcret in tabstractvarsym(sym).varoptions then
  1304. begin
  1305. symname:='result';
  1306. symrealname:='$result'
  1307. end
  1308. else
  1309. begin
  1310. symname:=sym.name;
  1311. symrealname:=sym.EscapedRealName;
  1312. end;
  1313. result:=search_struct_member(trecorddef(nestedvarsdef),symname);
  1314. if not assigned(result) then
  1315. begin
  1316. { mark that this symbol is mirrored in the parentfpstruct }
  1317. tabstractnormalvarsym(sym).inparentfpstruct:=true;
  1318. { add field to the struct holding all locals accessed
  1319. by nested routines }
  1320. nestedvarsst:=trecorddef(nestedvarsdef).symtable;
  1321. { indicate whether or not this is a var/out/constref/... parameter }
  1322. if addrparam then
  1323. fieldvardef:=cpointerdef.getreusable(vardef)
  1324. else
  1325. fieldvardef:=vardef;
  1326. result:=cfieldvarsym.create(symrealname,vs_value,fieldvardef,[]);
  1327. nestedvarsst.insertsym(result);
  1328. trecordsymtable(nestedvarsst).addfield(tfieldvarsym(result),vis_public);
  1329. { add initialization with original value if it's a parameter }
  1330. if (sym.typ=paravarsym) then
  1331. begin
  1332. old_filepos:=current_filepos;
  1333. fillchar(current_filepos,sizeof(current_filepos),0);
  1334. initcode:=cloadnode.create(sym,sym.owner);
  1335. { indicate that this load should not be transformed into a load
  1336. from the parentfpstruct, but instead should load the original
  1337. value }
  1338. include(initcode.flags,nf_internal);
  1339. { in case it's a var/out/constref parameter, store the address of the
  1340. parameter in the struct }
  1341. if addrparam then
  1342. begin
  1343. initcode:=caddrnode.create_internal(initcode);
  1344. include(taddrnode(initcode).addrnodeflags,anf_typedaddr);
  1345. end;
  1346. initcode:=cassignmentnode.create(
  1347. csubscriptnode.create(result,cloadnode.create(pd.parentfpstruct,pd.parentfpstruct.owner)),
  1348. initcode);
  1349. tblocknode(pd.parentfpinitblock).left:=cstatementnode.create
  1350. (initcode,tblocknode(pd.parentfpinitblock).left);
  1351. current_filepos:=old_filepos;
  1352. end;
  1353. end;
  1354. end;
  1355. procedure redirect_parentfpstruct_local_syms(pd: tprocdef);
  1356. var
  1357. nestedvarsdef: trecorddef;
  1358. sl: tpropaccesslist;
  1359. fsym,
  1360. lsym,
  1361. aliassym: tsym;
  1362. i: longint;
  1363. begin
  1364. nestedvarsdef:=trecorddef(tlocalvarsym(pd.parentfpstruct).vardef);
  1365. for i:=0 to nestedvarsdef.symtable.symlist.count-1 do
  1366. begin
  1367. fsym:=tsym(nestedvarsdef.symtable.symlist[i]);
  1368. if fsym.typ<>fieldvarsym then
  1369. continue;
  1370. lsym:=tsym(pd.localst.find(fsym.name));
  1371. if not assigned(lsym) then
  1372. lsym:=tsym(pd.parast.find(fsym.name));
  1373. if not assigned(lsym) then
  1374. internalerror(2011060408);
  1375. { add an absolute variable that redirects to the field }
  1376. sl:=tpropaccesslist.create;
  1377. sl.addsym(sl_load,pd.parentfpstruct);
  1378. sl.addsym(sl_subscript,tfieldvarsym(fsym));
  1379. aliassym:=cabsolutevarsym.create_ref(lsym.EscapedRealName,tfieldvarsym(fsym).vardef,sl);
  1380. { hide the original variable (can't delete, because there
  1381. may be other loadnodes that reference it)
  1382. -- only for locals; hiding parameters changes the
  1383. function signature }
  1384. if lsym.typ<>paravarsym then
  1385. hidesym(lsym);
  1386. { insert the absolute variable in the localst of the
  1387. routine; ignore duplicates, because this will also check the
  1388. parasymtable and we want to override parameters with our local
  1389. versions }
  1390. pd.localst.insertsym(aliassym,false);
  1391. end;
  1392. end;
  1393. function find_sym_in_parentfpstruct(pd: tprocdef; sym: tsym): tsym;
  1394. var
  1395. nestedvarsdef: tdef;
  1396. begin
  1397. nestedvarsdef:=tlocalvarsym(pd.parentfpstruct).vardef;
  1398. result:=search_struct_member(trecorddef(nestedvarsdef),sym.name);
  1399. end;
  1400. procedure finish_parentfpstruct(pd: tprocdef);
  1401. begin
  1402. trecordsymtable(trecorddef(tlocalvarsym(pd.parentfpstruct).vardef).symtable).addalignmentpadding;
  1403. end;
  1404. function make_field_static(recst: tsymtable; fieldvs: tfieldvarsym): tstaticvarsym;
  1405. var
  1406. static_name: string;
  1407. hstaticvs: tstaticvarsym;
  1408. tmp: tabsolutevarsym;
  1409. sl: tpropaccesslist;
  1410. begin
  1411. include(fieldvs.symoptions,sp_static);
  1412. { generate the symbol which reserves the space }
  1413. static_name:=lower(generate_nested_name(recst,'_'))+'_'+fieldvs.name;
  1414. hstaticvs:=cstaticvarsym.create_from_fieldvar(static_name,fieldvs);
  1415. {$ifdef jvm}
  1416. { for the JVM, static field accesses are name-based and
  1417. hence we have to keep the original name of the field.
  1418. Create a staticvarsym instead of a fieldvarsym so we can
  1419. nevertheless use a loadn instead of a subscriptn though,
  1420. since a subscriptn requires something to subscript and
  1421. there is nothing in this case (class+field name will be
  1422. encoded in the mangled symbol name) }
  1423. recst.insertsym(hstaticvs);
  1424. { only set the staticvarsym's basename (= field name, without any
  1425. mangling), because generating the fully mangled name right now can
  1426. result in a wrong string in case the field's type is a forward
  1427. declared class whose external name will change when the actual
  1428. definition is parsed }
  1429. if (vo_has_mangledname in fieldvs.varoptions) then
  1430. hstaticvs.set_mangledbasename(fieldvs.externalname^)
  1431. else
  1432. hstaticvs.set_mangledbasename(fieldvs.realname);
  1433. { for definition in class file }
  1434. hstaticvs.visibility:=fieldvs.visibility;
  1435. {$else jvm}
  1436. include(hstaticvs.symoptions,sp_internal);
  1437. if df_generic in tdef(recst.defowner).defoptions then
  1438. tabstractrecordsymtable(recst).insertsym(hstaticvs)
  1439. else
  1440. tdef(tabstractrecordsymtable(recst).defowner).get_top_level_symtable(false).insertsym(hstaticvs);
  1441. {$endif jvm}
  1442. { generate the symbol for the access }
  1443. sl:=tpropaccesslist.create;
  1444. sl.addsym(sl_load,hstaticvs);
  1445. { do *not* change the visibility of this absolutevarsym from vis_public
  1446. to anything else, because its visibility is used by visibility checks
  1447. after turning a class property referring to a class variable into a
  1448. load node (handle_staticfield_access -> searchsym_in_class ->
  1449. is_visible_for_object), which means that the load will fail if this
  1450. symbol is e.g. "strict private" while the property is public }
  1451. tmp:=cabsolutevarsym.create_ref('$'+static_name,fieldvs.vardef,sl);
  1452. recst.insertsym(tmp);
  1453. result:=hstaticvs;
  1454. end;
  1455. procedure call_through_new_name(orgpd: tprocdef; const newname: TSymStr);
  1456. var
  1457. newpd: tprocdef;
  1458. begin
  1459. { we have a forward declaration like
  1460. procedure test; (in the unit interface or "forward")
  1461. and then an implementation like
  1462. procedure test; external name 'something';
  1463. To solve this, we create a new external procdef for the
  1464. implementation, and then generate a procedure body for the original
  1465. one that calls through to the external procdef. This is necessary
  1466. because there may already be references to the mangled name for the
  1467. non-external "test".
  1468. }
  1469. { prefixing the parameters here is useless, because the new procdef will
  1470. just be an external declaration without a body }
  1471. newpd:=tprocdef(orgpd.getcopyas(procdef,pc_bareproc,'',true));
  1472. insert_funcret_para(newpd);
  1473. newpd.procoptions:=newpd.procoptions+orgpd.procoptions*[po_external,po_has_importname,po_has_importdll];
  1474. stringdispose(orgpd.import_name);
  1475. stringdispose(orgpd.import_dll);
  1476. orgpd.import_nr:=0;
  1477. newpd.setmangledname(newname);
  1478. finish_copied_procdef(newpd,'__FPC_IMPL_EXTERNAL_REDIRECT_'+newname,current_module.localsymtable,nil);
  1479. newpd.forwarddef:=false;
  1480. { ideally we would prefix the parameters of the original routine here, but since it
  1481. can be an interface definition, we cannot do that without risking to change the
  1482. interface crc }
  1483. orgpd.skpara:=newpd;
  1484. orgpd.synthetickind:=tsk_callthrough;
  1485. orgpd.procoptions:=orgpd.procoptions-[po_external,po_has_importname,po_has_importdll];
  1486. orgpd.forwarddef:=true;
  1487. end;
  1488. function generate_pkg_stub(pd:tprocdef):tnode;
  1489. begin
  1490. if target_info.system in systems_all_windows+systems_nativent then
  1491. begin
  1492. insert_funcret_local(pd);
  1493. result:=cassignmentnode.create(
  1494. cloadnode.create(pd.funcretsym,pd.localst),
  1495. cordconstnode.create(1,bool32type,false)
  1496. );
  1497. end
  1498. else
  1499. result:=cnothingnode.create;
  1500. end;
  1501. procedure generate_attr_constrs(attrs:tfpobjectlist);
  1502. var
  1503. ps : tprocsym;
  1504. pd : tprocdef;
  1505. pi : tcgprocinfo;
  1506. i : sizeint;
  1507. attr : trtti_attribute;
  1508. begin
  1509. if attrs.count=0 then
  1510. exit;
  1511. { if this isn't set then this unit shouldn't have any attributes }
  1512. if not assigned(class_tcustomattribute) then
  1513. internalerror(2019071003);
  1514. for i:=0 to attrs.count-1 do
  1515. begin
  1516. attr:=trtti_attribute(attrs[i]);
  1517. {Generate a procsym for main}
  1518. ps:=cprocsym.create('$rttiattrconstr$'+tostr(i));
  1519. { always register the symbol }
  1520. ps.register_sym;
  1521. { the RTTI always references this symbol }
  1522. inc(ps.refs);
  1523. current_module.localsymtable.insertsym(ps);
  1524. pd:=cprocdef.create(normal_function_level,true);
  1525. { always register the def }
  1526. pd.register_def;
  1527. pd.procsym:=ps;
  1528. ps.ProcdefList.Add(pd);
  1529. { set procdef options }
  1530. pd.proctypeoption:=potype_function;
  1531. pd.proccalloption:=pocall_default;
  1532. include(pd.procoptions,po_hascallingconvention);
  1533. pd.returndef:=class_tcustomattribute;
  1534. insert_funcret_para(pd);
  1535. pd.calcparas;
  1536. pd.forwarddef:=false;
  1537. pd.aliasnames.insert(pd.mangledname);
  1538. handle_calling_convention(pd,hcc_default_actions_impl);
  1539. { set procinfo and current_procinfo.procdef }
  1540. pi:=tcgprocinfo(cprocinfo.create(nil));
  1541. pi.procdef:=pd;
  1542. { we always do a call, namely to the constructor }
  1543. include(pi.flags,pi_do_call);
  1544. insert_funcret_local(pd);
  1545. pi.code:=cassignmentnode.create(
  1546. cloadnode.create(pd.funcretsym,pd.localst),
  1547. attr.constructorcall.getcopy
  1548. );
  1549. pi.generate_code;
  1550. attr.constructorpd:=pd;
  1551. end;
  1552. end;
  1553. end.