]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - gcc/cp/rtti.c
Wrap option names in gcc internal messages with %< and %>.
[thirdparty/gcc.git] / gcc / cp / rtti.c
index 0f7225fc5426f380f4148455207426ed55c316f1..3ca2b5e7b886fd60dd33aa9e8c8f5ae3ba264ae3 100644 (file)
@@ -1,7 +1,5 @@
 /* RunTime Type Identification
-   Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
-   2005, 2006, 2007, 2008, 2009
-   Free Software Foundation, Inc.
+   Copyright (C) 1995-2019 Free Software Foundation, Inc.
    Mostly written by Jason Merrill (jason@cygnus.com).
 
 This file is part of GCC.
@@ -22,18 +20,16 @@ along with GCC; see the file COPYING3.  If not see
 
 #include "config.h"
 #include "system.h"
-#include "intl.h"
 #include "coretypes.h"
-#include "tm.h"
-#include "tree.h"
-#include "cp-tree.h"
-#include "flags.h"
-#include "output.h"
-#include "assert.h"
-#include "toplev.h"
-#include "convert.h"
 #include "target.h"
-#include "c-pragma.h"
+#include "cp-tree.h"
+#include "memmodel.h"
+#include "tm_p.h"
+#include "stringpool.h"
+#include "intl.h"
+#include "stor-layout.h"
+#include "c-family/c-pragma.h"
+#include "gcc-rich-location.h"
 
 /* C++ returns type information to the user in struct type_info
    objects. We also use type information to implement dynamic_cast and
@@ -56,7 +52,7 @@ along with GCC; see the file COPYING3.  If not see
    type_info objects for static initialization.
 
    The type information VAR_DECL of a type is held on the
-   IDENTIFIER_GLOBAL_VALUE of the type's mangled name. That VAR_DECL
+   get_global_binding of the type's mangled name. That VAR_DECL
    will be the internal type.  It will usually have the correct
    internal type reflecting the kind of type it represents (pointer,
    array, function, class, inherited class, etc).  When the type it
@@ -65,7 +61,7 @@ along with GCC; see the file COPYING3.  If not see
    translation, when we are emitting the type info objects.  */
 
 /* Auxiliary data we hold for each type_info derived object we need.  */
-typedef struct GTY (()) tinfo_s {
+struct GTY (()) tinfo_s {
   tree type;  /* The RECORD_TYPE for this type_info object */
 
   tree vtable; /* The VAR_DECL of the vtable.  Only filled at end of
@@ -73,16 +69,15 @@ typedef struct GTY (()) tinfo_s {
 
   tree name;  /* IDENTIFIER_NODE for the ABI specified name of
                 the type_info derived type.  */
-} tinfo_s;
+};
 
-DEF_VEC_O(tinfo_s);
-DEF_VEC_ALLOC_O(tinfo_s,gc);
 
-typedef enum tinfo_kind
+enum tinfo_kind
 {
   TK_TYPE_INFO_TYPE,    /* abi::__type_info_pseudo */
   TK_BASE_TYPE,                /* abi::__base_class_type_info */
-  TK_BUILTIN_TYPE,     /* abi::__fundamental_type_info */
+  TK_DERIVED_TYPES,    /* Start of types derived from abi::__type_info  */
+  TK_BUILTIN_TYPE = TK_DERIVED_TYPES,  /* abi::__fundamental_type_info */
   TK_ARRAY_TYPE,       /* abi::__array_type_info */
   TK_FUNCTION_TYPE,    /* abi::__function_type_info */
   TK_ENUMERAL_TYPE,    /* abi::__enum_type_info */
@@ -90,24 +85,47 @@ typedef enum tinfo_kind
   TK_POINTER_MEMBER_TYPE, /* abi::__pointer_to_member_type_info */
   TK_CLASS_TYPE,       /* abi::__class_type_info */
   TK_SI_CLASS_TYPE,    /* abi::__si_class_type_info */
-  TK_FIXED             /* end of fixed descriptors. */
-  /* ...                  abi::__vmi_type_info<I> */
-} tinfo_kind;
+  TK_VMI_CLASS_TYPES,  /* abi::__vmi_class_type_info<int> */
+  TK_MAX
+};
+
+/* Names of the tinfo types.  Must be same order as TK enumeration
+   above.  */
+
+static const char *const tinfo_names[TK_MAX] =
+{
+  "__type_info",
+  "__base_class_type_info",
+  "__fundamental_type_info",
+  "__array_type_info",
+  "__function_type_info",
+  "__enum_type_info",
+  "__pointer_type_info",
+  "__pointer_to_member_type_info",
+  "__class_type_info",
+  "__si_class_type_info",
+  "__vmi_class_type_info"
+};
+
+/* Helper macro to get maximum scalar-width of pointer or of the 'long'-type.
+   This of interest for llp64 targets.  */
+#define LONGPTR_T \
+  integer_types[(POINTER_SIZE <= TYPE_PRECISION (integer_types[itk_long]) \
+                ? itk_long : itk_long_long)]
 
 /* A vector of all tinfo decls that haven't yet been emitted.  */
-VEC(tree,gc) *unemitted_tinfo_decls;
+vec<tree, va_gc> *unemitted_tinfo_decls;
 
 /* A vector of all type_info derived types we need.  The first few are
    fixed and created early. The remainder are for multiple inheritance
    and are generated as needed. */
-static GTY (()) VEC(tinfo_s,gc) *tinfo_descs;
+static GTY (()) vec<tinfo_s, va_gc> *tinfo_descs;
 
-static tree ifnonnull (tree, tree);
+static tree ifnonnull (tree, tree, tsubst_flags_t);
 static tree tinfo_name (tree, bool);
 static tree build_dynamic_cast_1 (tree, tree, tsubst_flags_t);
 static tree throw_bad_cast (void);
 static tree throw_bad_typeid (void);
-static tree get_tinfo_decl_dynamic (tree);
 static tree get_tinfo_ptr (tree);
 static bool typeid_ok_p (void);
 static int qualifier_flags (tree);
@@ -116,10 +134,10 @@ static tree tinfo_base_init (tinfo_s *, tree);
 static tree generic_initializer (tinfo_s *, tree);
 static tree ptr_initializer (tinfo_s *, tree);
 static tree ptm_initializer (tinfo_s *, tree);
-static tree class_initializer (tinfo_s *, tree, tree);
-static void create_pseudo_type_info (int, const char *, ...);
+static tree class_initializer (tinfo_s *, tree, unsigned, ...);
 static tree get_pseudo_ti_init (tree, unsigned);
 static unsigned get_pseudo_ti_index (tree);
+static tinfo_s *get_tinfo_desc (unsigned);
 static void create_tinfo_types (void);
 static bool typeinfo_in_lib_p (tree);
 
@@ -155,10 +173,10 @@ init_rtti_processing (void)
                             /*tag_scope=*/ts_current, false);
   pop_namespace ();
   const_type_info_type_node
-    = build_qualified_type (type_info_type, TYPE_QUAL_CONST);
+    = cp_build_qualified_type (type_info_type, TYPE_QUAL_CONST);
   type_info_ptr_type = build_pointer_type (const_type_info_type_node);
 
-  unemitted_tinfo_decls = VEC_alloc (tree, gc, 124);
+  vec_alloc (unemitted_tinfo_decls, 124);
 
   create_tinfo_types ();
 }
@@ -175,7 +193,7 @@ build_headof (tree exp)
   tree offset;
   tree index;
 
-  gcc_assert (TREE_CODE (type) == POINTER_TYPE);
+  gcc_assert (TYPE_PTR_P (type));
   type = TREE_TYPE (type);
 
   if (!TYPE_POLYMORPHIC_P (type))
