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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [gcc-4.5.1/] [gcc/] [cp/] [decl2.c] - Blame information for rev 847

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

Line No. Rev Author Line
1 283 jeremybenn
/* Process declarations and variables for C++ compiler.
2
   Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3
   1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009
4
   Free Software Foundation, Inc.
5
   Hacked 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
/* Process declarations and symbol lookup for C++ front end.
25
   Also constructs types; the standard scalar types at initialization,
26
   and structure, union, array and enum types when they are declared.  */
27
 
28
/* ??? not all decl nodes are given the most useful possible
29
   line numbers.  For example, the CONST_DECLs for enum values.  */
30
 
31
#include "config.h"
32
#include "system.h"
33
#include "coretypes.h"
34
#include "tm.h"
35
#include "tree.h"
36
#include "rtl.h"
37
#include "expr.h"
38
#include "flags.h"
39
#include "cp-tree.h"
40
#include "decl.h"
41
#include "output.h"
42
#include "except.h"
43
#include "toplev.h"
44
#include "timevar.h"
45
#include "cpplib.h"
46
#include "target.h"
47
#include "c-common.h"
48
#include "tree-mudflap.h"
49
#include "cgraph.h"
50
#include "tree-inline.h"
51
#include "c-pragma.h"
52
#include "tree-dump.h"
53
#include "intl.h"
54
#include "gimple.h"
55
#include "pointer-set.h"
56
 
57
extern cpp_reader *parse_in;
58
 
59
/* This structure contains information about the initializations
60
   and/or destructions required for a particular priority level.  */
61
typedef struct priority_info_s {
62
  /* Nonzero if there have been any initializations at this priority
63
     throughout the translation unit.  */
64
  int initializations_p;
65
  /* Nonzero if there have been any destructions at this priority
66
     throughout the translation unit.  */
67
  int destructions_p;
68
} *priority_info;
69
 
70
static void mark_vtable_entries (tree);
71
static bool maybe_emit_vtables (tree);
72
static bool acceptable_java_type (tree);
73
static tree start_objects (int, int);
74
static void finish_objects (int, int, tree);
75
static tree start_static_storage_duration_function (unsigned);
76
static void finish_static_storage_duration_function (tree);
77
static priority_info get_priority_info (int);
78
static void do_static_initialization_or_destruction (tree, bool);
79
static void one_static_initialization_or_destruction (tree, tree, bool);
80
static void generate_ctor_or_dtor_function (bool, int, location_t *);
81
static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
82
                                                          void *);
83
static tree prune_vars_needing_no_initialization (tree *);
84
static void write_out_vars (tree);
85
static void import_export_class (tree);
86
static tree get_guard_bits (tree);
87
static void determine_visibility_from_class (tree, tree);
88
static bool decl_defined_p (tree);
89
 
90
/* A list of static class variables.  This is needed, because a
91
   static class variable can be declared inside the class without
92
   an initializer, and then initialized, statically, outside the class.  */
93
static GTY(()) VEC(tree,gc) *pending_statics;
94
 
95
/* A list of functions which were declared inline, but which we
96
   may need to emit outline anyway.  */
97
static GTY(()) VEC(tree,gc) *deferred_fns;
98
 
99
/* A list of decls that use types with no linkage, which we need to make
100
   sure are defined.  */
101
static GTY(()) VEC(tree,gc) *no_linkage_decls;
102
 
103
/* Nonzero if we're done parsing and into end-of-file activities.  */
104
 
105
int at_eof;
106
 
107
 
108
 
109
/* Return a member function type (a METHOD_TYPE), given FNTYPE (a
110
   FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
111
   that apply to the function).  */
112
 
113
tree
114
build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals)
115
{
116
  tree raises;
117
  tree attrs;
118
  int type_quals;
119
 
120
  if (fntype == error_mark_node || ctype == error_mark_node)
121
    return error_mark_node;
122
 
123
  gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
124
              || TREE_CODE (fntype) == METHOD_TYPE);
125
 
126
  type_quals = quals & ~TYPE_QUAL_RESTRICT;
127
  ctype = cp_build_qualified_type (ctype, type_quals);
128
  raises = TYPE_RAISES_EXCEPTIONS (fntype);
129
  attrs = TYPE_ATTRIBUTES (fntype);
130
  fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
131
                                       (TREE_CODE (fntype) == METHOD_TYPE
132
                                        ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
133
                                        : TYPE_ARG_TYPES (fntype)));
134
  if (raises)
135
    fntype = build_exception_variant (fntype, raises);
136
  if (attrs)
137
    fntype = cp_build_type_attribute_variant (fntype, attrs);
138
 
139
  return fntype;
140
}
141
 
142
/* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
143
   return type changed to NEW_RET.  */
144
 
145
tree
146
change_return_type (tree new_ret, tree fntype)
147
{
148
  tree newtype;
149
  tree args = TYPE_ARG_TYPES (fntype);
150
  tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
151
  tree attrs = TYPE_ATTRIBUTES (fntype);
152
 
153
  if (same_type_p (new_ret, TREE_TYPE (fntype)))
154
    return fntype;
155
 
156
  if (TREE_CODE (fntype) == FUNCTION_TYPE)
157
    newtype = build_function_type (new_ret, args);
158
  else
159
    newtype = build_method_type_directly
160
      (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fntype))),
161
       new_ret, TREE_CHAIN (args));
162
  if (raises)
163
    newtype = build_exception_variant (newtype, raises);
164
  if (attrs)
165
    newtype = cp_build_type_attribute_variant (newtype, attrs);
166
 
167
  return newtype;
168
}
169
 
170
/* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
171
   appropriately.  */
172
 
173
tree
174
cp_build_parm_decl (tree name, tree type)
175
{
176
  tree parm = build_decl (input_location,
177
                          PARM_DECL, name, type);
178
  /* DECL_ARG_TYPE is only used by the back end and the back end never
179
     sees templates.  */
180
  if (!processing_template_decl)
181
    DECL_ARG_TYPE (parm) = type_passed_as (type);
182
 
183
  /* If the type is a pack expansion, then we have a function
184
     parameter pack. */
185
  if (type && TREE_CODE (type) == TYPE_PACK_EXPANSION)
186
    FUNCTION_PARAMETER_PACK_P (parm) = 1;
187
 
188
  return parm;
189
}
190
 
191
/* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
192
   indicated NAME.  */
193
 
194
tree
195
build_artificial_parm (tree name, tree type)
196
{
197
  tree parm = cp_build_parm_decl (name, type);
198
  DECL_ARTIFICIAL (parm) = 1;
199
  /* All our artificial parms are implicitly `const'; they cannot be
200
     assigned to.  */
201
  TREE_READONLY (parm) = 1;
202
  return parm;
203
}
204
 
205
/* Constructors for types with virtual baseclasses need an "in-charge" flag
206
   saying whether this constructor is responsible for initialization of
207
   virtual baseclasses or not.  All destructors also need this "in-charge"
208
   flag, which additionally determines whether or not the destructor should
209
   free the memory for the object.
210
 
211
   This function adds the "in-charge" flag to member function FN if
212
   appropriate.  It is called from grokclassfn and tsubst.
213
   FN must be either a constructor or destructor.
214
 
215
   The in-charge flag follows the 'this' parameter, and is followed by the
216
   VTT parm (if any), then the user-written parms.  */
217
 
218
void
219
maybe_retrofit_in_chrg (tree fn)
220
{
221
  tree basetype, arg_types, parms, parm, fntype;
222
 
223
  /* If we've already add the in-charge parameter don't do it again.  */
224
  if (DECL_HAS_IN_CHARGE_PARM_P (fn))
225
    return;
226
 
227
  /* When processing templates we can't know, in general, whether or
228
     not we're going to have virtual baseclasses.  */
229
  if (processing_template_decl)
230
    return;
231
 
232
  /* We don't need an in-charge parameter for constructors that don't
233
     have virtual bases.  */
234
  if (DECL_CONSTRUCTOR_P (fn)
235
      && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
236
    return;
237
 
238
  arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
239
  basetype = TREE_TYPE (TREE_VALUE (arg_types));
240
  arg_types = TREE_CHAIN (arg_types);
241
 
242
  parms = TREE_CHAIN (DECL_ARGUMENTS (fn));
243
 
244
  /* If this is a subobject constructor or destructor, our caller will
245
     pass us a pointer to our VTT.  */
246
  if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
247
    {
248
      parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
249
 
250
      /* First add it to DECL_ARGUMENTS between 'this' and the real args...  */
251
      TREE_CHAIN (parm) = parms;
252
      parms = parm;
253
 
254
      /* ...and then to TYPE_ARG_TYPES.  */
255
      arg_types = hash_tree_chain (vtt_parm_type, arg_types);
256
 
257
      DECL_HAS_VTT_PARM_P (fn) = 1;
258
    }
259
 
260
  /* Then add the in-charge parm (before the VTT parm).  */
261
  parm = build_artificial_parm (in_charge_identifier, integer_type_node);
262
  TREE_CHAIN (parm) = parms;
263
  parms = parm;
264
  arg_types = hash_tree_chain (integer_type_node, arg_types);
265
 
266
  /* Insert our new parameter(s) into the list.  */
267
  TREE_CHAIN (DECL_ARGUMENTS (fn)) = parms;
268
 
269
  /* And rebuild the function type.  */
270
  fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
271
                                       arg_types);
272
  if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
273
    fntype = build_exception_variant (fntype,
274
                                      TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
275
  if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
276
    fntype = (cp_build_type_attribute_variant
277
              (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
278
  TREE_TYPE (fn) = fntype;
279
 
280
  /* Now we've got the in-charge parameter.  */
281
  DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
282
}
283
 
284
/* Classes overload their constituent function names automatically.
285
   When a function name is declared in a record structure,
286
   its name is changed to it overloaded name.  Since names for
287
   constructors and destructors can conflict, we place a leading
288
   '$' for destructors.
289
 
290
   CNAME is the name of the class we are grokking for.
291
 
292
   FUNCTION is a FUNCTION_DECL.  It was created by `grokdeclarator'.
293
 
294
   FLAGS contains bits saying what's special about today's
295
   arguments.  DTOR_FLAG == DESTRUCTOR.
296
 
297
   If FUNCTION is a destructor, then we must add the `auto-delete' field
298
   as a second parameter.  There is some hair associated with the fact
299
   that we must "declare" this variable in the manner consistent with the
300
   way the rest of the arguments were declared.
301
 
302
   QUALS are the qualifiers for the this pointer.  */
303
 
304
void
305
grokclassfn (tree ctype, tree function, enum overload_flags flags)
306
{
307
  tree fn_name = DECL_NAME (function);
308
 
309
  /* Even within an `extern "C"' block, members get C++ linkage.  See
310
     [dcl.link] for details.  */
311
  SET_DECL_LANGUAGE (function, lang_cplusplus);
312
 
313
  if (fn_name == NULL_TREE)
314
    {
315
      error ("name missing for member function");
316
      fn_name = get_identifier ("<anonymous>");
317
      DECL_NAME (function) = fn_name;
318
    }
319
 
320
  DECL_CONTEXT (function) = ctype;
321
 
322
  if (flags == DTOR_FLAG)
323
    DECL_DESTRUCTOR_P (function) = 1;
324
 
325
  if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
326
    maybe_retrofit_in_chrg (function);
327
}
328
 
329
/* Create an ARRAY_REF, checking for the user doing things backwards
330
   along the way.  */
331
 
332
tree
333
grok_array_decl (tree array_expr, tree index_exp)
334
{
335
  tree type;
336
  tree expr;
337
  tree orig_array_expr = array_expr;
338
  tree orig_index_exp = index_exp;
339
 
340
  if (error_operand_p (array_expr) || error_operand_p (index_exp))
341
    return error_mark_node;
342
 
343
  if (processing_template_decl)
344
    {
345
      if (type_dependent_expression_p (array_expr)
346
          || type_dependent_expression_p (index_exp))
347
        return build_min_nt (ARRAY_REF, array_expr, index_exp,
348
                             NULL_TREE, NULL_TREE);
349
      array_expr = build_non_dependent_expr (array_expr);
350
      index_exp = build_non_dependent_expr (index_exp);
351
    }
352
 
353
  type = TREE_TYPE (array_expr);
354
  gcc_assert (type);
355
  type = non_reference (type);
356
 
357
  /* If they have an `operator[]', use that.  */
358
  if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
359
    expr = build_new_op (ARRAY_REF, LOOKUP_NORMAL,
360
                         array_expr, index_exp, NULL_TREE,
361
                         /*overloaded_p=*/NULL, tf_warning_or_error);
362
  else
363
    {
364
      tree p1, p2, i1, i2;
365
 
366
      /* Otherwise, create an ARRAY_REF for a pointer or array type.
367
         It is a little-known fact that, if `a' is an array and `i' is
368
         an int, you can write `i[a]', which means the same thing as
369
         `a[i]'.  */
370
      if (TREE_CODE (type) == ARRAY_TYPE)
371
        p1 = array_expr;
372
      else
373
        p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
374
 
375
      if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
376
        p2 = index_exp;
377
      else
378
        p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
379
 
380
      i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
381
                                       false);
382
      i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
383
                                       false);
384
 
385
      if ((p1 && i2) && (i1 && p2))
386
        error ("ambiguous conversion for array subscript");
387
 
388
      if (p1 && i2)
389
        array_expr = p1, index_exp = i2;
390
      else if (i1 && p2)
391
        array_expr = p2, index_exp = i1;
392
      else
393
        {
394
          error ("invalid types %<%T[%T]%> for array subscript",
395
                 type, TREE_TYPE (index_exp));
396
          return error_mark_node;
397
        }
398
 
399
      if (array_expr == error_mark_node || index_exp == error_mark_node)
400
        error ("ambiguous conversion for array subscript");
401
 
402
      expr = build_array_ref (input_location, array_expr, index_exp);
403
    }
404
  if (processing_template_decl && expr != error_mark_node)
405
    return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
406
                              NULL_TREE, NULL_TREE);
407
  return expr;
408
}
409
 
410
/* Given the cast expression EXP, checking out its validity.   Either return
411
   an error_mark_node if there was an unavoidable error, return a cast to
412
   void for trying to delete a pointer w/ the value 0, or return the
413
   call to delete.  If DOING_VEC is true, we handle things differently
414
   for doing an array delete.
415
   Implements ARM $5.3.4.  This is called from the parser.  */
416
 
