OSDN Git Service

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