pjvm.pas 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. 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. implementation
  30. uses
  31. globtype,
  32. cutils,cclasses,
  33. verbose,systems,
  34. fmodule,
  35. parabase,
  36. pdecsub,
  37. symbase,symtype,symtable,symconst,symsym,symcreat,defcmp,jvmdef,
  38. defutil,paramgr;
  39. { the JVM specs require that you add a default parameterless
  40. constructor in case the programmer hasn't specified any }
  41. procedure maybe_add_public_default_java_constructor(obj: tabstractrecorddef);
  42. var
  43. sym: tsym;
  44. ps: tprocsym;
  45. pd: tprocdef;
  46. topowner: tdefentry;
  47. i: longint;
  48. sstate: symcreat.tscannerstate;
  49. needclassconstructor: boolean;
  50. begin
  51. { if there is at least one constructor for a class, do nothing (for
  52. records, we'll always also need a parameterless constructor) }
  53. if not is_javaclass(obj) or
  54. not (oo_has_constructor in obj.objectoptions) then
  55. begin
  56. { check whether the parent has a parameterless constructor that we can
  57. call (in case of a class; all records will derive from
  58. java.lang.Object or a shim on top of that with a parameterless
  59. constructor) }
  60. if is_javaclass(obj) then
  61. begin
  62. pd:=nil;
  63. sym:=tsym(tobjectdef(obj).childof.symtable.find('CREATE'));
  64. if assigned(sym) and
  65. (sym.typ=procsym) then
  66. pd:=tprocsym(sym).find_bytype_parameterless(potype_constructor);
  67. if not assigned(pd) then
  68. begin
  69. Message(sym_e_no_matching_inherited_parameterless_constructor);
  70. exit
  71. end;
  72. end;
  73. { we call all constructors CREATE, because they don't have a name in
  74. Java and otherwise we can't determine whether multiple overloads
  75. are created with the same parameters }
  76. sym:=tsym(obj.symtable.find('CREATE'));
  77. if assigned(sym) then
  78. begin
  79. { does another, non-procsym, symbol already exist with that name? }
  80. if (sym.typ<>procsym) then
  81. begin
  82. Message1(sym_e_duplicate_id_create_java_constructor,sym.realname);
  83. exit;
  84. end;
  85. ps:=tprocsym(sym);
  86. { is there already a parameterless function/procedure create? }
  87. pd:=ps.find_bytype_parameterless(potype_function);
  88. if not assigned(pd) then
  89. pd:=ps.find_bytype_parameterless(potype_procedure);
  90. if assigned(pd) then
  91. begin
  92. Message1(sym_e_duplicate_id_create_java_constructor,pd.fullprocname(false));
  93. exit;
  94. end;
  95. end;
  96. if not assigned(sym) then
  97. begin
  98. ps:=tprocsym.create('Create');
  99. obj.symtable.insert(ps);
  100. end;
  101. { determine symtable level }
  102. topowner:=obj;
  103. while not(topowner.owner.symtabletype in [staticsymtable,globalsymtable,localsymtable]) do
  104. topowner:=topowner.owner.defowner;
  105. { create procdef }
  106. pd:=tprocdef.create(topowner.owner.symtablelevel+1);
  107. { method of this objectdef }
  108. pd.struct:=obj;
  109. { associated procsym }
  110. pd.procsym:=ps;
  111. { constructor }
  112. pd.proctypeoption:=potype_constructor;
  113. { needs to be exported }
  114. include(pd.procoptions,po_global);
  115. { for Delphi mode }
  116. include(pd.procoptions,po_overload);
  117. { generate anonymous inherited call in the implementation }
  118. pd.synthetickind:=tsk_anon_inherited;
  119. { public }
  120. pd.visibility:=vis_public;
  121. { result type }
  122. pd.returndef:=obj;
  123. { calling convention, self, ... }
  124. handle_calling_convention(pd);
  125. { register forward declaration with procsym }
  126. proc_add_definition(pd);
  127. end;
  128. { also add class constructor if class fields that need wrapping, and
  129. if none was defined }
  130. if obj.find_procdef_bytype(potype_class_constructor)=nil then
  131. begin
  132. needclassconstructor:=false;
  133. for i:=0 to obj.symtable.symlist.count-1 do
  134. begin
  135. if (tsym(obj.symtable.symlist[i]).typ=staticvarsym) and
  136. jvmimplicitpointertype(tstaticvarsym(obj.symtable.symlist[i]).vardef) then
  137. begin
  138. needclassconstructor:=true;
  139. break;
  140. end;
  141. end;
  142. if needclassconstructor then
  143. begin
  144. replace_scanner('custom_class_constructor',sstate);
  145. if str_parse_method_dec('constructor fpc_jvm_class_constructor;',potype_class_constructor,true,obj,pd) then
  146. pd.synthetickind:=tsk_empty
  147. else
  148. internalerror(2011040501);
  149. restore_scanner(sstate);
  150. end;
  151. end;
  152. end;
  153. procedure add_java_default_record_methods_intf(def: trecorddef);
  154. var
  155. sstate: tscannerstate;
  156. pd: tprocdef;
  157. begin
  158. maybe_add_public_default_java_constructor(def);
  159. replace_scanner('record_jvm_helpers',sstate);
  160. { no override, because not supported in records; the parser will still
  161. accept "inherited" though }
  162. if str_parse_method_dec('function clone: JLObject;',potype_function,false,def,pd) then
  163. pd.synthetickind:=tsk_jvm_clone
  164. else
  165. internalerror(2011032806);
  166. { can't use def.typesym, not yet set at this point }
  167. if def.symtable.realname^='' then
  168. internalerror(2011032803);
  169. if str_parse_method_dec('procedure fpcDeepCopy(out result:'+def.symtable.realname^+');',potype_procedure,false,def,pd) then
  170. pd.synthetickind:=tsk_record_deepcopy
  171. else
  172. internalerror(2011032807);
  173. restore_scanner(sstate);
  174. end;
  175. {******************************************************************
  176. jvm type validity checking
  177. *******************************************************************}
  178. function jvmencodetype(def: tdef): string;
  179. var
  180. errordef: tdef;
  181. begin
  182. if not jvmtryencodetype(def,result,errordef) then
  183. internalerror(2011012305);
  184. end;
  185. function jvmchecktype(def: tdef; out founderror: tdef): boolean;
  186. var
  187. encodedtype: string;
  188. begin
  189. { don't duplicate the code like in objcdef, since the resulting strings
  190. are much shorter here so it's not worth it }
  191. result:=jvmtryencodetype(def,encodedtype,founderror);
  192. end;
  193. end.