OSDN Git Service

2005-06-01 Paul Thomas <pault@gcc.gnu.org>
[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,gc) *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, gc, 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, gc, 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, 
423                          cp_convert (TREE_TYPE (test), integer_zero_node)),
424                  cp_convert (TREE_TYPE (result), integer_zero_node),
425                  result);
426 }
427
428 /* Execute a dynamic cast, as described in section 5.2.6 of the 9/93 working
429    paper.  */
430
431 static tree
432 build_dynamic_cast_1 (tree type, tree expr)
433 {
434   enum tree_code tc = TREE_CODE (type);
435   tree exprtype = TREE_TYPE (expr);
436   tree dcast_fn;
437   tree old_expr = expr;
438   const char *errstr = NULL;
439
440   /* T shall be a pointer or reference to a complete class type, or
441      `pointer to cv void''.  */
442   switch (tc)
443     {
444     case POINTER_TYPE:
445       if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
446         break;
447       /* Fall through.  */
448     case REFERENCE_TYPE:
449       if (! IS_AGGR_TYPE (TREE_TYPE (type)))
450         {
451           errstr = "target is not pointer or reference to class";
452           goto fail;
453         }
454       if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
455         {
456           errstr = "target is not pointer or reference to complete type";
457           goto fail;
458         }
459       break;
460
461     default:
462       errstr = "target is not pointer or reference";
463       goto fail;
464     }
465
466   if (tc == POINTER_TYPE)
467     {
468       /* If T is a pointer type, v shall be an rvalue of a pointer to
469          complete class type, and the result is an rvalue of type T.  */
470
471       if (TREE_CODE (exprtype) != POINTER_TYPE)
472         {
473           errstr = "source is not a pointer";
474           goto fail;
475         }
476       if (! IS_AGGR_TYPE (TREE_TYPE (exprtype)))
477         {
478           errstr = "source is not a pointer to class";
479           goto fail;
480         }
481       if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
482         {
483           errstr = "source is a pointer to incomplete type";
484           goto fail;
485         }
486     }
487   else
488     {
489       /* Apply trivial conversion T -> T& for dereferenced ptrs.  */
490       exprtype = build_reference_type (exprtype);
491       expr = convert_to_reference (exprtype, expr, CONV_IMPLICIT,
492                                    LOOKUP_NORMAL, NULL_TREE);
493
494       /* T is a reference type, v shall be an lvalue of a complete class
495          type, and the result is an lvalue of the type referred to by T.  */
496
497       if (! IS_AGGR_TYPE (TREE_TYPE (exprtype)))
498         {
499           errstr = "source is not of class type";
500           goto fail;
501         }
502       if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
503         {
504           errstr = "source is of incomplete class type";
505           goto fail;
506         }
507       
508     }
509
510   /* The dynamic_cast operator shall not cast away constness.  */
511   if (!at_least_as_qualified_p (TREE_TYPE (type),
512                                 TREE_TYPE (exprtype)))
513     {
514       errstr = "conversion casts away constness";
515       goto fail;
516     }
517
518   /* If *type is an unambiguous accessible base class of *exprtype,
519      convert statically.  */
520   {
521     tree binfo;
522
523     binfo = lookup_base (TREE_TYPE (exprtype), TREE_TYPE (type),
524                          ba_check, NULL);
525
526     if (binfo)
527       {
528         expr = build_base_path (PLUS_EXPR, convert_from_reference (expr),
529                                 binfo, 0);
530         if (TREE_CODE (exprtype) == POINTER_TYPE)
531           expr = non_lvalue (expr);
532         return expr;
533       }
534   }
535
536   /* Otherwise *exprtype must be a polymorphic class (have a vtbl).  */
537   if (TYPE_POLYMORPHIC_P (TREE_TYPE (exprtype)))
538     {
539       tree expr1;
540       /* if TYPE is `void *', return pointer to complete object.  */
541       if (tc == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (type)))
542         {
543           /* if b is an object, dynamic_cast<void *>(&b) == (void *)&b.  */
544           if (TREE_CODE (expr) == ADDR_EXPR
545               && TREE_CODE (TREE_OPERAND (expr, 0)) == VAR_DECL
546               && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == RECORD_TYPE)
547             return build1 (NOP_EXPR, type, expr);
548
549           /* Since expr is used twice below, save it.  */
550           expr = save_expr (expr);
551
552           expr1 = build_headof (expr);
553           if (TREE_TYPE (expr1) != type)
554             expr1 = build1 (NOP_EXPR, type, expr1);
555           return ifnonnull (expr, expr1);
556         }
557       else
558         {
559           tree retval;
560           tree result, td2, td3, elems;
561           tree static_type, target_type, boff;
562
563           /* If we got here, we can't convert statically.  Therefore,
564              dynamic_cast<D&>(b) (b an object) cannot succeed.  */
565           if (tc == REFERENCE_TYPE)
566             {
567               if (TREE_CODE (old_expr) == VAR_DECL
568                   && TREE_CODE (TREE_TYPE (old_expr)) == RECORD_TYPE)
569                 {
570                   tree expr = throw_bad_cast ();
571                   warning (0, "dynamic_cast of %q#D to %q#T can never succeed",
572                            old_expr, type);
573                   /* Bash it to the expected type.  */
574                   TREE_TYPE (expr) = type;
575                   return expr;
576                 }
577             }
578           /* Ditto for dynamic_cast<D*>(&b).  */
579           else if (TREE_CODE (expr) == ADDR_EXPR)
580             {
581               tree op = TREE_OPERAND (expr, 0);
582               if (TREE_CODE (op) == VAR_DECL
583                   && TREE_CODE (TREE_TYPE (op)) == RECORD_TYPE)
584                 {
585                   warning (0, "dynamic_cast of %q#D to %q#T can never succeed",
586                            op, type);
587                   retval = build_int_cst (type, 0); 
588                   return retval;
589                 }
590             }
591
592           target_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
593           static_type = TYPE_MAIN_VARIANT (TREE_TYPE (exprtype));
594           td2 = get_tinfo_decl (target_type);
595           mark_used (td2);
596           td2 = build_unary_op (ADDR_EXPR, td2, 0);
597           td3 = get_tinfo_decl (static_type);
598           mark_used (td3);
599           td3 = build_unary_op (ADDR_EXPR, td3, 0);
600
601           /* Determine how T and V are related.  */
602           boff = dcast_base_hint (static_type, target_type);
603           
604           /* Since expr is used twice below, save it.  */
605           expr = save_expr (expr);
606
607           expr1 = expr;
608           if (tc == REFERENCE_TYPE)
609             expr1 = build_unary_op (ADDR_EXPR, expr1, 0);
610
611           elems = tree_cons
612             (NULL_TREE, expr1, tree_cons
613              (NULL_TREE, td3, tree_cons
614               (NULL_TREE, td2, tree_cons
615                (NULL_TREE, boff, NULL_TREE))));
616
617           dcast_fn = dynamic_cast_node;
618           if (!dcast_fn)
619             {
620               tree tmp;
621               tree tinfo_ptr;
622               tree ns = abi_node;
623               const char *name;
624               
625               push_nested_namespace (ns);
626               tinfo_ptr = xref_tag (class_type,
627                                     get_identifier ("__class_type_info"),
628                                     /*tag_scope=*/ts_current, false);
629               
630               tinfo_ptr = build_pointer_type
631                 (build_qualified_type
632                  (tinfo_ptr, TYPE_QUAL_CONST));
633               name = "__dynamic_cast";
634               tmp = tree_cons
635                 (NULL_TREE, const_ptr_type_node, tree_cons
636                  (NULL_TREE, tinfo_ptr, tree_cons
637                   (NULL_TREE, tinfo_ptr, tree_cons
638                    (NULL_TREE, ptrdiff_type_node, void_list_node))));
639               tmp = build_function_type (ptr_type_node, tmp);
640               dcast_fn = build_library_fn_ptr (name, tmp);
641               DECL_IS_PURE (dcast_fn) = 1;
642               pop_nested_namespace (ns);
643               dynamic_cast_node = dcast_fn;
644             }
645           result = build_cxx_call (dcast_fn, elems);
646
647           if (tc == REFERENCE_TYPE)
648             {
649               tree bad = throw_bad_cast ();
650               
651               result = save_expr (result);
652               return build3 (COND_EXPR, type, result, result, bad);
653             }
654
655           /* Now back to the type we want from a void*.  */
656           result = cp_convert (type, result);
657           return ifnonnull (expr, result);
658         }
659     }
660   else
661     errstr = "source type is not polymorphic";
662
663  fail:
664   error ("cannot dynamic_cast %qE (of type %q#T) to type %q#T (%s)",
665          expr, exprtype, type, errstr);
666   return error_mark_node;
667 }
668
669 tree
670 build_dynamic_cast (tree type, tree expr)
671 {
672   if (type == error_mark_node || expr == error_mark_node)
673     return error_mark_node;
674   
675   if (processing_template_decl)
676     {
677       expr = build_min (DYNAMIC_CAST_EXPR, type, expr);
678       TREE_SIDE_EFFECTS (expr) = 1;
679       
680       return expr;
681     }
682
683   return convert_from_reference (build_dynamic_cast_1 (type, expr));
684 }
685 \f
686 /* Return the runtime bit mask encoding the qualifiers of TYPE.  */
687
688 static int
689 qualifier_flags (tree type)
690 {
691   int flags = 0;
692   int quals = cp_type_quals (type);
693   
694   if (quals & TYPE_QUAL_CONST)
695     flags |= 1;
696   if (quals & TYPE_QUAL_VOLATILE)
697     flags |= 2;
698   if (quals & TYPE_QUAL_RESTRICT)
699     flags |= 4;
700   return flags;
701 }
702
703 /* Return true, if the pointer chain TYPE ends at an incomplete type, or
704    contains a pointer to member of an incomplete class.  */
705
706 static bool
707 target_incomplete_p (tree type)
708 {
709   while (true)
710     if (TYPE_PTRMEM_P (type))
711       {
712         if (!COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type)))
713           return true;
714         type = TYPE_PTRMEM_POINTED_TO_TYPE (type);
715       }
716     else if (TREE_CODE (type) == POINTER_TYPE)
717       type = TREE_TYPE (type);
718     else
719       return !COMPLETE_OR_VOID_TYPE_P (type);
720 }
721
722 /* Returns true if TYPE involves an incomplete class type; in that
723    case, typeinfo variables for TYPE should be emitted with internal
724    linkage.  */
725
726 static bool
727 involves_incomplete_p (tree type)
728 {
729   switch (TREE_CODE (type))
730     {
731     case POINTER_TYPE:
732       return target_incomplete_p (TREE_TYPE (type));
733
734     case OFFSET_TYPE:
735     ptrmem:
736       return 
737         (target_incomplete_p (TYPE_PTRMEM_POINTED_TO_TYPE (type))
738          || !COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type)));
739
740     case RECORD_TYPE:
741       if (TYPE_PTRMEMFUNC_P (type))
742         goto ptrmem;
743       /* Fall through.  */
744     case UNION_TYPE:
745       if (!COMPLETE_TYPE_P (type))
746         return true;
747
748     default:
749       /* All other types do not involve incomplete class types.  */
750       return false;
751     }
752 }
753
754 /* Return a CONSTRUCTOR for the common part of the type_info objects. This
755    is the vtable pointer and NTBS name.  The NTBS name is emitted as a
756    comdat const char array, so it becomes a unique key for the type. Generate
757    and emit that VAR_DECL here.  (We can't always emit the type_info itself
758    as comdat, because of pointers to incomplete.) */
759
760 static tree
761 tinfo_base_init (tree desc, tree target)
762 {
763   tree init = NULL_TREE;
764   tree name_decl;
765   tree vtable_ptr;
766   
767   {
768     tree name_name;
769     
770     /* Generate the NTBS array variable.  */
771     tree name_type = build_cplus_array_type
772                      (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
773                      NULL_TREE);
774     tree name_string = tinfo_name (target);
775
776     /* Determine the name of the variable -- and remember with which
777        type it is associated.  */
778     name_name = mangle_typeinfo_string_for_type (target);
779     TREE_TYPE (name_name) = target;
780
781     name_decl = build_lang_decl (VAR_DECL, name_name, name_type);
782     SET_DECL_ASSEMBLER_NAME (name_decl, name_name);
783     DECL_ARTIFICIAL (name_decl) = 1;
784     DECL_IGNORED_P (name_decl) = 1;
785     TREE_READONLY (name_decl) = 1;
786     TREE_STATIC (name_decl) = 1;
787     DECL_EXTERNAL (name_decl) = 0;
788     DECL_TINFO_P (name_decl) = 1;
789     if (involves_incomplete_p (target))
790       {
791         TREE_PUBLIC (name_decl) = 0;
792         DECL_INTERFACE_KNOWN (name_decl) = 1;
793       }
794     else
795       set_linkage_according_to_type (target, name_decl);
796     import_export_decl (name_decl);
797     DECL_INITIAL (name_decl) = name_string;
798     mark_used (name_decl);
799     pushdecl_top_level_and_finish (name_decl, name_string);
800   }
801
802   vtable_ptr = TINFO_VTABLE_DECL (desc);
803   if (!vtable_ptr)
804     {
805       tree real_type;
806   
807       push_nested_namespace (abi_node);
808       real_type = xref_tag (class_type, TINFO_REAL_NAME (desc),
809                             /*tag_scope=*/ts_current, false);
810       pop_nested_namespace (abi_node);
811   
812       if (!COMPLETE_TYPE_P (real_type))
813         {
814           /* We never saw a definition of this type, so we need to
815              tell the compiler that this is an exported class, as
816              indeed all of the __*_type_info classes are.  */
817           SET_CLASSTYPE_INTERFACE_KNOWN (real_type);
818           CLASSTYPE_INTERFACE_ONLY (real_type) = 1;
819         }
820
821       vtable_ptr = get_vtable_decl (real_type, /*complete=*/1);
822       vtable_ptr = build_unary_op (ADDR_EXPR, vtable_ptr, 0);
823
824       /* We need to point into the middle of the vtable.  */
825       vtable_ptr = build2
826         (PLUS_EXPR, TREE_TYPE (vtable_ptr), vtable_ptr,
827          size_binop (MULT_EXPR,
828                      size_int (2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE),
829                      TYPE_SIZE_UNIT (vtable_entry_type)));
830
831       TINFO_VTABLE_DECL (desc) = vtable_ptr;
832     }
833
834   init = tree_cons (NULL_TREE, vtable_ptr, init);
835   
836   init = tree_cons (NULL_TREE, decay_conversion (name_decl), init);
837   
838   init = build_constructor (NULL_TREE, nreverse (init));
839   TREE_CONSTANT (init) = 1;
840   TREE_INVARIANT (init) = 1;
841   TREE_STATIC (init) = 1;
842   init = tree_cons (NULL_TREE, init, NULL_TREE);
843   
844   return init;
845 }
846
847 /* Return the CONSTRUCTOR expr for a type_info of TYPE. DESC provides the
848    information about the particular type_info derivation, which adds no
849    additional fields to the type_info base.  */
850
851 static tree
852 generic_initializer (tree desc, tree target)
853 {
854   tree init = tinfo_base_init (desc, target);
855   
856   init = build_constructor (NULL_TREE, init);
857   TREE_CONSTANT (init) = 1;
858   TREE_INVARIANT (init) = 1;
859   TREE_STATIC (init) = 1;
860   return init;
861 }
862
863 /* Return the CONSTRUCTOR expr for a type_info of pointer TYPE.
864    DESC provides information about the particular type_info derivation,
865    which adds target type and qualifier flags members to the type_info base.  */
866
867 static tree
868 ptr_initializer (tree desc, tree target)
869 {
870   tree init = tinfo_base_init (desc, target);
871   tree to = TREE_TYPE (target);
872   int flags = qualifier_flags (to);
873   bool incomplete = target_incomplete_p (to);
874   
875   if (incomplete)
876     flags |= 8;
877   init = tree_cons (NULL_TREE, build_int_cst (NULL_TREE, flags), init);
878   init = tree_cons (NULL_TREE,
879                     get_tinfo_ptr (TYPE_MAIN_VARIANT (to)),
880                     init);
881   
882   init = build_constructor (NULL_TREE, nreverse (init));
883   TREE_CONSTANT (init) = 1;
884   TREE_INVARIANT (init) = 1;
885   TREE_STATIC (init) = 1;
886   return init;
887 }
888
889 /* Return the CONSTRUCTOR expr for a type_info of pointer to member data TYPE.
890    DESC provides information about the particular type_info derivation,
891    which adds class, target type and qualifier flags members to the type_info
892    base.  */
893
894 static tree
895 ptm_initializer (tree desc, tree target)
896 {
897   tree init = tinfo_base_init (desc, target);
898   tree to = TYPE_PTRMEM_POINTED_TO_TYPE (target);
899   tree klass = TYPE_PTRMEM_CLASS_TYPE (target);
900   int flags = qualifier_flags (to);
901   bool incomplete = target_incomplete_p (to);
902   
903   if (incomplete)
904     flags |= 0x8;
905   if (!COMPLETE_TYPE_P (klass))
906     flags |= 0x10;
907   init = tree_cons (NULL_TREE, build_int_cst (NULL_TREE, flags), init);
908   init = tree_cons (NULL_TREE,
909                     get_tinfo_ptr (TYPE_MAIN_VARIANT (to)),
910                     init);
911   init = tree_cons (NULL_TREE,
912                     get_tinfo_ptr (klass),
913                     init);  
914   
915   init = build_constructor (NULL_TREE, nreverse (init));
916   TREE_CONSTANT (init) = 1;
917   TREE_INVARIANT (init) = 1;
918   TREE_STATIC (init) = 1;
919   return init;  
920 }
921
922 /* Return the CONSTRUCTOR expr for a type_info of class TYPE.
923    DESC provides information about the particular __class_type_info derivation,
924    which adds hint flags and TRAIL initializers to the type_info base.  */
925
926 static tree
927 class_initializer (tree desc, tree target, tree trail)
928 {
929   tree init = tinfo_base_init (desc, target);
930   
931   TREE_CHAIN (init) = trail;
932   init = build_constructor (NULL_TREE, init);
933   TREE_CONSTANT (init) = 1;
934   TREE_INVARIANT (init) = 1;
935   TREE_STATIC (init) = 1;
936   return init;  
937 }
938
939 /* Returns true if the typeinfo for type should be placed in
940    the runtime library.  */
941
942 static bool
943 typeinfo_in_lib_p (tree type)
944 {
945   /* The typeinfo objects for `T*' and `const T*' are in the runtime
946      library for simple types T.  */
947   if (TREE_CODE (type) == POINTER_TYPE
948       && (cp_type_quals (TREE_TYPE (type)) == TYPE_QUAL_CONST
949           || cp_type_quals (TREE_TYPE (type)) == TYPE_UNQUALIFIED))
950     type = TREE_TYPE (type);
951
952   switch (TREE_CODE (type))
953     {
954     case INTEGER_TYPE:
955     case BOOLEAN_TYPE:
956     case CHAR_TYPE:
957     case REAL_TYPE:
958     case VOID_TYPE:
959       return true;
960     
961     default:
962       return false;
963     }
964 }
965
966 /* Generate the initializer for the type info describing TYPE.  */
967
968 static tree
969 get_pseudo_ti_init (tree type, tree var_desc)
970 {
971   gcc_assert (at_eof);
972   switch (TREE_CODE (type))
973     {
974     case OFFSET_TYPE:
975       return ptm_initializer (var_desc, type);
976     case POINTER_TYPE:
977       return ptr_initializer (var_desc, type);
978     case ENUMERAL_TYPE:
979       return generic_initializer (var_desc, type);
980       break;
981     case FUNCTION_TYPE:
982       return generic_initializer (var_desc, type);
983       break;
984     case ARRAY_TYPE:
985       return generic_initializer (var_desc, type);
986       break;
987     case UNION_TYPE:
988     case RECORD_TYPE:
989       if (TYPE_PTRMEMFUNC_P (type))
990         return ptm_initializer (var_desc, type);
991       else if (var_desc == class_desc_type_node)
992         return class_initializer (var_desc, type, NULL_TREE);
993       else if (var_desc == si_class_desc_type_node)
994         {
995           tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), 0);
996           tree tinfo = get_tinfo_ptr (BINFO_TYPE (base_binfo));
997           tree base_inits = tree_cons (NULL_TREE, tinfo, NULL_TREE);
998           
999           return class_initializer (var_desc, type, base_inits);
1000         }
1001       else
1002         {
1003           int hint = ((CLASSTYPE_REPEATED_BASE_P (type) << 0)
1004                       | (CLASSTYPE_DIAMOND_SHAPED_P (type) << 1));
1005           tree binfo = TYPE_BINFO (type);
1006           int nbases = BINFO_N_BASE_BINFOS (binfo);
1007           VEC(tree,gc) *base_accesses = BINFO_BASE_ACCESSES (binfo);
1008           tree base_inits = NULL_TREE;
1009           int ix;
1010           
1011           /* Generate the base information initializer.  */
1012           for (ix = nbases; ix--;)
1013             {
1014               tree base_binfo = BINFO_BASE_BINFO (binfo, ix);
1015               tree base_init = NULL_TREE;
1016               int flags = 0;
1017               tree tinfo;
1018               tree offset;
1019               
1020               if (VEC_index (tree, base_accesses, ix) == access_public_node)
1021                 flags |= 2;
1022               tinfo = get_tinfo_ptr (BINFO_TYPE (base_binfo));
1023               if (BINFO_VIRTUAL_P (base_binfo))
1024                 {
1025                    /* We store the vtable offset at which the virtual
1026                       base offset can be found.  */
1027                   offset = BINFO_VPTR_FIELD (base_binfo);
1028                   offset = convert (sizetype, offset);
1029                   flags |= 1;
1030                 }
1031               else
1032                 offset = BINFO_OFFSET (base_binfo);
1033               
1034               /* Combine offset and flags into one field.  */
1035               offset = cp_build_binary_op (LSHIFT_EXPR, offset,
1036                                            build_int_cst (NULL_TREE, 8));
1037               offset = cp_build_binary_op (BIT_IOR_EXPR, offset,
1038                                            build_int_cst (NULL_TREE, flags));
1039               base_init = tree_cons (NULL_TREE, offset, base_init);
1040               base_init = tree_cons (NULL_TREE, tinfo, base_init);
1041               base_init = build_constructor (NULL_TREE, base_init);
1042               base_inits = tree_cons (NULL_TREE, base_init, base_inits);
1043             }
1044           base_inits = build_constructor (NULL_TREE, base_inits);
1045           base_inits = tree_cons (NULL_TREE, base_inits, NULL_TREE);
1046           /* Prepend the number of bases.  */
1047           base_inits = tree_cons (NULL_TREE,
1048                                   build_int_cst (NULL_TREE, nbases),
1049                                   base_inits);
1050           /* Prepend the hint flags.  */
1051           base_inits = tree_cons (NULL_TREE,
1052                                   build_int_cst (NULL_TREE, hint),
1053                                   base_inits);
1054
1055           return class_initializer (var_desc, type, base_inits);
1056         }
1057       break;
1058
1059     default:
1060       return generic_initializer (var_desc, type);
1061     }
1062 }
1063
1064 /* Generate the RECORD_TYPE containing the data layout of a type_info
1065    derivative as used by the runtime. This layout must be consistent with
1066    that defined in the runtime support. Also generate the VAR_DECL for the
1067    type's vtable. We explicitly manage the vtable member, and name it for
1068    real type as used in the runtime. The RECORD type has a different name,
1069    to avoid collisions.  Return a TREE_LIST who's TINFO_PSEUDO_TYPE
1070    is the generated type and TINFO_VTABLE_NAME is the name of the
1071    vtable.  We have to delay generating the VAR_DECL of the vtable
1072    until the end of the translation, when we'll have seen the library
1073    definition, if there was one.
1074    
1075    REAL_NAME is the runtime's name of the type. Trailing arguments are
1076    additional FIELD_DECL's for the structure. The final argument must be
1077    NULL.  */
1078
1079 static tree
1080 create_pseudo_type_info (const char *real_name, int ident, ...)
1081 {
1082   tree pseudo_type;
1083   char *pseudo_name;
1084   tree fields;
1085   tree field_decl;
1086   tree result;
1087   va_list ap;
1088
1089   va_start (ap, ident);
1090
1091   /* Generate the pseudo type name.  */
1092   pseudo_name = alloca (strlen (real_name) + 30);
1093   strcpy (pseudo_name, real_name);
1094   strcat (pseudo_name, "_pseudo");
1095   if (ident)
1096     sprintf (pseudo_name + strlen (pseudo_name), "%d", ident);
1097   
1098   /* First field is the pseudo type_info base class.  */
1099   fields = build_decl (FIELD_DECL, NULL_TREE, ti_desc_type_node);
1100   
1101   /* Now add the derived fields.  */
1102   while ((field_decl = va_arg (ap, tree)))
1103     {
1104       TREE_CHAIN (field_decl) = fields;
1105       fields = field_decl;
1106     }
1107   
1108   /* Create the pseudo type.  */
1109   pseudo_type = make_aggr_type (RECORD_TYPE);
1110   finish_builtin_struct (pseudo_type, pseudo_name, fields, NULL_TREE);
1111   CLASSTYPE_AS_BASE (pseudo_type) = pseudo_type;
1112
1113   result = tree_cons (NULL_TREE, NULL_TREE, NULL_TREE);
1114   TINFO_REAL_NAME (result) = get_identifier (real_name);
1115   TINFO_PSEUDO_TYPE (result) =
1116     cp_build_qualified_type (pseudo_type, TYPE_QUAL_CONST);
1117   
1118   va_end (ap);
1119   return result;
1120 }
1121
1122 /* Return a pseudo type info type node used to describe TYPE.  TYPE
1123    must be a complete type (or cv void), except at the end of the
1124    translation unit.  */
1125
1126 static tree
1127 get_pseudo_ti_desc (tree type)
1128 {
1129   switch (TREE_CODE (type))
1130     {
1131     case OFFSET_TYPE:
1132       return ptm_desc_type_node;
1133     case POINTER_TYPE:
1134       return ptr_desc_type_node;
1135     case ENUMERAL_TYPE:
1136       return enum_desc_type_node;
1137     case FUNCTION_TYPE:
1138       return func_desc_type_node;
1139     case ARRAY_TYPE:
1140       return ary_desc_type_node;
1141     case UNION_TYPE:
1142     case RECORD_TYPE:
1143       if (TYPE_PTRMEMFUNC_P (type))
1144         return ptm_desc_type_node;
1145       else if (!COMPLETE_TYPE_P (type))
1146         {
1147           if (!at_eof)
1148             cxx_incomplete_type_error (NULL_TREE, type);
1149           return class_desc_type_node;
1150         }
1151       else if (!BINFO_N_BASE_BINFOS (TYPE_BINFO (type)))
1152         return class_desc_type_node;
1153       else
1154         {
1155           tree binfo = TYPE_BINFO (type);
1156           VEC(tree,gc) *base_accesses = BINFO_BASE_ACCESSES (binfo);
1157           tree base_binfo = BINFO_BASE_BINFO (binfo, 0);
1158           int num_bases = BINFO_N_BASE_BINFOS (binfo);
1159           
1160           if (num_bases == 1
1161               && VEC_index (tree, base_accesses, 0) == access_public_node
1162               && !BINFO_VIRTUAL_P (base_binfo)
1163               && integer_zerop (BINFO_OFFSET (base_binfo)))
1164             /* single non-virtual public.  */
1165             return si_class_desc_type_node;
1166           else
1167             {
1168               tree var_desc;
1169               tree array_domain, base_array;
1170               
1171               if (TREE_VEC_LENGTH (vmi_class_desc_type_node) <= num_bases)
1172                 {
1173                   int ix;
1174                   tree extend = make_tree_vec (num_bases + 5);
1175                   
1176                   for (ix = TREE_VEC_LENGTH (vmi_class_desc_type_node); ix--;)
1177                     TREE_VEC_ELT (extend, ix)
1178                       = TREE_VEC_ELT (vmi_class_desc_type_node, ix);
1179                   vmi_class_desc_type_node = extend;
1180                 }
1181               var_desc = TREE_VEC_ELT (vmi_class_desc_type_node, num_bases);
1182               if (var_desc)
1183                 return var_desc;
1184   
1185               /* Create the array of __base_class_type_info entries.
1186                  G++ 3.2 allocated an array that had one too many
1187                  entries, and then filled that extra entries with
1188                  zeros.  */
1189               if (abi_version_at_least (2))
1190                 array_domain = build_index_type (size_int (num_bases - 1));
1191               else
1192                 array_domain = build_index_type (size_int (num_bases));
1193               base_array =
1194                 build_array_type (base_desc_type_node, array_domain);
1195
1196               push_nested_namespace (abi_node);
1197               var_desc = create_pseudo_type_info
1198                 ("__vmi_class_type_info", num_bases,
1199                  build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1200                  build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1201                  build_decl (FIELD_DECL, NULL_TREE, base_array),
1202                  NULL);
1203               pop_nested_namespace (abi_node);
1204
1205               TREE_VEC_ELT (vmi_class_desc_type_node, num_bases) = var_desc;
1206               return var_desc;
1207             }
1208         }
1209     default:
1210       return bltn_desc_type_node;
1211     }
1212 }
1213
1214 /* Make sure the required builtin types exist for generating the type_info
1215    variable definitions.  */
1216
1217 static void
1218 create_tinfo_types (void)
1219 {
1220   gcc_assert (!ti_desc_type_node);
1221
1222   push_nested_namespace (abi_node);
1223   
1224   /* Create the internal type_info structure. This is used as a base for
1225      the other structures.  */
1226   {
1227     tree field, fields;
1228
1229     ti_desc_type_node = make_aggr_type (RECORD_TYPE);
1230     field = build_decl (FIELD_DECL, NULL_TREE, const_ptr_type_node);
1231     fields = field;
1232     
1233     field = build_decl (FIELD_DECL, NULL_TREE, const_string_type_node);
1234     TREE_CHAIN (field) = fields;
1235     fields = field;
1236     
1237     finish_builtin_struct (ti_desc_type_node, "__type_info_pseudo",
1238                            fields, NULL_TREE);
1239     TYPE_HAS_CONSTRUCTOR (ti_desc_type_node) = 1;
1240   }
1241   
1242   /* Fundamental type_info */
1243   bltn_desc_type_node = create_pseudo_type_info
1244       ("__fundamental_type_info", 0,
1245        NULL);
1246
1247   /* Array, function and enum type_info. No additional fields.  */
1248   ary_desc_type_node = create_pseudo_type_info
1249       ("__array_type_info", 0,
1250        NULL);
1251   func_desc_type_node = create_pseudo_type_info
1252        ("__function_type_info", 0,
1253         NULL);
1254   enum_desc_type_node = create_pseudo_type_info
1255        ("__enum_type_info", 0,
1256         NULL);
1257   
1258   /* Class type_info. Add a flags field.  */
1259   class_desc_type_node = create_pseudo_type_info
1260         ("__class_type_info", 0,
1261          NULL);
1262   
1263   /* Single public non-virtual base class. Add pointer to base class. 
1264      This is really a descendant of __class_type_info.  */
1265   si_class_desc_type_node = create_pseudo_type_info
1266            ("__si_class_type_info", 0,
1267             build_decl (FIELD_DECL, NULL_TREE, type_info_ptr_type),
1268             NULL);
1269   
1270   /* Base class internal helper. Pointer to base type, offset to base,
1271      flags.  */
1272   {
1273     tree field, fields;
1274     
1275     field = build_decl (FIELD_DECL, NULL_TREE, type_info_ptr_type);
1276     fields = field;
1277     
1278     field = build_decl (FIELD_DECL, NULL_TREE, integer_types[itk_long]);
1279     TREE_CHAIN (field) = fields;
1280     fields = field;
1281   
1282     base_desc_type_node = make_aggr_type (RECORD_TYPE);
1283     finish_builtin_struct (base_desc_type_node, "__base_class_type_info_pseudo",
1284                            fields, NULL_TREE);
1285     TYPE_HAS_CONSTRUCTOR (base_desc_type_node) = 1;
1286   }
1287   
1288   /* General hierarchy is created as necessary in this vector.  */
1289   vmi_class_desc_type_node = make_tree_vec (10);
1290   
1291   /* Pointer type_info. Adds two fields, qualification mask
1292      and pointer to the pointed to type.  This is really a descendant of
1293      __pbase_type_info.  */
1294   ptr_desc_type_node = create_pseudo_type_info
1295       ("__pointer_type_info", 0,
1296        build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1297        build_decl (FIELD_DECL, NULL_TREE, type_info_ptr_type),
1298        NULL);
1299
1300   /* Pointer to member data type_info.  Add qualifications flags,
1301      pointer to the member's type info and pointer to the class.
1302      This is really a descendant of __pbase_type_info.  */
1303   ptm_desc_type_node = create_pseudo_type_info
1304        ("__pointer_to_member_type_info", 0,
1305         build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1306         build_decl (FIELD_DECL, NULL_TREE, type_info_ptr_type),
1307         build_decl (FIELD_DECL, NULL_TREE, type_info_ptr_type),
1308         NULL);
1309
1310   pop_nested_namespace (abi_node);
1311 }
1312
1313 /* Emit the type_info descriptors which are guaranteed to be in the runtime
1314    support.  Generating them here guarantees consistency with the other
1315    structures.  We use the following heuristic to determine when the runtime
1316    is being generated.  If std::__fundamental_type_info is defined, and its
1317    destructor is defined, then the runtime is being built.  */
1318
1319 void
1320 emit_support_tinfos (void)
1321 {
1322   static tree *const fundamentals[] =
1323   {
1324     &void_type_node,
1325     &boolean_type_node,
1326     &wchar_type_node,
1327     &char_type_node, &signed_char_type_node, &unsigned_char_type_node,
1328     &short_integer_type_node, &short_unsigned_type_node,
1329     &integer_type_node, &unsigned_type_node,
1330     &long_integer_type_node, &long_unsigned_type_node,
1331     &long_long_integer_type_node, &long_long_unsigned_type_node,
1332     &float_type_node, &double_type_node, &long_double_type_node,
1333     0
1334   };
1335   int ix;
1336   tree bltn_type, dtor;
1337   
1338   push_nested_namespace (abi_node);
1339   bltn_type = xref_tag (class_type,
1340                         get_identifier ("__fundamental_type_info"), 
1341                         /*tag_scope=*/ts_current, false);
1342   pop_nested_namespace (abi_node);
1343   if (!COMPLETE_TYPE_P (bltn_type))
1344     return;
1345   dtor = CLASSTYPE_DESTRUCTORS (bltn_type);
1346   if (!dtor || DECL_EXTERNAL (dtor))
1347     return;
1348   doing_runtime = 1;
1349   for (ix = 0; fundamentals[ix]; ix++)
1350     {
1351       tree bltn = *fundamentals[ix];
1352       tree types[3];
1353       int i;
1354
1355       types[0] = bltn;
1356       types[1] = build_pointer_type (bltn);
1357       types[2] = build_pointer_type (build_qualified_type (bltn, 
1358                                                            TYPE_QUAL_CONST));
1359  
1360       for (i = 0; i < 3; ++i)
1361         {
1362           tree tinfo;
1363
1364           tinfo = get_tinfo_decl (types[i]);
1365           TREE_USED (tinfo) = 1;
1366           mark_needed (tinfo);
1367           /* The C++ ABI requires that these objects be COMDAT.  But,
1368              On systems without weak symbols, initialized COMDAT 
1369              objects are emitted with internal linkage.  (See
1370              comdat_linkage for details.)  Since we want these objects
1371              to have external linkage so that copies do not have to be
1372              emitted in code outside the runtime library, we make them
1373              non-COMDAT here.  */
1374           if (!flag_weak)
1375             {
1376               gcc_assert (TREE_PUBLIC (tinfo) && !DECL_COMDAT (tinfo));
1377               DECL_INTERFACE_KNOWN (tinfo) = 1;
1378             }
1379         }
1380     }
1381 }
1382
1383 /* Finish a type info decl. DECL_PTR is a pointer to an unemitted
1384    tinfo decl.  Determine whether it needs emitting, and if so
1385    generate the initializer.  */
1386
1387 bool
1388 emit_tinfo_decl (tree decl)
1389 {
1390   tree type = TREE_TYPE (DECL_NAME (decl));
1391   int in_library = typeinfo_in_lib_p (type);
1392   tree var_desc, var_init;
1393
1394   gcc_assert (DECL_TINFO_P (decl)); 
1395   
1396   if (in_library)
1397     {
1398       if (doing_runtime)
1399         DECL_EXTERNAL (decl) = 0;
1400       else
1401         {
1402           /* If we're not in the runtime, then DECL (which is already
1403              DECL_EXTERNAL) will not be defined here.  */
1404           DECL_INTERFACE_KNOWN (decl) = 1;
1405           return false;
1406         }
1407     }
1408   else if (involves_incomplete_p (type))
1409     {
1410       if (!decl_needed_p (decl))
1411         return false;
1412       /* If TYPE involves an incomplete class type, then the typeinfo
1413          object will be emitted with internal linkage.  There is no
1414          way to know whether or not types are incomplete until the end
1415          of the compilation, so this determination must be deferred
1416          until this point.  */
1417       TREE_PUBLIC (decl) = 0;
1418       DECL_EXTERNAL (decl) = 0;
1419       DECL_INTERFACE_KNOWN (decl) = 1;
1420     }
1421
1422   import_export_decl (decl);
1423   if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
1424     {
1425       DECL_EXTERNAL (decl) = 0;
1426       var_desc = get_pseudo_ti_desc (type);
1427       var_init = get_pseudo_ti_init (type, var_desc);
1428       DECL_INITIAL (decl) = var_init;
1429       mark_used (decl);
1430       cp_finish_decl (decl, var_init, NULL_TREE, 0);
1431       return true;
1432     }
1433   else
1434     return false;
1435 }