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