417
tree
418
delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete)
419
{
420
  tree t, type;
421
 
422
  if (exp == error_mark_node)
423
    return exp;
424
 
425
  if (processing_template_decl)
426
    {
427
      t = build_min (DELETE_EXPR, void_type_node, exp, size);
428
      DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
429
      DELETE_EXPR_USE_VEC (t) = doing_vec;
430
      TREE_SIDE_EFFECTS (t) = 1;
431
      return t;
432
    }
433
 
434
  /* An array can't have been allocated by new, so complain.  */
435
  if (TREE_CODE (exp) == VAR_DECL
436
      && TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
437
    warning (0, "deleting array %q#D", exp);
438
 
439
  t = build_expr_type_conversion (WANT_POINTER, exp, true);
440
 
441
  if (t == NULL_TREE || t == error_mark_node)
442
    {
443
      error ("type %q#T argument given to %<delete%>, expected pointer",
444
             TREE_TYPE (exp));
445
      return error_mark_node;
446
    }
447
 
448
  type = TREE_TYPE (t);
449
 
450
  /* As of Valley Forge, you can delete a pointer to const.  */
451
 
452
  /* You can't delete functions.  */
453
  if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
454
    {
455
      error ("cannot delete a function.  Only pointer-to-objects are "
456
             "valid arguments to %<delete%>");
457
      return error_mark_node;
458
    }
459
 
460
  /* Deleting ptr to void is undefined behavior [expr.delete/3].  */
461
  if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
462
    {
463
      warning (0, "deleting %qT is undefined", type);
464
      doing_vec = 0;
465
    }
466
 
467
  /* Deleting a pointer with the value zero is valid and has no effect.  */
468
  if (integer_zerop (t))
469
    return build1 (NOP_EXPR, void_type_node, t);
470
 
471
  if (doing_vec)
472
    return build_vec_delete (t, /*maxindex=*/NULL_TREE,
473
                             sfk_deleting_destructor,
474
                             use_global_delete);
475
  else
476
    return build_delete (type, t, sfk_deleting_destructor,
477
                         LOOKUP_NORMAL, use_global_delete);
478
}
479
 
480
/* Report an error if the indicated template declaration is not the
481
   sort of thing that should be a member template.  */
482
 
483
void
484
check_member_template (tree tmpl)
485
{
486
  tree decl;
487
 
488
  gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
489
  decl = DECL_TEMPLATE_RESULT (tmpl);
490
 
491
  if (TREE_CODE (decl) == FUNCTION_DECL
492
      || (TREE_CODE (decl) == TYPE_DECL
493
          && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
494
    {
495
      /* The parser rejects template declarations in local classes.  */
496
      gcc_assert (!current_function_decl);
497
      /* The parser rejects any use of virtual in a function template.  */
498
      gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
499
                    && DECL_VIRTUAL_P (decl)));
500
 
501
      /* The debug-information generating code doesn't know what to do
502
         with member templates.  */
503
      DECL_IGNORED_P (tmpl) = 1;
504
    }
505
  else
506
    error ("template declaration of %q#D", decl);
507
}
508
 
509
/* Return true iff TYPE is a valid Java parameter or return type.  */
510
 
511
static bool
512
acceptable_java_type (tree type)
513
{
514
  if (type == error_mark_node)
515
    return false;
516
 
517
  if (TREE_CODE (type) == VOID_TYPE || TYPE_FOR_JAVA (type))
518
    return true;
519
  if (TREE_CODE (type) == POINTER_TYPE || TREE_CODE (type) == REFERENCE_TYPE)
520
    {
521
      type = TREE_TYPE (type);
522
      if (TREE_CODE (type) == RECORD_TYPE)
523
        {
524
          tree args;  int i;
525
          if (! TYPE_FOR_JAVA (type))
526
            return false;
527
          if (! CLASSTYPE_TEMPLATE_INFO (type))
528
            return true;
529
          args = CLASSTYPE_TI_ARGS (type);
530
          i = TREE_VEC_LENGTH (args);
531
          while (--i >= 0)
532
            {
533
              type = TREE_VEC_ELT (args, i);
534
              if (TREE_CODE (type) == POINTER_TYPE)
535
                type = TREE_TYPE (type);
536
              if (! TYPE_FOR_JAVA (type))
537
                return false;
538
            }
539
          return true;
540
        }
541
    }
542
  return false;
543
}
544
 
545
/* For a METHOD in a Java class CTYPE, return true if
546
   the parameter and return types are valid Java types.
547
   Otherwise, print appropriate error messages, and return false.  */
548
 
549
bool
550
check_java_method (tree method)
551
{
552
  bool jerr = false;
553
  tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
554
  tree ret_type = TREE_TYPE (TREE_TYPE (method));
555
 
556
  if (!acceptable_java_type (ret_type))
557
    {
558
      error ("Java method %qD has non-Java return type %qT",
559
             method, ret_type);
560
      jerr = true;
561
    }
562
 
563
  arg_types = TREE_CHAIN (arg_types);
564
  if (DECL_HAS_IN_CHARGE_PARM_P (method))
565
    arg_types = TREE_CHAIN (arg_types);
566
  if (DECL_HAS_VTT_PARM_P (method))
567
    arg_types = TREE_CHAIN (arg_types);
568
 
569
  for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
570
    {
571
      tree type = TREE_VALUE (arg_types);
572
      if (!acceptable_java_type (type))
573
        {
574
          if (type != error_mark_node)
575
            error ("Java method %qD has non-Java parameter type %qT",
576
                   method, type);
577
          jerr = true;
578
        }
579
    }
580
  return !jerr;
581
}
582
 
583
/* Sanity check: report error if this function FUNCTION is not
584
   really a member of the class (CTYPE) it is supposed to belong to.
585
   TEMPLATE_PARMS is used to specify the template parameters of a member
586
   template passed as FUNCTION_DECL. If the member template is passed as a
587
   TEMPLATE_DECL, it can be NULL since the parameters can be extracted
588
   from the declaration. If the function is not a function template, it
589
   must be NULL.
590
   It returns the original declaration for the function, NULL_TREE if
591
   no declaration was found, error_mark_node if an error was emitted.  */
592
 
593
tree
594
check_classfn (tree ctype, tree function, tree template_parms)
595
{
596
  int ix;
597
  bool is_template;
598
  tree pushed_scope;
599
 
600
  if (DECL_USE_TEMPLATE (function)
601
      && !(TREE_CODE (function) == TEMPLATE_DECL
602
           && DECL_TEMPLATE_SPECIALIZATION (function))
603
      && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
604
    /* Since this is a specialization of a member template,
605
       we're not going to find the declaration in the class.
606
       For example, in:
607
 
608
         struct S { template <typename T> void f(T); };
609
         template <> void S::f(int);
610
 
611
       we're not going to find `S::f(int)', but there's no
612
       reason we should, either.  We let our callers know we didn't
613
       find the method, but we don't complain.  */
614
    return NULL_TREE;
615
 
616
  /* Basic sanity check: for a template function, the template parameters
617
     either were not passed, or they are the same of DECL_TEMPLATE_PARMS.  */
618
  if (TREE_CODE (function) == TEMPLATE_DECL)
619
    {
620
      if (template_parms
621
          && !comp_template_parms (template_parms,
622
                                   DECL_TEMPLATE_PARMS (function)))
623
        {
624
          error ("template parameter lists provided don't match the "
625
                 "template parameters of %qD", function);
626
          return error_mark_node;
627
        }
628
      template_parms = DECL_TEMPLATE_PARMS (function);
629
    }
630
 
631
  /* OK, is this a definition of a member template?  */
632
  is_template = (template_parms != NULL_TREE);
633
 
634
  /* We must enter the scope here, because conversion operators are
635
     named by target type, and type equivalence relies on typenames
636
     resolving within the scope of CTYPE.  */
637
  pushed_scope = push_scope (ctype);
638
  ix = class_method_index_for_fn (complete_type (ctype), function);
639
  if (ix >= 0)
640
    {
641
      VEC(tree,gc) *methods = CLASSTYPE_METHOD_VEC (ctype);
642
      tree fndecls, fndecl = 0;
643
      bool is_conv_op;
644
      const char *format = NULL;
645
 
646
      for (fndecls = VEC_index (tree, methods, ix);
647
           fndecls; fndecls = OVL_NEXT (fndecls))
648
        {
649
          tree p1, p2;
650
 
651
          fndecl = OVL_CURRENT (fndecls);
652
          p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
653
          p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
654
 
655
          /* We cannot simply call decls_match because this doesn't
656
             work for static member functions that are pretending to
657
             be methods, and because the name may have been changed by
658
             asm("new_name").  */
659
 
660
           /* Get rid of the this parameter on functions that become
661
              static.  */
662
          if (DECL_STATIC_FUNCTION_P (fndecl)
663
              && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
664
            p1 = TREE_CHAIN (p1);
665
 
666
          /* A member template definition only matches a member template
667
             declaration.  */
668
          if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
669
            continue;
670
 
671
          if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
672
                           TREE_TYPE (TREE_TYPE (fndecl)))
673
              && compparms (p1, p2)
674
              && (!is_template
675
                  || comp_template_parms (template_parms,
676
                                          DECL_TEMPLATE_PARMS (fndecl)))
677
              && (DECL_TEMPLATE_SPECIALIZATION (function)
678
                  == DECL_TEMPLATE_SPECIALIZATION (fndecl))
679
              && (!DECL_TEMPLATE_SPECIALIZATION (function)
680
                  || (DECL_TI_TEMPLATE (function)
681
                      == DECL_TI_TEMPLATE (fndecl))))
682
            break;
683
        }
684
      if (fndecls)
685
        {
686
          if (pushed_scope)
687
            pop_scope (pushed_scope);
688
          return OVL_CURRENT (fndecls);
689
        }
690
 
691
      error_at (DECL_SOURCE_LOCATION (function),
692
                "prototype for %q#D does not match any in class %qT",
693
                function, ctype);
694
      is_conv_op = DECL_CONV_FN_P (fndecl);
695
 
696
      if (is_conv_op)
697
        ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
698
      fndecls = VEC_index (tree, methods, ix);
699
      while (fndecls)
700
        {
701
          fndecl = OVL_CURRENT (fndecls);
702
          fndecls = OVL_NEXT (fndecls);
703
 
704
          if (!fndecls && is_conv_op)
705
            {
706
              if (VEC_length (tree, methods) > (size_t) ++ix)
707
                {
708
                  fndecls = VEC_index (tree, methods, ix);
709
                  if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
710
                    {
711
                      fndecls = NULL_TREE;
712
                      is_conv_op = false;
713
                    }
714
                }
715
              else
716
                is_conv_op = false;
717
            }
718
          if (format)
719
            format = "                %+#D";
720
          else if (fndecls)
721
            format = N_("candidates are: %+#D");
722
          else
723
            format = N_("candidate is: %+#D");
724
          error (format, fndecl);
725
        }
726
    }
727
  else if (!COMPLETE_TYPE_P (ctype))
728
    cxx_incomplete_type_error (function, ctype);
729
  else
730
    error ("no %q#D member function declared in class %qT",
731
           function, ctype);
732
 
733
  if (pushed_scope)
734
    pop_scope (pushed_scope);
735
  return error_mark_node;
736
}
737
 
738
/* DECL is a function with vague linkage.  Remember it so that at the
739
   end of the translation unit we can decide whether or not to emit
740
   it.  */
741
 
742
void
743
note_vague_linkage_fn (tree decl)
744
{
745
  DECL_DEFER_OUTPUT (decl) = 1;
746
  VEC_safe_push (tree, gc, deferred_fns, decl);
747
}
748
 
749
/* We have just processed the DECL, which is a static data member.
750
   The other parameters are as for cp_finish_decl.  */
751
 
752
void
753
finish_static_data_member_decl (tree decl,
754
                                tree init, bool init_const_expr_p,
755
                                tree asmspec_tree,
756
                                int flags)
757
{
758
  DECL_CONTEXT (decl) = current_class_type;
759
 
760
  /* We cannot call pushdecl here, because that would fill in the
761
     TREE_CHAIN of our decl.  Instead, we modify cp_finish_decl to do
762
     the right thing, namely, to put this decl out straight away.  */
763
 
764
  if (! processing_template_decl)
765
    VEC_safe_push (tree, gc, pending_statics, decl);
766
 
767
  if (LOCAL_CLASS_P (current_class_type))
768
    permerror (input_location, "local class %q#T shall not have static data member %q#D",
769
               current_class_type, decl);
770
 
771
  /* Static consts need not be initialized in the class definition.  */
772
  if (init != NULL_TREE && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
773
    {
774
      static int explained = 0;
775
 
776
      error ("initializer invalid for static member with constructor");
777
      if (!explained)
778
        {
779
          error ("(an out of class initialization is required)");
780
          explained = 1;
781
        }
782
      init = NULL_TREE;
783
    }
784
 
785
  DECL_INITIAL (decl) = init;
786
  DECL_IN_AGGR_P (decl) = 1;
787
 
788
  cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
789
}
790
 
791
/* DECLARATOR and DECLSPECS correspond to a class member.  The other
792
   parameters are as for cp_finish_decl.  Return the DECL for the
793
   class member declared.  */
794
 
795
tree
796
grokfield (const cp_declarator *declarator,
797
           cp_decl_specifier_seq *declspecs,
798
           tree init, bool init_const_expr_p,
799
           tree asmspec_tree,
800
           tree attrlist)
801
{
802
  tree value;
803
  const char *asmspec = 0;
804
  int flags = LOOKUP_ONLYCONVERTING;
805
  tree name;
806
 
807
  if (init
808
      && TREE_CODE (init) == TREE_LIST
809
      && TREE_VALUE (init) == error_mark_node
810
      && TREE_CHAIN (init) == NULL_TREE)
811
    init = NULL_TREE;
812
 
813
  value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
814
  if (! value || error_operand_p (value))
815
    /* friend or constructor went bad.  */
816
    return error_mark_node;
817
 
818
  if (TREE_CODE (value) == TYPE_DECL && init)
819
    {
820
      error ("typedef %qD is initialized (use decltype instead)", value);
821
      init = NULL_TREE;
822
    }
823
 
824
  /* Pass friendly classes back.  */
825
  if (value == void_type_node)
826
    return value;
827
 
828
  /* Pass friend decls back.  */
829
  if ((TREE_CODE (value) == FUNCTION_DECL
830
       || TREE_CODE (value) == TEMPLATE_DECL)
831
      && DECL_CONTEXT (value) != current_class_type)
832
    return value;
833
 
834
  name = DECL_NAME (value);
835
 
836
  if (name != NULL_TREE)
837
    {
838
      if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
839
        {
840
          error ("explicit template argument list not allowed");
841
          return error_mark_node;
842
        }
843
 
844
      if (IDENTIFIER_POINTER (name)[0] == '_'
845
          && ! strcmp (IDENTIFIER_POINTER (name), "_vptr"))
846
        error ("member %qD conflicts with virtual function table field name",
847
               value);
848
    }
849
 
850
  /* Stash away type declarations.  */
851
  if (TREE_CODE (value) == TYPE_DECL)
852
    {
853
      DECL_NONLOCAL (value) = 1;
854
      DECL_CONTEXT (value) = current_class_type;
855
 
856
      if (processing_template_decl)
857
        value = push_template_decl (value);
858
 
859
      if (attrlist)
860
        {
861
          int attrflags = 0;
862
 
863
          /* If this is a typedef that names the class for linkage purposes
864
             (7.1.3p8), apply any attributes directly to the type.  */
865
          if (TAGGED_TYPE_P (TREE_TYPE (value))
866
              && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
867
            attrflags = ATTR_FLAG_TYPE_IN_PLACE;
868
 
869
          cplus_decl_attributes (&value, attrlist, attrflags);
870
        }
871
 
872
      if (declspecs->specs[(int)ds_typedef]
873
          && TREE_TYPE (value) != error_mark_node
874
          && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
875
        cp_set_underlying_type (value);
876
 
877
      return value;
878
    }
879
 
880
  if (DECL_IN_AGGR_P (value))
881
    {
882
      error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
883
      return void_type_node;
884
    }
885
 
886
  if (asmspec_tree && asmspec_tree != error_mark_node)
887
    asmspec = TREE_STRING_POINTER (asmspec_tree);
888
 
889
  if (init)
890
    {
891
      if (TREE_CODE (value) == FUNCTION_DECL)
892
        {
893
          /* Initializers for functions are rejected early in the parser.
894
             If we get here, it must be a pure specifier for a method.  */
895
          if (init == ridpointers[(int)RID_DELETE])
896
            {
897
              DECL_DELETED_FN (value) = 1;
898
              DECL_DECLARED_INLINE_P (value) = 1;
899
              DECL_INITIAL (value) = error_mark_node;
900
            }
901
          else if (init == ridpointers[(int)RID_DEFAULT])
902
            {
903
              if (defaultable_fn_check (value))
904
                {
905
                  DECL_DEFAULTED_FN (value) = 1;
906
                  DECL_INITIALIZED_IN_CLASS_P (value) = 1;
907
                  DECL_DECLARED_INLINE_P (value) = 1;
908
                }
909
            }
910
          else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
911
            {
912
              if (integer_zerop (init))
913
                DECL_PURE_VIRTUAL_P (value) = 1;
914
              else if (error_operand_p (init))
915
                ; /* An error has already been reported.  */
916
              else
917
                error ("invalid initializer for member function %qD",
918
                       value);
919
            }
920
          else
921
            {
922
              gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
923
              error ("initializer specified for static member function %qD",
924
                     value);
925
            }
926
        }
927
      else if (pedantic && TREE_CODE (value) != VAR_DECL)
928
        /* Already complained in grokdeclarator.  */
929
        init = NULL_TREE;
930
      else if (!processing_template_decl)
931
        {
932
          if (TREE_CODE (init) == CONSTRUCTOR)
933
            init = digest_init (TREE_TYPE (value), init);
934
          else
935
            init = integral_constant_value (init);
936
 
937
          if (init != error_mark_node && !TREE_CONSTANT (init))
938
            {
939
              /* We can allow references to things that are effectively
940
                 static, since references are initialized with the
941
                 address.  */
942
              if (TREE_CODE (TREE_TYPE (value)) != REFERENCE_TYPE
943
                  || (TREE_STATIC (init) == 0
944
                      && (!DECL_P (init) || DECL_EXTERNAL (init) == 0)))
945
                {
946
                  error ("field initializer is not constant");
947
                  init = error_mark_node;
948
                }
949
            }
950
        }
951
    }
952
 
953
  if (processing_template_decl
954
      && (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == FUNCTION_DECL))
955
    {
956
      value = push_template_decl (value);
957
      if (error_operand_p (value))
958
        return error_mark_node;
959
    }
960
 
961
  if (attrlist)
962
    cplus_decl_attributes (&value, attrlist, 0);
963
 
964
  switch (TREE_CODE (value))
965
    {
966
    case VAR_DECL:
967
      finish_static_data_member_decl (value, init, init_const_expr_p,
968
                                      asmspec_tree, flags);
969
      return value;
970
 
971
    case FIELD_DECL:
972
      if (asmspec)
973
        error ("%<asm%> specifiers are not permitted on non-static data members");
974
      if (DECL_INITIAL (value) == error_mark_node)
975
        init = error_mark_node;
976
      cp_finish_decl (value, init, /*init_const_expr_p=*/false,
977
                      NULL_TREE, flags);
978
      DECL_INITIAL (value) = init;
979
      DECL_IN_AGGR_P (value) = 1;
980
      return value;
981
 
982
    case  FUNCTION_DECL:
983
      if (asmspec)
984
        set_user_assembler_name (value, asmspec);
985
 
986
      cp_finish_decl (value,
987
                      /*init=*/NULL_TREE,
988
                      /*init_const_expr_p=*/false,
989
                      asmspec_tree, flags);
990
 
991
      /* Pass friends back this way.  */
992
      if (DECL_FRIEND_P (value))
993
        return void_type_node;
994
 
995
      DECL_IN_AGGR_P (value) = 1;
996
      return value;
997
 
998
    default:
999
      gcc_unreachable ();
1000
    }
1001
  return NULL_TREE;
1002
}
1003
 
1004
/* Like `grokfield', but for bitfields.
1005
   WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node.  */
1006
 
1007
tree
1008
grokbitfield (const cp_declarator *declarator,
1009
              cp_decl_specifier_seq *declspecs, tree width,
1010
              tree attrlist)
1011
{
1012
  tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, &attrlist);
1013
 
1014
  if (value == error_mark_node)
1015
    return NULL_TREE; /* friends went bad.  */
1016
 
1017
  /* Pass friendly classes back.  */
1018
  if (TREE_CODE (value) == VOID_TYPE)
1019
    return void_type_node;
1020
 
1021
  if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (value))
1022
      && (POINTER_TYPE_P (value)
1023
          || !dependent_type_p (TREE_TYPE (value))))
1024
    {
1025
      error ("bit-field %qD with non-integral type", value);
1026
      return error_mark_node;
1027
    }
1028
 
1029
  if (TREE_CODE (value) == TYPE_DECL)
1030
    {
1031
      error ("cannot declare %qD to be a bit-field type", value);
1032
      return NULL_TREE;
1033
    }
1034
 
1035
  /* Usually, finish_struct_1 catches bitfields with invalid types.
1036
     But, in the case of bitfields with function type, we confuse
1037
     ourselves into thinking they are member functions, so we must
1038
     check here.  */
1039
  if (TREE_CODE (value) == FUNCTION_DECL)
1040
    {
1041
      error ("cannot declare bit-field %qD with function type",
1042
             DECL_NAME (value));
1043
      return NULL_TREE;
1044
    }
1045
 
1046
  if (DECL_IN_AGGR_P (value))
1047
    {
1048
      error ("%qD is already defined in the class %qT", value,
1049
             DECL_CONTEXT (value));
1050
      return void_type_node;
1051
    }
1052
 
1053
  if (TREE_STATIC (value))
1054
    {
1055
      error ("static member %qD cannot be a bit-field", value);
1056
      return NULL_TREE;
1057
    }
1058
  cp_finish_decl (value, NULL_TREE, false, NULL_TREE, 0);
1059
 
1060
  if (width != error_mark_node)
1061
    {
1062
      constant_expression_warning (width);
1063
      DECL_INITIAL (value) = width;
1064
      SET_DECL_C_BIT_FIELD (value);
1065
    }
1066
 
1067
  DECL_IN_AGGR_P (value) = 1;
1068
 
1069
  if (attrlist)
1070
    cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1071
 
1072
  return value;
1073
}
1074
 
1075
 
1076
/* Returns true iff ATTR is an attribute which needs to be applied at
1077
   instantiation time rather than template definition time.  */
1078
 
1079
static bool
1080
is_late_template_attribute (tree attr, tree decl)
1081
{
1082
  tree name = TREE_PURPOSE (attr);
1083
  tree args = TREE_VALUE (attr);
1084
  const struct attribute_spec *spec = lookup_attribute_spec (name);
1085
  tree arg;
1086
 
1087
  if (!spec)
1088
    /* Unknown attribute.  */
1089
    return false;
1090
 
1091
  /* Attribute weak handling wants to write out assembly right away.  */
1092
  if (is_attribute_p ("weak", name))
1093
    return true;
1094
 
1095
  /* If any of the arguments are dependent expressions, we can't evaluate
1096
     the attribute until instantiation time.  */
1097
  for (arg = args; arg; arg = TREE_CHAIN (arg))
1098
    {
1099
      tree t = TREE_VALUE (arg);
1100
 
1101
      /* If the first attribute argument is an identifier, only consider
1102
         second and following arguments.  Attributes like mode, format,
1103
         cleanup and several target specific attributes aren't late
1104
         just because they have an IDENTIFIER_NODE as first argument.  */
1105
      if (arg == args && TREE_CODE (t) == IDENTIFIER_NODE)
1106
        continue;
1107
 
1108
      if (value_dependent_expression_p (t)
1109
          || type_dependent_expression_p (t))
1110
        return true;
1111
    }
1112
 
1113
  if (TREE_CODE (decl) == TYPE_DECL
1114
      || TYPE_P (decl)
1115
      || spec->type_required)
1116
    {
1117
      tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1118
 
1119
      /* We can't apply any attributes to a completely unknown type until
1120
         instantiation time.  */
1121
      enum tree_code code = TREE_CODE (type);
1122
      if (code == TEMPLATE_TYPE_PARM
1123
          || code == BOUND_TEMPLATE_TEMPLATE_PARM
1124
          || code == TYPENAME_TYPE)
1125
        return true;
1126
      /* Also defer most attributes on dependent types.  This is not
1127
         necessary in all cases, but is the better default.  */
1128
      else if (dependent_type_p (type)
1129
               /* But attribute visibility specifically works on
1130
                  templates.  */
1131
               && !is_attribute_p ("visibility", name))
1132
        return true;
1133
      else
1134
        return false;
1135
    }
1136
  else
1137
    return false;
1138
}
1139
 
1140
/* ATTR_P is a list of attributes.  Remove any attributes which need to be
1141
   applied at instantiation time and return them.  If IS_DEPENDENT is true,
1142
   the declaration itself is dependent, so all attributes should be applied
1143
   at instantiation time.  */
1144
 
1145
static tree
1146
splice_template_attributes (tree *attr_p, tree decl)
1147
{
1148
  tree *p = attr_p;
1149
  tree late_attrs = NULL_TREE;
1150
  tree *q = &late_attrs;
1151
 
1152
  if (!p)
1153
    return NULL_TREE;
1154
 
1155
  for (; *p; )
1156
    {
1157
      if (is_late_template_attribute (*p, decl))
1158
        {
1159
          ATTR_IS_DEPENDENT (*p) = 1;
1160
          *q = *p;
1161
          *p = TREE_CHAIN (*p);
1162
          q = &TREE_CHAIN (*q);
1163
          *q = NULL_TREE;
1164
        }
1165
      else
1166
        p = &TREE_CHAIN (*p);
1167
    }
1168
 
1169
  return late_attrs;
1170
}
1171
 
1172
/* Remove any late attributes from the list in ATTR_P and attach them to
1173
   DECL_P.  */
1174
 
1175
static void
1176
save_template_attributes (tree *attr_p, tree *decl_p)
1177
{
1178
  tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1179
  tree *q;
1180
  tree old_attrs = NULL_TREE;
1181
 
1182
  if (!late_attrs)
1183
    return;
1184
 
1185
  if (DECL_P (*decl_p))
1186
    q = &DECL_ATTRIBUTES (*decl_p);
1187
  else
1188
    q = &TYPE_ATTRIBUTES (*decl_p);
1189
 
1190
  old_attrs = *q;
1191
 
1192
  /* Place the late attributes at the beginning of the attribute
1193
     list.  */
1194
  TREE_CHAIN (tree_last (late_attrs)) = *q;
1195
  *q = late_attrs;
1196
 
1197
  if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1198
    {
1199
      /* We've added new attributes directly to the main variant, so
1200
         now we need to update all of the other variants to include
1201
         these new attributes.  */
1202
      tree variant;
1203
      for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1204
           variant = TYPE_NEXT_VARIANT (variant))
1205
        {
1206
          gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1207
          TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1208
        }
1209
    }
1210
}
1211
 
