OSDN Git Service

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