jvmdef.pas 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. {
  2. Copyright (c) 2010 by Jonas Maebe
  3. This unit implements some JVM type helper routines (minimal
  4. unit dependencies, usable in symdef).
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. {$i fpcdefs.inc}
  19. unit jvmdef;
  20. interface
  21. uses
  22. globtype,
  23. node,
  24. symbase,symtype;
  25. { returns whether a def can make use of an extra type signature (for
  26. Java-style generics annotations; not use for FPC-style generics or their
  27. translations, but to annotate the kind of classref a java.lang.Class is
  28. and things like that) }
  29. function jvmtypeneedssignature(def: tdef): boolean;
  30. { create a signature encoding of a particular type; requires that
  31. jvmtypeneedssignature returned "true" for this type }
  32. procedure jvmaddencodedsignature(def: tdef; bpacked: boolean; var encodedstr: TSymStr);
  33. { Encode a type into the internal format used by the JVM (descriptor).
  34. Returns false if a type is not representable by the JVM,
  35. and in that case also the failing definition. }
  36. function jvmtryencodetype(def: tdef; out encodedtype: TSymStr; forcesignature: boolean; out founderror: tdef): boolean;
  37. { same as above, but throws an internal error on failure }
  38. function jvmencodetype(def: tdef; withsignature: boolean): TSymStr;
  39. { Check whether a type can be used in a JVM methom signature or field
  40. declaration. }
  41. function jvmchecktype(def: tdef; out founderror: tdef): boolean;
  42. { incremental version of jvmtryencodetype() }
  43. function jvmaddencodedtype(def: tdef; bpacked: boolean; var encodedstr: TSymStr; forcesignature: boolean; out founderror: tdef): boolean;
  44. { add type prefix (package name) to a type }
  45. procedure jvmaddtypeownerprefix(owner: tsymtable; var name: TSymStr);
  46. { returns type string for a single-dimensional array (different from normal
  47. typestring in case of a primitive type) }
  48. function jvmarrtype(def: tdef; out primitivetype: boolean): TSymStr;
  49. function jvmarrtype_setlength(def: tdef): char;
  50. { returns whether a def is emulated using an implicit pointer type on the
  51. JVM target (e.g., records, regular arrays, ...) }
  52. function jvmimplicitpointertype(def: tdef): boolean;
  53. { returns the mangled base name for a tsym (type + symbol name, no
  54. visibility etc); also adds signature attribute if requested and
  55. appropriate }
  56. function jvmmangledbasename(sym: tsym; withsignature: boolean): TSymStr;
  57. function jvmmangledbasename(sym: tsym; const usesymname: TSymStr; withsignature: boolean): TSymStr;
  58. { sometimes primitive types have to be boxed/unboxed via class types. This
  59. routine returns the appropriate box type for the passed primitive type }
  60. procedure jvmgetboxtype(def: tdef; out objdef, paradef: tdef; mergeints: boolean);
  61. function jvmgetunboxmethod(def: tdef): string;
  62. function jvmgetcorrespondingclassdef(def: tdef): tdef;
  63. implementation
  64. uses
  65. cutils,cclasses,
  66. verbose,systems,
  67. fmodule,
  68. symtable,symconst,symsym,symdef,symcreat,
  69. defutil,paramgr;
  70. {******************************************************************
  71. Type encoding
  72. *******************************************************************}
  73. function jvmtypeneedssignature(def: tdef): boolean;
  74. var
  75. i: longint;
  76. begin
  77. result:=false;
  78. case def.typ of
  79. classrefdef,
  80. setdef:
  81. begin
  82. result:=true;
  83. end;
  84. arraydef :
  85. begin
  86. result:=jvmtypeneedssignature(tarraydef(def).elementdef);
  87. end;
  88. procvardef :
  89. begin
  90. { may change in the future }
  91. end;
  92. procdef :
  93. begin
  94. for i:=0 to tprocdef(def).paras.count-1 do
  95. begin
  96. result:=jvmtypeneedssignature(tparavarsym(tprocdef(def).paras[i]).vardef);
  97. if result then
  98. exit;
  99. end;
  100. end
  101. else
  102. result:=false;
  103. end;
  104. end;
  105. procedure jvmaddencodedsignature(def: tdef; bpacked: boolean; var encodedstr: TSymStr);
  106. var
  107. founderror: tdef;
  108. begin
  109. case def.typ of
  110. pointerdef :
  111. begin
  112. { maybe one day }
  113. internalerror(2011051403);
  114. end;
  115. classrefdef :
  116. begin
  117. { Ljava/lang/Class<+SomeClassType> means
  118. "Ljava/lang/Class<SomeClassType_or_any_of_its_descendents>" }
  119. encodedstr:=encodedstr+'Ljava/lang/Class<+';
  120. jvmaddencodedtype(tclassrefdef(def).pointeddef,false,encodedstr,true,founderror);
  121. encodedstr:=encodedstr+'>;';
  122. end;
  123. setdef :
  124. begin
  125. if tsetdef(def).elementdef.typ=enumdef then
  126. begin
  127. encodedstr:=encodedstr+'Ljava/util/EnumSet<';
  128. jvmaddencodedtype(tenumdef(tsetdef(def).elementdef).getbasedef,false,encodedstr,true,founderror);
  129. encodedstr:=encodedstr+'>;';
  130. end
  131. else
  132. internalerror(2011051404);
  133. end;
  134. arraydef :
  135. begin
  136. if is_array_of_const(def) then
  137. begin
  138. internalerror(2011051405);
  139. end
  140. else if is_packed_array(def) then
  141. begin
  142. internalerror(2011051406);
  143. end
  144. else
  145. begin
  146. encodedstr:=encodedstr+'[';
  147. jvmaddencodedsignature(tarraydef(def).elementdef,false,encodedstr);
  148. end;
  149. end;
  150. procvardef :
  151. begin
  152. { maybe one day }
  153. internalerror(2011051407);
  154. end;
  155. objectdef :
  156. begin
  157. { maybe one day }
  158. end;
  159. undefineddef,
  160. errordef :
  161. begin
  162. internalerror(2011051408);
  163. end;
  164. procdef :
  165. { must be done via jvmencodemethod() }
  166. internalerror(2011051401);
  167. else
  168. internalerror(2011051402);
  169. end;
  170. end;
  171. function jvmaddencodedtype(def: tdef; bpacked: boolean; var encodedstr: TSymStr; forcesignature: boolean; out founderror: tdef): boolean;
  172. var
  173. c: char;
  174. begin
  175. result:=true;
  176. case def.typ of
  177. stringdef :
  178. begin
  179. case tstringdef(def).stringtype of
  180. { translated into java.lang.String }
  181. st_widestring,
  182. st_unicodestring:
  183. result:=jvmaddencodedtype(java_jlstring,false,encodedstr,forcesignature,founderror);
  184. st_ansistring:
  185. result:=jvmaddencodedtype(java_ansistring,false,encodedstr,forcesignature,founderror);
  186. st_shortstring:
  187. result:=jvmaddencodedtype(java_shortstring,false,encodedstr,forcesignature,founderror);
  188. else
  189. { May be handled via wrapping later }
  190. result:=false;
  191. end;
  192. end;
  193. enumdef:
  194. begin
  195. result:=jvmaddencodedtype(tenumdef(def).classdef,false,encodedstr,forcesignature,founderror);
  196. end;
  197. orddef :
  198. begin
  199. { for procedure "results" }
  200. if is_void(def) then
  201. c:='V'
  202. { only Pascal-style booleans conform to Java's definition of
  203. Boolean }
  204. else if is_pasbool(def) and
  205. (def.size=1) then
  206. c:='Z'
  207. else if is_widechar(def) then
  208. c:='C'
  209. else
  210. begin
  211. case def.size of
  212. 1:
  213. c:='B';
  214. 2:
  215. c:='S';
  216. 4:
  217. c:='I';
  218. 8:
  219. c:='J';
  220. else
  221. internalerror(2010121905);
  222. end;
  223. end;
  224. encodedstr:=encodedstr+c;
  225. end;
  226. pointerdef :
  227. begin
  228. if def=voidpointertype then
  229. result:=jvmaddencodedtype(java_jlobject,false,encodedstr,forcesignature,founderror)
  230. else if jvmimplicitpointertype(tpointerdef(def).pointeddef) then
  231. result:=jvmaddencodedtype(tpointerdef(def).pointeddef,false,encodedstr,forcesignature,founderror)
  232. else
  233. begin
  234. { used for internal pointer constructs }
  235. encodedstr:=encodedstr+'[';
  236. result:=jvmaddencodedtype(tpointerdef(def).pointeddef,false,encodedstr,forcesignature,founderror);
  237. end;
  238. end;
  239. floatdef :
  240. begin
  241. case tfloatdef(def).floattype of
  242. s32real:
  243. c:='F';
  244. s64real:
  245. c:='D';
  246. else
  247. result:=false;
  248. end;
  249. encodedstr:=encodedstr+c;
  250. end;
  251. filedef :
  252. result:=false;
  253. recorddef :
  254. begin
  255. encodedstr:=encodedstr+'L'+trecorddef(def).jvm_full_typename(true)+';'
  256. end;
  257. variantdef :
  258. begin
  259. { will be hanlded via wrapping later, although wrapping may
  260. happen at higher level }
  261. result:=false;
  262. end;
  263. classrefdef :
  264. begin
  265. if not forcesignature then
  266. { unfortunately, java.lang.Class is final, so we can't create
  267. different versions for difference class reference types }
  268. encodedstr:=encodedstr+'Ljava/lang/Class;'
  269. { we can however annotate it with extra signature information in
  270. using Java's generic annotations }
  271. else
  272. jvmaddencodedsignature(def,false,encodedstr);
  273. result:=true;
  274. end;
  275. setdef :
  276. begin
  277. if tsetdef(def).elementdef.typ=enumdef then
  278. begin
  279. if forcesignature then
  280. jvmaddencodedsignature(def,false,encodedstr)
  281. else
  282. result:=jvmaddencodedtype(java_juenumset,false,encodedstr,forcesignature,founderror)
  283. end
  284. else
  285. result:=jvmaddencodedtype(java_jubitset,false,encodedstr,forcesignature,founderror)
  286. end;
  287. formaldef :
  288. begin
  289. { var/const/out x: JLObject }
  290. result:=jvmaddencodedtype(java_jlobject,false,encodedstr,forcesignature,founderror);
  291. end;
  292. arraydef :
  293. begin
  294. if is_array_of_const(def) then
  295. {$ifndef nounsupported}
  296. result:=jvmaddencodedtype(java_jlobject,false,encodedstr,forcesignature,founderror)
  297. {$else}
  298. result:=false
  299. {$endif}
  300. else if is_packed_array(def) then
  301. result:=false
  302. else
  303. begin
  304. encodedstr:=encodedstr+'[';
  305. if not jvmaddencodedtype(tarraydef(def).elementdef,false,encodedstr,forcesignature,founderror) then
  306. begin
  307. result:=false;
  308. { report the exact (nested) error defintion }
  309. exit;
  310. end;
  311. end;
  312. end;
  313. procvardef :
  314. begin
  315. result:=jvmaddencodedtype(tprocvardef(def).classdef,false,encodedstr,forcesignature,founderror);
  316. end;
  317. objectdef :
  318. case tobjectdef(def).objecttype of
  319. odt_javaclass,
  320. odt_interfacejava:
  321. encodedstr:=encodedstr+'L'+tobjectdef(def).jvm_full_typename(true)+';'
  322. else
  323. result:=false;
  324. end;
  325. undefineddef,
  326. errordef :
  327. result:=false;
  328. procdef :
  329. { must be done via jvmencodemethod() }
  330. internalerror(2010121903);
  331. else
  332. internalerror(2010121904);
  333. end;
  334. if not result then
  335. founderror:=def;
  336. end;
  337. function jvmtryencodetype(def: tdef; out encodedtype: TSymStr; forcesignature: boolean; out founderror: tdef): boolean;
  338. begin
  339. encodedtype:='';
  340. result:=jvmaddencodedtype(def,false,encodedtype,forcesignature,founderror);
  341. end;
  342. procedure jvmaddtypeownerprefix(owner: tsymtable; var name: TSymStr);
  343. var
  344. owningcontainer: tsymtable;
  345. tmpresult: TSymStr;
  346. module: tmodule;
  347. nameendpos: longint;
  348. begin
  349. { see tprocdef.jvmmangledbasename for description of the format }
  350. owningcontainer:=owner;
  351. while (owningcontainer.symtabletype=localsymtable) do
  352. owningcontainer:=owningcontainer.defowner.owner;
  353. case owningcontainer.symtabletype of
  354. globalsymtable,
  355. staticsymtable:
  356. begin
  357. module:=find_module_from_symtable(owningcontainer);
  358. tmpresult:='';
  359. if assigned(module.namespace) then
  360. tmpresult:=module.namespace^+'/';
  361. tmpresult:=tmpresult+module.realmodulename^+'/';
  362. end;
  363. objectsymtable:
  364. case tobjectdef(owningcontainer.defowner).objecttype of
  365. odt_javaclass,
  366. odt_interfacejava:
  367. begin
  368. tmpresult:=tobjectdef(owningcontainer.defowner).jvm_full_typename(true)+'/'
  369. end
  370. else
  371. internalerror(2010122606);
  372. end;
  373. recordsymtable:
  374. tmpresult:=trecorddef(owningcontainer.defowner).jvm_full_typename(true)+'/'
  375. else
  376. internalerror(2010122605);
  377. end;
  378. name:=tmpresult+name;
  379. nameendpos:=pos(' ',name);
  380. if nameendpos=0 then
  381. nameendpos:=length(name)+1;
  382. insert('''',name,nameendpos);
  383. name:=''''+name;
  384. end;
  385. function jvmarrtype(def: tdef; out primitivetype: boolean): TSymStr;
  386. var
  387. errdef: tdef;
  388. begin
  389. if not jvmtryencodetype(def,result,false,errdef) then
  390. internalerror(2011012205);
  391. primitivetype:=false;
  392. if length(result)=1 then
  393. begin
  394. case result[1] of
  395. 'Z': result:='boolean';
  396. 'C': result:='char';
  397. 'B': result:='byte';
  398. 'S': result:='short';
  399. 'I': result:='int';
  400. 'J': result:='long';
  401. 'F': result:='float';
  402. 'D': result:='double';
  403. else
  404. internalerror(2011012206);
  405. end;
  406. primitivetype:=true;
  407. end
  408. else if (result[1]='L') then
  409. begin
  410. { in case of a class reference, strip the leading 'L' and the
  411. trailing ';' }
  412. setlength(result,length(result)-1);
  413. delete(result,1,1);
  414. end;
  415. { for arrays, use the actual reference type }
  416. end;
  417. function jvmarrtype_setlength(def: tdef): char;
  418. var
  419. errdef: tdef;
  420. res: TSymStr;
  421. begin
  422. { keep in sync with rtl/java/jdynarrh.inc and usage in njvminl }
  423. if is_record(def) then
  424. result:='R'
  425. else if is_shortstring(def) then
  426. result:='T'
  427. else if def.typ=setdef then
  428. begin
  429. if tsetdef(def).elementdef.typ=enumdef then
  430. result:='E'
  431. else
  432. result:='L'
  433. end
  434. else if (def.typ=procvardef) and
  435. not tprocvardef(def).is_addressonly then
  436. result:='P'
  437. else
  438. begin
  439. if not jvmtryencodetype(def,res,false,errdef) then
  440. internalerror(2011012209);
  441. if length(res)=1 then
  442. result:=res[1]
  443. else
  444. result:='A';
  445. end;
  446. end;
  447. function jvmimplicitpointertype(def: tdef): boolean;
  448. begin
  449. case def.typ of
  450. arraydef:
  451. result:=(tarraydef(def).highrange>=tarraydef(def).lowrange) or
  452. is_open_array(def) or
  453. is_array_of_const(def) or
  454. is_array_constructor(def);
  455. recorddef,
  456. setdef:
  457. result:=true;
  458. objectdef:
  459. result:=is_object(def);
  460. stringdef :
  461. result:=tstringdef(def).stringtype in [st_shortstring,st_longstring];
  462. procvardef:
  463. result:=not tprocvardef(def).is_addressonly;
  464. else
  465. result:=false;
  466. end;
  467. end;
  468. { mergeints = true means that all integer types are mapped to jllong,
  469. otherwise they are mapped to the closest corresponding type }
  470. procedure jvmgetboxtype(def: tdef; out objdef, paradef: tdef; mergeints: boolean);
  471. begin
  472. case def.typ of
  473. orddef:
  474. begin
  475. case torddef(def).ordtype of
  476. pasbool8:
  477. begin
  478. objdef:=tobjectdef(search_system_type('JLBOOLEAN').typedef);
  479. paradef:=pasbool8type;
  480. end;
  481. uwidechar:
  482. begin
  483. objdef:=tobjectdef(search_system_type('JLCHARACTER').typedef);
  484. paradef:=cwidechartype;
  485. end;
  486. else
  487. begin
  488. { wrap all integer types into a JLLONG, so that we don't get
  489. errors after returning a byte assigned to a long etc }
  490. if mergeints or
  491. (torddef(def).ordtype in [s64bit,u64bit,scurrency,bool64bit,pasbool64]) then
  492. begin
  493. objdef:=tobjectdef(search_system_type('JLLONG').typedef);
  494. paradef:=s64inttype;
  495. end
  496. else
  497. begin
  498. case torddef(def).ordtype of
  499. s8bit,
  500. u8bit,
  501. uchar,
  502. bool8bit:
  503. begin
  504. objdef:=tobjectdef(search_system_type('JLBYTE').typedef);
  505. paradef:=s8inttype;
  506. end;
  507. s16bit,
  508. u16bit,
  509. bool16bit,
  510. pasbool16:
  511. begin
  512. objdef:=tobjectdef(search_system_type('JLSHORT').typedef);
  513. paradef:=s16inttype;
  514. end;
  515. s32bit,
  516. u32bit,
  517. bool32bit,
  518. pasbool32:
  519. begin
  520. objdef:=tobjectdef(search_system_type('JLINTEGER').typedef);
  521. paradef:=s32inttype;
  522. end;
  523. else
  524. internalerror(2011052101);
  525. end;
  526. end;
  527. end;
  528. end;
  529. end;
  530. floatdef:
  531. begin
  532. case tfloatdef(def).floattype of
  533. s32real:
  534. begin
  535. objdef:=tobjectdef(search_system_type('JLFLOAT').typedef);
  536. paradef:=s32floattype;
  537. end;
  538. s64real:
  539. begin
  540. objdef:=tobjectdef(search_system_type('JLDOUBLE').typedef);
  541. paradef:=s64floattype;
  542. end;
  543. else
  544. internalerror(2011052102);
  545. end;
  546. end;
  547. else
  548. internalerror(2011052103);
  549. end;
  550. end;
  551. function jvmgetunboxmethod(def: tdef): string;
  552. begin
  553. case def.typ of
  554. orddef:
  555. begin
  556. case torddef(def).ordtype of
  557. pasbool8:
  558. result:='BOOLEANVALUE';
  559. s8bit,
  560. u8bit,
  561. uchar,
  562. bool8bit:
  563. result:='BYTEVALUE';
  564. s16bit,
  565. u16bit,
  566. bool16bit,
  567. pasbool16:
  568. result:='SHORTVALUE';
  569. s32bit,
  570. u32bit,
  571. bool32bit,
  572. pasbool32:
  573. result:='INTVALUE';
  574. s64bit,
  575. u64bit,
  576. scurrency,
  577. bool64bit,
  578. pasbool64:
  579. result:='LONGVALUE';
  580. uwidechar:
  581. result:='CHARVALUE';
  582. else
  583. internalerror(2011071702);
  584. end;
  585. end;
  586. floatdef:
  587. begin
  588. case tfloatdef(def).floattype of
  589. s32real:
  590. result:='FLOATVALUE';
  591. s64real:
  592. result:='DOUBLEVALUE';
  593. else
  594. internalerror(2011071703);
  595. end;
  596. end;
  597. else
  598. internalerror(2011071704);
  599. end;
  600. end;
  601. function jvmgetcorrespondingclassdef(def: tdef): tdef;
  602. var
  603. paradef: tdef;
  604. begin
  605. if def.typ in [orddef,floatdef] then
  606. jvmgetboxtype(def,result,paradef,false)
  607. else
  608. begin
  609. case def.typ of
  610. stringdef :
  611. begin
  612. case tstringdef(def).stringtype of
  613. { translated into java.lang.String }
  614. st_widestring,
  615. st_unicodestring:
  616. result:=java_jlstring;
  617. st_ansistring:
  618. result:=java_ansistring;
  619. st_shortstring:
  620. result:=java_shortstring;
  621. else
  622. internalerror(2011072409);
  623. end;
  624. end;
  625. enumdef:
  626. begin
  627. result:=tenumdef(def).classdef;
  628. end;
  629. pointerdef :
  630. begin
  631. if def=voidpointertype then
  632. result:=java_jlobject
  633. else if jvmimplicitpointertype(tpointerdef(def).pointeddef) then
  634. result:=tpointerdef(def).pointeddef
  635. else
  636. internalerror(2011072410);
  637. end;
  638. recorddef :
  639. begin
  640. result:=def;
  641. end;
  642. variantdef :
  643. begin
  644. result:=cvarianttype;
  645. end;
  646. classrefdef :
  647. begin
  648. result:=search_system_type('JLCLASS').typedef;
  649. end;
  650. setdef :
  651. begin
  652. if tsetdef(def).elementdef.typ=enumdef then
  653. result:=java_juenumset
  654. else
  655. result:=java_jubitset;
  656. end;
  657. formaldef :
  658. begin
  659. result:=java_jlobject;
  660. end;
  661. arraydef :
  662. begin
  663. { cannot represent statically }
  664. internalerror(2011072411);
  665. end;
  666. procvardef :
  667. begin
  668. result:=tprocvardef(def).classdef;
  669. end;
  670. objectdef :
  671. case tobjectdef(def).objecttype of
  672. odt_javaclass,
  673. odt_interfacejava:
  674. result:=def
  675. else
  676. internalerror(2011072412);
  677. end;
  678. else
  679. internalerror(2011072413);
  680. end;
  681. end;
  682. end;
  683. function jvmmangledbasename(sym: tsym; const usesymname: TSymStr; withsignature: boolean): TSymStr;
  684. var
  685. container: tsymtable;
  686. vsym: tabstractvarsym;
  687. csym: tconstsym;
  688. begin
  689. case sym.typ of
  690. staticvarsym,
  691. paravarsym,
  692. localvarsym,
  693. fieldvarsym:
  694. begin
  695. vsym:=tabstractvarsym(sym);
  696. result:=jvmencodetype(vsym.vardef,false);
  697. if withsignature and
  698. jvmtypeneedssignature(vsym.vardef) then
  699. begin
  700. result:=result+' signature "';
  701. result:=result+jvmencodetype(vsym.vardef,true)+'"';
  702. end;
  703. if (vsym.typ=paravarsym) and
  704. (vo_is_self in tparavarsym(vsym).varoptions) then
  705. result:='''this'' ' +result
  706. else if (vsym.typ in [paravarsym,localvarsym]) and
  707. ([vo_is_funcret,vo_is_result] * tabstractnormalvarsym(vsym).varoptions <> []) then
  708. result:='''result'' '+result
  709. else
  710. begin
  711. { add array indirection if required }
  712. if (vsym.typ=paravarsym) and
  713. (vsym.vardef.typ=formaldef) or
  714. ((vsym.varspez in [vs_var,vs_out,vs_constref]) and
  715. not jvmimplicitpointertype(vsym.vardef)) then
  716. result:='['+result;
  717. { single quotes for definitions to prevent clashes with Java
  718. opcodes }
  719. if withsignature then
  720. result:=usesymname+''' '+result
  721. else
  722. result:=usesymname+' '+result;
  723. { we have to mangle staticvarsyms in localsymtables to
  724. prevent name clashes... }
  725. if (vsym.typ=staticvarsym) then
  726. begin
  727. container:=sym.Owner;
  728. while (container.symtabletype=localsymtable) do
  729. begin
  730. if tdef(container.defowner).typ<>procdef then
  731. internalerror(2011040303);
  732. { defid is added to prevent problem with overloads }
  733. result:=tprocdef(container.defowner).procsym.realname+'$$'+tostr(tprocdef(container.defowner).defid)+'$'+result;
  734. container:=container.defowner.owner;
  735. end;
  736. end;
  737. if withsignature then
  738. result:=''''+result
  739. end;
  740. end;
  741. constsym:
  742. begin
  743. csym:=tconstsym(sym);
  744. { some constants can be untyped }
  745. if assigned (csym.constdef) then
  746. begin
  747. result:=jvmencodetype(csym.constdef,false);
  748. if withsignature and
  749. jvmtypeneedssignature(csym.constdef) then
  750. begin
  751. result:=result+' signature "';
  752. result:=result+jvmencodetype(csym.constdef,true)+'"';
  753. end;
  754. end
  755. else
  756. begin
  757. case csym.consttyp of
  758. constord:
  759. result:=jvmencodetype(s32inttype,withsignature);
  760. constreal:
  761. result:=jvmencodetype(s64floattype,withsignature);
  762. constset:
  763. internalerror(2011040701);
  764. constpointer,
  765. constnil:
  766. result:=jvmencodetype(java_jlobject,withsignature);
  767. constwstring,
  768. conststring:
  769. result:=jvmencodetype(java_jlstring,withsignature);
  770. constresourcestring:
  771. internalerror(2011040702);
  772. else
  773. internalerror(2011040703);
  774. end;
  775. end;
  776. if withsignature then
  777. result:=''''+usesymname+''' '+result
  778. else
  779. result:=usesymname+' '+result
  780. end;
  781. else
  782. internalerror(2011021703);
  783. end;
  784. end;
  785. function jvmmangledbasename(sym: tsym; withsignature: boolean): TSymStr;
  786. begin
  787. if (sym.typ=fieldvarsym) and
  788. assigned(tfieldvarsym(sym).externalname) then
  789. result:=jvmmangledbasename(sym,tfieldvarsym(sym).externalname^,withsignature)
  790. else
  791. result:=jvmmangledbasename(sym,sym.RealName,withsignature);
  792. end;
  793. {******************************************************************
  794. jvm type validity checking
  795. *******************************************************************}
  796. function jvmencodetype(def: tdef; withsignature: boolean): TSymStr;
  797. var
  798. errordef: tdef;
  799. begin
  800. if not jvmtryencodetype(def,result,withsignature,errordef) then
  801. internalerror(2011012305);
  802. end;
  803. function jvmchecktype(def: tdef; out founderror: tdef): boolean;
  804. var
  805. encodedtype: TSymStr;
  806. begin
  807. { don't duplicate the code like in objcdef, since the resulting strings
  808. are much shorter here so it's not worth it }
  809. result:=jvmtryencodetype(def,encodedtype,false,founderror);
  810. end;
  811. end.