1212
/* Like reconstruct_complex_type, but handle also template trees.  */
1213
 
1214
tree
1215
cp_reconstruct_complex_type (tree type, tree bottom)
1216
{
1217
  tree inner, outer;
1218
 
1219
  if (TREE_CODE (type) == POINTER_TYPE)
1220
    {
1221
      inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1222
      outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1223
                                           TYPE_REF_CAN_ALIAS_ALL (type));
1224
    }
1225
  else if (TREE_CODE (type) == REFERENCE_TYPE)
1226
    {
1227
      inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1228
      outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1229
                                             TYPE_REF_CAN_ALIAS_ALL (type));
1230
    }
1231
  else if (TREE_CODE (type) == ARRAY_TYPE)
1232
    {
1233
      inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1234
      outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1235
      /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1236
         element type qualification will be handled by the recursive
1237
         cp_reconstruct_complex_type call and cp_build_qualified_type
1238
         for ARRAY_TYPEs changes the element type.  */
1239
      return outer;
1240
    }
1241
  else if (TREE_CODE (type) == FUNCTION_TYPE)
1242
    {
1243
      inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1244
      outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1245
    }
1246
  else if (TREE_CODE (type) == METHOD_TYPE)
1247
    {
1248
      inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1249
      /* The build_method_type_directly() routine prepends 'this' to argument list,
1250
         so we must compensate by getting rid of it.  */
1251
      outer
1252
        = build_method_type_directly
1253
            (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
1254
             inner,
1255
             TREE_CHAIN (TYPE_ARG_TYPES (type)));
1256
    }
1257
  else if (TREE_CODE (type) == OFFSET_TYPE)
1258
    {
1259
      inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1260
      outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1261
    }
1262
  else
1263
    return bottom;
1264
 
1265
  if (TYPE_ATTRIBUTES (type))
1266
    outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1267
  return cp_build_qualified_type (outer, TYPE_QUALS (type));
1268
}
1269
 
1270
/* Like decl_attributes, but handle C++ complexity.  */
1271
 
1272
void
1273
cplus_decl_attributes (tree *decl, tree attributes, int flags)
1274
{
1275
  if (*decl == NULL_TREE || *decl == void_type_node
1276
      || *decl == error_mark_node
1277
      || attributes == NULL_TREE)
1278
    return;
1279
 
1280
  if (processing_template_decl)
1281
    {
1282
      if (check_for_bare_parameter_packs (attributes))
1283
        return;
1284
 
1285
      save_template_attributes (&attributes, decl);
1286
      if (attributes == NULL_TREE)
1287
        return;
1288
    }
1289
 
1290
  if (TREE_CODE (*decl) == TEMPLATE_DECL)
1291
    decl = &DECL_TEMPLATE_RESULT (*decl);
1292
 
1293
  decl_attributes (decl, attributes, flags);
1294
 
1295
  if (TREE_CODE (*decl) == TYPE_DECL)
1296
    SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1297
}
1298
 
1299
/* Walks through the namespace- or function-scope anonymous union
1300
   OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1301
   Returns one of the fields for use in the mangled name.  */
1302
 
1303
static tree
1304
build_anon_union_vars (tree type, tree object)
1305
{
1306
  tree main_decl = NULL_TREE;
1307
  tree field;
1308
 
1309
  /* Rather than write the code to handle the non-union case,
1310
     just give an error.  */
1311
  if (TREE_CODE (type) != UNION_TYPE)
1312
    error ("anonymous struct not inside named type");
1313
 
1314
  for (field = TYPE_FIELDS (type);
1315
       field != NULL_TREE;
1316
       field = TREE_CHAIN (field))
1317
    {
1318
      tree decl;
1319
      tree ref;
1320
 
1321
      if (DECL_ARTIFICIAL (field))
1322
        continue;
1323
      if (TREE_CODE (field) != FIELD_DECL)
1324
        {
1325
          permerror (input_location, "%q+#D invalid; an anonymous union can only "
1326
                     "have non-static data members", field);
1327
          continue;
1328
        }
1329
 
1330
      if (TREE_PRIVATE (field))
1331
        permerror (input_location, "private member %q+#D in anonymous union", field);
1332
      else if (TREE_PROTECTED (field))
1333
        permerror (input_location, "protected member %q+#D in anonymous union", field);
1334
 
1335
      if (processing_template_decl)
1336
        ref = build_min_nt (COMPONENT_REF, object,
1337
                            DECL_NAME (field), NULL_TREE);
1338
      else
1339
        ref = build_class_member_access_expr (object, field, NULL_TREE,
1340
                                              false, tf_warning_or_error);
1341
 
1342
      if (DECL_NAME (field))
1343
        {
1344
          tree base;
1345
 
1346
          decl = build_decl (input_location,
1347
                             VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1348
          DECL_ANON_UNION_VAR_P (decl) = 1;
1349
          DECL_ARTIFICIAL (decl) = 1;
1350
 
1351
          base = get_base_address (object);
1352
          TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1353
          TREE_STATIC (decl) = TREE_STATIC (base);
1354
          DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1355
 
1356
          SET_DECL_VALUE_EXPR (decl, ref);
1357
          DECL_HAS_VALUE_EXPR_P (decl) = 1;
1358
 
1359
          decl = pushdecl (decl);
1360
        }
1361
      else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1362
        decl = build_anon_union_vars (TREE_TYPE (field), ref);
1363
      else
1364
        decl = 0;
1365
 
1366
      if (main_decl == NULL_TREE)
1367
        main_decl = decl;
1368
    }
1369
 
1370
  return main_decl;
1371
}
1372
 
1373
/* Finish off the processing of a UNION_TYPE structure.  If the union is an
1374
   anonymous union, then all members must be laid out together.  PUBLIC_P
1375
   is nonzero if this union is not declared static.  */
1376
 
1377
void
1378
finish_anon_union (tree anon_union_decl)
1379
{
1380
  tree type;
1381
  tree main_decl;
1382
  bool public_p;
1383
 
1384
  if (anon_union_decl == error_mark_node)
1385
    return;
1386
 
1387
  type = TREE_TYPE (anon_union_decl);
1388
  public_p = TREE_PUBLIC (anon_union_decl);
1389
 
1390
  /* The VAR_DECL's context is the same as the TYPE's context.  */
1391
  DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1392
 
1393
  if (TYPE_FIELDS (type) == NULL_TREE)
1394
    return;
1395
 
1396
  if (public_p)
1397
    {
1398
      error ("namespace-scope anonymous aggregates must be static");
1399
      return;
1400
    }
1401
 
1402
  main_decl = build_anon_union_vars (type, anon_union_decl);
1403
  if (main_decl == error_mark_node)
1404
    return;
1405
  if (main_decl == NULL_TREE)
1406
    {
1407
      warning (0, "anonymous union with no members");
1408
      return;
1409
    }
1410
 
1411
  if (!processing_template_decl)
1412
    {
1413
      /* Use main_decl to set the mangled name.  */
1414
      DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1415
      maybe_commonize_var (anon_union_decl);
1416
      mangle_decl (anon_union_decl);
1417
      DECL_NAME (anon_union_decl) = NULL_TREE;
1418
    }
1419
 
1420
  pushdecl (anon_union_decl);
1421
  if (building_stmt_tree ()
1422
      && at_function_scope_p ())
1423
    add_decl_expr (anon_union_decl);
1424
  else if (!processing_template_decl)
1425
    rest_of_decl_compilation (anon_union_decl,
1426
                              toplevel_bindings_p (), at_eof);
1427
}
1428
 
1429
/* Auxiliary functions to make type signatures for
1430
   `operator new' and `operator delete' correspond to
1431
   what compiler will be expecting.  */
1432
 
1433
tree
1434
coerce_new_type (tree type)
1435
{
1436
  int e = 0;
1437
  tree args = TYPE_ARG_TYPES (type);
1438
 
1439
  gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1440
 
1441
  if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1442
    {
1443
      e = 1;
1444
      error ("%<operator new%> must return type %qT", ptr_type_node);
1445
    }
1446
 
1447
  if (args && args != void_list_node)
1448
    {
1449
      if (TREE_PURPOSE (args))
1450
        {
1451
          /* [basic.stc.dynamic.allocation]
1452
 
1453
             The first parameter shall not have an associated default
1454
             argument.  */
1455
          error ("the first parameter of %<operator new%> cannot "
1456
                 "have a default argument");
1457
          /* Throw away the default argument.  */
1458
          TREE_PURPOSE (args) = NULL_TREE;
1459
        }
1460
 
1461
      if (!same_type_p (TREE_VALUE (args), size_type_node))
1462
        {
1463
          e = 2;
1464
          args = TREE_CHAIN (args);
1465
        }
1466
    }
1467
  else
1468
    e = 2;
1469
 
1470
  if (e == 2)
1471
    permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1472
               "as first parameter", size_type_node);
1473
 
1474
  switch (e)
1475
  {
1476
    case 2:
1477
      args = tree_cons (NULL_TREE, size_type_node, args);
1478
      /* Fall through.  */
1479
    case 1:
1480
      type = build_exception_variant
1481
              (build_function_type (ptr_type_node, args),
1482
               TYPE_RAISES_EXCEPTIONS (type));
1483
      /* Fall through.  */
1484
    default:;
1485
  }
1486
  return type;
1487
}
1488
 
1489
tree
1490
coerce_delete_type (tree type)
1491
{
1492
  int e = 0;
1493
  tree args = TYPE_ARG_TYPES (type);
1494
 
1495
  gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1496
 
1497
  if (!same_type_p (TREE_TYPE (type), void_type_node))
1498
    {
1499
      e = 1;
1500
      error ("%<operator delete%> must return type %qT", void_type_node);
1501
    }
1502
 
1503
  if (!args || args == void_list_node
1504
      || !same_type_p (TREE_VALUE (args), ptr_type_node))
1505
    {
1506
      e = 2;
1507
      if (args && args != void_list_node)
1508
        args = TREE_CHAIN (args);
1509
      error ("%<operator delete%> takes type %qT as first parameter",
1510
             ptr_type_node);
1511
    }
1512
  switch (e)
1513
  {
1514
    case 2:
1515
      args = tree_cons (NULL_TREE, ptr_type_node, args);
1516
      /* Fall through.  */
1517
    case 1:
1518
      type = build_exception_variant
1519
              (build_function_type (void_type_node, args),
1520
               TYPE_RAISES_EXCEPTIONS (type));
1521
      /* Fall through.  */
1522
    default:;
1523
  }
1524
 
1525
  return type;
1526
}
1527
 
1528
/* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1529
   and mark them as needed.  */
1530
 
1531
static void
1532
mark_vtable_entries (tree decl)
1533
{
1534
  tree fnaddr;
1535
  unsigned HOST_WIDE_INT idx;
1536
 
1537
  FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1538
                              idx, fnaddr)
1539
    {
1540
      tree fn;
1541
 
1542
      STRIP_NOPS (fnaddr);
1543
 
1544
      if (TREE_CODE (fnaddr) != ADDR_EXPR
1545
          && TREE_CODE (fnaddr) != FDESC_EXPR)
1546
        /* This entry is an offset: a virtual base class offset, a
1547
           virtual call offset, an RTTI offset, etc.  */
1548
        continue;
1549
 
1550
      fn = TREE_OPERAND (fnaddr, 0);
1551
      TREE_ADDRESSABLE (fn) = 1;
1552
      /* When we don't have vcall offsets, we output thunks whenever
1553
         we output the vtables that contain them.  With vcall offsets,
1554
         we know all the thunks we'll need when we emit a virtual
1555
         function, so we emit the thunks there instead.  */
1556
      if (DECL_THUNK_P (fn))
1557
        use_thunk (fn, /*emit_p=*/0);
1558
      mark_used (fn);
1559
    }
1560
}
1561
 
1562
/* Set DECL up to have the closest approximation of "initialized common"
1563
   linkage available.  */
1564
 
1565
void
1566
comdat_linkage (tree decl)
1567
{
1568
  if (flag_weak)
1569
    make_decl_one_only (decl, cxx_comdat_group (decl));
1570
  else if (TREE_CODE (decl) == FUNCTION_DECL
1571
           || (TREE_CODE (decl) == VAR_DECL && DECL_ARTIFICIAL (decl)))
1572
    /* We can just emit function and compiler-generated variables
1573
       statically; having multiple copies is (for the most part) only
1574
       a waste of space.
1575
 
1576
       There are two correctness issues, however: the address of a
1577
       template instantiation with external linkage should be the
1578
       same, independent of what translation unit asks for the
1579
       address, and this will not hold when we emit multiple copies of
1580
       the function.  However, there's little else we can do.
1581
 
1582
       Also, by default, the typeinfo implementation assumes that
1583
       there will be only one copy of the string used as the name for
1584
       each type.  Therefore, if weak symbols are unavailable, the
1585
       run-time library should perform a more conservative check; it
1586
       should perform a string comparison, rather than an address
1587
       comparison.  */
1588
    TREE_PUBLIC (decl) = 0;
1589
  else
1590
    {
1591
      /* Static data member template instantiations, however, cannot
1592
         have multiple copies.  */
1593
      if (DECL_INITIAL (decl) == 0
1594
          || DECL_INITIAL (decl) == error_mark_node)
1595
        DECL_COMMON (decl) = 1;
1596
      else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1597
        {
1598
          DECL_COMMON (decl) = 1;
1599
          DECL_INITIAL (decl) = error_mark_node;
1600
        }
1601
      else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1602
        {
1603
          /* We can't do anything useful; leave vars for explicit
1604
             instantiation.  */
1605
          DECL_EXTERNAL (decl) = 1;
1606
          DECL_NOT_REALLY_EXTERN (decl) = 0;
1607
        }
1608
    }
1609
 
1610
  DECL_COMDAT (decl) = 1;
1611
}
1612
 
1613
/* For win32 we also want to put explicit instantiations in
1614
   linkonce sections, so that they will be merged with implicit
1615
   instantiations; otherwise we get duplicate symbol errors.
1616
   For Darwin we do not want explicit instantiations to be
1617
   linkonce.  */
1618
 
