OSDN Git Service

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