@@ -188,14 +206,12 @@ build_headof (tree exp)
   index = build_int_cst (NULL_TREE,
                         -2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
 
-  offset = build_vtbl_ref (cp_build_indirect_ref (exp, RO_NULL, 
-                                                  tf_warning_or_error), 
+  offset = build_vtbl_ref (cp_build_fold_indirect_ref (exp),
                            index);
 
-  type = build_qualified_type (ptr_type_node,
-                              cp_type_quals (TREE_TYPE (exp)));
-  return build2 (POINTER_PLUS_EXPR, type, exp,
-                convert_to_integer (sizetype, offset));
+  type = cp_build_qualified_type (ptr_type_node,
+                                 cp_type_quals (TREE_TYPE (exp)));
+  return fold_build_pointer_plus (exp, offset);
 }
 
 /* Get a bad_cast node for the program to throw...
@@ -205,12 +221,17 @@ build_headof (tree exp)
 static tree
 throw_bad_cast (void)
 {
-  tree fn = get_identifier ("__cxa_bad_cast");
-  if (!get_global_value_if_present (fn, &fn))
-    fn = push_throw_library_fn (fn, build_function_type (ptr_type_node,
-                                                        void_list_node));
+  static tree fn;
+  if (!fn)
+    {
+      tree name = get_identifier ("__cxa_bad_cast");
+      fn = get_global_binding (name);
+      if (!fn)
+       fn = push_throw_library_fn
+         (name, build_function_type_list (ptr_type_node, NULL_TREE));
+    }
 
-  return build_cxx_call (fn, 0, NULL);
+  return build_cxx_call (fn, 0, NULL, tf_warning_or_error);
 }
 
 /* Return an expression for "__cxa_bad_typeid()".  The expression
@@ -219,17 +240,20 @@ throw_bad_cast (void)
 static tree
 throw_bad_typeid (void)
 {
-  tree fn = get_identifier ("__cxa_bad_typeid");
-  if (!get_global_value_if_present (fn, &fn))
+  static tree fn;
+  if (!fn)
     {
-      tree t;
-
-      t = build_reference_type (const_type_info_type_node);
-      t = build_function_type (t, void_list_node);
-      fn = push_throw_library_fn (fn, t);
+      tree name = get_identifier ("__cxa_bad_typeid");
+      fn = get_global_binding (name);
+      if (!fn)
+       {
+         tree t = build_reference_type (const_type_info_type_node);
+         t = build_function_type_list (t, NULL_TREE);
+         fn = push_throw_library_fn (name, t);
+       }
     }
 
-  return build_cxx_call (fn, 0, NULL);
+  return build_cxx_call (fn, 0, NULL, tf_warning_or_error);
 }
 \f
 /* Return an lvalue expression whose type is "const std::type_info"
@@ -238,7 +262,7 @@ throw_bad_typeid (void)
    otherwise return the static type of the expression.  */
 
 static tree
-get_tinfo_decl_dynamic (tree exp)
+get_tinfo_decl_dynamic (tree exp, tsubst_flags_t complain)
 {
   tree type;
   tree t;
@@ -246,17 +270,18 @@ get_tinfo_decl_dynamic (tree exp)
   if (error_operand_p (exp))
     return error_mark_node;
 
-  exp = resolve_nondeduced_context (exp);
+  exp = resolve_nondeduced_context (exp, complain);
 
   /* peel back references, so they match.  */
-  type = non_reference (TREE_TYPE (exp));
+  type = non_reference (unlowered_expr_type (exp));
 
   /* Peel off cv qualifiers.  */
   type = TYPE_MAIN_VARIANT (type);
 
   /* For UNKNOWN_TYPEs call complete_type_or_else to get diagnostics.  */
-  if (CLASS_TYPE_P (type) || TREE_CODE (type) == UNKNOWN_TYPE)
-    type = complete_type_or_else (type, exp);
+  if (CLASS_TYPE_P (type) || type == unknown_type_node
+      || type == init_list_type_node)
+    type = complete_type_or_maybe_complain (type, exp, complain);
 
   if (!type)
     return error_mark_node;
@@ -277,37 +302,38 @@ get_tinfo_decl_dynamic (tree exp)
     /* Otherwise return the type_info for the static type of the expr.  */
     t = get_tinfo_ptr (TYPE_MAIN_VARIANT (type));
 
-  return cp_build_indirect_ref (t, RO_NULL, tf_warning_or_error);
+  return cp_build_fold_indirect_ref (t);
 }
 
 static bool
 typeid_ok_p (void)
 {
-  tree pseudo_type_info, type_info_type;
-
   if (! flag_rtti)
     {
-      error ("cannot use typeid with -fno-rtti");
+      error ("cannot use %<typeid%> with %<-fno-rtti%>");
       return false;
     }
 
   if (!COMPLETE_TYPE_P (const_type_info_type_node))
     {
-      error ("must #include <typeinfo> before using typeid");
+      gcc_rich_location richloc (input_location);
+      maybe_add_include_fixit (&richloc, "<typeinfo>", false);
+      error_at (&richloc,
+               "must %<#include <typeinfo>%> before using"
+               " %<typeid%>");
+
       return false;
     }
 
-  pseudo_type_info
-    = VEC_index (tinfo_s, tinfo_descs, TK_TYPE_INFO_TYPE)->type;
-  type_info_type = TYPE_MAIN_VARIANT (const_type_info_type_node);
+  tree pseudo = TYPE_MAIN_VARIANT (get_tinfo_desc (TK_TYPE_INFO_TYPE)->type);
+  tree real = TYPE_MAIN_VARIANT (const_type_info_type_node);
 
   /* Make sure abi::__type_info_pseudo has the same alias set
      as std::type_info.  */
-  if (! TYPE_ALIAS_SET_KNOWN_P (pseudo_type_info))
-    TYPE_ALIAS_SET (pseudo_type_info) = get_alias_set (type_info_type);
+  if (! TYPE_ALIAS_SET_KNOWN_P (pseudo))
+    TYPE_ALIAS_SET (pseudo) = get_alias_set (real);
   else
-    gcc_assert (TYPE_ALIAS_SET (pseudo_type_info)
-               == get_alias_set (type_info_type));
+    gcc_assert (TYPE_ALIAS_SET (pseudo) == get_alias_set (real));
 
   return true;
 }
@@ -316,9 +342,9 @@ typeid_ok_p (void)
    an lvalue of type "const std::type_info".  */
 
 tree
-build_typeid (tree exp)
+build_typeid (tree exp, tsubst_flags_t complain)
 {
-  tree cond = NULL_TREE;
+  tree cond = NULL_TREE, initial_expr = exp;
   int nonnull = 0;
 
   if (exp == error_mark_node || !typeid_ok_p ())
@@ -327,17 +353,21 @@ build_typeid (tree exp)
   if (processing_template_decl)
     return build_min (TYPEID_EXPR, const_type_info_type_node, exp);
 
-  if (TREE_CODE (exp) == INDIRECT_REF
-      && TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == POINTER_TYPE
-      && TYPE_POLYMORPHIC_P (TREE_TYPE (exp))
+  /* FIXME when integrating with c_fully_fold, mark
+     resolves_to_fixed_type_p case as a non-constant expression.  */
+  if (TYPE_POLYMORPHIC_P (TREE_TYPE (exp))
       && ! resolves_to_fixed_type_p (exp, &nonnull)
       && ! nonnull)
     {
-      exp = stabilize_reference (exp);
-      cond = cp_convert (boolean_type_node, TREE_OPERAND (exp, 0));
+      /* So we need to look into the vtable of the type of exp.
+         Make sure it isn't a null lvalue.  */
+      exp = cp_build_addr_expr (exp, complain);
+      exp = save_expr (exp);
+      cond = cp_convert (boolean_type_node, exp, complain);
+      exp = cp_build_fold_indirect_ref (exp);
     }
 
-  exp = get_tinfo_decl_dynamic (exp);
+  exp = get_tinfo_decl_dynamic (exp, complain);
 
   if (exp == error_mark_node)
     return error_mark_node;
@@ -348,6 +378,8 @@ build_typeid (tree exp)
 
       exp = build3 (COND_EXPR, TREE_TYPE (exp), cond, exp, bad);
     }
+  else
+    mark_type_use (initial_expr);
 
   return exp;
 }
@@ -400,6 +432,8 @@ get_tinfo_decl (tree type)
     type = build_function_type (TREE_TYPE (type),
                                TREE_CHAIN (TYPE_ARG_TYPES (type)));
 
+  type = complete_type (type);
+
   /* For a class type, the variable is cached in the type node
      itself.  */
   if (CLASS_TYPE_P (type))
@@ -411,12 +445,12 @@ get_tinfo_decl (tree type)
 
   name = mangle_typeinfo_for_type (type);
 
-  d = IDENTIFIER_GLOBAL_VALUE (name);
+  d = get_global_binding (name);
   if (!d)
     {
       int ix = get_pseudo_ti_index (type);
-      tinfo_s *ti = VEC_index (tinfo_s, tinfo_descs, ix);
-
+      const tinfo_s *ti = get_tinfo_desc (ix);
+      
       d = build_lang_decl (VAR_DECL, name, ti->type);
       SET_DECL_ASSEMBLER_NAME (d, name);
       /* Remember the type it is for.  */
@@ -437,7 +471,7 @@ get_tinfo_decl (tree type)
        CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type)) = d;
 
       /* Add decl to the global array of tinfo decls.  */
-      VEC_safe_push (tree, gc, unemitted_tinfo_decls, d);
+      vec_safe_push (unemitted_tinfo_decls, d);
     }
 
   return d;
