OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [gcc-4.5.1/] [gcc/] [cp/] [rtti.c] - Blame information for rev 304

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 283 jeremybenn
/* RunTime Type Identification
2
   Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3
   2005, 2006, 2007, 2008, 2009
4
   Free Software Foundation, Inc.
5
   Mostly written by Jason Merrill (jason@cygnus.com).
6
 
7
This file is part of GCC.
8
 
9
GCC is free software; you can redistribute it and/or modify
10
it under the terms of the GNU General Public License as published by
11
the Free Software Foundation; either version 3, or (at your option)
12
any later version.
13
 
14
GCC is distributed in the hope that it will be useful,
15
but WITHOUT ANY WARRANTY; without even the implied warranty of
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
GNU General Public License for more details.
18
 
19
You should have received a copy of the GNU General Public License
20
along with GCC; see the file COPYING3.  If not see
21
<http://www.gnu.org/licenses/>.  */
22
 
23
#include "config.h"
24
#include "system.h"
25
#include "intl.h"
26
#include "coretypes.h"
27
#include "tm.h"
28
#include "tree.h"
29
#include "cp-tree.h"
30
#include "flags.h"
31
#include "output.h"
32
#include "assert.h"
33
#include "toplev.h"
34
#include "convert.h"
35
#include "target.h"
36
#include "c-pragma.h"
37
 
38
/* C++ returns type information to the user in struct type_info
39
   objects. We also use type information to implement dynamic_cast and
40
   exception handlers. Type information for a particular type is
41
   indicated with an ABI defined structure derived from type_info.
42
   This would all be very straight forward, but for the fact that the
43
   runtime library provides the definitions of the type_info structure
44
   and the ABI defined derived classes. We cannot build declarations
45
   of them directly in the compiler, but we need to layout objects of
46
   their type.  Somewhere we have to lie.
47
 
48
   We define layout compatible POD-structs with compiler-defined names
49
   and generate the appropriate initializations for them (complete
50
   with explicit mention of their vtable). When we have to provide a
51
   type_info to the user we reinterpret_cast the internal compiler
52
   type to type_info.  A well formed program can only explicitly refer
53
   to the type_infos of complete types (& cv void).  However, we chain
54
   pointer type_infos to the pointed-to-type, and that can be
55
   incomplete.  We only need the addresses of such incomplete
56
   type_info objects for static initialization.
57
 
58
   The type information VAR_DECL of a type is held on the
59
   IDENTIFIER_GLOBAL_VALUE of the type's mangled name. That VAR_DECL
60
   will be the internal type.  It will usually have the correct
61
   internal type reflecting the kind of type it represents (pointer,
62
   array, function, class, inherited class, etc).  When the type it
63
   represents is incomplete, it will have the internal type
64
   corresponding to type_info.  That will only happen at the end of
65
   translation, when we are emitting the type info objects.  */
66
 
67
/* Auxiliary data we hold for each type_info derived object we need.  */
68
typedef struct GTY (()) tinfo_s {
69
  tree type;  /* The RECORD_TYPE for this type_info object */
70
 
71
  tree vtable; /* The VAR_DECL of the vtable.  Only filled at end of
72
                  translation.  */
73
 
74
  tree name;  /* IDENTIFIER_NODE for the ABI specified name of
75
                 the type_info derived type.  */
76
} tinfo_s;
77
 
78
DEF_VEC_O(tinfo_s);
79
DEF_VEC_ALLOC_O(tinfo_s,gc);
80
 
81
typedef enum tinfo_kind
82
{
83
  TK_TYPE_INFO_TYPE,    /* abi::__type_info_pseudo */
84
  TK_BASE_TYPE,         /* abi::__base_class_type_info */
85
  TK_BUILTIN_TYPE,      /* abi::__fundamental_type_info */
86
  TK_ARRAY_TYPE,        /* abi::__array_type_info */
87
  TK_FUNCTION_TYPE,     /* abi::__function_type_info */
88
  TK_ENUMERAL_TYPE,     /* abi::__enum_type_info */
89
  TK_POINTER_TYPE,      /* abi::__pointer_type_info */
90
  TK_POINTER_MEMBER_TYPE, /* abi::__pointer_to_member_type_info */
91
  TK_CLASS_TYPE,        /* abi::__class_type_info */
92
  TK_SI_CLASS_TYPE,     /* abi::__si_class_type_info */
93
  TK_FIXED              /* end of fixed descriptors. */
94
  /* ...                   abi::__vmi_type_info<I> */
95
} tinfo_kind;
96
 
97
/* A vector of all tinfo decls that haven't yet been emitted.  */
98
VEC(tree,gc) *unemitted_tinfo_decls;
99
 
100
/* A vector of all type_info derived types we need.  The first few are
101
   fixed and created early. The remainder are for multiple inheritance
102
   and are generated as needed. */
103
static GTY (()) VEC(tinfo_s,gc) *tinfo_descs;
104
 
105
static tree ifnonnull (tree, tree);
106
static tree tinfo_name (tree, bool);
107
static tree build_dynamic_cast_1 (tree, tree, tsubst_flags_t);
108
static tree throw_bad_cast (void);
109
static tree throw_bad_typeid (void);
110
static tree get_tinfo_decl_dynamic (tree);
111
static tree get_tinfo_ptr (tree);
112
static bool typeid_ok_p (void);
113
static int qualifier_flags (tree);
114
static bool target_incomplete_p (tree);
115
static tree tinfo_base_init (tinfo_s *, tree);
116
static tree generic_initializer (tinfo_s *, tree);
117
static tree ptr_initializer (tinfo_s *, tree);
118
static tree ptm_initializer (tinfo_s *, tree);
119
static tree class_initializer (tinfo_s *, tree, tree);
120
static void create_pseudo_type_info (int, const char *, ...);
121
static tree get_pseudo_ti_init (tree, unsigned);
122
static unsigned get_pseudo_ti_index (tree);
123
static void create_tinfo_types (void);
124
static bool typeinfo_in_lib_p (tree);
125
 
126
static int doing_runtime = 0;
127
 
128
static void
129
push_abi_namespace (void)
130
{
131
  push_nested_namespace (abi_node);
132
  push_visibility ("default", 2);
133
}
134
 
135
static void
136
pop_abi_namespace (void)
137
{
138
  pop_visibility (2);
139
  pop_nested_namespace (abi_node);
140
}
141
 
142
/* Declare language defined type_info type and a pointer to const
143
   type_info.  This is incomplete here, and will be completed when
144
   the user #includes <typeinfo>.  There are language defined
145
   restrictions on what can be done until that is included.  Create
146
   the internal versions of the ABI types.  */
147
 
148
void
149
init_rtti_processing (void)
150
{
151
  tree type_info_type;
152
 
153
  push_namespace (std_identifier);
154
  type_info_type = xref_tag (class_type, get_identifier ("type_info"),
155
                             /*tag_scope=*/ts_current, false);
156
  pop_namespace ();
157
  const_type_info_type_node
158
    = build_qualified_type (type_info_type, TYPE_QUAL_CONST);
159
  type_info_ptr_type = build_pointer_type (const_type_info_type_node);
160
 
161
  unemitted_tinfo_decls = VEC_alloc (tree, gc, 124);
162
 
163
  create_tinfo_types ();
164
}
165
 
166
/* Given the expression EXP of type `class *', return the head of the
167
   object pointed to by EXP with type cv void*, if the class has any
168
   virtual functions (TYPE_POLYMORPHIC_P), else just return the
169
   expression.  */
170
 
