OSDN Git Service

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