symcpu.pas 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  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.getcopy);
  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.getcopy);
  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, self, ... }
  431. if obj.typ=recorddef then
  432. handle_calling_convention(pd,[hcc_declaration,hcc_check])
  433. else
  434. handle_calling_convention(pd,hcc_default_actions_intf);
  435. { register forward declaration with procsym }
  436. proc_add_definition(pd);
  437. end;
  438. { make the property call this new function }
  439. propaccesslist[accesstyp].addsym(sl_call,ps);
  440. propaccesslist[accesstyp].procdef:=pd;
  441. finally
  442. symtablestack.pop(obj.symtable);
  443. end;
  444. end;
  445. procedure tcpupropertysym.finalize_getter_or_setter_for_sym(getset: tpropaccesslisttypes; sym: tsym; fielddef: tdef; accessordef: tprocdef);
  446. var
  447. orgaccesspd: tprocdef;
  448. pprefix: pshortstring;
  449. wrongvisibility: boolean;
  450. begin
  451. inherited;
  452. if getset=palt_read then
  453. pprefix:=@prop_auto_getter_prefix
  454. else
  455. pprefix:=@prop_auto_setter_prefix;
  456. case sym.typ of
  457. procsym:
  458. begin
  459. orgaccesspd:=tprocdef(propaccesslist[getset].procdef);
  460. wrongvisibility:=tprocdef(propaccesslist[getset].procdef).visibility<visibility;
  461. { if the visibility of the accessor is lower than
  462. the visibility of the property, wrap it so that
  463. we can call it from all contexts in which the
  464. property is visible }
  465. if wrongvisibility or
  466. ((pprefix^<>'') and
  467. (sym.RealName<>pprefix^+RealName)) then
  468. create_getter_or_setter_for_property(orgaccesspd,getset=palt_read)
  469. end;
  470. fieldvarsym:
  471. begin
  472. { if the visibility of the field is lower than the
  473. visibility of the property, wrap it in a getter
  474. so that we can access it from all contexts in
  475. which the property is visibile }
  476. if (pprefix^<>'') or
  477. (tfieldvarsym(sym).visibility<visibility) then
  478. create_getter_or_setter_for_property(nil,getset=palt_read);
  479. end;
  480. else
  481. internalerror(2014061101);
  482. end;
  483. end;
  484. procedure tcpupropertysym.maybe_create_overridden_getter_or_setter(getset: tpropaccesslisttypes);
  485. var
  486. sym: tsym;
  487. accessordef: tprocdef;
  488. psym: tpropertysym;
  489. begin
  490. { find the last defined getter/setter/field accessed by an inherited
  491. property }
  492. psym:=overriddenpropsym;
  493. while not assigned(psym.propaccesslist[getset].firstsym) do
  494. begin
  495. psym:=psym.overriddenpropsym;
  496. { if there is simply no getter/setter for this property, we're done }
  497. if not assigned(psym) then
  498. exit;
  499. end;
  500. sym:=psym.propaccesslist[getset].firstsym^.sym;
  501. case sym.typ of
  502. procsym:
  503. begin
  504. accessordef:=tprocdef(psym.propaccesslist[getset].procdef);
  505. if accessordef.visibility>=visibility then
  506. exit;
  507. end;
  508. fieldvarsym:
  509. begin
  510. if sym.visibility>=visibility then
  511. exit;
  512. accessordef:=nil;
  513. end;
  514. else
  515. internalerror(2014061102);
  516. end;
  517. propaccesslist[getset]:=psym.propaccesslist[getset].getcopy;
  518. finalize_getter_or_setter_for_sym(getset,sym,propdef,accessordef);
  519. end;
  520. procedure tcpupropertysym.inherit_accessor(getset: tpropaccesslisttypes);
  521. begin
  522. inherited;
  523. { new property has higher visibility than previous one -> maybe override
  524. the getters/setters }
  525. if assigned(overriddenpropsym) and
  526. (overriddenpropsym.visibility<visibility) then
  527. maybe_create_overridden_getter_or_setter(getset);
  528. end;
  529. {****************************************************************************
  530. tcpuenumdef
  531. ****************************************************************************}
  532. procedure tcpuenumdef.ppuload_platform(ppufile: tcompilerppufile);
  533. begin
  534. inherited;
  535. ppufile.getderef(classdefderef);
  536. end;
  537. procedure tcpuenumdef.ppuwrite_platform(ppufile: tcompilerppufile);
  538. begin
  539. inherited;
  540. ppufile.putderef(classdefderef);
  541. end;
  542. function tcpuenumdef.getcopy: tstoreddef;
  543. begin
  544. result:=inherited;
  545. tcpuenumdef(result).classdef:=classdef;
  546. end;
  547. procedure tcpuenumdef.buildderef;
  548. begin
  549. inherited;
  550. classdefderef.build(classdef);
  551. end;
  552. procedure tcpuenumdef.deref;
  553. begin
  554. inherited;
  555. classdef:=tobjectdef(classdefderef.resolve);
  556. end;
  557. {****************************************************************************
  558. tcpuprocdef
  559. ****************************************************************************}
  560. function tcpuprocdef.jvmmangledbasename(signature: boolean): TSymStr;
  561. var
  562. vs: tparavarsym;
  563. i: longint;
  564. founderror: tdef;
  565. tmpresult: TSymStr;
  566. container: tsymtable;
  567. begin
  568. { format:
  569. * method definition (in Jasmin):
  570. (private|protected|public) [static] method(parametertypes)returntype
  571. * method invocation
  572. package/class/method(parametertypes)returntype
  573. -> store common part: method(parametertypes)returntype and
  574. adorn as required when using it.
  575. }
  576. if not signature then
  577. begin
  578. { method name }
  579. { special names for constructors and class constructors }
  580. if proctypeoption=potype_constructor then
  581. tmpresult:='<init>'
  582. else if proctypeoption in [potype_class_constructor,potype_unitinit] then
  583. tmpresult:='<clinit>'
  584. else if po_has_importname in procoptions then
  585. begin
  586. if assigned(import_name) then
  587. tmpresult:=import_name^
  588. else
  589. internalerror(2010122608);
  590. end
  591. else
  592. begin
  593. tmpresult:=procsym.realname;
  594. if tmpresult[1]='$' then
  595. tmpresult:=copy(tmpresult,2,length(tmpresult)-1);
  596. { nested functions }
  597. container:=owner;
  598. while container.symtabletype=localsymtable do
  599. begin
  600. tmpresult:='$'+tprocdef(owner.defowner).procsym.realname+'$$'+tprocdef(owner.defowner).unique_id_str+'$'+tmpresult;
  601. container:=container.defowner.owner;
  602. end;
  603. end;
  604. end
  605. else
  606. tmpresult:='';
  607. { parameter types }
  608. tmpresult:=tmpresult+'(';
  609. { not the case for the main program (not required for defaultmangledname
  610. because setmangledname() is called for the main program; in case of
  611. the JVM, this only sets the importname, however) }
  612. if assigned(paras) then
  613. begin
  614. init_paraloc_info(callerside);
  615. for i:=0 to paras.count-1 do
  616. begin
  617. vs:=tparavarsym(paras[i]);
  618. { function result is not part of the mangled name }
  619. if vo_is_funcret in vs.varoptions then
  620. continue;
  621. { self pointer neither, except for class methods (the JVM only
  622. supports static class methods natively, so the self pointer
  623. here is a regular parameter as far as the JVM is concerned }
  624. if not(po_classmethod in procoptions) and
  625. (vo_is_self in vs.varoptions) then
  626. continue;
  627. { passing by reference is emulated by passing an array of one
  628. element containing the value; for types that aren't pointers
  629. in regular Pascal, simply passing the underlying pointer type
  630. does achieve regular call-by-reference semantics though;
  631. formaldefs always have to be passed like that because their
  632. contents can be replaced }
  633. if paramanager.push_copyout_param(vs.varspez,vs.vardef,proccalloption) then
  634. tmpresult:=tmpresult+'[';
  635. { Add the parameter type. }
  636. if not jvmaddencodedtype(vs.vardef,false,tmpresult,signature,founderror) then
  637. { an internalerror here is also triggered in case of errors in the source code }
  638. tmpresult:='<error>';
  639. end;
  640. end;
  641. tmpresult:=tmpresult+')';
  642. { And the type of the function result (void in case of a procedure and
  643. constructor). }
  644. if (proctypeoption in [potype_constructor,potype_class_constructor]) then
  645. jvmaddencodedtype(voidtype,false,tmpresult,signature,founderror)
  646. else if not jvmaddencodedtype(returndef,false,tmpresult,signature,founderror) then
  647. { an internalerror here is also triggered in case of errors in the source code }
  648. tmpresult:='<error>';
  649. result:=tmpresult;
  650. end;
  651. function tcpuprocdef.mangledname: TSymStr;
  652. begin
  653. if _mangledname='' then
  654. begin
  655. result:=jvmmangledbasename(false);
  656. if (po_has_importdll in procoptions) then
  657. begin
  658. { import_dll comes from "external 'import_dll_name' name 'external_name'" }
  659. if assigned(import_dll) then
  660. result:=import_dll^+'/'+result
  661. else
  662. internalerror(2010122607);
  663. end
  664. else
  665. jvmaddtypeownerprefix(owner,mangledname);
  666. _mangledname:=result;
  667. end
  668. else
  669. result:=_mangledname;
  670. end;
  671. function tcpuprocdef.getfuncretsyminfo(out ressym: tsym; out resdef: tdef): boolean;
  672. begin
  673. { constructors don't have a result on the JVM platform }
  674. if proctypeoption<>potype_constructor then
  675. result:=inherited
  676. else
  677. result:=false;
  678. end;
  679. destructor tcpuprocdef.destroy;
  680. begin
  681. exprasmlist.free;
  682. inherited destroy;
  683. end;
  684. {****************************************************************************
  685. tcpuprocvardef
  686. ****************************************************************************}
  687. procedure tcpuprocvardef.ppuwrite_platform(ppufile: tcompilerppufile);
  688. begin
  689. inherited;
  690. ppufile.putderef(classdefderef);
  691. end;
  692. procedure tcpuprocvardef.ppuload_platform(ppufile: tcompilerppufile);
  693. begin
  694. inherited;
  695. ppufile.getderef(classdefderef);
  696. end;
  697. procedure tcpuprocvardef.buildderef;
  698. begin
  699. inherited buildderef;
  700. classdefderef.build(classdef);
  701. end;
  702. procedure tcpuprocvardef.deref;
  703. begin
  704. inherited deref;
  705. classdef:=tobjectdef(classdefderef.resolve);
  706. end;
  707. function tcpuprocvardef.getcopy: tstoreddef;
  708. begin
  709. result:=inherited;
  710. tcpuprocvardef(result).classdef:=classdef;
  711. end;
  712. {****************************************************************************
  713. tcpuprocsym
  714. ****************************************************************************}
  715. procedure tcpuprocsym.check_forward;
  716. var
  717. curri, checki: longint;
  718. currpd, checkpd: tprocdef;
  719. begin
  720. inherited;
  721. { check for conflicts based on mangled name, because several FPC
  722. types/constructs map to the same JVM mangled name }
  723. for curri:=0 to FProcdefList.Count-2 do
  724. begin
  725. currpd:=tprocdef(FProcdefList[curri]);
  726. if (po_external in currpd.procoptions) or
  727. (currpd.proccalloption=pocall_internproc) then
  728. continue;
  729. for checki:=curri+1 to FProcdefList.Count-1 do
  730. begin
  731. checkpd:=tprocdef(FProcdefList[checki]);
  732. if po_external in checkpd.procoptions then
  733. continue;
  734. if currpd.mangledname=checkpd.mangledname then
  735. begin
  736. MessagePos(checkpd.fileinfo,parser_e_overloaded_have_same_mangled_name);
  737. MessagePos1(currpd.fileinfo,sym_e_param_list,currpd.customprocname([pno_mangledname]));
  738. MessagePos1(checkpd.fileinfo,sym_e_param_list,checkpd.customprocname([pno_mangledname]));
  739. end;
  740. end;
  741. end;
  742. inherited;
  743. end;
  744. {****************************************************************************
  745. tcpustaticvarsym
  746. ****************************************************************************}
  747. procedure tcpustaticvarsym.set_mangledname(const s: TSymStr);
  748. begin
  749. inherited;
  750. _mangledname:=jvmmangledbasename(self,s,false);
  751. jvmaddtypeownerprefix(owner,_mangledname);
  752. end;
  753. function tcpustaticvarsym.mangledname: TSymStr;
  754. begin
  755. if _mangledname='' then
  756. begin
  757. if _mangledbasename='' then
  758. _mangledname:=jvmmangledbasename(self,false)
  759. else
  760. _mangledname:=jvmmangledbasename(self,_mangledbasename,false);
  761. jvmaddtypeownerprefix(owner,_mangledname);
  762. end;
  763. result:=_mangledname;
  764. end;
  765. {****************************************************************************
  766. tcpufieldvarsym
  767. ****************************************************************************}
  768. procedure tcpufieldvarsym.set_externalname(const s: string);
  769. begin
  770. { make sure it is recalculated }
  771. cachedmangledname:='';
  772. if is_java_class_or_interface(tdef(owner.defowner)) then
  773. begin
  774. externalname:=stringdup(s);
  775. include(varoptions,vo_has_mangledname);
  776. end
  777. else
  778. internalerror(2011031201);
  779. end;
  780. function tcpufieldvarsym.mangledname: TSymStr;
  781. begin
  782. if is_java_class_or_interface(tdef(owner.defowner)) or
  783. (tdef(owner.defowner).typ=recorddef) then
  784. begin
  785. if cachedmangledname<>'' then
  786. result:=cachedmangledname
  787. else
  788. begin
  789. result:=jvmmangledbasename(self,false);
  790. jvmaddtypeownerprefix(owner,result);
  791. cachedmangledname:=result;
  792. end;
  793. end
  794. else
  795. result:=inherited;
  796. end;
  797. begin
  798. { used tdef classes }
  799. cfiledef:=tcpufiledef;
  800. cvariantdef:=tcpuvariantdef;
  801. cformaldef:=tcpuformaldef;
  802. cforwarddef:=tcpuforwarddef;
  803. cundefineddef:=tcpuundefineddef;
  804. cerrordef:=tcpuerrordef;
  805. cpointerdef:=tcpupointerdef;
  806. crecorddef:=tcpurecorddef;
  807. cimplementedinterface:=tcpuimplementedinterface;
  808. cobjectdef:=tcpuobjectdef;
  809. cclassrefdef:=tcpuclassrefdef;
  810. carraydef:=tcpuarraydef;
  811. corddef:=tcpuorddef;
  812. cfloatdef:=tcpufloatdef;
  813. cprocvardef:=tcpuprocvardef;
  814. cprocdef:=tcpuprocdef;
  815. cstringdef:=tcpustringdef;
  816. cenumdef:=tcpuenumdef;
  817. csetdef:=tcpusetdef;
  818. { used tsym classes }
  819. clabelsym:=tcpulabelsym;
  820. cunitsym:=tcpuunitsym;
  821. cprogramparasym:=tcpuprogramparasym;
  822. cnamespacesym:=tcpunamespacesym;
  823. cprocsym:=tcpuprocsym;
  824. ctypesym:=tcputypesym;
  825. cfieldvarsym:=tcpufieldvarsym;
  826. clocalvarsym:=tcpulocalvarsym;
  827. cparavarsym:=tcpuparavarsym;
  828. cstaticvarsym:=tcpustaticvarsym;
  829. cabsolutevarsym:=tcpuabsolutevarsym;
  830. cpropertysym:=tcpupropertysym;
  831. cconstsym:=tcpuconstsym;
  832. cenumsym:=tcpuenumsym;
  833. csyssym:=tcpusyssym;
  834. end.