symcpu.pas 32 KB

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