symcreat.pas 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  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,
  23. aasmdata,
  24. symconst,symbase,symtype,symdef,symsym;
  25. type
  26. tscannerstate = record
  27. old_scanner: tscannerfile;
  28. old_filepos: tfileposinfo;
  29. old_token: ttoken;
  30. old_c: char;
  31. old_orgpattern: string;
  32. old_modeswitches: tmodeswitches;
  33. old_idtoken: ttoken;
  34. valid: boolean;
  35. end;
  36. { save/restore the scanner state before/after injecting }
  37. procedure replace_scanner(const tempname: string; out sstate: tscannerstate);
  38. procedure restore_scanner(const sstate: tscannerstate);
  39. { parses a (class or regular) method/constructor/destructor declaration from
  40. str, as if it were declared in astruct's declaration body
  41. WARNING: save the scanner state before calling this routine, and restore
  42. when done. }
  43. function str_parse_method_dec(str: ansistring; potype: tproctypeoption; is_classdef: boolean; astruct: tabstractrecorddef; out pd: tprocdef): boolean;
  44. { parses a (class or regular) method/constructor/destructor implementation
  45. from str, as if it appeared in the current unit's implementation section
  46. WARNINGS:
  47. * save the scanner state before calling this routine, and restore when done.
  48. * the code *must* be written in objfpc style
  49. }
  50. function str_parse_method_impl(str: ansistring; usefwpd: tprocdef; is_classdef: boolean):boolean;
  51. { parses a typed constant assignment to ssym
  52. WARNINGS:
  53. * save the scanner state before calling this routine, and restore when done.
  54. * the code *must* be written in objfpc style
  55. }
  56. procedure str_parse_typedconst(list: TAsmList; str: ansistring; ssym: tstaticvarsym);
  57. { in the JVM, constructors are not automatically inherited (so you can hide
  58. them). To emulate the Pascal behaviour, we have to automatically add
  59. all parent constructors to the current class as well. We also have to do
  60. the same for the (emulated) virtual class methods }
  61. procedure add_missing_parent_constructors_intf(obj: tobjectdef; addvirtclassmeth: boolean; forcevis: tvisibility);
  62. { goes through all defs in st to add implementations for synthetic methods
  63. added earlier }
  64. procedure add_synthetic_method_implementations(st: tsymtable);
  65. procedure finish_copied_procdef(var pd: tprocdef; const realname: string; newparentst: tsymtable; newstruct: tabstractrecorddef);
  66. { create "parent frame pointer" record skeleton for procdef, in which local
  67. variables and parameters from pd accessed from nested routines can be
  68. stored }
  69. procedure build_parentfpstruct(pd: tprocdef);
  70. { checks whether sym (a local or para of pd) already has a counterpart in
  71. pd's parentfpstruct, and if not adds a new field to the struct with type
  72. "vardef" (can be different from sym's type in case it's a call-by-reference
  73. parameter, which is indicated by addrparam). If it already has a field in
  74. the parentfpstruct, this field is returned. }
  75. function maybe_add_sym_to_parentfpstruct(pd: tprocdef; sym: tsym; vardef: tdef; addrparam: boolean): tsym;
  76. { given a localvarsym or paravarsym of pd, returns the field of the
  77. parentfpstruct corresponding to this sym }
  78. function find_sym_in_parentfpstruct(pd: tprocdef; sym: tsym): tsym;
  79. { replaces all local and paravarsyms that have been mirrored in the
  80. parentfpstruct with aliasvarsyms that redirect to these fields (used to
  81. make sure that references to these syms in the owning procdef itself also
  82. use the ones in the parentfpstructs) }
  83. procedure redirect_parentfpstruct_local_syms(pd: tprocdef);
  84. { finalises the parentfpstruct (alignment padding, ...) }
  85. procedure finish_parentfpstruct(pd: tprocdef);
  86. procedure maybe_guarantee_record_typesym(var def: tdef; st: tsymtable);
  87. { turns a fieldvarsym into a class/static field definition, and returns the
  88. created staticvarsym that is responsible for allocating the global storage }
  89. function make_field_static(recst: tsymtable; fieldvs: tfieldvarsym): tstaticvarsym;
  90. implementation
  91. uses
  92. cutils,cclasses,globals,verbose,systems,comphook,fmodule,
  93. symtable,defutil,
  94. pbase,pdecobj,pdecsub,psub,ptconst,
  95. {$ifdef jvm}
  96. pjvm,jvmdef,
  97. {$endif jvm}
  98. node,nbas,nld,nmem,ngenutil,
  99. defcmp,
  100. paramgr;
  101. procedure replace_scanner(const tempname: string; out sstate: tscannerstate);
  102. var
  103. old_block_type: tblock_type;
  104. begin
  105. { would require saving of cstringpattern, patternw }
  106. if (token=_CSTRING) or
  107. (token=_CWCHAR) or
  108. (token=_CWSTRING) then
  109. internalerror(2011032201);
  110. sstate.old_scanner:=current_scanner;
  111. sstate.old_filepos:=current_filepos;
  112. sstate.old_token:=token;
  113. sstate.old_c:=c;
  114. sstate.old_orgpattern:=orgpattern;
  115. sstate.old_modeswitches:=current_settings.modeswitches;
  116. sstate.old_idtoken:=idtoken;
  117. sstate.valid:=true;
  118. { creating a new scanner resets the block type, while we want to continue
  119. in the current one }
  120. old_block_type:=block_type;
  121. current_scanner:=tscannerfile.Create('_Macro_.'+tempname,true);
  122. block_type:=old_block_type;
  123. { required for e.g. FpcDeepCopy record method (uses "out" parameter; field
  124. names are escaped via &, so should not cause conflicts }
  125. current_settings.modeswitches:=objfpcmodeswitches;
  126. end;
  127. procedure restore_scanner(const sstate: tscannerstate);
  128. begin
  129. if sstate.valid then
  130. begin
  131. current_scanner.free;
  132. current_scanner:=sstate.old_scanner;
  133. current_filepos:=sstate.old_filepos;
  134. token:=sstate.old_token;
  135. current_settings.modeswitches:=sstate.old_modeswitches;
  136. c:=sstate.old_c;
  137. orgpattern:=sstate.old_orgpattern;
  138. pattern:=upper(sstate.old_orgpattern);
  139. idtoken:=sstate.old_idtoken;
  140. end;
  141. end;
  142. function str_parse_method_dec(str: ansistring; potype: tproctypeoption; is_classdef: boolean; astruct: tabstractrecorddef; out pd: tprocdef): boolean;
  143. var
  144. oldparse_only: boolean;
  145. begin
  146. Message1(parser_d_internal_parser_string,str);
  147. oldparse_only:=parse_only;
  148. parse_only:=true;
  149. result:=false;
  150. { in case multiple strings are injected, make sure to always close the
  151. previous macro inputfile to prevent memory leaks }
  152. if assigned(current_scanner.inputfile) and
  153. not(current_scanner.inputfile.closed) then
  154. current_scanner.closeinputfile;
  155. { inject the string in the scanner }
  156. str:=str+'end;';
  157. current_scanner.substitutemacro('meth_head_macro',@str[1],length(str),current_scanner.line_no,current_scanner.inputfile.ref_index);
  158. current_scanner.readtoken(false);
  159. { and parse it... }
  160. case potype of
  161. potype_class_constructor:
  162. pd:=class_constructor_head(astruct);
  163. potype_class_destructor:
  164. pd:=class_destructor_head(astruct);
  165. potype_constructor:
  166. pd:=constructor_head;
  167. potype_destructor:
  168. pd:=destructor_head;
  169. else if assigned(astruct) and
  170. (astruct.typ=recorddef) then
  171. pd:=parse_record_method_dec(astruct,is_classdef)
  172. else
  173. pd:=method_dec(astruct,is_classdef);
  174. end;
  175. if assigned(pd) then
  176. result:=true;
  177. parse_only:=oldparse_only;
  178. { remove the temporary macro input file again }
  179. current_scanner.closeinputfile;
  180. current_scanner.nextfile;
  181. current_scanner.tempopeninputfile;
  182. end;
  183. function str_parse_method_impl(str: ansistring; usefwpd: tprocdef; is_classdef: boolean):boolean;
  184. var
  185. oldparse_only: boolean;
  186. tmpstr: ansistring;
  187. begin
  188. if ((status.verbosity and v_debug)<>0) then
  189. begin
  190. if assigned(usefwpd) then
  191. Message1(parser_d_internal_parser_string,usefwpd.customprocname([pno_proctypeoption,pno_paranames,pno_ownername,pno_noclassmarker,pno_noleadingdollar])+str)
  192. else
  193. begin
  194. if is_classdef then
  195. tmpstr:='class '
  196. else
  197. tmpstr:='';
  198. Message1(parser_d_internal_parser_string,tmpstr+str);
  199. end;
  200. end;
  201. oldparse_only:=parse_only;
  202. parse_only:=false;
  203. result:=false;
  204. { inject the string in the scanner }
  205. str:=str+'const;';
  206. current_scanner.substitutemacro('meth_impl_macro',@str[1],length(str),current_scanner.line_no,current_scanner.inputfile.ref_index);
  207. current_scanner.readtoken(false);
  208. { and parse it... }
  209. read_proc(is_classdef,usefwpd);
  210. parse_only:=oldparse_only;
  211. { remove the temporary macro input file again }
  212. current_scanner.closeinputfile;
  213. current_scanner.nextfile;
  214. current_scanner.tempopeninputfile;
  215. result:=true;
  216. end;
  217. procedure str_parse_typedconst(list: TAsmList; str: ansistring; ssym: tstaticvarsym);
  218. var
  219. old_block_type: tblock_type;
  220. old_parse_only: boolean;
  221. begin
  222. Message1(parser_d_internal_parser_string,str);
  223. { a string that will be interpreted as the start of a new section ->
  224. typed constant parsing will stop }
  225. str:=str+'type ';
  226. old_parse_only:=parse_only;
  227. old_block_type:=block_type;
  228. parse_only:=true;
  229. block_type:=bt_const;
  230. current_scanner.substitutemacro('typed_const_macro',@str[1],length(str),current_scanner.line_no,current_scanner.inputfile.ref_index);
  231. current_scanner.readtoken(false);
  232. read_typed_const(list,ssym,ssym.owner.symtabletype in [recordsymtable,objectsymtable]);
  233. parse_only:=old_parse_only;
  234. block_type:=old_block_type;
  235. { remove the temporary macro input file again }
  236. current_scanner.closeinputfile;
  237. current_scanner.nextfile;
  238. current_scanner.tempopeninputfile;
  239. end;
  240. procedure add_missing_parent_constructors_intf(obj: tobjectdef; addvirtclassmeth: boolean; forcevis: tvisibility);
  241. var
  242. parent: tobjectdef;
  243. def: tdef;
  244. parentpd,
  245. childpd: tprocdef;
  246. i: longint;
  247. srsym: tsym;
  248. srsymtable: tsymtable;
  249. begin
  250. if (oo_is_external in obj.objectoptions) or
  251. not assigned(obj.childof) then
  252. exit;
  253. parent:=obj.childof;
  254. { find all constructor in the parent }
  255. for i:=0 to tobjectsymtable(parent.symtable).deflist.count-1 do
  256. begin
  257. def:=tdef(tobjectsymtable(parent.symtable).deflist[i]);
  258. if (def.typ<>procdef) or
  259. ((tprocdef(def).proctypeoption<>potype_constructor) and
  260. (not addvirtclassmeth or
  261. not([po_classmethod,po_virtualmethod]<=tprocdef(def).procoptions))) or
  262. not is_visible_for_object(tprocdef(def),obj) then
  263. continue;
  264. parentpd:=tprocdef(def);
  265. { do we have this constructor too? (don't use
  266. search_struct_member/searchsym_in_class, since those will
  267. search parents too) }
  268. if searchsym_in_record(obj,parentpd.procsym.name,srsym,srsymtable) then
  269. begin
  270. { there's a symbol with the same name, is it a routine of the
  271. same type with the same parameters? }
  272. if srsym.typ=procsym then
  273. begin
  274. childpd:=tprocsym(srsym).find_procdef_bytype_and_para(
  275. tprocdef(def).proctypeoption,parentpd.paras,nil,
  276. [cpo_ignorehidden,cpo_ignoreuniv,cpo_openequalisexact]);
  277. if assigned(childpd) then
  278. continue;
  279. end;
  280. end;
  281. { if we get here, we did not find it in the current objectdef ->
  282. add }
  283. childpd:=tprocdef(parentpd.getcopy);
  284. { get rid of the import name for inherited virtual class methods,
  285. it has to be regenerated rather than amended }
  286. if [po_classmethod,po_virtualmethod]<=childpd.procoptions then
  287. begin
  288. stringdispose(childpd.import_name);
  289. exclude(childpd.procoptions,po_has_importname);
  290. end;
  291. if forcevis<>vis_none then
  292. childpd.visibility:=forcevis;
  293. if po_virtualmethod in childpd.procoptions then
  294. include(childpd.procoptions,po_overridingmethod);
  295. { ignore this artificially added procdef when looking for overloads }
  296. include(childpd.procoptions,po_ignore_for_overload_resolution);
  297. finish_copied_procdef(childpd,parentpd.procsym.realname,obj.symtable,obj);
  298. exclude(childpd.procoptions,po_external);
  299. childpd.synthetickind:=tsk_anon_inherited;
  300. include(obj.objectoptions,oo_has_constructor);
  301. end;
  302. end;
  303. procedure implement_anon_inherited(pd: tprocdef);
  304. var
  305. str: ansistring;
  306. isclassmethod: boolean;
  307. begin
  308. isclassmethod:=
  309. (po_classmethod in pd.procoptions) and
  310. not(pd.proctypeoption in [potype_constructor,potype_destructor]);
  311. str:='begin ';
  312. if (pd.proctypeoption<>potype_constructor) and
  313. not is_void(pd.returndef) then
  314. str:=str+'result:=';
  315. str:=str+'inherited end;';
  316. str_parse_method_impl(str,pd,isclassmethod);
  317. end;
  318. procedure implement_jvm_clone(pd: tprocdef);
  319. var
  320. struct: tabstractrecorddef;
  321. str: ansistring;
  322. i: longint;
  323. sym: tsym;
  324. fsym: tfieldvarsym;
  325. begin
  326. if not(pd.struct.typ in [recorddef,objectdef]) then
  327. internalerror(2011032802);
  328. struct:=pd.struct;
  329. { anonymous record types must get an artificial name, so we can generate
  330. a typecast at the scanner level }
  331. if (struct.typ=recorddef) and
  332. not assigned(struct.typesym) then
  333. internalerror(2011032812);
  334. { We can easily use the inherited clone in case we have to create a deep
  335. copy of certain fields. The reason is that e.g. sets are pointers at
  336. the JVM level, but not in Pascal. So the JVM clone routine will copy the
  337. pointer to the set from the old record (= class instance) to the new
  338. one, but we have no way to change this pointer itself from inside Pascal
  339. code.
  340. We solve this by relying on the fact that the JVM is garbage collected:
  341. we simply declare a temporary instance on the stack, which will be
  342. allocated/initialized by the temp generator. We return its address as
  343. the result of the clone routine, so it remains live. }
  344. str:='type _fpc_ptrt = ^'+struct.typesym.realname+
  345. '; var __fpc_newcopy:'+ struct.typesym.realname+'; begin clone:=JLObject(@__fpc_newcopy);';
  346. { copy all field contents }
  347. for i:=0 to struct.symtable.symlist.count-1 do
  348. begin
  349. sym:=tsym(struct.symtable.symlist[i]);
  350. if (sym.typ=fieldvarsym) then
  351. begin
  352. fsym:=tfieldvarsym(sym);
  353. str:=str+'__fpc_newcopy.&'+fsym.realname+':=&'+fsym.realname+';';
  354. end;
  355. end;
  356. str:=str+'end;';
  357. str_parse_method_impl(str,pd,false);
  358. end;
  359. procedure implement_record_deepcopy(pd: tprocdef);
  360. var
  361. struct: tabstractrecorddef;
  362. str: ansistring;
  363. i: longint;
  364. sym: tsym;
  365. fsym: tfieldvarsym;
  366. begin
  367. if not(pd.struct.typ in [recorddef,objectdef]) then
  368. internalerror(2011032810);
  369. struct:=pd.struct;
  370. { anonymous record types must get an artificial name, so we can generate
  371. a typecast at the scanner level }
  372. if (struct.typ=recorddef) and
  373. not assigned(struct.typesym) then
  374. internalerror(2011032811);
  375. { copy all fields }
  376. str:='type _fpc_ptrt = ^'+struct.typesym.realname+'; var res: _fpc_ptrt; begin res:=_fpc_ptrt(result);';
  377. for i:=0 to struct.symtable.symlist.count-1 do
  378. begin
  379. sym:=tsym(struct.symtable.symlist[i]);
  380. if (sym.typ=fieldvarsym) then
  381. begin
  382. fsym:=tfieldvarsym(sym);
  383. str:=str+'res^.&'+fsym.realname+':=&'+fsym.realname+';';
  384. end;
  385. end;
  386. str:=str+'end;';
  387. str_parse_method_impl(str,pd,false);
  388. end;
  389. procedure implement_record_initialize(pd: tprocdef);
  390. var
  391. struct: tabstractrecorddef;
  392. str: ansistring;
  393. i: longint;
  394. sym: tsym;
  395. fsym: tfieldvarsym;
  396. begin
  397. if not(pd.struct.typ in [recorddef,objectdef]) then
  398. internalerror(2011071710);
  399. struct:=pd.struct;
  400. { anonymous record types must get an artificial name, so we can generate
  401. a typecast at the scanner level }
  402. if (struct.typ=recorddef) and
  403. not assigned(struct.typesym) then
  404. internalerror(2011032811);
  405. { walk over all fields that need initialization }
  406. str:='begin ';
  407. for i:=0 to struct.symtable.symlist.count-1 do
  408. begin
  409. sym:=tsym(struct.symtable.symlist[i]);
  410. if (sym.typ=fieldvarsym) then
  411. begin
  412. fsym:=tfieldvarsym(sym);
  413. if fsym.vardef.needs_inittable then
  414. str:=str+'system.initialize(&'+fsym.realname+');';
  415. end;
  416. end;
  417. str:=str+'end;';
  418. str_parse_method_impl(str,pd,false);
  419. end;
  420. procedure implement_empty(pd: tprocdef);
  421. var
  422. str: ansistring;
  423. isclassmethod: boolean;
  424. begin
  425. isclassmethod:=
  426. (po_classmethod in pd.procoptions) and
  427. not(pd.proctypeoption in [potype_constructor,potype_destructor]);
  428. str:='begin end;';
  429. str_parse_method_impl(str,pd,isclassmethod);
  430. end;
  431. procedure addvisibibleparameters(var str: ansistring; pd: tprocdef);
  432. var
  433. currpara: tparavarsym;
  434. i: longint;
  435. firstpara: boolean;
  436. begin
  437. firstpara:=true;
  438. for i:=0 to pd.paras.count-1 do
  439. begin
  440. currpara:=tparavarsym(pd.paras[i]);
  441. if not(vo_is_hidden_para in currpara.varoptions) then
  442. begin
  443. if not firstpara then
  444. str:=str+',';
  445. firstpara:=false;
  446. str:=str+currpara.realname;
  447. end;
  448. end;
  449. end;
  450. procedure implement_callthrough(pd: tprocdef);
  451. var
  452. str: ansistring;
  453. callpd: tprocdef;
  454. isclassmethod: boolean;
  455. begin
  456. isclassmethod:=
  457. (po_classmethod in pd.procoptions) and
  458. not(pd.proctypeoption in [potype_constructor,potype_destructor]);
  459. callpd:=tprocdef(pd.skpara);
  460. str:='begin ';
  461. if pd.returndef<>voidtype then
  462. str:=str+'result:=';
  463. str:=str+callpd.procsym.realname+'(';
  464. addvisibibleparameters(str,pd);
  465. str:=str+') end;';
  466. str_parse_method_impl(str,pd,isclassmethod);
  467. end;
  468. {$ifdef jvm}
  469. procedure implement_jvm_enum_values(pd: tprocdef);
  470. begin
  471. str_parse_method_impl('begin result:=__fpc_FVALUES end;',pd,true);
  472. end;
  473. procedure implement_jvm_enum_valuof(pd: tprocdef);
  474. begin
  475. str_parse_method_impl('begin result:=__FPC_TEnumClassAlias(inherited valueOf(JLClass(__FPC_TEnumClassAlias),__fpc_str)) end;',pd,true);
  476. end;
  477. procedure implement_jvm_enum_jumps_constr(pd: tprocdef);
  478. begin
  479. str_parse_method_impl('begin inherited create(__fpc_name,__fpc_ord); __fpc_fenumval:=__fpc_initenumval end;',pd,false);
  480. end;
  481. procedure implement_jvm_enum_fpcordinal(pd: tprocdef);
  482. var
  483. enumclass: tobjectdef;
  484. enumdef: tenumdef;
  485. begin
  486. enumclass:=tobjectdef(pd.owner.defowner);
  487. enumdef:=tenumdef(ttypesym(search_struct_member(enumclass,'__FPC_TENUMALIAS')).typedef);
  488. if not enumdef.has_jumps then
  489. str_parse_method_impl('begin result:=ordinal end;',pd,false)
  490. else
  491. str_parse_method_impl('begin result:=__fpc_fenumval end;',pd,false);
  492. end;
  493. procedure implement_jvm_enum_fpcvalueof(pd: tprocdef);
  494. var
  495. enumclass: tobjectdef;
  496. enumdef: tenumdef;
  497. isclassmethod: boolean;
  498. begin
  499. isclassmethod:=
  500. (po_classmethod in pd.procoptions) and
  501. not(pd.proctypeoption in [potype_constructor,potype_destructor]);
  502. enumclass:=tobjectdef(pd.owner.defowner);
  503. enumdef:=tenumdef(ttypesym(search_struct_member(enumclass,'__FPC_TENUMALIAS')).typedef);
  504. { convert integer to corresponding enum instance: in case of no jumps
  505. get it from the $VALUES array, otherwise from the __fpc_ord2enum
  506. hashmap }
  507. if not enumdef.has_jumps then
  508. str_parse_method_impl('begin result:=__fpc_FVALUES[__fpc_int] end;',pd,isclassmethod)
  509. else
  510. str_parse_method_impl('begin result:=__FPC_TEnumClassAlias(__fpc_ord2enum.get(JLInteger.valueOf(__fpc_int))) end;',pd,isclassmethod);
  511. end;
  512. function CompareEnumSyms(Item1, Item2: Pointer): Integer;
  513. var
  514. I1 : tenumsym absolute Item1;
  515. I2 : tenumsym absolute Item2;
  516. begin
  517. Result:=I1.value-I2.value;
  518. end;
  519. procedure implement_jvm_enum_classconstr(pd: tprocdef);
  520. var
  521. enumclass: tobjectdef;
  522. enumdef: tenumdef;
  523. enumname,
  524. str: ansistring;
  525. i: longint;
  526. enumsym: tenumsym;
  527. orderedenums: tfpobjectlist;
  528. begin
  529. enumclass:=tobjectdef(pd.owner.defowner);
  530. enumdef:=tenumdef(ttypesym(search_struct_member(enumclass,'__FPC_TENUMALIAS')).typedef);
  531. if not assigned(enumdef) then
  532. internalerror(2011062305);
  533. str:='begin ';
  534. if enumdef.has_jumps then
  535. { init hashmap for ordinal -> enum instance mapping; don't let it grow,
  536. and set the capacity to the next prime following the total number of
  537. enum elements to minimise the number of collisions }
  538. str:=str+'__fpc_ord2enum:=JUHashMap.Create('+tostr(next_prime(enumdef.symtable.symlist.count))+',1.0);';
  539. { iterate over all enum elements and initialise the class fields, and
  540. store them in the values array. Since the java.lang.Enum doCompare
  541. method is final and hardcoded to compare based on declaration order
  542. (= java.lang.Enum.ordinal() value), we have to create them in order of
  543. ascending FPC ordinal values (which may not be the same as the FPC
  544. declaration order in case of jumps }
  545. orderedenums:=tfpobjectlist.create(false);
  546. for i:=0 to enumdef.symtable.symlist.count-1 do
  547. orderedenums.add(enumdef.symtable.symlist[i]);
  548. if enumdef.has_jumps then
  549. orderedenums.sort(@CompareEnumSyms);
  550. for i:=0 to orderedenums.count-1 do
  551. begin
  552. enumsym:=tenumsym(orderedenums[i]);
  553. enumname:=enumsym.realname;
  554. str:=str+enumsym.name+':=__FPC_TEnumClassAlias.Create('''+enumname+''','+tostr(i);
  555. if enumdef.has_jumps then
  556. str:=str+','+tostr(enumsym.value);
  557. str:=str+');';
  558. { alias for $VALUES array used internally by the JDK, and also by FPC
  559. in case of no jumps }
  560. str:=str+'__fpc_FVALUES['+tostr(i)+']:='+enumname+';';
  561. if enumdef.has_jumps then
  562. str:=str+'__fpc_ord2enum.put(JLInteger.valueOf('+tostr(enumsym.value)+'),'+enumname+');';
  563. end;
  564. orderedenums.free;
  565. str:=str+' end;';
  566. str_parse_method_impl(str,pd,true);
  567. end;
  568. procedure implement_jvm_enum_long2set(pd: tprocdef);
  569. begin
  570. str_parse_method_impl(
  571. 'var '+
  572. 'i, setval: jint;'+
  573. 'begin '+
  574. 'result:=JUEnumSet.noneOf(JLClass(__FPC_TEnumClassAlias));'+
  575. 'if __val<>0 then '+
  576. 'begin '+
  577. '__setsize:=__setsize*8;'+
  578. 'for i:=0 to __setsize-1 do '+
  579. // setsize-i because JVM = big endian
  580. 'if (__val and (jlong(1) shl (__setsize-i)))<>0 then '+
  581. 'result.add(fpcValueOf(i+__setbase));'+
  582. 'end '+
  583. 'end;',
  584. pd,true);
  585. end;
  586. procedure implement_jvm_enum_bitset2set(pd: tprocdef);
  587. begin
  588. str_parse_method_impl(
  589. 'var '+
  590. 'i, setval: jint;'+
  591. 'begin '+
  592. 'result:=JUEnumSet.noneOf(JLClass(__FPC_TEnumClassAlias));'+
  593. 'i:=__val.nextSetBit(0);'+
  594. 'while i>=0 do '+
  595. 'begin '+
  596. 'setval:=-__fromsetbase;'+
  597. 'result.add(fpcValueOf(setval+__tosetbase));'+
  598. 'i:=__val.nextSetBit(i+1);'+
  599. 'end '+
  600. 'end;',
  601. pd,true);
  602. end;
  603. procedure implement_jvm_enum_set2set(pd: tprocdef);
  604. begin
  605. str_parse_method_impl(
  606. 'var '+
  607. 'it: JUIterator;'+
  608. 'ele: FpcEnumValueObtainable;'+
  609. 'i: longint;'+
  610. 'begin '+
  611. 'result:=JUEnumSet.noneOf(JLClass(__FPC_TEnumClassAlias));'+
  612. 'it:=__val.iterator;'+
  613. 'while it.hasNext do '+
  614. 'begin '+
  615. 'ele:=FpcEnumValueObtainable(it.next);'+
  616. 'i:=ele.fpcOrdinal-__fromsetbase;'+
  617. 'result.add(fpcValueOf(i+__tosetbase));'+
  618. 'end '+
  619. 'end;',
  620. pd,true);
  621. end;
  622. procedure implement_jvm_procvar_invoke(pd: tprocdef);
  623. var
  624. pvclass: tobjectdef;
  625. procvar: tprocvardef;
  626. paraname,str,endstr: ansistring;
  627. pvs: tparavarsym;
  628. paradef,boxdef,boxargdef: tdef;
  629. i: longint;
  630. firstpara: boolean;
  631. begin
  632. pvclass:=tobjectdef(pd.owner.defowner);
  633. procvar:=tprocvardef(ttypesym(search_struct_member(pvclass,'__FPC_PROCVARALIAS')).typedef);
  634. { the procvar wrapper class has a tmethod member called "method", whose
  635. "code" field is a JLRMethod, and whose "data" field is the self pointer
  636. if any (if none is required, it's ignored by the JVM, so there's no
  637. problem with always passing it) }
  638. { force extended syntax to allow calling invokeObjectFunc() without using
  639. its result }
  640. str:='';
  641. endstr:='';
  642. { create local pointer to result type for typecasting in case of an
  643. implicit pointer type }
  644. if jvmimplicitpointertype(procvar.returndef) then
  645. str:=str+'type __FPC_returnptrtype = ^'+procvar.returndef.typename+';';
  646. str:=str+'begin ';
  647. { result handling (skip for generic definitions, we'll generate a new
  648. version for the specialized definition) ) }
  649. if not is_void(procvar.returndef) and
  650. (procvar.returndef.typ<>undefineddef) then
  651. begin
  652. str:=str+'invoke:=';
  653. if procvar.returndef.typ in [orddef,floatdef] then
  654. begin
  655. { primitivetype(boxtype(..).unboxmethod) }
  656. jvmgetboxtype(procvar.returndef,boxdef,boxargdef,false);
  657. str:=str+procvar.returndef.typename+'('+boxdef.typename+'(';
  658. endstr:=').'+jvmgetunboxmethod(procvar.returndef)+')';
  659. end
  660. else if jvmimplicitpointertype(procvar.returndef) then
  661. begin
  662. str:=str+'__FPC_returnptrtype(';
  663. { dereference }
  664. endstr:=')^';
  665. end
  666. else
  667. begin
  668. str:=str+procvar.returndef.typename+'(';
  669. endstr:=')';
  670. end;
  671. end;
  672. str:=str+'invokeObjectFunc([';
  673. { parameters are a constant array of jlobject }
  674. firstpara:=true;
  675. for i:=0 to procvar.paras.count-1 do
  676. begin
  677. { skip self/vmt/parentfp, passed separately }
  678. pvs:=tparavarsym(procvar.paras[i]);
  679. if ([vo_is_self,vo_is_vmt,vo_is_parentfp]*pvs.varoptions)<>[] then
  680. continue;
  681. if not firstpara then
  682. str:=str+',';
  683. firstpara:=false;
  684. paraname:=pvs.realname;
  685. paradef:=pvs.vardef;
  686. { Pascalize hidden high parameter }
  687. if vo_is_high_para in pvs.varoptions then
  688. paraname:='high('+tparavarsym(procvar.paras[i-1]).realname+')'
  689. else if vo_is_hidden_para in pvs.varoptions then
  690. begin
  691. if ([vo_is_range_check,vo_is_overflow_check]*pvs.varoptions)<>[] then
  692. { ok, simple boolean parameters }
  693. else
  694. internalerror(2011072403);
  695. end;
  696. { var/out/constref parameters -> pass address through (same for
  697. implicit pointer types) }
  698. if paramanager.push_copyout_param(pvs.varspez,paradef,procvar.proccalloption) or
  699. jvmimplicitpointertype(paradef) then
  700. begin
  701. paraname:='@'+paraname;
  702. paradef:=java_jlobject;
  703. end;
  704. if paradef.typ in [orddef,floatdef] then
  705. begin
  706. { box primitive types; use valueOf() rather than create because it
  707. can give better performance }
  708. jvmgetboxtype(paradef,boxdef,boxargdef,false);
  709. str:=str+boxdef.typename+'.valueOf('+boxargdef.typename+'('+paraname+'))'
  710. end
  711. else
  712. str:=str+'JLObject('+paraname+')';
  713. end;
  714. str:=str+'])'+endstr+' end;';
  715. str_parse_method_impl(str,pd,false)
  716. end;
  717. procedure implement_jvm_procvar_intconstr(pd: tprocdef);
  718. var
  719. pvdef: tprocvardef;
  720. begin
  721. { ideal, and most performant, would be to keep the interface instance
  722. passed to the constructor around and always call its method directly
  723. rather than working via reflection. Unfortunately, the procvar semantics
  724. that allow directly modifying the procvar via typecasting it to a
  725. tmethod make this very hard.
  726. So for now we simply take the address of the interface instance's
  727. method and assign it to the tmethod of this procvar }
  728. pvdef:=tprocvardef(pd.skpara);
  729. str_parse_method_impl('begin method:=System.TMethod(@__intf.'+pvdef.typesym.RealName+'Callback) end;',pd,false);
  730. end;
  731. procedure implement_jvm_virtual_clmethod(pd: tprocdef);
  732. var
  733. str: ansistring;
  734. callpd: tprocdef;
  735. begin
  736. callpd:=tprocdef(pd.skpara);
  737. str:='var pv: __fpc_virtualclassmethod_pv_t'+tostr(pd.defid)+'; begin '
  738. + 'pv:=@'+callpd.procsym.RealName+';';
  739. if (pd.proctypeoption<>potype_constructor) and
  740. not is_void(pd.returndef) then
  741. str:=str+'result:=';
  742. str:=str+'pv(';
  743. addvisibibleparameters(str,pd);
  744. str:=str+') end;';
  745. str_parse_method_impl(str,pd,true)
  746. end;
  747. {$endif jvm}
  748. procedure implement_field_getter(pd: tprocdef);
  749. var
  750. str: ansistring;
  751. callthroughprop: tpropertysym;
  752. begin
  753. callthroughprop:=tpropertysym(pd.skpara);
  754. str:='begin result:='+callthroughprop.realname+'; end;';
  755. str_parse_method_impl(str,pd,po_classmethod in pd.procoptions)
  756. end;
  757. procedure implement_field_setter(pd: tprocdef);
  758. var
  759. str: ansistring;
  760. callthroughprop: tpropertysym;
  761. begin
  762. callthroughprop:=tpropertysym(pd.skpara);
  763. str:='begin '+callthroughprop.realname+':=__fpc_newval__; end;';
  764. str_parse_method_impl(str,pd,po_classmethod in pd.procoptions)
  765. end;
  766. procedure add_synthetic_method_implementations_for_st(st: tsymtable);
  767. var
  768. i : longint;
  769. def : tdef;
  770. pd : tprocdef;
  771. begin
  772. for i:=0 to st.deflist.count-1 do
  773. begin
  774. def:=tdef(st.deflist[i]);
  775. if (def.typ<>procdef) then
  776. continue;
  777. { skip methods when processing unit symtable }
  778. if def.owner<>st then
  779. continue;
  780. pd:=tprocdef(def);
  781. case pd.synthetickind of
  782. tsk_none:
  783. ;
  784. tsk_anon_inherited:
  785. implement_anon_inherited(pd);
  786. tsk_jvm_clone:
  787. implement_jvm_clone(pd);
  788. tsk_record_deepcopy:
  789. implement_record_deepcopy(pd);
  790. tsk_record_initialize:
  791. implement_record_initialize(pd);
  792. tsk_empty,
  793. { special handling for this one is done in tnodeutils.wrap_proc_body }
  794. tsk_tcinit:
  795. implement_empty(pd);
  796. tsk_callthrough:
  797. implement_callthrough(pd);
  798. tsk_callthrough_nonabstract:
  799. begin
  800. if (pd.owner.defowner.typ<>objectdef) or
  801. (tobjectdef(pd.owner.defowner).abstractcnt=0) then
  802. implement_callthrough(pd)
  803. else
  804. implement_empty(pd);
  805. end;
  806. {$ifdef jvm}
  807. tsk_jvm_enum_values:
  808. implement_jvm_enum_values(pd);
  809. tsk_jvm_enum_valueof:
  810. implement_jvm_enum_valuof(pd);
  811. tsk_jvm_enum_classconstr:
  812. implement_jvm_enum_classconstr(pd);
  813. tsk_jvm_enum_jumps_constr:
  814. implement_jvm_enum_jumps_constr(pd);
  815. tsk_jvm_enum_fpcordinal:
  816. implement_jvm_enum_fpcordinal(pd);
  817. tsk_jvm_enum_fpcvalueof:
  818. implement_jvm_enum_fpcvalueof(pd);
  819. tsk_jvm_enum_long2set:
  820. implement_jvm_enum_long2set(pd);
  821. tsk_jvm_enum_bitset2set:
  822. implement_jvm_enum_bitset2set(pd);
  823. tsk_jvm_enum_set2set:
  824. implement_jvm_enum_set2set(pd);
  825. tsk_jvm_procvar_invoke:
  826. implement_jvm_procvar_invoke(pd);
  827. tsk_jvm_procvar_intconstr:
  828. implement_jvm_procvar_intconstr(pd);
  829. tsk_jvm_virtual_clmethod:
  830. implement_jvm_virtual_clmethod(pd);
  831. {$endif jvm}
  832. tsk_field_getter:
  833. implement_field_getter(pd);
  834. tsk_field_setter:
  835. implement_field_setter(pd);
  836. else
  837. internalerror(2011032801);
  838. end;
  839. end;
  840. end;
  841. procedure add_synthetic_method_implementations(st: tsymtable);
  842. var
  843. i: longint;
  844. def: tdef;
  845. sstate: tscannerstate;
  846. begin
  847. { only necessary for the JVM target currently }
  848. if not (target_info.system in systems_jvm) then
  849. exit;
  850. replace_scanner('synthetic_impl',sstate);
  851. add_synthetic_method_implementations_for_st(st);
  852. for i:=0 to st.deflist.count-1 do
  853. begin
  854. def:=tdef(st.deflist[i]);
  855. if (def.typ=procdef) and
  856. assigned(tprocdef(def).localst) and
  857. { not true for the "main" procedure, whose localsymtable is the staticsymtable }
  858. (tprocdef(def).localst.symtabletype=localsymtable) then
  859. add_synthetic_method_implementations(tprocdef(def).localst)
  860. else if (is_javaclass(def) and
  861. not(oo_is_external in tobjectdef(def).objectoptions)) or
  862. (def.typ=recorddef) then
  863. begin
  864. { also complete nested types }
  865. add_synthetic_method_implementations(tabstractrecorddef(def).symtable);
  866. end;
  867. end;
  868. restore_scanner(sstate);
  869. end;
  870. procedure finish_copied_procdef(var pd: tprocdef; const realname: string; newparentst: tsymtable; newstruct: tabstractrecorddef);
  871. var
  872. sym: tsym;
  873. parasym: tparavarsym;
  874. ps: tprocsym;
  875. stname: string;
  876. i: longint;
  877. begin
  878. { add generic flag if required }
  879. if df_generic in newstruct.defoptions then
  880. include(pd.defoptions,df_generic);
  881. { associate the procdef with a procsym in the owner }
  882. if not(pd.proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  883. stname:=upper(realname)
  884. else
  885. stname:=lower(realname);
  886. sym:=tsym(newparentst.find(stname));
  887. if assigned(sym) then
  888. begin
  889. if sym.typ<>procsym then
  890. internalerror(2011040601);
  891. ps:=tprocsym(sym);
  892. end
  893. else
  894. begin
  895. ps:=tprocsym.create(realname);
  896. newparentst.insert(ps);
  897. end;
  898. pd.procsym:=ps;
  899. pd.struct:=newstruct;
  900. { in case of methods, replace the special parameter types with new ones }
  901. if assigned(newstruct) then
  902. begin
  903. symtablestack.push(pd.parast);
  904. { may not be assigned in case we converted a procvar into a procdef }
  905. if assigned(pd.paras) then
  906. begin
  907. for i:=0 to pd.paras.count-1 do
  908. begin
  909. parasym:=tparavarsym(pd.paras[i]);
  910. if vo_is_self in parasym.varoptions then
  911. begin
  912. if parasym.vardef.typ=classrefdef then
  913. parasym.vardef:=tclassrefdef.create(newstruct)
  914. else
  915. parasym.vardef:=newstruct;
  916. end
  917. end;
  918. end;
  919. { also fix returndef in case of a constructor }
  920. if pd.proctypeoption=potype_constructor then
  921. pd.returndef:=newstruct;
  922. symtablestack.pop(pd.parast);
  923. end;
  924. pd.calcparas;
  925. proc_add_definition(pd);
  926. end;
  927. procedure build_parentfpstruct(pd: tprocdef);
  928. var
  929. nestedvars: tsym;
  930. nestedvarsst: tsymtable;
  931. pnestedvarsdef,
  932. nestedvarsdef: tdef;
  933. old_symtablestack: tsymtablestack;
  934. begin
  935. { make sure the defs are not registered in the current symtablestack,
  936. because they may be for a parent procdef (changeowner does remove a def
  937. from the symtable in which it was originally created, so that by itself
  938. is not enough) }
  939. old_symtablestack:=symtablestack;
  940. symtablestack:=old_symtablestack.getcopyuntil(current_module.localsymtable);
  941. { create struct to hold local variables and parameters that are
  942. accessed from within nested routines (start with extra dollar to prevent
  943. the JVM from thinking this is a nested class in the unit) }
  944. nestedvarsst:=trecordsymtable.create('$'+current_module.realmodulename^+'$$_fpc_nestedvars$'+tostr(pd.defid),current_settings.alignment.localalignmax);
  945. nestedvarsdef:=trecorddef.create(nestedvarsst.name^,nestedvarsst);
  946. {$ifdef jvm}
  947. maybe_guarantee_record_typesym(nestedvarsdef,nestedvarsdef.owner);
  948. { don't add clone/FpcDeepCopy, because the field names are not all
  949. representable in source form and we don't need them anyway }
  950. symtablestack.push(trecorddef(nestedvarsdef).symtable);
  951. maybe_add_public_default_java_constructor(trecorddef(nestedvarsdef));
  952. insert_record_hidden_paras(trecorddef(nestedvarsdef));
  953. symtablestack.pop(trecorddef(nestedvarsdef).symtable);
  954. {$endif}
  955. symtablestack.free;
  956. symtablestack:=old_symtablestack.getcopyuntil(pd.localst);
  957. pnestedvarsdef:=getpointerdef(nestedvarsdef);
  958. nestedvars:=tlocalvarsym.create('$nestedvars',vs_var,nestedvarsdef,[]);
  959. pd.localst.insert(nestedvars);
  960. pd.parentfpstruct:=nestedvars;
  961. pd.parentfpstructptrtype:=pnestedvarsdef;
  962. pd.parentfpinitblock:=cblocknode.create(nil);
  963. symtablestack.free;
  964. symtablestack:=old_symtablestack;
  965. end;
  966. function maybe_add_sym_to_parentfpstruct(pd: tprocdef; sym: tsym; vardef: tdef; addrparam: boolean): tsym;
  967. var
  968. fieldvardef,
  969. nestedvarsdef: tdef;
  970. nestedvarsst: tsymtable;
  971. initcode: tnode;
  972. old_filepos: tfileposinfo;
  973. begin
  974. nestedvarsdef:=tlocalvarsym(pd.parentfpstruct).vardef;
  975. result:=search_struct_member(trecorddef(nestedvarsdef),sym.name);
  976. if not assigned(result) then
  977. begin
  978. { mark that this symbol is mirrored in the parentfpstruct }
  979. tabstractnormalvarsym(sym).inparentfpstruct:=true;
  980. { add field to the struct holding all locals accessed
  981. by nested routines }
  982. nestedvarsst:=trecorddef(nestedvarsdef).symtable;
  983. { indicate whether or not this is a var/out/constref/... parameter }
  984. if addrparam then
  985. fieldvardef:=getpointerdef(vardef)
  986. else
  987. fieldvardef:=vardef;
  988. result:=tfieldvarsym.create(sym.realname,vs_value,fieldvardef,[]);
  989. if nestedvarsst.symlist.count=0 then
  990. include(tfieldvarsym(result).varoptions,vo_is_first_field);
  991. nestedvarsst.insert(result);
  992. trecordsymtable(nestedvarsst).addfield(tfieldvarsym(result),vis_public);
  993. { add initialization with original value if it's a parameter }
  994. if (sym.typ=paravarsym) then
  995. begin
  996. old_filepos:=current_filepos;
  997. fillchar(current_filepos,sizeof(current_filepos),0);
  998. initcode:=cloadnode.create(sym,sym.owner);
  999. { indicate that this load should not be transformed into a load
  1000. from the parentfpstruct, but instead should load the original
  1001. value }
  1002. include(initcode.flags,nf_internal);
  1003. { in case it's a var/out/constref parameter, store the address of the
  1004. parameter in the struct }
  1005. if addrparam then
  1006. begin
  1007. initcode:=caddrnode.create_internal(initcode);
  1008. include(initcode.flags,nf_typedaddr);
  1009. end;
  1010. initcode:=cassignmentnode.create(
  1011. csubscriptnode.create(result,cloadnode.create(pd.parentfpstruct,pd.parentfpstruct.owner)),
  1012. initcode);
  1013. tblocknode(pd.parentfpinitblock).left:=cstatementnode.create
  1014. (initcode,tblocknode(pd.parentfpinitblock).left);
  1015. current_filepos:=old_filepos;
  1016. end;
  1017. end;
  1018. end;
  1019. procedure redirect_parentfpstruct_local_syms(pd: tprocdef);
  1020. var
  1021. nestedvarsdef: trecorddef;
  1022. sl: tpropaccesslist;
  1023. fsym,
  1024. lsym,
  1025. aliassym: tsym;
  1026. i: longint;
  1027. begin
  1028. nestedvarsdef:=trecorddef(tlocalvarsym(pd.parentfpstruct).vardef);
  1029. for i:=0 to nestedvarsdef.symtable.symlist.count-1 do
  1030. begin
  1031. fsym:=tsym(nestedvarsdef.symtable.symlist[i]);
  1032. if fsym.typ<>fieldvarsym then
  1033. continue;
  1034. lsym:=tsym(pd.localst.find(fsym.name));
  1035. if not assigned(lsym) then
  1036. lsym:=tsym(pd.parast.find(fsym.name));
  1037. if not assigned(lsym) then
  1038. internalerror(2011060408);
  1039. { add an absolute variable that redirects to the field }
  1040. sl:=tpropaccesslist.create;
  1041. sl.addsym(sl_load,pd.parentfpstruct);
  1042. sl.addsym(sl_subscript,tfieldvarsym(fsym));
  1043. aliassym:=tabsolutevarsym.create_ref(lsym.name,tfieldvarsym(fsym).vardef,sl);
  1044. { hide the original variable (can't delete, because there
  1045. may be other loadnodes that reference it)
  1046. -- only for locals; hiding parameters changes the
  1047. function signature }
  1048. if lsym.typ<>paravarsym then
  1049. hidesym(lsym);
  1050. { insert the absolute variable in the localst of the
  1051. routine; ignore duplicates, because this will also check the
  1052. parasymtable and we want to override parameters with our local
  1053. versions }
  1054. pd.localst.insert(aliassym,false);
  1055. end;
  1056. end;
  1057. function find_sym_in_parentfpstruct(pd: tprocdef; sym: tsym): tsym;
  1058. var
  1059. nestedvarsdef: tdef;
  1060. begin
  1061. nestedvarsdef:=tlocalvarsym(pd.parentfpstruct).vardef;
  1062. result:=search_struct_member(trecorddef(nestedvarsdef),sym.name);
  1063. end;
  1064. procedure finish_parentfpstruct(pd: tprocdef);
  1065. begin
  1066. trecordsymtable(trecorddef(tlocalvarsym(pd.parentfpstruct).vardef).symtable).addalignmentpadding;
  1067. end;
  1068. procedure maybe_guarantee_record_typesym(var def: tdef; st: tsymtable);
  1069. var
  1070. ts: ttypesym;
  1071. begin
  1072. { create a dummy typesym for the JVM target, because the record
  1073. has to be wrapped by a class }
  1074. if (target_info.system in systems_jvm) and
  1075. (def.typ=recorddef) and
  1076. not assigned(def.typesym) then
  1077. begin
  1078. ts:=ttypesym.create(trecorddef(def).symtable.realname^,def);
  1079. st.insert(ts);
  1080. ts.visibility:=vis_strictprivate;
  1081. { this typesym can't be used by any Pascal code, so make sure we don't
  1082. print a hint about it being unused }
  1083. addsymref(ts);
  1084. end;
  1085. end;
  1086. function make_field_static(recst: tsymtable; fieldvs: tfieldvarsym): tstaticvarsym;
  1087. var
  1088. static_name: string;
  1089. hstaticvs: tstaticvarsym;
  1090. tmp: tabsolutevarsym;
  1091. sl: tpropaccesslist;
  1092. begin
  1093. include(fieldvs.symoptions,sp_static);
  1094. { generate the symbol which reserves the space }
  1095. static_name:=lower(generate_nested_name(recst,'_'))+'_'+fieldvs.name;
  1096. hstaticvs:=tstaticvarsym.create(internal_static_field_name(static_name),vs_value,fieldvs.vardef,[]);
  1097. {$ifdef jvm}
  1098. { for the JVM, static field accesses are name-based and
  1099. hence we have to keep the original name of the field.
  1100. Create a staticvarsym instead of a fieldvarsym so we can
  1101. nevertheless use a loadn instead of a subscriptn though,
  1102. since a subscriptn requires something to subscript and
  1103. there is nothing in this case (class+field name will be
  1104. encoded in the mangled symbol name) }
  1105. recst.insert(hstaticvs);
  1106. { only set the staticvarsym's basename (= field name, without any
  1107. mangling), because generating the fully mangled name right now can
  1108. result in a wrong string in case the field's type is a forward
  1109. declared class whose external name will change when the actual
  1110. definition is parsed }
  1111. if (vo_has_mangledname in fieldvs.varoptions) then
  1112. hstaticvs.set_mangledbasename(fieldvs.externalname^)
  1113. else
  1114. hstaticvs.set_mangledbasename(fieldvs.realname);
  1115. { for definition in class file }
  1116. hstaticvs.visibility:=fieldvs.visibility;
  1117. {$else jvm}
  1118. include(hstaticvs.symoptions,sp_internal);
  1119. tabstractrecordsymtable(recst).get_unit_symtable.insert(hstaticvs);
  1120. {$endif jvm}
  1121. { generate the symbol for the access }
  1122. sl:=tpropaccesslist.create;
  1123. sl.addsym(sl_load,hstaticvs);
  1124. { do *not* change the visibility of this absolutevarsym from vis_public
  1125. to anything else, because its visibility is used by visibility checks
  1126. after turning a class property referring to a class variable into a
  1127. load node (handle_staticfield_access -> searchsym_in_class ->
  1128. is_visible_for_object), which means that the load will fail if this
  1129. symbol is e.g. "strict private" while the property is public }
  1130. tmp:=tabsolutevarsym.create_ref('$'+static_name,fieldvs.vardef,sl);
  1131. recst.insert(tmp);
  1132. result:=hstaticvs;
  1133. end;
  1134. end.