OSDN Git Service

2009-08-14 Douglas B Rupp <rupp@gnat.com>
[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);
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.  */
353 static tree
354 tinfo_name (tree type)
355 {
356   const char *name;
357   tree name_string;
358
359   name = mangle_type_string_for_rtti (type);
360   name_string = fix_string_type (build_string (strlen (name) + 1, name));
361   return name_string;
362 }
363
364 /* Return a VAR_DECL for the internal ABI defined type_info object for
365    TYPE. You must arrange that the decl is mark_used, if actually use
366    it --- decls in vtables are only used if the vtable is output.  */
367
368 tree
369 get_tinfo_decl (tree type)
370 {
371   tree name;
372   tree d;
373
374   if (variably_modified_type_p (type, /*fn=*/NULL_TREE))
375     {
376       error ("cannot create type information for type %qT because "
377              "it involves types of variable size",
378              type);
379       return error_mark_node;
380     }
381
382   if (TREE_CODE (type) == METHOD_TYPE)
383     type = build_function_type (TREE_TYPE (type),
384                                 TREE_CHAIN (TYPE_ARG_TYPES (type)));
385
386   /* For a class type, the variable is cached in the type node
387      itself.  */
388   if (CLASS_TYPE_P (type))
389     {
390       d = CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type));
391       if (d)
392         return d;
393     }
394
395   name = mangle_typeinfo_for_type (type);
396
397   d = IDENTIFIER_GLOBAL_VALUE (name);
398   if (!d)
399     {
400       int ix = get_pseudo_ti_index (type);
401       tinfo_s *ti = VEC_index (tinfo_s, tinfo_descs, ix);
402
403       d = build_lang_decl (VAR_DECL, name, ti->type);
404       SET_DECL_ASSEMBLER_NAME (d, name);
405       /* Remember the type it is for.  */
406       TREE_TYPE (name) = type;
407       DECL_TINFO_P (d) = 1;
408       DECL_ARTIFICIAL (d) = 1;
409       DECL_IGNORED_P (d) = 1;
410       TREE_READONLY (d) = 1;
411       TREE_STATIC (d) = 1;
412       /* Mark the variable as undefined -- but remember that we can
413          define it later if we need to do so.  */
414       DECL_EXTERNAL (d) = 1;
415       DECL_NOT_REALLY_EXTERN (d) = 1;
416       set_linkage_according_to_type (type, d);
417
418       d = pushdecl_top_level_and_finish (d, NULL_TREE);
419       if (CLASS_TYPE_P (type))
420         CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type)) = d;
421
422       /* Add decl to the global array of tinfo decls.  */
423       VEC_safe_push (tree, gc, unemitted_tinfo_decls, d);
424     }
425
426   return d;
427 }
428
429 /* Return a pointer to a type_info object describing TYPE, suitably
430    cast to the language defined type.  */
431
432 static tree
433 get_tinfo_ptr (tree type)
434 {
435   tree decl = get_tinfo_decl (type);
436
437   mark_used (decl);
438   return build_nop (type_info_ptr_type,
439                     build_address (decl));
440 }
441
442 /* Return the type_info object for TYPE.  */
443
444 tree
445 get_typeid (tree type)
446 {
447   if (type == error_mark_node || !typeid_ok_p ())
448     return error_mark_node;
449
450   if (processing_template_decl)
451     return build_min (TYPEID_EXPR, const_type_info_type_node, type);
452
453   /* If the type of the type-id is a reference type, the result of the
454      typeid expression refers to a type_info object representing the
455      referenced type.  */
456   type = non_reference (type);
457
458   /* The top-level cv-qualifiers of the lvalue expression or the type-id
459      that is the operand of typeid are always ignored.  */
460   type = TYPE_MAIN_VARIANT (type);
461
462   /* For UNKNOWN_TYPEs call complete_type_or_else to get diagnostics.  */
463   if (CLASS_TYPE_P (type) || TREE_CODE (type) == UNKNOWN_TYPE)
464     type = complete_type_or_else (type, NULL_TREE);
465
466   if (!type)
467     return error_mark_node;
468
469   return cp_build_indirect_ref (get_tinfo_ptr (type), NULL, 
470                                 tf_warning_or_error);
471 }
472
473 /* Check whether TEST is null before returning RESULT.  If TEST is used in
474    RESULT, it must have previously had a save_expr applied to it.  */
475
476 static tree
477 ifnonnull (tree test, tree result)
478 {
479   return build3 (COND_EXPR, TREE_TYPE (result),
480                  build2 (EQ_EXPR, boolean_type_node, test,
481                          cp_convert (TREE_TYPE (test), integer_zero_node)),
482                  cp_convert (TREE_TYPE (result), integer_zero_node),
483                  result);
484 }
485
486 /* Execute a dynamic cast, as described in section 5.2.6 of the 9/93 working
487    paper.  */
488
489 static tree
490 build_dynamic_cast_1 (tree type, tree expr, tsubst_flags_t complain)
491 {
492   enum tree_code tc = TREE_CODE (type);
493   tree exprtype = TREE_TYPE (expr);
494   tree dcast_fn;
495   tree old_expr = expr;
496   const char *errstr = NULL;
497
498   /* Save casted types in the function's used types hash table.  */
499   used_types_insert (type);
500
501   /* T shall be a pointer or reference to a complete class type, or
502      `pointer to cv void''.  */
503   switch (tc)
504     {
505     case POINTER_TYPE:
506       if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
507         break;
508       /* Fall through.  */
509     case REFERENCE_TYPE:
510       if (! MAYBE_CLASS_TYPE_P (TREE_TYPE (type)))
511         {
512           errstr = "target is not pointer or reference to class";
513           goto fail;
514         }
515       if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
516         {
517           errstr = "target is not pointer or reference to complete type";
518           goto fail;
519         }
520       break;
521
522     default:
523       errstr = "target is not pointer or reference";
524       goto fail;
525     }
526
527   if (tc == POINTER_TYPE)
528     {
529       /* If T is a pointer type, v shall be an rvalue of a pointer to
530          complete class type, and the result is an rvalue of type T.  */
531
532       if (TREE_CODE (exprtype) != POINTER_TYPE)
533         {
534           errstr = "source is not a pointer";
535           goto fail;
536         }
537       if (! MAYBE_CLASS_TYPE_P (TREE_TYPE (exprtype)))
538         {
539           errstr = "source is not a pointer to class";
540           goto fail;
541         }
542       if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
543         {
544           errstr = "source is a pointer to incomplete type";
545           goto fail;
546         }
547     }
548   else
549     {
550       exprtype = build_reference_type (exprtype);
551
552       /* T is a reference type, v shall be an lvalue of a complete class
553          type, and the result is an lvalue of the type referred to by T.  */
554
555       if (! MAYBE_CLASS_TYPE_P (TREE_TYPE (exprtype)))
556         {
557           errstr = "source is not of class type";
558           goto fail;
559         }
560       if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
561         {
562           errstr = "source is of incomplete class type";
563           goto fail;
564         }
565
566       /* Apply trivial conversion T -> T& for dereferenced ptrs.  */
567       expr = convert_to_reference (exprtype, expr, CONV_IMPLICIT,
568                                    LOOKUP_NORMAL, NULL_TREE);
569     }
570
571   /* The dynamic_cast operator shall not cast away constness.  */
572   if (!at_least_as_qualified_p (TREE_TYPE (type),
573                                 TREE_TYPE (exprtype)))
574     {
575       errstr = "conversion casts away constness";
576       goto fail;
577     }
578
579   /* If *type is an unambiguous accessible base class of *exprtype,
580      convert statically.  */
581   {
582     tree binfo;
583
584     binfo = lookup_base (TREE_TYPE (exprtype), TREE_TYPE (type),
585                          ba_check, NULL);
586
587     if (binfo)
588       {
589         expr = build_base_path (PLUS_EXPR, convert_from_reference (expr),
590                                 binfo, 0);
591         if (TREE_CODE (exprtype) == POINTER_TYPE)
592           expr = rvalue (expr);
593         return expr;
594       }
595   }
596
597   /* Otherwise *exprtype must be a polymorphic class (have a vtbl).  */
598   if (TYPE_POLYMORPHIC_P (TREE_TYPE (exprtype)))
599     {
600       tree expr1;
601       /* if TYPE is `void *', return pointer to complete object.  */
602       if (tc == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (type)))
603         {
604           /* if b is an object, dynamic_cast<void *>(&b) == (void *)&b.  */
605           if (TREE_CODE (expr) == ADDR_EXPR
606               && TREE_CODE (TREE_OPERAND (expr, 0)) == VAR_DECL
607               && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == RECORD_TYPE)
608             return build1 (NOP_EXPR, type, expr);
609
610           /* Since expr is used twice below, save it.  */
611           expr = save_expr (expr);
612
613           expr1 = build_headof (expr);
614           if (TREE_TYPE (expr1) != type)
615             expr1 = build1 (NOP_EXPR, type, expr1);
616           return ifnonnull (expr, expr1);
617         }
618       else
619         {
620           tree retval;
621           tree result, td2, td3;
622           tree elems[4];
623           tree static_type, target_type, boff;
624
625           /* If we got here, we can't convert statically.  Therefore,
626              dynamic_cast<D&>(b) (b an object) cannot succeed.  */
627           if (tc == REFERENCE_TYPE)
628             {
629               if (TREE_CODE (old_expr) == VAR_DECL
630                   && TREE_CODE (TREE_TYPE (old_expr)) == RECORD_TYPE)
631                 {
632                   tree expr = throw_bad_cast ();
633                   if (complain & tf_warning)
634                     warning (0, "dynamic_cast of %q#D to %q#T can never succeed",
635                              old_expr, type);
636                   /* Bash it to the expected type.  */
637                   TREE_TYPE (expr) = type;
638                   return expr;
639                 }
640             }
641           /* Ditto for dynamic_cast<D*>(&b).  */
642           else if (TREE_CODE (expr) == ADDR_EXPR)
643             {
644               tree op = TREE_OPERAND (expr, 0);
645               if (TREE_CODE (op) == VAR_DECL
646                   && TREE_CODE (TREE_TYPE (op)) == RECORD_TYPE)
647                 {
648                   if (complain & tf_warning)
649                     warning (0, "dynamic_cast of %q#D to %q#T can never succeed",
650                              op, type);
651                   retval = build_int_cst (type, 0);
652                   return retval;
653                 }
654             }
655
656           /* Use of dynamic_cast when -fno-rtti is prohibited.  */
657           if (!flag_rtti)
658             {
659               if (complain & tf_error)
660                 error ("%<dynamic_cast%> not permitted with -fno-rtti");
661               return error_mark_node;
662             }
663
664           target_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
665           static_type = TYPE_MAIN_VARIANT (TREE_TYPE (exprtype));
666           td2 = get_tinfo_decl (target_type);
667           mark_used (td2);
668           td2 = cp_build_unary_op (ADDR_EXPR, td2, 0, complain);
669           td3 = get_tinfo_decl (static_type);
670           mark_used (td3);
671           td3 = cp_build_unary_op (ADDR_EXPR, td3, 0, complain);
672
673           /* Determine how T and V are related.  */
674           boff = dcast_base_hint (static_type, target_type);
675
676           /* Since expr is used twice below, save it.  */
677           expr = save_expr (expr);
678
679           expr1 = expr;
680           if (tc == REFERENCE_TYPE)
681             expr1 = cp_build_unary_op (ADDR_EXPR, expr1, 0, complain);
682
683           elems[0] = expr1;
684           elems[1] = td3;
685           elems[2] = td2;
686           elems[3] = boff;
687
688           dcast_fn = dynamic_cast_node;
689           if (!dcast_fn)
690             {
691               tree tmp;
692               tree tinfo_ptr;
693               const char *name;
694
695               push_abi_namespace ();
696               tinfo_ptr = xref_tag (class_type,
697                                     get_identifier ("__class_type_info"),
698                                     /*tag_scope=*/ts_current, false);
699
700               tinfo_ptr = build_pointer_type
701                 (build_qualified_type
702                  (tinfo_ptr, TYPE_QUAL_CONST));
703               name = "__dynamic_cast";
704               tmp = tree_cons
705                 (NULL_TREE, const_ptr_type_node, tree_cons
706                  (NULL_TREE, tinfo_ptr, tree_cons
707                   (NULL_TREE, tinfo_ptr, tree_cons
708                    (NULL_TREE, ptrdiff_type_node, void_list_node))));
709               tmp = build_function_type (ptr_type_node, tmp);
710               dcast_fn = build_library_fn_ptr (name, tmp);
711               DECL_PURE_P (dcast_fn) = 1;
712               pop_abi_namespace ();
713               dynamic_cast_node = dcast_fn;
714             }
715           result = build_cxx_call (dcast_fn, 4, elems);
716
717           if (tc == REFERENCE_TYPE)
718             {
719               tree bad = throw_bad_cast ();
720               tree neq;
721
722               result = save_expr (result);
723               neq = c_common_truthvalue_conversion (input_location, result);
724               return cp_convert (type,
725                                  build3 (COND_EXPR, TREE_TYPE (result),
726                                          neq, result, bad));
727             }
728
729           /* Now back to the type we want from a void*.  */
730           result = cp_convert (type, result);
731           return ifnonnull (expr, result);
732         }
733     }
734   else
735     errstr = "source type is not polymorphic";
736
737  fail:
738   if (complain & tf_error)
739     error ("cannot dynamic_cast %qE (of type %q#T) to type %q#T (%s)",
740            expr, exprtype, type, errstr);
741   return error_mark_node;
742 }
743
744 tree
745 build_dynamic_cast (tree type, tree expr, tsubst_flags_t complain)
746 {
747   if (type == error_mark_node || expr == error_mark_node)
748     return error_mark_node;
749
750   if (processing_template_decl)
751     {
752       expr = build_min (DYNAMIC_CAST_EXPR, type, expr);
753       TREE_SIDE_EFFECTS (expr) = 1;
754       return convert_from_reference (expr);
755     }
756
757   return convert_from_reference (build_dynamic_cast_1 (type, expr, complain));
758 }
759 \f
760 /* Return the runtime bit mask encoding the qualifiers of TYPE.  */
761
762 static int
763 qualifier_flags (tree type)
764 {
765   int flags = 0;
766   int quals = cp_type_quals (type);
767
768   if (quals & TYPE_QUAL_CONST)
769     flags |= 1;
770   if (quals & TYPE_QUAL_VOLATILE)
771     flags |= 2;
772   if (quals & TYPE_QUAL_RESTRICT)
773     flags |= 4;
774   return flags;
775 }
776
777 /* Return true, if the pointer chain TYPE ends at an incomplete type, or
778    contains a pointer to member of an incomplete class.  */
779
780 static bool
781 target_incomplete_p (tree type)
782 {
783   while (true)
784     if (TYPE_PTRMEM_P (type))
785       {
786         if (!COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type)))
787           return true;
788         type = TYPE_PTRMEM_POINTED_TO_TYPE (type);
789       }
790     else if (TREE_CODE (type) == POINTER_TYPE)
791       type = TREE_TYPE (type);
792     else
793       return !COMPLETE_OR_VOID_TYPE_P (type);
794 }
795
796 /* Returns true if TYPE involves an incomplete class type; in that
797    case, typeinfo variables for TYPE should be emitted with internal
798    linkage.  */
799
800 static bool
801 involves_incomplete_p (tree type)
802 {
803   switch (TREE_CODE (type))
804     {
805     case POINTER_TYPE:
806       return target_incomplete_p (TREE_TYPE (type));
807
808     case OFFSET_TYPE:
809     ptrmem:
810       return
811         (target_incomplete_p (TYPE_PTRMEM_POINTED_TO_TYPE (type))
812          || !COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type)));
813
814     case RECORD_TYPE:
815       if (TYPE_PTRMEMFUNC_P (type))
816         goto ptrmem;
817       /* Fall through.  */
818     case UNION_TYPE:
819       if (!COMPLETE_TYPE_P (type))
820         return true;
821
822     default:
823       /* All other types do not involve incomplete class types.  */
824       return false;
825     }
826 }
827
828 /* Return a CONSTRUCTOR for the common part of the type_info objects. This
829    is the vtable pointer and NTBS name.  The NTBS name is emitted as a
830    comdat const char array, so it becomes a unique key for the type. Generate
831    and emit that VAR_DECL here.  (We can't always emit the type_info itself
832    as comdat, because of pointers to incomplete.) */
833
834 static tree
835 tinfo_base_init (tinfo_s *ti, tree target)
836 {
837   tree init = NULL_TREE;
838   tree name_decl;
839   tree vtable_ptr;
840
841   {
842     tree name_name;
843
844     /* Generate the NTBS array variable.  */
845     tree name_type = build_cplus_array_type
846                      (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
847                      NULL_TREE);
848     tree name_string = tinfo_name (target);
849
850     /* Determine the name of the variable -- and remember with which
851        type it is associated.  */
852     name_name = mangle_typeinfo_string_for_type (target);
853     TREE_TYPE (name_name) = target;
854
855     name_decl = build_lang_decl (VAR_DECL, name_name, name_type);
856     SET_DECL_ASSEMBLER_NAME (name_decl, name_name);
857     DECL_ARTIFICIAL (name_decl) = 1;
858     DECL_IGNORED_P (name_decl) = 1;
859     TREE_READONLY (name_decl) = 1;
860     TREE_STATIC (name_decl) = 1;
861     DECL_EXTERNAL (name_decl) = 0;
862     DECL_TINFO_P (name_decl) = 1;
863     set_linkage_according_to_type (target, name_decl);
864     import_export_decl (name_decl);
865     DECL_INITIAL (name_decl) = name_string;
866     mark_used (name_decl);
867     pushdecl_top_level_and_finish (name_decl, name_string);
868   }
869
870   vtable_ptr = ti->vtable;
871   if (!vtable_ptr)
872     {
873       tree real_type;
874       push_abi_namespace ();
875       real_type = xref_tag (class_type, ti->name,
876                             /*tag_scope=*/ts_current, false);
877       pop_abi_namespace ();
878
879       if (!COMPLETE_TYPE_P (real_type))
880         {
881           /* We never saw a definition of this type, so we need to
882              tell the compiler that this is an exported class, as
883              indeed all of the __*_type_info classes are.  */
884           SET_CLASSTYPE_INTERFACE_KNOWN (real_type);
885           CLASSTYPE_INTERFACE_ONLY (real_type) = 1;
886         }
887
888       vtable_ptr = get_vtable_decl (real_type, /*complete=*/1);
889       vtable_ptr = cp_build_unary_op (ADDR_EXPR, vtable_ptr, 0, 
890                                    tf_warning_or_error);
891
892       /* We need to point into the middle of the vtable.  */
893       vtable_ptr = build2
894         (POINTER_PLUS_EXPR, TREE_TYPE (vtable_ptr), vtable_ptr,
895          size_binop (MULT_EXPR,
896                      size_int (2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE),
897                      TYPE_SIZE_UNIT (vtable_entry_type)));
898
899       ti->vtable = vtable_ptr;
900     }
901
902   init = tree_cons (NULL_TREE, vtable_ptr, init);
903
904   init = tree_cons (NULL_TREE, decay_conversion (name_decl), init);
905
906   init = build_constructor_from_list (init_list_type_node, nreverse (init));
907   TREE_CONSTANT (init) = 1;
908   TREE_STATIC (init) = 1;
909   init = tree_cons (NULL_TREE, init, NULL_TREE);
910
911   return init;
912 }
913
914 /* Return the CONSTRUCTOR expr for a type_info of TYPE. TI provides the
915    information about the particular type_info derivation, which adds no
916    additional fields to the type_info base.  */
917
918 static tree
919 generic_initializer (tinfo_s *ti, tree target)
920 {
921   tree init = tinfo_base_init (ti, target);
922
923   init = build_constructor_from_list (init_list_type_node, init);
924   TREE_CONSTANT (init) = 1;
925   TREE_STATIC (init) = 1;
926   return init;
927 }
928
929 /* Return the CONSTRUCTOR expr for a type_info of pointer TYPE.
930    TI provides information about the particular type_info derivation,
931    which adds target type and qualifier flags members to the type_info base.  */
932
933 static tree
934 ptr_initializer (tinfo_s *ti, tree target)
935 {
936   tree init = tinfo_base_init (ti, target);
937   tree to = TREE_TYPE (target);
938   int flags = qualifier_flags (to);
939   bool incomplete = target_incomplete_p (to);
940
941   if (incomplete)
942     flags |= 8;
943   init = tree_cons (NULL_TREE, build_int_cst (NULL_TREE, flags), init);
944   init = tree_cons (NULL_TREE,
945                     get_tinfo_ptr (TYPE_MAIN_VARIANT (to)),
946                     init);
947
948   init = build_constructor_from_list (init_list_type_node, nreverse (init));
949   TREE_CONSTANT (init) = 1;
950   TREE_STATIC (init) = 1;
951   return init;
952 }
953
954 /* Return the CONSTRUCTOR expr for a type_info of pointer to member data TYPE.
955    TI provides information about the particular type_info derivation,
956    which adds class, target type and qualifier flags members to the type_info
957    base.  */
958
959 static tree
960 ptm_initializer (tinfo_s *ti, tree target)
961 {
962   tree init = tinfo_base_init (ti, target);
963   tree to = TYPE_PTRMEM_POINTED_TO_TYPE (target);
964   tree klass = TYPE_PTRMEM_CLASS_TYPE (target);
965   int flags = qualifier_flags (to);
966   bool incomplete = target_incomplete_p (to);
967
968   if (incomplete)
969     flags |= 0x8;
970   if (!COMPLETE_TYPE_P (klass))
971     flags |= 0x10;
972   init = tree_cons (NULL_TREE, build_int_cst (NULL_TREE, flags), init);
973   init = tree_cons (NULL_TREE,
974                     get_tinfo_ptr (TYPE_MAIN_VARIANT (to)),
975                     init);
976   init = tree_cons (NULL_TREE,
977                     get_tinfo_ptr (klass),
978                     init);
979
980   init = build_constructor_from_list (init_list_type_node, nreverse (init));
981   TREE_CONSTANT (init) = 1;
982   TREE_STATIC (init) = 1;
983   return init;
984 }
985
986 /* Return the CONSTRUCTOR expr for a type_info of class TYPE.
987    TI provides information about the particular __class_type_info derivation,
988    which adds hint flags and TRAIL initializers to the type_info base.  */
989
990 static tree
991 class_initializer (tinfo_s *ti, tree target, tree trail)
992 {
993   tree init = tinfo_base_init (ti, target);
994
995   TREE_CHAIN (init) = trail;
996   init = build_constructor_from_list (init_list_type_node, init);
997   TREE_CONSTANT (init) = 1;
998   TREE_STATIC (init) = 1;
999   return init;
1000 }
1001
1002 /* Returns true if the typeinfo for type should be placed in
1003    the runtime library.  */
1004
1005 static bool
1006 typeinfo_in_lib_p (tree type)
1007 {
1008   /* The typeinfo objects for `T*' and `const T*' are in the runtime
1009      library for simple types T.  */
1010   if (TREE_CODE (type) == POINTER_TYPE
1011       && (cp_type_quals (TREE_TYPE (type)) == TYPE_QUAL_CONST
1012           || cp_type_quals (TREE_TYPE (type)) == TYPE_UNQUALIFIED))
1013     type = TREE_TYPE (type);
1014
1015   switch (TREE_CODE (type))
1016     {
1017     case INTEGER_TYPE:
1018     case BOOLEAN_TYPE:
1019     case REAL_TYPE:
1020     case VOID_TYPE:
1021       return true;
1022
1023     default:
1024       return false;
1025     }
1026 }
1027
1028 /* Generate the initializer for the type info describing TYPE.  TK_INDEX is
1029    the index of the descriptor in the tinfo_desc vector. */
1030
1031 static tree
1032 get_pseudo_ti_init (tree type, unsigned tk_index)
1033 {
1034   tinfo_s *ti = VEC_index (tinfo_s, tinfo_descs, tk_index);
1035
1036   gcc_assert (at_eof);
1037   switch (tk_index)
1038     {
1039     case TK_POINTER_MEMBER_TYPE:
1040       return ptm_initializer (ti, type);
1041
1042     case TK_POINTER_TYPE:
1043       return ptr_initializer (ti, type);
1044
1045     case TK_BUILTIN_TYPE:
1046     case TK_ENUMERAL_TYPE:
1047     case TK_FUNCTION_TYPE:
1048     case TK_ARRAY_TYPE:
1049       return generic_initializer (ti, type);
1050
1051     case TK_CLASS_TYPE:
1052       return class_initializer (ti, type, NULL_TREE);
1053
1054     case TK_SI_CLASS_TYPE:
1055       {
1056         tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), 0);
1057         tree tinfo = get_tinfo_ptr (BINFO_TYPE (base_binfo));
1058         tree base_inits = tree_cons (NULL_TREE, tinfo, NULL_TREE);
1059
1060         /* get_tinfo_ptr might have reallocated the tinfo_descs vector.  */
1061         ti = VEC_index (tinfo_s, tinfo_descs, tk_index);
1062         return class_initializer (ti, type, base_inits);
1063       }
1064
1065     default:
1066       {
1067         int hint = ((CLASSTYPE_REPEATED_BASE_P (type) << 0)
1068                     | (CLASSTYPE_DIAMOND_SHAPED_P (type) << 1));
1069         tree binfo = TYPE_BINFO (type);
1070         int nbases = BINFO_N_BASE_BINFOS (binfo);
1071         VEC(tree,gc) *base_accesses = BINFO_BASE_ACCESSES (binfo);
1072         tree offset_type = integer_types[itk_long];
1073         tree base_inits = NULL_TREE;
1074         int ix;
1075
1076         gcc_assert (tk_index >= TK_FIXED);
1077
1078         /* Generate the base information initializer.  */
1079         for (ix = nbases; ix--;)
1080           {
1081             tree base_binfo = BINFO_BASE_BINFO (binfo, ix);
1082             tree base_init = NULL_TREE;
1083             int flags = 0;
1084             tree tinfo;
1085             tree offset;
1086
1087             if (VEC_index (tree, base_accesses, ix) == access_public_node)
1088               flags |= 2;
1089             tinfo = get_tinfo_ptr (BINFO_TYPE (base_binfo));
1090             if (BINFO_VIRTUAL_P (base_binfo))
1091               {
1092                 /* We store the vtable offset at which the virtual
1093                    base offset can be found.  */
1094                 offset = BINFO_VPTR_FIELD (base_binfo);
1095                 flags |= 1;
1096               }
1097             else
1098               offset = BINFO_OFFSET (base_binfo);
1099
1100             /* Combine offset and flags into one field.  */
1101             offset = fold_convert (offset_type, offset);
1102             offset = fold_build2_loc (input_location,
1103                                   LSHIFT_EXPR, offset_type, offset,
1104                                   build_int_cst (offset_type, 8));
1105             offset = fold_build2_loc (input_location,
1106                                   BIT_IOR_EXPR, offset_type, offset,
1107                                   build_int_cst (offset_type, flags));
1108             base_init = tree_cons (NULL_TREE, offset, base_init);
1109             base_init = tree_cons (NULL_TREE, tinfo, base_init);
1110             base_init = build_constructor_from_list (init_list_type_node, base_init);
1111             base_inits = tree_cons (NULL_TREE, base_init, base_inits);
1112           }
1113         base_inits = build_constructor_from_list (init_list_type_node, base_inits);
1114         base_inits = tree_cons (NULL_TREE, base_inits, NULL_TREE);
1115         /* Prepend the number of bases.  */
1116         base_inits = tree_cons (NULL_TREE,
1117                                 build_int_cst (NULL_TREE, nbases),
1118                                 base_inits);
1119         /* Prepend the hint flags.  */
1120         base_inits = tree_cons (NULL_TREE,
1121                                 build_int_cst (NULL_TREE, hint),
1122                                 base_inits);
1123
1124         /* get_tinfo_ptr might have reallocated the tinfo_descs vector.  */
1125         ti = VEC_index (tinfo_s, tinfo_descs, tk_index);
1126         return class_initializer (ti, type, base_inits);
1127       }
1128     }
1129 }
1130
1131 /* Generate the RECORD_TYPE containing the data layout of a type_info
1132    derivative as used by the runtime. This layout must be consistent with
1133    that defined in the runtime support. Also generate the VAR_DECL for the
1134    type's vtable. We explicitly manage the vtable member, and name it for
1135    real type as used in the runtime. The RECORD type has a different name,
1136    to avoid collisions.  Return a TREE_LIST who's TINFO_PSEUDO_TYPE
1137    is the generated type and TINFO_VTABLE_NAME is the name of the
1138    vtable.  We have to delay generating the VAR_DECL of the vtable
1139    until the end of the translation, when we'll have seen the library
1140    definition, if there was one.
1141
1142    REAL_NAME is the runtime's name of the type. Trailing arguments are
1143    additional FIELD_DECL's for the structure. The final argument must be
1144    NULL.  */
1145
1146 static void
1147 create_pseudo_type_info (int tk, const char *real_name, ...)
1148 {
1149   tinfo_s *ti;
1150   tree pseudo_type;
1151   char *pseudo_name;
1152   tree fields;
1153   tree field_decl;
1154   va_list ap;
1155
1156   va_start (ap, real_name);
1157
1158   /* Generate the pseudo type name.  */
1159   pseudo_name = (char *) alloca (strlen (real_name) + 30);
1160   strcpy (pseudo_name, real_name);
1161   strcat (pseudo_name, "_pseudo");
1162   if (tk >= TK_FIXED)
1163     sprintf (pseudo_name + strlen (pseudo_name), "%d", tk - TK_FIXED);
1164
1165   /* First field is the pseudo type_info base class.  */
1166   fields = build_decl (input_location,
1167                        FIELD_DECL, NULL_TREE,
1168                        VEC_index (tinfo_s, tinfo_descs,
1169                                   TK_TYPE_INFO_TYPE)->type);
1170
1171   /* Now add the derived fields.  */
1172   while ((field_decl = va_arg (ap, tree)))
1173     {
1174       TREE_CHAIN (field_decl) = fields;
1175       fields = field_decl;
1176     }
1177
1178   /* Create the pseudo type.  */
1179   pseudo_type = make_class_type (RECORD_TYPE);
1180   finish_builtin_struct (pseudo_type, pseudo_name, fields, NULL_TREE);
1181   CLASSTYPE_AS_BASE (pseudo_type) = pseudo_type;
1182
1183   ti = VEC_index (tinfo_s, tinfo_descs, tk);
1184   ti->type = cp_build_qualified_type (pseudo_type, TYPE_QUAL_CONST);
1185   ti->name = get_identifier (real_name);
1186   ti->vtable = NULL_TREE;
1187
1188   /* Pretend this is public so determine_visibility doesn't give vtables
1189      internal linkage.  */
1190   TREE_PUBLIC (TYPE_MAIN_DECL (ti->type)) = 1;
1191
1192   va_end (ap);
1193 }
1194
1195 /* Return the index of a pseudo type info type node used to describe
1196    TYPE.  TYPE must be a complete type (or cv void), except at the end
1197    of the translation unit.  */
1198
1199 static unsigned
1200 get_pseudo_ti_index (tree type)
1201 {
1202   unsigned ix;
1203
1204   switch (TREE_CODE (type))
1205     {
1206     case OFFSET_TYPE:
1207       ix = TK_POINTER_MEMBER_TYPE;
1208       break;
1209
1210     case POINTER_TYPE:
1211       ix = TK_POINTER_TYPE;
1212       break;
1213
1214     case ENUMERAL_TYPE:
1215       ix = TK_ENUMERAL_TYPE;
1216       break;
1217
1218     case FUNCTION_TYPE:
1219       ix = TK_FUNCTION_TYPE;
1220       break;
1221
1222     case ARRAY_TYPE:
1223       ix = TK_ARRAY_TYPE;
1224       break;
1225
1226     case UNION_TYPE:
1227     case RECORD_TYPE:
1228       if (TYPE_PTRMEMFUNC_P (type))
1229         {
1230           ix = TK_POINTER_MEMBER_TYPE;
1231           break;
1232         }
1233       else if (!COMPLETE_TYPE_P (type))
1234         {
1235           if (!at_eof)
1236             cxx_incomplete_type_error (NULL_TREE, type);
1237           ix = TK_CLASS_TYPE;
1238           break;
1239         }
1240       else if (!BINFO_N_BASE_BINFOS (TYPE_BINFO (type)))
1241         {
1242           ix = TK_CLASS_TYPE;
1243           break;
1244         }
1245       else
1246         {
1247           tree binfo = TYPE_BINFO (type);
1248           VEC(tree,gc) *base_accesses = BINFO_BASE_ACCESSES (binfo);
1249           tree base_binfo = BINFO_BASE_BINFO (binfo, 0);
1250           int num_bases = BINFO_N_BASE_BINFOS (binfo);
1251
1252           if (num_bases == 1
1253               && VEC_index (tree, base_accesses, 0) == access_public_node
1254               && !BINFO_VIRTUAL_P (base_binfo)
1255               && integer_zerop (BINFO_OFFSET (base_binfo)))
1256             {
1257               /* single non-virtual public.  */
1258               ix = TK_SI_CLASS_TYPE;
1259               break;
1260             }
1261           else
1262             {
1263               tinfo_s *ti;
1264               tree array_domain, base_array;
1265
1266               ix = TK_FIXED + num_bases;
1267               if (VEC_length (tinfo_s, tinfo_descs) <= ix)
1268                 {
1269                   /* too short, extend.  */
1270                   unsigned len = VEC_length (tinfo_s, tinfo_descs);
1271
1272                   VEC_safe_grow (tinfo_s, gc, tinfo_descs, ix + 1);
1273                   while (VEC_iterate (tinfo_s, tinfo_descs, len++, ti))
1274                     ti->type = ti->vtable = ti->name = NULL_TREE;
1275                 }
1276               else if (VEC_index (tinfo_s, tinfo_descs, ix)->type)
1277                 /* already created.  */
1278                 break;
1279
1280               /* Create the array of __base_class_type_info entries.
1281                  G++ 3.2 allocated an array that had one too many
1282                  entries, and then filled that extra entries with
1283                  zeros.  */
1284               if (abi_version_at_least (2))
1285                 array_domain = build_index_type (size_int (num_bases - 1));
1286               else
1287                 array_domain = build_index_type (size_int (num_bases));
1288               base_array =
1289                 build_array_type (VEC_index (tinfo_s, tinfo_descs,
1290                                              TK_BASE_TYPE)->type,
1291                                   array_domain);
1292
1293               push_abi_namespace ();
1294               create_pseudo_type_info
1295                 (ix, "__vmi_class_type_info",
1296                  build_decl (input_location,
1297                              FIELD_DECL, NULL_TREE, integer_type_node),
1298                  build_decl (input_location,
1299                              FIELD_DECL, NULL_TREE, integer_type_node),
1300                  build_decl (input_location,
1301                              FIELD_DECL, NULL_TREE, base_array),
1302                  NULL);
1303               pop_abi_namespace ();
1304               break;
1305             }
1306         }
1307     default:
1308       ix = TK_BUILTIN_TYPE;
1309       break;
1310     }
1311   return ix;
1312 }
1313
1314 /* Make sure the required builtin types exist for generating the type_info
1315    variable definitions.  */
1316
1317 static void
1318 create_tinfo_types (void)
1319 {
1320   tinfo_s *ti;
1321
1322   gcc_assert (!tinfo_descs);
1323
1324   VEC_safe_grow (tinfo_s, gc, tinfo_descs, TK_FIXED);
1325
1326   push_abi_namespace ();
1327
1328   /* Create the internal type_info structure. This is used as a base for
1329      the other structures.  */
1330   {
1331     tree field, fields;
1332
1333     field = build_decl (BUILTINS_LOCATION,
1334                         FIELD_DECL, NULL_TREE, const_ptr_type_node);
1335     fields = field;
1336
1337     field = build_decl (BUILTINS_LOCATION,
1338                         FIELD_DECL, NULL_TREE, const_string_type_node);
1339     TREE_CHAIN (field) = fields;
1340     fields = field;
1341
1342     ti = VEC_index (tinfo_s, tinfo_descs, TK_TYPE_INFO_TYPE);
1343     ti->type = make_class_type (RECORD_TYPE);
1344     ti->vtable = NULL_TREE;
1345     ti->name = NULL_TREE;
1346     finish_builtin_struct (ti->type, "__type_info_pseudo",
1347                            fields, NULL_TREE);
1348   }
1349
1350   /* Fundamental type_info */
1351   create_pseudo_type_info (TK_BUILTIN_TYPE, "__fundamental_type_info", NULL);
1352
1353   /* Array, function and enum type_info. No additional fields.  */
1354   create_pseudo_type_info (TK_ARRAY_TYPE, "__array_type_info", NULL);
1355   create_pseudo_type_info (TK_FUNCTION_TYPE, "__function_type_info", NULL);
1356   create_pseudo_type_info (TK_ENUMERAL_TYPE, "__enum_type_info", NULL);
1357
1358   /* Class type_info.  No additional fields.  */
1359   create_pseudo_type_info (TK_CLASS_TYPE, "__class_type_info", NULL);
1360
1361   /* Single public non-virtual base class. Add pointer to base class.
1362      This is really a descendant of __class_type_info.  */
1363   create_pseudo_type_info (TK_SI_CLASS_TYPE, "__si_class_type_info",
1364             build_decl (BUILTINS_LOCATION,
1365                         FIELD_DECL, NULL_TREE, type_info_ptr_type),
1366             NULL);
1367
1368   /* Base class internal helper. Pointer to base type, offset to base,
1369      flags.  */
1370   {
1371     tree field, fields;
1372
1373     field = build_decl (BUILTINS_LOCATION,
1374                         FIELD_DECL, NULL_TREE, type_info_ptr_type);
1375     fields = field;
1376
1377     field = build_decl (BUILTINS_LOCATION,
1378                         FIELD_DECL, NULL_TREE, integer_types[itk_long]);
1379     TREE_CHAIN (field) = fields;
1380     fields = field;
1381
1382     ti = VEC_index (tinfo_s, tinfo_descs, TK_BASE_TYPE);
1383
1384     ti->type = make_class_type (RECORD_TYPE);
1385     ti->vtable = NULL_TREE;
1386     ti->name = NULL_TREE;
1387     finish_builtin_struct (ti->type, "__base_class_type_info_pseudo",
1388                            fields, NULL_TREE);
1389   }
1390
1391   /* Pointer type_info. Adds two fields, qualification mask
1392      and pointer to the pointed to type.  This is really a descendant of
1393      __pbase_type_info.  */
1394   create_pseudo_type_info (TK_POINTER_TYPE, "__pointer_type_info",
1395        build_decl (BUILTINS_LOCATION, 
1396                    FIELD_DECL, NULL_TREE, integer_type_node),
1397        build_decl (BUILTINS_LOCATION,
1398                    FIELD_DECL, NULL_TREE, type_info_ptr_type),
1399        NULL);
1400
1401   /* Pointer to member data type_info.  Add qualifications flags,
1402      pointer to the member's type info and pointer to the class.
1403      This is really a descendant of __pbase_type_info.  */
1404   create_pseudo_type_info (TK_POINTER_MEMBER_TYPE,
1405        "__pointer_to_member_type_info",
1406         build_decl (BUILTINS_LOCATION,
1407                     FIELD_DECL, NULL_TREE, integer_type_node),
1408         build_decl (BUILTINS_LOCATION,
1409                     FIELD_DECL, NULL_TREE, type_info_ptr_type),
1410         build_decl (BUILTINS_LOCATION,
1411                     FIELD_DECL, NULL_TREE, type_info_ptr_type),
1412         NULL);
1413
1414   pop_abi_namespace ();
1415 }
1416
1417 /* Emit the type_info descriptors which are guaranteed to be in the runtime
1418    support.  Generating them here guarantees consistency with the other
1419    structures.  We use the following heuristic to determine when the runtime
1420    is being generated.  If std::__fundamental_type_info is defined, and its
1421    destructor is defined, then the runtime is being built.  */
1422
1423 void
1424 emit_support_tinfos (void)
1425 {
1426   static tree *const fundamentals[] =
1427   {
1428     &void_type_node,
1429     &boolean_type_node,
1430     &wchar_type_node, &char16_type_node, &char32_type_node,
1431     &char_type_node, &signed_char_type_node, &unsigned_char_type_node,
1432     &short_integer_type_node, &short_unsigned_type_node,
1433     &integer_type_node, &unsigned_type_node,
1434     &long_integer_type_node, &long_unsigned_type_node,
1435     &long_long_integer_type_node, &long_long_unsigned_type_node,
1436     &float_type_node, &double_type_node, &long_double_type_node,
1437     0
1438   };
1439   int ix;
1440   tree bltn_type, dtor;
1441
1442   push_abi_namespace ();
1443   bltn_type = xref_tag (class_type,
1444                         get_identifier ("__fundamental_type_info"),
1445                         /*tag_scope=*/ts_current, false);
1446   pop_abi_namespace ();
1447   if (!COMPLETE_TYPE_P (bltn_type))
1448     return;
1449   dtor = CLASSTYPE_DESTRUCTORS (bltn_type);
1450   if (!dtor || DECL_EXTERNAL (dtor))
1451     return;
1452   doing_runtime = 1;
1453   for (ix = 0; fundamentals[ix]; ix++)
1454     {
1455       tree bltn = *fundamentals[ix];
1456       tree types[3];
1457       int i;
1458
1459       types[0] = bltn;
1460       types[1] = build_pointer_type (bltn);
1461       types[2] = build_pointer_type (build_qualified_type (bltn,
1462                                                            TYPE_QUAL_CONST));
1463
1464       for (i = 0; i < 3; ++i)
1465         {
1466           tree tinfo;
1467
1468           tinfo = get_tinfo_decl (types[i]);
1469           TREE_USED (tinfo) = 1;
1470           mark_needed (tinfo);
1471           /* The C++ ABI requires that these objects be COMDAT.  But,
1472              On systems without weak symbols, initialized COMDAT
1473              objects are emitted with internal linkage.  (See
1474              comdat_linkage for details.)  Since we want these objects
1475              to have external linkage so that copies do not have to be
1476              emitted in code outside the runtime library, we make them
1477              non-COMDAT here.  
1478
1479              It might also not be necessary to follow this detail of the
1480              ABI.  */
1481           if (!flag_weak || ! targetm.cxx.library_rtti_comdat ())
1482             {
1483               gcc_assert (TREE_PUBLIC (tinfo) && !DECL_COMDAT (tinfo));
1484               DECL_INTERFACE_KNOWN (tinfo) = 1;
1485             }
1486         }
1487     }
1488 }
1489
1490 /* Finish a type info decl. DECL_PTR is a pointer to an unemitted
1491    tinfo decl.  Determine whether it needs emitting, and if so
1492    generate the initializer.  */
1493
1494 bool
1495 emit_tinfo_decl (tree decl)
1496 {
1497   tree type = TREE_TYPE (DECL_NAME (decl));
1498   int in_library = typeinfo_in_lib_p (type);
1499
1500   gcc_assert (DECL_TINFO_P (decl));
1501
1502   if (in_library)
1503     {
1504       if (doing_runtime)
1505         DECL_EXTERNAL (decl) = 0;
1506       else
1507         {
1508           /* If we're not in the runtime, then DECL (which is already
1509              DECL_EXTERNAL) will not be defined here.  */
1510           DECL_INTERFACE_KNOWN (decl) = 1;
1511           return false;
1512         }
1513     }
1514   else if (involves_incomplete_p (type))
1515     {
1516       if (!decl_needed_p (decl))
1517         return false;
1518       /* If TYPE involves an incomplete class type, then the typeinfo
1519          object will be emitted with internal linkage.  There is no
1520          way to know whether or not types are incomplete until the end
1521          of the compilation, so this determination must be deferred
1522          until this point.  */
1523       TREE_PUBLIC (decl) = 0;
1524       DECL_EXTERNAL (decl) = 0;
1525       DECL_INTERFACE_KNOWN (decl) = 1;
1526     }
1527
1528   import_export_decl (decl);
1529   if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
1530     {
1531       tree init;
1532
1533       DECL_EXTERNAL (decl) = 0;
1534       init = get_pseudo_ti_init (type, get_pseudo_ti_index (type));
1535       DECL_INITIAL (decl) = init;
1536       mark_used (decl);
1537       cp_finish_decl (decl, init, false, NULL_TREE, 0);
1538       return true;
1539     }
1540   else
1541     return false;
1542 }
1543
1544 #include "gt-cp-rtti.h"