symcpu.pas 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. {
  2. Copyright (c) 2014 by Florian Klaempfl
  3. Symbol table overrides for JVM
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit symcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,
  22. aasmdata,
  23. symtype,
  24. symdef,symsym;
  25. type
  26. { defs }
  27. tcpufiledef = class(tfiledef)
  28. end;
  29. tcpufiledefclass = class of tcpufiledef;
  30. tcpuvariantdef = class(tvariantdef)
  31. end;
  32. tcpuvariantdefclass = class of tcpuvariantdef;
  33. tcpuformaldef = class(tformaldef)
  34. end;
  35. tcpuformaldefclass = class of tcpuformaldef;
  36. tcpuforwarddef = class(tforwarddef)
  37. end;
  38. tcpuforwarddefclass = class of tcpuforwarddef;
  39. tcpuundefineddef = class(tundefineddef)
  40. end;
  41. tcpuundefineddefclass = class of tcpuundefineddef;
  42. tcpuerrordef = class(terrordef)
  43. end;
  44. tcpuerrordefclass = class of tcpuerrordef;
  45. tcpupointerdef = class(tpointerdef)
  46. end;
  47. tcpupointerdefclass = class of tcpupointerdef;
  48. tcpurecorddef = class(trecorddef)
  49. end;
  50. tcpurecorddefclass = class of tcpurecorddef;
  51. tcpuimplementedinterface = class(timplementedinterface)
  52. end;
  53. tcpuimplementedinterfaceclass = class of tcpuimplementedinterface;
  54. tcpuobjectdef = class(tobjectdef)
  55. end;
  56. tcpuobjectdefclass = class of tcpuobjectdef;
  57. tcpuclassrefdef = class(tclassrefdef)
  58. end;
  59. tcpuclassrefdefclass = class of tcpuclassrefdef;
  60. tcpuarraydef = class(tarraydef)
  61. end;
  62. tcpuarraydefclass = class of tcpuarraydef;
  63. tcpuorddef = class(torddef)
  64. end;
  65. tcpuorddefclass = class of tcpuorddef;
  66. tcpufloatdef = class(tfloatdef)
  67. end;
  68. tcpufloatdefclass = class of tcpufloatdef;
  69. tcpuprocvardef = class(tprocvardef)
  70. protected
  71. procedure ppuwrite_platform(ppufile: tcompilerppufile); override;
  72. procedure ppuload_platform(ppufile: tcompilerppufile); override;
  73. public
  74. { class representing this procvar on the Java side }
  75. classdef : tobjectdef;
  76. classdefderef : tderef;
  77. procedure buildderef;override;
  78. procedure deref;override;
  79. function getcopy: tstoreddef; override;
  80. end;
  81. tcpuprocvardefclass = class of tcpuprocvardef;
  82. tcpuprocdef = class(tprocdef)
  83. { generated assembler code; used by JVM backend so it can afterwards
  84. easily write out all methods grouped per class }
  85. exprasmlist : TAsmList;
  86. function jvmmangledbasename(signature: boolean): TSymStr;
  87. function mangledname: TSymStr; override;
  88. 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. ((pprefix^<>'') and
  461. (sym.RealName<>pprefix^+RealName)) then
  462. newaccesspd:=create_getter_or_setter_for_property(orgaccesspd,getset=palt_read)
  463. end;
  464. fieldvarsym:
  465. begin
  466. { if the visibility of the field is lower than the
  467. visibility of the property, wrap it in a getter
  468. so that we can access it from all contexts in
  469. which the property is visibile }
  470. if (pprefix^<>'') or
  471. (tfieldvarsym(sym).visibility<visibility) then
  472. newaccesspd:=create_getter_or_setter_for_property(nil,getset=palt_read);
  473. end;
  474. else
  475. internalerror(2014061101);
  476. end;
  477. { update the getter/setter used for this property (already done in case
  478. a new method was created from scratch, but not if we overrode a
  479. getter/setter generated for the inherited property) }
  480. if assigned(newaccesspd) then
  481. begin
  482. if propaccesslist[getset].firstsym^.sym.typ<>procsym then
  483. internalerror(2014061201);
  484. propaccesslist[getset].procdef:=newaccesspd;
  485. propaccesslist[getset].firstsym^.sym:=newaccesspd.procsym;
  486. end;
  487. end;
  488. procedure tcpupropertysym.register_override(overriddenprop: tpropertysym);
  489. var
  490. sym: tsym;
  491. begin
  492. inherited;
  493. { new property has higher visibility than previous one -> maybe override
  494. the getters/setters }
  495. if (overriddenprop.visibility<visibility) then
  496. begin
  497. maybe_create_overridden_getter_or_setter(palt_read);
  498. maybe_create_overridden_getter_or_setter(palt_write);
  499. end;
  500. end;
  501. procedure tcpupropertysym.maybe_create_overridden_getter_or_setter(getset: tpropaccesslisttypes);
  502. var
  503. sym: tsym;
  504. fielddef: tdef;
  505. accessordef: tprocdef;
  506. psym: tpropertysym;
  507. begin
  508. { find the last defined getter/setter/field accessed by an inherited
  509. property }
  510. psym:=overriddenpropsym;
  511. while not assigned(psym.propaccesslist[getset].firstsym) do
  512. begin
  513. psym:=psym.overriddenpropsym;
  514. { if there is simply no getter/setter for this property, we're done }
  515. if not assigned(psym) then
  516. exit;
  517. end;
  518. sym:=psym.propaccesslist[getset].firstsym^.sym;
  519. case sym.typ of
  520. procsym:
  521. begin
  522. accessordef:=tprocdef(psym.propaccesslist[getset].procdef);
  523. if accessordef.visibility>=visibility then
  524. exit;
  525. fielddef:=nil;
  526. end;
  527. fieldvarsym:
  528. begin
  529. if sym.visibility>=visibility then
  530. exit;
  531. accessordef:=nil;
  532. fielddef:=tfieldvarsym(sym).vardef;
  533. end;
  534. else
  535. internalerror(2014061102);
  536. end;
  537. propaccesslist[getset]:=psym.propaccesslist[getset].getcopy;
  538. finalize_getter_or_setter_for_sym(getset,sym,propdef,accessordef);
  539. end;
  540. {****************************************************************************
  541. tcpuenumdef
  542. ****************************************************************************}
  543. procedure tcpuenumdef.ppuload_platform(ppufile: tcompilerppufile);
  544. begin
  545. inherited;
  546. ppufile.getderef(classdefderef);
  547. end;
  548. procedure tcpuenumdef.ppuwrite_platform(ppufile: tcompilerppufile);
  549. begin
  550. inherited;
  551. ppufile.putderef(classdefderef);
  552. end;
  553. function tcpuenumdef.getcopy: tstoreddef;
  554. begin
  555. result:=inherited;
  556. tcpuenumdef(result).classdef:=classdef;
  557. end;
  558. procedure tcpuenumdef.buildderef;
  559. begin
  560. inherited;
  561. classdefderef.build(classdef);
  562. end;
  563. procedure tcpuenumdef.deref;
  564. begin
  565. inherited;
  566. classdef:=tobjectdef(classdefderef.resolve);
  567. end;
  568. {****************************************************************************
  569. tcpuprocdef
  570. ****************************************************************************}
  571. function tcpuprocdef.jvmmangledbasename(signature: boolean): TSymStr;
  572. var
  573. vs: tparavarsym;
  574. i: longint;
  575. founderror: tdef;
  576. tmpresult: TSymStr;
  577. container: tsymtable;
  578. begin
  579. { format:
  580. * method definition (in Jasmin):
  581. (private|protected|public) [static] method(parametertypes)returntype
  582. * method invocation
  583. package/class/method(parametertypes)returntype
  584. -> store common part: method(parametertypes)returntype and
  585. adorn as required when using it.
  586. }
  587. if not signature then
  588. begin
  589. { method name }
  590. { special names for constructors and class constructors }
  591. if proctypeoption=potype_constructor then
  592. tmpresult:='<init>'
  593. else if proctypeoption in [potype_class_constructor,potype_unitinit] then
  594. tmpresult:='<clinit>'
  595. else if po_has_importname in procoptions then
  596. begin
  597. if assigned(import_name) then
  598. tmpresult:=import_name^
  599. else
  600. internalerror(2010122608);
  601. end
  602. else
  603. begin
  604. tmpresult:=procsym.realname;
  605. if tmpresult[1]='$' then
  606. tmpresult:=copy(tmpresult,2,length(tmpresult)-1);
  607. { nested functions }
  608. container:=owner;
  609. while container.symtabletype=localsymtable do
  610. begin
  611. tmpresult:='$'+tprocdef(owner.defowner).procsym.realname+'$'+tostr(tprocdef(owner.defowner).procsym.symid)+'$'+tmpresult;
  612. container:=container.defowner.owner;
  613. end;
  614. end;
  615. end
  616. else
  617. tmpresult:='';
  618. { parameter types }
  619. tmpresult:=tmpresult+'(';
  620. { not the case for the main program (not required for defaultmangledname
  621. because setmangledname() is called for the main program; in case of
  622. the JVM, this only sets the importname, however) }
  623. if assigned(paras) then
  624. begin
  625. init_paraloc_info(callerside);
  626. for i:=0 to paras.count-1 do
  627. begin
  628. vs:=tparavarsym(paras[i]);
  629. { function result is not part of the mangled name }
  630. if vo_is_funcret in vs.varoptions then
  631. continue;
  632. { self pointer neither, except for class methods (the JVM only
  633. supports static class methods natively, so the self pointer
  634. here is a regular parameter as far as the JVM is concerned }
  635. if not(po_classmethod in procoptions) and
  636. (vo_is_self in vs.varoptions) then
  637. continue;
  638. { passing by reference is emulated by passing an array of one
  639. element containing the value; for types that aren't pointers
  640. in regular Pascal, simply passing the underlying pointer type
  641. does achieve regular call-by-reference semantics though;
  642. formaldefs always have to be passed like that because their
  643. contents can be replaced }
  644. if paramanager.push_copyout_param(vs.varspez,vs.vardef,proccalloption) then
  645. tmpresult:=tmpresult+'[';
  646. { Add the parameter type. }
  647. if not jvmaddencodedtype(vs.vardef,false,tmpresult,signature,founderror) then
  648. { an internalerror here is also triggered in case of errors in the source code }
  649. tmpresult:='<error>';
  650. end;
  651. end;
  652. tmpresult:=tmpresult+')';
  653. { And the type of the function result (void in case of a procedure and
  654. constructor). }
  655. if (proctypeoption in [potype_constructor,potype_class_constructor]) then
  656. jvmaddencodedtype(voidtype,false,tmpresult,signature,founderror)
  657. else if not jvmaddencodedtype(returndef,false,tmpresult,signature,founderror) then
  658. { an internalerror here is also triggered in case of errors in the source code }
  659. tmpresult:='<error>';
  660. result:=tmpresult;
  661. end;
  662. function tcpuprocdef.mangledname: TSymStr;
  663. begin
  664. if _mangledname='' then
  665. begin
  666. result:=jvmmangledbasename(false);
  667. if (po_has_importdll in procoptions) then
  668. begin
  669. { import_dll comes from "external 'import_dll_name' name 'external_name'" }
  670. if assigned(import_dll) then
  671. result:=import_dll^+'/'+result
  672. else
  673. internalerror(2010122607);
  674. end
  675. else
  676. jvmaddtypeownerprefix(owner,mangledname);
  677. _mangledname:=result;
  678. end
  679. else
  680. result:=_mangledname;
  681. end;
  682. destructor tcpuprocdef.destroy;
  683. begin
  684. exprasmlist.free;
  685. inherited destroy;
  686. end;
  687. {****************************************************************************
  688. tcpuprocvardef
  689. ****************************************************************************}
  690. procedure tcpuprocvardef.ppuwrite_platform(ppufile: tcompilerppufile);
  691. begin
  692. inherited;
  693. ppufile.putderef(classdefderef);
  694. end;
  695. procedure tcpuprocvardef.ppuload_platform(ppufile: tcompilerppufile);
  696. begin
  697. inherited;
  698. ppufile.getderef(classdefderef);
  699. end;
  700. procedure tcpuprocvardef.buildderef;
  701. begin
  702. inherited buildderef;
  703. classdefderef.build(classdef);
  704. end;
  705. procedure tcpuprocvardef.deref;
  706. begin
  707. inherited deref;
  708. classdef:=tobjectdef(classdefderef.resolve);
  709. end;
  710. function tcpuprocvardef.getcopy: tstoreddef;
  711. begin
  712. result:=inherited;
  713. tcpuprocvardef(result).classdef:=classdef;
  714. end;
  715. {****************************************************************************
  716. tcpuprocsym
  717. ****************************************************************************}
  718. procedure tcpuprocsym.check_forward;
  719. var
  720. curri, checki: longint;
  721. currpd, checkpd: tprocdef;
  722. begin
  723. inherited;
  724. { check for conflicts based on mangled name, because several FPC
  725. types/constructs map to the same JVM mangled name }
  726. for curri:=0 to FProcdefList.Count-2 do
  727. begin
  728. currpd:=tprocdef(FProcdefList[curri]);
  729. if (po_external in currpd.procoptions) or
  730. (currpd.proccalloption=pocall_internproc) then
  731. continue;
  732. for checki:=curri+1 to FProcdefList.Count-1 do
  733. begin
  734. checkpd:=tprocdef(FProcdefList[checki]);
  735. if po_external in checkpd.procoptions then
  736. continue;
  737. if currpd.mangledname=checkpd.mangledname then
  738. begin
  739. MessagePos(checkpd.fileinfo,parser_e_overloaded_have_same_mangled_name);
  740. MessagePos1(currpd.fileinfo,sym_e_param_list,currpd.customprocname([pno_mangledname]));
  741. MessagePos1(checkpd.fileinfo,sym_e_param_list,checkpd.customprocname([pno_mangledname]));
  742. end;
  743. end;
  744. end;
  745. inherited;
  746. end;
  747. {****************************************************************************
  748. tcpustaticvarsym
  749. ****************************************************************************}
  750. procedure tcpustaticvarsym.set_mangledname(const s: TSymStr);
  751. begin
  752. inherited;
  753. _mangledname:=jvmmangledbasename(self,s,false);
  754. jvmaddtypeownerprefix(owner,_mangledname);
  755. end;
  756. function tcpustaticvarsym.mangledname: TSymStr;
  757. begin
  758. if _mangledname='' then
  759. begin
  760. if _mangledbasename='' then
  761. _mangledname:=jvmmangledbasename(self,false)
  762. else
  763. _mangledname:=jvmmangledbasename(self,_mangledbasename,false);
  764. jvmaddtypeownerprefix(owner,_mangledname);
  765. end;
  766. result:=_mangledname;
  767. end;
  768. {****************************************************************************
  769. tcpufieldvarsym
  770. ****************************************************************************}
  771. procedure tcpufieldvarsym.set_externalname(const s: string);
  772. begin
  773. { make sure it is recalculated }
  774. cachedmangledname:='';
  775. if is_java_class_or_interface(tdef(owner.defowner)) then
  776. begin
  777. externalname:=stringdup(s);
  778. include(varoptions,vo_has_mangledname);
  779. end
  780. else
  781. internalerror(2011031201);
  782. end;
  783. function tcpufieldvarsym.mangledname: TSymStr;
  784. begin
  785. if is_java_class_or_interface(tdef(owner.defowner)) or
  786. (tdef(owner.defowner).typ=recorddef) then
  787. begin
  788. if cachedmangledname<>'' then
  789. result:=cachedmangledname
  790. else
  791. begin
  792. result:=jvmmangledbasename(self,false);
  793. jvmaddtypeownerprefix(owner,result);
  794. cachedmangledname:=result;
  795. end;
  796. end
  797. else
  798. result:=inherited;
  799. end;
  800. begin
  801. { used tdef classes }
  802. cfiledef:=tcpufiledef;
  803. cvariantdef:=tcpuvariantdef;
  804. cformaldef:=tcpuformaldef;
  805. cforwarddef:=tcpuforwarddef;
  806. cundefineddef:=tcpuundefineddef;
  807. cerrordef:=tcpuerrordef;
  808. cpointerdef:=tcpupointerdef;
  809. crecorddef:=tcpurecorddef;
  810. cimplementedinterface:=tcpuimplementedinterface;
  811. cobjectdef:=tcpuobjectdef;
  812. cclassrefdef:=tcpuclassrefdef;
  813. carraydef:=tcpuarraydef;
  814. corddef:=tcpuorddef;
  815. cfloatdef:=tcpufloatdef;
  816. cprocvardef:=tcpuprocvardef;
  817. cprocdef:=tcpuprocdef;
  818. cstringdef:=tcpustringdef;
  819. cenumdef:=tcpuenumdef;
  820. csetdef:=tcpusetdef;
  821. { used tsym classes }
  822. clabelsym:=tcpulabelsym;
  823. cunitsym:=tcpuunitsym;
  824. cnamespacesym:=tcpunamespacesym;
  825. cprocsym:=tcpuprocsym;
  826. ctypesym:=tcputypesym;
  827. cfieldvarsym:=tcpufieldvarsym;
  828. clocalvarsym:=tcpulocalvarsym;
  829. cparavarsym:=tcpuparavarsym;
  830. cstaticvarsym:=tcpustaticvarsym;
  831. cabsolutevarsym:=tcpuabsolutevarsym;
  832. cpropertysym:=tcpupropertysym;
  833. cconstsym:=tcpuconstsym;
  834. cenumsym:=tcpuenumsym;
  835. csyssym:=tcpusyssym;
  836. end.