OSDN Git Service

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