OSDN Git Service

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