@@ -459,7 +493,7 @@ get_tinfo_ptr (tree type)
 /* Return the type_info object for TYPE.  */
 
 tree
-get_typeid (tree type)
+get_typeid (tree type, tsubst_flags_t complain)
 {
   if (type == error_mark_node || !typeid_ok_p ())
     return error_mark_node;
@@ -472,32 +506,44 @@ get_typeid (tree type)
      referenced type.  */
   type = non_reference (type);
 
+  /* This is not one of the uses of a qualified function type in 8.3.5.  */
+  if (TREE_CODE (type) == FUNCTION_TYPE
+      && (type_memfn_quals (type) != TYPE_UNQUALIFIED
+         || type_memfn_rqual (type) != REF_QUAL_NONE))
+    {
+      if (complain & tf_error)
+       error ("typeid of qualified function type %qT", type);
+      return error_mark_node;
+    }
+
   /* The top-level cv-qualifiers of the lvalue expression or the type-id
      that is the operand of typeid are always ignored.  */
   type = TYPE_MAIN_VARIANT (type);
 
   /* For UNKNOWN_TYPEs call complete_type_or_else to get diagnostics.  */
-  if (CLASS_TYPE_P (type) || TREE_CODE (type) == UNKNOWN_TYPE)
-    type = complete_type_or_else (type, NULL_TREE);
+  if (CLASS_TYPE_P (type) || type == unknown_type_node
+      || type == init_list_type_node)
+    type = complete_type_or_maybe_complain (type, NULL_TREE, complain);
 
   if (!type)
     return error_mark_node;
 
-  return cp_build_indirect_ref (get_tinfo_ptr (type), RO_NULL, 
-                                tf_warning_or_error);
+  return cp_build_fold_indirect_ref (get_tinfo_ptr (type));
 }
 
 /* Check whether TEST is null before returning RESULT.  If TEST is used in
    RESULT, it must have previously had a save_expr applied to it.  */
 
 static tree
-ifnonnull (tree test, tree result)
+ifnonnull (tree test, tree result, tsubst_flags_t complain)
 {
-  return build3 (COND_EXPR, TREE_TYPE (result),
-                build2 (EQ_EXPR, boolean_type_node, test,
-                        cp_convert (TREE_TYPE (test), integer_zero_node)),
-                cp_convert (TREE_TYPE (result), integer_zero_node),
-                result);
+  tree cond = build2 (NE_EXPR, boolean_type_node, test,
+                     cp_convert (TREE_TYPE (test), nullptr_node, complain));
+  /* This is a compiler generated comparison, don't emit
+     e.g. -Wnonnull-compare warning for it.  */
+  TREE_NO_WARNING (cond) = 1;
+  return build3 (COND_EXPR, TREE_TYPE (result), cond, result,
+                cp_convert (TREE_TYPE (result), nullptr_node, complain));
 }
 
 /* Execute a dynamic cast, as described in section 5.2.6 of the 9/93 working
@@ -507,7 +553,7 @@ static tree
 build_dynamic_cast_1 (tree type, tree expr, tsubst_flags_t complain)
 {
   enum tree_code tc = TREE_CODE (type);
-  tree exprtype = TREE_TYPE (expr);
+  tree exprtype;
   tree dcast_fn;
   tree old_expr = expr;
   const char *errstr = NULL;
@@ -520,7 +566,7 @@ build_dynamic_cast_1 (tree type, tree expr, tsubst_flags_t complain)
   switch (tc)
     {
     case POINTER_TYPE:
-      if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
+      if (VOID_TYPE_P (TREE_TYPE (type)))
        break;
       /* Fall through.  */
     case REFERENCE_TYPE:
