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