1619
void
1620
maybe_make_one_only (tree decl)
1621
{
1622
  /* We used to say that this was not necessary on targets that support weak
1623
     symbols, because the implicit instantiations will defer to the explicit
1624
     one.  However, that's not actually the case in SVR4; a strong definition
1625
     after a weak one is an error.  Also, not making explicit
1626
     instantiations one_only means that we can end up with two copies of
1627
     some template instantiations.  */
1628
  if (! flag_weak)
1629
    return;
1630
 
1631
  /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1632
     we can get away with not emitting them if they aren't used.  We need
1633
     to for variables so that cp_finish_decl will update their linkage,
1634
     because their DECL_INITIAL may not have been set properly yet.  */
1635
 
1636
  if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1637
      || (! DECL_EXPLICIT_INSTANTIATION (decl)
1638
          && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1639
    {
1640
      make_decl_one_only (decl, cxx_comdat_group (decl));
1641
 
1642
      if (TREE_CODE (decl) == VAR_DECL)
1643
        {
1644
          DECL_COMDAT (decl) = 1;
1645
          /* Mark it needed so we don't forget to emit it.  */
1646
          mark_decl_referenced (decl);
1647
        }
1648
    }
1649
}
1650
 
1651
/* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1652
   This predicate will give the right answer during parsing of the
1653
   function, which other tests may not.  */
1654
 
1655
bool
1656
vague_linkage_p (tree decl)
1657
{
1658
  /* Unfortunately, import_export_decl has not always been called
1659
     before the function is processed, so we cannot simply check
1660
     DECL_COMDAT.  */
1661
  return (DECL_COMDAT (decl)
1662
          || (((TREE_CODE (decl) == FUNCTION_DECL
1663
                && DECL_DECLARED_INLINE_P (decl))
1664
               || (DECL_LANG_SPECIFIC (decl)
1665
                   && DECL_TEMPLATE_INSTANTIATION (decl)))
1666
              && TREE_PUBLIC (decl)));
1667
}
1668
 
1669
/* Determine whether or not we want to specifically import or export CTYPE,
1670
   using various heuristics.  */
1671
 
1672
static void
1673
import_export_class (tree ctype)
1674
{
1675
  /* -1 for imported, 1 for exported.  */
1676
  int import_export = 0;
1677
 
1678
  /* It only makes sense to call this function at EOF.  The reason is
1679
     that this function looks at whether or not the first non-inline
1680
     non-abstract virtual member function has been defined in this
1681
     translation unit.  But, we can't possibly know that until we've
1682
     seen the entire translation unit.  */
1683
  gcc_assert (at_eof);
1684
 
1685
  if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1686
    return;
1687
 
1688
  /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1689
     we will have CLASSTYPE_INTERFACE_ONLY set but not
1690
     CLASSTYPE_INTERFACE_KNOWN.  In that case, we don't want to use this
1691
     heuristic because someone will supply a #pragma implementation
1692
     elsewhere, and deducing it here would produce a conflict.  */
1693
  if (CLASSTYPE_INTERFACE_ONLY (ctype))
1694
    return;
1695
 
1696
  if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1697
    import_export = -1;
1698
  else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1699
    import_export = 1;
1700
  else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1701
           && !flag_implicit_templates)
1702
    /* For a template class, without -fimplicit-templates, check the
1703
       repository.  If the virtual table is assigned to this
1704
       translation unit, then export the class; otherwise, import
1705
       it.  */
1706
      import_export = repo_export_class_p (ctype) ? 1 : -1;
1707
  else if (TYPE_POLYMORPHIC_P (ctype))
1708
    {
1709
      /* The ABI specifies that the virtual table and associated
1710
         information are emitted with the key method, if any.  */
1711
      tree method = CLASSTYPE_KEY_METHOD (ctype);
1712
      /* If weak symbol support is not available, then we must be
1713
         careful not to emit the vtable when the key function is
1714
         inline.  An inline function can be defined in multiple
1715
         translation units.  If we were to emit the vtable in each
1716
         translation unit containing a definition, we would get
1717
         multiple definition errors at link-time.  */
1718
      if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1719
        import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1720
    }
1721
 
1722
  /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1723
     a definition anywhere else.  */
1724
  if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1725
    import_export = 0;
1726
 
1727
  /* Allow back ends the chance to overrule the decision.  */
1728
  if (targetm.cxx.import_export_class)
1729
    import_export = targetm.cxx.import_export_class (ctype, import_export);
1730
 
1731
  if (import_export)
1732
    {
1733
      SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1734
      CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1735
    }
1736
}
1737
 
1738
/* Return true if VAR has already been provided to the back end; in that
1739
   case VAR should not be modified further by the front end.  */
1740
static bool
1741
var_finalized_p (tree var)
1742
{
1743
  return varpool_node (var)->finalized;
1744
}
1745
 
1746
/* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1747
   must be emitted in this translation unit.  Mark it as such.  */
1748
 
1749
void
1750
mark_needed (tree decl)
1751
{
1752
  /* It's possible that we no longer need to set
1753
     TREE_SYMBOL_REFERENCED here directly, but doing so is
1754
     harmless.  */
1755
  TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)) = 1;
1756
  mark_decl_referenced (decl);
1757
}
1758
 
1759
/* DECL is either a FUNCTION_DECL or a VAR_DECL.  This function
1760
   returns true if a definition of this entity should be provided in
1761
   this object file.  Callers use this function to determine whether
1762
   or not to let the back end know that a definition of DECL is
1763
   available in this translation unit.  */
1764
 
1765
bool
1766
decl_needed_p (tree decl)
1767
{
1768
  gcc_assert (TREE_CODE (decl) == VAR_DECL
1769
              || TREE_CODE (decl) == FUNCTION_DECL);
1770
  /* This function should only be called at the end of the translation
1771
     unit.  We cannot be sure of whether or not something will be
1772
     COMDAT until that point.  */
1773
  gcc_assert (at_eof);
1774
 
1775
  /* All entities with external linkage that are not COMDAT should be
1776
     emitted; they may be referred to from other object files.  */
1777
  if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
1778
    return true;
1779
  /* If this entity was used, let the back end see it; it will decide
1780
     whether or not to emit it into the object file.  */
1781
  if (TREE_USED (decl)
1782
      || (DECL_ASSEMBLER_NAME_SET_P (decl)
1783
          && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))))
1784
      return true;
1785
  /* Functions marked "dllexport" must be emitted so that they are
1786
     visible to other DLLs.  */
1787
  if (lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
1788
    return true;
1789
  /* Otherwise, DECL does not need to be emitted -- yet.  A subsequent
1790
     reference to DECL might cause it to be emitted later.  */
1791
  return false;
1792
}
1793
 
1794
/* If necessary, write out the vtables for the dynamic class CTYPE.
1795
   Returns true if any vtables were emitted.  */
1796
 
1797
static bool
1798
maybe_emit_vtables (tree ctype)
1799
{
1800
  tree vtbl;
1801
  tree primary_vtbl;
1802
  int needed = 0;
1803
 
1804
  /* If the vtables for this class have already been emitted there is
1805
     nothing more to do.  */
1806
  primary_vtbl = CLASSTYPE_VTABLES (ctype);
1807
  if (var_finalized_p (primary_vtbl))
1808
    return false;
1809
  /* Ignore dummy vtables made by get_vtable_decl.  */
1810
  if (TREE_TYPE (primary_vtbl) == void_type_node)
1811
    return false;
1812
 
1813
  /* On some targets, we cannot determine the key method until the end
1814
     of the translation unit -- which is when this function is
1815
     called.  */
1816
  if (!targetm.cxx.key_method_may_be_inline ())
1817
    determine_key_method (ctype);
1818
 
1819
  /* See if any of the vtables are needed.  */
1820
  for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = TREE_CHAIN (vtbl))
1821
    {
1822
      import_export_decl (vtbl);
1823
      if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
1824
        needed = 1;
1825
    }
1826
  if (!needed)
1827
    {
1828
      /* If the references to this class' vtables are optimized away,
1829
         still emit the appropriate debugging information.  See
1830
         dfs_debug_mark.  */
1831
      if (DECL_COMDAT (primary_vtbl)
1832
          && CLASSTYPE_DEBUG_REQUESTED (ctype))
1833
        note_debug_info_needed (ctype);
1834
      return false;
1835
    }
1836
 
1837
  /* The ABI requires that we emit all of the vtables if we emit any
1838
     of them.  */
1839
  for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = TREE_CHAIN (vtbl))
1840
    {
1841
      /* Mark entities references from the virtual table as used.  */
1842
      mark_vtable_entries (vtbl);
1843
 
1844
      if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
1845
        {
1846
          tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), LOOKUP_NORMAL);
1847
 
1848
          /* It had better be all done at compile-time.  */
1849
          gcc_assert (!expr);
1850
        }
1851
 
1852
      /* Write it out.  */
1853
      DECL_EXTERNAL (vtbl) = 0;
1854
      rest_of_decl_compilation (vtbl, 1, 1);
1855
 
1856
      /* Because we're only doing syntax-checking, we'll never end up
1857
         actually marking the variable as written.  */
1858
      if (flag_syntax_only)
1859
        TREE_ASM_WRITTEN (vtbl) = 1;
1860
    }
1861
 
1862
  /* Since we're writing out the vtable here, also write the debug
1863
     info.  */
1864
  note_debug_info_needed (ctype);
1865
 
1866
  return true;
1867
}
1868
 
1869
/* A special return value from type_visibility meaning internal
1870
   linkage.  */
1871
 
1872
enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
1873
 
1874
/* walk_tree helper function for type_visibility.  */
1875
 
1876
static tree
1877
min_vis_r (tree *tp, int *walk_subtrees, void *data)
1878
{
1879
  int *vis_p = (int *)data;
1880
  if (! TYPE_P (*tp))
1881
    {
1882
      *walk_subtrees = 0;
1883
    }
1884
  else if (CLASS_TYPE_P (*tp))
1885
    {
1886
      if (!TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
1887
        {
1888
          *vis_p = VISIBILITY_ANON;
1889
          return *tp;
1890
        }
1891
      else if (CLASSTYPE_VISIBILITY (*tp) > *vis_p)
1892
        *vis_p = CLASSTYPE_VISIBILITY (*tp);
1893
    }
1894
  return NULL;
1895
}
1896
 
1897
/* Returns the visibility of TYPE, which is the minimum visibility of its
1898
   component types.  */
1899
 
1900
static int
1901
type_visibility (tree type)
1902
{
1903
  int vis = VISIBILITY_DEFAULT;
1904
  cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
1905
  return vis;
1906
}
1907
 
1908
/* Limit the visibility of DECL to VISIBILITY, if not explicitly
1909
   specified (or if VISIBILITY is static).  */
1910
 
1911
static bool
1912
constrain_visibility (tree decl, int visibility)
1913
{
1914
  if (visibility == VISIBILITY_ANON)
1915
    {
1916
      /* extern "C" declarations aren't affected by the anonymous
1917
         namespace.  */
1918
      if (!DECL_EXTERN_C_P (decl))
1919
        {
1920
          TREE_PUBLIC (decl) = 0;
1921
          DECL_WEAK (decl) = 0;
1922
          DECL_COMMON (decl) = 0;
1923
          DECL_COMDAT_GROUP (decl) = NULL_TREE;
1924
          DECL_INTERFACE_KNOWN (decl) = 1;
1925
          if (DECL_LANG_SPECIFIC (decl))
1926
            DECL_NOT_REALLY_EXTERN (decl) = 1;
1927
        }
1928
    }
1929
  else if (visibility > DECL_VISIBILITY (decl)
1930
           && !DECL_VISIBILITY_SPECIFIED (decl))
1931
    {
1932
      DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
1933
      return true;
1934
    }
1935
  return false;
1936
}
1937
 
1938
/* Constrain the visibility of DECL based on the visibility of its template
1939
   arguments.  */
1940
 
1941
static void
1942
constrain_visibility_for_template (tree decl, tree targs)
1943
{
1944
  /* If this is a template instantiation, check the innermost
1945
     template args for visibility constraints.  The outer template
1946
     args are covered by the class check.  */
1947
  tree args = INNERMOST_TEMPLATE_ARGS (targs);
1948
  int i;
1949
  for (i = TREE_VEC_LENGTH (args); i > 0; --i)
1950
    {
1951
      int vis = 0;
1952
 
1953
      tree arg = TREE_VEC_ELT (args, i-1);
1954
      if (TYPE_P (arg))
1955
        vis = type_visibility (arg);
1956
      else if (TREE_TYPE (arg) && POINTER_TYPE_P (TREE_TYPE (arg)))
1957
        {
1958
          STRIP_NOPS (arg);
1959
          if (TREE_CODE (arg) == ADDR_EXPR)
1960
            arg = TREE_OPERAND (arg, 0);
1961
          if (TREE_CODE (arg) == VAR_DECL
1962
              || TREE_CODE (arg) == FUNCTION_DECL)
1963
            {
1964
              if (! TREE_PUBLIC (arg))
1965
                vis = VISIBILITY_ANON;
1966
              else
1967
                vis = DECL_VISIBILITY (arg);
1968
            }
1969
        }
1970
      if (vis)
1971
        constrain_visibility (decl, vis);
1972
    }
1973
}
1974
 
1975
/* Like c_determine_visibility, but with additional C++-specific
1976
   behavior.
1977
 
1978
   Function-scope entities can rely on the function's visibility because
1979
   it is set in start_preparsed_function.
1980
 
1981
   Class-scope entities cannot rely on the class's visibility until the end
1982
   of the enclosing class definition.
1983
 
1984
   Note that because namespaces have multiple independent definitions,
1985
   namespace visibility is handled elsewhere using the #pragma visibility
1986
   machinery rather than by decorating the namespace declaration.
1987
 
1988
   The goal is for constraints from the type to give a diagnostic, and
1989
   other constraints to be applied silently.  */
1990
 
1991
void
1992
determine_visibility (tree decl)
1993
{
1994
  tree class_type = NULL_TREE;
1995
  bool use_template;
1996
  bool orig_visibility_specified;
1997
  enum symbol_visibility orig_visibility;
1998
 
1999
  /* Remember that all decls get VISIBILITY_DEFAULT when built.  */
2000
 
2001
  /* Only relevant for names with external linkage.  */
2002
  if (!TREE_PUBLIC (decl))
2003
    return;
2004
 
2005
  /* Cloned constructors and destructors get the same visibility as
2006
     the underlying function.  That should be set up in
2007
     maybe_clone_body.  */
2008
  gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2009
 
2010
  orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2011
  orig_visibility = DECL_VISIBILITY (decl);
2012
 
2013
  if (TREE_CODE (decl) == TYPE_DECL)
2014
    {
2015
      if (CLASS_TYPE_P (TREE_TYPE (decl)))
2016
        use_template = CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl));
2017
      else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2018
        use_template = 1;
2019
      else
2020
        use_template = 0;
2021
    }
2022
  else if (DECL_LANG_SPECIFIC (decl))
2023
    use_template = DECL_USE_TEMPLATE (decl);
2024
  else
2025
    use_template = 0;
2026
 
2027
  /* If DECL is a member of a class, visibility specifiers on the
2028
     class can influence the visibility of the DECL.  */
2029
  if (DECL_CLASS_SCOPE_P (decl))
2030
    class_type = DECL_CONTEXT (decl);
2031
  else
2032
    {
2033
      /* Not a class member.  */
2034
 
2035
      /* Virtual tables have DECL_CONTEXT set to their associated class,
2036
         so they are automatically handled above.  */
2037
      gcc_assert (TREE_CODE (decl) != VAR_DECL
2038
                  || !DECL_VTABLE_OR_VTT_P (decl));
2039
 
2040
      if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2041
        {
2042
          /* Local statics and classes get the visibility of their
2043
             containing function by default, except that
2044
             -fvisibility-inlines-hidden doesn't affect them.  */
2045
          tree fn = DECL_CONTEXT (decl);
2046
          if (DECL_VISIBILITY_SPECIFIED (fn) || ! DECL_CLASS_SCOPE_P (fn))
2047
            {
2048
              DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2049
              DECL_VISIBILITY_SPECIFIED (decl) =
2050
                DECL_VISIBILITY_SPECIFIED (fn);
2051
            }
2052
          else
2053
            determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2054
 
2055
          /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2056
             but have no TEMPLATE_INFO, so don't try to check it.  */
2057
          use_template = 0;
2058
        }
2059
      else if (TREE_CODE (decl) == VAR_DECL && DECL_TINFO_P (decl)
2060
               && flag_visibility_ms_compat)
2061
        {
2062
          /* Under -fvisibility-ms-compat, types are visible by default,
2063
             even though their contents aren't.  */
2064
          tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2065
          int underlying_vis = type_visibility (underlying_type);
2066
          if (underlying_vis == VISIBILITY_ANON
2067
              || CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type))
2068
            constrain_visibility (decl, underlying_vis);
2069
          else
2070
            DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2071
        }
2072
      else if (TREE_CODE (decl) == VAR_DECL && DECL_TINFO_P (decl))
2073
        {
2074
          /* tinfo visibility is based on the type it's for.  */
2075
          constrain_visibility
2076
            (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))));
2077
 
2078
          /* Give the target a chance to override the visibility associated
2079
             with DECL.  */
2080
          if (TREE_PUBLIC (decl)
2081
              && !DECL_REALLY_EXTERN (decl)
2082
              && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2083
              && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2084
            targetm.cxx.determine_class_data_visibility (decl);
2085
        }
2086
      else if (use_template)
2087
        /* Template instantiations and specializations get visibility based
2088
           on their template unless they override it with an attribute.  */;
2089
      else if (! DECL_VISIBILITY_SPECIFIED (decl))
2090
        {
2091
          /* Set default visibility to whatever the user supplied with
2092
             #pragma GCC visibility or a namespace visibility attribute.  */
2093
          DECL_VISIBILITY (decl) = default_visibility;
2094
          DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2095
        }
2096
    }
2097
 
2098
  if (use_template)
2099
    {
2100
      /* If the specialization doesn't specify visibility, use the
2101
         visibility from the template.  */
2102
      tree tinfo = (TREE_CODE (decl) == TYPE_DECL
2103
                    ? TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2104
                    : DECL_TEMPLATE_INFO (decl));
2105
      tree args = TI_ARGS (tinfo);
2106
 
2107
      if (args != error_mark_node)
2108
        {
2109
          int depth = TMPL_ARGS_DEPTH (args);
2110
          tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2111
 
2112
          if (!DECL_VISIBILITY_SPECIFIED (decl))
2113
            {
2114
              DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2115
              DECL_VISIBILITY_SPECIFIED (decl)
2116
                = DECL_VISIBILITY_SPECIFIED (pattern);
2117
            }
2118
 
2119
          /* FIXME should TMPL_ARGS_DEPTH really return 1 for null input? */
2120
          if (args && depth > template_class_depth (class_type))
2121
            /* Limit visibility based on its template arguments.  */
2122
            constrain_visibility_for_template (decl, args);
2123
        }
2124
    }
2125
 
2126
  if (class_type)
2127
    determine_visibility_from_class (decl, class_type);
2128
 
2129
  if (decl_anon_ns_mem_p (decl))
2130
    /* Names in an anonymous namespace get internal linkage.
2131
       This might change once we implement export.  */
2132
    constrain_visibility (decl, VISIBILITY_ANON);
2133
  else if (TREE_CODE (decl) != TYPE_DECL)
2134
    {
2135
      /* Propagate anonymity from type to decl.  */
2136
      int tvis = type_visibility (TREE_TYPE (decl));
2137
      if (tvis == VISIBILITY_ANON
2138
          || ! DECL_VISIBILITY_SPECIFIED (decl))
2139
        constrain_visibility (decl, tvis);
2140
    }
2141
  else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2142
    /* DR 757: A type without linkage shall not be used as the type of a
2143
       variable or function with linkage, unless
2144
       o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2145
       o the variable or function is not used (3.2 [basic.def.odr]) or is
2146
       defined in the same translation unit.
2147
 
2148
       Since non-extern "C" decls need to be defined in the same
2149
       translation unit, we can make the type internal.  */
2150
    constrain_visibility (decl, VISIBILITY_ANON);
2151
 
2152
  /* If visibility changed and DECL already has DECL_RTL, ensure
2153
     symbol flags are updated.  */
