symcpu.pas 32 KB

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