OSDN Git Service

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