OSDN Git Service

* collect2.c (main, write_c_file_stat), gcc.c (translate_options,
[pf3gnuchains/gcc-fork.git] / gcc / cp / rtti.c
1 /* RunTime Type Identification
2    Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000
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 #include "defaults.h"
33
34 /* Accessors for the type_info objects. We need to remember several things
35    about each of the type_info types. The global tree nodes such as
36    bltn_desc_type_node are TREE_LISTs, and these macros are used to access
37    the required information. */
38 /* The RECORD_TYPE of a type_info derived class. */
39 #define TINFO_PSEUDO_TYPE(NODE) TREE_TYPE (NODE)
40 /* The VAR_DECL of the vtable for the type_info derived class. */
41 #define TINFO_VTABLE_DECL(NODE) TREE_VALUE (NODE)
42
43 extern struct obstack permanent_obstack;
44
45 static tree build_headof_sub PARAMS((tree));
46 static tree build_headof PARAMS((tree));
47 static tree get_tinfo_var PARAMS((tree));
48 static tree ifnonnull PARAMS((tree, tree));
49 static tree tinfo_name PARAMS((tree));
50 static tree get_base_offset PARAMS((tree, tree));
51 static tree build_dynamic_cast_1 PARAMS((tree, tree));
52 static void expand_si_desc PARAMS((tree, tree));
53 static void expand_class_desc PARAMS((tree, tree));
54 static void expand_attr_desc PARAMS((tree, tree));
55 static void expand_ptr_desc PARAMS((tree, tree));
56 static void expand_generic_desc PARAMS((tree, tree, const char *));
57 static tree throw_bad_cast PARAMS((void));
58 static tree throw_bad_typeid PARAMS((void));
59 static tree get_tinfo_decl_dynamic PARAMS((tree));
60 static tree tinfo_from_decl PARAMS((tree));
61 static int qualifier_flags PARAMS((tree));
62 static int target_incomplete_p PARAMS((tree));
63 static tree tinfo_base_init PARAMS((tree, tree));
64 static tree generic_initializer PARAMS((tree, tree));
65 static tree ptr_initializer PARAMS((tree, tree, int *));
66 static tree ptm_initializer PARAMS((tree, tree, int *));
67 static tree dfs_class_hint_mark PARAMS ((tree, void *));
68 static tree dfs_class_hint_unmark PARAMS ((tree, void *));
69 static int class_hint_flags PARAMS((tree));
70 static tree class_initializer PARAMS((tree, tree, tree));
71 static tree synthesize_tinfo_var PARAMS((tree, tree));
72 static tree create_real_tinfo_var PARAMS((tree, tree, tree, int));
73 static tree create_pseudo_type_info PARAMS((const char *, int, ...));
74 static tree get_vmi_pseudo_type_info PARAMS((int));
75 static void create_tinfo_types PARAMS((void));
76
77 static int doing_runtime = 0;
78 \f
79 void
80 init_rtti_processing ()
81 {
82   if (flag_honor_std)
83     push_namespace (std_identifier);
84   type_info_type_node = xref_tag
85     (class_type_node, get_identifier ("type_info"), 1);
86   if (flag_honor_std)
87     pop_namespace ();
88   if (!new_abi_rtti_p ())
89     {
90       tinfo_decl_id = get_identifier ("__tf");
91       tinfo_decl_type = build_function_type
92         (build_reference_type
93           (build_qualified_type
94             (type_info_type_node, TYPE_QUAL_CONST)),
95          void_list_node);
96       tinfo_var_id = get_identifier ("__ti");
97     }
98   else
99     {
100       /* FIXME: These identifier prefixes are not set in stone yet.  */
101       tinfo_decl_id = get_identifier ("__ti");
102       tinfo_var_id = get_identifier ("__tn");
103       tinfo_decl_type = build_qualified_type
104                           (type_info_type_node, TYPE_QUAL_CONST);
105     }
106 }
107
108 /* Given a pointer to an object with at least one virtual table
109    pointer somewhere, return a pointer to a possible sub-object that
110    has a virtual table pointer in it that is the vtable parent for
111    that sub-object.  */
112
113 static tree
114 build_headof_sub (exp)
115      tree exp;
116 {
117   tree type = TREE_TYPE (TREE_TYPE (exp));
118   tree basetype = CLASSTYPE_RTTI (type);
119   tree binfo = get_binfo (basetype, type, 0);
120
121   exp = convert_pointer_to_real (binfo, exp);
122   return exp;
123 }
124
125 /* Given the expression EXP of type `class *', return the head of the
126    object pointed to by EXP with type cv void*, if the class has any
127    virtual functions (TYPE_POLYMORPHIC_P), else just return the
128    expression.  */
129
130 static tree
131 build_headof (exp)
132      tree exp;
133 {
134   tree type = TREE_TYPE (exp);
135   tree aref;
136   tree offset;
137   tree index;
138
139   my_friendly_assert (TREE_CODE (type) == POINTER_TYPE, 20000112);
140   type = TREE_TYPE (type);
141
142   if (!TYPE_POLYMORPHIC_P (type))
143     return exp;
144   if (CLASSTYPE_COM_INTERFACE (type))
145     {
146       cp_error ("RTTI not supported for COM interface type `%T'", type);
147       return error_mark_node;
148     }
149
150   /* If we don't have rtti stuff, get to a sub-object that does.  */
151   if (!CLASSTYPE_VFIELDS (TREE_TYPE (TREE_TYPE (exp))))
152     exp = build_headof_sub (exp);
153
154   /* We use this a couple of times below, protect it.  */
155   exp = save_expr (exp);
156
157   /* Under the new ABI, the offset-to-top field is at index -2 from
158      the vptr.  */
159   if (new_abi_rtti_p ())
160     index = build_int_2 (-2, -1);
161   /* But under the old ABI, it is at offset zero.  */
162   else
163     index = integer_zero_node;
164
165   aref = build_vtbl_ref (build_indirect_ref (exp, NULL_PTR), index);
166
167   if (flag_vtable_thunks)
168     offset = aref;
169   else
170     offset = build_component_ref (aref, delta_identifier, NULL_TREE, 0);
171
172   type = build_qualified_type (ptr_type_node, 
173                                CP_TYPE_QUALS (TREE_TYPE (exp)));
174   return build (PLUS_EXPR, type, exp,
175                 cp_convert (ptrdiff_type_node, offset));
176 }
177
178 /* Get a bad_cast node for the program to throw...
179
180    See libstdc++/exception.cc for __throw_bad_cast */
181
182 static tree
183 throw_bad_cast ()
184 {
185   tree fn = get_identifier ("__throw_bad_cast");
186   if (IDENTIFIER_GLOBAL_VALUE (fn))
187     fn = IDENTIFIER_GLOBAL_VALUE (fn);
188   else
189     fn = push_throw_library_fn (fn, build_function_type (ptr_type_node,
190                                                          void_list_node));
191   
192   return build_call (fn, NULL_TREE);
193 }
194
195 static tree
196 throw_bad_typeid ()
197 {
198   tree fn = get_identifier ("__throw_bad_typeid");
199   if (IDENTIFIER_GLOBAL_VALUE (fn))
200     fn = IDENTIFIER_GLOBAL_VALUE (fn);
201   else
202     {
203       tree t = build_qualified_type (type_info_type_node, TYPE_QUAL_CONST);
204       t = build_function_type (build_reference_type (t), void_list_node);
205       fn = push_throw_library_fn (fn, t);
206     }
207
208   return build_call (fn, NULL_TREE);
209 }
210 \f
211 /* Return a pointer to type_info function associated with the expression EXP.
212    If EXP is a reference to a polymorphic class, return the dynamic type;
213    otherwise return the static type of the expression.  */
214
215 static tree
216 get_tinfo_decl_dynamic (exp)
217      tree exp;
218 {
219   tree type;
220   
221   if (exp == error_mark_node)
222     return error_mark_node;
223
224   type = TREE_TYPE (exp);
225
226   /* peel back references, so they match.  */
227   if (TREE_CODE (type) == REFERENCE_TYPE)
228     type = TREE_TYPE (type);
229
230   /* Peel off cv qualifiers.  */
231   type = TYPE_MAIN_VARIANT (type);
232   
233   if (!VOID_TYPE_P (type))
234     type = complete_type_or_else (type, exp);
235   
236   if (!type)
237     return error_mark_node;
238
239   /* If exp is a reference to polymorphic type, get the real type_info.  */
240   if (TYPE_POLYMORPHIC_P (type) && ! resolves_to_fixed_type_p (exp, 0))
241     {
242       /* build reference to type_info from vtable.  */
243       tree t;
244       tree index;
245
246       if (! flag_rtti)
247         error ("taking dynamic typeid of object with -fno-rtti");
248       if (CLASSTYPE_COM_INTERFACE (type))
249         {
250           cp_error ("RTTI not supported for COM interface type `%T'", type);
251           return error_mark_node;
252         }
253
254       /* If we don't have rtti stuff, get to a sub-object that does.  */
255       if (! CLASSTYPE_VFIELDS (type))
256         {
257           exp = build_unary_op (ADDR_EXPR, exp, 0);
258           exp = build_headof_sub (exp);
259           exp = build_indirect_ref (exp, NULL_PTR);
260         }
261
262       /* The RTTI information is always in the vtable, but it's at
263          different indices depending on the ABI.  */
264       if (new_abi_rtti_p ())
265         index = minus_one_node;
266       else if (flag_vtable_thunks)
267         index = integer_one_node;
268       else
269         index = integer_zero_node;
270       t = build_vfn_ref ((tree *) 0, exp, index);
271       TREE_TYPE (t) = build_pointer_type (tinfo_decl_type);
272       return t;
273     }
274
275   /* otherwise return the type_info for the static type of the expr.  */
276   exp = get_tinfo_decl (TYPE_MAIN_VARIANT (type));
277   return build_unary_op (ADDR_EXPR, exp, 0);
278 }
279
280 tree
281 build_typeid (exp)
282      tree exp;
283 {
284   tree cond = NULL_TREE;
285   int nonnull = 0;
286
287   if (! flag_rtti)
288     {
289       error ("cannot use typeid with -fno-rtti");
290       return error_mark_node;
291     }
292   
293   if (!COMPLETE_TYPE_P (type_info_type_node))
294     {
295       error ("must #include <typeinfo> before using typeid");
296       return error_mark_node;
297     }
298   
299   if (processing_template_decl)
300     return build_min_nt (TYPEID_EXPR, exp);
301
302   if (TREE_CODE (exp) == INDIRECT_REF
303       && TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == POINTER_TYPE
304       && TYPE_POLYMORPHIC_P (TREE_TYPE (exp))
305       && ! resolves_to_fixed_type_p (exp, &nonnull)
306       && ! nonnull)
307     {
308       exp = stabilize_reference (exp);
309       cond = cp_convert (boolean_type_node, TREE_OPERAND (exp, 0));
310     }
311
312   exp = get_tinfo_decl_dynamic (exp);
313
314   if (exp == error_mark_node)
315     return error_mark_node;
316
317   exp = tinfo_from_decl (exp);
318
319   if (cond)
320     {
321       tree bad = throw_bad_typeid ();
322
323       exp = build (COND_EXPR, TREE_TYPE (exp), cond, exp, bad);
324     }
325
326   return convert_from_reference (exp);
327 }
328
329 static tree
330 get_tinfo_var (type)
331      tree type;
332 {
333   tree tname = build_overload_with_type (tinfo_var_id, type);
334   tree arrtype;
335   int size;
336
337   my_friendly_assert (!new_abi_rtti_p (), 20000118);
338   if (IDENTIFIER_GLOBAL_VALUE (tname))
339     return IDENTIFIER_GLOBAL_VALUE (tname);
340     
341   /* Figure out how much space we need to allocate for the type_info object.
342      If our struct layout or the type_info classes are changed, this will
343      need to be modified.  */
344   if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
345     size = 3 * POINTER_SIZE + INT_TYPE_SIZE;
346   else if (TREE_CODE (type) == POINTER_TYPE
347            && ! (TREE_CODE (TREE_TYPE (type)) == OFFSET_TYPE
348                  || TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE))
349     size = 3 * POINTER_SIZE;
350   else if (IS_AGGR_TYPE (type))
351     {
352       if (CLASSTYPE_N_BASECLASSES (type) == 0)
353         size = 2 * POINTER_SIZE;
354       else if (! TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type)
355                && (TREE_VIA_PUBLIC
356                    (TREE_VEC_ELT (TYPE_BINFO_BASETYPES (type), 0))))
357         size = 3 * POINTER_SIZE;
358       else
359         size = 3 * POINTER_SIZE + TYPE_PRECISION (sizetype);
360     }
361   else
362     size = 2 * POINTER_SIZE;
363
364   /* The type for a character array of the appropriate size.  */
365   arrtype = build_cplus_array_type
366     (unsigned_char_type_node,
367      build_index_type (size_int (size / BITS_PER_UNIT - 1)));
368
369   return declare_global_var (tname, arrtype);
370 }
371
372 /* Generate the NTBS name of a type.  */
373 static tree
374 tinfo_name (type)
375      tree type;
376 {
377   const char *name;
378   tree name_string;
379
380   if (flag_new_abi)
381     name = mangle_type_string (type);
382   else
383     name = build_overload_name (type, 1, 1);
384   name_string = combine_strings (build_string (strlen (name) + 1, name));
385   return name_string;
386 }
387
388 /* Returns a decl for a function or variable which can be used to obtain a
389    type_info object for TYPE.  The old-abi uses functions, the new-abi
390    uses the type_info object directly.  You can take the address of the
391    returned decl, to save the decl.  To use the decl call
392    tinfo_from_decl.  You must arrange that the decl is mark_used, if
393    actually use it --- decls in vtables are only used if the vtable is
394    output.  */
395
396 tree
397 get_tinfo_decl (type)
398      tree type;
399 {
400   tree name;
401   tree d;
402
403   if (TREE_CODE (type) == OFFSET_TYPE)
404     type = TREE_TYPE (type);
405   if (TREE_CODE (type) == METHOD_TYPE)
406     type = build_function_type (TREE_TYPE (type),
407                                 TREE_CHAIN (TYPE_ARG_TYPES (type)));
408
409   if (flag_new_abi)
410     name = mangle_typeinfo_for_type (type);
411   else
412     name = build_overload_with_type (tinfo_decl_id, type);
413
414   d = IDENTIFIER_GLOBAL_VALUE (name);
415   if (d)
416     /* OK */;
417   else if (!new_abi_rtti_p ())
418     {
419       /* The tinfo decl is a function returning a reference to the
420          type_info object.  */
421       d = push_library_fn (name, tinfo_decl_type);
422       DECL_NOT_REALLY_EXTERN (d) = 1;
423       SET_DECL_TINFO_FN_P (d);
424       TREE_TYPE (name) = type;
425       defer_fn (d);
426     }
427   else
428     {
429       /* The tinfo decl is the type_info object itself.  We make all
430          tinfo objects look as type_info, even though they will end up
431          being a subclass of that when emitted.  This means the we'll
432          erroneously think we know the dynamic type -- be careful in the
433          runtime.  */
434       d = build_lang_decl (VAR_DECL, name, tinfo_decl_type);
435       
436       DECL_ARTIFICIAL (d) = 1;
437       DECL_ALIGN (d) = TYPE_ALIGN (ptr_type_node);
438       DECL_USER_ALIGN (d) = 0;
439       TREE_READONLY (d) = 1;
440       TREE_STATIC (d) = 1;
441       DECL_EXTERNAL (d) = 1;
442       TREE_PUBLIC (d) = 1;
443       comdat_linkage (d);
444       DECL_ASSEMBLER_NAME (d) = DECL_NAME (d);
445       cp_finish_decl (d, NULL_TREE, NULL_TREE, 0);
446
447       pushdecl_top_level (d);
448       /* Remember the type it is for.  */
449       TREE_TYPE (name) = type;
450       TREE_USED (name) = 1;
451     }
452   return d;
453 }
454
455 /* Given an expr produced by get_tinfo_decl, return an expr which
456    produces a reference to the type_info object.  */
457
458 static tree
459 tinfo_from_decl (expr)
460      tree expr;
461 {
462   tree t;
463   
464   if (!new_abi_rtti_p ())
465     t = build_call (expr, NULL_TREE);
466   else if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
467     t = build_indirect_ref (expr, NULL);
468   else
469     t = expr;
470   
471   return t;
472 }
473
474 tree
475 get_typeid_1 (type)
476      tree type;
477 {
478   tree t;
479
480   t = get_tinfo_decl (type);
481   t = tinfo_from_decl (t);
482   return convert_from_reference (t);
483 }
484   
485 /* Return the type_info object for TYPE.  */
486
487 tree
488 get_typeid (type)
489      tree type;
490 {
491   if (type == error_mark_node)
492     return error_mark_node;
493
494   if (!COMPLETE_TYPE_P (type_info_type_node))
495     {
496       error ("must #include <typeinfo> before using typeid");
497       return error_mark_node;
498     }
499   
500   if (processing_template_decl)
501     return build_min_nt (TYPEID_EXPR, type);
502
503   /* If the type of the type-id is a reference type, the result of the
504      typeid expression refers to a type_info object representing the
505      referenced type.  */
506   if (TREE_CODE (type) == REFERENCE_TYPE)
507     type = TREE_TYPE (type);
508
509   /* The top-level cv-qualifiers of the lvalue expression or the type-id
510      that is the operand of typeid are always ignored.  */
511   type = TYPE_MAIN_VARIANT (type);
512
513   if (!VOID_TYPE_P (type))
514     type = complete_type_or_else (type, NULL_TREE);
515   
516   if (!type)
517     return error_mark_node;
518
519   return get_typeid_1 (type);
520 }
521
522 /* Check whether TEST is null before returning RESULT.  If TEST is used in
523    RESULT, it must have previously had a save_expr applied to it.  */
524
525 static tree
526 ifnonnull (test, result)
527      tree test, result;
528 {
529   return build (COND_EXPR, TREE_TYPE (result),
530                 build (EQ_EXPR, boolean_type_node, test, integer_zero_node),
531                 cp_convert (TREE_TYPE (result), integer_zero_node),
532                 result);
533 }
534
535 /* Generate the constant expression describing where direct base BINFO
536    appears within the PARENT. How to interpret this expression depends on
537    details of the ABI, which the runtime must be aware of.  */
538
539 static tree
540 get_base_offset (binfo, parent)
541      tree binfo;
542      tree parent;
543 {
544   if (! TREE_VIA_VIRTUAL (binfo))
545     return BINFO_OFFSET (binfo);
546   else if (! vbase_offsets_in_vtable_p ())
547     {
548       const char *name;
549       tree result;
550       tree field;
551     
552       FORMAT_VBASE_NAME (name, BINFO_TYPE (binfo));
553       field = lookup_field (parent, get_identifier (name), 0, 0);
554       result = byte_position (field);
555       
556       if (DECL_CONTEXT (field) != parent)
557         {
558           /* The vbase pointer might be in a non-virtual base of PARENT.
559            * Adjust for the offset of that base in PARENT.  */
560           tree path;
561           
562           get_base_distance (DECL_CONTEXT (field), parent, -1, &path);
563           result = build (PLUS_EXPR, TREE_TYPE (result),
564                           result, BINFO_OFFSET (path));
565           result = fold (result);
566         }
567       return result;
568     }
569   else
570     /* Under the new ABI, we store the vtable offset at which
571        the virtual base offset can be found.  */
572     return convert (sizetype,
573                     BINFO_VPTR_FIELD (binfo_for_vbase (BINFO_TYPE (binfo),
574                                                        parent)));
575
576 }
577
578 /* Execute a dynamic cast, as described in section 5.2.6 of the 9/93 working
579    paper.  */
580
581 static tree
582 build_dynamic_cast_1 (type, expr)
583      tree type, expr;
584 {
585   enum tree_code tc = TREE_CODE (type);
586   tree exprtype = TREE_TYPE (expr);
587   tree dcast_fn;
588   tree old_expr = expr;
589   const char *errstr = NULL;
590
591   /* T shall be a pointer or reference to a complete class type, or
592      `pointer to cv void''.  */
593   switch (tc)
594     {
595     case POINTER_TYPE:
596       if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
597         break;
598     case REFERENCE_TYPE:
599       if (! IS_AGGR_TYPE (TREE_TYPE (type)))
600         {
601           errstr = "target is not pointer or reference to class";
602           goto fail;
603         }
604       if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
605         {
606           errstr = "target is not pointer or reference to complete type";
607           goto fail;
608         }
609       break;
610
611     default:
612       errstr = "target is not pointer or reference";
613       goto fail;
614     }
615
616   if (TREE_CODE (expr) == OFFSET_REF)
617     {
618       expr = resolve_offset_ref (expr);
619       exprtype = TREE_TYPE (expr);
620     }
621
622   if (tc == POINTER_TYPE)
623     expr = convert_from_reference (expr);
624   else if (TREE_CODE (exprtype) != REFERENCE_TYPE)
625     {
626       /* Apply trivial conversion T -> T& for dereferenced ptrs.  */
627       exprtype = build_reference_type (exprtype);
628       expr = convert_to_reference (exprtype, expr, CONV_IMPLICIT,
629                                    LOOKUP_NORMAL, NULL_TREE);
630     }
631
632   exprtype = TREE_TYPE (expr);
633
634   if (tc == POINTER_TYPE)
635     {
636       /* If T is a pointer type, v shall be an rvalue of a pointer to
637          complete class type, and the result is an rvalue of type T.  */
638
639       if (TREE_CODE (exprtype) != POINTER_TYPE)
640         {
641           errstr = "source is not a pointer";
642           goto fail;
643         }
644       if (! IS_AGGR_TYPE (TREE_TYPE (exprtype)))
645         {
646           errstr = "source is not a pointer to class";
647           goto fail;
648         }
649       if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
650         {
651           errstr = "source is a pointer to incomplete type";
652           goto fail;
653         }
654     }
655   else
656     {
657       /* T is a reference type, v shall be an lvalue of a complete class
658          type, and the result is an lvalue of the type referred to by T.  */
659
660       if (! IS_AGGR_TYPE (TREE_TYPE (exprtype)))
661         {
662           errstr = "source is not of class type";
663           goto fail;
664         }
665       if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
666         {
667           errstr = "source is of incomplete class type";
668           goto fail;
669         }
670       
671     }
672
673   /* The dynamic_cast operator shall not cast away constness.  */
674   if (!at_least_as_qualified_p (TREE_TYPE (type),
675                                 TREE_TYPE (exprtype)))
676     {
677       errstr = "conversion casts away constness";
678       goto fail;
679     }
680
681   /* If *type is an unambiguous accessible base class of *exprtype,
682      convert statically.  */
683   {
684     int distance;
685     tree path;
686
687     distance = get_base_distance (TREE_TYPE (type), TREE_TYPE (exprtype), 1,
688                                   &path);
689
690     if (distance == -2)
691       {
692         cp_error ("dynamic_cast from `%T' to ambiguous base class `%T'",
693                   TREE_TYPE (exprtype), TREE_TYPE (type));
694         return error_mark_node;
695       }
696     if (distance == -3)
697       {
698         cp_error ("dynamic_cast from `%T' to private base class `%T'",
699                   TREE_TYPE (exprtype), TREE_TYPE (type));
700         return error_mark_node;
701       }
702
703     if (distance >= 0)
704       {
705         expr = build_vbase_path (PLUS_EXPR, type, expr, path, 0);
706         if (TREE_CODE (exprtype) == POINTER_TYPE)
707           expr = non_lvalue (expr);
708         return expr;
709       }
710   }
711
712   /* Otherwise *exprtype must be a polymorphic class (have a vtbl).  */
713   if (TYPE_POLYMORPHIC_P (TREE_TYPE (exprtype)))
714     {
715       tree expr1;
716       /* if TYPE is `void *', return pointer to complete object.  */
717       if (tc == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (type)))
718         {
719           /* if b is an object, dynamic_cast<void *>(&b) == (void *)&b.  */
720           if (TREE_CODE (expr) == ADDR_EXPR
721               && TREE_CODE (TREE_OPERAND (expr, 0)) == VAR_DECL
722               && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == RECORD_TYPE)
723             return build1 (NOP_EXPR, type, expr);
724
725           /* Since expr is used twice below, save it.  */
726           expr = save_expr (expr);
727
728           expr1 = build_headof (expr);
729           if (TREE_TYPE (expr1) != type)
730             expr1 = build1 (NOP_EXPR, type, expr1);
731           return ifnonnull (expr, expr1);
732         }
733       else
734         {
735           tree retval;
736           tree result, td2, td3, elems;
737           tree static_type, target_type, boff;
738
739           /* If we got here, we can't convert statically.  Therefore,
740              dynamic_cast<D&>(b) (b an object) cannot succeed.  */
741           if (tc == REFERENCE_TYPE)
742             {
743               if (TREE_CODE (old_expr) == VAR_DECL
744                   && TREE_CODE (TREE_TYPE (old_expr)) == RECORD_TYPE)
745                 {
746                   tree expr = throw_bad_cast ();
747                   cp_warning ("dynamic_cast of `%#D' to `%#T' can never succeed",
748                               old_expr, type);
749                   /* Bash it to the expected type.  */
750                   TREE_TYPE (expr) = type;
751                   return expr;
752                 }
753             }
754           /* Ditto for dynamic_cast<D*>(&b).  */
755           else if (TREE_CODE (expr) == ADDR_EXPR)
756             {
757               tree op = TREE_OPERAND (expr, 0);
758               if (TREE_CODE (op) == VAR_DECL
759                   && TREE_CODE (TREE_TYPE (op)) == RECORD_TYPE)
760                 {
761                   cp_warning ("dynamic_cast of `%#D' to `%#T' can never succeed",
762                               op, type);
763                   retval = build_int_2 (0, 0); 
764                   TREE_TYPE (retval) = type; 
765                   return retval;
766                 }
767             }
768
769           target_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
770           static_type = TYPE_MAIN_VARIANT (TREE_TYPE (exprtype));
771           td2 = build_unary_op (ADDR_EXPR, get_tinfo_decl (target_type), 0);
772           td3 = build_unary_op (ADDR_EXPR, get_tinfo_decl (static_type), 0);
773
774           /* Determine how T and V are related.  */
775           boff = get_dynamic_cast_base_type (static_type, target_type);
776           
777           /* Since expr is used twice below, save it.  */
778           expr = save_expr (expr);
779
780           expr1 = expr;
781           if (tc == REFERENCE_TYPE)
782             expr1 = build_unary_op (ADDR_EXPR, expr1, 0);
783
784           if (!new_abi_rtti_p ())
785             {
786               tree expr2 = build_headof (expr1);
787               tree td1 = expr;
788
789               if (tc == POINTER_TYPE)
790                 td1 = build_indirect_ref (td1, NULL_PTR);
791               td1 = get_tinfo_decl_dynamic (td1);
792           
793               elems = tree_cons
794                 (NULL_TREE, td1, tree_cons
795                   (NULL_TREE, td2, tree_cons
796                     (NULL_TREE, boff, tree_cons
797                       (NULL_TREE, expr2, tree_cons
798                         (NULL_TREE, td3, tree_cons
799                           (NULL_TREE, expr1, NULL_TREE))))));
800             }
801           else
802             elems = tree_cons
803               (NULL_TREE, expr1, tree_cons
804                 (NULL_TREE, td3, tree_cons
805                   (NULL_TREE, td2, tree_cons
806                     (NULL_TREE, boff, NULL_TREE))));
807
808           dcast_fn = dynamic_cast_node;
809           if (!dcast_fn)
810             {
811               tree tmp;
812               tree tinfo_ptr;
813               tree ns = new_abi_rtti_p () ? abi_node : global_namespace;
814               const char *name;
815               
816               push_nested_namespace (ns);
817               if (!new_abi_rtti_p ())
818                 {
819                   tinfo_ptr = build_pointer_type (tinfo_decl_type);
820                   name = "__dynamic_cast_2";
821                   tmp = tree_cons
822                     (NULL_TREE, tinfo_ptr, tree_cons
823                       (NULL_TREE, tinfo_ptr, tree_cons
824                         (NULL_TREE, integer_type_node, tree_cons
825                           (NULL_TREE, ptr_type_node, tree_cons
826                             (NULL_TREE, tinfo_ptr, tree_cons
827                               (NULL_TREE, ptr_type_node, void_list_node))))));
828                 }
829               else
830                 {
831                   tinfo_ptr = xref_tag (class_type_node,
832                                         get_identifier ("__class_type_info"),
833                                         1);
834                     
835                   tinfo_ptr = build_pointer_type
836                                 (build_qualified_type
837                                   (tinfo_ptr, TYPE_QUAL_CONST));
838                   name = "__dynamic_cast";
839                   tmp = tree_cons
840                     (NULL_TREE, const_ptr_type_node, tree_cons
841                       (NULL_TREE, tinfo_ptr, tree_cons
842                         (NULL_TREE, tinfo_ptr, tree_cons
843                           (NULL_TREE, ptrdiff_type_node, void_list_node))));
844                 }
845               tmp = build_function_type (ptr_type_node, tmp);
846               dcast_fn = build_library_fn_ptr (name, tmp);
847               pop_nested_namespace (ns);
848               dynamic_cast_node = dcast_fn;
849             }
850           result = build_call (dcast_fn, elems);
851
852           if (tc == REFERENCE_TYPE)
853             {
854               tree bad = throw_bad_cast ();
855               
856               result = save_expr (result);
857               return build (COND_EXPR, type, result, result, bad);
858             }
859
860           /* Now back to the type we want from a void*.  */
861           result = cp_convert (type, result);
862           return ifnonnull (expr, result);
863         }
864     }
865   else
866     errstr = "source type is not polymorphic";
867
868  fail:
869   cp_error ("cannot dynamic_cast `%E' (of type `%#T') to type `%#T' (%s)",
870             expr, exprtype, type, errstr);
871   return error_mark_node;
872 }
873
874 tree
875 build_dynamic_cast (type, expr)
876      tree type, expr;
877 {
878   if (type == error_mark_node || expr == error_mark_node)
879     return error_mark_node;
880   
881   if (processing_template_decl)
882     return build_min (DYNAMIC_CAST_EXPR, type, expr);
883
884   return convert_from_reference (build_dynamic_cast_1 (type, expr));
885 }
886 \f
887 /* Build and initialize various sorts of descriptors.  Every descriptor
888    node has a name associated with it (the name created by mangling).
889    For this reason, we use the identifier as our access to the __*_desc
890    nodes, instead of sticking them directly in the types.  Otherwise we
891    would burden all built-in types (and pointer types) with slots that
892    we don't necessarily want to use.
893
894    For each descriptor we build, we build a variable that contains
895    the descriptor's information.  When we need this info at runtime,
896    all we need is access to these variables.
897
898    Note: these constructors always return the address of the descriptor
899    info, since that is simplest for their mutual interaction.  */
900
901 /* Build an initializer for a __si_type_info node.  */
902
903 static void
904 expand_si_desc (tdecl, type)
905      tree tdecl;
906      tree type;
907 {
908   tree t, elems, fn;
909   tree name_string = tinfo_name (type);
910
911   type = BINFO_TYPE (TREE_VEC_ELT (TYPE_BINFO_BASETYPES (type), 0));
912   finish_expr_stmt (get_typeid_1 (type));
913   t = decay_conversion (get_tinfo_var (type));
914   elems = tree_cons
915     (NULL_TREE, decay_conversion (tdecl), tree_cons
916      (NULL_TREE, decay_conversion (name_string), tree_cons
917       (NULL_TREE, t, NULL_TREE)));
918
919   fn = get_identifier ("__rtti_si");
920   if (IDENTIFIER_GLOBAL_VALUE (fn))
921     fn = IDENTIFIER_GLOBAL_VALUE (fn);
922   else
923     {
924       tree tmp;
925       tmp = tree_cons
926         (NULL_TREE, ptr_type_node, tree_cons
927          (NULL_TREE, const_string_type_node, tree_cons
928           (NULL_TREE, build_pointer_type (type_info_type_node),
929            void_list_node)));
930       fn = push_void_library_fn (fn, tmp);
931     }
932
933   fn = build_call (fn, elems);
934   finish_expr_stmt (fn);
935 }
936
937 /* Build an initializer for a __class_type_info node.  */
938
939 static void
940 expand_class_desc (tdecl, type)
941      tree tdecl;
942      tree type;
943 {
944   tree name_string;
945   tree fn, tmp;
946
947   int i = CLASSTYPE_N_BASECLASSES (type);
948   int base_cnt = 0;
949   tree binfos = TYPE_BINFO_BASETYPES (type);
950   tree base, elems, access, offset, isvir;
951   tree elt, elts = NULL_TREE;
952
953   if (base_desc_type_node == NULL_TREE)
954     {
955       tree fields [4];
956
957       /* A reasonably close approximation of __class_type_info::base_info */
958
959       base_desc_type_node = make_aggr_type (RECORD_TYPE);
960
961       /* Actually const __user_type_info * */
962       fields [0] = build_decl
963         (FIELD_DECL, NULL_TREE,
964          build_pointer_type (build_qualified_type
965                              (type_info_type_node,
966                               TYPE_QUAL_CONST)));
967       fields [1] = build_decl
968         (FIELD_DECL, NULL_TREE, 
969          flag_new_abi ? intSI_type_node : unsigned_intSI_type_node);
970       DECL_BIT_FIELD (fields[1]) = 1;
971       DECL_SIZE (fields[1]) = bitsize_int (29);
972
973       fields [2] = build_decl (FIELD_DECL, NULL_TREE, boolean_type_node);
974       DECL_BIT_FIELD (fields[2]) = 1;
975       DECL_SIZE (fields[2]) = bitsize_one_node;
976
977       /* Actually enum access */
978       fields [3] = build_decl (FIELD_DECL, NULL_TREE, integer_type_node);
979       DECL_BIT_FIELD (fields[3]) = 1;
980       DECL_SIZE (fields[3]) = bitsize_int (2);
981
982       finish_builtin_type (base_desc_type_node, "__base_info", fields,
983                            3, ptr_type_node);
984     }
985
986   while (--i >= 0)
987     {
988       tree binfo = TREE_VEC_ELT (binfos, i);
989
990       finish_expr_stmt (get_typeid_1 (BINFO_TYPE (binfo)));
991       base = decay_conversion (get_tinfo_var (BINFO_TYPE (binfo)));
992       offset = get_base_offset (binfo, type);
993       
994       if (TREE_VIA_PUBLIC (binfo))
995         access = access_public_node;
996       else if (TREE_VIA_PROTECTED (binfo))
997         access = access_protected_node;
998       else
999         access = access_private_node;
1000       if (TREE_VIA_VIRTUAL (binfo))
1001         isvir = boolean_true_node;
1002       else
1003         isvir = boolean_false_node;
1004
1005       elt = build
1006         (CONSTRUCTOR, base_desc_type_node, NULL_TREE, tree_cons
1007          (NULL_TREE, base, tree_cons
1008           (NULL_TREE, offset, tree_cons
1009            (NULL_TREE, isvir, tree_cons
1010             (NULL_TREE, access, NULL_TREE)))));
1011       TREE_HAS_CONSTRUCTOR (elt) = TREE_CONSTANT (elt) = TREE_STATIC (elt) = 1;
1012       elts = tree_cons (NULL_TREE, elt, elts);
1013       base_cnt++;
1014     }
1015
1016   name_string = tinfo_name (type);
1017
1018   {
1019     tree arrtype = build_array_type (base_desc_type_node, NULL_TREE);
1020     elts = build (CONSTRUCTOR, arrtype, NULL_TREE, elts);
1021     TREE_HAS_CONSTRUCTOR (elts) = TREE_CONSTANT (elts)
1022       = TREE_STATIC (elts) = 1;
1023     complete_array_type (arrtype, elts, 1);
1024   }
1025
1026   elems = tree_cons
1027     (NULL_TREE, decay_conversion (tdecl), tree_cons
1028      (NULL_TREE, decay_conversion (name_string), tree_cons
1029       (NULL_TREE, decay_conversion (elts), tree_cons
1030        (NULL_TREE, cp_convert (sizetype, build_int_2 (base_cnt, 0)),
1031         NULL_TREE))));
1032
1033   fn = get_identifier ("__rtti_class");
1034   if (IDENTIFIER_GLOBAL_VALUE (fn))
1035     fn = IDENTIFIER_GLOBAL_VALUE (fn);
1036   else
1037     {
1038       tmp = tree_cons
1039         (NULL_TREE, ptr_type_node, tree_cons
1040          (NULL_TREE, const_string_type_node, tree_cons
1041           (NULL_TREE, build_pointer_type (base_desc_type_node), tree_cons
1042            (NULL_TREE, sizetype, void_list_node))));
1043
1044       fn = push_void_library_fn (fn, tmp);
1045     }
1046
1047   fn = build_call (fn, elems);
1048   finish_expr_stmt (fn);
1049 }
1050
1051 /* Build an initializer for a __pointer_type_info node.  */
1052
1053 static void
1054 expand_ptr_desc (tdecl, type)
1055      tree tdecl;
1056      tree type;
1057 {
1058   tree t, elems, fn;
1059   tree name_string = tinfo_name (type);
1060
1061   type = TREE_TYPE (type);
1062   finish_expr_stmt (get_typeid_1 (type));
1063   t = decay_conversion (get_tinfo_var (type));
1064   elems = tree_cons
1065     (NULL_TREE, decay_conversion (tdecl), tree_cons
1066      (NULL_TREE, decay_conversion (name_string), tree_cons
1067       (NULL_TREE, t, NULL_TREE)));
1068
1069   fn = get_identifier ("__rtti_ptr");
1070   if (IDENTIFIER_GLOBAL_VALUE (fn))
1071     fn = IDENTIFIER_GLOBAL_VALUE (fn);
1072   else
1073     {
1074       tree tmp;
1075       tmp = tree_cons
1076         (NULL_TREE, ptr_type_node, tree_cons
1077          (NULL_TREE, const_string_type_node, tree_cons
1078           (NULL_TREE, build_pointer_type (type_info_type_node),
1079            void_list_node)));
1080       fn = push_void_library_fn (fn, tmp);
1081     }
1082
1083   fn = build_call (fn, elems);
1084   finish_expr_stmt (fn);
1085 }
1086
1087 /* Build an initializer for a __attr_type_info node.  */
1088
1089 static void
1090 expand_attr_desc (tdecl, type)
1091      tree tdecl;
1092      tree type;
1093 {
1094   tree elems, t, fn;
1095   tree name_string = tinfo_name (type);
1096   tree attrval = build_int_2 (TYPE_QUALS (type), 0);
1097
1098   finish_expr_stmt (get_typeid_1 (TYPE_MAIN_VARIANT (type)));
1099   t = decay_conversion (get_tinfo_var (TYPE_MAIN_VARIANT (type)));
1100   elems = tree_cons
1101     (NULL_TREE, decay_conversion (tdecl), tree_cons
1102      (NULL_TREE, decay_conversion (name_string), tree_cons
1103       (NULL_TREE, attrval, tree_cons (NULL_TREE, t, NULL_TREE))));
1104
1105   fn = get_identifier ("__rtti_attr");
1106   if (IDENTIFIER_GLOBAL_VALUE (fn))
1107     fn = IDENTIFIER_GLOBAL_VALUE (fn);
1108   else
1109     {
1110       tree tmp;
1111       tmp = tree_cons
1112         (NULL_TREE, ptr_type_node, tree_cons
1113          (NULL_TREE, const_string_type_node, tree_cons
1114           (NULL_TREE, integer_type_node, tree_cons
1115            (NULL_TREE, build_pointer_type (type_info_type_node),
1116             void_list_node))));
1117       fn = push_void_library_fn (fn, tmp);
1118     }
1119
1120   fn = build_call (fn, elems);
1121   finish_expr_stmt (fn);
1122 }
1123
1124 /* Build an initializer for a type_info node that just has a name.  */
1125
1126 static void
1127 expand_generic_desc (tdecl, type, fnname)
1128      tree tdecl;
1129      tree type;
1130      const char *fnname;
1131 {
1132   tree name_string = tinfo_name (type);
1133   tree elems = tree_cons
1134     (NULL_TREE, decay_conversion (tdecl), tree_cons
1135      (NULL_TREE, decay_conversion (name_string), NULL_TREE));
1136
1137   tree fn = get_identifier (fnname);
1138   if (IDENTIFIER_GLOBAL_VALUE (fn))
1139     fn = IDENTIFIER_GLOBAL_VALUE (fn);
1140   else
1141     {
1142       tree tmp;
1143       tmp = tree_cons
1144         (NULL_TREE, ptr_type_node, tree_cons
1145          (NULL_TREE, const_string_type_node, void_list_node));
1146       fn = push_void_library_fn (fn, tmp);
1147     }
1148
1149   fn = build_call (fn, elems);
1150   finish_expr_stmt (fn);
1151 }
1152
1153 /* Generate the code for a type_info initialization function.
1154    Note that we take advantage of the passage
1155
1156    5.2.7  Type identification                               [expr.typeid]
1157    
1158    Whether or not the destructor is called for the type_info object at the
1159    end of the program is unspecified.
1160
1161    and don't bother to arrange for these objects to be destroyed.  It
1162    doesn't matter, anyway, since the destructors don't do anything.
1163        
1164    This must only be called from toplevel (i.e. from finish_file)!  */
1165
1166 void
1167 synthesize_tinfo_fn (fndecl)
1168      tree fndecl;
1169 {
1170   tree type = TREE_TYPE (DECL_NAME (fndecl));
1171   tree tmp, addr, tdecl;
1172   tree compound_stmt;
1173   tree if_stmt;
1174   tree then_clause;
1175
1176   my_friendly_assert (!new_abi_rtti_p (), 20000118);
1177   if (at_eof)
1178     {
1179       import_export_decl (fndecl);
1180       if (DECL_REALLY_EXTERN (fndecl))
1181         return;
1182     }
1183
1184   /* Declare the static typeinfo variable.  */
1185   tdecl = get_tinfo_var (type);
1186   DECL_EXTERNAL (tdecl) = 0;
1187   TREE_STATIC (tdecl) = 1;
1188   DECL_COMMON (tdecl) = 1;
1189   TREE_USED (tdecl) = 1;
1190   DECL_ALIGN (tdecl) = TYPE_ALIGN (ptr_type_node);
1191   DECL_USER_ALIGN (tdecl) = 0;
1192   cp_finish_decl (tdecl, NULL_TREE, NULL_TREE, 0);
1193
1194   /* Begin processing the function.  */
1195   start_function (NULL_TREE, fndecl, NULL_TREE, 
1196                   SF_DEFAULT | SF_PRE_PARSED);
1197   DECL_DEFER_OUTPUT (fndecl) = 1;
1198   clear_last_expr ();
1199
1200   /* Begin the body of the function.  */
1201   compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
1202
1203   /* For convenience, we save away the address of the static
1204      variable.  */
1205   addr = decay_conversion (tdecl);
1206
1207   /* If the first word of the array (the vtable) is non-zero, we've already
1208      initialized the object, so don't do it again.  */
1209   if_stmt = begin_if_stmt ();
1210   tmp = cp_convert (build_pointer_type (ptr_type_node), addr);
1211   tmp = build_indirect_ref (tmp, 0);
1212   tmp = cp_build_binary_op (EQ_EXPR, tmp, integer_zero_node);
1213   finish_if_stmt_cond (tmp, if_stmt);
1214   then_clause = begin_compound_stmt (/*has_no_scope=*/0);
1215
1216   if (TREE_CODE (type) == FUNCTION_TYPE)
1217     expand_generic_desc (tdecl, type, "__rtti_func");
1218   else if (TREE_CODE (type) == ARRAY_TYPE)
1219     expand_generic_desc (tdecl, type, "__rtti_array");
1220   else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
1221     expand_attr_desc (tdecl, type);
1222   else if (TREE_CODE (type) == POINTER_TYPE)
1223     {
1224       if (TREE_CODE (TREE_TYPE (type)) == OFFSET_TYPE)
1225         expand_generic_desc (tdecl, type, "__rtti_ptmd");
1226       else if (TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
1227         expand_generic_desc (tdecl, type, "__rtti_ptmf");
1228       else
1229         expand_ptr_desc (tdecl, type);
1230     }
1231   else if (TYPE_PTRMEMFUNC_P (type))
1232     expand_generic_desc (tdecl, type, "__rtti_ptmf");
1233   else if (IS_AGGR_TYPE (type))
1234     {
1235       if (CLASSTYPE_N_BASECLASSES (type) == 0)
1236         expand_generic_desc (tdecl, type, "__rtti_user");
1237       else if (! TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type)
1238                && (TREE_VIA_PUBLIC
1239                    (TREE_VEC_ELT (TYPE_BINFO_BASETYPES (type), 0))))
1240         expand_si_desc (tdecl, type);
1241       else
1242         expand_class_desc (tdecl, type);
1243     }
1244   else if (TREE_CODE (type) == ENUMERAL_TYPE)
1245     expand_generic_desc (tdecl, type, "__rtti_user");
1246   else
1247     my_friendly_abort (252);
1248
1249   finish_compound_stmt (/*has_no_scope=*/0, then_clause);
1250   finish_then_clause (if_stmt);
1251   finish_if_stmt ();
1252
1253   /* OK, now return the type_info object.  */
1254   tmp = cp_convert (build_pointer_type (type_info_type_node), addr);
1255   tmp = build_indirect_ref (tmp, 0);
1256   finish_return_stmt (tmp);
1257   /* Finish the function body.  */
1258   finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
1259   expand_body (finish_function (0));
1260 }
1261
1262 /* Return the runtime bit mask encoding the qualifiers of TYPE.  */
1263
1264 static int
1265 qualifier_flags (type)
1266      tree type;
1267 {
1268   int flags = 0;
1269   /* we want the qualifiers on this type, not any array core, it might have */
1270   int quals = TYPE_QUALS (type);
1271   
1272   if (quals & TYPE_QUAL_CONST)
1273     flags |= 1;
1274   if (quals & TYPE_QUAL_VOLATILE)
1275     flags |= 2;
1276   if (quals & TYPE_QUAL_RESTRICT)
1277     flags |= 4;
1278   return flags;
1279 }
1280
1281 /* Return non-zero, if the pointer chain TYPE ends at an incomplete type, or
1282    contains a pointer to member of an incomplete class.  */
1283
1284 static int
1285 target_incomplete_p (type)
1286      tree type;
1287 {
1288   while (TREE_CODE (type) == POINTER_TYPE)
1289     if (TYPE_PTRMEM_P (type))
1290       {
1291         if (!COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type)))
1292           return 1;
1293         type = TYPE_PTRMEM_POINTED_TO_TYPE (type);
1294       }
1295     else
1296       type = TREE_TYPE (type);
1297   if (!COMPLETE_OR_VOID_TYPE_P (type))
1298     return 1;
1299   
1300   return 0;
1301 }
1302
1303 /* Return a CONSTRUCTOR for the common part of the type_info objects. This
1304    is the vtable pointer and NTBS name.  The NTBS name is emitted as a
1305    comdat const char array, so it becomes a unique key for the type. Generate
1306    and emit that VAR_DECL here.  (We can't always emit the type_info itself
1307    as comdat, because of pointers to incomplete.) */
1308
1309 static tree
1310 tinfo_base_init (desc, target)
1311      tree desc;
1312      tree target;
1313 {
1314   tree init = NULL_TREE;
1315   tree name_decl;
1316   
1317   {
1318     tree name_name;
1319     
1320     /* Generate the NTBS array variable.  */
1321     tree name_type = build_cplus_array_type
1322                      (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
1323                      NULL_TREE);
1324     tree name_string = tinfo_name (target);
1325
1326     if (flag_new_abi)
1327       name_name = mangle_typeinfo_for_type (target);
1328     else
1329       name_name = build_overload_with_type (tinfo_var_id, target);
1330     name_decl = build_lang_decl (VAR_DECL, name_name, name_type);
1331     
1332     DECL_ARTIFICIAL (name_decl) = 1;
1333     TREE_READONLY (name_decl) = 1;
1334     TREE_STATIC (name_decl) = 1;
1335     DECL_EXTERNAL (name_decl) = 0;
1336     TREE_PUBLIC (name_decl) = 1;
1337     comdat_linkage (name_decl);
1338     if (flag_new_abi)
1339       /* The new ABI specifies the external name of the string
1340          containing the type's name.  */
1341       DECL_ASSEMBLER_NAME (name_decl) 
1342         = mangle_typeinfo_string_for_type (target);
1343     else
1344       DECL_ASSEMBLER_NAME (name_decl) = DECL_NAME (name_decl);
1345     DECL_INITIAL (name_decl) = name_string;
1346     cp_finish_decl (name_decl, name_string, NULL_TREE, 0);
1347   }
1348   
1349   if (TINFO_VTABLE_DECL (desc))
1350     {
1351       tree vtbl_ptr = TINFO_VTABLE_DECL (desc);
1352       init = tree_cons (NULL_TREE, vtbl_ptr, init);
1353     }
1354   
1355   init = tree_cons (NULL_TREE, decay_conversion (name_decl), init);
1356   
1357   init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, nreverse (init));
1358   TREE_HAS_CONSTRUCTOR (init) = TREE_CONSTANT (init) = TREE_STATIC (init) = 1;
1359   init = tree_cons (NULL_TREE, init, NULL_TREE);
1360   
1361   return init;
1362 }
1363
1364 /* Return the CONSTRUCTOR expr for a type_info of TYPE. DESC provides the
1365    information about the particular type_info derivation, which adds no
1366    additional fields to the type_info base.  */
1367
1368 static tree
1369 generic_initializer (desc, target)
1370      tree desc;
1371      tree target;
1372 {
1373   tree init = tinfo_base_init (desc, target);
1374   
1375   init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, init);
1376   TREE_HAS_CONSTRUCTOR (init) = TREE_CONSTANT (init) = TREE_STATIC (init) = 1;
1377   return init;
1378 }
1379
1380 /* Return the CONSTRUCTOR expr for a type_info of pointer TYPE.
1381    DESC provides information about the particular type_info derivation,
1382    which adds target type and qualifier flags members to the type_info base.  */
1383
1384 static tree
1385 ptr_initializer (desc, target, non_public_ptr)
1386      tree desc;
1387      tree target;
1388      int *non_public_ptr;
1389 {
1390   tree init = tinfo_base_init (desc, target);
1391   tree to = TREE_TYPE (target);
1392   int flags = qualifier_flags (to);
1393   int incomplete = target_incomplete_p (to);
1394   
1395   if (incomplete)
1396     {
1397       flags |= 8;
1398       *non_public_ptr = 1;
1399     }
1400   init = tree_cons (NULL_TREE, build_int_2 (flags, 0), init);
1401   init = tree_cons (NULL_TREE,
1402                     build_unary_op (ADDR_EXPR,
1403                                     get_tinfo_decl (TYPE_MAIN_VARIANT (to)), 0),
1404                     init);
1405   
1406   init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, nreverse (init));
1407   TREE_HAS_CONSTRUCTOR (init) = TREE_CONSTANT (init) = TREE_STATIC (init) = 1;
1408   return init;
1409 }
1410
1411 /* Return the CONSTRUCTOR expr for a type_info of pointer to member data TYPE.
1412    DESC provides information about the particular type_info derivation,
1413    which adds class, target type and qualifier flags members to the type_info
1414    base.  */
1415
1416 static tree
1417 ptm_initializer (desc, target, non_public_ptr)
1418      tree desc;
1419      tree target;
1420      int *non_public_ptr;
1421 {
1422   tree init = tinfo_base_init (desc, target);
1423   tree to = TYPE_PTRMEM_POINTED_TO_TYPE (target);
1424   tree klass = TYPE_PTRMEM_CLASS_TYPE (target);
1425   int flags = qualifier_flags (to);
1426   int incomplete = target_incomplete_p (to);
1427   
1428   if (incomplete)
1429     {
1430       flags |= 0x8;
1431       *non_public_ptr = 1;
1432     }
1433   if (!COMPLETE_TYPE_P (klass))
1434     {
1435       flags |= 0x10;
1436       *non_public_ptr = 1;
1437     }
1438   init = tree_cons (NULL_TREE, build_int_2 (flags, 0), init);
1439   init = tree_cons (NULL_TREE,
1440                     build_unary_op (ADDR_EXPR,
1441                                     get_tinfo_decl (TYPE_MAIN_VARIANT (to)), 0),
1442                     init);
1443   init = tree_cons (NULL_TREE,
1444                     build_unary_op (ADDR_EXPR, get_tinfo_decl (klass), 0),
1445                     init);  
1446   
1447   init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, nreverse (init));
1448   TREE_HAS_CONSTRUCTOR (init) = TREE_CONSTANT (init) = TREE_STATIC (init) = 1;
1449   return init;  
1450 }
1451
1452 /* Check base BINFO to set hint flags in *DATA, which is really an int.
1453    We use CLASSTYPE_MARKED to tag types we've found as non-virtual bases and
1454    CLASSTYPE_MARKED2 to tag those which are virtual bases. Remember it is
1455    possible for a type to be both a virtual and non-virtual base.  */
1456
1457 static tree
1458 dfs_class_hint_mark (binfo, data)
1459      tree binfo;
1460      void *data;
1461 {
1462   tree basetype = BINFO_TYPE (binfo);
1463   int *hint = (int *) data;
1464   
1465   if (TREE_VIA_VIRTUAL (binfo))
1466     {
1467       if (CLASSTYPE_MARKED (basetype))
1468         *hint |= 1;
1469       if (CLASSTYPE_MARKED2 (basetype))
1470         *hint |= 2;
1471       SET_CLASSTYPE_MARKED2 (basetype);
1472     }
1473   else
1474     {
1475       if (CLASSTYPE_MARKED (basetype) || CLASSTYPE_MARKED2 (basetype))
1476         *hint |= 1;
1477       SET_CLASSTYPE_MARKED (basetype);
1478     }
1479   if (!TREE_VIA_PUBLIC (binfo) && TYPE_BINFO (basetype) != binfo)
1480     *hint |= 4;
1481   return NULL_TREE;
1482 };
1483
1484 /* Clear the base's dfs marks, after searching for duplicate bases. */
1485
1486 static tree
1487 dfs_class_hint_unmark (binfo, data)
1488      tree binfo;
1489      void *data ATTRIBUTE_UNUSED;
1490 {
1491   tree basetype = BINFO_TYPE (binfo);
1492   
1493   CLEAR_CLASSTYPE_MARKED (basetype);
1494   CLEAR_CLASSTYPE_MARKED2 (basetype);
1495   return NULL_TREE;
1496 }
1497
1498 /* Determine the hint flags describing the features of a class's heirarchy.  */
1499
1500 static int
1501 class_hint_flags (type)
1502      tree type;
1503 {
1504   int hint_flags = 0;
1505   int i;
1506   
1507   dfs_walk (TYPE_BINFO (type), dfs_class_hint_mark, NULL, &hint_flags);
1508   dfs_walk (TYPE_BINFO (type), dfs_class_hint_unmark, NULL, NULL);
1509   
1510   for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); ++i)
1511     {
1512       tree base_binfo = BINFO_BASETYPE (TYPE_BINFO (type), i);
1513       
1514       if (TREE_VIA_PUBLIC (base_binfo))
1515         hint_flags |= 0x8;
1516     }
1517   return hint_flags;
1518 }
1519         
1520 /* Return the CONSTRUCTOR expr for a type_info of class TYPE.
1521    DESC provides information about the particular __class_type_info derivation,
1522    which adds hint flags and TRAIL initializers to the type_info base.  */
1523
1524 static tree
1525 class_initializer (desc, target, trail)
1526      tree desc;
1527      tree target;
1528      tree trail;
1529 {
1530   tree init = tinfo_base_init (desc, target);
1531   
1532   TREE_CHAIN (init) = trail;
1533   init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, init);
1534   TREE_HAS_CONSTRUCTOR (init) = TREE_CONSTANT (init) = TREE_STATIC (init) = 1;
1535   return init;  
1536 }
1537
1538 /* Generate a pseudo_type_info VAR_DECL suitable for the supplied
1539    TARGET_TYPE and given the REAL_NAME. This is the structure expected by
1540    the runtime, and therefore has additional fields.  If we need not emit a
1541    definition (because the runtime must contain it), return NULL_TREE,
1542    otherwise return the VAR_DECL.  */
1543
1544 static tree
1545 synthesize_tinfo_var (target_type, real_name)
1546      tree target_type;
1547      tree real_name;
1548 {
1549   tree var_init = NULL_TREE;
1550   tree var_type = NULL_TREE;
1551   int non_public = 0;
1552   
1553   my_friendly_assert (new_abi_rtti_p (), 20000118);
1554
1555   switch (TREE_CODE (target_type))
1556     {
1557     case POINTER_TYPE:
1558       if (TYPE_PTRMEM_P (target_type))
1559         {
1560           var_type = ptm_desc_type_node;
1561           var_init = ptm_initializer (var_type, target_type, &non_public);
1562         }
1563       else
1564         {
1565           int code = TREE_CODE (TREE_TYPE (target_type));
1566           
1567           if ((CP_TYPE_QUALS (TREE_TYPE (target_type)) | TYPE_QUAL_CONST)
1568               == TYPE_QUAL_CONST
1569               && (code == INTEGER_TYPE || code == BOOLEAN_TYPE
1570                   || code == CHAR_TYPE || code == REAL_TYPE
1571                   || code == VOID_TYPE)
1572               && !doing_runtime)
1573             /* These are in the runtime.  */
1574             return NULL_TREE;
1575           var_type = ptr_desc_type_node;
1576           var_init = ptr_initializer (var_type, target_type, &non_public);
1577         }
1578       break;
1579     case ENUMERAL_TYPE:
1580       var_type = enum_desc_type_node;
1581       var_init = generic_initializer (var_type, target_type);
1582       break;
1583     case FUNCTION_TYPE:
1584       var_type = func_desc_type_node;
1585       var_init = generic_initializer (var_type, target_type);
1586       break;
1587     case ARRAY_TYPE:
1588       var_type = ary_desc_type_node;
1589       var_init = generic_initializer (var_type, target_type);
1590       break;
1591     case UNION_TYPE:
1592     case RECORD_TYPE:
1593       if (TYPE_PTRMEMFUNC_P (target_type))
1594         {
1595           var_type = ptm_desc_type_node;
1596           var_init = ptm_initializer (var_type, target_type, &non_public);
1597         }
1598       else if (!COMPLETE_TYPE_P (target_type))
1599         {
1600           /* Emit a non-public class_type_info.  */
1601           non_public = 1;
1602           var_type = class_desc_type_node;
1603           var_init = class_initializer (var_type, target_type, NULL_TREE);
1604         }
1605       else if (!CLASSTYPE_N_BASECLASSES (target_type))
1606         {
1607           var_type = class_desc_type_node;
1608           var_init = class_initializer (var_type, target_type, NULL_TREE);
1609         }
1610       else
1611         {
1612           /* if this has a single public non-virtual base, it's easier */
1613           tree binfo = TYPE_BINFO (target_type);
1614           int nbases = BINFO_N_BASETYPES (binfo);
1615           tree base_binfos = BINFO_BASETYPES (binfo);
1616           tree base_inits = NULL_TREE;
1617           int is_simple = nbases == 1;
1618           int ix;
1619           
1620           /* Generate the base information initializer.  */
1621           for (ix = nbases; ix--;)
1622             {
1623               tree base_binfo = TREE_VEC_ELT (base_binfos, ix);
1624               tree base_init = NULL_TREE;
1625               int flags = 0;
1626               tree tinfo;
1627               tree offset;
1628               
1629               if (TREE_VIA_VIRTUAL (base_binfo))
1630                 flags |= 1;
1631               if (TREE_PUBLIC (base_binfo))
1632                 flags |= 2;
1633               tinfo = get_tinfo_decl (BINFO_TYPE (base_binfo));
1634               tinfo = build_unary_op (ADDR_EXPR, tinfo, 0);
1635               offset = get_base_offset (base_binfo, target_type);
1636               
1637               /* is it a single public inheritance? */
1638               if (is_simple && flags == 2 && integer_zerop (offset))
1639                 {
1640                   base_inits = tree_cons (NULL_TREE, tinfo, NULL_TREE);
1641                   break;
1642                 }
1643               is_simple = 0;
1644               
1645               /* combine offset and flags into one field */
1646               offset = cp_build_binary_op (LSHIFT_EXPR, offset,
1647                                            build_int_2 (8, 0));
1648               offset = cp_build_binary_op (BIT_IOR_EXPR, offset,
1649                                            build_int_2 (flags, 0));
1650               base_init = tree_cons (NULL_TREE, offset, base_init);
1651               base_init = tree_cons (NULL_TREE, tinfo, base_init);
1652               base_init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, base_init);
1653               base_inits = tree_cons (NULL_TREE, base_init, base_inits);
1654             }
1655           
1656           if (is_simple)
1657             var_type = si_class_desc_type_node;
1658           else
1659             {
1660               int hint = class_hint_flags (target_type);
1661               
1662               base_inits = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, base_inits);
1663               base_inits = tree_cons (NULL_TREE, base_inits, NULL_TREE);
1664               /* Prepend the number of bases.  */
1665               base_inits = tree_cons (NULL_TREE,
1666                                       build_int_2 (nbases, 0), base_inits);
1667               /* Prepend the hint flags. */
1668               base_inits = tree_cons (NULL_TREE,
1669                                       build_int_2 (hint, 0), base_inits);
1670               var_type = get_vmi_pseudo_type_info (nbases);
1671             }
1672           var_init = class_initializer (var_type, target_type, base_inits);
1673         }
1674       break;
1675     case INTEGER_TYPE:
1676     case BOOLEAN_TYPE:
1677     case CHAR_TYPE:
1678     case REAL_TYPE:
1679     case VOID_TYPE:
1680       if (!doing_runtime)
1681         /* These are guaranteed to be in the runtime.  */
1682         return NULL_TREE;
1683       var_type = bltn_desc_type_node;
1684       var_init = generic_initializer (var_type, target_type);
1685       break;
1686     default:
1687       my_friendly_abort (20000117);
1688     }
1689   
1690   
1691   return create_real_tinfo_var (real_name, TINFO_PSEUDO_TYPE (var_type),
1692                                 var_init, non_public);
1693 }
1694
1695 /* Create the real typeinfo variable.  NON_PUBLIC indicates that we cannot
1696    make this variable public (comdat). */
1697
1698 static tree
1699 create_real_tinfo_var (name, type, init, non_public)
1700      tree name;
1701      tree type;
1702      tree init;
1703      int non_public;
1704 {
1705   static int count = 0;
1706   tree decl;
1707   tree hidden_name;
1708   char hidden[30];
1709   
1710   sprintf (hidden, "%.*s_%d",
1711            IDENTIFIER_LENGTH (tinfo_decl_id), IDENTIFIER_POINTER (tinfo_decl_id),
1712            count++);
1713   hidden_name = get_identifier (hidden);
1714   
1715   decl = build_lang_decl (VAR_DECL, hidden_name,
1716                           build_qualified_type (type, TYPE_QUAL_CONST));
1717   DECL_ARTIFICIAL (decl) = 1;
1718   TREE_READONLY (decl) = 1;
1719   TREE_STATIC (decl) = 1;
1720   DECL_EXTERNAL (decl) = 0;
1721   
1722   if (!non_public)
1723     {
1724       TREE_PUBLIC (decl) = 1;
1725       comdat_linkage (decl);
1726     }
1727   DECL_ASSEMBLER_NAME (decl) = name;
1728   DECL_INITIAL (decl) = init;
1729   cp_finish_decl (decl, init, NULL_TREE, 0);
1730   pushdecl_top_level (decl);
1731   TREE_USED (decl) = 1;
1732   return decl;
1733 }
1734
1735 /* Generate the RECORD_TYPE containing the data layout of a type_info
1736    derivative as used by the runtime. This layout must be consistent with
1737    that defined in the runtime support. Also generate the VAR_DECL for the
1738    type's vtable. We explicitly manage the vtable member, and name it for
1739    real type as used in the runtime. The RECORD type has a different name,
1740    to avoid collisions.  Return a TREE_LIST who's TINFO_PSEUDO_TYPE
1741    is the generated type and TINFO_VTABLE_DECL is the vtable decl.
1742    
1743    REAL_NAME is the runtime's name of the type. Trailing arguments are
1744    additional FIELD_DECL's for the structure. The final argument must be
1745    NULL.  */
1746
1747 static tree
1748 create_pseudo_type_info VPARAMS((const char *real_name, int ident, ...))
1749 {
1750 #ifndef ANSI_PROTOTYPES
1751   char const *real_name;
1752   int ident;
1753 #endif
1754   va_list ap;
1755   tree real_type, pseudo_type;
1756   char *pseudo_name;
1757   tree vtable_decl;
1758   int ix;
1759   tree fields[10];
1760   tree field_decl;
1761   tree result;
1762   
1763   VA_START (ap, ident);
1764 #ifndef ANSI_PROTOTYPES
1765   real_name = va_arg (ap, char const *);
1766   ident = va_arg (app, int);
1767 #endif
1768
1769   /* Generate the pseudo type name. */
1770   pseudo_name = (char *)alloca (strlen (real_name) + 30);
1771   strcpy (pseudo_name, real_name);
1772   strcat (pseudo_name, "_pseudo");
1773   if (ident)
1774     sprintf (pseudo_name + strlen (pseudo_name), "%d", ident);
1775   
1776   /* Get the vtable decl. */
1777   real_type = xref_tag (class_type_node, get_identifier (real_name), 1);
1778   vtable_decl = get_vtable_decl (real_type, /*complete=*/1);
1779   vtable_decl = build_unary_op (ADDR_EXPR, vtable_decl, 0);
1780
1781   /* Under the new ABI, we need to point into the middle of the
1782      vtable.  */
1783   if (flag_new_abi)
1784     {
1785       vtable_decl = build (PLUS_EXPR,
1786                            TREE_TYPE (vtable_decl),
1787                            vtable_decl,
1788                            size_binop (MULT_EXPR,
1789                                        size_int (2),
1790                                        TYPE_SIZE_UNIT (vtable_entry_type)));
1791       TREE_CONSTANT (vtable_decl) = 1;
1792     }
1793
1794   /* First field is the pseudo type_info base class. */
1795   fields[0] = build_decl (FIELD_DECL, NULL_TREE, ti_desc_type_node);
1796   
1797   /* Now add the derived fields.  */
1798   for (ix = 0; (field_decl = va_arg (ap, tree));)
1799     fields[++ix] = field_decl;
1800   
1801   /* Create the pseudo type. */
1802   pseudo_type = make_aggr_type (RECORD_TYPE);
1803   finish_builtin_type (pseudo_type, pseudo_name, fields, ix, ptr_type_node);
1804   TYPE_HAS_CONSTRUCTOR (pseudo_type) = 1;
1805   va_end (ap);
1806   
1807   result = tree_cons (NULL_TREE, NULL_TREE, NULL_TREE);
1808   TINFO_VTABLE_DECL (result) = vtable_decl;
1809   TINFO_PSEUDO_TYPE (result) = pseudo_type;
1810   
1811   return result;
1812 }
1813
1814 /* Return a descriptor for a vmi type with NUM_BASES bases.  */
1815
1816 static tree
1817 get_vmi_pseudo_type_info (num_bases)
1818      int num_bases;
1819 {
1820   tree desc;
1821   tree array_domain, base_array;
1822   
1823   if (TREE_VEC_LENGTH (vmi_class_desc_type_node) <= num_bases)
1824     {
1825       int ix;
1826       tree extend = make_tree_vec (num_bases + 5);
1827       
1828       for (ix = TREE_VEC_LENGTH (vmi_class_desc_type_node); ix--;)
1829         TREE_VEC_ELT (extend, ix) = TREE_VEC_ELT (vmi_class_desc_type_node, ix);
1830       vmi_class_desc_type_node = extend;
1831     }
1832   desc = TREE_VEC_ELT (vmi_class_desc_type_node, num_bases);
1833   
1834   if (desc)
1835     return desc;
1836   
1837   /* Add number of bases and trailing array of base_class_type_info.  */
1838   array_domain = build_index_type (build_int_2 (num_bases, 0));
1839   base_array = build_array_type (base_desc_type_node, array_domain);
1840
1841   push_nested_namespace (abi_node);
1842
1843   desc = create_pseudo_type_info
1844             ("__vmi_class_type_info", num_bases,
1845              build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1846              build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1847              build_decl (FIELD_DECL, NULL_TREE, base_array),
1848              NULL);
1849
1850   pop_nested_namespace (abi_node);
1851
1852   TREE_VEC_ELT (vmi_class_desc_type_node, num_bases) = desc;
1853   return desc;
1854 }
1855
1856 /* Make sure the required builtin types exist for generating the type_info
1857    varable definitions.  */
1858
1859 static void
1860 create_tinfo_types ()
1861 {
1862   tree ptr_type_info;
1863   
1864   if (bltn_desc_type_node)
1865     return;
1866   push_nested_namespace (abi_node);
1867
1868   ptr_type_info = build_pointer_type
1869                     (build_qualified_type
1870                       (type_info_type_node, TYPE_QUAL_CONST));
1871   
1872   /* Create the internal type_info structure. This is used as a base for
1873      the other structures.  */
1874   {
1875     tree fields[2];
1876
1877     ti_desc_type_node = make_aggr_type (RECORD_TYPE);
1878     fields[0] = build_decl (FIELD_DECL, NULL_TREE, const_ptr_type_node);
1879     fields[1] = build_decl (FIELD_DECL, NULL_TREE, const_string_type_node);
1880     finish_builtin_type (ti_desc_type_node, "__type_info_pseudo",
1881                          fields, 1, ptr_type_node);
1882     TYPE_HAS_CONSTRUCTOR (ti_desc_type_node) = 1;
1883   }
1884   
1885   /* Fundamental type_info */
1886   bltn_desc_type_node = create_pseudo_type_info
1887       ("__fundamental_type_info", 0,
1888        NULL);
1889
1890   /* Array, function and enum type_info. No additional fields. */
1891   ary_desc_type_node = create_pseudo_type_info
1892       ("__array_type_info", 0,
1893        NULL);
1894   func_desc_type_node = create_pseudo_type_info
1895        ("__function_type_info", 0,
1896         NULL);
1897   enum_desc_type_node = create_pseudo_type_info
1898        ("__enum_type_info", 0,
1899         NULL);
1900   
1901   /* Class type_info. Add a flags field.  */
1902   class_desc_type_node = create_pseudo_type_info
1903         ("__class_type_info", 0,
1904          NULL);
1905   
1906   /* Single public non-virtual base class. Add pointer to base class. 
1907      This is really a descendant of __class_type_info.  */
1908   si_class_desc_type_node = create_pseudo_type_info
1909            ("__si_class_type_info", 0,
1910             build_decl (FIELD_DECL, NULL_TREE, ptr_type_info),
1911             NULL);
1912   
1913   /* Base class internal helper. Pointer to base type, offset to base,
1914      flags. */
1915   {
1916     tree fields[2];
1917     
1918     fields[0] = build_decl (FIELD_DECL, NULL_TREE, ptr_type_info);
1919     fields[1] = build_decl (FIELD_DECL, NULL_TREE, integer_types[itk_long]);
1920     base_desc_type_node = make_aggr_type (RECORD_TYPE);
1921     finish_builtin_type (base_desc_type_node, "__base_class_type_info_pseudo",
1922                          fields, 1, ptr_type_node);
1923     TYPE_HAS_CONSTRUCTOR (base_desc_type_node) = 1;
1924   }
1925   
1926   /* General heirarchy is created as necessary in this vector. */
1927   vmi_class_desc_type_node = make_tree_vec (10);
1928   
1929   /* Pointer type_info. Adds two fields, qualification mask
1930      and pointer to the pointed to type.  This is really a descendant of
1931      __pbase_type_info. */
1932   ptr_desc_type_node = create_pseudo_type_info
1933       ("__pointer_type_info", 0,
1934        build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1935        build_decl (FIELD_DECL, NULL_TREE, ptr_type_info),
1936        NULL);
1937
1938   /* Pointer to member data type_info.  Add qualifications flags,
1939      pointer to the member's type info and pointer to the class.
1940      This is really a descendant of __pbase_type_info.  */
1941   ptm_desc_type_node = create_pseudo_type_info
1942        ("__pointer_to_member_type_info", 0,
1943         build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1944         build_decl (FIELD_DECL, NULL_TREE, ptr_type_info),
1945         build_decl (FIELD_DECL, NULL_TREE, ptr_type_info),
1946         NULL);
1947
1948   pop_nested_namespace (abi_node);
1949 }
1950
1951 /* Emit the type_info descriptors which are guaranteed to be in the runtime
1952    support.  Generating them here guarantees consistency with the other
1953    structures.  We use the following heuristic to determine when the runtime
1954    is being generated.  If std::__fundamental_type_info is defined, and it's
1955    destructor is defined, then the runtime is being built.  */
1956
1957 void
1958 emit_support_tinfos ()
1959 {
1960   static tree *const fundamentals[] =
1961   {
1962     &void_type_node,
1963     &boolean_type_node,
1964     &wchar_type_node,
1965     &char_type_node, &signed_char_type_node, &unsigned_char_type_node,
1966     &short_integer_type_node, &short_unsigned_type_node,
1967     &integer_type_node, &unsigned_type_node,
1968     &long_integer_type_node, &long_unsigned_type_node,
1969     &long_long_integer_type_node, &long_long_unsigned_type_node,
1970     &float_type_node, &double_type_node, &long_double_type_node,
1971     0
1972   };
1973   int ix;
1974   tree bltn_type, dtor;
1975   
1976   push_nested_namespace (abi_node);
1977   bltn_type = xref_tag (class_type_node,
1978                         get_identifier ("__fundamental_type_info"), 1);
1979   pop_nested_namespace (abi_node);
1980   if (!COMPLETE_TYPE_P (bltn_type))
1981     return;
1982   dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (bltn_type), 1);
1983   if (DECL_EXTERNAL (dtor))
1984     return;
1985   doing_runtime = 1;
1986   for (ix = 0; fundamentals[ix]; ix++)
1987     {
1988       tree bltn = *fundamentals[ix];
1989       tree bltn_ptr = build_pointer_type (bltn);
1990       tree bltn_const_ptr = build_pointer_type
1991               (build_qualified_type (bltn, TYPE_QUAL_CONST));
1992       tree tinfo;
1993       
1994       tinfo = get_tinfo_decl (bltn);
1995       TREE_USED (tinfo) = 1;
1996       TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (tinfo)) = 1;
1997       
1998       tinfo = get_tinfo_decl (bltn_ptr);
1999       TREE_USED (tinfo) = 1;
2000       TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (tinfo)) = 1;
2001       
2002       tinfo = get_tinfo_decl (bltn_const_ptr);
2003       TREE_USED (tinfo) = 1;
2004       TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (tinfo)) = 1;
2005     }
2006 }
2007
2008 /* Return non-zero, iff T is a type_info variable which has not had a
2009    definition emitted for it.  */
2010
2011 int
2012 tinfo_decl_p (t, data)
2013      tree t;
2014      void *data ATTRIBUTE_UNUSED;
2015 {
2016   return TREE_CODE (t) == VAR_DECL
2017          && IDENTIFIER_GLOBAL_VALUE (DECL_NAME (t)) == (t)
2018          && TREE_TYPE (t) == tinfo_decl_type
2019          && TREE_TYPE (DECL_NAME (t));
2020 }
2021
2022 /* Emit a suitable type_info definition for the type_info decl pointed to by
2023    DECL_PTR. We emit a completely new variable, of the correct type for the
2024    actual type this is describing. The DECL_ASSEMBLER_NAME of the generated
2025    definition is set to that of the supplied decl, so that they can be tied
2026    up. Mark the supplied decl as having been dealt with. Emitting one
2027    definition might cause other definitions to be required.
2028    
2029    We need to do things this way, because we're trying to do something like
2030    
2031       struct B : A {
2032         ...
2033       };
2034    
2035       extern const A tinfo_var;
2036    
2037       const B tinfo_var = {...};
2038    
2039    which is not permitted. Also, we've not necessarily seen the definition of B.
2040    So we do something like the following,
2041    
2042       extern const A tinfo_var;
2043    
2044       struct pseudo_A {
2045         const void *vtable_ptr;
2046         const char *name;
2047       };
2048       struct pseudo_B {
2049         pseudo_A base;
2050         ...
2051       };
2052       
2053       const pseudo_B proxy_tinfo_var attribute((assembler_name="tinfo_var")) =
2054       {
2055         {&B::vtable, "..."},
2056         ...
2057       };
2058    
2059    pseudo_A and pseudo_B must be layout equivalent to the real definitions in
2060    the runtime.  */
2061
2062 int
2063 emit_tinfo_decl (decl_ptr, data)
2064      tree *decl_ptr;
2065      void *data ATTRIBUTE_UNUSED;
2066 {
2067   tree tinfo_decl = *decl_ptr;
2068   tree tinfo_type, decl;
2069   
2070   my_friendly_assert (TREE_TYPE (tinfo_decl) == tinfo_decl_type, 20000121);
2071   tinfo_type = TREE_TYPE (DECL_NAME (tinfo_decl));
2072   my_friendly_assert (tinfo_type != NULL_TREE, 20000120);
2073   
2074   if (!DECL_NEEDED_P (tinfo_decl))
2075     return 0;
2076   /* Say we've dealt with it.  */
2077   TREE_TYPE (DECL_NAME (tinfo_decl)) = NULL_TREE;
2078   
2079   create_tinfo_types ();
2080   decl = synthesize_tinfo_var (tinfo_type, DECL_ASSEMBLER_NAME (tinfo_decl));
2081   
2082   return decl != 0;
2083 }