2
0

jvmdef.pas 15 KB

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