171
tree
172
build_headof (tree exp)
173
{
174
  tree type = TREE_TYPE (exp);
175
  tree offset;
176
  tree index;
177
 
178
  gcc_assert (TREE_CODE (type) == POINTER_TYPE);
179
  type = TREE_TYPE (type);
180
 
181
  if (!TYPE_POLYMORPHIC_P (type))
182
    return exp;
183
 
184
  /* We use this a couple of times below, protect it.  */
185
  exp = save_expr (exp);
186
 
187
  /* The offset-to-top field is at index -2 from the vptr.  */
188
  index = build_int_cst (NULL_TREE,
189
                         -2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
190
 
191
  offset = build_vtbl_ref (cp_build_indirect_ref (exp, RO_NULL,
192
                                                  tf_warning_or_error),
193
                           index);
194
 
195
  type = build_qualified_type (ptr_type_node,
196
                               cp_type_quals (TREE_TYPE (exp)));
197
  return build2 (POINTER_PLUS_EXPR, type, exp,
198
                 convert_to_integer (sizetype, offset));
199
}
200
 
201
/* Get a bad_cast node for the program to throw...
202
 
203
   See libstdc++/exception.cc for __throw_bad_cast */
204
 
205
static tree
206
throw_bad_cast (void)
207
{
208
  tree fn = get_identifier ("__cxa_bad_cast");
209
  if (!get_global_value_if_present (fn, &fn))
210
    fn = push_throw_library_fn (fn, build_function_type (ptr_type_node,
211
                                                         void_list_node));
212
 
213
  return build_cxx_call (fn, 0, NULL);
214
}
215
 
216
/* Return an expression for "__cxa_bad_typeid()".  The expression
217
   returned is an lvalue of type "const std::type_info".  */
218
 
219
static tree
220
throw_bad_typeid (void)
221
{
222
  tree fn = get_identifier ("__cxa_bad_typeid");
223
  if (!get_global_value_if_present (fn, &fn))
224
    {
225
      tree t;
226
 
227
      t = build_reference_type (const_type_info_type_node);
228
      t = build_function_type (t, void_list_node);
229
      fn = push_throw_library_fn (fn, t);
230
    }
231
 
232
  return build_cxx_call (fn, 0, NULL);
233
}
234
 
235
/* Return an lvalue expression whose type is "const std::type_info"
236
   and whose value indicates the type of the expression EXP.  If EXP
237
   is a reference to a polymorphic class, return the dynamic type;
238
   otherwise return the static type of the expression.  */
239
 
240
static tree
241
get_tinfo_decl_dynamic (tree exp)
242
{
243
  tree type;
244
  tree t;
245
 
246
  if (error_operand_p (exp))
247
    return error_mark_node;
248
 
249
  exp = resolve_nondeduced_context (exp);
250
 
251
  /* peel back references, so they match.  */
252
  type = non_reference (TREE_TYPE (exp));
253
 
254
  /* Peel off cv qualifiers.  */
255
  type = TYPE_MAIN_VARIANT (type);
256
 
257
  /* For UNKNOWN_TYPEs call complete_type_or_else to get diagnostics.  */
258
  if (CLASS_TYPE_P (type) || TREE_CODE (type) == UNKNOWN_TYPE)
259
    type = complete_type_or_else (type, exp);
260
 
261
  if (!type)
262
    return error_mark_node;
263
 
264
  /* If exp is a reference to polymorphic type, get the real type_info.  */
265
  if (TYPE_POLYMORPHIC_P (type) && ! resolves_to_fixed_type_p (exp, 0))
266
    {
267
      /* build reference to type_info from vtable.  */
268
      tree index;
269
 
270
      /* The RTTI information is at index -1.  */
271
      index = build_int_cst (NULL_TREE,
272
                             -1 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
273
      t = build_vtbl_ref (exp, index);
274
      t = convert (type_info_ptr_type, t);
275
    }
276
  else
277
    /* Otherwise return the type_info for the static type of the expr.  */
278
    t = get_tinfo_ptr (TYPE_MAIN_VARIANT (type));
279
 
280
  return cp_build_indirect_ref (t, RO_NULL, tf_warning_or_error);
281
}
282
 
283
static bool
284
typeid_ok_p (void)
285
{
286
  tree pseudo_type_info, type_info_type;
287
 
288
  if (! flag_rtti)
289
    {
290
      error ("cannot use typeid with -fno-rtti");
291
      return false;
292
    }
293
 
294
  if (!COMPLETE_TYPE_P (const_type_info_type_node))
295
    {
296
      error ("must #include <typeinfo> before using typeid");
297
      return false;
298
    }
299
 
300
  pseudo_type_info
301
    = VEC_index (tinfo_s, tinfo_descs, TK_TYPE_INFO_TYPE)->type;
302
  type_info_type = TYPE_MAIN_VARIANT (const_type_info_type_node);
303
 
304
  /* Make sure abi::__type_info_pseudo has the same alias set
305
     as std::type_info.  */
306
  if (! TYPE_ALIAS_SET_KNOWN_P (pseudo_type_info))
307
    TYPE_ALIAS_SET (pseudo_type_info) = get_alias_set (type_info_type);
308
  else
309
    gcc_assert (TYPE_ALIAS_SET (pseudo_type_info)
310
                == get_alias_set (type_info_type));
311
 
312
  return true;
313
}
314
 
315
/* Return an expression for "typeid(EXP)".  The expression returned is
316
   an lvalue of type "const std::type_info".  */
317
 
318
tree
319
build_typeid (tree exp)
320
{
321
  tree cond = NULL_TREE;
322
  int nonnull = 0;
323
 
324
  if (exp == error_mark_node || !typeid_ok_p ())
325
    return error_mark_node;
326
 
327
  if (processing_template_decl)
328
    return build_min (TYPEID_EXPR, const_type_info_type_node, exp);
329
 
330
  if (TREE_CODE (exp) == INDIRECT_REF
331
      && TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == POINTER_TYPE
332
      && TYPE_POLYMORPHIC_P (TREE_TYPE (exp))
333
      && ! resolves_to_fixed_type_p (exp, &nonnull)
334
      && ! nonnull)
335
    {
336
      exp = stabilize_reference (exp);
337
      cond = cp_convert (boolean_type_node, TREE_OPERAND (exp, 0));
338
    }
339
 
340
  exp = get_tinfo_decl_dynamic (exp);
341
 
342
  if (exp == error_mark_node)
343
    return error_mark_node;
344
 
345
  if (cond)
346
    {
347
      tree bad = throw_bad_typeid ();
348
 
349
      exp = build3 (COND_EXPR, TREE_TYPE (exp), cond, exp, bad);
350
    }
351
 
352
  return exp;
353
}
354
 
355
/* Generate the NTBS name of a type.  If MARK_PRIVATE, put a '*' in front so that
356
   comparisons will be done by pointer rather than string comparison.  */
357
static tree
358
tinfo_name (tree type, bool mark_private)
359
{
360
  const char *name;
361
  int length;
362
  tree name_string;
363
 
364
  name = mangle_type_string (type);
365
  length = strlen (name);
366
 
367
  if (mark_private)
368
    {
369
      /* Inject '*' at beginning of name to force pointer comparison.  */
370
      char* buf = (char*) XALLOCAVEC (char, length + 2);
371
      buf[0] = '*';
372
      memcpy (buf + 1, name, length + 1);
373
      name_string = build_string (length + 2, buf);
374
    }
375
  else
376
    name_string = build_string (length + 1, name);
377
 
378
  return fix_string_type (name_string);
379
}
380
 
381
/* Return a VAR_DECL for the internal ABI defined type_info object for
382
   TYPE. You must arrange that the decl is mark_used, if actually use
383
   it --- decls in vtables are only used if the vtable is output.  */
384
 
385
tree
386
get_tinfo_decl (tree type)
387
{
388
  tree name;
389
  tree d;
390
 
391
  if (variably_modified_type_p (type, /*fn=*/NULL_TREE))
392
    {
393
      error ("cannot create type information for type %qT because "
394
             "it involves types of variable size",
395
             type);
396
      return error_mark_node;
397
    }
398
 
399
  if (TREE_CODE (type) == METHOD_TYPE)
400
    type = build_function_type (TREE_TYPE (type),
401
                                TREE_CHAIN (TYPE_ARG_TYPES (type)));
402
 
403
  /* For a class type, the variable is cached in the type node
404
     itself.  */
405
  if (CLASS_TYPE_P (type))
406
    {
407
      d = CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type));
408
      if (d)
409
        return d;
410
    }
411
 
412
  name = mangle_typeinfo_for_type (type);
413
 
414
  d = IDENTIFIER_GLOBAL_VALUE (name);
415
  if (!d)
416
    {
417
      int ix = get_pseudo_ti_index (type);
418
      tinfo_s *ti = VEC_index (tinfo_s, tinfo_descs, ix);
419
 
420
      d = build_lang_decl (VAR_DECL, name, ti->type);
421
      SET_DECL_ASSEMBLER_NAME (d, name);
422
      /* Remember the type it is for.  */
423
      TREE_TYPE (name) = type;
424
      DECL_TINFO_P (d) = 1;
425
      DECL_ARTIFICIAL (d) = 1;
426
      DECL_IGNORED_P (d) = 1;
427
      TREE_READONLY (d) = 1;
428
      TREE_STATIC (d) = 1;
429
      /* Mark the variable as undefined -- but remember that we can
430
         define it later if we need to do so.  */
431
      DECL_EXTERNAL (d) = 1;
432
      DECL_NOT_REALLY_EXTERN (d) = 1;
433
      set_linkage_according_to_type (type, d);
434
 
435
      d = pushdecl_top_level_and_finish (d, NULL_TREE);
436
      if (CLASS_TYPE_P (type))
437
        CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type)) = d;
