jvmdef.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. implementation
  48. uses
  49. globtype,
  50. cutils,cclasses,
  51. verbose,systems,
  52. fmodule,
  53. symtable,symconst,symsym,symdef,
  54. defutil,paramgr;
  55. {******************************************************************
  56. Type encoding
  57. *******************************************************************}
  58. function jvmaddencodedtype(def: tdef; bpacked: boolean; var encodedstr: string; out founderror: tdef): boolean;
  59. var
  60. c: char;
  61. begin
  62. result:=true;
  63. case def.typ of
  64. stringdef :
  65. begin
  66. case tstringdef(def).stringtype of
  67. { translated into Java.Lang.String }
  68. st_widestring:
  69. encodedstr:=encodedstr+'Ljava/lang/String;';
  70. else
  71. { May be handled via wrapping later }
  72. result:=false;
  73. end;
  74. end;
  75. enumdef,
  76. orddef :
  77. begin
  78. { for procedure "results" }
  79. if is_void(def) then
  80. c:='V'
  81. { only Pascal-style booleans conform to Java's definition of
  82. Boolean }
  83. else if is_pasbool(def) and
  84. (def.size=1) then
  85. c:='Z'
  86. else if is_widechar(def) then
  87. c:='C'
  88. else
  89. begin
  90. case def.size of
  91. 1:
  92. c:='B';
  93. 2:
  94. c:='S';
  95. 4:
  96. c:='I';
  97. 8:
  98. c:='J';
  99. else
  100. internalerror(2010121905);
  101. end;
  102. end;
  103. encodedstr:=encodedstr+c;
  104. end;
  105. pointerdef :
  106. begin
  107. { some may be handled via wrapping later }
  108. result:=false;
  109. end;
  110. floatdef :
  111. begin
  112. case tfloatdef(def).floattype of
  113. s32real:
  114. c:='F';
  115. s64real:
  116. c:='D';
  117. else
  118. result:=false;
  119. end;
  120. encodedstr:=encodedstr+c;
  121. end;
  122. filedef :
  123. result:=false;
  124. recorddef :
  125. begin
  126. { will be hanlded via wrapping later, although wrapping may
  127. happen at higher level }
  128. result:=false;
  129. end;
  130. variantdef :
  131. begin
  132. { will be hanlded via wrapping later, although wrapping may
  133. happen at higher level }
  134. result:=false;
  135. end;
  136. classrefdef :
  137. begin
  138. { may be handled via wrapping later }
  139. result:=false;
  140. end;
  141. setdef :
  142. begin
  143. if is_smallset(def) then
  144. encodedstr:=encodedstr+'I'
  145. else
  146. { will be hanlded via wrapping later, although wrapping may
  147. happen at higher level }
  148. result:=false;
  149. end;
  150. formaldef :
  151. begin
  152. { not supported (may be changed into "java.lang.Object" later) }
  153. result:=false;
  154. end;
  155. arraydef :
  156. begin
  157. if is_array_of_const(def) or
  158. is_packed_array(def) then
  159. result:=false
  160. else
  161. begin
  162. encodedstr:=encodedstr+'[';
  163. if not jvmaddencodedtype(tarraydef(def).elementdef,false,encodedstr,founderror) then
  164. begin
  165. result:=false;
  166. { report the exact (nested) error defintion }
  167. exit;
  168. end;
  169. end;
  170. end;
  171. procvardef :
  172. begin
  173. { will be hanlded via wrapping later, although wrapping may
  174. happen at higher level }
  175. result:=false;
  176. end;
  177. objectdef :
  178. case tobjectdef(def).objecttype of
  179. odt_javaclass,
  180. odt_interfacejava:
  181. encodedstr:=encodedstr+'L'+tobjectdef(def).jvm_full_typename(true)+';'
  182. else
  183. result:=false;
  184. end;
  185. undefineddef,
  186. errordef :
  187. result:=false;
  188. procdef :
  189. { must be done via jvmencodemethod() }
  190. internalerror(2010121903);
  191. else
  192. internalerror(2010121904);
  193. end;
  194. if not result then
  195. founderror:=def;
  196. end;
  197. function jvmtryencodetype(def: tdef; out encodedtype: string; out founderror: tdef): boolean;
  198. begin
  199. encodedtype:='';
  200. result:=jvmaddencodedtype(def,false,encodedtype,founderror);
  201. end;
  202. procedure jvmaddtypeownerprefix(owner: tsymtable; var name: string);
  203. var
  204. owningunit: tsymtable;
  205. tmpresult: string;
  206. begin
  207. { see tprocdef.jvmmangledbasename for description of the format }
  208. case owner.symtabletype of
  209. globalsymtable,
  210. staticsymtable,
  211. localsymtable:
  212. begin
  213. owningunit:=owner;
  214. while (owningunit.symtabletype in [localsymtable,objectsymtable,recordsymtable]) do
  215. owningunit:=owningunit.defowner.owner;
  216. tmpresult:=find_module_from_symtable(owningunit).realmodulename^+'/';
  217. end;
  218. objectsymtable:
  219. case tobjectdef(owner.defowner).objecttype of
  220. odt_javaclass,
  221. odt_interfacejava:
  222. begin
  223. tmpresult:=tobjectdef(owner.defowner).jvm_full_typename(true)+'/'
  224. end
  225. else
  226. internalerror(2010122606);
  227. end
  228. else
  229. internalerror(2010122605);
  230. end;
  231. name:=tmpresult+name;
  232. end;
  233. function jvmarrtype(def: tdef; out primitivetype: boolean): string;
  234. var
  235. errdef: tdef;
  236. begin
  237. if not jvmtryencodetype(def,result,errdef) then
  238. internalerror(2011012205);
  239. primitivetype:=false;
  240. if length(result)=1 then
  241. begin
  242. case result[1] of
  243. 'Z': result:='boolean';
  244. 'C': result:='char';
  245. 'B': result:='byte';
  246. 'S': result:='short';
  247. 'I': result:='int';
  248. 'J': result:='long';
  249. 'F': result:='float';
  250. 'D': result:='double';
  251. else
  252. internalerror(2011012206);
  253. end;
  254. primitivetype:=true;
  255. end
  256. else if (result[1]='L') then
  257. begin
  258. { in case of a class reference, strip the leading 'L' and the
  259. trailing ';' }
  260. setlength(result,length(result)-1);
  261. delete(result,1,1);
  262. end;
  263. { for arrays, use the actual reference type }
  264. end;
  265. function jvmarrtype_setlength(def: tdef): char;
  266. var
  267. errdef: tdef;
  268. res: string;
  269. begin
  270. if not jvmtryencodetype(def,res,errdef) then
  271. internalerror(2011012209);
  272. if length(res)=1 then
  273. result:=res[1]
  274. else
  275. result:='A';
  276. end;
  277. function jvmimplicitpointertype(def: tdef): boolean;
  278. begin
  279. case def.typ of
  280. arraydef:
  281. result:=(tarraydef(def).highrange>=tarraydef(def).lowrange) or
  282. is_open_array(def) or
  283. is_array_of_const(def) or
  284. is_array_constructor(def);
  285. recorddef:
  286. result:=true;
  287. objectdef:
  288. result:=is_object(def);
  289. setdef:
  290. result:=not is_smallset(def);
  291. stringdef :
  292. result:=tstringdef(def).stringtype in [st_shortstring,st_longstring];
  293. else
  294. result:=false;
  295. end;
  296. end;
  297. function jvmmangledbasename(sym: tsym): string;
  298. var
  299. vsym: tabstractvarsym;
  300. csym: tconstsym;
  301. founderror: tdef;
  302. begin
  303. case sym.typ of
  304. staticvarsym,
  305. paravarsym,
  306. localvarsym,
  307. fieldvarsym:
  308. begin
  309. vsym:=tabstractvarsym(sym);
  310. result:=jvmencodetype(vsym.vardef);
  311. if (vsym.typ=paravarsym) and
  312. (vo_is_self in tparavarsym(vsym).varoptions) then
  313. result:='this ' +result
  314. else if (vsym.typ in [paravarsym,localvarsym]) and
  315. ([vo_is_funcret,vo_is_result] * tabstractnormalvarsym(vsym).varoptions <> []) then
  316. result:='result '+result
  317. else
  318. result:=vsym.realname+' '+result;
  319. end;
  320. constsym:
  321. begin
  322. csym:=tconstsym(sym);
  323. result:=jvmencodetype(csym.constdef);
  324. result:=csym.realname+' '+result;
  325. end;
  326. else
  327. internalerror(2011021703);
  328. end;
  329. end;
  330. {******************************************************************
  331. jvm type validity checking
  332. *******************************************************************}
  333. function jvmencodetype(def: tdef): string;
  334. var
  335. errordef: tdef;
  336. begin
  337. if not jvmtryencodetype(def,result,errordef) then
  338. internalerror(2011012305);
  339. end;
  340. function jvmchecktype(def: tdef; out founderror: tdef): boolean;
  341. var
  342. encodedtype: string;
  343. begin
  344. { don't duplicate the code like in objcdef, since the resulting strings
  345. are much shorter here so it's not worth it }
  346. result:=jvmtryencodetype(def,encodedtype,founderror);
  347. end;
  348. end.