OSDN Git Service

PR c++/50089
[pf3gnuchains/gcc-fork.git] / gcc / cp / rtti.c
index 9a7faec..434b772 100644 (file)
@@ -1,6 +1,6 @@
 /* RunTime Type Identification
    Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
-   2005, 2006, 2007, 2008, 2009
+   2005, 2006, 2007, 2008, 2009, 2010
    Free Software Foundation, Inc.
    Mostly written by Jason Merrill (jason@cygnus.com).
 
@@ -29,11 +29,9 @@ along with GCC; see the file COPYING3.  If not see
 #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 "c-family/c-pragma.h"
 
 /* C++ returns type information to the user in struct type_info
    objects. We also use type information to implement dynamic_cast and
@@ -116,7 +114,7 @@ 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 tree class_initializer (tinfo_s *, tree, unsigned, ...);
 static void create_pseudo_type_info (int, const char *, ...);
 static tree get_pseudo_ti_init (tree, unsigned);
 static unsigned get_pseudo_ti_index (tree);
@@ -155,7 +153,7 @@ 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);
@@ -192,10 +190,9 @@ build_headof (tree exp)
                                                   tf_warning_or_error), 
                            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...
@@ -207,8 +204,8 @@ 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));
+    fn = push_throw_library_fn (fn, build_function_type_list (ptr_type_node,
+                                                             NULL_TREE));
 
   return build_cxx_call (fn, 0, NULL);
 }
@@ -225,7 +222,7 @@ throw_bad_typeid (void)
       tree t;
 
       t = build_reference_type (const_type_info_type_node);
-      t = build_function_type (t, void_list_node);
+      t = build_function_type_list (t, NULL_TREE);
       fn = push_throw_library_fn (fn, t);
     }
 
@@ -255,7 +252,8 @@ get_tinfo_decl_dynamic (tree exp)
   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)
+  if (CLASS_TYPE_P (type) || type == unknown_type_node
+      || type == init_list_type_node)
     type = complete_type_or_else (type, exp);
 
   if (!type)
@@ -327,6 +325,8 @@ build_typeid (tree exp)
   if (processing_template_decl)
     return build_min (TYPEID_EXPR, const_type_info_type_node, exp);
 
+  /* FIXME when integrating with c_fully_fold, mark
+     resolves_to_fixed_type_p case as a non-constant expression.  */
   if (TREE_CODE (exp) == INDIRECT_REF
       && TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == POINTER_TYPE
       && TYPE_POLYMORPHIC_P (TREE_TYPE (exp))
@@ -405,6 +405,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))
@@ -482,7 +484,8 @@ get_typeid (tree type)
   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)
+  if (CLASS_TYPE_P (type) || type == unknown_type_node
+      || type == init_list_type_node)
     type = complete_type_or_else (type, NULL_TREE);
 
   if (!type)
@@ -691,10 +694,10 @@ build_dynamic_cast_1 (tree type, tree expr, tsubst_flags_t complain)
          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);
+         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);
+         td3 = cp_build_addr_expr (td3, complain);
 
          /* Determine how T and V are related.  */
          boff = dcast_base_hint (static_type, target_type);
@@ -704,7 +707,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;
@@ -724,15 +727,13 @@ 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);
+             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);
              DECL_PURE_P (dcast_fn) = 1;
              pop_abi_namespace ();
@@ -860,16 +861,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,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
@@ -912,12 +914,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)));
@@ -925,14 +926,13 @@ tinfo_base_init (tinfo_s *ti, tree target)
       ti->vtable = vtable_ptr;
     }
 
-  init = tree_cons (NULL_TREE, vtable_ptr, init);
-
-  init = tree_cons (NULL_TREE, decay_conversion (name_decl), init);
+  v = VEC_alloc (constructor_elt, gc, 2);
+  CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, vtable_ptr);
+  CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, decay_conversion (name_decl));
 
-  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;
 }
@@ -946,7 +946,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;
@@ -963,15 +963,16 @@ 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,gc) *v = VEC_alloc (constructor_elt, gc, 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);
+  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;
@@ -990,20 +991,19 @@ 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,gc) *v = VEC_alloc (constructor_elt, gc, 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;
@@ -1011,15 +1011,23 @@ 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);
+  va_list extra_inits;
+  unsigned i;
+  VEC(constructor_elt,gc) *v = VEC_alloc (constructor_elt, gc, n+1);
 