438
 
439
      /* Add decl to the global array of tinfo decls.  */
440
      VEC_safe_push (tree, gc, unemitted_tinfo_decls, d);
441
    }
442
 
443
  return d;
444
}
445
 
446
/* Return a pointer to a type_info object describing TYPE, suitably
447
   cast to the language defined type.  */
448
 
449
static tree
450
get_tinfo_ptr (tree type)
451
{
452
  tree decl = get_tinfo_decl (type);
453
 
454
  mark_used (decl);
455
  return build_nop (type_info_ptr_type,
456
                    build_address (decl));
457
}
458
 
459
/* Return the type_info object for TYPE.  */
460
 
461
tree
462
get_typeid (tree type)
463
{
464
  if (type == error_mark_node || !typeid_ok_p ())
465
    return error_mark_node;
466
 
467
  if (processing_template_decl)
468
    return build_min (TYPEID_EXPR, const_type_info_type_node, type);
469
 
470
  /* If the type of the type-id is a reference type, the result of the
471
     typeid expression refers to a type_info object representing the
472
     referenced type.  */
473
  type = non_reference (type);
474
 
475
  /* The top-level cv-qualifiers of the lvalue expression or the type-id
476
     that is the operand of typeid are always ignored.  */
477
  type = TYPE_MAIN_VARIANT (type);
478
 
479
  /* For UNKNOWN_TYPEs call complete_type_or_else to get diagnostics.  */
480
  if (CLASS_TYPE_P (type) || TREE_CODE (type) == UNKNOWN_TYPE)
481
    type = complete_type_or_else (type, NULL_TREE);
482
 
483
  if (!type)
484
    return error_mark_node;
485
 
486
  return cp_build_indirect_ref (get_tinfo_ptr (type), RO_NULL,
487
                                tf_warning_or_error);
488
}
489
 
490
/* Check whether TEST is null before returning RESULT.  If TEST is used in
491
   RESULT, it must have previously had a save_expr applied to it.  */
492
 
493
static tree
494
ifnonnull (tree test, tree result)
495
{
496
  return build3 (COND_EXPR, TREE_TYPE (result),
497
                 build2 (EQ_EXPR, boolean_type_node, test,
498
                         cp_convert (TREE_TYPE (test), integer_zero_node)),
499
                 cp_convert (TREE_TYPE (result), integer_zero_node),
500
                 result);
501
}
502
 
503
/* Execute a dynamic cast, as described in section 5.2.6 of the 9/93 working
504
   paper.  */
505
 
