symcpu.pas 32 KB

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