OSDN Git Service

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