506
static tree
507
build_dynamic_cast_1 (tree type, tree expr, tsubst_flags_t complain)
508
{
509
  enum tree_code tc = TREE_CODE (type);
510
  tree exprtype = TREE_TYPE (expr);
511
  tree dcast_fn;
512
  tree old_expr = expr;
513
  const char *errstr = NULL;
514
 
515
  /* Save casted types in the function's used types hash table.  */
516
  used_types_insert (type);
517
 
518
  /* T shall be a pointer or reference to a complete class type, or
519
     `pointer to cv void''.  */
520
  switch (tc)
521
    {
522
    case POINTER_TYPE:
523
      if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
524
        break;
525
      /* Fall through.  */
526
    case REFERENCE_TYPE:
527
      if (! MAYBE_CLASS_TYPE_P (TREE_TYPE (type)))
528
        {
529
          errstr = _("target is not pointer or reference to class");
530
          goto fail;
531
        }
532
      if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
533
        {
534
          errstr = _("target is not pointer or reference to complete type");
535
          goto fail;
536
        }
537
      break;
538
 
539
    default:
540
      errstr = _("target is not pointer or reference");
541
      goto fail;
542
    }
543
 
544
  if (tc == POINTER_TYPE)
545
    {
546
      /* If T is a pointer type, v shall be an rvalue of a pointer to
547
         complete class type, and the result is an rvalue of type T.  */
548
 
549
      if (TREE_CODE (exprtype) != POINTER_TYPE)
550
        {
551
          errstr = _("source is not a pointer");
552
          goto fail;
553
        }
554
      if (! MAYBE_CLASS_TYPE_P (TREE_TYPE (exprtype)))
555
        {
556
          errstr = _("source is not a pointer to class");
557
          goto fail;
558
        }
559
      if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
560
        {
561
          errstr = _("source is a pointer to incomplete type");
562
          goto fail;
563
        }
564
    }
565
  else
566
    {
567
      exprtype = build_reference_type (exprtype);
568
 
569
      /* T is a reference type, v shall be an lvalue of a complete class
570
         type, and the result is an lvalue of the type referred to by T.  */
571
 
572
      if (! MAYBE_CLASS_TYPE_P (TREE_TYPE (exprtype)))
573
        {
574
          errstr = _("source is not of class type");
575
          goto fail;
576
        }
577
      if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
578
        {
579
          errstr = _("source is of incomplete class type");
580
          goto fail;
581
        }
582
 
583
      /* Apply trivial conversion T -> T& for dereferenced ptrs.  */
584
      expr = convert_to_reference (exprtype, expr, CONV_IMPLICIT,
585
                                   LOOKUP_NORMAL, NULL_TREE);
586
    }
587
 
588
  /* The dynamic_cast operator shall not cast away constness.  */
589
  if (!at_least_as_qualified_p (TREE_TYPE (type),
590
                                TREE_TYPE (exprtype)))
591
    {
592
      errstr = _("conversion casts away constness");
593
      goto fail;
594
    }
595
 
596
  /* If *type is an unambiguous accessible base class of *exprtype,
597
     convert statically.  */
598
  {
599
    tree binfo;
600
 
601
    binfo = lookup_base (TREE_TYPE (exprtype), TREE_TYPE (type),
602
                         ba_check, NULL);
603
 
604
    if (binfo)
605
      {
606
        expr = build_base_path (PLUS_EXPR, convert_from_reference (expr),
607
                                binfo, 0);
608
        if (TREE_CODE (exprtype) == POINTER_TYPE)
609
          expr = rvalue (expr);
610
        return expr;
611
      }
612
  }
613
 
614
  /* Otherwise *exprtype must be a polymorphic class (have a vtbl).  */
615
  if (TYPE_POLYMORPHIC_P (TREE_TYPE (exprtype)))
616
    {
617
      tree expr1;
618
      /* if TYPE is `void *', return pointer to complete object.  */
619
      if (tc == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (type)))
620
        {
621
          /* if b is an object, dynamic_cast<void *>(&b) == (void *)&b.  */
622
          if (TREE_CODE (expr) == ADDR_EXPR
623
              && TREE_CODE (TREE_OPERAND (expr, 0)) == VAR_DECL
624
              && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == RECORD_TYPE)
625
            return build1 (NOP_EXPR, type, expr);
626
 
627
          /* Since expr is used twice below, save it.  */
628
          expr = save_expr (expr);
629
 
630
          expr1 = build_headof (expr);
631
          if (TREE_TYPE (expr1) != type)
632
            expr1 = build1 (NOP_EXPR, type, expr1);
633
          return ifnonnull (expr, expr1);
634
        }
635
      else
636
        {
637
          tree retval;
638
          tree result, td2, td3;
639
          tree elems[4];
640
          tree static_type, target_type, boff;
641
 
642
          /* If we got here, we can't convert statically.  Therefore,
643
             dynamic_cast<D&>(b) (b an object) cannot succeed.  */
644
          if (tc == REFERENCE_TYPE)
645
            {
646
              if (TREE_CODE (old_expr) == VAR_DECL
647
                  && TREE_CODE (TREE_TYPE (old_expr)) == RECORD_TYPE)
648
                {
649
                  tree expr = throw_bad_cast ();
650
                  if (complain & tf_warning)
651
                    warning (0, "dynamic_cast of %q#D to %q#T can never succeed",
652
                             old_expr, type);
653
                  /* Bash it to the expected type.  */
654
                  TREE_TYPE (expr) = type;
655
                  return expr;
656
                }
657
            }
658
          /* Ditto for dynamic_cast<D*>(&b).  */
659
          else if (TREE_CODE (expr) == ADDR_EXPR)
660
            {
661
              tree op = TREE_OPERAND (expr, 0);
662
              if (TREE_CODE (op) == VAR_DECL
663
                  && TREE_CODE (TREE_TYPE (op)) == RECORD_TYPE)
664
                {
665
                  if (complain & tf_warning)
666
                    warning (0, "dynamic_cast of %q#D to %q#T can never succeed",
667
                             op, type);
668
                  retval = build_int_cst (type, 0);
669
                  return retval;
670
                }
671
            }
672
 
673
          /* Use of dynamic_cast when -fno-rtti is prohibited.  */
674
          if (!flag_rtti)
675
            {
676
              if (complain & tf_error)
677
                error ("%<dynamic_cast%> not permitted with -fno-rtti");
678
              return error_mark_node;
679
            }
680
 
681
          target_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
682
          static_type = TYPE_MAIN_VARIANT (TREE_TYPE (exprtype));
683
          td2 = get_tinfo_decl (target_type);
684
          mark_used (td2);
685
          td2 = cp_build_unary_op (ADDR_EXPR, td2, 0, complain);
686
          td3 = get_tinfo_decl (static_type);
687
          mark_used (td3);
688
          td3 = cp_build_unary_op (ADDR_EXPR, td3, 0, complain);
689
 
690
          /* Determine how T and V are related.  */
691
          boff = dcast_base_hint (static_type, target_type);
692
 
693
          /* Since expr is used twice below, save it.  */
694
          expr = save_expr (expr);
695
 
696
          expr1 = expr;
697
          if (tc == REFERENCE_TYPE)
698
            expr1 = cp_build_unary_op (ADDR_EXPR, expr1, 0, complain);
699
 
700
          elems[0] = expr1;
701
          elems[1] = td3;
702
          elems[2] = td2;
703
          elems[3] = boff;
704
 
705
          dcast_fn = dynamic_cast_node;
706
          if (!dcast_fn)
707
            {
708
              tree tmp;
709
              tree tinfo_ptr;
710
              const char *name;
711
 
712
              push_abi_namespace ();
713
              tinfo_ptr = xref_tag (class_type,
714
                                    get_identifier ("__class_type_info"),
715
                                    /*tag_scope=*/ts_current, false);
716
 
717
              tinfo_ptr = build_pointer_type
718
                (build_qualified_type
719
                 (tinfo_ptr, TYPE_QUAL_CONST));
720
              name = "__dynamic_cast";
721
              tmp = tree_cons
722
                (NULL_TREE, const_ptr_type_node, tree_cons
723
                 (NULL_TREE, tinfo_ptr, tree_cons
724
                  (NULL_TREE, tinfo_ptr, tree_cons
725
                   (NULL_TREE, ptrdiff_type_node, void_list_node))));
726
              tmp = build_function_type (ptr_type_node, tmp);
727
              dcast_fn = build_library_fn_ptr (name, tmp);
728
              DECL_PURE_P (dcast_fn) = 1;
729
              pop_abi_namespace ();
730
              dynamic_cast_node = dcast_fn;
731
            }
732
          result = build_cxx_call (dcast_fn, 4, elems);
733
 
734
          if (tc == REFERENCE_TYPE)
735
            {
736
              tree bad = throw_bad_cast ();
737
              tree neq;
738
 
739
              result = save_expr (result);
740
              neq = c_common_truthvalue_conversion (input_location, result);
741
              return cp_convert (type,
742
                                 build3 (COND_EXPR, TREE_TYPE (result),
743
                                         neq, result, bad));
744
            }
745
 
746
          /* Now back to the type we want from a void*.  */
747
          result = cp_convert (type, result);
748
          return ifnonnull (expr, result);
749
        }
750
    }
751
  else
752
    errstr = _("source type is not polymorphic");
753
 
754
 fail:
755
  if (complain & tf_error)
756
    error ("cannot dynamic_cast %qE (of type %q#T) to type %q#T (%s)",
757
           expr, exprtype, type, errstr);
758
  return error_mark_node;
759
}
760
 
761
tree
762
build_dynamic_cast (tree type, tree expr, tsubst_flags_t complain)
763
{
764
  if (type == error_mark_node || expr == error_mark_node)
765
    return error_mark_node;
766
 
767
  if (processing_template_decl)
768
    {
769
      expr = build_min (DYNAMIC_CAST_EXPR, type, expr);
770
      TREE_SIDE_EFFECTS (expr) = 1;
771
      return convert_from_reference (expr);
772
    }
773
 
774
  return convert_from_reference (build_dynamic_cast_1 (type, expr, complain));
775
}
776
 
777
/* Return the runtime bit mask encoding the qualifiers of TYPE.  */
778
 
779
static int
780
qualifier_flags (tree type)
781
{
782
  int flags = 0;
783
  int quals = cp_type_quals (type);
784
 
785
  if (quals & TYPE_QUAL_CONST)
786
    flags |= 1;
787
  if (quals & TYPE_QUAL_VOLATILE)
788
    flags |= 2;
789
  if (quals & TYPE_QUAL_RESTRICT)
790
    flags |= 4;
791
  return flags;
792
}
793
 
794
/* Return true, if the pointer chain TYPE ends at an incomplete type, or
795
   contains a pointer to member of an incomplete class.  */
796
 
797
static bool
798
target_incomplete_p (tree type)
799
{
800
  while (true)
801
    if (TYPE_PTRMEM_P (type))
802
      {
803
        if (!COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type)))
804
          return true;
805
        type = TYPE_PTRMEM_POINTED_TO_TYPE (type);
806
      }
807
    else if (TREE_CODE (type) == POINTER_TYPE)
808
      type = TREE_TYPE (type);
809
    else
810
      return !COMPLETE_OR_VOID_TYPE_P (type);
811
}
812
 
813
/* Returns true if TYPE involves an incomplete class type; in that
814
   case, typeinfo variables for TYPE should be emitted with internal
815
   linkage.  */
816
 
817
static bool
818
involves_incomplete_p (tree type)
819
{
820
  switch (TREE_CODE (type))
821
    {
822
    case POINTER_TYPE:
823
      return target_incomplete_p (TREE_TYPE (type));
824
 
825
    case OFFSET_TYPE:
826
    ptrmem:
827
      return
828
        (target_incomplete_p (TYPE_PTRMEM_POINTED_TO_TYPE (type))
829
         || !COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type)));
