From a7801c18fac9b20ee47d09ed15380a83fabeeec9 Mon Sep 17 00:00:00 2001
From: Maximilian Downey Twiss <creatorsmithmdt@gmail.com>
Date: Fri, 18 Nov 2022 09:34:30 +1100
Subject: [PATCH 20/56] Re-add Java handling to gcc and gcc/cp.

gcc/cp/ChangeLog:

	* call.cc (build_java_interface_fn_ref): Re-add.
	(build_over_call): Handle TYPE_JAVA_INTERFACE.
	(java_iface_lookup_fn): Re-add.
	* class.cc (add_method): Handle TYPE_FOR_JAVA.
	(add_implicitly_declared_members): Likewise.
	(determine_key_method): Likewise.
	(finish_struct_1): Likewise.
	(push_lang_context): Handle lang_name_java.
	* cp-lang.cc (cp_eh_personality): Handle pragma_java_exceptions.
	* cp-tree.h (enum cp_tree_index): Re-add CPTI_JAVA_*, CPTI_LANG_NAME_JAVA and CPTI_JCLASS.
	(java_byte_type_node): Re-add.
	(java_short_type_node): Likewise.
	(java_int_type_node): Likewise.
	(java_long_type_node): Likewise.
	(java_float_type_node): Likewise.
	(java_double_type_node): Likewise.
	(java_char_type_node): Likewise.
	(java_boolean_type_node): Likewise.
	(lang_name_java): Likewise.
	(jclass_node): Likewise.
	(cp_global_trees): Re-add TYPE_FOR_JAVA.
	(enum languages): Re-add lang_java.
	(TYPE_FOR_JAVA): Re-add.
	(struct lang_type): Re-add java_interface.
	(TYPE_JAVA_INTERFACE): Re-add.
	(struct lang_decl_base): Increase language by 1.
	(check_java_method): Re-add.
	(build_java_class_ref): Likewise.
	(pragma_java_exceptions): Likewise.
	* decl.cc (record_builtin_java_type): Re-add.
	(initialize_predefined_identifiers): Re-add Java.
	(cxx_init_decl_processing): Re-add java_*_type_node.
	(cp_finish_decl): Handle TYPE_FOR_JAVA.
	(grokfndecl): Likewise.
	(check_special_function_return_type): Likewise.
	(grokdeclarator): Set TYPE_FOR_JAVA.
	(grokparms): Handle TYPE_FOR_JAVA.
	(xref_basetypes): Likewise.
	(check_function_type): Likewise.
	(maybe_return_this): Likewise.
	* decl2.cc (acceptable_java_type): Re-add.
	(check_java_method): Likewise.
	(import_export_decl): Handle TYPE_FOR_JAVA.
	(build_java_method_aliases): Re-add.
	(c_parse_final_cleanups): Call build_java_method_aliases.
	(possibly_inlined_p): Test pragma_java_exceptions.
	* error.cc (language_to_string): Handle lang_java.
	* except.cc (decl_is_java_type): Re-add.
	(eh_type_info): Re-add java type handling.
	(choose_personality_routine): Re-add.
	(initialize_handler_parm): Call choose_personality_routine.
	(expand_start_catch_block): Handle java types.
	(build_throw): Likewise.
	* init.cc (build_new_1): Handle TYPE_FOR_JAVA.
	(build_java_class_ref): Likewise.
	* lex.cc (handle_pragma_java_exceptions): Re-add.
	(init_cp_pragma): Register GCC java_exceptions pragma.
	(set_decl_linkage): Handle lang_name_java.
	* mangle.cc (write_java_integer_type_codes): Re-add.
	(write_builtin_type): Handle TYPE_FOR_JAVA.
	(write_bare_function_type): Handle java_*_type_node.
	* method.cc (implicitly_declare_fn): Handle TYPE_FOR_JAVA.
	* name-lookup.cc (pushtag): Set TYPE_FOR_JAVA.
	* pt.cc (maybe_new_partial_specialization): Copy TYPE_FOR_JAVA.
	(lookup_template_class): Likewise.
	(instantiate_class_template): Likewise.
	* tree.cc (handle_java_interface_attribute): Re-add.
	* typeck.cc (structural_comptypes): Compare TYPE_FOR_JAVA.

gcc/ChangeLog:

	* dwarf2out.cc (is_java): Re-add.
	(output_pubname): Handle is_java.
	(lower_bound_default): Handle DW_LANG_Java.
	(gen_compile_unit_die): Likewise.
	* gimplify.cc (mostly_copy_tree_r): Re-add Java-special part.
	* ipa-free-lang-data.cc (free_lang_data_in_type): Handle Java's usage of TYPE_MIN_VALUE.
	(find_decls_types_r): Handle Java's usage of TYPE_BINFO.
	* tree.cc (verify_type): Handle Java's usage of TYPE_MIN_VALUE, TYPE_BINFO, and TYPE_STRING_FLAG.
---
 gcc/cp/call.cc            |  66 ++++++++++++++++-
 gcc/cp/class.cc           |  50 ++++++++++++-
 gcc/cp/cp-lang.cc         |   5 +-
 gcc/cp/cp-tree.h          |  45 ++++++++++-
 gcc/cp/decl.cc            | 124 +++++++++++++++++++++++++++++--
 gcc/cp/decl2.cc           | 122 +++++++++++++++++++++++++++++-
 gcc/cp/error.cc           |   3 +
 gcc/cp/except.cc          | 152 +++++++++++++++++++++++++++++++++++++-
 gcc/cp/init.cc            | 128 +++++++++++++++++++++++++++++---
 gcc/cp/lex.cc             |  18 +++++
 gcc/cp/mangle.cc          |  62 +++++++++++++++-
 gcc/cp/method.cc          |   2 +-
 gcc/cp/name-lookup.cc     |   3 +
 gcc/cp/pt.cc              |   3 +
 gcc/cp/tree.cc            |  28 +++++++
 gcc/cp/typeck.cc          |   2 +
 gcc/dwarf2out.cc          |  17 ++++-
 gcc/gimplify.cc           |   7 +-
 gcc/ipa-free-lang-data.cc |  16 +++-
 gcc/tree.cc               |  30 +++++++-
 20 files changed, 839 insertions(+), 44 deletions(-)

diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
index 459e86b5f09..32ead577737 100644
--- a/gcc/cp/call.cc
+++ b/gcc/cp/call.cc
@@ -167,6 +167,7 @@ static int compare_ics (conversion *, conversion *);
 static void maybe_warn_class_memaccess (location_t, tree,
 					const vec<tree, va_gc> *);
 static tree build_over_call (struct z_candidate *, int, tsubst_flags_t);
+static tree build_java_interface_fn_ref (tree, tree);
 static tree convert_like (conversion *, tree, tsubst_flags_t);
 static tree convert_like_with_context (conversion *, tree, tree, int,
 				       tsubst_flags_t);
@@ -10242,7 +10243,10 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
       if (TREE_SIDE_EFFECTS (argarray[0]))
 	argarray[0] = save_expr (argarray[0]);
       t = build_pointer_type (TREE_TYPE (fn));
-      fn = build_vfn_ref (argarray[0], DECL_VINDEX (fn));
+      if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
+	fn = build_java_interface_fn_ref (fn, argarray[0]);
+      else
+	fn = build_vfn_ref (argarray[0], DECL_VINDEX (fn));
       TREE_TYPE (fn) = t;
     }
   else
@@ -10819,6 +10823,66 @@ build_cxx_call (tree fn, int nargs, tree *argarray,
   return convert_from_reference (fn);
 }
 
+static GTY(()) tree java_iface_lookup_fn;
+
+/* Make an expression which yields the address of the Java interface
+   method FN.  This is achieved by generating a call to libjava's
+   _Jv_LookupInterfaceMethodIdx().  */
+
+static tree
+build_java_interface_fn_ref (tree fn, tree instance)
+{
+  tree lookup_fn, method, idx;
+  tree klass_ref, iface, iface_ref;
+  int i;
+
+  if (!java_iface_lookup_fn)
+    {
+      tree ftype = build_function_type_list (ptr_type_node,
+					     ptr_type_node, ptr_type_node,
+					     java_int_type_node, NULL_TREE);
+      java_iface_lookup_fn
+	= add_builtin_function ("_Jv_LookupInterfaceMethodIdx", ftype,
+				0, NOT_BUILT_IN, NULL, NULL_TREE);
+    }
+
+  /* Look up the pointer to the runtime java.lang.Class object for `instance'.
+     This is the first entry in the vtable.  */
+  klass_ref = build_vtbl_ref (cp_build_fold_indirect_ref (instance),
+			      integer_zero_node);
+
+  /* Get the java.lang.Class pointer for the interface being called.  */
+  iface = DECL_CONTEXT (fn);
+  iface_ref = lookup_field (iface, get_identifier ("class$"), 0, false);
+  if (!iface_ref || !VAR_P (iface_ref)
+      || DECL_CONTEXT (iface_ref) != iface)
+    {
+      error ("could not find class$ field in java interface type %qT",
+		iface);
+      return error_mark_node;
+    }
+  iface_ref = build_address (iface_ref);
+  iface_ref = convert (build_pointer_type (iface), iface_ref);
+
+  /* Determine the itable index of FN.  */
+  i = 1;
+  for (method = TYPE_FIELDS (iface); method; method = DECL_CHAIN (method))
+    {
+      if (!DECL_VIRTUAL_P (method))
+	continue;
+      if (fn == method)
+	break;
+      i++;
+    }
+  idx = build_int_cst (NULL_TREE, i);
+
+  lookup_fn = build1 (ADDR_EXPR,
+		      build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
+		      java_iface_lookup_fn);
+  return build_call_nary (ptr_type_node, lookup_fn,
+			  3, klass_ref, iface_ref, idx);
+}
+
 /* Returns the value to use for the in-charge parameter when making a
    call to a function with the indicated NAME.
 
diff --git a/gcc/cp/class.cc b/gcc/cp/class.cc
index aebcb53739e..2dafb54b8c1 100644
--- a/gcc/cp/class.cc
+++ b/gcc/cp/class.cc
@@ -1247,6 +1247,16 @@ add_method (tree type, tree method, bool via_using)
   /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc.  */
   grok_special_member_properties (method);
 
