pjvm.pas 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. {
  2. Copyright (c) 2011 by Jonas Maebe
  3. This unit implements some JVM parser helper routines.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. {$i fpcdefs.inc}
  18. unit pjvm;
  19. interface
  20. uses
  21. globtype,
  22. symconst,symtype,symbase,symdef,symsym;
  23. { the JVM specs require that you add a default parameterless
  24. constructor in case the programmer hasn't specified any }
  25. procedure maybe_add_public_default_java_constructor(obj: tabstractrecorddef);
  26. { records are emulated via Java classes. They require a default constructor
  27. to initialise temps, a deep copy helper for assignments, and clone()
  28. to initialse dynamic arrays }
  29. procedure add_java_default_record_methods_intf(def: trecorddef);
  30. procedure jvm_maybe_create_enum_class(const name: TIDString; def: tdef);
  31. procedure jvm_create_procvar_class(const name: TIDString; def: tdef);
  32. function jvm_add_typed_const_initializer(csym: tconstsym): tstaticvarsym;
  33. function jvm_wrap_method_with_vis(pd: tprocdef; vis: tvisibility): tprocdef;
  34. implementation
  35. uses
  36. cutils,cclasses,
  37. verbose,systems,
  38. fmodule,
  39. parabase,aasmdata,
  40. pdecsub,ngenutil,pparautl,
  41. symtable,symcreat,defcmp,jvmdef,nobj,
  42. defutil,paramgr;
  43. { the JVM specs require that you add a default parameterless
  44. constructor in case the programmer hasn't specified any }
  45. procedure maybe_add_public_default_java_constructor(obj: tabstractrecorddef);
  46. var
  47. sym: tsym;
  48. ps: tprocsym;
  49. pd: tprocdef;
  50. topowner: tdefentry;
  51. i: longint;
  52. sstate: tscannerstate;
  53. needclassconstructor: boolean;
  54. begin
  55. { if there is at least one constructor for a class, do nothing (for
  56. records, we'll always also need a parameterless constructor) }
  57. if not is_javaclass(obj) or
  58. not (oo_has_constructor in obj.objectoptions) then
  59. begin
  60. { check whether the parent has a parameterless constructor that we can
  61. call (in case of a class; all records will derive from
  62. java.lang.Object or a shim on top of that with a parameterless
  63. constructor) }
  64. if is_javaclass(obj) then
  65. begin
  66. pd:=nil;
  67. sym:=tsym(tobjectdef(obj).childof.symtable.find('CREATE'));
  68. if assigned(sym) and
  69. (sym.typ=procsym) then
  70. pd:=tprocsym(sym).find_bytype_parameterless(potype_constructor);
  71. if not assigned(pd) then
  72. begin
  73. Message(sym_e_no_matching_inherited_parameterless_constructor);
  74. exit
  75. end;
  76. end;
  77. { we call all constructors CREATE, because they don't have a name in
  78. Java and otherwise we can't determine whether multiple overloads
  79. are created with the same parameters }
  80. sym:=tsym(obj.symtable.find('CREATE'));
  81. if assigned(sym) then
  82. begin
  83. { does another, non-procsym, symbol already exist with that name? }
  84. if (sym.typ<>procsym) then
  85. begin
  86. Message1(sym_e_duplicate_id_create_java_constructor,sym.realname);
  87. exit;
  88. end;
  89. ps:=tprocsym(sym);
  90. { is there already a parameterless function/procedure create? }
  91. pd:=ps.find_bytype_parameterless(potype_function);
  92. if not assigned(pd) then
  93. pd:=ps.find_bytype_parameterless(potype_procedure);
  94. if assigned(pd) then
  95. begin
  96. Message1(sym_e_duplicate_id_create_java_constructor,pd.fullprocname(false));
  97. exit;
  98. end;
  99. end;
  100. if not assigned(sym) then
  101. begin
  102. ps:=tprocsym.create('Create');
  103. obj.symtable.insert(ps);
  104. end;
  105. { determine symtable level }
  106. topowner:=obj;
  107. while not(topowner.owner.symtabletype in [staticsymtable,globalsymtable]) do
  108. topowner:=topowner.owner.defowner;
  109. { create procdef }
  110. pd:=tprocdef.create(topowner.owner.symtablelevel+1);
  111. { method of this objectdef }
  112. pd.struct:=obj;
  113. { associated procsym }
  114. pd.procsym:=ps;
  115. { constructor }
  116. pd.proctypeoption:=potype_constructor;
  117. { needs to be exported }
  118. include(pd.procoptions,po_global);
  119. { for Delphi mode }
  120. include(pd.procoptions,po_overload);
  121. { generate anonymous inherited call in the implementation }
  122. pd.synthetickind:=tsk_anon_inherited;
  123. { public }
  124. pd.visibility:=vis_public;
  125. { result type }
  126. pd.returndef:=obj;
  127. { calling convention, self, ... }
  128. handle_calling_convention(pd);
  129. { register forward declaration with procsym }
  130. proc_add_definition(pd);
  131. end;
  132. { also add class constructor if class fields that need wrapping, and
  133. if none was defined }
  134. if obj.find_procdef_bytype(potype_class_constructor)=nil then
  135. begin
  136. needclassconstructor:=false;
  137. for i:=0 to obj.symtable.symlist.count-1 do
  138. begin
  139. if (tsym(obj.symtable.symlist[i]).typ=staticvarsym) and
  140. jvmimplicitpointertype(tstaticvarsym(obj.symtable.symlist[i]).vardef) then
  141. begin
  142. needclassconstructor:=true;
  143. break;
  144. end;
  145. end;
  146. if needclassconstructor then
  147. begin
  148. replace_scanner('custom_class_constructor',sstate);
  149. if str_parse_method_dec('constructor fpc_jvm_class_constructor;',potype_class_constructor,true,obj,pd) then
  150. pd.synthetickind:=tsk_empty
  151. else
  152. internalerror(2011040501);
  153. restore_scanner(sstate);
  154. end;
  155. end;
  156. end;
  157. procedure add_java_default_record_methods_intf(def: trecorddef);
  158. var
  159. sstate: tscannerstate;
  160. pd: tprocdef;
  161. begin
  162. maybe_add_public_default_java_constructor(def);
  163. replace_scanner('record_jvm_helpers',sstate);
  164. { no override, because not supported in records; the parser will still
  165. accept "inherited" though }
  166. if str_parse_method_dec('function clone: JLObject;',potype_function,false,def,pd) then
  167. pd.synthetickind:=tsk_jvm_clone
  168. else
  169. internalerror(2011032806);
  170. { can't use def.typesym, not yet set at this point }
  171. if not assigned(def.symtable.realname) then
  172. internalerror(2011032803);
  173. if str_parse_method_dec('procedure fpcDeepCopy(out result:'+def.symtable.realname^+');',potype_procedure,false,def,pd) then
  174. pd.synthetickind:=tsk_record_deepcopy
  175. else
  176. internalerror(2011032807);
  177. if def.needs_inittable then
  178. begin
  179. { 'var' instead of 'out' parameter, because 'out' would trigger
  180. calling the initialize method recursively }
  181. if str_parse_method_dec('procedure fpcInitializeRec;',potype_procedure,false,def,pd) then
  182. pd.synthetickind:=tsk_record_initialize
  183. else
  184. internalerror(2011071711);
  185. end;
  186. restore_scanner(sstate);
  187. end;
  188. procedure setup_for_new_class(const scannername: string; out sstate: tscannerstate; out islocal: boolean; out oldsymtablestack: TSymtablestack);
  189. begin
  190. replace_scanner(scannername,sstate);
  191. oldsymtablestack:=symtablestack;
  192. islocal:=symtablestack.top.symtablelevel>=normal_function_level;
  193. if islocal then
  194. begin
  195. { we cannot add a class local to a procedure -> insert it in the
  196. static symtable. This is not ideal because this means that it will
  197. be saved to the ppu file for no good reason, and loaded again
  198. even though it contains a reference to a type that was never
  199. saved to the ppu file (the locally defined enum type). Since this
  200. alias for the locally defined enumtype is only used while
  201. implementing the class' methods, this is however no problem. }
  202. symtablestack:=symtablestack.getcopyuntil(current_module.localsymtable);
  203. end;
  204. end;
  205. procedure restore_after_new_class(const sstate: tscannerstate; const islocal: boolean; const oldsymtablestack: TSymtablestack);
  206. begin
  207. if islocal then
  208. begin
  209. symtablestack.free;
  210. symtablestack:=oldsymtablestack;
  211. end;
  212. restore_scanner(sstate);
  213. end;
  214. procedure jvm_maybe_create_enum_class(const name: TIDString; def: tdef);
  215. var
  216. vmtbuilder: tvmtbuilder;
  217. arrdef: tarraydef;
  218. arrsym: ttypesym;
  219. juhashmap: tdef;
  220. enumclass: tobjectdef;
  221. pd: tprocdef;
  222. old_current_structdef: tabstractrecorddef;
  223. i: longint;
  224. sym: tstaticvarsym;
  225. fsym: tfieldvarsym;
  226. sstate: tscannerstate;
  227. sl: tpropaccesslist;
  228. temptypesym: ttypesym;
  229. oldsymtablestack: tsymtablestack;
  230. islocal: boolean;
  231. begin
  232. { if it's a subrange type, don't create a new class }
  233. if assigned(tenumdef(def).basedef) then
  234. exit;
  235. setup_for_new_class('jvm_enum_class',sstate,islocal,oldsymtablestack);
  236. { create new class (different internal name than enum to prevent name
  237. clash; at unit level because we don't want its methods to be nested
  238. inside a function in case its a local type) }
  239. enumclass:=tobjectdef.create(odt_javaclass,'$'+current_module.realmodulename^+'$'+name+'$InternEnum$'+tostr(def.defid),java_jlenum);
  240. tenumdef(def).classdef:=enumclass;
  241. include(enumclass.objectoptions,oo_is_enum_class);
  242. include(enumclass.objectoptions,oo_is_sealed);
  243. { implement FpcEnumValueObtainable interface }
  244. enumclass.ImplementedInterfaces.add(TImplementedInterface.Create(tobjectdef(search_system_type('FPCENUMVALUEOBTAINABLE').typedef)));
  245. { create an alias for this type inside itself: this way we can choose a
  246. name that can be used in generated Pascal code without risking an
  247. identifier conflict (since it is local to this class; the global name
  248. is unique because it's an identifier that contains $-signs) }
  249. enumclass.symtable.insert(ttypesym.create('__FPC_TEnumClassAlias',enumclass));
  250. { also create an alias for the enum type so that we can iterate over
  251. all enum values when creating the body of the class constructor }
  252. temptypesym:=ttypesym.create('__FPC_TEnumAlias',nil);
  253. { don't pass def to the ttypesym constructor, because then it
  254. will replace the current (real) typesym of that def with the alias }
  255. temptypesym.typedef:=def;
  256. enumclass.symtable.insert(temptypesym);
  257. { but the name of the class as far as the JVM is concerned will match
  258. the enum's original name (the enum type itself won't be output in
  259. any class file, so no conflict there) }
  260. if not islocal then
  261. enumclass.objextname:=stringdup(name)
  262. else
  263. { for local types, use a unique name to prevent conflicts (since such
  264. types are not visible outside the routine anyway, it doesn't matter
  265. }
  266. begin
  267. enumclass.objextname:=stringdup(enumclass.objrealname^);
  268. { also mark it as private (not strict private, because the class
  269. is not a subclass of the unit in which it is declared, so then
  270. the unit's procedures would not be able to use it) }
  271. enumclass.typesym.visibility:=vis_private;
  272. end;
  273. { now add a bunch of extra things to the enum class }
  274. old_current_structdef:=current_structdef;
  275. current_structdef:=enumclass;
  276. symtablestack.push(enumclass.symtable);
  277. { create static fields representing all enums }
  278. for i:=0 to tenumdef(def).symtable.symlist.count-1 do
  279. begin
  280. sym:=tstaticvarsym.create(tenumsym(tenumdef(def).symtable.symlist[i]).realname,vs_final,enumclass,[]);
  281. enumclass.symtable.insert(sym);
  282. { alias for consistency with parsed staticvarsyms }
  283. sl:=tpropaccesslist.create;
  284. sl.addsym(sl_load,sym);
  285. enumclass.symtable.insert(tabsolutevarsym.create_ref('$'+internal_static_field_name(sym.name),enumclass,sl));
  286. end;
  287. { create local "array of enumtype" type for the "values" functionality
  288. (used internally by the JDK) }
  289. arrdef:=tarraydef.create(0,tenumdef(def).symtable.symlist.count-1,s32inttype);
  290. arrdef.elementdef:=enumclass;
  291. arrsym:=ttypesym.create('__FPC_TEnumValues',arrdef);
  292. enumclass.symtable.insert(arrsym);
  293. { insert "public static values: array of enumclass" that returns $VALUES.clone()
  294. (rather than a dynamic array and using clone --which we don't support yet for arrays--
  295. simply use a fixed length array and copy it) }
  296. if not str_parse_method_dec('function values: __FPC_TEnumValues;static;',potype_function,true,enumclass,pd) then
  297. internalerror(2011062301);
  298. include(pd.procoptions,po_staticmethod);
  299. pd.synthetickind:=tsk_jvm_enum_values;
  300. { do we have to store the ordinal value separately? (if no jumps, we can
  301. just call the default ordinal() java.lang.Enum function) }
  302. if tenumdef(def).has_jumps then
  303. begin
  304. { add field for the value }
  305. fsym:=tfieldvarsym.create('__fpc_fenumval',vs_final,s32inttype,[]);
  306. enumclass.symtable.insert(fsym);
  307. tobjectsymtable(enumclass.symtable).addfield(fsym,vis_strictprivate);
  308. { add class field with hash table that maps from FPC-declared ordinal value -> enum instance }
  309. juhashmap:=search_system_type('JUHASHMAP').typedef;
  310. sym:=tstaticvarsym.create('__fpc_ord2enum',vs_final,juhashmap,[]);
  311. enumclass.symtable.insert(sym);
  312. { alias for consistency with parsed staticvarsyms }
  313. sl:=tpropaccesslist.create;
  314. sl.addsym(sl_load,sym);
  315. enumclass.symtable.insert(tabsolutevarsym.create_ref('$'+internal_static_field_name(sym.name),enumclass,sl));
  316. { add custom constructor }
  317. if not str_parse_method_dec('constructor Create(const __fpc_name: JLString; const __fpc_ord, __fpc_initenumval: longint);',potype_constructor,false,enumclass,pd) then
  318. internalerror(2011062401);
  319. pd.synthetickind:=tsk_jvm_enum_jumps_constr;
  320. pd.visibility:=vis_strictprivate;
  321. end
  322. else
  323. begin
  324. { insert "private constructor(string,int,int)" that calls inherited and
  325. initialises the FPC value field }
  326. add_missing_parent_constructors_intf(enumclass,vis_strictprivate);
  327. end;
  328. { add instance method to get the enum's value as declared in FPC }
  329. if not str_parse_method_dec('function FPCOrdinal: longint;',potype_function,false,enumclass,pd) then
  330. internalerror(2011062402);
  331. pd.synthetickind:=tsk_jvm_enum_fpcordinal;
  332. { add static class method to convert an ordinal to the corresponding enum }
  333. if not str_parse_method_dec('function FPCValueOf(__fpc_int: longint): __FPC_TEnumClassAlias; static;',potype_function,true,enumclass,pd) then
  334. internalerror(2011062402);
  335. pd.synthetickind:=tsk_jvm_enum_fpcvalueof;
  336. { similar (instance) function for use in set factories; implements FpcEnumValueObtainable interface }
  337. if not str_parse_method_dec('function fpcGenericValueOf(__fpc_int: longint): JLEnum;',potype_function,false,enumclass,pd) then
  338. internalerror(2011062402);
  339. pd.synthetickind:=tsk_jvm_enum_fpcvalueof;
  340. { insert "public static valueOf(string): tenumclass" that returns tenumclass(inherited valueOf(tenumclass,string)) }
  341. if not str_parse_method_dec('function valueOf(const __fpc_str: JLString): __FPC_TEnumClassAlias; static;',potype_function,true,enumclass,pd) then
  342. internalerror(2011062302);
  343. include(pd.procoptions,po_staticmethod);
  344. pd.synthetickind:=tsk_jvm_enum_valueof;
  345. { add instance method to convert an ordinal and an array into a set of
  346. (we always need/can use both in case of subrange types and/or array
  347. -> set type casts) }
  348. if not str_parse_method_dec('function fpcLongToEnumSet(__val: jlong; __setbase, __setsize: jint): JUEnumSet;',potype_function,true,enumclass,pd) then
  349. internalerror(2011070501);
  350. pd.synthetickind:=tsk_jvm_enum_long2set;
  351. if not str_parse_method_dec('function fpcBitSetToEnumSet(const __val: FpcBitSet; __fromsetbase, __tosetbase: jint): JUEnumSet; static;',potype_function,true,enumclass,pd) then
  352. internalerror(2011071004);
  353. pd.synthetickind:=tsk_jvm_enum_bitset2set;
  354. if not str_parse_method_dec('function fpcEnumSetToEnumSet(const __val: JUEnumSet; __fromsetbase, __tosetbase: jint): JUEnumSet; static;',potype_function,true,enumclass,pd) then
  355. internalerror(2011071005);
  356. pd.synthetickind:=tsk_jvm_enum_set2set;
  357. { create array called "$VALUES" that will contain a reference to all
  358. enum instances (JDK convention)
  359. Disable duplicate identifier checking when inserting, because it will
  360. check for a conflict with "VALUES" ($<id> normally means "check for
  361. <id> without uppercasing first"), which will conflict with the
  362. "Values" instance method -- that's also the reason why we insert the
  363. field only now, because we cannot disable duplicate identifier
  364. checking when creating the "Values" method }
  365. sym:=tstaticvarsym.create('$VALUES',vs_final,arrdef,[]);
  366. sym.visibility:=vis_strictprivate;
  367. enumclass.symtable.insert(sym,false);
  368. { alias for consistency with parsed staticvarsyms }
  369. sl:=tpropaccesslist.create;
  370. sl.addsym(sl_load,sym);
  371. enumclass.symtable.insert(tabsolutevarsym.create_ref('$'+internal_static_field_name(sym.name),arrdef,sl));
  372. { alias for accessing the field in generated Pascal code }
  373. sl:=tpropaccesslist.create;
  374. sl.addsym(sl_load,sym);
  375. enumclass.symtable.insert(tabsolutevarsym.create_ref('__fpc_FVALUES',arrdef,sl));
  376. { add initialization of the static class fields created above }
  377. if not str_parse_method_dec('constructor fpc_enum_class_constructor;',potype_class_constructor,true,enumclass,pd) then
  378. internalerror(2011062303);
  379. pd.synthetickind:=tsk_jvm_enum_classconstr;
  380. symtablestack.pop(enumclass.symtable);
  381. vmtbuilder:=TVMTBuilder.Create(enumclass);
  382. vmtbuilder.generate_vmt;
  383. vmtbuilder.free;
  384. restore_after_new_class(sstate,islocal,oldsymtablestack);
  385. current_structdef:=old_current_structdef;
  386. end;
  387. procedure jvm_create_procvar_class(const name: TIDString; def: tdef);
  388. var
  389. vmtbuilder: tvmtbuilder;
  390. oldsymtablestack: tsymtablestack;
  391. pvclass: tobjectdef;
  392. temptypesym: ttypesym;
  393. sstate: tscannerstate;
  394. methoddef: tprocdef;
  395. islocal: boolean;
  396. begin
  397. { inlined definition of procvar -> generate name, derive from
  398. FpcBaseNestedProcVarType, pass nestedfpstruct to constructor and
  399. copy it }
  400. if name='' then
  401. internalerror(2011071901);
  402. setup_for_new_class('jvm_pvar_class',sstate,islocal,oldsymtablestack);
  403. { create new class (different internal name than pvar to prevent name
  404. clash; at unit level because we don't want its methods to be nested
  405. inside a function in case its a local type) }
  406. pvclass:=tobjectdef.create(odt_javaclass,'$'+current_module.realmodulename^+'$'+name+'$InternProcvar$'+tostr(def.defid),java_procvarbase);
  407. tprocvardef(def).classdef:=pvclass;
  408. include(pvclass.objectoptions,oo_is_sealed);
  409. { associate typesym }
  410. pvclass.symtable.insert(ttypesym.create('__FPC_TProcVarClassAlias',pvclass));
  411. { set external name to match procvar type name }
  412. if not islocal then
  413. pvclass.objextname:=stringdup(name)
  414. else
  415. pvclass.objextname:=stringdup(pvclass.objrealname^);
  416. symtablestack.push(pvclass.symtable);
  417. { inherit constructor and keep public }
  418. add_missing_parent_constructors_intf(pvclass,vis_public);
  419. { add a method to call the procvar using unwrapped arguments, which
  420. then wraps them and calls through to JLRMethod.invoke }
  421. methoddef:=tprocdef(tprocvardef(def).getcopyas(procdef,pc_procvar2bareproc));
  422. finish_copied_procdef(methoddef,'invoke',pvclass.symtable,pvclass);
  423. insert_self_and_vmt_para(methoddef);
  424. methoddef.synthetickind:=tsk_jvm_procvar_invoke;
  425. methoddef.calcparas;
  426. { add local alias for the procvartype that we can use when implementing
  427. the invoke method }
  428. temptypesym:=ttypesym.create('__FPC_ProcVarAlias',nil);
  429. { don't pass def to the ttypesym constructor, because then it
  430. will replace the current (real) typesym of that def with the alias }
  431. temptypesym.typedef:=def;
  432. pvclass.symtable.insert(temptypesym);
  433. symtablestack.pop(pvclass.symtable);
  434. vmtbuilder:=TVMTBuilder.Create(pvclass);
  435. vmtbuilder.generate_vmt;
  436. vmtbuilder.free;
  437. restore_after_new_class(sstate,islocal,oldsymtablestack);
  438. end;
  439. function jvm_add_typed_const_initializer(csym: tconstsym): tstaticvarsym;
  440. var
  441. ssym: tstaticvarsym;
  442. esym: tenumsym;
  443. i: longint;
  444. sstate: tscannerstate;
  445. elemdef: tdef;
  446. elemdefname,
  447. conststr: ansistring;
  448. first: boolean;
  449. begin
  450. case csym.constdef.typ of
  451. enumdef:
  452. begin
  453. replace_scanner('jvm_enum_const',sstate);
  454. { make sure we don't emit a definition for this field (we'll do
  455. that for the constsym already) -> mark as external }
  456. ssym:=tstaticvarsym.create(internal_static_field_name(csym.realname),vs_final,csym.constdef,[vo_is_external]);
  457. csym.owner.insert(ssym);
  458. { alias storage to the constsym }
  459. ssym.set_mangledname(csym.realname);
  460. for i:=0 to tenumdef(csym.constdef).symtable.symlist.count-1 do
  461. begin
  462. esym:=tenumsym(tenumdef(csym.constdef).symtable.symlist[i]);
  463. if esym.value=csym.value.valueord.svalue then
  464. break;
  465. esym:=nil;
  466. end;
  467. { can happen in case of explicit typecast from integer constant
  468. to enum type }
  469. if not assigned(esym) then
  470. begin
  471. MessagePos(csym.fileinfo,parser_e_range_check_error);
  472. exit;
  473. end;
  474. str_parse_typedconst(current_asmdata.asmlists[al_typedconsts],esym.name+';',ssym);
  475. restore_scanner(sstate);
  476. result:=ssym;
  477. end;
  478. setdef:
  479. begin
  480. replace_scanner('jvm_set_const',sstate);
  481. { make sure we don't emit a definition for this field (we'll do
  482. that for the constsym already) -> mark as external;
  483. on the other hand, we don't create instances for constsyms in
  484. (or external syms) the program/unit initialization code -> add
  485. vo_has_local_copy to indicate that this should be done after all
  486. (in thlcgjvm.allocate_implicit_structs_for_st_with_base_ref) }
  487. { the constant can be defined in the body of a function and its
  488. def can also belong to that -> will be freed when the function
  489. has been compiler -> insert a copy in the unit's staticsymtable
  490. }
  491. symtablestack.push(current_module.localsymtable);
  492. ssym:=tstaticvarsym.create(internal_static_field_name(csym.realname),vs_final,tsetdef(csym.constdef).getcopy,[vo_is_external,vo_has_local_copy]);
  493. symtablestack.top.insert(ssym);
  494. symtablestack.pop(current_module.localsymtable);
  495. { alias storage to the constsym }
  496. ssym.set_mangledname(csym.realname);
  497. { ensure that we allocate space for global symbols (won't actually
  498. allocate space for this one, since it's external, but for the
  499. constsym) }
  500. cnodeutils.insertbssdata(ssym);
  501. elemdef:=tsetdef(csym.constdef).elementdef;
  502. if not assigned(elemdef) then
  503. begin
  504. internalerror(2011070502);
  505. end
  506. else
  507. begin
  508. elemdefname:=elemdef.typename;
  509. conststr:='[';
  510. first:=true;
  511. for i:=0 to 255 do
  512. if i in pnormalset(csym.value.valueptr)^ then
  513. begin
  514. if not first then
  515. conststr:=conststr+',';
  516. first:=false;
  517. { instead of looking up all enum value names/boolean
  518. names, type cast integers to the required type }
  519. conststr:=conststr+elemdefname+'('+tostr(i)+')';
  520. end;
  521. conststr:=conststr+'];';
  522. end;
  523. str_parse_typedconst(current_asmdata.asmlists[al_typedconsts],conststr,ssym);
  524. restore_scanner(sstate);
  525. result:=ssym;
  526. end;
  527. else
  528. internalerror(2011062701);
  529. end;
  530. end;
  531. function jvm_wrap_method_with_vis(pd: tprocdef; vis: tvisibility): tprocdef;
  532. var
  533. obj: tabstractrecorddef;
  534. visname: string;
  535. begin
  536. obj:=current_structdef;
  537. { if someone gets the idea to add a property to an external class
  538. definition, don't try to wrap it since we cannot add methods to
  539. external classes }
  540. if oo_is_external in obj.objectoptions then
  541. begin
  542. result:=pd;
  543. exit
  544. end;
  545. symtablestack.push(obj.symtable);
  546. result:=tprocdef(pd.getcopy);
  547. result.visibility:=vis;
  548. visname:=visibilityName[vis];
  549. replace(visname,' ','_');
  550. { create a name that is unique amongst all units (start with '$unitname$$') and
  551. unique in this unit (result.defid) }
  552. finish_copied_procdef(result,'$'+current_module.realmodulename^+'$$'+tostr(result.defid)+pd.procsym.realname+'$'+visname,obj.symtable,obj);
  553. { in case the referred method is from an external class }
  554. exclude(result.procoptions,po_external);
  555. { not virtual/override/abstract/... }
  556. result.procoptions:=result.procoptions*[po_classmethod,po_staticmethod,po_varargs,po_public];
  557. result.synthetickind:=tsk_callthrough;
  558. { so we know the name of the routine to call through to }
  559. result.skpara:=pd;
  560. symtablestack.pop(obj.symtable);
  561. end;
  562. end.