830
 
831
    case RECORD_TYPE:
832
      if (TYPE_PTRMEMFUNC_P (type))
833
        goto ptrmem;
834
      /* Fall through.  */
835
    case UNION_TYPE:
836
      if (!COMPLETE_TYPE_P (type))
837
        return true;
838
 
839
    default:
840
      /* All other types do not involve incomplete class types.  */
841
      return false;
842
    }
843
}
844
 
845
/* Return a CONSTRUCTOR for the common part of the type_info objects. This
846
   is the vtable pointer and NTBS name.  The NTBS name is emitted as a
847
   comdat const char array, so it becomes a unique key for the type. Generate
848
   and emit that VAR_DECL here.  (We can't always emit the type_info itself
849
   as comdat, because of pointers to incomplete.) */
850
 
851
static tree
852
tinfo_base_init (tinfo_s *ti, tree target)
853
{
854
  tree init = NULL_TREE;
855
  tree name_decl;
856
  tree vtable_ptr;
857
 
858
  {
859
    tree name_name, name_string;
860
 
861
    /* Generate the NTBS array variable.  */
862
    tree name_type = build_cplus_array_type
863
                     (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
864
                     NULL_TREE);
865
 
866
    /* Determine the name of the variable -- and remember with which
867
       type it is associated.  */
868
    name_name = mangle_typeinfo_string_for_type (target);
869
    TREE_TYPE (name_name) = target;
870
 
871
    name_decl = build_lang_decl (VAR_DECL, name_name, name_type);
872
    SET_DECL_ASSEMBLER_NAME (name_decl, name_name);
873
    DECL_ARTIFICIAL (name_decl) = 1;
874
    DECL_IGNORED_P (name_decl) = 1;
875
    TREE_READONLY (name_decl) = 1;
876
    TREE_STATIC (name_decl) = 1;
877
    DECL_EXTERNAL (name_decl) = 0;
878
    DECL_TINFO_P (name_decl) = 1;
879
    set_linkage_according_to_type (target, name_decl);
880
    import_export_decl (name_decl);
881
    name_string = tinfo_name (target, !TREE_PUBLIC (name_decl));
882
    DECL_INITIAL (name_decl) = name_string;
883
    mark_used (name_decl);
884
    pushdecl_top_level_and_finish (name_decl, name_string);
885
  }
886
 
887
  vtable_ptr = ti->vtable;
888
  if (!vtable_ptr)
889
    {
890
      tree real_type;
891
      push_abi_namespace ();
892
      real_type = xref_tag (class_type, ti->name,
893
                            /*tag_scope=*/ts_current, false);
894
      pop_abi_namespace ();
895
 
896
      if (!COMPLETE_TYPE_P (real_type))
897
        {
898
          /* We never saw a definition of this type, so we need to
899
             tell the compiler that this is an exported class, as
900
             indeed all of the __*_type_info classes are.  */
901
          SET_CLASSTYPE_INTERFACE_KNOWN (real_type);
902
          CLASSTYPE_INTERFACE_ONLY (real_type) = 1;
903
        }
904
 
905
      vtable_ptr = get_vtable_decl (real_type, /*complete=*/1);
906
      vtable_ptr = cp_build_unary_op (ADDR_EXPR, vtable_ptr, 0,
907
                                   tf_warning_or_error);
908
 
909
      /* We need to point into the middle of the vtable.  */
910
      vtable_ptr = build2
911
        (POINTER_PLUS_EXPR, TREE_TYPE (vtable_ptr), vtable_ptr,
912
         size_binop (MULT_EXPR,
913
                     size_int (2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE),
914
                     TYPE_SIZE_UNIT (vtable_entry_type)));
915
 
916
      ti->vtable = vtable_ptr;
917
    }
918
 
919
  init = tree_cons (NULL_TREE, vtable_ptr, init);
920
 
921
  init = tree_cons (NULL_TREE, decay_conversion (name_decl), init);
922
 
923
  init = build_constructor_from_list (init_list_type_node, nreverse (init));
924
  TREE_CONSTANT (init) = 1;
925
  TREE_STATIC (init) = 1;
926
  init = tree_cons (NULL_TREE, init, NULL_TREE);
927
 
928
  return init;
929
}
930
 
931
/* Return the CONSTRUCTOR expr for a type_info of TYPE. TI provides the
932
   information about the particular type_info derivation, which adds no
933
   additional fields to the type_info base.  */
934
 
935
static tree
936
generic_initializer (tinfo_s *ti, tree target)
937
{
938
  tree init = tinfo_base_init (ti, target);
939
 
940
  init = build_constructor_from_list (init_list_type_node, init);
941
  TREE_CONSTANT (init) = 1;
942
  TREE_STATIC (init) = 1;
943
  return init;
944
}
945
 
946
/* Return the CONSTRUCTOR expr for a type_info of pointer TYPE.
947
   TI provides information about the particular type_info derivation,
948
   which adds target type and qualifier flags members to the type_info base.  */
949
 
950
static tree
951
ptr_initializer (tinfo_s *ti, tree target)
952
{
953
  tree init = tinfo_base_init (ti, target);
954
  tree to = TREE_TYPE (target);
955
  int flags = qualifier_flags (to);
956
  bool incomplete = target_incomplete_p (to);
957
 
958
  if (incomplete)
959
    flags |= 8;
960
  init = tree_cons (NULL_TREE, build_int_cst (NULL_TREE, flags), init);
961
  init = tree_cons (NULL_TREE,
962
                    get_tinfo_ptr (TYPE_MAIN_VARIANT (to)),
963
                    init);
964
 
965
  init = build_constructor_from_list (init_list_type_node, nreverse (init));
966
  TREE_CONSTANT (init) = 1;
967
  TREE_STATIC (init) = 1;
968
  return init;
969
}
970
 
971
/* Return the CONSTRUCTOR expr for a type_info of pointer to member data TYPE.
972
   TI provides information about the particular type_info derivation,
973
   which adds class, target type and qualifier flags members to the type_info
974
   base.  */
975
 
976
static tree
977
ptm_initializer (tinfo_s *ti, tree target)
978
{
979
  tree init = tinfo_base_init (ti, target);
980
  tree to = TYPE_PTRMEM_POINTED_TO_TYPE (target);
981
  tree klass = TYPE_PTRMEM_CLASS_TYPE (target);
982
  int flags = qualifier_flags (to);
983
  bool incomplete = target_incomplete_p (to);
984
 
985
  if (incomplete)
986
    flags |= 0x8;
987
  if (!COMPLETE_TYPE_P (klass))
988
    flags |= 0x10;
989
  init = tree_cons (NULL_TREE, build_int_cst (NULL_TREE, flags), init);
990
  init = tree_cons (NULL_TREE,
991
                    get_tinfo_ptr (TYPE_MAIN_VARIANT (to)),
992
                    init);
993
  init = tree_cons (NULL_TREE,
994
                    get_tinfo_ptr (klass),
995
                    init);
996
 
997
  init = build_constructor_from_list (init_list_type_node, nreverse (init));
998
  TREE_CONSTANT (init) = 1;
999
  TREE_STATIC (init) = 1;
1000
  return init;
1001
}
1002
 
1003
/* Return the CONSTRUCTOR expr for a type_info of class TYPE.
1004
   TI provides information about the particular __class_type_info derivation,
1005
   which adds hint flags and TRAIL initializers to the type_info base.  */
1006
 
1007
static tree
1008
class_initializer (tinfo_s *ti, tree target, tree trail)
1009
{
1010
  tree init = tinfo_base_init (ti, target);
1011
 
1012
  TREE_CHAIN (init) = trail;
1013
  init = build_constructor_from_list (init_list_type_node, init);
1014
  TREE_CONSTANT (init) = 1;
1015
  TREE_STATIC (init) = 1;
1016
  return init;
1017
}
1018
 
1019
/* Returns true if the typeinfo for type should be placed in
1020
   the runtime library.  */
