symcreat.pas 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278
  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; // no nil needed
  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. orderedenums := nil;
  649. str:=str+' end;';
  650. str_parse_method_impl(str,pd,true);
  651. end;
  652. procedure implement_jvm_enum_long2set(pd: tprocdef);
  653. begin
  654. str_parse_method_impl(
  655. 'var '+
  656. 'i, setval: jint;'+
  657. 'begin '+
  658. 'result:=JUEnumSet.noneOf(JLClass(__FPC_TEnumClassAlias));'+
  659. 'if __val<>0 then '+
  660. 'begin '+
  661. '__setsize:=__setsize*8;'+
  662. 'for i:=0 to __setsize-1 do '+
  663. // setsize-i because JVM = big endian
  664. 'if (__val and (jlong(1) shl (__setsize-i)))<>0 then '+
  665. 'result.add(fpcValueOf(i+__setbase));'+
  666. 'end '+
  667. 'end;',
  668. pd,true);
  669. end;
  670. procedure implement_jvm_enum_bitset2set(pd: tprocdef);
  671. begin
  672. str_parse_method_impl(
  673. 'var '+
  674. 'i, setval: jint;'+
  675. 'begin '+
  676. 'result:=JUEnumSet.noneOf(JLClass(__FPC_TEnumClassAlias));'+
  677. 'i:=__val.nextSetBit(0);'+
  678. 'while i>=0 do '+
  679. 'begin '+
  680. 'setval:=-__fromsetbase;'+
  681. 'result.add(fpcValueOf(setval+__tosetbase));'+
  682. 'i:=__val.nextSetBit(i+1);'+
  683. 'end '+
  684. 'end;',
  685. pd,true);
  686. end;
  687. procedure implement_jvm_enum_set2set(pd: tprocdef);
  688. begin
  689. str_parse_method_impl(
  690. 'var '+
  691. 'it: JUIterator;'+
  692. 'ele: FpcEnumValueObtainable;'+
  693. 'i: longint;'+
  694. 'begin '+
  695. 'result:=JUEnumSet.noneOf(JLClass(__FPC_TEnumClassAlias));'+
  696. 'it:=__val.iterator;'+
  697. 'while it.hasNext do '+
  698. 'begin '+
  699. 'ele:=FpcEnumValueObtainable(it.next);'+
  700. 'i:=ele.fpcOrdinal-__fromsetbase;'+
  701. 'result.add(fpcValueOf(i+__tosetbase));'+
  702. 'end '+
  703. 'end;',
  704. pd,true);
  705. end;
  706. procedure implement_jvm_procvar_invoke(pd: tprocdef);
  707. var
  708. pvclass: tobjectdef;
  709. procvar: tprocvardef;
  710. paraname,str,endstr: ansistring;
  711. pvs: tparavarsym;
  712. paradef,boxdef,boxargdef: tdef;
  713. i: longint;
  714. firstpara: boolean;
  715. begin
  716. pvclass:=tobjectdef(pd.owner.defowner);
  717. procvar:=tprocvardef(ttypesym(search_struct_member(pvclass,'__FPC_PROCVARALIAS')).typedef);
  718. { the procvar wrapper class has a tmethod member called "method", whose
  719. "code" field is a JLRMethod, and whose "data" field is the self pointer
  720. if any (if none is required, it's ignored by the JVM, so there's no
  721. problem with always passing it) }
  722. { force extended syntax to allow calling invokeObjectFunc() without using
  723. its result }
  724. str:='';
  725. endstr:='';
  726. { create local pointer to result type for typecasting in case of an
  727. implicit pointer type }
  728. if jvmimplicitpointertype(procvar.returndef) then
  729. str:=str+'type __FPC_returnptrtype = ^'+procvar.returndef.typename+';';
  730. str:=str+'begin ';
  731. { result handling (skip for generic definitions, we'll generate a new
  732. version for the specialized definition) ) }
  733. if not is_void(procvar.returndef) and
  734. (procvar.returndef.typ<>undefineddef) then
  735. begin
  736. str:=str+'invoke:=';
  737. if procvar.returndef.typ in [orddef,floatdef] then
  738. begin
  739. { primitivetype(boxtype(..).unboxmethod) }
  740. jvmgetboxtype(procvar.returndef,boxdef,boxargdef,false);
  741. str:=str+procvar.returndef.typename+'('+boxdef.typename+'(';
  742. endstr:=').'+jvmgetunboxmethod(procvar.returndef)+')';
  743. end
  744. else if jvmimplicitpointertype(procvar.returndef) then
  745. begin
  746. str:=str+'__FPC_returnptrtype(';
  747. { dereference }
  748. endstr:=')^';
  749. end
  750. else
  751. begin
  752. str:=str+procvar.returndef.typename+'(';
  753. endstr:=')';
  754. end;
  755. end;
  756. str:=str+'invokeObjectFunc([';
  757. { parameters are a constant array of jlobject }
  758. firstpara:=true;
  759. for i:=0 to procvar.paras.count-1 do
  760. begin
  761. { skip self/vmt/parentfp, passed separately }
  762. pvs:=tparavarsym(procvar.paras[i]);
  763. if ([vo_is_self,vo_is_vmt,vo_is_parentfp]*pvs.varoptions)<>[] then
  764. continue;
  765. if not firstpara then
  766. str:=str+',';
  767. firstpara:=false;
  768. paraname:=pvs.realname;
  769. paradef:=pvs.vardef;
  770. { Pascalize hidden high parameter }
  771. if vo_is_high_para in pvs.varoptions then
  772. paraname:='high('+tparavarsym(procvar.paras[i-1]).realname+')'
  773. else if vo_is_hidden_para in pvs.varoptions then
  774. begin
  775. if ([vo_is_range_check,vo_is_overflow_check]*pvs.varoptions)<>[] then
  776. { ok, simple boolean parameters }
  777. else
  778. internalerror(2011072403);
  779. end;
  780. { var/out/constref parameters -> pass address through (same for
  781. implicit pointer types) }
  782. if paramanager.push_copyout_param(pvs.varspez,paradef,procvar.proccalloption) or
  783. jvmimplicitpointertype(paradef) then
  784. begin
  785. paraname:='@'+paraname;
  786. paradef:=java_jlobject;
  787. end;
  788. if paradef.typ in [orddef,floatdef] then
  789. begin
  790. { box primitive types; use valueOf() rather than create because it
  791. can give better performance }
  792. jvmgetboxtype(paradef,boxdef,boxargdef,false);
  793. str:=str+boxdef.typename+'.valueOf('+boxargdef.typename+'('+paraname+'))'
  794. end
  795. else
  796. str:=str+'JLObject('+paraname+')';
  797. end;
  798. str:=str+'])'+endstr+' end;';
  799. str_parse_method_impl(str,pd,false)
  800. end;
  801. procedure implement_jvm_procvar_intconstr(pd: tprocdef);
  802. var
  803. pvdef: tprocvardef;
  804. begin
  805. { ideal, and most performant, would be to keep the interface instance
  806. passed to the constructor around and always call its method directly
  807. rather than working via reflection. Unfortunately, the procvar semantics
  808. that allow directly modifying the procvar via typecasting it to a
  809. tmethod make this very hard.
  810. So for now we simply take the address of the interface instance's
  811. method and assign it to the tmethod of this procvar }
  812. pvdef:=tprocvardef(pd.skpara);
  813. str_parse_method_impl('begin method:=System.TMethod(@__intf.'+pvdef.typesym.RealName+'Callback) end;',pd,false);
  814. end;
  815. procedure implement_jvm_virtual_clmethod(pd: tprocdef);
  816. var
  817. str: ansistring;
  818. callpd: tprocdef;
  819. begin
  820. callpd:=tprocdef(pd.skpara);
  821. str:='var pv: __fpc_virtualclassmethod_pv_t'+pd.unique_id_str+'; begin '
  822. + 'pv:=@'+callpd.procsym.RealName+';';
  823. if (pd.proctypeoption<>potype_constructor) and
  824. not is_void(pd.returndef) then
  825. str:=str+'result:=';
  826. str:=str+'pv(';
  827. addvisibleparameters(str,pd);
  828. str:=str+') end;';
  829. str_parse_method_impl(str,pd,true)
  830. end;
  831. {$endif jvm}
  832. {$ifdef wasm}
  833. procedure addvisibleparameterdeclarations(var str: ansistring; pd: tprocdef);
  834. var
  835. currpara: tparavarsym;
  836. i: longint;
  837. firstpara: boolean;
  838. begin
  839. firstpara:=true;
  840. for i:=0 to pd.paras.count-1 do
  841. begin
  842. currpara:=tparavarsym(pd.paras[i]);
  843. if not(vo_is_hidden_para in currpara.varoptions) then
  844. begin
  845. if not firstpara then
  846. str:=str+';';
  847. firstpara:=false;
  848. case currpara.varspez of
  849. vs_constref:
  850. str:=str+'constref ';
  851. vs_out:
  852. str:=str+'out ';
  853. vs_var:
  854. str:=str+'var ';
  855. vs_const:
  856. str:=str+'const ';
  857. vs_value:
  858. ;
  859. else
  860. internalerror(2023061108);
  861. end;
  862. str:=str+currpara.realname;
  863. if currpara.vardef.typ<>formaldef then
  864. str:=str+':'+currpara.vardef.fulltypename;
  865. end;
  866. end;
  867. end;
  868. procedure implement_wasm_suspending(pd: tcpuprocdef; last: Boolean);
  869. var
  870. str: ansistring;
  871. wrapper_name: ansistring;
  872. begin
  873. wrapper_name:=pd.suspending_wrapper_name;
  874. if is_void(pd.returndef) then
  875. str:='procedure '
  876. else
  877. str:='function ';
  878. str:=str+wrapper_name+'(';
  879. if last then
  880. begin
  881. addvisibleparameterdeclarations(str,pd);
  882. if str[Length(str)]<>'(' then
  883. str:=str+';';
  884. str:=str+'__fpc_wasm_susp: WasmExternRef';
  885. end
  886. else
  887. begin
  888. str:=str+'__fpc_wasm_susp: WasmExternRef;';
  889. addvisibleparameterdeclarations(str,pd);
  890. if str[Length(str)]=';' then
  891. delete(str,Length(str),1);
  892. end;
  893. str:=str+')';
  894. if not is_void(pd.returndef) then
  895. str:=str+': '+pd.returndef.fulltypename;
  896. str:=str+'; external '''+pd.import_dll^+ ''' name '''+pd.import_name^+''';';
  897. str_parse_method_impl(str,nil,false);
  898. str:='var __fpc_wasm_suspender_copy:WasmExternRef; begin __fpc_wasm_suspender_copy:=__fpc_wasm_suspender; ';
  899. if not is_void(pd.returndef) then
  900. str:=str+' result:=';
  901. str:=str+wrapper_name+'(__fpc_wasm_suspender_copy,';
  902. addvisibleparameters(str,pd);
  903. if str[Length(str)]=',' then
  904. delete(str,Length(str),1);
  905. str:=str+');';
  906. str:=str+' __fpc_wasm_suspender:=__fpc_wasm_suspender_copy;';
  907. str:=str+' end;';
  908. str_parse_method_impl(str,pd,false);
  909. exclude(pd.procoptions,po_external);
  910. end;
  911. function implement_wasm_promising_wrapper(pd: tcpuprocdef;last:boolean):tprocdef;
  912. var
  913. str: ansistring;
  914. wrapper_name: ansistring;
  915. begin
  916. wrapper_name:=pd.promising_wrapper_name(last);
  917. if is_void(pd.returndef) then
  918. str:='procedure '
  919. else
  920. str:='function ';
  921. str:=str+wrapper_name+'(';
  922. if last then
  923. begin
  924. addvisibleparameterdeclarations(str,pd);
  925. if str[Length(str)]<>'(' then
  926. str:=str+';';
  927. str:=str+'__fpc_wasm_susp: WasmExternRef';
  928. end
  929. else
  930. begin
  931. str:=str+'__fpc_wasm_susp: WasmExternRef;';
  932. addvisibleparameterdeclarations(str,pd);
  933. if str[Length(str)]=';' then
  934. delete(str,Length(str),1);
  935. end;
  936. str:=str+')';
  937. if not is_void(pd.returndef) then
  938. str:=str+': '+pd.returndef.fulltypename;
  939. str:=str+'; begin __fpc_wasm_suspender:=__fpc_wasm_susp;';
  940. if not is_void(pd.returndef) then
  941. str:=str+' result:=';
  942. str:=str+pd.procsym.RealName+'(';
  943. addvisibleparameters(str,pd);
  944. if str[Length(str)]=',' then
  945. delete(str,Length(str),1);
  946. str:=str+'); end;';
  947. str_parse_method_impl(str,nil,false,result);
  948. end;
  949. procedure implement_wasm_promising(pd: tcpuprocdef);
  950. var
  951. new_wrapper_pd: tprocdef;
  952. begin
  953. if pd.promising_first_export_name<>'' then
  954. begin
  955. new_wrapper_pd:=implement_wasm_promising_wrapper(pd,false);
  956. current_asmdata.asmlists[al_exports].Concat(tai_export_name.create(pd.promising_first_export_name,new_wrapper_pd.mangledname,ie_Func));
  957. end;
  958. if pd.promising_last_export_name<>'' then
  959. begin
  960. new_wrapper_pd:=implement_wasm_promising_wrapper(pd,true);
  961. current_asmdata.asmlists[al_exports].Concat(tai_export_name.create(pd.promising_last_export_name,new_wrapper_pd.mangledname,ie_Func));
  962. end;
  963. end;
  964. {$endif wasm}
  965. procedure implement_field_getter(pd: tprocdef);
  966. var
  967. i: longint;
  968. pvs: tparavarsym;
  969. str: ansistring;
  970. callthroughprop: tpropertysym;
  971. propaccesslist: tpropaccesslist;
  972. lastparanr: longint;
  973. firstpara: boolean;
  974. begin
  975. callthroughprop:=tpropertysym(pd.skpara);
  976. str:='begin result:='+callthroughprop.realname;
  977. if ppo_hasparameters in callthroughprop.propoptions then
  978. begin
  979. if not callthroughprop.getpropaccesslist(palt_read,propaccesslist) then
  980. internalerror(2012100701);
  981. str:=str+'[';
  982. firstpara:=true;
  983. lastparanr:=tprocdef(propaccesslist.procdef).paras.count-1;
  984. if ppo_indexed in callthroughprop.propoptions then
  985. dec(lastparanr);
  986. for i:=0 to lastparanr do
  987. begin
  988. { skip self/vmt/parentfp, passed implicitly }
  989. pvs:=tparavarsym(tprocdef(propaccesslist.procdef).paras[i]);
  990. if ([vo_is_self,vo_is_vmt,vo_is_parentfp]*pvs.varoptions)<>[] then
  991. continue;
  992. if not firstpara then
  993. str:=str+',';
  994. firstpara:=false;
  995. str:=str+pvs.realname;
  996. end;
  997. str:=str+']';
  998. end;
  999. str:=str+'; end;';
  1000. str_parse_method_impl(str,pd,po_classmethod in pd.procoptions)
  1001. end;
  1002. procedure implement_field_setter(pd: tprocdef);
  1003. var
  1004. i, lastparaindex: longint;
  1005. pvs: tparavarsym;
  1006. paraname, str: ansistring;
  1007. callthroughprop: tpropertysym;
  1008. propaccesslist: tpropaccesslist;
  1009. firstpara: boolean;
  1010. begin
  1011. callthroughprop:=tpropertysym(pd.skpara);
  1012. str:='begin '+callthroughprop.realname;
  1013. if not callthroughprop.getpropaccesslist(palt_write,propaccesslist) then
  1014. internalerror(2012100702);
  1015. if ppo_hasparameters in callthroughprop.propoptions then
  1016. begin
  1017. str:=str+'[';
  1018. firstpara:=true;
  1019. { last parameter is the value to be set, skip (only add index
  1020. parameters here) }
  1021. lastparaindex:=tprocdef(propaccesslist.procdef).paras.count-2;
  1022. if ppo_indexed in callthroughprop.propoptions then
  1023. dec(lastparaindex);
  1024. for i:=0 to lastparaindex do
  1025. begin
  1026. { skip self/vmt/parentfp/index, passed implicitly }
  1027. pvs:=tparavarsym(tprocdef(propaccesslist.procdef).paras[i]);
  1028. if ([vo_is_self,vo_is_vmt,vo_is_parentfp]*pvs.varoptions)<>[] then
  1029. continue;
  1030. if not firstpara then
  1031. str:=str+',';
  1032. firstpara:=false;
  1033. str:=str+pvs.realname;
  1034. end;
  1035. str:=str+']';
  1036. end;
  1037. { the value-to-be-set }
  1038. if assigned(propaccesslist.procdef) then
  1039. begin
  1040. pvs:=tparavarsym(tprocdef(propaccesslist.procdef).paras[tprocdef(propaccesslist.procdef).paras.count-1]);
  1041. paraname:=pvs.realname;
  1042. end
  1043. else
  1044. paraname:='__fpc_newval__';
  1045. str:=str+':='+paraname+'; end;';
  1046. str_parse_method_impl(str,pd,po_classmethod in pd.procoptions)
  1047. end;
  1048. procedure implement_block_invoke_procvar(pd: tprocdef);
  1049. var
  1050. str: ansistring;
  1051. begin
  1052. str:='';
  1053. str:='begin ';
  1054. if pd.returndef<>voidtype then
  1055. str:=str+'result:=';
  1056. str:=str+'__FPC_BLOCK_INVOKE_PV_TYPE(PFPC_Block_literal_complex_procvar(FPC_Block_Self)^.pv)(';
  1057. addvisibleparameters(str,pd);
  1058. str:=str+') end;';
  1059. str_parse_method_impl(str,pd,false);
  1060. end;
  1061. procedure implement_interface_wrapper(pd: tprocdef);
  1062. var
  1063. wrapperinfo: pskpara_interface_wrapper;
  1064. callthroughpd, tmpproc: tprocdef;
  1065. str: ansistring;
  1066. fileinfo: tfileposinfo;
  1067. begin
  1068. wrapperinfo:=pskpara_interface_wrapper(pd.skpara);
  1069. if not assigned(wrapperinfo) then
  1070. internalerror(2015090801);
  1071. callthroughpd:=tprocdef(wrapperinfo^.pd);
  1072. str:='begin ';
  1073. { self right now points to the VMT of interface inside the instance ->
  1074. adjust so it points to the start of the instance }
  1075. str:=str+'pointer(self):=pointer(self) - '+tostr(wrapperinfo^.offset)+';';
  1076. { now call through to the actual method }
  1077. if pd.returndef<>voidtype then
  1078. str:=str+'result:=';
  1079. str:=str+'&'+callthroughpd.procsym.realname+'(';
  1080. addvisibleparameters(str,pd);
  1081. str:=str+') end;';
  1082. { add dummy file info so we can step in/through it }
  1083. if pd.owner.iscurrentunit then
  1084. fileinfo:=pd.fileinfo
  1085. else
  1086. begin
  1087. fileinfo.moduleindex:=current_module.moduleid;
  1088. fileinfo.fileindex:=1;
  1089. fileinfo.line:=1;
  1090. fileinfo.column:=1;
  1091. end;
  1092. str_parse_method_impl_with_fileinfo(str,pd,fileinfo.fileindex,fileinfo.line,false,tmpproc);
  1093. dispose(wrapperinfo);
  1094. pd.skpara:=nil;
  1095. end;
  1096. procedure implement_call_no_parameters(pd: tprocdef);
  1097. var
  1098. callpd: tprocdef;
  1099. str: ansistring;
  1100. warningson,
  1101. isclassmethod: boolean;
  1102. begin
  1103. { avoid warnings about unset function results in these abstract wrappers }
  1104. warningson:=(status.verbosity and V_Warning)<>0;
  1105. setverbosity('W-');
  1106. str:='begin ';
  1107. callpd:=tprocdef(pd.skpara);
  1108. str:=str+def_unit_name_prefix_if_toplevel(callpd)+callpd.procsym.realname+'; end;';
  1109. isclassmethod:=
  1110. (po_classmethod in pd.procoptions) and
  1111. not(pd.proctypeoption in [potype_constructor,potype_destructor]);
  1112. str_parse_method_impl(str,pd,isclassmethod);
  1113. if warningson then
  1114. setverbosity('W+');
  1115. end;
  1116. function get_method_paramtype(vardef : Tdef; asPointer : Boolean; out isAnonymousArrayDef : Boolean) : ansistring; forward;
  1117. function str_parse_method(str: ansistring; allowgenericid : boolean): tprocdef; forward;
  1118. function str_parse_method(str: ansistring): tprocdef;
  1119. begin
  1120. Result:=str_parse_method(str,false);
  1121. end;
  1122. procedure implement_invoke_helper(cn : string;pd: tprocdef; idx : integer);
  1123. var
  1124. sarg,str : ansistring;
  1125. pt, pn,d : shortstring;
  1126. sym : tsym;
  1127. aArg,argcount,i : integer;
  1128. isarray,haveresult : boolean;
  1129. para : tparavarsym;
  1130. hasopenarray, washigh: Boolean;
  1131. begin
  1132. str:='procedure __invoke_helper__';
  1133. pn:=pd.procsym.realname;
  1134. str:=str+cn+'__'+pn+'_'+tostr(idx);
  1135. for I:=1 to length(str) do
  1136. if str[i]='.' then
  1137. str[i]:='_';
  1138. str:=str+'(Instance : Pointer; Args : PPointer);'#10;
  1139. argCount:=0;
  1140. for i:=0 to pd.paras.Count-1 do
  1141. begin
  1142. para:=tparavarsym(pd.paras[i]);
  1143. if vo_is_hidden_para in para.varoptions then
  1144. continue;
  1145. inc(argCount);
  1146. if argCount=1 then
  1147. str:=str+'Type'#10;
  1148. pt:=get_method_paramtype(para.vardef,true,isArray);
  1149. if isArray then
  1150. begin
  1151. str:=str+' tpa'+tostr(argcount)+' = '+pt+';'#10;
  1152. pt:='^tpa'+tostr(argcount);
  1153. end;
  1154. str:=str+' tp'+tostr(argcount)+' = '+pt+';'#10;
  1155. end;
  1156. haveresult:=pd.returndef<>voidtype;
  1157. if haveresult then
  1158. begin
  1159. if argCount=0 then
  1160. str:=str+'Type'#10;
  1161. pt:=get_method_paramtype(pd.returndef ,true,isArray);
  1162. if isArray then
  1163. begin
  1164. str:=str+' tra'+tostr(argcount)+' = '+pt+';'#10;
  1165. pt:='^tra';
  1166. end;
  1167. str:=str+' tr = '+pt+';'#10;
  1168. end;
  1169. str:=str+'begin'#10' ';
  1170. if haveResult then
  1171. str:=str+'TR(args[0])^:=';
  1172. str:=str+cn+'(Instance).'+pn+'(';
  1173. argCount:=0;
  1174. for i:=0 to pd.paras.Count-1 do
  1175. begin
  1176. para:=tparavarsym(pd.paras[i]);
  1177. if vo_is_hidden_para in para.varoptions then
  1178. continue;
  1179. inc(argCount);
  1180. sarg:=tostr(argcount);
  1181. if argCount>1 then
  1182. str:=str+',';
  1183. str:=str+'tp'+sarg+'(Args['+sarg+'])^';
  1184. end;
  1185. str:=str+');'#10;
  1186. str:=str+'end;'#10;
  1187. pd.invoke_helper:=str_parse_method(str,true);
  1188. end;
  1189. procedure add_synthetic_method_implementations_for_st(st: tsymtable);
  1190. var
  1191. i : longint;
  1192. def : tdef;
  1193. pd : tprocdef;
  1194. cn : shortstring;
  1195. begin
  1196. for i:=0 to st.deflist.count-1 do
  1197. begin
  1198. def:=tdef(st.deflist[i]);
  1199. if (def.typ<>procdef) then
  1200. continue;
  1201. { skip methods when processing unit symtable }
  1202. if def.owner<>st then
  1203. continue;
  1204. pd:=tprocdef(def);
  1205. case pd.synthetickind of
  1206. tsk_none:
  1207. ;
  1208. tsk_anon_inherited:
  1209. implement_anon_inherited(pd);
  1210. tsk_jvm_clone:
  1211. implement_jvm_clone(pd);
  1212. tsk_record_deepcopy:
  1213. implement_record_deepcopy(pd);
  1214. tsk_record_initialize:
  1215. implement_record_initialize(pd);
  1216. tsk_empty,
  1217. { special handling for this one is done in tnodeutils.wrap_proc_body }
  1218. tsk_tcinit:
  1219. implement_empty(pd);
  1220. tsk_callthrough:
  1221. implement_callthrough(pd);
  1222. tsk_callthrough_nonabstract:
  1223. begin
  1224. if (pd.owner.defowner.typ<>objectdef) or
  1225. (tobjectdef(pd.owner.defowner).abstractcnt=0) then
  1226. implement_callthrough(pd)
  1227. else
  1228. implement_empty(pd);
  1229. end;
  1230. {$ifdef jvm}
  1231. tsk_jvm_enum_values:
  1232. implement_jvm_enum_values(pd);
  1233. tsk_jvm_enum_valueof:
  1234. implement_jvm_enum_valuof(pd);
  1235. tsk_jvm_enum_classconstr:
  1236. implement_jvm_enum_classconstr(pd);
  1237. tsk_jvm_enum_jumps_constr:
  1238. implement_jvm_enum_jumps_constr(pd);
  1239. tsk_jvm_enum_fpcordinal:
  1240. implement_jvm_enum_fpcordinal(pd);
  1241. tsk_jvm_enum_fpcvalueof:
  1242. implement_jvm_enum_fpcvalueof(pd);
  1243. tsk_jvm_enum_long2set:
  1244. implement_jvm_enum_long2set(pd);
  1245. tsk_jvm_enum_bitset2set:
  1246. implement_jvm_enum_bitset2set(pd);
  1247. tsk_jvm_enum_set2set:
  1248. implement_jvm_enum_set2set(pd);
  1249. tsk_jvm_procvar_invoke:
  1250. implement_jvm_procvar_invoke(pd);
  1251. tsk_jvm_procvar_intconstr:
  1252. implement_jvm_procvar_intconstr(pd);
  1253. tsk_jvm_virtual_clmethod:
  1254. implement_jvm_virtual_clmethod(pd);
  1255. {$else}
  1256. tsk_jvm_enum_values,
  1257. tsk_jvm_enum_valueof,
  1258. tsk_jvm_enum_classconstr,
  1259. tsk_jvm_enum_jumps_constr,
  1260. tsk_jvm_enum_fpcordinal,
  1261. tsk_jvm_enum_fpcvalueof,
  1262. tsk_jvm_enum_long2set,
  1263. tsk_jvm_enum_bitset2set,
  1264. tsk_jvm_enum_set2set,
  1265. tsk_jvm_procvar_invoke,
  1266. tsk_jvm_procvar_intconstr,
  1267. tsk_jvm_virtual_clmethod:
  1268. internalerror(2011032801);
  1269. {$endif jvm}
  1270. {$ifdef wasm}
  1271. tsk_wasm_suspending_first:
  1272. implement_wasm_suspending(tcpuprocdef(pd),false);
  1273. tsk_wasm_suspending_last:
  1274. implement_wasm_suspending(tcpuprocdef(pd),true);
  1275. tsk_wasm_promising:
  1276. implement_wasm_promising(tcpuprocdef(pd));
  1277. {$else wasm}
  1278. tsk_wasm_suspending_first,
  1279. tsk_wasm_suspending_last,
  1280. tsk_wasm_promising:
  1281. internalerror(2023061107);
  1282. {$endif wasm}
  1283. tsk_field_getter:
  1284. implement_field_getter(pd);
  1285. tsk_field_setter:
  1286. implement_field_setter(pd);
  1287. tsk_block_invoke_procvar:
  1288. implement_block_invoke_procvar(pd);
  1289. tsk_interface_wrapper:
  1290. implement_interface_wrapper(pd);
  1291. tsk_call_no_parameters:
  1292. implement_call_no_parameters(pd);
  1293. tsk_invoke_helper:
  1294. begin
  1295. if (pd.owner.defowner) is tobjectdef then
  1296. cn:=tobjectdef(def.owner.defowner).GetTypeName
  1297. else
  1298. internalerror(2023061107);
  1299. implement_invoke_helper(cn,pd,i);
  1300. end;
  1301. end;
  1302. end;
  1303. end;
  1304. function get_method_paramtype(vardef : Tdef; asPointer : Boolean; out isAnonymousArrayDef : Boolean) : ansistring;
  1305. var
  1306. p : integer;
  1307. arrdef : tarraydef absolute vardef;
  1308. begin
  1309. {
  1310. None of the existing routines fulltypename,OwnerHierarchyName,FullOwnerHierarchyName,typename
  1311. results in a workable definition for open array parameters.
  1312. }
  1313. isAnonymousArrayDef:=false;
  1314. if asPointer and (vardef.typ=formaldef) then
  1315. exit('pointer');
  1316. if (vardef is tprocvardef) then
  1317. begin
  1318. result:=vardef.fullownerhierarchyname(false);
  1319. if Assigned(vardef.typesym) then
  1320. Result:=Result+(vardef.typesym.Name);
  1321. end
  1322. else if not (vardef is tarraydef) then
  1323. result:=vardef.fulltypename
  1324. else
  1325. begin
  1326. if (ado_isarrayofconst in arrdef.arrayoptions) then
  1327. begin
  1328. if asPointer then
  1329. Result:='Array of TVarRec'
  1330. else
  1331. result:='Array Of Const';
  1332. asPointer:=False;
  1333. isAnonymousArrayDef:=true;
  1334. end
  1335. else if (ado_OpenArray in arrdef.arrayoptions) then
  1336. begin
  1337. result:='Array of '+arrdef.elementdef.fulltypename;
  1338. asPointer:=False;
  1339. isAnonymousArrayDef:=true;
  1340. end
  1341. else
  1342. begin
  1343. result:=vardef.fulltypename;
  1344. end;
  1345. end;
  1346. // ansistring(0) -> ansistring
  1347. p:=pos('(',result);
  1348. if p=0 then
  1349. p:=pos('[',result);
  1350. if p>0 then
  1351. result:=copy(result,1,p-1);
  1352. if asPointer then
  1353. Result:='^'+Result;
  1354. end;
  1355. function get_method_paramtype(vardef : Tdef; asPointer : Boolean) : ansistring;
  1356. var
  1357. ad : boolean;
  1358. begin
  1359. result:=get_method_paramtype(vardef,aspointer,ad);
  1360. end;
  1361. function create_intf_method_args(p : tprocdef; out argcount: integer) : ansistring;
  1362. const
  1363. varspezprefixes : array[tvarspez] of shortstring =
  1364. ('','const','var','out','constref','final');
  1365. var
  1366. i : integer;
  1367. s : string;
  1368. para : tparavarsym;
  1369. begin
  1370. result:='';
  1371. argCount:=0;
  1372. for i:=0 to p.paras.Count-1 do
  1373. begin
  1374. para:=tparavarsym(p.paras[i]);
  1375. if vo_is_hidden_para in para.varoptions then
  1376. continue;
  1377. if Result<>'' then
  1378. Result:=Result+';';
  1379. inc(argCount);
  1380. result:=result+varspezprefixes[para.varspez]+' p'+tostr(argcount);
  1381. if Assigned(para.vardef) and not (para.vardef is tformaldef) then
  1382. result:=Result+' : '+get_method_paramtype(para.vardef,false);
  1383. end;
  1384. if Result<>'' then
  1385. Result:='('+Result+')';
  1386. end;
  1387. function generate_thunkclass_name(acount: Integer; objdef : tobjectdef) : shortstring;
  1388. var
  1389. cn : shortstring;
  1390. i : integer;
  1391. begin
  1392. cn:=ObjDef.GetTypeName;
  1393. for i:=0 to Length(cn) do
  1394. begin
  1395. if cn[i]='.' then
  1396. cn[i]:='_'
  1397. else if (cn[i]='<') or (cn[i]='>') then
  1398. cn[i]:='_';
  1399. end;
  1400. result:='_t_hidden'+tostr(acount)+cn;
  1401. end;
  1402. function get_thunkclass_interface_vmtoffset(objdef : tobjectdef) : integer;
  1403. var
  1404. i,j,offs : integer;
  1405. sym : tsym;
  1406. proc : tprocsym absolute sym;
  1407. pd : tprocdef;
  1408. begin
  1409. offs:=maxint;
  1410. for I:=0 to objdef.symtable.symList.Count-1 do
  1411. begin
  1412. sym:=tsym(objdef.symtable.symList[i]);
  1413. if Not assigned(sym) then
  1414. continue;
  1415. if (Sym.typ<>procsym) then
  1416. continue;
  1417. for j:=0 to proc.ProcdefList.Count-1 do
  1418. begin
  1419. pd:=tprocdef(proc.ProcdefList[j]);
  1420. if pd.extnumber<offs then
  1421. offs:=pd.extnumber;
  1422. end;
  1423. end;
  1424. if offs=maxint then
  1425. offs:=0;
  1426. result:=offs;
  1427. end;
  1428. // return parent Interface def, but skip iunknown.
  1429. function getparent_interface_def(odef : tobjectdef) : tobjectdef;
  1430. begin
  1431. if (odef.getparentdef is tobjectdef) then
  1432. begin
  1433. result:=odef.getparentdef as tobjectdef;
  1434. if result=interface_iunknown then
  1435. result:=Nil;
  1436. end
  1437. else
  1438. result:=nil;
  1439. end;
  1440. procedure implement_interface_thunkclass_decl(cn : shortstring; objdef : tobjectdef);
  1441. var
  1442. parentname,str : ansistring;
  1443. sym : tsym;
  1444. proc : tprocsym absolute sym;
  1445. pd : tprocdef;
  1446. odef,def : tobjectdef;
  1447. offs,argcount,i,j : integer;
  1448. intfDef : tobjectdef;
  1449. begin
  1450. str:='type '#10;
  1451. odef:=getparent_interface_def(objdef);
  1452. if (oDef=Nil) or (oDef.hiddenclassdef=Nil) then
  1453. parentname:='TInterfaceThunk'
  1454. else
  1455. parentname:=odef.hiddenclassdef.GetTypeName;
  1456. str:=str+cn+' = class('+parentname+','+objdef.GetTypeName+')'#10;
  1457. str:=str+' protected '#10;
  1458. Intfdef:=objdef;
  1459. Repeat
  1460. if not IntfDef.is_generic then
  1461. for I:=0 to intfdef.symtable.symList.Count-1 do
  1462. begin
  1463. sym:=tsym(intfdef.symtable.symList[i]);
  1464. if Not assigned(sym) then
  1465. continue;
  1466. if (Sym.typ<>procsym) then
  1467. continue;
  1468. for j:=0 to proc.ProcdefList.Count-1 do
  1469. begin
  1470. pd:=tprocdef(proc.ProcdefList[j]);
  1471. if pd.returndef<>voidtype then
  1472. str:=str+'function '
  1473. else
  1474. str:=str+'procedure ';
  1475. str:=str+proc.RealName;
  1476. str:=str+create_intf_method_args(pd,argcount);
  1477. if pd.returndef<>voidtype then
  1478. str:=str+' : '+get_method_paramtype(pd.returndef,false);
  1479. str:=str+';'#10;
  1480. end;
  1481. end;
  1482. // Check parent class
  1483. intfdef:=getparent_interface_def(intfdef);
  1484. // If we already have a hidden class def for it, no need to continue
  1485. if (IntfDef<>nil) and (IntfDef.hiddenclassdef<>nil) then
  1486. IntfDef:=Nil;
  1487. until intfdef=nil;
  1488. offs:=get_thunkclass_interface_vmtoffset(objdef);
  1489. if offs>0 then
  1490. begin
  1491. str:=str+'public '#10;
  1492. str:=str+' function InterfaceVMTOffset : word; override;'#10;
  1493. end;
  1494. str:=str+' end;'#10;
  1495. def:=str_parse_objecttypedef(cn,str);
  1496. if assigned(def) then
  1497. begin
  1498. def.created_in_current_module:=true;
  1499. if not def.typesym.is_registered then
  1500. def.typesym.register_sym;
  1501. def.buildderef;
  1502. include(def.objectoptions,oo_can_have_published);
  1503. end;
  1504. objdef.hiddenclassdef:=def;
  1505. end;
  1506. function str_parse_method(str: ansistring; allowgenericid : boolean): tprocdef;
  1507. var
  1508. oldparse_only: boolean;
  1509. tmpstr: ansistring;
  1510. flags : tread_proc_flags;
  1511. oldallow : boolean;
  1512. begin
  1513. Message1(parser_d_internal_parser_string,str);
  1514. oldparse_only:=parse_only;
  1515. parse_only:=false;
  1516. { "const" starts a new kind of block and hence makes the scanner return }
  1517. str:=str+'const;';
  1518. block_type:=bt_none;
  1519. { inject the string in the scanner }
  1520. oldallow:=current_scanner.allowgenericid;
  1521. current_scanner.allowgenericid:=allowgenericid;
  1522. current_scanner.substitutemacro('hidden_interface_method',@str[1],length(str),current_scanner.line_no,current_scanner.inputfile.ref_index,true);
  1523. current_scanner.readtoken(false);
  1524. Result:=read_proc([],Nil);
  1525. parse_only:=oldparse_only;
  1526. { remove the temporary macro input file again }
  1527. current_scanner.closeinputfile;
  1528. current_scanner.nextfile;
  1529. current_scanner.tempopeninputfile;
  1530. current_scanner.allowgenericid:=oldallow;
  1531. end;
  1532. procedure implement_interface_thunkclass_impl_method(cn : shortstring; objdef : tobjectdef; proc : tprocsym; pd : tprocdef);
  1533. var
  1534. rest,str : ansistring;
  1535. pn,d : shortstring;
  1536. sym : tsym;
  1537. aArg,argcount,i : integer;
  1538. haveresult : boolean;
  1539. para : tparavarsym;
  1540. hasopenarray, washigh: Boolean;
  1541. begin
  1542. rest:='';
  1543. str:='';
  1544. if pd.returndef<>voidtype then
  1545. str:=str+'function '
  1546. else
  1547. str:=str+'procedure ';
  1548. pn:=proc.RealName;
  1549. str:=str+cn+'.'+pn;
  1550. str:=str+create_intf_method_args(pd,argcount);
  1551. haveresult:=pd.returndef<>voidtype;
  1552. if haveresult then
  1553. begin
  1554. rest:=get_method_paramtype(pd.returndef,false);
  1555. str:=str+' : '+rest;
  1556. end;
  1557. str:=str+';'#10;
  1558. str:=str+'var '#10;
  1559. str:=str+' data : array[0..'+tostr(argcount)+'] of System.TInterfaceThunk.TArgData;'#10;
  1560. if haveresult then
  1561. str:=str+' res : '+rest+';'#10;
  1562. str:=str+'begin'#10;
  1563. // initialize result.
  1564. if HaveResult then
  1565. begin
  1566. str:=Str+' data[0].addr:=@Res;'#10;
  1567. str:=Str+' data[0].info:=TypeInfo(Res);'#10;
  1568. end
  1569. else
  1570. begin
  1571. str:=Str+' data[0].addr:=nil;'#10;
  1572. str:=Str+' data[0].idx:=-1;'#10;
  1573. end;
  1574. str:=Str+' data[0].idx:=-1;'#10;
  1575. str:=Str+' data[0].ahigh:=-1;'#10;
  1576. // Fill rest of data
  1577. aArg:=0;
  1578. washigh:=false;
  1579. d:='0';
  1580. for i:=0 to pd.paras.Count-1 do
  1581. begin
  1582. para:=tparavarsym(pd.paras[i]);
  1583. // previous was open array. Record high
  1584. if (i>1) then
  1585. begin
  1586. WasHigh:=(vo_is_high_para in para.varoptions);
  1587. if Washigh then
  1588. // D is still value of previous (real) parameter
  1589. str:=str+' data['+d+'].ahigh:=High(p'+d+');'#10
  1590. else
  1591. str:=str+' data['+d+'].ahigh:=-1;'#10;
  1592. end;
  1593. if vo_is_hidden_para in para.varoptions then
  1594. continue;
  1595. inc(aArg);
  1596. d:=tostr(aArg);
  1597. Str:=Str+' data['+d+'].addr:=@p'+d+';'#10;
  1598. Str:=Str+' data['+d+'].idx:='+tostr(i)+';'#10;
  1599. if Assigned(para.vardef) and not (para.vardef is tformaldef) then
  1600. Str:=Str+' data['+d+'].info:=TypeInfo(p'+d+');'#10
  1601. else
  1602. Str:=Str+' data['+d+'].info:=Nil;'#10
  1603. end;
  1604. // if last was not high, set to sentinel.
  1605. if not WasHigh then
  1606. str:=str+' data['+d+'].ahigh:=-1;'#10;
  1607. str:=str+' Thunk('+tostr(pd.extnumber)+','+tostr(argcount)+',@Data);'#10;
  1608. if HaveResult then
  1609. str:=str+' Result:=res;'#10;
  1610. str:=str+'end;'#10;
  1611. pd:=str_parse_method(str,true);
  1612. end;
  1613. procedure implement_thunkclass_interfacevmtoffset(cn : shortstring; objdef : tobjectdef; offs : integer);
  1614. var
  1615. str : ansistring;
  1616. begin
  1617. str:='function '+cn+'.InterfaceVMTOffset : word;'#10;
  1618. str:=str+'begin'#10;
  1619. str:=str+' result:='+toStr(offs)+';'#10;
  1620. str:=str+'end;'#10;
  1621. str_parse_method(str);
  1622. end;
  1623. procedure implement_interface_thunkclass_impl(cn: shortstring; objdef : tobjectdef);
  1624. var
  1625. str : ansistring;
  1626. sym : tsym;
  1627. proc : tprocsym absolute sym;
  1628. pd : tprocdef;
  1629. offs,i,j : integer;
  1630. intfDef : tobjectdef;
  1631. begin
  1632. offs:=get_thunkclass_interface_vmtoffset(objdef);
  1633. if offs>0 then
  1634. implement_thunkclass_interfacevmtoffset(cn,objdef,offs);
  1635. intfDef:=objdef;
  1636. repeat
  1637. for I:=0 to intfdef.symtable.symList.Count-1 do
  1638. begin
  1639. sym:=tsym(intfdef.symtable.symList[i]);
  1640. if Not assigned(sym) then
  1641. continue;
  1642. if (Sym.typ<>procsym) then
  1643. continue;
  1644. for j:=0 to proc.ProcdefList.Count-1 do
  1645. begin
  1646. pd:=tprocdef(proc.ProcdefList[j]);
  1647. implement_interface_thunkclass_impl_method(cn,intfdef,proc,pd);
  1648. end;
  1649. end;
  1650. // Check parent class.
  1651. intfdef:=getparent_interface_def(intfdef);
  1652. // If we already have a hidden class def for it, no need to continue
  1653. if (intfdef<>Nil) and (IntfDef.hiddenclassdef<>nil) then
  1654. IntfDef:=Nil;
  1655. until (intfdef=Nil);
  1656. end;
  1657. procedure add_synthetic_interface_classes_for_st(st : tsymtable; gen_intf, gen_impl : boolean);
  1658. var
  1659. i : longint;
  1660. def : tdef;
  1661. objdef : tobjectdef absolute def;
  1662. recdef : trecorddef absolute def;
  1663. sstate: tscannerstate;
  1664. cn : shortstring;
  1665. isDelphiMode : boolean;
  1666. oldsettings: tmodeswitches;
  1667. begin
  1668. { skip if any errors have occurred, since then this can only cause more
  1669. errors }
  1670. if ErrorCount<>0 then
  1671. exit;
  1672. isDelphiMode:=(m_delphi in current_settings.modeswitches);
  1673. replace_scanner('hiddenclass_impl',sstate);
  1674. sstate.new_scanner.allowgenericid:=true;
  1675. if isDelphiMode then
  1676. begin
  1677. oldsettings:=current_settings.modeswitches;
  1678. current_settings.modeswitches:=current_settings.modeswitches+[m_delphi]-[m_objfpc];
  1679. end;
  1680. for i:=0 to st.deflist.count-1 do
  1681. begin
  1682. def:=tdef(st.deflist[i]);
  1683. if (def.typ<>objectdef) then
  1684. continue;
  1685. if not (objdef.objecttype in objecttypes_with_thunk) then
  1686. continue;
  1687. if not (oo_can_have_published in objdef.objectoptions) then
  1688. continue;
  1689. // need to add here extended rtti check when it is available
  1690. cn:=generate_thunkclass_name(i,objdef);
  1691. if gen_intf then
  1692. implement_interface_thunkclass_decl(cn,objdef);
  1693. if gen_impl then
  1694. implement_interface_thunkclass_impl(cn,objdef);
  1695. end;
  1696. restore_scanner(sstate);
  1697. // Recurse for interfaces defined in a type section of a class/record.
  1698. for i:=0 to st.deflist.count-1 do
  1699. begin
  1700. def:=tdef(st.deflist[i]);
  1701. if (def.typ=objectdef) and (objdef.objecttype=odt_class) then
  1702. add_synthetic_interface_classes_for_st(objdef.symtable,gen_intf,gen_impl)
  1703. else if (def.typ=recorddef) and (m_advanced_records in current_settings.modeswitches) then
  1704. add_synthetic_interface_classes_for_st(recdef.symtable,gen_intf,gen_impl);
  1705. end;
  1706. end;
  1707. procedure add_synthetic_method_implementations(st: tsymtable);
  1708. var
  1709. i: longint;
  1710. def: tdef;
  1711. sstate: tscannerstate;
  1712. begin
  1713. { skip if any errors have occurred, since then this can only cause more
  1714. errors }
  1715. if ErrorCount<>0 then
  1716. exit;
  1717. replace_scanner('synthetic_impl',sstate);
  1718. add_synthetic_method_implementations_for_st(st);
  1719. for i:=0 to st.deflist.count-1 do
  1720. begin
  1721. def:=tdef(st.deflist[i]);
  1722. if (def.typ=procdef) and
  1723. assigned(tprocdef(def).localst) and
  1724. { not true for the "main" procedure, whose localsymtable is the staticsymtable }
  1725. (tprocdef(def).localst.symtabletype=localsymtable) then
  1726. add_synthetic_method_implementations(tprocdef(def).localst)
  1727. else if ((def.typ=objectdef) and
  1728. not(oo_is_external in tobjectdef(def).objectoptions) and
  1729. { we must not create duplicate synthetic methods for a unique
  1730. type declaration as that simply shares the VMT of the aliased
  1731. types }
  1732. not(tobjectdef(def).is_unique_objpasdef)) or
  1733. (def.typ=recorddef) then
  1734. begin
  1735. { also complete nested types }
  1736. add_synthetic_method_implementations(tabstractrecorddef(def).symtable);
  1737. end;
  1738. end;
  1739. restore_scanner(sstate);
  1740. end;
  1741. function create_procdef_alias(pd: tprocdef; const newrealname: string; const newmangledname: TSymStr; newparentst: tsymtable; newstruct: tabstractrecorddef;
  1742. sk: tsynthetickind; skpara: pointer): tprocdef;
  1743. begin
  1744. { bare copy so we don't copy the aliasnames (specify prefix for
  1745. parameter names so we don't get issues in the body in case
  1746. we e.g. reference system.initialize and one of the parameters
  1747. is called "system") }
  1748. result:=tprocdef(pd.getcopyas(procdef,pc_bareproc,'__FPCW_',true));
  1749. { set the mangled name to the wrapper name }
  1750. result.setmangledname(newmangledname);
  1751. { finish creating the copy }
  1752. finish_copied_procdef(result,newrealname,newparentst,newstruct);
  1753. { insert hidden high parameters }
  1754. result.parast.SymList.ForEachCall(@insert_hidden_para,result);
  1755. { now insert self/vmt }
  1756. insert_self_and_vmt_para(result);
  1757. { and the function result }
  1758. insert_funcret_para(result);
  1759. { recalculate the parameters now that we've added the missing ones }
  1760. result.calcparas;
  1761. { set the info required to generate the implementation }
  1762. result.synthetickind:=sk;
  1763. result.skpara:=skpara;
  1764. end;
  1765. procedure finish_copied_procdef(var pd: tprocdef; const realname: string; newparentst: tsymtable; newstruct: tabstractrecorddef);
  1766. var
  1767. sym: tsym;
  1768. parasym: tparavarsym;
  1769. ps: tprocsym;
  1770. stname: string;
  1771. i: longint;
  1772. begin
  1773. { add generic flag if required }
  1774. if assigned(newstruct) and
  1775. (df_generic in newstruct.defoptions) then
  1776. include(pd.defoptions,df_generic);
  1777. { associate the procdef with a procsym in the owner }
  1778. if not(pd.proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  1779. stname:=upper(realname)
  1780. else
  1781. stname:=lower(realname);
  1782. sym:=tsym(newparentst.find(stname));
  1783. if assigned(sym) then
  1784. begin
  1785. if sym.typ<>procsym then
  1786. internalerror(2011040601);
  1787. ps:=tprocsym(sym);
  1788. end
  1789. else
  1790. begin
  1791. ps:=cprocsym.create(realname);
  1792. newparentst.insertsym(ps);
  1793. end;
  1794. pd.procsym:=ps;
  1795. pd.struct:=newstruct;
  1796. { in case of methods, replace the special parameter types with new ones }
  1797. if assigned(newstruct) then
  1798. begin
  1799. symtablestack.push(pd.parast);
  1800. { may not be assigned in case we converted a procvar into a procdef }
  1801. if assigned(pd.paras) then
  1802. begin
  1803. for i:=0 to pd.paras.count-1 do
  1804. begin
  1805. parasym:=tparavarsym(pd.paras[i]);
  1806. if vo_is_self in parasym.varoptions then
  1807. begin
  1808. if parasym.vardef.typ=classrefdef then
  1809. parasym.vardef:=cclassrefdef.create(newstruct)
  1810. else
  1811. parasym.vardef:=newstruct;
  1812. end
  1813. end;
  1814. end;
  1815. { also fix returndef in case of a constructor }
  1816. if pd.proctypeoption=potype_constructor then
  1817. pd.returndef:=newstruct;
  1818. symtablestack.pop(pd.parast);
  1819. end;
  1820. pd.calcparas;
  1821. proc_add_definition(pd);
  1822. end;
  1823. function maybe_add_sym_to_parentfpstruct(pd: tprocdef; sym: tsym; vardef: tdef; addrparam: boolean): tsym;
  1824. var
  1825. fieldvardef,
  1826. nestedvarsdef: tdef;
  1827. nestedvarsst: tsymtable;
  1828. initcode: tnode;
  1829. old_filepos: tfileposinfo;
  1830. symname,
  1831. symrealname: TSymStr;
  1832. highsym: tabstractvarsym;
  1833. begin
  1834. nestedvarsdef:=tlocalvarsym(pd.parentfpstruct).vardef;
  1835. { redirect all aliases for the function result also to the function
  1836. result }
  1837. if vo_is_funcret in tabstractvarsym(sym).varoptions then
  1838. begin
  1839. symname:='result';
  1840. symrealname:='$result'
  1841. end
  1842. else
  1843. begin
  1844. symname:=sym.name;
  1845. symrealname:=sym.EscapedRealName;
  1846. end;
  1847. result:=search_struct_member(trecorddef(nestedvarsdef),symname);
  1848. if not assigned(result) then
  1849. begin
  1850. { mark that this symbol is mirrored in the parentfpstruct }
  1851. tabstractnormalvarsym(sym).inparentfpstruct:=true;
  1852. { add field to the struct holding all locals accessed
  1853. by nested routines }
  1854. nestedvarsst:=trecorddef(nestedvarsdef).symtable;
  1855. { indicate whether or not this is a var/out/constref/... parameter }
  1856. if addrparam then
  1857. fieldvardef:=cpointerdef.getreusable(vardef)
  1858. else
  1859. fieldvardef:=vardef;
  1860. result:=cfieldvarsym.create(symrealname,vs_value,fieldvardef,[]);
  1861. nestedvarsst.insertsym(result);
  1862. trecordsymtable(nestedvarsst).addfield(tfieldvarsym(result),vis_public);
  1863. { add initialization with original value if it's a parameter }
  1864. if (sym.typ=paravarsym) then
  1865. begin
  1866. old_filepos:=current_filepos;
  1867. fillchar(current_filepos,sizeof(current_filepos),0);
  1868. initcode:=cloadnode.create(sym,sym.owner);
  1869. { indicate that this load should not be transformed into a load
  1870. from the parentfpstruct, but instead should load the original
  1871. value }
  1872. include(initcode.flags,nf_internal);
  1873. { in case it's a var/out/constref parameter, store the address of the
  1874. parameter in the struct }
  1875. if addrparam then
  1876. begin
  1877. initcode:=caddrnode.create_internal(initcode);
  1878. include(taddrnode(initcode).addrnodeflags,anf_typedaddr);
  1879. end;
  1880. initcode:=cassignmentnode.create(
  1881. csubscriptnode.create(result,cloadnode.create(pd.parentfpstruct,pd.parentfpstruct.owner)),
  1882. initcode);
  1883. tblocknode(pd.parentfpinitblock).left:=cstatementnode.create
  1884. (initcode,tblocknode(pd.parentfpinitblock).left);
  1885. current_filepos:=old_filepos;
  1886. { also add the associated high para, if any. It may not be accessed
  1887. during code generation, and we need to catch 'em all (TM) during
  1888. the typecheck/firstpass }
  1889. highsym:=get_high_value_sym(tparavarsym(sym));
  1890. if assigned(highsym) then
  1891. maybe_add_sym_to_parentfpstruct(pd, highsym, highsym.vardef, false);
  1892. end;
  1893. end;
  1894. end;
  1895. procedure redirect_parentfpstruct_local_syms(pd: tprocdef);
  1896. var
  1897. nestedvarsdef: trecorddef;
  1898. sl: tpropaccesslist;
  1899. fsym,
  1900. lsym,
  1901. aliassym: tsym;
  1902. i: longint;
  1903. begin
  1904. nestedvarsdef:=trecorddef(tlocalvarsym(pd.parentfpstruct).vardef);
  1905. for i:=0 to nestedvarsdef.symtable.symlist.count-1 do
  1906. begin
  1907. fsym:=tsym(nestedvarsdef.symtable.symlist[i]);
  1908. if fsym.typ<>fieldvarsym then
  1909. continue;
  1910. lsym:=tsym(pd.localst.find(fsym.name));
  1911. if not assigned(lsym) then
  1912. lsym:=tsym(pd.parast.find(fsym.name));
  1913. if not assigned(lsym) then
  1914. internalerror(2011060408);
  1915. { add an absolute variable that redirects to the field }
  1916. sl:=tpropaccesslist.create;
  1917. sl.addsym(sl_load,pd.parentfpstruct);
  1918. sl.addsym(sl_subscript,tfieldvarsym(fsym));
  1919. aliassym:=cabsolutevarsym.create_ref(lsym.EscapedRealName,tfieldvarsym(fsym).vardef,sl);
  1920. { hide the original variable (can't delete, because there
  1921. may be other loadnodes that reference it)
  1922. -- only for locals; hiding parameters changes the
  1923. function signature }
  1924. if lsym.typ<>paravarsym then
  1925. hidesym(lsym);
  1926. { insert the absolute variable in the localst of the
  1927. routine; ignore duplicates, because this will also check the
  1928. parasymtable and we want to override parameters with our local
  1929. versions }
  1930. pd.localst.insertsym(aliassym,false);
  1931. end;
  1932. end;
  1933. function find_sym_in_parentfpstruct(pd: tprocdef; sym: tsym): tsym;
  1934. var
  1935. nestedvarsdef: tdef;
  1936. begin
  1937. nestedvarsdef:=tlocalvarsym(pd.parentfpstruct).vardef;
  1938. result:=search_struct_member(trecorddef(nestedvarsdef),sym.name);
  1939. end;
  1940. procedure finish_parentfpstruct(pd: tprocdef);
  1941. begin
  1942. trecordsymtable(trecorddef(tlocalvarsym(pd.parentfpstruct).vardef).symtable).addalignmentpadding;
  1943. end;
  1944. function make_field_static(recst: tsymtable; fieldvs: tfieldvarsym): tstaticvarsym;
  1945. var
  1946. static_name: string;
  1947. hstaticvs: tstaticvarsym;
  1948. tmp: tabsolutevarsym;
  1949. sl: tpropaccesslist;
  1950. begin
  1951. include(fieldvs.symoptions,sp_static);
  1952. { generate the symbol which reserves the space }
  1953. static_name:=lower(generate_nested_name(recst,'_'))+'_'+fieldvs.name;
  1954. hstaticvs:=cstaticvarsym.create_from_fieldvar(static_name,fieldvs);
  1955. {$ifdef jvm}
  1956. { for the JVM, static field accesses are name-based and
  1957. hence we have to keep the original name of the field.
  1958. Create a staticvarsym instead of a fieldvarsym so we can
  1959. nevertheless use a loadn instead of a subscriptn though,
  1960. since a subscriptn requires something to subscript and
  1961. there is nothing in this case (class+field name will be
  1962. encoded in the mangled symbol name) }
  1963. recst.insertsym(hstaticvs);
  1964. { only set the staticvarsym's basename (= field name, without any
  1965. mangling), because generating the fully mangled name right now can
  1966. result in a wrong string in case the field's type is a forward
  1967. declared class whose external name will change when the actual
  1968. definition is parsed }
  1969. if (vo_has_mangledname in fieldvs.varoptions) then
  1970. hstaticvs.set_mangledbasename(fieldvs.externalname^)
  1971. else
  1972. hstaticvs.set_mangledbasename(fieldvs.realname);
  1973. { for definition in class file }
  1974. hstaticvs.visibility:=fieldvs.visibility;
  1975. {$else jvm}
  1976. include(hstaticvs.symoptions,sp_internal);
  1977. if df_generic in tdef(recst.defowner).defoptions then
  1978. tabstractrecordsymtable(recst).insertsym(hstaticvs)
  1979. else
  1980. tdef(tabstractrecordsymtable(recst).defowner).get_top_level_symtable(false).insertsym(hstaticvs);
  1981. {$endif jvm}
  1982. { generate the symbol for the access }
  1983. sl:=tpropaccesslist.create;
  1984. sl.addsym(sl_load,hstaticvs);
  1985. { do *not* change the visibility of this absolutevarsym from vis_public
  1986. to anything else, because its visibility is used by visibility checks
  1987. after turning a class property referring to a class variable into a
  1988. load node (handle_staticfield_access -> searchsym_in_class ->
  1989. is_visible_for_object), which means that the load will fail if this
  1990. symbol is e.g. "strict private" while the property is public }
  1991. tmp:=cabsolutevarsym.create_ref('$'+static_name,fieldvs.vardef,sl);
  1992. recst.insertsym(tmp);
  1993. result:=hstaticvs;
  1994. end;
  1995. procedure call_through_new_name(orgpd: tprocdef; const newname: TSymStr);
  1996. var
  1997. newpd: tprocdef;
  1998. begin
  1999. { we have a forward declaration like
  2000. procedure test; (in the unit interface or "forward")
  2001. and then an implementation like
  2002. procedure test; external name 'something';
  2003. To solve this, we create a new external procdef for the
  2004. implementation, and then generate a procedure body for the original
  2005. one that calls through to the external procdef. This is necessary
  2006. because there may already be references to the mangled name for the
  2007. non-external "test".
  2008. }
  2009. { prefixing the parameters here is useless, because the new procdef will
  2010. just be an external declaration without a body }
  2011. newpd:=tprocdef(orgpd.getcopyas(procdef,pc_bareproc,'',true));
  2012. insert_funcret_para(newpd);
  2013. newpd.procoptions:=newpd.procoptions+orgpd.procoptions*[po_external,po_has_importname,po_has_importdll];
  2014. stringdispose(orgpd.import_name);
  2015. stringdispose(orgpd.import_dll);
  2016. orgpd.import_nr:=0;
  2017. newpd.setmangledname(newname);
  2018. finish_copied_procdef(newpd,'__FPC_IMPL_EXTERNAL_REDIRECT_'+newname,current_module.localsymtable,nil);
  2019. newpd.forwarddef:=false;
  2020. { ideally we would prefix the parameters of the original routine here, but since it
  2021. can be an interface definition, we cannot do that without risking to change the
  2022. interface crc }
  2023. orgpd.skpara:=newpd;
  2024. orgpd.synthetickind:=tsk_callthrough;
  2025. orgpd.procoptions:=orgpd.procoptions-[po_external,po_has_importname,po_has_importdll];
  2026. orgpd.forwarddef:=true;
  2027. end;
  2028. function generate_pkg_stub(pd:tprocdef):tnode;
  2029. begin
  2030. if target_info.system in systems_all_windows+systems_nativent then
  2031. begin
  2032. insert_funcret_local(pd);
  2033. result:=cassignmentnode.create(
  2034. cloadnode.create(pd.funcretsym,pd.localst),
  2035. cordconstnode.create(1,bool32type,false)
  2036. );
  2037. end
  2038. else
  2039. result:=cnothingnode.create;
  2040. end;
  2041. procedure generate_attr_constrs(attrs:tfpobjectlist);
  2042. var
  2043. ps : tprocsym;
  2044. pd : tprocdef;
  2045. pi : tcgprocinfo;
  2046. i : sizeint;
  2047. attr : trtti_attribute;
  2048. begin
  2049. if attrs.count=0 then
  2050. exit;
  2051. { if this isn't set then this unit shouldn't have any attributes }
  2052. if not assigned(class_tcustomattribute) then
  2053. internalerror(2019071003);
  2054. for i:=0 to attrs.count-1 do
  2055. begin
  2056. attr:=trtti_attribute(attrs[i]);
  2057. {Generate a procsym for main}
  2058. ps:=cprocsym.create('$rttiattrconstr$'+tostr(i));
  2059. { always register the symbol }
  2060. ps.register_sym;
  2061. { the RTTI always references this symbol }
  2062. inc(ps.refs);
  2063. current_module.localsymtable.insertsym(ps);
  2064. pd:=cprocdef.create(normal_function_level,true);
  2065. { always register the def }
  2066. pd.register_def;
  2067. pd.procsym:=ps;
  2068. ps.ProcdefList.Add(pd);
  2069. { set procdef options }
  2070. pd.proctypeoption:=potype_function;
  2071. pd.proccalloption:=pocall_default;
  2072. include(pd.procoptions,po_hascallingconvention);
  2073. pd.returndef:=class_tcustomattribute;
  2074. insert_funcret_para(pd);
  2075. pd.calcparas;
  2076. pd.forwarddef:=false;
  2077. pd.aliasnames.insert(pd.mangledname);
  2078. handle_calling_convention(pd,hcc_default_actions_impl);
  2079. { set procinfo and current_procinfo.procdef }
  2080. pi:=tcgprocinfo(cprocinfo.create(nil));
  2081. pi.procdef:=pd;
  2082. { we always do a call, namely to the constructor }
  2083. include(pi.flags,pi_do_call);
  2084. insert_funcret_local(pd);
  2085. pi.code:=cassignmentnode.create(
  2086. cloadnode.create(pd.funcretsym,pd.localst),
  2087. attr.constructorcall.getcopy
  2088. );
  2089. pi.generate_code;
  2090. attr.constructorpd:=pd;
  2091. end;
  2092. end;
  2093. end.