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