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