@@ -543,10 +589,15 @@ build_dynamic_cast_1 (tree type, tree expr, tsubst_flags_t complain)
 
   if (tc == POINTER_TYPE)
     {
+      expr = decay_conversion (expr, complain);
+      exprtype = TREE_TYPE (expr);
+
       /* If T is a pointer type, v shall be an rvalue of a pointer to
         complete class type, and the result is an rvalue of type T.  */
 
-      if (TREE_CODE (exprtype) != POINTER_TYPE)
+      expr = mark_rvalue_use (expr);
+
+      if (!TYPE_PTR_P (exprtype))
        {
          errstr = _("source is not a pointer");
          goto fail;
@@ -564,25 +615,23 @@ build_dynamic_cast_1 (tree type, tree expr, tsubst_flags_t complain)
     }
   else
     {
-      exprtype = build_reference_type (exprtype);
+      expr = mark_lvalue_use (expr);
+      exprtype = TREE_TYPE (expr);
 
       /* T is a reference type, v shall be an lvalue of a complete class
         type, and the result is an lvalue of the type referred to by T.  */
-
-      if (! MAYBE_CLASS_TYPE_P (TREE_TYPE (exprtype)))
+      if (! MAYBE_CLASS_TYPE_P (exprtype))
        {
          errstr = _("source is not of class type");
          goto fail;
        }
-      if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
+      if (!COMPLETE_TYPE_P (complete_type (exprtype)))
        {
          errstr = _("source is of incomplete class type");
          goto fail;
        }
 
-      /* Apply trivial conversion T -> T& for dereferenced ptrs.  */
-      expr = convert_to_reference (exprtype, expr, CONV_IMPLICIT,
-                                  LOOKUP_NORMAL, NULL_TREE);
+      exprtype = cp_build_reference_type (exprtype, !lvalue_p (expr));
     }
 
   /* The dynamic_cast operator shall not cast away constness.  */
@@ -596,21 +645,17 @@ build_dynamic_cast_1 (tree type, tree expr, tsubst_flags_t complain)
   /* If *type is an unambiguous accessible base class of *exprtype,
      convert statically.  */
   {
-    tree binfo;
-
-    binfo = lookup_base (TREE_TYPE (exprtype), TREE_TYPE (type),
-                        ba_check, NULL);
-
+    tree binfo = lookup_base (TREE_TYPE (exprtype), TREE_TYPE (type),
+                             ba_check, NULL, complain);
     if (binfo)
-      {
-       expr = build_base_path (PLUS_EXPR, convert_from_reference (expr),
-                               binfo, 0);
-       if (TREE_CODE (exprtype) == POINTER_TYPE)
-         expr = rvalue (expr);
-       return expr;
-      }
+      return build_static_cast (type, expr, complain);
   }
 
+  /* Apply trivial conversion T -> T& for dereferenced ptrs.  */
+  if (tc == REFERENCE_TYPE)
+    expr = convert_to_reference (exprtype, expr, CONV_IMPLICIT,
+                                LOOKUP_NORMAL, NULL_TREE, complain);
+
   /* Otherwise *exprtype must be a polymorphic class (have a vtbl).  */
   if (TYPE_POLYMORPHIC_P (TREE_TYPE (exprtype)))
     {
@@ -620,7 +665,7 @@ build_dynamic_cast_1 (tree type, tree expr, tsubst_flags_t complain)
        {
          /* if b is an object, dynamic_cast<void *>(&b) == (void *)&b.  */
          if (TREE_CODE (expr) == ADDR_EXPR
-             && TREE_CODE (TREE_OPERAND (expr, 0)) == VAR_DECL
+             && VAR_P (TREE_OPERAND (expr, 0))
              && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == RECORD_TYPE)
            return build1 (NOP_EXPR, type, expr);
 
@@ -630,7 +675,7 @@ build_dynamic_cast_1 (tree type, tree expr, tsubst_flags_t complain)
          expr1 = build_headof (expr);
          if (TREE_TYPE (expr1) != type)
            expr1 = build1 (NOP_EXPR, type, expr1);
-         return ifnonnull (expr, expr1);
+         return ifnonnull (expr, expr1, complain);
        }
       else
        {
@@ -643,7 +688,7 @@ build_dynamic_cast_1 (tree type, tree expr, tsubst_flags_t complain)
             dynamic_cast<D&>(b) (b an object) cannot succeed.  */
          if (tc == REFERENCE_TYPE)
            {
-             if (TREE_CODE (old_expr) == VAR_DECL
+             if (VAR_P (old_expr)
                  && TREE_CODE (TREE_TYPE (old_expr)) == RECORD_TYPE)
                {
                  tree expr = throw_bad_cast ();
@@ -659,7 +704,7 @@ build_dynamic_cast_1 (tree type, tree expr, tsubst_flags_t complain)
          else if (TREE_CODE (expr) == ADDR_EXPR)
            {
              tree op = TREE_OPERAND (expr, 0);
-             if (TREE_CODE (op) == VAR_DECL
+             if (VAR_P (op)
                  && TREE_CODE (TREE_TYPE (op)) == RECORD_TYPE)
                {
                   if (complain & tf_warning)
@@ -674,18 +719,20 @@ build_dynamic_cast_1 (tree type, tree expr, tsubst_flags_t complain)
          if (!flag_rtti)
            {
               if (complain & tf_error)
-                error ("%<dynamic_cast%> not permitted with -fno-rtti");
+               error ("%<dynamic_cast%> not permitted with %<-fno-rtti%>");
              return error_mark_node;
            }
 
          target_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
          static_type = TYPE_MAIN_VARIANT (TREE_TYPE (exprtype));
          td2 = get_tinfo_decl (target_type);
-         mark_used (td2);
-         td2 = cp_build_unary_op (ADDR_EXPR, td2, 0, complain);
+         if (!mark_used (td2, complain) && !(complain & tf_error))
+           return error_mark_node;
+         td2 = cp_build_addr_expr (td2, complain);
          td3 = get_tinfo_decl (static_type);
-         mark_used (td3);
-         td3 = cp_build_unary_op (ADDR_EXPR, td3, 0, complain);
+         if (!mark_used (td3, complain) && !(complain & tf_error))
+           return error_mark_node;
+         td3 = cp_build_addr_expr (td3, complain);
 
          /* Determine how T and V are related.  */
          boff = dcast_base_hint (static_type, target_type);
@@ -695,7 +742,7 @@ build_dynamic_cast_1 (tree type, tree expr, tsubst_flags_t complain)
 
          expr1 = expr;
          if (tc == REFERENCE_TYPE)
-           expr1 = cp_build_unary_op (ADDR_EXPR, expr1, 0, complain);
+           expr1 = cp_build_addr_expr (expr1, complain);
 
          elems[0] = expr1;
          elems[1] = td3;
@@ -715,21 +762,19 @@ build_dynamic_cast_1 (tree type, tree expr, tsubst_flags_t complain)
                                    /*tag_scope=*/ts_current, false);
 
              tinfo_ptr = build_pointer_type
-               (build_qualified_type
+               (cp_build_qualified_type
                 (tinfo_ptr, TYPE_QUAL_CONST));
              name = "__dynamic_cast";
-             tmp = tree_cons
-               (NULL_TREE, const_ptr_type_node, tree_cons
-                (NULL_TREE, tinfo_ptr, tree_cons
-                 (NULL_TREE, tinfo_ptr, tree_cons
-                  (NULL_TREE, ptrdiff_type_node, void_list_node))));
-             tmp = build_function_type (ptr_type_node, tmp);
-             dcast_fn = build_library_fn_ptr (name, tmp);
-             DECL_PURE_P (dcast_fn) = 1;
+             tmp = build_function_type_list (ptr_type_node,
+                                             const_ptr_type_node,
+                                             tinfo_ptr, tinfo_ptr,
+                                             ptrdiff_type_node, NULL_TREE);
+             dcast_fn = build_library_fn_ptr (name, tmp,
+                                              ECF_LEAF | ECF_PURE | ECF_NOTHROW);
              pop_abi_namespace ();
              dynamic_cast_node = dcast_fn;
            }
-         result = build_cxx_call (dcast_fn, 4, elems);
+         result = build_cxx_call (dcast_fn, 4, elems, complain);
 
          if (tc == REFERENCE_TYPE)
            {
@@ -737,15 +782,15 @@ build_dynamic_cast_1 (tree type, tree expr, tsubst_flags_t complain)
              tree neq;
 
              result = save_expr (result);
-             neq = c_common_truthvalue_conversion (input_location, result);
+             neq = cp_truthvalue_conversion (result);
              return cp_convert (type,
                                 build3 (COND_EXPR, TREE_TYPE (result),
-                                        neq, result, bad));
+                                        neq, result, bad), complain);
            }
 
          /* Now back to the type we want from a void*.  */
-         result = cp_convert (type, result);
-         return ifnonnull (expr, result);
+         result = cp_convert (type, result, complain);
+         return ifnonnull (expr, result, complain);
        }
     }
   else
@@ -754,13 +799,15 @@ build_dynamic_cast_1 (tree type, tree expr, tsubst_flags_t complain)
  fail:
   if (complain & tf_error)
     error ("cannot dynamic_cast %qE (of type %q#T) to type %q#T (%s)",
-           expr, exprtype, type, errstr);
+           old_expr, TREE_TYPE (old_expr), type, errstr);
   return error_mark_node;
 }
 
 tree
 build_dynamic_cast (tree type, tree expr, tsubst_flags_t complain)
 {
+  tree r;
+
   if (type == error_mark_node || expr == error_mark_node)
     return error_mark_node;
 
@@ -771,9 +818,12 @@ build_dynamic_cast (tree type, tree expr, tsubst_flags_t complain)
       return convert_from_reference (expr);
     }
 
-  return convert_from_reference (build_dynamic_cast_1 (type, expr, complain));
+  r = convert_from_reference (build_dynamic_cast_1 (type, expr, complain));
+  if (r != error_mark_node)
+    maybe_warn_about_useless_cast (type, expr, complain);
+  return r;
 }
-\f
+
 /* Return the runtime bit mask encoding the qualifiers of TYPE.  */
 
 static int
@@ -798,13 +848,13 @@ static bool
 target_incomplete_p (tree type)
 {
   while (true)
-    if (TYPE_PTRMEM_P (type))
+    if (TYPE_PTRDATAMEM_P (type))
       {
        if (!COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type)))
          return true;
        type = TYPE_PTRMEM_POINTED_TO_TYPE (type);
       }
-    else if (TREE_CODE (type) == POINTER_TYPE)
+    else if (TYPE_PTR_P (type))
       type = TREE_TYPE (type);
     else
       return !COMPLETE_OR_VOID_TYPE_P (type);
@@ -835,7 +885,7 @@ involves_incomplete_p (tree type)
     case UNION_TYPE:
       if (!COMPLETE_TYPE_P (type))
        return true;
-
+      /* Fall through.  */
     default:
       /* All other types do not involve incomplete class types.  */
       return false;
@@ -851,16 +901,17 @@ involves_incomplete_p (tree type)
 static tree
 tinfo_base_init (tinfo_s *ti, tree target)
 {
-  tree init = NULL_TREE;
+  tree init;
   tree name_decl;
   tree vtable_ptr;
+  vec<constructor_elt, va_gc> *v;
 
   {
     tree name_name, name_string;
 
     /* Generate the NTBS array variable.  */
     tree name_type = build_cplus_array_type
-                    (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
+                    (cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST),
                     NULL_TREE);
 
     /* Determine the name of the variable -- and remember with which
@@ -903,12 +954,11 @@ tinfo_base_init (tinfo_s *ti, tree target)
        }
 
       vtable_ptr = get_vtable_decl (real_type, /*complete=*/1);
-      vtable_ptr = cp_build_unary_op (ADDR_EXPR, vtable_ptr, 0, 
-                                   tf_warning_or_error);
+      vtable_ptr = cp_build_addr_expr (vtable_ptr, tf_warning_or_error);
 
       /* We need to point into the middle of the vtable.  */
-      vtable_ptr = build2
-       (POINTER_PLUS_EXPR, TREE_TYPE (vtable_ptr), vtable_ptr,
+      vtable_ptr = fold_build_pointer_plus
+       (vtable_ptr,
         size_binop (MULT_EXPR,
                     size_int (2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE),
                     TYPE_SIZE_UNIT (vtable_entry_type)));
@@ -916,14 +966,14 @@ tinfo_base_init (tinfo_s *ti, tree target)
       ti->vtable = vtable_ptr;
     }
 
-  init = tree_cons (NULL_TREE, vtable_ptr, init);
+  vec_alloc (v, 2);
+  CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, vtable_ptr);
+  CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
+                         decay_conversion (name_decl, tf_warning_or_error));
 
-  init = tree_cons (NULL_TREE, decay_conversion (name_decl), init);
-
-  init = build_constructor_from_list (init_list_type_node, nreverse (init));
+  init = build_constructor (init_list_type_node, v);
   TREE_CONSTANT (init) = 1;
   TREE_STATIC (init) = 1;
-  init = tree_cons (NULL_TREE, init, NULL_TREE);
 
   return init;
 }