2154
  if ((DECL_VISIBILITY (decl) != orig_visibility
2155
       || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2156
      && ((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
2157
          || TREE_CODE (decl) == FUNCTION_DECL)
2158
      && DECL_RTL_SET_P (decl))
2159
    make_decl_rtl (decl);
2160
}
2161
 
2162
/* By default, static data members and function members receive
2163
   the visibility of their containing class.  */
2164
 
2165
static void
2166
determine_visibility_from_class (tree decl, tree class_type)
2167
{
2168
  if (DECL_VISIBILITY_SPECIFIED (decl))
2169
    return;
2170
 
2171
  if (visibility_options.inlines_hidden
2172
      /* Don't do this for inline templates; specializations might not be
2173
         inline, and we don't want them to inherit the hidden
2174
         visibility.  We'll set it here for all inline instantiations.  */
2175
      && !processing_template_decl
2176
      && TREE_CODE (decl) == FUNCTION_DECL
2177
      && DECL_DECLARED_INLINE_P (decl)
2178
      && (! DECL_LANG_SPECIFIC (decl)
2179
          || ! DECL_EXPLICIT_INSTANTIATION (decl)))
2180
    DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2181
  else
2182
    {
2183
      /* Default to the class visibility.  */
2184
      DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2185
      DECL_VISIBILITY_SPECIFIED (decl)
2186
        = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2187
    }
2188
 
2189
  /* Give the target a chance to override the visibility associated
2190
     with DECL.  */
2191
  if (TREE_CODE (decl) == VAR_DECL
2192
      && (DECL_TINFO_P (decl)
2193
          || (DECL_VTABLE_OR_VTT_P (decl)
2194
              /* Construction virtual tables are not exported because
2195
                 they cannot be referred to from other object files;
2196
                 their name is not standardized by the ABI.  */
2197
              && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2198
      && TREE_PUBLIC (decl)
2199
      && !DECL_REALLY_EXTERN (decl)
2200
      && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2201
    targetm.cxx.determine_class_data_visibility (decl);
2202
}
2203
 
2204
/* Constrain the visibility of a class TYPE based on the visibility of its
2205
   field types.  Warn if any fields require lesser visibility.  */
2206
 
2207
void
2208
constrain_class_visibility (tree type)
2209
{
2210
  tree binfo;
2211
  tree t;
2212
  int i;
2213
 
2214
  int vis = type_visibility (type);
2215
 
2216
  if (vis == VISIBILITY_ANON
2217
      || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2218
    return;
2219
 
2220
  /* Don't warn about visibility if the class has explicit visibility.  */
2221
  if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2222
    vis = VISIBILITY_INTERNAL;
2223
 
2224
  for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t))
2225
    if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2226
      {
2227
        tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2228
        int subvis = type_visibility (ftype);
2229
 
2230
        if (subvis == VISIBILITY_ANON)
2231
          {
2232
            if (!in_main_input_context ())
2233
              warning (0, "\
2234
%qT has a field %qD whose type uses the anonymous namespace",
2235
                       type, t);
2236
          }
2237
        else if (MAYBE_CLASS_TYPE_P (ftype)
2238
                 && vis < VISIBILITY_HIDDEN
2239
                 && subvis >= VISIBILITY_HIDDEN)
2240
          warning (OPT_Wattributes, "\
2241
%qT declared with greater visibility than the type of its field %qD",
2242
                   type, t);
2243
      }
2244
 
2245
  binfo = TYPE_BINFO (type);
2246
  for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2247
    {
2248
      int subvis = type_visibility (TREE_TYPE (t));
2249
 
2250
      if (subvis == VISIBILITY_ANON)
2251
        {
2252
          if (!in_main_input_context())
2253
            warning (0, "\
2254
%qT has a base %qT whose type uses the anonymous namespace",
2255
                     type, TREE_TYPE (t));
2256
        }
2257
      else if (vis < VISIBILITY_HIDDEN
2258
               && subvis >= VISIBILITY_HIDDEN)
2259
        warning (OPT_Wattributes, "\
2260
%qT declared with greater visibility than its base %qT",
2261
                 type, TREE_TYPE (t));
2262
    }
2263
}
2264
 
2265
/* DECL is a FUNCTION_DECL or VAR_DECL.  If the object file linkage
2266
   for DECL has not already been determined, do so now by setting
2267
   DECL_EXTERNAL, DECL_COMDAT and other related flags.  Until this
2268
   function is called entities with vague linkage whose definitions
2269
   are available must have TREE_PUBLIC set.
2270
 
2271
   If this function decides to place DECL in COMDAT, it will set
2272
   appropriate flags -- but will not clear DECL_EXTERNAL.  It is up to
2273
   the caller to decide whether or not to clear DECL_EXTERNAL.  Some
2274
   callers defer that decision until it is clear that DECL is actually
2275
   required.  */
2276
 
2277
void
2278
import_export_decl (tree decl)
2279
{
2280
  int emit_p;
2281
  bool comdat_p;
2282
  bool import_p;
2283
  tree class_type = NULL_TREE;
2284
 
2285
  if (DECL_INTERFACE_KNOWN (decl))
2286
    return;
2287
 
2288
  /* We cannot determine what linkage to give to an entity with vague
2289
     linkage until the end of the file.  For example, a virtual table
2290
     for a class will be defined if and only if the key method is
2291
     defined in this translation unit.  As a further example, consider
2292
     that when compiling a translation unit that uses PCH file with
2293
     "-frepo" it would be incorrect to make decisions about what
2294
     entities to emit when building the PCH; those decisions must be
2295
     delayed until the repository information has been processed.  */
2296
  gcc_assert (at_eof);
2297
  /* Object file linkage for explicit instantiations is handled in
2298
     mark_decl_instantiated.  For static variables in functions with
2299
     vague linkage, maybe_commonize_var is used.
2300
 
2301
     Therefore, the only declarations that should be provided to this
2302
     function are those with external linkage that are:
2303
 
2304
     * implicit instantiations of function templates
2305
 
2306
     * inline function
2307
 
2308
     * implicit instantiations of static data members of class
2309
       templates
2310
 
2311
     * virtual tables
2312
 
2313
     * typeinfo objects
2314
 
2315
     Furthermore, all entities that reach this point must have a
2316
     definition available in this translation unit.
2317
 
2318
     The following assertions check these conditions.  */
2319
  gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
2320
              || TREE_CODE (decl) == VAR_DECL);
2321
  /* Any code that creates entities with TREE_PUBLIC cleared should
2322
     also set DECL_INTERFACE_KNOWN.  */
2323
  gcc_assert (TREE_PUBLIC (decl));
2324
  if (TREE_CODE (decl) == FUNCTION_DECL)
2325
    gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2326
                || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2327
                || DECL_DECLARED_INLINE_P (decl));
2328
  else
2329
    gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2330
                || DECL_VTABLE_OR_VTT_P (decl)
2331
                || DECL_TINFO_P (decl));
2332
  /* Check that a definition of DECL is available in this translation
2333
     unit.  */
2334
  gcc_assert (!DECL_REALLY_EXTERN (decl));
2335
 
2336
  /* Assume that DECL will not have COMDAT linkage.  */
2337
  comdat_p = false;
2338
  /* Assume that DECL will not be imported into this translation
2339
     unit.  */
2340
  import_p = false;
2341
 
2342
  /* See if the repository tells us whether or not to emit DECL in
2343
     this translation unit.  */
2344
  emit_p = repo_emit_p (decl);
2345
  if (emit_p == 0)
2346
    import_p = true;
2347
  else if (emit_p == 1)
2348
    {
2349
      /* The repository indicates that this entity should be defined
2350
         here.  Make sure the back end honors that request.  */
2351
      if (TREE_CODE (decl) == VAR_DECL)
2352
        mark_needed (decl);
2353
      else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
2354
               || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
2355
        {
2356
          tree clone;
2357
          FOR_EACH_CLONE (clone, decl)
2358
            mark_needed (clone);
2359
        }
2360
      else
2361
        mark_needed (decl);
2362
      /* Output the definition as an ordinary strong definition.  */
2363
      DECL_EXTERNAL (decl) = 0;
2364
      DECL_INTERFACE_KNOWN (decl) = 1;
2365
      return;
2366
    }
2367
 
2368
  if (import_p)
2369
    /* We have already decided what to do with this DECL; there is no
2370
       need to check anything further.  */
2371
    ;
2372
  else if (TREE_CODE (decl) == VAR_DECL && DECL_VTABLE_OR_VTT_P (decl))
2373
    {
2374
      class_type = DECL_CONTEXT (decl);
2375
      import_export_class (class_type);
2376
      if (TYPE_FOR_JAVA (class_type))
2377
        import_p = true;
2378
      else if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2379
               && CLASSTYPE_INTERFACE_ONLY (class_type))
2380
        import_p = true;
2381
      else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2382
               && !CLASSTYPE_USE_TEMPLATE (class_type)
2383
               && CLASSTYPE_KEY_METHOD (class_type)
2384
               && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2385
        /* The ABI requires that all virtual tables be emitted with
2386
           COMDAT linkage.  However, on systems where COMDAT symbols
2387
           don't show up in the table of contents for a static
2388
           archive, or on systems without weak symbols (where we
2389
           approximate COMDAT linkage by using internal linkage), the
2390
           linker will report errors about undefined symbols because
2391
           it will not see the virtual table definition.  Therefore,
2392
           in the case that we know that the virtual table will be
2393
           emitted in only one translation unit, we make the virtual
2394
           table an ordinary definition with external linkage.  */
2395
        DECL_EXTERNAL (decl) = 0;
2396
      else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2397
        {
2398
          /* CLASS_TYPE is being exported from this translation unit,
2399
             so DECL should be defined here.  */
2400
          if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2401
            /* If a class is declared in a header with the "extern
2402
               template" extension, then it will not be instantiated,
2403
               even in translation units that would normally require
2404
               it.  Often such classes are explicitly instantiated in
2405
               one translation unit.  Therefore, the explicit
2406
               instantiation must be made visible to other translation
2407
               units.  */
2408
            DECL_EXTERNAL (decl) = 0;
2409
          else
2410
            {
2411
              /* The generic C++ ABI says that class data is always
2412
                 COMDAT, even if there is a key function.  Some
2413
                 variants (e.g., the ARM EABI) says that class data
2414
                 only has COMDAT linkage if the class data might be
2415
                 emitted in more than one translation unit.  When the
2416
                 key method can be inline and is inline, we still have
2417
                 to arrange for comdat even though
2418
                 class_data_always_comdat is false.  */
2419
              if (!CLASSTYPE_KEY_METHOD (class_type)
2420
                  || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2421
                  || targetm.cxx.class_data_always_comdat ())
2422
                {
2423
                  /* The ABI requires COMDAT linkage.  Normally, we
2424
                     only emit COMDAT things when they are needed;
2425
                     make sure that we realize that this entity is
2426
                     indeed needed.  */
2427
                  comdat_p = true;
2428
                  mark_needed (decl);
2429
                }
2430
            }
2431
        }
2432
      else if (!flag_implicit_templates
2433
               && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2434
        import_p = true;
2435
      else
2436
        comdat_p = true;
2437
    }
2438
  else if (TREE_CODE (decl) == VAR_DECL && DECL_TINFO_P (decl))
2439
    {
2440
      tree type = TREE_TYPE (DECL_NAME (decl));
2441
      if (CLASS_TYPE_P (type))
2442
        {
2443
          class_type = type;
2444
          import_export_class (type);
2445
          if (CLASSTYPE_INTERFACE_KNOWN (type)
2446
              && TYPE_POLYMORPHIC_P (type)
2447
              && CLASSTYPE_INTERFACE_ONLY (type)
2448
              /* If -fno-rtti was specified, then we cannot be sure
2449
                 that RTTI information will be emitted with the
2450
                 virtual table of the class, so we must emit it
2451
                 wherever it is used.  */
2452
              && flag_rtti)
2453
            import_p = true;
2454
          else
2455
            {
2456
              if (CLASSTYPE_INTERFACE_KNOWN (type)
2457
                  && !CLASSTYPE_INTERFACE_ONLY (type))
2458
                {
2459
                  comdat_p = (targetm.cxx.class_data_always_comdat ()
2460
                              || (CLASSTYPE_KEY_METHOD (type)
2461
                                  && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2462
                  mark_needed (decl);
2463
                  if (!flag_weak)
2464
                    {
2465
                      comdat_p = false;
2466
                      DECL_EXTERNAL (decl) = 0;
2467
                    }
2468
                }
2469
              else
2470
                comdat_p = true;
2471
            }
2472
        }
2473
      else
2474
        comdat_p = true;
2475
    }
2476
  else if (DECL_TEMPLATE_INSTANTIATION (decl)
2477
           || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
2478
    {
2479
      /* DECL is an implicit instantiation of a function or static
2480
         data member.  */
2481
      if ((flag_implicit_templates
2482
           && !flag_use_repository)
2483
          || (flag_implicit_inline_templates
2484
              && TREE_CODE (decl) == FUNCTION_DECL
2485
              && DECL_DECLARED_INLINE_P (decl)))
2486
        comdat_p = true;
2487
      else
2488
        /* If we are not implicitly generating templates, then mark
2489
           this entity as undefined in this translation unit.  */
2490
        import_p = true;
2491
    }
2492
  else if (DECL_FUNCTION_MEMBER_P (decl))
2493
    {
2494
      if (!DECL_DECLARED_INLINE_P (decl))
2495
        {
2496
          tree ctype = DECL_CONTEXT (decl);
2497
          import_export_class (ctype);
2498
          if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2499
            {
2500
              DECL_NOT_REALLY_EXTERN (decl)
2501
                = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2502
                     || (DECL_DECLARED_INLINE_P (decl)
2503
                         && ! flag_implement_inlines
2504
                         && !DECL_VINDEX (decl)));
2505
 
2506
              if (!DECL_NOT_REALLY_EXTERN (decl))
2507
                DECL_EXTERNAL (decl) = 1;
2508
 
2509
              /* Always make artificials weak.  */
2510
              if (DECL_ARTIFICIAL (decl) && flag_weak)
2511
                comdat_p = true;
2512
              else
2513
                maybe_make_one_only (decl);
2514
            }
2515
        }
2516
      else
2517
        comdat_p = true;
2518
    }
2519
  else
2520
    comdat_p = true;
2521
 
2522
  if (import_p)
2523
    {
2524
      /* If we are importing DECL into this translation unit, mark is
2525
         an undefined here.  */
2526
      DECL_EXTERNAL (decl) = 1;
2527
      DECL_NOT_REALLY_EXTERN (decl) = 0;
2528
    }
2529
  else if (comdat_p)
2530
    {
2531
      /* If we decided to put DECL in COMDAT, mark it accordingly at
2532
         this point.  */
2533
      comdat_linkage (decl);
2534
    }
2535
 
2536
  DECL_INTERFACE_KNOWN (decl) = 1;
2537
}
2538
 
2539
/* Return an expression that performs the destruction of DECL, which
2540
   must be a VAR_DECL whose type has a non-trivial destructor, or is
2541
   an array whose (innermost) elements have a non-trivial destructor.  */
2542
 
2543
tree
2544
build_cleanup (tree decl)
2545
{
2546
  tree temp;
2547
  tree type = TREE_TYPE (decl);
2548
 
2549
  /* This function should only be called for declarations that really
2550
     require cleanups.  */
2551
  gcc_assert (!TYPE_HAS_TRIVIAL_DESTRUCTOR (type));
2552
 
2553
  /* Treat all objects with destructors as used; the destructor may do
2554
     something substantive.  */
2555
  mark_used (decl);
2556
 
2557
  if (TREE_CODE (type) == ARRAY_TYPE)
2558
    temp = decl;
2559
  else
2560
    temp = build_address (decl);
2561
  temp = build_delete (TREE_TYPE (temp), temp,
2562
                       sfk_complete_destructor,
2563
                       LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
2564
  return temp;
2565
}
2566
 
2567
/* Returns the initialization guard variable for the variable DECL,
2568
   which has static storage duration.  */
2569
 
2570
tree
2571
get_guard (tree decl)
2572
{
2573
  tree sname;
2574
  tree guard;
2575
 
2576
  sname = mangle_guard_variable (decl);
2577
  guard = IDENTIFIER_GLOBAL_VALUE (sname);
2578
  if (! guard)
2579
    {
2580
      tree guard_type;
2581
 
2582
      /* We use a type that is big enough to contain a mutex as well
2583
         as an integer counter.  */
2584
      guard_type = targetm.cxx.guard_type ();
2585
      guard = build_decl (DECL_SOURCE_LOCATION (decl),
2586
                          VAR_DECL, sname, guard_type);
2587
 
2588
      /* The guard should have the same linkage as what it guards.  */
2589
      TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
2590
      TREE_STATIC (guard) = TREE_STATIC (decl);
2591
      DECL_COMMON (guard) = DECL_COMMON (decl);
2592
      DECL_COMDAT (guard) = DECL_COMDAT (decl);
2593
      if (DECL_ONE_ONLY (decl))
2594
        make_decl_one_only (guard, cxx_comdat_group (guard));
2595
      if (TREE_PUBLIC (decl))
2596
        DECL_WEAK (guard) = DECL_WEAK (decl);
2597
      DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
2598
      DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
2599
 
2600
      DECL_ARTIFICIAL (guard) = 1;
2601
      DECL_IGNORED_P (guard) = 1;
2602
      TREE_USED (guard) = 1;
2603
      pushdecl_top_level_and_finish (guard, NULL_TREE);
2604
    }
2605
  return guard;
2606
}
2607
 
2608
/* Return those bits of the GUARD variable that should be set when the
2609
   guarded entity is actually initialized.  */
2610
 
2611
static tree
2612
get_guard_bits (tree guard)
2613
{
2614
  if (!targetm.cxx.guard_mask_bit ())
2615
    {
2616
      /* We only set the first byte of the guard, in order to leave room
2617
         for a mutex in the high-order bits.  */
2618
      guard = build1 (ADDR_EXPR,
2619
                      build_pointer_type (TREE_TYPE (guard)),
2620
                      guard);
2621
      guard = build1 (NOP_EXPR,
2622
                      build_pointer_type (char_type_node),
2623
                      guard);
2624
      guard = build1 (INDIRECT_REF, char_type_node, guard);
2625
    }
2626
 
2627
  return guard;
2628
}
2629
 
2630
/* Return an expression which determines whether or not the GUARD
2631
   variable has already been initialized.  */
2632
 
2633
tree
2634
get_guard_cond (tree guard)
2635
{
2636
  tree guard_value;
2637
 
2638
  /* Check to see if the GUARD is zero.  */
2639
  guard = get_guard_bits (guard);
2640
 
2641
  /* Mask off all but the low bit.  */
2642
  if (targetm.cxx.guard_mask_bit ())
2643
    {
2644
      guard_value = integer_one_node;
2645
      if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2646
        guard_value = convert (TREE_TYPE (guard), guard_value);
2647
      guard = cp_build_binary_op (input_location,
2648
                                  BIT_AND_EXPR, guard, guard_value,
2649
                                  tf_warning_or_error);
2650
    }
2651
 
2652
  guard_value = integer_zero_node;
2653
  if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2654
    guard_value = convert (TREE_TYPE (guard), guard_value);
2655
  return cp_build_binary_op (input_location,
2656
                             EQ_EXPR, guard, guard_value,
2657
                             tf_warning_or_error);
2658
}
2659
 
2660
/* Return an expression which sets the GUARD variable, indicating that
2661
   the variable being guarded has been initialized.  */
2662
 
2663
tree
2664
set_guard (tree guard)
2665
{
2666
  tree guard_init;
2667
 
2668
  /* Set the GUARD to one.  */
2669
  guard = get_guard_bits (guard);
2670
  guard_init = integer_one_node;
2671
  if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
2672
    guard_init = convert (TREE_TYPE (guard), guard_init);
2673
  return cp_build_modify_expr (guard, NOP_EXPR, guard_init,
2674
                               tf_warning_or_error);
2675
}
2676
 
2677
/* Start the process of running a particular set of global constructors
2678
   or destructors.  Subroutine of do_[cd]tors.  */
2679
 
2680
static tree
2681
start_objects (int method_type, int initp)
2682
{
2683
  tree body;
2684
  tree fndecl;
2685
  char type[10];
2686
 
2687
  /* Make ctor or dtor function.  METHOD_TYPE may be 'I' or 'D'.  */
2688
 
2689
  if (initp != DEFAULT_INIT_PRIORITY)
2690
    {
2691
      char joiner;
2692
 
2693
#ifdef JOINER
2694
      joiner = JOINER;
2695
#else
2696
      joiner = '_';
2697
#endif
2698
 
2699
      sprintf (type, "%c%c%.5u", method_type, joiner, initp);
2700
    }
2701
  else
2702
    sprintf (type, "%c", method_type);
2703
 
2704
  fndecl = build_lang_decl (FUNCTION_DECL,
2705
                            get_file_function_name (type),
2706
                            build_function_type (void_type_node,
2707
                                                 void_list_node));
2708
  start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
2709
 
2710
  TREE_PUBLIC (current_function_decl) = 0;
2711
 
2712
  /* Mark as artificial because it's not explicitly in the user's
2713
     source code.  */
2714
  DECL_ARTIFICIAL (current_function_decl) = 1;
2715
 
2716
  /* Mark this declaration as used to avoid spurious warnings.  */
2717
  TREE_USED (current_function_decl) = 1;
2718
 
2719
  /* Mark this function as a global constructor or destructor.  */
2720
  if (method_type == 'I')
2721
    DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
2722
  else
2723
    DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
2724
 
2725
  body = begin_compound_stmt (BCS_FN_BODY);
2726
 
2727
  return body;
2728
}
2729
 
2730
/* Finish the process of running a particular set of global constructors
2731
   or destructors.  Subroutine of do_[cd]tors.  */
2732
 
2733
static void
2734
finish_objects (int method_type, int initp, tree body)
2735
{
2736
  tree fn;
2737
 
2738
  /* Finish up.  */
2739
  finish_compound_stmt (body);
2740
  fn = finish_function (0);
2741
 
2742
  if (method_type == 'I')
2743
    {
2744
      DECL_STATIC_CONSTRUCTOR (fn) = 1;
2745
      decl_init_priority_insert (fn, initp);
2746
    }
2747
  else
2748
    {
2749
      DECL_STATIC_DESTRUCTOR (fn) = 1;
2750
      decl_fini_priority_insert (fn, initp);
2751
    }
2752
 
2753
  expand_or_defer_fn (fn);
2754
}
2755
 
2756
/* The names of the parameters to the function created to handle
2757
   initializations and destructions for objects with static storage
2758
   duration.  */
2759
#define INITIALIZE_P_IDENTIFIER "__initialize_p"
2760
#define PRIORITY_IDENTIFIER "__priority"
2761
 
2762
/* The name of the function we create to handle initializations and
2763
   destructions for objects with static storage duration.  */
2764
#define SSDF_IDENTIFIER "__static_initialization_and_destruction"
2765
 
2766
/* The declaration for the __INITIALIZE_P argument.  */
2767
static GTY(()) tree initialize_p_decl;
2768
 
2769
/* The declaration for the __PRIORITY argument.  */
2770
static GTY(()) tree priority_decl;
2771
 
2772
/* The declaration for the static storage duration function.  */
2773
static GTY(()) tree ssdf_decl;
2774
 
2775
/* All the static storage duration functions created in this
2776
   translation unit.  */
2777
static GTY(()) VEC(tree,gc) *ssdf_decls;
2778
 
2779
/* A map from priority levels to information about that priority
2780
   level.  There may be many such levels, so efficient lookup is
2781
   important.  */
2782
static splay_tree priority_info_map;
2783
 
2784
/* Begins the generation of the function that will handle all
2785
   initialization and destruction of objects with static storage
2786
   duration.  The function generated takes two parameters of type
2787
   `int': __INITIALIZE_P and __PRIORITY.  If __INITIALIZE_P is
2788
   nonzero, it performs initializations.  Otherwise, it performs
2789
   destructions.  It only performs those initializations or
2790
   destructions with the indicated __PRIORITY.  The generated function
2791
   returns no value.
2792
 
2793
   It is assumed that this function will only be called once per
2794
   translation unit.  */
2795
 
2796
static tree
2797
start_static_storage_duration_function (unsigned count)
2798
{
2799
  tree parm_types;
2800
  tree type;
2801
  tree body;
2802
  char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
2803
 
2804
  /* Create the identifier for this function.  It will be of the form
2805
     SSDF_IDENTIFIER_<number>.  */
2806
  sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
2807
 
2808
  /* Create the parameters.  */
2809
  parm_types = void_list_node;
2810
  parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
2811
  parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
2812
  type = build_function_type (void_type_node, parm_types);
2813
 
2814
  /* Create the FUNCTION_DECL itself.  */
2815
  ssdf_decl = build_lang_decl (FUNCTION_DECL,
2816
                               get_identifier (id),
2817
                               type);
2818
  TREE_PUBLIC (ssdf_decl) = 0;
2819
  DECL_ARTIFICIAL (ssdf_decl) = 1;
2820
 
2821
  /* Put this function in the list of functions to be called from the
2822
     static constructors and destructors.  */
2823
  if (!ssdf_decls)
2824
    {
2825
      ssdf_decls = VEC_alloc (tree, gc, 32);
2826
 
2827
      /* Take this opportunity to initialize the map from priority
2828
         numbers to information about that priority level.  */
2829
      priority_info_map = splay_tree_new (splay_tree_compare_ints,
2830
                                          /*delete_key_fn=*/0,
2831
                                          /*delete_value_fn=*/
2832
                                          (splay_tree_delete_value_fn) &free);
2833
 
2834
      /* We always need to generate functions for the
2835
         DEFAULT_INIT_PRIORITY so enter it now.  That way when we walk
2836
         priorities later, we'll be sure to find the
2837
         DEFAULT_INIT_PRIORITY.  */
2838
      get_priority_info (DEFAULT_INIT_PRIORITY);
2839
    }
2840
 
2841
  VEC_safe_push (tree, gc, ssdf_decls, ssdf_decl);
2842
 
2843
  /* Create the argument list.  */
2844
  initialize_p_decl = cp_build_parm_decl
2845
    (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
2846
  DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
2847
  TREE_USED (initialize_p_decl) = 1;
2848
  priority_decl = cp_build_parm_decl
2849
    (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
2850
  DECL_CONTEXT (priority_decl) = ssdf_decl;
2851
  TREE_USED (priority_decl) = 1;
2852
 
2853
  TREE_CHAIN (initialize_p_decl) = priority_decl;
2854
  DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
2855
 
2856
  /* Put the function in the global scope.  */
2857
  pushdecl (ssdf_decl);
2858
 
2859
  /* Start the function itself.  This is equivalent to declaring the
2860
     function as:
2861
 
2862
       static void __ssdf (int __initialize_p, init __priority_p);
2863
 
2864
     It is static because we only need to call this function from the
2865
     various constructor and destructor functions for this module.  */
2866
  start_preparsed_function (ssdf_decl,
2867
                            /*attrs=*/NULL_TREE,
2868
                            SF_PRE_PARSED);
2869
 
2870
  /* Set up the scope of the outermost block in the function.  */
2871
  body = begin_compound_stmt (BCS_FN_BODY);
2872
 
2873
  return body;
2874
}
2875
 
2876
/* Finish the generation of the function which performs initialization
2877
   and destruction of objects with static storage duration.  After
2878
   this point, no more such objects can be created.  */
2879
 
2880
static void
2881
finish_static_storage_duration_function (tree body)
2882
{
2883
  /* Close out the function.  */
2884
  finish_compound_stmt (body);
2885
  expand_or_defer_fn (finish_function (0));
2886
}
2887
 
2888
/* Return the information about the indicated PRIORITY level.  If no
2889
   code to handle this level has yet been generated, generate the
2890
   appropriate prologue.  */
2891
 
2892
static priority_info
2893
get_priority_info (int priority)
2894
{
2895
  priority_info pi;
2896
  splay_tree_node n;
2897
 
2898
  n = splay_tree_lookup (priority_info_map,
2899
                         (splay_tree_key) priority);
2900
  if (!n)
2901
    {
2902
      /* Create a new priority information structure, and insert it
2903
         into the map.  */
2904
      pi = XNEW (struct priority_info_s);
2905
      pi->initializations_p = 0;
2906
      pi->destructions_p = 0;
2907
      splay_tree_insert (priority_info_map,
2908
                         (splay_tree_key) priority,
2909
                         (splay_tree_value) pi);
2910
    }
2911
  else
2912
    pi = (priority_info) n->value;
2913
 
2914
  return pi;
2915
}
2916
 
2917
/* The effective initialization priority of a DECL.  */
2918
 
2919
#define DECL_EFFECTIVE_INIT_PRIORITY(decl)                                    \
2920
        ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
2921
         ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
2922
 
2923
/* Whether a DECL needs a guard to protect it against multiple
2924
   initialization.  */
2925
 
2926
#define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl)      \
2927
                                                    || DECL_ONE_ONLY (decl) \
2928
                                                    || DECL_WEAK (decl)))
2929
 
2930
/* Called from one_static_initialization_or_destruction(),
2931
   via walk_tree.
2932
   Walks the initializer list of a global variable and looks for
2933
   temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
2934
   and that have their DECL_CONTEXT() == NULL.
2935
   For each such temporary variable, set their DECL_CONTEXT() to
2936
   the current function. This is necessary because otherwise
2937
   some optimizers (enabled by -O2 -fprofile-arcs) might crash
2938
   when trying to refer to a temporary variable that does not have
2939
   it's DECL_CONTECT() properly set.  */
2940
static tree
2941
fix_temporary_vars_context_r (tree *node,
2942
                              int  *unused ATTRIBUTE_UNUSED,
2943
                              void *unused1 ATTRIBUTE_UNUSED)
2944
{
2945
  gcc_assert (current_function_decl);
2946
 
2947
  if (TREE_CODE (*node) == BIND_EXPR)
2948
    {
2949
      tree var;
2950
 
2951
      for (var = BIND_EXPR_VARS (*node); var; var = TREE_CHAIN (var))
2952
        if (TREE_CODE (var) == VAR_DECL
2953
          && !DECL_NAME (var)
2954
          && DECL_ARTIFICIAL (var)
2955
          && !DECL_CONTEXT (var))
2956
          DECL_CONTEXT (var) = current_function_decl;
2957
    }
2958
 
2959
  return NULL_TREE;
2960
}
2961
 
2962
/* Set up to handle the initialization or destruction of DECL.  If
2963
   INITP is nonzero, we are initializing the variable.  Otherwise, we
2964
   are destroying it.  */
2965
 
2966
static void
2967
one_static_initialization_or_destruction (tree decl, tree init, bool initp)
2968
{
2969
  tree guard_if_stmt = NULL_TREE;
2970
  tree guard;
2971
 
2972
  /* If we are supposed to destruct and there's a trivial destructor,
2973
     nothing has to be done.  */
2974
  if (!initp
2975
      && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
2976
    return;
2977
 
2978
  /* Trick the compiler into thinking we are at the file and line
2979
     where DECL was declared so that error-messages make sense, and so
2980
     that the debugger will show somewhat sensible file and line
2981
     information.  */
2982
  input_location = DECL_SOURCE_LOCATION (decl);
2983
 
2984
  /* Make sure temporary variables in the initialiser all have
2985
     their DECL_CONTEXT() set to a value different from NULL_TREE.
2986
     This can happen when global variables initialisers are built.
2987
     In that case, the DECL_CONTEXT() of the global variables _AND_ of all
2988
     the temporary variables that might have been generated in the
2989
     accompagning initialisers is NULL_TREE, meaning the variables have been
2990
     declared in the global namespace.
2991
     What we want to do here is to fix that and make sure the DECL_CONTEXT()
2992
     of the temporaries are set to the current function decl.  */
2993
  cp_walk_tree_without_duplicates (&init,
2994
                                   fix_temporary_vars_context_r,
2995
                                   NULL);
2996
 
2997
  /* Because of:
2998
 
2999
       [class.access.spec]
3000
 
3001
       Access control for implicit calls to the constructors,
3002
       the conversion functions, or the destructor called to
3003
       create and destroy a static data member is performed as
3004
       if these calls appeared in the scope of the member's
3005
       class.
3006
 
3007
     we pretend we are in a static member function of the class of
3008
     which the DECL is a member.  */
3009
  if (member_p (decl))
3010
    {
3011
      DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3012
      DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3013
    }
3014
 
3015
  /* Assume we don't need a guard.  */
3016
  guard = NULL_TREE;
3017
  /* We need a guard if this is an object with external linkage that
3018
     might be initialized in more than one place.  (For example, a
3019
     static data member of a template, when the data member requires
3020
     construction.)  */
3021
  if (NEEDS_GUARD_P (decl))
3022
    {
3023
      tree guard_cond;
3024
 
3025
      guard = get_guard (decl);
3026
 
3027
      /* When using __cxa_atexit, we just check the GUARD as we would
3028
         for a local static.  */
3029
      if (flag_use_cxa_atexit)
3030
        {
3031
          /* When using __cxa_atexit, we never try to destroy
3032
             anything from a static destructor.  */
3033
          gcc_assert (initp);
3034
          guard_cond = get_guard_cond (guard);
3035
        }
3036
      /* If we don't have __cxa_atexit, then we will be running
3037
         destructors from .fini sections, or their equivalents.  So,
3038
         we need to know how many times we've tried to initialize this
3039
         object.  We do initializations only if the GUARD is zero,
3040
         i.e., if we are the first to initialize the variable.  We do
3041
         destructions only if the GUARD is one, i.e., if we are the
3042
         last to destroy the variable.  */
3043
      else if (initp)
3044
        guard_cond
3045
          = cp_build_binary_op (input_location,
3046
                                EQ_EXPR,
3047
                                cp_build_unary_op (PREINCREMENT_EXPR,
3048
                                                   guard,
3049
                                                   /*noconvert=*/1,
3050
                                                   tf_warning_or_error),
3051
                                integer_one_node,
3052
                                tf_warning_or_error);
3053
      else
3054
        guard_cond
3055
          = cp_build_binary_op (input_location,
3056
                                EQ_EXPR,
3057
                                cp_build_unary_op (PREDECREMENT_EXPR,
3058
                                                   guard,
3059
                                                   /*noconvert=*/1,
3060
                                                   tf_warning_or_error),
3061
                                integer_zero_node,
3062
                                tf_warning_or_error);
3063
 
3064
      guard_if_stmt = begin_if_stmt ();
3065
      finish_if_stmt_cond (guard_cond, guard_if_stmt);
3066
    }
3067
 
3068
 
3069
  /* If we're using __cxa_atexit, we have not already set the GUARD,
3070
     so we must do so now.  */
3071
  if (guard && initp && flag_use_cxa_atexit)
3072
    finish_expr_stmt (set_guard (guard));
3073
 
3074
  /* Perform the initialization or destruction.  */
3075
  if (initp)
3076
    {
3077
      if (init)
3078
        finish_expr_stmt (init);
3079
 
3080
      /* If we're using __cxa_atexit, register a function that calls the
3081
         destructor for the object.  */
3082
      if (flag_use_cxa_atexit)
3083
        finish_expr_stmt (register_dtor_fn (decl));
3084
    }
3085
  else
3086
    finish_expr_stmt (build_cleanup (decl));
3087
 
3088
  /* Finish the guard if-stmt, if necessary.  */
3089
  if (guard)
3090
    {
3091
      finish_then_clause (guard_if_stmt);
3092
      finish_if_stmt (guard_if_stmt);
3093
    }
3094
 
3095
  /* Now that we're done with DECL we don't need to pretend to be a
3096
     member of its class any longer.  */
3097
  DECL_CONTEXT (current_function_decl) = NULL_TREE;
3098
  DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3099
}
3100
 
3101
/* Generate code to do the initialization or destruction of the decls in VARS,
3102
   a TREE_LIST of VAR_DECL with static storage duration.
3103
   Whether initialization or destruction is performed is specified by INITP.  */
3104
 
3105
static void
3106
do_static_initialization_or_destruction (tree vars, bool initp)
3107
{
3108
  tree node, init_if_stmt, cond;
3109
 
3110
  /* Build the outer if-stmt to check for initialization or destruction.  */
3111
  init_if_stmt = begin_if_stmt ();
3112
  cond = initp ? integer_one_node : integer_zero_node;
3113
  cond = cp_build_binary_op (input_location,
3114
                             EQ_EXPR,
3115
                             initialize_p_decl,
3116
                             cond,
3117
                             tf_warning_or_error);
3118
  finish_if_stmt_cond (cond, init_if_stmt);
3119
 
3120
  node = vars;
3121
  do {
3122
    tree decl = TREE_VALUE (node);
3123
    tree priority_if_stmt;
3124
    int priority;
3125
    priority_info pi;
3126
 
3127
    /* If we don't need a destructor, there's nothing to do.  Avoid
3128
       creating a possibly empty if-stmt.  */
3129
    if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3130
      {
3131
        node = TREE_CHAIN (node);
3132
        continue;
3133
      }
3134
 
3135
    /* Remember that we had an initialization or finalization at this
3136
       priority.  */
3137
    priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3138
    pi = get_priority_info (priority);
3139
    if (initp)
3140
      pi->initializations_p = 1;
3141
    else
3142
      pi->destructions_p = 1;
3143
 
3144
    /* Conditionalize this initialization on being in the right priority
3145
       and being initializing/finalizing appropriately.  */
3146
    priority_if_stmt = begin_if_stmt ();
3147
    cond = cp_build_binary_op (input_location,
3148
                               EQ_EXPR,
3149
                               priority_decl,
3150
                               build_int_cst (NULL_TREE, priority),
3151
                               tf_warning_or_error);
3152
    finish_if_stmt_cond (cond, priority_if_stmt);
3153
 
3154
    /* Process initializers with same priority.  */
3155
    for (; node
3156
           && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3157
         node = TREE_CHAIN (node))
3158
      /* Do one initialization or destruction.  */
3159
      one_static_initialization_or_destruction (TREE_VALUE (node),
3160
                                                TREE_PURPOSE (node), initp);
3161
 
3162
    /* Finish up the priority if-stmt body.  */
3163
    finish_then_clause (priority_if_stmt);
3164
    finish_if_stmt (priority_if_stmt);
3165
 
3166
  } while (node);
3167
 
3168
  /* Finish up the init/destruct if-stmt body.  */
3169
  finish_then_clause (init_if_stmt);
3170
  finish_if_stmt (init_if_stmt);
3171
}
3172
 
3173
/* VARS is a list of variables with static storage duration which may
3174
   need initialization and/or finalization.  Remove those variables
3175
   that don't really need to be initialized or finalized, and return
3176
   the resulting list.  The order in which the variables appear in
3177
   VARS is in reverse order of the order in which they should actually
3178
   be initialized.  The list we return is in the unreversed order;
3179
   i.e., the first variable should be initialized first.  */
3180
 
3181
static tree
3182
prune_vars_needing_no_initialization (tree *vars)
3183
{
3184
  tree *var = vars;
3185
  tree result = NULL_TREE;
3186
 
3187
  while (*var)
3188
    {
3189
      tree t = *var;
3190
      tree decl = TREE_VALUE (t);
3191
      tree init = TREE_PURPOSE (t);
3192
 
3193
      /* Deal gracefully with error.  */
3194
      if (decl == error_mark_node)
3195
        {
3196
          var = &TREE_CHAIN (t);
3197
          continue;
3198
        }
3199
 
3200
      /* The only things that can be initialized are variables.  */
3201
      gcc_assert (TREE_CODE (decl) == VAR_DECL);
3202
 
3203
      /* If this object is not defined, we don't need to do anything
3204
         here.  */
3205
      if (DECL_EXTERNAL (decl))
3206
        {
3207
          var = &TREE_CHAIN (t);
3208
          continue;
3209
        }
3210
 
3211
      /* Also, if the initializer already contains errors, we can bail
3212
         out now.  */
3213
      if (init && TREE_CODE (init) == TREE_LIST
3214
          && value_member (error_mark_node, init))
3215
        {
3216
          var = &TREE_CHAIN (t);
3217
          continue;
3218
        }
3219
 
3220
      /* This variable is going to need initialization and/or
3221
         finalization, so we add it to the list.  */
3222
      *var = TREE_CHAIN (t);
3223
      TREE_CHAIN (t) = result;
3224
      result = t;
3225
    }
3226
 
3227
  return result;
3228
}
3229
 
3230
/* Make sure we have told the back end about all the variables in
3231
   VARS.  */
3232
 
3233
static void
3234
write_out_vars (tree vars)
3235
{
3236
  tree v;
3237
 
3238
  for (v = vars; v; v = TREE_CHAIN (v))
3239
    {
3240
      tree var = TREE_VALUE (v);
3241
      if (!var_finalized_p (var))
3242
        {
3243
          import_export_decl (var);
3244
          rest_of_decl_compilation (var, 1, 1);
3245
        }
3246
    }
3247
}
3248
 
3249
/* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3250
   (otherwise) that will initialize all global objects with static
3251
   storage duration having the indicated PRIORITY.  */
3252
 
3253
static void
3254
generate_ctor_or_dtor_function (bool constructor_p, int priority,
3255
                                location_t *locus)
3256
{
3257
  char function_key;
3258
  tree arguments;
3259
  tree fndecl;
3260
  tree body;
3261
  size_t i;
3262
 
3263
  input_location = *locus;
3264
  /* ??? */
3265
  /* Was: locus->line++; */
3266
 
3267
  /* We use `I' to indicate initialization and `D' to indicate
3268
     destruction.  */
3269
  function_key = constructor_p ? 'I' : 'D';
3270
 
3271
  /* We emit the function lazily, to avoid generating empty
3272
     global constructors and destructors.  */
3273
  body = NULL_TREE;
3274
 
3275
  /* For Objective-C++, we may need to initialize metadata found in this module.
3276
     This must be done _before_ any other static initializations.  */
3277
  if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3278
      && constructor_p && objc_static_init_needed_p ())
3279
    {
3280
      body = start_objects (function_key, priority);
3281
      objc_generate_static_init_call (NULL_TREE);
3282
    }
3283
 
3284
  /* Call the static storage duration function with appropriate
3285
     arguments.  */
3286
  for (i = 0; VEC_iterate (tree, ssdf_decls, i, fndecl); ++i)
3287
    {
3288
      /* Calls to pure or const functions will expand to nothing.  */
3289
      if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3290
        {
3291
          if (! body)
3292
            body = start_objects (function_key, priority);
3293
 
3294
          arguments = tree_cons (NULL_TREE,
3295
                                 build_int_cst (NULL_TREE, priority),
3296
                                 NULL_TREE);
3297
          arguments = tree_cons (NULL_TREE,
3298
                                 build_int_cst (NULL_TREE, constructor_p),
3299
                                 arguments);
3300
          finish_expr_stmt (cp_build_function_call (fndecl, arguments,
3301
                                                    tf_warning_or_error));
3302
        }
3303
    }
3304
 
3305
  /* Close out the function.  */
3306
  if (body)
3307
    finish_objects (function_key, priority, body);
3308
}
3309
 
3310
/* Generate constructor and destructor functions for the priority
3311
   indicated by N.  */
3312
 
3313
static int
3314
generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
3315
{
3316
  location_t *locus = (location_t *) data;
3317
  int priority = (int) n->key;
3318
  priority_info pi = (priority_info) n->value;
3319
 
3320
  /* Generate the functions themselves, but only if they are really
3321
     needed.  */
3322
  if (pi->initializations_p)
3323
    generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
3324
  if (pi->destructions_p)
3325
    generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
3326
 
3327
  /* Keep iterating.  */
3328
  return 0;
3329
}
3330
 
3331
/* Called via LANGHOOK_CALLGRAPH_ANALYZE_EXPR.  It is supposed to mark
3332
   decls referenced from front-end specific constructs; it will be called
3333
   only for language-specific tree nodes.
3334
 
3335
   Here we must deal with member pointers.  */
3336
 
3337
tree
3338
cxx_callgraph_analyze_expr (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED)
3339
{
3340
  tree t = *tp;
3341
 
3342
  switch (TREE_CODE (t))
3343
    {
3344
    case PTRMEM_CST:
3345
      if (TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
3346
        cgraph_mark_address_taken_node (cgraph_node (PTRMEM_CST_MEMBER (t)));
3347
      break;
3348
    case BASELINK:
3349
      if (TREE_CODE (BASELINK_FUNCTIONS (t)) == FUNCTION_DECL)
3350
        cgraph_mark_address_taken_node (cgraph_node (BASELINK_FUNCTIONS (t)));
3351
      break;
3352
    case VAR_DECL:
3353
      if (DECL_VTABLE_OR_VTT_P (t))
3354
        {
3355
          /* The ABI requires that all virtual tables be emitted
3356
             whenever one of them is.  */
3357
          tree vtbl;
3358
          for (vtbl = CLASSTYPE_VTABLES (DECL_CONTEXT (t));
3359
               vtbl;
3360
               vtbl = TREE_CHAIN (vtbl))
3361
            mark_decl_referenced (vtbl);
3362
        }
3363
      else if (DECL_CONTEXT (t)
3364
               && flag_use_repository
3365
               && TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL)
3366
        /* If we need a static variable in a function, then we
3367
           need the containing function.  */
3368
        mark_decl_referenced (DECL_CONTEXT (t));
3369
      break;
3370
    default:
3371
      break;
3372
    }
3373
 
3374
  return NULL;
3375
}
3376
 
3377
/* Java requires that we be able to reference a local address for a
3378
   method, and not be confused by PLT entries.  If hidden aliases are
3379
   supported, collect and return all the functions for which we should
3380
   emit a hidden alias.  */
3381
 
3382
static struct pointer_set_t *
3383
collect_candidates_for_java_method_aliases (void)
3384
{
3385
  struct cgraph_node *node;
3386
  struct pointer_set_t *candidates = NULL;
3387
 
3388
#ifndef HAVE_GAS_HIDDEN
3389
  return candidates;
3390
#endif
3391
 
3392
  for (node = cgraph_nodes; node ; node = node->next)
3393
    {
3394
      tree fndecl = node->decl;
3395
 
3396
      if (DECL_CONTEXT (fndecl)
3397
          && TYPE_P (DECL_CONTEXT (fndecl))
3398
          && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl))
3399
          && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl))
3400
        {
3401
          if (candidates == NULL)
3402
            candidates = pointer_set_create ();
3403
          pointer_set_insert (candidates, fndecl);
3404
        }
3405
    }
3406
 
3407
  return candidates;
3408
}
3409
 
3410
 
3411
/* Java requires that we be able to reference a local address for a
3412
   method, and not be confused by PLT entries.  If hidden aliases are
3413
   supported, emit one for each java function that we've emitted.
3414
   CANDIDATES is the set of FUNCTION_DECLs that were gathered
3415
   by collect_candidates_for_java_method_aliases.  */
3416
 
3417
static void
3418
build_java_method_aliases (struct pointer_set_t *candidates)
3419
{
3420
  struct cgraph_node *node;
3421
 
3422
#ifndef HAVE_GAS_HIDDEN
3423
  return;
3424
#endif
3425
 
3426
  for (node = cgraph_nodes; node ; node = node->next)
3427
    {
3428
      tree fndecl = node->decl;
3429
 
3430
      if (TREE_ASM_WRITTEN (fndecl)
3431
          && pointer_set_contains (candidates, fndecl))
3432
        {
3433
          /* Mangle the name in a predictable way; we need to reference
3434
             this from a java compiled object file.  */
3435
          tree oid, nid, alias;
3436
          const char *oname;
3437
          char *nname;
3438
 
3439
          oid = DECL_ASSEMBLER_NAME (fndecl);
3440
          oname = IDENTIFIER_POINTER (oid);
3441
          gcc_assert (oname[0] == '_' && oname[1] == 'Z');
3442
          nname = ACONCAT (("_ZGA", oname+2, NULL));
3443
          nid = get_identifier (nname);
3444
 
3445
          alias = make_alias_for (fndecl, nid);
3446
          TREE_PUBLIC (alias) = 1;
3447
          DECL_VISIBILITY (alias) = VISIBILITY_HIDDEN;
3448
 
3449
          assemble_alias (alias, oid);
3450
        }
3451
    }
3452
}
3453
 
3454
/* Returns true iff there is a definition available for variable or
3455
   function DECL.  */
3456
 
3457
static bool
3458
decl_defined_p (tree decl)
3459
{
3460
  if (TREE_CODE (decl) == FUNCTION_DECL)
3461
    return (DECL_INITIAL (decl) != NULL_TREE);
3462
  else
3463
    {
3464
      gcc_assert (TREE_CODE (decl) == VAR_DECL);
3465
      return !DECL_EXTERNAL (decl);
3466
    }
3467
}
3468
 
3469
/* Complain that DECL uses a type with no linkage but is never defined.  */
3470
 
3471
static void
3472
no_linkage_error (tree decl)
3473
{
3474
  tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
3475
  if (TYPE_ANONYMOUS_P (t))
3476
    {
3477
      permerror (0, "%q+#D, declared using anonymous type, "
3478
                 "is used but never defined", decl);
3479
      if (is_typedef_decl (TYPE_NAME (t)))
3480
        permerror (0, "%q+#D does not refer to the unqualified type, "
3481
                   "so it is not used for linkage", TYPE_NAME (t));
3482
    }
3483
  else
3484
    permerror (0, "%q+#D, declared using local type %qT, "
3485
               "is used but never defined", decl, t);
3486
}
3487
 
3488
/* This routine is called at the end of compilation.
3489
   Its job is to create all the code needed to initialize and
3490
   destroy the global aggregates.  We do the destruction
3491
   first, since that way we only need to reverse the decls once.  */
3492
 
3493
void
3494
cp_write_global_declarations (void)
3495
{
3496
  tree vars;
3497
  bool reconsider;
3498
  size_t i;
3499
  location_t locus;
3500
  unsigned ssdf_count = 0;
3501
  int retries = 0;
3502
  tree decl;
3503
  struct pointer_set_t *candidates;
3504
 
3505
  locus = input_location;
3506
  at_eof = 1;
3507
 
3508
  /* Bad parse errors.  Just forget about it.  */
3509
  if (! global_bindings_p () || current_class_type || decl_namespace_list)
3510
    return;
3511
 
3512
  if (pch_file)
3513
    c_common_write_pch ();
3514
 
3515
  /* FIXME - huh?  was  input_line -= 1;*/
3516
 
3517
  /* We now have to write out all the stuff we put off writing out.
3518
     These include:
3519
 
3520
       o Template specializations that we have not yet instantiated,
3521
         but which are needed.
3522
       o Initialization and destruction for non-local objects with
3523
         static storage duration.  (Local objects with static storage
3524
         duration are initialized when their scope is first entered,
3525
         and are cleaned up via atexit.)
3526
       o Virtual function tables.
3527
 
3528
     All of these may cause others to be needed.  For example,
3529
     instantiating one function may cause another to be needed, and
3530
     generating the initializer for an object may cause templates to be
3531
     instantiated, etc., etc.  */
3532
 
3533
  timevar_push (TV_VARCONST);
3534
 
3535
  emit_support_tinfos ();
3536
 
3537
  do
3538
    {
3539
      tree t;
3540
      tree decl;
3541
 
3542
      reconsider = false;
3543
 
3544
      /* If there are templates that we've put off instantiating, do
3545
         them now.  */
3546
      instantiate_pending_templates (retries);
3547
      ggc_collect ();
3548
 
3549
      /* Write out virtual tables as required.  Note that writing out
3550
         the virtual table for a template class may cause the
3551
         instantiation of members of that class.  If we write out
3552
         vtables then we remove the class from our list so we don't
3553
         have to look at it again.  */
3554
 
3555
      while (keyed_classes != NULL_TREE
3556
             && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
3557
        {
3558
          reconsider = true;
3559
          keyed_classes = TREE_CHAIN (keyed_classes);
3560
        }
3561
 
3562
      t = keyed_classes;
3563
      if (t != NULL_TREE)
3564
        {
3565
          tree next = TREE_CHAIN (t);
3566
 
3567
          while (next)
3568
            {
3569
              if (maybe_emit_vtables (TREE_VALUE (next)))
3570
                {
3571
                  reconsider = true;
3572
                  TREE_CHAIN (t) = TREE_CHAIN (next);
3573
                }
3574
              else
3575
                t = next;
3576
 
3577
              next = TREE_CHAIN (t);
3578
            }
3579
        }
3580
 
3581
      /* Write out needed type info variables.  We have to be careful
3582
         looping through unemitted decls, because emit_tinfo_decl may
3583
         cause other variables to be needed. New elements will be
3584
         appended, and we remove from the vector those that actually
3585
         get emitted.  */
3586
      for (i = VEC_length (tree, unemitted_tinfo_decls);
3587
           VEC_iterate (tree, unemitted_tinfo_decls, --i, t);)
3588
        if (emit_tinfo_decl (t))
3589
          {
3590
            reconsider = true;
3591
            VEC_unordered_remove (tree, unemitted_tinfo_decls, i);
3592
          }
3593
 
3594
      /* The list of objects with static storage duration is built up
3595
         in reverse order.  We clear STATIC_AGGREGATES so that any new
3596
         aggregates added during the initialization of these will be
3597
         initialized in the correct order when we next come around the
3598
         loop.  */
3599
      vars = prune_vars_needing_no_initialization (&static_aggregates);
3600
 
3601
      if (vars)
3602
        {
3603
          /* We need to start a new initialization function each time
3604
             through the loop.  That's because we need to know which
3605
             vtables have been referenced, and TREE_SYMBOL_REFERENCED
3606
             isn't computed until a function is finished, and written
3607
             out.  That's a deficiency in the back end.  When this is
3608
             fixed, these initialization functions could all become
3609
             inline, with resulting performance improvements.  */
3610
          tree ssdf_body;
3611
 
3612
          /* Set the line and file, so that it is obviously not from
3613
             the source file.  */
3614
          input_location = locus;
3615
          ssdf_body = start_static_storage_duration_function (ssdf_count);
3616
 
3617
          /* Make sure the back end knows about all the variables.  */
3618
          write_out_vars (vars);
3619
 
3620
          /* First generate code to do all the initializations.  */
3621
          if (vars)
3622
            do_static_initialization_or_destruction (vars, /*initp=*/true);
3623
 
3624
          /* Then, generate code to do all the destructions.  Do these
3625
             in reverse order so that the most recently constructed
3626
             variable is the first destroyed.  If we're using
3627
             __cxa_atexit, then we don't need to do this; functions
3628
             were registered at initialization time to destroy the
3629
             local statics.  */
3630
          if (!flag_use_cxa_atexit && vars)
3631
            {
3632
              vars = nreverse (vars);
3633
              do_static_initialization_or_destruction (vars, /*initp=*/false);
3634
            }
3635
          else
3636
            vars = NULL_TREE;
3637
 
3638
          /* Finish up the static storage duration function for this
3639
             round.  */
3640
          input_location = locus;
3641
          finish_static_storage_duration_function (ssdf_body);
3642
 
3643
          /* All those initializations and finalizations might cause
3644
             us to need more inline functions, more template
3645
             instantiations, etc.  */
3646
          reconsider = true;
3647
          ssdf_count++;
3648
          /* ??? was:  locus.line++; */
3649
        }
3650
 
3651
      /* Go through the set of inline functions whose bodies have not
3652
         been emitted yet.  If out-of-line copies of these functions
3653
         are required, emit them.  */
3654
      for (i = 0; VEC_iterate (tree, deferred_fns, i, decl); ++i)
3655
        {
3656
          /* Does it need synthesizing?  */
3657
          if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
3658
              && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
3659
            {
3660
              /* Even though we're already at the top-level, we push
3661
                 there again.  That way, when we pop back a few lines
3662
                 hence, all of our state is restored.  Otherwise,
3663
                 finish_function doesn't clean things up, and we end
3664
                 up with CURRENT_FUNCTION_DECL set.  */
3665
              push_to_top_level ();
3666
              /* The decl's location will mark where it was first
3667
                 needed.  Save that so synthesize method can indicate
3668
                 where it was needed from, in case of error  */
3669
              input_location = DECL_SOURCE_LOCATION (decl);
3670
              synthesize_method (decl);
3671
              pop_from_top_level ();
3672
              reconsider = true;
3673
            }
3674
 
3675
          if (!DECL_SAVED_TREE (decl))
3676
            continue;
3677
 
3678
          /* We lie to the back end, pretending that some functions
3679
             are not defined when they really are.  This keeps these
3680
             functions from being put out unnecessarily.  But, we must
3681
             stop lying when the functions are referenced, or if they
3682
             are not comdat since they need to be put out now.  If
3683
             DECL_INTERFACE_KNOWN, then we have already set
3684
             DECL_EXTERNAL appropriately, so there's no need to check
3685
             again, and we do not want to clear DECL_EXTERNAL if a
3686
             previous call to import_export_decl set it.
3687
 
3688
             This is done in a separate for cycle, because if some
3689
             deferred function is contained in another deferred
3690
             function later in deferred_fns varray,
3691
             rest_of_compilation would skip this function and we
3692
             really cannot expand the same function twice.  */
3693
          import_export_decl (decl);
3694
          if (DECL_NOT_REALLY_EXTERN (decl)
3695
              && DECL_INITIAL (decl)
3696
              && decl_needed_p (decl))
3697
            {
3698
              struct cgraph_node *node = cgraph_get_node (decl), *alias, *next;
3699
 
3700
              DECL_EXTERNAL (decl) = 0;
3701
              /* If we mark !DECL_EXTERNAL one of the same body aliases,
3702
                 we need to mark all of them that way.  */
3703
              if (node && node->same_body)
3704
                {
3705
                  DECL_EXTERNAL (node->decl) = 0;
3706
                  for (alias = node->same_body; alias; alias = alias->next)
3707
                    DECL_EXTERNAL (alias->decl) = 0;
3708
                }
3709
              /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
3710
                 group, we need to mark all symbols in the same comdat group
3711
                 that way.  */
3712
              if (node->same_comdat_group)
3713
                for (next = node->same_comdat_group;
3714
                     next != node;
3715
                     next = next->same_comdat_group)
3716
                  {
3717
                    DECL_EXTERNAL (next->decl) = 0;
3718
                    if (next->same_body)
3719
                      {
3720
                        for (alias = next->same_body;
3721
                             alias;
3722
                             alias = alias->next)
3723
                          DECL_EXTERNAL (alias->decl) = 0;
3724
                      }
3725
                  }
3726
            }
3727
 
3728
          /* If we're going to need to write this function out, and
3729
             there's already a body for it, create RTL for it now.
3730
             (There might be no body if this is a method we haven't
3731
             gotten around to synthesizing yet.)  */
3732
          if (!DECL_EXTERNAL (decl)
3733
              && decl_needed_p (decl)
3734
              && !TREE_ASM_WRITTEN (decl)
3735
              && !cgraph_node (decl)->local.finalized)
3736
            {
3737
              /* We will output the function; no longer consider it in this
3738
                 loop.  */
3739
              DECL_DEFER_OUTPUT (decl) = 0;
3740
              /* Generate RTL for this function now that we know we
3741
                 need it.  */
3742
              expand_or_defer_fn (decl);
3743
              /* If we're compiling -fsyntax-only pretend that this
3744
                 function has been written out so that we don't try to
3745
                 expand it again.  */
3746
              if (flag_syntax_only)
3747
                TREE_ASM_WRITTEN (decl) = 1;
3748
              reconsider = true;
3749
            }
3750
        }
3751
 
3752
      if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
3753
        reconsider = true;
3754
 
3755
      /* Static data members are just like namespace-scope globals.  */
3756
      for (i = 0; VEC_iterate (tree, pending_statics, i, decl); ++i)
3757
        {
3758
          if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
3759
              /* Don't write it out if we haven't seen a definition.  */
3760
              || DECL_IN_AGGR_P (decl))
3761
            continue;
3762
          import_export_decl (decl);
3763
          /* If this static data member is needed, provide it to the
3764
             back end.  */
3765
          if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
3766
            DECL_EXTERNAL (decl) = 0;
3767
        }
3768
      if (VEC_length (tree, pending_statics) != 0
3769
          && wrapup_global_declarations (VEC_address (tree, pending_statics),
3770
                                         VEC_length (tree, pending_statics)))
3771
        reconsider = true;
3772
 
3773
      retries++;
3774
    }
3775
  while (reconsider);
3776
 
3777
  /* All used inline functions must have a definition at this point.  */
3778
  for (i = 0; VEC_iterate (tree, deferred_fns, i, decl); ++i)
3779
    {
3780
      if (/* Check online inline functions that were actually used.  */
3781
          DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
3782
          /* If the definition actually was available here, then the
3783
             fact that the function was not defined merely represents
3784
             that for some reason (use of a template repository,
3785
             #pragma interface, etc.) we decided not to emit the
3786
             definition here.  */
3787
          && !DECL_INITIAL (decl)
3788
          /* An explicit instantiation can be used to specify
3789
             that the body is in another unit. It will have
3790
             already verified there was a definition.  */
3791
          && !DECL_EXPLICIT_INSTANTIATION (decl))
3792
        {
3793
          warning (0, "inline function %q+D used but never defined", decl);
3794
          /* Avoid a duplicate warning from check_global_declaration_1.  */
3795
          TREE_NO_WARNING (decl) = 1;
3796
        }
3797
    }
3798
 
3799
  /* So must decls that use a type with no linkage.  */
3800
  for (i = 0; VEC_iterate (tree, no_linkage_decls, i, decl); ++i)
3801
    if (!decl_defined_p (decl))
3802
      no_linkage_error (decl);
3803
 
3804
  /* We give C linkage to static constructors and destructors.  */
3805
  push_lang_context (lang_name_c);
3806
 
3807
  /* Generate initialization and destruction functions for all
3808
     priorities for which they are required.  */
3809
  if (priority_info_map)
3810
    splay_tree_foreach (priority_info_map,
3811
                        generate_ctor_and_dtor_functions_for_priority,
3812
                        /*data=*/&locus);
3813
  else if (c_dialect_objc () && objc_static_init_needed_p ())
3814
    /* If this is obj-c++ and we need a static init, call
3815
       generate_ctor_or_dtor_function.  */
3816
    generate_ctor_or_dtor_function (/*constructor_p=*/true,
3817
                                    DEFAULT_INIT_PRIORITY, &locus);
3818
 
3819
  /* We're done with the splay-tree now.  */
3820
  if (priority_info_map)
3821
    splay_tree_delete (priority_info_map);
3822
 
3823
  /* Generate any missing aliases.  */
3824
  maybe_apply_pending_pragma_weaks ();
3825
 
3826
  /* We're done with static constructors, so we can go back to "C++"
3827
     linkage now.  */
3828
  pop_lang_context ();
3829
 
3830
  /* Collect candidates for Java hidden aliases.  */
3831
  candidates = collect_candidates_for_java_method_aliases ();
3832
 
3833
  cgraph_finalize_compilation_unit ();
3834
 
3835
  /* Now, issue warnings about static, but not defined, functions,
3836
     etc., and emit debugging information.  */
3837
  walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
3838
  if (VEC_length (tree, pending_statics) != 0)
3839
    {
3840
      check_global_declarations (VEC_address (tree, pending_statics),
3841
                                 VEC_length (tree, pending_statics));
3842
      emit_debug_global_declarations (VEC_address (tree, pending_statics),
3843
                                      VEC_length (tree, pending_statics));
3844
    }
3845
 
3846
  /* Generate hidden aliases for Java.  */
3847
  if (candidates)
3848
    {
3849
      build_java_method_aliases (candidates);
3850
      pointer_set_destroy (candidates);
3851
    }
3852
 
3853
  finish_repo ();
3854
 
3855
  /* The entire file is now complete.  If requested, dump everything
3856
     to a file.  */
3857
  {
3858
    int flags;
3859
    FILE *stream = dump_begin (TDI_tu, &flags);
3860
 
3861
    if (stream)
3862
      {
3863
        dump_node (global_namespace, flags & ~TDF_SLIM, stream);
3864
        dump_end (TDI_tu, stream);
3865
      }
3866
  }
3867
 
3868
  timevar_pop (TV_VARCONST);
3869
 
3870
  if (flag_detailed_statistics)
3871
    {
3872
      dump_tree_statistics ();
3873
      dump_time_statistics ();
3874
    }
3875
  input_location = locus;
3876
 
3877
#ifdef ENABLE_CHECKING
3878
  validate_conversion_obstack ();
3879
#endif /* ENABLE_CHECKING */
3880
}
3881
 
3882
/* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
3883
   function to call in parse-tree form; it has not yet been
3884
   semantically analyzed.  ARGS are the arguments to the function.
3885
   They have already been semantically analyzed.  This may change
3886
   ARGS.  */
3887
 
3888
tree
3889
build_offset_ref_call_from_tree (tree fn, VEC(tree,gc) **args)
3890
{
3891
  tree orig_fn;
3892
  VEC(tree,gc) *orig_args = NULL;
3893
  tree expr;
3894
  tree object;
3895
 
3896
  orig_fn = fn;
3897
  object = TREE_OPERAND (fn, 0);
3898
 
3899
  if (processing_template_decl)
3900
    {
3901
      gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
3902
                  || TREE_CODE (fn) == MEMBER_REF);
3903
      if (type_dependent_expression_p (fn)
3904
          || any_type_dependent_arguments_p (*args))
3905
        return build_nt_call_vec (fn, *args);
3906
 
3907
      orig_args = make_tree_vector_copy (*args);
3908
 
3909
      /* Transform the arguments and add the implicit "this"
3910
         parameter.  That must be done before the FN is transformed
3911
         because we depend on the form of FN.  */
3912
      make_args_non_dependent (*args);
3913
      object = build_non_dependent_expr (object);
3914
      if (TREE_CODE (fn) == DOTSTAR_EXPR)
3915
        object = cp_build_unary_op (ADDR_EXPR, object, 0, tf_warning_or_error);
3916
      VEC_safe_insert (tree, gc, *args, 0, object);
3917
      /* Now that the arguments are done, transform FN.  */
3918
      fn = build_non_dependent_expr (fn);
3919
    }
3920
 
3921
  /* A qualified name corresponding to a bound pointer-to-member is
3922
     represented as an OFFSET_REF:
3923
 
3924
        struct B { void g(); };
3925
        void (B::*p)();
3926
        void B::g() { (this->*p)(); }  */
3927
  if (TREE_CODE (fn) == OFFSET_REF)
3928
    {
3929
      tree object_addr = cp_build_unary_op (ADDR_EXPR, object, 0,
3930
                                         tf_warning_or_error);
3931
      fn = TREE_OPERAND (fn, 1);
3932
      fn = get_member_function_from_ptrfunc (&object_addr, fn);
3933
      VEC_safe_insert (tree, gc, *args, 0, object_addr);
3934
    }
3935
 
3936
  expr = cp_build_function_call_vec (fn, args, tf_warning_or_error);
3937
  if (processing_template_decl && expr != error_mark_node)
3938
    expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
3939
 
3940
  if (orig_args != NULL)
3941
    release_tree_vector (orig_args);
3942
 
3943
  return expr;
3944
}
3945
 
