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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [cp/] [class.c] - Blame information for rev 753

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

Line No. Rev Author Line
1 710 jeremybenn
/* Functions related to building classes and their related objects.
2
   Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3
   1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
4
   Free Software Foundation, Inc.
5
   Contributed by Michael Tiemann (tiemann@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
 
24
/* High-level class interface.  */
25
 
26
#include "config.h"
27
#include "system.h"
28
#include "coretypes.h"
29
#include "tm.h"
30
#include "tree.h"
31
#include "cp-tree.h"
32
#include "flags.h"
33
#include "output.h"
34
#include "toplev.h"
35
#include "target.h"
36
#include "convert.h"
37
#include "cgraph.h"
38
#include "tree-dump.h"
39
#include "splay-tree.h"
40
#include "pointer-set.h"
41
 
42
/* The number of nested classes being processed.  If we are not in the
43
   scope of any class, this is zero.  */
44
 
45
int current_class_depth;
46
 
47
/* In order to deal with nested classes, we keep a stack of classes.
48
   The topmost entry is the innermost class, and is the entry at index
49
   CURRENT_CLASS_DEPTH  */
50
 
51
typedef struct class_stack_node {
52
  /* The name of the class.  */
53
  tree name;
54
 
55
  /* The _TYPE node for the class.  */
56
  tree type;
57
 
58
  /* The access specifier pending for new declarations in the scope of
59
     this class.  */
60
  tree access;
61
 
62
  /* If were defining TYPE, the names used in this class.  */
63
  splay_tree names_used;
64
 
65
  /* Nonzero if this class is no longer open, because of a call to
66
     push_to_top_level.  */
67
  size_t hidden;
68
}* class_stack_node_t;
69
 
70
typedef struct vtbl_init_data_s
71
{
72
  /* The base for which we're building initializers.  */
73
  tree binfo;
74
  /* The type of the most-derived type.  */
75
  tree derived;
76
  /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
77
     unless ctor_vtbl_p is true.  */
78
  tree rtti_binfo;
79
  /* The negative-index vtable initializers built up so far.  These
80
     are in order from least negative index to most negative index.  */
81
  VEC(constructor_elt,gc) *inits;
82
  /* The binfo for the virtual base for which we're building
83
     vcall offset initializers.  */
84
  tree vbase;
85
  /* The functions in vbase for which we have already provided vcall
86
     offsets.  */
87
  VEC(tree,gc) *fns;
88
  /* The vtable index of the next vcall or vbase offset.  */
89
  tree index;
90
  /* Nonzero if we are building the initializer for the primary
91
     vtable.  */
92
  int primary_vtbl_p;
93
  /* Nonzero if we are building the initializer for a construction
94
     vtable.  */
95
  int ctor_vtbl_p;
96
  /* True when adding vcall offset entries to the vtable.  False when
97
     merely computing the indices.  */
98
  bool generate_vcall_entries;
99
} vtbl_init_data;
100
 
101
/* The type of a function passed to walk_subobject_offsets.  */
102
typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
103
 
104
/* The stack itself.  This is a dynamically resized array.  The
105
   number of elements allocated is CURRENT_CLASS_STACK_SIZE.  */
106
static int current_class_stack_size;
107
static class_stack_node_t current_class_stack;
108
 
109
/* The size of the largest empty class seen in this translation unit.  */
110
static GTY (()) tree sizeof_biggest_empty_class;
111
 
112
/* An array of all local classes present in this translation unit, in
113
   declaration order.  */
114
VEC(tree,gc) *local_classes;
115
 
116
static tree get_vfield_name (tree);
117
static void finish_struct_anon (tree);
118
static tree get_vtable_name (tree);
119
static tree get_basefndecls (tree, tree);
120
static int build_primary_vtable (tree, tree);
121
static int build_secondary_vtable (tree);
122
static void finish_vtbls (tree);
123
static void modify_vtable_entry (tree, tree, tree, tree, tree *);
124
static void finish_struct_bits (tree);
125
static int alter_access (tree, tree, tree);
126
static void handle_using_decl (tree, tree);
127
static tree dfs_modify_vtables (tree, void *);
128
static tree modify_all_vtables (tree, tree);
129
static void determine_primary_bases (tree);
130
static void finish_struct_methods (tree);
131
static void maybe_warn_about_overly_private_class (tree);
132
static int method_name_cmp (const void *, const void *);
133
static int resort_method_name_cmp (const void *, const void *);
134
static void add_implicitly_declared_members (tree, int, int);
135
static tree fixed_type_or_null (tree, int *, int *);
136
static tree build_simple_base_path (tree expr, tree binfo);
137
static tree build_vtbl_ref_1 (tree, tree);
138
static void build_vtbl_initializer (tree, tree, tree, tree, int *,
139
                                    VEC(constructor_elt,gc) **);
140
static int count_fields (tree);
141
static int add_fields_to_record_type (tree, struct sorted_fields_type*, int);
142
static bool check_bitfield_decl (tree);
143
static void check_field_decl (tree, tree, int *, int *, int *);
144
static void check_field_decls (tree, tree *, int *, int *);
145
static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
146
static void build_base_fields (record_layout_info, splay_tree, tree *);
147
static void check_methods (tree);
148
static void remove_zero_width_bit_fields (tree);
149
static void check_bases (tree, int *, int *);
150
static void check_bases_and_members (tree);
151
static tree create_vtable_ptr (tree, tree *);
152
static void include_empty_classes (record_layout_info);
153
static void layout_class_type (tree, tree *);
154
static void propagate_binfo_offsets (tree, tree);
155
static void layout_virtual_bases (record_layout_info, splay_tree);
156
static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
157
static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
158
static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
159
static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
160
static void add_vcall_offset (tree, tree, vtbl_init_data *);
161
static void layout_vtable_decl (tree, int);
162
static tree dfs_find_final_overrider_pre (tree, void *);
163
static tree dfs_find_final_overrider_post (tree, void *);
164
static tree find_final_overrider (tree, tree, tree);
165
static int make_new_vtable (tree, tree);
166
static tree get_primary_binfo (tree);
167
static int maybe_indent_hierarchy (FILE *, int, int);
168
static tree dump_class_hierarchy_r (FILE *, int, tree, tree, int);
169
static void dump_class_hierarchy (tree);
170
static void dump_class_hierarchy_1 (FILE *, int, tree);
171
static void dump_array (FILE *, tree);
172
static void dump_vtable (tree, tree, tree);
173
static void dump_vtt (tree, tree);
174
static void dump_thunk (FILE *, int, tree);
175
static tree build_vtable (tree, tree, tree);
176
static void initialize_vtable (tree, VEC(constructor_elt,gc) *);
177
static void layout_nonempty_base_or_field (record_layout_info,
178
                                           tree, tree, splay_tree);
179
static tree end_of_class (tree, int);
180
static bool layout_empty_base (record_layout_info, tree, tree, splay_tree);
181
static void accumulate_vtbl_inits (tree, tree, tree, tree, tree,
182
                                   VEC(constructor_elt,gc) **);
183
static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
184
                                       VEC(constructor_elt,gc) **);
185
static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
186
static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
187
static void clone_constructors_and_destructors (tree);
188
static tree build_clone (tree, tree);
189
static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
190
static void build_ctor_vtbl_group (tree, tree);
191
static void build_vtt (tree);
192
static tree binfo_ctor_vtable (tree);
193
static void build_vtt_inits (tree, tree, VEC(constructor_elt,gc) **, tree *);
194
static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
195
static tree dfs_fixup_binfo_vtbls (tree, void *);
196
static int record_subobject_offset (tree, tree, splay_tree);
197
static int check_subobject_offset (tree, tree, splay_tree);
198
static int walk_subobject_offsets (tree, subobject_offset_fn,
199
                                   tree, splay_tree, tree, int);
200
static void record_subobject_offsets (tree, tree, splay_tree, bool);
201
static int layout_conflict_p (tree, tree, splay_tree, int);
202
static int splay_tree_compare_integer_csts (splay_tree_key k1,
203
                                            splay_tree_key k2);
204
static void warn_about_ambiguous_bases (tree);
205
static bool type_requires_array_cookie (tree);
206
static bool contains_empty_class_p (tree);
207
static bool base_derived_from (tree, tree);
208
static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
209
static tree end_of_base (tree);
210
static tree get_vcall_index (tree, tree);
211
 
212
/* Variables shared between class.c and call.c.  */
213
 
214
#ifdef GATHER_STATISTICS
215
int n_vtables = 0;
216
int n_vtable_entries = 0;
217
int n_vtable_searches = 0;
218
int n_vtable_elems = 0;
219
int n_convert_harshness = 0;
220
int n_compute_conversion_costs = 0;
221
int n_inner_fields_searched = 0;
222
#endif
223
 
224
/* Convert to or from a base subobject.  EXPR is an expression of type
225
   `A' or `A*', an expression of type `B' or `B*' is returned.  To
226
   convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
227
   the B base instance within A.  To convert base A to derived B, CODE
228
   is MINUS_EXPR and BINFO is the binfo for the A instance within B.
229
   In this latter case, A must not be a morally virtual base of B.
230
   NONNULL is true if EXPR is known to be non-NULL (this is only
231
   needed when EXPR is of pointer type).  CV qualifiers are preserved
232
   from EXPR.  */
233
 
234
tree
235
build_base_path (enum tree_code code,
236
                 tree expr,
237
                 tree binfo,
238
                 int nonnull,
239
                 tsubst_flags_t complain)
240
{
241
  tree v_binfo = NULL_TREE;
242
  tree d_binfo = NULL_TREE;
243
  tree probe;
244
  tree offset;
245
  tree target_type;
246
  tree null_test = NULL;
247
  tree ptr_target_type;
248
  int fixed_type_p;
249
  int want_pointer = TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE;
250
  bool has_empty = false;
251
  bool virtual_access;
252
 
253
  if (expr == error_mark_node || binfo == error_mark_node || !binfo)
254
    return error_mark_node;
255
 
256
  for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
257
    {
258
      d_binfo = probe;
259
      if (is_empty_class (BINFO_TYPE (probe)))
260
        has_empty = true;
261
      if (!v_binfo && BINFO_VIRTUAL_P (probe))
262
        v_binfo = probe;
263
    }
264
 
265
  probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
266
  if (want_pointer)
267
    probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
268
 
269
  if (code == PLUS_EXPR
270
      && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
271
    {
272
      /* This can happen when adjust_result_of_qualified_name_lookup can't
273
         find a unique base binfo in a call to a member function.  We
274
         couldn't give the diagnostic then since we might have been calling
275
         a static member function, so we do it now.  */
276
      if (complain & tf_error)
277
        {
278
          tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
279
                                   ba_unique, NULL);
280
          gcc_assert (base == error_mark_node);
281
        }
282
      return error_mark_node;
283
    }
284
 
285
  gcc_assert ((code == MINUS_EXPR
286
               && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
287
              || code == PLUS_EXPR);
288
 
289
  if (binfo == d_binfo)
290
    /* Nothing to do.  */
291
    return expr;
292
 
293
  if (code == MINUS_EXPR && v_binfo)
294
    {
295
      if (complain & tf_error)
296
        error ("cannot convert from base %qT to derived type %qT via "
297
               "virtual base %qT", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
298
               BINFO_TYPE (v_binfo));
299
      return error_mark_node;
300
    }
301
 
302
  if (!want_pointer)
303
    /* This must happen before the call to save_expr.  */
304
    expr = cp_build_addr_expr (expr, complain);
305
  else
306
    expr = mark_rvalue_use (expr);
307
 
308
  offset = BINFO_OFFSET (binfo);
309
  fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
310
  target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
311
  /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
312
     cv-unqualified.  Extract the cv-qualifiers from EXPR so that the
313
     expression returned matches the input.  */
314
  target_type = cp_build_qualified_type
315
    (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
316
  ptr_target_type = build_pointer_type (target_type);
317
 
318
  /* Do we need to look in the vtable for the real offset?  */
319
  virtual_access = (v_binfo && fixed_type_p <= 0);
320
 
321
  /* Don't bother with the calculations inside sizeof; they'll ICE if the
322
     source type is incomplete and the pointer value doesn't matter.  In a
323
     template (even in fold_non_dependent_expr), we don't have vtables set
324
     up properly yet, and the value doesn't matter there either; we're just
325
     interested in the result of overload resolution.  */
326
  if (cp_unevaluated_operand != 0
327
      || (current_function_decl
328
          && uses_template_parms (current_function_decl)))
329
    {
330
      expr = build_nop (ptr_target_type, expr);
331
      if (!want_pointer)
332
        expr = build_indirect_ref (EXPR_LOCATION (expr), expr, RO_NULL);
333
      return expr;
334
    }
335
 
336
  /* If we're in an NSDMI, we don't have the full constructor context yet
337
     that we need for converting to a virtual base, so just build a stub
338
     CONVERT_EXPR and expand it later in bot_replace.  */
339
  if (virtual_access && fixed_type_p < 0
340
      && current_scope () != current_function_decl)
341
    {
342
      expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
343
      CONVERT_EXPR_VBASE_PATH (expr) = true;
344
      if (!want_pointer)
345
        expr = build_indirect_ref (EXPR_LOCATION (expr), expr, RO_NULL);
346
      return expr;
347
    }
348
 
349
  /* Do we need to check for a null pointer?  */
350
  if (want_pointer && !nonnull)
351
    {
352
      /* If we know the conversion will not actually change the value
353
         of EXPR, then we can avoid testing the expression for NULL.
354
         We have to avoid generating a COMPONENT_REF for a base class
355
         field, because other parts of the compiler know that such
356
         expressions are always non-NULL.  */
357
      if (!virtual_access && integer_zerop (offset))
358
        return build_nop (ptr_target_type, expr);
359
      null_test = error_mark_node;
360
    }
361
 
362
  /* Protect against multiple evaluation if necessary.  */
363
  if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
364
    expr = save_expr (expr);
365
 
366
  /* Now that we've saved expr, build the real null test.  */
367
  if (null_test)
368
    {
369
      tree zero = cp_convert (TREE_TYPE (expr), nullptr_node);
370
      null_test = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
371
                               expr, zero);
372
    }
373
 
374
  /* If this is a simple base reference, express it as a COMPONENT_REF.  */
375
  if (code == PLUS_EXPR && !virtual_access
376
      /* We don't build base fields for empty bases, and they aren't very
377
         interesting to the optimizers anyway.  */
378
      && !has_empty)
379
    {
380
      expr = cp_build_indirect_ref (expr, RO_NULL, complain);
381
      expr = build_simple_base_path (expr, binfo);
382
      if (want_pointer)
383
        expr = build_address (expr);
384
      target_type = TREE_TYPE (expr);
385
      goto out;
386
    }
387
 
388
  if (virtual_access)
389
    {
390
      /* Going via virtual base V_BINFO.  We need the static offset
391
         from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
392
         V_BINFO.  That offset is an entry in D_BINFO's vtable.  */
393
      tree v_offset;
394
 
395
      if (fixed_type_p < 0 && in_base_initializer)
396
        {
397
          /* In a base member initializer, we cannot rely on the
398
             vtable being set up.  We have to indirect via the
399
             vtt_parm.  */
400
          tree t;
401
 
402
          t = TREE_TYPE (TYPE_VFIELD (current_class_type));
403
          t = build_pointer_type (t);
404
          v_offset = convert (t, current_vtt_parm);
405
          v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
406
        }
407
      else
408
        v_offset = build_vfield_ref (cp_build_indirect_ref (expr, RO_NULL,
409
                                                            complain),
410
                                     TREE_TYPE (TREE_TYPE (expr)));
411
 
412
      v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
413
      v_offset = build1 (NOP_EXPR,
414
                         build_pointer_type (ptrdiff_type_node),
415
                         v_offset);
416
      v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
417
      TREE_CONSTANT (v_offset) = 1;
418
 
419
      offset = convert_to_integer (ptrdiff_type_node,
420
                                   size_diffop_loc (input_location, offset,
421
                                                BINFO_OFFSET (v_binfo)));
422
 
423
      if (!integer_zerop (offset))
424
        v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
425
 
426
      if (fixed_type_p < 0)
427
        /* Negative fixed_type_p means this is a constructor or destructor;
428
           virtual base layout is fixed in in-charge [cd]tors, but not in
429
           base [cd]tors.  */
430
        offset = build3 (COND_EXPR, ptrdiff_type_node,
431
                         build2 (EQ_EXPR, boolean_type_node,
432
                                 current_in_charge_parm, integer_zero_node),
433
                         v_offset,
434
                         convert_to_integer (ptrdiff_type_node,
435
                                             BINFO_OFFSET (binfo)));
436
      else
437
        offset = v_offset;
438
    }
439
 
440
  if (want_pointer)
441
    target_type = ptr_target_type;
442
 
443
  expr = build1 (NOP_EXPR, ptr_target_type, expr);
444
 
445
  if (!integer_zerop (offset))
446
    {
447
      offset = fold_convert (sizetype, offset);
448
      if (code == MINUS_EXPR)
449
        offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
450
      expr = fold_build_pointer_plus (expr, offset);
451
    }
452
  else
453
    null_test = NULL;
454
 
455
  if (!want_pointer)
456
    expr = cp_build_indirect_ref (expr, RO_NULL, complain);
457
 
458
 out:
459
  if (null_test)
460
    expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
461
                            build_zero_cst (target_type));
462
 
463
  return expr;
464
}
465
 
466
/* Subroutine of build_base_path; EXPR and BINFO are as in that function.
467
   Perform a derived-to-base conversion by recursively building up a
468
   sequence of COMPONENT_REFs to the appropriate base fields.  */
469
 
470
static tree
471
build_simple_base_path (tree expr, tree binfo)
472
{
473
  tree type = BINFO_TYPE (binfo);
474
  tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
475
  tree field;
476
 
477
  if (d_binfo == NULL_TREE)
478
    {
479
      tree temp;
480
 
481
      gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
482
 
483
      /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
484
         into `(*(a ?  &b : &c)).x', and so on.  A COND_EXPR is only
485
         an lvalue in the front end; only _DECLs and _REFs are lvalues
486
         in the back end.  */
487
      temp = unary_complex_lvalue (ADDR_EXPR, expr);
488
      if (temp)
489
        expr = cp_build_indirect_ref (temp, RO_NULL, tf_warning_or_error);
490
 
491
      return expr;
492
    }
493
 
494
  /* Recurse.  */
495
  expr = build_simple_base_path (expr, d_binfo);
496
 
497
  for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
498
       field; field = DECL_CHAIN (field))
499
    /* Is this the base field created by build_base_field?  */
500
    if (TREE_CODE (field) == FIELD_DECL
501
        && DECL_FIELD_IS_BASE (field)
502
        && TREE_TYPE (field) == type
503
        /* If we're looking for a field in the most-derived class,
504
           also check the field offset; we can have two base fields
505
           of the same type if one is an indirect virtual base and one
506
           is a direct non-virtual base.  */
507
        && (BINFO_INHERITANCE_CHAIN (d_binfo)
508
            || tree_int_cst_equal (byte_position (field),
509
                                   BINFO_OFFSET (binfo))))
510
      {
511
        /* We don't use build_class_member_access_expr here, as that
512
           has unnecessary checks, and more importantly results in
513
           recursive calls to dfs_walk_once.  */
514
        int type_quals = cp_type_quals (TREE_TYPE (expr));
515
 
516
        expr = build3 (COMPONENT_REF,
517
                       cp_build_qualified_type (type, type_quals),
518
                       expr, field, NULL_TREE);
519
        expr = fold_if_not_in_template (expr);
520
 
521
        /* Mark the expression const or volatile, as appropriate.
522
           Even though we've dealt with the type above, we still have
523
           to mark the expression itself.  */
524
        if (type_quals & TYPE_QUAL_CONST)
525
          TREE_READONLY (expr) = 1;
526
        if (type_quals & TYPE_QUAL_VOLATILE)
527
          TREE_THIS_VOLATILE (expr) = 1;
528
 
529
        return expr;
530
      }
531
 
532
  /* Didn't find the base field?!?  */
533
  gcc_unreachable ();
534
}
535
 
536
/* Convert OBJECT to the base TYPE.  OBJECT is an expression whose
537
   type is a class type or a pointer to a class type.  In the former
538
   case, TYPE is also a class type; in the latter it is another
539
   pointer type.  If CHECK_ACCESS is true, an error message is emitted
540
   if TYPE is inaccessible.  If OBJECT has pointer type, the value is
541
   assumed to be non-NULL.  */
542
 
543
tree
544
convert_to_base (tree object, tree type, bool check_access, bool nonnull,
545
                 tsubst_flags_t complain)
546
{
547
  tree binfo;
548
  tree object_type;
549
  base_access access;
550
 
551
  if (TYPE_PTR_P (TREE_TYPE (object)))
552
    {
553
      object_type = TREE_TYPE (TREE_TYPE (object));
554
      type = TREE_TYPE (type);
555
    }
556
  else
557
    object_type = TREE_TYPE (object);
558
 
559
  access = check_access ? ba_check : ba_unique;
560
  if (!(complain & tf_error))
561
    access |= ba_quiet;
562
  binfo = lookup_base (object_type, type,
563
                       access,
564
                       NULL);
565
  if (!binfo || binfo == error_mark_node)
566
    return error_mark_node;
567
 
568
  return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
569
}
570
 
571
/* EXPR is an expression with unqualified class type.  BASE is a base
572
   binfo of that class type.  Returns EXPR, converted to the BASE
573
   type.  This function assumes that EXPR is the most derived class;
574
   therefore virtual bases can be found at their static offsets.  */
575
 
576
tree
577
convert_to_base_statically (tree expr, tree base)
578
{
579
  tree expr_type;
580
 
581
  expr_type = TREE_TYPE (expr);
582
  if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
583
    {
584
      /* If this is a non-empty base, use a COMPONENT_REF.  */
585
      if (!is_empty_class (BINFO_TYPE (base)))
586
        return build_simple_base_path (expr, base);
587
 
588
      /* We use fold_build2 and fold_convert below to simplify the trees
589
         provided to the optimizers.  It is not safe to call these functions
590
         when processing a template because they do not handle C++-specific
591
         trees.  */
592
      gcc_assert (!processing_template_decl);
593
      expr = cp_build_addr_expr (expr, tf_warning_or_error);
594
      if (!integer_zerop (BINFO_OFFSET (base)))
595
        expr = fold_build_pointer_plus_loc (input_location,
596
                                            expr, BINFO_OFFSET (base));
597
      expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
598
      expr = build_fold_indirect_ref_loc (input_location, expr);
599
    }
600
 
601
  return expr;
602
}
603
 
604
 
605
tree
606
build_vfield_ref (tree datum, tree type)
607
{
608
  tree vfield, vcontext;
609
 
610
  if (datum == error_mark_node)
611
    return error_mark_node;
612
 
613
  /* First, convert to the requested type.  */
614
  if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
615
    datum = convert_to_base (datum, type, /*check_access=*/false,
616
                             /*nonnull=*/true, tf_warning_or_error);
617
 
618
  /* Second, the requested type may not be the owner of its own vptr.
619
     If not, convert to the base class that owns it.  We cannot use
620
     convert_to_base here, because VCONTEXT may appear more than once
621
     in the inheritance hierarchy of TYPE, and thus direct conversion
622
     between the types may be ambiguous.  Following the path back up
623
     one step at a time via primary bases avoids the problem.  */
624
  vfield = TYPE_VFIELD (type);
625
  vcontext = DECL_CONTEXT (vfield);
626
  while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
627
    {
628
      datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
629
      type = TREE_TYPE (datum);
630
    }
631
 
632
  return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
633
}
634
 
635
/* Given an object INSTANCE, return an expression which yields the
636
   vtable element corresponding to INDEX.  There are many special
637
   cases for INSTANCE which we take care of here, mainly to avoid
638
   creating extra tree nodes when we don't have to.  */
639
 
640
static tree
641
build_vtbl_ref_1 (tree instance, tree idx)
642
{
643
  tree aref;
644
  tree vtbl = NULL_TREE;
645
 
646
  /* Try to figure out what a reference refers to, and
647
     access its virtual function table directly.  */
648
 
649
  int cdtorp = 0;
650
  tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
651
 
652
  tree basetype = non_reference (TREE_TYPE (instance));
653
 
654
  if (fixed_type && !cdtorp)
655
    {
656
      tree binfo = lookup_base (fixed_type, basetype,
657
                                ba_unique | ba_quiet, NULL);
658
      if (binfo)
659
        vtbl = unshare_expr (BINFO_VTABLE (binfo));
660
    }
661
 
662
  if (!vtbl)
663
    vtbl = build_vfield_ref (instance, basetype);
664
 
665
  aref = build_array_ref (input_location, vtbl, idx);
666
  TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
667
 
668
  return aref;
669
}
670
 
671
tree
672
build_vtbl_ref (tree instance, tree idx)
673
{
674
  tree aref = build_vtbl_ref_1 (instance, idx);
675
 
676
  return aref;
677
}
678
 
679
/* Given a stable object pointer INSTANCE_PTR, return an expression which
680
   yields a function pointer corresponding to vtable element INDEX.  */
681
 
682
tree
683
build_vfn_ref (tree instance_ptr, tree idx)
684
{
685
  tree aref;
686
 
687
  aref = build_vtbl_ref_1 (cp_build_indirect_ref (instance_ptr, RO_NULL,
688
                                                  tf_warning_or_error),
689
                           idx);
690
 
691
  /* When using function descriptors, the address of the
692
     vtable entry is treated as a function pointer.  */
693
  if (TARGET_VTABLE_USES_DESCRIPTORS)
694
    aref = build1 (NOP_EXPR, TREE_TYPE (aref),
695
                   cp_build_addr_expr (aref, tf_warning_or_error));
696
 
697
  /* Remember this as a method reference, for later devirtualization.  */
698
  aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
699
 
700
  return aref;
701
}
702
 
703
/* Return the name of the virtual function table (as an IDENTIFIER_NODE)
704
   for the given TYPE.  */
705
 
706
static tree
707
get_vtable_name (tree type)
708
{
709
  return mangle_vtbl_for_type (type);
710
}
711
 
712
/* DECL is an entity associated with TYPE, like a virtual table or an
713
   implicitly generated constructor.  Determine whether or not DECL
714
   should have external or internal linkage at the object file
715
   level.  This routine does not deal with COMDAT linkage and other
716
   similar complexities; it simply sets TREE_PUBLIC if it possible for
717
   entities in other translation units to contain copies of DECL, in
718
   the abstract.  */
719
 
720
void
721
set_linkage_according_to_type (tree type ATTRIBUTE_UNUSED, tree decl)
722
{
723
  TREE_PUBLIC (decl) = 1;
724
  determine_visibility (decl);
725
}
726
 
727
/* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
728
   (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
729
   Use NAME for the name of the vtable, and VTABLE_TYPE for its type.  */
730
 
731
static tree
732
build_vtable (tree class_type, tree name, tree vtable_type)
733
{
734
  tree decl;
735
 
736
  decl = build_lang_decl (VAR_DECL, name, vtable_type);
737
  /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
738
     now to avoid confusion in mangle_decl.  */
739
  SET_DECL_ASSEMBLER_NAME (decl, name);
740
  DECL_CONTEXT (decl) = class_type;
741
  DECL_ARTIFICIAL (decl) = 1;
742
  TREE_STATIC (decl) = 1;
743
  TREE_READONLY (decl) = 1;
744
  DECL_VIRTUAL_P (decl) = 1;
745
  DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN;
746
  DECL_VTABLE_OR_VTT_P (decl) = 1;
747
  /* At one time the vtable info was grabbed 2 words at a time.  This
748
     fails on sparc unless you have 8-byte alignment.  (tiemann) */
749
  DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
750
                           DECL_ALIGN (decl));
751
  set_linkage_according_to_type (class_type, decl);
752
  /* The vtable has not been defined -- yet.  */
753
  DECL_EXTERNAL (decl) = 1;
754
  DECL_NOT_REALLY_EXTERN (decl) = 1;
755
 
756
  /* Mark the VAR_DECL node representing the vtable itself as a
757
     "gratuitous" one, thereby forcing dwarfout.c to ignore it.  It
758
     is rather important that such things be ignored because any
759
     effort to actually generate DWARF for them will run into
760
     trouble when/if we encounter code like:
761
 
762
     #pragma interface
763
     struct S { virtual void member (); };
764
 
765
     because the artificial declaration of the vtable itself (as
766
     manufactured by the g++ front end) will say that the vtable is
767
     a static member of `S' but only *after* the debug output for
768
     the definition of `S' has already been output.  This causes
769
     grief because the DWARF entry for the definition of the vtable
770
     will try to refer back to an earlier *declaration* of the
771
     vtable as a static member of `S' and there won't be one.  We
772
     might be able to arrange to have the "vtable static member"
773
     attached to the member list for `S' before the debug info for
774
     `S' get written (which would solve the problem) but that would
775
     require more intrusive changes to the g++ front end.  */
776
  DECL_IGNORED_P (decl) = 1;
777
 
778
  return decl;
779
}
780
 
781
/* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
782
   or even complete.  If this does not exist, create it.  If COMPLETE is
783
   nonzero, then complete the definition of it -- that will render it
784
   impossible to actually build the vtable, but is useful to get at those
785
   which are known to exist in the runtime.  */
786
 
787
tree
788
get_vtable_decl (tree type, int complete)
789
{
790
  tree decl;
791
 
792
  if (CLASSTYPE_VTABLES (type))
793
    return CLASSTYPE_VTABLES (type);
794
 
795
  decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
796
  CLASSTYPE_VTABLES (type) = decl;
797
 
798
  if (complete)
799
    {
800
      DECL_EXTERNAL (decl) = 1;
801
      cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
802
    }
803
 
804
  return decl;
805
}
806
 
807
/* Build the primary virtual function table for TYPE.  If BINFO is
808
   non-NULL, build the vtable starting with the initial approximation
809
   that it is the same as the one which is the head of the association
810
   list.  Returns a nonzero value if a new vtable is actually
811
   created.  */
812
 
813
static int
814
build_primary_vtable (tree binfo, tree type)
815
{
816
  tree decl;
817
  tree virtuals;
818
 
819
  decl = get_vtable_decl (type, /*complete=*/0);
820
 
821
  if (binfo)
822
    {
823
      if (BINFO_NEW_VTABLE_MARKED (binfo))
824
        /* We have already created a vtable for this base, so there's
825
           no need to do it again.  */
826
        return 0;
827
 
828
      virtuals = copy_list (BINFO_VIRTUALS (binfo));
829
      TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
830
      DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
831
      DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
832
    }
833
  else
834
    {
835
      gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
836
      virtuals = NULL_TREE;
837
    }
838
 
839
#ifdef GATHER_STATISTICS
840
  n_vtables += 1;
841
  n_vtable_elems += list_length (virtuals);
842
#endif
843
 
844
  /* Initialize the association list for this type, based
845
     on our first approximation.  */
846
  BINFO_VTABLE (TYPE_BINFO (type)) = decl;
847
  BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
848
  SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
849
  return 1;
850
}
851
 
852
/* Give BINFO a new virtual function table which is initialized
853
   with a skeleton-copy of its original initialization.  The only
854
   entry that changes is the `delta' entry, so we can really
855
   share a lot of structure.
856
 
857
   FOR_TYPE is the most derived type which caused this table to
858
   be needed.
859
 
860
   Returns nonzero if we haven't met BINFO before.
861
 
862
   The order in which vtables are built (by calling this function) for
863
   an object must remain the same, otherwise a binary incompatibility
864
   can result.  */
865
 
866
static int
867
build_secondary_vtable (tree binfo)
868
{
869
  if (BINFO_NEW_VTABLE_MARKED (binfo))
870
    /* We already created a vtable for this base.  There's no need to
871
       do it again.  */
872
    return 0;
873
 
874
  /* Remember that we've created a vtable for this BINFO, so that we
875
     don't try to do so again.  */
876
  SET_BINFO_NEW_VTABLE_MARKED (binfo);
877
 
878
  /* Make fresh virtual list, so we can smash it later.  */
879
  BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
880
 
881
  /* Secondary vtables are laid out as part of the same structure as
882
     the primary vtable.  */
883
  BINFO_VTABLE (binfo) = NULL_TREE;
884
  return 1;
885
}
886
 
887
/* Create a new vtable for BINFO which is the hierarchy dominated by
888
   T. Return nonzero if we actually created a new vtable.  */
889
 
890
static int
891
make_new_vtable (tree t, tree binfo)
892
{
893
  if (binfo == TYPE_BINFO (t))
894
    /* In this case, it is *type*'s vtable we are modifying.  We start
895
       with the approximation that its vtable is that of the
896
       immediate base class.  */
897
    return build_primary_vtable (binfo, t);
898
  else
899
    /* This is our very own copy of `basetype' to play with.  Later,
900
       we will fill in all the virtual functions that override the
901
       virtual functions in these base classes which are not defined
902
       by the current type.  */
903
    return build_secondary_vtable (binfo);
904
}
905
 
906
/* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
907
   (which is in the hierarchy dominated by T) list FNDECL as its
908
   BV_FN.  DELTA is the required constant adjustment from the `this'
909
   pointer where the vtable entry appears to the `this' required when
910
   the function is actually called.  */
911
 
912
static void
913
modify_vtable_entry (tree t,
914
                     tree binfo,
915
                     tree fndecl,
916
                     tree delta,
917
                     tree *virtuals)
918
{
919
  tree v;
920
 
921
  v = *virtuals;
922
 
923
  if (fndecl != BV_FN (v)
924
      || !tree_int_cst_equal (delta, BV_DELTA (v)))
925
    {
926
      /* We need a new vtable for BINFO.  */
927
      if (make_new_vtable (t, binfo))
928
        {
929
          /* If we really did make a new vtable, we also made a copy
930
             of the BINFO_VIRTUALS list.  Now, we have to find the
931
             corresponding entry in that list.  */
932
          *virtuals = BINFO_VIRTUALS (binfo);
933
          while (BV_FN (*virtuals) != BV_FN (v))
934
            *virtuals = TREE_CHAIN (*virtuals);
935
          v = *virtuals;
936
        }
937
 
938
      BV_DELTA (v) = delta;
939
      BV_VCALL_INDEX (v) = NULL_TREE;
940
      BV_FN (v) = fndecl;
941
    }
942
}
943
 
944
 
945
/* Add method METHOD to class TYPE.  If USING_DECL is non-null, it is
946
   the USING_DECL naming METHOD.  Returns true if the method could be
947
   added to the method vec.  */
948
 
949
bool
950
add_method (tree type, tree method, tree using_decl)
951
{
952
  unsigned slot;
953
  tree overload;
954
  bool template_conv_p = false;
955
  bool conv_p;
956
  VEC(tree,gc) *method_vec;
957
  bool complete_p;
958
  bool insert_p = false;
959
  tree current_fns;
960
  tree fns;
961
 
962
  if (method == error_mark_node)
963
    return false;
964
 
965
  complete_p = COMPLETE_TYPE_P (type);
966
  conv_p = DECL_CONV_FN_P (method);
967
  if (conv_p)
968
    template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
969
                       && DECL_TEMPLATE_CONV_FN_P (method));
970
 
971
  method_vec = CLASSTYPE_METHOD_VEC (type);
972
  if (!method_vec)
973
    {
974
      /* Make a new method vector.  We start with 8 entries.  We must
975
         allocate at least two (for constructors and destructors), and
976
         we're going to end up with an assignment operator at some
977
         point as well.  */
978
      method_vec = VEC_alloc (tree, gc, 8);
979
      /* Create slots for constructors and destructors.  */
980
      VEC_quick_push (tree, method_vec, NULL_TREE);
981
      VEC_quick_push (tree, method_vec, NULL_TREE);
982
      CLASSTYPE_METHOD_VEC (type) = method_vec;
983
    }
984
 
985
  /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc.  */
986
  grok_special_member_properties (method);
987
 
988
  /* Constructors and destructors go in special slots.  */
989
  if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
990
    slot = CLASSTYPE_CONSTRUCTOR_SLOT;
991
  else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
992
    {
993
      slot = CLASSTYPE_DESTRUCTOR_SLOT;
994
 
995
      if (TYPE_FOR_JAVA (type))
996
        {
997
          if (!DECL_ARTIFICIAL (method))
998
            error ("Java class %qT cannot have a destructor", type);
999
          else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
1000
            error ("Java class %qT cannot have an implicit non-trivial "
1001
                   "destructor",
1002
                   type);
1003
        }
1004
    }
1005
  else
1006
    {
1007
      tree m;
1008
 
1009
      insert_p = true;
1010
      /* See if we already have an entry with this name.  */
1011
      for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1012
           VEC_iterate (tree, method_vec, slot, m);
1013
           ++slot)
1014
        {
1015
          m = OVL_CURRENT (m);
1016
          if (template_conv_p)
1017
            {
1018
              if (TREE_CODE (m) == TEMPLATE_DECL
1019
                  && DECL_TEMPLATE_CONV_FN_P (m))
1020
                insert_p = false;
1021
              break;
1022
            }
1023
          if (conv_p && !DECL_CONV_FN_P (m))
1024
            break;
1025
          if (DECL_NAME (m) == DECL_NAME (method))
1026
            {
1027
              insert_p = false;
1028
              break;
1029
            }
1030
          if (complete_p
1031
              && !DECL_CONV_FN_P (m)
1032
              && DECL_NAME (m) > DECL_NAME (method))
1033
            break;
1034
        }
1035
    }
1036
  current_fns = insert_p ? NULL_TREE : VEC_index (tree, method_vec, slot);
1037
 
1038
  /* Check to see if we've already got this method.  */
1039
  for (fns = current_fns; fns; fns = OVL_NEXT (fns))
1040
    {
1041
      tree fn = OVL_CURRENT (fns);
1042
      tree fn_type;
1043
      tree method_type;
1044
      tree parms1;
1045
      tree parms2;
1046
 
1047
      if (TREE_CODE (fn) != TREE_CODE (method))
1048
        continue;
1049
 
1050
      /* [over.load] Member function declarations with the
1051
         same name and the same parameter types cannot be
1052
         overloaded if any of them is a static member
1053
         function declaration.
1054
 
1055
         [namespace.udecl] When a using-declaration brings names
1056
         from a base class into a derived class scope, member
1057
         functions in the derived class override and/or hide member
1058
         functions with the same name and parameter types in a base
1059
         class (rather than conflicting).  */
1060
      fn_type = TREE_TYPE (fn);
1061
      method_type = TREE_TYPE (method);
1062
      parms1 = TYPE_ARG_TYPES (fn_type);
1063
      parms2 = TYPE_ARG_TYPES (method_type);
1064
 
1065
      /* Compare the quals on the 'this' parm.  Don't compare
1066
         the whole types, as used functions are treated as
1067
         coming from the using class in overload resolution.  */
1068
      if (! DECL_STATIC_FUNCTION_P (fn)
1069
          && ! DECL_STATIC_FUNCTION_P (method)
1070
          && TREE_TYPE (TREE_VALUE (parms1)) != error_mark_node
1071
          && TREE_TYPE (TREE_VALUE (parms2)) != error_mark_node
1072
          && (cp_type_quals (TREE_TYPE (TREE_VALUE (parms1)))
1073
              != cp_type_quals (TREE_TYPE (TREE_VALUE (parms2)))))
1074
        continue;
1075
 
1076
      /* For templates, the return type and template parameters
1077
         must be identical.  */
1078
      if (TREE_CODE (fn) == TEMPLATE_DECL
1079
          && (!same_type_p (TREE_TYPE (fn_type),
1080
                            TREE_TYPE (method_type))
1081
              || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1082
                                       DECL_TEMPLATE_PARMS (method))))
1083
        continue;
1084
 
1085
      if (! DECL_STATIC_FUNCTION_P (fn))
1086
        parms1 = TREE_CHAIN (parms1);
1087
      if (! DECL_STATIC_FUNCTION_P (method))
1088
        parms2 = TREE_CHAIN (parms2);
1089
 
1090
      if (compparms (parms1, parms2)
1091
          && (!DECL_CONV_FN_P (fn)
1092
              || same_type_p (TREE_TYPE (fn_type),
1093
                              TREE_TYPE (method_type))))
1094
        {
1095
          if (using_decl)
1096
            {
1097
              if (DECL_CONTEXT (fn) == type)
1098
                /* Defer to the local function.  */
1099
                return false;
1100
            }
1101
          else
1102
            {
1103
              error ("%q+#D cannot be overloaded", method);
1104
              error ("with %q+#D", fn);
1105
            }
1106
 
1107
          /* We don't call duplicate_decls here to merge the
1108
             declarations because that will confuse things if the
1109
             methods have inline definitions.  In particular, we
1110
             will crash while processing the definitions.  */
1111
          return false;
1112
        }
1113
    }
1114
 
1115
  /* A class should never have more than one destructor.  */
1116
  if (current_fns && DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1117
    return false;
1118
 
1119
  /* Add the new binding.  */
1120
  overload = build_overload (method, current_fns);
1121
  if (using_decl && TREE_CODE (overload) == OVERLOAD)
1122
    OVL_USED (overload) = true;
1123
 
1124
  if (conv_p)
1125
    TYPE_HAS_CONVERSION (type) = 1;
1126
  else if (slot >= CLASSTYPE_FIRST_CONVERSION_SLOT && !complete_p)
1127
    push_class_level_binding (DECL_NAME (method), overload);
1128
 
1129
  if (insert_p)
1130
    {
1131
      bool reallocated;
1132
 
1133
      /* We only expect to add few methods in the COMPLETE_P case, so
1134
         just make room for one more method in that case.  */
1135
      if (complete_p)
1136
        reallocated = VEC_reserve_exact (tree, gc, method_vec, 1);
1137
      else
1138
        reallocated = VEC_reserve (tree, gc, method_vec, 1);
1139
      if (reallocated)
1140
        CLASSTYPE_METHOD_VEC (type) = method_vec;
1141
      if (slot == VEC_length (tree, method_vec))
1142
        VEC_quick_push (tree, method_vec, overload);
1143
      else
1144
        VEC_quick_insert (tree, method_vec, slot, overload);
1145
    }
1146
  else
1147
    /* Replace the current slot.  */
1148
    VEC_replace (tree, method_vec, slot, overload);
1149
  return true;
1150
}
1151
 
1152
/* Subroutines of finish_struct.  */
1153
 
1154
/* Change the access of FDECL to ACCESS in T.  Return 1 if change was
1155
   legit, otherwise return 0.  */
1156
 
1157
static int
1158
alter_access (tree t, tree fdecl, tree access)
1159
{
1160
  tree elem;
1161
 
1162
  if (!DECL_LANG_SPECIFIC (fdecl))
1163
    retrofit_lang_decl (fdecl);
1164
 
1165
  gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1166
 
1167
  elem = purpose_member (t, DECL_ACCESS (fdecl));
1168
  if (elem)
1169
    {
1170
      if (TREE_VALUE (elem) != access)
1171
        {
1172
          if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1173
            error ("conflicting access specifications for method"
1174
                   " %q+D, ignored", TREE_TYPE (fdecl));
1175
          else
1176
            error ("conflicting access specifications for field %qE, ignored",
1177
                   DECL_NAME (fdecl));
1178
        }
1179
      else
1180
        {
1181
          /* They're changing the access to the same thing they changed
1182
             it to before.  That's OK.  */
1183
          ;
1184
        }
1185
    }
1186
  else
1187
    {
1188
      perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl);
1189
      DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1190
      return 1;
1191
    }
1192
  return 0;
1193
}
1194
 
1195
/* Process the USING_DECL, which is a member of T.  */
1196
 
1197
static void
1198
handle_using_decl (tree using_decl, tree t)
1199
{
1200
  tree decl = USING_DECL_DECLS (using_decl);
1201
  tree name = DECL_NAME (using_decl);
1202
  tree access
1203
    = TREE_PRIVATE (using_decl) ? access_private_node
1204
    : TREE_PROTECTED (using_decl) ? access_protected_node
1205
    : access_public_node;
1206
  tree flist = NULL_TREE;
1207
  tree old_value;
1208
 
1209
  gcc_assert (!processing_template_decl && decl);
1210
 
1211
  old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1212
                             tf_warning_or_error);
1213
  if (old_value)
1214
    {
1215
      if (is_overloaded_fn (old_value))
1216
        old_value = OVL_CURRENT (old_value);
1217
 
1218
      if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1219
        /* OK */;
1220
      else
1221
        old_value = NULL_TREE;
1222
    }
1223
 
1224
  cp_emit_debug_info_for_using (decl, USING_DECL_SCOPE (using_decl));
1225
 
1226
  if (is_overloaded_fn (decl))
1227
    flist = decl;
1228
 
1229
  if (! old_value)
1230
    ;
1231
  else if (is_overloaded_fn (old_value))
1232
    {
1233
      if (flist)
1234
        /* It's OK to use functions from a base when there are functions with
1235
           the same name already present in the current class.  */;
1236
      else
1237
        {
1238
          error ("%q+D invalid in %q#T", using_decl, t);
1239
          error ("  because of local method %q+#D with same name",
1240
                 OVL_CURRENT (old_value));
1241
          return;
1242
        }
1243
    }
1244
  else if (!DECL_ARTIFICIAL (old_value))
1245
    {
1246
      error ("%q+D invalid in %q#T", using_decl, t);
1247
      error ("  because of local member %q+#D with same name", old_value);
1248
      return;
1249
    }
1250
 
1251
  /* Make type T see field decl FDECL with access ACCESS.  */
1252
  if (flist)
1253
    for (; flist; flist = OVL_NEXT (flist))
1254
      {
1255
        add_method (t, OVL_CURRENT (flist), using_decl);
1256
        alter_access (t, OVL_CURRENT (flist), access);
1257
      }
1258
  else
1259
    alter_access (t, decl, access);
1260
}
1261
 
1262
/* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1263
   and NO_CONST_ASN_REF_P.  Also set flag bits in T based on
1264
   properties of the bases.  */
1265
 
1266
static void
1267
check_bases (tree t,
1268
             int* cant_have_const_ctor_p,
1269
             int* no_const_asn_ref_p)
1270
{
1271
  int i;
1272
  bool seen_non_virtual_nearly_empty_base_p = 0;
1273
  int seen_tm_mask = 0;
1274
  tree base_binfo;
1275
  tree binfo;
1276
  tree field = NULL_TREE;
1277
 
1278
  if (!CLASSTYPE_NON_STD_LAYOUT (t))
1279
    for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1280
      if (TREE_CODE (field) == FIELD_DECL)
1281
        break;
1282
 
1283
  for (binfo = TYPE_BINFO (t), i = 0;
1284
       BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1285
    {
1286
      tree basetype = TREE_TYPE (base_binfo);
1287
 
1288
      gcc_assert (COMPLETE_TYPE_P (basetype));
1289
 
1290
      if (CLASSTYPE_FINAL (basetype))
1291
        error ("cannot derive from %<final%> base %qT in derived type %qT",
1292
               basetype, t);
1293
 
1294
      /* If any base class is non-literal, so is the derived class.  */
1295
      if (!CLASSTYPE_LITERAL_P (basetype))
1296
        CLASSTYPE_LITERAL_P (t) = false;
1297
 
1298
      /* Effective C++ rule 14.  We only need to check TYPE_POLYMORPHIC_P
1299
         here because the case of virtual functions but non-virtual
1300
         dtor is handled in finish_struct_1.  */
1301
      if (!TYPE_POLYMORPHIC_P (basetype))
1302
        warning (OPT_Weffc__,
1303
                 "base class %q#T has a non-virtual destructor", basetype);
1304
 
1305
      /* If the base class doesn't have copy constructors or
1306
         assignment operators that take const references, then the
1307
         derived class cannot have such a member automatically
1308
         generated.  */
1309
      if (TYPE_HAS_COPY_CTOR (basetype)
1310
          && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1311
        *cant_have_const_ctor_p = 1;
1312
      if (TYPE_HAS_COPY_ASSIGN (basetype)
1313
          && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1314
        *no_const_asn_ref_p = 1;
1315
 
1316
      if (BINFO_VIRTUAL_P (base_binfo))
1317
        /* A virtual base does not effect nearly emptiness.  */
1318
        ;
1319
      else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1320
        {
1321
          if (seen_non_virtual_nearly_empty_base_p)
1322
            /* And if there is more than one nearly empty base, then the
1323
               derived class is not nearly empty either.  */
1324
            CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1325
          else
1326
            /* Remember we've seen one.  */
1327
            seen_non_virtual_nearly_empty_base_p = 1;
1328
        }
1329
      else if (!is_empty_class (basetype))
1330
        /* If the base class is not empty or nearly empty, then this
1331
           class cannot be nearly empty.  */
1332
        CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1333
 
1334
      /* A lot of properties from the bases also apply to the derived
1335
         class.  */
1336
      TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1337
      TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1338
        |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1339
      TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1340
        |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1341
            || !TYPE_HAS_COPY_ASSIGN (basetype));
1342
      TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1343
                                         || !TYPE_HAS_COPY_CTOR (basetype));
1344
      TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1345
        |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1346
      TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1347
      TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1348
      CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1349
        |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1350
      TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1351
                                    || TYPE_HAS_COMPLEX_DFLT (basetype));
1352
 
1353
      /*  A standard-layout class is a class that:
1354
          ...
1355
          * has no non-standard-layout base classes,  */
1356
      CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1357
      if (!CLASSTYPE_NON_STD_LAYOUT (t))
1358
        {
1359
          tree basefield;
1360
          /* ...has no base classes of the same type as the first non-static
1361
             data member...  */
1362
          if (field && DECL_CONTEXT (field) == t
1363
              && (same_type_ignoring_top_level_qualifiers_p
1364
                  (TREE_TYPE (field), basetype)))
1365
            CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1366
          else
1367
            /* ...either has no non-static data members in the most-derived
1368
               class and at most one base class with non-static data
1369
               members, or has no base classes with non-static data
1370
               members */
1371
            for (basefield = TYPE_FIELDS (basetype); basefield;
1372
                 basefield = DECL_CHAIN (basefield))
1373
              if (TREE_CODE (basefield) == FIELD_DECL)
1374
                {
1375
                  if (field)
1376
                    CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1377
                  else
1378
                    field = basefield;
1379
                  break;
1380
                }
1381
        }
1382
 
1383
      /* Don't bother collecting tm attributes if transactional memory
1384
         support is not enabled.  */
1385
      if (flag_tm)
1386
        {
1387
          tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1388
          if (tm_attr)
1389
            seen_tm_mask |= tm_attr_to_mask (tm_attr);
1390
        }
1391
    }
1392
 
1393
  /* If one of the base classes had TM attributes, and the current class
1394
     doesn't define its own, then the current class inherits one.  */
1395
  if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1396
    {
1397
      tree tm_attr = tm_mask_to_attr (seen_tm_mask & -seen_tm_mask);
1398
      TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1399
    }
1400
}
1401
 
1402
/* Determine all the primary bases within T.  Sets BINFO_PRIMARY_BASE_P for
1403
   those that are primaries.  Sets BINFO_LOST_PRIMARY_P for those
1404
   that have had a nearly-empty virtual primary base stolen by some
1405
   other base in the hierarchy.  Determines CLASSTYPE_PRIMARY_BASE for
1406
   T.  */
1407
 
1408
static void
1409
determine_primary_bases (tree t)
1410
{
1411
  unsigned i;
1412
  tree primary = NULL_TREE;
1413
  tree type_binfo = TYPE_BINFO (t);
1414
  tree base_binfo;
1415
 
1416
  /* Determine the primary bases of our bases.  */
1417
  for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1418
       base_binfo = TREE_CHAIN (base_binfo))
1419
    {
1420
      tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1421
 
1422
      /* See if we're the non-virtual primary of our inheritance
1423
         chain.  */
1424
      if (!BINFO_VIRTUAL_P (base_binfo))
1425
        {
1426
          tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1427
          tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1428
 
1429
          if (parent_primary
1430
              && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1431
                                    BINFO_TYPE (parent_primary)))
1432
            /* We are the primary binfo.  */
1433
            BINFO_PRIMARY_P (base_binfo) = 1;
1434
        }
1435
      /* Determine if we have a virtual primary base, and mark it so.
1436
       */
1437
      if (primary && BINFO_VIRTUAL_P (primary))
1438
        {
1439
          tree this_primary = copied_binfo (primary, base_binfo);
1440
 
1441
          if (BINFO_PRIMARY_P (this_primary))
1442
            /* Someone already claimed this base.  */
1443
            BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1444
          else
1445
            {
1446
              tree delta;
1447
 
1448
              BINFO_PRIMARY_P (this_primary) = 1;
1449
              BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1450
 
1451
              /* A virtual binfo might have been copied from within
1452
                 another hierarchy. As we're about to use it as a
1453
                 primary base, make sure the offsets match.  */
1454
              delta = size_diffop_loc (input_location,
1455
                                   convert (ssizetype,
1456
                                            BINFO_OFFSET (base_binfo)),
1457
                                   convert (ssizetype,
1458
                                            BINFO_OFFSET (this_primary)));
1459
 
1460
              propagate_binfo_offsets (this_primary, delta);
1461
            }
1462
        }
1463
    }
1464
 
1465
  /* First look for a dynamic direct non-virtual base.  */
1466
  for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1467
    {
1468
      tree basetype = BINFO_TYPE (base_binfo);
1469
 
1470
      if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1471
        {
1472
          primary = base_binfo;
1473
          goto found;
1474
        }
1475
    }
1476
 
1477
  /* A "nearly-empty" virtual base class can be the primary base
1478
     class, if no non-virtual polymorphic base can be found.  Look for
1479
     a nearly-empty virtual dynamic base that is not already a primary
1480
     base of something in the hierarchy.  If there is no such base,
1481
     just pick the first nearly-empty virtual base.  */
1482
 
1483
  for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1484
       base_binfo = TREE_CHAIN (base_binfo))
1485
    if (BINFO_VIRTUAL_P (base_binfo)
1486
        && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1487
      {
1488
        if (!BINFO_PRIMARY_P (base_binfo))
1489
          {
1490
            /* Found one that is not primary.  */
1491
            primary = base_binfo;
1492
            goto found;
1493
          }
1494
        else if (!primary)
1495
          /* Remember the first candidate.  */
1496
          primary = base_binfo;
1497
      }
1498
 
1499
 found:
1500
  /* If we've got a primary base, use it.  */
1501
  if (primary)
1502
    {
1503
      tree basetype = BINFO_TYPE (primary);
1504
 
1505
      CLASSTYPE_PRIMARY_BINFO (t) = primary;
1506
      if (BINFO_PRIMARY_P (primary))
1507
        /* We are stealing a primary base.  */
1508
        BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1509
      BINFO_PRIMARY_P (primary) = 1;
1510
      if (BINFO_VIRTUAL_P (primary))
1511
        {
1512
          tree delta;
1513
 
1514
          BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1515
          /* A virtual binfo might have been copied from within
1516
             another hierarchy. As we're about to use it as a primary
1517
             base, make sure the offsets match.  */
1518
          delta = size_diffop_loc (input_location, ssize_int (0),
1519
                               convert (ssizetype, BINFO_OFFSET (primary)));
1520
 
1521
          propagate_binfo_offsets (primary, delta);
1522
        }
1523
 
1524
      primary = TYPE_BINFO (basetype);
1525
 
1526
      TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1527
      BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
1528
      BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
1529
    }
1530
}
1531
 
1532
/* Update the variant types of T.  */
1533
 
1534
void
1535
fixup_type_variants (tree t)
1536
{
1537
  tree variants;
1538
 
1539
  if (!t)
1540
    return;
1541
 
1542
  for (variants = TYPE_NEXT_VARIANT (t);
1543
       variants;
1544
       variants = TYPE_NEXT_VARIANT (variants))
1545
    {
1546
      /* These fields are in the _TYPE part of the node, not in
1547
         the TYPE_LANG_SPECIFIC component, so they are not shared.  */
1548
      TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
1549
      TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1550
      TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1551
        = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1552
 
1553
      TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1554
 
1555
      TYPE_BINFO (variants) = TYPE_BINFO (t);
1556
 
1557
      /* Copy whatever these are holding today.  */
1558
      TYPE_VFIELD (variants) = TYPE_VFIELD (t);
1559
      TYPE_METHODS (variants) = TYPE_METHODS (t);
1560
      TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1561
    }
1562
}
1563
 
1564
/* Early variant fixups: we apply attributes at the beginning of the class
1565
   definition, and we need to fix up any variants that have already been
1566
   made via elaborated-type-specifier so that check_qualified_type works.  */
1567
 
1568
void
1569
fixup_attribute_variants (tree t)
1570
{
1571
  tree variants;
1572
 
1573
  if (!t)
1574
    return;
1575
 
1576
  for (variants = TYPE_NEXT_VARIANT (t);
1577
       variants;
1578
       variants = TYPE_NEXT_VARIANT (variants))
1579
    {
1580
      /* These are the two fields that check_qualified_type looks at and
1581
         are affected by attributes.  */
1582
      TYPE_ATTRIBUTES (variants) = TYPE_ATTRIBUTES (t);
1583
      TYPE_ALIGN (variants) = TYPE_ALIGN (t);
1584
    }
1585
}
1586
 
1587
/* Set memoizing fields and bits of T (and its variants) for later
1588
   use.  */
1589
 
1590
static void
1591
finish_struct_bits (tree t)
1592
{
1593
  /* Fix up variants (if any).  */
1594
  fixup_type_variants (t);
1595
 
1596
  if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
1597
    /* For a class w/o baseclasses, 'finish_struct' has set
1598
       CLASSTYPE_PURE_VIRTUALS correctly (by definition).
1599
       Similarly for a class whose base classes do not have vtables.
1600
       When neither of these is true, we might have removed abstract
1601
       virtuals (by providing a definition), added some (by declaring
1602
       new ones), or redeclared ones from a base class.  We need to
1603
       recalculate what's really an abstract virtual at this point (by
1604
       looking in the vtables).  */
1605
    get_pure_virtuals (t);
1606
 
1607
  /* If this type has a copy constructor or a destructor, force its
1608
     mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
1609
     nonzero.  This will cause it to be passed by invisible reference
1610
     and prevent it from being returned in a register.  */
1611
  if (type_has_nontrivial_copy_init (t)
1612
      || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
1613
    {
1614
      tree variants;
1615
      DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1616
      for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1617
        {
1618
          SET_TYPE_MODE (variants, BLKmode);
1619
          TREE_ADDRESSABLE (variants) = 1;
1620
        }
1621
    }
1622
}
1623
 
1624
/* Issue warnings about T having private constructors, but no friends,
1625
   and so forth.
1626
 
1627
   HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1628
   static members.  HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1629
   non-private static member functions.  */
1630
 
1631
static void
1632
maybe_warn_about_overly_private_class (tree t)
1633
{
1634
  int has_member_fn = 0;
1635
  int has_nonprivate_method = 0;
1636
  tree fn;
1637
 
1638
  if (!warn_ctor_dtor_privacy
1639
      /* If the class has friends, those entities might create and
1640
         access instances, so we should not warn.  */
1641
      || (CLASSTYPE_FRIEND_CLASSES (t)
1642
          || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1643
      /* We will have warned when the template was declared; there's
1644
         no need to warn on every instantiation.  */
1645
      || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1646
    /* There's no reason to even consider warning about this
1647
       class.  */
1648
    return;
1649
 
1650
  /* We only issue one warning, if more than one applies, because
1651
     otherwise, on code like:
1652
 
1653
     class A {
1654
       // Oops - forgot `public:'
1655
       A();
1656
       A(const A&);
1657
       ~A();
1658
     };
1659
 
1660
     we warn several times about essentially the same problem.  */
1661
 
1662
  /* Check to see if all (non-constructor, non-destructor) member
1663
     functions are private.  (Since there are no friends or
1664
     non-private statics, we can't ever call any of the private member
1665
     functions.)  */
1666
  for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
1667
    /* We're not interested in compiler-generated methods; they don't
1668
       provide any way to call private members.  */
1669
    if (!DECL_ARTIFICIAL (fn))
1670
      {
1671
        if (!TREE_PRIVATE (fn))
1672
          {
1673
            if (DECL_STATIC_FUNCTION_P (fn))
1674
              /* A non-private static member function is just like a
1675
                 friend; it can create and invoke private member
1676
                 functions, and be accessed without a class
1677
                 instance.  */
1678
              return;
1679
 
1680
            has_nonprivate_method = 1;
1681
            /* Keep searching for a static member function.  */
1682
          }
1683
        else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1684
          has_member_fn = 1;
1685
      }
1686
 
1687
  if (!has_nonprivate_method && has_member_fn)
1688
    {
1689
      /* There are no non-private methods, and there's at least one
1690
         private member function that isn't a constructor or
1691
         destructor.  (If all the private members are
1692
         constructors/destructors we want to use the code below that
1693
         issues error messages specifically referring to
1694
         constructors/destructors.)  */
1695
      unsigned i;
1696
      tree binfo = TYPE_BINFO (t);
1697
 
1698
      for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
1699
        if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
1700
          {
1701
            has_nonprivate_method = 1;
1702
            break;
1703
          }
1704
      if (!has_nonprivate_method)
1705
        {
1706
          warning (OPT_Wctor_dtor_privacy,
1707
                   "all member functions in class %qT are private", t);
1708
          return;
1709
        }
1710
    }
1711
 
1712
  /* Even if some of the member functions are non-private, the class
1713
     won't be useful for much if all the constructors or destructors
1714
     are private: such an object can never be created or destroyed.  */
1715
  fn = CLASSTYPE_DESTRUCTORS (t);
1716
  if (fn && TREE_PRIVATE (fn))
1717
    {
1718
      warning (OPT_Wctor_dtor_privacy,
1719
               "%q#T only defines a private destructor and has no friends",
1720
               t);
1721
      return;
1722
    }
1723
 
1724
  /* Warn about classes that have private constructors and no friends.  */
1725
  if (TYPE_HAS_USER_CONSTRUCTOR (t)
1726
      /* Implicitly generated constructors are always public.  */
1727
      && (!CLASSTYPE_LAZY_DEFAULT_CTOR (t)
1728
          || !CLASSTYPE_LAZY_COPY_CTOR (t)))
1729
    {
1730
      int nonprivate_ctor = 0;
1731
 
1732
      /* If a non-template class does not define a copy
1733
         constructor, one is defined for it, enabling it to avoid
1734
         this warning.  For a template class, this does not
1735
         happen, and so we would normally get a warning on:
1736
 
1737
           template <class T> class C { private: C(); };
1738
 
1739
         To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR.  All
1740
         complete non-template or fully instantiated classes have this
1741
         flag set.  */
1742
      if (!TYPE_HAS_COPY_CTOR (t))
1743
        nonprivate_ctor = 1;
1744
      else
1745
        for (fn = CLASSTYPE_CONSTRUCTORS (t); fn; fn = OVL_NEXT (fn))
1746
          {
1747
            tree ctor = OVL_CURRENT (fn);
1748
            /* Ideally, we wouldn't count copy constructors (or, in
1749
               fact, any constructor that takes an argument of the
1750
               class type as a parameter) because such things cannot
1751
               be used to construct an instance of the class unless
1752
               you already have one.  But, for now at least, we're
1753
               more generous.  */
1754
            if (! TREE_PRIVATE (ctor))
1755
              {
1756
                nonprivate_ctor = 1;
1757
                break;
1758
              }
1759
          }
1760
 
1761
      if (nonprivate_ctor == 0)
1762
        {
1763
          warning (OPT_Wctor_dtor_privacy,
1764
                   "%q#T only defines private constructors and has no friends",
1765
                   t);
1766
          return;
1767
        }
1768
    }
1769
}
1770
 
1771
static struct {
1772
  gt_pointer_operator new_value;
1773
  void *cookie;
1774
} resort_data;
1775
 
1776
/* Comparison function to compare two TYPE_METHOD_VEC entries by name.  */
1777
 
1778
static int
1779
method_name_cmp (const void* m1_p, const void* m2_p)
1780
{
1781
  const tree *const m1 = (const tree *) m1_p;
1782
  const tree *const m2 = (const tree *) m2_p;
1783
 
1784
  if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1785
    return 0;
1786
  if (*m1 == NULL_TREE)
1787
    return -1;
1788
  if (*m2 == NULL_TREE)
1789
    return 1;
1790
  if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
1791
    return -1;
1792
  return 1;
1793
}
1794
 
1795
/* This routine compares two fields like method_name_cmp but using the
1796
   pointer operator in resort_field_decl_data.  */
1797
 
1798
static int
1799
resort_method_name_cmp (const void* m1_p, const void* m2_p)
1800
{
1801
  const tree *const m1 = (const tree *) m1_p;
1802
  const tree *const m2 = (const tree *) m2_p;
1803
  if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1804
    return 0;
1805
  if (*m1 == NULL_TREE)
1806
    return -1;
1807
  if (*m2 == NULL_TREE)
1808
    return 1;
1809
  {
1810
    tree d1 = DECL_NAME (OVL_CURRENT (*m1));
1811
    tree d2 = DECL_NAME (OVL_CURRENT (*m2));
1812
    resort_data.new_value (&d1, resort_data.cookie);
1813
    resort_data.new_value (&d2, resort_data.cookie);
1814
    if (d1 < d2)
1815
      return -1;
1816
  }
1817
  return 1;
1818
}
1819
 
1820
/* Resort TYPE_METHOD_VEC because pointers have been reordered.  */
1821
 
1822
void
1823
resort_type_method_vec (void* obj,
1824
                        void* orig_obj ATTRIBUTE_UNUSED ,
1825
                        gt_pointer_operator new_value,
1826
                        void* cookie)
1827
{
1828
  VEC(tree,gc) *method_vec = (VEC(tree,gc) *) obj;
1829
  int len = VEC_length (tree, method_vec);
1830
  size_t slot;
1831
  tree fn;
1832
 
1833
  /* The type conversion ops have to live at the front of the vec, so we
1834
     can't sort them.  */
1835
  for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1836
       VEC_iterate (tree, method_vec, slot, fn);
1837
       ++slot)
1838
    if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1839
      break;
1840
 
1841
  if (len - slot > 1)
1842
    {
1843
      resort_data.new_value = new_value;
1844
      resort_data.cookie = cookie;
1845
      qsort (VEC_address (tree, method_vec) + slot, len - slot, sizeof (tree),
1846
             resort_method_name_cmp);
1847
    }
1848
}
1849
 
1850
/* Warn about duplicate methods in fn_fields.
1851
 
1852
   Sort methods that are not special (i.e., constructors, destructors,
1853
   and type conversion operators) so that we can find them faster in
1854
   search.  */
1855
 
1856
static void
1857
finish_struct_methods (tree t)
1858
{
1859
  tree fn_fields;
1860
  VEC(tree,gc) *method_vec;
1861
  int slot, len;
1862
 
1863
  method_vec = CLASSTYPE_METHOD_VEC (t);
1864
  if (!method_vec)
1865
    return;
1866
 
1867
  len = VEC_length (tree, method_vec);
1868
 
1869
  /* Clear DECL_IN_AGGR_P for all functions.  */
1870
  for (fn_fields = TYPE_METHODS (t); fn_fields;
1871
       fn_fields = DECL_CHAIN (fn_fields))
1872
    DECL_IN_AGGR_P (fn_fields) = 0;
1873
 
1874
  /* Issue warnings about private constructors and such.  If there are
1875
     no methods, then some public defaults are generated.  */
1876
  maybe_warn_about_overly_private_class (t);
1877
 
1878
  /* The type conversion ops have to live at the front of the vec, so we
1879
     can't sort them.  */
1880
  for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1881
       VEC_iterate (tree, method_vec, slot, fn_fields);
1882
       ++slot)
1883
    if (!DECL_CONV_FN_P (OVL_CURRENT (fn_fields)))
1884
      break;
1885
  if (len - slot > 1)
1886
    qsort (VEC_address (tree, method_vec) + slot,
1887
           len-slot, sizeof (tree), method_name_cmp);
1888
}
1889
 
1890
/* Make BINFO's vtable have N entries, including RTTI entries,
1891
   vbase and vcall offsets, etc.  Set its type and call the back end
1892
   to lay it out.  */
1893
 
1894
static void
1895
layout_vtable_decl (tree binfo, int n)
1896
{
1897
  tree atype;
1898
  tree vtable;
1899
 
1900
  atype = build_array_of_n_type (vtable_entry_type, n);
1901
  layout_type (atype);
1902
 
1903
  /* We may have to grow the vtable.  */
1904
  vtable = get_vtbl_decl_for_binfo (binfo);
1905
  if (!same_type_p (TREE_TYPE (vtable), atype))
1906
    {
1907
      TREE_TYPE (vtable) = atype;
1908
      DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
1909
      layout_decl (vtable, 0);
1910
    }
1911
}
1912
 
1913
/* True iff FNDECL and BASE_FNDECL (both non-static member functions)
1914
   have the same signature.  */
1915
 
1916
int
1917
same_signature_p (const_tree fndecl, const_tree base_fndecl)
1918
{
1919
  /* One destructor overrides another if they are the same kind of
1920
     destructor.  */
1921
  if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
1922
      && special_function_p (base_fndecl) == special_function_p (fndecl))
1923
    return 1;
1924
  /* But a non-destructor never overrides a destructor, nor vice
1925
     versa, nor do different kinds of destructors override
1926
     one-another.  For example, a complete object destructor does not
1927
     override a deleting destructor.  */
1928
  if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
1929
    return 0;
1930
 
1931
  if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
1932
      || (DECL_CONV_FN_P (fndecl)
1933
          && DECL_CONV_FN_P (base_fndecl)
1934
          && same_type_p (DECL_CONV_FN_TYPE (fndecl),
1935
                          DECL_CONV_FN_TYPE (base_fndecl))))
1936
    {
1937
      tree types, base_types;
1938
      types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1939
      base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
1940
      if ((cp_type_quals (TREE_TYPE (TREE_VALUE (base_types)))
1941
           == cp_type_quals (TREE_TYPE (TREE_VALUE (types))))
1942
          && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
1943
        return 1;
1944
    }
1945
  return 0;
1946
}
1947
 
1948
/* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
1949
   subobject.  */
1950
 
1951
static bool
1952
base_derived_from (tree derived, tree base)
1953
{
1954
  tree probe;
1955
 
1956
  for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
1957
    {
1958
      if (probe == derived)
1959
        return true;
1960
      else if (BINFO_VIRTUAL_P (probe))
1961
        /* If we meet a virtual base, we can't follow the inheritance
1962
           any more.  See if the complete type of DERIVED contains
1963
           such a virtual base.  */
1964
        return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
1965
                != NULL_TREE);
1966
    }
1967
  return false;
1968
}
1969
 
1970
typedef struct find_final_overrider_data_s {
1971
  /* The function for which we are trying to find a final overrider.  */
1972
  tree fn;
1973
  /* The base class in which the function was declared.  */
1974
  tree declaring_base;
1975
  /* The candidate overriders.  */
1976
  tree candidates;
1977
  /* Path to most derived.  */
1978
  VEC(tree,heap) *path;
1979
} find_final_overrider_data;
1980
 
1981
/* Add the overrider along the current path to FFOD->CANDIDATES.
1982
   Returns true if an overrider was found; false otherwise.  */
1983
 
1984
static bool
1985
dfs_find_final_overrider_1 (tree binfo,
1986
                            find_final_overrider_data *ffod,
1987
                            unsigned depth)
1988
{
1989
  tree method;
1990
 
1991
  /* If BINFO is not the most derived type, try a more derived class.
1992
     A definition there will overrider a definition here.  */
1993
  if (depth)
1994
    {
1995
      depth--;
1996
      if (dfs_find_final_overrider_1
1997
          (VEC_index (tree, ffod->path, depth), ffod, depth))
1998
        return true;
1999
    }
2000
 
2001
  method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2002
  if (method)
2003
    {
2004
      tree *candidate = &ffod->candidates;
2005
 
2006
      /* Remove any candidates overridden by this new function.  */
2007
      while (*candidate)
2008
        {
2009
          /* If *CANDIDATE overrides METHOD, then METHOD
2010
             cannot override anything else on the list.  */
2011
          if (base_derived_from (TREE_VALUE (*candidate), binfo))
2012
            return true;
2013
          /* If METHOD overrides *CANDIDATE, remove *CANDIDATE.  */
2014
          if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2015
            *candidate = TREE_CHAIN (*candidate);
2016
          else
2017
            candidate = &TREE_CHAIN (*candidate);
2018
        }
2019
 
2020
      /* Add the new function.  */
2021
      ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2022
      return true;
2023
    }
2024
 
2025
  return false;
2026
}
2027
 
2028
/* Called from find_final_overrider via dfs_walk.  */
2029
 
2030
static tree
2031
dfs_find_final_overrider_pre (tree binfo, void *data)
2032
{
2033
  find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2034
 
2035
  if (binfo == ffod->declaring_base)
2036
    dfs_find_final_overrider_1 (binfo, ffod, VEC_length (tree, ffod->path));
2037
  VEC_safe_push (tree, heap, ffod->path, binfo);
2038
 
2039
  return NULL_TREE;
2040
}
2041
 
2042
static tree
2043
dfs_find_final_overrider_post (tree binfo ATTRIBUTE_UNUSED, void *data)
2044
{
2045
  find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2046
  VEC_pop (tree, ffod->path);
2047
 
2048
  return NULL_TREE;
2049
}
2050
 
2051
/* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2052
   FN and whose TREE_VALUE is the binfo for the base where the
2053
   overriding occurs.  BINFO (in the hierarchy dominated by the binfo
2054
   DERIVED) is the base object in which FN is declared.  */
2055
 
2056
static tree
2057
find_final_overrider (tree derived, tree binfo, tree fn)
2058
{
2059
  find_final_overrider_data ffod;
2060
 
2061
  /* Getting this right is a little tricky.  This is valid:
2062
 
2063
       struct S { virtual void f (); };
2064
       struct T { virtual void f (); };
2065
       struct U : public S, public T { };
2066
 
2067
     even though calling `f' in `U' is ambiguous.  But,
2068
 
2069
       struct R { virtual void f(); };
2070
       struct S : virtual public R { virtual void f (); };
2071
       struct T : virtual public R { virtual void f (); };
2072
       struct U : public S, public T { };
2073
 
2074
     is not -- there's no way to decide whether to put `S::f' or
2075
     `T::f' in the vtable for `R'.
2076
 
2077
     The solution is to look at all paths to BINFO.  If we find
2078
     different overriders along any two, then there is a problem.  */
2079
  if (DECL_THUNK_P (fn))
2080
    fn = THUNK_TARGET (fn);
2081
 
2082
  /* Determine the depth of the hierarchy.  */
2083
  ffod.fn = fn;
2084
  ffod.declaring_base = binfo;
2085
  ffod.candidates = NULL_TREE;
2086
  ffod.path = VEC_alloc (tree, heap, 30);
2087
 
2088
  dfs_walk_all (derived, dfs_find_final_overrider_pre,
2089
                dfs_find_final_overrider_post, &ffod);
2090
 
2091
  VEC_free (tree, heap, ffod.path);
2092
 
2093
  /* If there was no winner, issue an error message.  */
2094
  if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2095
    return error_mark_node;
2096
 
2097
  return ffod.candidates;
2098
}
2099
 
2100
/* Return the index of the vcall offset for FN when TYPE is used as a
2101
   virtual base.  */
2102
 
2103
static tree
2104
get_vcall_index (tree fn, tree type)
2105
{
2106
  VEC(tree_pair_s,gc) *indices = CLASSTYPE_VCALL_INDICES (type);
2107
  tree_pair_p p;
2108
  unsigned ix;
2109
 
2110
  FOR_EACH_VEC_ELT (tree_pair_s, indices, ix, p)
2111
    if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2112
        || same_signature_p (fn, p->purpose))
2113
      return p->value;
2114
 
2115
  /* There should always be an appropriate index.  */
2116
  gcc_unreachable ();
2117
}
2118
 
2119
/* Update an entry in the vtable for BINFO, which is in the hierarchy
2120
   dominated by T.  FN is the old function; VIRTUALS points to the
2121
   corresponding position in the new BINFO_VIRTUALS list.  IX is the index
2122
   of that entry in the list.  */
2123
 
2124
static void
2125
update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2126
                            unsigned ix)
2127
{
2128
  tree b;
2129
  tree overrider;
2130
  tree delta;
2131
  tree virtual_base;
2132
  tree first_defn;
2133
  tree overrider_fn, overrider_target;
2134
  tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2135
  tree over_return, base_return;
2136
  bool lost = false;
2137
 
2138
  /* Find the nearest primary base (possibly binfo itself) which defines
2139
     this function; this is the class the caller will convert to when
2140
     calling FN through BINFO.  */
2141
  for (b = binfo; ; b = get_primary_binfo (b))
2142
    {
2143
      gcc_assert (b);
2144
      if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2145
        break;
2146
 
2147
      /* The nearest definition is from a lost primary.  */
2148
      if (BINFO_LOST_PRIMARY_P (b))
2149
        lost = true;
2150
    }
2151
  first_defn = b;
2152
 
2153
  /* Find the final overrider.  */
2154
  overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2155
  if (overrider == error_mark_node)
2156
    {
2157
      error ("no unique final overrider for %qD in %qT", target_fn, t);
2158
      return;
2159
    }
2160
  overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2161
 
2162
  /* Check for adjusting covariant return types.  */
2163
  over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2164
  base_return = TREE_TYPE (TREE_TYPE (target_fn));
2165
 
2166
  if (POINTER_TYPE_P (over_return)
2167
      && TREE_CODE (over_return) == TREE_CODE (base_return)
2168
      && CLASS_TYPE_P (TREE_TYPE (over_return))
2169
      && CLASS_TYPE_P (TREE_TYPE (base_return))
2170
      /* If the overrider is invalid, don't even try.  */
2171
      && !DECL_INVALID_OVERRIDER_P (overrider_target))
2172
    {
2173
      /* If FN is a covariant thunk, we must figure out the adjustment
2174
         to the final base FN was converting to. As OVERRIDER_TARGET might
2175
         also be converting to the return type of FN, we have to
2176
         combine the two conversions here.  */
2177
      tree fixed_offset, virtual_offset;
2178
 
2179
      over_return = TREE_TYPE (over_return);
2180
      base_return = TREE_TYPE (base_return);
2181
 
2182
      if (DECL_THUNK_P (fn))
2183
        {
2184
          gcc_assert (DECL_RESULT_THUNK_P (fn));
2185
          fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2186
          virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2187
        }
2188
      else
2189
        fixed_offset = virtual_offset = NULL_TREE;
2190
 
2191
      if (virtual_offset)
2192
        /* Find the equivalent binfo within the return type of the
2193
           overriding function. We will want the vbase offset from
2194
           there.  */
2195
        virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2196
                                          over_return);
2197
      else if (!same_type_ignoring_top_level_qualifiers_p
2198
               (over_return, base_return))
2199
        {
2200
          /* There was no existing virtual thunk (which takes
2201
             precedence).  So find the binfo of the base function's
2202
             return type within the overriding function's return type.
2203
             We cannot call lookup base here, because we're inside a
2204
             dfs_walk, and will therefore clobber the BINFO_MARKED
2205
             flags.  Fortunately we know the covariancy is valid (it
2206
             has already been checked), so we can just iterate along
2207
             the binfos, which have been chained in inheritance graph
2208
             order.  Of course it is lame that we have to repeat the
2209
             search here anyway -- we should really be caching pieces
2210
             of the vtable and avoiding this repeated work.  */
2211
          tree thunk_binfo, base_binfo;
2212
 
2213
          /* Find the base binfo within the overriding function's
2214
             return type.  We will always find a thunk_binfo, except
2215
             when the covariancy is invalid (which we will have
2216
             already diagnosed).  */
2217
          for (base_binfo = TYPE_BINFO (base_return),
2218
               thunk_binfo = TYPE_BINFO (over_return);
2219
               thunk_binfo;
2220
               thunk_binfo = TREE_CHAIN (thunk_binfo))
2221
            if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2222
                                   BINFO_TYPE (base_binfo)))
2223
              break;
2224
 
2225
          /* See if virtual inheritance is involved.  */
2226
          for (virtual_offset = thunk_binfo;
2227
               virtual_offset;
2228
               virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2229
            if (BINFO_VIRTUAL_P (virtual_offset))
2230
              break;
2231
 
2232
          if (virtual_offset
2233
              || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2234
            {
2235
              tree offset = convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2236
 
2237
              if (virtual_offset)
2238
                {
2239
                  /* We convert via virtual base.  Adjust the fixed
2240
                     offset to be from there.  */
2241
                  offset =
2242
                    size_diffop (offset,
2243
                                 convert (ssizetype,
2244
                                          BINFO_OFFSET (virtual_offset)));
2245
                }
2246
              if (fixed_offset)
2247
                /* There was an existing fixed offset, this must be
2248
                   from the base just converted to, and the base the
2249
                   FN was thunking to.  */
2250
                fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2251
              else
2252
                fixed_offset = offset;
2253
            }
2254
        }
2255
 
2256
      if (fixed_offset || virtual_offset)
2257
        /* Replace the overriding function with a covariant thunk.  We
2258
           will emit the overriding function in its own slot as
2259
           well.  */
2260
        overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2261
                                   fixed_offset, virtual_offset);
2262
    }
2263
  else
2264
    gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2265
                !DECL_THUNK_P (fn));
2266
 
2267
  /* If we need a covariant thunk, then we may need to adjust first_defn.
2268
     The ABI specifies that the thunks emitted with a function are
2269
     determined by which bases the function overrides, so we need to be
2270
     sure that we're using a thunk for some overridden base; even if we
2271
     know that the necessary this adjustment is zero, there may not be an
2272
     appropriate zero-this-adjusment thunk for us to use since thunks for
2273
     overriding virtual bases always use the vcall offset.
2274
 
2275
     Furthermore, just choosing any base that overrides this function isn't
2276
     quite right, as this slot won't be used for calls through a type that
2277
     puts a covariant thunk here.  Calling the function through such a type
2278
     will use a different slot, and that slot is the one that determines
2279
     the thunk emitted for that base.
2280
 
2281
     So, keep looking until we find the base that we're really overriding
2282
     in this slot: the nearest primary base that doesn't use a covariant
2283
     thunk in this slot.  */
2284
  if (overrider_target != overrider_fn)
2285
    {
2286
      if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2287
        /* We already know that the overrider needs a covariant thunk.  */
2288
        b = get_primary_binfo (b);
2289
      for (; ; b = get_primary_binfo (b))
2290
        {
2291
          tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2292
          tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2293
          if (!DECL_THUNK_P (TREE_VALUE (bv)))
2294
            break;
2295
          if (BINFO_LOST_PRIMARY_P (b))
2296
            lost = true;
2297
        }
2298
      first_defn = b;
2299
    }
2300
 
2301
  /* Assume that we will produce a thunk that convert all the way to
2302
     the final overrider, and not to an intermediate virtual base.  */
2303
  virtual_base = NULL_TREE;
2304
 
2305
  /* See if we can convert to an intermediate virtual base first, and then
2306
     use the vcall offset located there to finish the conversion.  */
2307
  for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2308
    {
2309
      /* If we find the final overrider, then we can stop
2310
         walking.  */
2311
      if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2312
                             BINFO_TYPE (TREE_VALUE (overrider))))
2313
        break;
2314
 
2315
      /* If we find a virtual base, and we haven't yet found the
2316
         overrider, then there is a virtual base between the
2317
         declaring base (first_defn) and the final overrider.  */
2318
      if (BINFO_VIRTUAL_P (b))
2319
        {
2320
          virtual_base = b;
2321
          break;
2322
        }
2323
    }
2324
 
2325
  /* Compute the constant adjustment to the `this' pointer.  The
2326
     `this' pointer, when this function is called, will point at BINFO
2327
     (or one of its primary bases, which are at the same offset).  */
2328
  if (virtual_base)
2329
    /* The `this' pointer needs to be adjusted from the declaration to
2330
       the nearest virtual base.  */
2331
    delta = size_diffop_loc (input_location,
2332
                         convert (ssizetype, BINFO_OFFSET (virtual_base)),
2333
                         convert (ssizetype, BINFO_OFFSET (first_defn)));
2334
  else if (lost)
2335
    /* If the nearest definition is in a lost primary, we don't need an
2336
       entry in our vtable.  Except possibly in a constructor vtable,
2337
       if we happen to get our primary back.  In that case, the offset
2338
       will be zero, as it will be a primary base.  */
2339
    delta = size_zero_node;
2340
  else
2341
    /* The `this' pointer needs to be adjusted from pointing to
2342
       BINFO to pointing at the base where the final overrider
2343
       appears.  */
2344
    delta = size_diffop_loc (input_location,
2345
                         convert (ssizetype,
2346
                                  BINFO_OFFSET (TREE_VALUE (overrider))),
2347
                         convert (ssizetype, BINFO_OFFSET (binfo)));
2348
 
2349
  modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2350
 
2351
  if (virtual_base)
2352
    BV_VCALL_INDEX (*virtuals)
2353
      = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2354
  else
2355
    BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2356
 
2357
  BV_LOST_PRIMARY (*virtuals) = lost;
2358
}
2359
 
2360
/* Called from modify_all_vtables via dfs_walk.  */
2361
 
2362
static tree
2363
dfs_modify_vtables (tree binfo, void* data)
2364
{
2365
  tree t = (tree) data;
2366
  tree virtuals;
2367
  tree old_virtuals;
2368
  unsigned ix;
2369
 
2370
  if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2371
    /* A base without a vtable needs no modification, and its bases
2372
       are uninteresting.  */
2373
    return dfs_skip_bases;
2374
 
2375
  if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2376
      && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2377
    /* Don't do the primary vtable, if it's new.  */
2378
    return NULL_TREE;
2379
 
2380
  if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2381
    /* There's no need to modify the vtable for a non-virtual primary
2382
       base; we're not going to use that vtable anyhow.  We do still
2383
       need to do this for virtual primary bases, as they could become
2384
       non-primary in a construction vtable.  */
2385
    return NULL_TREE;
2386
 
2387
  make_new_vtable (t, binfo);
2388
 
2389
  /* Now, go through each of the virtual functions in the virtual
2390
     function table for BINFO.  Find the final overrider, and update
2391
     the BINFO_VIRTUALS list appropriately.  */
2392
  for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2393
         old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2394
       virtuals;
2395
       ix++, virtuals = TREE_CHAIN (virtuals),
2396
         old_virtuals = TREE_CHAIN (old_virtuals))
2397
    update_vtable_entry_for_fn (t,
2398
                                binfo,
2399
                                BV_FN (old_virtuals),
2400
                                &virtuals, ix);
2401
 
2402
  return NULL_TREE;
2403
}
2404
 
2405
/* Update all of the primary and secondary vtables for T.  Create new
2406
   vtables as required, and initialize their RTTI information.  Each
2407
   of the functions in VIRTUALS is declared in T and may override a
2408
   virtual function from a base class; find and modify the appropriate
2409
   entries to point to the overriding functions.  Returns a list, in
2410
   declaration order, of the virtual functions that are declared in T,
2411
   but do not appear in the primary base class vtable, and which
2412
   should therefore be appended to the end of the vtable for T.  */
2413
 
2414
static tree
2415
modify_all_vtables (tree t, tree virtuals)
2416
{
2417
  tree binfo = TYPE_BINFO (t);
2418
  tree *fnsp;
2419
 
2420
  /* Update all of the vtables.  */
2421
  dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2422
 
2423
  /* Add virtual functions not already in our primary vtable. These
2424
     will be both those introduced by this class, and those overridden
2425
     from secondary bases.  It does not include virtuals merely
2426
     inherited from secondary bases.  */
2427
  for (fnsp = &virtuals; *fnsp; )
2428
    {
2429
      tree fn = TREE_VALUE (*fnsp);
2430
 
2431
      if (!value_member (fn, BINFO_VIRTUALS (binfo))
2432
          || DECL_VINDEX (fn) == error_mark_node)
2433
        {
2434
          /* We don't need to adjust the `this' pointer when
2435
             calling this function.  */
2436
          BV_DELTA (*fnsp) = integer_zero_node;
2437
          BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2438
 
2439
          /* This is a function not already in our vtable.  Keep it.  */
2440
          fnsp = &TREE_CHAIN (*fnsp);
2441
        }
2442
      else
2443
        /* We've already got an entry for this function.  Skip it.  */
2444
        *fnsp = TREE_CHAIN (*fnsp);
2445
    }
2446
 
2447
  return virtuals;
2448
}
2449
 
2450
/* Get the base virtual function declarations in T that have the
2451
   indicated NAME.  */
2452
 
2453
static tree
2454
get_basefndecls (tree name, tree t)
2455
{
2456
  tree methods;
2457
  tree base_fndecls = NULL_TREE;
2458
  int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2459
  int i;
2460
 
2461
  /* Find virtual functions in T with the indicated NAME.  */
2462
  i = lookup_fnfields_1 (t, name);
2463
  if (i != -1)
2464
    for (methods = VEC_index (tree, CLASSTYPE_METHOD_VEC (t), i);
2465
         methods;
2466
         methods = OVL_NEXT (methods))
2467
      {
2468
        tree method = OVL_CURRENT (methods);
2469
 
2470
        if (TREE_CODE (method) == FUNCTION_DECL
2471
            && DECL_VINDEX (method))
2472
          base_fndecls = tree_cons (NULL_TREE, method, base_fndecls);
2473
      }
2474
 
2475
  if (base_fndecls)
2476
    return base_fndecls;
2477
 
2478
  for (i = 0; i < n_baseclasses; i++)
2479
    {
2480
      tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2481
      base_fndecls = chainon (get_basefndecls (name, basetype),
2482
                              base_fndecls);
2483
    }
2484
 
2485
  return base_fndecls;
2486
}
2487
 
2488
/* If this declaration supersedes the declaration of
2489
   a method declared virtual in the base class, then
2490
   mark this field as being virtual as well.  */
2491
 
2492
void
2493
check_for_override (tree decl, tree ctype)
2494
{
2495
  bool overrides_found = false;
2496
  if (TREE_CODE (decl) == TEMPLATE_DECL)
2497
    /* In [temp.mem] we have:
2498
 
2499
         A specialization of a member function template does not
2500
         override a virtual function from a base class.  */
2501
    return;
2502
  if ((DECL_DESTRUCTOR_P (decl)
2503
       || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2504
       || DECL_CONV_FN_P (decl))
2505
      && look_for_overrides (ctype, decl)
2506
      && !DECL_STATIC_FUNCTION_P (decl))
2507
    /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2508
       the error_mark_node so that we know it is an overriding
2509
       function.  */
2510
    {
2511
      DECL_VINDEX (decl) = decl;
2512
      overrides_found = true;
2513
    }
2514
 
2515
  if (DECL_VIRTUAL_P (decl))
2516
    {
2517
      if (!DECL_VINDEX (decl))
2518
        DECL_VINDEX (decl) = error_mark_node;
2519
      IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2520
      if (DECL_DESTRUCTOR_P (decl))
2521
        TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
2522
    }
2523
  else if (DECL_FINAL_P (decl))
2524
    error ("%q+#D marked final, but is not virtual", decl);
2525
  if (DECL_OVERRIDE_P (decl) && !overrides_found)
2526
    error ("%q+#D marked override, but does not override", decl);
2527
}
2528
 
2529
/* Warn about hidden virtual functions that are not overridden in t.
2530
   We know that constructors and destructors don't apply.  */
2531
 
2532
static void
2533
warn_hidden (tree t)
2534
{
2535
  VEC(tree,gc) *method_vec = CLASSTYPE_METHOD_VEC (t);
2536
  tree fns;
2537
  size_t i;
2538
 
2539
  /* We go through each separately named virtual function.  */
2540
  for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
2541
       VEC_iterate (tree, method_vec, i, fns);
2542
       ++i)
2543
    {
2544
      tree fn;
2545
      tree name;
2546
      tree fndecl;
2547
      tree base_fndecls;
2548
      tree base_binfo;
2549
      tree binfo;
2550
      int j;
2551
 
2552
      /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
2553
         have the same name.  Figure out what name that is.  */
2554
      name = DECL_NAME (OVL_CURRENT (fns));
2555
      /* There are no possibly hidden functions yet.  */
2556
      base_fndecls = NULL_TREE;
2557
      /* Iterate through all of the base classes looking for possibly
2558
         hidden functions.  */
2559
      for (binfo = TYPE_BINFO (t), j = 0;
2560
           BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
2561
        {
2562
          tree basetype = BINFO_TYPE (base_binfo);
2563
          base_fndecls = chainon (get_basefndecls (name, basetype),
2564
                                  base_fndecls);
2565
        }
2566
 
2567
      /* If there are no functions to hide, continue.  */
2568
      if (!base_fndecls)
2569
        continue;
2570
 
2571
      /* Remove any overridden functions.  */
2572
      for (fn = fns; fn; fn = OVL_NEXT (fn))
2573
        {
2574
          fndecl = OVL_CURRENT (fn);
2575
          if (DECL_VINDEX (fndecl))
2576
            {
2577
              tree *prev = &base_fndecls;
2578
 
2579
              while (*prev)
2580
                /* If the method from the base class has the same
2581
                   signature as the method from the derived class, it
2582
                   has been overridden.  */
2583
                if (same_signature_p (fndecl, TREE_VALUE (*prev)))
2584
                  *prev = TREE_CHAIN (*prev);
2585
                else
2586
                  prev = &TREE_CHAIN (*prev);
2587
            }
2588
        }
2589
 
2590
      /* Now give a warning for all base functions without overriders,
2591
         as they are hidden.  */
2592
      while (base_fndecls)
2593
        {
2594
          /* Here we know it is a hider, and no overrider exists.  */
2595
          warning (OPT_Woverloaded_virtual, "%q+D was hidden", TREE_VALUE (base_fndecls));
2596
          warning (OPT_Woverloaded_virtual, "  by %q+D", fns);
2597
          base_fndecls = TREE_CHAIN (base_fndecls);
2598
        }
2599
    }
2600
}
2601
 
2602
/* Check for things that are invalid.  There are probably plenty of other
2603
   things we should check for also.  */
2604
 
2605
static void
2606
finish_struct_anon (tree t)
2607
{
2608
  tree field;
2609
 
2610
  for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2611
    {
2612
      if (TREE_STATIC (field))
2613
        continue;
2614
      if (TREE_CODE (field) != FIELD_DECL)
2615
        continue;
2616
 
2617
      if (DECL_NAME (field) == NULL_TREE
2618
          && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2619
        {
2620
          bool is_union = TREE_CODE (TREE_TYPE (field)) == UNION_TYPE;
2621
          tree elt = TYPE_FIELDS (TREE_TYPE (field));
2622
          for (; elt; elt = DECL_CHAIN (elt))
2623
            {
2624
              /* We're generally only interested in entities the user
2625
                 declared, but we also find nested classes by noticing
2626
                 the TYPE_DECL that we create implicitly.  You're
2627
                 allowed to put one anonymous union inside another,
2628
                 though, so we explicitly tolerate that.  We use
2629
                 TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
2630
                 we also allow unnamed types used for defining fields.  */
2631
              if (DECL_ARTIFICIAL (elt)
2632
                  && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2633
                      || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
2634
                continue;
2635
 
2636
              if (TREE_CODE (elt) != FIELD_DECL)
2637
                {
2638
                  if (is_union)
2639
                    permerror (input_location, "%q+#D invalid; an anonymous union can "
2640
                               "only have non-static data members", elt);
2641
                  else
2642
                    permerror (input_location, "%q+#D invalid; an anonymous struct can "
2643
                               "only have non-static data members", elt);
2644
                  continue;
2645
                }
2646
 
2647
              if (TREE_PRIVATE (elt))
2648
                {
2649
                  if (is_union)
2650
                    permerror (input_location, "private member %q+#D in anonymous union", elt);
2651
                  else
2652
                    permerror (input_location, "private member %q+#D in anonymous struct", elt);
2653
                }
2654
              else if (TREE_PROTECTED (elt))
2655
                {
2656
                  if (is_union)
2657
                    permerror (input_location, "protected member %q+#D in anonymous union", elt);
2658
                  else
2659
                    permerror (input_location, "protected member %q+#D in anonymous struct", elt);
2660
                }
2661
 
2662
              TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2663
              TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2664
            }
2665
        }
2666
    }
2667
}
2668
 
2669
/* Add T to CLASSTYPE_DECL_LIST of current_class_type which
2670
   will be used later during class template instantiation.
2671
   When FRIEND_P is zero, T can be a static member data (VAR_DECL),
2672
   a non-static member data (FIELD_DECL), a member function
2673
   (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
2674
   a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
2675
   When FRIEND_P is nonzero, T is either a friend class
2676
   (RECORD_TYPE, TEMPLATE_DECL) or a friend function
2677
   (FUNCTION_DECL, TEMPLATE_DECL).  */
2678
 
2679
void
2680
maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
2681
{
2682
  /* Save some memory by not creating TREE_LIST if TYPE is not template.  */
2683
  if (CLASSTYPE_TEMPLATE_INFO (type))
2684
    CLASSTYPE_DECL_LIST (type)
2685
      = tree_cons (friend_p ? NULL_TREE : type,
2686
                   t, CLASSTYPE_DECL_LIST (type));
2687
}
2688
 
2689
/* This function is called from declare_virt_assop_and_dtor via
2690
   dfs_walk_all.
2691
 
2692
   DATA is a type that direcly or indirectly inherits the base
2693
   represented by BINFO.  If BINFO contains a virtual assignment [copy
2694
   assignment or move assigment] operator or a virtual constructor,
2695
   declare that function in DATA if it hasn't been already declared.  */
2696
 
2697
static tree
2698
dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
2699
{
2700
  tree bv, fn, t = (tree)data;
2701
  tree opname = ansi_assopname (NOP_EXPR);
2702
 
2703
  gcc_assert (t && CLASS_TYPE_P (t));
2704
  gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
2705
 
2706
  if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2707
    /* A base without a vtable needs no modification, and its bases
2708
       are uninteresting.  */
2709
    return dfs_skip_bases;
2710
 
2711
  if (BINFO_PRIMARY_P (binfo))
2712
    /* If this is a primary base, then we have already looked at the
2713
       virtual functions of its vtable.  */
2714
    return NULL_TREE;
2715
 
2716
  for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
2717
    {
2718
      fn = BV_FN (bv);
2719
 
2720
      if (DECL_NAME (fn) == opname)
2721
        {
2722
          if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
2723
            lazily_declare_fn (sfk_copy_assignment, t);
2724
          if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
2725
            lazily_declare_fn (sfk_move_assignment, t);
2726
        }
2727
      else if (DECL_DESTRUCTOR_P (fn)
2728
               && CLASSTYPE_LAZY_DESTRUCTOR (t))
2729
        lazily_declare_fn (sfk_destructor, t);
2730
    }
2731
 
2732
  return NULL_TREE;
2733
}
2734
 
2735
/* If the class type T has a direct or indirect base that contains a
2736
   virtual assignment operator or a virtual destructor, declare that
2737
   function in T if it hasn't been already declared.  */
2738
 
2739
static void
2740
declare_virt_assop_and_dtor (tree t)
2741
{
2742
  if (!(TYPE_POLYMORPHIC_P (t)
2743
        && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
2744
            || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
2745
            || CLASSTYPE_LAZY_DESTRUCTOR (t))))
2746
    return;
2747
 
2748
  dfs_walk_all (TYPE_BINFO (t),
2749
                dfs_declare_virt_assop_and_dtor,
2750
                NULL, t);
2751
}
2752
 
2753
/* Create default constructors, assignment operators, and so forth for
2754
   the type indicated by T, if they are needed.  CANT_HAVE_CONST_CTOR,
2755
   and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
2756
   the class cannot have a default constructor, copy constructor
2757
   taking a const reference argument, or an assignment operator taking
2758
   a const reference, respectively.  */
2759
 
2760
static void
2761
add_implicitly_declared_members (tree t,
2762
                                 int cant_have_const_cctor,
2763
                                 int cant_have_const_assignment)
2764
{
2765
  bool move_ok = false;
2766
 
2767
  if (cxx_dialect >= cxx0x && !CLASSTYPE_DESTRUCTORS (t)
2768
      && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
2769
      && !type_has_move_constructor (t) && !type_has_move_assign (t))
2770
    move_ok = true;
2771
 
2772
  /* Destructor.  */
2773
  if (!CLASSTYPE_DESTRUCTORS (t))
2774
    {
2775
      /* In general, we create destructors lazily.  */
2776
      CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
2777
 
2778
      if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
2779
          && TYPE_FOR_JAVA (t))
2780
        /* But if this is a Java class, any non-trivial destructor is
2781
           invalid, even if compiler-generated.  Therefore, if the
2782
           destructor is non-trivial we create it now.  */
2783
        lazily_declare_fn (sfk_destructor, t);
2784
    }
2785
 
2786
  /* [class.ctor]
2787
 
2788
     If there is no user-declared constructor for a class, a default
2789
     constructor is implicitly declared.  */
2790
  if (! TYPE_HAS_USER_CONSTRUCTOR (t))
2791
    {
2792
      TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
2793
      CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
2794
      if (cxx_dialect >= cxx0x)
2795
        TYPE_HAS_CONSTEXPR_CTOR (t)
2796
          /* This might force the declaration.  */
2797
          = type_has_constexpr_default_constructor (t);
2798
    }
2799
 
2800
  /* [class.ctor]
2801
 
2802
     If a class definition does not explicitly declare a copy
2803
     constructor, one is declared implicitly.  */
2804
  if (! TYPE_HAS_COPY_CTOR (t) && ! TYPE_FOR_JAVA (t))
2805
    {
2806
      TYPE_HAS_COPY_CTOR (t) = 1;
2807
      TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
2808
      CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
2809
      if (move_ok)
2810
        CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
2811
    }
2812
 
2813
  /* If there is no assignment operator, one will be created if and
2814
     when it is needed.  For now, just record whether or not the type
2815
     of the parameter to the assignment operator will be a const or
2816
     non-const reference.  */
2817
  if (!TYPE_HAS_COPY_ASSIGN (t) && !TYPE_FOR_JAVA (t))
2818
    {
2819
      TYPE_HAS_COPY_ASSIGN (t) = 1;
2820
      TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
2821
      CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
2822
      if (move_ok)
2823
        CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
2824
    }
2825
 
2826
  /* We can't be lazy about declaring functions that might override
2827
     a virtual function from a base class.  */
2828
  declare_virt_assop_and_dtor (t);
2829
}
2830
 
2831
/* Subroutine of finish_struct_1.  Recursively count the number of fields
2832
   in TYPE, including anonymous union members.  */
2833
 
2834
static int
2835
count_fields (tree fields)
2836
{
2837
  tree x;
2838
  int n_fields = 0;
2839
  for (x = fields; x; x = DECL_CHAIN (x))
2840
    {
2841
      if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2842
        n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
2843
      else
2844
        n_fields += 1;
2845
    }
2846
  return n_fields;
2847
}
2848
 
2849
/* Subroutine of finish_struct_1.  Recursively add all the fields in the
2850
   TREE_LIST FIELDS to the SORTED_FIELDS_TYPE elts, starting at offset IDX.  */
2851
 
2852
static int
2853
add_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, int idx)
2854
{
2855
  tree x;
2856
  for (x = fields; x; x = DECL_CHAIN (x))
2857
    {
2858
      if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2859
        idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
2860
      else
2861
        field_vec->elts[idx++] = x;
2862
    }
2863
  return idx;
2864
}
2865
 
2866
/* FIELD is a bit-field.  We are finishing the processing for its
2867
   enclosing type.  Issue any appropriate messages and set appropriate
2868
   flags.  Returns false if an error has been diagnosed.  */
2869
 
2870
static bool
2871
check_bitfield_decl (tree field)
2872
{
2873
  tree type = TREE_TYPE (field);
2874
  tree w;
2875
 
2876
  /* Extract the declared width of the bitfield, which has been
2877
     temporarily stashed in DECL_INITIAL.  */
2878
  w = DECL_INITIAL (field);
2879
  gcc_assert (w != NULL_TREE);
2880
  /* Remove the bit-field width indicator so that the rest of the
2881
     compiler does not treat that value as an initializer.  */
2882
  DECL_INITIAL (field) = NULL_TREE;
2883
 
2884
  /* Detect invalid bit-field type.  */
2885
  if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
2886
    {
2887
      error ("bit-field %q+#D with non-integral type", field);
2888
      w = error_mark_node;
2889
    }
2890
  else
2891
    {
2892
      location_t loc = input_location;
2893
      /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs.  */
2894
      STRIP_NOPS (w);
2895
 
2896
      /* detect invalid field size.  */
2897
      input_location = DECL_SOURCE_LOCATION (field);
2898
      w = cxx_constant_value (w);
2899
      input_location = loc;
2900
 
2901
      if (TREE_CODE (w) != INTEGER_CST)
2902
        {
2903
          error ("bit-field %q+D width not an integer constant", field);
2904
          w = error_mark_node;
2905
        }
2906
      else if (tree_int_cst_sgn (w) < 0)
2907
        {
2908
          error ("negative width in bit-field %q+D", field);
2909
          w = error_mark_node;
2910
        }
2911
      else if (integer_zerop (w) && DECL_NAME (field) != 0)
2912
        {
2913
          error ("zero width for bit-field %q+D", field);
2914
          w = error_mark_node;
2915
        }
2916
      else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
2917
               && TREE_CODE (type) != ENUMERAL_TYPE
2918
               && TREE_CODE (type) != BOOLEAN_TYPE)
2919
        warning (0, "width of %q+D exceeds its type", field);
2920
      else if (TREE_CODE (type) == ENUMERAL_TYPE
2921
               && (0 > (compare_tree_int
2922
                        (w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type))))))
2923
        warning (0, "%q+D is too small to hold all values of %q#T", field, type);
2924
    }
2925
 
2926
  if (w != error_mark_node)
2927
    {
2928
      DECL_SIZE (field) = convert (bitsizetype, w);
2929
      DECL_BIT_FIELD (field) = 1;
2930
      return true;
2931
    }
2932
  else
2933
    {
2934
      /* Non-bit-fields are aligned for their type.  */
2935
      DECL_BIT_FIELD (field) = 0;
2936
      CLEAR_DECL_C_BIT_FIELD (field);
2937
      return false;
2938
    }
2939
}
2940
 
2941
/* FIELD is a non bit-field.  We are finishing the processing for its
2942
   enclosing type T.  Issue any appropriate messages and set appropriate
2943
   flags.  */
2944
 
2945
static void
2946
check_field_decl (tree field,
2947
                  tree t,
2948
                  int* cant_have_const_ctor,
2949
                  int* no_const_asn_ref,
2950
                  int* any_default_members)
2951
{
2952
  tree type = strip_array_types (TREE_TYPE (field));
2953
 
2954
  /* In C++98 an anonymous union cannot contain any fields which would change
2955
     the settings of CANT_HAVE_CONST_CTOR and friends.  */
2956
  if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx0x)
2957
    ;
2958
  /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
2959
     structs.  So, we recurse through their fields here.  */
2960
  else if (ANON_AGGR_TYPE_P (type))
2961
    {
2962
      tree fields;
2963
 
2964
      for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
2965
        if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
2966
          check_field_decl (fields, t, cant_have_const_ctor,
2967
                            no_const_asn_ref, any_default_members);
2968
    }
2969
  /* Check members with class type for constructors, destructors,
2970
     etc.  */
2971
  else if (CLASS_TYPE_P (type))
2972
    {
2973
      /* Never let anything with uninheritable virtuals
2974
         make it through without complaint.  */
2975
      abstract_virtuals_error (field, type);
2976
 
2977
      if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx0x)
2978
        {
2979
          static bool warned;
2980
          int oldcount = errorcount;
2981
          if (TYPE_NEEDS_CONSTRUCTING (type))
2982
            error ("member %q+#D with constructor not allowed in union",
2983
                   field);
2984
          if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
2985
            error ("member %q+#D with destructor not allowed in union", field);
2986
          if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
2987
            error ("member %q+#D with copy assignment operator not allowed in union",
2988
                   field);
2989
          if (!warned && errorcount > oldcount)
2990
            {
2991
              inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
2992
                      "only available with -std=c++11 or -std=gnu++11");
2993
              warned = true;
2994
            }
2995
        }
2996
      else
2997
        {
2998
          TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
2999
          TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3000
            |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3001
          TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3002
            |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3003
                || !TYPE_HAS_COPY_ASSIGN (type));
3004
          TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3005
                                             || !TYPE_HAS_COPY_CTOR (type));
3006
          TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3007
          TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3008
          TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3009
                                        || TYPE_HAS_COMPLEX_DFLT (type));
3010
        }
3011
 
3012
      if (TYPE_HAS_COPY_CTOR (type)
3013
          && !TYPE_HAS_CONST_COPY_CTOR (type))
3014
        *cant_have_const_ctor = 1;
3015
 
3016
      if (TYPE_HAS_COPY_ASSIGN (type)
3017
          && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3018
        *no_const_asn_ref = 1;
3019
    }
3020
  if (DECL_INITIAL (field) != NULL_TREE)
3021
    {
3022
      /* `build_class_init_list' does not recognize
3023
         non-FIELD_DECLs.  */
3024
      if (TREE_CODE (t) == UNION_TYPE && *any_default_members != 0)
3025
        error ("multiple fields in union %qT initialized", t);
3026
      *any_default_members = 1;
3027
    }
3028
}
3029
 
3030
/* Check the data members (both static and non-static), class-scoped
3031
   typedefs, etc., appearing in the declaration of T.  Issue
3032
   appropriate diagnostics.  Sets ACCESS_DECLS to a list (in
3033
   declaration order) of access declarations; each TREE_VALUE in this
3034
   list is a USING_DECL.
3035
 
3036
   In addition, set the following flags:
3037
 
3038
     EMPTY_P
3039
       The class is empty, i.e., contains no non-static data members.
3040
 
3041
     CANT_HAVE_CONST_CTOR_P
3042
       This class cannot have an implicitly generated copy constructor
3043
       taking a const reference.
3044
 
3045
     CANT_HAVE_CONST_ASN_REF
3046
       This class cannot have an implicitly generated assignment
3047
       operator taking a const reference.
3048
 
3049
   All of these flags should be initialized before calling this
3050
   function.
3051
 
3052
   Returns a pointer to the end of the TYPE_FIELDs chain; additional
3053
   fields can be added by adding to this chain.  */
3054
 
3055
static void
3056
check_field_decls (tree t, tree *access_decls,
3057
                   int *cant_have_const_ctor_p,
3058
                   int *no_const_asn_ref_p)
3059
{
3060
  tree *field;
3061
  tree *next;
3062
  bool has_pointers;
3063
  int any_default_members;
3064
  int cant_pack = 0;
3065
  int field_access = -1;
3066
 
3067
  /* Assume there are no access declarations.  */
3068
  *access_decls = NULL_TREE;
3069
  /* Assume this class has no pointer members.  */
3070
  has_pointers = false;
3071
  /* Assume none of the members of this class have default
3072
     initializations.  */
3073
  any_default_members = 0;
3074
 
3075
  for (field = &TYPE_FIELDS (t); *field; field = next)
3076
    {
3077
      tree x = *field;
3078
      tree type = TREE_TYPE (x);
3079
      int this_field_access;
3080
 
3081
      next = &DECL_CHAIN (x);
3082
 
3083
      if (TREE_CODE (x) == USING_DECL)
3084
        {
3085
          /* Save the access declarations for our caller.  */
3086
          *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3087
          continue;
3088
        }
3089
 
3090
      if (TREE_CODE (x) == TYPE_DECL
3091
          || TREE_CODE (x) == TEMPLATE_DECL)
3092
        continue;
3093
 
3094
      /* If we've gotten this far, it's a data member, possibly static,
3095
         or an enumerator.  */
3096
      DECL_CONTEXT (x) = t;
3097
 
3098
      /* When this goes into scope, it will be a non-local reference.  */
3099
      DECL_NONLOCAL (x) = 1;
3100
 
3101
      if (TREE_CODE (t) == UNION_TYPE)
3102
        {
3103
          /* [class.union]
3104
 
3105
             If a union contains a static data member, or a member of
3106
             reference type, the program is ill-formed.  */
3107
          if (TREE_CODE (x) == VAR_DECL)
3108
            {
3109
              error ("%q+D may not be static because it is a member of a union", x);
3110
              continue;
3111
            }
3112
          if (TREE_CODE (type) == REFERENCE_TYPE)
3113
            {
3114
              error ("%q+D may not have reference type %qT because"
3115
                     " it is a member of a union",
3116
                     x, type);
3117
              continue;
3118
            }
3119
        }
3120
 
3121
      /* Perform error checking that did not get done in
3122
         grokdeclarator.  */
3123
      if (TREE_CODE (type) == FUNCTION_TYPE)
3124
        {
3125
          error ("field %q+D invalidly declared function type", x);
3126
          type = build_pointer_type (type);
3127
          TREE_TYPE (x) = type;
3128
        }
3129
      else if (TREE_CODE (type) == METHOD_TYPE)
3130
        {
3131
          error ("field %q+D invalidly declared method type", x);
3132
          type = build_pointer_type (type);
3133
          TREE_TYPE (x) = type;
3134
        }
3135
 
3136
      if (type == error_mark_node)
3137
        continue;
3138
 
3139
      if (TREE_CODE (x) == CONST_DECL || TREE_CODE (x) == VAR_DECL)
3140
        continue;
3141
 
3142
      /* Now it can only be a FIELD_DECL.  */
3143
 
3144
      if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3145
        CLASSTYPE_NON_AGGREGATE (t) = 1;
3146
 
3147
      /* If at least one non-static data member is non-literal, the whole
3148
         class becomes non-literal.  */
3149
      if (!literal_type_p (type))
3150
        CLASSTYPE_LITERAL_P (t) = false;
3151
 
3152
      /* A standard-layout class is a class that:
3153
         ...
3154
         has the same access control (Clause 11) for all non-static data members,
3155
         ...  */
3156
      this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3157
      if (field_access == -1)
3158
        field_access = this_field_access;
3159
      else if (this_field_access != field_access)
3160
        CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3161
 
3162
      /* If this is of reference type, check if it needs an init.  */
3163
      if (TREE_CODE (type) == REFERENCE_TYPE)
3164
        {
3165
          CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3166
          CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3167
          if (DECL_INITIAL (x) == NULL_TREE)
3168
            SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3169
 
3170
          /* ARM $12.6.2: [A member initializer list] (or, for an
3171
             aggregate, initialization by a brace-enclosed list) is the
3172
             only way to initialize nonstatic const and reference
3173
             members.  */
3174
          TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3175
          TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3176
        }
3177
 
3178
      type = strip_array_types (type);
3179
 
3180
      if (TYPE_PACKED (t))
3181
        {
3182
          if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3183
            {
3184
              warning
3185
                (0,
3186
                 "ignoring packed attribute because of unpacked non-POD field %q+#D",
3187
                 x);
3188
              cant_pack = 1;
3189
            }
3190
          else if (DECL_C_BIT_FIELD (x)
3191
                   || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3192
            DECL_PACKED (x) = 1;
3193
        }
3194
 
3195
      if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3196
        /* We don't treat zero-width bitfields as making a class
3197
           non-empty.  */
3198
        ;
3199
      else
3200
        {
3201
          /* The class is non-empty.  */
3202
          CLASSTYPE_EMPTY_P (t) = 0;
3203
          /* The class is not even nearly empty.  */
3204
          CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3205
          /* If one of the data members contains an empty class,
3206
             so does T.  */
3207
          if (CLASS_TYPE_P (type)
3208
              && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3209
            CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3210
        }
3211
 
3212
      /* This is used by -Weffc++ (see below). Warn only for pointers
3213
         to members which might hold dynamic memory. So do not warn
3214
         for pointers to functions or pointers to members.  */
3215
      if (TYPE_PTR_P (type)
3216
          && !TYPE_PTRFN_P (type)
3217
          && !TYPE_PTR_TO_MEMBER_P (type))
3218
        has_pointers = true;
3219
 
3220
      if (CLASS_TYPE_P (type))
3221
        {
3222
          if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3223
            SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3224
          if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3225
            SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3226
        }
3227
 
3228
      if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3229
        CLASSTYPE_HAS_MUTABLE (t) = 1;
3230
 
3231
      if (! layout_pod_type_p (type))
3232
        /* DR 148 now allows pointers to members (which are POD themselves),
3233
           to be allowed in POD structs.  */
3234
        CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3235
 
3236
      if (!std_layout_type_p (type))
3237
        CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3238
 
3239
      if (! zero_init_p (type))
3240
        CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3241
 
3242
      /* We set DECL_C_BIT_FIELD in grokbitfield.
3243
         If the type and width are valid, we'll also set DECL_BIT_FIELD.  */
3244
      if (! DECL_C_BIT_FIELD (x) || ! check_bitfield_decl (x))
3245
        check_field_decl (x, t,
3246
                          cant_have_const_ctor_p,
3247
                          no_const_asn_ref_p,
3248
                          &any_default_members);
3249
 
3250
      /* Now that we've removed bit-field widths from DECL_INITIAL,
3251
         anything left in DECL_INITIAL is an NSDMI that makes the class
3252
         non-aggregate.  */
3253
      if (DECL_INITIAL (x))
3254
        CLASSTYPE_NON_AGGREGATE (t) = true;
3255
 
3256
      /* If any field is const, the structure type is pseudo-const.  */
3257
      if (CP_TYPE_CONST_P (type))
3258
        {
3259
          C_TYPE_FIELDS_READONLY (t) = 1;
3260
          if (DECL_INITIAL (x) == NULL_TREE)
3261
            SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3262
 
3263
          /* ARM $12.6.2: [A member initializer list] (or, for an
3264
             aggregate, initialization by a brace-enclosed list) is the
3265
             only way to initialize nonstatic const and reference
3266
             members.  */
3267
          TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3268
          TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3269
        }
3270
      /* A field that is pseudo-const makes the structure likewise.  */
3271
      else if (CLASS_TYPE_P (type))
3272
        {
3273
          C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3274
          SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3275
            CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3276
            | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3277
        }
3278
 
3279
      /* Core issue 80: A nonstatic data member is required to have a
3280
         different name from the class iff the class has a
3281
         user-declared constructor.  */
3282
      if (constructor_name_p (DECL_NAME (x), t)
3283
          && TYPE_HAS_USER_CONSTRUCTOR (t))
3284
        permerror (input_location, "field %q+#D with same name as class", x);
3285
    }
3286
 
3287
  /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3288
     it should also define a copy constructor and an assignment operator to
3289
     implement the correct copy semantic (deep vs shallow, etc.). As it is
3290
     not feasible to check whether the constructors do allocate dynamic memory
3291
     and store it within members, we approximate the warning like this:
3292
 
3293
     -- Warn only if there are members which are pointers
3294
     -- Warn only if there is a non-trivial constructor (otherwise,
3295
        there cannot be memory allocated).
3296
     -- Warn only if there is a non-trivial destructor. We assume that the
3297
        user at least implemented the cleanup correctly, and a destructor
3298
        is needed to free dynamic memory.
3299
 
3300
     This seems enough for practical purposes.  */
3301
  if (warn_ecpp
3302
      && has_pointers
3303
      && TYPE_HAS_USER_CONSTRUCTOR (t)
3304
      && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3305
      && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3306
    {
3307
      warning (OPT_Weffc__, "%q#T has pointer data members", t);
3308
 
3309
      if (! TYPE_HAS_COPY_CTOR (t))
3310
        {
3311
          warning (OPT_Weffc__,
3312
                   "  but does not override %<%T(const %T&)%>", t, t);
3313
          if (!TYPE_HAS_COPY_ASSIGN (t))
3314
            warning (OPT_Weffc__, "  or %<operator=(const %T&)%>", t);
3315
        }
3316
      else if (! TYPE_HAS_COPY_ASSIGN (t))
3317
        warning (OPT_Weffc__,
3318
                 "  but does not override %<operator=(const %T&)%>", t);
3319
    }
3320
 
3321
  /* Non-static data member initializers make the default constructor
3322
     non-trivial.  */
3323
  if (any_default_members)
3324
    {
3325
      TYPE_NEEDS_CONSTRUCTING (t) = true;
3326
      TYPE_HAS_COMPLEX_DFLT (t) = true;
3327
    }
3328
 
3329
  /* If any of the fields couldn't be packed, unset TYPE_PACKED.  */
3330
  if (cant_pack)
3331
    TYPE_PACKED (t) = 0;
3332
 
3333
  /* Check anonymous struct/anonymous union fields.  */
3334
  finish_struct_anon (t);
3335
 
3336
  /* We've built up the list of access declarations in reverse order.
3337
     Fix that now.  */
3338
  *access_decls = nreverse (*access_decls);
3339
}
3340
 
3341
/* If TYPE is an empty class type, records its OFFSET in the table of
3342
   OFFSETS.  */
3343
 
3344
static int
3345
record_subobject_offset (tree type, tree offset, splay_tree offsets)
3346
{
3347
  splay_tree_node n;
3348
 
3349
  if (!is_empty_class (type))
3350
    return 0;
3351
 
3352
  /* Record the location of this empty object in OFFSETS.  */
3353
  n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3354
  if (!n)
3355
    n = splay_tree_insert (offsets,
3356
                           (splay_tree_key) offset,
3357
                           (splay_tree_value) NULL_TREE);
3358
  n->value = ((splay_tree_value)
3359
              tree_cons (NULL_TREE,
3360
                         type,
3361
                         (tree) n->value));
3362
 
3363
  return 0;
3364
}
3365
 
3366
/* Returns nonzero if TYPE is an empty class type and there is
3367
   already an entry in OFFSETS for the same TYPE as the same OFFSET.  */
3368
 
3369
static int
3370
check_subobject_offset (tree type, tree offset, splay_tree offsets)
3371
{
3372
  splay_tree_node n;
3373
  tree t;
3374
 
3375
  if (!is_empty_class (type))
3376
    return 0;
3377
 
3378
  /* Record the location of this empty object in OFFSETS.  */
3379
  n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3380
  if (!n)
3381
    return 0;
3382
 
3383
  for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3384
    if (same_type_p (TREE_VALUE (t), type))
3385
      return 1;
3386
 
3387
  return 0;
3388
}
3389
 
3390
/* Walk through all the subobjects of TYPE (located at OFFSET).  Call
3391
   F for every subobject, passing it the type, offset, and table of
3392
   OFFSETS.  If VBASES_P is one, then virtual non-primary bases should
3393
   be traversed.
3394
 
3395
   If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3396
   than MAX_OFFSET will not be walked.
3397
 
3398
   If F returns a nonzero value, the traversal ceases, and that value
3399
   is returned.  Otherwise, returns zero.  */
3400
 
3401
static int
3402
walk_subobject_offsets (tree type,
3403
                        subobject_offset_fn f,
3404
                        tree offset,
3405
                        splay_tree offsets,
3406
                        tree max_offset,
3407
                        int vbases_p)
3408
{
3409
  int r = 0;
3410
  tree type_binfo = NULL_TREE;
3411
 
3412
  /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3413
     stop.  */
3414
  if (max_offset && INT_CST_LT (max_offset, offset))
3415
    return 0;
3416
 
3417
  if (type == error_mark_node)
3418
    return 0;
3419
 
3420
  if (!TYPE_P (type))
3421
    {
3422
      if (abi_version_at_least (2))
3423
        type_binfo = type;
3424
      type = BINFO_TYPE (type);
3425
    }
3426
 
3427
  if (CLASS_TYPE_P (type))
3428
    {
3429
      tree field;
3430
      tree binfo;
3431
      int i;
3432
 
3433
      /* Avoid recursing into objects that are not interesting.  */
3434
      if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3435
        return 0;
3436
 
3437
      /* Record the location of TYPE.  */
3438
      r = (*f) (type, offset, offsets);
3439
      if (r)
3440
        return r;
3441
 
3442
      /* Iterate through the direct base classes of TYPE.  */
3443
      if (!type_binfo)
3444
        type_binfo = TYPE_BINFO (type);
3445
      for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
3446
        {
3447
          tree binfo_offset;
3448
 
3449
          if (abi_version_at_least (2)
3450
              && BINFO_VIRTUAL_P (binfo))
3451
            continue;
3452
 
3453
          if (!vbases_p
3454
              && BINFO_VIRTUAL_P (binfo)
3455
              && !BINFO_PRIMARY_P (binfo))
3456
            continue;
3457
 
3458
          if (!abi_version_at_least (2))
3459
            binfo_offset = size_binop (PLUS_EXPR,
3460
                                       offset,
3461
                                       BINFO_OFFSET (binfo));
3462
          else
3463
            {
3464
              tree orig_binfo;
3465
              /* We cannot rely on BINFO_OFFSET being set for the base
3466
                 class yet, but the offsets for direct non-virtual
3467
                 bases can be calculated by going back to the TYPE.  */
3468
              orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
3469
              binfo_offset = size_binop (PLUS_EXPR,
3470
                                         offset,
3471
                                         BINFO_OFFSET (orig_binfo));
3472
            }
3473
 
3474
          r = walk_subobject_offsets (binfo,
3475
                                      f,
3476
                                      binfo_offset,
3477
                                      offsets,
3478
                                      max_offset,
3479
                                      (abi_version_at_least (2)
3480
                                       ? /*vbases_p=*/0 : vbases_p));
3481
          if (r)
3482
            return r;
3483
        }
3484
 
3485
      if (abi_version_at_least (2) && CLASSTYPE_VBASECLASSES (type))
3486
        {
3487
          unsigned ix;
3488
          VEC(tree,gc) *vbases;
3489
 
3490
          /* Iterate through the virtual base classes of TYPE.  In G++
3491
             3.2, we included virtual bases in the direct base class
3492
             loop above, which results in incorrect results; the
3493
             correct offsets for virtual bases are only known when
3494
             working with the most derived type.  */
3495
          if (vbases_p)
3496
            for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
3497
                 VEC_iterate (tree, vbases, ix, binfo); ix++)
3498
              {
3499
                r = walk_subobject_offsets (binfo,
3500
                                            f,
3501
                                            size_binop (PLUS_EXPR,
3502
                                                        offset,
3503
                                                        BINFO_OFFSET (binfo)),
3504
                                            offsets,
3505
                                            max_offset,
3506
                                            /*vbases_p=*/0);
3507
                if (r)
3508
                  return r;
3509
              }
3510
          else
3511
            {
3512
              /* We still have to walk the primary base, if it is
3513
                 virtual.  (If it is non-virtual, then it was walked
3514
                 above.)  */
3515
              tree vbase = get_primary_binfo (type_binfo);
3516
 
3517
              if (vbase && BINFO_VIRTUAL_P (vbase)
3518
                  && BINFO_PRIMARY_P (vbase)
3519
                  && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
3520
                {
3521
                  r = (walk_subobject_offsets
3522
                       (vbase, f, offset,
3523
                        offsets, max_offset, /*vbases_p=*/0));
3524
                  if (r)
3525
                    return r;
3526
                }
3527
            }
3528
        }
3529
 
3530
      /* Iterate through the fields of TYPE.  */
3531
      for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3532
        if (TREE_CODE (field) == FIELD_DECL && !DECL_ARTIFICIAL (field))
3533
          {
3534
            tree field_offset;
3535
 
3536
            if (abi_version_at_least (2))
3537
              field_offset = byte_position (field);
3538
            else
3539
              /* In G++ 3.2, DECL_FIELD_OFFSET was used.  */
3540
              field_offset = DECL_FIELD_OFFSET (field);
3541
 
3542
            r = walk_subobject_offsets (TREE_TYPE (field),
3543
                                        f,
3544
                                        size_binop (PLUS_EXPR,
3545
                                                    offset,
3546
                                                    field_offset),
3547
                                        offsets,
3548
                                        max_offset,
3549
                                        /*vbases_p=*/1);
3550
            if (r)
3551
              return r;
3552
          }
3553
    }
3554
  else if (TREE_CODE (type) == ARRAY_TYPE)
3555
    {
3556
      tree element_type = strip_array_types (type);
3557
      tree domain = TYPE_DOMAIN (type);
3558
      tree index;
3559
 
3560
      /* Avoid recursing into objects that are not interesting.  */
3561
      if (!CLASS_TYPE_P (element_type)
3562
          || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
3563
        return 0;
3564
 
3565
      /* Step through each of the elements in the array.  */
3566
      for (index = size_zero_node;
3567
           /* G++ 3.2 had an off-by-one error here.  */
3568
           (abi_version_at_least (2)
3569
            ? !INT_CST_LT (TYPE_MAX_VALUE (domain), index)
3570
            : INT_CST_LT (index, TYPE_MAX_VALUE (domain)));
3571
           index = size_binop (PLUS_EXPR, index, size_one_node))
3572
        {
3573
          r = walk_subobject_offsets (TREE_TYPE (type),
3574
                                      f,
3575
                                      offset,
3576
                                      offsets,
3577
                                      max_offset,
3578
                                      /*vbases_p=*/1);
3579
          if (r)
3580
            return r;
3581
          offset = size_binop (PLUS_EXPR, offset,
3582
                               TYPE_SIZE_UNIT (TREE_TYPE (type)));
3583
          /* If this new OFFSET is bigger than the MAX_OFFSET, then
3584
             there's no point in iterating through the remaining
3585
             elements of the array.  */
3586
          if (max_offset && INT_CST_LT (max_offset, offset))
3587
            break;
3588
        }
3589
    }
3590
 
3591
  return 0;
3592
}
3593
 
3594
/* Record all of the empty subobjects of TYPE (either a type or a
3595
   binfo).  If IS_DATA_MEMBER is true, then a non-static data member
3596
   is being placed at OFFSET; otherwise, it is a base class that is
3597
   being placed at OFFSET.  */
3598
 
3599
static void
3600
record_subobject_offsets (tree type,
3601
                          tree offset,
3602
                          splay_tree offsets,
3603
                          bool is_data_member)
3604
{
3605
  tree max_offset;
3606
  /* If recording subobjects for a non-static data member or a
3607
     non-empty base class , we do not need to record offsets beyond
3608
     the size of the biggest empty class.  Additional data members
3609
     will go at the end of the class.  Additional base classes will go
3610
     either at offset zero (if empty, in which case they cannot
3611
     overlap with offsets past the size of the biggest empty class) or
3612
     at the end of the class.
3613
 
3614
     However, if we are placing an empty base class, then we must record
3615
     all offsets, as either the empty class is at offset zero (where
3616
     other empty classes might later be placed) or at the end of the
3617
     class (where other objects might then be placed, so other empty
3618
     subobjects might later overlap).  */
3619
  if (is_data_member
3620
      || !is_empty_class (BINFO_TYPE (type)))
3621
    max_offset = sizeof_biggest_empty_class;
3622
  else
3623
    max_offset = NULL_TREE;
3624
  walk_subobject_offsets (type, record_subobject_offset, offset,
3625
                          offsets, max_offset, is_data_member);
3626
}
3627
 
3628
/* Returns nonzero if any of the empty subobjects of TYPE (located at
3629
   OFFSET) conflict with entries in OFFSETS.  If VBASES_P is nonzero,
3630
   virtual bases of TYPE are examined.  */
3631
 
3632
static int
3633
layout_conflict_p (tree type,
3634
                   tree offset,
3635
                   splay_tree offsets,
3636
                   int vbases_p)
3637
{
3638
  splay_tree_node max_node;
3639
 
3640
  /* Get the node in OFFSETS that indicates the maximum offset where
3641
     an empty subobject is located.  */
3642
  max_node = splay_tree_max (offsets);
3643
  /* If there aren't any empty subobjects, then there's no point in
3644
     performing this check.  */
3645
  if (!max_node)
3646
    return 0;
3647
 
3648
  return walk_subobject_offsets (type, check_subobject_offset, offset,
3649
                                 offsets, (tree) (max_node->key),
3650
                                 vbases_p);
3651
}
3652
 
3653
/* DECL is a FIELD_DECL corresponding either to a base subobject of a
3654
   non-static data member of the type indicated by RLI.  BINFO is the
3655
   binfo corresponding to the base subobject, OFFSETS maps offsets to
3656
   types already located at those offsets.  This function determines
3657
   the position of the DECL.  */
3658
 
3659
static void
3660
layout_nonempty_base_or_field (record_layout_info rli,
3661
                               tree decl,
3662
                               tree binfo,
3663
                               splay_tree offsets)
3664
{
3665
  tree offset = NULL_TREE;
3666
  bool field_p;
3667
  tree type;
3668
 
3669
  if (binfo)
3670
    {
3671
      /* For the purposes of determining layout conflicts, we want to
3672
         use the class type of BINFO; TREE_TYPE (DECL) will be the
3673
         CLASSTYPE_AS_BASE version, which does not contain entries for
3674
         zero-sized bases.  */
3675
      type = TREE_TYPE (binfo);
3676
      field_p = false;
3677
    }
3678
  else
3679
    {
3680
      type = TREE_TYPE (decl);
3681
      field_p = true;
3682
    }
3683
 
3684
  /* Try to place the field.  It may take more than one try if we have
3685
     a hard time placing the field without putting two objects of the
3686
     same type at the same address.  */
3687
  while (1)
3688
    {
3689
      struct record_layout_info_s old_rli = *rli;
3690
 
3691
      /* Place this field.  */
3692
      place_field (rli, decl);
3693
      offset = byte_position (decl);
3694
 
3695
      /* We have to check to see whether or not there is already
3696
         something of the same type at the offset we're about to use.
3697
         For example, consider:
3698
 
3699
           struct S {};
3700
           struct T : public S { int i; };
3701
           struct U : public S, public T {};
3702
 
3703
         Here, we put S at offset zero in U.  Then, we can't put T at
3704
         offset zero -- its S component would be at the same address
3705
         as the S we already allocated.  So, we have to skip ahead.
3706
         Since all data members, including those whose type is an
3707
         empty class, have nonzero size, any overlap can happen only
3708
         with a direct or indirect base-class -- it can't happen with
3709
         a data member.  */
3710
      /* In a union, overlap is permitted; all members are placed at
3711
         offset zero.  */
3712
      if (TREE_CODE (rli->t) == UNION_TYPE)
3713
        break;
3714
      /* G++ 3.2 did not check for overlaps when placing a non-empty
3715
         virtual base.  */
3716
      if (!abi_version_at_least (2) && binfo && BINFO_VIRTUAL_P (binfo))
3717
        break;
3718
      if (layout_conflict_p (field_p ? type : binfo, offset,
3719
                             offsets, field_p))
3720
        {
3721
          /* Strip off the size allocated to this field.  That puts us
3722
             at the first place we could have put the field with
3723
             proper alignment.  */
3724
          *rli = old_rli;
3725
 
3726
          /* Bump up by the alignment required for the type.  */
3727
          rli->bitpos
3728
            = size_binop (PLUS_EXPR, rli->bitpos,
3729
                          bitsize_int (binfo
3730
                                       ? CLASSTYPE_ALIGN (type)
3731
                                       : TYPE_ALIGN (type)));
3732
          normalize_rli (rli);
3733
        }
3734
      else
3735
        /* There was no conflict.  We're done laying out this field.  */
3736
        break;
3737
    }
3738
 
3739
  /* Now that we know where it will be placed, update its
3740
     BINFO_OFFSET.  */
3741
  if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
3742
    /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
3743
       this point because their BINFO_OFFSET is copied from another
3744
       hierarchy.  Therefore, we may not need to add the entire
3745
       OFFSET.  */
3746
    propagate_binfo_offsets (binfo,
3747
                             size_diffop_loc (input_location,
3748
                                          convert (ssizetype, offset),
3749
                                          convert (ssizetype,
3750
                                                   BINFO_OFFSET (binfo))));
3751
}
3752
 
3753
/* Returns true if TYPE is empty and OFFSET is nonzero.  */
3754
 
3755
static int
3756
empty_base_at_nonzero_offset_p (tree type,
3757
                                tree offset,
3758
                                splay_tree offsets ATTRIBUTE_UNUSED)
3759
{
3760
  return is_empty_class (type) && !integer_zerop (offset);
3761
}
3762
 
3763
/* Layout the empty base BINFO.  EOC indicates the byte currently just
3764
   past the end of the class, and should be correctly aligned for a
3765
   class of the type indicated by BINFO; OFFSETS gives the offsets of
3766
   the empty bases allocated so far. T is the most derived
3767
   type.  Return nonzero iff we added it at the end.  */
3768
 
3769
static bool
3770
layout_empty_base (record_layout_info rli, tree binfo,
3771
                   tree eoc, splay_tree offsets)
3772
{
3773
  tree alignment;
3774
  tree basetype = BINFO_TYPE (binfo);
3775
  bool atend = false;
3776
 
3777
  /* This routine should only be used for empty classes.  */
3778
  gcc_assert (is_empty_class (basetype));
3779
  alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
3780
 
3781
  if (!integer_zerop (BINFO_OFFSET (binfo)))
3782
    {
3783
      if (abi_version_at_least (2))
3784
        propagate_binfo_offsets
3785
          (binfo, size_diffop_loc (input_location,
3786
                               size_zero_node, BINFO_OFFSET (binfo)));
3787
      else
3788
        warning (OPT_Wabi,
3789
                 "offset of empty base %qT may not be ABI-compliant and may"
3790
                 "change in a future version of GCC",
3791
                 BINFO_TYPE (binfo));
3792
    }
3793
 
3794
  /* This is an empty base class.  We first try to put it at offset
3795
     zero.  */
3796
  if (layout_conflict_p (binfo,
3797
                         BINFO_OFFSET (binfo),
3798
                         offsets,
3799
                         /*vbases_p=*/0))
3800
    {
3801
      /* That didn't work.  Now, we move forward from the next
3802
         available spot in the class.  */
3803
      atend = true;
3804
      propagate_binfo_offsets (binfo, convert (ssizetype, eoc));
3805
      while (1)
3806
        {
3807
          if (!layout_conflict_p (binfo,
3808
                                  BINFO_OFFSET (binfo),
3809
                                  offsets,
3810
                                  /*vbases_p=*/0))
3811
            /* We finally found a spot where there's no overlap.  */
3812
            break;
3813
 
3814
          /* There's overlap here, too.  Bump along to the next spot.  */
3815
          propagate_binfo_offsets (binfo, alignment);
3816
        }
3817
    }
3818
 
3819
  if (CLASSTYPE_USER_ALIGN (basetype))
3820
    {
3821
      rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
3822
      if (warn_packed)
3823
        rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
3824
      TYPE_USER_ALIGN (rli->t) = 1;
3825
    }
3826
 
3827
  return atend;
3828
}
3829
 
3830
/* Layout the base given by BINFO in the class indicated by RLI.
3831
   *BASE_ALIGN is a running maximum of the alignments of
3832
   any base class.  OFFSETS gives the location of empty base
3833
   subobjects.  T is the most derived type.  Return nonzero if the new
3834
   object cannot be nearly-empty.  A new FIELD_DECL is inserted at
3835
   *NEXT_FIELD, unless BINFO is for an empty base class.
3836
 
3837
   Returns the location at which the next field should be inserted.  */
3838
 
3839
static tree *
3840
build_base_field (record_layout_info rli, tree binfo,
3841
                  splay_tree offsets, tree *next_field)
3842
{
3843
  tree t = rli->t;
3844
  tree basetype = BINFO_TYPE (binfo);
3845
 
3846
  if (!COMPLETE_TYPE_P (basetype))
3847
    /* This error is now reported in xref_tag, thus giving better
3848
       location information.  */
3849
    return next_field;
3850
 
3851
  /* Place the base class.  */
3852
  if (!is_empty_class (basetype))
3853
    {
3854
      tree decl;
3855
 
3856
      /* The containing class is non-empty because it has a non-empty
3857
         base class.  */
3858
      CLASSTYPE_EMPTY_P (t) = 0;
3859
 
3860
      /* Create the FIELD_DECL.  */
3861
      decl = build_decl (input_location,
3862
                         FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
3863
      DECL_ARTIFICIAL (decl) = 1;
3864
      DECL_IGNORED_P (decl) = 1;
3865
      DECL_FIELD_CONTEXT (decl) = t;
3866
      if (CLASSTYPE_AS_BASE (basetype))
3867
        {
3868
          DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
3869
          DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
3870
          DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
3871
          DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
3872
          DECL_MODE (decl) = TYPE_MODE (basetype);
3873
          DECL_FIELD_IS_BASE (decl) = 1;
3874
 
3875
          /* Try to place the field.  It may take more than one try if we
3876
             have a hard time placing the field without putting two
3877
             objects of the same type at the same address.  */
3878
          layout_nonempty_base_or_field (rli, decl, binfo, offsets);
3879
          /* Add the new FIELD_DECL to the list of fields for T.  */
3880
          DECL_CHAIN (decl) = *next_field;
3881
          *next_field = decl;
3882
          next_field = &DECL_CHAIN (decl);
3883
        }
3884
    }
3885
  else
3886
    {
3887
      tree eoc;
3888
      bool atend;
3889
 
3890
      /* On some platforms (ARM), even empty classes will not be
3891
         byte-aligned.  */
3892
      eoc = round_up_loc (input_location,
3893
                      rli_size_unit_so_far (rli),
3894
                      CLASSTYPE_ALIGN_UNIT (basetype));
3895
      atend = layout_empty_base (rli, binfo, eoc, offsets);
3896
      /* A nearly-empty class "has no proper base class that is empty,
3897
         not morally virtual, and at an offset other than zero."  */
3898
      if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
3899
        {
3900
          if (atend)
3901
            CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3902
          /* The check above (used in G++ 3.2) is insufficient because
3903
             an empty class placed at offset zero might itself have an
3904
             empty base at a nonzero offset.  */
3905
          else if (walk_subobject_offsets (basetype,
3906
                                           empty_base_at_nonzero_offset_p,
3907
                                           size_zero_node,
3908
                                           /*offsets=*/NULL,
3909
                                           /*max_offset=*/NULL_TREE,
3910
                                           /*vbases_p=*/true))
3911
            {
3912
              if (abi_version_at_least (2))
3913
                CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3914
              else
3915
                warning (OPT_Wabi,
3916
                         "class %qT will be considered nearly empty in a "
3917
                         "future version of GCC", t);
3918
            }
3919
        }
3920
 
3921
      /* We do not create a FIELD_DECL for empty base classes because
3922
         it might overlap some other field.  We want to be able to
3923
         create CONSTRUCTORs for the class by iterating over the
3924
         FIELD_DECLs, and the back end does not handle overlapping
3925
         FIELD_DECLs.  */
3926
 
3927
      /* An empty virtual base causes a class to be non-empty
3928
         -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
3929
         here because that was already done when the virtual table
3930
         pointer was created.  */
3931
    }
3932
 
3933
  /* Record the offsets of BINFO and its base subobjects.  */
3934
  record_subobject_offsets (binfo,
3935
                            BINFO_OFFSET (binfo),
3936
                            offsets,
3937
                            /*is_data_member=*/false);
3938
 
3939
  return next_field;
3940
}
3941
 
3942
/* Layout all of the non-virtual base classes.  Record empty
3943
   subobjects in OFFSETS.  T is the most derived type.  Return nonzero
3944
   if the type cannot be nearly empty.  The fields created
3945
   corresponding to the base classes will be inserted at
3946
   *NEXT_FIELD.  */
3947
 
3948
static void
3949
build_base_fields (record_layout_info rli,
3950
                   splay_tree offsets, tree *next_field)
3951
{
3952
  /* Chain to hold all the new FIELD_DECLs which stand in for base class
3953
     subobjects.  */
3954
  tree t = rli->t;
3955
  int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
3956
  int i;
3957
 
3958
  /* The primary base class is always allocated first.  */
3959
  if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
3960
    next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
3961
                                   offsets, next_field);
3962
 
3963
  /* Now allocate the rest of the bases.  */
3964
  for (i = 0; i < n_baseclasses; ++i)
3965
    {
3966
      tree base_binfo;
3967
 
3968
      base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
3969
 
3970
      /* The primary base was already allocated above, so we don't
3971
         need to allocate it again here.  */
3972
      if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
3973
        continue;
3974
 
3975
      /* Virtual bases are added at the end (a primary virtual base
3976
         will have already been added).  */
3977
      if (BINFO_VIRTUAL_P (base_binfo))
3978
        continue;
3979
 
3980
      next_field = build_base_field (rli, base_binfo,
3981
                                     offsets, next_field);
3982
    }
3983
}
3984
 
3985
/* Go through the TYPE_METHODS of T issuing any appropriate
3986
   diagnostics, figuring out which methods override which other
3987
   methods, and so forth.  */
3988
 
3989
static void
3990
check_methods (tree t)
3991
{
3992
  tree x;
3993
 
3994
  for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
3995
    {
3996
      check_for_override (x, t);
3997
      if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
3998
        error ("initializer specified for non-virtual method %q+D", x);
3999
      /* The name of the field is the original field name
4000
         Save this in auxiliary field for later overloading.  */
4001
      if (DECL_VINDEX (x))
4002
        {
4003
          TYPE_POLYMORPHIC_P (t) = 1;
4004
          if (DECL_PURE_VIRTUAL_P (x))
4005
            VEC_safe_push (tree, gc, CLASSTYPE_PURE_VIRTUALS (t), x);
4006
        }
4007
      /* All user-provided destructors are non-trivial.
4008
         Constructors and assignment ops are handled in
4009
         grok_special_member_properties.  */
4010
      if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
4011
        TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
4012
    }
4013
}
4014
 
4015
/* FN is a constructor or destructor.  Clone the declaration to create
4016
   a specialized in-charge or not-in-charge version, as indicated by
4017
   NAME.  */
4018
 
4019
static tree
4020
build_clone (tree fn, tree name)
4021
{
4022
  tree parms;
4023
  tree clone;
4024
 
4025
  /* Copy the function.  */
4026
  clone = copy_decl (fn);
4027
  /* Reset the function name.  */
4028
  DECL_NAME (clone) = name;
4029
  SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4030
  /* Remember where this function came from.  */
4031
  DECL_ABSTRACT_ORIGIN (clone) = fn;
4032
  /* Make it easy to find the CLONE given the FN.  */
4033
  DECL_CHAIN (clone) = DECL_CHAIN (fn);
4034
  DECL_CHAIN (fn) = clone;
4035
 
4036
  /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT.  */
4037
  if (TREE_CODE (clone) == TEMPLATE_DECL)
4038
    {
4039
      tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4040
      DECL_TEMPLATE_RESULT (clone) = result;
4041
      DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4042
      DECL_TI_TEMPLATE (result) = clone;
4043
      TREE_TYPE (clone) = TREE_TYPE (result);
4044
      return clone;
4045
    }
4046
 
4047
  DECL_CLONED_FUNCTION (clone) = fn;
4048
  /* There's no pending inline data for this function.  */
4049
  DECL_PENDING_INLINE_INFO (clone) = NULL;
4050
  DECL_PENDING_INLINE_P (clone) = 0;
4051
 
4052
  /* The base-class destructor is not virtual.  */
4053
  if (name == base_dtor_identifier)
4054
    {
4055
      DECL_VIRTUAL_P (clone) = 0;
4056
      if (TREE_CODE (clone) != TEMPLATE_DECL)
4057
        DECL_VINDEX (clone) = NULL_TREE;
4058
    }
4059
 
4060
  /* If there was an in-charge parameter, drop it from the function
4061
     type.  */
4062
  if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4063
    {
4064
      tree basetype;
4065
      tree parmtypes;
4066
      tree exceptions;
4067
 
4068
      exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4069
      basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4070
      parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4071
      /* Skip the `this' parameter.  */
4072
      parmtypes = TREE_CHAIN (parmtypes);
4073
      /* Skip the in-charge parameter.  */
4074
      parmtypes = TREE_CHAIN (parmtypes);
4075
      /* And the VTT parm, in a complete [cd]tor.  */
4076
      if (DECL_HAS_VTT_PARM_P (fn)
4077
          && ! DECL_NEEDS_VTT_PARM_P (clone))
4078
        parmtypes = TREE_CHAIN (parmtypes);
4079
       /* If this is subobject constructor or destructor, add the vtt
4080
         parameter.  */
4081
      TREE_TYPE (clone)
4082
        = build_method_type_directly (basetype,
4083
                                      TREE_TYPE (TREE_TYPE (clone)),
4084
                                      parmtypes);
4085
      if (exceptions)
4086
        TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4087
                                                     exceptions);
4088
      TREE_TYPE (clone)
4089
        = cp_build_type_attribute_variant (TREE_TYPE (clone),
4090
                                           TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4091
    }
4092
 
4093
  /* Copy the function parameters.  */
4094
  DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4095
  /* Remove the in-charge parameter.  */
4096
  if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4097
    {
4098
      DECL_CHAIN (DECL_ARGUMENTS (clone))
4099
        = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4100
      DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4101
    }
4102
  /* And the VTT parm, in a complete [cd]tor.  */
4103
  if (DECL_HAS_VTT_PARM_P (fn))
4104
    {
4105
      if (DECL_NEEDS_VTT_PARM_P (clone))
4106
        DECL_HAS_VTT_PARM_P (clone) = 1;
4107
      else
4108
        {
4109
          DECL_CHAIN (DECL_ARGUMENTS (clone))
4110
            = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4111
          DECL_HAS_VTT_PARM_P (clone) = 0;
4112
        }
4113
    }
4114
 
4115
  for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4116
    {
4117
      DECL_CONTEXT (parms) = clone;
4118
      cxx_dup_lang_specific_decl (parms);
4119
    }
4120
 
4121
  /* Create the RTL for this function.  */
4122
  SET_DECL_RTL (clone, NULL);
4123
  rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4124
 
4125
  if (pch_file)
4126
    note_decl_for_pch (clone);
4127
 
4128
  return clone;
4129
}
4130
 
4131
/* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4132
   not invoke this function directly.
4133
 
4134
   For a non-thunk function, returns the address of the slot for storing
4135
   the function it is a clone of.  Otherwise returns NULL_TREE.
4136
 
4137
   If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4138
   cloned_function is unset.  This is to support the separate
4139
   DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4140
   on a template makes sense, but not the former.  */
4141
 
4142
tree *
4143
decl_cloned_function_p (const_tree decl, bool just_testing)
4144
{
4145
  tree *ptr;
4146
  if (just_testing)
4147
    decl = STRIP_TEMPLATE (decl);
4148
 
4149
  if (TREE_CODE (decl) != FUNCTION_DECL
4150
      || !DECL_LANG_SPECIFIC (decl)
4151
      || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4152
    {
4153
#if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4154
      if (!just_testing)
4155
        lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4156
      else
4157
#endif
4158
        return NULL;
4159
    }
4160
 
4161
  ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4162
  if (just_testing && *ptr == NULL_TREE)
4163
    return NULL;
4164
  else
4165
    return ptr;
4166
}
4167
 
4168
/* Produce declarations for all appropriate clones of FN.  If
4169
   UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
4170
   CLASTYPE_METHOD_VEC as well.  */
4171
 
4172
void
4173
clone_function_decl (tree fn, int update_method_vec_p)
4174
{
4175
  tree clone;
4176
 
4177
  /* Avoid inappropriate cloning.  */
4178
  if (DECL_CHAIN (fn)
4179
      && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4180
    return;
4181
 
4182
  if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4183
    {
4184
      /* For each constructor, we need two variants: an in-charge version
4185
         and a not-in-charge version.  */
4186
      clone = build_clone (fn, complete_ctor_identifier);
4187
      if (update_method_vec_p)
4188
        add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4189
      clone = build_clone (fn, base_ctor_identifier);
4190
      if (update_method_vec_p)
4191
        add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4192
    }
4193
  else
4194
    {
4195
      gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4196
 
4197
      /* For each destructor, we need three variants: an in-charge
4198
         version, a not-in-charge version, and an in-charge deleting
4199
         version.  We clone the deleting version first because that
4200
         means it will go second on the TYPE_METHODS list -- and that
4201
         corresponds to the correct layout order in the virtual
4202
         function table.
4203
 
4204
         For a non-virtual destructor, we do not build a deleting
4205
         destructor.  */
4206
      if (DECL_VIRTUAL_P (fn))
4207
        {
4208
          clone = build_clone (fn, deleting_dtor_identifier);
4209
          if (update_method_vec_p)
4210
            add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4211
        }
4212
      clone = build_clone (fn, complete_dtor_identifier);
4213
      if (update_method_vec_p)
4214
        add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4215
      clone = build_clone (fn, base_dtor_identifier);
4216
      if (update_method_vec_p)
4217
        add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4218
    }
4219
 
4220
  /* Note that this is an abstract function that is never emitted.  */
4221
  DECL_ABSTRACT (fn) = 1;
4222
}
4223
 
4224
/* DECL is an in charge constructor, which is being defined. This will
4225
   have had an in class declaration, from whence clones were
4226
   declared. An out-of-class definition can specify additional default
4227
   arguments. As it is the clones that are involved in overload
4228
   resolution, we must propagate the information from the DECL to its
4229
   clones.  */
4230
 
4231
void
4232
adjust_clone_args (tree decl)
4233
{
4234
  tree clone;
4235
 
4236
  for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4237
       clone = DECL_CHAIN (clone))
4238
    {
4239
      tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4240
      tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4241
      tree decl_parms, clone_parms;
4242
 
4243
      clone_parms = orig_clone_parms;
4244
 
4245
      /* Skip the 'this' parameter.  */
4246
      orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4247
      orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4248
 
4249
      if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4250
        orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4251
      if (DECL_HAS_VTT_PARM_P (decl))
4252
        orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4253
 
4254
      clone_parms = orig_clone_parms;
4255
      if (DECL_HAS_VTT_PARM_P (clone))
4256
        clone_parms = TREE_CHAIN (clone_parms);
4257
 
4258
      for (decl_parms = orig_decl_parms; decl_parms;
4259
           decl_parms = TREE_CHAIN (decl_parms),
4260
             clone_parms = TREE_CHAIN (clone_parms))
4261
        {
4262
          gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4263
                                   TREE_TYPE (clone_parms)));
4264
 
4265
          if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4266
            {
4267
              /* A default parameter has been added. Adjust the
4268
                 clone's parameters.  */
4269
              tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4270
              tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone));
4271
              tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4272
              tree type;
4273
 
4274
              clone_parms = orig_decl_parms;
4275
 
4276
              if (DECL_HAS_VTT_PARM_P (clone))
4277
                {
4278
                  clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4279
                                           TREE_VALUE (orig_clone_parms),
4280
                                           clone_parms);
4281
                  TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4282
                }
4283
              type = build_method_type_directly (basetype,
4284
                                                 TREE_TYPE (TREE_TYPE (clone)),
4285
                                                 clone_parms);
4286
              if (exceptions)
4287
                type = build_exception_variant (type, exceptions);
4288
              if (attrs)
4289
                type = cp_build_type_attribute_variant (type, attrs);
4290
              TREE_TYPE (clone) = type;
4291
 
4292
              clone_parms = NULL_TREE;
4293
              break;
4294
            }
4295
        }
4296
      gcc_assert (!clone_parms);
4297
    }
4298
}
4299
 
4300
/* For each of the constructors and destructors in T, create an
4301
   in-charge and not-in-charge variant.  */
4302
 
4303
static void
4304
clone_constructors_and_destructors (tree t)
4305
{
4306
  tree fns;
4307
 
4308
  /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4309
     out now.  */
4310
  if (!CLASSTYPE_METHOD_VEC (t))
4311
    return;
4312
 
4313
  for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4314
    clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4315
  for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4316
    clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4317
}
4318
 
4319
/* Subroutine of set_one_vmethod_tm_attributes.  Search base classes
4320
   of TYPE for virtual functions which FNDECL overrides.  Return a
4321
   mask of the tm attributes found therein.  */
4322
 
4323
static int
4324
look_for_tm_attr_overrides (tree type, tree fndecl)
4325
{
4326
  tree binfo = TYPE_BINFO (type);
4327
  tree base_binfo;
4328
  int ix, found = 0;
4329
 
4330
  for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
4331
    {
4332
      tree o, basetype = BINFO_TYPE (base_binfo);
4333
 
4334
      if (!TYPE_POLYMORPHIC_P (basetype))
4335
        continue;
4336
 
4337
      o = look_for_overrides_here (basetype, fndecl);
4338
      if (o)
4339
        found |= tm_attr_to_mask (find_tm_attribute
4340
                                  (TYPE_ATTRIBUTES (TREE_TYPE (o))));
4341
      else
4342
        found |= look_for_tm_attr_overrides (basetype, fndecl);
4343
    }
4344
 
4345
  return found;
4346
}
4347
 
4348
/* Subroutine of set_method_tm_attributes.  Handle the checks and
4349
   inheritance for one virtual method FNDECL.  */
4350
 
4351
static void
4352
set_one_vmethod_tm_attributes (tree type, tree fndecl)
4353
{
4354
  tree tm_attr;
4355
  int found, have;
4356
 
4357
  found = look_for_tm_attr_overrides (type, fndecl);
4358
 
4359
  /* If FNDECL doesn't actually override anything (i.e. T is the
4360
     class that first declares FNDECL virtual), then we're done.  */
4361
  if (found == 0)
4362
    return;
4363
 
4364
  tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
4365
  have = tm_attr_to_mask (tm_attr);
4366
 
4367
  /* Intel STM Language Extension 3.0, Section 4.2 table 4:
4368
     tm_pure must match exactly, otherwise no weakening of
4369
     tm_safe > tm_callable > nothing.  */
4370
  /* ??? The tm_pure attribute didn't make the transition to the
4371
     multivendor language spec.  */
4372
  if (have == TM_ATTR_PURE)
4373
    {
4374
      if (found != TM_ATTR_PURE)
4375
        {
4376
          found &= -found;
4377
          goto err_override;
4378
        }
4379
    }
4380
  /* If the overridden function is tm_pure, then FNDECL must be.  */
4381
  else if (found == TM_ATTR_PURE && tm_attr)
4382
    goto err_override;
4383
  /* Look for base class combinations that cannot be satisfied.  */
4384
  else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
4385
    {
4386
      found &= ~TM_ATTR_PURE;
4387
      found &= -found;
4388
      error_at (DECL_SOURCE_LOCATION (fndecl),
4389
                "method overrides both %<transaction_pure%> and %qE methods",
4390
                tm_mask_to_attr (found));
4391
    }
4392
  /* If FNDECL did not declare an attribute, then inherit the most
4393
     restrictive one.  */
4394
  else if (tm_attr == NULL)
4395
    {
4396
      apply_tm_attr (fndecl, tm_mask_to_attr (found & -found));
4397
    }
4398
  /* Otherwise validate that we're not weaker than a function
4399
     that is being overridden.  */
4400
  else
4401
    {
4402
      found &= -found;
4403
      if (found <= TM_ATTR_CALLABLE && have > found)
4404
        goto err_override;
4405
    }
4406
  return;
4407
 
4408
 err_override:
4409
  error_at (DECL_SOURCE_LOCATION (fndecl),
4410
            "method declared %qE overriding %qE method",
4411
            tm_attr, tm_mask_to_attr (found));
4412
}
4413
 
4414
/* For each of the methods in T, propagate a class-level tm attribute.  */
4415
 
4416
static void
4417
set_method_tm_attributes (tree t)
4418
{
4419
  tree class_tm_attr, fndecl;
4420
 
4421
  /* Don't bother collecting tm attributes if transactional memory
4422
     support is not enabled.  */
4423
  if (!flag_tm)
4424
    return;
4425
 
4426
  /* Process virtual methods first, as they inherit directly from the
4427
     base virtual function and also require validation of new attributes.  */
4428
  if (TYPE_CONTAINS_VPTR_P (t))
4429
    {
4430
      tree vchain;
4431
      for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
4432
           vchain = TREE_CHAIN (vchain))
4433
        {
4434
          fndecl = BV_FN (vchain);
4435
          if (DECL_THUNK_P (fndecl))
4436
            fndecl = THUNK_TARGET (fndecl);
4437
          set_one_vmethod_tm_attributes (t, fndecl);
4438
        }
4439
    }
4440
 
4441
  /* If the class doesn't have an attribute, nothing more to do.  */
4442
  class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
4443
  if (class_tm_attr == NULL)
4444
    return;
4445
 
4446
  /* Any method that does not yet have a tm attribute inherits
4447
     the one from the class.  */
4448
  for (fndecl = TYPE_METHODS (t); fndecl; fndecl = TREE_CHAIN (fndecl))
4449
    {
4450
      if (!find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
4451
        apply_tm_attr (fndecl, class_tm_attr);
4452
    }
4453
}
4454
 
4455
/* Returns true iff class T has a user-defined constructor other than
4456
   the default constructor.  */
4457
 
4458
bool
4459
type_has_user_nondefault_constructor (tree t)
4460
{
4461
  tree fns;
4462
 
4463
  if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4464
    return false;
4465
 
4466
  for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4467
    {
4468
      tree fn = OVL_CURRENT (fns);
4469
      if (!DECL_ARTIFICIAL (fn)
4470
          && (TREE_CODE (fn) == TEMPLATE_DECL
4471
              || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
4472
                  != NULL_TREE)))
4473
        return true;
4474
    }
4475
 
4476
  return false;
4477
}
4478
 
4479
/* Returns the defaulted constructor if T has one. Otherwise, returns
4480
   NULL_TREE.  */
4481
 
4482
tree
4483
in_class_defaulted_default_constructor (tree t)
4484
{
4485
  tree fns, args;
4486
 
4487
  if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4488
    return NULL_TREE;
4489
 
4490
  for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4491
    {
4492
      tree fn = OVL_CURRENT (fns);
4493
 
4494
      if (DECL_DEFAULTED_IN_CLASS_P (fn))
4495
        {
4496
          args = FUNCTION_FIRST_USER_PARMTYPE (fn);
4497
          while (args && TREE_PURPOSE (args))
4498
            args = TREE_CHAIN (args);
4499
          if (!args || args == void_list_node)
4500
            return fn;
4501
        }
4502
    }
4503
 
4504
  return NULL_TREE;
4505
}
4506
 
4507
/* Returns true iff FN is a user-provided function, i.e. user-declared
4508
   and not defaulted at its first declaration; or explicit, private,
4509
   protected, or non-const.  */
4510
 
4511
bool
4512
user_provided_p (tree fn)
4513
{
4514
  if (TREE_CODE (fn) == TEMPLATE_DECL)
4515
    return true;
4516
  else
4517
    return (!DECL_ARTIFICIAL (fn)
4518
            && !DECL_DEFAULTED_IN_CLASS_P (fn));
4519
}
4520
 
4521
/* Returns true iff class T has a user-provided constructor.  */
4522
 
4523
bool
4524
type_has_user_provided_constructor (tree t)
4525
{
4526
  tree fns;
4527
 
4528
  if (!CLASS_TYPE_P (t))
4529
    return false;
4530
 
4531
  if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4532
    return false;
4533
 
4534
  /* This can happen in error cases; avoid crashing.  */
4535
  if (!CLASSTYPE_METHOD_VEC (t))
4536
    return false;
4537
 
4538
  for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4539
    if (user_provided_p (OVL_CURRENT (fns)))
4540
      return true;
4541
 
4542
  return false;
4543
}
4544
 
4545
/* Returns true iff class T has a user-provided default constructor.  */
4546
 
4547
bool
4548
type_has_user_provided_default_constructor (tree t)
4549
{
4550
  tree fns;
4551
 
4552
  if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4553
    return false;
4554
 
4555
  for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4556
    {
4557
      tree fn = OVL_CURRENT (fns);
4558
      if (TREE_CODE (fn) == FUNCTION_DECL
4559
          && user_provided_p (fn)
4560
          && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)))
4561
        return true;
4562
    }
4563
 
4564
  return false;
4565
}
4566
 
4567
/* If default-initialization leaves part of TYPE uninitialized, returns
4568
   a DECL for the field or TYPE itself (DR 253).  */
4569
 
4570
tree
4571
default_init_uninitialized_part (tree type)
4572
{
4573
  tree t, r, binfo;
4574
  int i;
4575
 
4576
  type = strip_array_types (type);
4577
  if (!CLASS_TYPE_P (type))
4578
    return type;
4579
  if (type_has_user_provided_default_constructor (type))
4580
    return NULL_TREE;
4581
  for (binfo = TYPE_BINFO (type), i = 0;
4582
       BINFO_BASE_ITERATE (binfo, i, t); ++i)
4583
    {
4584
      r = default_init_uninitialized_part (BINFO_TYPE (t));
4585
      if (r)
4586
        return r;
4587
    }
4588
  for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
4589
    if (TREE_CODE (t) == FIELD_DECL
4590
        && !DECL_ARTIFICIAL (t)
4591
        && !DECL_INITIAL (t))
4592
      {
4593
        r = default_init_uninitialized_part (TREE_TYPE (t));
4594
        if (r)
4595
          return DECL_P (r) ? r : t;
4596
      }
4597
 
4598
  return NULL_TREE;
4599
}
4600
 
4601
/* Returns true iff for class T, a trivial synthesized default constructor
4602
   would be constexpr.  */
4603
 
4604
bool
4605
trivial_default_constructor_is_constexpr (tree t)
4606
{
4607
  /* A defaulted trivial default constructor is constexpr
4608
     if there is nothing to initialize.  */
4609
  gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
4610
  return is_really_empty_class (t);
4611
}
4612
 
4613
/* Returns true iff class T has a constexpr default constructor.  */
4614
 
4615
bool
4616
type_has_constexpr_default_constructor (tree t)
4617
{
4618
  tree fns;
4619
 
4620
  if (!CLASS_TYPE_P (t))
4621
    {
4622
      /* The caller should have stripped an enclosing array.  */
4623
      gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
4624
      return false;
4625
    }
4626
  if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
4627
    {
4628
      if (!TYPE_HAS_COMPLEX_DFLT (t))
4629
        return trivial_default_constructor_is_constexpr (t);
4630
      /* Non-trivial, we need to check subobject constructors.  */
4631
      lazily_declare_fn (sfk_constructor, t);
4632
    }
4633
  fns = locate_ctor (t);
4634
  return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
4635
}
4636
 
4637
/* Returns true iff class TYPE has a virtual destructor.  */
4638
 
4639
bool
4640
type_has_virtual_destructor (tree type)
4641
{
4642
  tree dtor;
4643
 
4644
  if (!CLASS_TYPE_P (type))
4645
    return false;
4646
 
4647
  gcc_assert (COMPLETE_TYPE_P (type));
4648
  dtor = CLASSTYPE_DESTRUCTORS (type);
4649
  return (dtor && DECL_VIRTUAL_P (dtor));
4650
}
4651
 
4652
/* Returns true iff class T has a move constructor.  */
4653
 
4654
bool
4655
type_has_move_constructor (tree t)
4656
{
4657
  tree fns;
4658
 
4659
  if (CLASSTYPE_LAZY_MOVE_CTOR (t))
4660
    {
4661
      gcc_assert (COMPLETE_TYPE_P (t));
4662
      lazily_declare_fn (sfk_move_constructor, t);
4663
    }
4664
 
4665
  if (!CLASSTYPE_METHOD_VEC (t))
4666
    return false;
4667
 
4668
  for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4669
    if (move_fn_p (OVL_CURRENT (fns)))
4670
      return true;
4671
 
4672
  return false;
4673
}
4674
 
4675
/* Returns true iff class T has a move assignment operator.  */
4676
 
4677
bool
4678
type_has_move_assign (tree t)
4679
{
4680
  tree fns;
4681
 
4682
  if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
4683
    {
4684
      gcc_assert (COMPLETE_TYPE_P (t));
4685
      lazily_declare_fn (sfk_move_assignment, t);
4686
    }
4687
 
4688
  for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
4689
       fns; fns = OVL_NEXT (fns))
4690
    if (move_fn_p (OVL_CURRENT (fns)))
4691
      return true;
4692
 
4693
  return false;
4694
}
4695
 
4696
/* Returns true iff class T has a move constructor that was explicitly
4697
   declared in the class body.  Note that this is different from
4698
   "user-provided", which doesn't include functions that are defaulted in
4699
   the class.  */
4700
 
4701
bool
4702
type_has_user_declared_move_constructor (tree t)
4703
{
4704
  tree fns;
4705
 
4706
  if (CLASSTYPE_LAZY_MOVE_CTOR (t))
4707
    return false;
4708
 
4709
  if (!CLASSTYPE_METHOD_VEC (t))
4710
    return false;
4711
 
4712
  for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4713
    {
4714
      tree fn = OVL_CURRENT (fns);
4715
      if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
4716
        return true;
4717
    }
4718
 
4719
  return false;
4720
}
4721
 
4722
/* Returns true iff class T has a move assignment operator that was
4723
   explicitly declared in the class body.  */
4724
 
4725
bool
4726
type_has_user_declared_move_assign (tree t)
4727
{
4728
  tree fns;
4729
 
4730
  if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
4731
    return false;
4732
 
4733
  for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
4734
       fns; fns = OVL_NEXT (fns))
4735
    {
4736
      tree fn = OVL_CURRENT (fns);
4737
      if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
4738
        return true;
4739
    }
4740
 
4741
  return false;
4742
}
4743
 
4744
/* Nonzero if we need to build up a constructor call when initializing an
4745
   object of this class, either because it has a user-provided constructor
4746
   or because it doesn't have a default constructor (so we need to give an
4747
   error if no initializer is provided).  Use TYPE_NEEDS_CONSTRUCTING when
4748
   what you care about is whether or not an object can be produced by a
4749
   constructor (e.g. so we don't set TREE_READONLY on const variables of
4750
   such type); use this function when what you care about is whether or not
4751
   to try to call a constructor to create an object.  The latter case is
4752
   the former plus some cases of constructors that cannot be called.  */
4753
 
4754
bool
4755
type_build_ctor_call (tree t)
4756
{
4757
  tree inner;
4758
  if (TYPE_NEEDS_CONSTRUCTING (t))
4759
    return true;
4760
  inner = strip_array_types (t);
4761
  return (CLASS_TYPE_P (inner) && !TYPE_HAS_DEFAULT_CONSTRUCTOR (inner)
4762
          && !ANON_AGGR_TYPE_P (inner));
4763
}
4764
 
4765
/* Remove all zero-width bit-fields from T.  */
4766
 
4767
static void
4768
remove_zero_width_bit_fields (tree t)
4769
{
4770
  tree *fieldsp;
4771
 
4772
  fieldsp = &TYPE_FIELDS (t);
4773
  while (*fieldsp)
4774
    {
4775
      if (TREE_CODE (*fieldsp) == FIELD_DECL
4776
          && DECL_C_BIT_FIELD (*fieldsp)
4777
          /* We should not be confused by the fact that grokbitfield
4778
             temporarily sets the width of the bit field into
4779
             DECL_INITIAL (*fieldsp).
4780
             check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
4781
             to that width.  */
4782
          && integer_zerop (DECL_SIZE (*fieldsp)))
4783
        *fieldsp = DECL_CHAIN (*fieldsp);
4784
      else
4785
        fieldsp = &DECL_CHAIN (*fieldsp);
4786
    }
4787
}
4788
 
4789
/* Returns TRUE iff we need a cookie when dynamically allocating an
4790
   array whose elements have the indicated class TYPE.  */
4791
 
4792
static bool
4793
type_requires_array_cookie (tree type)
4794
{
4795
  tree fns;
4796
  bool has_two_argument_delete_p = false;
4797
 
4798
  gcc_assert (CLASS_TYPE_P (type));
4799
 
4800
  /* If there's a non-trivial destructor, we need a cookie.  In order
4801
     to iterate through the array calling the destructor for each
4802
     element, we'll have to know how many elements there are.  */
4803
  if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4804
    return true;
4805
 
4806
  /* If the usual deallocation function is a two-argument whose second
4807
     argument is of type `size_t', then we have to pass the size of
4808
     the array to the deallocation function, so we will need to store
4809
     a cookie.  */
4810
  fns = lookup_fnfields (TYPE_BINFO (type),
4811
                         ansi_opname (VEC_DELETE_EXPR),
4812
                         /*protect=*/0);
4813
  /* If there are no `operator []' members, or the lookup is
4814
     ambiguous, then we don't need a cookie.  */
4815
  if (!fns || fns == error_mark_node)
4816
    return false;
4817
  /* Loop through all of the functions.  */
4818
  for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
4819
    {
4820
      tree fn;
4821
      tree second_parm;
4822
 
4823
      /* Select the current function.  */
4824
      fn = OVL_CURRENT (fns);
4825
      /* See if this function is a one-argument delete function.  If
4826
         it is, then it will be the usual deallocation function.  */
4827
      second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
4828
      if (second_parm == void_list_node)
4829
        return false;
4830
      /* Do not consider this function if its second argument is an
4831
         ellipsis.  */
4832
      if (!second_parm)
4833
        continue;
4834
      /* Otherwise, if we have a two-argument function and the second
4835
         argument is `size_t', it will be the usual deallocation
4836
         function -- unless there is one-argument function, too.  */
4837
      if (TREE_CHAIN (second_parm) == void_list_node
4838
          && same_type_p (TREE_VALUE (second_parm), size_type_node))
4839
        has_two_argument_delete_p = true;
4840
    }
4841
 
4842
  return has_two_argument_delete_p;
4843
}
4844
 
4845
/* Finish computing the `literal type' property of class type T.
4846
 
4847
   At this point, we have already processed base classes and
4848
   non-static data members.  We need to check whether the copy
4849
   constructor is trivial, the destructor is trivial, and there
4850
   is a trivial default constructor or at least one constexpr
4851
   constructor other than the copy constructor.  */
4852
 
4853
static void
4854
finalize_literal_type_property (tree t)
4855
{
4856
  tree fn;
4857
 
4858
  if (cxx_dialect < cxx0x
4859
      || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
4860
    CLASSTYPE_LITERAL_P (t) = false;
4861
  else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
4862
           && CLASSTYPE_NON_AGGREGATE (t)
4863
           && !TYPE_HAS_CONSTEXPR_CTOR (t))
4864
    CLASSTYPE_LITERAL_P (t) = false;
4865
 
4866
  if (!CLASSTYPE_LITERAL_P (t))
4867
    for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
4868
      if (DECL_DECLARED_CONSTEXPR_P (fn)
4869
          && TREE_CODE (fn) != TEMPLATE_DECL
4870
          && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
4871
          && !DECL_CONSTRUCTOR_P (fn))
4872
        {
4873
          DECL_DECLARED_CONSTEXPR_P (fn) = false;
4874
          if (!DECL_GENERATED_P (fn))
4875
            {
4876
              error ("enclosing class of constexpr non-static member "
4877
                     "function %q+#D is not a literal type", fn);
4878
              explain_non_literal_class (t);
4879
            }
4880
        }
4881
}
4882
 
4883
/* T is a non-literal type used in a context which requires a constant
4884
   expression.  Explain why it isn't literal.  */
4885
 
4886
void
4887
explain_non_literal_class (tree t)
4888
{
4889
  static struct pointer_set_t *diagnosed;
4890
 
4891
  if (!CLASS_TYPE_P (t))
4892
    return;
4893
  t = TYPE_MAIN_VARIANT (t);
4894
 
4895
  if (diagnosed == NULL)
4896
    diagnosed = pointer_set_create ();
4897
  if (pointer_set_insert (diagnosed, t) != 0)
4898
    /* Already explained.  */
4899
    return;
4900
 
4901
  inform (0, "%q+T is not literal because:", t);
4902
  if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
4903
    inform (0, "  %q+T has a non-trivial destructor", t);
4904
  else if (CLASSTYPE_NON_AGGREGATE (t)
4905
           && !TYPE_HAS_TRIVIAL_DFLT (t)
4906
           && !TYPE_HAS_CONSTEXPR_CTOR (t))
4907
    {
4908
      inform (0, "  %q+T is not an aggregate, does not have a trivial "
4909
              "default constructor, and has no constexpr constructor that "
4910
              "is not a copy or move constructor", t);
4911
      if (TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
4912
          && !type_has_user_provided_default_constructor (t))
4913
        {
4914
          /* Note that we can't simply call locate_ctor because when the
4915
             constructor is deleted it just returns NULL_TREE.  */
4916
          tree fns;
4917
          for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4918
            {
4919
              tree fn = OVL_CURRENT (fns);
4920
              tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
4921
 
4922
              parms = skip_artificial_parms_for (fn, parms);
4923
 
4924
              if (sufficient_parms_p (parms))
4925
                {
4926
                  if (DECL_DELETED_FN (fn))
4927
                    maybe_explain_implicit_delete (fn);
4928
                  else
4929
                    explain_invalid_constexpr_fn (fn);
4930
                  break;
4931
                }
4932
            }
4933
        }
4934
    }
4935
  else
4936
    {
4937
      tree binfo, base_binfo, field; int i;
4938
      for (binfo = TYPE_BINFO (t), i = 0;
4939
           BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
4940
        {
4941
          tree basetype = TREE_TYPE (base_binfo);
4942
          if (!CLASSTYPE_LITERAL_P (basetype))
4943
            {
4944
              inform (0, "  base class %qT of %q+T is non-literal",
4945
                      basetype, t);
4946
              explain_non_literal_class (basetype);
4947
              return;
4948
            }
4949
        }
4950
      for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
4951
        {
4952
          tree ftype;
4953
          if (TREE_CODE (field) != FIELD_DECL)
4954
            continue;
4955
          ftype = TREE_TYPE (field);
4956
          if (!literal_type_p (ftype))
4957
            {
4958
              inform (0, "  non-static data member %q+D has "
4959
                      "non-literal type", field);
4960
              if (CLASS_TYPE_P (ftype))
4961
                explain_non_literal_class (ftype);
4962
            }
4963
        }
4964
    }
4965
}
4966
 
4967
/* Check the validity of the bases and members declared in T.  Add any
4968
   implicitly-generated functions (like copy-constructors and
4969
   assignment operators).  Compute various flag bits (like
4970
   CLASSTYPE_NON_LAYOUT_POD_T) for T.  This routine works purely at the C++
4971
   level: i.e., independently of the ABI in use.  */
4972
 
4973
static void
4974
check_bases_and_members (tree t)
4975
{
4976
  /* Nonzero if the implicitly generated copy constructor should take
4977
     a non-const reference argument.  */
4978
  int cant_have_const_ctor;
4979
  /* Nonzero if the implicitly generated assignment operator
4980
     should take a non-const reference argument.  */
4981
  int no_const_asn_ref;
4982
  tree access_decls;
4983
  bool saved_complex_asn_ref;
4984
  bool saved_nontrivial_dtor;
4985
  tree fn;
4986
 
4987
  /* By default, we use const reference arguments and generate default
4988
     constructors.  */
4989
  cant_have_const_ctor = 0;
4990
  no_const_asn_ref = 0;
4991
 
4992
  /* Check all the base-classes.  */
4993
  check_bases (t, &cant_have_const_ctor,
4994
               &no_const_asn_ref);
4995
 
4996
  /* Check all the method declarations.  */
4997
  check_methods (t);
4998
 
4999
  /* Save the initial values of these flags which only indicate whether
5000
     or not the class has user-provided functions.  As we analyze the
5001
     bases and members we can set these flags for other reasons.  */
5002
  saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5003
  saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5004
 
5005
  /* Check all the data member declarations.  We cannot call
5006
     check_field_decls until we have called check_bases check_methods,
5007
     as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5008
     being set appropriately.  */
5009
  check_field_decls (t, &access_decls,
5010
                     &cant_have_const_ctor,
5011
                     &no_const_asn_ref);
5012
 
5013
  /* A nearly-empty class has to be vptr-containing; a nearly empty
5014
     class contains just a vptr.  */
5015
  if (!TYPE_CONTAINS_VPTR_P (t))
5016
    CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5017
 
5018
  /* Do some bookkeeping that will guide the generation of implicitly
5019
     declared member functions.  */
5020
  TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5021
  TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5022
  /* We need to call a constructor for this class if it has a
5023
     user-provided constructor, or if the default constructor is going
5024
     to initialize the vptr.  (This is not an if-and-only-if;
5025
     TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5026
     themselves need constructing.)  */
5027
  TYPE_NEEDS_CONSTRUCTING (t)
5028
    |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
5029
  /* [dcl.init.aggr]
5030
 
5031
     An aggregate is an array or a class with no user-provided
5032
     constructors ... and no virtual functions.
5033
 
5034
     Again, other conditions for being an aggregate are checked
5035
     elsewhere.  */
5036
  CLASSTYPE_NON_AGGREGATE (t)
5037
    |= (type_has_user_provided_constructor (t) || TYPE_POLYMORPHIC_P (t));
5038
  /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5039
     retain the old definition internally for ABI reasons.  */
5040
  CLASSTYPE_NON_LAYOUT_POD_P (t)
5041
    |= (CLASSTYPE_NON_AGGREGATE (t)
5042
        || saved_nontrivial_dtor || saved_complex_asn_ref);
5043
  CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
5044
  TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5045
  TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5046
  TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
5047
 
5048
  /* If the class has no user-declared constructor, but does have
5049
     non-static const or reference data members that can never be
5050
     initialized, issue a warning.  */
5051
  if (warn_uninitialized
5052
      /* Classes with user-declared constructors are presumed to
5053
         initialize these members.  */
5054
      && !TYPE_HAS_USER_CONSTRUCTOR (t)
5055
      /* Aggregates can be initialized with brace-enclosed
5056
         initializers.  */
5057
      && CLASSTYPE_NON_AGGREGATE (t))
5058
    {
5059
      tree field;
5060
 
5061
      for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5062
        {
5063
          tree type;
5064
 
5065
          if (TREE_CODE (field) != FIELD_DECL)
5066
            continue;
5067
 
5068
          type = TREE_TYPE (field);
5069
          if (TREE_CODE (type) == REFERENCE_TYPE)
5070
            warning (OPT_Wuninitialized, "non-static reference %q+#D "
5071
                     "in class without a constructor", field);
5072
          else if (CP_TYPE_CONST_P (type)
5073
                   && (!CLASS_TYPE_P (type)
5074
                       || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5075
            warning (OPT_Wuninitialized, "non-static const member %q+#D "
5076
                     "in class without a constructor", field);
5077
        }
5078
    }
5079
 
5080
  /* Synthesize any needed methods.  */
5081
  add_implicitly_declared_members (t,
5082
                                   cant_have_const_ctor,
5083
                                   no_const_asn_ref);
5084
 
5085
  /* Check defaulted declarations here so we have cant_have_const_ctor
5086
     and don't need to worry about clones.  */
5087
  for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5088
    if (DECL_DEFAULTED_IN_CLASS_P (fn))
5089
      {
5090
        int copy = copy_fn_p (fn);
5091
        if (copy > 0)
5092
          {
5093
            bool imp_const_p
5094
              = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
5095
                 : !no_const_asn_ref);
5096
            bool fn_const_p = (copy == 2);
5097
 
5098
            if (fn_const_p && !imp_const_p)
5099
              /* If the function is defaulted outside the class, we just
5100
                 give the synthesis error.  */
5101
              error ("%q+D declared to take const reference, but implicit "
5102
                     "declaration would take non-const", fn);
5103
            else if (imp_const_p && !fn_const_p)
5104
              error ("%q+D declared to take non-const reference cannot be "
5105
                     "defaulted in the class body", fn);
5106
          }
5107
        defaulted_late_check (fn);
5108
      }
5109
 
5110
  if (LAMBDA_TYPE_P (t))
5111
    {
5112
      /* "The closure type associated with a lambda-expression has a deleted
5113
         default constructor and a deleted copy assignment operator."  */
5114
      TYPE_NEEDS_CONSTRUCTING (t) = 1;
5115
      TYPE_HAS_COMPLEX_DFLT (t) = 1;
5116
      TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
5117
      CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 0;
5118
 
5119
      /* "This class type is not an aggregate."  */
5120
      CLASSTYPE_NON_AGGREGATE (t) = 1;
5121
    }
5122
 
5123
  /* Compute the 'literal type' property before we
5124
     do anything with non-static member functions.  */
5125
  finalize_literal_type_property (t);
5126
 
5127
  /* Create the in-charge and not-in-charge variants of constructors
5128
     and destructors.  */
5129
  clone_constructors_and_destructors (t);
5130
 
5131
  /* Process the using-declarations.  */
5132
  for (; access_decls; access_decls = TREE_CHAIN (access_decls))
5133
    handle_using_decl (TREE_VALUE (access_decls), t);
5134
 
5135
  /* Build and sort the CLASSTYPE_METHOD_VEC.  */
5136
  finish_struct_methods (t);
5137
 
5138
  /* Figure out whether or not we will need a cookie when dynamically
5139
     allocating an array of this type.  */
5140
  TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
5141
    = type_requires_array_cookie (t);
5142
}
5143
 
5144
/* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5145
   accordingly.  If a new vfield was created (because T doesn't have a
5146
   primary base class), then the newly created field is returned.  It
5147
   is not added to the TYPE_FIELDS list; it is the caller's
5148
   responsibility to do that.  Accumulate declared virtual functions
5149
   on VIRTUALS_P.  */
5150
 
5151
static tree
5152
create_vtable_ptr (tree t, tree* virtuals_p)
5153
{
5154
  tree fn;
5155
 
5156
  /* Collect the virtual functions declared in T.  */
5157
  for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5158
    if (DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
5159
        && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
5160
      {
5161
        tree new_virtual = make_node (TREE_LIST);
5162
 
5163
        BV_FN (new_virtual) = fn;
5164
        BV_DELTA (new_virtual) = integer_zero_node;
5165
        BV_VCALL_INDEX (new_virtual) = NULL_TREE;
5166
 
5167
        TREE_CHAIN (new_virtual) = *virtuals_p;
5168
        *virtuals_p = new_virtual;
5169
      }
5170
 
5171
  /* If we couldn't find an appropriate base class, create a new field
5172
     here.  Even if there weren't any new virtual functions, we might need a
5173
     new virtual function table if we're supposed to include vptrs in
5174
     all classes that need them.  */
5175
  if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
5176
    {
5177
      /* We build this decl with vtbl_ptr_type_node, which is a
5178
         `vtable_entry_type*'.  It might seem more precise to use
5179
         `vtable_entry_type (*)[N]' where N is the number of virtual
5180
         functions.  However, that would require the vtable pointer in
5181
         base classes to have a different type than the vtable pointer
5182
         in derived classes.  We could make that happen, but that
5183
         still wouldn't solve all the problems.  In particular, the
5184
         type-based alias analysis code would decide that assignments
5185
         to the base class vtable pointer can't alias assignments to
5186
         the derived class vtable pointer, since they have different
5187
         types.  Thus, in a derived class destructor, where the base
5188
         class constructor was inlined, we could generate bad code for
5189
         setting up the vtable pointer.
5190
 
5191
         Therefore, we use one type for all vtable pointers.  We still
5192
         use a type-correct type; it's just doesn't indicate the array
5193
         bounds.  That's better than using `void*' or some such; it's
5194
         cleaner, and it let's the alias analysis code know that these
5195
         stores cannot alias stores to void*!  */
5196
      tree field;
5197
 
5198
      field = build_decl (input_location,
5199
                          FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
5200
      DECL_VIRTUAL_P (field) = 1;
5201
      DECL_ARTIFICIAL (field) = 1;
5202
      DECL_FIELD_CONTEXT (field) = t;
5203
      DECL_FCONTEXT (field) = t;
5204
      if (TYPE_PACKED (t))
5205
        DECL_PACKED (field) = 1;
5206
 
5207
      TYPE_VFIELD (t) = field;
5208
 
5209
      /* This class is non-empty.  */
5210
      CLASSTYPE_EMPTY_P (t) = 0;
5211
 
5212
      return field;
5213
    }
5214
 
5215
  return NULL_TREE;
5216
}
5217
 
5218
/* Add OFFSET to all base types of BINFO which is a base in the
5219
   hierarchy dominated by T.
5220
 
5221
   OFFSET, which is a type offset, is number of bytes.  */
5222
 
5223
static void
5224
propagate_binfo_offsets (tree binfo, tree offset)
5225
{
5226
  int i;
5227
  tree primary_binfo;
5228
  tree base_binfo;
5229
 
5230
  /* Update BINFO's offset.  */
5231
  BINFO_OFFSET (binfo)
5232
    = convert (sizetype,
5233
               size_binop (PLUS_EXPR,
5234
                           convert (ssizetype, BINFO_OFFSET (binfo)),
5235
                           offset));
5236
 
5237
  /* Find the primary base class.  */
5238
  primary_binfo = get_primary_binfo (binfo);
5239
 
5240
  if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
5241
    propagate_binfo_offsets (primary_binfo, offset);
5242
 
5243
  /* Scan all of the bases, pushing the BINFO_OFFSET adjust
5244
     downwards.  */
5245
  for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5246
    {
5247
      /* Don't do the primary base twice.  */
5248
      if (base_binfo == primary_binfo)
5249
        continue;
5250
 
5251
      if (BINFO_VIRTUAL_P (base_binfo))
5252
        continue;
5253
 
5254
      propagate_binfo_offsets (base_binfo, offset);
5255
    }
5256
}
5257
 
5258
/* Set BINFO_OFFSET for all of the virtual bases for RLI->T.  Update
5259
   TYPE_ALIGN and TYPE_SIZE for T.  OFFSETS gives the location of
5260
   empty subobjects of T.  */
5261
 
5262
static void
5263
layout_virtual_bases (record_layout_info rli, splay_tree offsets)
5264
{
5265
  tree vbase;
5266
  tree t = rli->t;
5267
  bool first_vbase = true;
5268
  tree *next_field;
5269
 
5270
  if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
5271
    return;
5272
 
5273
  if (!abi_version_at_least(2))
5274
    {
5275
      /* In G++ 3.2, we incorrectly rounded the size before laying out
5276
         the virtual bases.  */
5277
      finish_record_layout (rli, /*free_p=*/false);
5278
#ifdef STRUCTURE_SIZE_BOUNDARY
5279
      /* Packed structures don't need to have minimum size.  */
5280
      if (! TYPE_PACKED (t))
5281
        TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), (unsigned) STRUCTURE_SIZE_BOUNDARY);
5282
#endif
5283
      rli->offset = TYPE_SIZE_UNIT (t);
5284
      rli->bitpos = bitsize_zero_node;
5285
      rli->record_align = TYPE_ALIGN (t);
5286
    }
5287
 
5288
  /* Find the last field.  The artificial fields created for virtual
5289
     bases will go after the last extant field to date.  */
5290
  next_field = &TYPE_FIELDS (t);
5291
  while (*next_field)
5292
    next_field = &DECL_CHAIN (*next_field);
5293
 
5294
  /* Go through the virtual bases, allocating space for each virtual
5295
     base that is not already a primary base class.  These are
5296
     allocated in inheritance graph order.  */
5297
  for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
5298
    {
5299
      if (!BINFO_VIRTUAL_P (vbase))
5300
        continue;
5301
 
5302
      if (!BINFO_PRIMARY_P (vbase))
5303
        {
5304
          tree basetype = TREE_TYPE (vbase);
5305
 
5306
          /* This virtual base is not a primary base of any class in the
5307
             hierarchy, so we have to add space for it.  */
5308
          next_field = build_base_field (rli, vbase,
5309
                                         offsets, next_field);
5310
 
5311
          /* If the first virtual base might have been placed at a
5312
             lower address, had we started from CLASSTYPE_SIZE, rather
5313
             than TYPE_SIZE, issue a warning.  There can be both false
5314
             positives and false negatives from this warning in rare
5315
             cases; to deal with all the possibilities would probably
5316
             require performing both layout algorithms and comparing
5317
             the results which is not particularly tractable.  */
5318
          if (warn_abi
5319
              && first_vbase
5320
              && (tree_int_cst_lt
5321
                  (size_binop (CEIL_DIV_EXPR,
5322
                               round_up_loc (input_location,
5323
                                         CLASSTYPE_SIZE (t),
5324
                                         CLASSTYPE_ALIGN (basetype)),
5325
                               bitsize_unit_node),
5326
                   BINFO_OFFSET (vbase))))
5327
            warning (OPT_Wabi,
5328
                     "offset of virtual base %qT is not ABI-compliant and "
5329
                     "may change in a future version of GCC",
5330
                     basetype);
5331
 
5332
          first_vbase = false;
5333
        }
5334
    }
5335
}
5336
 
5337
/* Returns the offset of the byte just past the end of the base class
5338
   BINFO.  */
5339
 
5340
static tree
5341
end_of_base (tree binfo)
5342
{
5343
  tree size;
5344
 
5345
  if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
5346
    size = TYPE_SIZE_UNIT (char_type_node);
5347
  else if (is_empty_class (BINFO_TYPE (binfo)))
5348
    /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
5349
       allocate some space for it. It cannot have virtual bases, so
5350
       TYPE_SIZE_UNIT is fine.  */
5351
    size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5352
  else
5353
    size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5354
 
5355
  return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
5356
}
5357
 
5358
/* Returns the offset of the byte just past the end of the base class
5359
   with the highest offset in T.  If INCLUDE_VIRTUALS_P is zero, then
5360
   only non-virtual bases are included.  */
5361
 
5362
static tree
5363
end_of_class (tree t, int include_virtuals_p)
5364
{
5365
  tree result = size_zero_node;
5366
  VEC(tree,gc) *vbases;
5367
  tree binfo;
5368
  tree base_binfo;
5369
  tree offset;
5370
  int i;
5371
 
5372
  for (binfo = TYPE_BINFO (t), i = 0;
5373
       BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5374
    {
5375
      if (!include_virtuals_p
5376
          && BINFO_VIRTUAL_P (base_binfo)
5377
          && (!BINFO_PRIMARY_P (base_binfo)
5378
              || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
5379
        continue;
5380
 
5381
      offset = end_of_base (base_binfo);
5382
      if (INT_CST_LT_UNSIGNED (result, offset))
5383
        result = offset;
5384
    }
5385
 
5386
  /* G++ 3.2 did not check indirect virtual bases.  */
5387
  if (abi_version_at_least (2) && include_virtuals_p)
5388
    for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5389
         VEC_iterate (tree, vbases, i, base_binfo); i++)
5390
      {
5391
        offset = end_of_base (base_binfo);
5392
        if (INT_CST_LT_UNSIGNED (result, offset))
5393
          result = offset;
5394
      }
5395
 
5396
  return result;
5397
}
5398
 
5399
/* Warn about bases of T that are inaccessible because they are
5400
   ambiguous.  For example:
5401
 
5402
     struct S {};
5403
     struct T : public S {};
5404
     struct U : public S, public T {};
5405
 
5406
   Here, `(S*) new U' is not allowed because there are two `S'
5407
   subobjects of U.  */
5408
 
5409
static void
5410
warn_about_ambiguous_bases (tree t)
5411
{
5412
  int i;
5413
  VEC(tree,gc) *vbases;
5414
  tree basetype;
5415
  tree binfo;
5416
  tree base_binfo;
5417
 
5418
  /* If there are no repeated bases, nothing can be ambiguous.  */
5419
  if (!CLASSTYPE_REPEATED_BASE_P (t))
5420
    return;
5421
 
5422
  /* Check direct bases.  */
5423
  for (binfo = TYPE_BINFO (t), i = 0;
5424
       BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5425
    {
5426
      basetype = BINFO_TYPE (base_binfo);
5427
 
5428
      if (!lookup_base (t, basetype, ba_unique | ba_quiet, NULL))
5429
        warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
5430
                 basetype, t);
5431
    }
5432
 
5433
  /* Check for ambiguous virtual bases.  */
5434
  if (extra_warnings)
5435
    for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5436
         VEC_iterate (tree, vbases, i, binfo); i++)
5437
      {
5438
        basetype = BINFO_TYPE (binfo);
5439
 
5440
        if (!lookup_base (t, basetype, ba_unique | ba_quiet, NULL))
5441
          warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due to ambiguity",
5442
                   basetype, t);
5443
      }
5444
}
5445
 
5446
/* Compare two INTEGER_CSTs K1 and K2.  */
5447
 
5448
static int
5449
splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
5450
{
5451
  return tree_int_cst_compare ((tree) k1, (tree) k2);
5452
}
5453
 
5454
/* Increase the size indicated in RLI to account for empty classes
5455
   that are "off the end" of the class.  */
5456
 
5457
static void
5458
include_empty_classes (record_layout_info rli)
5459
{
5460
  tree eoc;
5461
  tree rli_size;
5462
 
5463
  /* It might be the case that we grew the class to allocate a
5464
     zero-sized base class.  That won't be reflected in RLI, yet,
5465
     because we are willing to overlay multiple bases at the same
5466
     offset.  However, now we need to make sure that RLI is big enough
5467
     to reflect the entire class.  */
5468
  eoc = end_of_class (rli->t,
5469
                      CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
5470
  rli_size = rli_size_unit_so_far (rli);
5471
  if (TREE_CODE (rli_size) == INTEGER_CST
5472
      && INT_CST_LT_UNSIGNED (rli_size, eoc))
5473
    {
5474
      if (!abi_version_at_least (2))
5475
        /* In version 1 of the ABI, the size of a class that ends with
5476
           a bitfield was not rounded up to a whole multiple of a
5477
           byte.  Because rli_size_unit_so_far returns only the number
5478
           of fully allocated bytes, any extra bits were not included
5479
           in the size.  */
5480
        rli->bitpos = round_down (rli->bitpos, BITS_PER_UNIT);
5481
      else
5482
        /* The size should have been rounded to a whole byte.  */
5483
        gcc_assert (tree_int_cst_equal
5484
                    (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
5485
      rli->bitpos
5486
        = size_binop (PLUS_EXPR,
5487
                      rli->bitpos,
5488
                      size_binop (MULT_EXPR,
5489
                                  convert (bitsizetype,
5490
                                           size_binop (MINUS_EXPR,
5491
                                                       eoc, rli_size)),
5492
                                  bitsize_int (BITS_PER_UNIT)));
5493
      normalize_rli (rli);
5494
    }
5495
}
5496
 
5497
/* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T.  Calculate
5498
   BINFO_OFFSETs for all of the base-classes.  Position the vtable
5499
   pointer.  Accumulate declared virtual functions on VIRTUALS_P.  */
5500
 
5501
static void
5502
layout_class_type (tree t, tree *virtuals_p)
5503
{
5504
  tree non_static_data_members;
5505
  tree field;
5506
  tree vptr;
5507
  record_layout_info rli;
5508
  /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
5509
     types that appear at that offset.  */
5510
  splay_tree empty_base_offsets;
5511
  /* True if the last field layed out was a bit-field.  */
5512
  bool last_field_was_bitfield = false;
5513
  /* The location at which the next field should be inserted.  */
5514
  tree *next_field;
5515
  /* T, as a base class.  */
5516
  tree base_t;
5517
 
5518
  /* Keep track of the first non-static data member.  */
5519
  non_static_data_members = TYPE_FIELDS (t);
5520
 
5521
  /* Start laying out the record.  */
5522
  rli = start_record_layout (t);
5523
 
5524
  /* Mark all the primary bases in the hierarchy.  */
5525
  determine_primary_bases (t);
5526
 
5527
  /* Create a pointer to our virtual function table.  */
5528
  vptr = create_vtable_ptr (t, virtuals_p);
5529
 
5530
  /* The vptr is always the first thing in the class.  */
5531
  if (vptr)
5532
    {
5533
      DECL_CHAIN (vptr) = TYPE_FIELDS (t);
5534
      TYPE_FIELDS (t) = vptr;
5535
      next_field = &DECL_CHAIN (vptr);
5536
      place_field (rli, vptr);
5537
    }
5538
  else
5539
    next_field = &TYPE_FIELDS (t);
5540
 
5541
  /* Build FIELD_DECLs for all of the non-virtual base-types.  */
5542
  empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
5543
                                       NULL, NULL);
5544
  build_base_fields (rli, empty_base_offsets, next_field);
5545
 
5546
  /* Layout the non-static data members.  */
5547
  for (field = non_static_data_members; field; field = DECL_CHAIN (field))
5548
    {
5549
      tree type;
5550
      tree padding;
5551
 
5552
      /* We still pass things that aren't non-static data members to
5553
         the back end, in case it wants to do something with them.  */
5554
      if (TREE_CODE (field) != FIELD_DECL)
5555
        {
5556
          place_field (rli, field);
5557
          /* If the static data member has incomplete type, keep track
5558
             of it so that it can be completed later.  (The handling
5559
             of pending statics in finish_record_layout is
5560
             insufficient; consider:
5561
 
5562
               struct S1;
5563
               struct S2 { static S1 s1; };
5564
 
5565
             At this point, finish_record_layout will be called, but
5566
             S1 is still incomplete.)  */
5567
          if (TREE_CODE (field) == VAR_DECL)
5568
            {
5569
              maybe_register_incomplete_var (field);
5570
              /* The visibility of static data members is determined
5571
                 at their point of declaration, not their point of
5572
                 definition.  */
5573
              determine_visibility (field);
5574
            }
5575
          continue;
5576
        }
5577
 
5578
      type = TREE_TYPE (field);
5579
      if (type == error_mark_node)
5580
        continue;
5581
 
5582
      padding = NULL_TREE;
5583
 
5584
      /* If this field is a bit-field whose width is greater than its
5585
         type, then there are some special rules for allocating
5586
         it.  */
5587
      if (DECL_C_BIT_FIELD (field)
5588
          && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
5589
        {
5590
          unsigned int itk;
5591
          tree integer_type;
5592
          bool was_unnamed_p = false;
5593
          /* We must allocate the bits as if suitably aligned for the
5594
             longest integer type that fits in this many bits.  type
5595
             of the field.  Then, we are supposed to use the left over
5596
             bits as additional padding.  */
5597
          for (itk = itk_char; itk != itk_none; ++itk)
5598
            if (integer_types[itk] != NULL_TREE
5599
                && (INT_CST_LT (size_int (MAX_FIXED_MODE_SIZE),
5600
                                TYPE_SIZE (integer_types[itk]))
5601
                    || INT_CST_LT (DECL_SIZE (field),
5602
                                   TYPE_SIZE (integer_types[itk]))))
5603
              break;
5604
 
5605
          /* ITK now indicates a type that is too large for the
5606
             field.  We have to back up by one to find the largest
5607
             type that fits.  */
5608
          do
5609
          {
5610
            --itk;
5611
            integer_type = integer_types[itk];
5612
          } while (itk > 0 && integer_type == NULL_TREE);
5613
 
5614
          /* Figure out how much additional padding is required.  GCC
5615
             3.2 always created a padding field, even if it had zero
5616
             width.  */
5617
          if (!abi_version_at_least (2)
5618
              || INT_CST_LT (TYPE_SIZE (integer_type), DECL_SIZE (field)))
5619
            {
5620
              if (abi_version_at_least (2) && TREE_CODE (t) == UNION_TYPE)
5621
                /* In a union, the padding field must have the full width
5622
                   of the bit-field; all fields start at offset zero.  */
5623
                padding = DECL_SIZE (field);
5624
              else
5625
                {
5626
                  if (TREE_CODE (t) == UNION_TYPE)
5627
                    warning (OPT_Wabi, "size assigned to %qT may not be "
5628
                             "ABI-compliant and may change in a future "
5629
                             "version of GCC",
5630
                             t);
5631
                  padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
5632
                                        TYPE_SIZE (integer_type));
5633
                }
5634
            }
5635
#ifdef PCC_BITFIELD_TYPE_MATTERS
5636
          /* An unnamed bitfield does not normally affect the
5637
             alignment of the containing class on a target where
5638
             PCC_BITFIELD_TYPE_MATTERS.  But, the C++ ABI does not
5639
             make any exceptions for unnamed bitfields when the
5640
             bitfields are longer than their types.  Therefore, we
5641
             temporarily give the field a name.  */
5642
          if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
5643
            {
5644
              was_unnamed_p = true;
5645
              DECL_NAME (field) = make_anon_name ();
5646
            }
5647
#endif
5648
          DECL_SIZE (field) = TYPE_SIZE (integer_type);
5649
          DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
5650
          DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
5651
          layout_nonempty_base_or_field (rli, field, NULL_TREE,
5652
                                         empty_base_offsets);
5653
          if (was_unnamed_p)
5654
            DECL_NAME (field) = NULL_TREE;
5655
          /* Now that layout has been performed, set the size of the
5656
             field to the size of its declared type; the rest of the
5657
             field is effectively invisible.  */
5658
          DECL_SIZE (field) = TYPE_SIZE (type);
5659
          /* We must also reset the DECL_MODE of the field.  */
5660
          if (abi_version_at_least (2))
5661
            DECL_MODE (field) = TYPE_MODE (type);
5662
          else if (warn_abi
5663
                   && DECL_MODE (field) != TYPE_MODE (type))
5664
            /* Versions of G++ before G++ 3.4 did not reset the
5665
               DECL_MODE.  */
5666
            warning (OPT_Wabi,
5667
                     "the offset of %qD may not be ABI-compliant and may "
5668
                     "change in a future version of GCC", field);
5669
        }
5670
      else
5671
        layout_nonempty_base_or_field (rli, field, NULL_TREE,
5672
                                       empty_base_offsets);
5673
 
5674
      /* Remember the location of any empty classes in FIELD.  */
5675
      if (abi_version_at_least (2))
5676
        record_subobject_offsets (TREE_TYPE (field),
5677
                                  byte_position(field),
5678
                                  empty_base_offsets,
5679
                                  /*is_data_member=*/true);
5680
 
5681
      /* If a bit-field does not immediately follow another bit-field,
5682
         and yet it starts in the middle of a byte, we have failed to
5683
         comply with the ABI.  */
5684
      if (warn_abi
5685
          && DECL_C_BIT_FIELD (field)
5686
          /* The TREE_NO_WARNING flag gets set by Objective-C when
5687
             laying out an Objective-C class.  The ObjC ABI differs
5688
             from the C++ ABI, and so we do not want a warning
5689
             here.  */
5690
          && !TREE_NO_WARNING (field)
5691
          && !last_field_was_bitfield
5692
          && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
5693
                                         DECL_FIELD_BIT_OFFSET (field),
5694
                                         bitsize_unit_node)))
5695
        warning (OPT_Wabi, "offset of %q+D is not ABI-compliant and may "
5696
                 "change in a future version of GCC", field);
5697
 
5698
      /* G++ used to use DECL_FIELD_OFFSET as if it were the byte
5699
         offset of the field.  */
5700
      if (warn_abi
5701
          && !abi_version_at_least (2)
5702
          && !tree_int_cst_equal (DECL_FIELD_OFFSET (field),
5703
                                  byte_position (field))
5704
          && contains_empty_class_p (TREE_TYPE (field)))
5705
        warning (OPT_Wabi, "%q+D contains empty classes which may cause base "
5706
                 "classes to be placed at different locations in a "
5707
                 "future version of GCC", field);
5708
 
5709
      /* The middle end uses the type of expressions to determine the
5710
         possible range of expression values.  In order to optimize
5711
         "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
5712
         must be made aware of the width of "i", via its type.
5713
 
5714
         Because C++ does not have integer types of arbitrary width,
5715
         we must (for the purposes of the front end) convert from the
5716
         type assigned here to the declared type of the bitfield
5717
         whenever a bitfield expression is used as an rvalue.
5718
         Similarly, when assigning a value to a bitfield, the value
5719
         must be converted to the type given the bitfield here.  */
5720
      if (DECL_C_BIT_FIELD (field))
5721
        {
5722
          unsigned HOST_WIDE_INT width;
5723
          tree ftype = TREE_TYPE (field);
5724
          width = tree_low_cst (DECL_SIZE (field), /*unsignedp=*/1);
5725
          if (width != TYPE_PRECISION (ftype))
5726
            {
5727
              TREE_TYPE (field)
5728
                = c_build_bitfield_integer_type (width,
5729
                                                 TYPE_UNSIGNED (ftype));
5730
              TREE_TYPE (field)
5731
                = cp_build_qualified_type (TREE_TYPE (field),
5732
                                           cp_type_quals (ftype));
5733
            }
5734
        }
5735
 
5736
      /* If we needed additional padding after this field, add it
5737
         now.  */
5738
      if (padding)
5739
        {
5740
          tree padding_field;
5741
 
5742
          padding_field = build_decl (input_location,
5743
                                      FIELD_DECL,
5744
                                      NULL_TREE,
5745
                                      char_type_node);
5746
          DECL_BIT_FIELD (padding_field) = 1;
5747
          DECL_SIZE (padding_field) = padding;
5748
          DECL_CONTEXT (padding_field) = t;
5749
          DECL_ARTIFICIAL (padding_field) = 1;
5750
          DECL_IGNORED_P (padding_field) = 1;
5751
          layout_nonempty_base_or_field (rli, padding_field,
5752
                                         NULL_TREE,
5753
                                         empty_base_offsets);
5754
        }
5755
 
5756
      last_field_was_bitfield = DECL_C_BIT_FIELD (field);
5757
    }
5758
 
5759
  if (abi_version_at_least (2) && !integer_zerop (rli->bitpos))
5760
    {
5761
      /* Make sure that we are on a byte boundary so that the size of
5762
         the class without virtual bases will always be a round number
5763
         of bytes.  */
5764
      rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
5765
      normalize_rli (rli);
5766
    }
5767
 
5768
  /* G++ 3.2 does not allow virtual bases to be overlaid with tail
5769
     padding.  */
5770
  if (!abi_version_at_least (2))
5771
    include_empty_classes(rli);
5772
 
5773
  /* Delete all zero-width bit-fields from the list of fields.  Now
5774
     that the type is laid out they are no longer important.  */
5775
  remove_zero_width_bit_fields (t);
5776
 
5777
  /* Create the version of T used for virtual bases.  We do not use
5778
     make_class_type for this version; this is an artificial type.  For
5779
     a POD type, we just reuse T.  */
5780
  if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
5781
    {
5782
      base_t = make_node (TREE_CODE (t));
5783
 
5784
      /* Set the size and alignment for the new type.  In G++ 3.2, all
5785
         empty classes were considered to have size zero when used as
5786
         base classes.  */
5787
      if (!abi_version_at_least (2) && CLASSTYPE_EMPTY_P (t))
5788
        {
5789
          TYPE_SIZE (base_t) = bitsize_zero_node;
5790
          TYPE_SIZE_UNIT (base_t) = size_zero_node;
5791
          if (warn_abi && !integer_zerop (rli_size_unit_so_far (rli)))
5792
            warning (OPT_Wabi,
5793
                     "layout of classes derived from empty class %qT "
5794
                     "may change in a future version of GCC",
5795
                     t);
5796
        }
5797
      else
5798
        {
5799
          tree eoc;
5800
 
5801
          /* If the ABI version is not at least two, and the last
5802
             field was a bit-field, RLI may not be on a byte
5803
             boundary.  In particular, rli_size_unit_so_far might
5804
             indicate the last complete byte, while rli_size_so_far
5805
             indicates the total number of bits used.  Therefore,
5806
             rli_size_so_far, rather than rli_size_unit_so_far, is
5807
             used to compute TYPE_SIZE_UNIT.  */
5808
          eoc = end_of_class (t, /*include_virtuals_p=*/0);
5809
          TYPE_SIZE_UNIT (base_t)
5810
            = size_binop (MAX_EXPR,
5811
                          convert (sizetype,
5812
                                   size_binop (CEIL_DIV_EXPR,
5813
                                               rli_size_so_far (rli),
5814
                                               bitsize_int (BITS_PER_UNIT))),
5815
                          eoc);
5816
          TYPE_SIZE (base_t)
5817
            = size_binop (MAX_EXPR,
5818
                          rli_size_so_far (rli),
5819
                          size_binop (MULT_EXPR,
5820
                                      convert (bitsizetype, eoc),
5821
                                      bitsize_int (BITS_PER_UNIT)));
5822
        }
5823
      TYPE_ALIGN (base_t) = rli->record_align;
5824
      TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
5825
 
5826
      /* Copy the fields from T.  */
5827
      next_field = &TYPE_FIELDS (base_t);
5828
      for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5829
        if (TREE_CODE (field) == FIELD_DECL)
5830
          {
5831
            *next_field = build_decl (input_location,
5832
                                      FIELD_DECL,
5833
                                      DECL_NAME (field),
5834
                                      TREE_TYPE (field));
5835
            DECL_CONTEXT (*next_field) = base_t;
5836
            DECL_FIELD_OFFSET (*next_field) = DECL_FIELD_OFFSET (field);
5837
            DECL_FIELD_BIT_OFFSET (*next_field)
5838
              = DECL_FIELD_BIT_OFFSET (field);
5839
            DECL_SIZE (*next_field) = DECL_SIZE (field);
5840
            DECL_MODE (*next_field) = DECL_MODE (field);
5841
            next_field = &DECL_CHAIN (*next_field);
5842
          }
5843
 
5844
      /* Record the base version of the type.  */
5845
      CLASSTYPE_AS_BASE (t) = base_t;
5846
      TYPE_CONTEXT (base_t) = t;
5847
    }
5848
  else
5849
    CLASSTYPE_AS_BASE (t) = t;
5850
 
5851
  /* Every empty class contains an empty class.  */
5852
  if (CLASSTYPE_EMPTY_P (t))
5853
    CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
5854
 
5855
  /* Set the TYPE_DECL for this type to contain the right
5856
     value for DECL_OFFSET, so that we can use it as part
5857
     of a COMPONENT_REF for multiple inheritance.  */
5858
  layout_decl (TYPE_MAIN_DECL (t), 0);
5859
 
5860
  /* Now fix up any virtual base class types that we left lying
5861
     around.  We must get these done before we try to lay out the
5862
     virtual function table.  As a side-effect, this will remove the
5863
     base subobject fields.  */
5864
  layout_virtual_bases (rli, empty_base_offsets);
5865
 
5866
  /* Make sure that empty classes are reflected in RLI at this
5867
     point.  */
5868
  include_empty_classes(rli);
5869
 
5870
  /* Make sure not to create any structures with zero size.  */
5871
  if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
5872
    place_field (rli,
5873
                 build_decl (input_location,
5874
                             FIELD_DECL, NULL_TREE, char_type_node));
5875
 
5876
  /* If this is a non-POD, declaring it packed makes a difference to how it
5877
     can be used as a field; don't let finalize_record_size undo it.  */
5878
  if (TYPE_PACKED (t) && !layout_pod_type_p (t))
5879
    rli->packed_maybe_necessary = true;
5880
 
5881
  /* Let the back end lay out the type.  */
5882
  finish_record_layout (rli, /*free_p=*/true);
5883
 
5884
  /* Warn about bases that can't be talked about due to ambiguity.  */
5885
  warn_about_ambiguous_bases (t);
5886
 
5887
  /* Now that we're done with layout, give the base fields the real types.  */
5888
  for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5889
    if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
5890
      TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
5891
 
5892
  /* Clean up.  */
5893
  splay_tree_delete (empty_base_offsets);
5894
 
5895
  if (CLASSTYPE_EMPTY_P (t)
5896
      && tree_int_cst_lt (sizeof_biggest_empty_class,
5897
                          TYPE_SIZE_UNIT (t)))
5898
    sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
5899
}
5900
 
5901
/* Determine the "key method" for the class type indicated by TYPE,
5902
   and set CLASSTYPE_KEY_METHOD accordingly.  */
5903
 
5904
void
5905
determine_key_method (tree type)
5906
{
5907
  tree method;
5908
 
5909
  if (TYPE_FOR_JAVA (type)
5910
      || processing_template_decl
5911
      || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
5912
      || CLASSTYPE_INTERFACE_KNOWN (type))
5913
    return;
5914
 
5915
  /* The key method is the first non-pure virtual function that is not
5916
     inline at the point of class definition.  On some targets the
5917
     key function may not be inline; those targets should not call
5918
     this function until the end of the translation unit.  */
5919
  for (method = TYPE_METHODS (type); method != NULL_TREE;
5920
       method = DECL_CHAIN (method))
5921
    if (DECL_VINDEX (method) != NULL_TREE
5922
        && ! DECL_DECLARED_INLINE_P (method)
5923
        && ! DECL_PURE_VIRTUAL_P (method))
5924
      {
5925
        CLASSTYPE_KEY_METHOD (type) = method;
5926
        break;
5927
      }
5928
 
5929
  return;
5930
}
5931
 
5932
 
5933
/* Allocate and return an instance of struct sorted_fields_type with
5934
   N fields.  */
5935
 
5936
static struct sorted_fields_type *
5937
sorted_fields_type_new (int n)
5938
{
5939
  struct sorted_fields_type *sft;
5940
  sft = ggc_alloc_sorted_fields_type (sizeof (struct sorted_fields_type)
5941
                                      + n * sizeof (tree));
5942
  sft->len = n;
5943
 
5944
  return sft;
5945
}
5946
 
5947
 
5948
/* Perform processing required when the definition of T (a class type)
5949
   is complete.  */
5950
 
5951
void
5952
finish_struct_1 (tree t)
5953
{
5954
  tree x;
5955
  /* A TREE_LIST.  The TREE_VALUE of each node is a FUNCTION_DECL.  */
5956
  tree virtuals = NULL_TREE;
5957
  int n_fields = 0;
5958
 
5959
  if (COMPLETE_TYPE_P (t))
5960
    {
5961
      gcc_assert (MAYBE_CLASS_TYPE_P (t));
5962
      error ("redefinition of %q#T", t);
5963
      popclass ();
5964
      return;
5965
    }
5966
 
5967
  /* If this type was previously laid out as a forward reference,
5968
     make sure we lay it out again.  */
5969
  TYPE_SIZE (t) = NULL_TREE;
5970
  CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
5971
 
5972
  /* Make assumptions about the class; we'll reset the flags if
5973
     necessary.  */
5974
  CLASSTYPE_EMPTY_P (t) = 1;
5975
  CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
5976
  CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
5977
  CLASSTYPE_LITERAL_P (t) = true;
5978
 
5979
  /* Do end-of-class semantic processing: checking the validity of the
5980
     bases and members and add implicitly generated methods.  */
5981
  check_bases_and_members (t);
5982
 
5983
  /* Find the key method.  */
5984
  if (TYPE_CONTAINS_VPTR_P (t))
5985
    {
5986
      /* The Itanium C++ ABI permits the key method to be chosen when
5987
         the class is defined -- even though the key method so
5988
         selected may later turn out to be an inline function.  On
5989
         some systems (such as ARM Symbian OS) the key method cannot
5990
         be determined until the end of the translation unit.  On such
5991
         systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
5992
         will cause the class to be added to KEYED_CLASSES.  Then, in
5993
         finish_file we will determine the key method.  */
5994
      if (targetm.cxx.key_method_may_be_inline ())
5995
        determine_key_method (t);
5996
 
5997
      /* If a polymorphic class has no key method, we may emit the vtable
5998
         in every translation unit where the class definition appears.  */
5999
      if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE)
6000
        keyed_classes = tree_cons (NULL_TREE, t, keyed_classes);
6001
    }
6002
 
6003
  /* Layout the class itself.  */
6004
  layout_class_type (t, &virtuals);
6005
  if (CLASSTYPE_AS_BASE (t) != t)
6006
    /* We use the base type for trivial assignments, and hence it
6007
       needs a mode.  */
6008
    compute_record_mode (CLASSTYPE_AS_BASE (t));
6009
 
6010
  virtuals = modify_all_vtables (t, nreverse (virtuals));
6011
 
6012
  /* If necessary, create the primary vtable for this class.  */
6013
  if (virtuals || TYPE_CONTAINS_VPTR_P (t))
6014
    {
6015
      /* We must enter these virtuals into the table.  */
6016
      if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6017
        build_primary_vtable (NULL_TREE, t);
6018
      else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
6019
        /* Here we know enough to change the type of our virtual
6020
           function table, but we will wait until later this function.  */
6021
        build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
6022
    }
6023
 
6024
  if (TYPE_CONTAINS_VPTR_P (t))
6025
    {
6026
      int vindex;
6027
      tree fn;
6028
 
6029
      if (BINFO_VTABLE (TYPE_BINFO (t)))
6030
        gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
6031
      if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6032
        gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
6033
 
6034
      /* Add entries for virtual functions introduced by this class.  */
6035
      BINFO_VIRTUALS (TYPE_BINFO (t))
6036
        = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
6037
 
6038
      /* Set DECL_VINDEX for all functions declared in this class.  */
6039
      for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
6040
           fn;
6041
           fn = TREE_CHAIN (fn),
6042
             vindex += (TARGET_VTABLE_USES_DESCRIPTORS
6043
                        ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
6044
        {
6045
          tree fndecl = BV_FN (fn);
6046
 
6047
          if (DECL_THUNK_P (fndecl))
6048
            /* A thunk. We should never be calling this entry directly
6049
               from this vtable -- we'd use the entry for the non
6050
               thunk base function.  */
6051
            DECL_VINDEX (fndecl) = NULL_TREE;
6052
          else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
6053
            DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
6054
        }
6055
    }
6056
 
6057
  finish_struct_bits (t);
6058
  set_method_tm_attributes (t);
6059
 
6060
  /* Complete the rtl for any static member objects of the type we're
6061
     working on.  */
6062
  for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6063
    if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
6064
        && TREE_TYPE (x) != error_mark_node
6065
        && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
6066
      DECL_MODE (x) = TYPE_MODE (t);
6067
 
6068
  /* Done with FIELDS...now decide whether to sort these for
6069
     faster lookups later.
6070
 
6071
     We use a small number because most searches fail (succeeding
6072
     ultimately as the search bores through the inheritance
6073
     hierarchy), and we want this failure to occur quickly.  */
6074
 
6075
  n_fields = count_fields (TYPE_FIELDS (t));
6076
  if (n_fields > 7)
6077
    {
6078
      struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
6079
      add_fields_to_record_type (TYPE_FIELDS (t), field_vec, 0);
6080
      qsort (field_vec->elts, n_fields, sizeof (tree),
6081
             field_decl_cmp);
6082
      CLASSTYPE_SORTED_FIELDS (t) = field_vec;
6083
    }
6084
 
6085
  /* Complain if one of the field types requires lower visibility.  */
6086
  constrain_class_visibility (t);
6087
 
6088
  /* Make the rtl for any new vtables we have created, and unmark
6089
     the base types we marked.  */
6090
  finish_vtbls (t);
6091
 
6092
  /* Build the VTT for T.  */
6093
  build_vtt (t);
6094
 
6095
  /* This warning does not make sense for Java classes, since they
6096
     cannot have destructors.  */
6097
  if (!TYPE_FOR_JAVA (t) && warn_nonvdtor && TYPE_POLYMORPHIC_P (t))
6098
    {
6099
      tree dtor;
6100
 
6101
      dtor = CLASSTYPE_DESTRUCTORS (t);
6102
      if (/* An implicitly declared destructor is always public.  And,
6103
             if it were virtual, we would have created it by now.  */
6104
          !dtor
6105
          || (!DECL_VINDEX (dtor)
6106
              && (/* public non-virtual */
6107
                  (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
6108
                   || (/* non-public non-virtual with friends */
6109
                       (TREE_PRIVATE (dtor) || TREE_PROTECTED (dtor))
6110
                        && (CLASSTYPE_FRIEND_CLASSES (t)
6111
                        || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))))))
6112
        warning (OPT_Wnon_virtual_dtor,
6113
                 "%q#T has virtual functions and accessible"
6114
                 " non-virtual destructor", t);
6115
    }
6116
 
6117
  complete_vars (t);
6118
 
6119
  if (warn_overloaded_virtual)
6120
    warn_hidden (t);
6121
 
6122
  /* Class layout, assignment of virtual table slots, etc., is now
6123
     complete.  Give the back end a chance to tweak the visibility of
6124
     the class or perform any other required target modifications.  */
6125
  targetm.cxx.adjust_class_at_definition (t);
6126
 
6127
  maybe_suppress_debug_info (t);
6128
 
6129
  dump_class_hierarchy (t);
6130
 
6131
  /* Finish debugging output for this type.  */
6132
  rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
6133
 
6134
  if (TYPE_TRANSPARENT_AGGR (t))
6135
    {
6136
      tree field = first_field (t);
6137
      if (field == NULL_TREE || error_operand_p (field))
6138
        {
6139
          error ("type transparent class %qT does not have any fields", t);
6140
          TYPE_TRANSPARENT_AGGR (t) = 0;
6141
        }
6142
      else if (DECL_ARTIFICIAL (field))
6143
        {
6144
          if (DECL_FIELD_IS_BASE (field))
6145
            error ("type transparent class %qT has base classes", t);
6146
          else
6147
            {
6148
              gcc_checking_assert (DECL_VIRTUAL_P (field));
6149
              error ("type transparent class %qT has virtual functions", t);
6150
            }
6151
          TYPE_TRANSPARENT_AGGR (t) = 0;
6152
        }
6153
    }
6154
}
6155
 
6156
/* When T was built up, the member declarations were added in reverse
6157
   order.  Rearrange them to declaration order.  */
6158
 
6159
void
6160
unreverse_member_declarations (tree t)
6161
{
6162
  tree next;
6163
  tree prev;
6164
  tree x;
6165
 
6166
  /* The following lists are all in reverse order.  Put them in
6167
     declaration order now.  */
6168
  TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
6169
  CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
6170
 
6171
  /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
6172
     reverse order, so we can't just use nreverse.  */
6173
  prev = NULL_TREE;
6174
  for (x = TYPE_FIELDS (t);
6175
       x && TREE_CODE (x) != TYPE_DECL;
6176
       x = next)
6177
    {
6178
      next = DECL_CHAIN (x);
6179
      DECL_CHAIN (x) = prev;
6180
      prev = x;
6181
    }
6182
  if (prev)
6183
    {
6184
      DECL_CHAIN (TYPE_FIELDS (t)) = x;
6185
      if (prev)
6186
        TYPE_FIELDS (t) = prev;
6187
    }
6188
}
6189
 
6190
tree
6191
finish_struct (tree t, tree attributes)
6192
{
6193
  location_t saved_loc = input_location;
6194
 
6195
  /* Now that we've got all the field declarations, reverse everything
6196
     as necessary.  */
6197
  unreverse_member_declarations (t);
6198
 
6199
  cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
6200
 
6201
  /* Nadger the current location so that diagnostics point to the start of
6202
     the struct, not the end.  */
6203
  input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
6204
 
6205
  if (processing_template_decl)
6206
    {
6207
      tree x;
6208
 
6209
      finish_struct_methods (t);
6210
      TYPE_SIZE (t) = bitsize_zero_node;
6211
      TYPE_SIZE_UNIT (t) = size_zero_node;
6212
 
6213
      /* We need to emit an error message if this type was used as a parameter
6214
         and it is an abstract type, even if it is a template. We construct
6215
         a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
6216
         account and we call complete_vars with this type, which will check
6217
         the PARM_DECLS. Note that while the type is being defined,
6218
         CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
6219
         (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it.  */
6220
      CLASSTYPE_PURE_VIRTUALS (t) = NULL;
6221
      for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
6222
        if (DECL_PURE_VIRTUAL_P (x))
6223
          VEC_safe_push (tree, gc, CLASSTYPE_PURE_VIRTUALS (t), x);
6224
      complete_vars (t);
6225
      /* We need to add the target functions to the CLASSTYPE_METHOD_VEC if
6226
         an enclosing scope is a template class, so that this function be
6227
         found by lookup_fnfields_1 when the using declaration is not
6228
         instantiated yet.  */
6229
      for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6230
        if (TREE_CODE (x) == USING_DECL)
6231
          {
6232
            tree fn = strip_using_decl (x);
6233
            if (is_overloaded_fn (fn))
6234
              for (; fn; fn = OVL_NEXT (fn))
6235
                add_method (t, OVL_CURRENT (fn), x);
6236
          }
6237
 
6238
      /* Remember current #pragma pack value.  */
6239
      TYPE_PRECISION (t) = maximum_field_alignment;
6240
    }
6241
  else
6242
    finish_struct_1 (t);
6243
 
6244
  input_location = saved_loc;
6245
 
6246
  TYPE_BEING_DEFINED (t) = 0;
6247
 
6248
  if (current_class_type)
6249
    popclass ();
6250
  else
6251
    error ("trying to finish struct, but kicked out due to previous parse errors");
6252
 
6253
  if (processing_template_decl && at_function_scope_p ())
6254
    add_stmt (build_min (TAG_DEFN, t));
6255
 
6256
  return t;
6257
}
6258
 
6259
/* Return the dynamic type of INSTANCE, if known.
6260
   Used to determine whether the virtual function table is needed
6261
   or not.
6262
 
6263
   *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
6264
   of our knowledge of its type.  *NONNULL should be initialized
6265
   before this function is called.  */
6266
 
6267
static tree
6268
fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
6269
{
6270
#define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
6271
 
6272
  switch (TREE_CODE (instance))
6273
    {
6274
    case INDIRECT_REF:
6275
      if (POINTER_TYPE_P (TREE_TYPE (instance)))
6276
        return NULL_TREE;
6277
      else
6278
        return RECUR (TREE_OPERAND (instance, 0));
6279
 
6280
    case CALL_EXPR:
6281
      /* This is a call to a constructor, hence it's never zero.  */
6282
      if (TREE_HAS_CONSTRUCTOR (instance))
6283
        {
6284
          if (nonnull)
6285
            *nonnull = 1;
6286
          return TREE_TYPE (instance);
6287
        }
6288
      return NULL_TREE;
6289
 
6290
    case SAVE_EXPR:
6291
      /* This is a call to a constructor, hence it's never zero.  */
6292
      if (TREE_HAS_CONSTRUCTOR (instance))
6293
        {
6294
          if (nonnull)
6295
            *nonnull = 1;
6296
          return TREE_TYPE (instance);
6297
        }
6298
      return RECUR (TREE_OPERAND (instance, 0));
6299
 
6300
    case POINTER_PLUS_EXPR:
6301
    case PLUS_EXPR:
6302
    case MINUS_EXPR:
6303
      if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
6304
        return RECUR (TREE_OPERAND (instance, 0));
6305
      if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
6306
        /* Propagate nonnull.  */
6307
        return RECUR (TREE_OPERAND (instance, 0));
6308
 
6309
      return NULL_TREE;
6310
 
6311
    CASE_CONVERT:
6312
      return RECUR (TREE_OPERAND (instance, 0));
6313
 
6314
    case ADDR_EXPR:
6315
      instance = TREE_OPERAND (instance, 0);
6316
      if (nonnull)
6317
        {
6318
          /* Just because we see an ADDR_EXPR doesn't mean we're dealing
6319
             with a real object -- given &p->f, p can still be null.  */
6320
          tree t = get_base_address (instance);
6321
          /* ??? Probably should check DECL_WEAK here.  */
6322
          if (t && DECL_P (t))
6323
            *nonnull = 1;
6324
        }
6325
      return RECUR (instance);
6326
 
6327
    case COMPONENT_REF:
6328
      /* If this component is really a base class reference, then the field
6329
         itself isn't definitive.  */
6330
      if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
6331
        return RECUR (TREE_OPERAND (instance, 0));
6332
      return RECUR (TREE_OPERAND (instance, 1));
6333
 
6334
    case VAR_DECL:
6335
    case FIELD_DECL:
6336
      if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
6337
          && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
6338
        {
6339
          if (nonnull)
6340
            *nonnull = 1;
6341
          return TREE_TYPE (TREE_TYPE (instance));
6342
        }
6343
      /* fall through...  */
6344
    case TARGET_EXPR:
6345
    case PARM_DECL:
6346
    case RESULT_DECL:
6347
      if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
6348
        {
6349
          if (nonnull)
6350
            *nonnull = 1;
6351
          return TREE_TYPE (instance);
6352
        }
6353
      else if (instance == current_class_ptr)
6354
        {
6355
          if (nonnull)
6356
            *nonnull = 1;
6357
 
6358
          /* if we're in a ctor or dtor, we know our type.  If
6359
             current_class_ptr is set but we aren't in a function, we're in
6360
             an NSDMI (and therefore a constructor).  */
6361
          if (current_scope () != current_function_decl
6362
              || (DECL_LANG_SPECIFIC (current_function_decl)
6363
                  && (DECL_CONSTRUCTOR_P (current_function_decl)
6364
                      || DECL_DESTRUCTOR_P (current_function_decl))))
6365
            {
6366
              if (cdtorp)
6367
                *cdtorp = 1;
6368
              return TREE_TYPE (TREE_TYPE (instance));
6369
            }
6370
        }
6371
      else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
6372
        {
6373
          /* We only need one hash table because it is always left empty.  */
6374
          static htab_t ht;
6375
          if (!ht)
6376
            ht = htab_create (37,
6377
                              htab_hash_pointer,
6378
                              htab_eq_pointer,
6379
                              /*htab_del=*/NULL);
6380
 
6381
          /* Reference variables should be references to objects.  */
6382
          if (nonnull)
6383
            *nonnull = 1;
6384
 
6385
          /* Enter the INSTANCE in a table to prevent recursion; a
6386
             variable's initializer may refer to the variable
6387
             itself.  */
6388
          if (TREE_CODE (instance) == VAR_DECL
6389
              && DECL_INITIAL (instance)
6390
              && !type_dependent_expression_p_push (DECL_INITIAL (instance))
6391
              && !htab_find (ht, instance))
6392
            {
6393
              tree type;
6394
              void **slot;
6395
 
6396
              slot = htab_find_slot (ht, instance, INSERT);
6397
              *slot = instance;
6398
              type = RECUR (DECL_INITIAL (instance));
6399
              htab_remove_elt (ht, instance);
6400
 
6401
              return type;
6402
            }
6403
        }
6404
      return NULL_TREE;
6405
 
6406
    default:
6407
      return NULL_TREE;
6408
    }
6409
#undef RECUR
6410
}
6411
 
6412
/* Return nonzero if the dynamic type of INSTANCE is known, and
6413
   equivalent to the static type.  We also handle the case where
6414
   INSTANCE is really a pointer. Return negative if this is a
6415
   ctor/dtor. There the dynamic type is known, but this might not be
6416
   the most derived base of the original object, and hence virtual
6417
   bases may not be layed out according to this type.
6418
 
6419
   Used to determine whether the virtual function table is needed
6420
   or not.
6421
 
6422
   *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
6423
   of our knowledge of its type.  *NONNULL should be initialized
6424
   before this function is called.  */
6425
 
6426
int
6427
resolves_to_fixed_type_p (tree instance, int* nonnull)
6428
{
6429
  tree t = TREE_TYPE (instance);
6430
  int cdtorp = 0;
6431
  tree fixed;
6432
 
6433
  if (processing_template_decl)
6434
    {
6435
      /* In a template we only care about the type of the result.  */
6436
      if (nonnull)
6437
        *nonnull = true;
6438
      return true;
6439
    }
6440
 
6441
  fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
6442
  if (fixed == NULL_TREE)
6443
    return 0;
6444
  if (POINTER_TYPE_P (t))
6445
    t = TREE_TYPE (t);
6446
  if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
6447
    return 0;
6448
  return cdtorp ? -1 : 1;
6449
}
6450
 
6451
 
6452
void
6453
init_class_processing (void)
6454
{
6455
  current_class_depth = 0;
6456
  current_class_stack_size = 10;
6457
  current_class_stack
6458
    = XNEWVEC (struct class_stack_node, current_class_stack_size);
6459
  local_classes = VEC_alloc (tree, gc, 8);
6460
  sizeof_biggest_empty_class = size_zero_node;
6461
 
6462
  ridpointers[(int) RID_PUBLIC] = access_public_node;
6463
  ridpointers[(int) RID_PRIVATE] = access_private_node;
6464
  ridpointers[(int) RID_PROTECTED] = access_protected_node;
6465
}
6466
 
6467
/* Restore the cached PREVIOUS_CLASS_LEVEL.  */
6468
 
6469
static void
6470
restore_class_cache (void)
6471
{
6472
  tree type;
6473
 
6474
  /* We are re-entering the same class we just left, so we don't
6475
     have to search the whole inheritance matrix to find all the
6476
     decls to bind again.  Instead, we install the cached
6477
     class_shadowed list and walk through it binding names.  */
6478
  push_binding_level (previous_class_level);
6479
  class_binding_level = previous_class_level;
6480
  /* Restore IDENTIFIER_TYPE_VALUE.  */
6481
  for (type = class_binding_level->type_shadowed;
6482
       type;
6483
       type = TREE_CHAIN (type))
6484
    SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
6485
}
6486
 
6487
/* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
6488
   appropriate for TYPE.
6489
 
6490
   So that we may avoid calls to lookup_name, we cache the _TYPE
6491
   nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
6492
 
6493
   For multiple inheritance, we perform a two-pass depth-first search
6494
   of the type lattice.  */
6495
 
6496
void
6497
pushclass (tree type)
6498
{
6499
  class_stack_node_t csn;
6500
 
6501
  type = TYPE_MAIN_VARIANT (type);
6502
 
6503
  /* Make sure there is enough room for the new entry on the stack.  */
6504
  if (current_class_depth + 1 >= current_class_stack_size)
6505
    {
6506
      current_class_stack_size *= 2;
6507
      current_class_stack
6508
        = XRESIZEVEC (struct class_stack_node, current_class_stack,
6509
                      current_class_stack_size);
6510
    }
6511
 
6512
  /* Insert a new entry on the class stack.  */
6513
  csn = current_class_stack + current_class_depth;
6514
  csn->name = current_class_name;
6515
  csn->type = current_class_type;
6516
  csn->access = current_access_specifier;
6517
  csn->names_used = 0;
6518
  csn->hidden = 0;
6519
  current_class_depth++;
6520
 
6521
  /* Now set up the new type.  */
6522
  current_class_name = TYPE_NAME (type);
6523
  if (TREE_CODE (current_class_name) == TYPE_DECL)
6524
    current_class_name = DECL_NAME (current_class_name);
6525
  current_class_type = type;
6526
 
6527
  /* By default, things in classes are private, while things in
6528
     structures or unions are public.  */
6529
  current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
6530
                              ? access_private_node
6531
                              : access_public_node);
6532
 
6533
  if (previous_class_level
6534
      && type != previous_class_level->this_entity
6535
      && current_class_depth == 1)
6536
    {
6537
      /* Forcibly remove any old class remnants.  */
6538
      invalidate_class_lookup_cache ();
6539
    }
6540
 
6541
  if (!previous_class_level
6542
      || type != previous_class_level->this_entity
6543
      || current_class_depth > 1)
6544
    pushlevel_class ();
6545
  else
6546
    restore_class_cache ();
6547
}
6548
 
6549
/* When we exit a toplevel class scope, we save its binding level so
6550
   that we can restore it quickly.  Here, we've entered some other
6551
   class, so we must invalidate our cache.  */
6552
 
6553
void
6554
invalidate_class_lookup_cache (void)
6555
{
6556
  previous_class_level = NULL;
6557
}
6558
 
6559
/* Get out of the current class scope. If we were in a class scope
6560
   previously, that is the one popped to.  */
6561
 
6562
void
6563
popclass (void)
6564
{
6565
  poplevel_class ();
6566
 
6567
  current_class_depth--;
6568
  current_class_name = current_class_stack[current_class_depth].name;
6569
  current_class_type = current_class_stack[current_class_depth].type;
6570
  current_access_specifier = current_class_stack[current_class_depth].access;
6571
  if (current_class_stack[current_class_depth].names_used)
6572
    splay_tree_delete (current_class_stack[current_class_depth].names_used);
6573
}
6574
 
6575
/* Mark the top of the class stack as hidden.  */
6576
 
6577
void
6578
push_class_stack (void)
6579
{
6580
  if (current_class_depth)
6581
    ++current_class_stack[current_class_depth - 1].hidden;
6582
}
6583
 
6584
/* Mark the top of the class stack as un-hidden.  */
6585
 
6586
void
6587
pop_class_stack (void)
6588
{
6589
  if (current_class_depth)
6590
    --current_class_stack[current_class_depth - 1].hidden;
6591
}
6592
 
6593
/* Returns 1 if the class type currently being defined is either T or
6594
   a nested type of T.  */
6595
 
6596
bool
6597
currently_open_class (tree t)
6598
{
6599
  int i;
6600
 
6601
  if (!CLASS_TYPE_P (t))
6602
    return false;
6603
 
6604
  t = TYPE_MAIN_VARIANT (t);
6605
 
6606
  /* We start looking from 1 because entry 0 is from global scope,
6607
     and has no type.  */
6608
  for (i = current_class_depth; i > 0; --i)
6609
    {
6610
      tree c;
6611
      if (i == current_class_depth)
6612
        c = current_class_type;
6613
      else
6614
        {
6615
          if (current_class_stack[i].hidden)
6616
            break;
6617
          c = current_class_stack[i].type;
6618
        }
6619
      if (!c)
6620
        continue;
6621
      if (same_type_p (c, t))
6622
        return true;
6623
    }
6624
  return false;
6625
}
6626
 
6627
/* If either current_class_type or one of its enclosing classes are derived
6628
   from T, return the appropriate type.  Used to determine how we found
6629
   something via unqualified lookup.  */
6630
 
6631
tree
6632
currently_open_derived_class (tree t)
6633
{
6634
  int i;
6635
 
6636
  /* The bases of a dependent type are unknown.  */
6637
  if (dependent_type_p (t))
6638
    return NULL_TREE;
6639
 
6640
  if (!current_class_type)
6641
    return NULL_TREE;
6642
 
6643
  if (DERIVED_FROM_P (t, current_class_type))
6644
    return current_class_type;
6645
 
6646
  for (i = current_class_depth - 1; i > 0; --i)
6647
    {
6648
      if (current_class_stack[i].hidden)
6649
        break;
6650
      if (DERIVED_FROM_P (t, current_class_stack[i].type))
6651
        return current_class_stack[i].type;
6652
    }
6653
 
6654
  return NULL_TREE;
6655
}
6656
 
6657
/* Returns the innermost class type which is not a lambda closure type.  */
6658
 
6659
tree
6660
current_nonlambda_class_type (void)
6661
{
6662
  int i;
6663
 
6664
  /* We start looking from 1 because entry 0 is from global scope,
6665
     and has no type.  */
6666
  for (i = current_class_depth; i > 0; --i)
6667
    {
6668
      tree c;
6669
      if (i == current_class_depth)
6670
        c = current_class_type;
6671
      else
6672
        {
6673
          if (current_class_stack[i].hidden)
6674
            break;
6675
          c = current_class_stack[i].type;
6676
        }
6677
      if (!c)
6678
        continue;
6679
      if (!LAMBDA_TYPE_P (c))
6680
        return c;
6681
    }
6682
  return NULL_TREE;
6683
}
6684
 
6685
/* When entering a class scope, all enclosing class scopes' names with
6686
   static meaning (static variables, static functions, types and
6687
   enumerators) have to be visible.  This recursive function calls
6688
   pushclass for all enclosing class contexts until global or a local
6689
   scope is reached.  TYPE is the enclosed class.  */
6690
 
6691
void
6692
push_nested_class (tree type)
6693
{
6694
  /* A namespace might be passed in error cases, like A::B:C.  */
6695
  if (type == NULL_TREE
6696
      || !CLASS_TYPE_P (type))
6697
    return;
6698
 
6699
  push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
6700
 
6701
  pushclass (type);
6702
}
6703
 
6704
/* Undoes a push_nested_class call.  */
6705
 
6706
void
6707
pop_nested_class (void)
6708
{
6709
  tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
6710
 
6711
  popclass ();
6712
  if (context && CLASS_TYPE_P (context))
6713
    pop_nested_class ();
6714
}
6715
 
6716
/* Returns the number of extern "LANG" blocks we are nested within.  */
6717
 
6718
int
6719
current_lang_depth (void)
6720
{
6721
  return VEC_length (tree, current_lang_base);
6722
}
6723
 
6724
/* Set global variables CURRENT_LANG_NAME to appropriate value
6725
   so that behavior of name-mangling machinery is correct.  */
6726
 
6727
void
6728
push_lang_context (tree name)
6729
{
6730
  VEC_safe_push (tree, gc, current_lang_base, current_lang_name);
6731
 
6732
  if (name == lang_name_cplusplus)
6733
    {
6734
      current_lang_name = name;
6735
    }
6736
  else if (name == lang_name_java)
6737
    {
6738
      current_lang_name = name;
6739
      /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
6740
         (See record_builtin_java_type in decl.c.)  However, that causes
6741
         incorrect debug entries if these types are actually used.
6742
         So we re-enable debug output after extern "Java".  */
6743
      DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
6744
      DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
6745
      DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
6746
      DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
6747
      DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
6748
      DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
6749
      DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
6750
      DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
6751
    }
6752
  else if (name == lang_name_c)
6753
    {
6754
      current_lang_name = name;
6755
    }
6756
  else
6757
    error ("language string %<\"%E\"%> not recognized", name);
6758
}
6759
 
6760
/* Get out of the current language scope.  */
6761
 
6762
void
6763
pop_lang_context (void)
6764
{
6765
  current_lang_name = VEC_pop (tree, current_lang_base);
6766
}
6767
 
6768
/* Type instantiation routines.  */
6769
 
6770
/* Given an OVERLOAD and a TARGET_TYPE, return the function that
6771
   matches the TARGET_TYPE.  If there is no satisfactory match, return
6772
   error_mark_node, and issue an error & warning messages under
6773
   control of FLAGS.  Permit pointers to member function if FLAGS
6774
   permits.  If TEMPLATE_ONLY, the name of the overloaded function was
6775
   a template-id, and EXPLICIT_TARGS are the explicitly provided
6776
   template arguments.
6777
 
6778
   If OVERLOAD is for one or more member functions, then ACCESS_PATH
6779
   is the base path used to reference those member functions.  If
6780
   TF_NO_ACCESS_CONTROL is not set in FLAGS, and the address is
6781
   resolved to a member function, access checks will be performed and
6782
   errors issued if appropriate.  */
6783
 
6784
static tree
6785
resolve_address_of_overloaded_function (tree target_type,
6786
                                        tree overload,
6787
                                        tsubst_flags_t flags,
6788
                                        bool template_only,
6789
                                        tree explicit_targs,
6790
                                        tree access_path)
6791
{
6792
  /* Here's what the standard says:
6793
 
6794
       [over.over]
6795
 
6796
       If the name is a function template, template argument deduction
6797
       is done, and if the argument deduction succeeds, the deduced
6798
       arguments are used to generate a single template function, which
6799
       is added to the set of overloaded functions considered.
6800
 
6801
       Non-member functions and static member functions match targets of
6802
       type "pointer-to-function" or "reference-to-function."  Nonstatic
6803
       member functions match targets of type "pointer-to-member
6804
       function;" the function type of the pointer to member is used to
6805
       select the member function from the set of overloaded member
6806
       functions.  If a nonstatic member function is selected, the
6807
       reference to the overloaded function name is required to have the
6808
       form of a pointer to member as described in 5.3.1.
6809
 
6810
       If more than one function is selected, any template functions in
6811
       the set are eliminated if the set also contains a non-template
6812
       function, and any given template function is eliminated if the
6813
       set contains a second template function that is more specialized
6814
       than the first according to the partial ordering rules 14.5.5.2.
6815
       After such eliminations, if any, there shall remain exactly one
6816
       selected function.  */
6817
 
6818
  int is_ptrmem = 0;
6819
  /* We store the matches in a TREE_LIST rooted here.  The functions
6820
     are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
6821
     interoperability with most_specialized_instantiation.  */
6822
  tree matches = NULL_TREE;
6823
  tree fn;
6824
  tree target_fn_type;
6825
 
6826
  /* By the time we get here, we should be seeing only real
6827
     pointer-to-member types, not the internal POINTER_TYPE to
6828
     METHOD_TYPE representation.  */
6829
  gcc_assert (TREE_CODE (target_type) != POINTER_TYPE
6830
              || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
6831
 
6832
  gcc_assert (is_overloaded_fn (overload));
6833
 
6834
  /* Check that the TARGET_TYPE is reasonable.  */
6835
  if (TYPE_PTRFN_P (target_type))
6836
    /* This is OK.  */;
6837
  else if (TYPE_PTRMEMFUNC_P (target_type))
6838
    /* This is OK, too.  */
6839
    is_ptrmem = 1;
6840
  else if (TREE_CODE (target_type) == FUNCTION_TYPE)
6841
    /* This is OK, too.  This comes from a conversion to reference
6842
       type.  */
6843
    target_type = build_reference_type (target_type);
6844
  else
6845
    {
6846
      if (flags & tf_error)
6847
        error ("cannot resolve overloaded function %qD based on"
6848
               " conversion to type %qT",
6849
               DECL_NAME (OVL_FUNCTION (overload)), target_type);
6850
      return error_mark_node;
6851
    }
6852
 
6853
  /* Non-member functions and static member functions match targets of type
6854
     "pointer-to-function" or "reference-to-function."  Nonstatic member
6855
     functions match targets of type "pointer-to-member-function;" the
6856
     function type of the pointer to member is used to select the member
6857
     function from the set of overloaded member functions.
6858
 
6859
     So figure out the FUNCTION_TYPE that we want to match against.  */
6860
  target_fn_type = static_fn_type (target_type);
6861
 
6862
  /* If we can find a non-template function that matches, we can just
6863
     use it.  There's no point in generating template instantiations
6864
     if we're just going to throw them out anyhow.  But, of course, we
6865
     can only do this when we don't *need* a template function.  */
6866
  if (!template_only)
6867
    {
6868
      tree fns;
6869
 
6870
      for (fns = overload; fns; fns = OVL_NEXT (fns))
6871
        {
6872
          tree fn = OVL_CURRENT (fns);
6873
 
6874
          if (TREE_CODE (fn) == TEMPLATE_DECL)
6875
            /* We're not looking for templates just yet.  */
6876
            continue;
6877
 
6878
          if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
6879
              != is_ptrmem)
6880
            /* We're looking for a non-static member, and this isn't
6881
               one, or vice versa.  */
6882
            continue;
6883
 
6884
          /* Ignore functions which haven't been explicitly
6885
             declared.  */
6886
          if (DECL_ANTICIPATED (fn))
6887
            continue;
6888
 
6889
          /* See if there's a match.  */
6890
          if (same_type_p (target_fn_type, static_fn_type (fn)))
6891
            matches = tree_cons (fn, NULL_TREE, matches);
6892
        }
6893
    }
6894
 
6895
  /* Now, if we've already got a match (or matches), there's no need
6896
     to proceed to the template functions.  But, if we don't have a
6897
     match we need to look at them, too.  */
6898
  if (!matches)
6899
    {
6900
      tree target_arg_types;
6901
      tree target_ret_type;
6902
      tree fns;
6903
      tree *args;
6904
      unsigned int nargs, ia;
6905
      tree arg;
6906
 
6907
      target_arg_types = TYPE_ARG_TYPES (target_fn_type);
6908
      target_ret_type = TREE_TYPE (target_fn_type);
6909
 
6910
      nargs = list_length (target_arg_types);
6911
      args = XALLOCAVEC (tree, nargs);
6912
      for (arg = target_arg_types, ia = 0;
6913
           arg != NULL_TREE && arg != void_list_node;
6914
           arg = TREE_CHAIN (arg), ++ia)
6915
        args[ia] = TREE_VALUE (arg);
6916
      nargs = ia;
6917
 
6918
      for (fns = overload; fns; fns = OVL_NEXT (fns))
6919
        {
6920
          tree fn = OVL_CURRENT (fns);
6921
          tree instantiation;
6922
          tree targs;
6923
 
6924
          if (TREE_CODE (fn) != TEMPLATE_DECL)
6925
            /* We're only looking for templates.  */
6926
            continue;
6927
 
6928
          if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
6929
              != is_ptrmem)
6930
            /* We're not looking for a non-static member, and this is
6931
               one, or vice versa.  */
6932
            continue;
6933
 
6934
          /* Try to do argument deduction.  */
6935
          targs = make_tree_vec (DECL_NTPARMS (fn));
6936
          if (fn_type_unification (fn, explicit_targs, targs, args, nargs,
6937
                                   target_ret_type, DEDUCE_EXACT,
6938
                                   LOOKUP_NORMAL, false))
6939
            /* Argument deduction failed.  */
6940
            continue;
6941
 
6942
          /* Instantiate the template.  */
6943
          instantiation = instantiate_template (fn, targs, flags);
6944
          if (instantiation == error_mark_node)
6945
            /* Instantiation failed.  */
6946
            continue;
6947
 
6948
          /* See if there's a match.  */
6949
          if (same_type_p (target_fn_type, static_fn_type (instantiation)))
6950
            matches = tree_cons (instantiation, fn, matches);
6951
        }
6952
 
6953
      /* Now, remove all but the most specialized of the matches.  */
6954
      if (matches)
6955
        {
6956
          tree match = most_specialized_instantiation (matches);
6957
 
6958
          if (match != error_mark_node)
6959
            matches = tree_cons (TREE_PURPOSE (match),
6960
                                 NULL_TREE,
6961
                                 NULL_TREE);
6962
        }
6963
    }
6964
 
6965
  /* Now we should have exactly one function in MATCHES.  */
6966
  if (matches == NULL_TREE)
6967
    {
6968
      /* There were *no* matches.  */
6969
      if (flags & tf_error)
6970
        {
6971
          error ("no matches converting function %qD to type %q#T",
6972
                 DECL_NAME (OVL_CURRENT (overload)),
6973
                 target_type);
6974
 
6975
          print_candidates (overload);
6976
        }
6977
      return error_mark_node;
6978
    }
6979
  else if (TREE_CHAIN (matches))
6980
    {
6981
      /* There were too many matches.  First check if they're all
6982
         the same function.  */
6983
      tree match;
6984
 
6985
      fn = TREE_PURPOSE (matches);
6986
      for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
6987
        if (!decls_match (fn, TREE_PURPOSE (match)))
6988
          break;
6989
 
6990
      if (match)
6991
        {
6992
          if (flags & tf_error)
6993
            {
6994
              error ("converting overloaded function %qD to type %q#T is ambiguous",
6995
                     DECL_NAME (OVL_FUNCTION (overload)),
6996
                     target_type);
6997
 
6998
              /* Since print_candidates expects the functions in the
6999
                 TREE_VALUE slot, we flip them here.  */
7000
              for (match = matches; match; match = TREE_CHAIN (match))
7001
                TREE_VALUE (match) = TREE_PURPOSE (match);
7002
 
7003
              print_candidates (matches);
7004
            }
7005
 
7006
          return error_mark_node;
7007
        }
7008
    }
7009
 
7010
  /* Good, exactly one match.  Now, convert it to the correct type.  */
7011
  fn = TREE_PURPOSE (matches);
7012
 
7013
  if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
7014
      && !(flags & tf_ptrmem_ok) && !flag_ms_extensions)
7015
    {
7016
      static int explained;
7017
 
7018
      if (!(flags & tf_error))
7019
        return error_mark_node;
7020
 
7021
      permerror (input_location, "assuming pointer to member %qD", fn);
7022
      if (!explained)
7023
        {
7024
          inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn);
7025
          explained = 1;
7026
        }
7027
    }
7028
 
7029
  /* If we're doing overload resolution purely for the purpose of
7030
     determining conversion sequences, we should not consider the
7031
     function used.  If this conversion sequence is selected, the
7032
     function will be marked as used at this point.  */
7033
  if (!(flags & tf_conv))
7034
    {
7035
      /* Make =delete work with SFINAE.  */
7036
      if (DECL_DELETED_FN (fn) && !(flags & tf_error))
7037
        return error_mark_node;
7038
 
7039
      mark_used (fn);
7040
    }
7041
 
7042
  /* We could not check access to member functions when this
7043
     expression was originally created since we did not know at that
7044
     time to which function the expression referred.  */
7045
  if (!(flags & tf_no_access_control)
7046
      && DECL_FUNCTION_MEMBER_P (fn))
7047
    {
7048
      gcc_assert (access_path);
7049
      perform_or_defer_access_check (access_path, fn, fn);
7050
    }
7051
 
7052
  if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
7053
    return cp_build_addr_expr (fn, flags);
7054
  else
7055
    {
7056
      /* The target must be a REFERENCE_TYPE.  Above, cp_build_unary_op
7057
         will mark the function as addressed, but here we must do it
7058
         explicitly.  */
7059
      cxx_mark_addressable (fn);
7060
 
7061
      return fn;
7062
    }
7063
}
7064
 
7065
/* This function will instantiate the type of the expression given in
7066
   RHS to match the type of LHSTYPE.  If errors exist, then return
7067
   error_mark_node. FLAGS is a bit mask.  If TF_ERROR is set, then
7068
   we complain on errors.  If we are not complaining, never modify rhs,
7069
   as overload resolution wants to try many possible instantiations, in
7070
   the hope that at least one will work.
7071
 
7072
   For non-recursive calls, LHSTYPE should be a function, pointer to
7073
   function, or a pointer to member function.  */
7074
 
7075
tree
7076
instantiate_type (tree lhstype, tree rhs, tsubst_flags_t flags)
7077
{
7078
  tsubst_flags_t flags_in = flags;
7079
  tree access_path = NULL_TREE;
7080
 
7081
  flags &= ~tf_ptrmem_ok;
7082
 
7083
  if (lhstype == unknown_type_node)
7084
    {
7085
      if (flags & tf_error)
7086
        error ("not enough type information");
7087
      return error_mark_node;
7088
    }
7089
 
7090
  if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
7091
    {
7092
      if (same_type_p (lhstype, TREE_TYPE (rhs)))
7093
        return rhs;
7094
      if (flag_ms_extensions
7095
          && TYPE_PTRMEMFUNC_P (lhstype)
7096
          && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
7097
        /* Microsoft allows `A::f' to be resolved to a
7098
           pointer-to-member.  */
7099
        ;
7100
      else
7101
        {
7102
          if (flags & tf_error)
7103
            error ("cannot convert %qE from type %qT to type %qT",
7104
                   rhs, TREE_TYPE (rhs), lhstype);
7105
          return error_mark_node;
7106
        }
7107
    }
7108
 
7109
  if (BASELINK_P (rhs))
7110
    {
7111
      access_path = BASELINK_ACCESS_BINFO (rhs);
7112
      rhs = BASELINK_FUNCTIONS (rhs);
7113
    }
7114
 
7115
  /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
7116
     deduce any type information.  */
7117
  if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
7118
    {
7119
      if (flags & tf_error)
7120
        error ("not enough type information");
7121
      return error_mark_node;
7122
    }
7123
 
7124
  /* There only a few kinds of expressions that may have a type
7125
     dependent on overload resolution.  */
7126
  gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
7127
              || TREE_CODE (rhs) == COMPONENT_REF
7128
              || really_overloaded_fn (rhs)
7129
              || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
7130
 
7131
  /* This should really only be used when attempting to distinguish
7132
     what sort of a pointer to function we have.  For now, any
7133
     arithmetic operation which is not supported on pointers
7134
     is rejected as an error.  */
7135
 
7136
  switch (TREE_CODE (rhs))
7137
    {
7138
    case COMPONENT_REF:
7139
      {
7140
        tree member = TREE_OPERAND (rhs, 1);
7141
 
7142
        member = instantiate_type (lhstype, member, flags);
7143
        if (member != error_mark_node
7144
            && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
7145
          /* Do not lose object's side effects.  */
7146
          return build2 (COMPOUND_EXPR, TREE_TYPE (member),
7147
                         TREE_OPERAND (rhs, 0), member);
7148
        return member;
7149
      }
7150
 
7151
    case OFFSET_REF:
7152
      rhs = TREE_OPERAND (rhs, 1);
7153
      if (BASELINK_P (rhs))
7154
        return instantiate_type (lhstype, rhs, flags_in);
7155
 
7156
      /* This can happen if we are forming a pointer-to-member for a
7157
         member template.  */
7158
      gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
7159
 
7160
      /* Fall through.  */
7161
 
7162
    case TEMPLATE_ID_EXPR:
7163
      {
7164
        tree fns = TREE_OPERAND (rhs, 0);
7165
        tree args = TREE_OPERAND (rhs, 1);
7166
 
7167
        return
7168
          resolve_address_of_overloaded_function (lhstype, fns, flags_in,
7169
                                                  /*template_only=*/true,
7170
                                                  args, access_path);
7171
      }
7172
 
7173
    case OVERLOAD:
7174
    case FUNCTION_DECL:
7175
      return
7176
        resolve_address_of_overloaded_function (lhstype, rhs, flags_in,
7177
                                                /*template_only=*/false,
7178
                                                /*explicit_targs=*/NULL_TREE,
7179
                                                access_path);
7180
 
7181
    case ADDR_EXPR:
7182
    {
7183
      if (PTRMEM_OK_P (rhs))
7184
        flags |= tf_ptrmem_ok;
7185
 
7186
      return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
7187
    }
7188
 
7189
    case ERROR_MARK:
7190
      return error_mark_node;
7191
 
7192
    default:
7193
      gcc_unreachable ();
7194
    }
7195
  return error_mark_node;
7196
}
7197
 
7198
/* Return the name of the virtual function pointer field
7199
   (as an IDENTIFIER_NODE) for the given TYPE.  Note that
7200
   this may have to look back through base types to find the
7201
   ultimate field name.  (For single inheritance, these could
7202
   all be the same name.  Who knows for multiple inheritance).  */
7203
 
7204
static tree
7205
get_vfield_name (tree type)
7206
{
7207
  tree binfo, base_binfo;
7208
  char *buf;
7209
 
7210
  for (binfo = TYPE_BINFO (type);
7211
       BINFO_N_BASE_BINFOS (binfo);
7212
       binfo = base_binfo)
7213
    {
7214
      base_binfo = BINFO_BASE_BINFO (binfo, 0);
7215
 
7216
      if (BINFO_VIRTUAL_P (base_binfo)
7217
          || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
7218
        break;
7219
    }
7220
 
7221
  type = BINFO_TYPE (binfo);
7222
  buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
7223
                         + TYPE_NAME_LENGTH (type) + 2);
7224
  sprintf (buf, VFIELD_NAME_FORMAT,
7225
           IDENTIFIER_POINTER (constructor_name (type)));
7226
  return get_identifier (buf);
7227
}
7228
 
7229
void
7230
print_class_statistics (void)
7231
{
7232
#ifdef GATHER_STATISTICS
7233
  fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
7234
  fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
7235
  if (n_vtables)
7236
    {
7237
      fprintf (stderr, "vtables = %d; vtable searches = %d\n",
7238
               n_vtables, n_vtable_searches);
7239
      fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
7240
               n_vtable_entries, n_vtable_elems);
7241
    }
7242
#endif
7243
}
7244
 
7245
/* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
7246
   according to [class]:
7247
                                          The class-name is also inserted
7248
   into  the scope of the class itself.  For purposes of access checking,
7249
   the inserted class name is treated as if it were a public member name.  */
7250
 
7251
void
7252
build_self_reference (void)
7253
{
7254
  tree name = constructor_name (current_class_type);
7255
  tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
7256
  tree saved_cas;
7257
 
7258
  DECL_NONLOCAL (value) = 1;
7259
  DECL_CONTEXT (value) = current_class_type;
7260
  DECL_ARTIFICIAL (value) = 1;
7261
  SET_DECL_SELF_REFERENCE_P (value);
7262
  set_underlying_type (value);
7263
 
7264
  if (processing_template_decl)
7265
    value = push_template_decl (value);
7266
 
7267
  saved_cas = current_access_specifier;
7268
  current_access_specifier = access_public_node;
7269
  finish_member_declaration (value);
7270
  current_access_specifier = saved_cas;
7271
}
7272
 
7273
/* Returns 1 if TYPE contains only padding bytes.  */
7274
 
7275
int
7276
is_empty_class (tree type)
7277
{
7278
  if (type == error_mark_node)
7279
    return 0;
7280
 
7281
  if (! CLASS_TYPE_P (type))
7282
    return 0;
7283
 
7284
  /* In G++ 3.2, whether or not a class was empty was determined by
7285
     looking at its size.  */
7286
  if (abi_version_at_least (2))
7287
    return CLASSTYPE_EMPTY_P (type);
7288
  else
7289
    return integer_zerop (CLASSTYPE_SIZE (type));
7290
}
7291
 
7292
/* Returns true if TYPE contains an empty class.  */
7293
 
7294
static bool
7295
contains_empty_class_p (tree type)
7296
{
7297
  if (is_empty_class (type))
7298
    return true;
7299
  if (CLASS_TYPE_P (type))
7300
    {
7301
      tree field;
7302
      tree binfo;
7303
      tree base_binfo;
7304
      int i;
7305
 
7306
      for (binfo = TYPE_BINFO (type), i = 0;
7307
           BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
7308
        if (contains_empty_class_p (BINFO_TYPE (base_binfo)))
7309
          return true;
7310
      for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
7311
        if (TREE_CODE (field) == FIELD_DECL
7312
            && !DECL_ARTIFICIAL (field)
7313
            && is_empty_class (TREE_TYPE (field)))
7314
          return true;
7315
    }
7316
  else if (TREE_CODE (type) == ARRAY_TYPE)
7317
    return contains_empty_class_p (TREE_TYPE (type));
7318
  return false;
7319
}
7320
 
7321
/* Returns true if TYPE contains no actual data, just various
7322
   possible combinations of empty classes and possibly a vptr.  */
7323
 
7324
bool
7325
is_really_empty_class (tree type)
7326
{
7327
  if (CLASS_TYPE_P (type))
7328
    {
7329
      tree field;
7330
      tree binfo;
7331
      tree base_binfo;
7332
      int i;
7333
 
7334
      /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
7335
         out, but we'd like to be able to check this before then.  */
7336
      if (COMPLETE_TYPE_P (type) && is_empty_class (type))
7337
        return true;
7338
 
7339
      for (binfo = TYPE_BINFO (type), i = 0;
7340
           BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
7341
        if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
7342
          return false;
7343
      for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7344
        if (TREE_CODE (field) == FIELD_DECL
7345
            && !DECL_ARTIFICIAL (field)
7346
            && !is_really_empty_class (TREE_TYPE (field)))
7347
          return false;
7348
      return true;
7349
    }
7350
  else if (TREE_CODE (type) == ARRAY_TYPE)
7351
    return is_really_empty_class (TREE_TYPE (type));
7352
  return false;
7353
}
7354
 
7355
/* Note that NAME was looked up while the current class was being
7356
   defined and that the result of that lookup was DECL.  */
7357
 
7358
void
7359
maybe_note_name_used_in_class (tree name, tree decl)
7360
{
7361
  splay_tree names_used;
7362
 
7363
  /* If we're not defining a class, there's nothing to do.  */
7364
  if (!(innermost_scope_kind() == sk_class
7365
        && TYPE_BEING_DEFINED (current_class_type)
7366
        && !LAMBDA_TYPE_P (current_class_type)))
7367
    return;
7368
 
7369
  /* If there's already a binding for this NAME, then we don't have
7370
     anything to worry about.  */
7371
  if (lookup_member (current_class_type, name,
7372
                     /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
7373
    return;
7374
 
7375
  if (!current_class_stack[current_class_depth - 1].names_used)
7376
    current_class_stack[current_class_depth - 1].names_used
7377
      = splay_tree_new (splay_tree_compare_pointers, 0, 0);
7378
  names_used = current_class_stack[current_class_depth - 1].names_used;
7379
 
7380
  splay_tree_insert (names_used,
7381
                     (splay_tree_key) name,
7382
                     (splay_tree_value) decl);
7383
}
7384
 
7385
/* Note that NAME was declared (as DECL) in the current class.  Check
7386
   to see that the declaration is valid.  */
7387
 
7388
void
7389
note_name_declared_in_class (tree name, tree decl)
7390
{
7391
  splay_tree names_used;
7392
  splay_tree_node n;
7393
 
7394
  /* Look to see if we ever used this name.  */
7395
  names_used
7396
    = current_class_stack[current_class_depth - 1].names_used;
7397
  if (!names_used)
7398
    return;
7399
  /* The C language allows members to be declared with a type of the same
7400
     name, and the C++ standard says this diagnostic is not required.  So
7401
     allow it in extern "C" blocks unless predantic is specified.
7402
     Allow it in all cases if -ms-extensions is specified.  */
7403
  if ((!pedantic && current_lang_name == lang_name_c)
7404
      || flag_ms_extensions)
7405
    return;
7406
  n = splay_tree_lookup (names_used, (splay_tree_key) name);
7407
  if (n)
7408
    {
7409
      /* [basic.scope.class]
7410
 
7411
         A name N used in a class S shall refer to the same declaration
7412
         in its context and when re-evaluated in the completed scope of
7413
         S.  */
7414
      permerror (input_location, "declaration of %q#D", decl);
7415
      permerror (input_location, "changes meaning of %qD from %q+#D",
7416
               DECL_NAME (OVL_CURRENT (decl)), (tree) n->value);
7417
    }
7418
}
7419
 
7420
/* Returns the VAR_DECL for the complete vtable associated with BINFO.
7421
   Secondary vtables are merged with primary vtables; this function
7422
   will return the VAR_DECL for the primary vtable.  */
7423
 
7424
tree
7425
get_vtbl_decl_for_binfo (tree binfo)
7426
{
7427
  tree decl;
7428
 
7429
  decl = BINFO_VTABLE (binfo);
7430
  if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
7431
    {
7432
      gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
7433
      decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
7434
    }
7435
  if (decl)
7436
    gcc_assert (TREE_CODE (decl) == VAR_DECL);
7437
  return decl;
7438
}
7439
 
7440
 
7441
/* Returns the binfo for the primary base of BINFO.  If the resulting
7442
   BINFO is a virtual base, and it is inherited elsewhere in the
7443
   hierarchy, then the returned binfo might not be the primary base of
7444
   BINFO in the complete object.  Check BINFO_PRIMARY_P or
7445
   BINFO_LOST_PRIMARY_P to be sure.  */
7446
 
7447
static tree
7448
get_primary_binfo (tree binfo)
7449
{
7450
  tree primary_base;
7451
 
7452
  primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
7453
  if (!primary_base)
7454
    return NULL_TREE;
7455
 
7456
  return copied_binfo (primary_base, binfo);
7457
}
7458
 
7459
/* If INDENTED_P is zero, indent to INDENT. Return nonzero.  */
7460
 
7461
static int
7462
maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
7463
{
7464
  if (!indented_p)
7465
    fprintf (stream, "%*s", indent, "");
7466
  return 1;
7467
}
7468
 
7469
/* Dump the offsets of all the bases rooted at BINFO to STREAM.
7470
   INDENT should be zero when called from the top level; it is
7471
   incremented recursively.  IGO indicates the next expected BINFO in
7472
   inheritance graph ordering.  */
7473
 
7474
static tree
7475
dump_class_hierarchy_r (FILE *stream,
7476
                        int flags,
7477
                        tree binfo,
7478
                        tree igo,
7479
                        int indent)
7480
{
7481
  int indented = 0;
7482
  tree base_binfo;
7483
  int i;
7484
 
7485
  indented = maybe_indent_hierarchy (stream, indent, 0);
7486
  fprintf (stream, "%s (0x%lx) ",
7487
           type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
7488
           (unsigned long) binfo);
7489
  if (binfo != igo)
7490
    {
7491
      fprintf (stream, "alternative-path\n");
7492
      return igo;
7493
    }
7494
  igo = TREE_CHAIN (binfo);
7495
 
7496
  fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
7497
           tree_low_cst (BINFO_OFFSET (binfo), 0));
7498
  if (is_empty_class (BINFO_TYPE (binfo)))
7499
    fprintf (stream, " empty");
7500
  else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
7501
    fprintf (stream, " nearly-empty");
7502
  if (BINFO_VIRTUAL_P (binfo))
7503
    fprintf (stream, " virtual");
7504
  fprintf (stream, "\n");
7505
 
7506
  indented = 0;
7507
  if (BINFO_PRIMARY_P (binfo))
7508
    {
7509
      indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7510
      fprintf (stream, " primary-for %s (0x%lx)",
7511
               type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
7512
                               TFF_PLAIN_IDENTIFIER),
7513
               (unsigned long)BINFO_INHERITANCE_CHAIN (binfo));
7514
    }
7515
  if (BINFO_LOST_PRIMARY_P (binfo))
7516
    {
7517
      indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7518
      fprintf (stream, " lost-primary");
7519
    }
7520
  if (indented)
7521
    fprintf (stream, "\n");
7522
 
7523
  if (!(flags & TDF_SLIM))
7524
    {
7525
      int indented = 0;
7526
 
7527
      if (BINFO_SUBVTT_INDEX (binfo))
7528
        {
7529
          indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7530
          fprintf (stream, " subvttidx=%s",
7531
                   expr_as_string (BINFO_SUBVTT_INDEX (binfo),
7532
                                   TFF_PLAIN_IDENTIFIER));
7533
        }
7534
      if (BINFO_VPTR_INDEX (binfo))
7535
        {
7536
          indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7537
          fprintf (stream, " vptridx=%s",
7538
                   expr_as_string (BINFO_VPTR_INDEX (binfo),
7539
                                   TFF_PLAIN_IDENTIFIER));
7540
        }
7541
      if (BINFO_VPTR_FIELD (binfo))
7542
        {
7543
          indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7544
          fprintf (stream, " vbaseoffset=%s",
7545
                   expr_as_string (BINFO_VPTR_FIELD (binfo),
7546
                                   TFF_PLAIN_IDENTIFIER));
7547
        }
7548
      if (BINFO_VTABLE (binfo))
7549
        {
7550
          indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7551
          fprintf (stream, " vptr=%s",
7552
                   expr_as_string (BINFO_VTABLE (binfo),
7553
                                   TFF_PLAIN_IDENTIFIER));
7554
        }
7555
 
7556
      if (indented)
7557
        fprintf (stream, "\n");
7558
    }
7559
 
7560
  for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
7561
    igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
7562
 
7563
  return igo;
7564
}
7565
 
7566
/* Dump the BINFO hierarchy for T.  */
7567
 
7568
static void
7569
dump_class_hierarchy_1 (FILE *stream, int flags, tree t)
7570
{
7571
  fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
7572
  fprintf (stream, "   size=%lu align=%lu\n",
7573
           (unsigned long)(tree_low_cst (TYPE_SIZE (t), 0) / BITS_PER_UNIT),
7574
           (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
7575
  fprintf (stream, "   base size=%lu base align=%lu\n",
7576
           (unsigned long)(tree_low_cst (TYPE_SIZE (CLASSTYPE_AS_BASE (t)), 0)
7577
                           / BITS_PER_UNIT),
7578
           (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
7579
                           / BITS_PER_UNIT));
7580
  dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
7581
  fprintf (stream, "\n");
7582
}
7583
 
7584
/* Debug interface to hierarchy dumping.  */
7585
 
7586
void
7587
debug_class (tree t)
7588
{
7589
  dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
7590
}
7591
 
7592
static void
7593
dump_class_hierarchy (tree t)
7594
{
7595
  int flags;
7596
  FILE *stream = dump_begin (TDI_class, &flags);
7597
 
7598
  if (stream)
7599
    {
7600
      dump_class_hierarchy_1 (stream, flags, t);
7601
      dump_end (TDI_class, stream);
7602
    }
7603
}
7604
 
7605
static void
7606
dump_array (FILE * stream, tree decl)
7607
{
7608
  tree value;
7609
  unsigned HOST_WIDE_INT ix;
7610
  HOST_WIDE_INT elt;
7611
  tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
7612
 
7613
  elt = (tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))), 0)
7614
         / BITS_PER_UNIT);
7615
  fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
7616
  fprintf (stream, " %s entries",
7617
           expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
7618
                           TFF_PLAIN_IDENTIFIER));
7619
  fprintf (stream, "\n");
7620
 
7621
  FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
7622
                              ix, value)
7623
    fprintf (stream, "%-4ld  %s\n", (long)(ix * elt),
7624
             expr_as_string (value, TFF_PLAIN_IDENTIFIER));
7625
}
7626
 
7627
static void
7628
dump_vtable (tree t, tree binfo, tree vtable)
7629
{
7630
  int flags;
7631
  FILE *stream = dump_begin (TDI_class, &flags);
7632
 
7633
  if (!stream)
7634
    return;
7635
 
7636
  if (!(flags & TDF_SLIM))
7637
    {
7638
      int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
7639
 
7640
      fprintf (stream, "%s for %s",
7641
               ctor_vtbl_p ? "Construction vtable" : "Vtable",
7642
               type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
7643
      if (ctor_vtbl_p)
7644
        {
7645
          if (!BINFO_VIRTUAL_P (binfo))
7646
            fprintf (stream, " (0x%lx instance)", (unsigned long)binfo);
7647
          fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
7648
        }
7649
      fprintf (stream, "\n");
7650
      dump_array (stream, vtable);
7651
      fprintf (stream, "\n");
7652
    }
7653
 
7654
  dump_end (TDI_class, stream);
7655
}
7656
 
7657
static void
7658
dump_vtt (tree t, tree vtt)
7659
{
7660
  int flags;
7661
  FILE *stream = dump_begin (TDI_class, &flags);
7662
 
7663
  if (!stream)
7664
    return;
7665
 
7666
  if (!(flags & TDF_SLIM))
7667
    {
7668
      fprintf (stream, "VTT for %s\n",
7669
               type_as_string (t, TFF_PLAIN_IDENTIFIER));
7670
      dump_array (stream, vtt);
7671
      fprintf (stream, "\n");
7672
    }
7673
 
7674
  dump_end (TDI_class, stream);
7675
}
7676
 
7677
/* Dump a function or thunk and its thunkees.  */
7678
 
7679
static void
7680
dump_thunk (FILE *stream, int indent, tree thunk)
7681
{
7682
  static const char spaces[] = "        ";
7683
  tree name = DECL_NAME (thunk);
7684
  tree thunks;
7685
 
7686
  fprintf (stream, "%.*s%p %s %s", indent, spaces,
7687
           (void *)thunk,
7688
           !DECL_THUNK_P (thunk) ? "function"
7689
           : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
7690
           name ? IDENTIFIER_POINTER (name) : "<unset>");
7691
  if (DECL_THUNK_P (thunk))
7692
    {
7693
      HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
7694
      tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
7695
 
7696
      fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
7697
      if (!virtual_adjust)
7698
        /*NOP*/;
7699
      else if (DECL_THIS_THUNK_P (thunk))
7700
        fprintf (stream, " vcall="  HOST_WIDE_INT_PRINT_DEC,
7701
                 tree_low_cst (virtual_adjust, 0));
7702
      else
7703
        fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
7704
                 tree_low_cst (BINFO_VPTR_FIELD (virtual_adjust), 0),
7705
                 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
7706
      if (THUNK_ALIAS (thunk))
7707
        fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
7708
    }
7709
  fprintf (stream, "\n");
7710
  for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
7711
    dump_thunk (stream, indent + 2, thunks);
7712
}
7713
 
7714
/* Dump the thunks for FN.  */
7715
 
7716
void
7717
debug_thunks (tree fn)
7718
{
7719
  dump_thunk (stderr, 0, fn);
7720
}
7721
 
7722
/* Virtual function table initialization.  */
7723
 
7724
/* Create all the necessary vtables for T and its base classes.  */
7725
 
7726
static void
7727
finish_vtbls (tree t)
7728
{
7729
  tree vbase;
7730
  VEC(constructor_elt,gc) *v = NULL;
7731
  tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
7732
 
7733
  /* We lay out the primary and secondary vtables in one contiguous
7734
     vtable.  The primary vtable is first, followed by the non-virtual
7735
     secondary vtables in inheritance graph order.  */
7736
  accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
7737
                         vtable, t, &v);
7738
 
7739
  /* Then come the virtual bases, also in inheritance graph order.  */
7740
  for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
7741
    {
7742
      if (!BINFO_VIRTUAL_P (vbase))
7743
        continue;
7744
      accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
7745
    }
7746
 
7747
  if (BINFO_VTABLE (TYPE_BINFO (t)))
7748
    initialize_vtable (TYPE_BINFO (t), v);
7749
}
7750
 
7751
/* Initialize the vtable for BINFO with the INITS.  */
7752
 
7753
static void
7754
initialize_vtable (tree binfo, VEC(constructor_elt,gc) *inits)
7755
{
7756
  tree decl;
7757
 
7758
  layout_vtable_decl (binfo, VEC_length (constructor_elt, inits));
7759
  decl = get_vtbl_decl_for_binfo (binfo);
7760
  initialize_artificial_var (decl, inits);
7761
  dump_vtable (BINFO_TYPE (binfo), binfo, decl);
7762
}
7763
 
7764
/* Build the VTT (virtual table table) for T.
7765
   A class requires a VTT if it has virtual bases.
7766
 
7767
   This holds
7768
   1 - primary virtual pointer for complete object T
7769
   2 - secondary VTTs for each direct non-virtual base of T which requires a
7770
       VTT
7771
   3 - secondary virtual pointers for each direct or indirect base of T which
7772
       has virtual bases or is reachable via a virtual path from T.
7773
   4 - secondary VTTs for each direct or indirect virtual base of T.
7774
 
7775
   Secondary VTTs look like complete object VTTs without part 4.  */
7776
 
7777
static void
7778
build_vtt (tree t)
7779
{
7780
  tree type;
7781
  tree vtt;
7782
  tree index;
7783
  VEC(constructor_elt,gc) *inits;
7784
 
7785
  /* Build up the initializers for the VTT.  */
7786
  inits = NULL;
7787
  index = size_zero_node;
7788
  build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
7789
 
7790
  /* If we didn't need a VTT, we're done.  */
7791
  if (!inits)
7792
    return;
7793
 
7794
  /* Figure out the type of the VTT.  */
7795
  type = build_array_of_n_type (const_ptr_type_node,
7796
                                VEC_length (constructor_elt, inits));
7797
 
7798
  /* Now, build the VTT object itself.  */
7799
  vtt = build_vtable (t, mangle_vtt_for_type (t), type);
7800
  initialize_artificial_var (vtt, inits);
7801
  /* Add the VTT to the vtables list.  */
7802
  DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
7803
  DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
7804
 
7805
  dump_vtt (t, vtt);
7806
}
7807
 
7808
/* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
7809
   PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
7810
   and CHAIN the vtable pointer for this binfo after construction is
7811
   complete.  VALUE can also be another BINFO, in which case we recurse.  */
7812
 
7813
static tree
7814
binfo_ctor_vtable (tree binfo)
7815
{
7816
  tree vt;
7817
 
7818
  while (1)
7819
    {
7820
      vt = BINFO_VTABLE (binfo);
7821
      if (TREE_CODE (vt) == TREE_LIST)
7822
        vt = TREE_VALUE (vt);
7823
      if (TREE_CODE (vt) == TREE_BINFO)
7824
        binfo = vt;
7825
      else
7826
        break;
7827
    }
7828
 
7829
  return vt;
7830
}
7831
 
7832
/* Data for secondary VTT initialization.  */
7833
typedef struct secondary_vptr_vtt_init_data_s
7834
{
7835
  /* Is this the primary VTT? */
7836
  bool top_level_p;
7837
 
7838
  /* Current index into the VTT.  */
7839
  tree index;
7840
 
7841
  /* Vector of initializers built up.  */
7842
  VEC(constructor_elt,gc) *inits;
7843
 
7844
  /* The type being constructed by this secondary VTT.  */
7845
  tree type_being_constructed;
7846
} secondary_vptr_vtt_init_data;
7847
 
7848
/* Recursively build the VTT-initializer for BINFO (which is in the
7849
   hierarchy dominated by T).  INITS points to the end of the initializer
7850
   list to date.  INDEX is the VTT index where the next element will be
7851
   replaced.  Iff BINFO is the binfo for T, this is the top level VTT (i.e.
7852
   not a subvtt for some base of T).  When that is so, we emit the sub-VTTs
7853
   for virtual bases of T. When it is not so, we build the constructor
7854
   vtables for the BINFO-in-T variant.  */
7855
 
7856
static void
7857
build_vtt_inits (tree binfo, tree t, VEC(constructor_elt,gc) **inits, tree *index)
7858
{
7859
  int i;
7860
  tree b;
7861
  tree init;
7862
  secondary_vptr_vtt_init_data data;
7863
  int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
7864
 
7865
  /* We only need VTTs for subobjects with virtual bases.  */
7866
  if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
7867
    return;
7868
 
7869
  /* We need to use a construction vtable if this is not the primary
7870
     VTT.  */
7871
  if (!top_level_p)
7872
    {
7873
      build_ctor_vtbl_group (binfo, t);
7874
 
7875
      /* Record the offset in the VTT where this sub-VTT can be found.  */
7876
      BINFO_SUBVTT_INDEX (binfo) = *index;
7877
    }
7878
 
7879
  /* Add the address of the primary vtable for the complete object.  */
7880
  init = binfo_ctor_vtable (binfo);
7881
  CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
7882
  if (top_level_p)
7883
    {
7884
      gcc_assert (!BINFO_VPTR_INDEX (binfo));
7885
      BINFO_VPTR_INDEX (binfo) = *index;
7886
    }
7887
  *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
7888
 
7889
  /* Recursively add the secondary VTTs for non-virtual bases.  */
7890
  for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
7891
    if (!BINFO_VIRTUAL_P (b))
7892
      build_vtt_inits (b, t, inits, index);
7893
 
7894
  /* Add secondary virtual pointers for all subobjects of BINFO with
7895
     either virtual bases or reachable along a virtual path, except
7896
     subobjects that are non-virtual primary bases.  */
7897
  data.top_level_p = top_level_p;
7898
  data.index = *index;
7899
  data.inits = *inits;
7900
  data.type_being_constructed = BINFO_TYPE (binfo);
7901
 
7902
  dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
7903
 
7904
  *index = data.index;
7905
 
7906
  /* data.inits might have grown as we added secondary virtual pointers.
7907
     Make sure our caller knows about the new vector.  */
7908
  *inits = data.inits;
7909
 
7910
  if (top_level_p)
7911
    /* Add the secondary VTTs for virtual bases in inheritance graph
7912
       order.  */
7913
    for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
7914
      {
7915
        if (!BINFO_VIRTUAL_P (b))
7916
          continue;
7917
 
7918
        build_vtt_inits (b, t, inits, index);
7919
      }
7920
  else
7921
    /* Remove the ctor vtables we created.  */
7922
    dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
7923
}
7924
 
7925
/* Called from build_vtt_inits via dfs_walk.  BINFO is the binfo for the base
7926
   in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure.  */
7927
 
7928
static tree
7929
dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
7930
{
7931
  secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
7932
 
7933
  /* We don't care about bases that don't have vtables.  */
7934
  if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
7935
    return dfs_skip_bases;
7936
 
7937
  /* We're only interested in proper subobjects of the type being
7938
     constructed.  */
7939
  if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
7940
    return NULL_TREE;
7941
 
7942
  /* We're only interested in bases with virtual bases or reachable
7943
     via a virtual path from the type being constructed.  */
7944
  if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
7945
        || binfo_via_virtual (binfo, data->type_being_constructed)))
7946
    return dfs_skip_bases;
7947
 
7948
  /* We're not interested in non-virtual primary bases.  */
7949
  if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
7950
    return NULL_TREE;
7951
 
7952
  /* Record the index where this secondary vptr can be found.  */
7953
  if (data->top_level_p)
7954
    {
7955
      gcc_assert (!BINFO_VPTR_INDEX (binfo));
7956
      BINFO_VPTR_INDEX (binfo) = data->index;
7957
 
7958
      if (BINFO_VIRTUAL_P (binfo))
7959
        {
7960
          /* It's a primary virtual base, and this is not a
7961
             construction vtable.  Find the base this is primary of in
7962
             the inheritance graph, and use that base's vtable
7963
             now.  */
7964
          while (BINFO_PRIMARY_P (binfo))
7965
            binfo = BINFO_INHERITANCE_CHAIN (binfo);
7966
        }
7967
    }
7968
 
7969
  /* Add the initializer for the secondary vptr itself.  */
7970
  CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
7971
 
7972
  /* Advance the vtt index.  */
7973
  data->index = size_binop (PLUS_EXPR, data->index,
7974
                            TYPE_SIZE_UNIT (ptr_type_node));
7975
 
7976
  return NULL_TREE;
7977
}
7978
 
7979
/* Called from build_vtt_inits via dfs_walk. After building
7980
   constructor vtables and generating the sub-vtt from them, we need
7981
   to restore the BINFO_VTABLES that were scribbled on.  DATA is the
7982
   binfo of the base whose sub vtt was generated.  */
7983
 
7984
static tree
7985
dfs_fixup_binfo_vtbls (tree binfo, void* data)
7986
{
7987
  tree vtable = BINFO_VTABLE (binfo);
7988
 
7989
  if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
7990
    /* If this class has no vtable, none of its bases do.  */
7991
    return dfs_skip_bases;
7992
 
7993
  if (!vtable)
7994
    /* This might be a primary base, so have no vtable in this
7995
       hierarchy.  */
7996
    return NULL_TREE;
7997
 
7998
  /* If we scribbled the construction vtable vptr into BINFO, clear it
7999
     out now.  */
8000
  if (TREE_CODE (vtable) == TREE_LIST
8001
      && (TREE_PURPOSE (vtable) == (tree) data))
8002
    BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
8003
 
8004
  return NULL_TREE;
8005
}
8006
 
8007
/* Build the construction vtable group for BINFO which is in the
8008
   hierarchy dominated by T.  */
8009
 
8010
static void
8011
build_ctor_vtbl_group (tree binfo, tree t)
8012
{
8013
  tree type;
8014
  tree vtbl;
8015
  tree id;
8016
  tree vbase;
8017
  VEC(constructor_elt,gc) *v;
8018
 
8019
  /* See if we've already created this construction vtable group.  */
8020
  id = mangle_ctor_vtbl_for_type (t, binfo);
8021
  if (IDENTIFIER_GLOBAL_VALUE (id))
8022
    return;
8023
 
8024
  gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
8025
  /* Build a version of VTBL (with the wrong type) for use in
8026
     constructing the addresses of secondary vtables in the
8027
     construction vtable group.  */
8028
  vtbl = build_vtable (t, id, ptr_type_node);
8029
  DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
8030
 
8031
  v = NULL;
8032
  accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
8033
                         binfo, vtbl, t, &v);
8034
 
8035
  /* Add the vtables for each of our virtual bases using the vbase in T
8036
     binfo.  */
8037
  for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
8038
       vbase;
8039
       vbase = TREE_CHAIN (vbase))
8040
    {
8041
      tree b;
8042
 
8043
      if (!BINFO_VIRTUAL_P (vbase))
8044
        continue;
8045
      b = copied_binfo (vbase, binfo);
8046
 
8047
      accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
8048
    }
8049
 
8050
  /* Figure out the type of the construction vtable.  */
8051
  type = build_array_of_n_type (vtable_entry_type,
8052
                                VEC_length (constructor_elt, v));
8053
  layout_type (type);
8054
  TREE_TYPE (vtbl) = type;
8055
  DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
8056
  layout_decl (vtbl, 0);
8057
 
8058
  /* Initialize the construction vtable.  */
8059
  CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
8060
  initialize_artificial_var (vtbl, v);
8061
  dump_vtable (t, binfo, vtbl);
8062
}
8063
 
8064
/* Add the vtbl initializers for BINFO (and its bases other than
8065
   non-virtual primaries) to the list of INITS.  BINFO is in the
8066
   hierarchy dominated by T.  RTTI_BINFO is the binfo within T of
8067
   the constructor the vtbl inits should be accumulated for. (If this
8068
   is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
8069
   ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
8070
   BINFO is the active base equivalent of ORIG_BINFO in the inheritance
8071
   graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
8072
   but are not necessarily the same in terms of layout.  */
8073
 
8074
static void
8075
accumulate_vtbl_inits (tree binfo,
8076
                       tree orig_binfo,
8077
                       tree rtti_binfo,
8078
                       tree vtbl,
8079
                       tree t,
8080
                       VEC(constructor_elt,gc) **inits)
8081
{
8082
  int i;
8083
  tree base_binfo;
8084
  int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8085
 
8086
  gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
8087
 
8088
  /* If it doesn't have a vptr, we don't do anything.  */
8089
  if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8090
    return;
8091
 
8092
  /* If we're building a construction vtable, we're not interested in
8093
     subobjects that don't require construction vtables.  */
8094
  if (ctor_vtbl_p
8095
      && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8096
      && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
8097
    return;
8098
 
8099
  /* Build the initializers for the BINFO-in-T vtable.  */
8100
  dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
8101
 
8102
  /* Walk the BINFO and its bases.  We walk in preorder so that as we
8103
     initialize each vtable we can figure out at what offset the
8104
     secondary vtable lies from the primary vtable.  We can't use
8105
     dfs_walk here because we need to iterate through bases of BINFO
8106
     and RTTI_BINFO simultaneously.  */
8107
  for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8108
    {
8109
      /* Skip virtual bases.  */
8110
      if (BINFO_VIRTUAL_P (base_binfo))
8111
        continue;
8112
      accumulate_vtbl_inits (base_binfo,
8113
                             BINFO_BASE_BINFO (orig_binfo, i),
8114
                             rtti_binfo, vtbl, t,
8115
                             inits);
8116
    }
8117
}
8118
 
8119
/* Called from accumulate_vtbl_inits.  Adds the initializers for the
8120
   BINFO vtable to L.  */
8121
 
8122
static void
8123
dfs_accumulate_vtbl_inits (tree binfo,
8124
                           tree orig_binfo,
8125
                           tree rtti_binfo,
8126
                           tree orig_vtbl,
8127
                           tree t,
8128
                           VEC(constructor_elt,gc) **l)
8129
{
8130
  tree vtbl = NULL_TREE;
8131
  int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8132
  int n_inits;
8133
 
8134
  if (ctor_vtbl_p
8135
      && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
8136
    {
8137
      /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
8138
         primary virtual base.  If it is not the same primary in
8139
         the hierarchy of T, we'll need to generate a ctor vtable
8140
         for it, to place at its location in T.  If it is the same
8141
         primary, we still need a VTT entry for the vtable, but it
8142
         should point to the ctor vtable for the base it is a
8143
         primary for within the sub-hierarchy of RTTI_BINFO.
8144
 
8145
         There are three possible cases:
8146
 
8147
         1) We are in the same place.
8148
         2) We are a primary base within a lost primary virtual base of
8149
         RTTI_BINFO.
8150
         3) We are primary to something not a base of RTTI_BINFO.  */
8151
 
8152
      tree b;
8153
      tree last = NULL_TREE;
8154
 
8155
      /* First, look through the bases we are primary to for RTTI_BINFO
8156
         or a virtual base.  */
8157
      b = binfo;
8158
      while (BINFO_PRIMARY_P (b))
8159
        {
8160
          b = BINFO_INHERITANCE_CHAIN (b);
8161
          last = b;
8162
          if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
8163
            goto found;
8164
        }
8165
      /* If we run out of primary links, keep looking down our
8166
         inheritance chain; we might be an indirect primary.  */
8167
      for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
8168
        if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
8169
          break;
8170
    found:
8171
 
8172
      /* If we found RTTI_BINFO, this is case 1.  If we found a virtual
8173
         base B and it is a base of RTTI_BINFO, this is case 2.  In
8174
         either case, we share our vtable with LAST, i.e. the
8175
         derived-most base within B of which we are a primary.  */
8176
      if (b == rtti_binfo
8177
          || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
8178
        /* Just set our BINFO_VTABLE to point to LAST, as we may not have
8179
           set LAST's BINFO_VTABLE yet.  We'll extract the actual vptr in
8180
           binfo_ctor_vtable after everything's been set up.  */
8181
        vtbl = last;
8182
 
8183
      /* Otherwise, this is case 3 and we get our own.  */
8184
    }
8185
  else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
8186
    return;
8187
 
8188
  n_inits = VEC_length (constructor_elt, *l);
8189
 
8190
  if (!vtbl)
8191
    {
8192
      tree index;
8193
      int non_fn_entries;
8194
 
8195
      /* Add the initializer for this vtable.  */
8196
      build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
8197
                              &non_fn_entries, l);
8198
 
8199
      /* Figure out the position to which the VPTR should point.  */
8200
      vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
8201
      index = size_binop (MULT_EXPR,
8202
                          TYPE_SIZE_UNIT (vtable_entry_type),
8203
                          size_int (non_fn_entries + n_inits));
8204
      vtbl = fold_build_pointer_plus (vtbl, index);
8205
    }
8206
 
8207
  if (ctor_vtbl_p)
8208
    /* For a construction vtable, we can't overwrite BINFO_VTABLE.
8209
       So, we make a TREE_LIST.  Later, dfs_fixup_binfo_vtbls will
8210
       straighten this out.  */
8211
    BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
8212
  else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
8213
    /* Throw away any unneeded intializers.  */
8214
    VEC_truncate (constructor_elt, *l, n_inits);
8215
  else
8216
     /* For an ordinary vtable, set BINFO_VTABLE.  */
8217
    BINFO_VTABLE (binfo) = vtbl;
8218
}
8219
 
8220
static GTY(()) tree abort_fndecl_addr;
8221
 
8222
/* Construct the initializer for BINFO's virtual function table.  BINFO
8223
   is part of the hierarchy dominated by T.  If we're building a
8224
   construction vtable, the ORIG_BINFO is the binfo we should use to
8225
   find the actual function pointers to put in the vtable - but they
8226
   can be overridden on the path to most-derived in the graph that
8227
   ORIG_BINFO belongs.  Otherwise,
8228
   ORIG_BINFO should be the same as BINFO.  The RTTI_BINFO is the
8229
   BINFO that should be indicated by the RTTI information in the
8230
   vtable; it will be a base class of T, rather than T itself, if we
8231
   are building a construction vtable.
8232
 
8233
   The value returned is a TREE_LIST suitable for wrapping in a
8234
   CONSTRUCTOR to use as the DECL_INITIAL for a vtable.  If
8235
   NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
8236
   number of non-function entries in the vtable.
8237
 
8238
   It might seem that this function should never be called with a
8239
   BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
8240
   base is always subsumed by a derived class vtable.  However, when
8241
   we are building construction vtables, we do build vtables for
8242
   primary bases; we need these while the primary base is being
8243
   constructed.  */
8244
 
8245
static void
8246
build_vtbl_initializer (tree binfo,
8247
                        tree orig_binfo,
8248
                        tree t,
8249
                        tree rtti_binfo,
8250
                        int* non_fn_entries_p,
8251
                        VEC(constructor_elt,gc) **inits)
8252
{
8253
  tree v;
8254
  vtbl_init_data vid;
8255
  unsigned ix, jx;
8256
  tree vbinfo;
8257
  VEC(tree,gc) *vbases;
8258
  constructor_elt *e;
8259
 
8260
  /* Initialize VID.  */
8261
  memset (&vid, 0, sizeof (vid));
8262
  vid.binfo = binfo;
8263
  vid.derived = t;
8264
  vid.rtti_binfo = rtti_binfo;
8265
  vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8266
  vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8267
  vid.generate_vcall_entries = true;
8268
  /* The first vbase or vcall offset is at index -3 in the vtable.  */
8269
  vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
8270
 
8271
  /* Add entries to the vtable for RTTI.  */
8272
  build_rtti_vtbl_entries (binfo, &vid);
8273
 
8274
  /* Create an array for keeping track of the functions we've
8275
     processed.  When we see multiple functions with the same
8276
     signature, we share the vcall offsets.  */
8277
  vid.fns = VEC_alloc (tree, gc, 32);
8278
  /* Add the vcall and vbase offset entries.  */
8279
  build_vcall_and_vbase_vtbl_entries (binfo, &vid);
8280
 
8281
  /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
8282
     build_vbase_offset_vtbl_entries.  */
8283
  for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
8284
       VEC_iterate (tree, vbases, ix, vbinfo); ix++)
8285
    BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
8286
 
8287
  /* If the target requires padding between data entries, add that now.  */
8288
  if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
8289
    {
8290
      int n_entries = VEC_length (constructor_elt, vid.inits);
8291
 
8292
      VEC_safe_grow (constructor_elt, gc, vid.inits,
8293
                     TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
8294
 
8295
      /* Move data entries into their new positions and add padding
8296
         after the new positions.  Iterate backwards so we don't
8297
         overwrite entries that we would need to process later.  */
8298
      for (ix = n_entries - 1;
8299
           VEC_iterate (constructor_elt, vid.inits, ix, e);
8300
           ix--)
8301
        {
8302
          int j;
8303
          int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
8304
                              + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
8305
 
8306
          VEC_replace (constructor_elt, vid.inits, new_position, e);
8307
 
8308
          for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
8309
            {
8310
              constructor_elt *f = VEC_index (constructor_elt, vid.inits,
8311
                                              new_position - j);
8312
              f->index = NULL_TREE;
8313
              f->value = build1 (NOP_EXPR, vtable_entry_type,
8314
                                 null_pointer_node);
8315
            }
8316
        }
8317
    }
8318
 
8319
  if (non_fn_entries_p)
8320
    *non_fn_entries_p = VEC_length (constructor_elt, vid.inits);
8321
 
8322
  /* The initializers for virtual functions were built up in reverse
8323
     order.  Straighten them out and add them to the running list in one
8324
     step.  */
8325
  jx = VEC_length (constructor_elt, *inits);
8326
  VEC_safe_grow (constructor_elt, gc, *inits,
8327
                 (jx + VEC_length (constructor_elt, vid.inits)));
8328
 
8329
  for (ix = VEC_length (constructor_elt, vid.inits) - 1;
8330
       VEC_iterate (constructor_elt, vid.inits, ix, e);
8331
       ix--, jx++)
8332
    VEC_replace (constructor_elt, *inits, jx, e);
8333
 
8334
  /* Go through all the ordinary virtual functions, building up
8335
     initializers.  */
8336
  for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
8337
    {
8338
      tree delta;
8339
      tree vcall_index;
8340
      tree fn, fn_original;
8341
      tree init = NULL_TREE;
8342
 
8343
      fn = BV_FN (v);
8344
      fn_original = fn;
8345
      if (DECL_THUNK_P (fn))
8346
        {
8347
          if (!DECL_NAME (fn))
8348
            finish_thunk (fn);
8349
          if (THUNK_ALIAS (fn))
8350
            {
8351
              fn = THUNK_ALIAS (fn);
8352
              BV_FN (v) = fn;
8353
            }
8354
          fn_original = THUNK_TARGET (fn);
8355
        }
8356
 
8357
      /* If the only definition of this function signature along our
8358
         primary base chain is from a lost primary, this vtable slot will
8359
         never be used, so just zero it out.  This is important to avoid
8360
         requiring extra thunks which cannot be generated with the function.
8361
 
8362
         We first check this in update_vtable_entry_for_fn, so we handle
8363
         restored primary bases properly; we also need to do it here so we
8364
         zero out unused slots in ctor vtables, rather than filling them
8365
         with erroneous values (though harmless, apart from relocation
8366
         costs).  */
8367
      if (BV_LOST_PRIMARY (v))
8368
        init = size_zero_node;
8369
 
8370
      if (! init)
8371
        {
8372
          /* Pull the offset for `this', and the function to call, out of
8373
             the list.  */
8374
          delta = BV_DELTA (v);
8375
          vcall_index = BV_VCALL_INDEX (v);
8376
 
8377
          gcc_assert (TREE_CODE (delta) == INTEGER_CST);
8378
          gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8379
 
8380
          /* You can't call an abstract virtual function; it's abstract.
8381
             So, we replace these functions with __pure_virtual.  */
8382
          if (DECL_PURE_VIRTUAL_P (fn_original))
8383
            {
8384
              fn = abort_fndecl;
8385
              if (!TARGET_VTABLE_USES_DESCRIPTORS)
8386
                {
8387
                  if (abort_fndecl_addr == NULL)
8388
                    abort_fndecl_addr
8389
                      = fold_convert (vfunc_ptr_type_node,
8390
                                      build_fold_addr_expr (fn));
8391
                  init = abort_fndecl_addr;
8392
                }
8393
            }
8394
          /* Likewise for deleted virtuals.  */
8395
          else if (DECL_DELETED_FN (fn_original))
8396
            {
8397
              fn = get_identifier ("__cxa_deleted_virtual");
8398
              if (!get_global_value_if_present (fn, &fn))
8399
                fn = push_library_fn (fn, (build_function_type_list
8400
                                           (void_type_node, NULL_TREE)),
8401
                                      NULL_TREE);
8402
              if (!TARGET_VTABLE_USES_DESCRIPTORS)
8403
                init = fold_convert (vfunc_ptr_type_node,
8404
                                     build_fold_addr_expr (fn));
8405
            }
8406
          else
8407
            {
8408
              if (!integer_zerop (delta) || vcall_index)
8409
                {
8410
                  fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index);
8411
                  if (!DECL_NAME (fn))
8412
                    finish_thunk (fn);
8413
                }
8414
              /* Take the address of the function, considering it to be of an
8415
                 appropriate generic type.  */
8416
              if (!TARGET_VTABLE_USES_DESCRIPTORS)
8417
                init = fold_convert (vfunc_ptr_type_node,
8418
                                     build_fold_addr_expr (fn));
8419
            }
8420
        }
8421
 
8422
      /* And add it to the chain of initializers.  */
8423
      if (TARGET_VTABLE_USES_DESCRIPTORS)
8424
        {
8425
          int i;
8426
          if (init == size_zero_node)
8427
            for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
8428
              CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8429
          else
8430
            for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
8431
              {
8432
                tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
8433
                                     fn, build_int_cst (NULL_TREE, i));
8434
                TREE_CONSTANT (fdesc) = 1;
8435
 
8436
                CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
8437
              }
8438
        }
8439
      else
8440
        CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8441
    }
8442
}
8443
 
8444
/* Adds to vid->inits the initializers for the vbase and vcall
8445
   offsets in BINFO, which is in the hierarchy dominated by T.  */
8446
 
8447
static void
8448
build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
8449
{
8450
  tree b;
8451
 
8452
  /* If this is a derived class, we must first create entries
8453
     corresponding to the primary base class.  */
8454
  b = get_primary_binfo (binfo);
8455
  if (b)
8456
    build_vcall_and_vbase_vtbl_entries (b, vid);
8457
 
8458
  /* Add the vbase entries for this base.  */
8459
  build_vbase_offset_vtbl_entries (binfo, vid);
8460
  /* Add the vcall entries for this base.  */
8461
  build_vcall_offset_vtbl_entries (binfo, vid);
8462
}
8463
 
8464
/* Returns the initializers for the vbase offset entries in the vtable
8465
   for BINFO (which is part of the class hierarchy dominated by T), in
8466
   reverse order.  VBASE_OFFSET_INDEX gives the vtable index
8467
   where the next vbase offset will go.  */
8468
 
8469
static void
8470
build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
8471
{
8472
  tree vbase;
8473
  tree t;
8474
  tree non_primary_binfo;
8475
 
8476
  /* If there are no virtual baseclasses, then there is nothing to
8477
     do.  */
8478
  if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
8479
    return;
8480
 
8481
  t = vid->derived;
8482
 
8483
  /* We might be a primary base class.  Go up the inheritance hierarchy
8484
     until we find the most derived class of which we are a primary base:
8485
     it is the offset of that which we need to use.  */
8486
  non_primary_binfo = binfo;
8487
  while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
8488
    {
8489
      tree b;
8490
 
8491
      /* If we have reached a virtual base, then it must be a primary
8492
         base (possibly multi-level) of vid->binfo, or we wouldn't
8493
         have called build_vcall_and_vbase_vtbl_entries for it.  But it
8494
         might be a lost primary, so just skip down to vid->binfo.  */
8495
      if (BINFO_VIRTUAL_P (non_primary_binfo))
8496
        {
8497
          non_primary_binfo = vid->binfo;
8498
          break;
8499
        }
8500
 
8501
      b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
8502
      if (get_primary_binfo (b) != non_primary_binfo)
8503
        break;
8504
      non_primary_binfo = b;
8505
    }
8506
 
8507
  /* Go through the virtual bases, adding the offsets.  */
8508
  for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
8509
       vbase;
8510
       vbase = TREE_CHAIN (vbase))
8511
    {
8512
      tree b;
8513
      tree delta;
8514
 
8515
      if (!BINFO_VIRTUAL_P (vbase))
8516
        continue;
8517
 
8518
      /* Find the instance of this virtual base in the complete
8519
         object.  */
8520
      b = copied_binfo (vbase, binfo);
8521
 
8522
      /* If we've already got an offset for this virtual base, we
8523
         don't need another one.  */
8524
      if (BINFO_VTABLE_PATH_MARKED (b))
8525
        continue;
8526
      BINFO_VTABLE_PATH_MARKED (b) = 1;
8527
 
8528
      /* Figure out where we can find this vbase offset.  */
8529
      delta = size_binop (MULT_EXPR,
8530
                          vid->index,
8531
                          convert (ssizetype,
8532
                                   TYPE_SIZE_UNIT (vtable_entry_type)));
8533
      if (vid->primary_vtbl_p)
8534
        BINFO_VPTR_FIELD (b) = delta;
8535
 
8536
      if (binfo != TYPE_BINFO (t))
8537
        /* The vbase offset had better be the same.  */
8538
        gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
8539
 
8540
      /* The next vbase will come at a more negative offset.  */
8541
      vid->index = size_binop (MINUS_EXPR, vid->index,
8542
                               ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
8543
 
8544
      /* The initializer is the delta from BINFO to this virtual base.
8545
         The vbase offsets go in reverse inheritance-graph order, and
8546
         we are walking in inheritance graph order so these end up in
8547
         the right order.  */
8548
      delta = size_diffop_loc (input_location,
8549
                           BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
8550
 
8551
      CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
8552
                              fold_build1_loc (input_location, NOP_EXPR,
8553
                                               vtable_entry_type, delta));
8554
    }
8555
}
8556
 
8557
/* Adds the initializers for the vcall offset entries in the vtable
8558
   for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
8559
   to VID->INITS.  */
8560
 
8561
static void
8562
build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
8563
{
8564
  /* We only need these entries if this base is a virtual base.  We
8565
     compute the indices -- but do not add to the vtable -- when
8566
     building the main vtable for a class.  */
8567
  if (binfo == TYPE_BINFO (vid->derived)
8568
      || (BINFO_VIRTUAL_P (binfo)
8569
          /* If BINFO is RTTI_BINFO, then (since BINFO does not
8570
             correspond to VID->DERIVED), we are building a primary
8571
             construction virtual table.  Since this is a primary
8572
             virtual table, we do not need the vcall offsets for
8573
             BINFO.  */
8574
          && binfo != vid->rtti_binfo))
8575
    {
8576
      /* We need a vcall offset for each of the virtual functions in this
8577
         vtable.  For example:
8578
 
8579
           class A { virtual void f (); };
8580
           class B1 : virtual public A { virtual void f (); };
8581
           class B2 : virtual public A { virtual void f (); };
8582
           class C: public B1, public B2 { virtual void f (); };
8583
 
8584
         A C object has a primary base of B1, which has a primary base of A.  A
8585
         C also has a secondary base of B2, which no longer has a primary base
8586
         of A.  So the B2-in-C construction vtable needs a secondary vtable for
8587
         A, which will adjust the A* to a B2* to call f.  We have no way of
8588
         knowing what (or even whether) this offset will be when we define B2,
8589
         so we store this "vcall offset" in the A sub-vtable and look it up in
8590
         a "virtual thunk" for B2::f.
8591
 
8592
         We need entries for all the functions in our primary vtable and
8593
         in our non-virtual bases' secondary vtables.  */
8594
      vid->vbase = binfo;
8595
      /* If we are just computing the vcall indices -- but do not need
8596
         the actual entries -- not that.  */
8597
      if (!BINFO_VIRTUAL_P (binfo))
8598
        vid->generate_vcall_entries = false;
8599
      /* Now, walk through the non-virtual bases, adding vcall offsets.  */
8600
      add_vcall_offset_vtbl_entries_r (binfo, vid);
8601
    }
8602
}
8603
 
8604
/* Build vcall offsets, starting with those for BINFO.  */
8605
 
8606
static void
8607
add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
8608
{
8609
  int i;
8610
  tree primary_binfo;
8611
  tree base_binfo;
8612
 
8613
  /* Don't walk into virtual bases -- except, of course, for the
8614
     virtual base for which we are building vcall offsets.  Any
8615
     primary virtual base will have already had its offsets generated
8616
     through the recursion in build_vcall_and_vbase_vtbl_entries.  */
8617
  if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
8618
    return;
8619
 
8620
  /* If BINFO has a primary base, process it first.  */
8621
  primary_binfo = get_primary_binfo (binfo);
8622
  if (primary_binfo)
8623
    add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
8624
 
8625
  /* Add BINFO itself to the list.  */
8626
  add_vcall_offset_vtbl_entries_1 (binfo, vid);
8627
 
8628
  /* Scan the non-primary bases of BINFO.  */
8629
  for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8630
    if (base_binfo != primary_binfo)
8631
      add_vcall_offset_vtbl_entries_r (base_binfo, vid);
8632
}
8633
 
8634
/* Called from build_vcall_offset_vtbl_entries_r.  */
8635
 
8636
static void
8637
add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
8638
{
8639
  /* Make entries for the rest of the virtuals.  */
8640
  if (abi_version_at_least (2))
8641
    {
8642
      tree orig_fn;
8643
 
8644
      /* The ABI requires that the methods be processed in declaration
8645
         order.  G++ 3.2 used the order in the vtable.  */
8646
      for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo));
8647
           orig_fn;
8648
           orig_fn = DECL_CHAIN (orig_fn))
8649
        if (DECL_VINDEX (orig_fn))
8650
          add_vcall_offset (orig_fn, binfo, vid);
8651
    }
8652
  else
8653
    {
8654
      tree derived_virtuals;
8655
      tree base_virtuals;
8656
      tree orig_virtuals;
8657
      /* If BINFO is a primary base, the most derived class which has
8658
         BINFO as a primary base; otherwise, just BINFO.  */
8659
      tree non_primary_binfo;
8660
 
8661
      /* We might be a primary base class.  Go up the inheritance hierarchy
8662
         until we find the most derived class of which we are a primary base:
8663
         it is the BINFO_VIRTUALS there that we need to consider.  */
8664
      non_primary_binfo = binfo;
8665
      while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
8666
        {
8667
          tree b;
8668
 
8669
          /* If we have reached a virtual base, then it must be vid->vbase,
8670
             because we ignore other virtual bases in
8671
             add_vcall_offset_vtbl_entries_r.  In turn, it must be a primary
8672
             base (possibly multi-level) of vid->binfo, or we wouldn't
8673
             have called build_vcall_and_vbase_vtbl_entries for it.  But it
8674
             might be a lost primary, so just skip down to vid->binfo.  */
8675
          if (BINFO_VIRTUAL_P (non_primary_binfo))
8676
            {
8677
              gcc_assert (non_primary_binfo == vid->vbase);
8678
              non_primary_binfo = vid->binfo;
8679
              break;
8680
            }
8681
 
8682
          b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
8683
          if (get_primary_binfo (b) != non_primary_binfo)
8684
            break;
8685
          non_primary_binfo = b;
8686
        }
8687
 
8688
      if (vid->ctor_vtbl_p)
8689
        /* For a ctor vtable we need the equivalent binfo within the hierarchy
8690
           where rtti_binfo is the most derived type.  */
8691
        non_primary_binfo
8692
          = original_binfo (non_primary_binfo, vid->rtti_binfo);
8693
 
8694
      for (base_virtuals = BINFO_VIRTUALS (binfo),
8695
             derived_virtuals = BINFO_VIRTUALS (non_primary_binfo),
8696
             orig_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
8697
           base_virtuals;
8698
           base_virtuals = TREE_CHAIN (base_virtuals),
8699
             derived_virtuals = TREE_CHAIN (derived_virtuals),
8700
             orig_virtuals = TREE_CHAIN (orig_virtuals))
8701
        {
8702
          tree orig_fn;
8703
 
8704
          /* Find the declaration that originally caused this function to
8705
             be present in BINFO_TYPE (binfo).  */
8706
          orig_fn = BV_FN (orig_virtuals);
8707
 
8708
          /* When processing BINFO, we only want to generate vcall slots for
8709
             function slots introduced in BINFO.  So don't try to generate
8710
             one if the function isn't even defined in BINFO.  */
8711
          if (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), DECL_CONTEXT (orig_fn)))
8712
            continue;
8713
 
8714
          add_vcall_offset (orig_fn, binfo, vid);
8715
        }
8716
    }
8717
}
8718
 
8719
/* Add a vcall offset entry for ORIG_FN to the vtable.  */
8720
 
8721
static void
8722
add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
8723
{
8724
  size_t i;
8725
  tree vcall_offset;
8726
  tree derived_entry;
8727
 
8728
  /* If there is already an entry for a function with the same
8729
     signature as FN, then we do not need a second vcall offset.
8730
     Check the list of functions already present in the derived
8731
     class vtable.  */
8732
  FOR_EACH_VEC_ELT (tree, vid->fns, i, derived_entry)
8733
    {
8734
      if (same_signature_p (derived_entry, orig_fn)
8735
          /* We only use one vcall offset for virtual destructors,
8736
             even though there are two virtual table entries.  */
8737
          || (DECL_DESTRUCTOR_P (derived_entry)
8738
              && DECL_DESTRUCTOR_P (orig_fn)))
8739
        return;
8740
    }
8741
 
8742
  /* If we are building these vcall offsets as part of building
8743
     the vtable for the most derived class, remember the vcall
8744
     offset.  */
8745
  if (vid->binfo == TYPE_BINFO (vid->derived))
8746
    {
8747
      tree_pair_p elt = VEC_safe_push (tree_pair_s, gc,
8748
                                       CLASSTYPE_VCALL_INDICES (vid->derived),
8749
                                       NULL);
8750
      elt->purpose = orig_fn;
8751
      elt->value = vid->index;
8752
    }
8753
 
8754
  /* The next vcall offset will be found at a more negative
8755
     offset.  */
8756
  vid->index = size_binop (MINUS_EXPR, vid->index,
8757
                           ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
8758
 
8759
  /* Keep track of this function.  */
8760
  VEC_safe_push (tree, gc, vid->fns, orig_fn);
8761
 
8762
  if (vid->generate_vcall_entries)
8763
    {
8764
      tree base;
8765
      tree fn;
8766
 
8767
      /* Find the overriding function.  */
8768
      fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
8769
      if (fn == error_mark_node)
8770
        vcall_offset = build_zero_cst (vtable_entry_type);
8771
      else
8772
        {
8773
          base = TREE_VALUE (fn);
8774
 
8775
          /* The vbase we're working on is a primary base of
8776
             vid->binfo.  But it might be a lost primary, so its
8777
             BINFO_OFFSET might be wrong, so we just use the
8778
             BINFO_OFFSET from vid->binfo.  */
8779
          vcall_offset = size_diffop_loc (input_location,
8780
                                      BINFO_OFFSET (base),
8781
                                      BINFO_OFFSET (vid->binfo));
8782
          vcall_offset = fold_build1_loc (input_location,
8783
                                      NOP_EXPR, vtable_entry_type,
8784
                                      vcall_offset);
8785
        }
8786
      /* Add the initializer to the vtable.  */
8787
      CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
8788
    }
8789
}
8790
 
8791
/* Return vtbl initializers for the RTTI entries corresponding to the
8792
   BINFO's vtable.  The RTTI entries should indicate the object given
8793
   by VID->rtti_binfo.  */
8794
 
8795
static void
8796
build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
8797
{
8798
  tree b;
8799
  tree t;
8800
  tree offset;
8801
  tree decl;
8802
  tree init;
8803
 
8804
  t = BINFO_TYPE (vid->rtti_binfo);
8805
 
8806
  /* To find the complete object, we will first convert to our most
8807
     primary base, and then add the offset in the vtbl to that value.  */
8808
  b = binfo;
8809
  while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8810
         && !BINFO_LOST_PRIMARY_P (b))
8811
    {
8812
      tree primary_base;
8813
 
8814
      primary_base = get_primary_binfo (b);
8815
      gcc_assert (BINFO_PRIMARY_P (primary_base)
8816
                  && BINFO_INHERITANCE_CHAIN (primary_base) == b);
8817
      b = primary_base;
8818
    }
8819
  offset = size_diffop_loc (input_location,
8820
                        BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
8821
 
8822
  /* The second entry is the address of the typeinfo object.  */
8823
  if (flag_rtti)
8824
    decl = build_address (get_tinfo_decl (t));
8825
  else
8826
    decl = integer_zero_node;
8827
 
8828
  /* Convert the declaration to a type that can be stored in the
8829
     vtable.  */
8830
  init = build_nop (vfunc_ptr_type_node, decl);
8831
  CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
8832
 
8833
  /* Add the offset-to-top entry.  It comes earlier in the vtable than
8834
     the typeinfo entry.  Convert the offset to look like a
8835
     function pointer, so that we can put it in the vtable.  */
8836
  init = build_nop (vfunc_ptr_type_node, offset);
8837
  CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
8838
}
8839
 
8840
#include "gt-cp-class.h"

powered by: WebSVN 2.1.0

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