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