3946
 
3947
void
3948
check_default_args (tree x)
3949
{
3950
  tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
3951
  bool saw_def = false;
3952
  int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
3953
  for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
3954
    {
3955
      if (TREE_PURPOSE (arg))
3956
        saw_def = true;
3957
      else if (saw_def)
3958
        {
3959
          error ("default argument missing for parameter %P of %q+#D", i, x);
3960
          TREE_PURPOSE (arg) = error_mark_node;
3961
        }
3962
    }
3963
}
3964
 
3965
/* Return true if function DECL can be inlined.  This is used to force
3966
   instantiation of methods that might be interesting for inlining.  */
3967
bool
3968
possibly_inlined_p (tree decl)
3969
{
3970
  gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
3971
  if (DECL_UNINLINABLE (decl))
3972
    return false;
3973
  if (!optimize || pragma_java_exceptions)
3974
    return DECL_DECLARED_INLINE_P (decl);
3975
  /* When optimizing, we might inline everything when flatten
3976
     attribute or heuristics inlining for size or autoinlining
3977
     is used.  */
3978
  return true;
3979
}
3980
 
3981
/* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
3982
   If DECL is a specialization or implicitly declared class member,
3983
   generate the actual definition.  */
3984
 
3985
void
3986
mark_used (tree decl)
3987
{
3988
  HOST_WIDE_INT saved_processing_template_decl = 0;
3989
 
3990
  /* If DECL is a BASELINK for a single function, then treat it just
3991
     like the DECL for the function.  Otherwise, if the BASELINK is
3992
     for an overloaded function, we don't know which function was
3993
     actually used until after overload resolution.  */
3994
  if (TREE_CODE (decl) == BASELINK)
3995
    {
3996
      decl = BASELINK_FUNCTIONS (decl);
3997
      if (really_overloaded_fn (decl))
3998
        return;
3999
      decl = OVL_CURRENT (decl);
4000
    }
4001
 
4002
  /* Set TREE_USED for the benefit of -Wunused.  */
4003
  TREE_USED (decl) = 1;
4004
  if (DECL_CLONED_FUNCTION_P (decl))
4005
    TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4006
 
4007
  if (TREE_CODE (decl) == FUNCTION_DECL
4008
      && DECL_DELETED_FN (decl))
4009
    {
4010
      if (DECL_ARTIFICIAL (decl))
4011
        {
4012
          if (DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
4013
              && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
4014
            {
4015
              /* We mark a lambda conversion op as deleted if we can't
4016
                 generate it properly; see maybe_add_lambda_conv_op.  */
4017
              sorry ("converting lambda which uses %<...%> to "
4018
                     "function pointer");
4019
              return;
4020
            }
4021
        }
4022
      error ("deleted function %q+D", decl);
4023
      error ("used here");
4024
      return;
4025
    }
4026
  /* If we don't need a value, then we don't need to synthesize DECL.  */
4027
  if (cp_unevaluated_operand != 0)
4028
    return;
4029
 
4030
  /* We can only check DECL_ODR_USED on variables or functions with
4031
     DECL_LANG_SPECIFIC set, and these are also the only decls that we
4032
     might need special handling for.  */
4033
  if ((TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL)
4034
      || DECL_LANG_SPECIFIC (decl) == NULL
4035
      || DECL_THUNK_P (decl))
4036
    return;
4037
 
4038
  /* We only want to do this processing once.  We don't need to keep trying
4039
     to instantiate inline templates, because unit-at-a-time will make sure
4040
     we get them compiled before functions that want to inline them.  */
4041
  if (DECL_ODR_USED (decl))
4042
    return;
4043
 
4044
  /* If within finish_function, defer the rest until that function
4045
     finishes, otherwise it might recurse.  */
4046
  if (defer_mark_used_calls)
4047
    {
4048
      VEC_safe_push (tree, gc, deferred_mark_used_calls, decl);
4049
      return;
4050
    }
4051
 
4052
  /* Normally, we can wait until instantiation-time to synthesize
4053
     DECL.  However, if DECL is a static data member initialized with
4054
     a constant, we need the value right now because a reference to
4055
     such a data member is not value-dependent.  */
4056
  if (TREE_CODE (decl) == VAR_DECL
4057
      && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)
4058
      && DECL_CLASS_SCOPE_P (decl))
4059
    {
4060
      /* Don't try to instantiate members of dependent types.  We
4061
         cannot just use dependent_type_p here because this function
4062
         may be called from fold_non_dependent_expr, and then we may
4063
         see dependent types, even though processing_template_decl
4064
         will not be set.  */
4065
      if (CLASSTYPE_TEMPLATE_INFO ((DECL_CONTEXT (decl)))
4066
          && uses_template_parms (CLASSTYPE_TI_ARGS (DECL_CONTEXT (decl))))
4067
        return;
4068
      /* Pretend that we are not in a template, even if we are, so
4069
         that the static data member initializer will be processed.  */
4070
      saved_processing_template_decl = processing_template_decl;
4071
      processing_template_decl = 0;
4072
    }
4073
 
4074
  if (processing_template_decl)
4075
    return;
4076
 
4077
  DECL_ODR_USED (decl) = 1;
4078
  if (DECL_CLONED_FUNCTION_P (decl))
4079
    DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4080
 
4081
  /* DR 757: A type without linkage shall not be used as the type of a
4082
     variable or function with linkage, unless
4083
   o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
4084
   o the variable or function is not used (3.2 [basic.def.odr]) or is
4085
   defined in the same translation unit.  */
4086
  if (cxx_dialect > cxx98
4087
      && decl_linkage (decl) != lk_none
4088
      && !DECL_EXTERN_C_P (decl)
4089
      && !DECL_ARTIFICIAL (decl)
4090
      && !decl_defined_p (decl)
4091
      && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
4092
    {
4093
      if (is_local_extern (decl))
4094
        /* There's no way to define a local extern, and adding it to
4095
           the vector interferes with GC, so give an error now.  */
4096
        no_linkage_error (decl);
4097
      else
4098
        VEC_safe_push (tree, gc, no_linkage_decls, decl);
4099
    }
4100
 
4101
  if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
4102
      && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
4103
    /* Remember it, so we can check it was defined.  */
4104
    note_vague_linkage_fn (decl);
4105
 
4106
  /* Is it a synthesized method that needs to be synthesized?  */
4107
  if (TREE_CODE (decl) == FUNCTION_DECL
4108
      && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
4109
      && DECL_DEFAULTED_FN (decl)
4110
      && ! DECL_INITIAL (decl))
4111
    {
4112
      /* Remember the current location for a function we will end up
4113
         synthesizing.  Then we can inform the user where it was
4114
         required in the case of error.  */
4115
      DECL_SOURCE_LOCATION (decl) = input_location;
4116
 
4117
      /* Synthesizing an implicitly defined member function will result in
4118
         garbage collection.  We must treat this situation as if we were
4119
         within the body of a function so as to avoid collecting live data
4120
         on the stack (such as overload resolution candidates).
4121
 
4122
         We could just let cp_write_global_declarations handle synthesizing
4123
         this function, since we just added it to deferred_fns, but doing
4124
         it at the use site produces better error messages.  */
4125
      ++function_depth;
4126
      synthesize_method (decl);
4127
      --function_depth;
4128
      /* If this is a synthesized method we don't need to
4129
         do the instantiation test below.  */
4130
    }
4131
  else if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
4132
           && DECL_TEMPLATE_INFO (decl)
4133
           && (!DECL_EXPLICIT_INSTANTIATION (decl)
4134
               || always_instantiate_p (decl)))
4135
    /* If this is a function or variable that is an instance of some
4136
       template, we now know that we will need to actually do the
4137
       instantiation. We check that DECL is not an explicit
4138
       instantiation because that is not checked in instantiate_decl.
4139
 
4140
       We put off instantiating functions in order to improve compile
4141
       times.  Maintaining a stack of active functions is expensive,
4142
       and the inliner knows to instantiate any functions it might
4143
       need.  Therefore, we always try to defer instantiation.  */
4144
    instantiate_decl (decl, /*defer_ok=*/true,
4145
                      /*expl_inst_class_mem_p=*/false);
4146
 
4147
  processing_template_decl = saved_processing_template_decl;
4148
}
4149
 
4150
#include "gt-cp-decl2.h"

powered by: WebSVN 2.1.0

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