symcpu.pas 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. {
  2. Copyright (c) 2014 by Florian Klaempfl
  3. Symbol table overrides for JVM
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit symcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,
  22. aasmdata,
  23. symtype,
  24. symdef,symsym;
  25. type
  26. { defs }
  27. tcpufiledef = class(tfiledef)
  28. end;
  29. tcpufiledefclass = class of tcpufiledef;
  30. tcpuvariantdef = class(tvariantdef)
  31. end;
  32. tcpuvariantdefclass = class of tcpuvariantdef;
  33. tcpuformaldef = class(tformaldef)
  34. end;
  35. tcpuformaldefclass = class of tcpuformaldef;
  36. tcpuforwarddef = class(tforwarddef)
  37. end;
  38. tcpuforwarddefclass = class of tcpuforwarddef;
  39. tcpuundefineddef = class(tundefineddef)
  40. end;
  41. tcpuundefineddefclass = class of tcpuundefineddef;
  42. tcpuerrordef = class(terrordef)
  43. end;
  44. tcpuerrordefclass = class of tcpuerrordef;
  45. tcpupointerdef = class(tpointerdef)
  46. end;
  47. tcpupointerdefclass = class of tcpupointerdef;
  48. tcpurecorddef = class(trecorddef)
  49. end;
  50. tcpurecorddefclass = class of tcpurecorddef;
  51. tcpuimplementedinterface = class(timplementedinterface)
  52. end;
  53. tcpuimplementedinterfaceclass = class of tcpuimplementedinterface;
  54. tcpuobjectdef = class(tobjectdef)
  55. end;
  56. tcpuobjectdefclass = class of tcpuobjectdef;
  57. tcpuclassrefdef = class(tclassrefdef)
  58. end;
  59. tcpuclassrefdefclass = class of tcpuclassrefdef;
  60. tcpuarraydef = class(tarraydef)
  61. end;
  62. tcpuarraydefclass = class of tcpuarraydef;
  63. tcpuorddef = class(torddef)
  64. end;
  65. tcpuorddefclass = class of tcpuorddef;
  66. tcpufloatdef = class(tfloatdef)
  67. end;
  68. tcpufloatdefclass = class of tcpufloatdef;
  69. tcpuprocvardef = class(tprocvardef)
  70. protected
  71. procedure ppuwrite_platform(ppufile: tcompilerppufile); override;
  72. procedure ppuload_platform(ppufile: tcompilerppufile); override;
  73. public
  74. { class representing this procvar on the Java side }
  75. classdef : tobjectdef;
  76. classdefderef : tderef;
  77. procedure buildderef;override;
  78. procedure deref;override;
  79. function getcopy: tstoreddef; override;
  80. end;
  81. tcpuprocvardefclass = class of tcpuprocvardef;
  82. tcpuprocdef = class(tprocdef)
  83. { generated assembler code; used by JVM backend so it can afterwards
  84. easily write out all methods grouped per class }
  85. exprasmlist : TAsmList;
  86. function jvmmangledbasename(signature: boolean): TSymStr;
  87. function mangledname: TSymStr; override;
  88. destructor destroy; override;
  89. end;
  90. tcpuprocdefclass = class of tcpuprocdef;
  91. tcpustringdef = class(tstringdef)
  92. end;
  93. tcpustringdefclass = class of tcpustringdef;
  94. tcpuenumdef = class(tenumdef)
  95. protected
  96. procedure ppuload_platform(ppufile: tcompilerppufile); override;
  97. procedure ppuwrite_platform(ppufile: tcompilerppufile); override;
  98. public
  99. { class representing this enum on the Java side }
  100. classdef : tobjectdef;
  101. classdefderef : tderef;
  102. function getcopy: tstoreddef; override;
  103. procedure buildderef; override;
  104. procedure deref; override;
  105. end;
  106. tcpuenumdefclass = class of tcpuenumdef;
  107. tcpusetdef = class(tsetdef)
  108. end;
  109. tcpusetdefclass = class of tcpusetdef;
  110. { syms }
  111. tcpulabelsym = class(tlabelsym)
  112. end;
  113. tcpulabelsymclass = class of tcpulabelsym;
  114. tcpuunitsym = class(tunitsym)
  115. end;
  116. tcpuunitsymclass = class of tcpuunitsym;
  117. tcpunamespacesym = class(tnamespacesym)
  118. end;
  119. tcpunamespacesymclass = class of tcpunamespacesym;
  120. tcpuprocsym = class(tprocsym)
  121. procedure check_forward; override;
  122. end;
  123. tcpuprocsymclass = class of tcpuprocsym;
  124. tcputypesym = class(ttypesym)
  125. end;
  126. tcpuypesymclass = class of tcputypesym;
  127. tcpufieldvarsym = class(tfieldvarsym)
  128. procedure set_externalname(const s: string); override;
  129. function mangledname: TSymStr; override;
  130. end;
  131. tcpufieldvarsymclass = class of tcpufieldvarsym;
  132. tcpulocalvarsym = class(tlocalvarsym)
  133. end;
  134. tcpulocalvarsymclass = class of tcpulocalvarsym;
  135. tcpuparavarsym = class(tparavarsym)
  136. end;
  137. tcpuparavarsymclass = class of tcpuparavarsym;
  138. tcpustaticvarsym = class(tstaticvarsym)
  139. procedure set_mangledname(const s: TSymStr); override;
  140. function mangledname: TSymStr; override;
  141. end;
  142. tcpustaticvarsymclass = class of tcpustaticvarsym;
  143. tcpuabsolutevarsym = class(tabsolutevarsym)
  144. end;
  145. tcpuabsolutevarsymclass = class of tcpuabsolutevarsym;
  146. tcpupropertysym = class(tpropertysym)
  147. protected
  148. { when a private/protected field is exposed via a property with a higher
  149. visibility, then we have to create a getter and/or setter with that same
  150. higher visibility to make sure that using the property does not result
  151. in JVM verification errors }
  152. procedure create_getter_or_setter_for_property(orgaccesspd: tprocdef; getter: boolean);
  153. procedure finalize_getter_or_setter_for_sym(getset: tpropaccesslisttypes; sym: tsym; fielddef: tdef; accessordef: tprocdef); override;
  154. end;
  155. tcpupropertysymclass = class of tcpupropertysym;
  156. tcpuconstsym = class(tconstsym)
  157. end;
  158. tcpuconstsymclass = class of tcpuconstsym;
  159. tcpuenumsym = class(tenumsym)
  160. end;
  161. tcpuenumsymclass = class of tcpuenumsym;
  162. tcpusyssym = class(tsyssym)
  163. end;
  164. tcpusyssymclass = class of tcpusyssym;
  165. const
  166. pbestrealtype : ^tdef = @s64floattype;
  167. implementation
  168. uses
  169. verbose,cutils,cclasses,globals,
  170. symconst,symbase,symtable,symcreat,jvmdef,
  171. pdecsub,pjvm,
  172. paramgr;
  173. {****************************************************************************
  174. tcpuproptertysym
  175. ****************************************************************************}
  176. procedure tcpupropertysym.create_getter_or_setter_for_property(orgaccesspd: tprocdef; getter: boolean);
  177. var
  178. obj: tabstractrecorddef;
  179. ps: tprocsym;
  180. pvs: tparavarsym;
  181. sym: tsym;
  182. pd, parentpd, accessorparapd: tprocdef;
  183. tmpaccesslist: tpropaccesslist;
  184. callthroughpropname,
  185. accessorname: string;
  186. callthroughprop: tpropertysym;
  187. accesstyp: tpropaccesslisttypes;
  188. sktype: tsynthetickind;
  189. procoptions: tprocoptions;
  190. paranr: word;
  191. explicitwrapper: boolean;
  192. begin
  193. obj:=current_structdef;
  194. { if someone gets the idea to add a property to an external class
  195. definition, don't try to wrap it since we cannot add methods to
  196. external classes }
  197. if oo_is_external in obj.objectoptions then
  198. exit;
  199. symtablestack.push(obj.symtable);
  200. try
  201. if getter then
  202. accesstyp:=palt_read
  203. else
  204. accesstyp:=palt_write;
  205. { we can't use str_parse_method_dec here because the type of the field
  206. may not be visible at the Pascal level }
  207. explicitwrapper:=
  208. { private methods are not visibile outside the current class, so
  209. no use in making life harder for us by introducing potential
  210. (future or current) naming conflicts }
  211. (visibility<>vis_private) and
  212. (getter and
  213. (prop_auto_getter_prefix<>'')) or
  214. (not getter and
  215. (prop_auto_setter_prefix<>''));
  216. sym:=nil;
  217. procoptions:=[];
  218. if explicitwrapper then
  219. begin
  220. if getter then
  221. accessorname:=prop_auto_getter_prefix+realname
  222. else
  223. accessorname:=prop_auto_setter_prefix+realname;
  224. sym:=search_struct_member_no_helper(obj,upper(accessorname));
  225. if getter then
  226. sktype:=tsk_field_getter
  227. else
  228. sktype:=tsk_field_setter;
  229. if assigned(sym) then
  230. begin
  231. if ((sym.typ<>procsym) or
  232. (tprocsym(sym).procdeflist.count<>1) or
  233. (tprocdef(tprocsym(sym).procdeflist[0]).synthetickind<>sktype)) and
  234. (not assigned(orgaccesspd) or
  235. (sym<>orgaccesspd.procsym)) then
  236. begin
  237. MessagePos2(fileinfo,parser_e_cannot_generate_property_getter_setter,accessorname,FullTypeName(tdef(sym.owner.defowner),nil)+'.'+accessorname);
  238. exit;
  239. end
  240. else
  241. begin
  242. if accessorname<>sym.realname then
  243. MessagePos2(fileinfo,parser_w_case_difference_auto_property_getter_setter_prefix,sym.realname,accessorname);
  244. { is the specified getter/setter defined in the current
  245. struct and was it originally specified as the getter/
  246. setter for this property? If so, simply adjust its
  247. visibility if necessary.
  248. }
  249. if assigned(orgaccesspd) then
  250. parentpd:=orgaccesspd
  251. else
  252. parentpd:=tprocdef(tprocsym(sym).procdeflist[0]);
  253. if parentpd.owner.defowner=owner.defowner then
  254. begin
  255. if parentpd.visibility<visibility then
  256. begin
  257. parentpd.visibility:=visibility;
  258. include(parentpd.procoptions,po_auto_raised_visibility);
  259. end;
  260. { we are done, no need to create a wrapper }
  261. exit
  262. end
  263. { a parent already included this getter/setter -> try to
  264. override it }
  265. else if parentpd.visibility<>vis_private then
  266. begin
  267. if po_virtualmethod in parentpd.procoptions then
  268. begin
  269. procoptions:=procoptions+[po_virtualmethod,po_overridingmethod];
  270. Message2(parser_w_overriding_property_getter_setter,accessorname,FullTypeName(tdef(parentpd.owner.defowner),nil));
  271. end;
  272. { otherwise we can't do anything, and
  273. proc_add_definition will give an error }
  274. end;
  275. { add method with the correct visibility }
  276. pd:=tprocdef(parentpd.getcopy);
  277. { get rid of the import accessorname for inherited virtual class methods,
  278. it has to be regenerated rather than amended }
  279. if [po_classmethod,po_virtualmethod]<=pd.procoptions then
  280. begin
  281. stringdispose(pd.import_name);
  282. exclude(pd.procoptions,po_has_importname);
  283. end;
  284. pd.visibility:=visibility;
  285. pd.procoptions:=pd.procoptions+procoptions;
  286. { ignore this artificially added procdef when looking for overloads }
  287. include(pd.procoptions,po_ignore_for_overload_resolution);
  288. finish_copied_procdef(pd,parentpd.procsym.realname,obj.symtable,obj);
  289. exclude(pd.procoptions,po_external);
  290. pd.synthetickind:=tsk_anon_inherited;
  291. exit;
  292. end;
  293. end;
  294. { make the artificial getter/setter virtual so we can override it in
  295. children if necessary }
  296. if not(sp_static in symoptions) and
  297. (obj.typ=objectdef) then
  298. include(procoptions,po_virtualmethod);
  299. { prevent problems in Delphi mode }
  300. include(procoptions,po_overload);
  301. end
  302. else
  303. begin
  304. { construct procsym accessorname (unique for this access; reusing the same
  305. helper for multiple accesses to the same field is hard because the
  306. propacesslist can contain subscript nodes etc) }
  307. accessorname:=visibilityName[visibility];
  308. replace(accessorname,' ','_');
  309. if getter then
  310. accessorname:=accessorname+'$getter'
  311. else
  312. accessorname:=accessorname+'$setter';
  313. end;
  314. { create procdef }
  315. if not assigned(orgaccesspd) then
  316. begin
  317. pd:=cprocdef.create(normal_function_level);
  318. if df_generic in obj.defoptions then
  319. include(pd.defoptions,df_generic);
  320. { method of this objectdef }
  321. pd.struct:=obj;
  322. { can only construct the artificial accessorname now, because it requires
  323. pd.defid }
  324. if not explicitwrapper then
  325. accessorname:='$'+obj.symtable.realname^+'$'+realname+'$'+accessorname+'$'+tostr(pd.defid);
  326. end
  327. else
  328. begin
  329. { getter/setter could have parameters in case of indexed access
  330. -> copy original procdef }
  331. pd:=tprocdef(orgaccesspd.getcopy);
  332. exclude(pd.procoptions,po_abstractmethod);
  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. finish_copied_procdef(pd,accessorname,obj.symtable,obj);
  338. sym:=pd.procsym;
  339. end;
  340. { add previously collected procoptions }
  341. pd.procoptions:=pd.procoptions+procoptions;
  342. { visibility }
  343. pd.visibility:=visibility;
  344. { new procsym? }
  345. if not assigned(sym) or
  346. (sym.owner<>owner) then
  347. begin
  348. ps:=cprocsym.create(accessorname);
  349. obj.symtable.insert(ps);
  350. end
  351. else
  352. ps:=tprocsym(sym);
  353. { associate procsym with procdef}
  354. pd.procsym:=ps;
  355. { function/procedure }
  356. accessorparapd:=nil;
  357. if getter then
  358. begin
  359. pd.proctypeoption:=potype_function;
  360. pd.synthetickind:=tsk_field_getter;
  361. { result type }
  362. pd.returndef:=propdef;
  363. if (ppo_hasparameters in propoptions) and
  364. not assigned(orgaccesspd) then
  365. accessorparapd:=pd;
  366. end
  367. else
  368. begin
  369. pd.proctypeoption:=potype_procedure;
  370. pd.synthetickind:=tsk_field_setter;
  371. pd.returndef:=voidtype;
  372. if not assigned(orgaccesspd) then
  373. begin
  374. { parameter with value to set }
  375. pvs:=cparavarsym.create('__fpc_newval__',10,vs_const,propdef,[]);
  376. pd.parast.insert(pvs);
  377. end;
  378. if (ppo_hasparameters in propoptions) and
  379. not assigned(orgaccesspd) then
  380. accessorparapd:=pd;
  381. end;
  382. { create a property for the old symaccesslist with a new accessorname, so that
  383. we can reuse it in the implementation (rather than having to
  384. translate the symaccesslist back to Pascal code) }
  385. callthroughpropname:='__fpc__'+realname;
  386. if getter then
  387. callthroughpropname:=callthroughpropname+'__getter_wrapper'
  388. else
  389. callthroughpropname:=callthroughpropname+'__setter_wrapper';
  390. callthroughprop:=cpropertysym.create(callthroughpropname);
  391. callthroughprop.visibility:=visibility;
  392. if getter then
  393. makeduplicate(callthroughprop,accessorparapd,nil,paranr)
  394. else
  395. makeduplicate(callthroughprop,nil,accessorparapd,paranr);
  396. callthroughprop.default:=longint($80000000);
  397. callthroughprop.default:=0;
  398. callthroughprop.propoptions:=callthroughprop.propoptions-[ppo_stored,ppo_enumerator_current,ppo_overrides,ppo_defaultproperty];
  399. if sp_static in symoptions then
  400. include(callthroughprop.symoptions, sp_static);
  401. { copy original property target to callthrough property (and replace
  402. original one with the new empty list; will be filled in later) }
  403. tmpaccesslist:=callthroughprop.propaccesslist[accesstyp];
  404. callthroughprop.propaccesslist[accesstyp]:=propaccesslist[accesstyp];
  405. propaccesslist[accesstyp]:=tmpaccesslist;
  406. owner.insert(callthroughprop);
  407. pd.skpara:=callthroughprop;
  408. { needs to be exported }
  409. include(pd.procoptions,po_global);
  410. { class property -> static class method }
  411. if sp_static in symoptions then
  412. pd.procoptions:=pd.procoptions+[po_classmethod,po_staticmethod];
  413. { in case we made a copy of the original accessor, this has all been
  414. done already }
  415. if not assigned(orgaccesspd) then
  416. begin
  417. { calling convention, self, ... }
  418. if obj.typ=recorddef then
  419. handle_calling_convention(pd,[hcc_check])
  420. else
  421. handle_calling_convention(pd,hcc_all);
  422. { register forward declaration with procsym }
  423. proc_add_definition(pd);
  424. end;
  425. { make the property call this new function }
  426. propaccesslist[accesstyp].addsym(sl_call,ps);
  427. propaccesslist[accesstyp].procdef:=pd;
  428. finally
  429. symtablestack.pop(obj.symtable);
  430. end;
  431. end;
  432. procedure tcpupropertysym.finalize_getter_or_setter_for_sym(getset: tpropaccesslisttypes; sym: tsym; fielddef: tdef; accessordef: tprocdef);
  433. var
  434. orgaccesspd: tprocdef;
  435. pprefix: pstring;
  436. wrongvisibility: boolean;
  437. begin
  438. inherited;
  439. if getset=palt_read then
  440. pprefix:=@prop_auto_getter_prefix
  441. else
  442. pprefix:=@prop_auto_setter_prefix;
  443. case sym.typ of
  444. procsym:
  445. begin
  446. orgaccesspd:=tprocdef(propaccesslist[getset].procdef);
  447. wrongvisibility:=tprocdef(propaccesslist[getset].procdef).visibility<visibility;
  448. if (pprefix^<>'') and
  449. (wrongvisibility or
  450. (sym.RealName<>pprefix^+RealName)) then
  451. create_getter_or_setter_for_property(orgaccesspd,getset=palt_read)
  452. { if the visibility of the accessor is lower than
  453. the visibility of the property, wrap it so that
  454. we can call it from all contexts in which the
  455. property is visible }
  456. else if wrongvisibility then
  457. begin
  458. propaccesslist[getset].procdef:=jvm_wrap_method_with_vis(tprocdef(propaccesslist[palt_read].procdef),visibility);
  459. propaccesslist[getset].firstsym^.sym:=tprocdef(propaccesslist[getset].procdef).procsym;
  460. end;
  461. end;
  462. fieldvarsym:
  463. begin
  464. { if the visibility of the field is lower than the
  465. visibility of the property, wrap it in a getter
  466. so that we can access it from all contexts in
  467. which the property is visibile }
  468. if (pprefix^<>'') or
  469. (tfieldvarsym(sym).visibility<visibility) then
  470. create_getter_or_setter_for_property(nil,getset=palt_read);
  471. end;
  472. else
  473. internalerror(2014061101);
  474. end;
  475. end;
  476. {****************************************************************************
  477. tcpuenumdef
  478. ****************************************************************************}
  479. procedure tcpuenumdef.ppuload_platform(ppufile: tcompilerppufile);
  480. begin
  481. inherited;
  482. ppufile.getderef(classdefderef);
  483. end;
  484. procedure tcpuenumdef.ppuwrite_platform(ppufile: tcompilerppufile);
  485. begin
  486. inherited;
  487. ppufile.putderef(classdefderef);
  488. end;
  489. function tcpuenumdef.getcopy: tstoreddef;
  490. begin
  491. result:=inherited;
  492. tcpuenumdef(result).classdef:=classdef;
  493. end;
  494. procedure tcpuenumdef.buildderef;
  495. begin
  496. inherited;
  497. classdefderef.build(classdef);
  498. end;
  499. procedure tcpuenumdef.deref;
  500. begin
  501. inherited;
  502. classdef:=tobjectdef(classdefderef.resolve);
  503. end;
  504. {****************************************************************************
  505. tcpuprocdef
  506. ****************************************************************************}
  507. function tcpuprocdef.jvmmangledbasename(signature: boolean): TSymStr;
  508. var
  509. vs: tparavarsym;
  510. i: longint;
  511. founderror: tdef;
  512. tmpresult: TSymStr;
  513. container: tsymtable;
  514. begin
  515. { format:
  516. * method definition (in Jasmin):
  517. (private|protected|public) [static] method(parametertypes)returntype
  518. * method invocation
  519. package/class/method(parametertypes)returntype
  520. -> store common part: method(parametertypes)returntype and
  521. adorn as required when using it.
  522. }
  523. if not signature then
  524. begin
  525. { method name }
  526. { special names for constructors and class constructors }
  527. if proctypeoption=potype_constructor then
  528. tmpresult:='<init>'
  529. else if proctypeoption in [potype_class_constructor,potype_unitinit] then
  530. tmpresult:='<clinit>'
  531. else if po_has_importname in procoptions then
  532. begin
  533. if assigned(import_name) then
  534. tmpresult:=import_name^
  535. else
  536. internalerror(2010122608);
  537. end
  538. else
  539. begin
  540. tmpresult:=procsym.realname;
  541. if tmpresult[1]='$' then
  542. tmpresult:=copy(tmpresult,2,length(tmpresult)-1);
  543. { nested functions }
  544. container:=owner;
  545. while container.symtabletype=localsymtable do
  546. begin
  547. tmpresult:='$'+tprocdef(owner.defowner).procsym.realname+'$'+tostr(tprocdef(owner.defowner).procsym.symid)+'$'+tmpresult;
  548. container:=container.defowner.owner;
  549. end;
  550. end;
  551. end
  552. else
  553. tmpresult:='';
  554. { parameter types }
  555. tmpresult:=tmpresult+'(';
  556. { not the case for the main program (not required for defaultmangledname
  557. because setmangledname() is called for the main program; in case of
  558. the JVM, this only sets the importname, however) }
  559. if assigned(paras) then
  560. begin
  561. init_paraloc_info(callerside);
  562. for i:=0 to paras.count-1 do
  563. begin
  564. vs:=tparavarsym(paras[i]);
  565. { function result is not part of the mangled name }
  566. if vo_is_funcret in vs.varoptions then
  567. continue;
  568. { self pointer neither, except for class methods (the JVM only
  569. supports static class methods natively, so the self pointer
  570. here is a regular parameter as far as the JVM is concerned }
  571. if not(po_classmethod in procoptions) and
  572. (vo_is_self in vs.varoptions) then
  573. continue;
  574. { passing by reference is emulated by passing an array of one
  575. element containing the value; for types that aren't pointers
  576. in regular Pascal, simply passing the underlying pointer type
  577. does achieve regular call-by-reference semantics though;
  578. formaldefs always have to be passed like that because their
  579. contents can be replaced }
  580. if paramanager.push_copyout_param(vs.varspez,vs.vardef,proccalloption) then
  581. tmpresult:=tmpresult+'[';
  582. { Add the parameter type. }
  583. if not jvmaddencodedtype(vs.vardef,false,tmpresult,signature,founderror) then
  584. { an internalerror here is also triggered in case of errors in the source code }
  585. tmpresult:='<error>';
  586. end;
  587. end;
  588. tmpresult:=tmpresult+')';
  589. { And the type of the function result (void in case of a procedure and
  590. constructor). }
  591. if (proctypeoption in [potype_constructor,potype_class_constructor]) then
  592. jvmaddencodedtype(voidtype,false,tmpresult,signature,founderror)
  593. else if not jvmaddencodedtype(returndef,false,tmpresult,signature,founderror) then
  594. { an internalerror here is also triggered in case of errors in the source code }
  595. tmpresult:='<error>';
  596. result:=tmpresult;
  597. end;
  598. function tcpuprocdef.mangledname: TSymStr;
  599. begin
  600. if _mangledname='' then
  601. begin
  602. result:=jvmmangledbasename(false);
  603. if (po_has_importdll in procoptions) then
  604. begin
  605. { import_dll comes from "external 'import_dll_name' name 'external_name'" }
  606. if assigned(import_dll) then
  607. result:=import_dll^+'/'+result
  608. else
  609. internalerror(2010122607);
  610. end
  611. else
  612. jvmaddtypeownerprefix(owner,mangledname);
  613. _mangledname:=result;
  614. end
  615. else
  616. result:=_mangledname;
  617. end;
  618. destructor tcpuprocdef.destroy;
  619. begin
  620. exprasmlist.free;
  621. inherited destroy;
  622. end;
  623. {****************************************************************************
  624. tcpuprocvardef
  625. ****************************************************************************}
  626. procedure tcpuprocvardef.ppuwrite_platform(ppufile: tcompilerppufile);
  627. begin
  628. inherited;
  629. ppufile.putderef(classdefderef);
  630. end;
  631. procedure tcpuprocvardef.ppuload_platform(ppufile: tcompilerppufile);
  632. begin
  633. inherited;
  634. ppufile.getderef(classdefderef);
  635. end;
  636. procedure tcpuprocvardef.buildderef;
  637. begin
  638. inherited buildderef;
  639. classdefderef.build(classdef);
  640. end;
  641. procedure tcpuprocvardef.deref;
  642. begin
  643. inherited deref;
  644. classdef:=tobjectdef(classdefderef.resolve);
  645. end;
  646. function tcpuprocvardef.getcopy: tstoreddef;
  647. begin
  648. result:=inherited;
  649. tcpuprocvardef(result).classdef:=classdef;
  650. end;
  651. {****************************************************************************
  652. tcpuprocsym
  653. ****************************************************************************}
  654. procedure tcpuprocsym.check_forward;
  655. var
  656. curri, checki: longint;
  657. currpd, checkpd: tprocdef;
  658. begin
  659. inherited;
  660. { check for conflicts based on mangled name, because several FPC
  661. types/constructs map to the same JVM mangled name }
  662. for curri:=0 to FProcdefList.Count-2 do
  663. begin
  664. currpd:=tprocdef(FProcdefList[curri]);
  665. if (po_external in currpd.procoptions) or
  666. (currpd.proccalloption=pocall_internproc) then
  667. continue;
  668. for checki:=curri+1 to FProcdefList.Count-1 do
  669. begin
  670. checkpd:=tprocdef(FProcdefList[checki]);
  671. if po_external in checkpd.procoptions then
  672. continue;
  673. if currpd.mangledname=checkpd.mangledname then
  674. begin
  675. MessagePos(checkpd.fileinfo,parser_e_overloaded_have_same_mangled_name);
  676. MessagePos1(currpd.fileinfo,sym_e_param_list,currpd.customprocname([pno_mangledname]));
  677. MessagePos1(checkpd.fileinfo,sym_e_param_list,checkpd.customprocname([pno_mangledname]));
  678. end;
  679. end;
  680. end;
  681. inherited;
  682. end;
  683. {****************************************************************************
  684. tcpustaticvarsym
  685. ****************************************************************************}
  686. procedure tcpustaticvarsym.set_mangledname(const s: TSymStr);
  687. begin
  688. inherited;
  689. _mangledname:=jvmmangledbasename(self,s,false);
  690. jvmaddtypeownerprefix(owner,_mangledname);
  691. end;
  692. function tcpustaticvarsym.mangledname: TSymStr;
  693. begin
  694. if _mangledname='' then
  695. begin
  696. if _mangledbasename='' then
  697. _mangledname:=jvmmangledbasename(self,false)
  698. else
  699. _mangledname:=jvmmangledbasename(self,_mangledbasename,false);
  700. jvmaddtypeownerprefix(owner,_mangledname);
  701. end;
  702. result:=_mangledname;
  703. end;
  704. {****************************************************************************
  705. tcpufieldvarsym
  706. ****************************************************************************}
  707. procedure tcpufieldvarsym.set_externalname(const s: string);
  708. begin
  709. { make sure it is recalculated }
  710. cachedmangledname:='';
  711. if is_java_class_or_interface(tdef(owner.defowner)) then
  712. begin
  713. externalname:=stringdup(s);
  714. include(varoptions,vo_has_mangledname);
  715. end
  716. else
  717. internalerror(2011031201);
  718. end;
  719. function tcpufieldvarsym.mangledname: TSymStr;
  720. begin
  721. if is_java_class_or_interface(tdef(owner.defowner)) or
  722. (tdef(owner.defowner).typ=recorddef) then
  723. begin
  724. if cachedmangledname<>'' then
  725. result:=cachedmangledname
  726. else
  727. begin
  728. result:=jvmmangledbasename(self,false);
  729. jvmaddtypeownerprefix(owner,result);
  730. cachedmangledname:=result;
  731. end;
  732. end
  733. else
  734. result:=inherited;
  735. end;
  736. begin
  737. { used tdef classes }
  738. cfiledef:=tcpufiledef;
  739. cvariantdef:=tcpuvariantdef;
  740. cformaldef:=tcpuformaldef;
  741. cforwarddef:=tcpuforwarddef;
  742. cundefineddef:=tcpuundefineddef;
  743. cerrordef:=tcpuerrordef;
  744. cpointerdef:=tcpupointerdef;
  745. crecorddef:=tcpurecorddef;
  746. cimplementedinterface:=tcpuimplementedinterface;
  747. cobjectdef:=tcpuobjectdef;
  748. cclassrefdef:=tcpuclassrefdef;
  749. carraydef:=tcpuarraydef;
  750. corddef:=tcpuorddef;
  751. cfloatdef:=tcpufloatdef;
  752. cprocvardef:=tcpuprocvardef;
  753. cprocdef:=tcpuprocdef;
  754. cstringdef:=tcpustringdef;
  755. cenumdef:=tcpuenumdef;
  756. csetdef:=tcpusetdef;
  757. { used tsym classes }
  758. clabelsym:=tcpulabelsym;
  759. cunitsym:=tcpuunitsym;
  760. cnamespacesym:=tcpunamespacesym;
  761. cprocsym:=tcpuprocsym;
  762. ctypesym:=tcputypesym;
  763. cfieldvarsym:=tcpufieldvarsym;
  764. clocalvarsym:=tcpulocalvarsym;
  765. cparavarsym:=tcpuparavarsym;
  766. cstaticvarsym:=tcpustaticvarsym;
  767. cabsolutevarsym:=tcpuabsolutevarsym;
  768. cpropertysym:=tcpupropertysym;
  769. cconstsym:=tcpuconstsym;
  770. cenumsym:=tcpuenumsym;
  771. csyssym:=tcpusyssym;
  772. end.