pjvm.pas 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  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. { when a private/protected field is exposed via a property with a higher
  36. visibility, then we have to create a getter and/or setter with that same
  37. higher visibility to make sure that using the property does not result
  38. in JVM verification errors }
  39. procedure jvm_create_getter_for_property(p: tpropertysym; orgaccesspd: tprocdef);
  40. procedure jvm_create_setter_for_property(p: tpropertysym; orgaccesspd: tprocdef);
  41. implementation
  42. uses
  43. cutils,cclasses,
  44. verbose,globals,systems,
  45. fmodule,
  46. parabase,aasmdata,
  47. pdecsub,ngenutil,pparautl,
  48. symtable,symcreat,defcmp,jvmdef,nobj,
  49. defutil,paramgr;
  50. { the JVM specs require that you add a default parameterless
  51. constructor in case the programmer hasn't specified any }
  52. procedure maybe_add_public_default_java_constructor(obj: tabstractrecorddef);
  53. var
  54. sym: tsym;
  55. ps: tprocsym;
  56. pd: tprocdef;
  57. topowner: tdefentry;
  58. i: longint;
  59. sstate: tscannerstate;
  60. needclassconstructor: boolean;
  61. begin
  62. ps:=nil;
  63. { if there is at least one constructor for a class, do nothing (for
  64. records, we'll always also need a parameterless constructor) }
  65. if not is_javaclass(obj) or
  66. not (oo_has_constructor in obj.objectoptions) then
  67. begin
  68. { check whether the parent has a parameterless constructor that we can
  69. call (in case of a class; all records will derive from
  70. java.lang.Object or a shim on top of that with a parameterless
  71. constructor) }
  72. if is_javaclass(obj) then
  73. begin
  74. pd:=nil;
  75. { childof may not be assigned in case of a parser error }
  76. if not assigned(tobjectdef(obj).childof) then
  77. exit;
  78. sym:=tsym(tobjectdef(obj).childof.symtable.find('CREATE'));
  79. if assigned(sym) and
  80. (sym.typ=procsym) then
  81. pd:=tprocsym(sym).find_bytype_parameterless(potype_constructor);
  82. if not assigned(pd) then
  83. begin
  84. Message(sym_e_no_matching_inherited_parameterless_constructor);
  85. exit
  86. end;
  87. end;
  88. { we call all constructors CREATE, because they don't have a name in
  89. Java and otherwise we can't determine whether multiple overloads
  90. are created with the same parameters }
  91. sym:=tsym(obj.symtable.find('CREATE'));
  92. if assigned(sym) then
  93. begin
  94. { does another, non-procsym, symbol already exist with that name? }
  95. if (sym.typ<>procsym) then
  96. begin
  97. Message1(sym_e_duplicate_id_create_java_constructor,sym.realname);
  98. exit;
  99. end;
  100. ps:=tprocsym(sym);
  101. { is there already a parameterless function/procedure create? }
  102. pd:=ps.find_bytype_parameterless(potype_function);
  103. if not assigned(pd) then
  104. pd:=ps.find_bytype_parameterless(potype_procedure);
  105. if assigned(pd) then
  106. begin
  107. Message1(sym_e_duplicate_id_create_java_constructor,pd.fullprocname(false));
  108. exit;
  109. end;
  110. end;
  111. if not assigned(sym) then
  112. begin
  113. ps:=tprocsym.create('Create');
  114. obj.symtable.insert(ps);
  115. end;
  116. { determine symtable level }
  117. topowner:=obj;
  118. while not(topowner.owner.symtabletype in [staticsymtable,globalsymtable]) do
  119. topowner:=topowner.owner.defowner;
  120. { create procdef }
  121. pd:=tprocdef.create(topowner.owner.symtablelevel+1);
  122. if df_generic in obj.defoptions then
  123. include(pd.defoptions,df_generic);
  124. { method of this objectdef }
  125. pd.struct:=obj;
  126. { associated procsym }
  127. pd.procsym:=ps;
  128. { constructor }
  129. pd.proctypeoption:=potype_constructor;
  130. { needs to be exported }
  131. include(pd.procoptions,po_global);
  132. { by default do not include this routine when looking for overloads }
  133. include(pd.procoptions,po_ignore_for_overload_resolution);
  134. { generate anonymous inherited call in the implementation }
  135. pd.synthetickind:=tsk_anon_inherited;
  136. { public }
  137. pd.visibility:=vis_public;
  138. { result type }
  139. pd.returndef:=obj;
  140. { calling convention, self, ... (not for advanced records, for those
  141. this is handled later) }
  142. if obj.typ=recorddef then
  143. handle_calling_convention(pd,[hcc_check])
  144. else
  145. handle_calling_convention(pd,hcc_all);
  146. { register forward declaration with procsym }
  147. proc_add_definition(pd);
  148. end;
  149. { also add class constructor if class fields that need wrapping, and
  150. if none was defined }
  151. if obj.find_procdef_bytype(potype_class_constructor)=nil then
  152. begin
  153. needclassconstructor:=false;
  154. for i:=0 to obj.symtable.symlist.count-1 do
  155. begin
  156. if (tsym(obj.symtable.symlist[i]).typ=staticvarsym) and
  157. jvmimplicitpointertype(tstaticvarsym(obj.symtable.symlist[i]).vardef) then
  158. begin
  159. needclassconstructor:=true;
  160. break;
  161. end;
  162. end;
  163. if needclassconstructor then
  164. begin
  165. replace_scanner('custom_class_constructor',sstate);
  166. if str_parse_method_dec('constructor fpc_jvm_class_constructor;',potype_class_constructor,true,obj,pd) then
  167. pd.synthetickind:=tsk_empty
  168. else
  169. internalerror(2011040501);
  170. restore_scanner(sstate);
  171. end;
  172. end;
  173. end;
  174. procedure add_java_default_record_methods_intf(def: trecorddef);
  175. var
  176. sstate: tscannerstate;
  177. pd: tprocdef;
  178. sym: tsym;
  179. i: longint;
  180. begin
  181. maybe_add_public_default_java_constructor(def);
  182. replace_scanner('record_jvm_helpers',sstate);
  183. { no override, because not supported in records. Only required in case
  184. some of the fields require deep copies (otherwise the default
  185. shallow clone is fine) }
  186. for i:=0 to def.symtable.symlist.count-1 do
  187. begin
  188. sym:=tsym(def.symtable.symlist[i]);
  189. if (sym.typ=fieldvarsym) and
  190. jvmimplicitpointertype(tfieldvarsym(sym).vardef) then
  191. begin
  192. if str_parse_method_dec('function clone: JLObject;',potype_function,false,def,pd) then
  193. pd.synthetickind:=tsk_jvm_clone
  194. else
  195. internalerror(2011032806);
  196. break;
  197. end;
  198. end;
  199. { can't use def.typesym, not yet set at this point }
  200. if not assigned(def.symtable.realname) then
  201. internalerror(2011032803);
  202. if str_parse_method_dec('procedure fpcDeepCopy(result: FpcBaseRecordType);',potype_procedure,false,def,pd) then
  203. begin
  204. pd.synthetickind:=tsk_record_deepcopy;
  205. { can't add to the declaration since record methods can't override;
  206. it is in fact an overriding method, because all records inherit
  207. from a Java base class }
  208. include(pd.procoptions,po_overridingmethod);
  209. end
  210. else
  211. internalerror(2011032807);
  212. if def.needs_inittable then
  213. begin
  214. { 'var' instead of 'out' parameter, because 'out' would trigger
  215. calling the initialize method recursively }
  216. if str_parse_method_dec('procedure fpcInitializeRec;',potype_procedure,false,def,pd) then
  217. pd.synthetickind:=tsk_record_initialize
  218. else
  219. internalerror(2011071711);
  220. end;
  221. restore_scanner(sstate);
  222. end;
  223. procedure setup_for_new_class(const scannername: string; out sstate: tscannerstate; out islocal: boolean; out oldsymtablestack: TSymtablestack);
  224. begin
  225. replace_scanner(scannername,sstate);
  226. oldsymtablestack:=symtablestack;
  227. islocal:=symtablestack.top.symtablelevel>=normal_function_level;
  228. if islocal then
  229. begin
  230. { we cannot add a class local to a procedure -> insert it in the
  231. static symtable. This is not ideal because this means that it will
  232. be saved to the ppu file for no good reason, and loaded again
  233. even though it contains a reference to a type that was never
  234. saved to the ppu file (the locally defined enum type). Since this
  235. alias for the locally defined enumtype is only used while
  236. implementing the class' methods, this is however no problem. }
  237. symtablestack:=symtablestack.getcopyuntil(current_module.localsymtable);
  238. end;
  239. end;
  240. procedure restore_after_new_class(const sstate: tscannerstate; const islocal: boolean; const oldsymtablestack: TSymtablestack);
  241. begin
  242. if islocal then
  243. begin
  244. symtablestack.free;
  245. symtablestack:=oldsymtablestack;
  246. end;
  247. restore_scanner(sstate);
  248. end;
  249. procedure jvm_maybe_create_enum_class(const name: TIDString; def: tdef);
  250. var
  251. vmtbuilder: tvmtbuilder;
  252. arrdef: tarraydef;
  253. arrsym: ttypesym;
  254. juhashmap: tdef;
  255. enumclass: tobjectdef;
  256. pd: tprocdef;
  257. old_current_structdef: tabstractrecorddef;
  258. i: longint;
  259. sym,
  260. aliassym: tstaticvarsym;
  261. fsym: tfieldvarsym;
  262. sstate: tscannerstate;
  263. sl: tpropaccesslist;
  264. temptypesym: ttypesym;
  265. oldsymtablestack: tsymtablestack;
  266. islocal: boolean;
  267. begin
  268. { if it's a subrange type, don't create a new class }
  269. if assigned(tenumdef(def).basedef) then
  270. exit;
  271. setup_for_new_class('jvm_enum_class',sstate,islocal,oldsymtablestack);
  272. { create new class (different internal name than enum to prevent name
  273. clash; at unit level because we don't want its methods to be nested
  274. inside a function in case its a local type) }
  275. enumclass:=tobjectdef.create(odt_javaclass,'$'+current_module.realmodulename^+'$'+name+'$InternEnum$'+tostr(def.defid),java_jlenum);
  276. tenumdef(def).classdef:=enumclass;
  277. include(enumclass.objectoptions,oo_is_enum_class);
  278. include(enumclass.objectoptions,oo_is_sealed);
  279. { implement FpcEnumValueObtainable interface }
  280. enumclass.ImplementedInterfaces.add(TImplementedInterface.Create(tobjectdef(search_system_type('FPCENUMVALUEOBTAINABLE').typedef)));
  281. { create an alias for this type inside itself: this way we can choose a
  282. name that can be used in generated Pascal code without risking an
  283. identifier conflict (since it is local to this class; the global name
  284. is unique because it's an identifier that contains $-signs) }
  285. enumclass.symtable.insert(ttypesym.create('__FPC_TEnumClassAlias',enumclass));
  286. { also create an alias for the enum type so that we can iterate over
  287. all enum values when creating the body of the class constructor }
  288. temptypesym:=ttypesym.create('__FPC_TEnumAlias',nil);
  289. { don't pass def to the ttypesym constructor, because then it
  290. will replace the current (real) typesym of that def with the alias }
  291. temptypesym.typedef:=def;
  292. enumclass.symtable.insert(temptypesym);
  293. { but the name of the class as far as the JVM is concerned will match
  294. the enum's original name (the enum type itself won't be output in
  295. any class file, so no conflict there)
  296. name can be empty in case of declaration such as "set of (ea,eb)" }
  297. if not islocal and
  298. (name <> '') then
  299. enumclass.objextname:=stringdup(name)
  300. else
  301. { for local types, use a unique name to prevent conflicts (since such
  302. types are not visible outside the routine anyway, it doesn't matter
  303. }
  304. begin
  305. enumclass.objextname:=stringdup(enumclass.objrealname^);
  306. { also mark it as private (not strict private, because the class
  307. is not a subclass of the unit in which it is declared, so then
  308. the unit's procedures would not be able to use it) }
  309. enumclass.typesym.visibility:=vis_private;
  310. end;
  311. { now add a bunch of extra things to the enum class }
  312. old_current_structdef:=current_structdef;
  313. current_structdef:=enumclass;
  314. symtablestack.push(enumclass.symtable);
  315. { create static fields representing all enums }
  316. for i:=0 to tenumdef(def).symtable.symlist.count-1 do
  317. begin
  318. fsym:=tfieldvarsym.create(tenumsym(tenumdef(def).symtable.symlist[i]).realname,vs_final,enumclass,[]);
  319. enumclass.symtable.insert(fsym);
  320. sym:=make_field_static(enumclass.symtable,fsym);
  321. { add alias for the field representing ordinal(0), for use in
  322. initialization code }
  323. if tenumsym(tenumdef(def).symtable.symlist[i]).value=0 then
  324. begin
  325. aliassym:=tstaticvarsym.create('__FPC_Zero_Initializer',vs_final,enumclass,[vo_is_external]);
  326. enumclass.symtable.insert(aliassym);
  327. aliassym.set_raw_mangledname(sym.mangledname);
  328. end;
  329. end;
  330. { create local "array of enumtype" type for the "values" functionality
  331. (used internally by the JDK) }
  332. arrdef:=tarraydef.create(0,tenumdef(def).symtable.symlist.count-1,s32inttype);
  333. arrdef.elementdef:=enumclass;
  334. arrsym:=ttypesym.create('__FPC_TEnumValues',arrdef);
  335. enumclass.symtable.insert(arrsym);
  336. { insert "public static values: array of enumclass" that returns $VALUES.clone()
  337. (rather than a dynamic array and using clone --which we don't support yet for arrays--
  338. simply use a fixed length array and copy it) }
  339. if not str_parse_method_dec('function values: __FPC_TEnumValues;static;',potype_function,true,enumclass,pd) then
  340. internalerror(2011062301);
  341. include(pd.procoptions,po_staticmethod);
  342. pd.synthetickind:=tsk_jvm_enum_values;
  343. { do we have to store the ordinal value separately? (if no jumps, we can
  344. just call the default ordinal() java.lang.Enum function) }
  345. if tenumdef(def).has_jumps then
  346. begin
  347. { add field for the value }
  348. fsym:=tfieldvarsym.create('__fpc_fenumval',vs_final,s32inttype,[]);
  349. enumclass.symtable.insert(fsym);
  350. tobjectsymtable(enumclass.symtable).addfield(fsym,vis_strictprivate);
  351. { add class field with hash table that maps from FPC-declared ordinal value -> enum instance }
  352. juhashmap:=search_system_type('JUHASHMAP').typedef;
  353. fsym:=tfieldvarsym.create('__fpc_ord2enum',vs_final,juhashmap,[]);
  354. enumclass.symtable.insert(fsym);
  355. make_field_static(enumclass.symtable,fsym);
  356. { add custom constructor }
  357. if not str_parse_method_dec('constructor Create(const __fpc_name: JLString; const __fpc_ord, __fpc_initenumval: longint);',potype_constructor,false,enumclass,pd) then
  358. internalerror(2011062401);
  359. pd.synthetickind:=tsk_jvm_enum_jumps_constr;
  360. pd.visibility:=vis_strictprivate;
  361. end
  362. else
  363. begin
  364. { insert "private constructor(string,int,int)" that calls inherited and
  365. initialises the FPC value field }
  366. add_missing_parent_constructors_intf(enumclass,false,vis_strictprivate);
  367. end;
  368. { add instance method to get the enum's value as declared in FPC }
  369. if not str_parse_method_dec('function FPCOrdinal: longint;',potype_function,false,enumclass,pd) then
  370. internalerror(2011062402);
  371. pd.synthetickind:=tsk_jvm_enum_fpcordinal;
  372. { add static class method to convert an ordinal to the corresponding enum }
  373. if not str_parse_method_dec('function FPCValueOf(__fpc_int: longint): __FPC_TEnumClassAlias; static;',potype_function,true,enumclass,pd) then
  374. internalerror(2011062402);
  375. pd.synthetickind:=tsk_jvm_enum_fpcvalueof;
  376. { similar (instance) function for use in set factories; implements FpcEnumValueObtainable interface }
  377. if not str_parse_method_dec('function fpcGenericValueOf(__fpc_int: longint): JLEnum;',potype_function,false,enumclass,pd) then
  378. internalerror(2011062402);
  379. pd.synthetickind:=tsk_jvm_enum_fpcvalueof;
  380. { insert "public static valueOf(string): tenumclass" that returns tenumclass(inherited valueOf(tenumclass,string)) }
  381. if not str_parse_method_dec('function valueOf(const __fpc_str: JLString): __FPC_TEnumClassAlias; static;',potype_function,true,enumclass,pd) then
  382. internalerror(2011062302);
  383. include(pd.procoptions,po_staticmethod);
  384. pd.synthetickind:=tsk_jvm_enum_valueof;
  385. { add instance method to convert an ordinal and an array into a set of
  386. (we always need/can use both in case of subrange types and/or array
  387. -> set type casts) }
  388. if not str_parse_method_dec('function fpcLongToEnumSet(__val: jlong; __setbase, __setsize: jint): JUEnumSet;',potype_function,true,enumclass,pd) then
  389. internalerror(2011070501);
  390. pd.synthetickind:=tsk_jvm_enum_long2set;
  391. if not str_parse_method_dec('function fpcBitSetToEnumSet(const __val: FpcBitSet; __fromsetbase, __tosetbase: jint): JUEnumSet; static;',potype_function,true,enumclass,pd) then
  392. internalerror(2011071004);
  393. pd.synthetickind:=tsk_jvm_enum_bitset2set;
  394. if not str_parse_method_dec('function fpcEnumSetToEnumSet(const __val: JUEnumSet; __fromsetbase, __tosetbase: jint): JUEnumSet; static;',potype_function,true,enumclass,pd) then
  395. internalerror(2011071005);
  396. pd.synthetickind:=tsk_jvm_enum_set2set;
  397. { create array called "$VALUES" that will contain a reference to all
  398. enum instances (JDK convention)
  399. Disable duplicate identifier checking when inserting, because it will
  400. check for a conflict with "VALUES" ($<id> normally means "check for
  401. <id> without uppercasing first"), which will conflict with the
  402. "Values" instance method -- that's also the reason why we insert the
  403. field only now, because we cannot disable duplicate identifier
  404. checking when creating the "Values" method }
  405. fsym:=tfieldvarsym.create('$VALUES',vs_final,arrdef,[]);
  406. fsym.visibility:=vis_strictprivate;
  407. enumclass.symtable.insert(fsym,false);
  408. sym:=make_field_static(enumclass.symtable,fsym);
  409. { alias for accessing the field in generated Pascal code }
  410. sl:=tpropaccesslist.create;
  411. sl.addsym(sl_load,sym);
  412. enumclass.symtable.insert(tabsolutevarsym.create_ref('__fpc_FVALUES',arrdef,sl));
  413. { add initialization of the static class fields created above }
  414. if not str_parse_method_dec('constructor fpc_enum_class_constructor;',potype_class_constructor,true,enumclass,pd) then
  415. internalerror(2011062303);
  416. pd.synthetickind:=tsk_jvm_enum_classconstr;
  417. symtablestack.pop(enumclass.symtable);
  418. vmtbuilder:=TVMTBuilder.Create(enumclass);
  419. vmtbuilder.generate_vmt;
  420. vmtbuilder.free;
  421. restore_after_new_class(sstate,islocal,oldsymtablestack);
  422. current_structdef:=old_current_structdef;
  423. end;
  424. procedure jvm_create_procvar_class_intern(const name: TIDString; def: tdef; force_no_callback_intf: boolean);
  425. var
  426. vmtbuilder: tvmtbuilder;
  427. oldsymtablestack: tsymtablestack;
  428. pvclass,
  429. pvintf: tobjectdef;
  430. temptypesym: ttypesym;
  431. sstate: tscannerstate;
  432. methoddef: tprocdef;
  433. old_current_structdef: tabstractrecorddef;
  434. islocal: boolean;
  435. begin
  436. { inlined definition of procvar -> generate name, derive from
  437. FpcBaseNestedProcVarType, pass nestedfpstruct to constructor and
  438. copy it }
  439. if name='' then
  440. internalerror(2011071901);
  441. setup_for_new_class('jvm_pvar_class',sstate,islocal,oldsymtablestack);
  442. { create new class (different internal name than pvar to prevent name
  443. clash; at unit level because we don't want its methods to be nested
  444. inside a function in case its a local type) }
  445. pvclass:=tobjectdef.create(odt_javaclass,'$'+current_module.realmodulename^+'$'+name+'$InternProcvar$'+tostr(def.defid),java_procvarbase);
  446. tprocvardef(def).classdef:=pvclass;
  447. include(pvclass.objectoptions,oo_is_sealed);
  448. if df_generic in def.defoptions then
  449. include(pvclass.defoptions,df_generic);
  450. { associate typesym }
  451. pvclass.symtable.insert(ttypesym.create('__FPC_TProcVarClassAlias',pvclass));
  452. { set external name to match procvar type name }
  453. if not islocal then
  454. pvclass.objextname:=stringdup(name)
  455. else
  456. pvclass.objextname:=stringdup(pvclass.objrealname^);
  457. symtablestack.push(pvclass.symtable);
  458. { inherit constructor and keep public }
  459. add_missing_parent_constructors_intf(pvclass,true,vis_public);
  460. { add a method to call the procvar using unwrapped arguments, which
  461. then wraps them and calls through to JLRMethod.invoke }
  462. methoddef:=tprocdef(tprocvardef(def).getcopyas(procdef,pc_bareproc));
  463. finish_copied_procdef(methoddef,'invoke',pvclass.symtable,pvclass);
  464. insert_self_and_vmt_para(methoddef);
  465. methoddef.synthetickind:=tsk_jvm_procvar_invoke;
  466. methoddef.calcparas;
  467. { add local alias for the procvartype that we can use when implementing
  468. the invoke method }
  469. temptypesym:=ttypesym.create('__FPC_ProcVarAlias',nil);
  470. { don't pass def to the ttypesym constructor, because then it
  471. will replace the current (real) typesym of that def with the alias }
  472. temptypesym.typedef:=def;
  473. pvclass.symtable.insert(temptypesym);
  474. { in case of a procedure of object, add a nested interface type that
  475. has one method that conforms to the procvartype (with name
  476. procvartypename+'Callback') and an extra constructor that takes
  477. an instance conforming to this interface and which sets up the
  478. procvar by taking the address of its Callback method (convenient to
  479. use from Java code) }
  480. if (po_methodpointer in tprocvardef(def).procoptions) and
  481. not islocal and
  482. not force_no_callback_intf then
  483. begin
  484. pvintf:=tobjectdef.create(odt_interfacejava,'Callback',nil);
  485. pvintf.objextname:=stringdup('Callback');
  486. if df_generic in def.defoptions then
  487. include(pvintf.defoptions,df_generic);
  488. { associate typesym }
  489. pvclass.symtable.insert(ttypesym.create('Callback',pvintf));
  490. { add a method prototype matching the procvar (like the invoke
  491. in the procvarclass itself) }
  492. symtablestack.push(pvintf.symtable);
  493. methoddef:=tprocdef(tprocvardef(def).getcopyas(procdef,pc_bareproc));
  494. finish_copied_procdef(methoddef,name+'Callback',pvintf.symtable,pvintf);
  495. insert_self_and_vmt_para(methoddef);
  496. { can't be final/static/private/protected, and must be virtual
  497. since it's an interface method }
  498. methoddef.procoptions:=methoddef.procoptions-[po_staticmethod,po_finalmethod];
  499. include(methoddef.procoptions,po_virtualmethod);
  500. methoddef.visibility:=vis_public;
  501. symtablestack.pop(pvintf.symtable);
  502. { add an extra constructor to the procvarclass that takes an
  503. instance of this interface as parameter }
  504. old_current_structdef:=current_structdef;
  505. current_structdef:=pvclass;
  506. if not str_parse_method_dec('constructor Create(__intf:'+pvintf.objextname^+');overload;',potype_constructor,false,pvclass,methoddef) then
  507. internalerror(2011092401);
  508. methoddef.synthetickind:=tsk_jvm_procvar_intconstr;
  509. methoddef.skpara:=def;
  510. current_structdef:=old_current_structdef;
  511. end;
  512. symtablestack.pop(pvclass.symtable);
  513. vmtbuilder:=TVMTBuilder.Create(pvclass);
  514. vmtbuilder.generate_vmt;
  515. vmtbuilder.free;
  516. restore_after_new_class(sstate,islocal,oldsymtablestack);
  517. end;
  518. procedure jvm_create_procvar_class(const name: TIDString; def: tdef);
  519. begin
  520. jvm_create_procvar_class_intern(name,def,false);
  521. end;
  522. procedure jvm_wrap_virtual_class_method(pd: tprocdef);
  523. var
  524. wrapperpd: tprocdef;
  525. wrapperpv: tprocvardef;
  526. typ: ttypesym;
  527. wrappername: shortstring;
  528. begin
  529. if (po_external in pd.procoptions) or
  530. (oo_is_external in pd.struct.objectoptions) then
  531. exit;
  532. { the JVM does not support virtual class methods -> we generate
  533. wrappers with the original name so they can be called normally,
  534. and these wrappers will then perform a dynamic lookup. To enable
  535. calling the class method by its intended name from external Java code,
  536. we have to change its external name so that we give that original
  537. name to the wrapper function -> "switch" the external names around for
  538. the original and wrapper methods }
  539. { replace importname of original procdef }
  540. include(pd.procoptions,po_has_importname);
  541. if not assigned(pd.import_name) then
  542. wrappername:=pd.procsym.realname
  543. else
  544. wrappername:=pd.import_name^;
  545. stringdispose(pd.import_name);
  546. pd.import_name:=stringdup(wrappername+'__fpcvirtualclassmethod__');
  547. { wrapper is part of the same symtable as the original procdef }
  548. symtablestack.push(pd.owner);
  549. { get a copy of the virtual class method }
  550. wrapperpd:=tprocdef(pd.getcopy);
  551. { this one is not virtual nor override }
  552. exclude(wrapperpd.procoptions,po_virtualmethod);
  553. exclude(wrapperpd.procoptions,po_overridingmethod);
  554. { import/external name = name of original class method }
  555. stringdispose(wrapperpd.import_name);
  556. wrapperpd.import_name:=stringdup(wrappername);
  557. include(wrapperpd.procoptions,po_has_importname);
  558. { associate with wrapper procsym (Pascal-level name = wrapper name ->
  559. in callnodes, we will have to replace the calls to virtual class
  560. methods with calls to the wrappers) }
  561. finish_copied_procdef(wrapperpd,pd.import_name^,pd.owner,tabstractrecorddef(pd.owner.defowner));
  562. { we only have to generate the dispatching routine for non-overriding
  563. methods; the overriding ones can use the original one, but generate
  564. a skeleton for that anyway because the overriding one may still
  565. change the visibility (but we can just call the inherited routine
  566. in that case) }
  567. if po_overridingmethod in pd.procoptions then
  568. begin
  569. { by default do not include this routine when looking for overloads }
  570. include(wrapperpd.procoptions,po_ignore_for_overload_resolution);
  571. wrapperpd.synthetickind:=tsk_anon_inherited;
  572. symtablestack.pop(pd.owner);
  573. exit;
  574. end;
  575. { implementation }
  576. wrapperpd.synthetickind:=tsk_jvm_virtual_clmethod;
  577. wrapperpd.skpara:=pd;
  578. { also create procvar type that we can use in the implementation }
  579. wrapperpv:=tprocvardef(pd.getcopyas(procvardef,pc_normal));
  580. wrapperpv.calcparas;
  581. { no use in creating a callback wrapper here, this procvar type isn't
  582. for public consumption }
  583. jvm_create_procvar_class_intern('__fpc_virtualclassmethod_pv_t'+tostr(wrapperpd.defid),wrapperpv,true);
  584. { create alias for the procvar type so we can use it in generated
  585. Pascal code }
  586. typ:=ttypesym.create('__fpc_virtualclassmethod_pv_t'+tostr(wrapperpd.defid),wrapperpv);
  587. wrapperpv.classdef.typesym.visibility:=vis_strictprivate;
  588. symtablestack.top.insert(typ);
  589. symtablestack.pop(pd.owner);
  590. end;
  591. procedure jvm_wrap_virtual_constructor(pd: tprocdef);
  592. var
  593. wrapperpd: tprocdef;
  594. begin
  595. { to avoid having to implement procvar-like support for dynamically
  596. invoking constructors, call the constructors from virtual class
  597. methods and replace calls to the constructors with calls to the
  598. virtual class methods -> we can reuse lots of infrastructure }
  599. if (po_external in pd.procoptions) or
  600. (oo_is_external in pd.struct.objectoptions) then
  601. exit;
  602. { wrapper is part of the same symtable as the original procdef }
  603. symtablestack.push(pd.owner);
  604. { get a copy of the constructor }
  605. wrapperpd:=tprocdef(pd.getcopyas(procdef,pc_bareproc));
  606. { this one is a class method rather than a constructor }
  607. include(wrapperpd.procoptions,po_classmethod);
  608. wrapperpd.proctypeoption:=potype_function;
  609. wrapperpd.returndef:=tobjectdef(pd.owner.defowner);
  610. { import/external name = name of original constructor (since
  611. constructors don't have names in Java, this won't conflict with the
  612. original constructor definition) }
  613. stringdispose(wrapperpd.import_name);
  614. wrapperpd.import_name:=stringdup(pd.procsym.realname);
  615. { associate with wrapper procsym (Pascal-level name = wrapper name ->
  616. in callnodes, we will have to replace the calls to virtual
  617. constructors with calls to the wrappers) }
  618. finish_copied_procdef(wrapperpd,pd.procsym.realname+'__fpcvirtconstrwrapper__',pd.owner,tabstractrecorddef(pd.owner.defowner));
  619. { since it was a bare copy, insert the self parameter (we can't just
  620. copy the vmt parameter from the constructor, that's different) }
  621. insert_self_and_vmt_para(wrapperpd);
  622. wrapperpd.calcparas;
  623. { implementation: call through to the constructor
  624. Exception: if the current class is abstract, do not call the
  625. constructor, since abstract class cannot be constructed (and the
  626. Android verifier does not accept such code, even if it is
  627. unreachable) }
  628. wrapperpd.synthetickind:=tsk_callthrough_nonabstract;
  629. wrapperpd.skpara:=pd;
  630. symtablestack.pop(pd.owner);
  631. { and now wrap this generated virtual static method itself as well }
  632. jvm_wrap_virtual_class_method(wrapperpd);
  633. end;
  634. procedure jvm_wrap_virtual_class_methods(obj: tobjectdef);
  635. var
  636. i: longint;
  637. def: tdef;
  638. begin
  639. { new methods will be inserted while we do this, but since
  640. symtable.deflist.count is evaluated at the start of the loop that
  641. doesn't matter }
  642. for i:=0 to obj.symtable.deflist.count-1 do
  643. begin
  644. def:=tdef(obj.symtable.deflist[i]);
  645. if def.typ<>procdef then
  646. continue;
  647. if [po_classmethod,po_virtualmethod]<=tprocdef(def).procoptions then
  648. jvm_wrap_virtual_class_method(tprocdef(def))
  649. else if (tprocdef(def).proctypeoption=potype_constructor) and
  650. (po_virtualmethod in tprocdef(def).procoptions) then
  651. jvm_wrap_virtual_constructor(tprocdef(def));
  652. end;
  653. end;
  654. function jvm_add_typed_const_initializer(csym: tconstsym): tstaticvarsym;
  655. var
  656. ssym: tstaticvarsym;
  657. esym: tenumsym;
  658. i: longint;
  659. sstate: tscannerstate;
  660. elemdef: tdef;
  661. elemdefname,
  662. conststr: ansistring;
  663. first: boolean;
  664. begin
  665. result:=nil;
  666. esym:=nil;
  667. case csym.constdef.typ of
  668. enumdef:
  669. begin
  670. { make sure we don't emit a definition for this field (we'll do
  671. that for the constsym already) -> mark as external }
  672. ssym:=tstaticvarsym.create(internal_static_field_name(csym.realname),vs_final,csym.constdef,[vo_is_external]);
  673. csym.owner.insert(ssym);
  674. { alias storage to the constsym }
  675. ssym.set_mangledname(csym.realname);
  676. for i:=0 to tenumdef(csym.constdef).symtable.symlist.count-1 do
  677. begin
  678. esym:=tenumsym(tenumdef(csym.constdef).symtable.symlist[i]);
  679. if esym.value=csym.value.valueord.svalue then
  680. break;
  681. esym:=nil;
  682. end;
  683. { can happen in case of explicit typecast from integer constant
  684. to enum type }
  685. if not assigned(esym) then
  686. begin
  687. MessagePos(csym.fileinfo,parser_e_range_check_error);
  688. exit;
  689. end;
  690. replace_scanner('jvm_enum_const',sstate);
  691. str_parse_typedconst(current_asmdata.asmlists[al_typedconsts],esym.name+';',ssym);
  692. restore_scanner(sstate);
  693. result:=ssym;
  694. end;
  695. setdef:
  696. begin
  697. replace_scanner('jvm_set_const',sstate);
  698. { make sure we don't emit a definition for this field (we'll do
  699. that for the constsym already) -> mark as external;
  700. on the other hand, we don't create instances for constsyms in
  701. (or external syms) the program/unit initialization code -> add
  702. vo_has_local_copy to indicate that this should be done after all
  703. (in thlcgjvm.allocate_implicit_structs_for_st_with_base_ref) }
  704. { the constant can be defined in the body of a function and its
  705. def can also belong to that -> will be freed when the function
  706. has been compiler -> insert a copy in the unit's staticsymtable
  707. }
  708. symtablestack.push(current_module.localsymtable);
  709. ssym:=tstaticvarsym.create(internal_static_field_name(csym.realname),vs_final,tsetdef(csym.constdef).getcopy,[vo_is_external,vo_has_local_copy]);
  710. symtablestack.top.insert(ssym);
  711. symtablestack.pop(current_module.localsymtable);
  712. { alias storage to the constsym }
  713. ssym.set_mangledname(csym.realname);
  714. { ensure that we allocate space for global symbols (won't actually
  715. allocate space for this one, since it's external, but for the
  716. constsym) }
  717. cnodeutils.insertbssdata(ssym);
  718. elemdef:=tsetdef(csym.constdef).elementdef;
  719. if not assigned(elemdef) then
  720. begin
  721. internalerror(2011070502);
  722. end
  723. else
  724. begin
  725. elemdefname:=elemdef.typename;
  726. conststr:='[';
  727. first:=true;
  728. for i:=0 to 255 do
  729. if i in pnormalset(csym.value.valueptr)^ then
  730. begin
  731. if not first then
  732. conststr:=conststr+',';
  733. first:=false;
  734. { instead of looking up all enum value names/boolean
  735. names, type cast integers to the required type }
  736. conststr:=conststr+elemdefname+'('+tostr(i)+')';
  737. end;
  738. conststr:=conststr+'];';
  739. end;
  740. str_parse_typedconst(current_asmdata.asmlists[al_typedconsts],conststr,ssym);
  741. restore_scanner(sstate);
  742. result:=ssym;
  743. end;
  744. else
  745. internalerror(2011062701);
  746. end;
  747. end;
  748. function jvm_wrap_method_with_vis(pd: tprocdef; vis: tvisibility): tprocdef;
  749. var
  750. obj: tabstractrecorddef;
  751. visname: string;
  752. begin
  753. obj:=current_structdef;
  754. { if someone gets the idea to add a property to an external class
  755. definition, don't try to wrap it since we cannot add methods to
  756. external classes }
  757. if oo_is_external in obj.objectoptions then
  758. begin
  759. result:=pd;
  760. exit
  761. end;
  762. symtablestack.push(obj.symtable);
  763. result:=tprocdef(pd.getcopy);
  764. result.visibility:=vis;
  765. visname:=visibilityName[vis];
  766. replace(visname,' ','_');
  767. { create a name that is unique amongst all units (start with '$unitname$$') and
  768. unique in this unit (result.defid) }
  769. finish_copied_procdef(result,'$'+current_module.realmodulename^+'$$'+tostr(result.defid)+pd.procsym.realname+'$'+visname,obj.symtable,obj);
  770. { in case the referred method is from an external class }
  771. exclude(result.procoptions,po_external);
  772. { not virtual/override/abstract/... }
  773. result.procoptions:=result.procoptions*[po_classmethod,po_staticmethod,po_varargs,po_public];
  774. result.synthetickind:=tsk_callthrough;
  775. { so we know the name of the routine to call through to }
  776. result.skpara:=pd;
  777. symtablestack.pop(obj.symtable);
  778. end;
  779. procedure jvm_create_getter_or_setter_for_property(p: tpropertysym; orgaccesspd: tprocdef; getter: boolean);
  780. var
  781. obj: tabstractrecorddef;
  782. ps: tprocsym;
  783. pvs: tparavarsym;
  784. sym: tsym;
  785. pd, parentpd, accessorparapd: tprocdef;
  786. tmpaccesslist: tpropaccesslist;
  787. callthroughpropname,
  788. name: string;
  789. callthroughprop: tpropertysym;
  790. accesstyp: tpropaccesslisttypes;
  791. sktype: tsynthetickind;
  792. procoptions: tprocoptions;
  793. paranr: word;
  794. explicitwrapper: boolean;
  795. begin
  796. obj:=current_structdef;
  797. { if someone gets the idea to add a property to an external class
  798. definition, don't try to wrap it since we cannot add methods to
  799. external classes }
  800. if oo_is_external in obj.objectoptions then
  801. exit;
  802. symtablestack.push(obj.symtable);
  803. try
  804. if getter then
  805. accesstyp:=palt_read
  806. else
  807. accesstyp:=palt_write;
  808. { we can't use str_parse_method_dec here because the type of the field
  809. may not be visible at the Pascal level }
  810. explicitwrapper:=
  811. { private methods are not visibile outside the current class, so
  812. no use in making life harder for us by introducing potential
  813. (future or current) naming conflicts }
  814. (p.visibility<>vis_private) and
  815. (getter and
  816. (prop_auto_getter_prefix<>'')) or
  817. (not getter and
  818. (prop_auto_setter_prefix<>''));
  819. sym:=nil;
  820. procoptions:=[];
  821. if explicitwrapper then
  822. begin
  823. if getter then
  824. name:=prop_auto_getter_prefix+p.realname
  825. else
  826. name:=prop_auto_setter_prefix+p.realname;
  827. sym:=search_struct_member_no_helper(obj,upper(name));
  828. if getter then
  829. sktype:=tsk_field_getter
  830. else
  831. sktype:=tsk_field_setter;
  832. if assigned(sym) then
  833. begin
  834. if ((sym.typ<>procsym) or
  835. (tprocsym(sym).procdeflist.count<>1) or
  836. (tprocdef(tprocsym(sym).procdeflist[0]).synthetickind<>sktype)) and
  837. (not assigned(orgaccesspd) or
  838. (sym<>orgaccesspd.procsym)) then
  839. begin
  840. MessagePos2(p.fileinfo,parser_e_cannot_generate_property_getter_setter,name,FullTypeName(tdef(sym.owner.defowner),nil)+'.'+name);
  841. exit;
  842. end
  843. else
  844. begin
  845. if name<>sym.realname then
  846. MessagePos2(p.fileinfo,parser_w_case_difference_auto_property_getter_setter_prefix,sym.realname,name);
  847. { is the specified getter/setter defined in the current
  848. struct and was it originally specified as the getter/
  849. setter for this property? If so, simply adjust its
  850. visibility if necessary.
  851. }
  852. if assigned(orgaccesspd) then
  853. parentpd:=orgaccesspd
  854. else
  855. parentpd:=tprocdef(tprocsym(sym).procdeflist[0]);
  856. if parentpd.owner.defowner=p.owner.defowner then
  857. begin
  858. if parentpd.visibility<p.visibility then
  859. begin
  860. parentpd.visibility:=p.visibility;
  861. include(parentpd.procoptions,po_auto_raised_visibility);
  862. end;
  863. { we are done, no need to create a wrapper }
  864. exit
  865. end
  866. { a parent already included this getter/setter -> try to
  867. override it }
  868. else if parentpd.visibility<>vis_private then
  869. begin
  870. if po_virtualmethod in parentpd.procoptions then
  871. begin
  872. procoptions:=procoptions+[po_virtualmethod,po_overridingmethod];
  873. Message2(parser_w_overriding_property_getter_setter,name,FullTypeName(tdef(parentpd.owner.defowner),nil));
  874. end;
  875. { otherwise we can't do anything, and
  876. proc_add_definition will give an error }
  877. end;
  878. { add method with the correct visibility }
  879. pd:=tprocdef(parentpd.getcopy);
  880. { get rid of the import name for inherited virtual class methods,
  881. it has to be regenerated rather than amended }
  882. if [po_classmethod,po_virtualmethod]<=pd.procoptions then
  883. begin
  884. stringdispose(pd.import_name);
  885. exclude(pd.procoptions,po_has_importname);
  886. end;
  887. pd.visibility:=p.visibility;
  888. pd.procoptions:=pd.procoptions+procoptions;
  889. { ignore this artificially added procdef when looking for overloads }
  890. include(pd.procoptions,po_ignore_for_overload_resolution);
  891. finish_copied_procdef(pd,parentpd.procsym.realname,obj.symtable,obj);
  892. exclude(pd.procoptions,po_external);
  893. pd.synthetickind:=tsk_anon_inherited;
  894. exit;
  895. end;
  896. end;
  897. { make the artificial getter/setter virtual so we can override it in
  898. children if necessary }
  899. if not(sp_static in p.symoptions) and
  900. (obj.typ=objectdef) then
  901. include(procoptions,po_virtualmethod);
  902. { prevent problems in Delphi mode }
  903. include(procoptions,po_overload);
  904. end
  905. else
  906. begin
  907. { construct procsym name (unique for this access; reusing the same
  908. helper for multiple accesses to the same field is hard because the
  909. propacesslist can contain subscript nodes etc) }
  910. name:=visibilityName[p.visibility];
  911. replace(name,' ','_');
  912. if getter then
  913. name:=name+'$getter'
  914. else
  915. name:=name+'$setter';
  916. end;
  917. { create procdef }
  918. if not assigned(orgaccesspd) then
  919. begin
  920. pd:=tprocdef.create(normal_function_level);
  921. if df_generic in obj.defoptions then
  922. include(pd.defoptions,df_generic);
  923. { method of this objectdef }
  924. pd.struct:=obj;
  925. { can only construct the artificial name now, because it requires
  926. pd.defid }
  927. if not explicitwrapper then
  928. name:='$'+obj.symtable.realname^+'$'+p.realname+'$'+name+'$'+tostr(pd.defid);
  929. end
  930. else
  931. begin
  932. { getter/setter could have parameters in case of indexed access
  933. -> copy original procdef }
  934. pd:=tprocdef(orgaccesspd.getcopy);
  935. exclude(pd.procoptions,po_abstractmethod);
  936. { can only construct the artificial name now, because it requires
  937. pd.defid }
  938. if not explicitwrapper then
  939. name:='$'+obj.symtable.realname^+'$'+p.realname+'$'+name+'$'+tostr(pd.defid);
  940. finish_copied_procdef(pd,name,obj.symtable,obj);
  941. sym:=pd.procsym;
  942. end;
  943. { add previously collected procoptions }
  944. pd.procoptions:=pd.procoptions+procoptions;
  945. { visibility }
  946. pd.visibility:=p.visibility;
  947. { new procsym? }
  948. if not assigned(sym) or
  949. (sym.owner<>p.owner) then
  950. begin
  951. ps:=tprocsym.create(name);
  952. obj.symtable.insert(ps);
  953. end
  954. else
  955. ps:=tprocsym(sym);
  956. { associate procsym with procdef}
  957. pd.procsym:=ps;
  958. { function/procedure }
  959. accessorparapd:=nil;
  960. if getter then
  961. begin
  962. pd.proctypeoption:=potype_function;
  963. pd.synthetickind:=tsk_field_getter;
  964. { result type }
  965. pd.returndef:=p.propdef;
  966. if (ppo_hasparameters in p.propoptions) and
  967. not assigned(orgaccesspd) then
  968. accessorparapd:=pd;
  969. end
  970. else
  971. begin
  972. pd.proctypeoption:=potype_procedure;
  973. pd.synthetickind:=tsk_field_setter;
  974. pd.returndef:=voidtype;
  975. if not assigned(orgaccesspd) then
  976. begin
  977. { parameter with value to set }
  978. pvs:=tparavarsym.create('__fpc_newval__',10,vs_const,p.propdef,[]);
  979. pd.parast.insert(pvs);
  980. end;
  981. if (ppo_hasparameters in p.propoptions) and
  982. not assigned(orgaccesspd) then
  983. accessorparapd:=pd;
  984. end;
  985. { create a property for the old symaccesslist with a new name, so that
  986. we can reuse it in the implementation (rather than having to
  987. translate the symaccesslist back to Pascal code) }
  988. callthroughpropname:='__fpc__'+p.realname;
  989. if getter then
  990. callthroughpropname:=callthroughpropname+'__getter_wrapper'
  991. else
  992. callthroughpropname:=callthroughpropname+'__setter_wrapper';
  993. callthroughprop:=tpropertysym.create(callthroughpropname);
  994. callthroughprop.visibility:=p.visibility;
  995. if getter then
  996. p.makeduplicate(callthroughprop,accessorparapd,nil,paranr)
  997. else
  998. p.makeduplicate(callthroughprop,nil,accessorparapd,paranr);
  999. callthroughprop.default:=longint($80000000);
  1000. callthroughprop.default:=0;
  1001. callthroughprop.propoptions:=callthroughprop.propoptions-[ppo_stored,ppo_enumerator_current,ppo_overrides,ppo_defaultproperty];
  1002. if sp_static in p.symoptions then
  1003. include(callthroughprop.symoptions, sp_static);
  1004. { copy original property target to callthrough property (and replace
  1005. original one with the new empty list; will be filled in later) }
  1006. tmpaccesslist:=callthroughprop.propaccesslist[accesstyp];
  1007. callthroughprop.propaccesslist[accesstyp]:=p.propaccesslist[accesstyp];
  1008. p.propaccesslist[accesstyp]:=tmpaccesslist;
  1009. p.owner.insert(callthroughprop);
  1010. pd.skpara:=callthroughprop;
  1011. { needs to be exported }
  1012. include(pd.procoptions,po_global);
  1013. { class property -> static class method }
  1014. if sp_static in p.symoptions then
  1015. pd.procoptions:=pd.procoptions+[po_classmethod,po_staticmethod];
  1016. { in case we made a copy of the original accessor, this has all been
  1017. done already }
  1018. if not assigned(orgaccesspd) then
  1019. begin
  1020. { calling convention, self, ... }
  1021. if obj.typ=recorddef then
  1022. handle_calling_convention(pd,[hcc_check])
  1023. else
  1024. handle_calling_convention(pd,hcc_all);
  1025. { register forward declaration with procsym }
  1026. proc_add_definition(pd);
  1027. end;
  1028. { make the property call this new function }
  1029. p.propaccesslist[accesstyp].addsym(sl_call,ps);
  1030. p.propaccesslist[accesstyp].procdef:=pd;
  1031. finally
  1032. symtablestack.pop(obj.symtable);
  1033. end;
  1034. end;
  1035. procedure jvm_create_getter_for_property(p: tpropertysym; orgaccesspd: tprocdef);
  1036. begin
  1037. jvm_create_getter_or_setter_for_property(p,orgaccesspd,true);
  1038. end;
  1039. procedure jvm_create_setter_for_property(p: tpropertysym; orgaccesspd: tprocdef);
  1040. begin
  1041. jvm_create_getter_or_setter_for_property(p,orgaccesspd,false);
  1042. end;
  1043. end.