OSDN Git Service

/gcc/cp
[pf3gnuchains/gcc-fork.git] / gcc / cp / rtti.c
1 /* RunTime Type Identification
2    Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3    2005, 2006, 2007, 2008, 2009
4    Free Software Foundation, Inc.
5    Mostly written by Jason Merrill (jason@cygnus.com).
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
13
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3.  If not see
21 <http://www.gnu.org/licenses/>.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "tree.h"
28 #include "cp-tree.h"
29 #include "flags.h"
30 #include "output.h"
31 #include "assert.h"
32 #include "toplev.h"
33 #include "convert.h"
34 #include "target.h"
35 #include "c-pragma.h"
36
37 /* C++ returns type information to the user in struct type_info
38    objects. We also use type information to implement dynamic_cast and
39    exception handlers. Type information for a particular type is
40    indicated with an ABI defined structure derived from type_info.
41    This would all be very straight forward, but for the fact that the
42    runtime library provides the definitions of the type_info structure
43    and the ABI defined derived classes. We cannot build declarations
44    of them directly in the compiler, but we need to layout objects of
45    their type.  Somewhere we have to lie.
46
47    We define layout compatible POD-structs with compiler-defined names
48    and generate the appropriate initializations for them (complete
49    with explicit mention of their vtable). When we have to provide a
50    type_info to the user we reinterpret_cast the internal compiler
51    type to type_info.  A well formed program can only explicitly refer
52    to the type_infos of complete types (& cv void).  However, we chain
53    pointer type_infos to the pointed-to-type, and that can be
54    incomplete.  We only need the addresses of such incomplete
55    type_info objects for static initialization.
56
57    The type information VAR_DECL of a type is held on the
58    IDENTIFIER_GLOBAL_VALUE of the type's mangled name. That VAR_DECL
59    will be the internal type.  It will usually have the correct
60    internal type reflecting the kind of type it represents (pointer,
61    array, function, class, inherited class, etc).  When the type it
62    represents is incomplete, it will have the internal type
63    corresponding to type_info.  That will only happen at the end of
64    translation, when we are emitting the type info objects.  */
65
66 /* Auxiliary data we hold for each type_info derived object we need.  */
67 typedef struct GTY (()) tinfo_s {
68   tree type;  /* The RECORD_TYPE for this type_info object */
69
70   tree vtable; /* The VAR_DECL of the vtable.  Only filled at end of
71                   translation.  */
72
73   tree name;  /* IDENTIFIER_NODE for the ABI specified name of
74                  the type_info derived type.  */
75 } tinfo_s;
76
77 DEF_VEC_O(tinfo_s);
78 DEF_VEC_ALLOC_O(tinfo_s,gc);
79
80 typedef enum tinfo_kind
81 {
82   TK_TYPE_INFO_TYPE,    /* abi::__type_info_pseudo */
83   TK_BASE_TYPE,         /* abi::__base_class_type_info */
84   TK_BUILTIN_TYPE,      /* abi::__fundamental_type_info */
85   TK_ARRAY_TYPE,        /* abi::__array_type_info */
86   TK_FUNCTION_TYPE,     /* abi::__function_type_info */
87   TK_ENUMERAL_TYPE,     /* abi::__enum_type_info */
88   TK_POINTER_TYPE,      /* abi::__pointer_type_info */
89   TK_POINTER_MEMBER_TYPE, /* abi::__pointer_to_member_type_info */
90   TK_CLASS_TYPE,        /* abi::__class_type_info */
91   TK_SI_CLASS_TYPE,     /* abi::__si_class_type_info */
92   TK_FIXED              /* end of fixed descriptors. */
93   /* ...                   abi::__vmi_type_info<I> */
94 } tinfo_kind;
95
96 /* A vector of all tinfo decls that haven't yet been emitted.  */
97 VEC(tree,gc) *unemitted_tinfo_decls;
98
99 /* A vector of all type_info derived types we need.  The first few are
100    fixed and created early. The remainder are for multiple inheritance
101    and are generated as needed. */
102 static GTY (()) VEC(tinfo_s,gc) *tinfo_descs;
103
104 static tree ifnonnull (tree, tree);
105 static tree tinfo_name (tree, bool);
106 static tree build_dynamic_cast_1 (tree, tree, tsubst_flags_t);
107 static tree throw_bad_cast (void);
108 static tree throw_bad_typeid (void);
109 static tree get_tinfo_decl_dynamic (tree);
110 static tree get_tinfo_ptr (tree);
111 static bool typeid_ok_p (void);
112 static int qualifier_flags (tree);
113 static bool target_incomplete_p (tree);
114 static tree tinfo_base_init (tinfo_s *, tree);
115 static tree generic_initializer (tinfo_s *, tree);
116 static tree ptr_initializer (tinfo_s *, tree);
117 static tree ptm_initializer (tinfo_s *, tree);
118 static tree class_initializer (tinfo_s *, tree, tree);
119 static void create_pseudo_type_info (int, const char *, ...);
120 static tree get_pseudo_ti_init (tree, unsigned);
121 static unsigned get_pseudo_ti_index (tree);
122 static void create_tinfo_types (void);
123 static bool typeinfo_in_lib_p (tree);
124
125 static int doing_runtime = 0;
126 \f
127 static void
128 push_abi_namespace (void)
129 {
130   push_nested_namespace (abi_node);
131   push_visibility ("default");
132 }
133
134 static void
135 pop_abi_namespace (void)
136 {
137   pop_visibility ();
138   pop_nested_namespace (abi_node);
139 }
140
141 /* Declare language defined type_info type and a pointer to const
142    type_info.  This is incomplete here, and will be completed when
143    the user #includes <typeinfo>.  There are language defined
144    restrictions on what can be done until that is included.  Create
145    the internal versions of the ABI types.  */
146
147 void
148 init_rtti_processing (void)
149 {
150   tree type_info_type;
151
152   push_namespace (std_identifier);
153   type_info_type = xref_tag (class_type, get_identifier ("type_info"),
154                              /*tag_scope=*/ts_current, false);
155   pop_namespace ();
156   const_type_info_type_node
157     = build_qualified_type (type_info_type, TYPE_QUAL_CONST);
158   type_info_ptr_type = build_pointer_type (const_type_info_type_node);
159
160   unemitted_tinfo_decls = VEC_alloc (tree, gc, 124);
161
162   create_tinfo_types ();
163 }
164
165 /* Given the expression EXP of type `class *', return the head of the
166    object pointed to by EXP with type cv void*, if the class has any
167    virtual functions (TYPE_POLYMORPHIC_P), else just return the
168    expression.  */
169
170 tree
171 build_headof (tree exp)
172 {
173   tree type = TREE_TYPE (exp);
174   tree offset;
175   tree index;
176
177   gcc_assert (TREE_CODE (type) == POINTER_TYPE);
178   type = TREE_TYPE (type);
179
180   if (!TYPE_POLYMORPHIC_P (type))
181     return exp;
182
183   /* We use this a couple of times below, protect it.  */
184   exp = save_expr (exp);
185
186   /* The offset-to-top field is at index -2 from the vptr.  */
187   index = build_int_cst (NULL_TREE,
188                          -2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
189
190   offset = build_vtbl_ref (cp_build_indirect_ref (exp, NULL, 
191                                                   tf_warning_or_error), 
192                            index);
193
194   type = build_qualified_type (ptr_type_node,
195                                cp_type_quals (TREE_TYPE (exp)));
196   return build2 (POINTER_PLUS_EXPR, type, exp,
197                  convert_to_integer (sizetype, offset));
198 }
199
200 /* Get a bad_cast node for the program to throw...
201
202    See libstdc++/exception.cc for __throw_bad_cast */
203
204 static tree
205 throw_bad_cast (void)
206 {
207   tree fn = get_identifier ("__cxa_bad_cast");
208   if (!get_global_value_if_present (fn, &fn))
209     fn = push_throw_library_fn (fn, build_function_type (ptr_type_node,
210                                                          void_list_node));
211
212   return build_cxx_call (fn, 0, NULL);
213 }
214
215 /* Return an expression for "__cxa_bad_typeid()".  The expression
216    returned is an lvalue of type "const std::type_info".  */
217
218 static tree
219 throw_bad_typeid (void)
220 {
221   tree fn = get_identifier ("__cxa_bad_typeid");
222   if (!get_global_value_if_present (fn, &fn))
223     {
224       tree t;
225
226       t = build_reference_type (const_type_info_type_node);
227       t = build_function_type (t, void_list_node);
228       fn = push_throw_library_fn (fn, t);
229     }
230
231   return build_cxx_call (fn, 0, NULL);
232 }
233 \f
234 /* Return an lvalue expression whose type is "const std::type_info"
235    and whose value indicates the type of the expression EXP.  If EXP
236    is a reference to a polymorphic class, return the dynamic type;
237    otherwise return the static type of the expression.  */
238
239 static tree
240 get_tinfo_decl_dynamic (tree exp)
241 {
242   tree type;
243   tree t;
244
245   if (error_operand_p (exp))
246     return error_mark_node;
247
248   /* peel back references, so they match.  */
249   type = non_reference (TREE_TYPE (exp));
250
251   /* Peel off cv qualifiers.  */
252   type = TYPE_MAIN_VARIANT (type);
253
254   /* For UNKNOWN_TYPEs call complete_type_or_else to get diagnostics.  */
255   if (CLASS_TYPE_P (type) || TREE_CODE (type) == UNKNOWN_TYPE)
256     type = complete_type_or_else (type, exp);
257
258   if (!type)
259     return error_mark_node;
260
261   /* If exp is a reference to polymorphic type, get the real type_info.  */
262   if (TYPE_POLYMORPHIC_P (type) && ! resolves_to_fixed_type_p (exp, 0))
263     {
264       /* build reference to type_info from vtable.  */
265       tree index;
266
267       /* The RTTI information is at index -1.  */
268       index = build_int_cst (NULL_TREE,
269                              -1 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
270       t = build_vtbl_ref (exp, index);
271       t = convert (type_info_ptr_type, t);
272     }
273   else
274     /* Otherwise return the type_info for the static type of the expr.  */
275     t = get_tinfo_ptr (TYPE_MAIN_VARIANT (type));
276
277   return cp_build_indirect_ref (t, NULL, tf_warning_or_error);
278 }
279
280 static bool
281 typeid_ok_p (void)
282 {
283   tree pseudo_type_info, type_info_type;
284
285   if (! flag_rtti)
286     {
287       error ("cannot use typeid with -fno-rtti");
288       return false;
289     }
290
291   if (!COMPLETE_TYPE_P (const_type_info_type_node))
292     {
293       error ("must #include <typeinfo> before using typeid");
294       return false;
295     }
296
297   pseudo_type_info
298     = VEC_index (tinfo_s, tinfo_descs, TK_TYPE_INFO_TYPE)->type;
299   type_info_type = TYPE_MAIN_VARIANT (const_type_info_type_node);
300
301   /* Make sure abi::__type_info_pseudo has the same alias set
302      as std::type_info.  */
303   if (! TYPE_ALIAS_SET_KNOWN_P (pseudo_type_info))
304     TYPE_ALIAS_SET (pseudo_type_info) = get_alias_set (type_info_type);
305   else
306     gcc_assert (TYPE_ALIAS_SET (pseudo_type_info)
307                 == get_alias_set (type_info_type));
308
309   return true;
310 }
311
312 /* Return an expression for "typeid(EXP)".  The expression returned is
313    an lvalue of type "const std::type_info".  */
314
315 tree
316 build_typeid (tree exp)
317 {
318   tree cond = NULL_TREE;
319   int nonnull = 0;
320
321   if (exp == error_mark_node || !typeid_ok_p ())
322     return error_mark_node;
323
324   if (processing_template_decl)
325     return build_min (TYPEID_EXPR, const_type_info_type_node, exp);
326
327   if (TREE_CODE (exp) == INDIRECT_REF
328       && TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == POINTER_TYPE
329       && TYPE_POLYMORPHIC_P (TREE_TYPE (exp))
330       && ! resolves_to_fixed_type_p (exp, &nonnull)
331       && ! nonnull)
332     {
333       exp = stabilize_reference (exp);
334       cond = cp_convert (boolean_type_node, TREE_OPERAND (exp, 0));
335     }
336
337   exp = get_tinfo_decl_dynamic (exp);
338
339   if (exp == error_mark_node)
340     return error_mark_node;
341
342   if (cond)
343     {
344       tree bad = throw_bad_typeid ();
345
346       exp = build3 (COND_EXPR, TREE_TYPE (exp), cond, exp, bad);
347     }
348
349   return exp;
350 }
351
352 /* Generate the NTBS name of a type.  If MARK_PRIVATE, put a '*' in front so that
353    comparisons will be done by pointer rather than string comparison.  */
354 static tree
355 tinfo_name (tree type, bool mark_private)
356 {
357   const char *name;
358   int length;
359   tree name_string;
360
361   name = mangle_type_string (type);
362   length = strlen (name);
363
364   if (mark_private)
365     {
366       /* Inject '*' at beginning of name to force pointer comparison.  */
367       char* buf = (char*) XALLOCAVEC (char, length + 1);
368       buf[0] = '*';
369       memcpy (buf + 1, name, length);
370       name_string = build_string (length + 1, buf);
371     }
372   else
373     name_string = build_string (length + 1, name);
374
375   return fix_string_type (name_string);
376 }
377
378 /* Return a VAR_DECL for the internal ABI defined type_info object for
379    TYPE. You must arrange that the decl is mark_used, if actually use
380    it --- decls in vtables are only used if the vtable is output.  */
381
382 tree
383 get_tinfo_decl (tree type)
384 {
385   tree name;
386   tree d;
387
388   if (variably_modified_type_p (type, /*fn=*/NULL_TREE))
389     {
390       error ("cannot create type information for type %qT because "
391              "it involves types of variable size",
392              type);
393       return error_mark_node;
394     }
395
396   if (TREE_CODE (type) == METHOD_TYPE)
397     type = build_function_type (TREE_TYPE (type),
398                                 TREE_CHAIN (TYPE_ARG_TYPES (type)));
399
400   /* For a class type, the variable is cached in the type node
401      itself.  */
402   if (CLASS_TYPE_P (type))
403     {
404       d = CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type));
405       if (d)
406         return d;
407     }
408
409   name = mangle_typeinfo_for_type (type);
410
411   d = IDENTIFIER_GLOBAL_VALUE (name);
412   if (!d)
413     {
414       int ix = get_pseudo_ti_index (type);
415       tinfo_s *ti = VEC_index (tinfo_s, tinfo_descs, ix);
416
417       d = build_lang_decl (VAR_DECL, name, ti->type);
418       SET_DECL_ASSEMBLER_NAME (d, name);
419       /* Remember the type it is for.  */
420       TREE_TYPE (name) = type;
421       DECL_TINFO_P (d) = 1;
422       DECL_ARTIFICIAL (d) = 1;
423       DECL_IGNORED_P (d) = 1;
424       TREE_READONLY (d) = 1;
425       TREE_STATIC (d) = 1;
426       /* Mark the variable as undefined -- but remember that we can
427          define it later if we need to do so.  */
428       DECL_EXTERNAL (d) = 1;
429       DECL_NOT_REALLY_EXTERN (d) = 1;
430       set_linkage_according_to_type (type, d);
431
432       d = pushdecl_top_level_and_finish (d, NULL_TREE);
433       if (CLASS_TYPE_P (type))
434         CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type)) = d;
435
436       /* Add decl to the global array of tinfo decls.  */
437       VEC_safe_push (tree, gc, unemitted_tinfo_decls, d);
438     }
439
440   return d;
441 }
442
443 /* Return a pointer to a type_info object describing TYPE, suitably
444    cast to the language defined type.  */
445
446 static tree
447 get_tinfo_ptr (tree type)
448 {
449   tree decl = get_tinfo_decl (type);
450
451   mark_used (decl);
452   return build_nop (type_info_ptr_type,
453                     build_address (decl));
454 }
455
456 /* Return the type_info object for TYPE.  */
457
458 tree
459 get_typeid (tree type)
460 {
461   if (type == error_mark_node || !typeid_ok_p ())
462     return error_mark_node;
463
464   if (processing_template_decl)
465     return build_min (TYPEID_EXPR, const_type_info_type_node, type);
466
467   /* If the type of the type-id is a reference type, the result of the
468      typeid expression refers to a type_info object representing the
469      referenced type.  */
470   type = non_reference (type);
471
472   /* The top-level cv-qualifiers of the lvalue expression or the type-id
473      that is the operand of typeid are always ignored.  */
474   type = TYPE_MAIN_VARIANT (type);
475
476   /* For UNKNOWN_TYPEs call complete_type_or_else to get diagnostics.  */
477   if (CLASS_TYPE_P (type) || TREE_CODE (type) == UNKNOWN_TYPE)
478     type = complete_type_or_else (type, NULL_TREE);
479
480   if (!type)
481     return error_mark_node;
482
483   return cp_build_indirect_ref (get_tinfo_ptr (type), NULL, 
484                                 tf_warning_or_error);
485 }
486
487 /* Check whether TEST is null before returning RESULT.  If TEST is used in
488    RESULT, it must have previously had a save_expr applied to it.  */
489
490 static tree
491 ifnonnull (tree test, tree result)
492 {
493   return build3 (COND_EXPR, TREE_TYPE (result),
494                  build2 (EQ_EXPR, boolean_type_node, test,
495                          cp_convert (TREE_TYPE (test), integer_zero_node)),
496                  cp_convert (TREE_TYPE (result), integer_zero_node),
497                  result);
498 }
499
500 /* Execute a dynamic cast, as described in section 5.2.6 of the 9/93 working
501    paper.  */
502
503 static tree
504 build_dynamic_cast_1 (tree type, tree expr, tsubst_flags_t complain)
505 {
506   enum tree_code tc = TREE_CODE (type);
507   tree exprtype = TREE_TYPE (expr);
508   tree dcast_fn;
509   tree old_expr = expr;
510   const char *errstr = NULL;
511
512   /* Save casted types in the function's used types hash table.  */
513   used_types_insert (type);
514
515   /* T shall be a pointer or reference to a complete class type, or
516      `pointer to cv void''.  */
517   switch (tc)
518     {
519     case POINTER_TYPE:
520       if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
521         break;
522       /* Fall through.  */
523     case REFERENCE_TYPE:
524       if (! MAYBE_CLASS_TYPE_P (TREE_TYPE (type)))
525         {
526           errstr = "target is not pointer or reference to class";
527           goto fail;
528         }
529       if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
530         {
531           errstr = "target is not pointer or reference to complete type";
532           goto fail;
533         }
534       break;
535
536     default:
537       errstr = "target is not pointer or reference";
538       goto fail;
539     }
540
541   if (tc == POINTER_TYPE)
542     {
543       /* If T is a pointer type, v shall be an rvalue of a pointer to
544          complete class type, and the result is an rvalue of type T.  */
545
546       if (TREE_CODE (exprtype) != POINTER_TYPE)
547         {
548           errstr = "source is not a pointer";
549           goto fail;
550         }
551       if (! MAYBE_CLASS_TYPE_P (TREE_TYPE (exprtype)))
552         {
553           errstr = "source is not a pointer to class";
554           goto fail;
555         }
556       if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
557         {
558           errstr = "source is a pointer to incomplete type";
559           goto fail;
560         }
561     }
562   else
563     {
564       exprtype = build_reference_type (exprtype);
565
566       /* T is a reference type, v shall be an lvalue of a complete class
567          type, and the result is an lvalue of the type referred to by T.  */
568
569       if (! MAYBE_CLASS_TYPE_P (TREE_TYPE (exprtype)))
570         {
571           errstr = "source is not of class type";
572           goto fail;
573         }
574       if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
575         {
576           errstr = "source is of incomplete class type";
577           goto fail;
578         }
579
580       /* Apply trivial conversion T -> T& for dereferenced ptrs.  */
581       expr = convert_to_reference (exprtype, expr, CONV_IMPLICIT,
582                                    LOOKUP_NORMAL, NULL_TREE);
583     }
584
585   /* The dynamic_cast operator shall not cast away constness.  */
586   if (!at_least_as_qualified_p (TREE_TYPE (type),
587                                 TREE_TYPE (exprtype)))
588     {
589       errstr = "conversion casts away constness";
590       goto fail;
591     }
592
593   /* If *type is an unambiguous accessible base class of *exprtype,
594      convert statically.  */
595   {
596     tree binfo;
597
598     binfo = lookup_base (TREE_TYPE (exprtype), TREE_TYPE (type),
599                          ba_check, NULL);
600
601     if (binfo)
602       {
603         expr = build_base_path (PLUS_EXPR, convert_from_reference (expr),
604                                 binfo, 0);
605         if (TREE_CODE (exprtype) == POINTER_TYPE)
606           expr = rvalue (expr);
607         return expr;
608       }
609   }
610
611   /* Otherwise *exprtype must be a polymorphic class (have a vtbl).  */
612   if (TYPE_POLYMORPHIC_P (TREE_TYPE (exprtype)))
613     {
614       tree expr1;
615       /* if TYPE is `void *', return pointer to complete object.  */
616       if (tc == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (type)))
617         {
618           /* if b is an object, dynamic_cast<void *>(&b) == (void *)&b.  */
619           if (TREE_CODE (expr) == ADDR_EXPR
620               && TREE_CODE (TREE_OPERAND (expr, 0)) == VAR_DECL
621               && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == RECORD_TYPE)
622             return build1 (NOP_EXPR, type, expr);
623
624           /* Since expr is used twice below, save it.  */
625           expr = save_expr (expr);
626
627           expr1 = build_headof (expr);
628           if (TREE_TYPE (expr1) != type)
629             expr1 = build1 (NOP_EXPR, type, expr1);
630           return ifnonnull (expr, expr1);
631         }
632       else
633         {
634           tree retval;
635           tree result, td2, td3;
636           tree elems[4];
637           tree static_type, target_type, boff;
638
639           /* If we got here, we can't convert statically.  Therefore,
640              dynamic_cast<D&>(b) (b an object) cannot succeed.  */
641           if (tc == REFERENCE_TYPE)
642             {
643               if (TREE_CODE (old_expr) == VAR_DECL
644                   && TREE_CODE (TREE_TYPE (old_expr)) == RECORD_TYPE)
645                 {
646                   tree expr = throw_bad_cast ();
647                   if (complain & tf_warning)
648                     warning (0, "dynamic_cast of %q#D to %q#T can never succeed",
649                              old_expr, type);
650                   /* Bash it to the expected type.  */
651                   TREE_TYPE (expr) = type;
652                   return expr;
653                 }
654             }
655           /* Ditto for dynamic_cast<D*>(&b).  */
656           else if (TREE_CODE (expr) == ADDR_EXPR)
657             {
658               tree op = TREE_OPERAND (expr, 0);
659               if (TREE_CODE (op) == VAR_DECL
660                   && TREE_CODE (TREE_TYPE (op)) == RECORD_TYPE)
661                 {
662                   if (complain & tf_warning)
663                     warning (0, "dynamic_cast of %q#D to %q#T can never succeed",
664                              op, type);
665                   retval = build_int_cst (type, 0);
666                   return retval;
667                 }
668             }
669
670           /* Use of dynamic_cast when -fno-rtti is prohibited.  */
671           if (!flag_rtti)
672             {
673               if (complain & tf_error)
674                 error ("%<dynamic_cast%> not permitted with -fno-rtti");
675               return error_mark_node;
676             }
677
678           target_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
679           static_type = TYPE_MAIN_VARIANT (TREE_TYPE (exprtype));
680           td2 = get_tinfo_decl (target_type);
681           mark_used (td2);
682           td2 = cp_build_unary_op (ADDR_EXPR, td2, 0, complain);
683           td3 = get_tinfo_decl (static_type);
684           mark_used (td3);
685           td3 = cp_build_unary_op (ADDR_EXPR, td3, 0, complain);
686
687           /* Determine how T and V are related.  */
688           boff = dcast_base_hint (static_type, target_type);
689
690           /* Since expr is used twice below, save it.  */
691           expr = save_expr (expr);
692
693           expr1 = expr;
694           if (tc == REFERENCE_TYPE)
695             expr1 = cp_build_unary_op (ADDR_EXPR, expr1, 0, complain);
696
697           elems[0] = expr1;
698           elems[1] = td3;
699           elems[2] = td2;
700           elems[3] = boff;
701
702           dcast_fn = dynamic_cast_node;
703           if (!dcast_fn)
704             {
705               tree tmp;
706               tree tinfo_ptr;
707               const char *name;
708
709               push_abi_namespace ();
710               tinfo_ptr = xref_tag (class_type,
711                                     get_identifier ("__class_type_info"),
712                                     /*tag_scope=*/ts_current, false);
713
714               tinfo_ptr = build_pointer_type
715                 (build_qualified_type
716                  (tinfo_ptr, TYPE_QUAL_CONST));
717               name = "__dynamic_cast";
718               tmp = tree_cons
719                 (NULL_TREE, const_ptr_type_node, tree_cons
720                  (NULL_TREE, tinfo_ptr, tree_cons
721                   (NULL_TREE, tinfo_ptr, tree_cons
722                    (NULL_TREE, ptrdiff_type_node, void_list_node))));
723               tmp = build_function_type (ptr_type_node, tmp);
724               dcast_fn = build_library_fn_ptr (name, tmp);
725               DECL_PURE_P (dcast_fn) = 1;
726               pop_abi_namespace ();
727               dynamic_cast_node = dcast_fn;
728             }
729           result = build_cxx_call (dcast_fn, 4, elems);
730
731           if (tc == REFERENCE_TYPE)
732             {
733               tree bad = throw_bad_cast ();
734               tree neq;
735
736               result = save_expr (result);
737               neq = c_common_truthvalue_conversion (input_location, result);
738               return cp_convert (type,
739                                  build3 (COND_EXPR, TREE_TYPE (result),
740                                          neq, result, bad));
741             }
742
743           /* Now back to the type we want from a void*.  */
744           result = cp_convert (type, result);
745           return ifnonnull (expr, result);
746         }
747     }
748   else
749     errstr = "source type is not polymorphic";
750
751  fail:
752   if (complain & tf_error)
753     error ("cannot dynamic_cast %qE (of type %q#T) to type %q#T (%s)",
754            expr, exprtype, type, errstr);
755   return error_mark_node;
756 }
757
758 tree
759 build_dynamic_cast (tree type, tree expr, tsubst_flags_t complain)
760 {
761   if (type == error_mark_node || expr == error_mark_node)
762     return error_mark_node;
763
764   if (processing_template_decl)
765     {
766       expr = build_min (DYNAMIC_CAST_EXPR, type, expr);
767       TREE_SIDE_EFFECTS (expr) = 1;
768       return convert_from_reference (expr);
769     }
770
771   return convert_from_reference (build_dynamic_cast_1 (type, expr, complain));
772 }
773 \f
774 /* Return the runtime bit mask encoding the qualifiers of TYPE.  */
775
776 static int
777 qualifier_flags (tree type)
778 {
779   int flags = 0;
780   int quals = cp_type_quals (type);
781
782   if (quals & TYPE_QUAL_CONST)
783     flags |= 1;
784   if (quals & TYPE_QUAL_VOLATILE)
785     flags |= 2;
786   if (quals & TYPE_QUAL_RESTRICT)
787     flags |= 4;
788   return flags;
789 }
790
791 /* Return true, if the pointer chain TYPE ends at an incomplete type, or
792    contains a pointer to member of an incomplete class.  */
793
794 static bool
795 target_incomplete_p (tree type)
796 {
797   while (true)
798     if (TYPE_PTRMEM_P (type))
799       {
800         if (!COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type)))
801           return true;
802         type = TYPE_PTRMEM_POINTED_TO_TYPE (type);
803       }
804     else if (TREE_CODE (type) == POINTER_TYPE)
805       type = TREE_TYPE (type);
806     else
807       return !COMPLETE_OR_VOID_TYPE_P (type);
808 }
809
810 /* Returns true if TYPE involves an incomplete class type; in that
811    case, typeinfo variables for TYPE should be emitted with internal
812    linkage.  */
813
814 static bool
815 involves_incomplete_p (tree type)
816 {
817   switch (TREE_CODE (type))
818     {
819     case POINTER_TYPE:
820       return target_incomplete_p (TREE_TYPE (type));
821
822     case OFFSET_TYPE:
823     ptrmem:
824       return
825         (target_incomplete_p (TYPE_PTRMEM_POINTED_TO_TYPE (type))
826          || !COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type)));
827
828     case RECORD_TYPE:
829       if (TYPE_PTRMEMFUNC_P (type))
830         goto ptrmem;
831       /* Fall through.  */
832     case UNION_TYPE:
833       if (!COMPLETE_TYPE_P (type))
834         return true;
835
836     default:
837       /* All other types do not involve incomplete class types.  */
838       return false;
839     }
840 }
841
842 /* Return a CONSTRUCTOR for the common part of the type_info objects. This
843    is the vtable pointer and NTBS name.  The NTBS name is emitted as a
844    comdat const char array, so it becomes a unique key for the type. Generate
845    and emit that VAR_DECL here.  (We can't always emit the type_info itself
846    as comdat, because of pointers to incomplete.) */
847
848 static tree
849 tinfo_base_init (tinfo_s *ti, tree target)
850 {
851   tree init = NULL_TREE;
852   tree name_decl;
853   tree vtable_ptr;
854
855   {
856     tree name_name, name_string;
857
858     /* Generate the NTBS array variable.  */
859     tree name_type = build_cplus_array_type
860                      (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
861                      NULL_TREE);
862
863     /* Determine the name of the variable -- and remember with which
864        type it is associated.  */
865     name_name = mangle_typeinfo_string_for_type (target);
866     TREE_TYPE (name_name) = target;
867
868     name_decl = build_lang_decl (VAR_DECL, name_name, name_type);
869     SET_DECL_ASSEMBLER_NAME (name_decl, name_name);
870     DECL_ARTIFICIAL (name_decl) = 1;
871     DECL_IGNORED_P (name_decl) = 1;
872     TREE_READONLY (name_decl) = 1;
873     TREE_STATIC (name_decl) = 1;
874     DECL_EXTERNAL (name_decl) = 0;
875     DECL_TINFO_P (name_decl) = 1;
876     set_linkage_according_to_type (target, name_decl);
877     import_export_decl (name_decl);
878     name_string = tinfo_name (target, !TREE_PUBLIC (name_decl));
879     DECL_INITIAL (name_decl) = name_string;
880     mark_used (name_decl);
881     pushdecl_top_level_and_finish (name_decl, name_string);
882   }
883
884   vtable_ptr = ti->vtable;
885   if (!vtable_ptr)
886     {
887       tree real_type;
888       push_abi_namespace ();
889       real_type = xref_tag (class_type, ti->name,
890                             /*tag_scope=*/ts_current, false);
891       pop_abi_namespace ();
892
893       if (!COMPLETE_TYPE_P (real_type))
894         {
895           /* We never saw a definition of this type, so we need to
896              tell the compiler that this is an exported class, as
897              indeed all of the __*_type_info classes are.  */
898           SET_CLASSTYPE_INTERFACE_KNOWN (real_type);
899           CLASSTYPE_INTERFACE_ONLY (real_type) = 1;
900         }
901
902       vtable_ptr = get_vtable_decl (real_type, /*complete=*/1);
903       vtable_ptr = cp_build_unary_op (ADDR_EXPR, vtable_ptr, 0, 
904                                    tf_warning_or_error);
905
906       /* We need to point into the middle of the vtable.  */
907       vtable_ptr = build2
908         (POINTER_PLUS_EXPR, TREE_TYPE (vtable_ptr), vtable_ptr,
909          size_binop (MULT_EXPR,
910                      size_int (2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE),
911                      TYPE_SIZE_UNIT (vtable_entry_type)));
912
913       ti->vtable = vtable_ptr;
914     }
915
916   init = tree_cons (NULL_TREE, vtable_ptr, init);
917
918   init = tree_cons (NULL_TREE, decay_conversion (name_decl), init);
919
920   init = build_constructor_from_list (init_list_type_node, nreverse (init));
921   TREE_CONSTANT (init) = 1;
922   TREE_STATIC (init) = 1;
923   init = tree_cons (NULL_TREE, init, NULL_TREE);
924
925   return init;
926 }
927
928 /* Return the CONSTRUCTOR expr for a type_info of TYPE. TI provides the
929    information about the particular type_info derivation, which adds no
930    additional fields to the type_info base.  */
931
932 static tree
933 generic_initializer (tinfo_s *ti, tree target)
934 {
935   tree init = tinfo_base_init (ti, target);
936
937   init = build_constructor_from_list (init_list_type_node, init);
938   TREE_CONSTANT (init) = 1;
939   TREE_STATIC (init) = 1;
940   return init;
941 }
942
943 /* Return the CONSTRUCTOR expr for a type_info of pointer TYPE.
944    TI provides information about the particular type_info derivation,
945    which adds target type and qualifier flags members to the type_info base.  */
946
947 static tree
948 ptr_initializer (tinfo_s *ti, tree target)
949 {
950   tree init = tinfo_base_init (ti, target);
951   tree to = TREE_TYPE (target);
952   int flags = qualifier_flags (to);
953   bool incomplete = target_incomplete_p (to);
954
955   if (incomplete)
956     flags |= 8;
957   init = tree_cons (NULL_TREE, build_int_cst (NULL_TREE, flags), init);
958   init = tree_cons (NULL_TREE,
959                     get_tinfo_ptr (TYPE_MAIN_VARIANT (to)),
960                     init);
961
962   init = build_constructor_from_list (init_list_type_node, nreverse (init));
963   TREE_CONSTANT (init) = 1;
964   TREE_STATIC (init) = 1;
965   return init;
966 }
967
968 /* Return the CONSTRUCTOR expr for a type_info of pointer to member data TYPE.
969    TI provides information about the particular type_info derivation,
970    which adds class, target type and qualifier flags members to the type_info
971    base.  */
972
973 static tree
974 ptm_initializer (tinfo_s *ti, tree target)
975 {
976   tree init = tinfo_base_init (ti, target);
977   tree to = TYPE_PTRMEM_POINTED_TO_TYPE (target);
978   tree klass = TYPE_PTRMEM_CLASS_TYPE (target);
979   int flags = qualifier_flags (to);
980   bool incomplete = target_incomplete_p (to);
981
982   if (incomplete)
983     flags |= 0x8;
984   if (!COMPLETE_TYPE_P (klass))
985     flags |= 0x10;
986   init = tree_cons (NULL_TREE, build_int_cst (NULL_TREE, flags), init);
987   init = tree_cons (NULL_TREE,
988                     get_tinfo_ptr (TYPE_MAIN_VARIANT (to)),
989                     init);
990   init = tree_cons (NULL_TREE,
991                     get_tinfo_ptr (klass),
992                     init);
993
994   init = build_constructor_from_list (init_list_type_node, nreverse (init));
995   TREE_CONSTANT (init) = 1;
996   TREE_STATIC (init) = 1;
997   return init;
998 }
999
1000 /* Return the CONSTRUCTOR expr for a type_info of class TYPE.
1001    TI provides information about the particular __class_type_info derivation,
1002    which adds hint flags and TRAIL initializers to the type_info base.  */
1003
1004 static tree
1005 class_initializer (tinfo_s *ti, tree target, tree trail)
1006 {
1007   tree init = tinfo_base_init (ti, target);
1008
1009   TREE_CHAIN (init) = trail;
1010   init = build_constructor_from_list (init_list_type_node, init);
1011   TREE_CONSTANT (init) = 1;
1012   TREE_STATIC (init) = 1;
1013   return init;
1014 }
1015
1016 /* Returns true if the typeinfo for type should be placed in
1017    the runtime library.  */
1018
1019 static bool
1020 typeinfo_in_lib_p (tree type)
1021 {
1022   /* The typeinfo objects for `T*' and `const T*' are in the runtime
1023      library for simple types T.  */
1024   if (TREE_CODE (type) == POINTER_TYPE
1025       && (cp_type_quals (TREE_TYPE (type)) == TYPE_QUAL_CONST
1026           || cp_type_quals (TREE_TYPE (type)) == TYPE_UNQUALIFIED))
1027     type = TREE_TYPE (type);
1028
1029   switch (TREE_CODE (type))
1030     {
1031     case INTEGER_TYPE:
1032     case BOOLEAN_TYPE:
1033     case REAL_TYPE:
1034     case VOID_TYPE:
1035       return true;
1036
1037     default:
1038       return false;
1039     }
1040 }
1041
1042 /* Generate the initializer for the type info describing TYPE.  TK_INDEX is
1043    the index of the descriptor in the tinfo_desc vector. */
1044
1045 static tree
1046 get_pseudo_ti_init (tree type, unsigned tk_index)
1047 {
1048   tinfo_s *ti = VEC_index (tinfo_s, tinfo_descs, tk_index);
1049
1050   gcc_assert (at_eof);
1051   switch (tk_index)
1052     {
1053     case TK_POINTER_MEMBER_TYPE:
1054       return ptm_initializer (ti, type);
1055
1056     case TK_POINTER_TYPE:
1057       return ptr_initializer (ti, type);
1058
1059     case TK_BUILTIN_TYPE:
1060     case TK_ENUMERAL_TYPE:
1061     case TK_FUNCTION_TYPE:
1062     case TK_ARRAY_TYPE:
1063       return generic_initializer (ti, type);
1064
1065     case TK_CLASS_TYPE:
1066       return class_initializer (ti, type, NULL_TREE);
1067
1068     case TK_SI_CLASS_TYPE:
1069       {
1070         tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), 0);
1071         tree tinfo = get_tinfo_ptr (BINFO_TYPE (base_binfo));
1072         tree base_inits = tree_cons (NULL_TREE, tinfo, NULL_TREE);
1073
1074         /* get_tinfo_ptr might have reallocated the tinfo_descs vector.  */
1075         ti = VEC_index (tinfo_s, tinfo_descs, tk_index);
1076         return class_initializer (ti, type, base_inits);
1077       }
1078
1079     default:
1080       {
1081         int hint = ((CLASSTYPE_REPEATED_BASE_P (type) << 0)
1082                     | (CLASSTYPE_DIAMOND_SHAPED_P (type) << 1));
1083         tree binfo = TYPE_BINFO (type);
1084         int nbases = BINFO_N_BASE_BINFOS (binfo);
1085         VEC(tree,gc) *base_accesses = BINFO_BASE_ACCESSES (binfo);
1086         tree offset_type = integer_types[itk_long];
1087         tree base_inits = NULL_TREE;
1088         int ix;
1089
1090         gcc_assert (tk_index >= TK_FIXED);
1091
1092         /* Generate the base information initializer.  */
1093         for (ix = nbases; ix--;)
1094           {
1095             tree base_binfo = BINFO_BASE_BINFO (binfo, ix);
1096             tree base_init = NULL_TREE;
1097             int flags = 0;
1098             tree tinfo;
1099             tree offset;
1100
1101             if (VEC_index (tree, base_accesses, ix) == access_public_node)
1102               flags |= 2;
1103             tinfo = get_tinfo_ptr (BINFO_TYPE (base_binfo));
1104             if (BINFO_VIRTUAL_P (base_binfo))
1105               {
1106                 /* We store the vtable offset at which the virtual
1107                    base offset can be found.  */
1108                 offset = BINFO_VPTR_FIELD (base_binfo);
1109                 flags |= 1;
1110               }
1111             else
1112               offset = BINFO_OFFSET (base_binfo);
1113
1114             /* Combine offset and flags into one field.  */
1115             offset = fold_convert (offset_type, offset);
1116             offset = fold_build2_loc (input_location,
1117                                   LSHIFT_EXPR, offset_type, offset,
1118                                   build_int_cst (offset_type, 8));
1119             offset = fold_build2_loc (input_location,
1120                                   BIT_IOR_EXPR, offset_type, offset,
1121                                   build_int_cst (offset_type, flags));
1122             base_init = tree_cons (NULL_TREE, offset, base_init);
1123             base_init = tree_cons (NULL_TREE, tinfo, base_init);
1124             base_init = build_constructor_from_list (init_list_type_node, base_init);
1125             base_inits = tree_cons (NULL_TREE, base_init, base_inits);
1126           }
1127         base_inits = build_constructor_from_list (init_list_type_node, base_inits);
1128         base_inits = tree_cons (NULL_TREE, base_inits, NULL_TREE);
1129         /* Prepend the number of bases.  */
1130         base_inits = tree_cons (NULL_TREE,
1131                                 build_int_cst (NULL_TREE, nbases),
1132                                 base_inits);
1133         /* Prepend the hint flags.  */
1134         base_inits = tree_cons (NULL_TREE,
1135                                 build_int_cst (NULL_TREE, hint),
1136                                 base_inits);
1137
1138         /* get_tinfo_ptr might have reallocated the tinfo_descs vector.  */
1139         ti = VEC_index (tinfo_s, tinfo_descs, tk_index);
1140         return class_initializer (ti, type, base_inits);
1141       }
1142     }
1143 }
1144
1145 /* Generate the RECORD_TYPE containing the data layout of a type_info
1146    derivative as used by the runtime. This layout must be consistent with
1147    that defined in the runtime support. Also generate the VAR_DECL for the
1148    type's vtable. We explicitly manage the vtable member, and name it for
1149    real type as used in the runtime. The RECORD type has a different name,
1150    to avoid collisions.  Return a TREE_LIST who's TINFO_PSEUDO_TYPE
1151    is the generated type and TINFO_VTABLE_NAME is the name of the
1152    vtable.  We have to delay generating the VAR_DECL of the vtable
1153    until the end of the translation, when we'll have seen the library
1154    definition, if there was one.
1155
1156    REAL_NAME is the runtime's name of the type. Trailing arguments are
1157    additional FIELD_DECL's for the structure. The final argument must be
1158    NULL.  */
1159
1160 static void
1161 create_pseudo_type_info (int tk, const char *real_name, ...)
1162 {
1163   tinfo_s *ti;
1164   tree pseudo_type;
1165   char *pseudo_name;
1166   tree fields;
1167   tree field_decl;
1168   va_list ap;
1169
1170   va_start (ap, real_name);
1171
1172   /* Generate the pseudo type name.  */
1173   pseudo_name = (char *) alloca (strlen (real_name) + 30);
1174   strcpy (pseudo_name, real_name);
1175   strcat (pseudo_name, "_pseudo");
1176   if (tk >= TK_FIXED)
1177     sprintf (pseudo_name + strlen (pseudo_name), "%d", tk - TK_FIXED);
1178
1179   /* First field is the pseudo type_info base class.  */
1180   fields = build_decl (input_location,
1181                        FIELD_DECL, NULL_TREE,
1182                        VEC_index (tinfo_s, tinfo_descs,
1183                                   TK_TYPE_INFO_TYPE)->type);
1184
1185   /* Now add the derived fields.  */
1186   while ((field_decl = va_arg (ap, tree)))
1187     {
1188       TREE_CHAIN (field_decl) = fields;
1189       fields = field_decl;
1190     }
1191
1192   /* Create the pseudo type.  */
1193   pseudo_type = make_class_type (RECORD_TYPE);
1194   finish_builtin_struct (pseudo_type, pseudo_name, fields, NULL_TREE);
1195   CLASSTYPE_AS_BASE (pseudo_type) = pseudo_type;
1196
1197   ti = VEC_index (tinfo_s, tinfo_descs, tk);
1198   ti->type = cp_build_qualified_type (pseudo_type, TYPE_QUAL_CONST);
1199   ti->name = get_identifier (real_name);
1200   ti->vtable = NULL_TREE;
1201
1202   /* Pretend this is public so determine_visibility doesn't give vtables
1203      internal linkage.  */
1204   TREE_PUBLIC (TYPE_MAIN_DECL (ti->type)) = 1;
1205
1206   va_end (ap);
1207 }
1208
1209 /* Return the index of a pseudo type info type node used to describe
1210    TYPE.  TYPE must be a complete type (or cv void), except at the end
1211    of the translation unit.  */
1212
1213 static unsigned
1214 get_pseudo_ti_index (tree type)
1215 {
1216   unsigned ix;
1217
1218   switch (TREE_CODE (type))
1219     {
1220     case OFFSET_TYPE:
1221       ix = TK_POINTER_MEMBER_TYPE;
1222       break;
1223
1224     case POINTER_TYPE:
1225       ix = TK_POINTER_TYPE;
1226       break;
1227
1228     case ENUMERAL_TYPE:
1229       ix = TK_ENUMERAL_TYPE;
1230       break;
1231
1232     case FUNCTION_TYPE:
1233       ix = TK_FUNCTION_TYPE;
1234       break;
1235
1236     case ARRAY_TYPE:
1237       ix = TK_ARRAY_TYPE;
1238       break;
1239
1240     case UNION_TYPE:
1241     case RECORD_TYPE:
1242       if (TYPE_PTRMEMFUNC_P (type))
1243         {
1244           ix = TK_POINTER_MEMBER_TYPE;
1245           break;
1246         }
1247       else if (!COMPLETE_TYPE_P (type))
1248         {
1249           if (!at_eof)
1250             cxx_incomplete_type_error (NULL_TREE, type);
1251           ix = TK_CLASS_TYPE;
1252           break;
1253         }
1254       else if (!BINFO_N_BASE_BINFOS (TYPE_BINFO (type)))
1255         {
1256           ix = TK_CLASS_TYPE;
1257           break;
1258         }
1259       else
1260         {
1261           tree binfo = TYPE_BINFO (type);
1262           VEC(tree,gc) *base_accesses = BINFO_BASE_ACCESSES (binfo);
1263           tree base_binfo = BINFO_BASE_BINFO (binfo, 0);
1264           int num_bases = BINFO_N_BASE_BINFOS (binfo);
1265
1266           if (num_bases == 1
1267               && VEC_index (tree, base_accesses, 0) == access_public_node
1268               && !BINFO_VIRTUAL_P (base_binfo)
1269               && integer_zerop (BINFO_OFFSET (base_binfo)))
1270             {
1271               /* single non-virtual public.  */
1272               ix = TK_SI_CLASS_TYPE;
1273               break;
1274             }
1275           else
1276             {
1277               tinfo_s *ti;
1278               tree array_domain, base_array;
1279
1280               ix = TK_FIXED + num_bases;
1281               if (VEC_length (tinfo_s, tinfo_descs) <= ix)
1282                 {
1283                   /* too short, extend.  */
1284                   unsigned len = VEC_length (tinfo_s, tinfo_descs);
1285
1286                   VEC_safe_grow (tinfo_s, gc, tinfo_descs, ix + 1);
1287                   while (VEC_iterate (tinfo_s, tinfo_descs, len++, ti))
1288                     ti->type = ti->vtable = ti->name = NULL_TREE;
1289                 }
1290               else if (VEC_index (tinfo_s, tinfo_descs, ix)->type)
1291                 /* already created.  */
1292                 break;
1293
1294               /* Create the array of __base_class_type_info entries.
1295                  G++ 3.2 allocated an array that had one too many
1296                  entries, and then filled that extra entries with
1297                  zeros.  */
1298               if (abi_version_at_least (2))
1299                 array_domain = build_index_type (size_int (num_bases - 1));
1300               else
1301                 array_domain = build_index_type (size_int (num_bases));
1302               base_array =
1303                 build_array_type (VEC_index (tinfo_s, tinfo_descs,
1304                                              TK_BASE_TYPE)->type,
1305                                   array_domain);
1306
1307               push_abi_namespace ();
1308               create_pseudo_type_info
1309                 (ix, "__vmi_class_type_info",
1310                  build_decl (input_location,
1311                              FIELD_DECL, NULL_TREE, integer_type_node),
1312                  build_decl (input_location,
1313                              FIELD_DECL, NULL_TREE, integer_type_node),
1314                  build_decl (input_location,
1315                              FIELD_DECL, NULL_TREE, base_array),
1316                  NULL);
1317               pop_abi_namespace ();
1318               break;
1319             }
1320         }
1321     default:
1322       ix = TK_BUILTIN_TYPE;
1323       break;
1324     }
1325   return ix;
1326 }
1327
1328 /* Make sure the required builtin types exist for generating the type_info
1329    variable definitions.  */
1330
1331 static void
1332 create_tinfo_types (void)
1333 {
1334   tinfo_s *ti;
1335
1336   gcc_assert (!tinfo_descs);
1337
1338   VEC_safe_grow (tinfo_s, gc, tinfo_descs, TK_FIXED);
1339
1340   push_abi_namespace ();
1341
1342   /* Create the internal type_info structure. This is used as a base for
1343      the other structures.  */
1344   {
1345     tree field, fields;
1346
1347     field = build_decl (BUILTINS_LOCATION,
1348                         FIELD_DECL, NULL_TREE, const_ptr_type_node);
1349     fields = field;
1350
1351     field = build_decl (BUILTINS_LOCATION,
1352                         FIELD_DECL, NULL_TREE, const_string_type_node);
1353     TREE_CHAIN (field) = fields;
1354     fields = field;
1355
1356     ti = VEC_index (tinfo_s, tinfo_descs, TK_TYPE_INFO_TYPE);
1357     ti->type = make_class_type (RECORD_TYPE);
1358     ti->vtable = NULL_TREE;
1359     ti->name = NULL_TREE;
1360     finish_builtin_struct (ti->type, "__type_info_pseudo",
1361                            fields, NULL_TREE);
1362   }
1363
1364   /* Fundamental type_info */
1365   create_pseudo_type_info (TK_BUILTIN_TYPE, "__fundamental_type_info", NULL);
1366
1367   /* Array, function and enum type_info. No additional fields.  */
1368   create_pseudo_type_info (TK_ARRAY_TYPE, "__array_type_info", NULL);
1369   create_pseudo_type_info (TK_FUNCTION_TYPE, "__function_type_info", NULL);
1370   create_pseudo_type_info (TK_ENUMERAL_TYPE, "__enum_type_info", NULL);
1371
1372   /* Class type_info.  No additional fields.  */
1373   create_pseudo_type_info (TK_CLASS_TYPE, "__class_type_info", NULL);
1374
1375   /* Single public non-virtual base class. Add pointer to base class.
1376      This is really a descendant of __class_type_info.  */
1377   create_pseudo_type_info (TK_SI_CLASS_TYPE, "__si_class_type_info",
1378             build_decl (BUILTINS_LOCATION,
1379                         FIELD_DECL, NULL_TREE, type_info_ptr_type),
1380             NULL);
1381
1382   /* Base class internal helper. Pointer to base type, offset to base,
1383      flags.  */
1384   {
1385     tree field, fields;
1386
1387     field = build_decl (BUILTINS_LOCATION,
1388                         FIELD_DECL, NULL_TREE, type_info_ptr_type);
1389     fields = field;
1390
1391     field = build_decl (BUILTINS_LOCATION,
1392                         FIELD_DECL, NULL_TREE, integer_types[itk_long]);
1393     TREE_CHAIN (field) = fields;
1394     fields = field;
1395
1396     ti = VEC_index (tinfo_s, tinfo_descs, TK_BASE_TYPE);
1397
1398     ti->type = make_class_type (RECORD_TYPE);
1399     ti->vtable = NULL_TREE;
1400     ti->name = NULL_TREE;
1401     finish_builtin_struct (ti->type, "__base_class_type_info_pseudo",
1402                            fields, NULL_TREE);
1403   }
1404
1405   /* Pointer type_info. Adds two fields, qualification mask
1406      and pointer to the pointed to type.  This is really a descendant of
1407      __pbase_type_info.  */
1408   create_pseudo_type_info (TK_POINTER_TYPE, "__pointer_type_info",
1409        build_decl (BUILTINS_LOCATION, 
1410                    FIELD_DECL, NULL_TREE, integer_type_node),
1411        build_decl (BUILTINS_LOCATION,
1412                    FIELD_DECL, NULL_TREE, type_info_ptr_type),
1413        NULL);
1414
1415   /* Pointer to member data type_info.  Add qualifications flags,
1416      pointer to the member's type info and pointer to the class.
1417      This is really a descendant of __pbase_type_info.  */
1418   create_pseudo_type_info (TK_POINTER_MEMBER_TYPE,
1419        "__pointer_to_member_type_info",
1420         build_decl (BUILTINS_LOCATION,
1421                     FIELD_DECL, NULL_TREE, integer_type_node),
1422         build_decl (BUILTINS_LOCATION,
1423                     FIELD_DECL, NULL_TREE, type_info_ptr_type),
1424         build_decl (BUILTINS_LOCATION,
1425                     FIELD_DECL, NULL_TREE, type_info_ptr_type),
1426         NULL);
1427
1428   pop_abi_namespace ();
1429 }
1430
1431 /* Emit the type_info descriptors which are guaranteed to be in the runtime
1432    support.  Generating them here guarantees consistency with the other
1433    structures.  We use the following heuristic to determine when the runtime
1434    is being generated.  If std::__fundamental_type_info is defined, and its
1435    destructor is defined, then the runtime is being built.  */
1436
1437 void
1438 emit_support_tinfos (void)
1439 {
1440   static tree *const fundamentals[] =
1441   {
1442     &void_type_node,
1443     &boolean_type_node,
1444     &wchar_type_node, &char16_type_node, &char32_type_node,
1445     &char_type_node, &signed_char_type_node, &unsigned_char_type_node,
1446     &short_integer_type_node, &short_unsigned_type_node,
1447     &integer_type_node, &unsigned_type_node,
1448     &long_integer_type_node, &long_unsigned_type_node,
1449     &long_long_integer_type_node, &long_long_unsigned_type_node,
1450     &float_type_node, &double_type_node, &long_double_type_node,
1451     0
1452   };
1453   int ix;
1454   tree bltn_type, dtor;
1455
1456   push_abi_namespace ();
1457   bltn_type = xref_tag (class_type,
1458                         get_identifier ("__fundamental_type_info"),
1459                         /*tag_scope=*/ts_current, false);
1460   pop_abi_namespace ();
1461   if (!COMPLETE_TYPE_P (bltn_type))
1462     return;
1463   dtor = CLASSTYPE_DESTRUCTORS (bltn_type);
1464   if (!dtor || DECL_EXTERNAL (dtor))
1465     return;
1466   doing_runtime = 1;
1467   for (ix = 0; fundamentals[ix]; ix++)
1468     {
1469       tree bltn = *fundamentals[ix];
1470       tree types[3];
1471       int i;
1472
1473       types[0] = bltn;
1474       types[1] = build_pointer_type (bltn);
1475       types[2] = build_pointer_type (build_qualified_type (bltn,
1476                                                            TYPE_QUAL_CONST));
1477
1478       for (i = 0; i < 3; ++i)
1479         {
1480           tree tinfo;
1481
1482           tinfo = get_tinfo_decl (types[i]);
1483           TREE_USED (tinfo) = 1;
1484           mark_needed (tinfo);
1485           /* The C++ ABI requires that these objects be COMDAT.  But,
1486              On systems without weak symbols, initialized COMDAT
1487              objects are emitted with internal linkage.  (See
1488              comdat_linkage for details.)  Since we want these objects
1489              to have external linkage so that copies do not have to be
1490              emitted in code outside the runtime library, we make them
1491              non-COMDAT here.  
1492
1493              It might also not be necessary to follow this detail of the
1494              ABI.  */
1495           if (!flag_weak || ! targetm.cxx.library_rtti_comdat ())
1496             {
1497               gcc_assert (TREE_PUBLIC (tinfo) && !DECL_COMDAT (tinfo));
1498               DECL_INTERFACE_KNOWN (tinfo) = 1;
1499             }
1500         }
1501     }
1502 }
1503
1504 /* Finish a type info decl. DECL_PTR is a pointer to an unemitted
1505    tinfo decl.  Determine whether it needs emitting, and if so
1506    generate the initializer.  */
1507
1508 bool
1509 emit_tinfo_decl (tree decl)
1510 {
1511   tree type = TREE_TYPE (DECL_NAME (decl));
1512   int in_library = typeinfo_in_lib_p (type);
1513
1514   gcc_assert (DECL_TINFO_P (decl));
1515
1516   if (in_library)
1517     {
1518       if (doing_runtime)
1519         DECL_EXTERNAL (decl) = 0;
1520       else
1521         {
1522           /* If we're not in the runtime, then DECL (which is already
1523              DECL_EXTERNAL) will not be defined here.  */
1524           DECL_INTERFACE_KNOWN (decl) = 1;
1525           return false;
1526         }
1527     }
1528   else if (involves_incomplete_p (type))
1529     {
1530       if (!decl_needed_p (decl))
1531         return false;
1532       /* If TYPE involves an incomplete class type, then the typeinfo
1533          object will be emitted with internal linkage.  There is no
1534          way to know whether or not types are incomplete until the end
1535          of the compilation, so this determination must be deferred
1536          until this point.  */
1537       TREE_PUBLIC (decl) = 0;
1538       DECL_EXTERNAL (decl) = 0;
1539       DECL_INTERFACE_KNOWN (decl) = 1;
1540     }
1541
1542   import_export_decl (decl);
1543   if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
1544     {
1545       tree init;
1546
1547       DECL_EXTERNAL (decl) = 0;
1548       init = get_pseudo_ti_init (type, get_pseudo_ti_index (type));
1549       DECL_INITIAL (decl) = init;
1550       mark_used (decl);
1551       cp_finish_decl (decl, init, false, NULL_TREE, 0);
1552       return true;
1553     }
1554   else
1555     return false;
1556 }
1557
1558 #include "gt-cp-rtti.h"