@@ -937,7 +987,7 @@ generic_initializer (tinfo_s *ti, tree target)
 {
   tree init = tinfo_base_init (ti, target);
 
-  init = build_constructor_from_list (init_list_type_node, init);
+  init = build_constructor_single (init_list_type_node, NULL_TREE, init);
   TREE_CONSTANT (init) = 1;
   TREE_STATIC (init) = 1;
   return init;
@@ -954,15 +1004,30 @@ ptr_initializer (tinfo_s *ti, tree target)
   tree to = TREE_TYPE (target);
   int flags = qualifier_flags (to);
   bool incomplete = target_incomplete_p (to);
+  vec<constructor_elt, va_gc> *v;
+  vec_alloc (v, 3);
 
   if (incomplete)
     flags |= 8;
-  init = tree_cons (NULL_TREE, build_int_cst (NULL_TREE, flags), init);
-  init = tree_cons (NULL_TREE,
-                   get_tinfo_ptr (TYPE_MAIN_VARIANT (to)),
-                   init);
+  if (tx_safe_fn_type_p (to))
+    {
+      flags |= 0x20;
+      to = tx_unsafe_fn_variant (to);
+    }
+  if (flag_noexcept_type
+      && (TREE_CODE (to) == FUNCTION_TYPE
+         || TREE_CODE (to) == METHOD_TYPE)
+      && TYPE_NOTHROW_P (to))
+    {
+      flags |= 0x40;
+      to = build_exception_variant (to, NULL_TREE);
+    }
+  CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
+  CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, flags));
+  CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
+                          get_tinfo_ptr (TYPE_MAIN_VARIANT (to)));
 
-  init = build_constructor_from_list (init_list_type_node, nreverse (init));
+  init = build_constructor (init_list_type_node, v);
   TREE_CONSTANT (init) = 1;
   TREE_STATIC (init) = 1;
   return init;
@@ -981,20 +1046,20 @@ ptm_initializer (tinfo_s *ti, tree target)
   tree klass = TYPE_PTRMEM_CLASS_TYPE (target);
   int flags = qualifier_flags (to);
   bool incomplete = target_incomplete_p (to);
+  vec<constructor_elt, va_gc> *v;
+  vec_alloc (v, 4);
 
   if (incomplete)
     flags |= 0x8;
   if (!COMPLETE_TYPE_P (klass))
     flags |= 0x10;
-  init = tree_cons (NULL_TREE, build_int_cst (NULL_TREE, flags), init);
-  init = tree_cons (NULL_TREE,
-                   get_tinfo_ptr (TYPE_MAIN_VARIANT (to)),
-                   init);
-  init = tree_cons (NULL_TREE,
-                   get_tinfo_ptr (klass),
-                   init);
-
-  init = build_constructor_from_list (init_list_type_node, nreverse (init));
+  CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
+  CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, flags));
+  CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
+                          get_tinfo_ptr (TYPE_MAIN_VARIANT (to)));
+  CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, get_tinfo_ptr (klass));
+
+  init = build_constructor (init_list_type_node, v);
   TREE_CONSTANT (init) = 1;
   TREE_STATIC (init) = 1;
   return init;
@@ -1002,15 +1067,24 @@ ptm_initializer (tinfo_s *ti, tree target)
 
 /* Return the CONSTRUCTOR expr for a type_info of class TYPE.
    TI provides information about the particular __class_type_info derivation,
-   which adds hint flags and TRAIL initializers to the type_info base.  */
+   which adds hint flags and N extra initializers to the type_info base.  */
 
 static tree
-class_initializer (tinfo_s *ti, tree target, tree trail)
+class_initializer (tinfo_s *ti, tree target, unsigned n, ...)
 {
   tree init = tinfo_base_init (ti, target);
-
-  TREE_CHAIN (init) = trail;
-  init = build_constructor_from_list (init_list_type_node, init);
+  va_list extra_inits;
+  unsigned i;
+  vec<constructor_elt, va_gc> *v;
+  vec_alloc (v, n+1);
+
+  CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
+  va_start (extra_inits, n);
+  for (i = 0; i < n; i++)
+    CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, va_arg (extra_inits, tree));
+  va_end (extra_inits);
+
+  init = build_constructor (init_list_type_node, v);
   TREE_CONSTANT (init) = 1;
   TREE_STATIC (init) = 1;
   return init;
@@ -1024,7 +1098,7 @@ typeinfo_in_lib_p (tree type)
 {
   /* The typeinfo objects for `T*' and `const T*' are in the runtime
      library for simple types T.  */
-  if (TREE_CODE (type) == POINTER_TYPE
+  if (TYPE_PTR_P (type)
       && (cp_type_quals (TREE_TYPE (type)) == TYPE_QUAL_CONST
          || cp_type_quals (TREE_TYPE (type)) == TYPE_UNQUALIFIED))
     type = TREE_TYPE (type);
@@ -1035,8 +1109,12 @@ typeinfo_in_lib_p (tree type)
     case BOOLEAN_TYPE:
     case REAL_TYPE:
     case VOID_TYPE:
+    case NULLPTR_TYPE:
       return true;
 
+    case LANG_TYPE:
+      /* fall through.  */
+
     default:
       return false;
     }
@@ -1048,7 +1126,7 @@ typeinfo_in_lib_p (tree type)
 static tree
 get_pseudo_ti_init (tree type, unsigned tk_index)
 {
-  tinfo_s *ti = VEC_index (tinfo_s, tinfo_descs, tk_index);
+  tinfo_s *ti = get_tinfo_desc (tk_index);
 
   gcc_assert (at_eof);
   switch (tk_index)
@@ -1066,17 +1144,16 @@ get_pseudo_ti_init (tree type, unsigned tk_index)
       return generic_initializer (ti, type);
 
     case TK_CLASS_TYPE:
-      return class_initializer (ti, type, NULL_TREE);
+      return class_initializer (ti, type, 0);
 
     case TK_SI_CLASS_TYPE:
       {
        tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), 0);
        tree tinfo = get_tinfo_ptr (BINFO_TYPE (base_binfo));
-       tree base_inits = tree_cons (NULL_TREE, tinfo, NULL_TREE);
 
        /* get_tinfo_ptr might have reallocated the tinfo_descs vector.  */
-       ti = VEC_index (tinfo_s, tinfo_descs, tk_index);
-       return class_initializer (ti, type, base_inits);
+       ti = &(*tinfo_descs)[tk_index];
+       return class_initializer (ti, type, 1, tinfo);
       }
 
     default:
@@ -1084,24 +1161,24 @@ get_pseudo_ti_init (tree type, unsigned tk_index)
        int hint = ((CLASSTYPE_REPEATED_BASE_P (type) << 0)
                    | (CLASSTYPE_DIAMOND_SHAPED_P (type) << 1));
        tree binfo = TYPE_BINFO (type);
-       int nbases = BINFO_N_BASE_BINFOS (binfo);
-       VEC(tree,gc) *base_accesses = BINFO_BASE_ACCESSES (binfo);
-       tree offset_type = integer_types[itk_long];
-       tree base_inits = NULL_TREE;
-       int ix;
+       unsigned nbases = BINFO_N_BASE_BINFOS (binfo);
+       vec<tree, va_gc> *base_accesses = BINFO_BASE_ACCESSES (binfo);
+       tree offset_type = LONGPTR_T;
+       vec<constructor_elt, va_gc> *init_vec = NULL;
 
-       gcc_assert (tk_index >= TK_FIXED);
+       gcc_assert (tk_index - TK_VMI_CLASS_TYPES + 1 == nbases);
 
+       vec_safe_grow (init_vec, nbases);
        /* Generate the base information initializer.  */
-       for (ix = nbases; ix--;)
+       for (unsigned ix = nbases; ix--;)
          {
            tree base_binfo = BINFO_BASE_BINFO (binfo, ix);
-           tree base_init = NULL_TREE;
            int flags = 0;
            tree tinfo;
            tree offset;
+           vec<constructor_elt, va_gc> *v;
 
-           if (VEC_index (tree, base_accesses, ix) == access_public_node)
+           if ((*base_accesses)[ix] == access_public_node)
              flags |= 2;
            tinfo = get_tinfo_ptr (BINFO_TYPE (base_binfo));
            if (BINFO_VIRTUAL_P (base_binfo))
@@ -1122,93 +1199,26 @@ get_pseudo_ti_init (tree type, unsigned tk_index)
            offset = fold_build2_loc (input_location,
                                  BIT_IOR_EXPR, offset_type, offset,
                                  build_int_cst (offset_type, flags));
-           base_init = tree_cons (NULL_TREE, offset, base_init);
-           base_init = tree_cons (NULL_TREE, tinfo, base_init);
-           base_init = build_constructor_from_list (init_list_type_node, base_init);
-           base_inits = tree_cons (NULL_TREE, base_init, base_inits);
+           vec_alloc (v, 2);
+           CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, tinfo);
+           CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, offset);
+           tree base_init = build_constructor (init_list_type_node, v);
+           constructor_elt *e = &(*init_vec)[ix];
+           e->index = NULL_TREE;
+           e->value = base_init;
          }
