]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/decl2.c
C++: more location wrapper nodes (PR c++/43064, PR c++/43486)
[thirdparty/gcc.git] / gcc / cp / decl2.c
CommitLineData
af86cc06 1/* Process declarations and variables for C++ compiler.
8e8f6434 2 Copyright (C) 1988-2018 Free Software Foundation, Inc.
471086d6 3 Hacked by Michael Tiemann (tiemann@cygnus.com)
4
510c8531 5This file is part of GCC.
471086d6 6
510c8531 7GCC is free software; you can redistribute it and/or modify
471086d6 8it under the terms of the GNU General Public License as published by
aa139c3f 9the Free Software Foundation; either version 3, or (at your option)
471086d6 10any later version.
11
510c8531 12GCC is distributed in the hope that it will be useful,
471086d6 13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
aa139c3f 18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
471086d6 20
21
510c8531 22/* Process declarations and symbol lookup for C++ front end.
471086d6 23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
25
26/* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
28
29#include "config.h"
b3ef7553 30#include "system.h"
805e22b2 31#include "coretypes.h"
ad7b10a2 32#include "memmodel.h"
4cba6f60 33#include "target.h"
4cba6f60 34#include "cp-tree.h"
35#include "c-family/c-common.h"
36#include "timevar.h"
9ed99284 37#include "stringpool.h"
4cba6f60 38#include "cgraph.h"
9ed99284 39#include "varasm.h"
40#include "attribs.h"
41#include "stor-layout.h"
42#include "calls.h"
471086d6 43#include "decl.h"
2a4e40b0 44#include "toplev.h"
6c536c4f 45#include "c-family/c-objc.h"
7bedc3a0 46#include "c-family/c-pragma.h"
b9ed1410 47#include "dumpfile.h"
3cf8b391 48#include "intl.h"
7bedc3a0 49#include "c-family/c-ada-spec.h"
085f6ebf 50#include "asan.h"
4ee9c684 51
88df6264 52/* Id for dumping the raw trees. */
53int raw_dump_id;
54
9751c00e 55extern cpp_reader *parse_in;
f65ee484 56
cec1f6a6 57/* This structure contains information about the initializations
58 and/or destructions required for a particular priority level. */
59typedef struct priority_info_s {
3160db1d 60 /* Nonzero if there have been any initializations at this priority
5c8ddb8a 61 throughout the translation unit. */
62 int initializations_p;
3160db1d 63 /* Nonzero if there have been any destructions at this priority
5c8ddb8a 64 throughout the translation unit. */
65 int destructions_p;
cec1f6a6 66} *priority_info;
67
26ac6687 68static void mark_vtable_entries (tree);
26ac6687 69static bool maybe_emit_vtables (tree);
26ac6687 70static tree start_objects (int, int);
71static void finish_objects (int, int, tree);
911483bc 72static tree start_static_storage_duration_function (unsigned);
26ac6687 73static void finish_static_storage_duration_function (tree);
74static priority_info get_priority_info (int);
a9f11a62 75static void do_static_initialization_or_destruction (tree, bool);
76static void one_static_initialization_or_destruction (tree, tree, bool);
911483bc 77static void generate_ctor_or_dtor_function (bool, int, location_t *);
26ac6687 78static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
653e5405 79 void *);
cd4dc755 80static tree prune_vars_needing_no_initialization (tree *);
26ac6687 81static void write_out_vars (tree);
82static void import_export_class (tree);
26ac6687 83static tree get_guard_bits (tree);
799435d8 84static void determine_visibility_from_class (tree, tree);
66f24c41 85static bool determine_hidden_inline (tree);
068632d3 86static void maybe_instantiate_decl (tree);
3f131089 87
471086d6 88/* A list of static class variables. This is needed, because a
89 static class variable can be declared inside the class without
aa977dcc 90 an initializer, and then initialized, statically, outside the class. */
f1f41a6c 91static GTY(()) vec<tree, va_gc> *pending_statics;
471086d6 92
0543e7a9 93/* A list of functions which were declared inline, but which we
96624a9e 94 may need to emit outline anyway. */
f1f41a6c 95static GTY(()) vec<tree, va_gc> *deferred_fns;
471086d6 96
74d6b34b 97/* A list of decls that use types with no linkage, which we need to make
98 sure are defined. */
f1f41a6c 99static GTY(()) vec<tree, va_gc> *no_linkage_decls;
74d6b34b 100
6e9c0030 101/* A vector of alternating decls and identifiers, where the latter
102 is to be an alias for the former if the former is defined. */
103static GTY(()) vec<tree, va_gc> *mangling_aliases;
104
6ff24f58 105/* hash traits for declarations. Hashes single decls via
971295a0 106 DECL_ASSEMBLER_NAME_RAW. */
6ff24f58 107
108struct mangled_decl_hash : ggc_remove <tree>
109{
110 typedef tree value_type; /* A DECL. */
111 typedef tree compare_type; /* An identifier. */
112
113 static hashval_t hash (const value_type decl)
114 {
971295a0 115 return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME_RAW (decl));
6ff24f58 116 }
117 static bool equal (const value_type existing, compare_type candidate)
118 {
971295a0 119 tree name = DECL_ASSEMBLER_NAME_RAW (existing);
6ff24f58 120 return candidate == name;
121 }
122
123 static inline void mark_empty (value_type &p) {p = NULL_TREE;}
124 static inline bool is_empty (value_type p) {return !p;}
125
7c129b68 126 static bool is_deleted (value_type e)
127 {
128 return e == reinterpret_cast <value_type> (1);
129 }
130 static void mark_deleted (value_type &e)
131 {
132 e = reinterpret_cast <value_type> (1);
133 }
6ff24f58 134};
135
136/* A hash table of decls keyed by mangled name. Used to figure out if
137 we need compatibility aliases. */
138static GTY(()) hash_table<mangled_decl_hash> *mangled_decls;
e845448c 139
e857e9c7 140/* Nonzero if we're done parsing and into end-of-file activities. */
141
142int at_eof;
d3d81eb2 143
144/* True if note_mangling_alias should enqueue mangling aliases for
145 later generation, rather than emitting them right away. */
146
147bool defer_mangling_aliases = true;
471086d6 148\f
7873fcd6 149
150/* Return a member function type (a METHOD_TYPE), given FNTYPE (a
151 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
152 that apply to the function). */
153
154tree
e116411c 155build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
156 cp_ref_qualifier rqual)
471086d6 157{
d0a0a946 158 if (fntype == error_mark_node || ctype == error_mark_node)
159 return error_mark_node;
160
897d42c2 161 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
162 || TREE_CODE (fntype) == METHOD_TYPE);
163
e5122931 164 cp_cv_quals type_quals = quals & ~TYPE_QUAL_RESTRICT;
3e04bd45 165 ctype = cp_build_qualified_type (ctype, type_quals);
e5122931 166
167 tree newtype
168 = build_method_type_directly (ctype, TREE_TYPE (fntype),
169 (TREE_CODE (fntype) == METHOD_TYPE
170 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
171 : TYPE_ARG_TYPES (fntype)));
172 if (tree attrs = TYPE_ATTRIBUTES (fntype))
173 newtype = cp_build_type_attribute_variant (newtype, attrs);
174 newtype = build_cp_fntype_variant (newtype, rqual,
175 TYPE_RAISES_EXCEPTIONS (fntype),
176 TYPE_HAS_LATE_RETURN_TYPE (fntype));
177
178 return newtype;
471086d6 179}
180
93529b28 181/* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
182 return type changed to NEW_RET. */
183
184tree
185change_return_type (tree new_ret, tree fntype)
186{
86359a65 187 if (new_ret == error_mark_node)
188 return fntype;
189
93529b28 190 if (same_type_p (new_ret, TREE_TYPE (fntype)))
191 return fntype;
192
e5122931 193 tree newtype;
194 tree args = TYPE_ARG_TYPES (fntype);
195
93529b28 196 if (TREE_CODE (fntype) == FUNCTION_TYPE)
2ec48521 197 {
198 newtype = build_function_type (new_ret, args);
e116411c 199 newtype = apply_memfn_quals (newtype,
e5122931 200 type_memfn_quals (fntype));
2ec48521 201 }
93529b28 202 else
43936711 203 newtype = build_method_type_directly
a8d966c1 204 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
e5122931 205
206 if (tree attrs = TYPE_ATTRIBUTES (fntype))
93529b28 207 newtype = cp_build_type_attribute_variant (newtype, attrs);
e5122931 208 newtype = cxx_copy_lang_qualifiers (newtype, fntype);
93529b28 209
210 return newtype;
211}
212
aa3ab9ba 213/* Build a PARM_DECL of FN with NAME and TYPE, and set DECL_ARG_TYPE
d145d8d5 214 appropriately. */
215
216tree
aa3ab9ba 217cp_build_parm_decl (tree fn, tree name, tree type)
d145d8d5 218{
e60a6f7b 219 tree parm = build_decl (input_location,
220 PARM_DECL, name, type);
aa3ab9ba 221 DECL_CONTEXT (parm) = fn;
222
8172be22 223 /* DECL_ARG_TYPE is only used by the back end and the back end never
224 sees templates. */
225 if (!processing_template_decl)
226 DECL_ARG_TYPE (parm) = type_passed_as (type);
d95d815d 227
d145d8d5 228 return parm;
229}
230
aa3ab9ba 231/* Returns a PARM_DECL of FN for a parameter of the indicated TYPE, with the
0ce25b06 232 indicated NAME. */
233
7873fcd6 234tree
aa3ab9ba 235build_artificial_parm (tree fn, tree name, tree type)
0ce25b06 236{
aa3ab9ba 237 tree parm = cp_build_parm_decl (fn, name, type);
ecdd3d84 238 DECL_ARTIFICIAL (parm) = 1;
bdea0415 239 /* All our artificial parms are implicitly `const'; they cannot be
240 assigned to. */
241 TREE_READONLY (parm) = 1;
0ce25b06 242 return parm;
243}
244
e46cb663 245/* Constructors for types with virtual baseclasses need an "in-charge" flag
246 saying whether this constructor is responsible for initialization of
247 virtual baseclasses or not. All destructors also need this "in-charge"
248 flag, which additionally determines whether or not the destructor should
249 free the memory for the object.
250
251 This function adds the "in-charge" flag to member function FN if
252 appropriate. It is called from grokclassfn and tsubst.
dcbeb3ef 253 FN must be either a constructor or destructor.
254
255 The in-charge flag follows the 'this' parameter, and is followed by the
256 VTT parm (if any), then the user-written parms. */
e46cb663 257
258void
26ac6687 259maybe_retrofit_in_chrg (tree fn)
e46cb663 260{
261 tree basetype, arg_types, parms, parm, fntype;
262
a23287c6 263 /* If we've already add the in-charge parameter don't do it again. */
264 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
e46cb663 265 return;
266
e4d39a4e 267 /* When processing templates we can't know, in general, whether or
268 not we're going to have virtual baseclasses. */
7f233616 269 if (processing_template_decl)
e4d39a4e 270 return;
271
a23287c6 272 /* We don't need an in-charge parameter for constructors that don't
273 have virtual bases. */
274 if (DECL_CONSTRUCTOR_P (fn)
1f0b839e 275 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
a23287c6 276 return;
e46cb663 277
e46cb663 278 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
279 basetype = TREE_TYPE (TREE_VALUE (arg_types));
dcbeb3ef 280 arg_types = TREE_CHAIN (arg_types);
281
1767a056 282 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
dcbeb3ef 283
284 /* If this is a subobject constructor or destructor, our caller will
285 pass us a pointer to our VTT. */
1f0b839e 286 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
dcbeb3ef 287 {
aa3ab9ba 288 parm = build_artificial_parm (fn, vtt_parm_identifier, vtt_parm_type);
dcbeb3ef 289
290 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
1767a056 291 DECL_CHAIN (parm) = parms;
dcbeb3ef 292 parms = parm;
293
294 /* ...and then to TYPE_ARG_TYPES. */
295 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
296
297 DECL_HAS_VTT_PARM_P (fn) = 1;
298 }
299
300 /* Then add the in-charge parm (before the VTT parm). */
aa3ab9ba 301 parm = build_artificial_parm (fn, in_charge_identifier, integer_type_node);
1767a056 302 DECL_CHAIN (parm) = parms;
dcbeb3ef 303 parms = parm;
304 arg_types = hash_tree_chain (integer_type_node, arg_types);
305
306 /* Insert our new parameter(s) into the list. */
1767a056 307 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
dcbeb3ef 308
309 /* And rebuild the function type. */
5bfb0742 310 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
311 arg_types);
897d42c2 312 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
313 fntype = (cp_build_type_attribute_variant
314 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
e5122931 315 fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (fn));
e46cb663 316 TREE_TYPE (fn) = fntype;
a23287c6 317
318 /* Now we've got the in-charge parameter. */
319 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
e46cb663 320}
321
471086d6 322/* Classes overload their constituent function names automatically.
323 When a function name is declared in a record structure,
324 its name is changed to it overloaded name. Since names for
325 constructors and destructors can conflict, we place a leading
326 '$' for destructors.
327
328 CNAME is the name of the class we are grokking for.
329
330 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
331
332 FLAGS contains bits saying what's special about today's
d6eb041a 333 arguments. DTOR_FLAG == DESTRUCTOR.
471086d6 334
335 If FUNCTION is a destructor, then we must add the `auto-delete' field
336 as a second parameter. There is some hair associated with the fact
337 that we must "declare" this variable in the manner consistent with the
338 way the rest of the arguments were declared.
339
340 QUALS are the qualifiers for the this pointer. */
341
342void
7873fcd6 343grokclassfn (tree ctype, tree function, enum overload_flags flags)
471086d6 344{
345 tree fn_name = DECL_NAME (function);
471086d6 346
4d4e6d4b 347 /* Even within an `extern "C"' block, members get C++ linkage. See
348 [dcl.link] for details. */
4b1984f5 349 SET_DECL_LANGUAGE (function, lang_cplusplus);
4d4e6d4b 350
471086d6 351 if (fn_name == NULL_TREE)
352 {
905d4035 353 error ("name missing for member function");
471086d6 354 fn_name = get_identifier ("<anonymous>");
355 DECL_NAME (function) = fn_name;
356 }
357
e46cb663 358 DECL_CONTEXT (function) = ctype;
e46cb663 359
7e64c604 360 if (flags == DTOR_FLAG)
c0a09bf1 361 DECL_CXX_DESTRUCTOR_P (function) = 1;
7e64c604 362
e46cb663 363 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
6795ece9 364 maybe_retrofit_in_chrg (function);
471086d6 365}
366
471086d6 367/* Create an ARRAY_REF, checking for the user doing things backwards
60427596 368 along the way. DECLTYPE_P is for N3276, as in the parser. */
96624a9e 369
471086d6 370tree
60427596 371grok_array_decl (location_t loc, tree array_expr, tree index_exp,
372 bool decltype_p)
471086d6 373{
13795292 374 tree type;
375 tree expr;
376 tree orig_array_expr = array_expr;
377 tree orig_index_exp = index_exp;
d175f019 378 tree overload = NULL_TREE;
471086d6 379
13795292 380 if (error_operand_p (array_expr) || error_operand_p (index_exp))
471086d6 381 return error_mark_node;
13795292 382
3cc0b4b9 383 if (processing_template_decl)
13795292 384 {
385 if (type_dependent_expression_p (array_expr)
386 || type_dependent_expression_p (index_exp))
255b5d15 387 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
388 NULL_TREE, NULL_TREE);
13795292 389 array_expr = build_non_dependent_expr (array_expr);
390 index_exp = build_non_dependent_expr (index_exp);
391 }
e857e9c7 392
13795292 393 type = TREE_TYPE (array_expr);
b4df430b 394 gcc_assert (type);
ef4534a3 395 type = non_reference (type);
471086d6 396
397 /* If they have an `operator[]', use that. */
95397ff9 398 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
60427596 399 {
400 tsubst_flags_t complain = tf_warning_or_error;
401 if (decltype_p)
402 complain |= tf_decltype;
403 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
d175f019 404 index_exp, NULL_TREE, &overload, complain);
60427596 405 }
1e66592c 406 else
13795292 407 {
408 tree p1, p2, i1, i2;
471086d6 409
13795292 410 /* Otherwise, create an ARRAY_REF for a pointer or array type.
411 It is a little-known fact that, if `a' is an array and `i' is
412 an int, you can write `i[a]', which means the same thing as
413 `a[i]'. */
76249021 414 if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
13795292 415 p1 = array_expr;
416 else
417 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
418
419 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
420 p2 = index_exp;
421 else
422 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
471086d6 423
9031d10b 424 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
13795292 425 false);
9031d10b 426 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
13795292 427 false);
471086d6 428
13795292 429 if ((p1 && i2) && (i1 && p2))
430 error ("ambiguous conversion for array subscript");
471086d6 431
13795292 432 if (p1 && i2)
433 array_expr = p1, index_exp = i2;
434 else if (i1 && p2)
435 array_expr = p2, index_exp = i1;
436 else
437 {
3ce479a6 438 error ("invalid types %<%T[%T]%> for array subscript",
653e5405 439 type, TREE_TYPE (index_exp));
13795292 440 return error_mark_node;
441 }
1e66592c 442
13795292 443 if (array_expr == error_mark_node || index_exp == error_mark_node)
444 error ("ambiguous conversion for array subscript");
1e66592c 445
90ad495b 446 if (TYPE_PTR_P (TREE_TYPE (array_expr)))
4cedc476 447 array_expr = mark_rvalue_use (array_expr);
448 else
449 array_expr = mark_lvalue_use_nonread (array_expr);
450 index_exp = mark_rvalue_use (index_exp);
1d291991 451 expr = build_array_ref (input_location, array_expr, index_exp);
13795292 452 }
453 if (processing_template_decl && expr != error_mark_node)
d175f019 454 {
455 if (overload != NULL_TREE)
456 return (build_min_non_dep_op_overload
457 (ARRAY_REF, expr, overload, orig_array_expr, orig_index_exp));
458
459 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
460 NULL_TREE, NULL_TREE);
461 }
13795292 462 return expr;
471086d6 463}
464
465/* Given the cast expression EXP, checking out its validity. Either return
466 an error_mark_node if there was an unavoidable error, return a cast to
467 void for trying to delete a pointer w/ the value 0, or return the
b4ce9ded 468 call to delete. If DOING_VEC is true, we handle things differently
469 for doing an array delete.
471086d6 470 Implements ARM $5.3.4. This is called from the parser. */
96624a9e 471
471086d6 472tree
9e505437 473delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
474 tsubst_flags_t complain)
471086d6 475{
bdd152ce 476 tree t, type;
e857e9c7 477
4742975e 478 if (exp == error_mark_node)
479 return exp;
480
3cc0b4b9 481 if (processing_template_decl)
e857e9c7 482 {
483 t = build_min (DELETE_EXPR, void_type_node, exp, size);
484 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
485 DELETE_EXPR_USE_VEC (t) = doing_vec;
a133b234 486 TREE_SIDE_EFFECTS (t) = 1;
e857e9c7 487 return t;
488 }
489
b4ce9ded 490 /* An array can't have been allocated by new, so complain. */
5a8ef569 491 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
492 warning (0, "deleting array %q#E", exp);
b4ce9ded 493
95873270 494 t = build_expr_type_conversion (WANT_POINTER, exp, true);
471086d6 495
bdd152ce 496 if (t == NULL_TREE || t == error_mark_node)
471086d6 497 {
3ce479a6 498 error ("type %q#T argument given to %<delete%>, expected pointer",
653e5405 499 TREE_TYPE (exp));
bdd152ce 500 return error_mark_node;
471086d6 501 }
502
bdd152ce 503 type = TREE_TYPE (t);
504
505 /* As of Valley Forge, you can delete a pointer to const. */
506
507 /* You can't delete functions. */
508 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
0543e7a9 509 {
3ce479a6 510 error ("cannot delete a function. Only pointer-to-objects are "
653e5405 511 "valid arguments to %<delete%>");
bdd152ce 512 return error_mark_node;
0543e7a9 513 }
bdd152ce 514
3160db1d 515 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
c21c015b 516 if (VOID_TYPE_P (TREE_TYPE (type)))
b81be439 517 {
3b393a70 518 warning (OPT_Wdelete_incomplete, "deleting %qT is undefined", type);
b81be439 519 doing_vec = 0;
520 }
521
bdd152ce 522 /* Deleting a pointer with the value zero is valid and has no effect. */
523 if (integer_zerop (t))
524 return build1 (NOP_EXPR, void_type_node, t);
471086d6 525
526 if (doing_vec)
9031d10b 527 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
b4ce9ded 528 sfk_deleting_destructor,
9e505437 529 use_global_delete, complain);
471086d6 530 else
7203b0e1 531 return build_delete (type, t, sfk_deleting_destructor,
9e505437 532 LOOKUP_NORMAL, use_global_delete,
533 complain);
471086d6 534}
535
64b4f183 536/* Report an error if the indicated template declaration is not the
537 sort of thing that should be a member template. */
538
539void
26ac6687 540check_member_template (tree tmpl)
64b4f183 541{
542 tree decl;
543
b4df430b 544 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
64b4f183 545 decl = DECL_TEMPLATE_RESULT (tmpl);
546
25300607 547 if (TREE_CODE (decl) == FUNCTION_DECL
6ffe24ee 548 || DECL_ALIAS_TEMPLATE_P (tmpl)
25300607 549 || (TREE_CODE (decl) == TYPE_DECL
95397ff9 550 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
64b4f183 551 {
814b90ef 552 /* The parser rejects template declarations in local classes
553 (with the exception of generic lambdas). */
554 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
5f69415d 555 /* The parser rejects any use of virtual in a function template. */
556 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
557 && DECL_VIRTUAL_P (decl)));
64b4f183 558
559 /* The debug-information generating code doesn't know what to do
9031d10b 560 with member templates. */
64b4f183 561 DECL_IGNORED_P (tmpl) = 1;
9031d10b 562 }
30afdfe9 563 else if (variable_template_p (tmpl))
564 /* OK */;
64b4f183 565 else
3ce479a6 566 error ("template declaration of %q#D", decl);
64b4f183 567}
568
471086d6 569/* Sanity check: report error if this function FUNCTION is not
570 really a member of the class (CTYPE) it is supposed to belong to.
4a44ba29 571 TEMPLATE_PARMS is used to specify the template parameters of a member
9031d10b 572 template passed as FUNCTION_DECL. If the member template is passed as a
0122f52b 573 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
574 from the declaration. If the function is not a function template, it
575 must be NULL.
e5252ce9 576 It returns the original declaration for the function, NULL_TREE if
577 no declaration was found, error_mark_node if an error was emitted. */
471086d6 578
ce28ee2e 579tree
0122f52b 580check_classfn (tree ctype, tree function, tree template_parms)
471086d6 581{
88e449ea 582 if (DECL_USE_TEMPLATE (function)
3467e461 583 && !(TREE_CODE (function) == TEMPLATE_DECL
584 && DECL_TEMPLATE_SPECIALIZATION (function))
38d89ee9 585 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
88e449ea 586 /* Since this is a specialization of a member template,
587 we're not going to find the declaration in the class.
588 For example, in:
9031d10b 589
653e5405 590 struct S { template <typename T> void f(T); };
591 template <> void S::f(int);
9031d10b 592
88e449ea 593 we're not going to find `S::f(int)', but there's no
594 reason we should, either. We let our callers know we didn't
595 find the method, but we don't complain. */
596 return NULL_TREE;
a81e6834 597
0122f52b 598 /* Basic sanity check: for a template function, the template parameters
599 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
600 if (TREE_CODE (function) == TEMPLATE_DECL)
601 {
df509dce 602 if (template_parms
603 && !comp_template_parms (template_parms,
604 DECL_TEMPLATE_PARMS (function)))
605 {
bf776685 606 error ("template parameter lists provided don%'t match the "
df509dce 607 "template parameters of %qD", function);
608 return error_mark_node;
609 }
0122f52b 610 template_parms = DECL_TEMPLATE_PARMS (function);
611 }
612
3417beef 613 /* OK, is this a definition of a member template? */
5a6b88f6 614 bool is_template = (template_parms != NULL_TREE);
3417beef 615
dbbbd81b 616 /* [temp.mem]
617
618 A destructor shall not be a member template. */
619 if (DECL_DESTRUCTOR_P (function) && is_template)
620 {
621 error ("destructor %qD declared as member template", function);
622 return error_mark_node;
623 }
624
e2423a3d 625 /* We must enter the scope here, because conversion operators are
626 named by target type, and type equivalence relies on typenames
627 resolving within the scope of CTYPE. */
5a6b88f6 628 tree pushed_scope = push_scope (ctype);
629 tree matched = NULL_TREE;
e12c5305 630 tree fns = get_class_binding (ctype, DECL_NAME (function));
5a6b88f6 631
632 for (ovl_iterator iter (fns); !matched && iter; ++iter)
471086d6 633 {
5a6b88f6 634 tree fndecl = *iter;
4d26487b 635
636 /* A member template definition only matches a member template
637 declaration. */
638 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
639 continue;
640
641 if (!DECL_DECLARES_FUNCTION_P (fndecl))
642 continue;
643
5a6b88f6 644 tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
645 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
646
647 /* We cannot simply call decls_match because this doesn't work
648 for static member functions that are pretending to be
649 methods, and because the name may have been changed by
650 asm("new_name"). */
651
652 /* Get rid of the this parameter on functions that become
653 static. */
654 if (DECL_STATIC_FUNCTION_P (fndecl)
655 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
656 p1 = TREE_CHAIN (p1);
657
5a6b88f6 658 /* ref-qualifier or absence of same must match. */
659 if (type_memfn_rqual (TREE_TYPE (function))
660 != type_memfn_rqual (TREE_TYPE (fndecl)))
661 continue;
662
663 // Include constraints in the match.
664 tree c1 = get_constraints (function);
665 tree c2 = get_constraints (fndecl);
666
667 /* While finding a match, same types and params are not enough
668 if the function is versioned. Also check version ("target")
669 attributes. */
670 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
671 TREE_TYPE (TREE_TYPE (fndecl)))
672 && compparms (p1, p2)
673 && !targetm.target_option.function_versions (function, fndecl)
674 && (!is_template
675 || comp_template_parms (template_parms,
676 DECL_TEMPLATE_PARMS (fndecl)))
677 && equivalent_constraints (c1, c2)
678 && (DECL_TEMPLATE_SPECIALIZATION (function)
679 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
680 && (!DECL_TEMPLATE_SPECIALIZATION (function)
681 || (DECL_TI_TEMPLATE (function) == DECL_TI_TEMPLATE (fndecl))))
682 matched = fndecl;
683 }
a81e6834 684
5a6b88f6 685 if (!matched)
686 {
687 if (!COMPLETE_TYPE_P (ctype))
688 cxx_incomplete_type_error (function, ctype);
689 else
05b229bf 690 {
5a6b88f6 691 if (DECL_CONV_FN_P (function))
e12c5305 692 fns = get_class_binding (ctype, conv_op_identifier);
5a6b88f6 693
694 error_at (DECL_SOURCE_LOCATION (function),
695 "no declaration matches %q#D", function);
696 if (fns)
697 print_candidates (fns);
698 else if (DECL_CONV_FN_P (function))
699 inform (DECL_SOURCE_LOCATION (function),
700 "no conversion operators declared");
701 else
702 inform (DECL_SOURCE_LOCATION (function),
703 "no functions named %qD", function);
704 inform (DECL_SOURCE_LOCATION (TYPE_NAME (ctype)),
705 "%#qT defined here", ctype);
471086d6 706 }
5a6b88f6 707 matched = error_mark_node;
471086d6 708 }
471086d6 709
e2423a3d 710 if (pushed_scope)
711 pop_scope (pushed_scope);
5a6b88f6 712
713 return matched;
471086d6 714}
715
caa6fdce 716/* DECL is a function with vague linkage. Remember it so that at the
717 end of the translation unit we can decide whether or not to emit
718 it. */
719
720void
721note_vague_linkage_fn (tree decl)
722{
2a1efcf5 723 if (processing_template_decl)
724 return;
725
27ccbdd9 726 DECL_DEFER_OUTPUT (decl) = 1;
f1f41a6c 727 vec_safe_push (deferred_fns, decl);
caa6fdce 728}
729
8b2ddcd8 730/* As above, but for variable template instantiations. */
731
732void
733note_variable_template_instantiation (tree decl)
734{
735 vec_safe_push (pending_statics, decl);
736}
737
2789f91b 738/* We have just processed the DECL, which is a static data member.
d91303a6 739 The other parameters are as for cp_finish_decl. */
2789f91b 740
741void
074ab442 742finish_static_data_member_decl (tree decl,
d91303a6 743 tree init, bool init_const_expr_p,
744 tree asmspec_tree,
653e5405 745 int flags)
2789f91b 746{
98eaf693 747 DECL_CONTEXT (decl) = current_class_type;
748
2789f91b 749 /* We cannot call pushdecl here, because that would fill in the
b53db2b0 750 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
2789f91b 751 the right thing, namely, to put this decl out straight away. */
7e64c604 752
f6771537 753 if (! processing_template_decl)
f1f41a6c 754 vec_safe_push (pending_statics, decl);
cec1f6a6 755
97858640 756 if (LOCAL_CLASS_P (current_class_type)
757 /* We already complained about the template definition. */
758 && !DECL_TEMPLATE_INSTANTIATION (decl))
00e113ad 759 permerror (DECL_SOURCE_LOCATION (decl),
760 "local class %q#T shall not have static data member %q#D",
561fec9d 761 current_class_type, decl);
46e37d59 762 else
763 for (tree t = current_class_type; TYPE_P (t);
764 t = CP_TYPE_CONTEXT (t))
4f86cbb0 765 if (TYPE_UNNAMED_P (t))
46e37d59 766 {
bc35ef65 767 auto_diagnostic_group d;
46e37d59 768 if (permerror (DECL_SOURCE_LOCATION (decl),
769 "static data member %qD in unnamed class", decl))
770 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
771 "unnamed class defined here");
772 break;
773 }
f1c6ad28 774
2789f91b 775 DECL_IN_AGGR_P (decl) = 1;
2789f91b 776
8b25863e 777 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
778 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
779 SET_VAR_HAD_UNKNOWN_BOUND (decl);
780
44de00a1 781 if (init)
782 {
783 /* Similarly to start_decl_1, we want to complete the type in order
784 to do the right thing in cp_apply_type_quals_to_decl, possibly
785 clear TYPE_QUAL_CONST (c++/65579). */
786 tree type = TREE_TYPE (decl) = complete_type (TREE_TYPE (decl));
787 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
788 }
789
d91303a6 790 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
2789f91b 791}
792
3b49899c 793/* DECLARATOR and DECLSPECS correspond to a class member. The other
d91303a6 794 parameters are as for cp_finish_decl. Return the DECL for the
074ab442 795 class member declared. */
471086d6 796
797tree
9031d10b 798grokfield (const cp_declarator *declarator,
799 cp_decl_specifier_seq *declspecs,
d91303a6 800 tree init, bool init_const_expr_p,
801 tree asmspec_tree,
653e5405 802 tree attrlist)
471086d6 803{
9581e01c 804 tree value;
023b3db6 805 const char *asmspec = 0;
d9c249a4 806 int flags;
471086d6 807
471086d6 808 if (init
809 && TREE_CODE (init) == TREE_LIST
810 && TREE_VALUE (init) == error_mark_node
811 && TREE_CHAIN (init) == NULL_TREE)
fff5e605 812 init = NULL_TREE;
471086d6 813
e3c541f0 814 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
53b567c1 815 if (! value || value == error_mark_node)
76c1ba40 816 /* friend or constructor went bad. */
abbfc22d 817 return error_mark_node;
53b567c1 818 if (TREE_TYPE (value) == error_mark_node)
819 return value;
abbfc22d 820
821 if (TREE_CODE (value) == TYPE_DECL && init)
822 {
67484828 823 error ("typedef %qD is initialized (use decltype instead)", value);
abbfc22d 824 init = NULL_TREE;
825 }
471086d6 826
827 /* Pass friendly classes back. */
69b6679c 828 if (value == void_type_node)
829 return value;
830
236cf4da 831 if (DECL_NAME (value)
832 && TREE_CODE (DECL_NAME (value)) == TEMPLATE_ID_EXPR)
b05fa836 833 {
236cf4da 834 error_at (declarator->id_loc,
835 "explicit template argument list not allowed");
836 return error_mark_node;
b05fa836 837 }
471086d6 838
839 /* Stash away type declarations. */
840 if (TREE_CODE (value) == TYPE_DECL)
841 {
842 DECL_NONLOCAL (value) = 1;
94f3b32d 843 DECL_CONTEXT (value) = current_class_type;
05910319 844
4e16cce3 845 if (attrlist)
4bcbb683 846 {
847 int attrflags = 0;
848
849 /* If this is a typedef that names the class for linkage purposes
850 (7.1.3p8), apply any attributes directly to the type. */
5b97892c 851 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
4bcbb683 852 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
853 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
854
855 cplus_decl_attributes (&value, attrlist, attrflags);
856 }
4e16cce3 857
a60f3e81 858 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
75c0b6e7 859 && TREE_TYPE (value) != error_mark_node
860 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
0d432ee0 861 set_underlying_type (value);
41771881 862
370478b1 863 /* It's important that push_template_decl below follows
864 set_underlying_type above so that the created template
865 carries the properly set type of VALUE. */
866 if (processing_template_decl)
867 value = push_template_decl (value);
868
a4e3ffad 869 record_locally_defined_typedef (value);
471086d6 870 return value;
871 }
872
cbe90ad7 873 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
874
875 if (!friendp && DECL_IN_AGGR_P (value))
471086d6 876 {
3ce479a6 877 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
471086d6 878 return void_type_node;
879 }
880
32955d67 881 if (asmspec_tree && asmspec_tree != error_mark_node)
471086d6 882 asmspec = TREE_STRING_POINTER (asmspec_tree);
883
884 if (init)
885 {
771665d8 886 if (TREE_CODE (value) == FUNCTION_DECL)
471086d6 887 {
2336da2a 888 if (init == ridpointers[(int)RID_DELETE])
889 {
288b3981 890 if (friendp && decl_defined_p (value))
891 {
892 error ("redefinition of %q#D", value);
893 inform (DECL_SOURCE_LOCATION (value),
894 "%q#D previously defined here", value);
895 }
896 else
897 {
898 DECL_DELETED_FN (value) = 1;
899 DECL_DECLARED_INLINE_P (value) = 1;
900 DECL_INITIAL (value) = error_mark_node;
901 }
2336da2a 902 }
903 else if (init == ridpointers[(int)RID_DEFAULT])
904 {
e8c9f615 905 if (defaultable_fn_check (value))
2336da2a 906 {
907 DECL_DEFAULTED_FN (value) = 1;
908 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
909 DECL_DECLARED_INLINE_P (value) = 1;
2336da2a 910 }
911 }
3ab40fb9 912 else if (TREE_CODE (init) == DEFAULT_ARG)
913 error ("invalid initializer for member function %qD", value);
2336da2a 914 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
5507d911 915 {
819f4a51 916 if (integer_zerop (init))
917 DECL_PURE_VIRTUAL_P (value) = 1;
918 else if (error_operand_p (init))
919 ; /* An error has already been reported. */
920 else
921 error ("invalid initializer for member function %qD",
922 value);
5507d911 923 }
924 else
925 {
926 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
cbe90ad7 927 if (friendp)
928 error ("initializer specified for friend function %qD",
929 value);
930 else
931 error ("initializer specified for static member function %qD",
932 value);
5507d911 933 }
471086d6 934 }
d9c249a4 935 else if (TREE_CODE (value) == FIELD_DECL)
936 /* C++11 NSDMI, keep going. */;
80a58eb0 937 else if (!VAR_P (value))
d9c249a4 938 gcc_unreachable ();
471086d6 939 }
940
cbe90ad7 941 /* Pass friend decls back. */
942 if ((TREE_CODE (value) == FUNCTION_DECL
943 || TREE_CODE (value) == TEMPLATE_DECL)
944 && DECL_CONTEXT (value) != current_class_type)
945 return value;
946
bfceaf54 947 /* Need to set this before push_template_decl. */
f4ae4202 948 if (VAR_P (value))
bfceaf54 949 DECL_CONTEXT (value) = current_class_type;
950
4cace8cb 951 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
fe338aca 952 {
953 value = push_template_decl (value);
954 if (error_operand_p (value))
955 return error_mark_node;
956 }
e857e9c7 957
e178a5a9 958 if (attrlist)
e3c541f0 959 cplus_decl_attributes (&value, attrlist, 0);
e178a5a9 960
8e8713cd 961 if (init && DIRECT_LIST_INIT_P (init))
d9c249a4 962 flags = LOOKUP_NORMAL;
963 else
964 flags = LOOKUP_IMPLICIT;
965
092b1d6f 966 switch (TREE_CODE (value))
471086d6 967 {
092b1d6f 968 case VAR_DECL:
d91303a6 969 finish_static_data_member_decl (value, init, init_const_expr_p,
970 asmspec_tree, flags);
471086d6 971 return value;
092b1d6f 972
973 case FIELD_DECL:
471086d6 974 if (asmspec)
1e5fcbe2 975 error ("%<asm%> specifiers are not permitted on non-static data members");
471086d6 976 if (DECL_INITIAL (value) == error_mark_node)
977 init = error_mark_node;
074ab442 978 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
d91303a6 979 NULL_TREE, flags);
471086d6 980 DECL_IN_AGGR_P (value) = 1;
981 return value;
092b1d6f 982
983 case FUNCTION_DECL:
a74e8896 984 if (asmspec)
b2c4af5e 985 set_user_assembler_name (value, asmspec);
9031d10b 986
074ab442 987 cp_finish_decl (value,
988 /*init=*/NULL_TREE,
989 /*init_const_expr_p=*/false,
d91303a6 990 asmspec_tree, flags);
471086d6 991
992 /* Pass friends back this way. */
993 if (DECL_FRIEND_P (value))
994 return void_type_node;
995
996 DECL_IN_AGGR_P (value) = 1;
997 return value;
9031d10b 998
092b1d6f 999 default:
1000 gcc_unreachable ();
471086d6 1001 }
471086d6 1002 return NULL_TREE;
1003}
1004
1005/* Like `grokfield', but for bitfields.
811c4f15 1006 WIDTH is the width of the bitfield, a constant expression.
1007 The other parameters are as for grokfield. */
471086d6 1008
1009tree
9031d10b 1010grokbitfield (const cp_declarator *declarator,
811c4f15 1011 cp_decl_specifier_seq *declspecs, tree width, tree init,
f922e029 1012 tree attrlist)
471086d6 1013{
811c4f15 1014 tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1015 init != NULL_TREE, &attrlist);
471086d6 1016
53b567c1 1017 if (value == error_mark_node)
70ed6de4 1018 return NULL_TREE; /* friends went bad. */
d974f078 1019
1020 tree type = TREE_TYPE (value);
1021 if (type == error_mark_node)
53b567c1 1022 return value;
471086d6 1023
1024 /* Pass friendly classes back. */
c21c015b 1025 if (VOID_TYPE_P (value))
471086d6 1026 return void_type_node;
1027
d974f078 1028 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type)
1029 && (INDIRECT_TYPE_P (type) || !dependent_type_p (type)))
d00ca5b2 1030 {
d974f078 1031 error_at (DECL_SOURCE_LOCATION (value),
1032 "bit-field %qD with non-integral type %qT",
1033 value, type);
d00ca5b2 1034 return error_mark_node;
1035 }
1036
471086d6 1037 if (TREE_CODE (value) == TYPE_DECL)
1038 {
3ce479a6 1039 error ("cannot declare %qD to be a bit-field type", value);
471086d6 1040 return NULL_TREE;
1041 }
1042
cf103c6c 1043 /* Usually, finish_struct_1 catches bitfields with invalid types.
4bac4c68 1044 But, in the case of bitfields with function type, we confuse
1045 ourselves into thinking they are member functions, so we must
1046 check here. */
1047 if (TREE_CODE (value) == FUNCTION_DECL)
1048 {
768666aa 1049 error_at (DECL_SOURCE_LOCATION (value),
1050 "cannot declare bit-field %qD with function type", value);
4bac4c68 1051 return NULL_TREE;
1052 }
1053
768666aa 1054 if (TYPE_WARN_IF_NOT_ALIGN (type))
4bec7283 1055 {
768666aa 1056 error_at (DECL_SOURCE_LOCATION (value), "cannot declare bit-field "
1057 "%qD with %<warn_if_not_aligned%> type", value);
4bec7283 1058 return NULL_TREE;
1059 }
1060
471086d6 1061 if (DECL_IN_AGGR_P (value))
1062 {
3ce479a6 1063 error ("%qD is already defined in the class %qT", value,
653e5405 1064 DECL_CONTEXT (value));
471086d6 1065 return void_type_node;
1066 }
1067
471086d6 1068 if (TREE_STATIC (value))
1069 {
768666aa 1070 error_at (DECL_SOURCE_LOCATION (value),
1071 "static member %qD cannot be a bit-field", value);
471086d6 1072 return NULL_TREE;
1073 }
811c4f15 1074
1075 int flags = LOOKUP_IMPLICIT;
1076 if (init && DIRECT_LIST_INIT_P (init))
1077 flags = LOOKUP_NORMAL;
1078 cp_finish_decl (value, init, false, NULL_TREE, flags);
471086d6 1079
1080 if (width != error_mark_node)
1081 {
e09e06c3 1082 /* The width must be an integer type. */
c4d7c2ba 1083 if (!type_dependent_expression_p (width)
1084 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
e09e06c3 1085 error ("width of bit-field %qD has non-integral type %qT", value,
1086 TREE_TYPE (width));
82c53ea9 1087 else
1088 {
52f26f82 1089 /* Temporarily stash the width in DECL_BIT_FIELD_REPRESENTATIVE.
1090 check_bitfield_decl picks it from there later and sets DECL_SIZE
1091 accordingly. */
1092 DECL_BIT_FIELD_REPRESENTATIVE (value) = width;
82c53ea9 1093 SET_DECL_C_BIT_FIELD (value);
1094 }
471086d6 1095 }
1096
1097 DECL_IN_AGGR_P (value) = 1;
f922e029 1098
1099 if (attrlist)
1100 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1101
471086d6 1102 return value;
1103}
1104
471086d6 1105\f
531be878 1106/* Returns true iff ATTR is an attribute which needs to be applied at
1107 instantiation time rather than template definition time. */
1108
2fdd6488 1109static bool
1110is_late_template_attribute (tree attr, tree decl)
531be878 1111{
ffcdbf9c 1112 tree name = get_attribute_name (attr);
51d7cfb2 1113 tree args = TREE_VALUE (attr);
2fdd6488 1114 const struct attribute_spec *spec = lookup_attribute_spec (name);
b2f4debb 1115 tree arg;
2fdd6488 1116
b8c42ec0 1117 if (!spec)
1118 /* Unknown attribute. */
1119 return false;
1120
d991e6e8 1121 /* Attribute weak handling wants to write out assembly right away. */
1122 if (is_attribute_p ("weak", name))
e03409d8 1123 return true;
1124
f5a21ec3 1125 /* Attributes used and unused are applied directly to typedefs for the
1126 benefit of maybe_warn_unused_local_typedefs. */
1127 if (TREE_CODE (decl) == TYPE_DECL
1128 && (is_attribute_p ("unused", name)
1129 || is_attribute_p ("used", name)))
71893c98 1130 return false;
1131
02b9d350 1132 /* Attribute tls_model wants to modify the symtab. */
1133 if (is_attribute_p ("tls_model", name))
1134 return true;
1135
bc7bff74 1136 /* #pragma omp declare simd attribute needs to be always deferred. */
1137 if (flag_openmp
1138 && is_attribute_p ("omp declare simd", name))
1139 return true;
1140
e829f68a 1141 /* An attribute pack is clearly dependent. */
1142 if (args && PACK_EXPANSION_P (args))
1143 return true;
1144
b2f4debb 1145 /* If any of the arguments are dependent expressions, we can't evaluate
1146 the attribute until instantiation time. */
1147 for (arg = args; arg; arg = TREE_CHAIN (arg))
1148 {
1149 tree t = TREE_VALUE (arg);
5128900d 1150
1151 /* If the first attribute argument is an identifier, only consider
1152 second and following arguments. Attributes like mode, format,
1153 cleanup and several target specific attributes aren't late
1154 just because they have an IDENTIFIER_NODE as first argument. */
feddf0dc 1155 if (arg == args && attribute_takes_identifier_p (name)
1156 && identifier_p (t))
5128900d 1157 continue;
1158
b2f4debb 1159 if (value_dependent_expression_p (t)
1160 || type_dependent_expression_p (t))
1161 return true;
1162 }
1163
1164 if (TREE_CODE (decl) == TYPE_DECL
1165 || TYPE_P (decl)
1166 || spec->type_required)
2fdd6488 1167 {
1168 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1169
1170 /* We can't apply any attributes to a completely unknown type until
1171 instantiation time. */
1172 enum tree_code code = TREE_CODE (type);
1173 if (code == TEMPLATE_TYPE_PARM
1174 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1175 || code == TYPENAME_TYPE)
1176 return true;
d54b9620 1177 /* Also defer most attributes on dependent types. This is not
1178 necessary in all cases, but is the better default. */
1179 else if (dependent_type_p (type)
59edbc2a 1180 /* But some attributes specifically apply to templates. */
42a2bdf5 1181 && !is_attribute_p ("abi_tag", name)
59edbc2a 1182 && !is_attribute_p ("deprecated", name)
d54b9620 1183 && !is_attribute_p ("visibility", name))
b2f4debb 1184 return true;
2fdd6488 1185 else
1186 return false;
1187 }
531be878 1188 else
1189 return false;
1190}
1191
1192/* ATTR_P is a list of attributes. Remove any attributes which need to be
2fdd6488 1193 applied at instantiation time and return them. If IS_DEPENDENT is true,
1194 the declaration itself is dependent, so all attributes should be applied
1195 at instantiation time. */
531be878 1196
1197static tree
2fdd6488 1198splice_template_attributes (tree *attr_p, tree decl)
531be878 1199{
1200 tree *p = attr_p;
1201 tree late_attrs = NULL_TREE;
1202 tree *q = &late_attrs;
1203
1204 if (!p)
1205 return NULL_TREE;
1206
1207 for (; *p; )
1208 {
2fdd6488 1209 if (is_late_template_attribute (*p, decl))
531be878 1210 {
2fdd6488 1211 ATTR_IS_DEPENDENT (*p) = 1;
531be878 1212 *q = *p;
1213 *p = TREE_CHAIN (*p);
1214 q = &TREE_CHAIN (*q);
1215 *q = NULL_TREE;
1216 }
1217 else
1218 p = &TREE_CHAIN (*p);
1219 }
1220
1221 return late_attrs;
1222}
1223
1224/* Remove any late attributes from the list in ATTR_P and attach them to
1225 DECL_P. */
1226
1227static void
21e97277 1228save_template_attributes (tree *attr_p, tree *decl_p, int flags)
531be878 1229{
531be878 1230 tree *q;
1231
a2679d09 1232 if (attr_p && *attr_p == error_mark_node)
1233 return;
1234
1235 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
531be878 1236 if (!late_attrs)
1237 return;
1238
1239 if (DECL_P (*decl_p))
1240 q = &DECL_ATTRIBUTES (*decl_p);
1241 else
1242 q = &TYPE_ATTRIBUTES (*decl_p);
1243
a2679d09 1244 tree old_attrs = *q;
9e5dd959 1245
ebc77885 1246 /* Merge the late attributes at the beginning with the attribute
9e5dd959 1247 list. */
ebc77885 1248 late_attrs = merge_attributes (late_attrs, *q);
21e97277 1249 if (*q != late_attrs
1250 && !DECL_P (*decl_p)
1251 && !(flags & ATTR_FLAG_TYPE_IN_PLACE))
1252 {
1253 if (!dependent_type_p (*decl_p))
1254 *decl_p = cp_build_type_attribute_variant (*decl_p, late_attrs);
1255 else
1256 {
1257 *decl_p = build_variant_type_copy (*decl_p);
1258 TYPE_ATTRIBUTES (*decl_p) = late_attrs;
1259 }
1260 }
1261 else
1262 *q = late_attrs;
9e5dd959 1263
1264 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1265 {
1266 /* We've added new attributes directly to the main variant, so
1267 now we need to update all of the other variants to include
1268 these new attributes. */
1269 tree variant;
1270 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1271 variant = TYPE_NEXT_VARIANT (variant))
1272 {
1273 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1274 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1275 }
1276 }
531be878 1277}
1278
80488e20 1279/* True if ATTRS contains any dependent attributes that affect type
1280 identity. */
1281
1282bool
1283any_dependent_type_attributes_p (tree attrs)
1284{
1285 for (tree a = attrs; a; a = TREE_CHAIN (a))
1286 if (ATTR_IS_DEPENDENT (a))
1287 {
1288 const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (a));
1289 if (as && as->affects_type_identity)
1290 return true;
1291 }
1292 return false;
1293}
1294
53874764 1295/* Return true iff ATTRS are acceptable attributes to be applied in-place
4f86cbb0 1296 to a typedef which gives a previously unnamed class or enum a name for
53874764 1297 linkage purposes. */
1298
1299bool
1300attributes_naming_typedef_ok (tree attrs)
1301{
1302 for (; attrs; attrs = TREE_CHAIN (attrs))
1303 {
1304 tree name = get_attribute_name (attrs);
1305 if (is_attribute_p ("vector_size", name))
1306 return false;
1307 }
1308 return true;
1309}
1310
d991e6e8 1311/* Like reconstruct_complex_type, but handle also template trees. */
1312
1313tree
1314cp_reconstruct_complex_type (tree type, tree bottom)
1315{
1316 tree inner, outer;
1317
c21c015b 1318 if (TYPE_PTR_P (type))
d991e6e8 1319 {
1320 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1321 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1322 TYPE_REF_CAN_ALIAS_ALL (type));
1323 }
90ad495b 1324 else if (TYPE_REF_P (type))
d991e6e8 1325 {
1326 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1327 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1328 TYPE_REF_CAN_ALIAS_ALL (type));
1329 }
1330 else if (TREE_CODE (type) == ARRAY_TYPE)
1331 {
1332 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1333 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1334 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1335 element type qualification will be handled by the recursive
1336 cp_reconstruct_complex_type call and cp_build_qualified_type
1337 for ARRAY_TYPEs changes the element type. */
1338 return outer;
1339 }
1340 else if (TREE_CODE (type) == FUNCTION_TYPE)
1341 {
1342 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1343 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
e5122931 1344 outer = apply_memfn_quals (outer, type_memfn_quals (type));
d991e6e8 1345 }
1346 else if (TREE_CODE (type) == METHOD_TYPE)
1347 {
1348 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1349 /* The build_method_type_directly() routine prepends 'this' to argument list,
1350 so we must compensate by getting rid of it. */
1351 outer
1352 = build_method_type_directly
a8d966c1 1353 (class_of_this_parm (type), inner,
d991e6e8 1354 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1355 }
1356 else if (TREE_CODE (type) == OFFSET_TYPE)
1357 {
1358 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1359 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1360 }
1361 else
1362 return bottom;
1363
897d42c2 1364 if (TYPE_ATTRIBUTES (type))
1365 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
0fad3000 1366 outer = cp_build_qualified_type (outer, cp_type_quals (type));
e5122931 1367 outer = cxx_copy_lang_qualifiers (outer, type);
0fad3000 1368
1369 return outer;
d991e6e8 1370}
1371
0b18f5f8 1372/* Replaces any constexpr expression that may be into the attributes
1373 arguments with their reduced value. */
1374
818af1aa 1375void
0b18f5f8 1376cp_check_const_attributes (tree attributes)
1377{
a2679d09 1378 if (attributes == error_mark_node)
1379 return;
1380
0b18f5f8 1381 tree attr;
1382 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1383 {
1384 tree arg;
80ef1e04 1385 for (arg = TREE_VALUE (attr); arg && TREE_CODE (arg) == TREE_LIST;
1386 arg = TREE_CHAIN (arg))
0b18f5f8 1387 {
1388 tree expr = TREE_VALUE (arg);
1389 if (EXPR_P (expr))
74894b80 1390 TREE_VALUE (arg) = fold_non_dependent_expr (expr);
0b18f5f8 1391 }
1392 }
1393}
1394
bc7bff74 1395/* Return true if TYPE is an OpenMP mappable type. */
1396bool
1397cp_omp_mappable_type (tree type)
1398{
1399 /* Mappable type has to be complete. */
1400 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1401 return false;
1402 /* Arrays have mappable type if the elements have mappable type. */
1403 while (TREE_CODE (type) == ARRAY_TYPE)
1404 type = TREE_TYPE (type);
1405 /* A mappable type cannot contain virtual members. */
1406 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1407 return false;
1408 /* All data members must be non-static. */
1409 if (CLASS_TYPE_P (type))
1410 {
1411 tree field;
1412 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
f4ae4202 1413 if (VAR_P (field))
bc7bff74 1414 return false;
1415 /* All fields must have mappable types. */
1416 else if (TREE_CODE (field) == FIELD_DECL
1417 && !cp_omp_mappable_type (TREE_TYPE (field)))
1418 return false;
1419 }
1420 return true;
1421}
1422
74744ce3 1423/* Return the last pushed declaration for the symbol DECL or NULL
1424 when no such declaration exists. */
1425
1426static tree
1427find_last_decl (tree decl)
1428{
1429 tree last_decl = NULL_TREE;
1430
1431 if (tree name = DECL_P (decl) ? DECL_NAME (decl) : NULL_TREE)
1432 {
1433 /* Look up the declaration in its scope. */
1434 tree pushed_scope = NULL_TREE;
1435 if (tree ctype = DECL_CONTEXT (decl))
1436 pushed_scope = push_scope (ctype);
1437
1438 last_decl = lookup_name (name);
1439
1440 if (pushed_scope)
1441 pop_scope (pushed_scope);
1442
1443 /* The declaration may be a member conversion operator
1444 or a bunch of overfloads (handle the latter below). */
1445 if (last_decl && BASELINK_P (last_decl))
1446 last_decl = BASELINK_FUNCTIONS (last_decl);
1447 }
1448
1449 if (!last_decl)
1450 return NULL_TREE;
1451
1452 if (DECL_P (last_decl) || TREE_CODE (last_decl) == OVERLOAD)
1453 {
1454 /* A set of overloads of the same function. */
1455 for (lkp_iterator iter (last_decl); iter; ++iter)
1456 {
1457 if (TREE_CODE (*iter) == OVERLOAD)
1458 continue;
1459
1460 if (decls_match (decl, *iter, /*record_decls=*/false))
1461 return *iter;
1462 }
1463 return NULL_TREE;
1464 }
1465
1466 return NULL_TREE;
1467}
1468
531be878 1469/* Like decl_attributes, but handle C++ complexity. */
1470
bea7d742 1471void
26ac6687 1472cplus_decl_attributes (tree *decl, tree attributes, int flags)
471086d6 1473{
61e0c529 1474 if (*decl == NULL_TREE || *decl == void_type_node
9797ae29 1475 || *decl == error_mark_node)
3f7d79e4 1476 return;
1477
bc7bff74 1478 /* Add implicit "omp declare target" attribute if requested. */
1479 if (scope_chain->omp_declare_target_attribute
f4ae4202 1480 && ((VAR_P (*decl)
1443772f 1481 && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
bc7bff74 1482 || TREE_CODE (*decl) == FUNCTION_DECL))
1483 {
f4ae4202 1484 if (VAR_P (*decl)
bc7bff74 1485 && DECL_CLASS_SCOPE_P (*decl))
1486 error ("%q+D static data member inside of declare target directive",
1487 *decl);
12f69ea4 1488 else if (VAR_P (*decl)
1489 && (processing_template_decl
1490 || !cp_omp_mappable_type (TREE_TYPE (*decl))))
1491 attributes = tree_cons (get_identifier ("omp declare target implicit"),
1492 NULL_TREE, attributes);
bc7bff74 1493 else
1494 attributes = tree_cons (get_identifier ("omp declare target"),
1495 NULL_TREE, attributes);
1496 }
1497
531be878 1498 if (processing_template_decl)
1499 {
830a6615 1500 if (check_for_bare_parameter_packs (attributes))
1501 return;
1502
21e97277 1503 save_template_attributes (&attributes, decl, flags);
531be878 1504 }
1505
0b18f5f8 1506 cp_check_const_attributes (attributes);
1507
9581e01c 1508 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1509 decl = &DECL_TEMPLATE_RESULT (*decl);
3f7d79e4 1510
6da7682f 1511 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1512 {
1513 attributes
1514 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
6b23c987 1515 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
6da7682f 1516 attributes, flags);
1517 }
1518 else
dab0e385 1519 {
74744ce3 1520 tree last_decl = find_last_decl (*decl);
dab0e385 1521 decl_attributes (decl, attributes, flags, last_decl);
1522 }
8c5c575d 1523
9581e01c 1524 if (TREE_CODE (*decl) == TYPE_DECL)
1525 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
59edbc2a 1526
1527 /* Propagate deprecation out to the template. */
1528 if (TREE_DEPRECATED (*decl))
1529 if (tree ti = get_template_info (*decl))
1530 {
1531 tree tmpl = TI_TEMPLATE (ti);
1532 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1533 : DECL_TEMPLATE_RESULT (tmpl));
1534 if (*decl == pattern)
1535 TREE_DEPRECATED (tmpl) = true;
1536 }
471086d6 1537}
471086d6 1538\f
4cbba981 1539/* Walks through the namespace- or function-scope anonymous union
50a7abc5 1540 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
4cbba981 1541 Returns one of the fields for use in the mangled name. */
727f288b 1542
e657e37c 1543static tree
4cbba981 1544build_anon_union_vars (tree type, tree object)
9b1ab1d1 1545{
9b1ab1d1 1546 tree main_decl = NULL_TREE;
8ef8f2b9 1547 tree field;
9b1ab1d1 1548
128e1d72 1549 /* Rather than write the code to handle the non-union case,
1550 just give an error. */
1551 if (TREE_CODE (type) != UNION_TYPE)
4f05c8a9 1552 {
1553 error ("anonymous struct not inside named type");
1554 return error_mark_node;
1555 }
128e1d72 1556
9031d10b 1557 for (field = TYPE_FIELDS (type);
1558 field != NULL_TREE;
1767a056 1559 field = DECL_CHAIN (field))
9b1ab1d1 1560 {
8ef8f2b9 1561 tree decl;
e657e37c 1562 tree ref;
6b0303ff 1563
1564 if (DECL_ARTIFICIAL (field))
9b1ab1d1 1565 continue;
6b0303ff 1566 if (TREE_CODE (field) != FIELD_DECL)
1567 {
0f4714ef 1568 permerror (DECL_SOURCE_LOCATION (field),
1569 "%q#D invalid; an anonymous union can only "
561fec9d 1570 "have non-static data members", field);
6b0303ff 1571 continue;
1572 }
9b1ab1d1 1573
8ef8f2b9 1574 if (TREE_PRIVATE (field))
0f4714ef 1575 permerror (DECL_SOURCE_LOCATION (field),
1576 "private member %q#D in anonymous union", field);
8ef8f2b9 1577 else if (TREE_PROTECTED (field))
0f4714ef 1578 permerror (DECL_SOURCE_LOCATION (field),
1579 "protected member %q#D in anonymous union", field);
8ef8f2b9 1580
954ad420 1581 if (processing_template_decl)
255b5d15 1582 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1583 DECL_NAME (field), NULL_TREE);
954ad420 1584 else
1585 ref = build_class_member_access_expr (object, field, NULL_TREE,
ebd21de4 1586 false, tf_warning_or_error);
e657e37c 1587
1588 if (DECL_NAME (field))
8ef8f2b9 1589 {
36b92eb7 1590 tree base;
1591
e60a6f7b 1592 decl = build_decl (input_location,
1593 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
8b2afb7a 1594 DECL_ANON_UNION_VAR_P (decl) = 1;
6ffc7890 1595 DECL_ARTIFICIAL (decl) = 1;
36b92eb7 1596
1597 base = get_base_address (object);
1598 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1599 TREE_STATIC (decl) = TREE_STATIC (base);
1600 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1601
50a7abc5 1602 SET_DECL_VALUE_EXPR (decl, ref);
1603 DECL_HAS_VALUE_EXPR_P (decl) = 1;
36b92eb7 1604
8ef8f2b9 1605 decl = pushdecl (decl);
8ef8f2b9 1606 }
e657e37c 1607 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
4cbba981 1608 decl = build_anon_union_vars (TREE_TYPE (field), ref);
6a1c27c3 1609 else
1610 decl = 0;
8ef8f2b9 1611
e657e37c 1612 if (main_decl == NULL_TREE)
1613 main_decl = decl;
8ef8f2b9 1614 }
e657e37c 1615
9b1ab1d1 1616 return main_decl;
1617}
1618
cf4ef810 1619/* Finish off the processing of a UNION_TYPE structure. If the union is an
1620 anonymous union, then all members must be laid out together. PUBLIC_P
1621 is nonzero if this union is not declared static. */
96624a9e 1622
471086d6 1623void
26ac6687 1624finish_anon_union (tree anon_union_decl)
471086d6 1625{
843ad097 1626 tree type;
8ef8f2b9 1627 tree main_decl;
843ad097 1628 bool public_p;
1629
1630 if (anon_union_decl == error_mark_node)
1631 return;
1632
1633 type = TREE_TYPE (anon_union_decl);
1634 public_p = TREE_PUBLIC (anon_union_decl);
471086d6 1635
a109dc3b 1636 /* The VAR_DECL's context is the same as the TYPE's context. */
138c02a6 1637 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
9031d10b 1638
8ef8f2b9 1639 if (TYPE_FIELDS (type) == NULL_TREE)
471086d6 1640 return;
1641
1642 if (public_p)
1643 {
c8470848 1644 error ("namespace-scope anonymous aggregates must be static");
471086d6 1645 return;
1646 }
1647
4cbba981 1648 main_decl = build_anon_union_vars (type, anon_union_decl);
662b4fae 1649 if (main_decl == error_mark_node)
1650 return;
954ad420 1651 if (main_decl == NULL_TREE)
205aec29 1652 {
f64621cd 1653 pedwarn (input_location, 0, "anonymous union with no members");
954ad420 1654 return;
1655 }
dd37e514 1656
954ad420 1657 if (!processing_template_decl)
1658 {
e657e37c 1659 /* Use main_decl to set the mangled name. */
1660 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
e3040053 1661 maybe_commonize_var (anon_union_decl);
808ccfc7 1662 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
772b23aa 1663 {
1664 if (DECL_DISCRIMINATOR_P (anon_union_decl))
1665 determine_local_discriminator (anon_union_decl);
1666 mangle_decl (anon_union_decl);
1667 }
e657e37c 1668 DECL_NAME (anon_union_decl) = NULL_TREE;
205aec29 1669 }
c8a606ae 1670
e657e37c 1671 pushdecl (anon_union_decl);
7b7a44c9 1672 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
471086d6 1673}
471086d6 1674\f
1675/* Auxiliary functions to make type signatures for
1676 `operator new' and `operator delete' correspond to
1677 what compiler will be expecting. */
1678
471086d6 1679tree
b117e4de 1680coerce_new_type (tree type, location_t loc)
471086d6 1681{
b7c82fb5 1682 int e = 0;
1683 tree args = TYPE_ARG_TYPES (type);
1684
b4df430b 1685 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
9031d10b 1686
b7c82fb5 1687 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
3ce479a6 1688 {
1689 e = 1;
b117e4de 1690 error_at (loc, "%<operator new%> must return type %qT",
1691 ptr_type_node);
3ce479a6 1692 }
b7c82fb5 1693
e0b2a9fb 1694 if (args && args != void_list_node)
b7c82fb5 1695 {
e0b2a9fb 1696 if (TREE_PURPOSE (args))
1697 {
1698 /* [basic.stc.dynamic.allocation]
1699
1700 The first parameter shall not have an associated default
1701 argument. */
b117e4de 1702 error_at (loc, "the first parameter of %<operator new%> cannot "
1703 "have a default argument");
e0b2a9fb 1704 /* Throw away the default argument. */
1705 TREE_PURPOSE (args) = NULL_TREE;
1706 }
1707
1708 if (!same_type_p (TREE_VALUE (args), size_type_node))
1709 {
1710 e = 2;
1711 args = TREE_CHAIN (args);
1712 }
b7c82fb5 1713 }
e0b2a9fb 1714 else
1715 e = 2;
1716
1717 if (e == 2)
b117e4de 1718 permerror (loc, "%<operator new%> takes type %<size_t%> (%qT) "
561fec9d 1719 "as first parameter", size_type_node);
e0b2a9fb 1720
b7c82fb5 1721 switch (e)
1722 {
1723 case 2:
654ef926 1724 args = tree_cons (NULL_TREE, size_type_node, args);
331bc0ad 1725 /* Fall through. */
b7c82fb5 1726 case 1:
e5122931 1727 type = (cxx_copy_lang_qualifiers
653e5405 1728 (build_function_type (ptr_type_node, args),
e5122931 1729 type));
331bc0ad 1730 /* Fall through. */
b7c82fb5 1731 default:;
1732 }
471086d6 1733 return type;
1734}
1735
76b94d4b 1736void
1737coerce_delete_type (tree decl, location_t loc)
471086d6 1738{
b7c82fb5 1739 int e = 0;
76b94d4b 1740 tree type = TREE_TYPE (decl);
b7c82fb5 1741 tree args = TYPE_ARG_TYPES (type);
9031d10b 1742
b4df430b 1743 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
02d7f858 1744
b7c82fb5 1745 if (!same_type_p (TREE_TYPE (type), void_type_node))
3ce479a6 1746 {
1747 e = 1;
b117e4de 1748 error_at (loc, "%<operator delete%> must return type %qT",
1749 void_type_node);
3ce479a6 1750 }
471086d6 1751
76b94d4b 1752 tree ptrtype = ptr_type_node;
1753 if (destroying_delete_p (decl))
1754 {
1755 if (DECL_CLASS_SCOPE_P (decl))
1756 /* If the function is a destroying operator delete declared in class type
1757 C, the type of its first parameter shall be C*. */
1758 ptrtype = TYPE_POINTER_TO (DECL_CONTEXT (decl));
1759 else
1760 /* A destroying operator delete shall be a class member function named
1761 operator delete. */
1762 error_at (loc, "destroying operator delete must be a member function");
1763 const ovl_op_info_t *op = IDENTIFIER_OVL_OP_INFO (DECL_NAME (decl));
1764 if (op->flags & OVL_OP_FLAG_VEC)
1765 error_at (loc, "operator delete[] cannot be a destroying delete");
1766 if (!usual_deallocation_fn_p (decl))
1767 error_at (loc, "destroying operator delete must be a usual "
1768 "deallocation function");
1769 }
1770
b7c82fb5 1771 if (!args || args == void_list_node
76b94d4b 1772 || !same_type_p (TREE_VALUE (args), ptrtype))
471086d6 1773 {
b7c82fb5 1774 e = 2;
1775 if (args && args != void_list_node)
653e5405 1776 args = TREE_CHAIN (args);
b117e4de 1777 error_at (loc, "%<operator delete%> takes type %qT as first parameter",
76b94d4b 1778 ptrtype);
471086d6 1779 }
b7c82fb5 1780 switch (e)
1781 {
1782 case 2:
76b94d4b 1783 args = tree_cons (NULL_TREE, ptrtype, args);
331bc0ad 1784 /* Fall through. */
b7c82fb5 1785 case 1:
e5122931 1786 type = (cxx_copy_lang_qualifiers
653e5405 1787 (build_function_type (void_type_node, args),
e5122931 1788 type));
331bc0ad 1789 /* Fall through. */
b7c82fb5 1790 default:;
1791 }
471086d6 1792
76b94d4b 1793 TREE_TYPE (decl) = type;
471086d6 1794}
1795\f
30abb346 1796/* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1797 and mark them as needed. */
1798
471086d6 1799static void
26ac6687 1800mark_vtable_entries (tree decl)
471086d6 1801{
c75b4594 1802 tree fnaddr;
1803 unsigned HOST_WIDE_INT idx;
ce28ee2e 1804
b874b738 1805 /* It's OK for the vtable to refer to deprecated virtual functions. */
1806 warning_sentinel w(warn_deprecated_decl);
1807
c75b4594 1808 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1809 idx, fnaddr)
471086d6 1810 {
a3fd53f0 1811 tree fn;
b04d4dc3 1812
1813 STRIP_NOPS (fnaddr);
1814
6bfa2cc1 1815 if (TREE_CODE (fnaddr) != ADDR_EXPR
1816 && TREE_CODE (fnaddr) != FDESC_EXPR)
b53fb33d 1817 /* This entry is an offset: a virtual base class offset, a
b53db2b0 1818 virtual call offset, an RTTI offset, etc. */
a3fd53f0 1819 continue;
1820
a3fd53f0 1821 fn = TREE_OPERAND (fnaddr, 0);
0543e7a9 1822 TREE_ADDRESSABLE (fn) = 1;
2b82dde2 1823 /* When we don't have vcall offsets, we output thunks whenever
1824 we output the vtables that contain them. With vcall offsets,
1825 we know all the thunks we'll need when we emit a virtual
1826 function, so we emit the thunks there instead. */
9031d10b 1827 if (DECL_THUNK_P (fn))
70050b43 1828 use_thunk (fn, /*emit_p=*/0);
e2f46fbf 1829 /* Set the location, as marking the function could cause
1830 instantiation. We do not need to preserve the incoming
1831 location, as we're called from c_parse_final_cleanups, which
1832 takes care of that. */
1833 input_location = DECL_SOURCE_LOCATION (fn);
e857e9c7 1834 mark_used (fn);
b0722fac 1835 }
1836}
1837
cb8ba767 1838/* Adjust the TLS model on variable DECL if need be, typically after
1839 the linkage of DECL has been modified. */
1840
1841static void
1842adjust_var_decl_tls_model (tree decl)
1843{
1844 if (CP_DECL_THREAD_LOCAL_P (decl)
1845 && !lookup_attribute ("tls_model", DECL_ATTRIBUTES (decl)))
1846 set_decl_tls_model (decl, decl_default_tls_model (decl));
1847}
1848
2739960c 1849/* Set DECL up to have the closest approximation of "initialized common"
1850 linkage available. */
1851
1852void
26ac6687 1853comdat_linkage (tree decl)
2739960c 1854{
2739960c 1855 if (flag_weak)
ecd88073 1856 make_decl_one_only (decl, cxx_comdat_group (decl));
9031d10b 1857 else if (TREE_CODE (decl) == FUNCTION_DECL
80a58eb0 1858 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
20e29201 1859 /* We can just emit function and compiler-generated variables
1860 statically; having multiple copies is (for the most part) only
9031d10b 1861 a waste of space.
20e29201 1862
1863 There are two correctness issues, however: the address of a
1864 template instantiation with external linkage should be the
a1abbd01 1865 same, independent of what translation unit asks for the
1866 address, and this will not hold when we emit multiple copies of
9031d10b 1867 the function. However, there's little else we can do.
20e29201 1868
5ad590ad 1869 Also, by default, the typeinfo implementation assumes that
1870 there will be only one copy of the string used as the name for
1871 each type. Therefore, if weak symbols are unavailable, the
1872 run-time library should perform a more conservative check; it
1873 should perform a string comparison, rather than an address
1874 comparison. */
d200dc92 1875 TREE_PUBLIC (decl) = 0;
08e76de4 1876 else
1877 {
efa0614d 1878 /* Static data member template instantiations, however, cannot
1879 have multiple copies. */
08e76de4 1880 if (DECL_INITIAL (decl) == 0
1881 || DECL_INITIAL (decl) == error_mark_node)
1882 DECL_COMMON (decl) = 1;
1883 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1884 {
1885 DECL_COMMON (decl) = 1;
1886 DECL_INITIAL (decl) = error_mark_node;
1887 }
c5d6e012 1888 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
08e76de4 1889 {
1890 /* We can't do anything useful; leave vars for explicit
1891 instantiation. */
1892 DECL_EXTERNAL (decl) = 1;
1893 DECL_NOT_REALLY_EXTERN (decl) = 0;
1894 }
1895 }
9eae53f2 1896
0f8f48d0 1897 if (TREE_PUBLIC (decl))
1898 DECL_COMDAT (decl) = 1;
cb8ba767 1899
1900 if (VAR_P (decl))
1901 adjust_var_decl_tls_model (decl);
2739960c 1902}
1903
347cbbaf 1904/* For win32 we also want to put explicit instantiations in
1905 linkonce sections, so that they will be merged with implicit
9031d10b 1906 instantiations; otherwise we get duplicate symbol errors.
1907 For Darwin we do not want explicit instantiations to be
7677ebb9 1908 linkonce. */
347cbbaf 1909
1910void
26ac6687 1911maybe_make_one_only (tree decl)
347cbbaf 1912{
38003e6d 1913 /* We used to say that this was not necessary on targets that support weak
1914 symbols, because the implicit instantiations will defer to the explicit
1915 one. However, that's not actually the case in SVR4; a strong definition
1916 after a weak one is an error. Also, not making explicit
1917 instantiations one_only means that we can end up with two copies of
a109dc3b 1918 some template instantiations. */
8458d888 1919 if (! flag_weak)
347cbbaf 1920 return;
1921
5dd72fac 1922 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
08e76de4 1923 we can get away with not emitting them if they aren't used. We need
1924 to for variables so that cp_finish_decl will update their linkage,
1925 because their DECL_INITIAL may not have been set properly yet. */
347cbbaf 1926
9bb1c6fd 1927 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
2f9fc8ef 1928 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1929 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
c4c3877b 1930 {
ecd88073 1931 make_decl_one_only (decl, cxx_comdat_group (decl));
2f9fc8ef 1932
80a58eb0 1933 if (VAR_P (decl))
2f9fc8ef 1934 {
97221fd7 1935 varpool_node *node = varpool_node::get_create (decl);
2f9fc8ef 1936 DECL_COMDAT (decl) = 1;
1937 /* Mark it needed so we don't forget to emit it. */
02774f2d 1938 node->forced_by_abi = true;
ff2a5ada 1939 TREE_USED (decl) = 1;
cb8ba767 1940
1941 adjust_var_decl_tls_model (decl);
2f9fc8ef 1942 }
c4c3877b 1943 }
347cbbaf 1944}
1945
1b155d83 1946/* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1947 This predicate will give the right answer during parsing of the
1948 function, which other tests may not. */
5f34abf8 1949
1950bool
1b155d83 1951vague_linkage_p (tree decl)
5f34abf8 1952{
8b2942f7 1953 if (!TREE_PUBLIC (decl))
1954 {
f11f2449 1955 /* maybe_thunk_body clears TREE_PUBLIC and DECL_ABSTRACT_P on the
1956 maybe-in-charge 'tor variants; in that case we need to check one of
1957 the "clones" for the real linkage. But only in that case; before
1958 maybe_clone_body we haven't yet copied the linkage to the clones. */
c6e04fca 1959 if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl)
f11f2449 1960 && !DECL_ABSTRACT_P (decl)
d7670757 1961 && DECL_CHAIN (decl)
2e32597d 1962 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)))
d7670757 1963 return vague_linkage_p (DECL_CHAIN (decl));
1964
8b2942f7 1965 gcc_checking_assert (!DECL_COMDAT (decl));
1966 return false;
1967 }
5f34abf8 1968 /* Unfortunately, import_export_decl has not always been called
1969 before the function is processed, so we cannot simply check
1970 DECL_COMDAT. */
14b525c6 1971 if (DECL_COMDAT (decl)
8b2942f7 1972 || (TREE_CODE (decl) == FUNCTION_DECL
1973 && DECL_DECLARED_INLINE_P (decl))
1974 || (DECL_LANG_SPECIFIC (decl)
81f19d90 1975 && DECL_TEMPLATE_INSTANTIATION (decl))
1976 || (VAR_P (decl) && DECL_INLINE_VAR_P (decl)))
14b525c6 1977 return true;
1978 else if (DECL_FUNCTION_SCOPE_P (decl))
1979 /* A local static in an inline effectively has vague linkage. */
1980 return (TREE_STATIC (decl)
1981 && vague_linkage_p (DECL_CONTEXT (decl)));
1982 else
1983 return false;
5f34abf8 1984}
1985
e3c182ae 1986/* Determine whether or not we want to specifically import or export CTYPE,
1987 using various heuristics. */
61a113f4 1988
7702ef60 1989static void
26ac6687 1990import_export_class (tree ctype)
471086d6 1991{
e3c182ae 1992 /* -1 for imported, 1 for exported. */
1993 int import_export = 0;
1994
7702ef60 1995 /* It only makes sense to call this function at EOF. The reason is
1996 that this function looks at whether or not the first non-inline
1997 non-abstract virtual member function has been defined in this
1998 translation unit. But, we can't possibly know that until we've
1999 seen the entire translation unit. */
b4df430b 2000 gcc_assert (at_eof);
7702ef60 2001
e3c182ae 2002 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2003 return;
2004
197e583a 2005 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
863c3f96 2006 we will have CLASSTYPE_INTERFACE_ONLY set but not
2007 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
2008 heuristic because someone will supply a #pragma implementation
2009 elsewhere, and deducing it here would produce a conflict. */
2010 if (CLASSTYPE_INTERFACE_ONLY (ctype))
2011 return;
2012
e3c541f0 2013 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
ed5635e4 2014 import_export = -1;
e3c541f0 2015 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
ed5635e4 2016 import_export = 1;
caa6fdce 2017 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
2018 && !flag_implicit_templates)
2019 /* For a template class, without -fimplicit-templates, check the
2020 repository. If the virtual table is assigned to this
2021 translation unit, then export the class; otherwise, import
2022 it. */
2023 import_export = repo_export_class_p (ctype) ? 1 : -1;
2024 else if (TYPE_POLYMORPHIC_P (ctype))
2025 {
2026 /* The ABI specifies that the virtual table and associated
2027 information are emitted with the key method, if any. */
9bacae7e 2028 tree method = CLASSTYPE_KEY_METHOD (ctype);
958b8ee9 2029 /* If weak symbol support is not available, then we must be
2030 careful not to emit the vtable when the key function is
2031 inline. An inline function can be defined in multiple
2032 translation units. If we were to emit the vtable in each
2033 translation unit containing a definition, we would get
2034 multiple definition errors at link-time. */
2035 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
89d7453c 2036 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
ac9386a0 2037 }
b31a9466 2038
197e583a 2039 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
2040 a definition anywhere else. */
2041 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
b31a9466 2042 import_export = 0;
e3c182ae 2043
a17c2a3a 2044 /* Allow back ends the chance to overrule the decision. */
52d6bee4 2045 if (targetm.cxx.import_export_class)
2046 import_export = targetm.cxx.import_export_class (ctype, import_export);
2047
e3c182ae 2048 if (import_export)
2049 {
2050 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
e3c182ae 2051 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
2052 }
61a113f4 2053}
434617bd 2054
6f0feeca 2055/* Return true if VAR has already been provided to the back end; in that
2056 case VAR should not be modified further by the front end. */
2057static bool
2058var_finalized_p (tree var)
2059{
97221fd7 2060 return varpool_node::get_create (var)->definition;
caa6fdce 2061}
2062
2063/* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
2064 must be emitted in this translation unit. Mark it as such. */
2065
2066void
2067mark_needed (tree decl)
2068{
2f4b5e45 2069 TREE_USED (decl) = 1;
6a1c0403 2070 if (TREE_CODE (decl) == FUNCTION_DECL)
2071 {
2072 /* Extern inline functions don't become needed when referenced.
2073 If we know a method will be emitted in other TU and no new
2074 functions can be marked reachable, just use the external
2075 definition. */
415d1b9a 2076 struct cgraph_node *node = cgraph_node::get_create (decl);
02774f2d 2077 node->forced_by_abi = true;
977e64ea 2078
2079 /* #pragma interface and -frepo code can call mark_needed for
2080 maybe-in-charge 'tors; mark the clones as well. */
2081 tree clone;
2082 FOR_EACH_CLONE (clone, decl)
2083 mark_needed (clone);
6a1c0403 2084 }
f4ae4202 2085 else if (VAR_P (decl))
6a1c0403 2086 {
97221fd7 2087 varpool_node *node = varpool_node::get_create (decl);
6a1c0403 2088 /* C++ frontend use mark_decl_references to force COMDAT variables
2089 to be output that might appear dead otherwise. */
02774f2d 2090 node->forced_by_abi = true;
6a1c0403 2091 }
caa6fdce 2092}
2093
2094/* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
2095 returns true if a definition of this entity should be provided in
2096 this object file. Callers use this function to determine whether
2097 or not to let the back end know that a definition of DECL is
2098 available in this translation unit. */
2099
2100bool
2101decl_needed_p (tree decl)
2102{
4cace8cb 2103 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
caa6fdce 2104 /* This function should only be called at the end of the translation
2105 unit. We cannot be sure of whether or not something will be
2106 COMDAT until that point. */
b4df430b 2107 gcc_assert (at_eof);
caa6fdce 2108
e9e7df9c 2109 /* All entities with external linkage that are not COMDAT/EXTERN should be
caa6fdce 2110 emitted; they may be referred to from other object files. */
e9e7df9c 2111 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
caa6fdce 2112 return true;
aed4eb5d 2113 /* Functions marked "dllexport" must be emitted so that they are
2114 visible to other DLLs. */
0675168d 2115 if (flag_keep_inline_dllexport
2116 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
aed4eb5d 2117 return true;
e9e7df9c 2118
2119 /* When not optimizing, do not bother to produce definitions for extern
2120 symbols. */
2121 if (DECL_REALLY_EXTERN (decl)
2122 && ((TREE_CODE (decl) != FUNCTION_DECL
2123 && !optimize)
2124 || (TREE_CODE (decl) == FUNCTION_DECL
2125 && !opt_for_fn (decl, optimize)))
2126 && !lookup_attribute ("always_inline", decl))
2127 return false;
2128
2129 /* If this entity was used, let the back end see it; it will decide
2130 whether or not to emit it into the object file. */
2131 if (TREE_USED (decl))
67d5f2c7 2132 return true;
2133
9af43b99 2134 /* Virtual functions might be needed for devirtualization. */
2135 if (flag_devirtualize
2136 && TREE_CODE (decl) == FUNCTION_DECL
2137 && DECL_VIRTUAL_P (decl))
2138 return true;
67d5f2c7 2139
caa6fdce 2140 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2141 reference to DECL might cause it to be emitted later. */
2142 return false;
6f0feeca 2143}
2144
6fc7a923 2145/* If necessary, write out the vtables for the dynamic class CTYPE.
26ac6687 2146 Returns true if any vtables were emitted. */
6fc7a923 2147
26ac6687 2148static bool
6fc7a923 2149maybe_emit_vtables (tree ctype)
42f3e1b9 2150{
6fc7a923 2151 tree vtbl;
2152 tree primary_vtbl;
caa6fdce 2153 int needed = 0;
098f44bc 2154 varpool_node *current = NULL, *last = NULL;
6fc7a923 2155
2156 /* If the vtables for this class have already been emitted there is
2157 nothing more to do. */
2158 primary_vtbl = CLASSTYPE_VTABLES (ctype);
6f0feeca 2159 if (var_finalized_p (primary_vtbl))
26ac6687 2160 return false;
6fc7a923 2161 /* Ignore dummy vtables made by get_vtable_decl. */
2162 if (TREE_TYPE (primary_vtbl) == void_type_node)
26ac6687 2163 return false;
6fc7a923 2164
219626ad 2165 /* On some targets, we cannot determine the key method until the end
2166 of the translation unit -- which is when this function is
2167 called. */
2168 if (!targetm.cxx.key_method_may_be_inline ())
2169 determine_key_method (ctype);
2170
6fc7a923 2171 /* See if any of the vtables are needed. */
1767a056 2172 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
1fec0e58 2173 {
caa6fdce 2174 import_export_decl (vtbl);
2175 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2176 needed = 1;
1fec0e58 2177 }
caa6fdce 2178 if (!needed)
6fc7a923 2179 {
2180 /* If the references to this class' vtables are optimized away,
2181 still emit the appropriate debugging information. See
2182 dfs_debug_mark. */
9031d10b 2183 if (DECL_COMDAT (primary_vtbl)
6fc7a923 2184 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2185 note_debug_info_needed (ctype);
26ac6687 2186 return false;
6fc7a923 2187 }
2188
2189 /* The ABI requires that we emit all of the vtables if we emit any
2190 of them. */
1767a056 2191 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
42f3e1b9 2192 {
caa6fdce 2193 /* Mark entities references from the virtual table as used. */
6fc7a923 2194 mark_vtable_entries (vtbl);
6cb758f0 2195
6fc7a923 2196 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
3afe9b43 2197 {
f1f41a6c 2198 vec<tree, va_gc> *cleanups = NULL;
c7b89256 2199 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2200 LOOKUP_NORMAL);
9031d10b 2201
3afe9b43 2202 /* It had better be all done at compile-time. */
c7b89256 2203 gcc_assert (!expr && !cleanups);
3afe9b43 2204 }
471086d6 2205
caa6fdce 2206 /* Write it out. */
2207 DECL_EXTERNAL (vtbl) = 0;
b2c4af5e 2208 rest_of_decl_compilation (vtbl, 1, 1);
434617bd 2209
a3d5bae2 2210 /* Because we're only doing syntax-checking, we'll never end up
2211 actually marking the variable as written. */
2212 if (flag_syntax_only)
6fc7a923 2213 TREE_ASM_WRITTEN (vtbl) = 1;
484a0b1b 2214 else if (DECL_ONE_ONLY (vtbl))
933b10c6 2215 {
97221fd7 2216 current = varpool_node::get_create (vtbl);
933b10c6 2217 if (last)
415d1b9a 2218 current->add_to_same_comdat_group (last);
933b10c6 2219 last = current;
933b10c6 2220 }
471086d6 2221 }
89d7453c 2222
6fc7a923 2223 /* Since we're writing out the vtable here, also write the debug
2224 info. */
2225 note_debug_info_needed (ctype);
1e66592c 2226
26ac6687 2227 return true;
1e66592c 2228}
2229
4a2849cb 2230/* A special return value from type_visibility meaning internal
2231 linkage. */
2232
6a47600c 2233enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
4a2849cb 2234
1c432fa5 2235static int expr_visibility (tree);
2236static int type_visibility (tree);
2237
4a2849cb 2238/* walk_tree helper function for type_visibility. */
2239
2240static tree
2241min_vis_r (tree *tp, int *walk_subtrees, void *data)
2242{
2243 int *vis_p = (int *)data;
2244 if (! TYPE_P (*tp))
2245 {
2246 *walk_subtrees = 0;
2247 }
5b97892c 2248 else if (OVERLOAD_TYPE_P (*tp)
7462eaa3 2249 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
4a2849cb 2250 {
7462eaa3 2251 *vis_p = VISIBILITY_ANON;
2252 return *tp;
4a2849cb 2253 }
7462eaa3 2254 else if (CLASS_TYPE_P (*tp)
2255 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2256 *vis_p = CLASSTYPE_VISIBILITY (*tp);
1c432fa5 2257 else if (TREE_CODE (*tp) == ARRAY_TYPE
2258 && uses_template_parms (TYPE_DOMAIN (*tp)))
2259 {
2260 int evis = expr_visibility (TYPE_MAX_VALUE (TYPE_DOMAIN (*tp)));
2261 if (evis > *vis_p)
2262 *vis_p = evis;
2263 }
4a2849cb 2264 return NULL;
2265}
2266
1c432fa5 2267/* walk_tree helper function for expr_visibility. */
2268
2269static tree
2270min_vis_expr_r (tree *tp, int */*walk_subtrees*/, void *data)
2271{
2272 int *vis_p = (int *)data;
2273 int tpvis = VISIBILITY_DEFAULT;
2274
2275 switch (TREE_CODE (*tp))
2276 {
2277 case CAST_EXPR:
2278 case IMPLICIT_CONV_EXPR:
2279 case STATIC_CAST_EXPR:
2280 case REINTERPRET_CAST_EXPR:
2281 case CONST_CAST_EXPR:
2282 case DYNAMIC_CAST_EXPR:
2283 case NEW_EXPR:
2284 case CONSTRUCTOR:
9c093b14 2285 case LAMBDA_EXPR:
1c432fa5 2286 tpvis = type_visibility (TREE_TYPE (*tp));
2287 break;
2288
2289 case VAR_DECL:
2290 case FUNCTION_DECL:
2291 if (! TREE_PUBLIC (*tp))
2292 tpvis = VISIBILITY_ANON;
2293 else
2294 tpvis = DECL_VISIBILITY (*tp);
2295 break;
2296
2297 default:
2298 break;
2299 }
2300
2301 if (tpvis > *vis_p)
2302 *vis_p = tpvis;
2303
2304 return NULL_TREE;
2305}
2306
4a2849cb 2307/* Returns the visibility of TYPE, which is the minimum visibility of its
2308 component types. */
2309
2310static int
2311type_visibility (tree type)
2312{
2313 int vis = VISIBILITY_DEFAULT;
20a8f962 2314 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
4a2849cb 2315 return vis;
2316}
2317
1c432fa5 2318/* Returns the visibility of an expression EXPR that appears in the signature
2319 of a function template, which is the minimum visibility of names that appear
2320 in its mangling. */
2321
2322static int
2323expr_visibility (tree expr)
2324{
2325 int vis = VISIBILITY_DEFAULT;
2326 cp_walk_tree_without_duplicates (&expr, min_vis_expr_r, &vis);
2327 return vis;
2328}
2329
6a47600c 2330/* Limit the visibility of DECL to VISIBILITY, if not explicitly
920f5a70 2331 specified (or if VISIBILITY is static). If TMPL is true, this
2332 constraint is for a template argument, and takes precedence
2333 over explicitly-specified visibility on the template. */
4a2849cb 2334
920f5a70 2335static void
2336constrain_visibility (tree decl, int visibility, bool tmpl)
4a2849cb 2337{
6a47600c 2338 if (visibility == VISIBILITY_ANON)
4a2849cb 2339 {
6a47600c 2340 /* extern "C" declarations aren't affected by the anonymous
2341 namespace. */
2342 if (!DECL_EXTERN_C_P (decl))
2343 {
2344 TREE_PUBLIC (decl) = 0;
1a3da51b 2345 DECL_WEAK (decl) = 0;
2346 DECL_COMMON (decl) = 0;
bc3900ee 2347 DECL_COMDAT (decl) = false;
f4ae4202 2348 if (VAR_OR_FUNCTION_DECL_P (decl))
8c016392 2349 {
415d1b9a 2350 struct symtab_node *snode = symtab_node::get (decl);
8c016392 2351
2352 if (snode)
2353 snode->set_comdat_group (NULL);
2354 }
6a47600c 2355 DECL_INTERFACE_KNOWN (decl) = 1;
2356 if (DECL_LANG_SPECIFIC (decl))
2357 DECL_NOT_REALLY_EXTERN (decl) = 1;
2358 }
4a2849cb 2359 }
6a47600c 2360 else if (visibility > DECL_VISIBILITY (decl)
920f5a70 2361 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
4a2849cb 2362 {
8458f4ca 2363 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
bf56937c 2364 /* This visibility was not specified. */
2365 DECL_VISIBILITY_SPECIFIED (decl) = false;
4a2849cb 2366 }
4a2849cb 2367}
2368
87c09cc3 2369/* Constrain the visibility of DECL based on the visibility of its template
4a2849cb 2370 arguments. */
2371
2372static void
2373constrain_visibility_for_template (tree decl, tree targs)
2374{
2375 /* If this is a template instantiation, check the innermost
2376 template args for visibility constraints. The outer template
2377 args are covered by the class check. */
2378 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2379 int i;
2380 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2381 {
2382 int vis = 0;
2383
2384 tree arg = TREE_VEC_ELT (args, i-1);
2385 if (TYPE_P (arg))
2386 vis = type_visibility (arg);
bc3900ee 2387 else
1c432fa5 2388 vis = expr_visibility (arg);
4a2849cb 2389 if (vis)
920f5a70 2390 constrain_visibility (decl, vis, true);
4a2849cb 2391 }
2392}
2393
3aa0c315 2394/* Like c_determine_visibility, but with additional C++-specific
799435d8 2395 behavior.
2396
2397 Function-scope entities can rely on the function's visibility because
2398 it is set in start_preparsed_function.
2399
2400 Class-scope entities cannot rely on the class's visibility until the end
2401 of the enclosing class definition.
2402
2403 Note that because namespaces have multiple independent definitions,
2404 namespace visibility is handled elsewhere using the #pragma visibility
4a2849cb 2405 machinery rather than by decorating the namespace declaration.
2406
2407 The goal is for constraints from the type to give a diagnostic, and
2408 other constraints to be applied silently. */
91caa6ca 2409
2410void
2411determine_visibility (tree decl)
2412{
4a2849cb 2413 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
91caa6ca 2414
799435d8 2415 /* Only relevant for names with external linkage. */
2416 if (!TREE_PUBLIC (decl))
2417 return;
2418
91caa6ca 2419 /* Cloned constructors and destructors get the same visibility as
2420 the underlying function. That should be set up in
2421 maybe_clone_body. */
b4df430b 2422 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
3aa0c315 2423
5d5f1138 2424 bool orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2425 enum symbol_visibility orig_visibility = DECL_VISIBILITY (decl);
2d9d8740 2426
5d5f1138 2427 /* The decl may be a template instantiation, which could influence
2428 visibilty. */
2429 tree template_decl = NULL_TREE;
4a2849cb 2430 if (TREE_CODE (decl) == TYPE_DECL)
2431 {
2432 if (CLASS_TYPE_P (TREE_TYPE (decl)))
5d5f1138 2433 {
2434 if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
2435 template_decl = decl;
2436 }
4a2849cb 2437 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
5d5f1138 2438 template_decl = decl;
4a2849cb 2439 }
5d5f1138 2440 else if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
2441 template_decl = decl;
4a2849cb 2442
3aa0c315 2443 /* If DECL is a member of a class, visibility specifiers on the
2444 class can influence the visibility of the DECL. */
5d5f1138 2445 tree class_type = NULL_TREE;
91caa6ca 2446 if (DECL_CLASS_SCOPE_P (decl))
2447 class_type = DECL_CONTEXT (decl);
91caa6ca 2448 else
2449 {
4a2849cb 2450 /* Not a class member. */
2451
91caa6ca 2452 /* Virtual tables have DECL_CONTEXT set to their associated class,
2453 so they are automatically handled above. */
80a58eb0 2454 gcc_assert (!VAR_P (decl)
b4df430b 2455 || !DECL_VTABLE_OR_VTT_P (decl));
799435d8 2456
825957d0 2457 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
799435d8 2458 {
4a2849cb 2459 /* Local statics and classes get the visibility of their
825957d0 2460 containing function by default, except that
2461 -fvisibility-inlines-hidden doesn't affect them. */
799435d8 2462 tree fn = DECL_CONTEXT (decl);
0488e949 2463 if (DECL_VISIBILITY_SPECIFIED (fn))
825957d0 2464 {
2465 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2466 DECL_VISIBILITY_SPECIFIED (decl) =
2467 DECL_VISIBILITY_SPECIFIED (fn);
2468 }
2469 else
66f24c41 2470 {
2471 if (DECL_CLASS_SCOPE_P (fn))
2472 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2473 else if (determine_hidden_inline (fn))
2474 {
2475 DECL_VISIBILITY (decl) = default_visibility;
2476 DECL_VISIBILITY_SPECIFIED (decl) =
2477 visibility_options.inpragma;
2478 }
2479 else
2480 {
2481 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2482 DECL_VISIBILITY_SPECIFIED (decl) =
2483 DECL_VISIBILITY_SPECIFIED (fn);
2484 }
2485 }
799435d8 2486
4a2849cb 2487 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
f11f2449 2488 but have no TEMPLATE_INFO, so don't try to check it. */
2489 template_decl = NULL_TREE;
4a2849cb 2490 }
80a58eb0 2491 else if (VAR_P (decl) && DECL_TINFO_P (decl)
54f1c2a2 2492 && flag_visibility_ms_compat)
2493 {
2494 /* Under -fvisibility-ms-compat, types are visible by default,
2495 even though their contents aren't. */
2496 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2497 int underlying_vis = type_visibility (underlying_type);
2498 if (underlying_vis == VISIBILITY_ANON
de0e5a52 2499 || (CLASS_TYPE_P (underlying_type)
2500 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
920f5a70 2501 constrain_visibility (decl, underlying_vis, false);
54f1c2a2 2502 else
2503 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2504 }
80a58eb0 2505 else if (VAR_P (decl) && DECL_TINFO_P (decl))
4a2849cb 2506 {
2507 /* tinfo visibility is based on the type it's for. */
2508 constrain_visibility
920f5a70 2509 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
4e496818 2510
2511 /* Give the target a chance to override the visibility associated
2512 with DECL. */
2513 if (TREE_PUBLIC (decl)
2514 && !DECL_REALLY_EXTERN (decl)
2515 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2516 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2517 targetm.cxx.determine_class_data_visibility (decl);
4a2849cb 2518 }
5d5f1138 2519 else if (template_decl)
4a2849cb 2520 /* Template instantiations and specializations get visibility based
2521 on their template unless they override it with an attribute. */;
2522 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2523 {
66f24c41 2524 if (determine_hidden_inline (decl))
2525 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2526 else
2527 {
2528 /* Set default visibility to whatever the user supplied with
2529 #pragma GCC visibility or a namespace visibility attribute. */
2530 DECL_VISIBILITY (decl) = default_visibility;
2531 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2532 }
4a2849cb 2533 }
91caa6ca 2534 }
2535
5d5f1138 2536 if (template_decl)
91caa6ca 2537 {
6a47600c 2538 /* If the specialization doesn't specify visibility, use the
2539 visibility from the template. */
5d5f1138 2540 tree tinfo = get_template_info (template_decl);
4a2849cb 2541 tree args = TI_ARGS (tinfo);
a837ff93 2542 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2543 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2544 : DECL_ATTRIBUTES (decl));
4603b8ee 2545
dc3784b9 2546 if (args != error_mark_node)
4a2849cb 2547 {
4603b8ee 2548 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2549
2550 if (!DECL_VISIBILITY_SPECIFIED (decl))
2551 {
66f24c41 2552 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2553 && determine_hidden_inline (decl))
2554 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2555 else
2556 {
2557 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2558 DECL_VISIBILITY_SPECIFIED (decl)
2559 = DECL_VISIBILITY_SPECIFIED (pattern);
2560 }
4603b8ee 2561 }
4a2849cb 2562
dc3784b9 2563 if (args
2564 /* Template argument visibility outweighs #pragma or namespace
2565 visibility, but not an explicit attribute. */
2566 && !lookup_attribute ("visibility", attribs))
2567 {
2568 int depth = TMPL_ARGS_DEPTH (args);
dc3784b9 2569 if (DECL_VISIBILITY_SPECIFIED (decl))
2570 {
2571 /* A class template member with explicit visibility
2572 overrides the class visibility, so we need to apply
2573 all the levels of template args directly. */
2574 int i;
2575 for (i = 1; i <= depth; ++i)
2576 {
2577 tree lev = TMPL_ARGS_LEVEL (args, i);
2578 constrain_visibility_for_template (decl, lev);
2579 }
2580 }
37c1d7cd 2581 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
dc3784b9 2582 /* Limit visibility based on its template arguments. */
2583 constrain_visibility_for_template (decl, args);
2584 }
4603b8ee 2585 }
074ab442 2586 }
6a47600c 2587
4a2849cb 2588 if (class_type)
2589 determine_visibility_from_class (decl, class_type);
2590
4a2849cb 2591 if (decl_anon_ns_mem_p (decl))
2592 /* Names in an anonymous namespace get internal linkage.
2593 This might change once we implement export. */
920f5a70 2594 constrain_visibility (decl, VISIBILITY_ANON, false);
b85623b5 2595 else if (TREE_CODE (decl) != TYPE_DECL)
2596 {
2597 /* Propagate anonymity from type to decl. */
2598 int tvis = type_visibility (TREE_TYPE (decl));
f918309c 2599 if (tvis == VISIBILITY_ANON
2600 || ! DECL_VISIBILITY_SPECIFIED (decl))
920f5a70 2601 constrain_visibility (decl, tvis, false);
b85623b5 2602 }
5f34abf8 2603 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2604 /* DR 757: A type without linkage shall not be used as the type of a
2605 variable or function with linkage, unless
2606 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2607 o the variable or function is not used (3.2 [basic.def.odr]) or is
2608 defined in the same translation unit.
2609
2610 Since non-extern "C" decls need to be defined in the same
2611 translation unit, we can make the type internal. */
920f5a70 2612 constrain_visibility (decl, VISIBILITY_ANON, false);
2d9d8740 2613
2614 /* If visibility changed and DECL already has DECL_RTL, ensure
2615 symbol flags are updated. */
2616 if ((DECL_VISIBILITY (decl) != orig_visibility
2617 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
80a58eb0 2618 && ((VAR_P (decl) && TREE_STATIC (decl))
2d9d8740 2619 || TREE_CODE (decl) == FUNCTION_DECL)
2620 && DECL_RTL_SET_P (decl))
2621 make_decl_rtl (decl);
799435d8 2622}
2623
4a2849cb 2624/* By default, static data members and function members receive
2625 the visibility of their containing class. */
2626
799435d8 2627static void
2628determine_visibility_from_class (tree decl, tree class_type)
2629{
098a01e7 2630 if (DECL_VISIBILITY_SPECIFIED (decl))
2631 return;
2632
66f24c41 2633 if (determine_hidden_inline (decl))
4a2849cb 2634 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
098a01e7 2635 else
6a47600c 2636 {
2637 /* Default to the class visibility. */
2638 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2639 DECL_VISIBILITY_SPECIFIED (decl)
2640 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2641 }
4a2849cb 2642
2643 /* Give the target a chance to override the visibility associated
2644 with DECL. */
80a58eb0 2645 if (VAR_P (decl)
4a2849cb 2646 && (DECL_TINFO_P (decl)
2647 || (DECL_VTABLE_OR_VTT_P (decl)
2648 /* Construction virtual tables are not exported because
2649 they cannot be referred to from other object files;
2650 their name is not standardized by the ABI. */
2651 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2652 && TREE_PUBLIC (decl)
2653 && !DECL_REALLY_EXTERN (decl)
6a47600c 2654 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
4a2849cb 2655 targetm.cxx.determine_class_data_visibility (decl);
2656}
2657
66f24c41 2658/* Returns true iff DECL is an inline that should get hidden visibility
2659 because of -fvisibility-inlines-hidden. */
2660
2661static bool
2662determine_hidden_inline (tree decl)
2663{
2664 return (visibility_options.inlines_hidden
2665 /* Don't do this for inline templates; specializations might not be
2666 inline, and we don't want them to inherit the hidden
2667 visibility. We'll set it here for all inline instantiations. */
2668 && !processing_template_decl
2669 && TREE_CODE (decl) == FUNCTION_DECL
2670 && DECL_DECLARED_INLINE_P (decl)
2671 && (! DECL_LANG_SPECIFIC (decl)
2672 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2673}
2674
4a2849cb 2675/* Constrain the visibility of a class TYPE based on the visibility of its
2676 field types. Warn if any fields require lesser visibility. */
2677
2678void
2679constrain_class_visibility (tree type)
2680{
6a47600c 2681 tree binfo;
4a2849cb 2682 tree t;
2683 int i;
2684
6a47600c 2685 int vis = type_visibility (type);
2686
b85623b5 2687 if (vis == VISIBILITY_ANON
2688 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
6a47600c 2689 return;
2690
2691 /* Don't warn about visibility if the class has explicit visibility. */
2692 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2693 vis = VISIBILITY_INTERNAL;
2694
1767a056 2695 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
f918c2c9 2696 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node
2697 && !DECL_ARTIFICIAL (t))
6a47600c 2698 {
57a0ed23 2699 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
b85623b5 2700 int subvis = type_visibility (ftype);
6a47600c 2701
2702 if (subvis == VISIBILITY_ANON)
b5237388 2703 {
ee48893a 2704 if (!in_main_input_context())
2705 {
2706 tree nlt = no_linkage_check (ftype, /*relaxed_p=*/false);
2707 if (nlt)
2708 {
2709 if (same_type_p (TREE_TYPE (t), nlt))
2710 warning (OPT_Wsubobject_linkage, "\
2711%qT has a field %qD whose type has no linkage",
2712 type, t);
2713 else
2714 warning (OPT_Wsubobject_linkage, "\
2715%qT has a field %qD whose type depends on the type %qT which has no linkage",
2716 type, t, nlt);
2717 }
2718 else
2719 warning (OPT_Wsubobject_linkage, "\
6a47600c 2720%qT has a field %qD whose type uses the anonymous namespace",
ee48893a 2721 type, t);
2722 }
b5237388 2723 }
95397ff9 2724 else if (MAYBE_CLASS_TYPE_P (ftype)
b85623b5 2725 && vis < VISIBILITY_HIDDEN
6a47600c 2726 && subvis >= VISIBILITY_HIDDEN)
2727 warning (OPT_Wattributes, "\
4a2849cb 2728%qT declared with greater visibility than the type of its field %qD",
6a47600c 2729 type, t);
2730 }
4a2849cb 2731
6a47600c 2732 binfo = TYPE_BINFO (type);
4a2849cb 2733 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
6a47600c 2734 {
2735 int subvis = type_visibility (TREE_TYPE (t));
2736
2737 if (subvis == VISIBILITY_ANON)
b5237388 2738 {
96cc8e6a 2739 if (!in_main_input_context())
ee48893a 2740 {
2741 tree nlt = no_linkage_check (TREE_TYPE (t), /*relaxed_p=*/false);
2742 if (nlt)
2743 {
2744 if (same_type_p (TREE_TYPE (t), nlt))
2745 warning (OPT_Wsubobject_linkage, "\
2746%qT has a base %qT whose type has no linkage",
2747 type, TREE_TYPE (t));
2748 else
2749 warning (OPT_Wsubobject_linkage, "\
2750%qT has a base %qT whose type depends on the type %qT which has no linkage",
2751 type, TREE_TYPE (t), nlt);
2752 }
2753 else
2754 warning (OPT_Wsubobject_linkage, "\
6a47600c 2755%qT has a base %qT whose type uses the anonymous namespace",
ee48893a 2756 type, TREE_TYPE (t));
2757 }
b5237388 2758 }
6a47600c 2759 else if (vis < VISIBILITY_HIDDEN
2760 && subvis >= VISIBILITY_HIDDEN)
2761 warning (OPT_Wattributes, "\
4a2849cb 2762%qT declared with greater visibility than its base %qT",
6a47600c 2763 type, TREE_TYPE (t));
2764 }
91caa6ca 2765}
2766
2381bbfa 2767/* Functions for adjusting the visibility of a tagged type and its nested
2768 types and declarations when it gets a name for linkage purposes from a
2769 typedef. */
2770
2771static void bt_reset_linkage_1 (binding_entry, void *);
2772static void bt_reset_linkage_2 (binding_entry, void *);
2773
2774/* First reset the visibility of all the types. */
2775
2776static void
2777reset_type_linkage_1 (tree type)
2778{
2779 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2780 if (CLASS_TYPE_P (type))
2781 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2782 bt_reset_linkage_1, NULL);
2783}
2784static void
2785bt_reset_linkage_1 (binding_entry b, void */*data*/)
2786{
2787 reset_type_linkage_1 (b->type);
2788}
2789
2790/* Then reset the visibility of any static data members or member
2791 functions that use those types. */
2792
2793static void
2794reset_decl_linkage (tree decl)
2795{
2796 if (TREE_PUBLIC (decl))
2797 return;
2798 if (DECL_CLONED_FUNCTION_P (decl))
2799 return;
2800 TREE_PUBLIC (decl) = true;
2801 DECL_INTERFACE_KNOWN (decl) = false;
2802 determine_visibility (decl);
2803 tentative_decl_linkage (decl);
2804}
ab87ee8f 2805
2381bbfa 2806static void
2807reset_type_linkage_2 (tree type)
2808{
2809 if (CLASS_TYPE_P (type))
2810 {
2811 if (tree vt = CLASSTYPE_VTABLES (type))
2812 {
2813 tree name = mangle_vtbl_for_type (type);
2814 DECL_NAME (vt) = name;
2815 SET_DECL_ASSEMBLER_NAME (vt, name);
2816 reset_decl_linkage (vt);
2817 }
2818 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2819 {
2820 tree name = mangle_typeinfo_for_type (type);
2821 DECL_NAME (ti) = name;
2822 SET_DECL_ASSEMBLER_NAME (ti, name);
2823 TREE_TYPE (name) = type;
2824 reset_decl_linkage (ti);
2825 }
2826 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
02d652ce 2827 {
2828 tree mem = STRIP_TEMPLATE (m);
ab87ee8f 2829 if (TREE_CODE (mem) == VAR_DECL || TREE_CODE (mem) == FUNCTION_DECL)
02d652ce 2830 reset_decl_linkage (mem);
2831 }
2381bbfa 2832 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2833 bt_reset_linkage_2, NULL);
2834 }
2835}
ab87ee8f 2836
2381bbfa 2837static void
2838bt_reset_linkage_2 (binding_entry b, void */*data*/)
2839{
2840 reset_type_linkage_2 (b->type);
2841}
2842void
2843reset_type_linkage (tree type)
2844{
2845 reset_type_linkage_1 (type);
2846 reset_type_linkage_2 (type);
2847}
2848
2849/* Set up our initial idea of what the linkage of DECL should be. */
2850
2851void
2852tentative_decl_linkage (tree decl)
2853{
2854 if (DECL_INTERFACE_KNOWN (decl))
2855 /* We've already made a decision as to how this function will
2856 be handled. */;
2857 else if (vague_linkage_p (decl))
2858 {
5e584863 2859 if (TREE_CODE (decl) == FUNCTION_DECL
2860 && decl_defined_p (decl))
2381bbfa 2861 {
2862 DECL_EXTERNAL (decl) = 1;
2863 DECL_NOT_REALLY_EXTERN (decl) = 1;
2864 note_vague_linkage_fn (decl);
2865 /* A non-template inline function with external linkage will
2866 always be COMDAT. As we must eventually determine the
2867 linkage of all functions, and as that causes writes to
2868 the data mapped in from the PCH file, it's advantageous
2869 to mark the functions at this point. */
2870 if (DECL_DECLARED_INLINE_P (decl)
2871 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2872 || DECL_DEFAULTED_FN (decl)))
2873 {
2874 /* This function must have external linkage, as
2875 otherwise DECL_INTERFACE_KNOWN would have been
2876 set. */
2877 gcc_assert (TREE_PUBLIC (decl));
2878 comdat_linkage (decl);
2879 DECL_INTERFACE_KNOWN (decl) = 1;
2880 }
2881 }
f4ae4202 2882 else if (VAR_P (decl))
5e584863 2883 maybe_commonize_var (decl);
2381bbfa 2884 }
2885}
2886
caa6fdce 2887/* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2888 for DECL has not already been determined, do so now by setting
2889 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2890 function is called entities with vague linkage whose definitions
2891 are available must have TREE_PUBLIC set.
2892
2893 If this function decides to place DECL in COMDAT, it will set
2894 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2895 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2896 callers defer that decision until it is clear that DECL is actually
2897 required. */
6495357a 2898
2899void
26ac6687 2900import_export_decl (tree decl)
6495357a 2901{
caa6fdce 2902 int emit_p;
2903 bool comdat_p;
2904 bool import_p;
7908506d 2905 tree class_type = NULL_TREE;
caa6fdce 2906
c25194fd 2907 if (DECL_INTERFACE_KNOWN (decl))
6495357a 2908 return;
2909
caa6fdce 2910 /* We cannot determine what linkage to give to an entity with vague
2911 linkage until the end of the file. For example, a virtual table
2912 for a class will be defined if and only if the key method is
2913 defined in this translation unit. As a further example, consider
2914 that when compiling a translation unit that uses PCH file with
2915 "-frepo" it would be incorrect to make decisions about what
2916 entities to emit when building the PCH; those decisions must be
2917 delayed until the repository information has been processed. */
b4df430b 2918 gcc_assert (at_eof);
caa6fdce 2919 /* Object file linkage for explicit instantiations is handled in
2920 mark_decl_instantiated. For static variables in functions with
2921 vague linkage, maybe_commonize_var is used.
2922
2923 Therefore, the only declarations that should be provided to this
8b652e89 2924 function are those with external linkage that are:
caa6fdce 2925
2926 * implicit instantiations of function templates
2927
2928 * inline function
2929
2930 * implicit instantiations of static data members of class
2931 templates
2932
2933 * virtual tables
2934
2935 * typeinfo objects
2936
2937 Furthermore, all entities that reach this point must have a
2938 definition available in this translation unit.
2939
2940 The following assertions check these conditions. */
4cace8cb 2941 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
caa6fdce 2942 /* Any code that creates entities with TREE_PUBLIC cleared should
2943 also set DECL_INTERFACE_KNOWN. */
b4df430b 2944 gcc_assert (TREE_PUBLIC (decl));
caa6fdce 2945 if (TREE_CODE (decl) == FUNCTION_DECL)
b4df430b 2946 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2947 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2948 || DECL_DECLARED_INLINE_P (decl));
caa6fdce 2949 else
b4df430b 2950 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2951 || DECL_VTABLE_OR_VTT_P (decl)
2952 || DECL_TINFO_P (decl));
caa6fdce 2953 /* Check that a definition of DECL is available in this translation
2954 unit. */
b4df430b 2955 gcc_assert (!DECL_REALLY_EXTERN (decl));
caa6fdce 2956
2957 /* Assume that DECL will not have COMDAT linkage. */
2958 comdat_p = false;
2959 /* Assume that DECL will not be imported into this translation
2960 unit. */
2961 import_p = false;
2962
2963 /* See if the repository tells us whether or not to emit DECL in
2964 this translation unit. */
2965 emit_p = repo_emit_p (decl);
2966 if (emit_p == 0)
2967 import_p = true;
2968 else if (emit_p == 1)
2969 {
2970 /* The repository indicates that this entity should be defined
2971 here. Make sure the back end honors that request. */
977e64ea 2972 mark_needed (decl);
caa6fdce 2973 /* Output the definition as an ordinary strong definition. */
2974 DECL_EXTERNAL (decl) = 0;
2975 DECL_INTERFACE_KNOWN (decl) = 1;
2976 return;
2977 }
2978
2979 if (import_p)
2980 /* We have already decided what to do with this DECL; there is no
2981 need to check anything further. */
2982 ;
80a58eb0 2983 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
caa6fdce 2984 {
7908506d 2985 class_type = DECL_CONTEXT (decl);
2986 import_export_class (class_type);
c93d719b 2987 if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2988 && CLASSTYPE_INTERFACE_ONLY (class_type))
caa6fdce 2989 import_p = true;
5712a9a8 2990 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
7908506d 2991 && !CLASSTYPE_USE_TEMPLATE (class_type)
2992 && CLASSTYPE_KEY_METHOD (class_type)
2993 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
caa6fdce 2994 /* The ABI requires that all virtual tables be emitted with
2995 COMDAT linkage. However, on systems where COMDAT symbols
2996 don't show up in the table of contents for a static
5712a9a8 2997 archive, or on systems without weak symbols (where we
2998 approximate COMDAT linkage by using internal linkage), the
2999 linker will report errors about undefined symbols because
3000 it will not see the virtual table definition. Therefore,
3001 in the case that we know that the virtual table will be
3002 emitted in only one translation unit, we make the virtual
3003 table an ordinary definition with external linkage. */
caa6fdce 3004 DECL_EXTERNAL (decl) = 0;
7908506d 3005 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
61aae38f 3006 {
7908506d 3007 /* CLASS_TYPE is being exported from this translation unit,
9031d10b 3008 so DECL should be defined here. */
7908506d 3009 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
5712a9a8 3010 /* If a class is declared in a header with the "extern
3011 template" extension, then it will not be instantiated,
3012 even in translation units that would normally require
3013 it. Often such classes are explicitly instantiated in
3014 one translation unit. Therefore, the explicit
3015 instantiation must be made visible to other translation
3016 units. */
3017 DECL_EXTERNAL (decl) = 0;
3018 else
3019 {
7908506d 3020 /* The generic C++ ABI says that class data is always
3021 COMDAT, even if there is a key function. Some
3022 variants (e.g., the ARM EABI) says that class data
a5a9f920 3023 only has COMDAT linkage if the class data might be
3024 emitted in more than one translation unit. When the
3025 key method can be inline and is inline, we still have
3026 to arrange for comdat even though
3027 class_data_always_comdat is false. */
7908506d 3028 if (!CLASSTYPE_KEY_METHOD (class_type)
ef97a256 3029 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
7908506d 3030 || targetm.cxx.class_data_always_comdat ())
3031 {
3032 /* The ABI requires COMDAT linkage. Normally, we
3033 only emit COMDAT things when they are needed;
3034 make sure that we realize that this entity is
3035 indeed needed. */
3036 comdat_p = true;
3037 mark_needed (decl);
3038 }
5712a9a8 3039 }
61aae38f 3040 }
caa6fdce 3041 else if (!flag_implicit_templates
7908506d 3042 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
caa6fdce 3043 import_p = true;
3044 else
3045 comdat_p = true;
3046 }
80a58eb0 3047 else if (VAR_P (decl) && DECL_TINFO_P (decl))
caa6fdce 3048 {
3049 tree type = TREE_TYPE (DECL_NAME (decl));
3050 if (CLASS_TYPE_P (type))
3051 {
7908506d 3052 class_type = type;
caa6fdce 3053 import_export_class (type);
3054 if (CLASSTYPE_INTERFACE_KNOWN (type)
3055 && TYPE_POLYMORPHIC_P (type)
3056 && CLASSTYPE_INTERFACE_ONLY (type)
3057 /* If -fno-rtti was specified, then we cannot be sure
3058 that RTTI information will be emitted with the
3059 virtual table of the class, so we must emit it
3060 wherever it is used. */
3061 && flag_rtti)
3062 import_p = true;
9031d10b 3063 else
caa6fdce 3064 {
caa6fdce 3065 if (CLASSTYPE_INTERFACE_KNOWN (type)
3066 && !CLASSTYPE_INTERFACE_ONLY (type))
5712a9a8 3067 {
a5a9f920 3068 comdat_p = (targetm.cxx.class_data_always_comdat ()
3069 || (CLASSTYPE_KEY_METHOD (type)
ef97a256 3070 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
07e166a5 3071 mark_needed (decl);
5712a9a8 3072 if (!flag_weak)
3073 {
3074 comdat_p = false;
3075 DECL_EXTERNAL (decl) = 0;
3076 }
3077 }
7908506d 3078 else
3079 comdat_p = true;
caa6fdce 3080 }
3081 }
3082 else
3083 comdat_p = true;
3084 }
1a05e335 3085 else if (DECL_TEMPLOID_INSTANTIATION (decl))
caa6fdce 3086 {
3087 /* DECL is an implicit instantiation of a function or static
3088 data member. */
af3f756d 3089 if ((flag_implicit_templates
3090 && !flag_use_repository)
caa6fdce 3091 || (flag_implicit_inline_templates
9031d10b 3092 && TREE_CODE (decl) == FUNCTION_DECL
caa6fdce 3093 && DECL_DECLARED_INLINE_P (decl)))
3094 comdat_p = true;
3095 else
3096 /* If we are not implicitly generating templates, then mark
3097 this entity as undefined in this translation unit. */
3098 import_p = true;
6495357a 3099 }
3100 else if (DECL_FUNCTION_MEMBER_P (decl))
3101 {
d3981643 3102 if (!DECL_DECLARED_INLINE_P (decl))
6495357a 3103 {
fc43d1f2 3104 tree ctype = DECL_CONTEXT (decl);
3105 import_export_class (ctype);
3106 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
3107 {
3108 DECL_NOT_REALLY_EXTERN (decl)
3109 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
9031d10b 3110 || (DECL_DECLARED_INLINE_P (decl)
d3981643 3111 && ! flag_implement_inlines
fc43d1f2 3112 && !DECL_VINDEX (decl)));
3113
61aae38f 3114 if (!DECL_NOT_REALLY_EXTERN (decl))
3115 DECL_EXTERNAL (decl) = 1;
3116
fc43d1f2 3117 /* Always make artificials weak. */
3118 if (DECL_ARTIFICIAL (decl) && flag_weak)
caa6fdce 3119 comdat_p = true;
fc43d1f2 3120 else
3121 maybe_make_one_only (decl);
3122 }
6495357a 3123 }
c25194fd 3124 else
caa6fdce 3125 comdat_p = true;
6495357a 3126 }
c25194fd 3127 else
caa6fdce 3128 comdat_p = true;
46f41950 3129
caa6fdce 3130 if (import_p)
46f41950 3131 {
caa6fdce 3132 /* If we are importing DECL into this translation unit, mark is
3133 an undefined here. */
3134 DECL_EXTERNAL (decl) = 1;
3135 DECL_NOT_REALLY_EXTERN (decl) = 0;
46f41950 3136 }
caa6fdce 3137 else if (comdat_p)
46f41950 3138 {
caa6fdce 3139 /* If we decided to put DECL in COMDAT, mark it accordingly at
3140 this point. */
3141 comdat_linkage (decl);
46f41950 3142 }
6a7638c9 3143
46f41950 3144 DECL_INTERFACE_KNOWN (decl) = 1;
3145}
3146
c1654f51 3147/* Return an expression that performs the destruction of DECL, which
3148 must be a VAR_DECL whose type has a non-trivial destructor, or is
3149 an array whose (innermost) elements have a non-trivial destructor. */
3150
3d4e092a 3151tree
26ac6687 3152build_cleanup (tree decl)
3d4e092a 3153{
9ede1ce4 3154 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
3155 gcc_assert (clean != NULL_TREE);
3156 return clean;
3d4e092a 3157}
3158
4eb32e62 3159/* Returns the initialization guard variable for the variable DECL,
3160 which has static storage duration. */
98eaf693 3161
4eb32e62 3162tree
26ac6687 3163get_guard (tree decl)
bf1b65b4 3164{
98eaf693 3165 tree sname;
4eb32e62 3166 tree guard;
3167
606b494c 3168 sname = mangle_guard_variable (decl);
64924d1d 3169 guard = get_global_binding (sname);
4eb32e62 3170 if (! guard)
3171 {
3172 tree guard_type;
3173
5ad590ad 3174 /* We use a type that is big enough to contain a mutex as well
3175 as an integer counter. */
c1dc02de 3176 guard_type = targetm.cxx.guard_type ();
e60a6f7b 3177 guard = build_decl (DECL_SOURCE_LOCATION (decl),
3178 VAR_DECL, sname, guard_type);
9031d10b 3179
a109dc3b 3180 /* The guard should have the same linkage as what it guards. */
fe939bb3 3181 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
3182 TREE_STATIC (guard) = TREE_STATIC (decl);
3183 DECL_COMMON (guard) = DECL_COMMON (decl);
d73f0fde 3184 DECL_COMDAT (guard) = DECL_COMDAT (decl);
800478e6 3185 CP_DECL_THREAD_LOCAL_P (guard) = CP_DECL_THREAD_LOCAL_P (decl);
5e68df57 3186 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
c9aa9f3f 3187 if (DECL_ONE_ONLY (decl))
3188 make_decl_one_only (guard, cxx_comdat_group (guard));
fe939bb3 3189 if (TREE_PUBLIC (decl))
653e5405 3190 DECL_WEAK (guard) = DECL_WEAK (decl);
e4ef06d0 3191 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
3192 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
9031d10b 3193
4eb32e62 3194 DECL_ARTIFICIAL (guard) = 1;
553acd9c 3195 DECL_IGNORED_P (guard) = 1;
4eb32e62 3196 TREE_USED (guard) = 1;
eab1674f 3197 pushdecl_top_level_and_finish (guard, NULL_TREE);
4eb32e62 3198 }
3199 return guard;
3200}
3201
0925e39e 3202/* Return an atomic load of src with the appropriate memory model. */
3203
3204static tree
3205build_atomic_load_byte (tree src, HOST_WIDE_INT model)
3206{
3207 tree ptr_type = build_pointer_type (char_type_node);
3208 tree mem_model = build_int_cst (integer_type_node, model);
3209 tree t, addr, val;
3210 unsigned int size;
3211 int fncode;
3212
3213 size = tree_to_uhwi (TYPE_SIZE_UNIT (char_type_node));
3214
3215 fncode = BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1;
3216 t = builtin_decl_implicit ((enum built_in_function) fncode);
3217
3218 addr = build1 (ADDR_EXPR, ptr_type, src);
3219 val = build_call_expr (t, 2, addr, mem_model);
3220 return val;
3221}
3222
4eb32e62 3223/* Return those bits of the GUARD variable that should be set when the
3224 guarded entity is actually initialized. */
3225
3226static tree
26ac6687 3227get_guard_bits (tree guard)
4eb32e62 3228{
c1dc02de 3229 if (!targetm.cxx.guard_mask_bit ())
3230 {
3231 /* We only set the first byte of the guard, in order to leave room
3232 for a mutex in the high-order bits. */
9031d10b 3233 guard = build1 (ADDR_EXPR,
c1dc02de 3234 build_pointer_type (TREE_TYPE (guard)),
3235 guard);
9031d10b 3236 guard = build1 (NOP_EXPR,
3237 build_pointer_type (char_type_node),
c1dc02de 3238 guard);
3239 guard = build1 (INDIRECT_REF, char_type_node, guard);
3240 }
4eb32e62 3241
3242 return guard;
3243}
3244
3245/* Return an expression which determines whether or not the GUARD
3246 variable has already been initialized. */
3247
3248tree
0925e39e 3249get_guard_cond (tree guard, bool thread_safe)
4eb32e62 3250{
3251 tree guard_value;
3252
0925e39e 3253 if (!thread_safe)
3254 guard = get_guard_bits (guard);
3255 else
3256 guard = build_atomic_load_byte (guard, MEMMODEL_ACQUIRE);
c1dc02de 3257
3258 /* Mask off all but the low bit. */
3259 if (targetm.cxx.guard_mask_bit ())
3260 {
3261 guard_value = integer_one_node;
3262 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
d2c63826 3263 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
8e70fb09 3264 guard = cp_build_binary_op (input_location,
3265 BIT_AND_EXPR, guard, guard_value,
ebd21de4 3266 tf_warning_or_error);
c1dc02de 3267 }
3268
4eb32e62 3269 guard_value = integer_zero_node;
3270 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
d2c63826 3271 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
8e70fb09 3272 return cp_build_binary_op (input_location,
3273 EQ_EXPR, guard, guard_value,
ebd21de4 3274 tf_warning_or_error);
4eb32e62 3275}
3276
3277/* Return an expression which sets the GUARD variable, indicating that
3278 the variable being guarded has been initialized. */
3279
3280tree
26ac6687 3281set_guard (tree guard)
4eb32e62 3282{
3283 tree guard_init;
3284
3285 /* Set the GUARD to one. */
3286 guard = get_guard_bits (guard);
3287 guard_init = integer_one_node;
3288 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
d2c63826 3289 guard_init = fold_convert (TREE_TYPE (guard), guard_init);
22a3f7bd 3290 return cp_build_modify_expr (input_location, guard, NOP_EXPR, guard_init,
ebd21de4 3291 tf_warning_or_error);
bf1b65b4 3292}
3293
462819c8 3294/* Returns true iff we can tell that VAR does not have a dynamic
3295 initializer. */
3296
3297static bool
3298var_defined_without_dynamic_init (tree var)
3299{
3300 /* If it's defined in another TU, we can't tell. */
3301 if (DECL_EXTERNAL (var))
3302 return false;
3303 /* If it has a non-trivial destructor, registering the destructor
3304 counts as dynamic initialization. */
3305 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3306 return false;
ea1fb9e5 3307 /* If it's in this TU, its initializer has been processed, unless
3308 it's a case of self-initialization, then DECL_INITIALIZED_P is
3309 false while the initializer is handled by finish_id_expression. */
3310 if (!DECL_INITIALIZED_P (var))
3311 return false;
462819c8 3312 /* If it has no initializer or a constant one, it's not dynamic. */
3313 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3314 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3315}
3316
3317/* Returns true iff VAR is a variable that needs uses to be
3318 wrapped for possible dynamic initialization. */
3319
3320static bool
3321var_needs_tls_wrapper (tree var)
3322{
e9464429 3323 return (!error_operand_p (var)
800478e6 3324 && CP_DECL_THREAD_LOCAL_P (var)
462819c8 3325 && !DECL_GNU_TLS_P (var)
3326 && !DECL_FUNCTION_SCOPE_P (var)
3327 && !var_defined_without_dynamic_init (var));
3328}
3329
1c4973d7 3330/* Get the FUNCTION_DECL for the shared TLS init function for this
3331 translation unit. */
3332
3333static tree
3334get_local_tls_init_fn (void)
3335{
3336 tree sname = get_identifier ("__tls_init");
64924d1d 3337 tree fn = get_global_binding (sname);
1c4973d7 3338 if (!fn)
3339 {
3340 fn = build_lang_decl (FUNCTION_DECL, sname,
3341 build_function_type (void_type_node,
3342 void_list_node));
3343 SET_DECL_LANGUAGE (fn, lang_c);
3344 TREE_PUBLIC (fn) = false;
3345 DECL_ARTIFICIAL (fn) = true;
3346 mark_used (fn);
64924d1d 3347 set_global_binding (fn);
1c4973d7 3348 }
3349 return fn;
3350}
3351
462819c8 3352/* Get a FUNCTION_DECL for the init function for the thread_local
3353 variable VAR. The init function will be an alias to the function
3354 that initializes all the non-local TLS variables in the translation
3355 unit. The init function is only used by the wrapper function. */
3356
3357static tree
3358get_tls_init_fn (tree var)
3359{
3360 /* Only C++11 TLS vars need this init fn. */
3361 if (!var_needs_tls_wrapper (var))
3362 return NULL_TREE;
3363
1c4973d7 3364 /* If -fno-extern-tls-init, assume that we don't need to call
3365 a tls init function for a variable defined in another TU. */
3366 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3367 return NULL_TREE;
3368
1c4973d7 3369 /* If the variable is internal, or if we can't generate aliases,
3370 call the local init function directly. */
07b8a412 3371 if (!TREE_PUBLIC (var) || !TARGET_SUPPORTS_ALIASES)
1c4973d7 3372 return get_local_tls_init_fn ();
3373
462819c8 3374 tree sname = mangle_tls_init_fn (var);
64924d1d 3375 tree fn = get_global_binding (sname);
462819c8 3376 if (!fn)
3377 {
3378 fn = build_lang_decl (FUNCTION_DECL, sname,
3379 build_function_type (void_type_node,
3380 void_list_node));
3381 SET_DECL_LANGUAGE (fn, lang_c);
3382 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3383 DECL_ARTIFICIAL (fn) = true;
3384 DECL_COMDAT (fn) = DECL_COMDAT (var);
b81ddff2 3385 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
462819c8 3386 if (DECL_ONE_ONLY (var))
3387 make_decl_one_only (fn, cxx_comdat_group (fn));
3388 if (TREE_PUBLIC (var))
3389 {
3390 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
1c4973d7 3391 /* If the variable is defined somewhere else and might have static
3392 initialization, make the init function a weak reference. */
462819c8 3393 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
fcae3eb4 3394 || TYPE_HAS_CONSTEXPR_CTOR (obtype)
3395 || TYPE_HAS_TRIVIAL_DFLT (obtype))
1c4973d7 3396 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3397 && DECL_EXTERNAL (var))
462819c8 3398 declare_weak (fn);
3399 else
3400 DECL_WEAK (fn) = DECL_WEAK (var);
3401 }
3402 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3403 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3404 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3405 DECL_IGNORED_P (fn) = 1;
3406 mark_used (fn);
3407
3408 DECL_BEFRIENDING_CLASSES (fn) = var;
3409
64924d1d 3410 set_global_binding (fn);
462819c8 3411 }
3412 return fn;
3413}
3414
3415/* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3416 variable VAR. The wrapper function calls the init function (if any) for
3417 VAR and then returns a reference to VAR. The wrapper function is used
3418 in place of VAR everywhere VAR is mentioned. */
3419
3420tree
3421get_tls_wrapper_fn (tree var)
3422{
3423 /* Only C++11 TLS vars need this wrapper fn. */
3424 if (!var_needs_tls_wrapper (var))
3425 return NULL_TREE;
3426
3427 tree sname = mangle_tls_wrapper_fn (var);
64924d1d 3428 tree fn = get_global_binding (sname);
462819c8 3429 if (!fn)
3430 {
3431 /* A named rvalue reference is an lvalue, so the wrapper should
3432 always return an lvalue reference. */
3433 tree type = non_reference (TREE_TYPE (var));
3434 type = build_reference_type (type);
3435 tree fntype = build_function_type (type, void_list_node);
3436 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3437 SET_DECL_LANGUAGE (fn, lang_c);
3438 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3439 DECL_ARTIFICIAL (fn) = true;
3440 DECL_IGNORED_P (fn) = 1;
3441 /* The wrapper is inline and emitted everywhere var is used. */
3442 DECL_DECLARED_INLINE_P (fn) = true;
3443 if (TREE_PUBLIC (var))
3444 {
3445 comdat_linkage (fn);
3446#ifdef HAVE_GAS_HIDDEN
3447 /* Make the wrapper bind locally; there's no reason to share
3448 the wrapper between multiple shared objects. */
3449 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3450 DECL_VISIBILITY_SPECIFIED (fn) = true;
3451#endif
3452 }
3453 if (!TREE_PUBLIC (fn))
3454 DECL_INTERFACE_KNOWN (fn) = true;
3455 mark_used (fn);
3456 note_vague_linkage_fn (fn);
3457
3458#if 0
3459 /* We want CSE to commonize calls to the wrapper, but marking it as
3460 pure is unsafe since it has side-effects. I guess we need a new
3461 ECF flag even weaker than ECF_PURE. FIXME! */
3462 DECL_PURE_P (fn) = true;
3463#endif
3464
3465 DECL_BEFRIENDING_CLASSES (fn) = var;
3466
64924d1d 3467 set_global_binding (fn);
462819c8 3468 }
3469 return fn;
3470}
3471
3472/* At EOF, generate the definition for the TLS wrapper function FN:
3473
3474 T& var_wrapper() {
3475 if (init_fn) init_fn();
3476 return var;
3477 } */
3478
3479static void
3480generate_tls_wrapper (tree fn)
3481{
3482 tree var = DECL_BEFRIENDING_CLASSES (fn);
3483
3484 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3485 tree body = begin_function_body ();
3486 /* Only call the init fn if there might be one. */
3487 if (tree init_fn = get_tls_init_fn (var))
3488 {
3489 tree if_stmt = NULL_TREE;
3490 /* If init_fn is a weakref, make sure it exists before calling. */
3491 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3492 {
3493 if_stmt = begin_if_stmt ();
3494 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3495 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3496 NE_EXPR, addr, nullptr_node,
3497 tf_warning_or_error);
3498 finish_if_stmt_cond (cond, if_stmt);
3499 }
3500 finish_expr_stmt (build_cxx_call
3501 (init_fn, 0, NULL, tf_warning_or_error));
3502 if (if_stmt)
3503 {
3504 finish_then_clause (if_stmt);
3505 finish_if_stmt (if_stmt);
3506 }
3507 }
1c4973d7 3508 else
3509 /* If there's no initialization, the wrapper is a constant function. */
3510 TREE_READONLY (fn) = true;
462819c8 3511 finish_return_stmt (convert_from_reference (var));
3512 finish_function_body (body);
4775c814 3513 expand_or_defer_fn (finish_function (/*inline_p=*/false));
462819c8 3514}
3515
7a160c55 3516/* Start the process of running a particular set of global constructors
b710ec85 3517 or destructors. Subroutine of do_[cd]tors. Also called from
3518 vtv_start_verification_constructor_init_function. */
7a160c55 3519
33851d97 3520static tree
26ac6687 3521start_objects (int method_type, int initp)
7a160c55 3522{
33851d97 3523 tree body;
3046c0a3 3524 tree fndecl;
62510893 3525 char type[14];
7a160c55 3526
3527 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3528
6d3fc1f2 3529 if (initp != DEFAULT_INIT_PRIORITY)
9bc65db1 3530 {
52a6e3cc 3531 char joiner;
3532
3533#ifdef JOINER
3534 joiner = JOINER;
3535#else
3536 joiner = '_';
3537#endif
9bc65db1 3538
62510893 3539 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
9bc65db1 3540 }
3541 else
62510893 3542 sprintf (type, "sub_%c", method_type);
9bc65db1 3543
9031d10b 3544 fndecl = build_lang_decl (FUNCTION_DECL,
db85cc4f 3545 get_file_function_name (type),
fb83f216 3546 build_function_type_list (void_type_node,
3547 NULL_TREE));
3046c0a3 3548 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
7a160c55 3549
a861fe52 3550 TREE_PUBLIC (current_function_decl) = 0;
e4d9ee75 3551
7ca587ef 3552 /* Mark as artificial because it's not explicitly in the user's
3553 source code. */
3554 DECL_ARTIFICIAL (current_function_decl) = 1;
3555
c24b2bff 3556 /* Mark this declaration as used to avoid spurious warnings. */
3557 TREE_USED (current_function_decl) = 1;
3558
a8ee6bb4 3559 /* Mark this function as a global constructor or destructor. */
3560 if (method_type == 'I')
3561 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3562 else
3563 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
a8ee6bb4 3564
2363ef00 3565 body = begin_compound_stmt (BCS_FN_BODY);
5ba298c9 3566
33851d97 3567 return body;
7a160c55 3568}
3569
3570/* Finish the process of running a particular set of global constructors
3571 or destructors. Subroutine of do_[cd]tors. */
3572
3573static void
26ac6687 3574finish_objects (int method_type, int initp, tree body)
7a160c55 3575{
33851d97 3576 tree fn;
7a160c55 3577
1f0fd02e 3578 /* Finish up. */
68f8f8cc 3579 finish_compound_stmt (body);
4775c814 3580 fn = finish_function (/*inline_p=*/false);
a3d5bae2 3581
09cd1329 3582 if (method_type == 'I')
01d15dc5 3583 {
09cd1329 3584 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3585 decl_init_priority_insert (fn, initp);
01d15dc5 3586 }
09cd1329 3587 else
3588 {
3589 DECL_STATIC_DESTRUCTOR (fn) = 1;
3590 decl_fini_priority_insert (fn, initp);
3591 }
3592
3593 expand_or_defer_fn (fn);
7a160c55 3594}
3595
cec1f6a6 3596/* The names of the parameters to the function created to handle
3597 initializations and destructions for objects with static storage
3598 duration. */
3599#define INITIALIZE_P_IDENTIFIER "__initialize_p"
3600#define PRIORITY_IDENTIFIER "__priority"
3601
3602/* The name of the function we create to handle initializations and
3603 destructions for objects with static storage duration. */
3604#define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3605
3606/* The declaration for the __INITIALIZE_P argument. */
1f3233d1 3607static GTY(()) tree initialize_p_decl;
cec1f6a6 3608
3609/* The declaration for the __PRIORITY argument. */
1f3233d1 3610static GTY(()) tree priority_decl;
cec1f6a6 3611
3612/* The declaration for the static storage duration function. */
1f3233d1 3613static GTY(()) tree ssdf_decl;
cec1f6a6 3614
5c8ddb8a 3615/* All the static storage duration functions created in this
3616 translation unit. */
f1f41a6c 3617static GTY(()) vec<tree, va_gc> *ssdf_decls;
5c8ddb8a 3618
cec1f6a6 3619/* A map from priority levels to information about that priority
3620 level. There may be many such levels, so efficient lookup is
3621 important. */
3622static splay_tree priority_info_map;
3623
3624/* Begins the generation of the function that will handle all
3625 initialization and destruction of objects with static storage
3626 duration. The function generated takes two parameters of type
3627 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3160db1d 3628 nonzero, it performs initializations. Otherwise, it performs
cec1f6a6 3629 destructions. It only performs those initializations or
3630 destructions with the indicated __PRIORITY. The generated function
9031d10b 3631 returns no value.
cec1f6a6 3632
3633 It is assumed that this function will only be called once per
3634 translation unit. */
7a160c55 3635
3f131089 3636static tree
911483bc 3637start_static_storage_duration_function (unsigned count)
7a160c55 3638{
cec1f6a6 3639 tree type;
3f131089 3640 tree body;
5c8ddb8a 3641 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3642
3643 /* Create the identifier for this function. It will be of the form
3644 SSDF_IDENTIFIER_<number>. */
911483bc 3645 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
cec1f6a6 3646
fb83f216 3647 type = build_function_type_list (void_type_node,
3648 integer_type_node, integer_type_node,
3649 NULL_TREE);
cec1f6a6 3650
3651 /* Create the FUNCTION_DECL itself. */
9031d10b 3652 ssdf_decl = build_lang_decl (FUNCTION_DECL,
5c8ddb8a 3653 get_identifier (id),
cec1f6a6 3654 type);
3655 TREE_PUBLIC (ssdf_decl) = 0;
3656 DECL_ARTIFICIAL (ssdf_decl) = 1;
5c8ddb8a 3657
3658 /* Put this function in the list of functions to be called from the
3659 static constructors and destructors. */
3660 if (!ssdf_decls)
3661 {
f1f41a6c 3662 vec_alloc (ssdf_decls, 32);
5c8ddb8a 3663
3664 /* Take this opportunity to initialize the map from priority
a109dc3b 3665 numbers to information about that priority level. */
5c8ddb8a 3666 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3667 /*delete_key_fn=*/0,
3668 /*delete_value_fn=*/
777e90ba 3669 splay_tree_delete_pointers);
5c8ddb8a 3670
3671 /* We always need to generate functions for the
3672 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3673 priorities later, we'll be sure to find the
3674 DEFAULT_INIT_PRIORITY. */
3675 get_priority_info (DEFAULT_INIT_PRIORITY);
3676 }
3677
f1f41a6c 3678 vec_safe_push (ssdf_decls, ssdf_decl);
cec1f6a6 3679
3680 /* Create the argument list. */
d145d8d5 3681 initialize_p_decl = cp_build_parm_decl
aa3ab9ba 3682 (ssdf_decl, get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
cec1f6a6 3683 TREE_USED (initialize_p_decl) = 1;
d145d8d5 3684 priority_decl = cp_build_parm_decl
aa3ab9ba 3685 (ssdf_decl, get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
cec1f6a6 3686 TREE_USED (priority_decl) = 1;
3687
1767a056 3688 DECL_CHAIN (initialize_p_decl) = priority_decl;
cec1f6a6 3689 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3690
96ff3e76 3691 /* Put the function in the global scope. */
3692 pushdecl (ssdf_decl);
3693
63eff20d 3694 /* Start the function itself. This is equivalent to declaring the
cec1f6a6 3695 function as:
3696
90d8d794 3697 static void __ssdf (int __initialize_p, init __priority_p);
9031d10b 3698
cec1f6a6 3699 It is static because we only need to call this function from the
3700 various constructor and destructor functions for this module. */
3046c0a3 3701 start_preparsed_function (ssdf_decl,
3702 /*attrs=*/NULL_TREE,
3703 SF_PRE_PARSED);
cec1f6a6 3704
3705 /* Set up the scope of the outermost block in the function. */
2363ef00 3706 body = begin_compound_stmt (BCS_FN_BODY);
9bc65db1 3707
3f131089 3708 return body;
7a160c55 3709}
3710
cec1f6a6 3711/* Finish the generation of the function which performs initialization
3712 and destruction of objects with static storage duration. After
3713 this point, no more such objects can be created. */
7a160c55 3714
3715static void
26ac6687 3716finish_static_storage_duration_function (tree body)
7a160c55 3717{
cec1f6a6 3718 /* Close out the function. */
68f8f8cc 3719 finish_compound_stmt (body);
4775c814 3720 expand_or_defer_fn (finish_function (/*inline_p=*/false));
cec1f6a6 3721}
7a160c55 3722
cec1f6a6 3723/* Return the information about the indicated PRIORITY level. If no
3724 code to handle this level has yet been generated, generate the
3725 appropriate prologue. */
7a160c55 3726
cec1f6a6 3727static priority_info
26ac6687 3728get_priority_info (int priority)
cec1f6a6 3729{
3730 priority_info pi;
3731 splay_tree_node n;
3732
9031d10b 3733 n = splay_tree_lookup (priority_info_map,
cec1f6a6 3734 (splay_tree_key) priority);
3735 if (!n)
3736 {
3737 /* Create a new priority information structure, and insert it
3738 into the map. */
56e60747 3739 pi = XNEW (struct priority_info_s);
5c8ddb8a 3740 pi->initializations_p = 0;
3741 pi->destructions_p = 0;
cec1f6a6 3742 splay_tree_insert (priority_info_map,
3743 (splay_tree_key) priority,
3744 (splay_tree_value) pi);
3745 }
3746 else
3747 pi = (priority_info) n->value;
7a160c55 3748
cec1f6a6 3749 return pi;
3750}
7a160c55 3751
a9f11a62 3752/* The effective initialization priority of a DECL. */
3753
3754#define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3755 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3756 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3757
ae915c82 3758/* Whether a DECL needs a guard to protect it against multiple
a9f11a62 3759 initialization. */
3760
3761#define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3762 || DECL_ONE_ONLY (decl) \
3763 || DECL_WEAK (decl)))
3764
199a63ca 3765/* Called from one_static_initialization_or_destruction(),
3766 via walk_tree.
3767 Walks the initializer list of a global variable and looks for
3768 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3769 and that have their DECL_CONTEXT() == NULL.
3770 For each such temporary variable, set their DECL_CONTEXT() to
3771 the current function. This is necessary because otherwise
3772 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3773 when trying to refer to a temporary variable that does not have
3774 it's DECL_CONTECT() properly set. */
3775static tree
3776fix_temporary_vars_context_r (tree *node,
a49c5913 3777 int * /*unused*/,
3778 void * /*unused1*/)
199a63ca 3779{
3780 gcc_assert (current_function_decl);
3781
3782 if (TREE_CODE (*node) == BIND_EXPR)
3783 {
3784 tree var;
3785
1767a056 3786 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
80a58eb0 3787 if (VAR_P (var)
199a63ca 3788 && !DECL_NAME (var)
3789 && DECL_ARTIFICIAL (var)
3790 && !DECL_CONTEXT (var))
3791 DECL_CONTEXT (var) = current_function_decl;
3792 }
3793
3794 return NULL_TREE;
3795}
3796
3f131089 3797/* Set up to handle the initialization or destruction of DECL. If
3160db1d 3798 INITP is nonzero, we are initializing the variable. Otherwise, we
3f131089 3799 are destroying it. */
3800
a9f11a62 3801static void
3802one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3f131089 3803{
4eb32e62 3804 tree guard_if_stmt = NULL_TREE;
4eb32e62 3805 tree guard;
3f131089 3806
a9f11a62 3807 /* If we are supposed to destruct and there's a trivial destructor,
3808 nothing has to be done. */
3809 if (!initp
3810 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3811 return;
3f131089 3812
3813 /* Trick the compiler into thinking we are at the file and line
3814 where DECL was declared so that error-messages make sense, and so
3815 that the debugger will show somewhat sensible file and line
3816 information. */
346064d9 3817 input_location = DECL_SOURCE_LOCATION (decl);
3f131089 3818
199a63ca 3819 /* Make sure temporary variables in the initialiser all have
3820 their DECL_CONTEXT() set to a value different from NULL_TREE.
2fbe7a32 3821 This can happen when global variables initializers are built.
199a63ca 3822 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3823 the temporary variables that might have been generated in the
2fbe7a32 3824 accompanying initializers is NULL_TREE, meaning the variables have been
199a63ca 3825 declared in the global namespace.
3826 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3827 of the temporaries are set to the current function decl. */
3828 cp_walk_tree_without_duplicates (&init,
3829 fix_temporary_vars_context_r,
3830 NULL);
3831
3f131089 3832 /* Because of:
3833
3834 [class.access.spec]
3835
3836 Access control for implicit calls to the constructors,
3837 the conversion functions, or the destructor called to
3838 create and destroy a static data member is performed as
3839 if these calls appeared in the scope of the member's
9031d10b 3840 class.
3f131089 3841
3842 we pretend we are in a static member function of the class of
3843 which the DECL is a member. */
3844 if (member_p (decl))
3845 {
9ba4048d 3846 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3f131089 3847 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3848 }
9031d10b 3849
4eb32e62 3850 /* Assume we don't need a guard. */
3851 guard = NULL_TREE;
3852 /* We need a guard if this is an object with external linkage that
98eaf693 3853 might be initialized in more than one place. (For example, a
3854 static data member of a template, when the data member requires
3855 construction.) */
a9f11a62 3856 if (NEEDS_GUARD_P (decl))
3f131089 3857 {
4eb32e62 3858 tree guard_cond;
3f131089 3859
4eb32e62 3860 guard = get_guard (decl);
3f131089 3861
4eb32e62 3862 /* When using __cxa_atexit, we just check the GUARD as we would
3863 for a local static. */
3864 if (flag_use_cxa_atexit)
3865 {
3866 /* When using __cxa_atexit, we never try to destroy
3867 anything from a static destructor. */
b4df430b 3868 gcc_assert (initp);
0925e39e 3869 guard_cond = get_guard_cond (guard, false);
4eb32e62 3870 }
7c24d881 3871 /* If we don't have __cxa_atexit, then we will be running
3872 destructors from .fini sections, or their equivalents. So,
3873 we need to know how many times we've tried to initialize this
3874 object. We do initializations only if the GUARD is zero,
3875 i.e., if we are the first to initialize the variable. We do
3876 destructions only if the GUARD is one, i.e., if we are the
3877 last to destroy the variable. */
4eb32e62 3878 else if (initp)
9031d10b 3879 guard_cond
8e70fb09 3880 = cp_build_binary_op (input_location,
3881 EQ_EXPR,
ebd21de4 3882 cp_build_unary_op (PREINCREMENT_EXPR,
8e70fb09 3883 guard,
b99cc6da 3884 /*noconvert=*/true,
8e70fb09 3885 tf_warning_or_error),
ebd21de4 3886 integer_one_node,
3887 tf_warning_or_error);
3f131089 3888 else
9031d10b 3889 guard_cond
8e70fb09 3890 = cp_build_binary_op (input_location,
3891 EQ_EXPR,
ebd21de4 3892 cp_build_unary_op (PREDECREMENT_EXPR,
8e70fb09 3893 guard,
b99cc6da 3894 /*noconvert=*/true,
8e70fb09 3895 tf_warning_or_error),
ebd21de4 3896 integer_zero_node,
3897 tf_warning_or_error);
29d00ba7 3898
a9f11a62 3899 guard_if_stmt = begin_if_stmt ();
3900 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3f131089 3901 }
3902
4eb32e62 3903
7c24d881 3904 /* If we're using __cxa_atexit, we have not already set the GUARD,
3905 so we must do so now. */
3906 if (guard && initp && flag_use_cxa_atexit)
4eb32e62 3907 finish_expr_stmt (set_guard (guard));
3f131089 3908
a9f11a62 3909 /* Perform the initialization or destruction. */
3910 if (initp)
3911 {
3912 if (init)
085f6ebf 3913 {
3914 finish_expr_stmt (init);
9917317a 3915 if (sanitize_flags_p (SANITIZE_ADDRESS, decl))
085f6ebf 3916 {
97221fd7 3917 varpool_node *vnode = varpool_node::get (decl);
085f6ebf 3918 if (vnode)
3919 vnode->dynamically_initialized = 1;
3920 }
3921 }
3f131089 3922
a9f11a62 3923 /* If we're using __cxa_atexit, register a function that calls the
074ab442 3924 destructor for the object. */
a9f11a62 3925 if (flag_use_cxa_atexit)
074ab442 3926 finish_expr_stmt (register_dtor_fn (decl));
a9f11a62 3927 }
3928 else
3929 finish_expr_stmt (build_cleanup (decl));
3f131089 3930
a9f11a62 3931 /* Finish the guard if-stmt, if necessary. */
3932 if (guard)
3933 {
3934 finish_then_clause (guard_if_stmt);
3935 finish_if_stmt (guard_if_stmt);
3936 }
3f131089 3937
3938 /* Now that we're done with DECL we don't need to pretend to be a
3939 member of its class any longer. */
9ba4048d 3940 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3f131089 3941 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3942}
3943
a9f11a62 3944/* Generate code to do the initialization or destruction of the decls in VARS,
3945 a TREE_LIST of VAR_DECL with static storage duration.
3946 Whether initialization or destruction is performed is specified by INITP. */
7a160c55 3947
cec1f6a6 3948static void
a9f11a62 3949do_static_initialization_or_destruction (tree vars, bool initp)
cec1f6a6 3950{
a9f11a62 3951 tree node, init_if_stmt, cond;
cec1f6a6 3952
a9f11a62 3953 /* Build the outer if-stmt to check for initialization or destruction. */
3954 init_if_stmt = begin_if_stmt ();
3955 cond = initp ? integer_one_node : integer_zero_node;
8e70fb09 3956 cond = cp_build_binary_op (input_location,
3957 EQ_EXPR,
ebd21de4 3958 initialize_p_decl,
3959 cond,
3960 tf_warning_or_error);
a9f11a62 3961 finish_if_stmt_cond (cond, init_if_stmt);
3962
085f6ebf 3963 /* To make sure dynamic construction doesn't access globals from other
3964 compilation units where they might not be yet constructed, for
3965 -fsanitize=address insert __asan_before_dynamic_init call that
3966 prevents access to either all global variables that need construction
3967 in other compilation units, or at least those that haven't been
3968 initialized yet. Variables that need dynamic construction in
3969 the current compilation unit are kept accessible. */
a940fdc7 3970 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
085f6ebf 3971 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3972
a9f11a62 3973 node = vars;
3974 do {
3975 tree decl = TREE_VALUE (node);
3976 tree priority_if_stmt;
3977 int priority;
3978 priority_info pi;
3979
3980 /* If we don't need a destructor, there's nothing to do. Avoid
3981 creating a possibly empty if-stmt. */
3982 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3983 {
3984 node = TREE_CHAIN (node);
3985 continue;
3986 }
3987
3988 /* Remember that we had an initialization or finalization at this
3989 priority. */
3990 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3991 pi = get_priority_info (priority);
3992 if (initp)
3993 pi->initializations_p = 1;
3994 else
3995 pi->destructions_p = 1;
3996
3997 /* Conditionalize this initialization on being in the right priority
3998 and being initializing/finalizing appropriately. */
3999 priority_if_stmt = begin_if_stmt ();
8e70fb09 4000 cond = cp_build_binary_op (input_location,
4001 EQ_EXPR,
a9f11a62 4002 priority_decl,
ebd21de4 4003 build_int_cst (NULL_TREE, priority),
4004 tf_warning_or_error);
a9f11a62 4005 finish_if_stmt_cond (cond, priority_if_stmt);
4006
4007 /* Process initializers with same priority. */
4008 for (; node
4009 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
4010 node = TREE_CHAIN (node))
4011 /* Do one initialization or destruction. */
4012 one_static_initialization_or_destruction (TREE_VALUE (node),
074ab442 4013 TREE_PURPOSE (node), initp);
a9f11a62 4014
4015 /* Finish up the priority if-stmt body. */
4016 finish_then_clause (priority_if_stmt);
4017 finish_if_stmt (priority_if_stmt);
4018
4019 } while (node);
4020
085f6ebf 4021 /* Revert what __asan_before_dynamic_init did by calling
4022 __asan_after_dynamic_init. */
a940fdc7 4023 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
085f6ebf 4024 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
4025
a9f11a62 4026 /* Finish up the init/destruct if-stmt body. */
4027 finish_then_clause (init_if_stmt);
4028 finish_if_stmt (init_if_stmt);
7a160c55 4029}
4030
3f131089 4031/* VARS is a list of variables with static storage duration which may
4032 need initialization and/or finalization. Remove those variables
4033 that don't really need to be initialized or finalized, and return
4034 the resulting list. The order in which the variables appear in
4035 VARS is in reverse order of the order in which they should actually
4036 be initialized. The list we return is in the unreversed order;
4037 i.e., the first variable should be initialized first. */
471086d6 4038
3f131089 4039static tree
cd4dc755 4040prune_vars_needing_no_initialization (tree *vars)
471086d6 4041{
cd4dc755 4042 tree *var = vars;
4043 tree result = NULL_TREE;
471086d6 4044
cd4dc755 4045 while (*var)
3f131089 4046 {
cd4dc755 4047 tree t = *var;
4048 tree decl = TREE_VALUE (t);
4049 tree init = TREE_PURPOSE (t);
f96b25bb 4050
3f131089 4051 /* Deal gracefully with error. */
c209618b 4052 if (error_operand_p (decl))
cd4dc755 4053 {
4054 var = &TREE_CHAIN (t);
4055 continue;
4056 }
e857e9c7 4057
3f131089 4058 /* The only things that can be initialized are variables. */
80a58eb0 4059 gcc_assert (VAR_P (decl));
471086d6 4060
3f131089 4061 /* If this object is not defined, we don't need to do anything
4062 here. */
4063 if (DECL_EXTERNAL (decl))
cd4dc755 4064 {
4065 var = &TREE_CHAIN (t);
4066 continue;
4067 }
471086d6 4068
3f131089 4069 /* Also, if the initializer already contains errors, we can bail
4070 out now. */
9031d10b 4071 if (init && TREE_CODE (init) == TREE_LIST
3f131089 4072 && value_member (error_mark_node, init))
cd4dc755 4073 {
4074 var = &TREE_CHAIN (t);
4075 continue;
4076 }
42f3e1b9 4077
3f131089 4078 /* This variable is going to need initialization and/or
4079 finalization, so we add it to the list. */
cd4dc755 4080 *var = TREE_CHAIN (t);
4081 TREE_CHAIN (t) = result;
4082 result = t;
3f131089 4083 }
999c9c6f 4084
3f131089 4085 return result;
4086}
999c9c6f 4087
3f131089 4088/* Make sure we have told the back end about all the variables in
4089 VARS. */
cec1f6a6 4090
3f131089 4091static void
26ac6687 4092write_out_vars (tree vars)
3f131089 4093{
4094 tree v;
cec1f6a6 4095
3f131089 4096 for (v = vars; v; v = TREE_CHAIN (v))
caa6fdce 4097 {
4098 tree var = TREE_VALUE (v);
4099 if (!var_finalized_p (var))
4100 {
4101 import_export_decl (var);
b2c4af5e 4102 rest_of_decl_compilation (var, 1, 1);
caa6fdce 4103 }
4104 }
cec1f6a6 4105}
4742975e 4106
cec1f6a6 4107/* Generate a static constructor (if CONSTRUCTOR_P) or destructor
5291a9d1 4108 (otherwise) that will initialize all global objects with static
cec1f6a6 4109 storage duration having the indicated PRIORITY. */
d3ce0f51 4110
cec1f6a6 4111static void
911483bc 4112generate_ctor_or_dtor_function (bool constructor_p, int priority,
4113 location_t *locus)
cec1f6a6 4114{
4115 char function_key;
1d934aae 4116 tree fndecl;
33851d97 4117 tree body;
5c8ddb8a 4118 size_t i;
d3ce0f51 4119
6a86d77b 4120 input_location = *locus;
357f7efa 4121 /* ??? */
9c85a98a 4122 /* Was: locus->line++; */
9031d10b 4123
cec1f6a6 4124 /* We use `I' to indicate initialization and `D' to indicate
4125 destruction. */
1d934aae 4126 function_key = constructor_p ? 'I' : 'D';
d3ce0f51 4127
1d934aae 4128 /* We emit the function lazily, to avoid generating empty
4129 global constructors and destructors. */
4130 body = NULL_TREE;
8417823c 4131
7a4e126b 4132 /* For Objective-C++, we may need to initialize metadata found in this module.
4133 This must be done _before_ any other static initializations. */
4134 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
4135 && constructor_p && objc_static_init_needed_p ())
4136 {
4137 body = start_objects (function_key, priority);
9af7fd5b 4138 objc_generate_static_init_call (NULL_TREE);
7a4e126b 4139 }
4140
cec1f6a6 4141 /* Call the static storage duration function with appropriate
4142 arguments. */
f1f41a6c 4143 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
ff1774cc 4144 {
4145 /* Calls to pure or const functions will expand to nothing. */
4146 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
4147 {
054da6d7 4148 tree call;
4149
ff1774cc 4150 if (! body)
4151 body = start_objects (function_key, priority);
4152
054da6d7 4153 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
4154 build_int_cst (NULL_TREE,
4155 constructor_p),
4156 build_int_cst (NULL_TREE,
4157 priority),
4158 NULL_TREE);
4159 finish_expr_stmt (call);
ff1774cc 4160 }
4161 }
4742975e 4162
cec1f6a6 4163 /* Close out the function. */
1d934aae 4164 if (body)
4165 finish_objects (function_key, priority, body);
cec1f6a6 4166}
dc76caae 4167
cec1f6a6 4168/* Generate constructor and destructor functions for the priority
5c8ddb8a 4169 indicated by N. */
dc76caae 4170
cec1f6a6 4171static int
911483bc 4172generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
cec1f6a6 4173{
cc52f165 4174 location_t *locus = (location_t *) data;
cec1f6a6 4175 int priority = (int) n->key;
4176 priority_info pi = (priority_info) n->value;
cec1f6a6 4177
4178 /* Generate the functions themselves, but only if they are really
4179 needed. */
9af7fd5b 4180 if (pi->initializations_p)
911483bc 4181 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
9af7fd5b 4182 if (pi->destructions_p)
911483bc 4183 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
cec1f6a6 4184
4185 /* Keep iterating. */
4186 return 0;
4187}
9bc65db1 4188
b8254e80 4189/* Return C++ property of T, based on given operation OP. */
4190
4191static int
c9743c6a 4192cpp_check (tree t, cpp_operation op)
b8254e80 4193{
4194 switch (op)
4195 {
99838ed7 4196 case HAS_DEPENDENT_TEMPLATE_ARGS:
4197 {
4198 tree ti = CLASSTYPE_TEMPLATE_INFO (t);
4199 if (!ti)
4200 return 0;
4201 ++processing_template_decl;
4202 const bool dep = any_dependent_template_arguments_p (TI_ARGS (ti));
4203 --processing_template_decl;
4204 return dep;
4205 }
b8254e80 4206 case IS_ABSTRACT:
4207 return DECL_PURE_VIRTUAL_P (t);
4208 case IS_CONSTRUCTOR:
4209 return DECL_CONSTRUCTOR_P (t);
4210 case IS_DESTRUCTOR:
4211 return DECL_DESTRUCTOR_P (t);
4212 case IS_COPY_CONSTRUCTOR:
4213 return DECL_COPY_CONSTRUCTOR_P (t);
67ede3e4 4214 case IS_MOVE_CONSTRUCTOR:
4215 return DECL_MOVE_CONSTRUCTOR_P (t);
b8254e80 4216 case IS_TEMPLATE:
4217 return TREE_CODE (t) == TEMPLATE_DECL;
e2a6a333 4218 case IS_TRIVIAL:
4219 return trivial_type_p (t);
b8254e80 4220 default:
4221 return 0;
4222 }
4223}
4224
4225/* Collect source file references recursively, starting from NAMESPC. */
4226
4227static void
4228collect_source_refs (tree namespc)
4229{
b8254e80 4230 /* Iterate over names in this name space. */
f7564df4 4231 for (tree t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4232 if (DECL_IS_BUILTIN (t))
4233 ;
4234 else if (TREE_CODE (t) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (t))
4235 collect_source_refs (t);
4236 else
b8254e80 4237 collect_source_ref (DECL_SOURCE_FILE (t));
b8254e80 4238}
4239
4240/* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4241 starting from NAMESPC. */
4242
4243static void
4244collect_ada_namespace (tree namespc, const char *source_file)
4245{
f7564df4 4246 tree decl = NAMESPACE_LEVEL (namespc)->names;
b8254e80 4247
f7564df4 4248 /* Collect decls from this namespace. This will skip
4249 NAMESPACE_DECLs (both aliases and regular, it cannot tell). */
4250 collect_ada_nodes (decl, source_file);
b8254e80 4251
f7564df4 4252 /* Now scan for namespace children, and dump them. */
4253 for (; decl; decl = TREE_CHAIN (decl))
4254 if (TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl))
4255 collect_ada_namespace (decl, source_file);
b8254e80 4256}
4257
74d6b34b 4258/* Returns true iff there is a definition available for variable or
4259 function DECL. */
4260
31db86df 4261bool
74d6b34b 4262decl_defined_p (tree decl)
4263{
4264 if (TREE_CODE (decl) == FUNCTION_DECL)
31db86df 4265 return (DECL_INITIAL (decl) != NULL_TREE
4266 /* A pending instantiation of a friend temploid is defined. */
4267 || (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
4268 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4269 (DECL_TI_TEMPLATE (decl)))));
74d6b34b 4270 else
4271 {
80a58eb0 4272 gcc_assert (VAR_P (decl));
74d6b34b 4273 return !DECL_EXTERNAL (decl);
4274 }
4275}
4276
d58b8a94 4277/* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4278
4279 [expr.const]
4280
4281 An integral constant-expression can only involve ... const
4282 variables of integral or enumeration types initialized with
4283 constant expressions ...
4284
4285 C++0x also allows constexpr variables and temporaries initialized
4286 with constant expressions. We handle the former here, but the latter
4287 are just folded away in cxx_eval_constant_expression.
4288
4289 The standard does not require that the expression be non-volatile.
4290 G++ implements the proposed correction in DR 457. */
4291
4292bool
4293decl_constant_var_p (tree decl)
4294{
9b4d7ff5 4295 if (!decl_maybe_constant_var_p (decl))
d58b8a94 4296 return false;
d58b8a94 4297
9b4d7ff5 4298 /* We don't know if a template static data member is initialized with
4299 a constant expression until we instantiate its initializer. Even
4300 in the case of a constexpr variable, we can't treat it as a
4301 constant until its initializer is complete in case it's used in
4302 its own initializer. */
068632d3 4303 maybe_instantiate_decl (decl);
9b4d7ff5 4304 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
d58b8a94 4305}
4306
4307/* Returns true if DECL could be a symbolic constant variable, depending on
4308 its initializer. */
4309
4310bool
4311decl_maybe_constant_var_p (tree decl)
4312{
4313 tree type = TREE_TYPE (decl);
80a58eb0 4314 if (!VAR_P (decl))
d58b8a94 4315 return false;
04ced4a2 4316 if (DECL_DECLARED_CONSTEXPR_P (decl) && !TREE_THIS_VOLATILE (decl))
d58b8a94 4317 return true;
354aade3 4318 if (DECL_HAS_VALUE_EXPR_P (decl))
a2cd9c5f 4319 /* A proxy isn't constant. */
4320 return false;
90ad495b 4321 if (TYPE_REF_P (type))
8d6486e1 4322 /* References can be constant. */;
4323 else if (CP_TYPE_CONST_NON_VOLATILE_P (type)
4324 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4325 /* And const integers. */;
4326 else
4327 return false;
4328
4329 if (DECL_INITIAL (decl)
4330 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
4331 /* We know the initializer, and it isn't constant. */
4332 return false;
4333 else
ac6641ca 4334 return true;
d58b8a94 4335}
4336
2381bbfa 4337/* Complain that DECL uses a type with no linkage. In C++98 mode this is
4338 called from grokfndecl and grokvardecl; in all modes it is called from
4339 cp_write_global_declarations. */
74d6b34b 4340
2381bbfa 4341void
74d6b34b 4342no_linkage_error (tree decl)
4343{
2381bbfa 4344 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4345 /* In C++11 it's ok if the decl is defined. */
4346 return;
74d6b34b 4347 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
2381bbfa 4348 if (t == NULL_TREE)
4349 /* The type that got us on no_linkage_decls must have gotten a name for
4350 linkage purposes. */;
4351 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4352 /* The type might end up having a typedef name for linkage purposes. */
4353 vec_safe_push (no_linkage_decls, decl);
4f86cbb0 4354 else if (TYPE_UNNAMED_P (t))
74d6b34b 4355 {
2381bbfa 4356 bool d = false;
bc35ef65 4357 auto_diagnostic_group grp;
2381bbfa 4358 if (cxx_dialect >= cxx11)
4359 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4f86cbb0 4360 "unnamed type, is used but never defined", decl);
2381bbfa 4361 else if (DECL_EXTERN_C_P (decl))
4362 /* Allow this; it's pretty common in C. */;
f4ae4202 4363 else if (VAR_P (decl))
2381bbfa 4364 /* DRs 132, 319 and 389 seem to indicate types with
4365 no linkage can only be used to declare extern "C"
4366 entities. Since it's not always an error in the
4367 ISO C++ 90 Standard, we only issue a warning. */
4f86cbb0 4368 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "unnamed type "
2381bbfa 4369 "with no linkage used to declare variable %q#D with "
4370 "linkage", decl);
4371 else
4f86cbb0 4372 d = permerror (DECL_SOURCE_LOCATION (decl), "unnamed type with no "
2381bbfa 4373 "linkage used to declare function %q#D with linkage",
4374 decl);
4375 if (d && is_typedef_decl (TYPE_NAME (t)))
4376 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4377 "to the unqualified type, so it is not used for linkage",
4378 TYPE_NAME (t));
74d6b34b 4379 }
2381bbfa 4380 else if (cxx_dialect >= cxx11)
e9f7d2bf 4381 {
f4ae4202 4382 if (VAR_P (decl) || !DECL_PURE_VIRTUAL_P (decl))
e9f7d2bf 4383 permerror (DECL_SOURCE_LOCATION (decl),
4384 "%q#D, declared using local type "
4385 "%qT, is used but never defined", decl, t);
4386 }
f4ae4202 4387 else if (VAR_P (decl))
2381bbfa 4388 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4389 "used to declare variable %q#D with linkage", t, decl);
74d6b34b 4390 else
2381bbfa 4391 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4392 "to declare function %q#D with linkage", t, decl);
74d6b34b 4393}
4394
b8254e80 4395/* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4396
4397static void
4398collect_all_refs (const char *source_file)
4399{
4400 collect_ada_namespace (global_namespace, source_file);
4401}
4402
d3d35aba 4403/* Clear DECL_EXTERNAL for NODE. */
4404
4405static bool
a49c5913 4406clear_decl_external (struct cgraph_node *node, void * /*data*/)
d3d35aba 4407{
02774f2d 4408 DECL_EXTERNAL (node->decl) = 0;
d3d35aba 4409 return false;
4410}
4411
462819c8 4412/* Build up the function to run dynamic initializers for thread_local
4413 variables in this translation unit and alias the init functions for the
4414 individual variables to it. */
4415
4416static void
4417handle_tls_init (void)
4418{
4419 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4420 if (vars == NULL_TREE)
4421 return;
4422
4423 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4424
462819c8 4425 write_out_vars (vars);
4426
4427 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4428 boolean_type_node);
4429 TREE_PUBLIC (guard) = false;
4430 TREE_STATIC (guard) = true;
4431 DECL_ARTIFICIAL (guard) = true;
4432 DECL_IGNORED_P (guard) = true;
4433 TREE_USED (guard) = true;
800478e6 4434 CP_DECL_THREAD_LOCAL_P (guard) = true;
5e68df57 4435 set_decl_tls_model (guard, decl_default_tls_model (guard));
eab1674f 4436 pushdecl_top_level_and_finish (guard, NULL_TREE);
462819c8 4437
1c4973d7 4438 tree fn = get_local_tls_init_fn ();
462819c8 4439 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4440 tree body = begin_function_body ();
4441 tree if_stmt = begin_if_stmt ();
4442 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4443 tf_warning_or_error);
4444 finish_if_stmt_cond (cond, if_stmt);
22a3f7bd 4445 finish_expr_stmt (cp_build_modify_expr (loc, guard, NOP_EXPR,
4446 boolean_true_node,
462819c8 4447 tf_warning_or_error));
4448 for (; vars; vars = TREE_CHAIN (vars))
4449 {
4450 tree var = TREE_VALUE (vars);
4451 tree init = TREE_PURPOSE (vars);
4452 one_static_initialization_or_destruction (var, init, true);
4453
1c4973d7 4454 /* Output init aliases even with -fno-extern-tls-init. */
07b8a412 4455 if (TARGET_SUPPORTS_ALIASES && TREE_PUBLIC (var))
1c4973d7 4456 {
4457 tree single_init_fn = get_tls_init_fn (var);
3d7d57de 4458 if (single_init_fn == NULL_TREE)
4459 continue;
1c4973d7 4460 cgraph_node *alias
415d1b9a 4461 = cgraph_node::get_create (fn)->create_same_body_alias
4462 (single_init_fn, fn);
1c4973d7 4463 gcc_assert (alias != NULL);
4464 }
462819c8 4465 }
4466
4467 finish_then_clause (if_stmt);
4468 finish_if_stmt (if_stmt);
4469 finish_function_body (body);
4775c814 4470 expand_or_defer_fn (finish_function (/*inline_p=*/false));
462819c8 4471}
4472
6e9c0030 4473/* We're at the end of compilation, so generate any mangling aliases that
4474 we've been saving up, if DECL is going to be output and ID2 isn't
4475 already taken by another declaration. */
4476
4477static void
4478generate_mangling_alias (tree decl, tree id2)
4479{
e845448c 4480 struct cgraph_node *n = NULL;
4481
4482 if (TREE_CODE (decl) == FUNCTION_DECL)
4483 {
4484 n = cgraph_node::get (decl);
4485 if (!n)
4486 /* Don't create an alias to an unreferenced function. */
4487 return;
4488 }
4489
6ff24f58 4490 tree *slot
4491 = mangled_decls->find_slot_with_hash (id2, IDENTIFIER_HASH_VALUE (id2),
4492 INSERT);
e845448c 4493
6e9c0030 4494 /* If there's a declaration already using this mangled name,
4495 don't create a compatibility alias that conflicts. */
6ff24f58 4496 if (*slot)
6e9c0030 4497 return;
4498
4499 tree alias = make_alias_for (decl, id2);
e845448c 4500 *slot = alias;
4501
6e9c0030 4502 DECL_IGNORED_P (alias) = 1;
4503 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
4504 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
4505 if (vague_linkage_p (decl))
4506 DECL_WEAK (alias) = 1;
e845448c 4507
4508 if (n)
6e9c0030 4509 n->create_same_body_alias (alias, decl);
4510 else
4511 varpool_node::create_extra_name_alias (alias, decl);
4512}
4513
4514/* Note that we might want to emit an alias with the symbol ID2 for DECL at
4515 the end of translation, for compatibility across bugs in the mangling
4516 implementation. */
4517
4518void
07b8a412 4519note_mangling_alias (tree decl, tree id2)
6e9c0030 4520{
07b8a412 4521 if (TARGET_SUPPORTS_ALIASES)
6e9c0030 4522 {
07b8a412 4523 if (!defer_mangling_aliases)
4524 generate_mangling_alias (decl, id2);
4525 else
4526 {
4527 vec_safe_push (mangling_aliases, decl);
4528 vec_safe_push (mangling_aliases, id2);
4529 }
6e9c0030 4530 }
6e9c0030 4531}
4532
d3d81eb2 4533/* Emit all mangling aliases that were deferred up to this point. */
4534
4535void
6e9c0030 4536generate_mangling_aliases ()
4537{
4538 while (!vec_safe_is_empty (mangling_aliases))
4539 {
4540 tree id2 = mangling_aliases->pop();
4541 tree decl = mangling_aliases->pop();
4542 generate_mangling_alias (decl, id2);
4543 }
d3d81eb2 4544 defer_mangling_aliases = false;
6e9c0030 4545}
4546
e845448c 4547/* Record a mangling of DECL, whose DECL_ASSEMBLER_NAME has just been
4548 set. NEED_WARNING is true if we must warn about collisions. We do
4549 this to spot changes in mangling that may require compatibility
4550 aliases. */
4551
4552void
4553record_mangling (tree decl, bool need_warning)
4554{
4555 if (!mangled_decls)
6ff24f58 4556 mangled_decls = hash_table<mangled_decl_hash>::create_ggc (499);
e845448c 4557
4558 gcc_checking_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
971295a0 4559 tree id = DECL_ASSEMBLER_NAME_RAW (decl);
6ff24f58 4560 tree *slot
4561 = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id),
4562 INSERT);
e845448c 4563
4564 /* If this is already an alias, remove the alias, because the real
391f5d5d 4565 decl takes precedence. */
6ff24f58 4566 if (*slot && DECL_ARTIFICIAL (*slot) && DECL_IGNORED_P (*slot))
e845448c 4567 if (symtab_node *n = symtab_node::get (*slot))
4568 if (n->cpp_implicit_alias)
4569 {
4570 n->remove ();
6ff24f58 4571 *slot = NULL_TREE;
e845448c 4572 }
4573
6ff24f58 4574 if (!*slot)
e845448c 4575 *slot = decl;
4576 else if (need_warning)
4577 {
4578 error_at (DECL_SOURCE_LOCATION (decl),
4579 "mangling of %q#D as %qE conflicts with a previous mangle",
4580 decl, id);
4581 inform (DECL_SOURCE_LOCATION (*slot),
4582 "previous mangling %q#D", *slot);
4583 inform (DECL_SOURCE_LOCATION (decl),
4584 "a later -fabi-version= (or =0)"
4585 " avoids this error with a change in mangling");
4586 *slot = decl;
4587 }
4588}
4589
7c129b68 4590/* The mangled name of DECL is being forcibly changed to NAME. Remove
4591 any existing knowledge of DECL's mangled name meaning DECL. */
4592
4593void
4594overwrite_mangling (tree decl, tree name)
4595{
4596 if (tree id = DECL_ASSEMBLER_NAME_RAW (decl))
4597 if ((TREE_CODE (decl) == VAR_DECL
4598 || TREE_CODE (decl) == FUNCTION_DECL)
4599 && mangled_decls)
4600 if (tree *slot
4601 = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id),
4602 NO_INSERT))
4603 if (*slot == decl)
4604 {
4605 mangled_decls->clear_slot (slot);
4606
4607 /* If this is an alias, remove it from the symbol table. */
4608 if (DECL_ARTIFICIAL (decl) && DECL_IGNORED_P (decl))
4609 if (symtab_node *n = symtab_node::get (decl))
4610 if (n->cpp_implicit_alias)
4611 n->remove ();
4612 }
4613
4614 DECL_ASSEMBLER_NAME_RAW (decl) = name;
4615}
4616
ddc9a346 4617/* The entire file is now complete. If requested, dump everything
4618 to a file. */
4619
4620static void
4621dump_tu (void)
4622{
3f6e5ced 4623 dump_flags_t flags;
88df6264 4624 if (FILE *stream = dump_begin (raw_dump_id, &flags))
ddc9a346 4625 {
4626 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
88df6264 4627 dump_end (raw_dump_id, stream);
ddc9a346 4628 }
4629}
4630
3a1c9df2 4631static location_t locus_at_end_of_parsing;
4632
d1856d2c 4633/* Check the deallocation functions for CODE to see if we want to warn that
4634 only one was defined. */
4635
4636static void
4637maybe_warn_sized_delete (enum tree_code code)
4638{
4639 tree sized = NULL_TREE;
4640 tree unsized = NULL_TREE;
4641
ca16a224 4642 for (ovl_iterator iter (get_global_binding (ovl_op_identifier (false, code)));
97a86f58 4643 iter; ++iter)
d1856d2c 4644 {
97a86f58 4645 tree fn = *iter;
d1856d2c 4646 /* We're only interested in usual deallocation functions. */
db8ffb40 4647 if (!usual_deallocation_fn_p (fn))
d1856d2c 4648 continue;
4649 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
4650 unsized = fn;
4651 else
4652 sized = fn;
4653 }
4654 if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
4655 warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
4656 "the program should also define %qD", sized);
4657 else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
4658 warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
4659 "the program should also define %qD", unsized);
4660}
4661
4662/* Check the global deallocation functions to see if we want to warn about
4663 defining unsized without sized (or vice versa). */
4664
4665static void
4666maybe_warn_sized_delete ()
4667{
4668 if (!flag_sized_deallocation || !warn_sized_deallocation)
4669 return;
4670 maybe_warn_sized_delete (DELETE_EXPR);
4671 maybe_warn_sized_delete (VEC_DELETE_EXPR);
4672}
4673
f16ed232 4674/* Earlier we left PTRMEM_CST in variable initializers alone so that we could
4675 look them up when evaluating non-type template parameters. Now we need to
4676 lower them to something the back end can understand. */
4677
4678static void
4679lower_var_init ()
4680{
4681 varpool_node *node;
4682 FOR_EACH_VARIABLE (node)
4683 {
4684 tree d = node->decl;
4685 if (tree init = DECL_INITIAL (d))
4686 DECL_INITIAL (d) = cplus_expand_constant (init);
4687 }
4688}
4689
15daca6e 4690/* This routine is called at the end of compilation.
cec1f6a6 4691 Its job is to create all the code needed to initialize and
4692 destroy the global aggregates. We do the destruction
4693 first, since that way we only need to reverse the decls once. */
dc76caae 4694
cec1f6a6 4695void
3a1c9df2 4696c_parse_final_cleanups (void)
cec1f6a6 4697{
cec1f6a6 4698 tree vars;
26ac6687 4699 bool reconsider;
cec1f6a6 4700 size_t i;
911483bc 4701 unsigned ssdf_count = 0;
68efb5b8 4702 int retries = 0;
3c8969b1 4703 tree decl;
471086d6 4704
3a1c9df2 4705 locus_at_end_of_parsing = input_location;
cec1f6a6 4706 at_eof = 1;
bb09dca5 4707
cec1f6a6 4708 /* Bad parse errors. Just forget about it. */
eab615be 4709 if (! global_bindings_p () || current_class_type
f1f41a6c 4710 || !vec_safe_is_empty (decl_namespace_list))
cec1f6a6 4711 return;
471086d6 4712
015b2593 4713 /* This is the point to write out a PCH if we're doing that.
4714 In that case we do not want to do anything else. */
573aba85 4715 if (pch_file)
015b2593 4716 {
49b5d7b9 4717 /* Mangle all symbols at PCH creation time. */
4718 symtab_node *node;
4719 FOR_EACH_SYMBOL (node)
4720 if (! is_a <varpool_node *> (node)
4721 || ! DECL_HARD_REGISTER (node->decl))
4722 DECL_ASSEMBLER_NAME (node->decl);
015b2593 4723 c_common_write_pch ();
ddc9a346 4724 dump_tu ();
9a26d403 4725 /* Ensure even the callers don't try to finalize the CU. */
4726 flag_syntax_only = 1;
015b2593 4727 return;
4728 }
573aba85 4729
3a1c9df2 4730 timevar_stop (TV_PHASE_PARSING);
4731 timevar_start (TV_PHASE_DEFERRED);
4732
35ee1c66 4733 symtab->process_same_body_aliases ();
c70f46b0 4734
b8254e80 4735 /* Handle -fdump-ada-spec[-slim] */
ef34afc1 4736 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
b8254e80 4737 {
ef34afc1 4738 if (flag_dump_ada_spec_slim)
b8254e80 4739 collect_source_ref (main_input_filename);
4740 else
4741 collect_source_refs (global_namespace);
4742
4743 dump_ada_specs (collect_all_refs, cpp_check);
4744 }
4745
9c85a98a 4746 /* FIXME - huh? was input_line -= 1;*/
e857e9c7 4747
cec1f6a6 4748 /* We now have to write out all the stuff we put off writing out.
4749 These include:
b0df6589 4750
cec1f6a6 4751 o Template specializations that we have not yet instantiated,
653e5405 4752 but which are needed.
cec1f6a6 4753 o Initialization and destruction for non-local objects with
653e5405 4754 static storage duration. (Local objects with static storage
cec1f6a6 4755 duration are initialized when their scope is first entered,
4756 and are cleaned up via atexit.)
9031d10b 4757 o Virtual function tables.
08e76de4 4758
cec1f6a6 4759 All of these may cause others to be needed. For example,
4760 instantiating one function may cause another to be needed, and
2e793478 4761 generating the initializer for an object may cause templates to be
cec1f6a6 4762 instantiated, etc., etc. */
471086d6 4763
1631c87f 4764 emit_support_tinfos ();
4ee9c684 4765
9031d10b 4766 do
cec1f6a6 4767 {
6fc7a923 4768 tree t;
92d84169 4769 tree decl;
6fc7a923 4770
26ac6687 4771 reconsider = false;
cec1f6a6 4772
0d4cab85 4773 /* If there are templates that we've put off instantiating, do
4774 them now. */
68efb5b8 4775 instantiate_pending_templates (retries);
dc721f36 4776 ggc_collect ();
cec1f6a6 4777
6860980e 4778 /* Write out virtual tables as required. Writing out the
4779 virtual table for a template class may cause the
653e5405 4780 instantiation of members of that class. If we write out
4781 vtables then we remove the class from our list so we don't
4782 have to look at it again. */
6860980e 4783 for (i = keyed_classes->length ();
4784 keyed_classes->iterate (--i, &t);)
4785 if (maybe_emit_vtables (t))
4786 {
4787 reconsider = true;
4788 keyed_classes->unordered_remove (i);
4789 }
e2f46fbf 4790 /* The input_location may have been changed during marking of
4791 vtable entries. */
4792 input_location = locus_at_end_of_parsing;
68efb5b8 4793
981bab5c 4794 /* Write out needed type info variables. We have to be careful
653e5405 4795 looping through unemitted decls, because emit_tinfo_decl may
4796 cause other variables to be needed. New elements will be
4797 appended, and we remove from the vector those that actually
4798 get emitted. */
f1f41a6c 4799 for (i = unemitted_tinfo_decls->length ();
4800 unemitted_tinfo_decls->iterate (--i, &t);)
2beb8f09 4801 if (emit_tinfo_decl (t))
4802 {
4803 reconsider = true;
f1f41a6c 4804 unemitted_tinfo_decls->unordered_remove (i);
2beb8f09 4805 }
6d318c49 4806
cec1f6a6 4807 /* The list of objects with static storage duration is built up
3f131089 4808 in reverse order. We clear STATIC_AGGREGATES so that any new
4809 aggregates added during the initialization of these will be
4810 initialized in the correct order when we next come around the
4811 loop. */
cd4dc755 4812 vars = prune_vars_needing_no_initialization (&static_aggregates);
90d8d794 4813
3f131089 4814 if (vars)
4815 {
3f131089 4816 /* We need to start a new initialization function each time
4817 through the loop. That's because we need to know which
4818 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4819 isn't computed until a function is finished, and written
a17c2a3a 4820 out. That's a deficiency in the back end. When this is
3f131089 4821 fixed, these initialization functions could all become
4822 inline, with resulting performance improvements. */
911483bc 4823 tree ssdf_body;
4824
9dbdf47d 4825 /* Make sure the back end knows about all the variables. */
4826 write_out_vars (vars);
4827
911483bc 4828 /* Set the line and file, so that it is obviously not from
4829 the source file. */
3a1c9df2 4830 input_location = locus_at_end_of_parsing;
911483bc 4831 ssdf_body = start_static_storage_duration_function (ssdf_count);
3f131089 4832
3f131089 4833 /* First generate code to do all the initializations. */
a9f11a62 4834 if (vars)
4835 do_static_initialization_or_destruction (vars, /*initp=*/true);
3f131089 4836
4837 /* Then, generate code to do all the destructions. Do these
4838 in reverse order so that the most recently constructed
060ff7d8 4839 variable is the first destroyed. If we're using
4840 __cxa_atexit, then we don't need to do this; functions
2fb96d35 4841 were registered at initialization time to destroy the
060ff7d8 4842 local statics. */
a9f11a62 4843 if (!flag_use_cxa_atexit && vars)
060ff7d8 4844 {
4845 vars = nreverse (vars);
a9f11a62 4846 do_static_initialization_or_destruction (vars, /*initp=*/false);
060ff7d8 4847 }
4848 else
4849 vars = NULL_TREE;
3f131089 4850
4851 /* Finish up the static storage duration function for this
4852 round. */
3a1c9df2 4853 input_location = locus_at_end_of_parsing;
3f131089 4854 finish_static_storage_duration_function (ssdf_body);
4855
4856 /* All those initializations and finalizations might cause
4857 us to need more inline functions, more template
4858 instantiations, etc. */
26ac6687 4859 reconsider = true;
911483bc 4860 ssdf_count++;
3a1c9df2 4861 /* ??? was: locus_at_end_of_parsing.line++; */
cec1f6a6 4862 }
9031d10b 4863
462819c8 4864 /* Now do the same for thread_local variables. */
4865 handle_tls_init ();
4866
caa6fdce 4867 /* Go through the set of inline functions whose bodies have not
4868 been emitted yet. If out-of-line copies of these functions
4869 are required, emit them. */
f1f41a6c 4870 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
cec1f6a6 4871 {
2df45762 4872 /* Does it need synthesizing? */
9a5d7e21 4873 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
3b116dc2 4874 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
cec1f6a6 4875 {
4876 /* Even though we're already at the top-level, we push
4877 there again. That way, when we pop back a few lines
4878 hence, all of our state is restored. Otherwise,
4879 finish_function doesn't clean things up, and we end
4880 up with CURRENT_FUNCTION_DECL set. */
4881 push_to_top_level ();
b7102079 4882 /* The decl's location will mark where it was first
653e5405 4883 needed. Save that so synthesize method can indicate
4884 where it was needed from, in case of error */
b7102079 4885 input_location = DECL_SOURCE_LOCATION (decl);
606b494c 4886 synthesize_method (decl);
cec1f6a6 4887 pop_from_top_level ();
26ac6687 4888 reconsider = true;
cec1f6a6 4889 }
cec1f6a6 4890
462819c8 4891 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4892 generate_tls_wrapper (decl);
4893
bfec3452 4894 if (!DECL_SAVED_TREE (decl))
31da194e 4895 continue;
b8f254f0 4896
bb9012bf 4897 cgraph_node *node = cgraph_node::get_create (decl);
4898
a17c2a3a 4899 /* We lie to the back end, pretending that some functions
2df45762 4900 are not defined when they really are. This keeps these
4901 functions from being put out unnecessarily. But, we must
4902 stop lying when the functions are referenced, or if they
d9803664 4903 are not comdat since they need to be put out now. If
4904 DECL_INTERFACE_KNOWN, then we have already set
4905 DECL_EXTERNAL appropriately, so there's no need to check
4906 again, and we do not want to clear DECL_EXTERNAL if a
4907 previous call to import_export_decl set it.
074ab442 4908
d9803664 4909 This is done in a separate for cycle, because if some
4910 deferred function is contained in another deferred
4911 function later in deferred_fns varray,
4912 rest_of_compilation would skip this function and we
4913 really cannot expand the same function twice. */
4914 import_export_decl (decl);
badde9f1 4915 if (DECL_NOT_REALLY_EXTERN (decl)
4916 && DECL_INITIAL (decl)
caa6fdce 4917 && decl_needed_p (decl))
ed772161 4918 {
02774f2d 4919 if (node->cpp_implicit_alias)
415d1b9a 4920 node = node->get_alias_target ();
d3d35aba 4921
415d1b9a 4922 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
4923 NULL, true);
61c2c7b1 4924 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4925 group, we need to mark all symbols in the same comdat group
4926 that way. */
02774f2d 4927 if (node->same_comdat_group)
bb9012bf 4928 for (cgraph_node *next
4929 = dyn_cast<cgraph_node *> (node->same_comdat_group);
61c2c7b1 4930 next != node;
415d1b9a 4931 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
4932 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
4933 NULL, true);
ed772161 4934 }
ec1ff36a 4935
4936 /* If we're going to need to write this function out, and
4937 there's already a body for it, create RTL for it now.
4938 (There might be no body if this is a method we haven't
4939 gotten around to synthesizing yet.) */
4940 if (!DECL_EXTERNAL (decl)
caa6fdce 4941 && decl_needed_p (decl)
6cb758f0 4942 && !TREE_ASM_WRITTEN (decl)
bb9012bf 4943 && !node->definition)
ec1ff36a 4944 {
6cb758f0 4945 /* We will output the function; no longer consider it in this
4946 loop. */
4947 DECL_DEFER_OUTPUT (decl) = 0;
ec1ff36a 4948 /* Generate RTL for this function now that we know we
4949 need it. */
6cb758f0 4950 expand_or_defer_fn (decl);
ec1ff36a 4951 /* If we're compiling -fsyntax-only pretend that this
4952 function has been written out so that we don't try to
4953 expand it again. */
4954 if (flag_syntax_only)
4955 TREE_ASM_WRITTEN (decl) = 1;
26ac6687 4956 reconsider = true;
ec1ff36a 4957 }
badde9f1 4958 }
4959
90305f89 4960 if (wrapup_namespace_globals ())
26ac6687 4961 reconsider = true;
cec1f6a6 4962
4963 /* Static data members are just like namespace-scope globals. */
f1f41a6c 4964 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
cec1f6a6 4965 {
24eb5916 4966 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4967 /* Don't write it out if we haven't seen a definition. */
81f19d90 4968 || (DECL_IN_AGGR_P (decl) && !DECL_INLINE_VAR_P (decl)))
cec1f6a6 4969 continue;
4970 import_export_decl (decl);
caa6fdce 4971 /* If this static data member is needed, provide it to the
4972 back end. */
4973 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
1e307880 4974 DECL_EXTERNAL (decl) = 0;
cec1f6a6 4975 }
f1f41a6c 4976 if (vec_safe_length (pending_statics) != 0
4977 && wrapup_global_declarations (pending_statics->address (),
4978 pending_statics->length ()))
26ac6687 4979 reconsider = true;
050e11c9 4980
68efb5b8 4981 retries++;
9031d10b 4982 }
cec1f6a6 4983 while (reconsider);
bea7d742 4984
f16ed232 4985 lower_var_init ();
4986
6e9c0030 4987 generate_mangling_aliases ();
4988
6beb3f76 4989 /* All used inline functions must have a definition at this point. */
f1f41a6c 4990 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
2df45762 4991 {
caa6fdce 4992 if (/* Check online inline functions that were actually used. */
411978d2 4993 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
d9803664 4994 /* If the definition actually was available here, then the
4995 fact that the function was not defined merely represents
4996 that for some reason (use of a template repository,
4997 #pragma interface, etc.) we decided not to emit the
4998 definition here. */
4999 && !DECL_INITIAL (decl)
6ba663d6 5000 /* Don't complain if the template was defined. */
5001 && !(DECL_TEMPLATE_INSTANTIATION (decl)
5002 && DECL_INITIAL (DECL_TEMPLATE_RESULT
5003 (template_for_substitution (decl)))))
1bf5bd08 5004 {
0f4714ef 5005 warning_at (DECL_SOURCE_LOCATION (decl), 0,
5006 "inline function %qD used but never defined", decl);
3a1c9df2 5007 /* Avoid a duplicate warning from check_global_declaration. */
7946223e 5008 TREE_NO_WARNING (decl) = 1;
1bf5bd08 5009 }
2df45762 5010 }
9031d10b 5011
74d6b34b 5012 /* So must decls that use a type with no linkage. */
f1f41a6c 5013 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
2381bbfa 5014 no_linkage_error (decl);
74d6b34b 5015
d1856d2c 5016 maybe_warn_sized_delete ();
5017
64cd9619 5018 /* Then, do the Objective-C stuff. This is where all the
5019 Objective-C module stuff gets generated (symtab,
5020 class/protocol/selector lists etc). This must be done after C++
5021 templates, destructors etc. so that selectors used in C++
5022 templates are properly allocated. */
5023 if (c_dialect_objc ())
5024 objc_write_global_declarations ();
5025
5c8ddb8a 5026 /* We give C linkage to static constructors and destructors. */
5027 push_lang_context (lang_name_c);
5028
5029 /* Generate initialization and destruction functions for all
5030 priorities for which they are required. */
5031 if (priority_info_map)
9031d10b 5032 splay_tree_foreach (priority_info_map,
5c8ddb8a 5033 generate_ctor_and_dtor_functions_for_priority,
3a1c9df2 5034 /*data=*/&locus_at_end_of_parsing);
9af7fd5b 5035 else if (c_dialect_objc () && objc_static_init_needed_p ())
5036 /* If this is obj-c++ and we need a static init, call
5037 generate_ctor_or_dtor_function. */
5038 generate_ctor_or_dtor_function (/*constructor_p=*/true,
3a1c9df2 5039 DEFAULT_INIT_PRIORITY,
5040 &locus_at_end_of_parsing);
5c8ddb8a 5041
5042 /* We're done with the splay-tree now. */
5043 if (priority_info_map)
5044 splay_tree_delete (priority_info_map);
5045
b42190bc 5046 /* Generate any missing aliases. */
5047 maybe_apply_pending_pragma_weaks ();
5048
5c8ddb8a 5049 /* We're done with static constructors, so we can go back to "C++"
5050 linkage now. */
5051 pop_lang_context ();
5052
b710ec85 5053 if (flag_vtable_verify)
5054 {
5055 vtv_recover_class_info ();
5056 vtv_compute_class_hierarchy_transitive_closure ();
5057 vtv_build_vtable_verify_fndecl ();
5058 }
5059
565fb345 5060 perform_deferred_noexcept_checks ();
5061
bb09dca5 5062 finish_repo ();
a050099a 5063 fini_constexpr ();
5064
ddea175e 5065 /* The entire file is now complete. If requested, dump everything
a109dc3b 5066 to a file. */
ddc9a346 5067 dump_tu ();
9031d10b 5068
471086d6 5069 if (flag_detailed_statistics)
a7ab4ece 5070 {
5071 dump_tree_statistics ();
5072 dump_time_statistics ();
5073 }
3a1c9df2 5074
5075 timevar_stop (TV_PHASE_DEFERRED);
5076 timevar_start (TV_PHASE_PARSING);
9f2b92c4 5077
5078 /* Indicate that we're done with front end processing. */
5079 at_eof = 2;
3a1c9df2 5080}
5081
5082/* Perform any post compilation-proper cleanups for the C++ front-end.
5083 This should really go away. No front-end should need to do
5084 anything past the compilation process. */
5085
5086void
5087cxx_post_compilation_parsing_cleanups (void)
5088{
5089 timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS);
5090
5091 if (flag_vtable_verify)
5092 {
5093 /* Generate the special constructor initialization function that
5094 calls __VLTRegisterPairs, and give it a very high
5095 initialization priority. This must be done after
5096 finalize_compilation_unit so that we have accurate
5097 information about which vtable will actually be emitted. */
5098 vtv_generate_init_routine ();
5099 }
5100
5101 input_location = locus_at_end_of_parsing;
1611df57 5102
5e8689fb 5103 if (flag_checking)
5104 validate_conversion_obstack ();
6198e8f6 5105
3a1c9df2 5106 timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS);
471086d6 5107}
ac9386a0 5108
9b7c46f2 5109/* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
5110 function to call in parse-tree form; it has not yet been
5111 semantically analyzed. ARGS are the arguments to the function.
f352a3fb 5112 They have already been semantically analyzed. This may change
5113 ARGS. */
f70cb9e6 5114
5115tree
f1f41a6c 5116build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
ef941b93 5117 tsubst_flags_t complain)
f70cb9e6 5118{
13795292 5119 tree orig_fn;
f1f41a6c 5120 vec<tree, va_gc> *orig_args = NULL;
13795292 5121 tree expr;
9b7c46f2 5122 tree object;
f70cb9e6 5123
13795292 5124 orig_fn = fn;
9b7c46f2 5125 object = TREE_OPERAND (fn, 0);
13795292 5126
5127 if (processing_template_decl)
5128 {
b4df430b 5129 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
5130 || TREE_CODE (fn) == MEMBER_REF);
13795292 5131 if (type_dependent_expression_p (fn)
f352a3fb 5132 || any_type_dependent_arguments_p (*args))
bd5c2b15 5133 return build_min_nt_call_vec (fn, *args);
f352a3fb 5134
5135 orig_args = make_tree_vector_copy (*args);
13795292 5136
5137 /* Transform the arguments and add the implicit "this"
5138 parameter. That must be done before the FN is transformed
5139 because we depend on the form of FN. */
f352a3fb 5140 make_args_non_dependent (*args);
20aeb169 5141 object = build_non_dependent_expr (object);
4c35e34c 5142 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5143 {
4c35e34c 5144 if (TREE_CODE (fn) == DOTSTAR_EXPR)
ef941b93 5145 object = cp_build_addr_expr (object, complain);
f1f41a6c 5146 vec_safe_insert (*args, 0, object);
4c35e34c 5147 }
13795292 5148 /* Now that the arguments are done, transform FN. */
5149 fn = build_non_dependent_expr (fn);
5150 }
f70cb9e6 5151
ef4534a3 5152 /* A qualified name corresponding to a bound pointer-to-member is
5153 represented as an OFFSET_REF:
f70cb9e6 5154
5155 struct B { void g(); };
5156 void (B::*p)();
5157 void B::g() { (this->*p)(); } */
13795292 5158 if (TREE_CODE (fn) == OFFSET_REF)
f70cb9e6 5159 {
ef941b93 5160 tree object_addr = cp_build_addr_expr (object, complain);
f70cb9e6 5161 fn = TREE_OPERAND (fn, 1);
4405c1ad 5162 fn = get_member_function_from_ptrfunc (&object_addr, fn,
ef941b93 5163 complain);
f1f41a6c 5164 vec_safe_insert (*args, 0, object_addr);
f70cb9e6 5165 }
f70cb9e6 5166
4c35e34c 5167 if (CLASS_TYPE_P (TREE_TYPE (fn)))
ef941b93 5168 expr = build_op_call (fn, args, complain);
4c35e34c 5169 else
ef941b93 5170 expr = cp_build_function_call_vec (fn, args, complain);
13795292 5171 if (processing_template_decl && expr != error_mark_node)
f352a3fb 5172 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
5173
5174 if (orig_args != NULL)
5175 release_tree_vector (orig_args);
5176
13795292 5177 return expr;
f70cb9e6 5178}
9031d10b 5179
ce28ee2e 5180
5181void
26ac6687 5182check_default_args (tree x)
ce28ee2e 5183{
5184 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
26ac6687 5185 bool saw_def = false;
9683081b 5186 bool noted_first_def = false;
5187 int idx_of_first_default_arg = 0;
5188 location_t loc_of_first_default_arg = UNKNOWN_LOCATION;
26ac6687 5189 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
9683081b 5190 tree fndecl = STRIP_TEMPLATE (x);
5191 auto_diagnostic_group d;
ce28ee2e 5192 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
5193 {
5194 if (TREE_PURPOSE (arg))
9683081b 5195 {
5196 if (!saw_def)
5197 {
5198 saw_def = true;
5199 idx_of_first_default_arg = i;
5200 location_t loc = get_fndecl_argument_location (fndecl, i);
5201 if (loc != DECL_SOURCE_LOCATION (x))
5202 loc_of_first_default_arg = loc;
5203 }
5204 }
192de636 5205 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
ce28ee2e 5206 {
9683081b 5207 error_at (get_fndecl_argument_location (fndecl, i),
5208 "default argument missing for parameter %P of %q#D", i, x);
5209 if (loc_of_first_default_arg != UNKNOWN_LOCATION
5210 && !noted_first_def)
5211 {
5212 inform (loc_of_first_default_arg,
5213 "...following parameter %P which has a default argument",
5214 idx_of_first_default_arg);
5215 noted_first_def = true;
5216 }
917e3348 5217 TREE_PURPOSE (arg) = error_mark_node;
ce28ee2e 5218 }
5219 }
5220}
3d4e092a 5221
3b116dc2 5222/* Return true if function DECL can be inlined. This is used to force
5223 instantiation of methods that might be interesting for inlining. */
5224bool
5225possibly_inlined_p (tree decl)
5226{
5227 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
5228 if (DECL_UNINLINABLE (decl))
5229 return false;
c93d719b 5230 if (!optimize)
3b116dc2 5231 return DECL_DECLARED_INLINE_P (decl);
5232 /* When optimizing, we might inline everything when flatten
5233 attribute or heuristics inlining for size or autoinlining
5234 is used. */
5235 return true;
5236}
5237
068632d3 5238/* Normally, we can wait until instantiation-time to synthesize DECL.
5239 However, if DECL is a static data member initialized with a constant
5240 or a constexpr function, we need it right now because a reference to
5241 such a data member or a call to such function is not value-dependent.
5242 For a function that uses auto in the return type, we need to instantiate
5243 it to find out its type. For OpenMP user defined reductions, we need
5244 them instantiated for reduction clauses which inline them by hand
5245 directly. */
5246
5247static void
5248maybe_instantiate_decl (tree decl)
5249{
5250 if (DECL_LANG_SPECIFIC (decl)
5251 && DECL_TEMPLATE_INFO (decl)
5252 && (decl_maybe_constant_var_p (decl)
5253 || (TREE_CODE (decl) == FUNCTION_DECL
5254 && DECL_OMP_DECLARE_REDUCTION_P (decl))
5255 || undeduced_auto_decl (decl))
5256 && !DECL_DECLARED_CONCEPT_P (decl)
5257 && !uses_template_parms (DECL_TI_ARGS (decl)))
5258 {
5259 /* Instantiating a function will result in garbage collection. We
5260 must treat this situation as if we were within the body of a
5261 function so as to avoid collecting live data only referenced from
5262 the stack (such as overload resolution candidates). */
5263 ++function_depth;
5264 instantiate_decl (decl, /*defer_ok=*/false,
5265 /*expl_inst_class_mem_p=*/false);
5266 --function_depth;
5267 }
5268}
5269
17847cff 5270/* Maybe warn if DECL is deprecated, subject to COMPLAIN. Returns whether or
5271 not a warning was emitted. */
5272
5273bool
5274cp_warn_deprecated_use (tree decl, tsubst_flags_t complain)
5275{
5276 if (!(complain & tf_warning) || !decl
5277 || deprecated_state == DEPRECATED_SUPPRESS)
5278 return false;
5279
5280 if (!TREE_DEPRECATED (decl))
5281 {
5282 /* Perhaps this is a deprecated typedef. */
5283 if (TYPE_P (decl) && TYPE_NAME (decl))
5284 decl = TYPE_NAME (decl);
5285
5286 if (!TREE_DEPRECATED (decl))
5287 return false;
5288 }
5289
5290 /* Don't warn within members of a deprecated type. */
5291 if (TYPE_P (decl)
5292 && currently_open_class (decl))
5293 return false;
5294
5295 bool warned = false;
5296 if (cxx_dialect >= cxx11
5297 && DECL_P (decl)
5298 && DECL_ARTIFICIAL (decl)
5299 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5300 && copy_fn_p (decl))
5301 {
482bd38e 5302 if (warn_deprecated_copy
5303 /* Don't warn about system library classes (c++/86342). */
5304 && (!DECL_IN_SYSTEM_HEADER (decl)
5305 || global_dc->dc_warn_system_headers))
17847cff 5306 {
482bd38e 5307 auto_diagnostic_group d;
17847cff 5308 tree ctx = DECL_CONTEXT (decl);
482bd38e 5309 tree other = classtype_has_depr_implicit_copy (ctx);
5310 int opt = (DECL_DESTRUCTOR_P (other)
5311 ? OPT_Wdeprecated_copy_dtor
5312 : OPT_Wdeprecated_copy);
5313 warned = warning (opt, "implicitly-declared %qD is deprecated",
5314 decl);
5315 if (warned)
5316 inform (DECL_SOURCE_LOCATION (other),
5317 "because %qT has user-provided %qD",
5318 ctx, other);
17847cff 5319 }
5320 }
5321 else
5322 warned = warn_deprecated_use (decl, NULL_TREE);
5323
5324 return warned;
5325}
5326
dbf08015 5327/* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
0638c434 5328 If DECL is a specialization or implicitly declared class member,
bfb588d6 5329 generate the actual definition. Return false if something goes
5330 wrong, true otherwise. */
1f481a8e 5331
bfb588d6 5332bool
7442ab85 5333mark_used (tree decl, tsubst_flags_t complain)
3d4e092a 5334{
e8a88621 5335 /* If we're just testing conversions or resolving overloads, we
5336 don't want any permanent effects like forcing functions to be
5337 output or instantiating templates. */
5338 if ((complain & tf_conv))
5339 return true;
5340
0638c434 5341 /* If DECL is a BASELINK for a single function, then treat it just
5342 like the DECL for the function. Otherwise, if the BASELINK is
5343 for an overloaded function, we don't know which function was
5344 actually used until after overload resolution. */
a00f651a 5345 if (BASELINK_P (decl))
0638c434 5346 {
5347 decl = BASELINK_FUNCTIONS (decl);
5348 if (really_overloaded_fn (decl))
bfb588d6 5349 return true;
6767ca9a 5350 decl = OVL_FIRST (decl);
0638c434 5351 }
5352
411978d2 5353 /* Set TREE_USED for the benefit of -Wunused. */
5354 TREE_USED (decl) = 1;
3f430531 5355 /* And for structured bindings also the underlying decl. */
5356 if (DECL_DECOMPOSITION_P (decl) && DECL_DECOMP_BASE (decl))
5357 TREE_USED (DECL_DECOMP_BASE (decl)) = 1;
83d5062b 5358
5359 if (TREE_CODE (decl) == TEMPLATE_DECL)
5360 return true;
5361
411978d2 5362 if (DECL_CLONED_FUNCTION_P (decl))
5363 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5364
c28ddc97 5365 /* Mark enumeration types as used. */
5366 if (TREE_CODE (decl) == CONST_DECL)
5367 used_types_insert (DECL_CONTEXT (decl));
5368
b9e17a4a 5369 if (TREE_CODE (decl) == FUNCTION_DECL
5370 && !maybe_instantiate_noexcept (decl, complain))
5371 return false;
f2c1aabc 5372
16f0449a 5373 if (TREE_CODE (decl) == FUNCTION_DECL
5374 && DECL_DELETED_FN (decl))
5375 {
ab87ee8f 5376 if (DECL_ARTIFICIAL (decl)
ef8f6502 5377 && DECL_CONV_FN_P (decl)
ab87ee8f 5378 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
5379 /* We mark a lambda conversion op as deleted if we can't
5380 generate it properly; see maybe_add_lambda_conv_op. */
ef8f6502 5381 sorry ("converting lambda that uses %<...%> to function pointer");
ab87ee8f 5382 else if (complain & tf_error)
7442ab85 5383 {
5384 error ("use of deleted function %qD", decl);
5385 if (!maybe_explain_implicit_delete (decl))
5386 inform (DECL_SOURCE_LOCATION (decl), "declared here");
5387 }
bfb588d6 5388 return false;
16f0449a 5389 }
038849b3 5390
17847cff 5391 cp_warn_deprecated_use (decl, complain);
59edbc2a 5392
411978d2 5393 /* We can only check DECL_ODR_USED on variables or functions with
5394 DECL_LANG_SPECIFIC set, and these are also the only decls that we
5395 might need special handling for. */
4cace8cb 5396 if (!VAR_OR_FUNCTION_DECL_P (decl)
b2e9b754 5397 || DECL_LANG_SPECIFIC (decl) == NULL
5398 || DECL_THUNK_P (decl))
86359a65 5399 {
71d4cbd2 5400 if (!processing_template_decl
5401 && !require_deduced_type (decl, complain))
5402 return false;
86359a65 5403 return true;
5404 }
411978d2 5405
67702eb8 5406 /* We only want to do this processing once. We don't need to keep trying
5407 to instantiate inline templates, because unit-at-a-time will make sure
5408 we get them compiled before functions that want to inline them. */
411978d2 5409 if (DECL_ODR_USED (decl))
bfb588d6 5410 return true;
67702eb8 5411
d58b8a94 5412 /* Normally, we can wait until instantiation-time to synthesize DECL.
5413 However, if DECL is a static data member initialized with a constant
5414 or a constexpr function, we need it right now because a reference to
86359a65 5415 such a data member or a call to such function is not value-dependent.
5416 For a function that uses auto in the return type, we need to instantiate
bc7bff74 5417 it to find out its type. For OpenMP user defined reductions, we need
5418 them instantiated for reduction clauses which inline them by hand
5419 directly. */
068632d3 5420 maybe_instantiate_decl (decl);
074ab442 5421
34ec198d 5422 if (processing_template_decl || in_template_function ())
1b93b5e5 5423 return true;
5424
21131a05 5425 /* Check this too in case we're within instantiate_non_dependent_expr. */
1b93b5e5 5426 if (DECL_TEMPLATE_INFO (decl)
5427 && uses_template_parms (DECL_TI_ARGS (decl)))
5428 return true;
5429
71d4cbd2 5430 if (!require_deduced_type (decl, complain))
5431 return false;
86359a65 5432
c9051796 5433 if (builtin_pack_fn_p (decl))
5434 {
5435 error ("use of built-in parameter pack %qD outside of a template",
5436 DECL_NAME (decl));
5437 return false;
5438 }
5439
d58b8a94 5440 /* If we don't need a value, then we don't need to synthesize DECL. */
66d09a96 5441 if (cp_unevaluated_operand || in_discarded_stmt)
bfb588d6 5442 return true;
d58b8a94 5443
411978d2 5444 DECL_ODR_USED (decl) = 1;
67702eb8 5445 if (DECL_CLONED_FUNCTION_P (decl))
411978d2 5446 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
67702eb8 5447
74d6b34b 5448 /* DR 757: A type without linkage shall not be used as the type of a
5449 variable or function with linkage, unless
5450 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5451 o the variable or function is not used (3.2 [basic.def.odr]) or is
5452 defined in the same translation unit. */
c0cc2275 5453 if (cxx_dialect > cxx98
5454 && decl_linkage (decl) != lk_none
b2e9b754 5455 && !DECL_EXTERN_C_P (decl)
5456 && !DECL_ARTIFICIAL (decl)
5457 && !decl_defined_p (decl)
5458 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
5459 {
5460 if (is_local_extern (decl))
5461 /* There's no way to define a local extern, and adding it to
5462 the vector interferes with GC, so give an error now. */
5463 no_linkage_error (decl);
5464 else
f1f41a6c 5465 vec_safe_push (no_linkage_decls, decl);
74d6b34b 5466 }
5467
2df45762 5468 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
27ccbdd9 5469 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
2df45762 5470 /* Remember it, so we can check it was defined. */
27ccbdd9 5471 note_vague_linkage_fn (decl);
9031d10b 5472
d3ce0f51 5473 /* Is it a synthesized method that needs to be synthesized? */
9ba4048d 5474 if (TREE_CODE (decl) == FUNCTION_DECL
5475 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
2336da2a 5476 && DECL_DEFAULTED_FN (decl)
dd0dcb3e 5477 /* A function defaulted outside the class is synthesized either by
5478 cp_finish_decl or instantiate_decl. */
5479 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
adbf3415 5480 && ! DECL_INITIAL (decl))
e55cba4c 5481 {
04b0e01b 5482 /* Defer virtual destructors so that thunks get the right
5483 linkage. */
5484 if (DECL_VIRTUAL_P (decl) && !at_eof)
5485 {
5486 note_vague_linkage_fn (decl);
5487 return true;
5488 }
5489
27ccbdd9 5490 /* Remember the current location for a function we will end up
5491 synthesizing. Then we can inform the user where it was
5492 required in the case of error. */
5493 DECL_SOURCE_LOCATION (decl) = input_location;
5494
2d51ed49 5495 /* Synthesizing an implicitly defined member function will result in
5496 garbage collection. We must treat this situation as if we were
5497 within the body of a function so as to avoid collecting live data
5498 on the stack (such as overload resolution candidates).
5499
9075d22f 5500 We could just let cp_write_global_declarations handle synthesizing
04b0e01b 5501 this function by adding it to deferred_fns, but doing
9075d22f 5502 it at the use site produces better error messages. */
2d51ed49 5503 ++function_depth;
e55cba4c 5504 synthesize_method (decl);
2d51ed49 5505 --function_depth;
9075d22f 5506 /* If this is a synthesized method we don't need to
0a045a26 5507 do the instantiation test below. */
5508 }
4cace8cb 5509 else if (VAR_OR_FUNCTION_DECL_P (decl)
b2e9b754 5510 && DECL_TEMPLATE_INFO (decl)
56c12fd4 5511 && !DECL_DECLARED_CONCEPT_P (decl)
0a045a26 5512 && (!DECL_EXPLICIT_INSTANTIATION (decl)
67702eb8 5513 || always_instantiate_p (decl)))
0a045a26 5514 /* If this is a function or variable that is an instance of some
5515 template, we now know that we will need to actually do the
5516 instantiation. We check that DECL is not an explicit
5517 instantiation because that is not checked in instantiate_decl.
074ab442 5518
0a045a26 5519 We put off instantiating functions in order to improve compile
caa6fdce 5520 times. Maintaining a stack of active functions is expensive,
5521 and the inliner knows to instantiate any functions it might
0a045a26 5522 need. Therefore, we always try to defer instantiation. */
cf797b5e 5523 {
5524 ++function_depth;
5525 instantiate_decl (decl, /*defer_ok=*/true,
5526 /*expl_inst_class_mem_p=*/false);
5527 --function_depth;
5528 }
bfb588d6 5529
5530 return true;
3d4e092a 5531}
c90e0199 5532
7442ab85 5533bool
5534mark_used (tree decl)
5535{
5536 return mark_used (decl, tf_warning_or_error);
5537}
5538
b710ec85 5539tree
5540vtv_start_verification_constructor_init_function (void)
5541{
5542 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5543}
5544
5545tree
5546vtv_finish_verification_constructor_init_function (tree function_body)
5547{
5548 tree fn;
5549
5550 finish_compound_stmt (function_body);
4775c814 5551 fn = finish_function (/*inline_p=*/false);
b710ec85 5552 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5553 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5554
5555 return fn;
5556}
5557
1f3233d1 5558#include "gt-cp-decl2.h"