symcreat.pas 43 KB

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