OSDN Git Service

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