OSDN Git Service

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