-       base_inits = build_constructor_from_list (init_list_type_node, base_inits);
-       base_inits = tree_cons (NULL_TREE, base_inits, NULL_TREE);
-       /* Prepend the number of bases.  */
-       base_inits = tree_cons (NULL_TREE,
-                               build_int_cst (NULL_TREE, nbases),
-                               base_inits);
-       /* Prepend the hint flags.  */
-       base_inits = tree_cons (NULL_TREE,
-                               build_int_cst (NULL_TREE, hint),
-                               base_inits);
+       tree base_inits = build_constructor (init_list_type_node, init_vec);
 
        /* get_tinfo_ptr might have reallocated the tinfo_descs vector.  */
-       ti = VEC_index (tinfo_s, tinfo_descs, tk_index);
-       return class_initializer (ti, type, base_inits);
+       ti = &(*tinfo_descs)[tk_index];
+       return class_initializer (ti, type, 3,
+                                 build_int_cst (NULL_TREE, hint),
+                                 build_int_cst (NULL_TREE, nbases),
+                                 base_inits);
       }
     }
 }
 
-/* Generate the RECORD_TYPE containing the data layout of a type_info
-   derivative as used by the runtime. This layout must be consistent with
-   that defined in the runtime support. Also generate the VAR_DECL for the
-   type's vtable. We explicitly manage the vtable member, and name it for
-   real type as used in the runtime. The RECORD type has a different name,
-   to avoid collisions.  Return a TREE_LIST who's TINFO_PSEUDO_TYPE
-   is the generated type and TINFO_VTABLE_NAME is the name of the
-   vtable.  We have to delay generating the VAR_DECL of the vtable
-   until the end of the translation, when we'll have seen the library
-   definition, if there was one.
-
-   REAL_NAME is the runtime's name of the type. Trailing arguments are
-   additional FIELD_DECL's for the structure. The final argument must be
-   NULL.  */
-
-static void
-create_pseudo_type_info (int tk, const char *real_name, ...)
-{
-  tinfo_s *ti;
-  tree pseudo_type;
-  char *pseudo_name;
-  tree fields;
-  tree field_decl;
-  va_list ap;
-
-  va_start (ap, real_name);
-
-  /* Generate the pseudo type name.  */
-  pseudo_name = (char *) alloca (strlen (real_name) + 30);
-  strcpy (pseudo_name, real_name);
-  strcat (pseudo_name, "_pseudo");
-  if (tk >= TK_FIXED)
-    sprintf (pseudo_name + strlen (pseudo_name), "%d", tk - TK_FIXED);
-
-  /* First field is the pseudo type_info base class.  */
-  fields = build_decl (input_location,
-                      FIELD_DECL, NULL_TREE,
-                      VEC_index (tinfo_s, tinfo_descs,
-                                 TK_TYPE_INFO_TYPE)->type);
-
-  /* Now add the derived fields.  */
-  while ((field_decl = va_arg (ap, tree)))
-    {
-      TREE_CHAIN (field_decl) = fields;
-      fields = field_decl;
-    }
-
-  /* Create the pseudo type.  */
-  pseudo_type = make_class_type (RECORD_TYPE);
-  finish_builtin_struct (pseudo_type, pseudo_name, fields, NULL_TREE);
-  CLASSTYPE_AS_BASE (pseudo_type) = pseudo_type;
-
-  ti = VEC_index (tinfo_s, tinfo_descs, tk);
-  ti->type = cp_build_qualified_type (pseudo_type, TYPE_QUAL_CONST);
-  ti->name = get_identifier (real_name);
-  ti->vtable = NULL_TREE;
-
-  /* Pretend this is public so determine_visibility doesn't give vtables
-     internal linkage.  */
-  TREE_PUBLIC (TYPE_MAIN_DECL (ti->type)) = 1;
-
-  va_end (ap);
-}
-
 /* Return the index of a pseudo type info type node used to describe
    TYPE.  TYPE must be a complete type (or cv void), except at the end
    of the translation unit.  */
@@ -1243,84 +1253,34 @@ get_pseudo_ti_index (tree type)
     case UNION_TYPE:
     case RECORD_TYPE:
       if (TYPE_PTRMEMFUNC_P (type))
-       {
-         ix = TK_POINTER_MEMBER_TYPE;
-         break;
-       }
+       ix = TK_POINTER_MEMBER_TYPE;
       else if (!COMPLETE_TYPE_P (type))
        {
          if (!at_eof)
            cxx_incomplete_type_error (NULL_TREE, type);
          ix = TK_CLASS_TYPE;
-         break;
-       }
-      else if (!BINFO_N_BASE_BINFOS (TYPE_BINFO (type)))
-       {
-         ix = TK_CLASS_TYPE;
-         break;
        }
+      else if (!TYPE_BINFO (type)
+              || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)))
+       ix = TK_CLASS_TYPE;
       else
        {
          tree binfo = TYPE_BINFO (type);
-         VEC(tree,gc) *base_accesses = BINFO_BASE_ACCESSES (binfo);
+         vec<tree, va_gc> *base_accesses = BINFO_BASE_ACCESSES (binfo);
          tree base_binfo = BINFO_BASE_BINFO (binfo, 0);
          int num_bases = BINFO_N_BASE_BINFOS (binfo);
 
          if (num_bases == 1
-             && VEC_index (tree, base_accesses, 0) == access_public_node
+             && (*base_accesses)[0] == access_public_node
              && !BINFO_VIRTUAL_P (base_binfo)
              && integer_zerop (BINFO_OFFSET (base_binfo)))
-           {
-             /* single non-virtual public.  */
-             ix = TK_SI_CLASS_TYPE;
-             break;
-           }
+           /* single non-virtual public.  */
+           ix = TK_SI_CLASS_TYPE;
          else
-           {
-             tinfo_s *ti;
-             tree array_domain, base_array;
-
-             ix = TK_FIXED + num_bases;
-             if (VEC_length (tinfo_s, tinfo_descs) <= ix)
-               {
-                 /* too short, extend.  */
-                 unsigned len = VEC_length (tinfo_s, tinfo_descs);
-
-                 VEC_safe_grow (tinfo_s, gc, tinfo_descs, ix + 1);
-                 while (VEC_iterate (tinfo_s, tinfo_descs, len++, ti))
-                   ti->type = ti->vtable = ti->name = NULL_TREE;
-               }
-             else if (VEC_index (tinfo_s, tinfo_descs, ix)->type)
-               /* already created.  */
-               break;
-
-             /* Create the array of __base_class_type_info entries.
-                G++ 3.2 allocated an array that had one too many
-                entries, and then filled that extra entries with
-                zeros.  */
-             if (abi_version_at_least (2))
-               array_domain = build_index_type (size_int (num_bases - 1));
-             else
-               array_domain = build_index_type (size_int (num_bases));
-             base_array =
-               build_array_type (VEC_index (tinfo_s, tinfo_descs,
-                                            TK_BASE_TYPE)->type,
-                                 array_domain);
-
-             push_abi_namespace ();
-             create_pseudo_type_info
-               (ix, "__vmi_class_type_info",
-                build_decl (input_location,
-                            FIELD_DECL, NULL_TREE, integer_type_node),
-                build_decl (input_location,
-                            FIELD_DECL, NULL_TREE, integer_type_node),
-                build_decl (input_location,
-                            FIELD_DECL, NULL_TREE, base_array),
-                NULL);
-             pop_abi_namespace ();
-             break;
-           }
+           ix = TK_VMI_CLASS_TYPES + num_bases - 1;
        }
+      break;
+
     default:
       ix = TK_BUILTIN_TYPE;
       break;
@@ -1328,107 +1288,240 @@ get_pseudo_ti_index (tree type)
   return ix;
 }
 
-/* Make sure the required builtin types exist for generating the type_info
-   variable definitions.  */
+/* Return pointer to tinfo descriptor.  Possibly creating the tinfo
+   descriptor in the first place.  */
 