1021
 
1022
static bool
1023
typeinfo_in_lib_p (tree type)
1024
{
1025
  /* The typeinfo objects for `T*' and `const T*' are in the runtime
1026
     library for simple types T.  */
1027
  if (TREE_CODE (type) == POINTER_TYPE
1028
      && (cp_type_quals (TREE_TYPE (type)) == TYPE_QUAL_CONST
1029
          || cp_type_quals (TREE_TYPE (type)) == TYPE_UNQUALIFIED))
1030
    type = TREE_TYPE (type);
1031
 
1032
  switch (TREE_CODE (type))
1033
    {
1034
    case INTEGER_TYPE:
1035
    case BOOLEAN_TYPE:
1036
    case REAL_TYPE:
1037
    case VOID_TYPE:
1038
      return true;
1039
 
1040
    default:
1041
      return false;
1042
    }
1043
}
1044
 
1045
/* Generate the initializer for the type info describing TYPE.  TK_INDEX is
1046
   the index of the descriptor in the tinfo_desc vector. */
1047
 
1048
static tree
1049
get_pseudo_ti_init (tree type, unsigned tk_index)
1050
{
1051
  tinfo_s *ti = VEC_index (tinfo_s, tinfo_descs, tk_index);
1052
 
1053
  gcc_assert (at_eof);
1054
  switch (tk_index)
1055
    {
1056
    case TK_POINTER_MEMBER_TYPE:
1057
      return ptm_initializer (ti, type);
1058
 
1059
    case TK_POINTER_TYPE:
1060
      return ptr_initializer (ti, type);
1061
 
1062
    case TK_BUILTIN_TYPE:
1063
    case TK_ENUMERAL_TYPE:
1064
    case TK_FUNCTION_TYPE:
1065
    case TK_ARRAY_TYPE:
1066
      return generic_initializer (ti, type);
1067
 
1068
    case TK_CLASS_TYPE:
1069
      return class_initializer (ti, type, NULL_TREE);
1070
 
1071
    case TK_SI_CLASS_TYPE:
1072
      {
1073
        tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), 0);
1074
        tree tinfo = get_tinfo_ptr (BINFO_TYPE (base_binfo));
1075
        tree base_inits = tree_cons (NULL_TREE, tinfo, NULL_TREE);
1076
 
1077
        /* get_tinfo_ptr might have reallocated the tinfo_descs vector.  */
1078
        ti = VEC_index (tinfo_s, tinfo_descs, tk_index);
1079
        return class_initializer (ti, type, base_inits);
1080
      }
1081
 
1082
    default:
1083
      {
1084
        int hint = ((CLASSTYPE_REPEATED_BASE_P (type) << 0)
1085
                    | (CLASSTYPE_DIAMOND_SHAPED_P (type) << 1));
1086
        tree binfo = TYPE_BINFO (type);
1087
        int nbases = BINFO_N_BASE_BINFOS (binfo);
1088
        VEC(tree,gc) *base_accesses = BINFO_BASE_ACCESSES (binfo);
1089
        tree offset_type = integer_types[itk_long];
1090
        tree base_inits = NULL_TREE;
1091
        int ix;
1092
 
1093
        gcc_assert (tk_index >= TK_FIXED);
1094
 
1095
        /* Generate the base information initializer.  */
1096
        for (ix = nbases; ix--;)
1097
          {
1098
            tree base_binfo = BINFO_BASE_BINFO (binfo, ix);
1099
            tree base_init = NULL_TREE;
1100
            int flags = 0;
1101
            tree tinfo;
1102
            tree offset;
1103
 
1104
            if (VEC_index (tree, base_accesses, ix) == access_public_node)
1105
              flags |= 2;
1106
            tinfo = get_tinfo_ptr (BINFO_TYPE (base_binfo));
1107
            if (BINFO_VIRTUAL_P (base_binfo))
1108
              {
1109
                /* We store the vtable offset at which the virtual
1110
                   base offset can be found.  */
1111
                offset = BINFO_VPTR_FIELD (base_binfo);
1112
                flags |= 1;
1113
              }
1114
            else
1115
              offset = BINFO_OFFSET (base_binfo);
1116
 
1117
            /* Combine offset and flags into one field.  */
1118
            offset = fold_convert (offset_type, offset);
1119
            offset = fold_build2_loc (input_location,
1120
                                  LSHIFT_EXPR, offset_type, offset,
1121
                                  build_int_cst (offset_type, 8));
1122
            offset = fold_build2_loc (input_location,
1123
                                  BIT_IOR_EXPR, offset_type, offset,
1124
                                  build_int_cst (offset_type, flags));
1125
            base_init = tree_cons (NULL_TREE, offset, base_init);
1126
            base_init = tree_cons (NULL_TREE, tinfo, base_init);
1127
            base_init = build_constructor_from_list (init_list_type_node, base_init);
1128
            base_inits = tree_cons (NULL_TREE, base_init, base_inits);
1129
          }
1130
        base_inits = build_constructor_from_list (init_list_type_node, base_inits);
1131
        base_inits = tree_cons (NULL_TREE, base_inits, NULL_TREE);
1132
        /* Prepend the number of bases.  */
1133
        base_inits = tree_cons (NULL_TREE,
1134
                                build_int_cst (NULL_TREE, nbases),
1135
                                base_inits);
1136
        /* Prepend the hint flags.  */
1137
        base_inits = tree_cons (NULL_TREE,
1138
                                build_int_cst (NULL_TREE, hint),
1139
                                base_inits);
1140
 
1141
        /* get_tinfo_ptr might have reallocated the tinfo_descs vector.  */
1142
        ti = VEC_index (tinfo_s, tinfo_descs, tk_index);
1143
        return class_initializer (ti, type, base_inits);
1144
      }
1145
    }
1146
}
1147
 
1148
/* Generate the RECORD_TYPE containing the data layout of a type_info
1149
   derivative as used by the runtime. This layout must be consistent with
1150
   that defined in the runtime support. Also generate the VAR_DECL for the
1151
   type's vtable. We explicitly manage the vtable member, and name it for
1152
   real type as used in the runtime. The RECORD type has a different name,
1153
   to avoid collisions.  Return a TREE_LIST who's TINFO_PSEUDO_TYPE
1154
   is the generated type and TINFO_VTABLE_NAME is the name of the
1155
   vtable.  We have to delay generating the VAR_DECL of the vtable
1156
   until the end of the translation, when we'll have seen the library
1157
   definition, if there was one.
1158
 
1159
   REAL_NAME is the runtime's name of the type. Trailing arguments are
1160
   additional FIELD_DECL's for the structure. The final argument must be
1161
   NULL.  */
1162
 
1163
static void
1164
create_pseudo_type_info (int tk, const char *real_name, ...)
1165
{
1166
  tinfo_s *ti;
1167
  tree pseudo_type;
1168
  char *pseudo_name;
1169
  tree fields;
1170
  tree field_decl;
1171
  va_list ap;
1172
 
1173
  va_start (ap, real_name);
1174
 
1175
  /* Generate the pseudo type name.  */
1176
  pseudo_name = (char *) alloca (strlen (real_name) + 30);
1177
  strcpy (pseudo_name, real_name);
1178
  strcat (pseudo_name, "_pseudo");
1179
  if (tk >= TK_FIXED)
1180
    sprintf (pseudo_name + strlen (pseudo_name), "%d", tk - TK_FIXED);
1181
 
1182
  /* First field is the pseudo type_info base class.  */
1183
  fields = build_decl (input_location,
1184
                       FIELD_DECL, NULL_TREE,
1185
                       VEC_index (tinfo_s, tinfo_descs,
1186
                                  TK_TYPE_INFO_TYPE)->type);
1187
 
1188
  /* Now add the derived fields.  */
1189
  while ((field_decl = va_arg (ap, tree)))
1190
    {
1191
      TREE_CHAIN (field_decl) = fields;
1192
      fields = field_decl;
1193
    }
1194
 
1195
  /* Create the pseudo type.  */
1196
  pseudo_type = make_class_type (RECORD_TYPE);
1197
  finish_builtin_struct (pseudo_type, pseudo_name, fields, NULL_TREE);
1198
  CLASSTYPE_AS_BASE (pseudo_type) = pseudo_type;
1199
 
1200
  ti = VEC_index (tinfo_s, tinfo_descs, tk);
1201
  ti->type = cp_build_qualified_type (pseudo_type, TYPE_QUAL_CONST);
1202
  ti->name = get_identifier (real_name);
1203
  ti->vtable = NULL_TREE;
1204
 
1205
  /* Pretend this is public so determine_visibility doesn't give vtables
1206
     internal linkage.  */
1207
  TREE_PUBLIC (TYPE_MAIN_DECL (ti->type)) = 1;
1208
 
1209
  va_end (ap);
1210
}
1211
 
