jvmdef.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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. 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: TSymStr);
  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: TSymStr; 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. begin
  183. result:=jvmaddencodedtype(tenumdef(def).classdef,false,encodedstr,forcesignature,founderror);
  184. end;
  185. orddef :
  186. begin
  187. { for procedure "results" }
  188. if is_void(def) then
  189. c:='V'
  190. { only Pascal-style booleans conform to Java's definition of
  191. Boolean }
  192. else if is_pasbool(def) and
  193. (def.size=1) then
  194. c:='Z'
  195. else if is_widechar(def) then
  196. c:='C'
  197. else
  198. begin
  199. case def.size of
  200. 1:
  201. c:='B';
  202. 2:
  203. c:='S';
  204. 4:
  205. c:='I';
  206. 8:
  207. c:='J';
  208. else
  209. internalerror(2010121905);
  210. end;
  211. end;
  212. encodedstr:=encodedstr+c;
  213. end;
  214. pointerdef :
  215. begin
  216. if def=voidpointertype then
  217. result:=jvmaddencodedtype(java_jlobject,false,encodedstr,forcesignature,founderror)
  218. else if jvmimplicitpointertype(tpointerdef(def).pointeddef) then
  219. result:=jvmaddencodedtype(tpointerdef(def).pointeddef,false,encodedstr,forcesignature,founderror)
  220. else
  221. begin
  222. { used for internal pointer constructs }
  223. encodedstr:=encodedstr+'[';
  224. result:=jvmaddencodedtype(tpointerdef(def).pointeddef,false,encodedstr,forcesignature,founderror);
  225. end;
  226. end;
  227. floatdef :
  228. begin
  229. case tfloatdef(def).floattype of
  230. s32real:
  231. c:='F';
  232. s64real:
  233. c:='D';
  234. else
  235. result:=false;
  236. end;
  237. encodedstr:=encodedstr+c;
  238. end;
  239. filedef :
  240. result:=false;
  241. recorddef :
  242. begin
  243. encodedstr:=encodedstr+'L'+trecorddef(def).jvm_full_typename(true)+';'
  244. end;
  245. variantdef :
  246. begin
  247. { will be hanlded via wrapping later, although wrapping may
  248. happen at higher level }
  249. result:=false;
  250. end;
  251. classrefdef :
  252. begin
  253. if not forcesignature then
  254. { unfortunately, java.lang.Class is final, so we can't create
  255. different versions for difference class reference types }
  256. encodedstr:=encodedstr+'Ljava/lang/Class;'
  257. { we can however annotate it with extra signature information in
  258. using Java's generic annotations }
  259. else
  260. begin
  261. encodedstr:=encodedstr+'Ljava/lang/Class<';
  262. result:=jvmaddencodedtype(tclassrefdef(def).pointeddef,true,encodedstr,forcesignature,founderror);
  263. encodedstr:=encodedstr+'>;';
  264. end;
  265. result:=true;
  266. end;
  267. setdef :
  268. begin
  269. if is_smallset(def) then
  270. encodedstr:=encodedstr+'I'
  271. else
  272. {$ifndef nounsupported}
  273. result:=jvmaddencodedtype(java_jlobject,false,encodedstr,forcesignature,founderror);
  274. {$else}
  275. { will be hanlded via wrapping later, although wrapping may
  276. happen at higher level }
  277. result:=false;
  278. {$endif}
  279. end;
  280. formaldef :
  281. begin
  282. {$ifndef nounsupported}
  283. { var/const/out x: JLObject }
  284. result:=jvmaddencodedtype(java_jlobject,false,encodedstr,forcesignature,founderror);
  285. {$else}
  286. result:=false;
  287. {$endif}
  288. end;
  289. arraydef :
  290. begin
  291. if is_array_of_const(def) then
  292. {$ifndef nounsupported}
  293. result:=jvmaddencodedtype(java_jlobject,false,encodedstr,forcesignature,founderror)
  294. {$else}
  295. result:=false
  296. {$endif}
  297. else if is_packed_array(def) then
  298. result:=false
  299. else
  300. begin
  301. encodedstr:=encodedstr+'[';
  302. if not jvmaddencodedtype(tarraydef(def).elementdef,false,encodedstr,forcesignature,founderror) then
  303. begin
  304. result:=false;
  305. { report the exact (nested) error defintion }
  306. exit;
  307. end;
  308. end;
  309. end;
  310. procvardef :
  311. begin
  312. {$ifndef nounsupported}
  313. result:=jvmaddencodedtype(java_jlobject,false,encodedstr,forcesignature,founderror);
  314. {$else}
  315. { will be hanlded via wrapping later, although wrapping may
  316. happen at higher level }
  317. result:=false;
  318. {$endif}
  319. end;
  320. objectdef :
  321. case tobjectdef(def).objecttype of
  322. odt_javaclass,
  323. odt_interfacejava:
  324. encodedstr:=encodedstr+'L'+tobjectdef(def).jvm_full_typename(true)+';'
  325. else
  326. result:=false;
  327. end;
  328. undefineddef,
  329. errordef :
  330. result:=false;
  331. procdef :
  332. { must be done via jvmencodemethod() }
  333. internalerror(2010121903);
  334. else
  335. internalerror(2010121904);
  336. end;
  337. if not result then
  338. founderror:=def;
  339. end;
  340. function jvmtryencodetype(def: tdef; out encodedtype: TSymStr; forcesignature: boolean; out founderror: tdef): boolean;
  341. begin
  342. encodedtype:='';
  343. result:=jvmaddencodedtype(def,false,encodedtype,forcesignature,founderror);
  344. end;
  345. procedure jvmaddtypeownerprefix(owner: tsymtable; var name: TSymStr);
  346. var
  347. owningcontainer: tsymtable;
  348. tmpresult: TSymStr;
  349. module: tmodule;
  350. nameendpos: longint;
  351. begin
  352. { see tprocdef.jvmmangledbasename for description of the format }
  353. owningcontainer:=owner;
  354. while (owningcontainer.symtabletype=localsymtable) do
  355. owningcontainer:=owningcontainer.defowner.owner;
  356. case owningcontainer.symtabletype of
  357. globalsymtable,
  358. staticsymtable:
  359. begin
  360. module:=find_module_from_symtable(owningcontainer);
  361. tmpresult:='';
  362. if assigned(module.namespace) then
  363. tmpresult:=module.namespace^+'/';
  364. tmpresult:=tmpresult+module.realmodulename^+'/';
  365. end;
  366. objectsymtable:
  367. case tobjectdef(owningcontainer.defowner).objecttype of
  368. odt_javaclass,
  369. odt_interfacejava:
  370. begin
  371. tmpresult:=tobjectdef(owningcontainer.defowner).jvm_full_typename(true)+'/'
  372. end
  373. else
  374. internalerror(2010122606);
  375. end;
  376. recordsymtable:
  377. tmpresult:=trecorddef(owningcontainer.defowner).jvm_full_typename(true)+'/'
  378. else
  379. internalerror(2010122605);
  380. end;
  381. name:=tmpresult+name;
  382. nameendpos:=pos(' ',name);
  383. if nameendpos=0 then
  384. nameendpos:=length(name)+1;
  385. insert('''',name,nameendpos);
  386. name:=''''+name;
  387. end;
  388. function jvmarrtype(def: tdef; out primitivetype: boolean): TSymStr;
  389. var
  390. errdef: tdef;
  391. begin
  392. if not jvmtryencodetype(def,result,false,errdef) then
  393. internalerror(2011012205);
  394. primitivetype:=false;
  395. if length(result)=1 then
  396. begin
  397. case result[1] of
  398. 'Z': result:='boolean';
  399. 'C': result:='char';
  400. 'B': result:='byte';
  401. 'S': result:='short';
  402. 'I': result:='int';
  403. 'J': result:='long';
  404. 'F': result:='float';
  405. 'D': result:='double';
  406. else
  407. internalerror(2011012206);
  408. end;
  409. primitivetype:=true;
  410. end
  411. else if (result[1]='L') then
  412. begin
  413. { in case of a class reference, strip the leading 'L' and the
  414. trailing ';' }
  415. setlength(result,length(result)-1);
  416. delete(result,1,1);
  417. end;
  418. { for arrays, use the actual reference type }
  419. end;
  420. function jvmarrtype_setlength(def: tdef): char;
  421. var
  422. errdef: tdef;
  423. res: TSymStr;
  424. begin
  425. { keep in sync with rtl/java/jdynarrh.inc and usage in njvminl }
  426. if is_record(def) then
  427. result:='R'
  428. else if is_shortstring(def) then
  429. result:='T'
  430. else
  431. begin
  432. if not jvmtryencodetype(def,res,false,errdef) then
  433. internalerror(2011012209);
  434. if length(res)=1 then
  435. result:=res[1]
  436. else
  437. result:='A';
  438. end;
  439. end;
  440. function jvmimplicitpointertype(def: tdef): boolean;
  441. begin
  442. case def.typ of
  443. arraydef:
  444. result:=(tarraydef(def).highrange>=tarraydef(def).lowrange) or
  445. is_open_array(def) or
  446. is_array_of_const(def) or
  447. is_array_constructor(def);
  448. recorddef:
  449. result:=true;
  450. objectdef:
  451. result:=is_object(def);
  452. setdef:
  453. result:=not is_smallset(def);
  454. stringdef :
  455. result:=tstringdef(def).stringtype in [st_shortstring,st_longstring];
  456. else
  457. result:=false;
  458. end;
  459. end;
  460. function jvmmangledbasename(sym: tsym; const usesymname: TSymStr; withsignature: boolean): TSymStr;
  461. var
  462. container: tsymtable;
  463. vsym: tabstractvarsym;
  464. csym: tconstsym;
  465. begin
  466. case sym.typ of
  467. staticvarsym,
  468. paravarsym,
  469. localvarsym,
  470. fieldvarsym:
  471. begin
  472. vsym:=tabstractvarsym(sym);
  473. result:=jvmencodetype(vsym.vardef,false);
  474. if withsignature and
  475. jvmtypeneedssignature(vsym.vardef) then
  476. begin
  477. result:=result+' signature "';
  478. result:=result+jvmencodetype(vsym.vardef,true)+'"';
  479. end;
  480. if (vsym.typ=paravarsym) and
  481. (vo_is_self in tparavarsym(vsym).varoptions) then
  482. result:='''this'' ' +result
  483. else if (vsym.typ in [paravarsym,localvarsym]) and
  484. ([vo_is_funcret,vo_is_result] * tabstractnormalvarsym(vsym).varoptions <> []) then
  485. result:='''result'' '+result
  486. else
  487. begin
  488. { single quotes for definitions to prevent clashes with Java
  489. opcodes }
  490. if withsignature then
  491. result:=usesymname+''' '+result
  492. else
  493. result:=usesymname+' '+result;
  494. { we have to mangle staticvarsyms in localsymtables to
  495. prevent name clashes... }
  496. if (vsym.typ=staticvarsym) then
  497. begin
  498. container:=sym.Owner;
  499. while (container.symtabletype=localsymtable) do
  500. begin
  501. if tdef(container.defowner).typ<>procdef then
  502. internalerror(2011040303);
  503. { symid is added to prevent problem with overloads }
  504. result:=tprocdef(container.defowner).procsym.realname+'$$'+tostr(tprocdef(container.defowner).procsym.symid)+'$'+result;
  505. container:=container.defowner.owner;
  506. end;
  507. end;
  508. if withsignature then
  509. result:=''''+result
  510. end;
  511. end;
  512. constsym:
  513. begin
  514. csym:=tconstsym(sym);
  515. { some constants can be untyped }
  516. if assigned (csym.constdef) then
  517. begin
  518. result:=jvmencodetype(csym.constdef,false);
  519. if withsignature and
  520. jvmtypeneedssignature(csym.constdef) then
  521. begin
  522. result:=result+' signature "';
  523. result:=result+jvmencodetype(csym.constdef,true)+'"';
  524. end;
  525. end
  526. else
  527. begin
  528. case csym.consttyp of
  529. constord:
  530. result:=jvmencodetype(s32inttype,withsignature);
  531. constreal:
  532. result:=jvmencodetype(s64floattype,withsignature);
  533. constset:
  534. internalerror(2011040701);
  535. constpointer,
  536. constnil:
  537. result:=jvmencodetype(java_jlobject,withsignature);
  538. constwstring,
  539. conststring:
  540. result:=jvmencodetype(java_jlstring,withsignature);
  541. constresourcestring:
  542. internalerror(2011040702);
  543. else
  544. internalerror(2011040703);
  545. end;
  546. end;
  547. if withsignature then
  548. result:=''''+usesymname+''' '+result
  549. else
  550. result:=usesymname+' '+result
  551. end;
  552. else
  553. internalerror(2011021703);
  554. end;
  555. end;
  556. function jvmmangledbasename(sym: tsym; withsignature: boolean): TSymStr;
  557. begin
  558. if (sym.typ=fieldvarsym) and
  559. assigned(tfieldvarsym(sym).externalname) then
  560. result:=jvmmangledbasename(sym,tfieldvarsym(sym).externalname^,withsignature)
  561. else
  562. result:=jvmmangledbasename(sym,sym.RealName,withsignature);
  563. end;
  564. {******************************************************************
  565. jvm type validity checking
  566. *******************************************************************}
  567. function jvmencodetype(def: tdef; withsignature: boolean): TSymStr;
  568. var
  569. errordef: tdef;
  570. begin
  571. if not jvmtryencodetype(def,result,withsignature,errordef) then
  572. internalerror(2011012305);
  573. end;
  574. function jvmchecktype(def: tdef; out founderror: tdef): boolean;
  575. var
  576. encodedtype: TSymStr;
  577. begin
  578. { don't duplicate the code like in objcdef, since the resulting strings
  579. are much shorter here so it's not worth it }
  580. result:=jvmtryencodetype(def,encodedtype,false,founderror);
  581. end;
  582. end.