jvmdef.pas 12 KB

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