jvmdef.pas 12 KB

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