1212
/* Return the index of a pseudo type info type node used to describe
1213
   TYPE.  TYPE must be a complete type (or cv void), except at the end
1214
   of the translation unit.  */
1215
 
1216
static unsigned
1217
get_pseudo_ti_index (tree type)
1218
{
1219
  unsigned ix;
1220
 
1221
  switch (TREE_CODE (type))
1222
    {
1223
    case OFFSET_TYPE:
1224
      ix = TK_POINTER_MEMBER_TYPE;
1225
      break;
1226
 
1227
    case POINTER_TYPE:
1228
      ix = TK_POINTER_TYPE;
1229
      break;
1230
 
1231
    case ENUMERAL_TYPE:
1232
      ix = TK_ENUMERAL_TYPE;
1233
      break;
1234
 
1235
    case FUNCTION_TYPE:
1236
      ix = TK_FUNCTION_TYPE;
1237
      break;
1238
 
1239
    case ARRAY_TYPE:
1240
      ix = TK_ARRAY_TYPE;
1241
      break;
1242
 
1243
    case UNION_TYPE:
1244
    case RECORD_TYPE:
1245
      if (TYPE_PTRMEMFUNC_P (type))
1246
        {
1247
          ix = TK_POINTER_MEMBER_TYPE;
1248
          break;
1249
        }
1250
      else if (!COMPLETE_TYPE_P (type))
1251
        {
1252
          if (!at_eof)
1253
            cxx_incomplete_type_error (NULL_TREE, type);
1254
          ix = TK_CLASS_TYPE;
1255
          break;
1256
        }
1257
      else if (!BINFO_N_BASE_BINFOS (TYPE_BINFO (type)))
1258
        {
1259
          ix = TK_CLASS_TYPE;
1260
          break;
1261
        }
1262
      else
1263
        {
1264
          tree binfo = TYPE_BINFO (type);
1265
          VEC(tree,gc) *base_accesses = BINFO_BASE_ACCESSES (binfo);
1266
          tree base_binfo = BINFO_BASE_BINFO (binfo, 0);
1267
          int num_bases = BINFO_N_BASE_BINFOS (binfo);
1268
 
1269
          if (num_bases == 1
1270
              && VEC_index (tree, base_accesses, 0) == access_public_node
1271
              && !BINFO_VIRTUAL_P (base_binfo)
1272
              && integer_zerop (BINFO_OFFSET (base_binfo)))
1273
            {
1274
              /* single non-virtual public.  */
1275
              ix = TK_SI_CLASS_TYPE;
1276
              break;
1277
            }
1278
          else
1279
            {
1280
              tinfo_s *ti;
1281
              tree array_domain, base_array;
1282
 
1283
              ix = TK_FIXED + num_bases;
1284
              if (VEC_length (tinfo_s, tinfo_descs) <= ix)
1285
                {
1286
                  /* too short, extend.  */
1287
                  unsigned len = VEC_length (tinfo_s, tinfo_descs);
1288
 
1289
                  VEC_safe_grow (tinfo_s, gc, tinfo_descs, ix + 1);
1290
                  while (VEC_iterate (tinfo_s, tinfo_descs, len++, ti))
1291
                    ti->type = ti->vtable = ti->name = NULL_TREE;
1292
                }
1293
              else if (VEC_index (tinfo_s, tinfo_descs, ix)->type)
1294
                /* already created.  */
1295
                break;
1296
 
1297
              /* Create the array of __base_class_type_info entries.
1298
                 G++ 3.2 allocated an array that had one too many
1299
                 entries, and then filled that extra entries with
1300
                 zeros.  */
1301
              if (abi_version_at_least (2))
1302
                array_domain = build_index_type (size_int (num_bases - 1));
1303
              else
1304
                array_domain = build_index_type (size_int (num_bases));
1305
              base_array =
1306
                build_array_type (VEC_index (tinfo_s, tinfo_descs,
1307
                                             TK_BASE_TYPE)->type,
1308
                                  array_domain);
1309
 
1310
              push_abi_namespace ();
1311
              create_pseudo_type_info
1312
                (ix, "__vmi_class_type_info",
1313
                 build_decl (input_location,
1314
                             FIELD_DECL, NULL_TREE, integer_type_node),
1315
                 build_decl (input_location,
1316
                             FIELD_DECL, NULL_TREE, integer_type_node),
1317
                 build_decl (input_location,
1318
                             FIELD_DECL, NULL_TREE, base_array),
1319
                 NULL);
1320
              pop_abi_namespace ();
1321
              break;
1322
            }
1323
        }
1324
    default:
1325
      ix = TK_BUILTIN_TYPE;
1326
      break;
1327
    }
1328
  return ix;
1329
}
1330
 
1331
/* Make sure the required builtin types exist for generating the type_info
1332
   variable definitions.  */
1333
 
