OSDN Git Service

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