jvmdef.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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. implementation
  59. uses
  60. cutils,cclasses,
  61. verbose,systems,
  62. fmodule,
  63. symtable,symconst,symsym,symdef,symcreat,
  64. defutil,paramgr;
  65. {******************************************************************
  66. Type encoding
  67. *******************************************************************}
  68. function jvmtypeneedssignature(def: tdef): boolean;
  69. var
  70. i: longint;
  71. begin
  72. result:=false;
  73. case def.typ of
  74. classrefdef,
  75. setdef:
  76. begin
  77. result:=true;
  78. end;
  79. arraydef :
  80. begin
  81. result:=jvmtypeneedssignature(tarraydef(def).elementdef);
  82. end;
  83. procvardef :
  84. begin
  85. { may change in the future }
  86. end;
  87. procdef :
  88. begin
  89. for i:=0 to tprocdef(def).paras.count-1 do
  90. begin
  91. result:=jvmtypeneedssignature(tparavarsym(tprocdef(def).paras[i]).vardef);
  92. if result then
  93. exit;
  94. end;
  95. end
  96. else
  97. result:=false;
  98. end;
  99. end;
  100. procedure jvmaddencodedsignature(def: tdef; bpacked: boolean; var encodedstr: TSymStr);
  101. var
  102. founderror: tdef;
  103. begin
  104. case def.typ of
  105. pointerdef :
  106. begin
  107. { maybe one day }
  108. internalerror(2011051403);
  109. end;
  110. classrefdef :
  111. begin
  112. { Ljava/lang/Class<+SomeClassType> means
  113. "Ljava/lang/Class<SomeClassType_or_any_of_its_descendents>" }
  114. encodedstr:=encodedstr+'Ljava/lang/Class<+';
  115. jvmaddencodedtype(tclassrefdef(def).pointeddef,false,encodedstr,true,founderror);
  116. encodedstr:=encodedstr+'>;';
  117. end;
  118. setdef :
  119. begin
  120. if tsetdef(def).elementdef.typ=enumdef then
  121. begin
  122. encodedstr:=encodedstr+'Ljava/util/EnumSet<';
  123. jvmaddencodedtype(tenumdef(tsetdef(def).elementdef).getbasedef,false,encodedstr,true,founderror);
  124. encodedstr:=encodedstr+'>;';
  125. end
  126. else
  127. internalerror(2011051404);
  128. end;
  129. arraydef :
  130. begin
  131. if is_array_of_const(def) then
  132. begin
  133. internalerror(2011051405);
  134. end
  135. else if is_packed_array(def) then
  136. begin
  137. internalerror(2011051406);
  138. end
  139. else
  140. begin
  141. encodedstr:=encodedstr+'[';
  142. jvmaddencodedsignature(tarraydef(def).elementdef,false,encodedstr);
  143. end;
  144. end;
  145. procvardef :
  146. begin
  147. { maybe one day }
  148. internalerror(2011051407);
  149. end;
  150. objectdef :
  151. begin
  152. { maybe one day }
  153. end;
  154. undefineddef,
  155. errordef :
  156. begin
  157. internalerror(2011051408);
  158. end;
  159. procdef :
  160. { must be done via jvmencodemethod() }
  161. internalerror(2011051401);
  162. else
  163. internalerror(2011051402);
  164. end;
  165. end;
  166. function jvmaddencodedtype(def: tdef; bpacked: boolean; var encodedstr: TSymStr; forcesignature: boolean; out founderror: tdef): boolean;
  167. var
  168. c: char;
  169. begin
  170. result:=true;
  171. case def.typ of
  172. stringdef :
  173. begin
  174. case tstringdef(def).stringtype of
  175. { translated into java.lang.String }
  176. st_widestring,
  177. st_unicodestring:
  178. result:=jvmaddencodedtype(java_jlstring,false,encodedstr,forcesignature,founderror);
  179. st_ansistring:
  180. result:=jvmaddencodedtype(java_ansistring,false,encodedstr,forcesignature,founderror);
  181. st_shortstring:
  182. result:=jvmaddencodedtype(java_shortstring,false,encodedstr,forcesignature,founderror);
  183. else
  184. { May be handled via wrapping later }
  185. result:=false;
  186. end;
  187. end;
  188. enumdef:
  189. begin
  190. result:=jvmaddencodedtype(tenumdef(def).classdef,false,encodedstr,forcesignature,founderror);
  191. end;
  192. orddef :
  193. begin
  194. { for procedure "results" }
  195. if is_void(def) then
  196. c:='V'
  197. { only Pascal-style booleans conform to Java's definition of
  198. Boolean }
  199. else if is_pasbool(def) and
  200. (def.size=1) then
  201. c:='Z'
  202. else if is_widechar(def) then
  203. c:='C'
  204. else
  205. begin
  206. case def.size of
  207. 1:
  208. c:='B';
  209. 2:
  210. c:='S';
  211. 4:
  212. c:='I';
  213. 8:
  214. c:='J';
  215. else
  216. internalerror(2010121905);
  217. end;
  218. end;
  219. encodedstr:=encodedstr+c;
  220. end;
  221. pointerdef :
  222. begin
  223. if def=voidpointertype then
  224. result:=jvmaddencodedtype(java_jlobject,false,encodedstr,forcesignature,founderror)
  225. else if jvmimplicitpointertype(tpointerdef(def).pointeddef) then
  226. result:=jvmaddencodedtype(tpointerdef(def).pointeddef,false,encodedstr,forcesignature,founderror)
  227. else
  228. begin
  229. { used for internal pointer constructs }
  230. encodedstr:=encodedstr+'[';
  231. result:=jvmaddencodedtype(tpointerdef(def).pointeddef,false,encodedstr,forcesignature,founderror);
  232. end;
  233. end;
  234. floatdef :
  235. begin
  236. case tfloatdef(def).floattype of
  237. s32real:
  238. c:='F';
  239. s64real:
  240. c:='D';
  241. else
  242. result:=false;
  243. end;
  244. encodedstr:=encodedstr+c;
  245. end;
  246. filedef :
  247. result:=false;
  248. recorddef :
  249. begin
  250. encodedstr:=encodedstr+'L'+trecorddef(def).jvm_full_typename(true)+';'
  251. end;
  252. variantdef :
  253. begin
  254. { will be hanlded via wrapping later, although wrapping may
  255. happen at higher level }
  256. result:=false;
  257. end;
  258. classrefdef :
  259. begin
  260. if not forcesignature then
  261. { unfortunately, java.lang.Class is final, so we can't create
  262. different versions for difference class reference types }
  263. encodedstr:=encodedstr+'Ljava/lang/Class;'
  264. { we can however annotate it with extra signature information in
  265. using Java's generic annotations }
  266. else
  267. jvmaddencodedsignature(def,false,encodedstr);
  268. result:=true;
  269. end;
  270. setdef :
  271. begin
  272. if tsetdef(def).elementdef.typ=enumdef then
  273. begin
  274. if forcesignature then
  275. jvmaddencodedsignature(def,false,encodedstr)
  276. else
  277. result:=jvmaddencodedtype(java_juenumset,false,encodedstr,forcesignature,founderror)
  278. end
  279. else
  280. result:=jvmaddencodedtype(java_jubitset,false,encodedstr,forcesignature,founderror)
  281. end;
  282. formaldef :
  283. begin
  284. {$ifndef nounsupported}
  285. { var/const/out x: JLObject }
  286. result:=jvmaddencodedtype(java_jlobject,false,encodedstr,forcesignature,founderror);
  287. {$else}
  288. result:=false;
  289. {$endif}
  290. end;
  291. arraydef :
  292. begin
  293. if is_array_of_const(def) then
  294. {$ifndef nounsupported}
  295. result:=jvmaddencodedtype(java_jlobject,false,encodedstr,forcesignature,founderror)
  296. {$else}
  297. result:=false
  298. {$endif}
  299. else if is_packed_array(def) then
  300. result:=false
  301. else
  302. begin
  303. encodedstr:=encodedstr+'[';
  304. if not jvmaddencodedtype(tarraydef(def).elementdef,false,encodedstr,forcesignature,founderror) then
  305. begin
  306. result:=false;
  307. { report the exact (nested) error defintion }
  308. exit;
  309. end;
  310. end;
  311. end;
  312. procvardef :
  313. begin
  314. {$ifndef nounsupported}
  315. result:=jvmaddencodedtype(java_jlobject,false,encodedstr,forcesignature,founderror);
  316. {$else}
  317. { will be hanlded via wrapping later, although wrapping may
  318. happen at higher level }
  319. result:=false;
  320. {$endif}
  321. end;
  322. objectdef :
  323. case tobjectdef(def).objecttype of
  324. odt_javaclass,
  325. odt_interfacejava:
  326. encodedstr:=encodedstr+'L'+tobjectdef(def).jvm_full_typename(true)+';'
  327. else
  328. result:=false;
  329. end;
  330. undefineddef,
  331. errordef :
  332. result:=false;
  333. procdef :
  334. { must be done via jvmencodemethod() }
  335. internalerror(2010121903);
  336. else
  337. internalerror(2010121904);
  338. end;
  339. if not result then
  340. founderror:=def;
  341. end;
  342. function jvmtryencodetype(def: tdef; out encodedtype: TSymStr; forcesignature: boolean; out founderror: tdef): boolean;
  343. begin
  344. encodedtype:='';
  345. result:=jvmaddencodedtype(def,false,encodedtype,forcesignature,founderror);
  346. end;
  347. procedure jvmaddtypeownerprefix(owner: tsymtable; var name: TSymStr);
  348. var
  349. owningcontainer: tsymtable;
  350. tmpresult: TSymStr;
  351. module: tmodule;
  352. nameendpos: longint;
  353. begin
  354. { see tprocdef.jvmmangledbasename for description of the format }
  355. owningcontainer:=owner;
  356. while (owningcontainer.symtabletype=localsymtable) do
  357. owningcontainer:=owningcontainer.defowner.owner;
  358. case owningcontainer.symtabletype of
  359. globalsymtable,
  360. staticsymtable:
  361. begin
  362. module:=find_module_from_symtable(owningcontainer);
  363. tmpresult:='';
  364. if assigned(module.namespace) then
  365. tmpresult:=module.namespace^+'/';
  366. tmpresult:=tmpresult+module.realmodulename^+'/';
  367. end;
  368. objectsymtable:
  369. case tobjectdef(owningcontainer.defowner).objecttype of
  370. odt_javaclass,
  371. odt_interfacejava:
  372. begin
  373. tmpresult:=tobjectdef(owningcontainer.defowner).jvm_full_typename(true)+'/'
  374. end
  375. else
  376. internalerror(2010122606);
  377. end;
  378. recordsymtable:
  379. tmpresult:=trecorddef(owningcontainer.defowner).jvm_full_typename(true)+'/'
  380. else
  381. internalerror(2010122605);
  382. end;
  383. name:=tmpresult+name;
  384. nameendpos:=pos(' ',name);
  385. if nameendpos=0 then
  386. nameendpos:=length(name)+1;
  387. insert('''',name,nameendpos);
  388. name:=''''+name;
  389. end;
  390. function jvmarrtype(def: tdef; out primitivetype: boolean): TSymStr;
  391. var
  392. errdef: tdef;
  393. begin
  394. if not jvmtryencodetype(def,result,false,errdef) then
  395. internalerror(2011012205);
  396. primitivetype:=false;
  397. if length(result)=1 then
  398. begin
  399. case result[1] of
  400. 'Z': result:='boolean';
  401. 'C': result:='char';
  402. 'B': result:='byte';
  403. 'S': result:='short';
  404. 'I': result:='int';
  405. 'J': result:='long';
  406. 'F': result:='float';
  407. 'D': result:='double';
  408. else
  409. internalerror(2011012206);
  410. end;
  411. primitivetype:=true;
  412. end
  413. else if (result[1]='L') then
  414. begin
  415. { in case of a class reference, strip the leading 'L' and the
  416. trailing ';' }
  417. setlength(result,length(result)-1);
  418. delete(result,1,1);
  419. end;
  420. { for arrays, use the actual reference type }
  421. end;
  422. function jvmarrtype_setlength(def: tdef): char;
  423. var
  424. errdef: tdef;
  425. res: TSymStr;
  426. begin
  427. { keep in sync with rtl/java/jdynarrh.inc and usage in njvminl }
  428. if is_record(def) then
  429. result:='R'
  430. else if is_shortstring(def) then
  431. result:='T'
  432. else if def.typ=setdef then
  433. begin
  434. if tsetdef(def).elementdef.typ=enumdef then
  435. result:='E'
  436. else
  437. result:='L'
  438. end
  439. else
  440. begin
  441. if not jvmtryencodetype(def,res,false,errdef) then
  442. internalerror(2011012209);
  443. if length(res)=1 then
  444. result:=res[1]
  445. else
  446. result:='A';
  447. end;
  448. end;
  449. function jvmimplicitpointertype(def: tdef): boolean;
  450. begin
  451. case def.typ of
  452. arraydef:
  453. result:=(tarraydef(def).highrange>=tarraydef(def).lowrange) or
  454. is_open_array(def) or
  455. is_array_of_const(def) or
  456. is_array_constructor(def);
  457. recorddef,
  458. setdef:
  459. result:=true;
  460. objectdef:
  461. result:=is_object(def);
  462. stringdef :
  463. result:=tstringdef(def).stringtype in [st_shortstring,st_longstring];
  464. else
  465. result:=false;
  466. end;
  467. end;
  468. function jvmmangledbasename(sym: tsym; const usesymname: TSymStr; withsignature: boolean): TSymStr;
  469. var
  470. container: tsymtable;
  471. vsym: tabstractvarsym;
  472. csym: tconstsym;
  473. begin
  474. case sym.typ of
  475. staticvarsym,
  476. paravarsym,
  477. localvarsym,
  478. fieldvarsym:
  479. begin
  480. vsym:=tabstractvarsym(sym);
  481. result:=jvmencodetype(vsym.vardef,false);
  482. if withsignature and
  483. jvmtypeneedssignature(vsym.vardef) then
  484. begin
  485. result:=result+' signature "';
  486. result:=result+jvmencodetype(vsym.vardef,true)+'"';
  487. end;
  488. if (vsym.typ=paravarsym) and
  489. (vo_is_self in tparavarsym(vsym).varoptions) then
  490. result:='''this'' ' +result
  491. else if (vsym.typ in [paravarsym,localvarsym]) and
  492. ([vo_is_funcret,vo_is_result] * tabstractnormalvarsym(vsym).varoptions <> []) then
  493. result:='''result'' '+result
  494. else
  495. begin
  496. { add array indirection if required }
  497. if (vsym.typ=paravarsym) and
  498. (vsym.vardef.typ=formaldef) or
  499. ((vsym.varspez in [vs_var,vs_out,vs_constref]) and
  500. not jvmimplicitpointertype(vsym.vardef)) then
  501. result:='['+result;
  502. { single quotes for definitions to prevent clashes with Java
  503. opcodes }
  504. if withsignature then
  505. result:=usesymname+''' '+result
  506. else
  507. result:=usesymname+' '+result;
  508. { we have to mangle staticvarsyms in localsymtables to
  509. prevent name clashes... }
  510. if (vsym.typ=staticvarsym) then
  511. begin
  512. container:=sym.Owner;
  513. while (container.symtabletype=localsymtable) do
  514. begin
  515. if tdef(container.defowner).typ<>procdef then
  516. internalerror(2011040303);
  517. { symid is added to prevent problem with overloads }
  518. result:=tprocdef(container.defowner).procsym.realname+'$$'+tostr(tprocdef(container.defowner).procsym.symid)+'$'+result;
  519. container:=container.defowner.owner;
  520. end;
  521. end;
  522. if withsignature then
  523. result:=''''+result
  524. end;
  525. end;
  526. constsym:
  527. begin
  528. csym:=tconstsym(sym);
  529. { some constants can be untyped }
  530. if assigned (csym.constdef) then
  531. begin
  532. result:=jvmencodetype(csym.constdef,false);
  533. if withsignature and
  534. jvmtypeneedssignature(csym.constdef) then
  535. begin
  536. result:=result+' signature "';
  537. result:=result+jvmencodetype(csym.constdef,true)+'"';
  538. end;
  539. end
  540. else
  541. begin
  542. case csym.consttyp of
  543. constord:
  544. result:=jvmencodetype(s32inttype,withsignature);
  545. constreal:
  546. result:=jvmencodetype(s64floattype,withsignature);
  547. constset:
  548. internalerror(2011040701);
  549. constpointer,
  550. constnil:
  551. result:=jvmencodetype(java_jlobject,withsignature);
  552. constwstring,
  553. conststring:
  554. result:=jvmencodetype(java_jlstring,withsignature);
  555. constresourcestring:
  556. internalerror(2011040702);
  557. else
  558. internalerror(2011040703);
  559. end;
  560. end;
  561. if withsignature then
  562. result:=''''+usesymname+''' '+result
  563. else
  564. result:=usesymname+' '+result
  565. end;
  566. else
  567. internalerror(2011021703);
  568. end;
  569. end;
  570. function jvmmangledbasename(sym: tsym; withsignature: boolean): TSymStr;
  571. begin
  572. if (sym.typ=fieldvarsym) and
  573. assigned(tfieldvarsym(sym).externalname) then
  574. result:=jvmmangledbasename(sym,tfieldvarsym(sym).externalname^,withsignature)
  575. else
  576. result:=jvmmangledbasename(sym,sym.RealName,withsignature);
  577. end;
  578. {******************************************************************
  579. jvm type validity checking
  580. *******************************************************************}
  581. function jvmencodetype(def: tdef; withsignature: boolean): TSymStr;
  582. var
  583. errordef: tdef;
  584. begin
  585. if not jvmtryencodetype(def,result,withsignature,errordef) then
  586. internalerror(2011012305);
  587. end;
  588. function jvmchecktype(def: tdef; out founderror: tdef): boolean;
  589. var
  590. encodedtype: TSymStr;
  591. begin
  592. { don't duplicate the code like in objcdef, since the resulting strings
  593. are much shorter here so it's not worth it }
  594. result:=jvmtryencodetype(def,encodedtype,false,founderror);
  595. end;
  596. end.