1334
static void
1335
create_tinfo_types (void)
1336
{
1337
  tinfo_s *ti;
1338
 
1339
  gcc_assert (!tinfo_descs);
1340
 
1341
  VEC_safe_grow (tinfo_s, gc, tinfo_descs, TK_FIXED);
1342
 
1343
  push_abi_namespace ();
1344
 
1345
  /* Create the internal type_info structure. This is used as a base for
1346
     the other structures.  */
1347
  {
1348
    tree field, fields;
1349
 
1350
    field = build_decl (BUILTINS_LOCATION,
1351
                        FIELD_DECL, NULL_TREE, const_ptr_type_node);
1352
    fields = field;
1353
 
1354
    field = build_decl (BUILTINS_LOCATION,
1355
                        FIELD_DECL, NULL_TREE, const_string_type_node);
1356
    TREE_CHAIN (field) = fields;
1357
    fields = field;
1358
 
1359
    ti = VEC_index (tinfo_s, tinfo_descs, TK_TYPE_INFO_TYPE);
1360
    ti->type = make_class_type (RECORD_TYPE);
1361
    ti->vtable = NULL_TREE;
1362
    ti->name = NULL_TREE;
1363
    finish_builtin_struct (ti->type, "__type_info_pseudo",
1364
                           fields, NULL_TREE);
1365
  }
1366
 
1367
  /* Fundamental type_info */
1368
  create_pseudo_type_info (TK_BUILTIN_TYPE, "__fundamental_type_info", NULL);
1369
 
1370
  /* Array, function and enum type_info. No additional fields.  */
1371
  create_pseudo_type_info (TK_ARRAY_TYPE, "__array_type_info", NULL);
1372
  create_pseudo_type_info (TK_FUNCTION_TYPE, "__function_type_info", NULL);
1373
  create_pseudo_type_info (TK_ENUMERAL_TYPE, "__enum_type_info", NULL);
1374
 
1375
  /* Class type_info.  No additional fields.  */
1376
  create_pseudo_type_info (TK_CLASS_TYPE, "__class_type_info", NULL);
1377
 
1378
  /* Single public non-virtual base class. Add pointer to base class.
1379
     This is really a descendant of __class_type_info.  */
1380
  create_pseudo_type_info (TK_SI_CLASS_TYPE, "__si_class_type_info",
1381
            build_decl (BUILTINS_LOCATION,
1382
                        FIELD_DECL, NULL_TREE, type_info_ptr_type),
1383
            NULL);
1384
 
1385
  /* Base class internal helper. Pointer to base type, offset to base,
1386
     flags.  */
1387
  {
1388
    tree field, fields;
1389
 
1390
    field = build_decl (BUILTINS_LOCATION,
1391
                        FIELD_DECL, NULL_TREE, type_info_ptr_type);
1392
    fields = field;
1393
 
1394
    field = build_decl (BUILTINS_LOCATION,
1395
                        FIELD_DECL, NULL_TREE, integer_types[itk_long]);
1396
    TREE_CHAIN (field) = fields;
1397
    fields = field;
1398
 
1399
    ti = VEC_index (tinfo_s, tinfo_descs, TK_BASE_TYPE);
1400
 
1401
    ti->type = make_class_type (RECORD_TYPE);
1402
    ti->vtable = NULL_TREE;
1403
    ti->name = NULL_TREE;
1404
    finish_builtin_struct (ti->type, "__base_class_type_info_pseudo",
1405
                           fields, NULL_TREE);
1406
  }
1407
 
1408
  /* Pointer type_info. Adds two fields, qualification mask
1409
     and pointer to the pointed to type.  This is really a descendant of
1410
     __pbase_type_info.  */
1411
  create_pseudo_type_info (TK_POINTER_TYPE, "__pointer_type_info",
1412
       build_decl (BUILTINS_LOCATION,
1413
                   FIELD_DECL, NULL_TREE, integer_type_node),
1414
       build_decl (BUILTINS_LOCATION,
1415
                   FIELD_DECL, NULL_TREE, type_info_ptr_type),
1416
       NULL);
1417
 
1418
  /* Pointer to member data type_info.  Add qualifications flags,
1419
     pointer to the member's type info and pointer to the class.
1420
     This is really a descendant of __pbase_type_info.  */
1421
  create_pseudo_type_info (TK_POINTER_MEMBER_TYPE,
1422
       "__pointer_to_member_type_info",
1423
        build_decl (BUILTINS_LOCATION,
1424
                    FIELD_DECL, NULL_TREE, integer_type_node),
1425
        build_decl (BUILTINS_LOCATION,
1426
                    FIELD_DECL, NULL_TREE, type_info_ptr_type),
1427
        build_decl (BUILTINS_LOCATION,
1428
                    FIELD_DECL, NULL_TREE, type_info_ptr_type),
1429
        NULL);
1430
 
1431
  pop_abi_namespace ();
1432
}
1433
 
1434
/* Emit the type_info descriptors which are guaranteed to be in the runtime
1435
   support.  Generating them here guarantees consistency with the other
1436
   structures.  We use the following heuristic to determine when the runtime
1437
   is being generated.  If std::__fundamental_type_info is defined, and its
1438
   destructor is defined, then the runtime is being built.  */
1439
 
1440
void
1441
emit_support_tinfos (void)
1442
{
1443
  static tree *const fundamentals[] =
1444
  {
1445
    &void_type_node,
1446
    &boolean_type_node,
1447
    &wchar_type_node, &char16_type_node, &char32_type_node,
1448
    &char_type_node, &signed_char_type_node, &unsigned_char_type_node,
1449
    &short_integer_type_node, &short_unsigned_type_node,
1450
    &integer_type_node, &unsigned_type_node,
1451
    &long_integer_type_node, &long_unsigned_type_node,
1452
    &long_long_integer_type_node, &long_long_unsigned_type_node,
1453
    &float_type_node, &double_type_node, &long_double_type_node,
1454
    &dfloat32_type_node, &dfloat64_type_node, &dfloat128_type_node,
1455
 
1456
  };
1457
  int ix;
1458
  tree bltn_type, dtor;
1459
 
1460
  push_abi_namespace ();
1461
  bltn_type = xref_tag (class_type,
1462
                        get_identifier ("__fundamental_type_info"),
1463
                        /*tag_scope=*/ts_current, false);
1464
  pop_abi_namespace ();
1465
  if (!COMPLETE_TYPE_P (bltn_type))
1466
    return;
1467
  dtor = CLASSTYPE_DESTRUCTORS (bltn_type);
1468
  if (!dtor || DECL_EXTERNAL (dtor))
1469
    return;
1470
  doing_runtime = 1;
1471
  for (ix = 0; fundamentals[ix]; ix++)
1472
    {
1473
      tree bltn = *fundamentals[ix];
1474
      tree types[3];
1475
      int i;
1476
 
1477
      types[0] = bltn;
1478
      types[1] = build_pointer_type (bltn);
1479
      types[2] = build_pointer_type (build_qualified_type (bltn,
1480
                                                           TYPE_QUAL_CONST));
1481
 
1482
      for (i = 0; i < 3; ++i)
1483
        {
1484
          tree tinfo;
1485
 
1486
          tinfo = get_tinfo_decl (types[i]);
1487
          TREE_USED (tinfo) = 1;
1488
          mark_needed (tinfo);
1489
          /* The C++ ABI requires that these objects be COMDAT.  But,
1490
             On systems without weak symbols, initialized COMDAT
1491
             objects are emitted with internal linkage.  (See
1492
             comdat_linkage for details.)  Since we want these objects
1493
             to have external linkage so that copies do not have to be
1494
             emitted in code outside the runtime library, we make them
1495
             non-COMDAT here.
1496
 
1497
             It might also not be necessary to follow this detail of the
1498
             ABI.  */
1499
          if (!flag_weak || ! targetm.cxx.library_rtti_comdat ())
1500
            {
1501
              gcc_assert (TREE_PUBLIC (tinfo) && !DECL_COMDAT (tinfo));
1502
              DECL_INTERFACE_KNOWN (tinfo) = 1;
1503
            }
1504
        }
1505
    }
1506
}
1507
 
1508
/* Finish a type info decl. DECL_PTR is a pointer to an unemitted
1509
   tinfo decl.  Determine whether it needs emitting, and if so
1510
   generate the initializer.  */
1511
 
1512
bool
1513
emit_tinfo_decl (tree decl)
1514
{
1515
  tree type = TREE_TYPE (DECL_NAME (decl));
1516
  int in_library = typeinfo_in_lib_p (type);
1517
 
1518
  gcc_assert (DECL_TINFO_P (decl));
1519
 
1520
  if (in_library)
1521
    {
1522
      if (doing_runtime)
1523
        DECL_EXTERNAL (decl) = 0;
1524
      else
1525
        {
1526
          /* If we're not in the runtime, then DECL (which is already
1527
             DECL_EXTERNAL) will not be defined here.  */
1528
          DECL_INTERFACE_KNOWN (decl) = 1;
1529
          return false;
1530
        }
1531
    }
1532
  else if (involves_incomplete_p (type))
1533
    {
1534
      if (!decl_needed_p (decl))
1535
        return false;
1536
      /* If TYPE involves an incomplete class type, then the typeinfo
1537
         object will be emitted with internal linkage.  There is no
1538
         way to know whether or not types are incomplete until the end
1539
         of the compilation, so this determination must be deferred
1540
         until this point.  */
1541
      TREE_PUBLIC (decl) = 0;
1542
      DECL_EXTERNAL (decl) = 0;
1543
      DECL_INTERFACE_KNOWN (decl) = 1;
1544
    }
1545
 
1546
  import_export_decl (decl);
1547
  if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
1548
    {
1549
      tree init;
1550
 
1551
      DECL_EXTERNAL (decl) = 0;
1552
      init = get_pseudo_ti_init (type, get_pseudo_ti_index (type));
1553
      DECL_INITIAL (decl) = init;
1554
      mark_used (decl);
1555
      cp_finish_decl (decl, init, false, NULL_TREE, 0);
1556
      return true;
1557
    }
1558
  else
1559
    return false;
1560
}
1561
 
1562
#include "gt-cp-rtti.h"

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.