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