+  if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method) && (TYPE_FOR_JAVA (type)))
+    {
+      if (!DECL_ARTIFICIAL (method))
+        error ("Java class %qT cannot have a destructor", type);
+      else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
+        error ("Java class %qT cannot have an implicit non-trivial "
+               "destructor",
+               type);
+    }
+
   *slot = current_fns;
 
   return true;
@@ -3306,9 +3316,18 @@ add_implicitly_declared_members (tree t, tree* access_decls,
 {
   /* Destructor.  */
   if (!CLASSTYPE_DESTRUCTOR (t))
+    {
     /* In general, we create destructors lazily.  */
     CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
 
+      if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
+	  && TYPE_FOR_JAVA (t))
+	/* But if this is a Java class, any non-trivial destructor is
+	   invalid, even if compiler-generated.  Therefore, if the
+	   destructor is non-trivial we create it now.  */
+	lazily_declare_fn (sfk_destructor, t);
+    }
+
   bool move_ok = false;
   if (cxx_dialect >= cxx11 && CLASSTYPE_LAZY_DESTRUCTOR (t)
       && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
@@ -3336,7 +3355,7 @@ add_implicitly_declared_members (tree t, tree* access_decls,
 
      If a class definition does not explicitly declare a copy
      constructor, one is declared implicitly.  */
-  if (! TYPE_HAS_COPY_CTOR (t))
+  if (! TYPE_HAS_COPY_CTOR (t) && ! TYPE_FOR_JAVA (t))
     {
       TYPE_HAS_COPY_CTOR (t) = 1;
       TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
@@ -3349,7 +3368,7 @@ add_implicitly_declared_members (tree t, tree* access_decls,
      when it is needed.  For now, just record whether or not the type
      of the parameter to the assignment operator will be a const or
      non-const reference.  */
-  if (!TYPE_HAS_COPY_ASSIGN (t))
+  if (!TYPE_HAS_COPY_ASSIGN (t) && !TYPE_FOR_JAVA (t))
     {
       TYPE_HAS_COPY_ASSIGN (t) = 1;
       TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
@@ -7031,7 +7050,8 @@ determine_key_method (tree type)
 {
   tree method;
 
-  if (processing_template_decl
+  if (TYPE_FOR_JAVA (type)
+      || processing_template_decl
       || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
       || CLASSTYPE_INTERFACE_KNOWN (type))
     return;
@@ -7619,7 +7639,9 @@ finish_struct_1 (tree t)
   /* Build the VTT for T.  */
   build_vtt (t);
 
-  if (warn_nonvdtor
+  /* This warning does not make sense for Java classes, since they
+     cannot have destructors.  */
+  if (!TYPE_FOR_JAVA (t) && warn_nonvdtor
       && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
       && !CLASSTYPE_FINAL (t))
     warning (OPT_Wnon_virtual_dtor,
@@ -8330,9 +8352,29 @@ push_lang_context (tree name)
   vec_safe_push (current_lang_base, current_lang_name);
 
   if (name == lang_name_cplusplus)
+    {
     current_lang_name = name;
+    }
+  else if (name == lang_name_java)
+    {
+      current_lang_name = name;
+      /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
+	 (See record_builtin_java_type in decl.cc.)  However, that causes
+	 incorrect debug entries if these types are actually used.
+	 So we re-enable debug output after extern "Java".  */
+      DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
+      DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
+      DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
+      DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
+      DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
+      DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
+      DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
+      DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
+    }
   else if (name == lang_name_c)
+    {
     current_lang_name = name;
+    }
   else
     error ("language string %<\"%E\"%> not recognized", name);
 }
diff --git a/gcc/cp/cp-lang.cc b/gcc/cp/cp-lang.cc
index a3f29eda0d6..af66fe5727f 100644
--- a/gcc/cp/cp-lang.cc
+++ b/gcc/cp/cp-lang.cc
@@ -152,7 +152,10 @@ static tree
 cp_eh_personality (void)
 {
   if (!cp_eh_personality_decl)
-    cp_eh_personality_decl = build_personality_function ("gxx");
+    {
+      const char *lang = (pragma_java_exceptions ? "gcj" : "gxx");
+      cp_eh_personality_decl = build_personality_function (lang);
+    }
 
   return cp_eh_personality_decl;
 }
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index 548b533266a..62543051359 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -130,6 +130,15 @@ operator == (const cp_expr &lhs, tree rhs)
 
 enum cp_tree_index
 {
+    CPTI_JAVA_BYTE_TYPE,
+    CPTI_JAVA_SHORT_TYPE,
+    CPTI_JAVA_INT_TYPE,
+    CPTI_JAVA_LONG_TYPE,
+    CPTI_JAVA_FLOAT_TYPE,
+    CPTI_JAVA_DOUBLE_TYPE,
+    CPTI_JAVA_CHAR_TYPE,
+    CPTI_JAVA_BOOLEAN_TYPE,
+
     CPTI_WCHAR_DECL,
     CPTI_VTABLE_ENTRY_TYPE,
     CPTI_DELTA_TYPE,
@@ -195,10 +204,12 @@ enum cp_tree_index
 
     CPTI_LANG_NAME_C,
     CPTI_LANG_NAME_CPLUSPLUS,
+    CPTI_LANG_NAME_JAVA,
 
     CPTI_EMPTY_EXCEPT_SPEC,
     CPTI_NOEXCEPT_TRUE_SPEC,
     CPTI_NOEXCEPT_FALSE_SPEC,
+    CPTI_JCLASS,
     CPTI_NOEXCEPT_DEFERRED_SPEC,
 
     CPTI_ANY_TARG,
@@ -246,6 +257,15 @@ enum cp_tree_index
 
 extern GTY(()) tree cp_global_trees[CPTI_MAX];
 
+#define java_byte_type_node            cp_global_trees[CPTI_JAVA_BYTE_TYPE]
+#define java_short_type_node           cp_global_trees[CPTI_JAVA_SHORT_TYPE]
+#define java_int_type_node             cp_global_trees[CPTI_JAVA_INT_TYPE]
+#define java_long_type_node            cp_global_trees[CPTI_JAVA_LONG_TYPE]
+#define java_float_type_node           cp_global_trees[CPTI_JAVA_FLOAT_TYPE]
+#define java_double_type_node          cp_global_trees[CPTI_JAVA_DOUBLE_TYPE]
+#define java_char_type_node            cp_global_trees[CPTI_JAVA_CHAR_TYPE]
+#define java_boolean_type_node         cp_global_trees[CPTI_JAVA_BOOLEAN_TYPE]
+
 #define wchar_decl_node			cp_global_trees[CPTI_WCHAR_DECL]
 #define vtable_entry_type		cp_global_trees[CPTI_VTABLE_ENTRY_TYPE]
 /* The type used to represent an offset by which to adjust the `this'
@@ -352,6 +372,7 @@ extern GTY(()) tree cp_global_trees[CPTI_MAX];
 #define omp_identifier			cp_global_trees[CPTI_OMP_IDENTIFIER]
 #define lang_name_c			cp_global_trees[CPTI_LANG_NAME_C]
 #define lang_name_cplusplus		cp_global_trees[CPTI_LANG_NAME_CPLUSPLUS]
+#define lang_name_java                 cp_global_trees[CPTI_LANG_NAME_JAVA]
 
 /* Exception specifiers used for throw(), noexcept(true),
    noexcept(false) and deferred noexcept.  We rely on these being
@@ -361,6 +382,9 @@ extern GTY(()) tree cp_global_trees[CPTI_MAX];
 #define noexcept_false_spec		cp_global_trees[CPTI_NOEXCEPT_FALSE_SPEC]
 #define noexcept_deferred_spec		cp_global_trees[CPTI_NOEXCEPT_DEFERRED_SPEC]
 
+/* If non-NULL, a POINTER_TYPE equivalent to (java::lang::Class*).  */
+#define jclass_node                    cp_global_trees[CPTI_JCLASS]
+
 /* Exception handling function declarations.  */
 #define terminate_fn			cp_global_trees[CPTI_TERMINATE_FN]
 #define call_unexpected_fn		cp_global_trees[CPTI_CALL_UNEXPECTED_FN]
@@ -537,6 +561,7 @@ extern GTY(()) tree cp_global_trees[CPTI_MAX];
    1: TYPE_HAS_USER_CONSTRUCTOR.
    2: TYPE_HAS_LATE_RETURN_TYPE (in FUNCTION_TYPE, METHOD_TYPE)
       TYPE_PTRMEMFUNC_FLAG (in RECORD_TYPE)
+   3: TYPE_FOR_JAVA.
    4: TYPE_HAS_NONTRIVIAL_DESTRUCTOR
    5: CLASS_TYPE_P (in RECORD_TYPE and UNION_TYPE)
       ENUM_FIXED_UNDERLYING_TYPE_P (in ENUMERAL_TYPE)
@@ -2219,7 +2244,7 @@ extern bool statement_code_p[MAX_TREE_CODES];
 
 #define STATEMENT_CODE_P(CODE) statement_code_p[(int) (CODE)]
 
-enum languages { lang_c, lang_cplusplus };
+enum languages { lang_c, lang_cplusplus, lang_java };
 
 /* Macros to make error reporting functions' lives easier.  */
 #define TYPE_LINKAGE_IDENTIFIER(NODE) \
@@ -2277,6 +2302,9 @@ enum languages { lang_c, lang_cplusplus };
 #define OVERLOAD_TYPE_P(T) \
   (CLASS_TYPE_P (T) || TREE_CODE (T) == ENUMERAL_TYPE)
 
+/* True if this a "Java" type, defined in 'extern "Java"'.  */
+#define TYPE_FOR_JAVA(NODE) TYPE_LANG_FLAG_3 (NODE)
+
 /* True if this type is dependent.  This predicate is only valid if
    TYPE_DEPENDENT_P_VALID is true.  */
 #define TYPE_DEPENDENT_P(NODE) TYPE_LANG_FLAG_0 (NODE)
@@ -2381,6 +2409,7 @@ struct GTY(()) lang_type {
   unsigned diamond_shaped : 1;
   unsigned repeated_base : 1;
   unsigned being_defined : 1;
+  unsigned java_interface : 1;
   unsigned debug_requested : 1;
   unsigned fields_readonly : 1;
   unsigned ptrmemfunc_flag : 1;
@@ -2416,7 +2445,7 @@ struct GTY(()) lang_type {
   /* There are some bits left to fill out a 32-bit word.  Keep track
      of this by updating the size of this bitfield whenever you add or
      remove a flag.  */
-  unsigned dummy : 3;
+  unsigned dummy : 2;
 
   tree primary_base;
   vec<tree_pair_s, va_gc> *vcall_indices;
@@ -2614,6 +2643,11 @@ struct GTY(()) lang_type {
 #define CLASSTYPE_ALIGN_UNIT(NODE) \
   (CLASSTYPE_ALIGN (NODE) / BITS_PER_UNIT)
 
+/* True if this a Java interface type, declared with
+   '__attribute__ ((java_interface))'.  */
+#define TYPE_JAVA_INTERFACE(NODE) \
+  (LANG_TYPE_CLASS_CHECK (NODE)->java_interface)
+
 /* A vec<tree> of virtual functions which cannot be inherited by
    derived classes.  When deriving from this type, the derived
    class must provide its own definition for each of these functions.  */
@@ -2860,7 +2894,7 @@ enum lang_decl_selector
 
 struct GTY(()) lang_decl_base {
   ENUM_BITFIELD(lang_decl_selector) selector : 3;
-  ENUM_BITFIELD(languages) language : 1;
+  ENUM_BITFIELD(languages) language : 2;
   unsigned use_template : 2;
   unsigned not_really_extern : 1;	   /* var or fn */
   unsigned initialized_in_class : 1;	   /* var or fn */
@@ -6556,6 +6590,9 @@ class_of_this_parm (const_tree fntype)
    e.g  "int f(void)".  */
 extern cp_parameter_declarator *no_parameters;
 
+/* True if we saw "#pragma GCC java_exceptions".  */
+extern bool pragma_java_exceptions;
+
 /* Various dump ids.  */
 extern int class_dump_id;
 extern int module_dump_id;
@@ -6950,6 +6987,7 @@ extern tree do_aggregate_paren_init		(tree, tree);
 extern void record_mangling			(tree, bool);
 extern void overwrite_mangling			(tree, tree);
 extern void note_mangling_alias			(tree, tree);
+extern bool check_java_method                  (tree);
 extern void generate_mangling_aliases		(void);
 extern tree build_memfn_type			(tree, tree, cp_cv_quals, cp_ref_qualifier);
 extern tree build_pointer_ptrmemfn_type	(tree);
@@ -7120,6 +7158,7 @@ extern tree build_vec_delete			(location_t, tree, tree,
 						 tsubst_flags_t);
 extern tree create_temporary_var		(tree);
 extern void initialize_vtbl_ptrs		(tree);
+extern tree build_java_class_ref               (tree);
 extern tree scalar_constant_value		(tree);
 extern tree decl_constant_value			(tree, bool);
 extern tree decl_really_constant_value		(tree, bool = true);
diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 06740d420d9..c91e65d1808 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -78,6 +78,7 @@ static void check_static_variable_definition (tree, tree);
 static void record_unknown_type (tree, const char *);
 static int member_function_or_else (tree, tree, enum overload_flags);
 static tree local_variable_p_walkfn (tree *, int *, void *);
+static tree record_builtin_java_type (const char *, int);
 static const char *tag_name (enum tag_types);
 static tree lookup_and_check_tag (enum tag_types, tree, TAG_how, bool);
 static void maybe_deduce_size_from_array_init (tree, tree);
@@ -4520,6 +4521,53 @@ record_builtin_type (enum rid rid_index,
     }
 }
 
+/* Record one of the standard Java types.
+ * Declare it as having the given NAME.
+ * If SIZE > 0, it is the size of one of the integral types;
+ * otherwise it is the negative of the size of one of the other types.  */
+
+static tree
+record_builtin_java_type (const char* name, int size)
+{
+  tree type, decl;
+  if (size > 0)
+    {
+      type = build_nonstandard_integer_type (size, 0);
+      type = build_distinct_type_copy (type);
+    }
+  else if (size > -32)
+    {
+      tree stype;
+      /* "__java_char" or ""__java_boolean".  */
+      type = build_nonstandard_integer_type (-size, 1);
+      type = build_distinct_type_copy (type);
+      /* Get the signed type cached and attached to the unsigned type,
+	so it doesn't get garbage-collected at "random" times,
+	causing potential codegen differences out of different UIDs
+	and different alias set numbers.  */
+      stype = build_nonstandard_integer_type (-size, 0);
+      stype = build_distinct_type_copy (stype);
+      TREE_CHAIN (type) = stype;
+      /*if (size == -1)        TREE_SET_CODE (type, BOOLEAN_TYPE);*/
+    }
+  else
+    { /* "__java_float" or ""__java_double".  */
+      type = make_node (REAL_TYPE);
+      TYPE_PRECISION (type) = - size;
+      layout_type (type);
+    }
+  record_builtin_type (RID_MAX, name, type);
+  decl = TYPE_NAME (type);
+
+  /* Suppress generate debug symbol entries for these types,
+     since for normal C++ they are just clutter.
+     However, push_lang_context undoes this if extern "Java" is seen.  */
+  DECL_IGNORED_P (decl) = 1;
+
+  TYPE_FOR_JAVA (type) = 1;
+  return type;
+}
+
 /* Push a type into the namespace so that the back ends ignore it.  */
 
 static void
@@ -4552,6 +4600,7 @@ initialize_predefined_identifiers (void)
   static const predefined_identifier predefined_identifiers[] = {
     {"C++", &lang_name_cplusplus, cik_normal},
     {"C", &lang_name_c, cik_normal},
+    { "Java", &lang_name_java, cik_normal},
     /* Some of these names have a trailing space so that it is
        impossible for them to conflict with names written by users.  */
     {"__ct ", &ctor_identifier, cik_ctor},
@@ -4665,6 +4714,15 @@ cxx_init_decl_processing (void)
 
   c_common_nodes_and_builtins ();
 
+  java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
+  java_short_type_node = record_builtin_java_type ("__java_short", 16);
+  java_int_type_node = record_builtin_java_type ("__java_int", 32);
+  java_long_type_node = record_builtin_java_type ("__java_long", 64);
+  java_float_type_node = record_builtin_java_type ("__java_float", -32);
+  java_double_type_node = record_builtin_java_type ("__java_double", -64);
+  java_char_type_node = record_builtin_java_type ("__java_char", -16);
+  java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
+
   tree bool_ftype = build_function_type_list (boolean_type_node, NULL_TREE);
   tree decl
     = add_builtin_function ("__builtin_is_constant_evaluated",
@@ -8591,6 +8649,19 @@ cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
 	     is *not* defined.  */
 	  && (!DECL_EXTERNAL (decl) || init))
 	{
+	if (TYPE_FOR_JAVA (type) && MAYBE_CLASS_TYPE_P (type))
+	  {
+	    tree jclass = get_global_binding (get_identifier ("jclass"));
+	    /* Allow libjava/prims.cc define primitive classes.  */
+	    if (init != NULL_TREE
+		|| jclass == NULL_TREE
+		|| TREE_CODE (jclass) != TYPE_DECL
+		|| !POINTER_TYPE_P (TREE_TYPE (jclass))
+		|| !same_type_ignoring_top_level_qualifiers_p
+		(type, TREE_TYPE (TREE_TYPE (jclass))))
+	      error ("Java object %qD not allocated with %<new%>", decl);
+	    init = NULL_TREE;
+	  }
 	  cleanups = make_tree_vector ();
 	  init = check_initializer (decl, init, flags, &cleanups);
 
@@ -8634,6 +8705,9 @@ cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
 	   so that we can decide later to emit debug info for them.  */
 	record_types_used_by_current_var_decl (decl);
     }
+  else if (TREE_CODE (decl) == FIELD_DECL
+	  && TYPE_FOR_JAVA (type) && MAYBE_CLASS_TYPE_P (type))
+    error ("non-static data member %qD has Java class type", decl);
 
   /* Add this declaration to the statement-tree.  This needs to happen
      after the call to check_initializer so that the DECL_EXPR for a
@@ -10682,7 +10756,9 @@ grokfndecl (tree ctype,
 	check_main_parameter_types (decl);
     }
 
-  if (ctype != NULL_TREE && check)
+  if (ctype != NULL_TREE
+      && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
+      && check)
     {
       tree old_decl = check_classfn (ctype, decl,
 				     (current_template_depth
@@ -11612,7 +11688,7 @@ check_special_function_return_type (special_function_kind sfk,
 	error_at (smallest_type_quals_location (type_quals, locations),
 		  "qualifiers are not allowed on constructor declaration");
 
-      if (targetm.cxx.cdtor_returns_this ())
+      if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (optype))
 	type = build_pointer_type (optype);
       else
 	type = void_type_node;
@@ -11627,8 +11703,10 @@ check_special_function_return_type (special_function_kind sfk,
 		  "qualifiers are not allowed on destructor declaration");
 
       /* We can't use the proper return type here because we run into
-	 problems with ambiguous bases and covariant returns.  */
-      if (targetm.cxx.cdtor_returns_this ())
+	 problems with ambiguous bases and covariant returns.
+	 Java classes are left unchanged because (void *) isn't a valid
+	 Java type, and we don't want to change the Java ABI.  */
+      if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (optype))
 	type = build_pointer_type (void_type_node);
       else
 	type = void_type_node;
@@ -13769,6 +13847,11 @@ grokdeclarator (const cp_declarator *declarator,
 	  return error_mark_node;
 	}
 
+      /* Note that the grammar rejects storage classes
+	in typenames, fields or parameters.  */
+      if (current_lang_name == lang_name_java)
+       TYPE_FOR_JAVA (type) = 1;
+
       /* This declaration:
 
 	   typedef void f(int) const;
@@ -15060,6 +15143,16 @@ grokparms (tree parmlist, tree *parms)
 	  TREE_TYPE (decl) = error_mark_node;
 	}
 
+      if (type != error_mark_node
+	 && TYPE_FOR_JAVA (type)
+	 && MAYBE_CLASS_TYPE_P (type))
+       {
+	 error ("parameter %qD has Java class type", decl);
+	 type = error_mark_node;
+	 TREE_TYPE (decl) = error_mark_node;
+	 init = NULL_TREE;
+       }
+
       if (type != error_mark_node)
 	{
 	  if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
@@ -16225,8 +16318,13 @@ xref_basetypes (tree ref, tree base_list)
     }
 
   if (max_bases > 1)
+    {
+      if (TYPE_FOR_JAVA (ref))
+       error ("Java class %qT cannot have multiple bases", ref);
+      else
     warning (OPT_Wmultiple_inheritance,
 	     "%qT defined with multiple direct bases", ref);
+    }
 
   if (max_vbases)
     {
@@ -16235,7 +16333,9 @@ xref_basetypes (tree ref, tree base_list)
 
       vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
 
-      if (max_dvbases)
+      if (TYPE_FOR_JAVA (ref))
+       error ("Java class %qT cannot have virtual bases", ref);
+      else if (max_dvbases)
 	warning (OPT_Wvirtual_inheritance,
 		 "%qT defined with direct virtual base", ref);
     }
@@ -16267,6 +16367,9 @@ xref_basetypes (tree ref, tree base_list)
 	  goto dropped_base;
 	}
 
+      if (TYPE_FOR_JAVA (basetype) && (current_lang_depth () == 0))
+       TYPE_FOR_JAVA (ref) = 1;
+
       base_binfo = NULL_TREE;
       if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
 	{
@@ -17127,11 +17230,15 @@ check_function_type (tree decl, tree current_function_parms)
   if (dependent_type_p (return_type)
       || type_uses_auto (return_type))
     return;
-  if (!COMPLETE_OR_VOID_TYPE_P (return_type))
+  if (!COMPLETE_OR_VOID_TYPE_P (return_type)
+      || (TYPE_FOR_JAVA (return_type) && MAYBE_CLASS_TYPE_P (return_type)))
     {
       tree args = TYPE_ARG_TYPES (fntype);
 
-      error ("return type %q#T is incomplete", return_type);
+      if (!COMPLETE_OR_VOID_TYPE_P (return_type))
+	error ("return type %q#T is incomplete", return_type);
+      else
+	error ("return type has Java class type %q#T", return_type);
 
       /* Make it return void instead.  */
       if (TREE_CODE (fntype) == METHOD_TYPE)
@@ -17747,7 +17854,8 @@ store_parm_decls (tree current_function_parms)
 void
 maybe_return_this (void)
 {
-  if (targetm.cxx.cdtor_returns_this ())
+  if (targetm.cxx.cdtor_returns_this ()
+    && (! TYPE_FOR_JAVA (current_class_type)))
     {
       /* Return the address of the object.  */
       tree val = DECL_ARGUMENTS (current_function_decl);
diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc
index f95529a5c9a..c3a00a59e51 100644
--- a/gcc/cp/decl2.cc
+++ b/gcc/cp/decl2.cc
@@ -56,6 +56,7 @@ int raw_dump_id;
  
 extern cpp_reader *parse_in;
 
+static bool acceptable_java_type (tree);
 static tree start_objects (bool, unsigned, bool);
 static tree finish_objects (bool, unsigned, tree, bool = true);
 static tree start_partial_init_fini_fn (bool, unsigned, unsigned);
@@ -705,6 +706,80 @@ check_member_template (tree tmpl)
     error ("template declaration of %q#D", decl);
 }
 
+/* Return true iff TYPE is a valid Java parameter or return type.  */
+
+static bool
+acceptable_java_type (tree type)
+{
+  if (type == error_mark_node)
+    return false;
+
+  if (VOID_TYPE_P (type) || TYPE_FOR_JAVA (type))
+    return true;
+  if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
+    {
+      type = TREE_TYPE (type);
+      if (TREE_CODE (type) == RECORD_TYPE)
+       {
+	 tree args;  int i;
+	 if (! TYPE_FOR_JAVA (type))
+	   return false;
+	 if (! CLASSTYPE_TEMPLATE_INFO (type))
+	   return true;
+	 args = CLASSTYPE_TI_ARGS (type);
+	 i = TREE_VEC_LENGTH (args);
+	 while (--i >= 0)
+	   {
+	     type = TREE_VEC_ELT (args, i);
+	     if (TYPE_PTR_P (type))
+	       type = TREE_TYPE (type);
+	     if (! TYPE_FOR_JAVA (type))
+	       return false;
+	   }
+	 return true;
+       }
+    }
+  return false;
+}
+
+/* For a METHOD in a Java class CTYPE, return true if
+   the parameter and return types are valid Java types.
+   Otherwise, print appropriate error messages, and return false.  */
+
+bool
+check_java_method (tree method)
+{
+  bool jerr = false;
+  tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
+  tree ret_type = TREE_TYPE (TREE_TYPE (method));
+
+  if (!acceptable_java_type (ret_type))
+    {
+      error ("Java method %qD has non-Java return type %qT",
+	    method, ret_type);
+      jerr = true;
+    }
+
+  arg_types = TREE_CHAIN (arg_types);
+  if (DECL_HAS_IN_CHARGE_PARM_P (method))
+    arg_types = TREE_CHAIN (arg_types);
+  if (DECL_HAS_VTT_PARM_P (method))
+    arg_types = TREE_CHAIN (arg_types);
+
+  for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
+    {
+      tree type = TREE_VALUE (arg_types);
+      if (!acceptable_java_type (type))
+       {
+	  if (type != error_mark_node)
+	   error ("Java method %qD has non-Java parameter type %qT",
+		  method, type);
+	 jerr = true;
+       }
+    }
+  return !jerr;
+}
+
 /* Sanity check: report error if this function FUNCTION is not
    really a member of the class (CTYPE) it is supposed to belong to.
    TEMPLATE_PARMS is used to specify the template parameters of a member
@@ -3248,8 +3323,10 @@ import_export_decl (tree decl)
     {
       class_type = DECL_CONTEXT (decl);
       import_export_class (class_type);
-      if (CLASSTYPE_INTERFACE_KNOWN (class_type)
-	  && CLASSTYPE_INTERFACE_ONLY (class_type))
+      if (TYPE_FOR_JAVA (class_type))
+        import_p = true;
+      else if (CLASSTYPE_INTERFACE_KNOWN (class_type)
+		&& CLASSTYPE_INTERFACE_ONLY (class_type))
 	import_p = true;
       else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
 	       && !CLASSTYPE_USE_TEMPLATE (class_type)
@@ -4352,6 +4429,42 @@ generate_ctor_or_dtor_function (bool initp, unsigned priority,
   expand_or_defer_fn (finish_objects (initp, priority, body, fns != NULL_TREE));
 }
 
+/* Java requires that we be able to reference a local address for a
+   method, and not be confused by PLT entries.  If supported, create a
+   hidden alias for all such methods.  */
+
+static void
+build_java_method_aliases (void)
+{
+#ifndef HAVE_GAS_HIDDEN
+  return;
+#endif
+
+  struct cgraph_node *node;
+  FOR_EACH_FUNCTION (node)
+    {
+      tree fndecl = node->decl;
+
+      if (DECL_CLASS_SCOPE_P (fndecl)
+	 && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl))
+	 && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl))
+       {
+	 /* Mangle the name in a predictable way; we need to reference
+	    this from a java compiled object file.  */
+	 tree oid = DECL_ASSEMBLER_NAME (fndecl);
+	 const char *oname = IDENTIFIER_POINTER (oid);
+	 gcc_assert (oname[0] == '_' && oname[1] == 'Z');
+	 char *nname = ACONCAT (("_ZGA", oname + 2, NULL));
+
+	 tree alias = make_alias_for (fndecl, get_identifier (nname));
+	 TREE_PUBLIC (alias) = 1;
+	 DECL_VISIBILITY (alias) = VISIBILITY_HIDDEN;
+
+	 cgraph_node::create_same_body_alias (alias, fndecl);
+       }
+    }
+}
+
 /* Return C++ property of T, based on given operation OP.  */
 
 static int
@@ -5242,6 +5355,9 @@ c_parse_final_cleanups (void)
   /* Generate any missing aliases.  */
   maybe_apply_pending_pragma_weaks ();
 
+  /* Generate Java hidden aliases.  */
+  build_java_method_aliases ();
+
   if (flag_vtable_verify)
     {
       vtv_recover_class_info ();
@@ -5420,7 +5536,7 @@ possibly_inlined_p (tree decl)
   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
   if (DECL_UNINLINABLE (decl))
     return false;
-  if (!optimize)
+  if (!optimize || pragma_java_exceptions)
     return DECL_DECLARED_INLINE_P (decl);
   /* When optimizing, we might inline everything when flatten
      attribute or heuristics inlining for size or autoinlining
diff --git a/gcc/cp/error.cc b/gcc/cp/error.cc
index 12b28e8ee5b..efc07f1ed90 100644
--- a/gcc/cp/error.cc
+++ b/gcc/cp/error.cc
@@ -3339,6 +3339,9 @@ language_to_string (enum languages c)
     case lang_cplusplus:
       return "C++";
 
+    case lang_java:
+      return "Java";
+
     default:
       gcc_unreachable ();
     }
diff --git a/gcc/cp/except.cc b/gcc/cp/except.cc
index 2677a9b7678..cb07d6d1783 100644
--- a/gcc/cp/except.cc
+++ b/gcc/cp/except.cc
@@ -36,6 +36,7 @@ static tree prepare_eh_type (tree);
 static tree do_begin_catch (void);
 static int dtor_nothrow (tree);
 static tree do_end_catch (tree);
+static bool decl_is_java_type (tree decl, int err);
 static void initialize_handler_parm (tree, tree);
 static tree do_allocate_exception (tree);
 static tree wrap_cleanups_r (tree *, int *, void *);
@@ -103,10 +104,17 @@ prepare_eh_type (tree type)
 tree
 eh_type_info (tree type)
 {
+  tree exp;
+
   if (type == NULL_TREE || type == error_mark_node)
     return type;
 
-  return get_tinfo_decl (type);
+  if (decl_is_java_type (type, 0))
+    exp = build_java_class_ref (TREE_TYPE (type));
+  else
+    exp = get_tinfo_decl (type);
+
+  return exp;
 }
 
 /* Build the address of a typeinfo decl for use in the runtime
@@ -270,6 +278,107 @@ push_eh_cleanup (tree type)
   finish_decl_cleanup (NULL_TREE, do_end_catch (type));
 }
 
+/* Return nonzero value if DECL is a Java type suitable for catch or
+   throw.  */
+
+static bool
+decl_is_java_type (tree decl, int err)
+{
+  bool r = (TYPE_PTR_P (decl)
+	    && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
+	    && TYPE_FOR_JAVA (TREE_TYPE (decl)));
+
+  if (err)
+    {
+      if (TREE_CODE (decl) == REFERENCE_TYPE
+	  && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
+	  && TYPE_FOR_JAVA (TREE_TYPE (decl)))
+	{
+	  /* Can't throw a reference.  */
+	  error ("type %qT is disallowed in Java %<throw%> or %<catch%>",
+		 decl);
+	}
+
+      if (r)
+	{
+	  tree jthrow_node
+	    = get_global_binding (get_identifier ("jthrowable"));
+
+	  if (jthrow_node == NULL_TREE)
+	    fatal_error
+	      (input_location,
+	       "call to Java %<catch%> or %<throw%> with %<jthrowable%> undefined");
+
+	  jthrow_node = TREE_TYPE (TREE_TYPE (jthrow_node));
+
+	  if (! DERIVED_FROM_P (jthrow_node, TREE_TYPE (decl)))
+	    {
+	      /* Thrown object must be a Throwable.  */
+	      error ("type %qT is not derived from %<java::lang::Throwable%>",
+		     TREE_TYPE (decl));
+	    }
+	}
+    }
+
+  return r;
+}
+
+/* Select the personality routine to be used for exception handling,
+   or issue an error if we need two different ones in the same
+   translation unit.
+   ??? At present DECL_FUNCTION_PERSONALITY is set via
+   LANG_HOOKS_EH_PERSONALITY.  Should it be done here instead?  */
+void
+choose_personality_routine (enum languages lang)
+{
+  static enum {
+    chose_none,
+    chose_cpp,
+    chose_java,
+    gave_error
+  } state;
+
+  switch (state)
+    {
+    case gave_error:
+      return;
+
+    case chose_cpp:
+      if (lang != lang_cplusplus)
+	goto give_error;
+      return;
+
+    case chose_java:
+      if (lang != lang_java)
+	goto give_error;
+      return;
+
+    case chose_none:
+      ; /* Proceed to language selection.  */
+    }
+
+  switch (lang)
+    {
+    case lang_cplusplus:
+      state = chose_cpp;
+      break;
+
+    case lang_java:
+      state = chose_java;
+      terminate_fn = builtin_decl_explicit (BUILT_IN_ABORT);
+      pragma_java_exceptions = true;
+      break;
+
+    default:
+      gcc_unreachable ();
+    }
+  return;
+
+ give_error:
+  error ("mixing C++ and Java catches in a single translation unit");
+  state = gave_error;
+}
+
 /* Wrap EXPR in a MUST_NOT_THROW_EXPR expressing that EXPR must
    not throw any exceptions if COND is true.  A condition of
    NULL_TREE is treated as 'true'.  */
@@ -325,6 +434,9 @@ initialize_handler_parm (tree decl, tree exp)
   if (!INDIRECT_TYPE_P (init_type))
     init_type = build_reference_type (init_type);
 
+  choose_personality_routine (decl_is_java_type (init_type, 0)
+			     ? lang_java : lang_cplusplus);
+
   /* Since pointers are passed by value, initialize a reference to
      pointer catch parm with the address of the temporary.  */
   if (TYPE_REF_P (init_type)
@@ -404,6 +516,22 @@ expand_start_catch_block (tree decl)
   else
     type = NULL_TREE;
 
+  if (decl && decl_is_java_type (type, 1))
+    {
+      /* Java only passes object via pointer and doesn't require
+	adjusting.  The java object is immediately before the
+	generic exception header.  */
+      exp = build_exc_ptr ();
+      exp = build1 (NOP_EXPR, build_pointer_type (type), exp);
+      exp = fold_build_pointer_plus (exp,
+		   fold_build1_loc (input_location,
+				    NEGATE_EXPR, sizetype,
+				    TYPE_SIZE_UNIT (TREE_TYPE (exp))));
+      exp = cp_build_fold_indirect_ref (exp);
+      initialize_handler_parm (decl, exp);
+      return type;
+    }
+
   /* Call __cxa_end_catch at the end of processing the exception.  */
   push_eh_cleanup (type);
 
@@ -631,7 +759,27 @@ build_throw (location_t loc, tree exp)
   if (! doing_eh ())
     return error_mark_node;
 
-  if (exp)
+  if (exp && decl_is_java_type (TREE_TYPE (exp), 1))
+    {
+      tree name = get_identifier ("_Jv_Throw");
+      tree fn = get_global_binding (name);
+      if (!fn)
+       {
+	 /* Declare void _Jv_Throw (void *).  */
+	 tree tmp;
+	 tmp = build_function_type_list (ptr_type_node,
+					 ptr_type_node, NULL_TREE);
+	 fn = push_throw_library_fn (name, tmp);
+       }
+      else if (really_overloaded_fn (fn))
+       {
+	 error ("%qD should never be overloaded", fn);
+	 return error_mark_node;
+       }
+      exp = cp_build_function_call_nary (fn, tf_warning_or_error,
+					exp, NULL_TREE);
+    }
+  else if (exp)
     {
       tree throw_type;
       tree temp_type;
diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc
index 2fff4ad2dc7..1885f2058b7 100644
--- a/gcc/cp/init.cc
+++ b/gcc/cp/init.cc
@@ -3037,6 +3037,7 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
   tree alloc_fn;
   tree cookie_expr, init_expr;
   int nothrow, check_new;
+  int use_java_new = 0;
   /* If non-NULL, the number of extra bytes to allocate at the
      beginning of the storage allocated for an array-new expression in
      order to store the number of elements.  */
@@ -3307,7 +3308,62 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
 
   bool member_new_p = false;
 
+  bool member_delete_p = (!globally_qualified_p
+			  && CLASS_TYPE_P (elt_type)
+			  && (array_p
+			      ? TYPE_GETS_VEC_DELETE (elt_type)
+			      : TYPE_GETS_REG_DELETE (elt_type)));
+
   /* Allocate the object.  */
+  if (vec_safe_is_empty (*placement) && TYPE_FOR_JAVA (elt_type))
+    {
+      tree class_addr;
+      tree class_decl;
+      static const char alloc_name[] = "_Jv_AllocObject";
+
+      if (!MAYBE_CLASS_TYPE_P (elt_type))
+	{
+	  error ("%qT isn%'t a valid Java class type", elt_type);
+	  return error_mark_node;
+	}
+
+      class_decl = build_java_class_ref (elt_type);
+      if (class_decl == error_mark_node)
+	return error_mark_node;
+
+      use_java_new = 1;
+
+      if (!(alloc_fn = get_global_binding (get_identifier (alloc_name))))
+	{
+	  if (complain & tf_error)
+	    error ("call to Java constructor with %qs undefined", alloc_name);
+	  return error_mark_node;
+	}
+      else if (really_overloaded_fn (alloc_fn))
+	{
+	  if (complain & tf_error)
+	    error ("%qD should never be overloaded", alloc_fn);
+	  return error_mark_node;
+	}
+      if (TREE_CODE (alloc_fn) != FUNCTION_DECL
+	  || TREE_CODE (TREE_TYPE (alloc_fn)) != FUNCTION_TYPE
+	  || !POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (alloc_fn))))
+	{
+	  if (complain & tf_error)
+	    error ("%qD is not a function returning a pointer", alloc_fn);
+	  return error_mark_node;
+	}
+      class_addr = build1 (ADDR_EXPR, jclass_node, class_decl);
+      alloc_call = cp_build_function_call_nary (alloc_fn, complain,
+						class_addr, NULL_TREE);
+    }
+  else if (TYPE_FOR_JAVA (elt_type) && MAYBE_CLASS_TYPE_P (elt_type))
+    {
+      error ("Java class %q#T object allocated using placement new", elt_type);
+      return error_mark_node;
+    }
+  else
+    {
   tree fnname;
   tree fns;
 
@@ -3319,12 +3375,6 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
 		     ? TYPE_HAS_ARRAY_NEW_OPERATOR (elt_type)
 		     : TYPE_HAS_NEW_OPERATOR (elt_type));
 
-  bool member_delete_p = (!globally_qualified_p
-			  && CLASS_TYPE_P (elt_type)
-			  && (array_p
-			      ? TYPE_GETS_VEC_DELETE (elt_type)
-			      : TYPE_GETS_REG_DELETE (elt_type)));
-
   if (member_new_p)
     {
       /* Use a class-specific operator new.  */
@@ -3409,8 +3459,11 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
 
       alloc_call = build_operator_new_call (fnname, placement,
 					    &size, &cookie_size,
-					    align_arg, outer_nelts_check,
+						align_arg,
+						outer_nelts_check,
 					    &alloc_fn, complain);
+
+	    }
     }
 
   if (alloc_call == error_mark_node)
@@ -3523,8 +3576,9 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
      So check for a null exception spec on the op new we just called.  */
 
   nothrow = TYPE_NOTHROW_P (TREE_TYPE (alloc_fn));
-  check_new
-    = flag_check_new || (nothrow && !std_placement_new_fn_p (alloc_fn));
+  check_new = (flag_check_new
+	      || (nothrow && !std_placement_new_fn_p (alloc_fn)))
+	      && ! use_java_new;
 
   if (cookie_size)
     {
@@ -3728,7 +3782,7 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
      unambiguous matching deallocation function can be found,
      propagating the exception does not cause the object's memory to be
      freed.  */
-  if (flag_exceptions && (init_expr || member_delete_p))
+  if (flag_exceptions && (init_expr || member_delete_p) && ! use_java_new)
     {
       enum tree_code dcode = array_p ? VEC_DELETE_EXPR : DELETE_EXPR;
       tree cleanup;
@@ -4028,6 +4082,60 @@ build_new (location_t loc, vec<tree, va_gc> **placement, tree type,
 
   return rval;
 }
+
+/* Given a Java class, return a decl for the corresponding java.lang.Class.  */
+
+tree
+build_java_class_ref (tree type)
+{
+  tree name = NULL_TREE, class_decl;
+  static tree CL_suffix = NULL_TREE;
+  if (CL_suffix == NULL_TREE)
+    CL_suffix = get_identifier("class$");
+  if (jclass_node == NULL_TREE)
+    {
+      jclass_node = get_global_binding (get_identifier ("jclass"));
+      if (jclass_node == NULL_TREE)
+	{
+	  error ("call to Java constructor, while %<jclass%> undefined");
+	  return error_mark_node;
+	}
+      jclass_node = TREE_TYPE (jclass_node);
+    }
+
+  /* Mangle the class$ field.  */
+  {
+    tree field;
+    for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
+      if (DECL_NAME (field) == CL_suffix)
+	{
+	  mangle_decl (field);
+	  name = DECL_ASSEMBLER_NAME (field);
+	  break;
+	}
+    if (!field)
+      {
+	error ("cannot find %<class$%> in %qT", type);
+	return error_mark_node;
+      }
+  }
+
+  class_decl = get_global_binding (name);
+  if (class_decl == NULL_TREE)
+    {
+      class_decl = build_decl (input_location,
+			       VAR_DECL, name, TREE_TYPE (jclass_node));
+      TREE_STATIC (class_decl) = 1;
+      DECL_EXTERNAL (class_decl) = 1;
+      TREE_PUBLIC (class_decl) = 1;
+      DECL_ARTIFICIAL (class_decl) = 1;
+      DECL_IGNORED_P (class_decl) = 1;
+      pushdecl_top_level (class_decl);
+      make_decl_rtl (class_decl);
+    }
+  return class_decl;
+}
+
 
 static tree
 build_vec_delete_1 (location_t loc, tree base, tree maxindex, tree type,
diff --git a/gcc/cp/lex.cc b/gcc/cp/lex.cc
index 22d1ab92add..d9ab437b411 100644
--- a/gcc/cp/lex.cc
+++ b/gcc/cp/lex.cc
@@ -42,6 +42,7 @@ static void handle_pragma_vtable (cpp_reader *);
 static void handle_pragma_unit (cpp_reader *);
 static void handle_pragma_interface (cpp_reader *);
 static void handle_pragma_implementation (cpp_reader *);
+static void handle_pragma_java_exceptions (cpp_reader *);
 
 static void init_operators (void);
 static void copy_lang_type (tree);
@@ -75,6 +76,9 @@ struct impl_files
 };
 
 static struct impl_files *impl_file_chain;
+
+/* True if we saw "#pragma GCC java_exceptions".  */
+bool pragma_java_exceptions;
 
 void
 cxx_finish (void)
@@ -292,6 +296,7 @@ init_cp_pragma (void)
   c_register_pragma (0, "implementation", handle_pragma_implementation);
   c_register_pragma ("GCC", "interface", handle_pragma_interface);
   c_register_pragma ("GCC", "implementation", handle_pragma_implementation);
+  c_register_pragma ("GCC", "java_exceptions", handle_pragma_java_exceptions);
 }
 
 /* TRUE if a code represents a statement.  */
@@ -704,6 +709,17 @@ handle_pragma_implementation (cpp_reader* /*dfile*/)
       impl_file_chain = ifiles;
     }
 }
+/* Indicate that this file uses Java-personality exception handling.  */
+static void
+handle_pragma_java_exceptions (cpp_reader* /*dfile*/)
+{
+  tree x;
+  if (pragma_lex (&x) != CPP_EOF)
+    warning (0, "junk at end of %<#pragma GCC%> %<java_exceptions%>");
+
+  choose_personality_routine (lang_java);
+  pragma_java_exceptions = true;
+}
 
 /* Issue an error message indicating that the lookup of NAME (an
    IDENTIFIER_NODE) failed.  Returns the ERROR_MARK_NODE.  */
@@ -934,6 +950,8 @@ set_decl_linkage (tree t)
     SET_DECL_LANGUAGE (t, lang_cplusplus);
   else if (current_lang_name == lang_name_c)
     SET_DECL_LANGUAGE (t, lang_c);
+  else if (current_lang_name == lang_name_java)
+    SET_DECL_LANGUAGE (t, lang_java);
   else
     gcc_unreachable ();
 }
diff --git a/gcc/cp/mangle.cc b/gcc/cp/mangle.cc
index e363ef35b9f..bf043609cd1 100644
--- a/gcc/cp/mangle.cc
+++ b/gcc/cp/mangle.cc
@@ -245,6 +245,10 @@ static bool equal_abi_tags (tree, tree);
 static inline void start_mangling (const tree);
 static tree mangle_special_for_type (const tree, const char *);
 
+/* Foreign language functions.  */
+
+static void write_java_integer_type_codes (const tree);
+
 /* Append a single character to the end of the mangled
    representation.  */
 #define write_char(CHAR)						\
@@ -2671,6 +2675,8 @@ write_builtin_type (tree type)
 	write_string ("Ds");
       else if (type == char32_type_node)
 	write_string ("Di");
+      else if (TYPE_FOR_JAVA (type))
+	write_java_integer_type_codes (type);
       else
 	{
 	  size_t itk;
@@ -2725,9 +2731,9 @@ write_builtin_type (tree type)
       break;
 
     case REAL_TYPE:
-      if (type == float_type_node)
+      if (type == float_type_node || type == java_float_type_node)
 	write_char ('f');
-      else if (type == double_type_node)
+      else if (type == double_type_node || type == java_double_type_node)
 	write_char ('d');
       else if (type == long_double_type_node)
 	write_char ('e');
@@ -2815,16 +2821,40 @@ write_function_type (const tree type)
 /* Non-terminal <bare-function-type>.  TYPE is a FUNCTION_TYPE or
    METHOD_TYPE.  If INCLUDE_RETURN_TYPE is nonzero, the return value
    is mangled before the parameter types.  If non-NULL, DECL is
-   FUNCTION_DECL for the function whose type is being emitted.  */
+   FUNCTION_DECL for the function whose type is being emitted.
+
+   If DECL is a member of a Java type, then a literal 'J'
+   is output and the return type is mangled as if INCLUDE_RETURN_TYPE
+   were nonzero.
+
+     <bare-function-type> ::= [J]</signature/ type>+  */
 
 static void
 write_bare_function_type (const tree type, const int include_return_type_p,
 			  const tree decl)
 {
+  int java_method_p;
+
   MANGLE_TRACE_TREE ("bare-function-type", type);
 
+  /* Detect Java methods and emit special encoding.  */
+  if (decl != NULL
+      && DECL_FUNCTION_MEMBER_P (decl)
+      && TYPE_FOR_JAVA (DECL_CONTEXT (decl))
+      && !DECL_CONSTRUCTOR_P (decl)
+      && !DECL_DESTRUCTOR_P (decl)
+      && !DECL_CONV_FN_P (decl))
+    {
+      java_method_p = 1;
+      write_char ('J');
+    }
+  else
+    {
+      java_method_p = 0;
+    }
+
   /* Mangle the return type, if requested.  */
-  if (include_return_type_p)
+  if (include_return_type_p || java_method_p)
     write_type (TREE_TYPE (type));
 
   /* Now mangle the types of the arguments.  */
@@ -4608,6 +4638,30 @@ mangle_template_parm_object (tree expr)
   return finish_mangling_get_identifier ();
 }
 
+
+/* Foreign language type mangling section.  */
+
+/* How to write the type codes for the integer Java type.  */
+
+static void
+write_java_integer_type_codes (const tree type)
+{
+  if (type == java_int_type_node)
+    write_char ('i');
+  else if (type == java_short_type_node)
+    write_char ('s');
+  else if (type == java_byte_type_node)
+    write_char ('c');
+  else if (type == java_char_type_node)
+    write_char ('w');
+  else if (type == java_long_type_node)
+    write_char ('x');
+  else if (type == java_boolean_type_node)
+    write_char ('b');
+  else
+    gcc_unreachable ();
+}
+
 /* Given a CLASS_TYPE, such as a record for std::bad_exception this
    function generates a mangled name for the vtable map variable of
    the class type.  For example, if the class type is
diff --git a/gcc/cp/method.cc b/gcc/cp/method.cc
index 1e962b6e3b1..5ddc5079e58 100644
--- a/gcc/cp/method.cc
+++ b/gcc/cp/method.cc
@@ -3091,7 +3091,7 @@ implicitly_declare_fn (special_function_kind kind, tree type,
 
   type = TYPE_MAIN_VARIANT (type);
 
-  if (targetm.cxx.cdtor_returns_this ())
+  if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (type))
     {
       if (kind == sfk_destructor)
 	/* See comment in check_special_function_return_type.  */
diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc
index b5f6e84cad9..bbfdbe384d7 100644
--- a/gcc/cp/name-lookup.cc
+++ b/gcc/cp/name-lookup.cc
@@ -7906,6 +7906,9 @@ pushtag (tree name, tree type, TAG_how how)
       if (!context)
 	context = current_namespace;
 
+      if (current_lang_name == lang_name_java)
+	TYPE_FOR_JAVA (type) = 1;
+
       tdef = create_implicit_typedef (name, type);
       DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
       set_originating_module (tdef);
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index fbf498ad16a..7f46ca2bd7d 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -957,6 +957,7 @@ maybe_new_partial_specialization (tree& type)
 	 for the newly declared specialization.  */
       tree t = make_class_type (TREE_CODE (type));
       CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
+      TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (type);
       SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
 
       /* We only need a separate type node for storing the definition of this
@@ -10059,6 +10060,7 @@ lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
 	  CLASSTYPE_DECLARED_CLASS (t)
 	    = CLASSTYPE_DECLARED_CLASS (template_type);
 	  SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
+	  TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
 
 	  /* A local class.  Make sure the decl gets registered properly.  */
 	  if (context == current_function_decl)
@@ -12062,6 +12064,7 @@ instantiate_class_template (tree type)
   TYPE_PACKED (type) = TYPE_PACKED (pattern);
   SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
   TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
+  TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
   CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
   if (ANON_AGGR_TYPE_P (pattern))
     SET_ANON_AGGR_TYPE_P (type);
diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc
index 04a055d9d77..282f3dde124 100644
--- a/gcc/cp/tree.cc
+++ b/gcc/cp/tree.cc
@@ -44,6 +44,7 @@ static tree build_target_expr (tree, tree, tsubst_flags_t);
 static tree count_trees_r (tree *, int *, void *);
 static tree verify_stmt_tree_r (tree *, int *, void *);
 
+static tree handle_java_interface_attribute (tree *, tree, tree, int, bool *);
 static tree handle_init_priority_attribute (tree *, tree, tree, int, bool *);
 static tree handle_abi_tag_attribute (tree *, tree, tree, int, bool *);
 static tree handle_contract_attribute (tree *, tree, tree, int, bool *);
@@ -5088,6 +5089,8 @@ const struct attribute_spec cxx_attribute_table[] =
 {
   /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
        affects_type_identity, handler, exclude } */
+  { "java_interface", 0, 0, false, false, false, false,
+    handle_java_interface_attribute, NULL },
   { "init_priority",  1, 1, true,  false, false, false,
     handle_init_priority_attribute, NULL },
   { "abi_tag", 1, -1, false, false, false, true,
@@ -5121,6 +5124,31 @@ const struct attribute_spec std_attribute_table[] =
   { NULL, 0, 0, false, false, false, false, NULL, NULL }
 };
 
+/* Handle a "java_interface" attribute; arguments as in
+   struct attribute_spec.handler.  */
+static tree
+handle_java_interface_attribute (tree* node,
+				tree name,
+				tree /*args*/,
+				int flags,
+				bool* no_add_attrs)
+{
+  if (DECL_P (*node)
+      || !CLASS_TYPE_P (*node)
+      || !TYPE_FOR_JAVA (*node))
+    {
+      error ("%qE attribute can only be applied to Java class definitions",
+	    name);
+      *no_add_attrs = true;
+      return NULL_TREE;
+    }
+  if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
+    *node = build_variant_type_copy (*node);
+  TYPE_JAVA_INTERFACE (*node) = 1;
+
+  return NULL_TREE;
+}
+
 /* Handle an "init_priority" attribute; arguments as in
    struct attribute_spec.handler.  */
 static tree
diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc
index d5757b27f49..093817d8489 100644
--- a/gcc/cp/typeck.cc
+++ b/gcc/cp/typeck.cc
@@ -1458,6 +1458,8 @@ structural_comptypes (tree t1, tree t2, int strict)
 				 ce_type))
 	return false;
     }
+  if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
+    return false;
 
   /* Allow for two different type nodes which have essentially the same
      definition.  Note that we already checked for equality of the type
diff --git a/gcc/dwarf2out.cc b/gcc/dwarf2out.cc
index e81044b8c48..b3a346a7b5a 100644
--- a/gcc/dwarf2out.cc
+++ b/gcc/dwarf2out.cc
@@ -5571,6 +5571,16 @@ is_cxx (const_tree decl)
   return is_cxx ();
 }
 
+/* Return TRUE if the language is Java.  */
+
+static inline bool
+is_java (void)
+{
+  unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
+
+  return lang == DW_LANG_Java;
+}
+
 /* Return TRUE if the language is Fortran.  */
 
 static inline bool
@@ -11629,7 +11639,7 @@ output_pubname (dw_offset die_offset, pubname_entry *entry)
         case DW_TAG_enumerator:
           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
-	  if (!is_cxx ())
+	  if (!is_cxx () && !is_java ())
 	    GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
           break;
         case DW_TAG_subprogram:
@@ -11658,7 +11668,7 @@ output_pubname (dw_offset die_offset, pubname_entry *entry)
         case DW_TAG_union_type:
         case DW_TAG_enumeration_type:
           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
-	  if (!is_cxx ())
+	  if (!is_cxx () && !is_java ())
 	    GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
           break;
         default:
@@ -21505,6 +21515,7 @@ lower_bound_default (void)
     case DW_LANG_C_plus_plus_14:
     case DW_LANG_ObjC:
     case DW_LANG_ObjC_plus_plus:
+    case DW_LANG_Java:
       return 0;
     case DW_LANG_Fortran77:
     case DW_LANG_Fortran90:
@@ -25221,6 +25232,8 @@ gen_compile_unit_die (const char *filename)
 		language = DW_LANG_Fortran08;
 	    }
 	}
+      else if (strcmp (language_string, "GNU Java") == 0)
+	language = DW_LANG_Java;
       else if (strcmp (language_string, "GNU Objective-C") == 0)
 	language = DW_LANG_ObjC;
       else if (strcmp (language_string, "GNU Objective-C++") == 0)
diff --git a/gcc/gimplify.cc b/gcc/gimplify.cc
index eb562c3a313..845038151c3 100644
--- a/gcc/gimplify.cc
+++ b/gcc/gimplify.cc
@@ -892,7 +892,12 @@ mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
   /* Stop at types, decls, constants like copy_tree_r.  */
   else if (TREE_CODE_CLASS (code) == tcc_type
 	   || TREE_CODE_CLASS (code) == tcc_declaration
-	   || TREE_CODE_CLASS (code) == tcc_constant)
+	   || TREE_CODE_CLASS (code) == tcc_constant
+	   /* We can't do anything sensible with a BLOCK used as an
+	      expression, but we also can't just die when we see it
+	      because of non-expression uses.  So we avert our eyes
+	      and cross our fingers.  Silly Java.  */
+	   || code == BLOCK)
     *walk_subtrees = 0;
 
   /* Cope with the statement expression extension.  */
diff --git a/gcc/ipa-free-lang-data.cc b/gcc/ipa-free-lang-data.cc
index ccdbf849c25..d82dab852d8 100644
--- a/gcc/ipa-free-lang-data.cc
+++ b/gcc/ipa-free-lang-data.cc
@@ -451,6 +451,8 @@ free_lang_data_in_type (tree type, class free_lang_data_d *fld)
 	  /* C++ FE uses TREE_PURPOSE to store initial values.  */
 	  TREE_PURPOSE (p) = NULL;
 	}
+      /* Java uses TYPE_MIN_VALUE for TYPE_ARGUMENT_SIGNATURE.  */
+      TYPE_MIN_VALUE (type) = NULL;
     }
   else if (TREE_CODE (type) == METHOD_TYPE)
     {
@@ -461,6 +463,8 @@ free_lang_data_in_type (tree type, class free_lang_data_d *fld)
 	  TREE_VALUE (p) = fld_simplified_type (TREE_VALUE (p), fld);
 	  TREE_PURPOSE (p) = NULL;
 	}
+      /* Java uses TYPE_MIN_VALUE for TYPE_ARGUMENT_SIGNATURE.  */
+      TYPE_MIN_VALUE (type) = NULL;
     }
   else if (RECORD_OR_UNION_TYPE_P (type))
     {
@@ -798,8 +802,16 @@ find_decls_types_r (tree *tp, int *ws, void *data)
 	  tree tem;
 	  FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (TYPE_BINFO (t)), i, tem)
 	    fld_worklist_push (TREE_TYPE (tem), fld);
-	  fld_worklist_push (BINFO_TYPE (TYPE_BINFO (t)), fld);
-	  fld_worklist_push (BINFO_VTABLE (TYPE_BINFO (t)), fld);
+	  tem = BINFO_VIRTUALS (TYPE_BINFO (t));
+	  if (tem
+	      /* The Java FE overloads BINFO_VIRTUALS for its own purpose.  */
+	      && TREE_CODE (tem) == TREE_LIST)
+	    do
+	      {
+		fld_worklist_push (TREE_VALUE (tem), fld);
+		tem = TREE_CHAIN (tem);
+	      }
+	    while (tem);
 	}
       if (RECORD_OR_UNION_TYPE_P (t))
 	{
diff --git a/gcc/tree.cc b/gcc/tree.cc
index 0dbac1cb1f4..eb7994d7cae 100644
--- a/gcc/tree.cc
+++ b/gcc/tree.cc
@@ -13900,8 +13900,17 @@ verify_type (const_tree t)
 				     TREE_TYPE (TYPE_MIN_VALUE (t))
 	 but does not for C sizetypes in LTO.  */
     }
