jvmdef.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. { Encode a type into the internal format used by the JVM (descriptor).
  25. Returns false if a type is not representable by the JVM,
  26. and in that case also the failing definition. }
  27. function jvmtryencodetype(def: tdef; out encodedtype: string; out founderror: tdef): boolean;
  28. { same as above, but throws an internal error on failure }
  29. function jvmencodetype(def: tdef): string;
  30. { Check whether a type can be used in a JVM methom signature or field
  31. declaration. }
  32. function jvmchecktype(def: tdef; out founderror: tdef): boolean;
  33. { incremental version of jvmtryencodetype() }
  34. function jvmaddencodedtype(def: tdef; bpacked: boolean; var encodedstr: string; out founderror: tdef): boolean;
  35. { add type prefix (package name) to a type }
  36. procedure jvmaddtypeownerprefix(owner: tsymtable; var name: string);
  37. { returns type string for a single-dimensional array (different from normal
  38. typestring in case of a primitive type) }
  39. function jvmarrtype(def: tdef; out primitivetype: boolean): string;
  40. function jvmarrtype_setlength(def: tdef): char;
  41. { returns whether a def is emulated using an implicit pointer type on the
  42. JVM target (e.g., records, regular arrays, ...) }
  43. function jvmimplicitpointertype(def: tdef): boolean;
  44. { returns the mangled base name for a tsym (type + symbol name, no
  45. visibility etc) }
  46. function jvmmangledbasename(sym: tsym): string;
  47. function jvmmangledbasename(sym: tsym; const usesymname: string): string;
  48. implementation
  49. uses
  50. globtype,
  51. cutils,cclasses,
  52. verbose,systems,
  53. fmodule,
  54. symtable,symconst,symsym,symdef,symcreat,
  55. defutil,paramgr;
  56. {******************************************************************
  57. Type encoding
  58. *******************************************************************}
  59. function jvmaddencodedtype(def: tdef; bpacked: boolean; var encodedstr: string; out founderror: tdef): boolean;
  60. var
  61. c: char;
  62. begin
  63. result:=true;
  64. case def.typ of
  65. stringdef :
  66. begin
  67. case tstringdef(def).stringtype of
  68. { translated into java.lang.String }
  69. st_widestring,
  70. st_unicodestring:
  71. encodedstr:=encodedstr+'Ljava/lang/String;';
  72. else
  73. {$ifndef nounsupported}
  74. result:=jvmaddencodedtype(java_jlobject,false,encodedstr,founderror);
  75. {$else}
  76. { May be handled via wrapping later }
  77. result:=false;
  78. {$endif}
  79. end;
  80. end;
  81. enumdef,
  82. orddef :
  83. begin
  84. { for procedure "results" }
  85. if is_void(def) then
  86. c:='V'
  87. { only Pascal-style booleans conform to Java's definition of
  88. Boolean }
  89. else if is_pasbool(def) and
  90. (def.size=1) then
  91. c:='Z'
  92. else if is_widechar(def) then
  93. c:='C'
  94. else
  95. begin
  96. case def.size of
  97. 1:
  98. c:='B';
  99. 2:
  100. c:='S';
  101. 4:
  102. c:='I';
  103. 8:
  104. c:='J';
  105. else
  106. internalerror(2010121905);
  107. end;
  108. end;
  109. encodedstr:=encodedstr+c;
  110. end;
  111. pointerdef :
  112. begin
  113. {$ifndef nounsupported}
  114. if def=voidpointertype then
  115. result:=jvmaddencodedtype(java_jlobject,false,encodedstr,founderror)
  116. else
  117. {$endif}
  118. { some may be handled via wrapping later }
  119. result:=false;
  120. end;
  121. floatdef :
  122. begin
  123. case tfloatdef(def).floattype of
  124. s32real:
  125. c:='F';
  126. s64real:
  127. c:='D';
  128. else
  129. result:=false;
  130. end;
  131. encodedstr:=encodedstr+c;
  132. end;
  133. filedef :
  134. result:=false;
  135. recorddef :
  136. begin
  137. encodedstr:=encodedstr+'L'+trecorddef(def).jvm_full_typename(true)+';'
  138. end;
  139. variantdef :
  140. begin
  141. { will be hanlded via wrapping later, although wrapping may
  142. happen at higher level }
  143. result:=false;
  144. end;
  145. classrefdef :
  146. begin
  147. {$ifndef nounsupported}
  148. result:=jvmaddencodedtype(java_jlobject,false,encodedstr,founderror);
  149. {$else}
  150. { may be handled via wrapping later }
  151. result:=false;
  152. {$endif}
  153. end;
  154. setdef :
  155. begin
  156. if is_smallset(def) then
  157. encodedstr:=encodedstr+'I'
  158. else
  159. {$ifndef nounsupported}
  160. result:=jvmaddencodedtype(java_jlobject,false,encodedstr,founderror);
  161. {$else}
  162. { will be hanlded via wrapping later, although wrapping may
  163. happen at higher level }
  164. result:=false;
  165. {$endif}
  166. end;
  167. formaldef :
  168. begin
  169. { not supported (may be changed into "java.lang.Object" later) }
  170. result:=false;
  171. end;
  172. arraydef :
  173. begin
  174. if is_array_of_const(def) then
  175. {$ifndef nounsupported}
  176. result:=jvmaddencodedtype(java_jlobject,false,encodedstr,founderror)
  177. {$else}
  178. result:=false
  179. {$endif}
  180. else if is_packed_array(def) then
  181. result:=false
  182. else
  183. begin
  184. encodedstr:=encodedstr+'[';
  185. if not jvmaddencodedtype(tarraydef(def).elementdef,false,encodedstr,founderror) then
  186. begin
  187. result:=false;
  188. { report the exact (nested) error defintion }
  189. exit;
  190. end;
  191. end;
  192. end;
  193. procvardef :
  194. begin
  195. {$ifndef nounsupported}
  196. result:=jvmaddencodedtype(java_jlobject,false,encodedstr,founderror);
  197. {$else}
  198. { will be hanlded via wrapping later, although wrapping may
  199. happen at higher level }
  200. result:=false;
  201. {$endif}
  202. end;
  203. objectdef :
  204. case tobjectdef(def).objecttype of
  205. odt_javaclass,
  206. odt_interfacejava:
  207. encodedstr:=encodedstr+'L'+tobjectdef(def).jvm_full_typename(true)+';'
  208. else
  209. result:=false;
  210. end;
  211. undefineddef,
  212. errordef :
  213. result:=false;
  214. procdef :
  215. { must be done via jvmencodemethod() }
  216. internalerror(2010121903);
  217. else
  218. internalerror(2010121904);
  219. end;
  220. if not result then
  221. founderror:=def;
  222. end;
  223. function jvmtryencodetype(def: tdef; out encodedtype: string; out founderror: tdef): boolean;
  224. begin
  225. encodedtype:='';
  226. result:=jvmaddencodedtype(def,false,encodedtype,founderror);
  227. end;
  228. procedure jvmaddtypeownerprefix(owner: tsymtable; var name: string);
  229. var
  230. owningcontainer: tsymtable;
  231. tmpresult: string;
  232. module: tmodule;
  233. begin
  234. { see tprocdef.jvmmangledbasename for description of the format }
  235. owningcontainer:=owner;
  236. while (owningcontainer.symtabletype=localsymtable) do
  237. owningcontainer:=owningcontainer.defowner.owner;
  238. case owningcontainer.symtabletype of
  239. globalsymtable,
  240. staticsymtable:
  241. begin
  242. module:=find_module_from_symtable(owningcontainer);
  243. tmpresult:='';
  244. if assigned(module.namespace) then
  245. tmpresult:=module.namespace^+'/';
  246. tmpresult:=tmpresult+module.realmodulename^+'/';
  247. end;
  248. objectsymtable:
  249. case tobjectdef(owningcontainer.defowner).objecttype of
  250. odt_javaclass,
  251. odt_interfacejava:
  252. begin
  253. tmpresult:=tobjectdef(owningcontainer.defowner).jvm_full_typename(true)+'/'
  254. end
  255. else
  256. internalerror(2010122606);
  257. end;
  258. recordsymtable:
  259. tmpresult:=trecorddef(owningcontainer.defowner).jvm_full_typename(true)+'/'
  260. else
  261. internalerror(2010122605);
  262. end;
  263. name:=tmpresult+name;
  264. end;
  265. function jvmarrtype(def: tdef; out primitivetype: boolean): string;
  266. var
  267. errdef: tdef;
  268. begin
  269. if not jvmtryencodetype(def,result,errdef) then
  270. internalerror(2011012205);
  271. primitivetype:=false;
  272. if length(result)=1 then
  273. begin
  274. case result[1] of
  275. 'Z': result:='boolean';
  276. 'C': result:='char';
  277. 'B': result:='byte';
  278. 'S': result:='short';
  279. 'I': result:='int';
  280. 'J': result:='long';
  281. 'F': result:='float';
  282. 'D': result:='double';
  283. else
  284. internalerror(2011012206);
  285. end;
  286. primitivetype:=true;
  287. end
  288. else if (result[1]='L') then
  289. begin
  290. { in case of a class reference, strip the leading 'L' and the
  291. trailing ';' }
  292. setlength(result,length(result)-1);
  293. delete(result,1,1);
  294. end;
  295. { for arrays, use the actual reference type }
  296. end;
  297. function jvmarrtype_setlength(def: tdef): char;
  298. var
  299. errdef: tdef;
  300. res: string;
  301. begin
  302. if is_record(def) then
  303. result:='R'
  304. else
  305. begin
  306. if not jvmtryencodetype(def,res,errdef) then
  307. internalerror(2011012209);
  308. if length(res)=1 then
  309. result:=res[1]
  310. else
  311. result:='A';
  312. end;
  313. end;
  314. function jvmimplicitpointertype(def: tdef): boolean;
  315. begin
  316. case def.typ of
  317. arraydef:
  318. result:=(tarraydef(def).highrange>=tarraydef(def).lowrange) or
  319. is_open_array(def) or
  320. is_array_of_const(def) or
  321. is_array_constructor(def);
  322. recorddef:
  323. result:=true;
  324. objectdef:
  325. result:=is_object(def);
  326. setdef:
  327. result:=not is_smallset(def);
  328. stringdef :
  329. result:=tstringdef(def).stringtype in [st_shortstring,st_longstring];
  330. else
  331. result:=false;
  332. end;
  333. end;
  334. function jvmmangledbasename(sym: tsym; const usesymname: string): string;
  335. var
  336. container: tsymtable;
  337. vsym: tabstractvarsym;
  338. csym: tconstsym;
  339. founderror: tdef;
  340. begin
  341. case sym.typ of
  342. staticvarsym,
  343. paravarsym,
  344. localvarsym,
  345. fieldvarsym:
  346. begin
  347. vsym:=tabstractvarsym(sym);
  348. result:=jvmencodetype(vsym.vardef);
  349. if (vsym.typ=paravarsym) and
  350. (vo_is_self in tparavarsym(vsym).varoptions) then
  351. result:='this ' +result
  352. else if (vsym.typ in [paravarsym,localvarsym]) and
  353. ([vo_is_funcret,vo_is_result] * tabstractnormalvarsym(vsym).varoptions <> []) then
  354. result:='result '+result
  355. else
  356. begin
  357. { we have to mangle staticvarsyms in localsymtables to
  358. prevent name clashes... }
  359. container:=sym.Owner;
  360. result:=usesymname+' '+result;
  361. while (container.symtabletype=localsymtable) do
  362. begin
  363. if tdef(container.defowner).typ<>procdef then
  364. internalerror(2011040303);
  365. { not safe yet in case of overloaded routines, need to
  366. encode parameters too or find another way for conflict
  367. resolution }
  368. result:=tprocdef(container.defowner).procsym.realname+'$'+result;
  369. container:=container.defowner.owner;
  370. end;
  371. end;
  372. end;
  373. constsym:
  374. begin
  375. csym:=tconstsym(sym);
  376. { some constants can be untyped }
  377. if assigned (csym.constdef) then
  378. result:=jvmencodetype(csym.constdef)
  379. else
  380. begin
  381. case csym.consttyp of
  382. constord:
  383. result:=jvmencodetype(s32inttype);
  384. constreal:
  385. result:=jvmencodetype(s64floattype);
  386. constset:
  387. internalerror(2011040701);
  388. constpointer,
  389. constnil:
  390. result:=jvmencodetype(java_jlobject);
  391. constwstring,
  392. conststring:
  393. result:=jvmencodetype(java_jlstring);
  394. constguid:
  395. internalerror(2011040702);
  396. else
  397. internalerror(2011040703);
  398. end;
  399. end;
  400. result:=usesymname+' '+result;
  401. end;
  402. else
  403. internalerror(2011021703);
  404. end;
  405. end;
  406. function jvmmangledbasename(sym: tsym): string;
  407. begin
  408. if (sym.typ=fieldvarsym) and
  409. assigned(tfieldvarsym(sym).externalname) then
  410. result:=jvmmangledbasename(sym,tfieldvarsym(sym).externalname^)
  411. else
  412. result:=jvmmangledbasename(sym,sym.RealName);
  413. end;
  414. {******************************************************************
  415. jvm type validity checking
  416. *******************************************************************}
  417. function jvmencodetype(def: tdef): string;
  418. var
  419. errordef: tdef;
  420. begin
  421. if not jvmtryencodetype(def,result,errordef) then
  422. internalerror(2011012305);
  423. end;
  424. function jvmchecktype(def: tdef; out founderror: tdef): boolean;
  425. var
  426. encodedtype: string;
  427. begin
  428. { don't duplicate the code like in objcdef, since the resulting strings
  429. are much shorter here so it's not worth it }
  430. result:=jvmtryencodetype(def,encodedtype,founderror);
  431. end;
  432. end.