-static void
-create_tinfo_types (void)
+static tinfo_s *
+get_tinfo_desc (unsigned ix)
 {
-  tinfo_s *ti;
+  unsigned len = tinfo_descs->length ();
 
-  gcc_assert (!tinfo_descs);
+  if (len <= ix)
+    {
+      /* too short, extend.  */
+      len = ix + 1 - len;
+      vec_safe_reserve (tinfo_descs, len);
+      tinfo_s elt;
+      elt.type = elt.vtable = elt.name = NULL_TREE;
+      while (len--)
+       tinfo_descs->quick_push (elt);
+    }
 
-  VEC_safe_grow (tinfo_s, gc, tinfo_descs, TK_FIXED);
+  tinfo_s *res = &(*tinfo_descs)[ix];
 
-  push_abi_namespace ();
+  if (res->type)
+    return res;
 
-  /* Create the internal type_info structure. This is used as a base for
-     the other structures.  */
-  {
-    tree field, fields;
-
-    field = build_decl (BUILTINS_LOCATION,
-                       FIELD_DECL, NULL_TREE, const_ptr_type_node);
-    fields = field;
-
-    field = build_decl (BUILTINS_LOCATION,
-                       FIELD_DECL, NULL_TREE, const_string_type_node);
-    TREE_CHAIN (field) = fields;
-    fields = field;
-
-    ti = VEC_index (tinfo_s, tinfo_descs, TK_TYPE_INFO_TYPE);
-    ti->type = make_class_type (RECORD_TYPE);
-    ti->vtable = NULL_TREE;
-    ti->name = NULL_TREE;
-    finish_builtin_struct (ti->type, "__type_info_pseudo",
-                          fields, NULL_TREE);
-  }
+  /* Ok, we have to create it.  This layout must be consistent with
+     that defined in the runtime support.  We explicitly manage the
+     vtable member, and name it for real type as used in the runtime.
+     The RECORD type has a different name, to avoid collisions.  We
+     have to delay generating the VAR_DECL of the vtable until the end
+     of the translation, when we'll have seen the library definition,
+     if there was one.  */
 
-  /* Fundamental type_info */
-  create_pseudo_type_info (TK_BUILTIN_TYPE, "__fundamental_type_info", NULL);
+  /* Fields to add, chained in reverse order.  */
+  tree fields = NULL_TREE;
 
-  /* Array, function and enum type_info. No additional fields.  */
-  create_pseudo_type_info (TK_ARRAY_TYPE, "__array_type_info", NULL);
-  create_pseudo_type_info (TK_FUNCTION_TYPE, "__function_type_info", NULL);
-  create_pseudo_type_info (TK_ENUMERAL_TYPE, "__enum_type_info", NULL);
+  if (ix >= TK_DERIVED_TYPES)
+    {
+      /* First field is the pseudo type_info base class.  */
+      tree fld_base = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
+                                 get_tinfo_desc (TK_TYPE_INFO_TYPE)->type);
 
-  /* Class type_info.  No additional fields.  */
-  create_pseudo_type_info (TK_CLASS_TYPE, "__class_type_info", NULL);
+      DECL_CHAIN (fld_base) = fields;
+      fields = fld_base;
+    }
 
-  /* Single public non-virtual base class. Add pointer to base class.
-     This is really a descendant of __class_type_info.  */
-  create_pseudo_type_info (TK_SI_CLASS_TYPE, "__si_class_type_info",
-           build_decl (BUILTINS_LOCATION,
-                       FIELD_DECL, NULL_TREE, type_info_ptr_type),
-           NULL);
+  switch (ix)
+    {
+    case TK_TYPE_INFO_TYPE:
+      {
+       tree fld_ptr = build_decl (BUILTINS_LOCATION, FIELD_DECL,
+                                  NULL_TREE, const_ptr_type_node);
+       fields = fld_ptr;
+
+       tree fld_str = build_decl (BUILTINS_LOCATION, FIELD_DECL,
+                                  NULL_TREE, const_string_type_node);
+       DECL_CHAIN (fld_str) = fields;
+       fields = fld_str;
+       break;
+      }
 
-  /* Base class internal helper. Pointer to base type, offset to base,
-     flags.  */
-  {
-    tree field, fields;
+    case TK_BASE_TYPE:
+      {
+       /* Base class internal helper. Pointer to base type, offset to
+          base, flags.  */
+       tree fld_ptr = build_decl (BUILTINS_LOCATION, FIELD_DECL,
+                                  NULL_TREE, type_info_ptr_type);
+       DECL_CHAIN (fld_ptr) = fields;
+       fields = fld_ptr;
+
+       tree fld_flag = build_decl (BUILTINS_LOCATION, FIELD_DECL,
+                                   NULL_TREE, LONGPTR_T);
+       DECL_CHAIN (fld_flag) = fields;
+       fields = fld_flag;
+       break;
+      }
 
-    field = build_decl (BUILTINS_LOCATION,
-                       FIELD_DECL, NULL_TREE, type_info_ptr_type);
-    fields = field;
+    case TK_BUILTIN_TYPE:
+      /* Fundamental type_info */
+      break;
 
-    field = build_decl (BUILTINS_LOCATION,
-                       FIELD_DECL, NULL_TREE, integer_types[itk_long]);
-    TREE_CHAIN (field) = fields;
-    fields = field;
+    case TK_ARRAY_TYPE:
+      break;
 
-    ti = VEC_index (tinfo_s, tinfo_descs, TK_BASE_TYPE);
+    case TK_FUNCTION_TYPE:
+      break;
 
-    ti->type = make_class_type (RECORD_TYPE);
-    ti->vtable = NULL_TREE;
-    ti->name = NULL_TREE;
-    finish_builtin_struct (ti->type, "__base_class_type_info_pseudo",
-                          fields, NULL_TREE);
-  }
+    case TK_ENUMERAL_TYPE:
+      break;
+
+    case TK_POINTER_TYPE:
+    case TK_POINTER_MEMBER_TYPE:
+      {
+       /* Pointer type_info. Adds two fields, qualification mask and
+          pointer to the pointed to type.  This is really a
+          descendant of __pbase_type_info.  */
+       tree fld_mask = build_decl (BUILTINS_LOCATION, FIELD_DECL,
+                                   NULL_TREE, integer_type_node);
+       DECL_CHAIN (fld_mask) = fields;
+       fields = fld_mask;
+
+       tree fld_ptr = build_decl (BUILTINS_LOCATION, FIELD_DECL,
+                                  NULL_TREE, type_info_ptr_type);
+       DECL_CHAIN (fld_ptr) = fields;
+       fields = fld_ptr;
+
+       if (ix == TK_POINTER_MEMBER_TYPE)
+         {
+           /* Add a pointer to the class too.  */
+           tree fld_cls = build_decl (BUILTINS_LOCATION, FIELD_DECL,
+                                  NULL_TREE, type_info_ptr_type);
+           DECL_CHAIN (fld_cls) = fields;
+           fields = fld_cls;
+         }
+       break;
+      }
+
+    case TK_CLASS_TYPE:
+      /* Class type_info.  No additional fields.  */
+      break;
+
+    case TK_SI_CLASS_TYPE:
+      {
+       /* Single public non-virtual base class. Add pointer to base
+          class.  This is really a descendant of
+          __class_type_info.  */
+       tree fld_ptr = build_decl (BUILTINS_LOCATION, FIELD_DECL,
+                                  NULL_TREE, type_info_ptr_type);
+       DECL_CHAIN (fld_ptr) = fields;
+       fields = fld_ptr;
+       break;
+      }
 
-  /* Pointer type_info. Adds two fields, qualification mask
-     and pointer to the pointed to type.  This is really a descendant of
-     __pbase_type_info.  */
-  create_pseudo_type_info (TK_POINTER_TYPE, "__pointer_type_info",
-       build_decl (BUILTINS_LOCATION, 
-                  FIELD_DECL, NULL_TREE, integer_type_node),
-       build_decl (BUILTINS_LOCATION,
-                  FIELD_DECL, NULL_TREE, type_info_ptr_type),
-       NULL);
-
-  /* Pointer to member data type_info.  Add qualifications flags,
-     pointer to the member's type info and pointer to the class.
-     This is really a descendant of __pbase_type_info.  */
-  create_pseudo_type_info (TK_POINTER_MEMBER_TYPE,
-       "__pointer_to_member_type_info",
-       build_decl (BUILTINS_LOCATION,
-                   FIELD_DECL, NULL_TREE, integer_type_node),
-       build_decl (BUILTINS_LOCATION,
-                   FIELD_DECL, NULL_TREE, type_info_ptr_type),
-       build_decl (BUILTINS_LOCATION,
-                   FIELD_DECL, NULL_TREE, type_info_ptr_type),
-       NULL);
+    default: /* Multiple inheritance.  */
+      {
+       unsigned num_bases = ix - TK_VMI_CLASS_TYPES + 1;
+
+       tree fld_flg = build_decl (BUILTINS_LOCATION, FIELD_DECL,
+                                  NULL_TREE, integer_type_node);
+       DECL_CHAIN (fld_flg) = fields;
+       fields = fld_flg;
+       
+       tree fld_cnt = build_decl (BUILTINS_LOCATION, FIELD_DECL,
+                                  NULL_TREE, integer_type_node);
+       DECL_CHAIN (fld_cnt) = fields;
+       fields = fld_cnt;
+
+       /* Create the array of __base_class_type_info entries.  */
+       tree domain = build_index_type (size_int (num_bases - 1));
+       tree array = build_array_type (get_tinfo_desc (TK_BASE_TYPE)->type,
+                                      domain);
+       tree fld_ary = build_decl (BUILTINS_LOCATION, FIELD_DECL,
+                                  NULL_TREE, array);
+       DECL_CHAIN (fld_ary) = fields;
+       fields = fld_ary;
+       break;
+      }
+    }
+
+  push_abi_namespace ();
+
+  /* Generate the pseudo type name.  */
+  const char *real_name = tinfo_names[ix < TK_VMI_CLASS_TYPES
+                                     ? ix : unsigned (TK_VMI_CLASS_TYPES)];
+  size_t name_len = strlen (real_name);
+  char *pseudo_name = (char *) alloca (name_len + 30);
+  memcpy (pseudo_name, real_name, name_len);
+  /* Those >= TK_VMI_CLASS_TYPES need a discriminator, may as well
+     apply it to all.  See get_peudo_tinfo_index where we make use of
+     this.  */
+  sprintf (pseudo_name + name_len, "_pseudo_%d", ix);
+
+  /* Create the pseudo type.  */
+  tree pseudo_type = make_class_type (RECORD_TYPE);
+  /* Pass the fields chained in reverse.  */
+  finish_builtin_struct (pseudo_type, pseudo_name, fields, NULL_TREE);
+  CLASSTYPE_AS_BASE (pseudo_type) = pseudo_type;
+
+  res->type = cp_build_qualified_type (pseudo_type, TYPE_QUAL_CONST);
+  res->name = get_identifier (real_name);
+
+  /* Pretend this is public so determine_visibility doesn't give vtables
+     internal linkage.  */
+  TREE_PUBLIC (TYPE_MAIN_DECL (res->type)) = 1;
 
   pop_abi_namespace ();
