jvmdef.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. { 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. encodedstr:=encodedstr+'L'+trecorddef(def).jvm_full_typename(true)+';'
  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. module: tmodule;
  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. module:=find_module_from_symtable(owningunit);
  218. tmpresult:='';
  219. if assigned(module.namespace) then
  220. tmpresult:=module.namespace^+'.';
  221. tmpresult:=tmpresult+module.realmodulename^+'/';
  222. end;
  223. objectsymtable:
  224. case tobjectdef(owner.defowner).objecttype of
  225. odt_javaclass,
  226. odt_interfacejava:
  227. begin
  228. tmpresult:=tobjectdef(owner.defowner).jvm_full_typename(true)+'/'
  229. end
  230. else
  231. internalerror(2010122606);
  232. end;
  233. recordsymtable:
  234. tmpresult:=trecorddef(owner.defowner).jvm_full_typename(true)+'/'
  235. else
  236. internalerror(2010122605);
  237. end;
  238. name:=tmpresult+name;
  239. end;
  240. function jvmarrtype(def: tdef; out primitivetype: boolean): string;
  241. var
  242. errdef: tdef;
  243. begin
  244. if not jvmtryencodetype(def,result,errdef) then
  245. internalerror(2011012205);
  246. primitivetype:=false;
  247. if length(result)=1 then
  248. begin
  249. case result[1] of
  250. 'Z': result:='boolean';
  251. 'C': result:='char';
  252. 'B': result:='byte';
  253. 'S': result:='short';
  254. 'I': result:='int';
  255. 'J': result:='long';
  256. 'F': result:='float';
  257. 'D': result:='double';
  258. else
  259. internalerror(2011012206);
  260. end;
  261. primitivetype:=true;
  262. end
  263. else if (result[1]='L') then
  264. begin
  265. { in case of a class reference, strip the leading 'L' and the
  266. trailing ';' }
  267. setlength(result,length(result)-1);
  268. delete(result,1,1);
  269. end;
  270. { for arrays, use the actual reference type }
  271. end;
  272. function jvmarrtype_setlength(def: tdef): char;
  273. var
  274. errdef: tdef;
  275. res: string;
  276. begin
  277. if is_record(def) then
  278. result:='R'
  279. else
  280. begin
  281. if not jvmtryencodetype(def,res,errdef) then
  282. internalerror(2011012209);
  283. if length(res)=1 then
  284. result:=res[1]
  285. else
  286. result:='A';
  287. end;
  288. end;
  289. function jvmimplicitpointertype(def: tdef): boolean;
  290. begin
  291. case def.typ of
  292. arraydef:
  293. result:=(tarraydef(def).highrange>=tarraydef(def).lowrange) or
  294. is_open_array(def) or
  295. is_array_of_const(def) or
  296. is_array_constructor(def);
  297. recorddef:
  298. result:=true;
  299. objectdef:
  300. result:=is_object(def);
  301. setdef:
  302. result:=not is_smallset(def);
  303. stringdef :
  304. result:=tstringdef(def).stringtype in [st_shortstring,st_longstring];
  305. else
  306. result:=false;
  307. end;
  308. end;
  309. function jvmmangledbasename(sym: tsym; const usesymname: string): string;
  310. var
  311. vsym: tabstractvarsym;
  312. csym: tconstsym;
  313. founderror: tdef;
  314. begin
  315. case sym.typ of
  316. staticvarsym,
  317. paravarsym,
  318. localvarsym,
  319. fieldvarsym:
  320. begin
  321. vsym:=tabstractvarsym(sym);
  322. result:=jvmencodetype(vsym.vardef);
  323. if (vsym.typ=paravarsym) and
  324. (vo_is_self in tparavarsym(vsym).varoptions) then
  325. result:='this ' +result
  326. else if (vsym.typ in [paravarsym,localvarsym]) and
  327. ([vo_is_funcret,vo_is_result] * tabstractnormalvarsym(vsym).varoptions <> []) then
  328. result:='result '+result
  329. else
  330. result:=usesymname+' '+result;
  331. end;
  332. constsym:
  333. begin
  334. csym:=tconstsym(sym);
  335. result:=jvmencodetype(csym.constdef);
  336. result:=usesymname+' '+result;
  337. end;
  338. else
  339. internalerror(2011021703);
  340. end;
  341. end;
  342. function jvmmangledbasename(sym: tsym): string;
  343. begin
  344. if (sym.typ=fieldvarsym) and
  345. assigned(tfieldvarsym(sym).externalname) then
  346. result:=jvmmangledbasename(sym,tfieldvarsym(sym).externalname^)
  347. else
  348. result:=jvmmangledbasename(sym,sym.RealName);
  349. end;
  350. {******************************************************************
  351. jvm type validity checking
  352. *******************************************************************}
  353. function jvmencodetype(def: tdef): string;
  354. var
  355. errordef: tdef;
  356. begin
  357. if not jvmtryencodetype(def,result,errordef) then
  358. internalerror(2011012305);
  359. end;
  360. function jvmchecktype(def: tdef; out founderror: tdef): boolean;
  361. var
  362. encodedtype: string;
  363. begin
  364. { don't duplicate the code like in objcdef, since the resulting strings
  365. are much shorter here so it's not worth it }
  366. result:=jvmtryencodetype(def,encodedtype,founderror);
  367. end;
  368. end.