symcpu.pas 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. {
  2. Copyright (c) 2014 by Florian Klaempfl
  3. Symbol table overrides for JVM
  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. unit symcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,
  22. aasmdata,
  23. symtype,
  24. symdef,symsym;
  25. type
  26. { defs }
  27. tcpufiledef = class(tfiledef)
  28. end;
  29. tcpufiledefclass = class of tcpufiledef;
  30. tcpuvariantdef = class(tvariantdef)
  31. end;
  32. tcpuvariantdefclass = class of tcpuvariantdef;
  33. tcpuformaldef = class(tformaldef)
  34. end;
  35. tcpuformaldefclass = class of tcpuformaldef;
  36. tcpuforwarddef = class(tforwarddef)
  37. end;
  38. tcpuforwarddefclass = class of tcpuforwarddef;
  39. tcpuundefineddef = class(tundefineddef)
  40. end;
  41. tcpuundefineddefclass = class of tcpuundefineddef;
  42. tcpuerrordef = class(terrordef)
  43. end;
  44. tcpuerrordefclass = class of tcpuerrordef;
  45. tcpupointerdef = class(tpointerdef)
  46. end;
  47. tcpupointerdefclass = class of tcpupointerdef;
  48. tcpurecorddef = class(trecorddef)
  49. end;
  50. tcpurecorddefclass = class of tcpurecorddef;
  51. tcpuimplementedinterface = class(timplementedinterface)
  52. end;
  53. tcpuimplementedinterfaceclass = class of tcpuimplementedinterface;
  54. tcpuobjectdef = class(tobjectdef)
  55. end;
  56. tcpuobjectdefclass = class of tcpuobjectdef;
  57. tcpuclassrefdef = class(tclassrefdef)
  58. end;
  59. tcpuclassrefdefclass = class of tcpuclassrefdef;
  60. tcpuarraydef = class(tarraydef)
  61. end;
  62. tcpuarraydefclass = class of tcpuarraydef;
  63. tcpuorddef = class(torddef)
  64. end;
  65. tcpuorddefclass = class of tcpuorddef;
  66. tcpufloatdef = class(tfloatdef)
  67. end;
  68. tcpufloatdefclass = class of tcpufloatdef;
  69. tcpuprocvardef = class(tprocvardef)
  70. protected
  71. procedure ppuwrite_platform(ppufile: tcompilerppufile); override;
  72. procedure ppuload_platform(ppufile: tcompilerppufile); override;
  73. public
  74. { class representing this procvar on the Java side }
  75. classdef : tobjectdef;
  76. classdefderef : tderef;
  77. procedure buildderef;override;
  78. procedure deref;override;
  79. function getcopy: tstoreddef; override;
  80. end;
  81. tcpuprocvardefclass = class of tcpuprocvardef;
  82. tcpuprocdef = class(tprocdef)
  83. { generated assembler code; used by JVM backend so it can afterwards
  84. easily write out all methods grouped per class }
  85. exprasmlist : TAsmList;
  86. function jvmmangledbasename(signature: boolean): TSymStr;
  87. function mangledname: TSymStr; override;
  88. function getfuncretsyminfo(out ressym: tsym; out resdef: tdef): boolean; override;
  89. destructor destroy; override;
  90. end;
  91. tcpuprocdefclass = class of tcpuprocdef;
  92. tcpustringdef = class(tstringdef)
  93. end;
  94. tcpustringdefclass = class of tcpustringdef;
  95. tcpuenumdef = class(tenumdef)
  96. protected
  97. procedure ppuload_platform(ppufile: tcompilerppufile); override;
  98. procedure ppuwrite_platform(ppufile: tcompilerppufile); override;
  99. public
  100. { class representing this enum on the Java side }
  101. classdef : tobjectdef;
  102. classdefderef : tderef;
  103. function getcopy: tstoreddef; override;
  104. procedure buildderef; override;
  105. procedure deref; override;
  106. end;
  107. tcpuenumdefclass = class of tcpuenumdef;
  108. tcpusetdef = class(tsetdef)
  109. end;
  110. tcpusetdefclass = class of tcpusetdef;
  111. { syms }
  112. tcpulabelsym = class(tlabelsym)
  113. end;
  114. tcpulabelsymclass = class of tcpulabelsym;
  115. tcpuunitsym = class(tunitsym)
  116. end;
  117. tcpuunitsymclass = class of tcpuunitsym;
  118. tcpuprogramparasym = class(tprogramparasym)
  119. end;
  120. tcpuprogramparasymclass = class(tprogramparasym);
  121. tcpunamespacesym = class(tnamespacesym)
  122. end;
  123. tcpunamespacesymclass = class of tcpunamespacesym;
  124. tcpuprocsym = class(tprocsym)
  125. procedure check_forward; override;
  126. end;
  127. tcpuprocsymclass = class of tcpuprocsym;
  128. tcputypesym = class(ttypesym)
  129. end;
  130. tcpuypesymclass = class of tcputypesym;
  131. tcpufieldvarsym = class(tfieldvarsym)
  132. procedure set_externalname(const s: string); override;
  133. function mangledname: TSymStr; override;
  134. end;
  135. tcpufieldvarsymclass = class of tcpufieldvarsym;
  136. tcpulocalvarsym = class(tlocalvarsym)
  137. end;
  138. tcpulocalvarsymclass = class of tcpulocalvarsym;
  139. tcpuparavarsym = class(tparavarsym)
  140. end;
  141. tcpuparavarsymclass = class of tcpuparavarsym;
  142. tcpustaticvarsym = class(tstaticvarsym)
  143. procedure set_mangledname(const s: TSymStr); override;
  144. function mangledname: TSymStr; override;
  145. end;
  146. tcpustaticvarsymclass = class of tcpustaticvarsym;
  147. tcpuabsolutevarsym = class(tabsolutevarsym)
  148. end;
  149. tcpuabsolutevarsymclass = class of tcpuabsolutevarsym;
  150. tcpupropertysym = class(tpropertysym)
  151. protected
  152. { when a private/protected field is exposed via a property with a higher
  153. visibility, then we have to create a getter and/or setter with that same
  154. higher visibility to make sure that using the property does not result
  155. in JVM verification errors }
  156. procedure create_getter_or_setter_for_property(orgaccesspd: tprocdef; getter: boolean);
  157. procedure finalize_getter_or_setter_for_sym(getset: tpropaccesslisttypes; sym: tsym; fielddef: tdef; accessordef: tprocdef); override;
  158. procedure maybe_create_overridden_getter_or_setter(getset: tpropaccesslisttypes);
  159. public
  160. procedure inherit_accessor(getset: tpropaccesslisttypes); override;
  161. end;
  162. tcpupropertysymclass = class of tcpupropertysym;
  163. tcpuconstsym = class(tconstsym)
  164. end;
  165. tcpuconstsymclass = class of tcpuconstsym;
  166. tcpuenumsym = class(tenumsym)
  167. end;
  168. tcpuenumsymclass = class of tcpuenumsym;
  169. tcpusyssym = class(tsyssym)
  170. end;
  171. tcpusyssymclass = class of tcpusyssym;
  172. const
  173. pbestrealtype : ^tdef = @s64floattype;
  174. implementation
  175. uses
  176. verbose,cutils,cclasses,globals,
  177. symconst,symbase,symtable,symcreat,jvmdef,
  178. pdecsub,pparautl,pjvm,
  179. paramgr;
  180. {****************************************************************************
  181. tcpuproptertysym
  182. ****************************************************************************}
  183. procedure tcpupropertysym.create_getter_or_setter_for_property(orgaccesspd: tprocdef; getter: boolean);
  184. var
  185. obj: tabstractrecorddef;
  186. ps: tprocsym;
  187. pvs: tparavarsym;
  188. sym: tsym;
  189. pd, parentpd, accessorparapd: tprocdef;
  190. tmpaccesslist: tpropaccesslist;
  191. callthroughpropname,
  192. accessorname: string;
  193. callthroughprop: tpropertysym;
  194. accesstyp: tpropaccesslisttypes;
  195. accessortyp: tprocoption;
  196. procoptions: tprocoptions;
  197. paranr: word;
  198. explicitwrapper: boolean;
  199. begin
  200. obj:=current_structdef;
  201. { if someone gets the idea to add a property to an external class
  202. definition, don't try to wrap it since we cannot add methods to
  203. external classes }
  204. if oo_is_external in obj.objectoptions then
  205. exit;
  206. symtablestack.push(obj.symtable);
  207. try
  208. if getter then
  209. accesstyp:=palt_read
  210. else
  211. accesstyp:=palt_write;
  212. { we can't use str_parse_method_dec here because the type of the field
  213. may not be visible at the Pascal level }
  214. explicitwrapper:=
  215. { private methods are not visibile outside the current class, so
  216. no use in making life harder for us by introducing potential
  217. (future or current) naming conflicts }
  218. (visibility<>vis_private) and
  219. (getter and
  220. (prop_auto_getter_prefix<>'')) or
  221. (not getter and
  222. (prop_auto_setter_prefix<>''));
  223. sym:=nil;
  224. if getter then
  225. accessortyp:=po_is_auto_getter
  226. else
  227. accessortyp:=po_is_auto_setter;
  228. procoptions:=[accessortyp];
  229. if explicitwrapper then
  230. begin
  231. if getter then
  232. accessorname:=prop_auto_getter_prefix+realname
  233. else
  234. accessorname:=prop_auto_setter_prefix+realname;
  235. sym:=search_struct_member_no_helper(obj,upper(accessorname));
  236. if assigned(sym) then
  237. begin
  238. if ((sym.typ<>procsym) or
  239. (tprocsym(sym).procdeflist.count<>1) or
  240. not(accessortyp in tprocdef(tprocsym(sym).procdeflist[0]).procoptions)) and
  241. (not assigned(orgaccesspd) or
  242. (sym<>orgaccesspd.procsym)) then
  243. begin
  244. MessagePos2(fileinfo,parser_e_cannot_generate_property_getter_setter,accessorname,FullTypeName(tdef(sym.owner.defowner),nil)+'.'+accessorname);
  245. exit;
  246. end
  247. else
  248. begin
  249. if accessorname<>sym.realname then
  250. MessagePos2(fileinfo,parser_w_case_difference_auto_property_getter_setter_prefix,sym.realname,accessorname);
  251. { is the specified getter/setter defined in the current
  252. struct and was it originally specified as the getter/
  253. setter for this property? If so, simply adjust its
  254. visibility if necessary.
  255. }
  256. if assigned(orgaccesspd) then
  257. parentpd:=orgaccesspd
  258. else
  259. parentpd:=tprocdef(tprocsym(sym).procdeflist[0]);
  260. if parentpd.owner.defowner=owner.defowner then
  261. begin
  262. if parentpd.visibility<visibility then
  263. begin
  264. parentpd.visibility:=visibility;
  265. include(parentpd.procoptions,po_auto_raised_visibility);
  266. end;
  267. { we are done, no need to create a wrapper }
  268. exit
  269. end
  270. { a parent already included this getter/setter -> try to
  271. override it }
  272. else if parentpd.visibility<>vis_private then
  273. begin
  274. if po_virtualmethod in parentpd.procoptions then
  275. begin
  276. procoptions:=procoptions+[po_virtualmethod,po_overridingmethod];
  277. if not(parentpd.synthetickind in [tsk_field_getter,tsk_field_setter]) then
  278. Message2(parser_w_overriding_property_getter_setter,accessorname,FullTypeName(tdef(parentpd.owner.defowner),nil));
  279. end;
  280. { otherwise we can't do anything, and
  281. proc_add_definition will give an error }
  282. end;
  283. { add method with the correct visibility }
  284. pd:=tprocdef(parentpd.getcopyas(procdef,pc_normal_no_hidden,''));
  285. { get rid of the import accessorname for inherited virtual class methods,
  286. it has to be regenerated rather than amended }
  287. if [po_classmethod,po_virtualmethod]<=pd.procoptions then
  288. begin
  289. stringdispose(pd.import_name);
  290. exclude(pd.procoptions,po_has_importname);
  291. end;
  292. pd.visibility:=visibility;
  293. pd.procoptions:=pd.procoptions+procoptions;
  294. { ignore this artificially added procdef when looking for overloads }
  295. include(pd.procoptions,po_ignore_for_overload_resolution);
  296. finish_copied_procdef(pd,parentpd.procsym.realname,obj.symtable,obj);
  297. exclude(pd.procoptions,po_external);
  298. pd.synthetickind:=tsk_anon_inherited;
  299. { set the accessor in the property }
  300. propaccesslist[accesstyp].clear;
  301. propaccesslist[accesstyp].addsym(sl_call,pd.procsym);
  302. propaccesslist[accesstyp].procdef:=pd;
  303. exit;
  304. end;
  305. end;
  306. { make the artificial getter/setter virtual so we can override it in
  307. children if necessary }
  308. if not(sp_static in symoptions) and
  309. (obj.typ=objectdef) then
  310. include(procoptions,po_virtualmethod);
  311. { prevent problems in Delphi mode }
  312. include(procoptions,po_overload);
  313. end
  314. else
  315. begin
  316. { construct procsym accessorname (unique for this access; reusing the same
  317. helper for multiple accesses to the same field is hard because the
  318. propacesslist can contain subscript nodes etc) }
  319. accessorname:=visibilityName[visibility];
  320. replace(accessorname,' ','_');
  321. if getter then
  322. accessorname:=accessorname+'$getter'
  323. else
  324. accessorname:=accessorname+'$setter';
  325. end;
  326. { create procdef }
  327. if not assigned(orgaccesspd) then
  328. begin
  329. pd:=cprocdef.create(normal_function_level,true);
  330. if df_generic in obj.defoptions then
  331. include(pd.defoptions,df_generic);
  332. { method of this objectdef }
  333. pd.struct:=obj;
  334. { can only construct the artificial accessorname now, because it requires
  335. pd.unique_id_str }
  336. if not explicitwrapper then
  337. accessorname:='$'+obj.symtable.realname^+'$'+realname+'$'+accessorname+'$'+pd.unique_id_str;
  338. end
  339. else
  340. begin
  341. { getter/setter could have parameters in case of indexed access
  342. -> copy original procdef }
  343. pd:=tprocdef(orgaccesspd.getcopyas(procdef,pc_normal_no_hidden,''));
  344. exclude(pd.procoptions,po_abstractmethod);
  345. exclude(pd.procoptions,po_overridingmethod);
  346. { can only construct the artificial accessorname now, because it requires
  347. pd.unique_id_str }
  348. if not explicitwrapper then
  349. accessorname:='$'+obj.symtable.realname^+'$'+realname+'$'+accessorname+'$'+pd.unique_id_str;
  350. finish_copied_procdef(pd,accessorname,obj.symtable,obj);
  351. sym:=pd.procsym;
  352. end;
  353. { add previously collected procoptions }
  354. pd.procoptions:=pd.procoptions+procoptions;
  355. { visibility }
  356. pd.visibility:=visibility;
  357. { new procsym? }
  358. if not assigned(sym) or
  359. (sym.owner<>owner) then
  360. begin
  361. ps:=cprocsym.create(accessorname);
  362. obj.symtable.insert(ps);
  363. end
  364. else
  365. ps:=tprocsym(sym);
  366. { associate procsym with procdef}
  367. pd.procsym:=ps;
  368. { function/procedure }
  369. accessorparapd:=nil;
  370. if getter then
  371. begin
  372. pd.proctypeoption:=potype_function;
  373. pd.synthetickind:=tsk_field_getter;
  374. { result type }
  375. pd.returndef:=propdef;
  376. if (ppo_hasparameters in propoptions) and
  377. not assigned(orgaccesspd) then
  378. accessorparapd:=pd;
  379. end
  380. else
  381. begin
  382. pd.proctypeoption:=potype_procedure;
  383. pd.synthetickind:=tsk_field_setter;
  384. pd.returndef:=voidtype;
  385. if not assigned(orgaccesspd) then
  386. begin
  387. { parameter with value to set }
  388. pvs:=cparavarsym.create('__fpc_newval__',10,vs_const,propdef,[]);
  389. pd.parast.insert(pvs);
  390. end;
  391. if (ppo_hasparameters in propoptions) and
  392. not assigned(orgaccesspd) then
  393. accessorparapd:=pd;
  394. end;
  395. { create a property for the old symaccesslist with a new accessorname, so that
  396. we can reuse it in the implementation (rather than having to
  397. translate the symaccesslist back to Pascal code) }
  398. callthroughpropname:='__fpc__'+realname;
  399. if getter then
  400. callthroughpropname:=callthroughpropname+'__getter_wrapper'
  401. else
  402. callthroughpropname:=callthroughpropname+'__setter_wrapper';
  403. callthroughprop:=cpropertysym.create(callthroughpropname);
  404. callthroughprop.visibility:=visibility;
  405. if getter then
  406. makeduplicate(callthroughprop,accessorparapd,nil,paranr)
  407. else
  408. makeduplicate(callthroughprop,nil,accessorparapd,paranr);
  409. callthroughprop.default:=longint($80000000);
  410. callthroughprop.default:=0;
  411. callthroughprop.propoptions:=callthroughprop.propoptions-[ppo_stored,ppo_enumerator_current,ppo_overrides,ppo_defaultproperty];
  412. if sp_static in symoptions then
  413. include(callthroughprop.symoptions, sp_static);
  414. { copy original property target to callthrough property (and replace
  415. original one with the new empty list; will be filled in later) }
  416. tmpaccesslist:=callthroughprop.propaccesslist[accesstyp];
  417. callthroughprop.propaccesslist[accesstyp]:=propaccesslist[accesstyp];
  418. propaccesslist[accesstyp]:=tmpaccesslist;
  419. owner.insert(callthroughprop);
  420. pd.skpara:=callthroughprop;
  421. { needs to be exported }
  422. include(pd.procoptions,po_global);
  423. { class property -> static class method }
  424. if sp_static in symoptions then
  425. pd.procoptions:=pd.procoptions+[po_classmethod,po_staticmethod];
  426. { in case we made a copy of the original accessor, this has all been
  427. done already }
  428. if not assigned(orgaccesspd) then
  429. begin
  430. { calling convention }
  431. handle_calling_convention(pd,hcc_default_actions_intf_struct);
  432. { register forward declaration with procsym }
  433. proc_add_definition(pd);
  434. end;
  435. { make the property call this new function }
  436. propaccesslist[accesstyp].addsym(sl_call,ps);
  437. propaccesslist[accesstyp].procdef:=pd;
  438. finally
  439. symtablestack.pop(obj.symtable);
  440. end;
  441. end;
  442. procedure tcpupropertysym.finalize_getter_or_setter_for_sym(getset: tpropaccesslisttypes; sym: tsym; fielddef: tdef; accessordef: tprocdef);
  443. var
  444. orgaccesspd: tprocdef;
  445. pprefix: pshortstring;
  446. wrongvisibility: boolean;
  447. begin
  448. inherited;
  449. if getset=palt_read then
  450. pprefix:=@prop_auto_getter_prefix
  451. else
  452. pprefix:=@prop_auto_setter_prefix;
  453. case sym.typ of
  454. procsym:
  455. begin
  456. orgaccesspd:=tprocdef(propaccesslist[getset].procdef);
  457. wrongvisibility:=tprocdef(propaccesslist[getset].procdef).visibility<visibility;
  458. { if the visibility of the accessor is lower than
  459. the visibility of the property, wrap it so that
  460. we can call it from all contexts in which the
  461. property is visible }
  462. if wrongvisibility or
  463. ((pprefix^<>'') and
  464. (sym.RealName<>pprefix^+RealName)) then
  465. create_getter_or_setter_for_property(orgaccesspd,getset=palt_read)
  466. end;
  467. fieldvarsym:
  468. begin
  469. { if the visibility of the field is lower than the
  470. visibility of the property, wrap it in a getter
  471. so that we can access it from all contexts in
  472. which the property is visibile }
  473. if (pprefix^<>'') or
  474. (tfieldvarsym(sym).visibility<visibility) then
  475. create_getter_or_setter_for_property(nil,getset=palt_read);
  476. end;
  477. else
  478. internalerror(2014061101);
  479. end;
  480. end;
  481. procedure tcpupropertysym.maybe_create_overridden_getter_or_setter(getset: tpropaccesslisttypes);
  482. var
  483. sym: tsym;
  484. accessordef: tprocdef;
  485. psym: tpropertysym;
  486. begin
  487. { find the last defined getter/setter/field accessed by an inherited
  488. property }
  489. psym:=overriddenpropsym;
  490. while not assigned(psym.propaccesslist[getset].firstsym) do
  491. begin
  492. psym:=psym.overriddenpropsym;
  493. { if there is simply no getter/setter for this property, we're done }
  494. if not assigned(psym) then
  495. exit;
  496. end;
  497. sym:=psym.propaccesslist[getset].firstsym^.sym;
  498. case sym.typ of
  499. procsym:
  500. begin
  501. accessordef:=tprocdef(psym.propaccesslist[getset].procdef);
  502. if accessordef.visibility>=visibility then
  503. exit;
  504. end;
  505. fieldvarsym:
  506. begin
  507. if sym.visibility>=visibility then
  508. exit;
  509. accessordef:=nil;
  510. end;
  511. else
  512. internalerror(2014061102);
  513. end;
  514. propaccesslist[getset]:=psym.propaccesslist[getset].getcopy;
  515. finalize_getter_or_setter_for_sym(getset,sym,propdef,accessordef);
  516. end;
  517. procedure tcpupropertysym.inherit_accessor(getset: tpropaccesslisttypes);
  518. begin
  519. inherited;
  520. { new property has higher visibility than previous one -> maybe override
  521. the getters/setters }
  522. if assigned(overriddenpropsym) and
  523. (overriddenpropsym.visibility<visibility) then
  524. maybe_create_overridden_getter_or_setter(getset);
  525. end;
  526. {****************************************************************************
  527. tcpuenumdef
  528. ****************************************************************************}
  529. procedure tcpuenumdef.ppuload_platform(ppufile: tcompilerppufile);
  530. begin
  531. inherited;
  532. ppufile.getderef(classdefderef);
  533. end;
  534. procedure tcpuenumdef.ppuwrite_platform(ppufile: tcompilerppufile);
  535. begin
  536. inherited;
  537. ppufile.putderef(classdefderef);
  538. end;
  539. function tcpuenumdef.getcopy: tstoreddef;
  540. begin
  541. result:=inherited;
  542. tcpuenumdef(result).classdef:=classdef;
  543. end;
  544. procedure tcpuenumdef.buildderef;
  545. begin
  546. inherited;
  547. classdefderef.build(classdef);
  548. end;
  549. procedure tcpuenumdef.deref;
  550. begin
  551. inherited;
  552. classdef:=tobjectdef(classdefderef.resolve);
  553. end;
  554. {****************************************************************************
  555. tcpuprocdef
  556. ****************************************************************************}
  557. function tcpuprocdef.jvmmangledbasename(signature: boolean): TSymStr;
  558. var
  559. vs: tparavarsym;
  560. i: longint;
  561. founderror: tdef;
  562. tmpresult: TSymStr;
  563. container: tsymtable;
  564. begin
  565. { format:
  566. * method definition (in Jasmin):
  567. (private|protected|public) [static] method(parametertypes)returntype
  568. * method invocation
  569. package/class/method(parametertypes)returntype
  570. -> store common part: method(parametertypes)returntype and
  571. adorn as required when using it.
  572. }
  573. if not signature then
  574. begin
  575. { method name }
  576. { special names for constructors and class constructors }
  577. if proctypeoption=potype_constructor then
  578. tmpresult:='<init>'
  579. else if proctypeoption in [potype_class_constructor,potype_unitinit] then
  580. tmpresult:='<clinit>'
  581. else if po_has_importname in procoptions then
  582. begin
  583. if assigned(import_name) then
  584. tmpresult:=import_name^
  585. else
  586. internalerror(2010122608);
  587. end
  588. else
  589. begin
  590. tmpresult:=procsym.realname;
  591. if tmpresult[1]='$' then
  592. tmpresult:=copy(tmpresult,2,length(tmpresult)-1);
  593. { nested functions }
  594. container:=owner;
  595. while container.symtabletype=localsymtable do
  596. begin
  597. tmpresult:='$'+tprocdef(owner.defowner).procsym.realname+'$$'+tprocdef(owner.defowner).unique_id_str+'$'+tmpresult;
  598. container:=container.defowner.owner;
  599. end;
  600. end;
  601. end
  602. else
  603. tmpresult:='';
  604. { parameter types }
  605. tmpresult:=tmpresult+'(';
  606. { not the case for the main program (not required for defaultmangledname
  607. because setmangledname() is called for the main program; in case of
  608. the JVM, this only sets the importname, however) }
  609. if assigned(paras) then
  610. begin
  611. for i:=0 to paras.count-1 do
  612. begin
  613. vs:=tparavarsym(paras[i]);
  614. { function result is not part of the mangled name }
  615. if vo_is_funcret in vs.varoptions then
  616. continue;
  617. { self pointer neither, except for class methods (the JVM only
  618. supports static class methods natively, so the self pointer
  619. here is a regular parameter as far as the JVM is concerned }
  620. if not(po_classmethod in procoptions) and
  621. (vo_is_self in vs.varoptions) then
  622. continue;
  623. { passing by reference is emulated by passing an array of one
  624. element containing the value; for types that aren't pointers
  625. in regular Pascal, simply passing the underlying pointer type
  626. does achieve regular call-by-reference semantics though;
  627. formaldefs always have to be passed like that because their
  628. contents can be replaced }
  629. if paramanager.push_copyout_param(vs.varspez,vs.vardef,proccalloption) then
  630. tmpresult:=tmpresult+'[';
  631. { Add the parameter type. }
  632. if not jvmaddencodedtype(vs.vardef,false,tmpresult,signature,founderror) then
  633. { an internalerror here is also triggered in case of errors in the source code }
  634. tmpresult:='<error>';
  635. end;
  636. end;
  637. tmpresult:=tmpresult+')';
  638. { And the type of the function result (void in case of a procedure and
  639. constructor). }
  640. if (proctypeoption in [potype_constructor,potype_class_constructor]) then
  641. jvmaddencodedtype(voidtype,false,tmpresult,signature,founderror)
  642. else if not jvmaddencodedtype(returndef,false,tmpresult,signature,founderror) then
  643. { an internalerror here is also triggered in case of errors in the source code }
  644. tmpresult:='<error>';
  645. result:=tmpresult;
  646. end;
  647. function tcpuprocdef.mangledname: TSymStr;
  648. begin
  649. if _mangledname='' then
  650. begin
  651. result:=jvmmangledbasename(false);
  652. if (po_has_importdll in procoptions) then
  653. begin
  654. { import_dll comes from "external 'import_dll_name' name 'external_name'" }
  655. if assigned(import_dll) then
  656. result:=import_dll^+'/'+result
  657. else
  658. internalerror(2010122607);
  659. end
  660. else
  661. jvmaddtypeownerprefix(owner,mangledname);
  662. _mangledname:=result;
  663. end
  664. else
  665. result:=_mangledname;
  666. end;
  667. function tcpuprocdef.getfuncretsyminfo(out ressym: tsym; out resdef: tdef): boolean;
  668. begin
  669. { constructors don't have a result on the JVM platform }
  670. if proctypeoption<>potype_constructor then
  671. result:=inherited
  672. else
  673. result:=false;
  674. end;
  675. destructor tcpuprocdef.destroy;
  676. begin
  677. exprasmlist.free;
  678. inherited destroy;
  679. end;
  680. {****************************************************************************
  681. tcpuprocvardef
  682. ****************************************************************************}
  683. procedure tcpuprocvardef.ppuwrite_platform(ppufile: tcompilerppufile);
  684. begin
  685. inherited;
  686. ppufile.putderef(classdefderef);
  687. end;
  688. procedure tcpuprocvardef.ppuload_platform(ppufile: tcompilerppufile);
  689. begin
  690. inherited;
  691. ppufile.getderef(classdefderef);
  692. end;
  693. procedure tcpuprocvardef.buildderef;
  694. begin
  695. inherited buildderef;
  696. classdefderef.build(classdef);
  697. end;
  698. procedure tcpuprocvardef.deref;
  699. begin
  700. inherited deref;
  701. classdef:=tobjectdef(classdefderef.resolve);
  702. end;
  703. function tcpuprocvardef.getcopy: tstoreddef;
  704. begin
  705. result:=inherited;
  706. tcpuprocvardef(result).classdef:=classdef;
  707. end;
  708. {****************************************************************************
  709. tcpuprocsym
  710. ****************************************************************************}
  711. procedure tcpuprocsym.check_forward;
  712. var
  713. curri, checki: longint;
  714. currpd, checkpd: tprocdef;
  715. begin
  716. inherited;
  717. { check for conflicts based on mangled name, because several FPC
  718. types/constructs map to the same JVM mangled name }
  719. for curri:=0 to FProcdefList.Count-2 do
  720. begin
  721. currpd:=tprocdef(FProcdefList[curri]);
  722. if (po_external in currpd.procoptions) or
  723. (currpd.proccalloption=pocall_internproc) then
  724. continue;
  725. for checki:=curri+1 to FProcdefList.Count-1 do
  726. begin
  727. checkpd:=tprocdef(FProcdefList[checki]);
  728. if po_external in checkpd.procoptions then
  729. continue;
  730. if currpd.mangledname=checkpd.mangledname then
  731. begin
  732. MessagePos(checkpd.fileinfo,parser_e_overloaded_have_same_mangled_name);
  733. MessagePos1(currpd.fileinfo,sym_e_param_list,currpd.customprocname([pno_mangledname]));
  734. MessagePos1(checkpd.fileinfo,sym_e_param_list,checkpd.customprocname([pno_mangledname]));
  735. end;
  736. end;
  737. end;
  738. inherited;
  739. end;
  740. {****************************************************************************
  741. tcpustaticvarsym
  742. ****************************************************************************}
  743. procedure tcpustaticvarsym.set_mangledname(const s: TSymStr);
  744. begin
  745. inherited;
  746. _mangledname:=jvmmangledbasename(self,s,false);
  747. jvmaddtypeownerprefix(owner,_mangledname);
  748. end;
  749. function tcpustaticvarsym.mangledname: TSymStr;
  750. begin
  751. if _mangledname='' then
  752. begin
  753. if _mangledbasename='' then
  754. _mangledname:=jvmmangledbasename(self,false)
  755. else
  756. _mangledname:=jvmmangledbasename(self,_mangledbasename,false);
  757. jvmaddtypeownerprefix(owner,_mangledname);
  758. end;
  759. result:=_mangledname;
  760. end;
  761. {****************************************************************************
  762. tcpufieldvarsym
  763. ****************************************************************************}
  764. procedure tcpufieldvarsym.set_externalname(const s: string);
  765. begin
  766. { make sure it is recalculated }
  767. cachedmangledname:='';
  768. if is_java_class_or_interface(tdef(owner.defowner)) then
  769. begin
  770. externalname:=stringdup(s);
  771. include(varoptions,vo_has_mangledname);
  772. end
  773. else
  774. internalerror(2011031201);
  775. end;
  776. function tcpufieldvarsym.mangledname: TSymStr;
  777. begin
  778. if is_java_class_or_interface(tdef(owner.defowner)) or
  779. (tdef(owner.defowner).typ=recorddef) then
  780. begin
  781. if cachedmangledname<>'' then
  782. result:=cachedmangledname
  783. else
  784. begin
  785. result:=jvmmangledbasename(self,false);
  786. jvmaddtypeownerprefix(owner,result);
  787. cachedmangledname:=result;
  788. end;
  789. end
  790. else
  791. result:=inherited;
  792. end;
  793. begin
  794. { used tdef classes }
  795. cfiledef:=tcpufiledef;
  796. cvariantdef:=tcpuvariantdef;
  797. cformaldef:=tcpuformaldef;
  798. cforwarddef:=tcpuforwarddef;
  799. cundefineddef:=tcpuundefineddef;
  800. cerrordef:=tcpuerrordef;
  801. cpointerdef:=tcpupointerdef;
  802. crecorddef:=tcpurecorddef;
  803. cimplementedinterface:=tcpuimplementedinterface;
  804. cobjectdef:=tcpuobjectdef;
  805. cclassrefdef:=tcpuclassrefdef;
  806. carraydef:=tcpuarraydef;
  807. corddef:=tcpuorddef;
  808. cfloatdef:=tcpufloatdef;
  809. cprocvardef:=tcpuprocvardef;
  810. cprocdef:=tcpuprocdef;
  811. cstringdef:=tcpustringdef;
  812. cenumdef:=tcpuenumdef;
  813. csetdef:=tcpusetdef;
  814. { used tsym classes }
  815. clabelsym:=tcpulabelsym;
  816. cunitsym:=tcpuunitsym;
  817. cprogramparasym:=tcpuprogramparasym;
  818. cnamespacesym:=tcpunamespacesym;
  819. cprocsym:=tcpuprocsym;
  820. ctypesym:=tcputypesym;
  821. cfieldvarsym:=tcpufieldvarsym;
  822. clocalvarsym:=tcpulocalvarsym;
  823. cparavarsym:=tcpuparavarsym;
  824. cstaticvarsym:=tcpustaticvarsym;
  825. cabsolutevarsym:=tcpuabsolutevarsym;
  826. cpropertysym:=tcpupropertysym;
  827. cconstsym:=tcpuconstsym;
  828. cenumsym:=tcpuenumsym;
  829. csyssym:=tcpusyssym;
  830. end.