+  return res;
+}
+
+/* We lazily create the type info types.  */
+
+static void
+create_tinfo_types (void)
+{
+  gcc_assert (!tinfo_descs);
+
+  vec_alloc (tinfo_descs, TK_MAX + 20);
+}
+
+/* Helper for emit_support_tinfos. Emits the type_info descriptor of
+   a single type.  */
+
+void
+emit_support_tinfo_1 (tree bltn)
+{
+  tree types[3];
+
+  if (bltn == NULL_TREE)
+    return;
+  types[0] = bltn;
+  types[1] = build_pointer_type (bltn);
+  types[2] = build_pointer_type (cp_build_qualified_type (bltn,
+                                                         TYPE_QUAL_CONST));
+
+  for (int i = 0; i < 3; ++i)
+    {
+      tree tinfo = get_tinfo_decl (types[i]);
+      TREE_USED (tinfo) = 1;
+      mark_needed (tinfo);
+      /* The C++ ABI requires that these objects be COMDAT.  But,
+        On systems without weak symbols, initialized COMDAT
+        objects are emitted with internal linkage.  (See
+        comdat_linkage for details.)  Since we want these objects
+        to have external linkage so that copies do not have to be
+        emitted in code outside the runtime library, we make them
+        non-COMDAT here.  
+
+        It might also not be necessary to follow this detail of the
+        ABI.  */
+      if (!flag_weak || ! targetm.cxx.library_rtti_comdat ())
+       {
+         gcc_assert (TREE_PUBLIC (tinfo) && !DECL_COMDAT (tinfo));
+         DECL_INTERFACE_KNOWN (tinfo) = 1;
+       }
+    }
 }
 
 /* Emit the type_info descriptors which are guaranteed to be in the runtime
@@ -1440,11 +1533,13 @@ create_tinfo_types (void)
 void
 emit_support_tinfos (void)
 {
+  /* Dummy static variable so we can put nullptr in the array; it will be
+     set before we actually start to walk the array.  */
   static tree *const fundamentals[] =
   {
     &void_type_node,
     &boolean_type_node,
-    &wchar_type_node, &char16_type_node, &char32_type_node,
+    &wchar_type_node, &char8_type_node, &char16_type_node, &char32_type_node,
     &char_type_node, &signed_char_type_node, &unsigned_char_type_node,
     &short_integer_type_node, &short_unsigned_type_node,
     &integer_type_node, &unsigned_type_node,
@@ -1452,57 +1547,39 @@ emit_support_tinfos (void)
     &long_long_integer_type_node, &long_long_unsigned_type_node,
     &float_type_node, &double_type_node, &long_double_type_node,
     &dfloat32_type_node, &dfloat64_type_node, &dfloat128_type_node,
+    &nullptr_type_node,
     0
   };
   int ix;
-  tree bltn_type, dtor;
 
-  push_abi_namespace ();
-  bltn_type = xref_tag (class_type,
-                       get_identifier ("__fundamental_type_info"),
-                       /*tag_scope=*/ts_current, false);
-  pop_abi_namespace ();
+  /* Look for a defined class.  */
+  tree bltn_type = lookup_qualified_name
+    (abi_node, get_identifier ("__fundamental_type_info"), true, false, false);
+  if (TREE_CODE (bltn_type) != TYPE_DECL)
+    return;
+
+  bltn_type = TREE_TYPE (bltn_type);
   if (!COMPLETE_TYPE_P (bltn_type))
     return;
-  dtor = CLASSTYPE_DESTRUCTORS (bltn_type);
+  tree dtor = CLASSTYPE_DESTRUCTOR (bltn_type);
   if (!dtor || DECL_EXTERNAL (dtor))
     return;
+
+  /* All these are really builtins.  So set the location.  */
+  location_t saved_loc = input_location;
+  input_location = BUILTINS_LOCATION;
   doing_runtime = 1;
   for (ix = 0; fundamentals[ix]; ix++)
-    {
-      tree bltn = *fundamentals[ix];
-      tree types[3];
-      int i;
-
-      types[0] = bltn;
-      types[1] = build_pointer_type (bltn);
-      types[2] = build_pointer_type (build_qualified_type (bltn,
-                                                          TYPE_QUAL_CONST));
-
-      for (i = 0; i < 3; ++i)
-       {
-         tree tinfo;
-
-         tinfo = get_tinfo_decl (types[i]);
-         TREE_USED (tinfo) = 1;
-         mark_needed (tinfo);
-         /* The C++ ABI requires that these objects be COMDAT.  But,
-            On systems without weak symbols, initialized COMDAT
-            objects are emitted with internal linkage.  (See
-            comdat_linkage for details.)  Since we want these objects
-            to have external linkage so that copies do not have to be
-            emitted in code outside the runtime library, we make them
-            non-COMDAT here.  
-
-            It might also not be necessary to follow this detail of the
-            ABI.  */
-         if (!flag_weak || ! targetm.cxx.library_rtti_comdat ())
-           {
-             gcc_assert (TREE_PUBLIC (tinfo) && !DECL_COMDAT (tinfo));
-             DECL_INTERFACE_KNOWN (tinfo) = 1;
-           }
-       }
-    }
+    emit_support_tinfo_1 (*fundamentals[ix]);
+  for (ix = 0; ix < NUM_INT_N_ENTS; ix ++)
+    if (int_n_enabled_p[ix])
+      {
+       emit_support_tinfo_1 (int_n_trees[ix].signed_type);
+       emit_support_tinfo_1 (int_n_trees[ix].unsigned_type);
+      }
+  for (tree t = registered_builtin_types; t; t = TREE_CHAIN (t))
+    emit_support_tinfo_1 (TREE_VALUE (t));
+  input_location = saved_loc;
 }
 
 /* Finish a type info decl. DECL_PTR is a pointer to an unemitted
@@ -1553,6 +1630,12 @@ emit_tinfo_decl (tree decl)
       DECL_INITIAL (decl) = init;
       mark_used (decl);
       cp_finish_decl (decl, init, false, NULL_TREE, 0);
+      /* Avoid targets optionally bumping up the alignment to improve
+        vector instruction accesses, tinfo are never accessed this way.  */
+#ifdef DATA_ABI_ALIGNMENT
+      SET_DECL_ALIGN (decl, DATA_ABI_ALIGNMENT (decl, TYPE_ALIGN (TREE_TYPE (decl))));
+      DECL_USER_ALIGN (decl) = true;
+#endif
       return true;
     }
   else