symcreat.pas 44 KB

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