pjvm.pas 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. {
  2. Copyright (c) 2011 by Jonas Maebe
  3. This unit implements some JVM parser helper routines.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. {$i fpcdefs.inc}
  18. unit pjvm;
  19. interface
  20. uses
  21. symtype,symdef;
  22. { the JVM specs require that you add a default parameterless
  23. constructor in case the programmer hasn't specified any }
  24. procedure maybe_add_public_default_java_constructor(obj: tabstractrecorddef);
  25. { records are emulated via Java classes. They require a default constructor
  26. to initialise temps, a deep copy helper for assignments, and clone()
  27. to initialse dynamic arrays }
  28. procedure add_java_default_record_methods_intf(def: trecorddef);
  29. procedure jvm_guarantee_record_typesym(var def: tdef);
  30. implementation
  31. uses
  32. globtype,
  33. cutils,cclasses,
  34. verbose,systems,
  35. fmodule,
  36. parabase,
  37. pdecsub,
  38. symbase,symtable,symconst,symsym,symcreat,defcmp,jvmdef,
  39. defutil,paramgr;
  40. { the JVM specs require that you add a default parameterless
  41. constructor in case the programmer hasn't specified any }
  42. procedure maybe_add_public_default_java_constructor(obj: tabstractrecorddef);
  43. var
  44. sym: tsym;
  45. ps: tprocsym;
  46. pd: tprocdef;
  47. topowner: tdefentry;
  48. i: longint;
  49. sstate: symcreat.tscannerstate;
  50. needclassconstructor: boolean;
  51. begin
  52. { if there is at least one constructor for a class, do nothing (for
  53. records, we'll always also need a parameterless constructor) }
  54. if not is_javaclass(obj) or
  55. not (oo_has_constructor in obj.objectoptions) then
  56. begin
  57. { check whether the parent has a parameterless constructor that we can
  58. call (in case of a class; all records will derive from
  59. java.lang.Object or a shim on top of that with a parameterless
  60. constructor) }
  61. if is_javaclass(obj) then
  62. begin
  63. pd:=nil;
  64. sym:=tsym(tobjectdef(obj).childof.symtable.find('CREATE'));
  65. if assigned(sym) and
  66. (sym.typ=procsym) then
  67. pd:=tprocsym(sym).find_bytype_parameterless(potype_constructor);
  68. if not assigned(pd) then
  69. begin
  70. Message(sym_e_no_matching_inherited_parameterless_constructor);
  71. exit
  72. end;
  73. end;
  74. { we call all constructors CREATE, because they don't have a name in
  75. Java and otherwise we can't determine whether multiple overloads
  76. are created with the same parameters }
  77. sym:=tsym(obj.symtable.find('CREATE'));
  78. if assigned(sym) then
  79. begin
  80. { does another, non-procsym, symbol already exist with that name? }
  81. if (sym.typ<>procsym) then
  82. begin
  83. Message1(sym_e_duplicate_id_create_java_constructor,sym.realname);
  84. exit;
  85. end;
  86. ps:=tprocsym(sym);
  87. { is there already a parameterless function/procedure create? }
  88. pd:=ps.find_bytype_parameterless(potype_function);
  89. if not assigned(pd) then
  90. pd:=ps.find_bytype_parameterless(potype_procedure);
  91. if assigned(pd) then
  92. begin
  93. Message1(sym_e_duplicate_id_create_java_constructor,pd.fullprocname(false));
  94. exit;
  95. end;
  96. end;
  97. if not assigned(sym) then
  98. begin
  99. ps:=tprocsym.create('Create');
  100. obj.symtable.insert(ps);
  101. end;
  102. { determine symtable level }
  103. topowner:=obj;
  104. while not(topowner.owner.symtabletype in [staticsymtable,globalsymtable,localsymtable]) do
  105. topowner:=topowner.owner.defowner;
  106. { create procdef }
  107. pd:=tprocdef.create(topowner.owner.symtablelevel+1);
  108. { method of this objectdef }
  109. pd.struct:=obj;
  110. { associated procsym }
  111. pd.procsym:=ps;
  112. { constructor }
  113. pd.proctypeoption:=potype_constructor;
  114. { needs to be exported }
  115. include(pd.procoptions,po_global);
  116. { for Delphi mode }
  117. include(pd.procoptions,po_overload);
  118. { generate anonymous inherited call in the implementation }
  119. pd.synthetickind:=tsk_anon_inherited;
  120. { public }
  121. pd.visibility:=vis_public;
  122. { result type }
  123. pd.returndef:=obj;
  124. { calling convention, self, ... }
  125. handle_calling_convention(pd);
  126. { register forward declaration with procsym }
  127. proc_add_definition(pd);
  128. end;
  129. { also add class constructor if class fields that need wrapping, and
  130. if none was defined }
  131. if obj.find_procdef_bytype(potype_class_constructor)=nil then
  132. begin
  133. needclassconstructor:=false;
  134. for i:=0 to obj.symtable.symlist.count-1 do
  135. begin
  136. if (tsym(obj.symtable.symlist[i]).typ=staticvarsym) and
  137. jvmimplicitpointertype(tstaticvarsym(obj.symtable.symlist[i]).vardef) then
  138. begin
  139. needclassconstructor:=true;
  140. break;
  141. end;
  142. end;
  143. if needclassconstructor then
  144. begin
  145. replace_scanner('custom_class_constructor',sstate);
  146. if str_parse_method_dec('constructor fpc_jvm_class_constructor;',potype_class_constructor,true,obj,pd) then
  147. pd.synthetickind:=tsk_empty
  148. else
  149. internalerror(2011040501);
  150. restore_scanner(sstate);
  151. end;
  152. end;
  153. end;
  154. procedure add_java_default_record_methods_intf(def: trecorddef);
  155. var
  156. sstate: tscannerstate;
  157. pd: tprocdef;
  158. begin
  159. maybe_add_public_default_java_constructor(def);
  160. replace_scanner('record_jvm_helpers',sstate);
  161. { no override, because not supported in records; the parser will still
  162. accept "inherited" though }
  163. if str_parse_method_dec('function clone: JLObject;',potype_function,false,def,pd) then
  164. pd.synthetickind:=tsk_jvm_clone
  165. else
  166. internalerror(2011032806);
  167. { can't use def.typesym, not yet set at this point }
  168. if not assigned(def.symtable.realname) then
  169. internalerror(2011032803);
  170. if str_parse_method_dec('procedure fpcDeepCopy(out result:'+def.symtable.realname^+');',potype_procedure,false,def,pd) then
  171. pd.synthetickind:=tsk_record_deepcopy
  172. else
  173. internalerror(2011032807);
  174. restore_scanner(sstate);
  175. end;
  176. procedure jvm_guarantee_record_typesym(var def: tdef);
  177. var
  178. ts: ttypesym;
  179. begin
  180. { create a dummy typesym for the JVM target, because the record
  181. has to be wrapped by a class }
  182. if (target_info.system=system_jvm_java32) and
  183. (def.typ=recorddef) and
  184. not assigned(def.typesym) then
  185. begin
  186. ts:=ttypesym.create(trecorddef(def).symtable.realname^,def);
  187. symtablestack.top.insert(ts);
  188. ts.visibility:=vis_strictprivate;
  189. def.typesym:=ts;
  190. end;
  191. end;
  192. {******************************************************************
  193. jvm type validity checking
  194. *******************************************************************}
  195. function jvmencodetype(def: tdef): string;
  196. var
  197. errordef: tdef;
  198. begin
  199. if not jvmtryencodetype(def,result,errordef) then
  200. internalerror(2011012305);
  201. end;
  202. function jvmchecktype(def: tdef; out founderror: tdef): boolean;
  203. var
  204. encodedtype: string;
  205. begin
  206. { don't duplicate the code like in objcdef, since the resulting strings
  207. are much shorter here so it's not worth it }
  208. result:=jvmtryencodetype(def,encodedtype,founderror);
  209. end;
  210. end.