symcreat.pas 79 KB

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