jvmdef.pas 21 KB

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