2
0

pjvm.pas 31 KB

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