symcreat.pas 46 KB

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