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