symcreat.pas 46 KB

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