jvmdef.pas 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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. globtype,
  23. node,
  24. symbase,symtype;
  25. { returns whether a def can make use of an extra type signature (for
  26. Java-style generics annotations; not use for FPC-style generics or their
  27. translations, but to annotate the kind of classref a java.lang.Class is
  28. and things like that) }
  29. function jvmtypeneedssignature(def: tdef): boolean;
  30. { create a signature encoding of a particular type; requires that
  31. jvmtypeneedssignature returned "true" for this type }
  32. procedure jvmaddencodedsignature(def: tdef; bpacked: boolean; var encodedstr: TSymStr);
  33. { Encode a type into the internal format used by the JVM (descriptor).
  34. Returns false if a type is not representable by the JVM,
  35. and in that case also the failing definition. }
  36. function jvmtryencodetype(def: tdef; out encodedtype: TSymStr; forcesignature: boolean; out founderror: tdef): boolean;
  37. { same as above, but throws an internal error on failure }
  38. function jvmencodetype(def: tdef; withsignature: boolean): TSymStr;
  39. { Check whether a type can be used in a JVM methom signature or field
  40. declaration. }
  41. function jvmchecktype(def: tdef; out founderror: tdef): boolean;
  42. { incremental version of jvmtryencodetype() }
  43. function jvmaddencodedtype(def: tdef; bpacked: boolean; var encodedstr: TSymStr; forcesignature: boolean; out founderror: tdef): boolean;
  44. { add type prefix (package name) to a type }
  45. procedure jvmaddtypeownerprefix(owner: tsymtable; var name: TSymStr);
  46. { returns type string for a single-dimensional array (different from normal
  47. typestring in case of a primitive type) }
  48. function jvmarrtype(def: tdef; out primitivetype: boolean): TSymStr;
  49. function jvmarrtype_setlength(def: tdef): char;
  50. { returns whether a def is emulated using an implicit pointer type on the
  51. JVM target (e.g., records, regular arrays, ...) }
  52. function jvmimplicitpointertype(def: tdef): boolean;
  53. { returns the mangled base name for a tsym (type + symbol name, no
  54. visibility etc); also adds signature attribute if requested and
  55. appropriate }
  56. function jvmmangledbasename(sym: tsym; withsignature: boolean): TSymStr;
  57. function jvmmangledbasename(sym: tsym; const usesymname: TSymStr; withsignature: boolean): TSymStr;
  58. { sometimes primitive types have to be boxed/unboxed via class types. This
  59. routine returns the appropriate box type for the passed primitive type }
  60. procedure jvmgetboxtype(def: tdef; out objdef, paradef: tdef; mergeints: boolean);
  61. function jvmgetunboxmethod(def: tdef): string;
  62. function jvmgetcorrespondingclassdef(def: tdef): tdef;
  63. implementation
  64. uses
  65. cutils,cclasses,
  66. verbose,systems,
  67. fmodule,
  68. symtable,symconst,symsym,symdef,symcreat,
  69. defutil,paramgr;
  70. {******************************************************************
  71. Type encoding
  72. *******************************************************************}
  73. function jvmtypeneedssignature(def: tdef): boolean;
  74. var
  75. i: longint;
  76. begin
  77. result:=false;
  78. case def.typ of
  79. classrefdef,
  80. setdef:
  81. begin
  82. result:=true;
  83. end;
  84. arraydef :
  85. begin
  86. result:=jvmtypeneedssignature(tarraydef(def).elementdef);
  87. end;
  88. procvardef :
  89. begin
  90. { may change in the future }
  91. end;
  92. procdef :
  93. begin
  94. for i:=0 to tprocdef(def).paras.count-1 do
  95. begin
  96. result:=jvmtypeneedssignature(tparavarsym(tprocdef(def).paras[i]).vardef);
  97. if result then
  98. exit;
  99. end;
  100. end
  101. else
  102. result:=false;
  103. end;
  104. end;
  105. procedure jvmaddencodedsignature(def: tdef; bpacked: boolean; var encodedstr: TSymStr);
  106. var
  107. founderror: tdef;
  108. begin
  109. case def.typ of
  110. pointerdef :
  111. begin
  112. { maybe one day }
  113. internalerror(2011051403);
  114. end;
  115. classrefdef :
  116. begin
  117. { Ljava/lang/Class<+SomeClassType> means
  118. "Ljava/lang/Class<SomeClassType_or_any_of_its_descendents>" }
  119. encodedstr:=encodedstr+'Ljava/lang/Class<+';
  120. jvmaddencodedtype(tclassrefdef(def).pointeddef,false,encodedstr,true,founderror);
  121. encodedstr:=encodedstr+'>;';
  122. end;
  123. setdef :
  124. begin
  125. if tsetdef(def).elementdef.typ=enumdef then
  126. begin
  127. encodedstr:=encodedstr+'Ljava/util/EnumSet<';
  128. jvmaddencodedtype(tenumdef(tsetdef(def).elementdef).getbasedef,false,encodedstr,true,founderror);
  129. encodedstr:=encodedstr+'>;';
  130. end
  131. else
  132. internalerror(2011051404);
  133. end;
  134. arraydef :
  135. begin
  136. if is_array_of_const(def) then
  137. begin
  138. internalerror(2011051405);
  139. end
  140. else if is_packed_array(def) then
  141. begin
  142. internalerror(2011051406);
  143. end
  144. else
  145. begin
  146. encodedstr:=encodedstr+'[';
  147. jvmaddencodedsignature(tarraydef(def).elementdef,false,encodedstr);
  148. end;
  149. end;
  150. procvardef :
  151. begin
  152. { maybe one day }
  153. internalerror(2011051407);
  154. end;
  155. objectdef :
  156. begin
  157. { maybe one day }
  158. end;
  159. undefineddef,
  160. errordef :
  161. begin
  162. internalerror(2011051408);
  163. end;
  164. procdef :
  165. { must be done via jvmencodemethod() }
  166. internalerror(2011051401);
  167. else
  168. internalerror(2011051402);
  169. end;
  170. end;
  171. function jvmaddencodedtype(def: tdef; bpacked: boolean; var encodedstr: TSymStr; forcesignature: boolean; out founderror: tdef): boolean;
  172. var
  173. c: char;
  174. begin
  175. result:=true;
  176. case def.typ of
  177. stringdef :
  178. begin
  179. case tstringdef(def).stringtype of
  180. { translated into java.lang.String }
  181. st_widestring,
  182. st_unicodestring:
  183. result:=jvmaddencodedtype(java_jlstring,false,encodedstr,forcesignature,founderror);
  184. st_ansistring:
  185. result:=jvmaddencodedtype(java_ansistring,false,encodedstr,forcesignature,founderror);
  186. st_shortstring:
  187. result:=jvmaddencodedtype(java_shortstring,false,encodedstr,forcesignature,founderror);
  188. else
  189. { May be handled via wrapping later }
  190. result:=false;
  191. end;
  192. end;
  193. enumdef:
  194. begin
  195. result:=jvmaddencodedtype(tenumdef(def).classdef,false,encodedstr,forcesignature,founderror);
  196. end;
  197. orddef :
  198. begin
  199. { for procedure "results" }
  200. if is_void(def) then
  201. c:='V'
  202. { only Pascal-style booleans conform to Java's definition of
  203. Boolean }
  204. else if is_pasbool(def) and
  205. (def.size=1) then
  206. c:='Z'
  207. else if is_widechar(def) then
  208. c:='C'
  209. else
  210. begin
  211. case def.size of
  212. 1:
  213. c:='B';
  214. 2:
  215. c:='S';
  216. 4:
  217. c:='I';
  218. 8:
  219. c:='J';
  220. else
  221. internalerror(2010121905);
  222. end;
  223. end;
  224. encodedstr:=encodedstr+c;
  225. end;
  226. pointerdef :
  227. begin
  228. if is_voidpointer(def) then
  229. result:=jvmaddencodedtype(java_jlobject,false,encodedstr,forcesignature,founderror)
  230. else if tpointerdef(def).pointeddef.typ in [orddef,floatdef] then
  231. begin
  232. encodedstr:=encodedstr+'[';
  233. result:=jvmaddencodedtype(tpointerdef(def).pointeddef,false,encodedstr,forcesignature,founderror);
  234. end
  235. else if jvmimplicitpointertype(tpointerdef(def).pointeddef) then
  236. result:=jvmaddencodedtype(tpointerdef(def).pointeddef,false,encodedstr,forcesignature,founderror)
  237. else
  238. begin
  239. { Semantically, these are pointers to types that are
  240. pointer-based themselves (or typecastable to pointer).
  241. Internally, we represent them all as array of JLObject so that
  242. they are assignment-compatible. We will perform the type
  243. checks when actually loading a value from them }
  244. encodedstr:=encodedstr+'[';
  245. result:=jvmaddencodedtype(java_jlobject,false,encodedstr,forcesignature,founderror)
  246. end;
  247. end;
  248. floatdef :
  249. begin
  250. case tfloatdef(def).floattype of
  251. s32real:
  252. c:='F';
  253. s64real:
  254. c:='D';
  255. else
  256. result:=false;
  257. end;
  258. encodedstr:=encodedstr+c;
  259. end;
  260. filedef :
  261. result:=false;
  262. recorddef :
  263. begin
  264. encodedstr:=encodedstr+'L'+trecorddef(def).jvm_full_typename(true)+';'
  265. end;
  266. variantdef :
  267. begin
  268. { will be hanlded via wrapping later, although wrapping may
  269. happen at higher level }
  270. result:=false;
  271. end;
  272. classrefdef :
  273. begin
  274. if not forcesignature then
  275. { unfortunately, java.lang.Class is final, so we can't create
  276. different versions for difference class reference types }
  277. encodedstr:=encodedstr+'Ljava/lang/Class;'
  278. { we can however annotate it with extra signature information in
  279. using Java's generic annotations }
  280. else
  281. jvmaddencodedsignature(def,false,encodedstr);
  282. result:=true;
  283. end;
  284. setdef :
  285. begin
  286. if tsetdef(def).elementdef.typ=enumdef then
  287. begin
  288. if forcesignature then
  289. jvmaddencodedsignature(def,false,encodedstr)
  290. else
  291. result:=jvmaddencodedtype(java_juenumset,false,encodedstr,forcesignature,founderror)
  292. end
  293. else
  294. result:=jvmaddencodedtype(java_jubitset,false,encodedstr,forcesignature,founderror)
  295. end;
  296. formaldef :
  297. begin
  298. { var/const/out x: JLObject }
  299. result:=jvmaddencodedtype(java_jlobject,false,encodedstr,forcesignature,founderror);
  300. end;
  301. arraydef :
  302. begin
  303. if is_array_of_const(def) then
  304. begin
  305. encodedstr:=encodedstr+'[';
  306. result:=jvmaddencodedtype(search_system_type('TVARREC').typedef,false,encodedstr,forcesignature,founderror);
  307. end
  308. else if is_packed_array(def) then
  309. result:=false
  310. else
  311. begin
  312. encodedstr:=encodedstr+'[';
  313. if not jvmaddencodedtype(tarraydef(def).elementdef,false,encodedstr,forcesignature,founderror) then
  314. begin
  315. result:=false;
  316. { report the exact (nested) error defintion }
  317. exit;
  318. end;
  319. end;
  320. end;
  321. procvardef :
  322. begin
  323. result:=jvmaddencodedtype(tprocvardef(def).classdef,false,encodedstr,forcesignature,founderror);
  324. end;
  325. objectdef :
  326. case tobjectdef(def).objecttype of
  327. odt_javaclass,
  328. odt_interfacejava:
  329. begin
  330. def:=maybe_find_real_class_definition(def,false);
  331. encodedstr:=encodedstr+'L'+tobjectdef(def).jvm_full_typename(true)+';'
  332. end
  333. else
  334. result:=false;
  335. end;
  336. undefineddef,
  337. errordef :
  338. result:=false;
  339. procdef :
  340. { must be done via jvmencodemethod() }
  341. internalerror(2010121903);
  342. else
  343. internalerror(2010121904);
  344. end;
  345. if not result then
  346. founderror:=def;
  347. end;
  348. function jvmtryencodetype(def: tdef; out encodedtype: TSymStr; forcesignature: boolean; out founderror: tdef): boolean;
  349. begin
  350. encodedtype:='';
  351. result:=jvmaddencodedtype(def,false,encodedtype,forcesignature,founderror);
  352. end;
  353. procedure jvmaddtypeownerprefix(owner: tsymtable; var name: TSymStr);
  354. var
  355. owningcontainer: tsymtable;
  356. tmpresult: TSymStr;
  357. module: tmodule;
  358. nameendpos: longint;
  359. begin
  360. { see tprocdef.jvmmangledbasename for description of the format }
  361. owningcontainer:=owner;
  362. while (owningcontainer.symtabletype=localsymtable) do
  363. owningcontainer:=owningcontainer.defowner.owner;
  364. case owningcontainer.symtabletype of
  365. globalsymtable,
  366. staticsymtable:
  367. begin
  368. module:=find_module_from_symtable(owningcontainer);
  369. tmpresult:='';
  370. if assigned(module.namespace) then
  371. tmpresult:=module.namespace^+'/';
  372. tmpresult:=tmpresult+module.realmodulename^+'/';
  373. end;
  374. objectsymtable:
  375. case tobjectdef(owningcontainer.defowner).objecttype of
  376. odt_javaclass,
  377. odt_interfacejava:
  378. begin
  379. tmpresult:=tobjectdef(owningcontainer.defowner).jvm_full_typename(true)+'/'
  380. end
  381. else
  382. internalerror(2010122606);
  383. end;
  384. recordsymtable:
  385. tmpresult:=trecorddef(owningcontainer.defowner).jvm_full_typename(true)+'/'
  386. else
  387. internalerror(2010122605);
  388. end;
  389. name:=tmpresult+name;
  390. nameendpos:=pos(' ',name);
  391. if nameendpos=0 then
  392. nameendpos:=length(name)+1;
  393. insert('''',name,nameendpos);
  394. name:=''''+name;
  395. end;
  396. function jvmarrtype(def: tdef; out primitivetype: boolean): TSymStr;
  397. var
  398. errdef: tdef;
  399. begin
  400. if not jvmtryencodetype(def,result,false,errdef) then
  401. internalerror(2011012205);
  402. primitivetype:=false;
  403. if length(result)=1 then
  404. begin
  405. case result[1] of
  406. 'Z': result:='boolean';
  407. 'C': result:='char';
  408. 'B': result:='byte';
  409. 'S': result:='short';
  410. 'I': result:='int';
  411. 'J': result:='long';
  412. 'F': result:='float';
  413. 'D': result:='double';
  414. else
  415. internalerror(2011012206);
  416. end;
  417. primitivetype:=true;
  418. end
  419. else if (result[1]='L') then
  420. begin
  421. { in case of a class reference, strip the leading 'L' and the
  422. trailing ';' }
  423. setlength(result,length(result)-1);
  424. delete(result,1,1);
  425. end;
  426. { for arrays, use the actual reference type }
  427. end;
  428. function jvmarrtype_setlength(def: tdef): char;
  429. var
  430. errdef: tdef;
  431. res: TSymStr;
  432. begin
  433. { keep in sync with rtl/java/jdynarrh.inc and usage in njvminl }
  434. if is_record(def) then
  435. result:='R'
  436. else if is_shortstring(def) then
  437. result:='T'
  438. else if def.typ=setdef then
  439. begin
  440. if tsetdef(def).elementdef.typ=enumdef then
  441. result:='E'
  442. else
  443. result:='L'
  444. end
  445. else if (def.typ=procvardef) and
  446. not tprocvardef(def).is_addressonly then
  447. result:='P'
  448. else
  449. begin
  450. if not jvmtryencodetype(def,res,false,errdef) then
  451. internalerror(2011012209);
  452. if length(res)=1 then
  453. result:=res[1]
  454. else
  455. result:='A';
  456. end;
  457. end;
  458. function jvmimplicitpointertype(def: tdef): boolean;
  459. begin
  460. case def.typ of
  461. arraydef:
  462. result:=(tarraydef(def).highrange>=tarraydef(def).lowrange) or
  463. is_open_array(def) or
  464. is_array_of_const(def) or
  465. is_array_constructor(def);
  466. recorddef,
  467. setdef:
  468. result:=true;
  469. objectdef:
  470. result:=is_object(def);
  471. stringdef :
  472. result:=tstringdef(def).stringtype in [st_shortstring,st_longstring];
  473. procvardef:
  474. result:=not tprocvardef(def).is_addressonly;
  475. else
  476. result:=false;
  477. end;
  478. end;
  479. { mergeints = true means that all integer types are mapped to jllong,
  480. otherwise they are mapped to the closest corresponding type }
  481. procedure jvmgetboxtype(def: tdef; out objdef, paradef: tdef; mergeints: boolean);
  482. begin
  483. case def.typ of
  484. orddef:
  485. begin
  486. case torddef(def).ordtype of
  487. pasbool8:
  488. begin
  489. objdef:=tobjectdef(search_system_type('JLBOOLEAN').typedef);
  490. paradef:=pasbool8type;
  491. end;
  492. uwidechar:
  493. begin
  494. objdef:=tobjectdef(search_system_type('JLCHARACTER').typedef);
  495. paradef:=cwidechartype;
  496. end;
  497. else
  498. begin
  499. { wrap all integer types into a JLLONG, so that we don't get
  500. errors after returning a byte assigned to a long etc }
  501. if mergeints or
  502. (torddef(def).ordtype in [s64bit,u64bit,scurrency,bool64bit,pasbool64]) then
  503. begin
  504. objdef:=tobjectdef(search_system_type('JLLONG').typedef);
  505. paradef:=s64inttype;
  506. end
  507. else
  508. begin
  509. case torddef(def).ordtype of
  510. s8bit,
  511. u8bit,
  512. uchar,
  513. bool8bit:
  514. begin
  515. objdef:=tobjectdef(search_system_type('JLBYTE').typedef);
  516. paradef:=s8inttype;
  517. end;
  518. s16bit,
  519. u16bit,
  520. bool16bit,
  521. pasbool16:
  522. begin
  523. objdef:=tobjectdef(search_system_type('JLSHORT').typedef);
  524. paradef:=s16inttype;
  525. end;
  526. s32bit,
  527. u32bit,
  528. bool32bit,
  529. pasbool32:
  530. begin
  531. objdef:=tobjectdef(search_system_type('JLINTEGER').typedef);
  532. paradef:=s32inttype;
  533. end;
  534. else
  535. internalerror(2011052101);
  536. end;
  537. end;
  538. end;
  539. end;
  540. end;
  541. floatdef:
  542. begin
  543. case tfloatdef(def).floattype of
  544. s32real:
  545. begin
  546. objdef:=tobjectdef(search_system_type('JLFLOAT').typedef);
  547. paradef:=s32floattype;
  548. end;
  549. s64real:
  550. begin
  551. objdef:=tobjectdef(search_system_type('JLDOUBLE').typedef);
  552. paradef:=s64floattype;
  553. end;
  554. else
  555. internalerror(2011052102);
  556. end;
  557. end;
  558. else
  559. internalerror(2011052103);
  560. end;
  561. end;
  562. function jvmgetunboxmethod(def: tdef): string;
  563. begin
  564. case def.typ of
  565. orddef:
  566. begin
  567. case torddef(def).ordtype of
  568. pasbool8:
  569. result:='BOOLEANVALUE';
  570. s8bit,
  571. u8bit,
  572. uchar,
  573. bool8bit:
  574. result:='BYTEVALUE';
  575. s16bit,
  576. u16bit,
  577. bool16bit,
  578. pasbool16:
  579. result:='SHORTVALUE';
  580. s32bit,
  581. u32bit,
  582. bool32bit,
  583. pasbool32:
  584. result:='INTVALUE';
  585. s64bit,
  586. u64bit,
  587. scurrency,
  588. bool64bit,
  589. pasbool64:
  590. result:='LONGVALUE';
  591. uwidechar:
  592. result:='CHARVALUE';
  593. else
  594. internalerror(2011071702);
  595. end;
  596. end;
  597. floatdef:
  598. begin
  599. case tfloatdef(def).floattype of
  600. s32real:
  601. result:='FLOATVALUE';
  602. s64real:
  603. result:='DOUBLEVALUE';
  604. else
  605. internalerror(2011071703);
  606. end;
  607. end;
  608. else
  609. internalerror(2011071704);
  610. end;
  611. end;
  612. function jvmgetcorrespondingclassdef(def: tdef): tdef;
  613. var
  614. paradef: tdef;
  615. begin
  616. if def.typ in [orddef,floatdef] then
  617. jvmgetboxtype(def,result,paradef,false)
  618. else
  619. begin
  620. case def.typ of
  621. stringdef :
  622. begin
  623. case tstringdef(def).stringtype of
  624. { translated into java.lang.String }
  625. st_widestring,
  626. st_unicodestring:
  627. result:=java_jlstring;
  628. st_ansistring:
  629. result:=java_ansistring;
  630. st_shortstring:
  631. result:=java_shortstring;
  632. else
  633. internalerror(2011072409);
  634. end;
  635. end;
  636. enumdef:
  637. begin
  638. result:=tenumdef(def).classdef;
  639. end;
  640. pointerdef :
  641. begin
  642. if def=voidpointertype then
  643. result:=java_jlobject
  644. else if jvmimplicitpointertype(tpointerdef(def).pointeddef) then
  645. result:=tpointerdef(def).pointeddef
  646. else
  647. internalerror(2011072410);
  648. end;
  649. recorddef :
  650. begin
  651. result:=def;
  652. end;
  653. variantdef :
  654. begin
  655. result:=cvarianttype;
  656. end;
  657. classrefdef :
  658. begin
  659. result:=search_system_type('JLCLASS').typedef;
  660. end;
  661. setdef :
  662. begin
  663. if tsetdef(def).elementdef.typ=enumdef then
  664. result:=java_juenumset
  665. else
  666. result:=java_jubitset;
  667. end;
  668. formaldef :
  669. begin
  670. result:=java_jlobject;
  671. end;
  672. arraydef :
  673. begin
  674. { cannot represent statically }
  675. internalerror(2011072411);
  676. end;
  677. procvardef :
  678. begin
  679. result:=tprocvardef(def).classdef;
  680. end;
  681. objectdef :
  682. case tobjectdef(def).objecttype of
  683. odt_javaclass,
  684. odt_interfacejava:
  685. result:=def
  686. else
  687. internalerror(2011072412);
  688. end;
  689. else
  690. internalerror(2011072413);
  691. end;
  692. end;
  693. end;
  694. function jvmmangledbasename(sym: tsym; const usesymname: TSymStr; withsignature: boolean): TSymStr;
  695. var
  696. container: tsymtable;
  697. vsym: tabstractvarsym;
  698. csym: tconstsym;
  699. begin
  700. case sym.typ of
  701. staticvarsym,
  702. paravarsym,
  703. localvarsym,
  704. fieldvarsym:
  705. begin
  706. vsym:=tabstractvarsym(sym);
  707. result:=jvmencodetype(vsym.vardef,false);
  708. if withsignature and
  709. jvmtypeneedssignature(vsym.vardef) then
  710. begin
  711. result:=result+' signature "';
  712. result:=result+jvmencodetype(vsym.vardef,true)+'"';
  713. end;
  714. if (vsym.typ=paravarsym) and
  715. (vo_is_self in tparavarsym(vsym).varoptions) then
  716. result:='''this'' ' +result
  717. else if (vsym.typ in [paravarsym,localvarsym]) and
  718. ([vo_is_funcret,vo_is_result] * tabstractnormalvarsym(vsym).varoptions <> []) then
  719. result:='''result'' '+result
  720. else
  721. begin
  722. { add array indirection if required }
  723. if (vsym.typ=paravarsym) and
  724. (vsym.vardef.typ=formaldef) or
  725. ((vsym.varspez in [vs_var,vs_out,vs_constref]) and
  726. not jvmimplicitpointertype(vsym.vardef)) then
  727. result:='['+result;
  728. { single quotes for definitions to prevent clashes with Java
  729. opcodes }
  730. if withsignature then
  731. result:=usesymname+''' '+result
  732. else
  733. result:=usesymname+' '+result;
  734. { we have to mangle staticvarsyms in localsymtables to
  735. prevent name clashes... }
  736. if (vsym.typ=staticvarsym) then
  737. begin
  738. container:=sym.Owner;
  739. while (container.symtabletype=localsymtable) do
  740. begin
  741. if tdef(container.defowner).typ<>procdef then
  742. internalerror(2011040303);
  743. { defid is added to prevent problem with overloads }
  744. result:=tprocdef(container.defowner).procsym.realname+'$$'+tostr(tprocdef(container.defowner).defid)+'$'+result;
  745. container:=container.defowner.owner;
  746. end;
  747. end;
  748. if withsignature then
  749. result:=''''+result
  750. end;
  751. end;
  752. constsym:
  753. begin
  754. csym:=tconstsym(sym);
  755. { some constants can be untyped }
  756. if assigned (csym.constdef) then
  757. begin
  758. result:=jvmencodetype(csym.constdef,false);
  759. if withsignature and
  760. jvmtypeneedssignature(csym.constdef) then
  761. begin
  762. result:=result+' signature "';
  763. result:=result+jvmencodetype(csym.constdef,true)+'"';
  764. end;
  765. end
  766. else
  767. begin
  768. case csym.consttyp of
  769. constord:
  770. result:=jvmencodetype(s32inttype,withsignature);
  771. constreal:
  772. result:=jvmencodetype(s64floattype,withsignature);
  773. constset:
  774. internalerror(2011040701);
  775. constpointer,
  776. constnil:
  777. result:=jvmencodetype(java_jlobject,withsignature);
  778. constwstring,
  779. conststring:
  780. result:=jvmencodetype(java_jlstring,withsignature);
  781. constresourcestring:
  782. internalerror(2011040702);
  783. else
  784. internalerror(2011040703);
  785. end;
  786. end;
  787. if withsignature then
  788. result:=''''+usesymname+''' '+result
  789. else
  790. result:=usesymname+' '+result
  791. end;
  792. else
  793. internalerror(2011021703);
  794. end;
  795. end;
  796. function jvmmangledbasename(sym: tsym; withsignature: boolean): TSymStr;
  797. begin
  798. if (sym.typ=fieldvarsym) and
  799. assigned(tfieldvarsym(sym).externalname) then
  800. result:=jvmmangledbasename(sym,tfieldvarsym(sym).externalname^,withsignature)
  801. else if (sym.typ=staticvarsym) and
  802. (tstaticvarsym(sym).mangledbasename<>'') then
  803. result:=jvmmangledbasename(sym,tstaticvarsym(sym).mangledbasename,withsignature)
  804. else
  805. result:=jvmmangledbasename(sym,sym.RealName,withsignature);
  806. end;
  807. {******************************************************************
  808. jvm type validity checking
  809. *******************************************************************}
  810. function jvmencodetype(def: tdef; withsignature: boolean): TSymStr;
  811. var
  812. errordef: tdef;
  813. begin
  814. if not jvmtryencodetype(def,result,withsignature,errordef) then
  815. internalerror(2011012305);
  816. end;
  817. function jvmchecktype(def: tdef; out founderror: tdef): boolean;
  818. var
  819. encodedtype: TSymStr;
  820. begin
  821. { don't duplicate the code like in objcdef, since the resulting strings
  822. are much shorter here so it's not worth it }
  823. result:=jvmtryencodetype(def,encodedtype,false,founderror);
  824. end;
  825. end.