OSDN Git Service

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