OSDN Git Service

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