-  TREE_CHAIN (init) = trail;
-  init = build_constructor_from_list (init_list_type_node, init);
+  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;
@@ -1047,6 +1055,9 @@ typeinfo_in_lib_p (tree type)
     case NULLPTR_TYPE:
       return true;
 
+    case LANG_TYPE:
+      /* fall through.  */
+
     default:
       return false;
     }
@@ -1076,17 +1087,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);
+       return class_initializer (ti, type, 1, tinfo);
       }
 
     default:
@@ -1099,17 +1109,21 @@ get_pseudo_ti_init (tree type, unsigned tk_index)
        tree offset_type = integer_types[itk_long];
        tree base_inits = NULL_TREE;
        int ix;
+       VEC(constructor_elt,gc) *init_vec = NULL;
+       constructor_elt *e;
 
        gcc_assert (tk_index >= TK_FIXED);
 
+       VEC_safe_grow (constructor_elt, gc, init_vec, nbases);
        /* Generate the base information initializer.  */
        for (ix = nbases; ix--;)
          {
            tree base_binfo = BINFO_BASE_BINFO (binfo, ix);
-           tree base_init = NULL_TREE;
+           tree base_init;
            int flags = 0;
            tree tinfo;
            tree offset;
+           VEC(constructor_elt,gc) *v;
 
            if (VEC_index (tree, base_accesses, ix) == access_public_node)
              flags |= 2;
@@ -1132,25 +1146,22 @@ 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);
+           v = VEC_alloc (constructor_elt, gc, 2);
+           CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, tinfo);
+           CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, offset);
+           base_init = build_constructor (init_list_type_node, v);
+           e = VEC_index (constructor_elt, 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);
+       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);
+       return class_initializer (ti, type, 3,
+                                 build_int_cst (NULL_TREE, hint),
+                                 build_int_cst (NULL_TREE, nbases),
+                                 base_inits);
       }
     }
 }
@@ -1198,7 +1209,7 @@ create_pseudo_type_info (int tk, const char *real_name, ...)
   /* Now add the derived fields.  */
   while ((field_decl = va_arg (ap, tree)))
     {
-      TREE_CHAIN (field_decl) = fields;
+      DECL_CHAIN (field_decl) = fields;
       fields = field_decl;
     }
 
@@ -1363,7 +1374,7 @@ create_tinfo_types (void)
 
     field = build_decl (BUILTINS_LOCATION,
                        FIELD_DECL, NULL_TREE, const_string_type_node);
-    TREE_CHAIN (field) = fields;
+    DECL_CHAIN (field) = fields;
     fields = field;
 
     ti = VEC_index (tinfo_s, tinfo_descs, TK_TYPE_INFO_TYPE);
@@ -1403,7 +1414,7 @@ create_tinfo_types (void)
 
     field = build_decl (BUILTINS_LOCATION,
                        FIELD_DECL, NULL_TREE, integer_types[itk_long]);
-    TREE_CHAIN (field) = fields;
+    DECL_CHAIN (field) = fields;
     fields = field;
 
     ti = VEC_index (tinfo_s, tinfo_descs, TK_BASE_TYPE);
@@ -1452,7 +1463,6 @@ 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 nullptr_type_node;
   static tree *const fundamentals[] =
   {
     &void_type_node,
@@ -1463,6 +1473,7 @@ emit_support_tinfos (void)
     &integer_type_node, &unsigned_type_node,
     &long_integer_type_node, &long_unsigned_type_node,
     &long_long_integer_type_node, &long_long_unsigned_type_node,
+    &int128_integer_type_node, &int128_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,
@@ -1482,17 +1493,18 @@ emit_support_tinfos (void)
   if (!dtor || DECL_EXTERNAL (dtor))
     return;
   doing_runtime = 1;
-  nullptr_type_node = TREE_TYPE (nullptr_node);
   for (ix = 0; fundamentals[ix]; ix++)
     {
       tree bltn = *fundamentals[ix];
       tree types[3];
       int i;
 
+      if (bltn == NULL_TREE)
+       continue;
       types[0] = bltn;
       types[1] = build_pointer_type (bltn);
-      types[2] = build_pointer_type (build_qualified_type (bltn,
-                                                          TYPE_QUAL_CONST));
+      types[2] = build_pointer_type (cp_build_qualified_type (bltn,
+                                                             TYPE_QUAL_CONST));
 
       for (i = 0; i < 3; ++i)
        {