OSDN Git Service

* Makefile.in (toplev.o, dwarfout.o, final.o): Don't depend on
[pf3gnuchains/gcc-fork.git] / gcc / cp / rtti.c
1 /* RunTime Type Identification
2    Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001
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 "tree.h"
27 #include "cp-tree.h"
28 #include "flags.h"
29 #include "output.h"
30 #include "assert.h"
31 #include "toplev.h"
32
33 /* Accessors for the type_info objects. We need to remember several things
34    about each of the type_info types. The global tree nodes such as
35    bltn_desc_type_node are TREE_LISTs, and these macros are used to access
36    the required information. */
37 /* The RECORD_TYPE of a type_info derived class. */
38 #define TINFO_PSEUDO_TYPE(NODE) TREE_TYPE (NODE)
39 /* The VAR_DECL of the vtable for the type_info derived class. */
40 #define TINFO_VTABLE_DECL(NODE) TREE_VALUE (NODE)
41
42 extern struct obstack permanent_obstack;
43
44 static tree build_headof_sub PARAMS((tree));
45 static tree build_headof PARAMS((tree));
46 static tree ifnonnull PARAMS((tree, tree));
47 static tree tinfo_name PARAMS((tree));
48 static tree get_base_offset PARAMS((tree, tree));
49 static tree build_dynamic_cast_1 PARAMS((tree, tree));
50 static tree throw_bad_cast PARAMS((void));
51 static tree throw_bad_typeid PARAMS((void));
52 static tree get_tinfo_decl_dynamic PARAMS((tree));
53 static tree tinfo_from_decl PARAMS((tree));
54 static int qualifier_flags PARAMS((tree));
55 static int target_incomplete_p PARAMS((tree));
56 static tree tinfo_base_init PARAMS((tree, tree));
57 static tree generic_initializer PARAMS((tree, tree));
58 static tree ptr_initializer PARAMS((tree, tree, int *));
59 static tree ptm_initializer PARAMS((tree, tree, int *));
60 static tree dfs_class_hint_mark PARAMS ((tree, void *));
61 static tree dfs_class_hint_unmark PARAMS ((tree, void *));
62 static int class_hint_flags PARAMS((tree));
63 static tree class_initializer PARAMS((tree, tree, tree));
64 static tree synthesize_tinfo_var PARAMS((tree, tree));
65 static tree create_real_tinfo_var PARAMS((tree, tree, tree, tree, int));
66 static tree create_pseudo_type_info PARAMS((const char *, int, ...));
67 static tree get_vmi_pseudo_type_info PARAMS((int));
68 static void create_tinfo_types PARAMS((void));
69 static int typeinfo_in_lib_p PARAMS((tree));
70
71 static int doing_runtime = 0;
72 \f
73 void
74 init_rtti_processing ()
75 {
76   if (flag_honor_std)
77     push_namespace (std_identifier);
78   type_info_type_node = xref_tag
79     (class_type_node, get_identifier ("type_info"), 1);
80   if (flag_honor_std)
81     pop_namespace ();
82   /* FIXME: These identifier prefixes are not set in stone yet.  */
83   tinfo_decl_id = get_identifier ("__ti");
84   tinfo_var_id = get_identifier ("__tn");
85   tinfo_decl_type = 
86     build_qualified_type (type_info_type_node, TYPE_QUAL_CONST);
87 }
88
89 /* Given a pointer to an object with at least one virtual table
90    pointer somewhere, return a pointer to a possible sub-object that
91    has a virtual table pointer in it that is the vtable parent for
92    that sub-object.  */
93
94 static tree
95 build_headof_sub (exp)
96      tree exp;
97 {
98   tree type = TREE_TYPE (TREE_TYPE (exp));
99   tree basetype = CLASSTYPE_RTTI (type);
100   tree binfo = get_binfo (basetype, type, 0);
101
102   exp = convert_pointer_to_real (binfo, exp);
103   return exp;
104 }
105
106 /* Given the expression EXP of type `class *', return the head of the
107    object pointed to by EXP with type cv void*, if the class has any
108    virtual functions (TYPE_POLYMORPHIC_P), else just return the
109    expression.  */
110
111 static tree
112 build_headof (exp)
113      tree exp;
114 {
115   tree type = TREE_TYPE (exp);
116   tree aref;
117   tree offset;
118   tree index;
119
120   my_friendly_assert (TREE_CODE (type) == POINTER_TYPE, 20000112);
121   type = TREE_TYPE (type);
122
123   if (!TYPE_POLYMORPHIC_P (type))
124     return exp;
125
126   /* If we don't have rtti stuff, get to a sub-object that does.  */
127   if (!CLASSTYPE_VFIELDS (TREE_TYPE (TREE_TYPE (exp))))
128     exp = build_headof_sub (exp);
129
130   /* We use this a couple of times below, protect it.  */
131   exp = save_expr (exp);
132
133   /* Under the new ABI, the offset-to-top field is at index -2 from
134      the vptr.  */
135   index = build_int_2 (-2, -1);
136
137   aref = build_vtbl_ref (build_indirect_ref (exp, NULL), index);
138
139   if (flag_vtable_thunks)
140     offset = aref;
141   else
142     offset = build_component_ref (aref, delta_identifier, NULL_TREE, 0);
143
144   type = build_qualified_type (ptr_type_node, 
145                                CP_TYPE_QUALS (TREE_TYPE (exp)));
146   return build (PLUS_EXPR, type, exp,
147                 cp_convert (ptrdiff_type_node, offset));
148 }
149
150 /* Get a bad_cast node for the program to throw...
151
152    See libstdc++/exception.cc for __throw_bad_cast */
153
154 static tree
155 throw_bad_cast ()
156 {
157   tree fn = get_identifier ("__cxa_bad_cast");
158   if (IDENTIFIER_GLOBAL_VALUE (fn))
159     fn = IDENTIFIER_GLOBAL_VALUE (fn);
160   else
161     fn = push_throw_library_fn (fn, build_function_type (ptr_type_node,
162                                                          void_list_node));
163   
164   return build_call (fn, NULL_TREE);
165 }
166
167 static tree
168 throw_bad_typeid ()
169 {
170   tree fn = get_identifier ("__cxa_bad_typeid");
171   if (IDENTIFIER_GLOBAL_VALUE (fn))
172     fn = IDENTIFIER_GLOBAL_VALUE (fn);
173   else
174     {
175       tree t = build_qualified_type (type_info_type_node, TYPE_QUAL_CONST);
176       t = build_function_type (build_reference_type (t), void_list_node);
177       fn = push_throw_library_fn (fn, t);
178     }
179
180   return build_call (fn, NULL_TREE);
181 }
182 \f
183 /* Return a pointer to type_info function associated with the expression EXP.
184    If EXP is a reference to a polymorphic class, return the dynamic type;
185    otherwise return the static type of the expression.  */
186
187 static tree
188 get_tinfo_decl_dynamic (exp)
189      tree exp;
190 {
191   tree type;
192   
193   if (exp == error_mark_node)
194     return error_mark_node;
195
196   type = TREE_TYPE (exp);
197
198   /* peel back references, so they match.  */
199   if (TREE_CODE (type) == REFERENCE_TYPE)
200     type = TREE_TYPE (type);
201
202   /* Peel off cv qualifiers.  */
203   type = TYPE_MAIN_VARIANT (type);
204   
205   if (!VOID_TYPE_P (type))
206     type = complete_type_or_else (type, exp);
207   
208   if (!type)
209     return error_mark_node;
210
211   /* If exp is a reference to polymorphic type, get the real type_info.  */
212   if (TYPE_POLYMORPHIC_P (type) && ! resolves_to_fixed_type_p (exp, 0))
213     {
214       /* build reference to type_info from vtable.  */
215       tree t;
216       tree index;
217
218       if (! flag_rtti)
219         error ("taking dynamic typeid of object with -fno-rtti");
220
221       /* If we don't have rtti stuff, get to a sub-object that does.  */
222       if (! CLASSTYPE_VFIELDS (type))
223         {
224           exp = build_unary_op (ADDR_EXPR, exp, 0);
225           exp = build_headof_sub (exp);
226           exp = build_indirect_ref (exp, NULL);
227         }
228
229       /* The RTTI information is at index -1.  */
230       index = integer_minus_one_node;
231       t = build_vfn_ref ((tree *) 0, exp, index);
232       TREE_TYPE (t) = build_pointer_type (tinfo_decl_type);
233       return t;
234     }
235
236   /* otherwise return the type_info for the static type of the expr.  */
237   exp = get_tinfo_decl (TYPE_MAIN_VARIANT (type));
238   return build_unary_op (ADDR_EXPR, exp, 0);
239 }
240
241 tree
242 build_typeid (exp)
243      tree exp;
244 {
245   tree cond = NULL_TREE;
246   int nonnull = 0;
247
248   if (! flag_rtti)
249     {
250       error ("cannot use typeid with -fno-rtti");
251       return error_mark_node;
252     }
253   
254   if (!COMPLETE_TYPE_P (type_info_type_node))
255     {
256       error ("must #include <typeinfo> before using typeid");
257       return error_mark_node;
258     }
259   
260   if (processing_template_decl)
261     return build_min_nt (TYPEID_EXPR, exp);
262
263   if (TREE_CODE (exp) == INDIRECT_REF
264       && TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == POINTER_TYPE
265       && TYPE_POLYMORPHIC_P (TREE_TYPE (exp))
266       && ! resolves_to_fixed_type_p (exp, &nonnull)
267       && ! nonnull)
268     {
269       exp = stabilize_reference (exp);
270       cond = cp_convert (boolean_type_node, TREE_OPERAND (exp, 0));
271     }
272
273   exp = get_tinfo_decl_dynamic (exp);
274
275   if (exp == error_mark_node)
276     return error_mark_node;
277
278   exp = tinfo_from_decl (exp);
279
280   if (cond)
281     {
282       tree bad = throw_bad_typeid ();
283
284       exp = build (COND_EXPR, TREE_TYPE (exp), cond, exp, bad);
285     }
286
287   return convert_from_reference (exp);
288 }
289
290 /* Generate the NTBS name of a type.  */
291 static tree
292 tinfo_name (type)
293      tree type;
294 {
295   const char *name;
296   tree name_string;
297
298   name = mangle_type_string (type);
299   name_string = combine_strings (build_string (strlen (name) + 1, name));
300   return name_string;
301 }
302
303 /* Returns a decl for a function or variable which can be used to obtain a
304    type_info object for TYPE.  The old-abi uses functions, the new-abi
305    uses the type_info object directly.  You can take the address of the
306    returned decl, to save the decl.  To use the decl call
307    tinfo_from_decl.  You must arrange that the decl is mark_used, if
308    actually use it --- decls in vtables are only used if the vtable is
309    output.  */ 
310
311 tree
312 get_tinfo_decl (type)
313      tree type;
314 {
315   tree name;
316   tree d;
317
318   if (COMPLETE_TYPE_P (type) 
319       && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
320     {
321       cp_error ("cannot create type information for type `%T' because its size is variable", 
322                 type);
323       return error_mark_node;
324     }
325
326   if (TREE_CODE (type) == OFFSET_TYPE)
327     type = TREE_TYPE (type);
328   if (TREE_CODE (type) == METHOD_TYPE)
329     type = build_function_type (TREE_TYPE (type),
330                                 TREE_CHAIN (TYPE_ARG_TYPES (type)));
331
332   name = mangle_typeinfo_for_type (type);
333
334   d = IDENTIFIER_GLOBAL_VALUE (name);
335   if (d)
336     /* OK */;
337   else
338     {
339       /* The tinfo decl is the type_info object itself.  We make all
340          tinfo objects look as type_info, even though they will end up
341          being a subclass of that when emitted.  This means that we'll
342          erroneously think we know the dynamic type -- be careful in the
343          runtime.  */
344       d = build_lang_decl (VAR_DECL, name, tinfo_decl_type);
345       
346       DECL_ARTIFICIAL (d) = 1;
347       DECL_ALIGN (d) = TYPE_ALIGN (ptr_type_node);
348       DECL_USER_ALIGN (d) = 0;
349       TREE_READONLY (d) = 1;
350       TREE_STATIC (d) = 1;
351       DECL_EXTERNAL (d) = 1;
352       TREE_PUBLIC (d) = 1;
353       if (flag_weak || !typeinfo_in_lib_p (d))
354         comdat_linkage (d);
355       SET_DECL_ASSEMBLER_NAME (d, name);
356       cp_finish_decl (d, NULL_TREE, NULL_TREE, 0);
357
358       pushdecl_top_level (d);
359       /* Remember the type it is for.  */
360       TREE_TYPE (name) = type;
361       TREE_USED (name) = 1;
362     }
363   return d;
364 }
365
366 /* Given an expr produced by get_tinfo_decl, return an expr which
367    produces a reference to the type_info object.  */
368
369 static tree
370 tinfo_from_decl (expr)
371      tree expr;
372 {
373   tree t;
374   
375   if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
376     t = build_indirect_ref (expr, NULL);
377   else
378     t = expr;
379   
380   return t;
381 }
382
383 tree
384 get_typeid_1 (type)
385      tree type;
386 {
387   tree t;
388
389   t = get_tinfo_decl (type);
390   t = tinfo_from_decl (t);
391   return convert_from_reference (t);
392 }
393   
394 /* Return the type_info object for TYPE.  */
395
396 tree
397 get_typeid (type)
398      tree type;
399 {
400   if (type == error_mark_node)
401     return error_mark_node;
402
403   if (!COMPLETE_TYPE_P (type_info_type_node))
404     {
405       error ("must #include <typeinfo> before using typeid");
406       return error_mark_node;
407     }
408   
409   if (processing_template_decl)
410     return build_min_nt (TYPEID_EXPR, type);
411
412   /* If the type of the type-id is a reference type, the result of the
413      typeid expression refers to a type_info object representing the
414      referenced type.  */
415   if (TREE_CODE (type) == REFERENCE_TYPE)
416     type = TREE_TYPE (type);
417
418   /* The top-level cv-qualifiers of the lvalue expression or the type-id
419      that is the operand of typeid are always ignored.  */
420   type = TYPE_MAIN_VARIANT (type);
421
422   if (!VOID_TYPE_P (type))
423     type = complete_type_or_else (type, NULL_TREE);
424   
425   if (!type)
426     return error_mark_node;
427
428   return get_typeid_1 (type);
429 }
430
431 /* Check whether TEST is null before returning RESULT.  If TEST is used in
432    RESULT, it must have previously had a save_expr applied to it.  */
433
434 static tree
435 ifnonnull (test, result)
436      tree test, result;
437 {
438   return build (COND_EXPR, TREE_TYPE (result),
439                 build (EQ_EXPR, boolean_type_node, test, integer_zero_node),
440                 cp_convert (TREE_TYPE (result), integer_zero_node),
441                 result);
442 }
443
444 /* Generate the constant expression describing where direct base BINFO
445    appears within the PARENT. How to interpret this expression depends on
446    details of the ABI, which the runtime must be aware of.  */
447
448 static tree
449 get_base_offset (binfo, parent)
450      tree binfo;
451      tree parent;
452 {
453   if (! TREE_VIA_VIRTUAL (binfo))
454     return BINFO_OFFSET (binfo);
455   else if (! vbase_offsets_in_vtable_p ())
456     {
457       const char *name;
458       tree result;
459       tree field;
460     
461       FORMAT_VBASE_NAME (name, BINFO_TYPE (binfo));
462       field = lookup_field (parent, get_identifier (name), 0, 0);
463       result = byte_position (field);
464       
465       if (DECL_CONTEXT (field) != parent)
466         {
467           /* The vbase pointer might be in a non-virtual base of PARENT.
468            * Adjust for the offset of that base in PARENT.  */
469           tree path;
470           
471           get_base_distance (DECL_CONTEXT (field), parent, -1, &path);
472           result = build (PLUS_EXPR, TREE_TYPE (result),
473                           result, BINFO_OFFSET (path));
474           result = fold (result);
475         }
476       return result;
477     }
478   else
479     /* Under the new ABI, we store the vtable offset at which
480        the virtual base offset can be found.  */
481     return convert (sizetype,
482                     BINFO_VPTR_FIELD (binfo_for_vbase (BINFO_TYPE (binfo),
483                                                        parent)));
484
485 }
486
487 /* Execute a dynamic cast, as described in section 5.2.6 of the 9/93 working
488    paper.  */
489
490 static tree
491 build_dynamic_cast_1 (type, expr)
492      tree type, expr;
493 {
494   enum tree_code tc = TREE_CODE (type);
495   tree exprtype = TREE_TYPE (expr);
496   tree dcast_fn;
497   tree old_expr = expr;
498   const char *errstr = NULL;
499
500   /* T shall be a pointer or reference to a complete class type, or
501      `pointer to cv void''.  */
502   switch (tc)
503     {
504     case POINTER_TYPE:
505       if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
506         break;
507     case REFERENCE_TYPE:
508       if (! IS_AGGR_TYPE (TREE_TYPE (type)))
509         {
510           errstr = "target is not pointer or reference to class";
511           goto fail;
512         }
513       if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
514         {
515           errstr = "target is not pointer or reference to complete type";
516           goto fail;
517         }
518       break;
519
520     default:
521       errstr = "target is not pointer or reference";
522       goto fail;
523     }
524
525   if (TREE_CODE (expr) == OFFSET_REF)
526     {
527       expr = resolve_offset_ref (expr);
528       exprtype = TREE_TYPE (expr);
529     }
530
531   if (tc == POINTER_TYPE)
532     expr = convert_from_reference (expr);
533   else if (TREE_CODE (exprtype) != REFERENCE_TYPE)
534     {
535       /* Apply trivial conversion T -> T& for dereferenced ptrs.  */
536       exprtype = build_reference_type (exprtype);
537       expr = convert_to_reference (exprtype, expr, CONV_IMPLICIT,
538                                    LOOKUP_NORMAL, NULL_TREE);
539     }
540
541   exprtype = TREE_TYPE (expr);
542
543   if (tc == POINTER_TYPE)
544     {
545       /* If T is a pointer type, v shall be an rvalue of a pointer to
546          complete class type, and the result is an rvalue of type T.  */
547
548       if (TREE_CODE (exprtype) != POINTER_TYPE)
549         {
550           errstr = "source is not a pointer";
551           goto fail;
552         }
553       if (! IS_AGGR_TYPE (TREE_TYPE (exprtype)))
554         {
555           errstr = "source is not a pointer to class";
556           goto fail;
557         }
558       if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
559         {
560           errstr = "source is a pointer to incomplete type";
561           goto fail;
562         }
563     }
564   else
565     {
566       /* T is a reference type, v shall be an lvalue of a complete class
567          type, and the result is an lvalue of the type referred to by T.  */
568
569       if (! IS_AGGR_TYPE (TREE_TYPE (exprtype)))
570         {
571           errstr = "source is not of class type";
572           goto fail;
573         }
574       if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
575         {
576           errstr = "source is of incomplete class type";
577           goto fail;
578         }
579       
580     }
581
582   /* The dynamic_cast operator shall not cast away constness.  */
583   if (!at_least_as_qualified_p (TREE_TYPE (type),
584                                 TREE_TYPE (exprtype)))
585     {
586       errstr = "conversion casts away constness";
587       goto fail;
588     }
589
590   /* If *type is an unambiguous accessible base class of *exprtype,
591      convert statically.  */
592   {
593     int distance;
594     tree path;
595
596     distance = get_base_distance (TREE_TYPE (type), TREE_TYPE (exprtype), 1,
597                                   &path);
598
599     if (distance == -2)
600       {
601         cp_error ("dynamic_cast from `%T' to ambiguous base class `%T'",
602                   TREE_TYPE (exprtype), TREE_TYPE (type));
603         return error_mark_node;
604       }
605     if (distance == -3)
606       {
607         cp_error ("dynamic_cast from `%T' to private base class `%T'",
608                   TREE_TYPE (exprtype), TREE_TYPE (type));
609         return error_mark_node;
610       }
611
612     if (distance >= 0)
613       {
614         expr = build_vbase_path (PLUS_EXPR, type, expr, path, 0);
615         if (TREE_CODE (exprtype) == POINTER_TYPE)
616           expr = non_lvalue (expr);
617         return expr;
618       }
619   }
620
621   /* Otherwise *exprtype must be a polymorphic class (have a vtbl).  */
622   if (TYPE_POLYMORPHIC_P (TREE_TYPE (exprtype)))
623     {
624       tree expr1;
625       /* if TYPE is `void *', return pointer to complete object.  */
626       if (tc == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (type)))
627         {
628           /* if b is an object, dynamic_cast<void *>(&b) == (void *)&b.  */
629           if (TREE_CODE (expr) == ADDR_EXPR
630               && TREE_CODE (TREE_OPERAND (expr, 0)) == VAR_DECL
631               && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == RECORD_TYPE)
632             return build1 (NOP_EXPR, type, expr);
633
634           /* Since expr is used twice below, save it.  */
635           expr = save_expr (expr);
636
637           expr1 = build_headof (expr);
638           if (TREE_TYPE (expr1) != type)
639             expr1 = build1 (NOP_EXPR, type, expr1);
640           return ifnonnull (expr, expr1);
641         }
642       else
643         {
644           tree retval;
645           tree result, td2, td3, elems;
646           tree static_type, target_type, boff;
647
648           /* If we got here, we can't convert statically.  Therefore,
649              dynamic_cast<D&>(b) (b an object) cannot succeed.  */
650           if (tc == REFERENCE_TYPE)
651             {
652               if (TREE_CODE (old_expr) == VAR_DECL
653                   && TREE_CODE (TREE_TYPE (old_expr)) == RECORD_TYPE)
654                 {
655                   tree expr = throw_bad_cast ();
656                   cp_warning ("dynamic_cast of `%#D' to `%#T' can never succeed",
657                               old_expr, type);
658                   /* Bash it to the expected type.  */
659                   TREE_TYPE (expr) = type;
660                   return expr;
661                 }
662             }
663           /* Ditto for dynamic_cast<D*>(&b).  */
664           else if (TREE_CODE (expr) == ADDR_EXPR)
665             {
666               tree op = TREE_OPERAND (expr, 0);
667               if (TREE_CODE (op) == VAR_DECL
668                   && TREE_CODE (TREE_TYPE (op)) == RECORD_TYPE)
669                 {
670                   cp_warning ("dynamic_cast of `%#D' to `%#T' can never succeed",
671                               op, type);
672                   retval = build_int_2 (0, 0); 
673                   TREE_TYPE (retval) = type; 
674                   return retval;
675                 }
676             }
677
678           target_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
679           static_type = TYPE_MAIN_VARIANT (TREE_TYPE (exprtype));
680           td2 = build_unary_op (ADDR_EXPR, get_tinfo_decl (target_type), 0);
681           td3 = build_unary_op (ADDR_EXPR, get_tinfo_decl (static_type), 0);
682
683           /* Determine how T and V are related.  */
684           boff = get_dynamic_cast_base_type (static_type, target_type);
685           
686           /* Since expr is used twice below, save it.  */
687           expr = save_expr (expr);
688
689           expr1 = expr;
690           if (tc == REFERENCE_TYPE)
691             expr1 = build_unary_op (ADDR_EXPR, expr1, 0);
692
693           elems = tree_cons
694             (NULL_TREE, expr1, tree_cons
695              (NULL_TREE, td3, tree_cons
696               (NULL_TREE, td2, tree_cons
697                (NULL_TREE, boff, NULL_TREE))));
698
699           dcast_fn = dynamic_cast_node;
700           if (!dcast_fn)
701             {
702               tree tmp;
703               tree tinfo_ptr;
704               tree ns = abi_node;
705               const char *name;
706               
707               push_nested_namespace (ns);
708               tinfo_ptr = xref_tag (class_type_node,
709                                     get_identifier ("__class_type_info"),
710                                     1);
711               
712               tinfo_ptr = build_pointer_type
713                 (build_qualified_type
714                  (tinfo_ptr, TYPE_QUAL_CONST));
715               name = "__dynamic_cast";
716               tmp = tree_cons
717                 (NULL_TREE, const_ptr_type_node, tree_cons
718                  (NULL_TREE, tinfo_ptr, tree_cons
719                   (NULL_TREE, tinfo_ptr, tree_cons
720                    (NULL_TREE, ptrdiff_type_node, void_list_node))));
721               tmp = build_function_type (ptr_type_node, tmp);
722               dcast_fn = build_library_fn_ptr (name, tmp);
723               pop_nested_namespace (ns);
724               dynamic_cast_node = dcast_fn;
725             }
726           result = build_call (dcast_fn, elems);
727
728           if (tc == REFERENCE_TYPE)
729             {
730               tree bad = throw_bad_cast ();
731               
732               result = save_expr (result);
733               return build (COND_EXPR, type, result, result, bad);
734             }
735
736           /* Now back to the type we want from a void*.  */
737           result = cp_convert (type, result);
738           return ifnonnull (expr, result);
739         }
740     }
741   else
742     errstr = "source type is not polymorphic";
743
744  fail:
745   cp_error ("cannot dynamic_cast `%E' (of type `%#T') to type `%#T' (%s)",
746             expr, exprtype, type, errstr);
747   return error_mark_node;
748 }
749
750 tree
751 build_dynamic_cast (type, expr)
752      tree type, expr;
753 {
754   if (type == error_mark_node || expr == error_mark_node)
755     return error_mark_node;
756   
757   if (processing_template_decl)
758     return build_min (DYNAMIC_CAST_EXPR, type, expr);
759
760   return convert_from_reference (build_dynamic_cast_1 (type, expr));
761 }
762 \f
763 /* Return the runtime bit mask encoding the qualifiers of TYPE.  */
764
765 static int
766 qualifier_flags (type)
767      tree type;
768 {
769   int flags = 0;
770   /* we want the qualifiers on this type, not any array core, it might have */
771   int quals = TYPE_QUALS (type);
772   
773   if (quals & TYPE_QUAL_CONST)
774     flags |= 1;
775   if (quals & TYPE_QUAL_VOLATILE)
776     flags |= 2;
777   if (quals & TYPE_QUAL_RESTRICT)
778     flags |= 4;
779   return flags;
780 }
781
782 /* Return non-zero, if the pointer chain TYPE ends at an incomplete type, or
783    contains a pointer to member of an incomplete class.  */
784
785 static int
786 target_incomplete_p (type)
787      tree type;
788 {
789   while (TREE_CODE (type) == POINTER_TYPE)
790     if (TYPE_PTRMEM_P (type))
791       {
792         if (!COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type)))
793           return 1;
794         type = TYPE_PTRMEM_POINTED_TO_TYPE (type);
795       }
796     else
797       type = TREE_TYPE (type);
798   if (!COMPLETE_OR_VOID_TYPE_P (type))
799     return 1;
800   
801   return 0;
802 }
803
804 /* Return a CONSTRUCTOR for the common part of the type_info objects. This
805    is the vtable pointer and NTBS name.  The NTBS name is emitted as a
806    comdat const char array, so it becomes a unique key for the type. Generate
807    and emit that VAR_DECL here.  (We can't always emit the type_info itself
808    as comdat, because of pointers to incomplete.) */
809
810 static tree
811 tinfo_base_init (desc, target)
812      tree desc;
813      tree target;
814 {
815   tree init = NULL_TREE;
816   tree name_decl;
817   
818   {
819     tree name_name;
820     
821     /* Generate the NTBS array variable.  */
822     tree name_type = build_cplus_array_type
823                      (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
824                      NULL_TREE);
825     tree name_string = tinfo_name (target);
826
827     name_name = mangle_typeinfo_string_for_type (target);
828     name_decl = build_lang_decl (VAR_DECL, name_name, name_type);
829     
830     DECL_ARTIFICIAL (name_decl) = 1;
831     TREE_READONLY (name_decl) = 1;
832     TREE_STATIC (name_decl) = 1;
833     DECL_EXTERNAL (name_decl) = 0;
834     TREE_PUBLIC (name_decl) = 1;
835     comdat_linkage (name_decl);
836     /* The new ABI specifies the external name of the string
837        containing the type's name.  */
838     SET_DECL_ASSEMBLER_NAME (name_decl,
839                              mangle_typeinfo_string_for_type (target));
840     DECL_INITIAL (name_decl) = name_string;
841     cp_finish_decl (name_decl, name_string, NULL_TREE, 0);
842     pushdecl_top_level (name_decl);
843   }
844   
845   if (TINFO_VTABLE_DECL (desc))
846     {
847       tree vtbl_ptr = TINFO_VTABLE_DECL (desc);
848       init = tree_cons (NULL_TREE, vtbl_ptr, init);
849     }
850   
851   init = tree_cons (NULL_TREE, decay_conversion (name_decl), init);
852   
853   init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, nreverse (init));
854   TREE_HAS_CONSTRUCTOR (init) = TREE_CONSTANT (init) = TREE_STATIC (init) = 1;
855   init = tree_cons (NULL_TREE, init, NULL_TREE);
856   
857   return init;
858 }
859
860 /* Return the CONSTRUCTOR expr for a type_info of TYPE. DESC provides the
861    information about the particular type_info derivation, which adds no
862    additional fields to the type_info base.  */
863
864 static tree
865 generic_initializer (desc, target)
866      tree desc;
867      tree target;
868 {
869   tree init = tinfo_base_init (desc, target);
870   
871   init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, init);
872   TREE_HAS_CONSTRUCTOR (init) = TREE_CONSTANT (init) = TREE_STATIC (init) = 1;
873   return init;
874 }
875
876 /* Return the CONSTRUCTOR expr for a type_info of pointer TYPE.
877    DESC provides information about the particular type_info derivation,
878    which adds target type and qualifier flags members to the type_info base.  */
879
880 static tree
881 ptr_initializer (desc, target, non_public_ptr)
882      tree desc;
883      tree target;
884      int *non_public_ptr;
885 {
886   tree init = tinfo_base_init (desc, target);
887   tree to = TREE_TYPE (target);
888   int flags = qualifier_flags (to);
889   int incomplete = target_incomplete_p (to);
890   
891   if (incomplete)
892     {
893       flags |= 8;
894       *non_public_ptr = 1;
895     }
896   init = tree_cons (NULL_TREE, build_int_2 (flags, 0), init);
897   init = tree_cons (NULL_TREE,
898                     build_unary_op (ADDR_EXPR,
899                                     get_tinfo_decl (TYPE_MAIN_VARIANT (to)), 0),
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 /* Return the CONSTRUCTOR expr for a type_info of pointer to member data TYPE.
908    DESC provides information about the particular type_info derivation,
909    which adds class, target type and qualifier flags members to the type_info
910    base.  */
911
912 static tree
913 ptm_initializer (desc, target, non_public_ptr)
914      tree desc;
915      tree target;
916      int *non_public_ptr;
917 {
918   tree init = tinfo_base_init (desc, target);
919   tree to = TYPE_PTRMEM_POINTED_TO_TYPE (target);
920   tree klass = TYPE_PTRMEM_CLASS_TYPE (target);
921   int flags = qualifier_flags (to);
922   int incomplete = target_incomplete_p (to);
923   
924   if (incomplete)
925     {
926       flags |= 0x8;
927       *non_public_ptr = 1;
928     }
929   if (!COMPLETE_TYPE_P (klass))
930     {
931       flags |= 0x10;
932       *non_public_ptr = 1;
933     }
934   init = tree_cons (NULL_TREE, build_int_2 (flags, 0), init);
935   init = tree_cons (NULL_TREE,
936                     build_unary_op (ADDR_EXPR,
937                                     get_tinfo_decl (TYPE_MAIN_VARIANT (to)), 0),
938                     init);
939   init = tree_cons (NULL_TREE,
940                     build_unary_op (ADDR_EXPR, get_tinfo_decl (klass), 0),
941                     init);  
942   
943   init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, nreverse (init));
944   TREE_HAS_CONSTRUCTOR (init) = TREE_CONSTANT (init) = TREE_STATIC (init) = 1;
945   return init;  
946 }
947
948 /* Check base BINFO to set hint flags in *DATA, which is really an int.
949    We use CLASSTYPE_MARKED to tag types we've found as non-virtual bases and
950    CLASSTYPE_MARKED2 to tag those which are virtual bases. Remember it is
951    possible for a type to be both a virtual and non-virtual base.  */
952
953 static tree
954 dfs_class_hint_mark (binfo, data)
955      tree binfo;
956      void *data;
957 {
958   tree basetype = BINFO_TYPE (binfo);
959   int *hint = (int *) data;
960   
961   if (TREE_VIA_VIRTUAL (binfo))
962     {
963       if (CLASSTYPE_MARKED (basetype))
964         *hint |= 1;
965       if (CLASSTYPE_MARKED2 (basetype))
966         *hint |= 2;
967       SET_CLASSTYPE_MARKED2 (basetype);
968     }
969   else
970     {
971       if (CLASSTYPE_MARKED (basetype) || CLASSTYPE_MARKED2 (basetype))
972         *hint |= 1;
973       SET_CLASSTYPE_MARKED (basetype);
974     }
975   if (!TREE_VIA_PUBLIC (binfo) && TYPE_BINFO (basetype) != binfo)
976     *hint |= 4;
977   return NULL_TREE;
978 };
979
980 /* Clear the base's dfs marks, after searching for duplicate bases. */
981
982 static tree
983 dfs_class_hint_unmark (binfo, data)
984      tree binfo;
985      void *data ATTRIBUTE_UNUSED;
986 {
987   tree basetype = BINFO_TYPE (binfo);
988   
989   CLEAR_CLASSTYPE_MARKED (basetype);
990   CLEAR_CLASSTYPE_MARKED2 (basetype);
991   return NULL_TREE;
992 }
993
994 /* Determine the hint flags describing the features of a class's heirarchy.  */
995
996 static int
997 class_hint_flags (type)
998      tree type;
999 {
1000   int hint_flags = 0;
1001   int i;
1002   
1003   dfs_walk (TYPE_BINFO (type), dfs_class_hint_mark, NULL, &hint_flags);
1004   dfs_walk (TYPE_BINFO (type), dfs_class_hint_unmark, NULL, NULL);
1005   
1006   for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); ++i)
1007     {
1008       tree base_binfo = BINFO_BASETYPE (TYPE_BINFO (type), i);
1009       
1010       if (TREE_VIA_PUBLIC (base_binfo))
1011         hint_flags |= 0x8;
1012     }
1013   return hint_flags;
1014 }
1015         
1016 /* Return the CONSTRUCTOR expr for a type_info of class TYPE.
1017    DESC provides information about the particular __class_type_info derivation,
1018    which adds hint flags and TRAIL initializers to the type_info base.  */
1019
1020 static tree
1021 class_initializer (desc, target, trail)
1022      tree desc;
1023      tree target;
1024      tree trail;
1025 {
1026   tree init = tinfo_base_init (desc, target);
1027   
1028   TREE_CHAIN (init) = trail;
1029   init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, init);
1030   TREE_HAS_CONSTRUCTOR (init) = TREE_CONSTANT (init) = TREE_STATIC (init) = 1;
1031   return init;  
1032 }
1033
1034 /* Returns non-zero if the typeinfo for type should be placed in 
1035    the runtime library.  */
1036
1037 static int
1038 typeinfo_in_lib_p (type)
1039      tree type;
1040 {
1041   /* The typeinfo objects for `T*' and `const T*' are in the runtime
1042      library for simple types T.  */
1043   if (TREE_CODE (type) == POINTER_TYPE
1044       && (CP_TYPE_QUALS (TREE_TYPE (type)) == TYPE_QUAL_CONST
1045           || CP_TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED))
1046     type = TREE_TYPE (type);
1047
1048   switch (TREE_CODE (type))
1049     {
1050     case INTEGER_TYPE:
1051     case BOOLEAN_TYPE:
1052     case CHAR_TYPE:
1053     case REAL_TYPE:
1054     case VOID_TYPE:
1055       return 1;
1056     
1057     default:
1058       return 0;
1059     }
1060 }
1061
1062 /* Generate a pseudo_type_info VAR_DECL suitable for the supplied
1063    TARGET_TYPE and given the REAL_NAME. This is the structure expected by
1064    the runtime, and therefore has additional fields.  If we need not emit a
1065    definition (because the runtime must contain it), return NULL_TREE,
1066    otherwise return the VAR_DECL.  */
1067
1068 static tree
1069 synthesize_tinfo_var (target_type, real_name)
1070      tree target_type;
1071      tree real_name;
1072 {
1073   tree var_init = NULL_TREE;
1074   tree var_type = NULL_TREE;
1075   int non_public = 0;
1076   
1077   switch (TREE_CODE (target_type))
1078     {
1079     case POINTER_TYPE:
1080       if (TYPE_PTRMEM_P (target_type))
1081         {
1082           var_type = ptm_desc_type_node;
1083           var_init = ptm_initializer (var_type, target_type, &non_public);
1084         }
1085       else
1086         {
1087           if (typeinfo_in_lib_p (target_type) && !doing_runtime)
1088             /* These are in the runtime.  */
1089             return NULL_TREE;
1090           var_type = ptr_desc_type_node;
1091           var_init = ptr_initializer (var_type, target_type, &non_public);
1092         }
1093       break;
1094     case ENUMERAL_TYPE:
1095       var_type = enum_desc_type_node;
1096       var_init = generic_initializer (var_type, target_type);
1097       break;
1098     case FUNCTION_TYPE:
1099       var_type = func_desc_type_node;
1100       var_init = generic_initializer (var_type, target_type);
1101       break;
1102     case ARRAY_TYPE:
1103       var_type = ary_desc_type_node;
1104       var_init = generic_initializer (var_type, target_type);
1105       break;
1106     case UNION_TYPE:
1107     case RECORD_TYPE:
1108       if (TYPE_PTRMEMFUNC_P (target_type))
1109         {
1110           var_type = ptm_desc_type_node;
1111           var_init = ptm_initializer (var_type, target_type, &non_public);
1112         }
1113       else if (!COMPLETE_TYPE_P (target_type))
1114         {
1115           /* Emit a non-public class_type_info.  */
1116           non_public = 1;
1117           var_type = class_desc_type_node;
1118           var_init = class_initializer (var_type, target_type, NULL_TREE);
1119         }
1120       else if (!CLASSTYPE_N_BASECLASSES (target_type))
1121         {
1122           var_type = class_desc_type_node;
1123           var_init = class_initializer (var_type, target_type, NULL_TREE);
1124         }
1125       else
1126         {
1127           /* if this has a single public non-virtual base, it's easier */
1128           tree binfo = TYPE_BINFO (target_type);
1129           int nbases = BINFO_N_BASETYPES (binfo);
1130           tree base_binfos = BINFO_BASETYPES (binfo);
1131           tree base_inits = NULL_TREE;
1132           int is_simple = nbases == 1;
1133           int ix;
1134           
1135           /* Generate the base information initializer.  */
1136           for (ix = nbases; ix--;)
1137             {
1138               tree base_binfo = TREE_VEC_ELT (base_binfos, ix);
1139               tree base_init = NULL_TREE;
1140               int flags = 0;
1141               tree tinfo;
1142               tree offset;
1143               
1144               if (TREE_VIA_VIRTUAL (base_binfo))
1145                 flags |= 1;
1146               if (TREE_PUBLIC (base_binfo))
1147                 flags |= 2;
1148               tinfo = get_tinfo_decl (BINFO_TYPE (base_binfo));
1149               tinfo = build_unary_op (ADDR_EXPR, tinfo, 0);
1150               offset = get_base_offset (base_binfo, target_type);
1151               
1152               /* is it a single public inheritance? */
1153               if (is_simple && flags == 2 && integer_zerop (offset))
1154                 {
1155                   base_inits = tree_cons (NULL_TREE, tinfo, NULL_TREE);
1156                   break;
1157                 }
1158               is_simple = 0;
1159               
1160               /* combine offset and flags into one field */
1161               offset = cp_build_binary_op (LSHIFT_EXPR, offset,
1162                                            build_int_2 (8, 0));
1163               offset = cp_build_binary_op (BIT_IOR_EXPR, offset,
1164                                            build_int_2 (flags, 0));
1165               base_init = tree_cons (NULL_TREE, offset, base_init);
1166               base_init = tree_cons (NULL_TREE, tinfo, base_init);
1167               base_init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, base_init);
1168               base_inits = tree_cons (NULL_TREE, base_init, base_inits);
1169             }
1170           
1171           if (is_simple)
1172             var_type = si_class_desc_type_node;
1173           else
1174             {
1175               int hint = class_hint_flags (target_type);
1176               
1177               base_inits = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, base_inits);
1178               base_inits = tree_cons (NULL_TREE, base_inits, NULL_TREE);
1179               /* Prepend the number of bases.  */
1180               base_inits = tree_cons (NULL_TREE,
1181                                       build_int_2 (nbases, 0), base_inits);
1182               /* Prepend the hint flags. */
1183               base_inits = tree_cons (NULL_TREE,
1184                                       build_int_2 (hint, 0), base_inits);
1185               var_type = get_vmi_pseudo_type_info (nbases);
1186             }
1187           var_init = class_initializer (var_type, target_type, base_inits);
1188         }
1189       break;
1190
1191     default:
1192       if (typeinfo_in_lib_p (target_type))
1193         {
1194           if (!doing_runtime)
1195             /* These are guaranteed to be in the runtime.  */
1196             return NULL_TREE;
1197           var_type = bltn_desc_type_node;
1198           var_init = generic_initializer (var_type, target_type);
1199           break;
1200         }
1201       my_friendly_abort (20000117);
1202     }
1203   
1204   
1205   return create_real_tinfo_var (target_type,
1206                                 real_name, TINFO_PSEUDO_TYPE (var_type),
1207                                 var_init, non_public);
1208 }
1209
1210 /* Create the real typeinfo variable.  NON_PUBLIC indicates that we cannot
1211    make this variable public (comdat). */
1212
1213 static tree
1214 create_real_tinfo_var (target_type, name, type, init, non_public)
1215      tree target_type;
1216      tree name;
1217      tree type;
1218      tree init;
1219      int non_public;
1220 {
1221   static int count = 0;
1222   tree decl;
1223   tree hidden_name;
1224   char hidden[30];
1225   
1226   sprintf (hidden, "%.*s_%d",
1227            IDENTIFIER_LENGTH (tinfo_decl_id), IDENTIFIER_POINTER (tinfo_decl_id),
1228            count++);
1229   hidden_name = get_identifier (hidden);
1230   
1231   decl = build_lang_decl (VAR_DECL, hidden_name,
1232                           build_qualified_type (type, TYPE_QUAL_CONST));
1233   DECL_ARTIFICIAL (decl) = 1;
1234   TREE_READONLY (decl) = 1;
1235   TREE_STATIC (decl) = 1;
1236   DECL_EXTERNAL (decl) = 0;
1237   
1238   if (!non_public)
1239     {
1240       TREE_PUBLIC (decl) = 1;
1241       if (flag_weak || !typeinfo_in_lib_p (target_type))
1242         comdat_linkage (decl);
1243     }
1244   SET_DECL_ASSEMBLER_NAME (decl, name);
1245   DECL_INITIAL (decl) = init;
1246   cp_finish_decl (decl, init, NULL_TREE, 0);
1247   pushdecl_top_level (decl);
1248   TREE_USED (decl) = 1;
1249   return decl;
1250 }
1251
1252 /* Generate the RECORD_TYPE containing the data layout of a type_info
1253    derivative as used by the runtime. This layout must be consistent with
1254    that defined in the runtime support. Also generate the VAR_DECL for the
1255    type's vtable. We explicitly manage the vtable member, and name it for
1256    real type as used in the runtime. The RECORD type has a different name,
1257    to avoid collisions.  Return a TREE_LIST who's TINFO_PSEUDO_TYPE
1258    is the generated type and TINFO_VTABLE_DECL is the vtable decl.
1259    
1260    REAL_NAME is the runtime's name of the type. Trailing arguments are
1261    additional FIELD_DECL's for the structure. The final argument must be
1262    NULL.  */
1263
1264 static tree
1265 create_pseudo_type_info VPARAMS((const char *real_name, int ident, ...))
1266 {
1267 #ifndef ANSI_PROTOTYPES
1268   char const *real_name;
1269   int ident;
1270 #endif
1271   va_list ap;
1272   tree real_type, pseudo_type;
1273   char *pseudo_name;
1274   tree vtable_decl;
1275   int ix;
1276   tree fields[10];
1277   tree field_decl;
1278   tree result;
1279   
1280   VA_START (ap, ident);
1281 #ifndef ANSI_PROTOTYPES
1282   real_name = va_arg (ap, char const *);
1283   ident = va_arg (app, int);
1284 #endif
1285
1286   /* Generate the pseudo type name. */
1287   pseudo_name = (char *)alloca (strlen (real_name) + 30);
1288   strcpy (pseudo_name, real_name);
1289   strcat (pseudo_name, "_pseudo");
1290   if (ident)
1291     sprintf (pseudo_name + strlen (pseudo_name), "%d", ident);
1292   
1293   /* Get the vtable decl. */
1294   real_type = xref_tag (class_type_node, get_identifier (real_name), 1);
1295   vtable_decl = get_vtable_decl (real_type, /*complete=*/1);
1296   vtable_decl = build_unary_op (ADDR_EXPR, vtable_decl, 0);
1297
1298   /* Under the new ABI, we need to point into the middle of the
1299      vtable.  */
1300   vtable_decl = build (PLUS_EXPR,
1301                        TREE_TYPE (vtable_decl),
1302                        vtable_decl,
1303                        size_binop (MULT_EXPR,
1304                                    size_int (2),
1305                                    TYPE_SIZE_UNIT (vtable_entry_type)));
1306   TREE_CONSTANT (vtable_decl) = 1;
1307
1308   /* First field is the pseudo type_info base class. */
1309   fields[0] = build_decl (FIELD_DECL, NULL_TREE, ti_desc_type_node);
1310   
1311   /* Now add the derived fields.  */
1312   for (ix = 0; (field_decl = va_arg (ap, tree));)
1313     fields[++ix] = field_decl;
1314   
1315   /* Create the pseudo type. */
1316   pseudo_type = make_aggr_type (RECORD_TYPE);
1317   finish_builtin_type (pseudo_type, pseudo_name, fields, ix, ptr_type_node);
1318   TYPE_HAS_CONSTRUCTOR (pseudo_type) = 1;
1319   va_end (ap);
1320   
1321   result = tree_cons (NULL_TREE, NULL_TREE, NULL_TREE);
1322   TINFO_VTABLE_DECL (result) = vtable_decl;
1323   TINFO_PSEUDO_TYPE (result) = pseudo_type;
1324   
1325   return result;
1326 }
1327
1328 /* Return a descriptor for a vmi type with NUM_BASES bases.  */
1329
1330 static tree
1331 get_vmi_pseudo_type_info (num_bases)
1332      int num_bases;
1333 {
1334   tree desc;
1335   tree array_domain, base_array;
1336   
1337   if (TREE_VEC_LENGTH (vmi_class_desc_type_node) <= num_bases)
1338     {
1339       int ix;
1340       tree extend = make_tree_vec (num_bases + 5);
1341       
1342       for (ix = TREE_VEC_LENGTH (vmi_class_desc_type_node); ix--;)
1343         TREE_VEC_ELT (extend, ix) = TREE_VEC_ELT (vmi_class_desc_type_node, ix);
1344       vmi_class_desc_type_node = extend;
1345     }
1346   desc = TREE_VEC_ELT (vmi_class_desc_type_node, num_bases);
1347   
1348   if (desc)
1349     return desc;
1350   
1351   /* Add number of bases and trailing array of base_class_type_info.  */
1352   array_domain = build_index_type (size_int (num_bases));
1353   base_array = build_array_type (base_desc_type_node, array_domain);
1354
1355   push_nested_namespace (abi_node);
1356
1357   desc = create_pseudo_type_info
1358             ("__vmi_class_type_info", num_bases,
1359              build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1360              build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1361              build_decl (FIELD_DECL, NULL_TREE, base_array),
1362              NULL);
1363
1364   pop_nested_namespace (abi_node);
1365
1366   TREE_VEC_ELT (vmi_class_desc_type_node, num_bases) = desc;
1367   return desc;
1368 }
1369
1370 /* Make sure the required builtin types exist for generating the type_info
1371    varable definitions.  */
1372
1373 static void
1374 create_tinfo_types ()
1375 {
1376   tree ptr_type_info;
1377   
1378   if (bltn_desc_type_node)
1379     return;
1380   push_nested_namespace (abi_node);
1381
1382   ptr_type_info = build_pointer_type
1383                     (build_qualified_type
1384                       (type_info_type_node, TYPE_QUAL_CONST));
1385   
1386   /* Create the internal type_info structure. This is used as a base for
1387      the other structures.  */
1388   {
1389     tree fields[2];
1390
1391     ti_desc_type_node = make_aggr_type (RECORD_TYPE);
1392     fields[0] = build_decl (FIELD_DECL, NULL_TREE, const_ptr_type_node);
1393     fields[1] = build_decl (FIELD_DECL, NULL_TREE, const_string_type_node);
1394     finish_builtin_type (ti_desc_type_node, "__type_info_pseudo",
1395                          fields, 1, ptr_type_node);
1396     TYPE_HAS_CONSTRUCTOR (ti_desc_type_node) = 1;
1397   }
1398   
1399   /* Fundamental type_info */
1400   bltn_desc_type_node = create_pseudo_type_info
1401       ("__fundamental_type_info", 0,
1402        NULL);
1403
1404   /* Array, function and enum type_info. No additional fields. */
1405   ary_desc_type_node = create_pseudo_type_info
1406       ("__array_type_info", 0,
1407        NULL);
1408   func_desc_type_node = create_pseudo_type_info
1409        ("__function_type_info", 0,
1410         NULL);
1411   enum_desc_type_node = create_pseudo_type_info
1412        ("__enum_type_info", 0,
1413         NULL);
1414   
1415   /* Class type_info. Add a flags field.  */
1416   class_desc_type_node = create_pseudo_type_info
1417         ("__class_type_info", 0,
1418          NULL);
1419   
1420   /* Single public non-virtual base class. Add pointer to base class. 
1421      This is really a descendant of __class_type_info.  */
1422   si_class_desc_type_node = create_pseudo_type_info
1423            ("__si_class_type_info", 0,
1424             build_decl (FIELD_DECL, NULL_TREE, ptr_type_info),
1425             NULL);
1426   
1427   /* Base class internal helper. Pointer to base type, offset to base,
1428      flags. */
1429   {
1430     tree fields[2];
1431     
1432     fields[0] = build_decl (FIELD_DECL, NULL_TREE, ptr_type_info);
1433     fields[1] = build_decl (FIELD_DECL, NULL_TREE, integer_types[itk_long]);
1434     base_desc_type_node = make_aggr_type (RECORD_TYPE);
1435     finish_builtin_type (base_desc_type_node, "__base_class_type_info_pseudo",
1436                          fields, 1, ptr_type_node);
1437     TYPE_HAS_CONSTRUCTOR (base_desc_type_node) = 1;
1438   }
1439   
1440   /* General heirarchy is created as necessary in this vector. */
1441   vmi_class_desc_type_node = make_tree_vec (10);
1442   
1443   /* Pointer type_info. Adds two fields, qualification mask
1444      and pointer to the pointed to type.  This is really a descendant of
1445      __pbase_type_info. */
1446   ptr_desc_type_node = create_pseudo_type_info
1447       ("__pointer_type_info", 0,
1448        build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1449        build_decl (FIELD_DECL, NULL_TREE, ptr_type_info),
1450        NULL);
1451
1452   /* Pointer to member data type_info.  Add qualifications flags,
1453      pointer to the member's type info and pointer to the class.
1454      This is really a descendant of __pbase_type_info.  */
1455   ptm_desc_type_node = create_pseudo_type_info
1456        ("__pointer_to_member_type_info", 0,
1457         build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1458         build_decl (FIELD_DECL, NULL_TREE, ptr_type_info),
1459         build_decl (FIELD_DECL, NULL_TREE, ptr_type_info),
1460         NULL);
1461
1462   pop_nested_namespace (abi_node);
1463 }
1464
1465 /* Emit the type_info descriptors which are guaranteed to be in the runtime
1466    support.  Generating them here guarantees consistency with the other
1467    structures.  We use the following heuristic to determine when the runtime
1468    is being generated.  If std::__fundamental_type_info is defined, and it's
1469    destructor is defined, then the runtime is being built.  */
1470
1471 void
1472 emit_support_tinfos ()
1473 {
1474   static tree *const fundamentals[] =
1475   {
1476     &void_type_node,
1477     &boolean_type_node,
1478     &wchar_type_node,
1479     &char_type_node, &signed_char_type_node, &unsigned_char_type_node,
1480     &short_integer_type_node, &short_unsigned_type_node,
1481     &integer_type_node, &unsigned_type_node,
1482     &long_integer_type_node, &long_unsigned_type_node,
1483     &long_long_integer_type_node, &long_long_unsigned_type_node,
1484     &float_type_node, &double_type_node, &long_double_type_node,
1485     0
1486   };
1487   int ix;
1488   tree bltn_type, dtor;
1489   
1490   push_nested_namespace (abi_node);
1491   bltn_type = xref_tag (class_type_node,
1492                         get_identifier ("__fundamental_type_info"), 1);
1493   pop_nested_namespace (abi_node);
1494   if (!COMPLETE_TYPE_P (bltn_type))
1495     return;
1496   dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (bltn_type), 1);
1497   if (DECL_EXTERNAL (dtor))
1498     return;
1499   doing_runtime = 1;
1500   for (ix = 0; fundamentals[ix]; ix++)
1501     {
1502       tree bltn = *fundamentals[ix];
1503       tree bltn_ptr = build_pointer_type (bltn);
1504       tree bltn_const_ptr = build_pointer_type
1505               (build_qualified_type (bltn, TYPE_QUAL_CONST));
1506       tree tinfo;
1507       
1508       tinfo = get_tinfo_decl (bltn);
1509       TREE_USED (tinfo) = 1;
1510       TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (tinfo)) = 1;
1511       
1512       tinfo = get_tinfo_decl (bltn_ptr);
1513       TREE_USED (tinfo) = 1;
1514       TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (tinfo)) = 1;
1515       
1516       tinfo = get_tinfo_decl (bltn_const_ptr);
1517       TREE_USED (tinfo) = 1;
1518       TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (tinfo)) = 1;
1519     }
1520 }
1521
1522 /* Return non-zero, iff T is a type_info variable which has not had a
1523    definition emitted for it.  */
1524
1525 int
1526 tinfo_decl_p (t, data)
1527      tree t;
1528      void *data ATTRIBUTE_UNUSED;
1529 {
1530   return TREE_CODE (t) == VAR_DECL
1531          && IDENTIFIER_GLOBAL_VALUE (DECL_NAME (t)) == (t)
1532          && TREE_TYPE (t) == tinfo_decl_type
1533          && TREE_TYPE (DECL_NAME (t));
1534 }
1535
1536 /* Emit a suitable type_info definition for the type_info decl pointed to by
1537    DECL_PTR. We emit a completely new variable, of the correct type for the
1538    actual type this is describing. The DECL_ASSEMBLER_NAME of the generated
1539    definition is set to that of the supplied decl, so that they can be tied
1540    up. Mark the supplied decl as having been dealt with. Emitting one
1541    definition might cause other definitions to be required.
1542    
1543    We need to do things this way, because we're trying to do something like
1544    
1545       struct B : A {
1546         ...
1547       };
1548    
1549       extern const A tinfo_var;
1550    
1551       const B tinfo_var = {...};
1552    
1553    which is not permitted. Also, we've not necessarily seen the definition of B.
1554    So we do something like the following,
1555    
1556       extern const A tinfo_var;
1557    
1558       struct pseudo_A {
1559         const void *vtable_ptr;
1560         const char *name;
1561       };
1562       struct pseudo_B {
1563         pseudo_A base;
1564         ...
1565       };
1566       
1567       const pseudo_B proxy_tinfo_var attribute((assembler_name="tinfo_var")) =
1568       {
1569         {&B::vtable, "..."},
1570         ...
1571       };
1572    
1573    pseudo_A and pseudo_B must be layout equivalent to the real definitions in
1574    the runtime.  */
1575
1576 int
1577 emit_tinfo_decl (decl_ptr, data)
1578      tree *decl_ptr;
1579      void *data ATTRIBUTE_UNUSED;
1580 {
1581   tree tinfo_decl = *decl_ptr;
1582   tree tinfo_type, decl;
1583   
1584   my_friendly_assert (TREE_TYPE (tinfo_decl) == tinfo_decl_type, 20000121);
1585   tinfo_type = TREE_TYPE (DECL_NAME (tinfo_decl));
1586   my_friendly_assert (tinfo_type != NULL_TREE, 20000120);
1587   
1588   if (!DECL_NEEDED_P (tinfo_decl))
1589     return 0;
1590   /* Say we've dealt with it.  */
1591   TREE_TYPE (DECL_NAME (tinfo_decl)) = NULL_TREE;
1592   
1593   create_tinfo_types ();
1594   decl = synthesize_tinfo_var (tinfo_type, DECL_ASSEMBLER_NAME (tinfo_decl));
1595   
1596   return decl != 0;
1597 }