symcpu.pas 32 KB

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