+  /* Java uses TYPE_MIN_VALUE_RAW for TYPE_ARGUMENT_SIGNATURE.  */
+  else if (TYPE_MIN_VALUE_RAW (t)
+	   && ((TREE_CODE (t) != METHOD_TYPE && TREE_CODE (t) != FUNCTION_TYPE)
+	       || in_lto_p))
+    {
+      error ("%<TYPE_MIN_VALUE_RAW%> non-NULL");
+      debug_tree (TYPE_MIN_VALUE_RAW (t));
+      error_found = true;
+    }
 
-  /* Check various uses of TYPE_MAXVAL_RAW.  */
+  /* Check various uses of TYPE_MAX_VALUE_RAW.  */
   if (RECORD_OR_UNION_TYPE_P (t))
     {
       if (!TYPE_BINFO (t))
@@ -13912,7 +13921,9 @@ verify_type (const_tree t)
 	  debug_tree (TYPE_BINFO (t));
 	  error_found = true;
 	}
-      else if (TREE_TYPE (TYPE_BINFO (t)) != TYPE_MAIN_VARIANT (t))
+      /* FIXME: Java builds invalid empty binfos that do not have
+	 TREE_TYPE set.  */
+      else if (TREE_TYPE (TYPE_BINFO (t)) != TYPE_MAIN_VARIANT (t) && 0)
 	{
 	  error ("%<TYPE_BINFO%> type is not %<TYPE_MAIN_VARIANT%>");
 	  debug_tree (TREE_TYPE (TYPE_BINFO (t)));
@@ -14118,6 +14129,21 @@ verify_type (const_tree t)
       error ("%<TYPE_CACHED_VALUES_P%> is set while it should not be");
       error_found = true;
     }
+  else if ((TREE_CODE (t) == ARRAY_TYPE || TREE_CODE (t) == INTEGER_TYPE)
+      && TYPE_STRING_FLAG (t))
+    {
+      const_tree b = t;
+      if (TREE_CODE (b) == ARRAY_TYPE)
+	b = TREE_TYPE (t);
+      /* Java builds arrays with TYPE_STRING_FLAG of promoted_char_type
+	 that is 32bits.  */
+      if (TREE_CODE (b) != INTEGER_TYPE)
+	{
+	  error ("%<TYPE_STRING_FLAG%> is set on type that does not look like "
+		 "%<char%> nor array of chars");
+	  error_found = true;
+	}
+    }
   
   /* ipa-devirt makes an assumption that TYPE_METHOD_BASETYPE is always
      TYPE_MAIN_VARIANT and it would be odd to add methods only to variatns
-- 
2.38.1

Reply via email to