OSDN Git Service

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