Update LOCAL_PATCHES after libsanitizer merge.
[official-gcc.git] / gcc / cp / decl2.c
bloba5ad0eed3ad6ed2253efe9938d743cf8762448ec
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2018 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
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.
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.
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/>. */
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. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
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"
52 /* Id for dumping the raw trees. */
53 int raw_dump_id;
55 extern cpp_reader *parse_in;
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;
68 static void mark_vtable_entries (tree);
69 static bool maybe_emit_vtables (tree);
70 static tree start_objects (int, int);
71 static void finish_objects (int, int, tree);
72 static tree start_static_storage_duration_function (unsigned);
73 static void finish_static_storage_duration_function (tree);
74 static priority_info get_priority_info (int);
75 static void do_static_initialization_or_destruction (tree, bool);
76 static void one_static_initialization_or_destruction (tree, tree, bool);
77 static void generate_ctor_or_dtor_function (bool, int, location_t *);
78 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
79 void *);
80 static tree prune_vars_needing_no_initialization (tree *);
81 static void write_out_vars (tree);
82 static void import_export_class (tree);
83 static tree get_guard_bits (tree);
84 static void determine_visibility_from_class (tree, tree);
85 static bool determine_hidden_inline (tree);
86 static void maybe_instantiate_decl (tree);
88 /* A list of static class variables. This is needed, because a
89 static class variable can be declared inside the class without
90 an initializer, and then initialized, statically, outside the class. */
91 static GTY(()) vec<tree, va_gc> *pending_statics;
93 /* A list of functions which were declared inline, but which we
94 may need to emit outline anyway. */
95 static GTY(()) vec<tree, va_gc> *deferred_fns;
97 /* A list of decls that use types with no linkage, which we need to make
98 sure are defined. */
99 static GTY(()) vec<tree, va_gc> *no_linkage_decls;
101 /* A vector of alternating decls and identifiers, where the latter
102 is to be an alias for the former if the former is defined. */
103 static GTY(()) vec<tree, va_gc> *mangling_aliases;
105 /* hash traits for declarations. Hashes single decls via
106 DECL_ASSEMBLER_NAME_RAW. */
108 struct mangled_decl_hash : ggc_remove <tree>
110 typedef tree value_type; /* A DECL. */
111 typedef tree compare_type; /* An identifier. */
113 static hashval_t hash (const value_type decl)
115 return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME_RAW (decl));
117 static bool equal (const value_type existing, compare_type candidate)
119 tree name = DECL_ASSEMBLER_NAME_RAW (existing);
120 return candidate == name;
123 static inline void mark_empty (value_type &p) {p = NULL_TREE;}
124 static inline bool is_empty (value_type p) {return !p;}
126 static bool is_deleted (value_type e)
128 return e == reinterpret_cast <value_type> (1);
130 static void mark_deleted (value_type &e)
132 e = reinterpret_cast <value_type> (1);
136 /* A hash table of decls keyed by mangled name. Used to figure out if
137 we need compatibility aliases. */
138 static GTY(()) hash_table<mangled_decl_hash> *mangled_decls;
140 /* Nonzero if we're done parsing and into end-of-file activities. */
142 int at_eof;
144 /* True if note_mangling_alias should enqueue mangling aliases for
145 later generation, rather than emitting them right away. */
147 bool defer_mangling_aliases = true;
150 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
151 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
152 that apply to the function). */
154 tree
155 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
156 cp_ref_qualifier rqual)
158 if (fntype == error_mark_node || ctype == error_mark_node)
159 return error_mark_node;
161 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
162 || TREE_CODE (fntype) == METHOD_TYPE);
164 cp_cv_quals type_quals = quals & ~TYPE_QUAL_RESTRICT;
165 ctype = cp_build_qualified_type (ctype, type_quals);
167 tree newtype
168 = build_method_type_directly (ctype, TREE_TYPE (fntype),
169 (TREE_CODE (fntype) == METHOD_TYPE
170 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
171 : TYPE_ARG_TYPES (fntype)));
172 if (tree attrs = TYPE_ATTRIBUTES (fntype))
173 newtype = cp_build_type_attribute_variant (newtype, attrs);
174 newtype = build_cp_fntype_variant (newtype, rqual,
175 TYPE_RAISES_EXCEPTIONS (fntype),
176 TYPE_HAS_LATE_RETURN_TYPE (fntype));
178 return newtype;
181 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
182 return type changed to NEW_RET. */
184 tree
185 change_return_type (tree new_ret, tree fntype)
187 if (new_ret == error_mark_node)
188 return fntype;
190 if (same_type_p (new_ret, TREE_TYPE (fntype)))
191 return fntype;
193 tree newtype;
194 tree args = TYPE_ARG_TYPES (fntype);
196 if (TREE_CODE (fntype) == FUNCTION_TYPE)
198 newtype = build_function_type (new_ret, args);
199 newtype = apply_memfn_quals (newtype,
200 type_memfn_quals (fntype));
202 else
203 newtype = build_method_type_directly
204 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
206 if (tree attrs = TYPE_ATTRIBUTES (fntype))
207 newtype = cp_build_type_attribute_variant (newtype, attrs);
208 newtype = cxx_copy_lang_qualifiers (newtype, fntype);
210 return newtype;
213 /* Build a PARM_DECL of FN with NAME and TYPE, and set DECL_ARG_TYPE
214 appropriately. */
216 tree
217 cp_build_parm_decl (tree fn, tree name, tree type)
219 tree parm = build_decl (input_location,
220 PARM_DECL, name, type);
221 DECL_CONTEXT (parm) = fn;
223 /* DECL_ARG_TYPE is only used by the back end and the back end never
224 sees templates. */
225 if (!processing_template_decl)
226 DECL_ARG_TYPE (parm) = type_passed_as (type);
228 return parm;
231 /* Returns a PARM_DECL of FN for a parameter of the indicated TYPE, with the
232 indicated NAME. */
234 tree
235 build_artificial_parm (tree fn, tree name, tree type)
237 tree parm = cp_build_parm_decl (fn, name, type);
238 DECL_ARTIFICIAL (parm) = 1;
239 /* All our artificial parms are implicitly `const'; they cannot be
240 assigned to. */
241 TREE_READONLY (parm) = 1;
242 return parm;
245 /* Constructors for types with virtual baseclasses need an "in-charge" flag
246 saying whether this constructor is responsible for initialization of
247 virtual baseclasses or not. All destructors also need this "in-charge"
248 flag, which additionally determines whether or not the destructor should
249 free the memory for the object.
251 This function adds the "in-charge" flag to member function FN if
252 appropriate. It is called from grokclassfn and tsubst.
253 FN must be either a constructor or destructor.
255 The in-charge flag follows the 'this' parameter, and is followed by the
256 VTT parm (if any), then the user-written parms. */
258 void
259 maybe_retrofit_in_chrg (tree fn)
261 tree basetype, arg_types, parms, parm, fntype;
263 /* If we've already add the in-charge parameter don't do it again. */
264 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
265 return;
267 /* When processing templates we can't know, in general, whether or
268 not we're going to have virtual baseclasses. */
269 if (processing_template_decl)
270 return;
272 /* We don't need an in-charge parameter for constructors that don't
273 have virtual bases. */
274 if (DECL_CONSTRUCTOR_P (fn)
275 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
276 return;
278 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
279 basetype = TREE_TYPE (TREE_VALUE (arg_types));
280 arg_types = TREE_CHAIN (arg_types);
282 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
284 /* If this is a subobject constructor or destructor, our caller will
285 pass us a pointer to our VTT. */
286 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
288 parm = build_artificial_parm (fn, vtt_parm_identifier, vtt_parm_type);
290 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
291 DECL_CHAIN (parm) = parms;
292 parms = parm;
294 /* ...and then to TYPE_ARG_TYPES. */
295 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
297 DECL_HAS_VTT_PARM_P (fn) = 1;
300 /* Then add the in-charge parm (before the VTT parm). */
301 parm = build_artificial_parm (fn, in_charge_identifier, integer_type_node);
302 DECL_CHAIN (parm) = parms;
303 parms = parm;
304 arg_types = hash_tree_chain (integer_type_node, arg_types);
306 /* Insert our new parameter(s) into the list. */
307 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
309 /* And rebuild the function type. */
310 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
311 arg_types);
312 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
313 fntype = (cp_build_type_attribute_variant
314 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
315 fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (fn));
316 TREE_TYPE (fn) = fntype;
318 /* Now we've got the in-charge parameter. */
319 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
322 /* Classes overload their constituent function names automatically.
323 When a function name is declared in a record structure,
324 its name is changed to it overloaded name. Since names for
325 constructors and destructors can conflict, we place a leading
326 '$' for destructors.
328 CNAME is the name of the class we are grokking for.
330 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
332 FLAGS contains bits saying what's special about today's
333 arguments. DTOR_FLAG == DESTRUCTOR.
335 If FUNCTION is a destructor, then we must add the `auto-delete' field
336 as a second parameter. There is some hair associated with the fact
337 that we must "declare" this variable in the manner consistent with the
338 way the rest of the arguments were declared.
340 QUALS are the qualifiers for the this pointer. */
342 void
343 grokclassfn (tree ctype, tree function, enum overload_flags flags)
345 tree fn_name = DECL_NAME (function);
347 /* Even within an `extern "C"' block, members get C++ linkage. See
348 [dcl.link] for details. */
349 SET_DECL_LANGUAGE (function, lang_cplusplus);
351 if (fn_name == NULL_TREE)
353 error ("name missing for member function");
354 fn_name = get_identifier ("<anonymous>");
355 DECL_NAME (function) = fn_name;
358 DECL_CONTEXT (function) = ctype;
360 if (flags == DTOR_FLAG)
361 DECL_CXX_DESTRUCTOR_P (function) = 1;
363 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
364 maybe_retrofit_in_chrg (function);
367 /* Create an ARRAY_REF, checking for the user doing things backwards
368 along the way. DECLTYPE_P is for N3276, as in the parser. */
370 tree
371 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
372 bool decltype_p)
374 tree type;
375 tree expr;
376 tree orig_array_expr = array_expr;
377 tree orig_index_exp = index_exp;
378 tree overload = NULL_TREE;
380 if (error_operand_p (array_expr) || error_operand_p (index_exp))
381 return error_mark_node;
383 if (processing_template_decl)
385 if (type_dependent_expression_p (array_expr)
386 || type_dependent_expression_p (index_exp))
387 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
388 NULL_TREE, NULL_TREE);
389 array_expr = build_non_dependent_expr (array_expr);
390 index_exp = build_non_dependent_expr (index_exp);
393 type = TREE_TYPE (array_expr);
394 gcc_assert (type);
395 type = non_reference (type);
397 /* If they have an `operator[]', use that. */
398 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
400 tsubst_flags_t complain = tf_warning_or_error;
401 if (decltype_p)
402 complain |= tf_decltype;
403 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
404 index_exp, NULL_TREE, &overload, complain);
406 else
408 tree p1, p2, i1, i2;
410 /* Otherwise, create an ARRAY_REF for a pointer or array type.
411 It is a little-known fact that, if `a' is an array and `i' is
412 an int, you can write `i[a]', which means the same thing as
413 `a[i]'. */
414 if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
415 p1 = array_expr;
416 else
417 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
419 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
420 p2 = index_exp;
421 else
422 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
424 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
425 false);
426 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
427 false);
429 if ((p1 && i2) && (i1 && p2))
430 error ("ambiguous conversion for array subscript");
432 if (p1 && i2)
433 array_expr = p1, index_exp = i2;
434 else if (i1 && p2)
435 array_expr = p2, index_exp = i1;
436 else
438 error ("invalid types %<%T[%T]%> for array subscript",
439 type, TREE_TYPE (index_exp));
440 return error_mark_node;
443 if (array_expr == error_mark_node || index_exp == error_mark_node)
444 error ("ambiguous conversion for array subscript");
446 if (TYPE_PTR_P (TREE_TYPE (array_expr)))
447 array_expr = mark_rvalue_use (array_expr);
448 else
449 array_expr = mark_lvalue_use_nonread (array_expr);
450 index_exp = mark_rvalue_use (index_exp);
451 expr = build_array_ref (input_location, array_expr, index_exp);
453 if (processing_template_decl && expr != error_mark_node)
455 if (overload != NULL_TREE)
456 return (build_min_non_dep_op_overload
457 (ARRAY_REF, expr, overload, orig_array_expr, orig_index_exp));
459 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
460 NULL_TREE, NULL_TREE);
462 return expr;
465 /* Given the cast expression EXP, checking out its validity. Either return
466 an error_mark_node if there was an unavoidable error, return a cast to
467 void for trying to delete a pointer w/ the value 0, or return the
468 call to delete. If DOING_VEC is true, we handle things differently
469 for doing an array delete.
470 Implements ARM $5.3.4. This is called from the parser. */
472 tree
473 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
474 tsubst_flags_t complain)
476 tree t, type;
478 if (exp == error_mark_node)
479 return exp;
481 if (processing_template_decl)
483 t = build_min (DELETE_EXPR, void_type_node, exp, size);
484 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
485 DELETE_EXPR_USE_VEC (t) = doing_vec;
486 TREE_SIDE_EFFECTS (t) = 1;
487 return t;
490 /* An array can't have been allocated by new, so complain. */
491 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
492 warning (0, "deleting array %q#E", exp);
494 t = build_expr_type_conversion (WANT_POINTER, exp, true);
496 if (t == NULL_TREE || t == error_mark_node)
498 error ("type %q#T argument given to %<delete%>, expected pointer",
499 TREE_TYPE (exp));
500 return error_mark_node;
503 type = TREE_TYPE (t);
505 /* As of Valley Forge, you can delete a pointer to const. */
507 /* You can't delete functions. */
508 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
510 error ("cannot delete a function. Only pointer-to-objects are "
511 "valid arguments to %<delete%>");
512 return error_mark_node;
515 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
516 if (VOID_TYPE_P (TREE_TYPE (type)))
518 warning (OPT_Wdelete_incomplete, "deleting %qT is undefined", type);
519 doing_vec = 0;
522 /* Deleting a pointer with the value zero is valid and has no effect. */
523 if (integer_zerop (t))
524 return build1 (NOP_EXPR, void_type_node, t);
526 if (doing_vec)
527 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
528 sfk_deleting_destructor,
529 use_global_delete, complain);
530 else
531 return build_delete (type, t, sfk_deleting_destructor,
532 LOOKUP_NORMAL, use_global_delete,
533 complain);
536 /* Report an error if the indicated template declaration is not the
537 sort of thing that should be a member template. */
539 void
540 check_member_template (tree tmpl)
542 tree decl;
544 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
545 decl = DECL_TEMPLATE_RESULT (tmpl);
547 if (TREE_CODE (decl) == FUNCTION_DECL
548 || DECL_ALIAS_TEMPLATE_P (tmpl)
549 || (TREE_CODE (decl) == TYPE_DECL
550 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
552 /* The parser rejects template declarations in local classes
553 (with the exception of generic lambdas). */
554 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
555 /* The parser rejects any use of virtual in a function template. */
556 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
557 && DECL_VIRTUAL_P (decl)));
559 /* The debug-information generating code doesn't know what to do
560 with member templates. */
561 DECL_IGNORED_P (tmpl) = 1;
563 else if (variable_template_p (tmpl))
564 /* OK */;
565 else
566 error ("template declaration of %q#D", decl);
569 /* Sanity check: report error if this function FUNCTION is not
570 really a member of the class (CTYPE) it is supposed to belong to.
571 TEMPLATE_PARMS is used to specify the template parameters of a member
572 template passed as FUNCTION_DECL. If the member template is passed as a
573 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
574 from the declaration. If the function is not a function template, it
575 must be NULL.
576 It returns the original declaration for the function, NULL_TREE if
577 no declaration was found, error_mark_node if an error was emitted. */
579 tree
580 check_classfn (tree ctype, tree function, tree template_parms)
582 if (DECL_USE_TEMPLATE (function)
583 && !(TREE_CODE (function) == TEMPLATE_DECL
584 && DECL_TEMPLATE_SPECIALIZATION (function))
585 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
586 /* Since this is a specialization of a member template,
587 we're not going to find the declaration in the class.
588 For example, in:
590 struct S { template <typename T> void f(T); };
591 template <> void S::f(int);
593 we're not going to find `S::f(int)', but there's no
594 reason we should, either. We let our callers know we didn't
595 find the method, but we don't complain. */
596 return NULL_TREE;
598 /* Basic sanity check: for a template function, the template parameters
599 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
600 if (TREE_CODE (function) == TEMPLATE_DECL)
602 if (template_parms
603 && !comp_template_parms (template_parms,
604 DECL_TEMPLATE_PARMS (function)))
606 error ("template parameter lists provided don%'t match the "
607 "template parameters of %qD", function);
608 return error_mark_node;
610 template_parms = DECL_TEMPLATE_PARMS (function);
613 /* OK, is this a definition of a member template? */
614 bool is_template = (template_parms != NULL_TREE);
616 /* [temp.mem]
618 A destructor shall not be a member template. */
619 if (DECL_DESTRUCTOR_P (function) && is_template)
621 error ("destructor %qD declared as member template", function);
622 return error_mark_node;
625 /* We must enter the scope here, because conversion operators are
626 named by target type, and type equivalence relies on typenames
627 resolving within the scope of CTYPE. */
628 tree pushed_scope = push_scope (ctype);
629 tree matched = NULL_TREE;
630 tree fns = get_class_binding (ctype, DECL_NAME (function));
632 for (ovl_iterator iter (fns); !matched && iter; ++iter)
634 tree fndecl = *iter;
636 /* A member template definition only matches a member template
637 declaration. */
638 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
639 continue;
641 if (!DECL_DECLARES_FUNCTION_P (fndecl))
642 continue;
644 tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
645 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
647 /* We cannot simply call decls_match because this doesn't work
648 for static member functions that are pretending to be
649 methods, and because the name may have been changed by
650 asm("new_name"). */
652 /* Get rid of the this parameter on functions that become
653 static. */
654 if (DECL_STATIC_FUNCTION_P (fndecl)
655 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
656 p1 = TREE_CHAIN (p1);
658 /* ref-qualifier or absence of same must match. */
659 if (type_memfn_rqual (TREE_TYPE (function))
660 != type_memfn_rqual (TREE_TYPE (fndecl)))
661 continue;
663 // Include constraints in the match.
664 tree c1 = get_constraints (function);
665 tree c2 = get_constraints (fndecl);
667 /* While finding a match, same types and params are not enough
668 if the function is versioned. Also check version ("target")
669 attributes. */
670 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
671 TREE_TYPE (TREE_TYPE (fndecl)))
672 && compparms (p1, p2)
673 && !targetm.target_option.function_versions (function, fndecl)
674 && (!is_template
675 || comp_template_parms (template_parms,
676 DECL_TEMPLATE_PARMS (fndecl)))
677 && equivalent_constraints (c1, c2)
678 && (DECL_TEMPLATE_SPECIALIZATION (function)
679 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
680 && (!DECL_TEMPLATE_SPECIALIZATION (function)
681 || (DECL_TI_TEMPLATE (function) == DECL_TI_TEMPLATE (fndecl))))
682 matched = fndecl;
685 if (!matched)
687 if (!COMPLETE_TYPE_P (ctype))
688 cxx_incomplete_type_error (function, ctype);
689 else
691 if (DECL_CONV_FN_P (function))
692 fns = get_class_binding (ctype, conv_op_identifier);
694 error_at (DECL_SOURCE_LOCATION (function),
695 "no declaration matches %q#D", function);
696 if (fns)
697 print_candidates (fns);
698 else if (DECL_CONV_FN_P (function))
699 inform (DECL_SOURCE_LOCATION (function),
700 "no conversion operators declared");
701 else
702 inform (DECL_SOURCE_LOCATION (function),
703 "no functions named %qD", function);
704 inform (DECL_SOURCE_LOCATION (TYPE_NAME (ctype)),
705 "%#qT defined here", ctype);
707 matched = error_mark_node;
710 if (pushed_scope)
711 pop_scope (pushed_scope);
713 return matched;
716 /* DECL is a function with vague linkage. Remember it so that at the
717 end of the translation unit we can decide whether or not to emit
718 it. */
720 void
721 note_vague_linkage_fn (tree decl)
723 if (processing_template_decl)
724 return;
726 DECL_DEFER_OUTPUT (decl) = 1;
727 vec_safe_push (deferred_fns, decl);
730 /* As above, but for variable template instantiations. */
732 void
733 note_variable_template_instantiation (tree decl)
735 vec_safe_push (pending_statics, decl);
738 /* We have just processed the DECL, which is a static data member.
739 The other parameters are as for cp_finish_decl. */
741 void
742 finish_static_data_member_decl (tree decl,
743 tree init, bool init_const_expr_p,
744 tree asmspec_tree,
745 int flags)
747 DECL_CONTEXT (decl) = current_class_type;
749 /* We cannot call pushdecl here, because that would fill in the
750 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
751 the right thing, namely, to put this decl out straight away. */
753 if (! processing_template_decl)
754 vec_safe_push (pending_statics, decl);
756 if (LOCAL_CLASS_P (current_class_type)
757 /* We already complained about the template definition. */
758 && !DECL_TEMPLATE_INSTANTIATION (decl))
759 permerror (DECL_SOURCE_LOCATION (decl),
760 "local class %q#T shall not have static data member %q#D",
761 current_class_type, decl);
762 else
763 for (tree t = current_class_type; TYPE_P (t);
764 t = CP_TYPE_CONTEXT (t))
765 if (TYPE_UNNAMED_P (t))
767 auto_diagnostic_group d;
768 if (permerror (DECL_SOURCE_LOCATION (decl),
769 "static data member %qD in unnamed class", decl))
770 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
771 "unnamed class defined here");
772 break;
775 DECL_IN_AGGR_P (decl) = 1;
777 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
778 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
779 SET_VAR_HAD_UNKNOWN_BOUND (decl);
781 if (init)
783 /* Similarly to start_decl_1, we want to complete the type in order
784 to do the right thing in cp_apply_type_quals_to_decl, possibly
785 clear TYPE_QUAL_CONST (c++/65579). */
786 tree type = TREE_TYPE (decl) = complete_type (TREE_TYPE (decl));
787 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
790 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
793 /* DECLARATOR and DECLSPECS correspond to a class member. The other
794 parameters are as for cp_finish_decl. Return the DECL for the
795 class member declared. */
797 tree
798 grokfield (const cp_declarator *declarator,
799 cp_decl_specifier_seq *declspecs,
800 tree init, bool init_const_expr_p,
801 tree asmspec_tree,
802 tree attrlist)
804 tree value;
805 const char *asmspec = 0;
806 int flags;
807 tree name;
809 if (init
810 && TREE_CODE (init) == TREE_LIST
811 && TREE_VALUE (init) == error_mark_node
812 && TREE_CHAIN (init) == NULL_TREE)
813 init = NULL_TREE;
815 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
816 if (! value || value == error_mark_node)
817 /* friend or constructor went bad. */
818 return error_mark_node;
819 if (TREE_TYPE (value) == error_mark_node)
820 return value;
822 if (TREE_CODE (value) == TYPE_DECL && init)
824 error ("typedef %qD is initialized (use decltype instead)", value);
825 init = NULL_TREE;
828 /* Pass friendly classes back. */
829 if (value == void_type_node)
830 return value;
833 name = DECL_NAME (value);
835 if (name != NULL_TREE)
837 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
839 error ("explicit template argument list not allowed");
840 return error_mark_node;
843 if (IDENTIFIER_POINTER (name)[0] == '_'
844 && id_equal (name, "_vptr"))
845 error ("member %qD conflicts with virtual function table field name",
846 value);
849 /* Stash away type declarations. */
850 if (TREE_CODE (value) == TYPE_DECL)
852 DECL_NONLOCAL (value) = 1;
853 DECL_CONTEXT (value) = current_class_type;
855 if (attrlist)
857 int attrflags = 0;
859 /* If this is a typedef that names the class for linkage purposes
860 (7.1.3p8), apply any attributes directly to the type. */
861 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
862 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
863 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
865 cplus_decl_attributes (&value, attrlist, attrflags);
868 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
869 && TREE_TYPE (value) != error_mark_node
870 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
871 set_underlying_type (value);
873 /* It's important that push_template_decl below follows
874 set_underlying_type above so that the created template
875 carries the properly set type of VALUE. */
876 if (processing_template_decl)
877 value = push_template_decl (value);
879 record_locally_defined_typedef (value);
880 return value;
883 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
885 if (!friendp && DECL_IN_AGGR_P (value))
887 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
888 return void_type_node;
891 if (asmspec_tree && asmspec_tree != error_mark_node)
892 asmspec = TREE_STRING_POINTER (asmspec_tree);
894 if (init)
896 if (TREE_CODE (value) == FUNCTION_DECL)
898 if (init == ridpointers[(int)RID_DELETE])
900 if (friendp && decl_defined_p (value))
902 error ("redefinition of %q#D", value);
903 inform (DECL_SOURCE_LOCATION (value),
904 "%q#D previously defined here", value);
906 else
908 DECL_DELETED_FN (value) = 1;
909 DECL_DECLARED_INLINE_P (value) = 1;
910 DECL_INITIAL (value) = error_mark_node;
913 else if (init == ridpointers[(int)RID_DEFAULT])
915 if (defaultable_fn_check (value))
917 DECL_DEFAULTED_FN (value) = 1;
918 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
919 DECL_DECLARED_INLINE_P (value) = 1;
922 else if (TREE_CODE (init) == DEFAULT_ARG)
923 error ("invalid initializer for member function %qD", value);
924 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
926 if (integer_zerop (init))
927 DECL_PURE_VIRTUAL_P (value) = 1;
928 else if (error_operand_p (init))
929 ; /* An error has already been reported. */
930 else
931 error ("invalid initializer for member function %qD",
932 value);
934 else
936 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
937 if (friendp)
938 error ("initializer specified for friend function %qD",
939 value);
940 else
941 error ("initializer specified for static member function %qD",
942 value);
945 else if (TREE_CODE (value) == FIELD_DECL)
946 /* C++11 NSDMI, keep going. */;
947 else if (!VAR_P (value))
948 gcc_unreachable ();
951 /* Pass friend decls back. */
952 if ((TREE_CODE (value) == FUNCTION_DECL
953 || TREE_CODE (value) == TEMPLATE_DECL)
954 && DECL_CONTEXT (value) != current_class_type)
955 return value;
957 /* Need to set this before push_template_decl. */
958 if (VAR_P (value))
959 DECL_CONTEXT (value) = current_class_type;
961 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
963 value = push_template_decl (value);
964 if (error_operand_p (value))
965 return error_mark_node;
968 if (attrlist)
969 cplus_decl_attributes (&value, attrlist, 0);
971 if (init && DIRECT_LIST_INIT_P (init))
972 flags = LOOKUP_NORMAL;
973 else
974 flags = LOOKUP_IMPLICIT;
976 switch (TREE_CODE (value))
978 case VAR_DECL:
979 finish_static_data_member_decl (value, init, init_const_expr_p,
980 asmspec_tree, flags);
981 return value;
983 case FIELD_DECL:
984 if (asmspec)
985 error ("%<asm%> specifiers are not permitted on non-static data members");
986 if (DECL_INITIAL (value) == error_mark_node)
987 init = error_mark_node;
988 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
989 NULL_TREE, flags);
990 DECL_IN_AGGR_P (value) = 1;
991 return value;
993 case FUNCTION_DECL:
994 if (asmspec)
995 set_user_assembler_name (value, asmspec);
997 cp_finish_decl (value,
998 /*init=*/NULL_TREE,
999 /*init_const_expr_p=*/false,
1000 asmspec_tree, flags);
1002 /* Pass friends back this way. */
1003 if (DECL_FRIEND_P (value))
1004 return void_type_node;
1006 DECL_IN_AGGR_P (value) = 1;
1007 return value;
1009 default:
1010 gcc_unreachable ();
1012 return NULL_TREE;
1015 /* Like `grokfield', but for bitfields.
1016 WIDTH is the width of the bitfield, a constant expression.
1017 The other parameters are as for grokfield. */
1019 tree
1020 grokbitfield (const cp_declarator *declarator,
1021 cp_decl_specifier_seq *declspecs, tree width, tree init,
1022 tree attrlist)
1024 tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1025 init != NULL_TREE, &attrlist);
1027 if (value == error_mark_node)
1028 return NULL_TREE; /* friends went bad. */
1029 if (TREE_TYPE (value) == error_mark_node)
1030 return value;
1032 /* Pass friendly classes back. */
1033 if (VOID_TYPE_P (value))
1034 return void_type_node;
1036 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
1037 && (INDIRECT_TYPE_P (value)
1038 || !dependent_type_p (TREE_TYPE (value))))
1040 error ("bit-field %qD with non-integral type", value);
1041 return error_mark_node;
1044 if (TREE_CODE (value) == TYPE_DECL)
1046 error ("cannot declare %qD to be a bit-field type", value);
1047 return NULL_TREE;
1050 /* Usually, finish_struct_1 catches bitfields with invalid types.
1051 But, in the case of bitfields with function type, we confuse
1052 ourselves into thinking they are member functions, so we must
1053 check here. */
1054 if (TREE_CODE (value) == FUNCTION_DECL)
1056 error ("cannot declare bit-field %qD with function type",
1057 DECL_NAME (value));
1058 return NULL_TREE;
1061 if (width && TYPE_WARN_IF_NOT_ALIGN (TREE_TYPE (value)))
1063 error ("cannot declare bit-field %qD with %<warn_if_not_aligned%> type",
1064 DECL_NAME (value));
1065 return NULL_TREE;
1068 if (DECL_IN_AGGR_P (value))
1070 error ("%qD is already defined in the class %qT", value,
1071 DECL_CONTEXT (value));
1072 return void_type_node;
1075 if (TREE_STATIC (value))
1077 error ("static member %qD cannot be a bit-field", value);
1078 return NULL_TREE;
1081 int flags = LOOKUP_IMPLICIT;
1082 if (init && DIRECT_LIST_INIT_P (init))
1083 flags = LOOKUP_NORMAL;
1084 cp_finish_decl (value, init, false, NULL_TREE, flags);
1086 if (width != error_mark_node)
1088 /* The width must be an integer type. */
1089 if (!type_dependent_expression_p (width)
1090 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1091 error ("width of bit-field %qD has non-integral type %qT", value,
1092 TREE_TYPE (width));
1093 else
1095 /* Temporarily stash the width in DECL_BIT_FIELD_REPRESENTATIVE.
1096 check_bitfield_decl picks it from there later and sets DECL_SIZE
1097 accordingly. */
1098 DECL_BIT_FIELD_REPRESENTATIVE (value) = width;
1099 SET_DECL_C_BIT_FIELD (value);
1103 DECL_IN_AGGR_P (value) = 1;
1105 if (attrlist)
1106 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1108 return value;
1112 /* Returns true iff ATTR is an attribute which needs to be applied at
1113 instantiation time rather than template definition time. */
1115 static bool
1116 is_late_template_attribute (tree attr, tree decl)
1118 tree name = get_attribute_name (attr);
1119 tree args = TREE_VALUE (attr);
1120 const struct attribute_spec *spec = lookup_attribute_spec (name);
1121 tree arg;
1123 if (!spec)
1124 /* Unknown attribute. */
1125 return false;
1127 /* Attribute weak handling wants to write out assembly right away. */
1128 if (is_attribute_p ("weak", name))
1129 return true;
1131 /* Attributes used and unused are applied directly to typedefs for the
1132 benefit of maybe_warn_unused_local_typedefs. */
1133 if (TREE_CODE (decl) == TYPE_DECL
1134 && (is_attribute_p ("unused", name)
1135 || is_attribute_p ("used", name)))
1136 return false;
1138 /* Attribute tls_model wants to modify the symtab. */
1139 if (is_attribute_p ("tls_model", name))
1140 return true;
1142 /* #pragma omp declare simd attribute needs to be always deferred. */
1143 if (flag_openmp
1144 && is_attribute_p ("omp declare simd", name))
1145 return true;
1147 /* An attribute pack is clearly dependent. */
1148 if (args && PACK_EXPANSION_P (args))
1149 return true;
1151 /* If any of the arguments are dependent expressions, we can't evaluate
1152 the attribute until instantiation time. */
1153 for (arg = args; arg; arg = TREE_CHAIN (arg))
1155 tree t = TREE_VALUE (arg);
1157 /* If the first attribute argument is an identifier, only consider
1158 second and following arguments. Attributes like mode, format,
1159 cleanup and several target specific attributes aren't late
1160 just because they have an IDENTIFIER_NODE as first argument. */
1161 if (arg == args && attribute_takes_identifier_p (name)
1162 && identifier_p (t))
1163 continue;
1165 if (value_dependent_expression_p (t)
1166 || type_dependent_expression_p (t))
1167 return true;
1170 if (TREE_CODE (decl) == TYPE_DECL
1171 || TYPE_P (decl)
1172 || spec->type_required)
1174 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1176 /* We can't apply any attributes to a completely unknown type until
1177 instantiation time. */
1178 enum tree_code code = TREE_CODE (type);
1179 if (code == TEMPLATE_TYPE_PARM
1180 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1181 || code == TYPENAME_TYPE)
1182 return true;
1183 /* Also defer most attributes on dependent types. This is not
1184 necessary in all cases, but is the better default. */
1185 else if (dependent_type_p (type)
1186 /* But some attributes specifically apply to templates. */
1187 && !is_attribute_p ("abi_tag", name)
1188 && !is_attribute_p ("deprecated", name)
1189 && !is_attribute_p ("visibility", name))
1190 return true;
1191 else
1192 return false;
1194 else
1195 return false;
1198 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1199 applied at instantiation time and return them. If IS_DEPENDENT is true,
1200 the declaration itself is dependent, so all attributes should be applied
1201 at instantiation time. */
1203 static tree
1204 splice_template_attributes (tree *attr_p, tree decl)
1206 tree *p = attr_p;
1207 tree late_attrs = NULL_TREE;
1208 tree *q = &late_attrs;
1210 if (!p)
1211 return NULL_TREE;
1213 for (; *p; )
1215 if (is_late_template_attribute (*p, decl))
1217 ATTR_IS_DEPENDENT (*p) = 1;
1218 *q = *p;
1219 *p = TREE_CHAIN (*p);
1220 q = &TREE_CHAIN (*q);
1221 *q = NULL_TREE;
1223 else
1224 p = &TREE_CHAIN (*p);
1227 return late_attrs;
1230 /* Remove any late attributes from the list in ATTR_P and attach them to
1231 DECL_P. */
1233 static void
1234 save_template_attributes (tree *attr_p, tree *decl_p, int flags)
1236 tree *q;
1238 if (attr_p && *attr_p == error_mark_node)
1239 return;
1241 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1242 if (!late_attrs)
1243 return;
1245 if (DECL_P (*decl_p))
1246 q = &DECL_ATTRIBUTES (*decl_p);
1247 else
1248 q = &TYPE_ATTRIBUTES (*decl_p);
1250 tree old_attrs = *q;
1252 /* Merge the late attributes at the beginning with the attribute
1253 list. */
1254 late_attrs = merge_attributes (late_attrs, *q);
1255 if (*q != late_attrs
1256 && !DECL_P (*decl_p)
1257 && !(flags & ATTR_FLAG_TYPE_IN_PLACE))
1259 if (!dependent_type_p (*decl_p))
1260 *decl_p = cp_build_type_attribute_variant (*decl_p, late_attrs);
1261 else
1263 *decl_p = build_variant_type_copy (*decl_p);
1264 TYPE_ATTRIBUTES (*decl_p) = late_attrs;
1267 else
1268 *q = late_attrs;
1270 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1272 /* We've added new attributes directly to the main variant, so
1273 now we need to update all of the other variants to include
1274 these new attributes. */
1275 tree variant;
1276 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1277 variant = TYPE_NEXT_VARIANT (variant))
1279 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1280 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1285 /* True if ATTRS contains any dependent attributes that affect type
1286 identity. */
1288 bool
1289 any_dependent_type_attributes_p (tree attrs)
1291 for (tree a = attrs; a; a = TREE_CHAIN (a))
1292 if (ATTR_IS_DEPENDENT (a))
1294 const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (a));
1295 if (as && as->affects_type_identity)
1296 return true;
1298 return false;
1301 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1302 to a typedef which gives a previously unnamed class or enum a name for
1303 linkage purposes. */
1305 bool
1306 attributes_naming_typedef_ok (tree attrs)
1308 for (; attrs; attrs = TREE_CHAIN (attrs))
1310 tree name = get_attribute_name (attrs);
1311 if (is_attribute_p ("vector_size", name))
1312 return false;
1314 return true;
1317 /* Like reconstruct_complex_type, but handle also template trees. */
1319 tree
1320 cp_reconstruct_complex_type (tree type, tree bottom)
1322 tree inner, outer;
1324 if (TYPE_PTR_P (type))
1326 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1327 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1328 TYPE_REF_CAN_ALIAS_ALL (type));
1330 else if (TYPE_REF_P (type))
1332 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1333 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1334 TYPE_REF_CAN_ALIAS_ALL (type));
1336 else if (TREE_CODE (type) == ARRAY_TYPE)
1338 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1339 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1340 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1341 element type qualification will be handled by the recursive
1342 cp_reconstruct_complex_type call and cp_build_qualified_type
1343 for ARRAY_TYPEs changes the element type. */
1344 return outer;
1346 else if (TREE_CODE (type) == FUNCTION_TYPE)
1348 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1349 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1350 outer = apply_memfn_quals (outer, type_memfn_quals (type));
1352 else if (TREE_CODE (type) == METHOD_TYPE)
1354 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1355 /* The build_method_type_directly() routine prepends 'this' to argument list,
1356 so we must compensate by getting rid of it. */
1357 outer
1358 = build_method_type_directly
1359 (class_of_this_parm (type), inner,
1360 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1362 else if (TREE_CODE (type) == OFFSET_TYPE)
1364 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1365 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1367 else
1368 return bottom;
1370 if (TYPE_ATTRIBUTES (type))
1371 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1372 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1373 outer = cxx_copy_lang_qualifiers (outer, type);
1375 return outer;
1378 /* Replaces any constexpr expression that may be into the attributes
1379 arguments with their reduced value. */
1381 static void
1382 cp_check_const_attributes (tree attributes)
1384 if (attributes == error_mark_node)
1385 return;
1387 tree attr;
1388 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1390 tree arg;
1391 for (arg = TREE_VALUE (attr); arg && TREE_CODE (arg) == TREE_LIST;
1392 arg = TREE_CHAIN (arg))
1394 tree expr = TREE_VALUE (arg);
1395 if (EXPR_P (expr))
1396 TREE_VALUE (arg) = fold_non_dependent_expr (expr);
1401 /* Return true if TYPE is an OpenMP mappable type. */
1402 bool
1403 cp_omp_mappable_type (tree type)
1405 /* Mappable type has to be complete. */
1406 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1407 return false;
1408 /* Arrays have mappable type if the elements have mappable type. */
1409 while (TREE_CODE (type) == ARRAY_TYPE)
1410 type = TREE_TYPE (type);
1411 /* A mappable type cannot contain virtual members. */
1412 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1413 return false;
1414 /* All data members must be non-static. */
1415 if (CLASS_TYPE_P (type))
1417 tree field;
1418 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1419 if (VAR_P (field))
1420 return false;
1421 /* All fields must have mappable types. */
1422 else if (TREE_CODE (field) == FIELD_DECL
1423 && !cp_omp_mappable_type (TREE_TYPE (field)))
1424 return false;
1426 return true;
1429 /* Return the last pushed declaration for the symbol DECL or NULL
1430 when no such declaration exists. */
1432 static tree
1433 find_last_decl (tree decl)
1435 tree last_decl = NULL_TREE;
1437 if (tree name = DECL_P (decl) ? DECL_NAME (decl) : NULL_TREE)
1439 /* Look up the declaration in its scope. */
1440 tree pushed_scope = NULL_TREE;
1441 if (tree ctype = DECL_CONTEXT (decl))
1442 pushed_scope = push_scope (ctype);
1444 last_decl = lookup_name (name);
1446 if (pushed_scope)
1447 pop_scope (pushed_scope);
1449 /* The declaration may be a member conversion operator
1450 or a bunch of overfloads (handle the latter below). */
1451 if (last_decl && BASELINK_P (last_decl))
1452 last_decl = BASELINK_FUNCTIONS (last_decl);
1455 if (!last_decl)
1456 return NULL_TREE;
1458 if (DECL_P (last_decl) || TREE_CODE (last_decl) == OVERLOAD)
1460 /* A set of overloads of the same function. */
1461 for (lkp_iterator iter (last_decl); iter; ++iter)
1463 if (TREE_CODE (*iter) == OVERLOAD)
1464 continue;
1466 if (decls_match (decl, *iter, /*record_decls=*/false))
1467 return *iter;
1469 return NULL_TREE;
1472 return NULL_TREE;
1475 /* Like decl_attributes, but handle C++ complexity. */
1477 void
1478 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1480 if (*decl == NULL_TREE || *decl == void_type_node
1481 || *decl == error_mark_node)
1482 return;
1484 /* Add implicit "omp declare target" attribute if requested. */
1485 if (scope_chain->omp_declare_target_attribute
1486 && ((VAR_P (*decl)
1487 && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
1488 || TREE_CODE (*decl) == FUNCTION_DECL))
1490 if (VAR_P (*decl)
1491 && DECL_CLASS_SCOPE_P (*decl))
1492 error ("%q+D static data member inside of declare target directive",
1493 *decl);
1494 else if (VAR_P (*decl)
1495 && (processing_template_decl
1496 || !cp_omp_mappable_type (TREE_TYPE (*decl))))
1497 attributes = tree_cons (get_identifier ("omp declare target implicit"),
1498 NULL_TREE, attributes);
1499 else
1500 attributes = tree_cons (get_identifier ("omp declare target"),
1501 NULL_TREE, attributes);
1504 if (processing_template_decl)
1506 if (check_for_bare_parameter_packs (attributes))
1507 return;
1509 save_template_attributes (&attributes, decl, flags);
1512 cp_check_const_attributes (attributes);
1514 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1515 decl = &DECL_TEMPLATE_RESULT (*decl);
1517 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1519 attributes
1520 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1521 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1522 attributes, flags);
1524 else
1526 tree last_decl = find_last_decl (*decl);
1527 decl_attributes (decl, attributes, flags, last_decl);
1530 if (TREE_CODE (*decl) == TYPE_DECL)
1531 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1533 /* Propagate deprecation out to the template. */
1534 if (TREE_DEPRECATED (*decl))
1535 if (tree ti = get_template_info (*decl))
1537 tree tmpl = TI_TEMPLATE (ti);
1538 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1539 : DECL_TEMPLATE_RESULT (tmpl));
1540 if (*decl == pattern)
1541 TREE_DEPRECATED (tmpl) = true;
1545 /* Walks through the namespace- or function-scope anonymous union
1546 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1547 Returns one of the fields for use in the mangled name. */
1549 static tree
1550 build_anon_union_vars (tree type, tree object)
1552 tree main_decl = NULL_TREE;
1553 tree field;
1555 /* Rather than write the code to handle the non-union case,
1556 just give an error. */
1557 if (TREE_CODE (type) != UNION_TYPE)
1559 error ("anonymous struct not inside named type");
1560 return error_mark_node;
1563 for (field = TYPE_FIELDS (type);
1564 field != NULL_TREE;
1565 field = DECL_CHAIN (field))
1567 tree decl;
1568 tree ref;
1570 if (DECL_ARTIFICIAL (field))
1571 continue;
1572 if (TREE_CODE (field) != FIELD_DECL)
1574 permerror (DECL_SOURCE_LOCATION (field),
1575 "%q#D invalid; an anonymous union can only "
1576 "have non-static data members", field);
1577 continue;
1580 if (TREE_PRIVATE (field))
1581 permerror (DECL_SOURCE_LOCATION (field),
1582 "private member %q#D in anonymous union", field);
1583 else if (TREE_PROTECTED (field))
1584 permerror (DECL_SOURCE_LOCATION (field),
1585 "protected member %q#D in anonymous union", field);
1587 if (processing_template_decl)
1588 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1589 DECL_NAME (field), NULL_TREE);
1590 else
1591 ref = build_class_member_access_expr (object, field, NULL_TREE,
1592 false, tf_warning_or_error);
1594 if (DECL_NAME (field))
1596 tree base;
1598 decl = build_decl (input_location,
1599 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1600 DECL_ANON_UNION_VAR_P (decl) = 1;
1601 DECL_ARTIFICIAL (decl) = 1;
1603 base = get_base_address (object);
1604 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1605 TREE_STATIC (decl) = TREE_STATIC (base);
1606 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1608 SET_DECL_VALUE_EXPR (decl, ref);
1609 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1611 decl = pushdecl (decl);
1613 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1614 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1615 else
1616 decl = 0;
1618 if (main_decl == NULL_TREE)
1619 main_decl = decl;
1622 return main_decl;
1625 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1626 anonymous union, then all members must be laid out together. PUBLIC_P
1627 is nonzero if this union is not declared static. */
1629 void
1630 finish_anon_union (tree anon_union_decl)
1632 tree type;
1633 tree main_decl;
1634 bool public_p;
1636 if (anon_union_decl == error_mark_node)
1637 return;
1639 type = TREE_TYPE (anon_union_decl);
1640 public_p = TREE_PUBLIC (anon_union_decl);
1642 /* The VAR_DECL's context is the same as the TYPE's context. */
1643 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1645 if (TYPE_FIELDS (type) == NULL_TREE)
1646 return;
1648 if (public_p)
1650 error ("namespace-scope anonymous aggregates must be static");
1651 return;
1654 main_decl = build_anon_union_vars (type, anon_union_decl);
1655 if (main_decl == error_mark_node)
1656 return;
1657 if (main_decl == NULL_TREE)
1659 pedwarn (input_location, 0, "anonymous union with no members");
1660 return;
1663 if (!processing_template_decl)
1665 /* Use main_decl to set the mangled name. */
1666 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1667 maybe_commonize_var (anon_union_decl);
1668 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1669 mangle_decl (anon_union_decl);
1670 DECL_NAME (anon_union_decl) = NULL_TREE;
1673 pushdecl (anon_union_decl);
1674 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1677 /* Auxiliary functions to make type signatures for
1678 `operator new' and `operator delete' correspond to
1679 what compiler will be expecting. */
1681 tree
1682 coerce_new_type (tree type, location_t loc)
1684 int e = 0;
1685 tree args = TYPE_ARG_TYPES (type);
1687 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1689 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1691 e = 1;
1692 error_at (loc, "%<operator new%> must return type %qT",
1693 ptr_type_node);
1696 if (args && args != void_list_node)
1698 if (TREE_PURPOSE (args))
1700 /* [basic.stc.dynamic.allocation]
1702 The first parameter shall not have an associated default
1703 argument. */
1704 error_at (loc, "the first parameter of %<operator new%> cannot "
1705 "have a default argument");
1706 /* Throw away the default argument. */
1707 TREE_PURPOSE (args) = NULL_TREE;
1710 if (!same_type_p (TREE_VALUE (args), size_type_node))
1712 e = 2;
1713 args = TREE_CHAIN (args);
1716 else
1717 e = 2;
1719 if (e == 2)
1720 permerror (loc, "%<operator new%> takes type %<size_t%> (%qT) "
1721 "as first parameter", size_type_node);
1723 switch (e)
1725 case 2:
1726 args = tree_cons (NULL_TREE, size_type_node, args);
1727 /* Fall through. */
1728 case 1:
1729 type = (cxx_copy_lang_qualifiers
1730 (build_function_type (ptr_type_node, args),
1731 type));
1732 /* Fall through. */
1733 default:;
1735 return type;
1738 tree
1739 coerce_delete_type (tree type, location_t loc)
1741 int e = 0;
1742 tree args = TYPE_ARG_TYPES (type);
1744 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1746 if (!same_type_p (TREE_TYPE (type), void_type_node))
1748 e = 1;
1749 error_at (loc, "%<operator delete%> must return type %qT",
1750 void_type_node);
1753 if (!args || args == void_list_node
1754 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1756 e = 2;
1757 if (args && args != void_list_node)
1758 args = TREE_CHAIN (args);
1759 error_at (loc, "%<operator delete%> takes type %qT as first parameter",
1760 ptr_type_node);
1762 switch (e)
1764 case 2:
1765 args = tree_cons (NULL_TREE, ptr_type_node, args);
1766 /* Fall through. */
1767 case 1:
1768 type = (cxx_copy_lang_qualifiers
1769 (build_function_type (void_type_node, args),
1770 type));
1771 /* Fall through. */
1772 default:;
1775 return type;
1778 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1779 and mark them as needed. */
1781 static void
1782 mark_vtable_entries (tree decl)
1784 tree fnaddr;
1785 unsigned HOST_WIDE_INT idx;
1787 /* It's OK for the vtable to refer to deprecated virtual functions. */
1788 warning_sentinel w(warn_deprecated_decl);
1790 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1791 idx, fnaddr)
1793 tree fn;
1795 STRIP_NOPS (fnaddr);
1797 if (TREE_CODE (fnaddr) != ADDR_EXPR
1798 && TREE_CODE (fnaddr) != FDESC_EXPR)
1799 /* This entry is an offset: a virtual base class offset, a
1800 virtual call offset, an RTTI offset, etc. */
1801 continue;
1803 fn = TREE_OPERAND (fnaddr, 0);
1804 TREE_ADDRESSABLE (fn) = 1;
1805 /* When we don't have vcall offsets, we output thunks whenever
1806 we output the vtables that contain them. With vcall offsets,
1807 we know all the thunks we'll need when we emit a virtual
1808 function, so we emit the thunks there instead. */
1809 if (DECL_THUNK_P (fn))
1810 use_thunk (fn, /*emit_p=*/0);
1811 /* Set the location, as marking the function could cause
1812 instantiation. We do not need to preserve the incoming
1813 location, as we're called from c_parse_final_cleanups, which
1814 takes care of that. */
1815 input_location = DECL_SOURCE_LOCATION (fn);
1816 mark_used (fn);
1820 /* Adjust the TLS model on variable DECL if need be, typically after
1821 the linkage of DECL has been modified. */
1823 static void
1824 adjust_var_decl_tls_model (tree decl)
1826 if (CP_DECL_THREAD_LOCAL_P (decl)
1827 && !lookup_attribute ("tls_model", DECL_ATTRIBUTES (decl)))
1828 set_decl_tls_model (decl, decl_default_tls_model (decl));
1831 /* Set DECL up to have the closest approximation of "initialized common"
1832 linkage available. */
1834 void
1835 comdat_linkage (tree decl)
1837 if (flag_weak)
1838 make_decl_one_only (decl, cxx_comdat_group (decl));
1839 else if (TREE_CODE (decl) == FUNCTION_DECL
1840 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1841 /* We can just emit function and compiler-generated variables
1842 statically; having multiple copies is (for the most part) only
1843 a waste of space.
1845 There are two correctness issues, however: the address of a
1846 template instantiation with external linkage should be the
1847 same, independent of what translation unit asks for the
1848 address, and this will not hold when we emit multiple copies of
1849 the function. However, there's little else we can do.
1851 Also, by default, the typeinfo implementation assumes that
1852 there will be only one copy of the string used as the name for
1853 each type. Therefore, if weak symbols are unavailable, the
1854 run-time library should perform a more conservative check; it
1855 should perform a string comparison, rather than an address
1856 comparison. */
1857 TREE_PUBLIC (decl) = 0;
1858 else
1860 /* Static data member template instantiations, however, cannot
1861 have multiple copies. */
1862 if (DECL_INITIAL (decl) == 0
1863 || DECL_INITIAL (decl) == error_mark_node)
1864 DECL_COMMON (decl) = 1;
1865 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1867 DECL_COMMON (decl) = 1;
1868 DECL_INITIAL (decl) = error_mark_node;
1870 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1872 /* We can't do anything useful; leave vars for explicit
1873 instantiation. */
1874 DECL_EXTERNAL (decl) = 1;
1875 DECL_NOT_REALLY_EXTERN (decl) = 0;
1879 if (TREE_PUBLIC (decl))
1880 DECL_COMDAT (decl) = 1;
1882 if (VAR_P (decl))
1883 adjust_var_decl_tls_model (decl);
1886 /* For win32 we also want to put explicit instantiations in
1887 linkonce sections, so that they will be merged with implicit
1888 instantiations; otherwise we get duplicate symbol errors.
1889 For Darwin we do not want explicit instantiations to be
1890 linkonce. */
1892 void
1893 maybe_make_one_only (tree decl)
1895 /* We used to say that this was not necessary on targets that support weak
1896 symbols, because the implicit instantiations will defer to the explicit
1897 one. However, that's not actually the case in SVR4; a strong definition
1898 after a weak one is an error. Also, not making explicit
1899 instantiations one_only means that we can end up with two copies of
1900 some template instantiations. */
1901 if (! flag_weak)
1902 return;
1904 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1905 we can get away with not emitting them if they aren't used. We need
1906 to for variables so that cp_finish_decl will update their linkage,
1907 because their DECL_INITIAL may not have been set properly yet. */
1909 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1910 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1911 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1913 make_decl_one_only (decl, cxx_comdat_group (decl));
1915 if (VAR_P (decl))
1917 varpool_node *node = varpool_node::get_create (decl);
1918 DECL_COMDAT (decl) = 1;
1919 /* Mark it needed so we don't forget to emit it. */
1920 node->forced_by_abi = true;
1921 TREE_USED (decl) = 1;
1923 adjust_var_decl_tls_model (decl);
1928 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1929 This predicate will give the right answer during parsing of the
1930 function, which other tests may not. */
1932 bool
1933 vague_linkage_p (tree decl)
1935 if (!TREE_PUBLIC (decl))
1937 /* maybe_thunk_body clears TREE_PUBLIC and DECL_ABSTRACT_P on the
1938 maybe-in-charge 'tor variants; in that case we need to check one of
1939 the "clones" for the real linkage. But only in that case; before
1940 maybe_clone_body we haven't yet copied the linkage to the clones. */
1941 if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl)
1942 && !DECL_ABSTRACT_P (decl)
1943 && DECL_CHAIN (decl)
1944 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)))
1945 return vague_linkage_p (DECL_CHAIN (decl));
1947 gcc_checking_assert (!DECL_COMDAT (decl));
1948 return false;
1950 /* Unfortunately, import_export_decl has not always been called
1951 before the function is processed, so we cannot simply check
1952 DECL_COMDAT. */
1953 if (DECL_COMDAT (decl)
1954 || (TREE_CODE (decl) == FUNCTION_DECL
1955 && DECL_DECLARED_INLINE_P (decl))
1956 || (DECL_LANG_SPECIFIC (decl)
1957 && DECL_TEMPLATE_INSTANTIATION (decl))
1958 || (VAR_P (decl) && DECL_INLINE_VAR_P (decl)))
1959 return true;
1960 else if (DECL_FUNCTION_SCOPE_P (decl))
1961 /* A local static in an inline effectively has vague linkage. */
1962 return (TREE_STATIC (decl)
1963 && vague_linkage_p (DECL_CONTEXT (decl)));
1964 else
1965 return false;
1968 /* Determine whether or not we want to specifically import or export CTYPE,
1969 using various heuristics. */
1971 static void
1972 import_export_class (tree ctype)
1974 /* -1 for imported, 1 for exported. */
1975 int import_export = 0;
1977 /* It only makes sense to call this function at EOF. The reason is
1978 that this function looks at whether or not the first non-inline
1979 non-abstract virtual member function has been defined in this
1980 translation unit. But, we can't possibly know that until we've
1981 seen the entire translation unit. */
1982 gcc_assert (at_eof);
1984 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1985 return;
1987 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1988 we will have CLASSTYPE_INTERFACE_ONLY set but not
1989 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1990 heuristic because someone will supply a #pragma implementation
1991 elsewhere, and deducing it here would produce a conflict. */
1992 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1993 return;
1995 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1996 import_export = -1;
1997 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1998 import_export = 1;
1999 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
2000 && !flag_implicit_templates)
2001 /* For a template class, without -fimplicit-templates, check the
2002 repository. If the virtual table is assigned to this
2003 translation unit, then export the class; otherwise, import
2004 it. */
2005 import_export = repo_export_class_p (ctype) ? 1 : -1;
2006 else if (TYPE_POLYMORPHIC_P (ctype))
2008 /* The ABI specifies that the virtual table and associated
2009 information are emitted with the key method, if any. */
2010 tree method = CLASSTYPE_KEY_METHOD (ctype);
2011 /* If weak symbol support is not available, then we must be
2012 careful not to emit the vtable when the key function is
2013 inline. An inline function can be defined in multiple
2014 translation units. If we were to emit the vtable in each
2015 translation unit containing a definition, we would get
2016 multiple definition errors at link-time. */
2017 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
2018 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
2021 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
2022 a definition anywhere else. */
2023 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
2024 import_export = 0;
2026 /* Allow back ends the chance to overrule the decision. */
2027 if (targetm.cxx.import_export_class)
2028 import_export = targetm.cxx.import_export_class (ctype, import_export);
2030 if (import_export)
2032 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
2033 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
2037 /* Return true if VAR has already been provided to the back end; in that
2038 case VAR should not be modified further by the front end. */
2039 static bool
2040 var_finalized_p (tree var)
2042 return varpool_node::get_create (var)->definition;
2045 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
2046 must be emitted in this translation unit. Mark it as such. */
2048 void
2049 mark_needed (tree decl)
2051 TREE_USED (decl) = 1;
2052 if (TREE_CODE (decl) == FUNCTION_DECL)
2054 /* Extern inline functions don't become needed when referenced.
2055 If we know a method will be emitted in other TU and no new
2056 functions can be marked reachable, just use the external
2057 definition. */
2058 struct cgraph_node *node = cgraph_node::get_create (decl);
2059 node->forced_by_abi = true;
2061 /* #pragma interface and -frepo code can call mark_needed for
2062 maybe-in-charge 'tors; mark the clones as well. */
2063 tree clone;
2064 FOR_EACH_CLONE (clone, decl)
2065 mark_needed (clone);
2067 else if (VAR_P (decl))
2069 varpool_node *node = varpool_node::get_create (decl);
2070 /* C++ frontend use mark_decl_references to force COMDAT variables
2071 to be output that might appear dead otherwise. */
2072 node->forced_by_abi = true;
2076 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
2077 returns true if a definition of this entity should be provided in
2078 this object file. Callers use this function to determine whether
2079 or not to let the back end know that a definition of DECL is
2080 available in this translation unit. */
2082 bool
2083 decl_needed_p (tree decl)
2085 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2086 /* This function should only be called at the end of the translation
2087 unit. We cannot be sure of whether or not something will be
2088 COMDAT until that point. */
2089 gcc_assert (at_eof);
2091 /* All entities with external linkage that are not COMDAT/EXTERN should be
2092 emitted; they may be referred to from other object files. */
2093 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
2094 return true;
2095 /* Functions marked "dllexport" must be emitted so that they are
2096 visible to other DLLs. */
2097 if (flag_keep_inline_dllexport
2098 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
2099 return true;
2101 /* When not optimizing, do not bother to produce definitions for extern
2102 symbols. */
2103 if (DECL_REALLY_EXTERN (decl)
2104 && ((TREE_CODE (decl) != FUNCTION_DECL
2105 && !optimize)
2106 || (TREE_CODE (decl) == FUNCTION_DECL
2107 && !opt_for_fn (decl, optimize)))
2108 && !lookup_attribute ("always_inline", decl))
2109 return false;
2111 /* If this entity was used, let the back end see it; it will decide
2112 whether or not to emit it into the object file. */
2113 if (TREE_USED (decl))
2114 return true;
2116 /* Virtual functions might be needed for devirtualization. */
2117 if (flag_devirtualize
2118 && TREE_CODE (decl) == FUNCTION_DECL
2119 && DECL_VIRTUAL_P (decl))
2120 return true;
2122 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2123 reference to DECL might cause it to be emitted later. */
2124 return false;
2127 /* If necessary, write out the vtables for the dynamic class CTYPE.
2128 Returns true if any vtables were emitted. */
2130 static bool
2131 maybe_emit_vtables (tree ctype)
2133 tree vtbl;
2134 tree primary_vtbl;
2135 int needed = 0;
2136 varpool_node *current = NULL, *last = NULL;
2138 /* If the vtables for this class have already been emitted there is
2139 nothing more to do. */
2140 primary_vtbl = CLASSTYPE_VTABLES (ctype);
2141 if (var_finalized_p (primary_vtbl))
2142 return false;
2143 /* Ignore dummy vtables made by get_vtable_decl. */
2144 if (TREE_TYPE (primary_vtbl) == void_type_node)
2145 return false;
2147 /* On some targets, we cannot determine the key method until the end
2148 of the translation unit -- which is when this function is
2149 called. */
2150 if (!targetm.cxx.key_method_may_be_inline ())
2151 determine_key_method (ctype);
2153 /* See if any of the vtables are needed. */
2154 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2156 import_export_decl (vtbl);
2157 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2158 needed = 1;
2160 if (!needed)
2162 /* If the references to this class' vtables are optimized away,
2163 still emit the appropriate debugging information. See
2164 dfs_debug_mark. */
2165 if (DECL_COMDAT (primary_vtbl)
2166 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2167 note_debug_info_needed (ctype);
2168 return false;
2171 /* The ABI requires that we emit all of the vtables if we emit any
2172 of them. */
2173 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2175 /* Mark entities references from the virtual table as used. */
2176 mark_vtable_entries (vtbl);
2178 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2180 vec<tree, va_gc> *cleanups = NULL;
2181 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2182 LOOKUP_NORMAL);
2184 /* It had better be all done at compile-time. */
2185 gcc_assert (!expr && !cleanups);
2188 /* Write it out. */
2189 DECL_EXTERNAL (vtbl) = 0;
2190 rest_of_decl_compilation (vtbl, 1, 1);
2192 /* Because we're only doing syntax-checking, we'll never end up
2193 actually marking the variable as written. */
2194 if (flag_syntax_only)
2195 TREE_ASM_WRITTEN (vtbl) = 1;
2196 else if (DECL_ONE_ONLY (vtbl))
2198 current = varpool_node::get_create (vtbl);
2199 if (last)
2200 current->add_to_same_comdat_group (last);
2201 last = current;
2205 /* Since we're writing out the vtable here, also write the debug
2206 info. */
2207 note_debug_info_needed (ctype);
2209 return true;
2212 /* A special return value from type_visibility meaning internal
2213 linkage. */
2215 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2217 /* walk_tree helper function for type_visibility. */
2219 static tree
2220 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2222 int *vis_p = (int *)data;
2223 if (! TYPE_P (*tp))
2225 *walk_subtrees = 0;
2227 else if (OVERLOAD_TYPE_P (*tp)
2228 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2230 *vis_p = VISIBILITY_ANON;
2231 return *tp;
2233 else if (CLASS_TYPE_P (*tp)
2234 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2235 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2236 return NULL;
2239 /* Returns the visibility of TYPE, which is the minimum visibility of its
2240 component types. */
2242 static int
2243 type_visibility (tree type)
2245 int vis = VISIBILITY_DEFAULT;
2246 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2247 return vis;
2250 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2251 specified (or if VISIBILITY is static). If TMPL is true, this
2252 constraint is for a template argument, and takes precedence
2253 over explicitly-specified visibility on the template. */
2255 static void
2256 constrain_visibility (tree decl, int visibility, bool tmpl)
2258 if (visibility == VISIBILITY_ANON)
2260 /* extern "C" declarations aren't affected by the anonymous
2261 namespace. */
2262 if (!DECL_EXTERN_C_P (decl))
2264 TREE_PUBLIC (decl) = 0;
2265 DECL_WEAK (decl) = 0;
2266 DECL_COMMON (decl) = 0;
2267 DECL_COMDAT (decl) = false;
2268 if (VAR_OR_FUNCTION_DECL_P (decl))
2270 struct symtab_node *snode = symtab_node::get (decl);
2272 if (snode)
2273 snode->set_comdat_group (NULL);
2275 DECL_INTERFACE_KNOWN (decl) = 1;
2276 if (DECL_LANG_SPECIFIC (decl))
2277 DECL_NOT_REALLY_EXTERN (decl) = 1;
2280 else if (visibility > DECL_VISIBILITY (decl)
2281 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2283 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2284 /* This visibility was not specified. */
2285 DECL_VISIBILITY_SPECIFIED (decl) = false;
2289 /* Constrain the visibility of DECL based on the visibility of its template
2290 arguments. */
2292 static void
2293 constrain_visibility_for_template (tree decl, tree targs)
2295 /* If this is a template instantiation, check the innermost
2296 template args for visibility constraints. The outer template
2297 args are covered by the class check. */
2298 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2299 int i;
2300 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2302 int vis = 0;
2304 tree arg = TREE_VEC_ELT (args, i-1);
2305 if (TYPE_P (arg))
2306 vis = type_visibility (arg);
2307 else
2309 if (REFERENCE_REF_P (arg))
2310 arg = TREE_OPERAND (arg, 0);
2311 if (TREE_TYPE (arg))
2312 STRIP_NOPS (arg);
2313 if (TREE_CODE (arg) == ADDR_EXPR)
2314 arg = TREE_OPERAND (arg, 0);
2315 if (VAR_OR_FUNCTION_DECL_P (arg))
2317 if (! TREE_PUBLIC (arg))
2318 vis = VISIBILITY_ANON;
2319 else
2320 vis = DECL_VISIBILITY (arg);
2323 if (vis)
2324 constrain_visibility (decl, vis, true);
2328 /* Like c_determine_visibility, but with additional C++-specific
2329 behavior.
2331 Function-scope entities can rely on the function's visibility because
2332 it is set in start_preparsed_function.
2334 Class-scope entities cannot rely on the class's visibility until the end
2335 of the enclosing class definition.
2337 Note that because namespaces have multiple independent definitions,
2338 namespace visibility is handled elsewhere using the #pragma visibility
2339 machinery rather than by decorating the namespace declaration.
2341 The goal is for constraints from the type to give a diagnostic, and
2342 other constraints to be applied silently. */
2344 void
2345 determine_visibility (tree decl)
2347 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2349 /* Only relevant for names with external linkage. */
2350 if (!TREE_PUBLIC (decl))
2351 return;
2353 /* Cloned constructors and destructors get the same visibility as
2354 the underlying function. That should be set up in
2355 maybe_clone_body. */
2356 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2358 bool orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2359 enum symbol_visibility orig_visibility = DECL_VISIBILITY (decl);
2361 /* The decl may be a template instantiation, which could influence
2362 visibilty. */
2363 tree template_decl = NULL_TREE;
2364 if (TREE_CODE (decl) == TYPE_DECL)
2366 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2368 if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
2369 template_decl = decl;
2371 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2372 template_decl = decl;
2374 else if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
2375 template_decl = decl;
2377 /* If DECL is a member of a class, visibility specifiers on the
2378 class can influence the visibility of the DECL. */
2379 tree class_type = NULL_TREE;
2380 if (DECL_CLASS_SCOPE_P (decl))
2381 class_type = DECL_CONTEXT (decl);
2382 else
2384 /* Not a class member. */
2386 /* Virtual tables have DECL_CONTEXT set to their associated class,
2387 so they are automatically handled above. */
2388 gcc_assert (!VAR_P (decl)
2389 || !DECL_VTABLE_OR_VTT_P (decl));
2391 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2393 /* Local statics and classes get the visibility of their
2394 containing function by default, except that
2395 -fvisibility-inlines-hidden doesn't affect them. */
2396 tree fn = DECL_CONTEXT (decl);
2397 if (DECL_VISIBILITY_SPECIFIED (fn))
2399 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2400 DECL_VISIBILITY_SPECIFIED (decl) =
2401 DECL_VISIBILITY_SPECIFIED (fn);
2403 else
2405 if (DECL_CLASS_SCOPE_P (fn))
2406 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2407 else if (determine_hidden_inline (fn))
2409 DECL_VISIBILITY (decl) = default_visibility;
2410 DECL_VISIBILITY_SPECIFIED (decl) =
2411 visibility_options.inpragma;
2413 else
2415 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2416 DECL_VISIBILITY_SPECIFIED (decl) =
2417 DECL_VISIBILITY_SPECIFIED (fn);
2421 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2422 but have no TEMPLATE_INFO, so don't try to check it. */
2423 template_decl = NULL_TREE;
2425 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2426 && flag_visibility_ms_compat)
2428 /* Under -fvisibility-ms-compat, types are visible by default,
2429 even though their contents aren't. */
2430 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2431 int underlying_vis = type_visibility (underlying_type);
2432 if (underlying_vis == VISIBILITY_ANON
2433 || (CLASS_TYPE_P (underlying_type)
2434 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2435 constrain_visibility (decl, underlying_vis, false);
2436 else
2437 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2439 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2441 /* tinfo visibility is based on the type it's for. */
2442 constrain_visibility
2443 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2445 /* Give the target a chance to override the visibility associated
2446 with DECL. */
2447 if (TREE_PUBLIC (decl)
2448 && !DECL_REALLY_EXTERN (decl)
2449 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2450 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2451 targetm.cxx.determine_class_data_visibility (decl);
2453 else if (template_decl)
2454 /* Template instantiations and specializations get visibility based
2455 on their template unless they override it with an attribute. */;
2456 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2458 if (determine_hidden_inline (decl))
2459 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2460 else
2462 /* Set default visibility to whatever the user supplied with
2463 #pragma GCC visibility or a namespace visibility attribute. */
2464 DECL_VISIBILITY (decl) = default_visibility;
2465 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2470 if (template_decl)
2472 /* If the specialization doesn't specify visibility, use the
2473 visibility from the template. */
2474 tree tinfo = get_template_info (template_decl);
2475 tree args = TI_ARGS (tinfo);
2476 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2477 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2478 : DECL_ATTRIBUTES (decl));
2480 if (args != error_mark_node)
2482 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2484 if (!DECL_VISIBILITY_SPECIFIED (decl))
2486 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2487 && determine_hidden_inline (decl))
2488 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2489 else
2491 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2492 DECL_VISIBILITY_SPECIFIED (decl)
2493 = DECL_VISIBILITY_SPECIFIED (pattern);
2497 if (args
2498 /* Template argument visibility outweighs #pragma or namespace
2499 visibility, but not an explicit attribute. */
2500 && !lookup_attribute ("visibility", attribs))
2502 int depth = TMPL_ARGS_DEPTH (args);
2503 if (DECL_VISIBILITY_SPECIFIED (decl))
2505 /* A class template member with explicit visibility
2506 overrides the class visibility, so we need to apply
2507 all the levels of template args directly. */
2508 int i;
2509 for (i = 1; i <= depth; ++i)
2511 tree lev = TMPL_ARGS_LEVEL (args, i);
2512 constrain_visibility_for_template (decl, lev);
2515 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2516 /* Limit visibility based on its template arguments. */
2517 constrain_visibility_for_template (decl, args);
2522 if (class_type)
2523 determine_visibility_from_class (decl, class_type);
2525 if (decl_anon_ns_mem_p (decl))
2526 /* Names in an anonymous namespace get internal linkage.
2527 This might change once we implement export. */
2528 constrain_visibility (decl, VISIBILITY_ANON, false);
2529 else if (TREE_CODE (decl) != TYPE_DECL)
2531 /* Propagate anonymity from type to decl. */
2532 int tvis = type_visibility (TREE_TYPE (decl));
2533 if (tvis == VISIBILITY_ANON
2534 || ! DECL_VISIBILITY_SPECIFIED (decl))
2535 constrain_visibility (decl, tvis, false);
2537 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2538 /* DR 757: A type without linkage shall not be used as the type of a
2539 variable or function with linkage, unless
2540 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2541 o the variable or function is not used (3.2 [basic.def.odr]) or is
2542 defined in the same translation unit.
2544 Since non-extern "C" decls need to be defined in the same
2545 translation unit, we can make the type internal. */
2546 constrain_visibility (decl, VISIBILITY_ANON, false);
2548 /* If visibility changed and DECL already has DECL_RTL, ensure
2549 symbol flags are updated. */
2550 if ((DECL_VISIBILITY (decl) != orig_visibility
2551 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2552 && ((VAR_P (decl) && TREE_STATIC (decl))
2553 || TREE_CODE (decl) == FUNCTION_DECL)
2554 && DECL_RTL_SET_P (decl))
2555 make_decl_rtl (decl);
2558 /* By default, static data members and function members receive
2559 the visibility of their containing class. */
2561 static void
2562 determine_visibility_from_class (tree decl, tree class_type)
2564 if (DECL_VISIBILITY_SPECIFIED (decl))
2565 return;
2567 if (determine_hidden_inline (decl))
2568 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2569 else
2571 /* Default to the class visibility. */
2572 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2573 DECL_VISIBILITY_SPECIFIED (decl)
2574 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2577 /* Give the target a chance to override the visibility associated
2578 with DECL. */
2579 if (VAR_P (decl)
2580 && (DECL_TINFO_P (decl)
2581 || (DECL_VTABLE_OR_VTT_P (decl)
2582 /* Construction virtual tables are not exported because
2583 they cannot be referred to from other object files;
2584 their name is not standardized by the ABI. */
2585 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2586 && TREE_PUBLIC (decl)
2587 && !DECL_REALLY_EXTERN (decl)
2588 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2589 targetm.cxx.determine_class_data_visibility (decl);
2592 /* Returns true iff DECL is an inline that should get hidden visibility
2593 because of -fvisibility-inlines-hidden. */
2595 static bool
2596 determine_hidden_inline (tree decl)
2598 return (visibility_options.inlines_hidden
2599 /* Don't do this for inline templates; specializations might not be
2600 inline, and we don't want them to inherit the hidden
2601 visibility. We'll set it here for all inline instantiations. */
2602 && !processing_template_decl
2603 && TREE_CODE (decl) == FUNCTION_DECL
2604 && DECL_DECLARED_INLINE_P (decl)
2605 && (! DECL_LANG_SPECIFIC (decl)
2606 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2609 /* Constrain the visibility of a class TYPE based on the visibility of its
2610 field types. Warn if any fields require lesser visibility. */
2612 void
2613 constrain_class_visibility (tree type)
2615 tree binfo;
2616 tree t;
2617 int i;
2619 int vis = type_visibility (type);
2621 if (vis == VISIBILITY_ANON
2622 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2623 return;
2625 /* Don't warn about visibility if the class has explicit visibility. */
2626 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2627 vis = VISIBILITY_INTERNAL;
2629 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2630 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node
2631 && !DECL_ARTIFICIAL (t))
2633 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2634 int subvis = type_visibility (ftype);
2636 if (subvis == VISIBILITY_ANON)
2638 if (!in_main_input_context())
2640 tree nlt = no_linkage_check (ftype, /*relaxed_p=*/false);
2641 if (nlt)
2643 if (same_type_p (TREE_TYPE (t), nlt))
2644 warning (OPT_Wsubobject_linkage, "\
2645 %qT has a field %qD whose type has no linkage",
2646 type, t);
2647 else
2648 warning (OPT_Wsubobject_linkage, "\
2649 %qT has a field %qD whose type depends on the type %qT which has no linkage",
2650 type, t, nlt);
2652 else
2653 warning (OPT_Wsubobject_linkage, "\
2654 %qT has a field %qD whose type uses the anonymous namespace",
2655 type, t);
2658 else if (MAYBE_CLASS_TYPE_P (ftype)
2659 && vis < VISIBILITY_HIDDEN
2660 && subvis >= VISIBILITY_HIDDEN)
2661 warning (OPT_Wattributes, "\
2662 %qT declared with greater visibility than the type of its field %qD",
2663 type, t);
2666 binfo = TYPE_BINFO (type);
2667 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2669 int subvis = type_visibility (TREE_TYPE (t));
2671 if (subvis == VISIBILITY_ANON)
2673 if (!in_main_input_context())
2675 tree nlt = no_linkage_check (TREE_TYPE (t), /*relaxed_p=*/false);
2676 if (nlt)
2678 if (same_type_p (TREE_TYPE (t), nlt))
2679 warning (OPT_Wsubobject_linkage, "\
2680 %qT has a base %qT whose type has no linkage",
2681 type, TREE_TYPE (t));
2682 else
2683 warning (OPT_Wsubobject_linkage, "\
2684 %qT has a base %qT whose type depends on the type %qT which has no linkage",
2685 type, TREE_TYPE (t), nlt);
2687 else
2688 warning (OPT_Wsubobject_linkage, "\
2689 %qT has a base %qT whose type uses the anonymous namespace",
2690 type, TREE_TYPE (t));
2693 else if (vis < VISIBILITY_HIDDEN
2694 && subvis >= VISIBILITY_HIDDEN)
2695 warning (OPT_Wattributes, "\
2696 %qT declared with greater visibility than its base %qT",
2697 type, TREE_TYPE (t));
2701 /* Functions for adjusting the visibility of a tagged type and its nested
2702 types and declarations when it gets a name for linkage purposes from a
2703 typedef. */
2705 static void bt_reset_linkage_1 (binding_entry, void *);
2706 static void bt_reset_linkage_2 (binding_entry, void *);
2708 /* First reset the visibility of all the types. */
2710 static void
2711 reset_type_linkage_1 (tree type)
2713 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2714 if (CLASS_TYPE_P (type))
2715 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2716 bt_reset_linkage_1, NULL);
2718 static void
2719 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2721 reset_type_linkage_1 (b->type);
2724 /* Then reset the visibility of any static data members or member
2725 functions that use those types. */
2727 static void
2728 reset_decl_linkage (tree decl)
2730 if (TREE_PUBLIC (decl))
2731 return;
2732 if (DECL_CLONED_FUNCTION_P (decl))
2733 return;
2734 TREE_PUBLIC (decl) = true;
2735 DECL_INTERFACE_KNOWN (decl) = false;
2736 determine_visibility (decl);
2737 tentative_decl_linkage (decl);
2740 static void
2741 reset_type_linkage_2 (tree type)
2743 if (CLASS_TYPE_P (type))
2745 if (tree vt = CLASSTYPE_VTABLES (type))
2747 tree name = mangle_vtbl_for_type (type);
2748 DECL_NAME (vt) = name;
2749 SET_DECL_ASSEMBLER_NAME (vt, name);
2750 reset_decl_linkage (vt);
2752 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2754 tree name = mangle_typeinfo_for_type (type);
2755 DECL_NAME (ti) = name;
2756 SET_DECL_ASSEMBLER_NAME (ti, name);
2757 TREE_TYPE (name) = type;
2758 reset_decl_linkage (ti);
2760 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2762 tree mem = STRIP_TEMPLATE (m);
2763 if (TREE_CODE (mem) == VAR_DECL || TREE_CODE (mem) == FUNCTION_DECL)
2764 reset_decl_linkage (mem);
2766 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2767 bt_reset_linkage_2, NULL);
2771 static void
2772 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2774 reset_type_linkage_2 (b->type);
2776 void
2777 reset_type_linkage (tree type)
2779 reset_type_linkage_1 (type);
2780 reset_type_linkage_2 (type);
2783 /* Set up our initial idea of what the linkage of DECL should be. */
2785 void
2786 tentative_decl_linkage (tree decl)
2788 if (DECL_INTERFACE_KNOWN (decl))
2789 /* We've already made a decision as to how this function will
2790 be handled. */;
2791 else if (vague_linkage_p (decl))
2793 if (TREE_CODE (decl) == FUNCTION_DECL
2794 && decl_defined_p (decl))
2796 DECL_EXTERNAL (decl) = 1;
2797 DECL_NOT_REALLY_EXTERN (decl) = 1;
2798 note_vague_linkage_fn (decl);
2799 /* A non-template inline function with external linkage will
2800 always be COMDAT. As we must eventually determine the
2801 linkage of all functions, and as that causes writes to
2802 the data mapped in from the PCH file, it's advantageous
2803 to mark the functions at this point. */
2804 if (DECL_DECLARED_INLINE_P (decl)
2805 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2806 || DECL_DEFAULTED_FN (decl)))
2808 /* This function must have external linkage, as
2809 otherwise DECL_INTERFACE_KNOWN would have been
2810 set. */
2811 gcc_assert (TREE_PUBLIC (decl));
2812 comdat_linkage (decl);
2813 DECL_INTERFACE_KNOWN (decl) = 1;
2816 else if (VAR_P (decl))
2817 maybe_commonize_var (decl);
2821 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2822 for DECL has not already been determined, do so now by setting
2823 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2824 function is called entities with vague linkage whose definitions
2825 are available must have TREE_PUBLIC set.
2827 If this function decides to place DECL in COMDAT, it will set
2828 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2829 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2830 callers defer that decision until it is clear that DECL is actually
2831 required. */
2833 void
2834 import_export_decl (tree decl)
2836 int emit_p;
2837 bool comdat_p;
2838 bool import_p;
2839 tree class_type = NULL_TREE;
2841 if (DECL_INTERFACE_KNOWN (decl))
2842 return;
2844 /* We cannot determine what linkage to give to an entity with vague
2845 linkage until the end of the file. For example, a virtual table
2846 for a class will be defined if and only if the key method is
2847 defined in this translation unit. As a further example, consider
2848 that when compiling a translation unit that uses PCH file with
2849 "-frepo" it would be incorrect to make decisions about what
2850 entities to emit when building the PCH; those decisions must be
2851 delayed until the repository information has been processed. */
2852 gcc_assert (at_eof);
2853 /* Object file linkage for explicit instantiations is handled in
2854 mark_decl_instantiated. For static variables in functions with
2855 vague linkage, maybe_commonize_var is used.
2857 Therefore, the only declarations that should be provided to this
2858 function are those with external linkage that are:
2860 * implicit instantiations of function templates
2862 * inline function
2864 * implicit instantiations of static data members of class
2865 templates
2867 * virtual tables
2869 * typeinfo objects
2871 Furthermore, all entities that reach this point must have a
2872 definition available in this translation unit.
2874 The following assertions check these conditions. */
2875 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2876 /* Any code that creates entities with TREE_PUBLIC cleared should
2877 also set DECL_INTERFACE_KNOWN. */
2878 gcc_assert (TREE_PUBLIC (decl));
2879 if (TREE_CODE (decl) == FUNCTION_DECL)
2880 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2881 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2882 || DECL_DECLARED_INLINE_P (decl));
2883 else
2884 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2885 || DECL_VTABLE_OR_VTT_P (decl)
2886 || DECL_TINFO_P (decl));
2887 /* Check that a definition of DECL is available in this translation
2888 unit. */
2889 gcc_assert (!DECL_REALLY_EXTERN (decl));
2891 /* Assume that DECL will not have COMDAT linkage. */
2892 comdat_p = false;
2893 /* Assume that DECL will not be imported into this translation
2894 unit. */
2895 import_p = false;
2897 /* See if the repository tells us whether or not to emit DECL in
2898 this translation unit. */
2899 emit_p = repo_emit_p (decl);
2900 if (emit_p == 0)
2901 import_p = true;
2902 else if (emit_p == 1)
2904 /* The repository indicates that this entity should be defined
2905 here. Make sure the back end honors that request. */
2906 mark_needed (decl);
2907 /* Output the definition as an ordinary strong definition. */
2908 DECL_EXTERNAL (decl) = 0;
2909 DECL_INTERFACE_KNOWN (decl) = 1;
2910 return;
2913 if (import_p)
2914 /* We have already decided what to do with this DECL; there is no
2915 need to check anything further. */
2917 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2919 class_type = DECL_CONTEXT (decl);
2920 import_export_class (class_type);
2921 if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2922 && CLASSTYPE_INTERFACE_ONLY (class_type))
2923 import_p = true;
2924 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2925 && !CLASSTYPE_USE_TEMPLATE (class_type)
2926 && CLASSTYPE_KEY_METHOD (class_type)
2927 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2928 /* The ABI requires that all virtual tables be emitted with
2929 COMDAT linkage. However, on systems where COMDAT symbols
2930 don't show up in the table of contents for a static
2931 archive, or on systems without weak symbols (where we
2932 approximate COMDAT linkage by using internal linkage), the
2933 linker will report errors about undefined symbols because
2934 it will not see the virtual table definition. Therefore,
2935 in the case that we know that the virtual table will be
2936 emitted in only one translation unit, we make the virtual
2937 table an ordinary definition with external linkage. */
2938 DECL_EXTERNAL (decl) = 0;
2939 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2941 /* CLASS_TYPE is being exported from this translation unit,
2942 so DECL should be defined here. */
2943 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2944 /* If a class is declared in a header with the "extern
2945 template" extension, then it will not be instantiated,
2946 even in translation units that would normally require
2947 it. Often such classes are explicitly instantiated in
2948 one translation unit. Therefore, the explicit
2949 instantiation must be made visible to other translation
2950 units. */
2951 DECL_EXTERNAL (decl) = 0;
2952 else
2954 /* The generic C++ ABI says that class data is always
2955 COMDAT, even if there is a key function. Some
2956 variants (e.g., the ARM EABI) says that class data
2957 only has COMDAT linkage if the class data might be
2958 emitted in more than one translation unit. When the
2959 key method can be inline and is inline, we still have
2960 to arrange for comdat even though
2961 class_data_always_comdat is false. */
2962 if (!CLASSTYPE_KEY_METHOD (class_type)
2963 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2964 || targetm.cxx.class_data_always_comdat ())
2966 /* The ABI requires COMDAT linkage. Normally, we
2967 only emit COMDAT things when they are needed;
2968 make sure that we realize that this entity is
2969 indeed needed. */
2970 comdat_p = true;
2971 mark_needed (decl);
2975 else if (!flag_implicit_templates
2976 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2977 import_p = true;
2978 else
2979 comdat_p = true;
2981 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2983 tree type = TREE_TYPE (DECL_NAME (decl));
2984 if (CLASS_TYPE_P (type))
2986 class_type = type;
2987 import_export_class (type);
2988 if (CLASSTYPE_INTERFACE_KNOWN (type)
2989 && TYPE_POLYMORPHIC_P (type)
2990 && CLASSTYPE_INTERFACE_ONLY (type)
2991 /* If -fno-rtti was specified, then we cannot be sure
2992 that RTTI information will be emitted with the
2993 virtual table of the class, so we must emit it
2994 wherever it is used. */
2995 && flag_rtti)
2996 import_p = true;
2997 else
2999 if (CLASSTYPE_INTERFACE_KNOWN (type)
3000 && !CLASSTYPE_INTERFACE_ONLY (type))
3002 comdat_p = (targetm.cxx.class_data_always_comdat ()
3003 || (CLASSTYPE_KEY_METHOD (type)
3004 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
3005 mark_needed (decl);
3006 if (!flag_weak)
3008 comdat_p = false;
3009 DECL_EXTERNAL (decl) = 0;
3012 else
3013 comdat_p = true;
3016 else
3017 comdat_p = true;
3019 else if (DECL_TEMPLOID_INSTANTIATION (decl))
3021 /* DECL is an implicit instantiation of a function or static
3022 data member. */
3023 if ((flag_implicit_templates
3024 && !flag_use_repository)
3025 || (flag_implicit_inline_templates
3026 && TREE_CODE (decl) == FUNCTION_DECL
3027 && DECL_DECLARED_INLINE_P (decl)))
3028 comdat_p = true;
3029 else
3030 /* If we are not implicitly generating templates, then mark
3031 this entity as undefined in this translation unit. */
3032 import_p = true;
3034 else if (DECL_FUNCTION_MEMBER_P (decl))
3036 if (!DECL_DECLARED_INLINE_P (decl))
3038 tree ctype = DECL_CONTEXT (decl);
3039 import_export_class (ctype);
3040 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
3042 DECL_NOT_REALLY_EXTERN (decl)
3043 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
3044 || (DECL_DECLARED_INLINE_P (decl)
3045 && ! flag_implement_inlines
3046 && !DECL_VINDEX (decl)));
3048 if (!DECL_NOT_REALLY_EXTERN (decl))
3049 DECL_EXTERNAL (decl) = 1;
3051 /* Always make artificials weak. */
3052 if (DECL_ARTIFICIAL (decl) && flag_weak)
3053 comdat_p = true;
3054 else
3055 maybe_make_one_only (decl);
3058 else
3059 comdat_p = true;
3061 else
3062 comdat_p = true;
3064 if (import_p)
3066 /* If we are importing DECL into this translation unit, mark is
3067 an undefined here. */
3068 DECL_EXTERNAL (decl) = 1;
3069 DECL_NOT_REALLY_EXTERN (decl) = 0;
3071 else if (comdat_p)
3073 /* If we decided to put DECL in COMDAT, mark it accordingly at
3074 this point. */
3075 comdat_linkage (decl);
3078 DECL_INTERFACE_KNOWN (decl) = 1;
3081 /* Return an expression that performs the destruction of DECL, which
3082 must be a VAR_DECL whose type has a non-trivial destructor, or is
3083 an array whose (innermost) elements have a non-trivial destructor. */
3085 tree
3086 build_cleanup (tree decl)
3088 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
3089 gcc_assert (clean != NULL_TREE);
3090 return clean;
3093 /* Returns the initialization guard variable for the variable DECL,
3094 which has static storage duration. */
3096 tree
3097 get_guard (tree decl)
3099 tree sname;
3100 tree guard;
3102 sname = mangle_guard_variable (decl);
3103 guard = get_global_binding (sname);
3104 if (! guard)
3106 tree guard_type;
3108 /* We use a type that is big enough to contain a mutex as well
3109 as an integer counter. */
3110 guard_type = targetm.cxx.guard_type ();
3111 guard = build_decl (DECL_SOURCE_LOCATION (decl),
3112 VAR_DECL, sname, guard_type);
3114 /* The guard should have the same linkage as what it guards. */
3115 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
3116 TREE_STATIC (guard) = TREE_STATIC (decl);
3117 DECL_COMMON (guard) = DECL_COMMON (decl);
3118 DECL_COMDAT (guard) = DECL_COMDAT (decl);
3119 CP_DECL_THREAD_LOCAL_P (guard) = CP_DECL_THREAD_LOCAL_P (decl);
3120 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
3121 if (DECL_ONE_ONLY (decl))
3122 make_decl_one_only (guard, cxx_comdat_group (guard));
3123 if (TREE_PUBLIC (decl))
3124 DECL_WEAK (guard) = DECL_WEAK (decl);
3125 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
3126 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
3128 DECL_ARTIFICIAL (guard) = 1;
3129 DECL_IGNORED_P (guard) = 1;
3130 TREE_USED (guard) = 1;
3131 pushdecl_top_level_and_finish (guard, NULL_TREE);
3133 return guard;
3136 /* Return an atomic load of src with the appropriate memory model. */
3138 static tree
3139 build_atomic_load_byte (tree src, HOST_WIDE_INT model)
3141 tree ptr_type = build_pointer_type (char_type_node);
3142 tree mem_model = build_int_cst (integer_type_node, model);
3143 tree t, addr, val;
3144 unsigned int size;
3145 int fncode;
3147 size = tree_to_uhwi (TYPE_SIZE_UNIT (char_type_node));
3149 fncode = BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1;
3150 t = builtin_decl_implicit ((enum built_in_function) fncode);
3152 addr = build1 (ADDR_EXPR, ptr_type, src);
3153 val = build_call_expr (t, 2, addr, mem_model);
3154 return val;
3157 /* Return those bits of the GUARD variable that should be set when the
3158 guarded entity is actually initialized. */
3160 static tree
3161 get_guard_bits (tree guard)
3163 if (!targetm.cxx.guard_mask_bit ())
3165 /* We only set the first byte of the guard, in order to leave room
3166 for a mutex in the high-order bits. */
3167 guard = build1 (ADDR_EXPR,
3168 build_pointer_type (TREE_TYPE (guard)),
3169 guard);
3170 guard = build1 (NOP_EXPR,
3171 build_pointer_type (char_type_node),
3172 guard);
3173 guard = build1 (INDIRECT_REF, char_type_node, guard);
3176 return guard;
3179 /* Return an expression which determines whether or not the GUARD
3180 variable has already been initialized. */
3182 tree
3183 get_guard_cond (tree guard, bool thread_safe)
3185 tree guard_value;
3187 if (!thread_safe)
3188 guard = get_guard_bits (guard);
3189 else
3190 guard = build_atomic_load_byte (guard, MEMMODEL_ACQUIRE);
3192 /* Mask off all but the low bit. */
3193 if (targetm.cxx.guard_mask_bit ())
3195 guard_value = integer_one_node;
3196 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3197 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3198 guard = cp_build_binary_op (input_location,
3199 BIT_AND_EXPR, guard, guard_value,
3200 tf_warning_or_error);
3203 guard_value = integer_zero_node;
3204 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3205 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3206 return cp_build_binary_op (input_location,
3207 EQ_EXPR, guard, guard_value,
3208 tf_warning_or_error);
3211 /* Return an expression which sets the GUARD variable, indicating that
3212 the variable being guarded has been initialized. */
3214 tree
3215 set_guard (tree guard)
3217 tree guard_init;
3219 /* Set the GUARD to one. */
3220 guard = get_guard_bits (guard);
3221 guard_init = integer_one_node;
3222 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3223 guard_init = fold_convert (TREE_TYPE (guard), guard_init);
3224 return cp_build_modify_expr (input_location, guard, NOP_EXPR, guard_init,
3225 tf_warning_or_error);
3228 /* Returns true iff we can tell that VAR does not have a dynamic
3229 initializer. */
3231 static bool
3232 var_defined_without_dynamic_init (tree var)
3234 /* If it's defined in another TU, we can't tell. */
3235 if (DECL_EXTERNAL (var))
3236 return false;
3237 /* If it has a non-trivial destructor, registering the destructor
3238 counts as dynamic initialization. */
3239 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3240 return false;
3241 /* If it's in this TU, its initializer has been processed, unless
3242 it's a case of self-initialization, then DECL_INITIALIZED_P is
3243 false while the initializer is handled by finish_id_expression. */
3244 if (!DECL_INITIALIZED_P (var))
3245 return false;
3246 /* If it has no initializer or a constant one, it's not dynamic. */
3247 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3248 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3251 /* Returns true iff VAR is a variable that needs uses to be
3252 wrapped for possible dynamic initialization. */
3254 static bool
3255 var_needs_tls_wrapper (tree var)
3257 return (!error_operand_p (var)
3258 && CP_DECL_THREAD_LOCAL_P (var)
3259 && !DECL_GNU_TLS_P (var)
3260 && !DECL_FUNCTION_SCOPE_P (var)
3261 && !var_defined_without_dynamic_init (var));
3264 /* Get the FUNCTION_DECL for the shared TLS init function for this
3265 translation unit. */
3267 static tree
3268 get_local_tls_init_fn (void)
3270 tree sname = get_identifier ("__tls_init");
3271 tree fn = get_global_binding (sname);
3272 if (!fn)
3274 fn = build_lang_decl (FUNCTION_DECL, sname,
3275 build_function_type (void_type_node,
3276 void_list_node));
3277 SET_DECL_LANGUAGE (fn, lang_c);
3278 TREE_PUBLIC (fn) = false;
3279 DECL_ARTIFICIAL (fn) = true;
3280 mark_used (fn);
3281 set_global_binding (fn);
3283 return fn;
3286 /* Get a FUNCTION_DECL for the init function for the thread_local
3287 variable VAR. The init function will be an alias to the function
3288 that initializes all the non-local TLS variables in the translation
3289 unit. The init function is only used by the wrapper function. */
3291 static tree
3292 get_tls_init_fn (tree var)
3294 /* Only C++11 TLS vars need this init fn. */
3295 if (!var_needs_tls_wrapper (var))
3296 return NULL_TREE;
3298 /* If -fno-extern-tls-init, assume that we don't need to call
3299 a tls init function for a variable defined in another TU. */
3300 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3301 return NULL_TREE;
3303 /* If the variable is internal, or if we can't generate aliases,
3304 call the local init function directly. */
3305 if (!TREE_PUBLIC (var) || !TARGET_SUPPORTS_ALIASES)
3306 return get_local_tls_init_fn ();
3308 tree sname = mangle_tls_init_fn (var);
3309 tree fn = get_global_binding (sname);
3310 if (!fn)
3312 fn = build_lang_decl (FUNCTION_DECL, sname,
3313 build_function_type (void_type_node,
3314 void_list_node));
3315 SET_DECL_LANGUAGE (fn, lang_c);
3316 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3317 DECL_ARTIFICIAL (fn) = true;
3318 DECL_COMDAT (fn) = DECL_COMDAT (var);
3319 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3320 if (DECL_ONE_ONLY (var))
3321 make_decl_one_only (fn, cxx_comdat_group (fn));
3322 if (TREE_PUBLIC (var))
3324 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3325 /* If the variable is defined somewhere else and might have static
3326 initialization, make the init function a weak reference. */
3327 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3328 || TYPE_HAS_CONSTEXPR_CTOR (obtype)
3329 || TYPE_HAS_TRIVIAL_DFLT (obtype))
3330 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3331 && DECL_EXTERNAL (var))
3332 declare_weak (fn);
3333 else
3334 DECL_WEAK (fn) = DECL_WEAK (var);
3336 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3337 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3338 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3339 DECL_IGNORED_P (fn) = 1;
3340 mark_used (fn);
3342 DECL_BEFRIENDING_CLASSES (fn) = var;
3344 set_global_binding (fn);
3346 return fn;
3349 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3350 variable VAR. The wrapper function calls the init function (if any) for
3351 VAR and then returns a reference to VAR. The wrapper function is used
3352 in place of VAR everywhere VAR is mentioned. */
3354 tree
3355 get_tls_wrapper_fn (tree var)
3357 /* Only C++11 TLS vars need this wrapper fn. */
3358 if (!var_needs_tls_wrapper (var))
3359 return NULL_TREE;
3361 tree sname = mangle_tls_wrapper_fn (var);
3362 tree fn = get_global_binding (sname);
3363 if (!fn)
3365 /* A named rvalue reference is an lvalue, so the wrapper should
3366 always return an lvalue reference. */
3367 tree type = non_reference (TREE_TYPE (var));
3368 type = build_reference_type (type);
3369 tree fntype = build_function_type (type, void_list_node);
3370 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3371 SET_DECL_LANGUAGE (fn, lang_c);
3372 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3373 DECL_ARTIFICIAL (fn) = true;
3374 DECL_IGNORED_P (fn) = 1;
3375 /* The wrapper is inline and emitted everywhere var is used. */
3376 DECL_DECLARED_INLINE_P (fn) = true;
3377 if (TREE_PUBLIC (var))
3379 comdat_linkage (fn);
3380 #ifdef HAVE_GAS_HIDDEN
3381 /* Make the wrapper bind locally; there's no reason to share
3382 the wrapper between multiple shared objects. */
3383 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3384 DECL_VISIBILITY_SPECIFIED (fn) = true;
3385 #endif
3387 if (!TREE_PUBLIC (fn))
3388 DECL_INTERFACE_KNOWN (fn) = true;
3389 mark_used (fn);
3390 note_vague_linkage_fn (fn);
3392 #if 0
3393 /* We want CSE to commonize calls to the wrapper, but marking it as
3394 pure is unsafe since it has side-effects. I guess we need a new
3395 ECF flag even weaker than ECF_PURE. FIXME! */
3396 DECL_PURE_P (fn) = true;
3397 #endif
3399 DECL_BEFRIENDING_CLASSES (fn) = var;
3401 set_global_binding (fn);
3403 return fn;
3406 /* At EOF, generate the definition for the TLS wrapper function FN:
3408 T& var_wrapper() {
3409 if (init_fn) init_fn();
3410 return var;
3411 } */
3413 static void
3414 generate_tls_wrapper (tree fn)
3416 tree var = DECL_BEFRIENDING_CLASSES (fn);
3418 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3419 tree body = begin_function_body ();
3420 /* Only call the init fn if there might be one. */
3421 if (tree init_fn = get_tls_init_fn (var))
3423 tree if_stmt = NULL_TREE;
3424 /* If init_fn is a weakref, make sure it exists before calling. */
3425 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3427 if_stmt = begin_if_stmt ();
3428 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3429 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3430 NE_EXPR, addr, nullptr_node,
3431 tf_warning_or_error);
3432 finish_if_stmt_cond (cond, if_stmt);
3434 finish_expr_stmt (build_cxx_call
3435 (init_fn, 0, NULL, tf_warning_or_error));
3436 if (if_stmt)
3438 finish_then_clause (if_stmt);
3439 finish_if_stmt (if_stmt);
3442 else
3443 /* If there's no initialization, the wrapper is a constant function. */
3444 TREE_READONLY (fn) = true;
3445 finish_return_stmt (convert_from_reference (var));
3446 finish_function_body (body);
3447 expand_or_defer_fn (finish_function (/*inline_p=*/false));
3450 /* Start the process of running a particular set of global constructors
3451 or destructors. Subroutine of do_[cd]tors. Also called from
3452 vtv_start_verification_constructor_init_function. */
3454 static tree
3455 start_objects (int method_type, int initp)
3457 tree body;
3458 tree fndecl;
3459 char type[14];
3461 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3463 if (initp != DEFAULT_INIT_PRIORITY)
3465 char joiner;
3467 #ifdef JOINER
3468 joiner = JOINER;
3469 #else
3470 joiner = '_';
3471 #endif
3473 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3475 else
3476 sprintf (type, "sub_%c", method_type);
3478 fndecl = build_lang_decl (FUNCTION_DECL,
3479 get_file_function_name (type),
3480 build_function_type_list (void_type_node,
3481 NULL_TREE));
3482 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3484 TREE_PUBLIC (current_function_decl) = 0;
3486 /* Mark as artificial because it's not explicitly in the user's
3487 source code. */
3488 DECL_ARTIFICIAL (current_function_decl) = 1;
3490 /* Mark this declaration as used to avoid spurious warnings. */
3491 TREE_USED (current_function_decl) = 1;
3493 /* Mark this function as a global constructor or destructor. */
3494 if (method_type == 'I')
3495 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3496 else
3497 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3499 body = begin_compound_stmt (BCS_FN_BODY);
3501 return body;
3504 /* Finish the process of running a particular set of global constructors
3505 or destructors. Subroutine of do_[cd]tors. */
3507 static void
3508 finish_objects (int method_type, int initp, tree body)
3510 tree fn;
3512 /* Finish up. */
3513 finish_compound_stmt (body);
3514 fn = finish_function (/*inline_p=*/false);
3516 if (method_type == 'I')
3518 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3519 decl_init_priority_insert (fn, initp);
3521 else
3523 DECL_STATIC_DESTRUCTOR (fn) = 1;
3524 decl_fini_priority_insert (fn, initp);
3527 expand_or_defer_fn (fn);
3530 /* The names of the parameters to the function created to handle
3531 initializations and destructions for objects with static storage
3532 duration. */
3533 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3534 #define PRIORITY_IDENTIFIER "__priority"
3536 /* The name of the function we create to handle initializations and
3537 destructions for objects with static storage duration. */
3538 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3540 /* The declaration for the __INITIALIZE_P argument. */
3541 static GTY(()) tree initialize_p_decl;
3543 /* The declaration for the __PRIORITY argument. */
3544 static GTY(()) tree priority_decl;
3546 /* The declaration for the static storage duration function. */
3547 static GTY(()) tree ssdf_decl;
3549 /* All the static storage duration functions created in this
3550 translation unit. */
3551 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3553 /* A map from priority levels to information about that priority
3554 level. There may be many such levels, so efficient lookup is
3555 important. */
3556 static splay_tree priority_info_map;
3558 /* Begins the generation of the function that will handle all
3559 initialization and destruction of objects with static storage
3560 duration. The function generated takes two parameters of type
3561 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3562 nonzero, it performs initializations. Otherwise, it performs
3563 destructions. It only performs those initializations or
3564 destructions with the indicated __PRIORITY. The generated function
3565 returns no value.
3567 It is assumed that this function will only be called once per
3568 translation unit. */
3570 static tree
3571 start_static_storage_duration_function (unsigned count)
3573 tree type;
3574 tree body;
3575 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3577 /* Create the identifier for this function. It will be of the form
3578 SSDF_IDENTIFIER_<number>. */
3579 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3581 type = build_function_type_list (void_type_node,
3582 integer_type_node, integer_type_node,
3583 NULL_TREE);
3585 /* Create the FUNCTION_DECL itself. */
3586 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3587 get_identifier (id),
3588 type);
3589 TREE_PUBLIC (ssdf_decl) = 0;
3590 DECL_ARTIFICIAL (ssdf_decl) = 1;
3592 /* Put this function in the list of functions to be called from the
3593 static constructors and destructors. */
3594 if (!ssdf_decls)
3596 vec_alloc (ssdf_decls, 32);
3598 /* Take this opportunity to initialize the map from priority
3599 numbers to information about that priority level. */
3600 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3601 /*delete_key_fn=*/0,
3602 /*delete_value_fn=*/
3603 splay_tree_delete_pointers);
3605 /* We always need to generate functions for the
3606 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3607 priorities later, we'll be sure to find the
3608 DEFAULT_INIT_PRIORITY. */
3609 get_priority_info (DEFAULT_INIT_PRIORITY);
3612 vec_safe_push (ssdf_decls, ssdf_decl);
3614 /* Create the argument list. */
3615 initialize_p_decl = cp_build_parm_decl
3616 (ssdf_decl, get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3617 TREE_USED (initialize_p_decl) = 1;
3618 priority_decl = cp_build_parm_decl
3619 (ssdf_decl, get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3620 TREE_USED (priority_decl) = 1;
3622 DECL_CHAIN (initialize_p_decl) = priority_decl;
3623 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3625 /* Put the function in the global scope. */
3626 pushdecl (ssdf_decl);
3628 /* Start the function itself. This is equivalent to declaring the
3629 function as:
3631 static void __ssdf (int __initialize_p, init __priority_p);
3633 It is static because we only need to call this function from the
3634 various constructor and destructor functions for this module. */
3635 start_preparsed_function (ssdf_decl,
3636 /*attrs=*/NULL_TREE,
3637 SF_PRE_PARSED);
3639 /* Set up the scope of the outermost block in the function. */
3640 body = begin_compound_stmt (BCS_FN_BODY);
3642 return body;
3645 /* Finish the generation of the function which performs initialization
3646 and destruction of objects with static storage duration. After
3647 this point, no more such objects can be created. */
3649 static void
3650 finish_static_storage_duration_function (tree body)
3652 /* Close out the function. */
3653 finish_compound_stmt (body);
3654 expand_or_defer_fn (finish_function (/*inline_p=*/false));
3657 /* Return the information about the indicated PRIORITY level. If no
3658 code to handle this level has yet been generated, generate the
3659 appropriate prologue. */
3661 static priority_info
3662 get_priority_info (int priority)
3664 priority_info pi;
3665 splay_tree_node n;
3667 n = splay_tree_lookup (priority_info_map,
3668 (splay_tree_key) priority);
3669 if (!n)
3671 /* Create a new priority information structure, and insert it
3672 into the map. */
3673 pi = XNEW (struct priority_info_s);
3674 pi->initializations_p = 0;
3675 pi->destructions_p = 0;
3676 splay_tree_insert (priority_info_map,
3677 (splay_tree_key) priority,
3678 (splay_tree_value) pi);
3680 else
3681 pi = (priority_info) n->value;
3683 return pi;
3686 /* The effective initialization priority of a DECL. */
3688 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3689 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3690 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3692 /* Whether a DECL needs a guard to protect it against multiple
3693 initialization. */
3695 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3696 || DECL_ONE_ONLY (decl) \
3697 || DECL_WEAK (decl)))
3699 /* Called from one_static_initialization_or_destruction(),
3700 via walk_tree.
3701 Walks the initializer list of a global variable and looks for
3702 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3703 and that have their DECL_CONTEXT() == NULL.
3704 For each such temporary variable, set their DECL_CONTEXT() to
3705 the current function. This is necessary because otherwise
3706 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3707 when trying to refer to a temporary variable that does not have
3708 it's DECL_CONTECT() properly set. */
3709 static tree
3710 fix_temporary_vars_context_r (tree *node,
3711 int * /*unused*/,
3712 void * /*unused1*/)
3714 gcc_assert (current_function_decl);
3716 if (TREE_CODE (*node) == BIND_EXPR)
3718 tree var;
3720 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3721 if (VAR_P (var)
3722 && !DECL_NAME (var)
3723 && DECL_ARTIFICIAL (var)
3724 && !DECL_CONTEXT (var))
3725 DECL_CONTEXT (var) = current_function_decl;
3728 return NULL_TREE;
3731 /* Set up to handle the initialization or destruction of DECL. If
3732 INITP is nonzero, we are initializing the variable. Otherwise, we
3733 are destroying it. */
3735 static void
3736 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3738 tree guard_if_stmt = NULL_TREE;
3739 tree guard;
3741 /* If we are supposed to destruct and there's a trivial destructor,
3742 nothing has to be done. */
3743 if (!initp
3744 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3745 return;
3747 /* Trick the compiler into thinking we are at the file and line
3748 where DECL was declared so that error-messages make sense, and so
3749 that the debugger will show somewhat sensible file and line
3750 information. */
3751 input_location = DECL_SOURCE_LOCATION (decl);
3753 /* Make sure temporary variables in the initialiser all have
3754 their DECL_CONTEXT() set to a value different from NULL_TREE.
3755 This can happen when global variables initializers are built.
3756 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3757 the temporary variables that might have been generated in the
3758 accompanying initializers is NULL_TREE, meaning the variables have been
3759 declared in the global namespace.
3760 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3761 of the temporaries are set to the current function decl. */
3762 cp_walk_tree_without_duplicates (&init,
3763 fix_temporary_vars_context_r,
3764 NULL);
3766 /* Because of:
3768 [class.access.spec]
3770 Access control for implicit calls to the constructors,
3771 the conversion functions, or the destructor called to
3772 create and destroy a static data member is performed as
3773 if these calls appeared in the scope of the member's
3774 class.
3776 we pretend we are in a static member function of the class of
3777 which the DECL is a member. */
3778 if (member_p (decl))
3780 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3781 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3784 /* Assume we don't need a guard. */
3785 guard = NULL_TREE;
3786 /* We need a guard if this is an object with external linkage that
3787 might be initialized in more than one place. (For example, a
3788 static data member of a template, when the data member requires
3789 construction.) */
3790 if (NEEDS_GUARD_P (decl))
3792 tree guard_cond;
3794 guard = get_guard (decl);
3796 /* When using __cxa_atexit, we just check the GUARD as we would
3797 for a local static. */
3798 if (flag_use_cxa_atexit)
3800 /* When using __cxa_atexit, we never try to destroy
3801 anything from a static destructor. */
3802 gcc_assert (initp);
3803 guard_cond = get_guard_cond (guard, false);
3805 /* If we don't have __cxa_atexit, then we will be running
3806 destructors from .fini sections, or their equivalents. So,
3807 we need to know how many times we've tried to initialize this
3808 object. We do initializations only if the GUARD is zero,
3809 i.e., if we are the first to initialize the variable. We do
3810 destructions only if the GUARD is one, i.e., if we are the
3811 last to destroy the variable. */
3812 else if (initp)
3813 guard_cond
3814 = cp_build_binary_op (input_location,
3815 EQ_EXPR,
3816 cp_build_unary_op (PREINCREMENT_EXPR,
3817 guard,
3818 /*noconvert=*/true,
3819 tf_warning_or_error),
3820 integer_one_node,
3821 tf_warning_or_error);
3822 else
3823 guard_cond
3824 = cp_build_binary_op (input_location,
3825 EQ_EXPR,
3826 cp_build_unary_op (PREDECREMENT_EXPR,
3827 guard,
3828 /*noconvert=*/true,
3829 tf_warning_or_error),
3830 integer_zero_node,
3831 tf_warning_or_error);
3833 guard_if_stmt = begin_if_stmt ();
3834 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3838 /* If we're using __cxa_atexit, we have not already set the GUARD,
3839 so we must do so now. */
3840 if (guard && initp && flag_use_cxa_atexit)
3841 finish_expr_stmt (set_guard (guard));
3843 /* Perform the initialization or destruction. */
3844 if (initp)
3846 if (init)
3848 finish_expr_stmt (init);
3849 if (sanitize_flags_p (SANITIZE_ADDRESS, decl))
3851 varpool_node *vnode = varpool_node::get (decl);
3852 if (vnode)
3853 vnode->dynamically_initialized = 1;
3857 /* If we're using __cxa_atexit, register a function that calls the
3858 destructor for the object. */
3859 if (flag_use_cxa_atexit)
3860 finish_expr_stmt (register_dtor_fn (decl));
3862 else
3863 finish_expr_stmt (build_cleanup (decl));
3865 /* Finish the guard if-stmt, if necessary. */
3866 if (guard)
3868 finish_then_clause (guard_if_stmt);
3869 finish_if_stmt (guard_if_stmt);
3872 /* Now that we're done with DECL we don't need to pretend to be a
3873 member of its class any longer. */
3874 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3875 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3878 /* Generate code to do the initialization or destruction of the decls in VARS,
3879 a TREE_LIST of VAR_DECL with static storage duration.
3880 Whether initialization or destruction is performed is specified by INITP. */
3882 static void
3883 do_static_initialization_or_destruction (tree vars, bool initp)
3885 tree node, init_if_stmt, cond;
3887 /* Build the outer if-stmt to check for initialization or destruction. */
3888 init_if_stmt = begin_if_stmt ();
3889 cond = initp ? integer_one_node : integer_zero_node;
3890 cond = cp_build_binary_op (input_location,
3891 EQ_EXPR,
3892 initialize_p_decl,
3893 cond,
3894 tf_warning_or_error);
3895 finish_if_stmt_cond (cond, init_if_stmt);
3897 /* To make sure dynamic construction doesn't access globals from other
3898 compilation units where they might not be yet constructed, for
3899 -fsanitize=address insert __asan_before_dynamic_init call that
3900 prevents access to either all global variables that need construction
3901 in other compilation units, or at least those that haven't been
3902 initialized yet. Variables that need dynamic construction in
3903 the current compilation unit are kept accessible. */
3904 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
3905 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3907 node = vars;
3908 do {
3909 tree decl = TREE_VALUE (node);
3910 tree priority_if_stmt;
3911 int priority;
3912 priority_info pi;
3914 /* If we don't need a destructor, there's nothing to do. Avoid
3915 creating a possibly empty if-stmt. */
3916 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3918 node = TREE_CHAIN (node);
3919 continue;
3922 /* Remember that we had an initialization or finalization at this
3923 priority. */
3924 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3925 pi = get_priority_info (priority);
3926 if (initp)
3927 pi->initializations_p = 1;
3928 else
3929 pi->destructions_p = 1;
3931 /* Conditionalize this initialization on being in the right priority
3932 and being initializing/finalizing appropriately. */
3933 priority_if_stmt = begin_if_stmt ();
3934 cond = cp_build_binary_op (input_location,
3935 EQ_EXPR,
3936 priority_decl,
3937 build_int_cst (NULL_TREE, priority),
3938 tf_warning_or_error);
3939 finish_if_stmt_cond (cond, priority_if_stmt);
3941 /* Process initializers with same priority. */
3942 for (; node
3943 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3944 node = TREE_CHAIN (node))
3945 /* Do one initialization or destruction. */
3946 one_static_initialization_or_destruction (TREE_VALUE (node),
3947 TREE_PURPOSE (node), initp);
3949 /* Finish up the priority if-stmt body. */
3950 finish_then_clause (priority_if_stmt);
3951 finish_if_stmt (priority_if_stmt);
3953 } while (node);
3955 /* Revert what __asan_before_dynamic_init did by calling
3956 __asan_after_dynamic_init. */
3957 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
3958 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3960 /* Finish up the init/destruct if-stmt body. */
3961 finish_then_clause (init_if_stmt);
3962 finish_if_stmt (init_if_stmt);
3965 /* VARS is a list of variables with static storage duration which may
3966 need initialization and/or finalization. Remove those variables
3967 that don't really need to be initialized or finalized, and return
3968 the resulting list. The order in which the variables appear in
3969 VARS is in reverse order of the order in which they should actually
3970 be initialized. The list we return is in the unreversed order;
3971 i.e., the first variable should be initialized first. */
3973 static tree
3974 prune_vars_needing_no_initialization (tree *vars)
3976 tree *var = vars;
3977 tree result = NULL_TREE;
3979 while (*var)
3981 tree t = *var;
3982 tree decl = TREE_VALUE (t);
3983 tree init = TREE_PURPOSE (t);
3985 /* Deal gracefully with error. */
3986 if (error_operand_p (decl))
3988 var = &TREE_CHAIN (t);
3989 continue;
3992 /* The only things that can be initialized are variables. */
3993 gcc_assert (VAR_P (decl));
3995 /* If this object is not defined, we don't need to do anything
3996 here. */
3997 if (DECL_EXTERNAL (decl))
3999 var = &TREE_CHAIN (t);
4000 continue;
4003 /* Also, if the initializer already contains errors, we can bail
4004 out now. */
4005 if (init && TREE_CODE (init) == TREE_LIST
4006 && value_member (error_mark_node, init))
4008 var = &TREE_CHAIN (t);
4009 continue;
4012 /* This variable is going to need initialization and/or
4013 finalization, so we add it to the list. */
4014 *var = TREE_CHAIN (t);
4015 TREE_CHAIN (t) = result;
4016 result = t;
4019 return result;
4022 /* Make sure we have told the back end about all the variables in
4023 VARS. */
4025 static void
4026 write_out_vars (tree vars)
4028 tree v;
4030 for (v = vars; v; v = TREE_CHAIN (v))
4032 tree var = TREE_VALUE (v);
4033 if (!var_finalized_p (var))
4035 import_export_decl (var);
4036 rest_of_decl_compilation (var, 1, 1);
4041 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
4042 (otherwise) that will initialize all global objects with static
4043 storage duration having the indicated PRIORITY. */
4045 static void
4046 generate_ctor_or_dtor_function (bool constructor_p, int priority,
4047 location_t *locus)
4049 char function_key;
4050 tree fndecl;
4051 tree body;
4052 size_t i;
4054 input_location = *locus;
4055 /* ??? */
4056 /* Was: locus->line++; */
4058 /* We use `I' to indicate initialization and `D' to indicate
4059 destruction. */
4060 function_key = constructor_p ? 'I' : 'D';
4062 /* We emit the function lazily, to avoid generating empty
4063 global constructors and destructors. */
4064 body = NULL_TREE;
4066 /* For Objective-C++, we may need to initialize metadata found in this module.
4067 This must be done _before_ any other static initializations. */
4068 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
4069 && constructor_p && objc_static_init_needed_p ())
4071 body = start_objects (function_key, priority);
4072 objc_generate_static_init_call (NULL_TREE);
4075 /* Call the static storage duration function with appropriate
4076 arguments. */
4077 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
4079 /* Calls to pure or const functions will expand to nothing. */
4080 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
4082 tree call;
4084 if (! body)
4085 body = start_objects (function_key, priority);
4087 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
4088 build_int_cst (NULL_TREE,
4089 constructor_p),
4090 build_int_cst (NULL_TREE,
4091 priority),
4092 NULL_TREE);
4093 finish_expr_stmt (call);
4097 /* Close out the function. */
4098 if (body)
4099 finish_objects (function_key, priority, body);
4102 /* Generate constructor and destructor functions for the priority
4103 indicated by N. */
4105 static int
4106 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
4108 location_t *locus = (location_t *) data;
4109 int priority = (int) n->key;
4110 priority_info pi = (priority_info) n->value;
4112 /* Generate the functions themselves, but only if they are really
4113 needed. */
4114 if (pi->initializations_p)
4115 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
4116 if (pi->destructions_p)
4117 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
4119 /* Keep iterating. */
4120 return 0;
4123 /* Return C++ property of T, based on given operation OP. */
4125 static int
4126 cpp_check (tree t, cpp_operation op)
4128 switch (op)
4130 case HAS_DEPENDENT_TEMPLATE_ARGS:
4132 tree ti = CLASSTYPE_TEMPLATE_INFO (t);
4133 if (!ti)
4134 return 0;
4135 ++processing_template_decl;
4136 const bool dep = any_dependent_template_arguments_p (TI_ARGS (ti));
4137 --processing_template_decl;
4138 return dep;
4140 case IS_ABSTRACT:
4141 return DECL_PURE_VIRTUAL_P (t);
4142 case IS_CONSTRUCTOR:
4143 return DECL_CONSTRUCTOR_P (t);
4144 case IS_DESTRUCTOR:
4145 return DECL_DESTRUCTOR_P (t);
4146 case IS_COPY_CONSTRUCTOR:
4147 return DECL_COPY_CONSTRUCTOR_P (t);
4148 case IS_MOVE_CONSTRUCTOR:
4149 return DECL_MOVE_CONSTRUCTOR_P (t);
4150 case IS_TEMPLATE:
4151 return TREE_CODE (t) == TEMPLATE_DECL;
4152 case IS_TRIVIAL:
4153 return trivial_type_p (t);
4154 default:
4155 return 0;
4159 /* Collect source file references recursively, starting from NAMESPC. */
4161 static void
4162 collect_source_refs (tree namespc)
4164 /* Iterate over names in this name space. */
4165 for (tree t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4166 if (DECL_IS_BUILTIN (t))
4168 else if (TREE_CODE (t) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (t))
4169 collect_source_refs (t);
4170 else
4171 collect_source_ref (DECL_SOURCE_FILE (t));
4174 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4175 starting from NAMESPC. */
4177 static void
4178 collect_ada_namespace (tree namespc, const char *source_file)
4180 tree decl = NAMESPACE_LEVEL (namespc)->names;
4182 /* Collect decls from this namespace. This will skip
4183 NAMESPACE_DECLs (both aliases and regular, it cannot tell). */
4184 collect_ada_nodes (decl, source_file);
4186 /* Now scan for namespace children, and dump them. */
4187 for (; decl; decl = TREE_CHAIN (decl))
4188 if (TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl))
4189 collect_ada_namespace (decl, source_file);
4192 /* Returns true iff there is a definition available for variable or
4193 function DECL. */
4195 bool
4196 decl_defined_p (tree decl)
4198 if (TREE_CODE (decl) == FUNCTION_DECL)
4199 return (DECL_INITIAL (decl) != NULL_TREE
4200 /* A pending instantiation of a friend temploid is defined. */
4201 || (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
4202 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4203 (DECL_TI_TEMPLATE (decl)))));
4204 else
4206 gcc_assert (VAR_P (decl));
4207 return !DECL_EXTERNAL (decl);
4211 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4213 [expr.const]
4215 An integral constant-expression can only involve ... const
4216 variables of integral or enumeration types initialized with
4217 constant expressions ...
4219 C++0x also allows constexpr variables and temporaries initialized
4220 with constant expressions. We handle the former here, but the latter
4221 are just folded away in cxx_eval_constant_expression.
4223 The standard does not require that the expression be non-volatile.
4224 G++ implements the proposed correction in DR 457. */
4226 bool
4227 decl_constant_var_p (tree decl)
4229 if (!decl_maybe_constant_var_p (decl))
4230 return false;
4232 /* We don't know if a template static data member is initialized with
4233 a constant expression until we instantiate its initializer. Even
4234 in the case of a constexpr variable, we can't treat it as a
4235 constant until its initializer is complete in case it's used in
4236 its own initializer. */
4237 maybe_instantiate_decl (decl);
4238 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4241 /* Returns true if DECL could be a symbolic constant variable, depending on
4242 its initializer. */
4244 bool
4245 decl_maybe_constant_var_p (tree decl)
4247 tree type = TREE_TYPE (decl);
4248 if (!VAR_P (decl))
4249 return false;
4250 if (DECL_DECLARED_CONSTEXPR_P (decl))
4251 return true;
4252 if (DECL_HAS_VALUE_EXPR_P (decl))
4253 /* A proxy isn't constant. */
4254 return false;
4255 if (TYPE_REF_P (type))
4256 /* References can be constant. */;
4257 else if (CP_TYPE_CONST_NON_VOLATILE_P (type)
4258 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4259 /* And const integers. */;
4260 else
4261 return false;
4263 if (DECL_INITIAL (decl)
4264 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
4265 /* We know the initializer, and it isn't constant. */
4266 return false;
4267 else
4268 return true;
4271 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4272 called from grokfndecl and grokvardecl; in all modes it is called from
4273 cp_write_global_declarations. */
4275 void
4276 no_linkage_error (tree decl)
4278 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4279 /* In C++11 it's ok if the decl is defined. */
4280 return;
4281 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4282 if (t == NULL_TREE)
4283 /* The type that got us on no_linkage_decls must have gotten a name for
4284 linkage purposes. */;
4285 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4286 /* The type might end up having a typedef name for linkage purposes. */
4287 vec_safe_push (no_linkage_decls, decl);
4288 else if (TYPE_UNNAMED_P (t))
4290 bool d = false;
4291 auto_diagnostic_group grp;
4292 if (cxx_dialect >= cxx11)
4293 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4294 "unnamed type, is used but never defined", decl);
4295 else if (DECL_EXTERN_C_P (decl))
4296 /* Allow this; it's pretty common in C. */;
4297 else if (VAR_P (decl))
4298 /* DRs 132, 319 and 389 seem to indicate types with
4299 no linkage can only be used to declare extern "C"
4300 entities. Since it's not always an error in the
4301 ISO C++ 90 Standard, we only issue a warning. */
4302 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "unnamed type "
4303 "with no linkage used to declare variable %q#D with "
4304 "linkage", decl);
4305 else
4306 d = permerror (DECL_SOURCE_LOCATION (decl), "unnamed type with no "
4307 "linkage used to declare function %q#D with linkage",
4308 decl);
4309 if (d && is_typedef_decl (TYPE_NAME (t)))
4310 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4311 "to the unqualified type, so it is not used for linkage",
4312 TYPE_NAME (t));
4314 else if (cxx_dialect >= cxx11)
4316 if (VAR_P (decl) || !DECL_PURE_VIRTUAL_P (decl))
4317 permerror (DECL_SOURCE_LOCATION (decl),
4318 "%q#D, declared using local type "
4319 "%qT, is used but never defined", decl, t);
4321 else if (VAR_P (decl))
4322 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4323 "used to declare variable %q#D with linkage", t, decl);
4324 else
4325 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4326 "to declare function %q#D with linkage", t, decl);
4329 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4331 static void
4332 collect_all_refs (const char *source_file)
4334 collect_ada_namespace (global_namespace, source_file);
4337 /* Clear DECL_EXTERNAL for NODE. */
4339 static bool
4340 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4342 DECL_EXTERNAL (node->decl) = 0;
4343 return false;
4346 /* Build up the function to run dynamic initializers for thread_local
4347 variables in this translation unit and alias the init functions for the
4348 individual variables to it. */
4350 static void
4351 handle_tls_init (void)
4353 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4354 if (vars == NULL_TREE)
4355 return;
4357 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4359 write_out_vars (vars);
4361 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4362 boolean_type_node);
4363 TREE_PUBLIC (guard) = false;
4364 TREE_STATIC (guard) = true;
4365 DECL_ARTIFICIAL (guard) = true;
4366 DECL_IGNORED_P (guard) = true;
4367 TREE_USED (guard) = true;
4368 CP_DECL_THREAD_LOCAL_P (guard) = true;
4369 set_decl_tls_model (guard, decl_default_tls_model (guard));
4370 pushdecl_top_level_and_finish (guard, NULL_TREE);
4372 tree fn = get_local_tls_init_fn ();
4373 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4374 tree body = begin_function_body ();
4375 tree if_stmt = begin_if_stmt ();
4376 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4377 tf_warning_or_error);
4378 finish_if_stmt_cond (cond, if_stmt);
4379 finish_expr_stmt (cp_build_modify_expr (loc, guard, NOP_EXPR,
4380 boolean_true_node,
4381 tf_warning_or_error));
4382 for (; vars; vars = TREE_CHAIN (vars))
4384 tree var = TREE_VALUE (vars);
4385 tree init = TREE_PURPOSE (vars);
4386 one_static_initialization_or_destruction (var, init, true);
4388 /* Output init aliases even with -fno-extern-tls-init. */
4389 if (TARGET_SUPPORTS_ALIASES && TREE_PUBLIC (var))
4391 tree single_init_fn = get_tls_init_fn (var);
4392 if (single_init_fn == NULL_TREE)
4393 continue;
4394 cgraph_node *alias
4395 = cgraph_node::get_create (fn)->create_same_body_alias
4396 (single_init_fn, fn);
4397 gcc_assert (alias != NULL);
4401 finish_then_clause (if_stmt);
4402 finish_if_stmt (if_stmt);
4403 finish_function_body (body);
4404 expand_or_defer_fn (finish_function (/*inline_p=*/false));
4407 /* We're at the end of compilation, so generate any mangling aliases that
4408 we've been saving up, if DECL is going to be output and ID2 isn't
4409 already taken by another declaration. */
4411 static void
4412 generate_mangling_alias (tree decl, tree id2)
4414 struct cgraph_node *n = NULL;
4416 if (TREE_CODE (decl) == FUNCTION_DECL)
4418 n = cgraph_node::get (decl);
4419 if (!n)
4420 /* Don't create an alias to an unreferenced function. */
4421 return;
4424 tree *slot
4425 = mangled_decls->find_slot_with_hash (id2, IDENTIFIER_HASH_VALUE (id2),
4426 INSERT);
4428 /* If there's a declaration already using this mangled name,
4429 don't create a compatibility alias that conflicts. */
4430 if (*slot)
4431 return;
4433 tree alias = make_alias_for (decl, id2);
4434 *slot = alias;
4436 DECL_IGNORED_P (alias) = 1;
4437 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
4438 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
4439 if (vague_linkage_p (decl))
4440 DECL_WEAK (alias) = 1;
4442 if (n)
4443 n->create_same_body_alias (alias, decl);
4444 else
4445 varpool_node::create_extra_name_alias (alias, decl);
4448 /* Note that we might want to emit an alias with the symbol ID2 for DECL at
4449 the end of translation, for compatibility across bugs in the mangling
4450 implementation. */
4452 void
4453 note_mangling_alias (tree decl, tree id2)
4455 if (TARGET_SUPPORTS_ALIASES)
4457 if (!defer_mangling_aliases)
4458 generate_mangling_alias (decl, id2);
4459 else
4461 vec_safe_push (mangling_aliases, decl);
4462 vec_safe_push (mangling_aliases, id2);
4467 /* Emit all mangling aliases that were deferred up to this point. */
4469 void
4470 generate_mangling_aliases ()
4472 while (!vec_safe_is_empty (mangling_aliases))
4474 tree id2 = mangling_aliases->pop();
4475 tree decl = mangling_aliases->pop();
4476 generate_mangling_alias (decl, id2);
4478 defer_mangling_aliases = false;
4481 /* Record a mangling of DECL, whose DECL_ASSEMBLER_NAME has just been
4482 set. NEED_WARNING is true if we must warn about collisions. We do
4483 this to spot changes in mangling that may require compatibility
4484 aliases. */
4486 void
4487 record_mangling (tree decl, bool need_warning)
4489 if (!mangled_decls)
4490 mangled_decls = hash_table<mangled_decl_hash>::create_ggc (499);
4492 gcc_checking_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
4493 tree id = DECL_ASSEMBLER_NAME_RAW (decl);
4494 tree *slot
4495 = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id),
4496 INSERT);
4498 /* If this is already an alias, remove the alias, because the real
4499 decl takes precedence. */
4500 if (*slot && DECL_ARTIFICIAL (*slot) && DECL_IGNORED_P (*slot))
4501 if (symtab_node *n = symtab_node::get (*slot))
4502 if (n->cpp_implicit_alias)
4504 n->remove ();
4505 *slot = NULL_TREE;
4508 if (!*slot)
4509 *slot = decl;
4510 else if (need_warning)
4512 error_at (DECL_SOURCE_LOCATION (decl),
4513 "mangling of %q#D as %qE conflicts with a previous mangle",
4514 decl, id);
4515 inform (DECL_SOURCE_LOCATION (*slot),
4516 "previous mangling %q#D", *slot);
4517 inform (DECL_SOURCE_LOCATION (decl),
4518 "a later -fabi-version= (or =0)"
4519 " avoids this error with a change in mangling");
4520 *slot = decl;
4524 /* The mangled name of DECL is being forcibly changed to NAME. Remove
4525 any existing knowledge of DECL's mangled name meaning DECL. */
4527 void
4528 overwrite_mangling (tree decl, tree name)
4530 if (tree id = DECL_ASSEMBLER_NAME_RAW (decl))
4531 if ((TREE_CODE (decl) == VAR_DECL
4532 || TREE_CODE (decl) == FUNCTION_DECL)
4533 && mangled_decls)
4534 if (tree *slot
4535 = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id),
4536 NO_INSERT))
4537 if (*slot == decl)
4539 mangled_decls->clear_slot (slot);
4541 /* If this is an alias, remove it from the symbol table. */
4542 if (DECL_ARTIFICIAL (decl) && DECL_IGNORED_P (decl))
4543 if (symtab_node *n = symtab_node::get (decl))
4544 if (n->cpp_implicit_alias)
4545 n->remove ();
4548 DECL_ASSEMBLER_NAME_RAW (decl) = name;
4551 /* The entire file is now complete. If requested, dump everything
4552 to a file. */
4554 static void
4555 dump_tu (void)
4557 dump_flags_t flags;
4558 if (FILE *stream = dump_begin (raw_dump_id, &flags))
4560 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4561 dump_end (raw_dump_id, stream);
4565 static location_t locus_at_end_of_parsing;
4567 /* Check the deallocation functions for CODE to see if we want to warn that
4568 only one was defined. */
4570 static void
4571 maybe_warn_sized_delete (enum tree_code code)
4573 tree sized = NULL_TREE;
4574 tree unsized = NULL_TREE;
4576 for (ovl_iterator iter (get_global_binding (ovl_op_identifier (false, code)));
4577 iter; ++iter)
4579 tree fn = *iter;
4580 /* We're only interested in usual deallocation functions. */
4581 if (!usual_deallocation_fn_p (fn))
4582 continue;
4583 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
4584 unsized = fn;
4585 else
4586 sized = fn;
4588 if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
4589 warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
4590 "the program should also define %qD", sized);
4591 else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
4592 warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
4593 "the program should also define %qD", unsized);
4596 /* Check the global deallocation functions to see if we want to warn about
4597 defining unsized without sized (or vice versa). */
4599 static void
4600 maybe_warn_sized_delete ()
4602 if (!flag_sized_deallocation || !warn_sized_deallocation)
4603 return;
4604 maybe_warn_sized_delete (DELETE_EXPR);
4605 maybe_warn_sized_delete (VEC_DELETE_EXPR);
4608 /* Earlier we left PTRMEM_CST in variable initializers alone so that we could
4609 look them up when evaluating non-type template parameters. Now we need to
4610 lower them to something the back end can understand. */
4612 static void
4613 lower_var_init ()
4615 varpool_node *node;
4616 FOR_EACH_VARIABLE (node)
4618 tree d = node->decl;
4619 if (tree init = DECL_INITIAL (d))
4620 DECL_INITIAL (d) = cplus_expand_constant (init);
4624 /* This routine is called at the end of compilation.
4625 Its job is to create all the code needed to initialize and
4626 destroy the global aggregates. We do the destruction
4627 first, since that way we only need to reverse the decls once. */
4629 void
4630 c_parse_final_cleanups (void)
4632 tree vars;
4633 bool reconsider;
4634 size_t i;
4635 unsigned ssdf_count = 0;
4636 int retries = 0;
4637 tree decl;
4639 locus_at_end_of_parsing = input_location;
4640 at_eof = 1;
4642 /* Bad parse errors. Just forget about it. */
4643 if (! global_bindings_p () || current_class_type
4644 || !vec_safe_is_empty (decl_namespace_list))
4645 return;
4647 /* This is the point to write out a PCH if we're doing that.
4648 In that case we do not want to do anything else. */
4649 if (pch_file)
4651 /* Mangle all symbols at PCH creation time. */
4652 symtab_node *node;
4653 FOR_EACH_SYMBOL (node)
4654 if (! is_a <varpool_node *> (node)
4655 || ! DECL_HARD_REGISTER (node->decl))
4656 DECL_ASSEMBLER_NAME (node->decl);
4657 c_common_write_pch ();
4658 dump_tu ();
4659 /* Ensure even the callers don't try to finalize the CU. */
4660 flag_syntax_only = 1;
4661 return;
4664 timevar_stop (TV_PHASE_PARSING);
4665 timevar_start (TV_PHASE_DEFERRED);
4667 symtab->process_same_body_aliases ();
4669 /* Handle -fdump-ada-spec[-slim] */
4670 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4672 if (flag_dump_ada_spec_slim)
4673 collect_source_ref (main_input_filename);
4674 else
4675 collect_source_refs (global_namespace);
4677 dump_ada_specs (collect_all_refs, cpp_check);
4680 /* FIXME - huh? was input_line -= 1;*/
4682 /* We now have to write out all the stuff we put off writing out.
4683 These include:
4685 o Template specializations that we have not yet instantiated,
4686 but which are needed.
4687 o Initialization and destruction for non-local objects with
4688 static storage duration. (Local objects with static storage
4689 duration are initialized when their scope is first entered,
4690 and are cleaned up via atexit.)
4691 o Virtual function tables.
4693 All of these may cause others to be needed. For example,
4694 instantiating one function may cause another to be needed, and
4695 generating the initializer for an object may cause templates to be
4696 instantiated, etc., etc. */
4698 emit_support_tinfos ();
4702 tree t;
4703 tree decl;
4705 reconsider = false;
4707 /* If there are templates that we've put off instantiating, do
4708 them now. */
4709 instantiate_pending_templates (retries);
4710 ggc_collect ();
4712 /* Write out virtual tables as required. Writing out the
4713 virtual table for a template class may cause the
4714 instantiation of members of that class. If we write out
4715 vtables then we remove the class from our list so we don't
4716 have to look at it again. */
4717 for (i = keyed_classes->length ();
4718 keyed_classes->iterate (--i, &t);)
4719 if (maybe_emit_vtables (t))
4721 reconsider = true;
4722 keyed_classes->unordered_remove (i);
4724 /* The input_location may have been changed during marking of
4725 vtable entries. */
4726 input_location = locus_at_end_of_parsing;
4728 /* Write out needed type info variables. We have to be careful
4729 looping through unemitted decls, because emit_tinfo_decl may
4730 cause other variables to be needed. New elements will be
4731 appended, and we remove from the vector those that actually
4732 get emitted. */
4733 for (i = unemitted_tinfo_decls->length ();
4734 unemitted_tinfo_decls->iterate (--i, &t);)
4735 if (emit_tinfo_decl (t))
4737 reconsider = true;
4738 unemitted_tinfo_decls->unordered_remove (i);
4741 /* The list of objects with static storage duration is built up
4742 in reverse order. We clear STATIC_AGGREGATES so that any new
4743 aggregates added during the initialization of these will be
4744 initialized in the correct order when we next come around the
4745 loop. */
4746 vars = prune_vars_needing_no_initialization (&static_aggregates);
4748 if (vars)
4750 /* We need to start a new initialization function each time
4751 through the loop. That's because we need to know which
4752 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4753 isn't computed until a function is finished, and written
4754 out. That's a deficiency in the back end. When this is
4755 fixed, these initialization functions could all become
4756 inline, with resulting performance improvements. */
4757 tree ssdf_body;
4759 /* Make sure the back end knows about all the variables. */
4760 write_out_vars (vars);
4762 /* Set the line and file, so that it is obviously not from
4763 the source file. */
4764 input_location = locus_at_end_of_parsing;
4765 ssdf_body = start_static_storage_duration_function (ssdf_count);
4767 /* First generate code to do all the initializations. */
4768 if (vars)
4769 do_static_initialization_or_destruction (vars, /*initp=*/true);
4771 /* Then, generate code to do all the destructions. Do these
4772 in reverse order so that the most recently constructed
4773 variable is the first destroyed. If we're using
4774 __cxa_atexit, then we don't need to do this; functions
4775 were registered at initialization time to destroy the
4776 local statics. */
4777 if (!flag_use_cxa_atexit && vars)
4779 vars = nreverse (vars);
4780 do_static_initialization_or_destruction (vars, /*initp=*/false);
4782 else
4783 vars = NULL_TREE;
4785 /* Finish up the static storage duration function for this
4786 round. */
4787 input_location = locus_at_end_of_parsing;
4788 finish_static_storage_duration_function (ssdf_body);
4790 /* All those initializations and finalizations might cause
4791 us to need more inline functions, more template
4792 instantiations, etc. */
4793 reconsider = true;
4794 ssdf_count++;
4795 /* ??? was: locus_at_end_of_parsing.line++; */
4798 /* Now do the same for thread_local variables. */
4799 handle_tls_init ();
4801 /* Go through the set of inline functions whose bodies have not
4802 been emitted yet. If out-of-line copies of these functions
4803 are required, emit them. */
4804 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4806 /* Does it need synthesizing? */
4807 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4808 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4810 /* Even though we're already at the top-level, we push
4811 there again. That way, when we pop back a few lines
4812 hence, all of our state is restored. Otherwise,
4813 finish_function doesn't clean things up, and we end
4814 up with CURRENT_FUNCTION_DECL set. */
4815 push_to_top_level ();
4816 /* The decl's location will mark where it was first
4817 needed. Save that so synthesize method can indicate
4818 where it was needed from, in case of error */
4819 input_location = DECL_SOURCE_LOCATION (decl);
4820 synthesize_method (decl);
4821 pop_from_top_level ();
4822 reconsider = true;
4825 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4826 generate_tls_wrapper (decl);
4828 if (!DECL_SAVED_TREE (decl))
4829 continue;
4831 cgraph_node *node = cgraph_node::get_create (decl);
4833 /* We lie to the back end, pretending that some functions
4834 are not defined when they really are. This keeps these
4835 functions from being put out unnecessarily. But, we must
4836 stop lying when the functions are referenced, or if they
4837 are not comdat since they need to be put out now. If
4838 DECL_INTERFACE_KNOWN, then we have already set
4839 DECL_EXTERNAL appropriately, so there's no need to check
4840 again, and we do not want to clear DECL_EXTERNAL if a
4841 previous call to import_export_decl set it.
4843 This is done in a separate for cycle, because if some
4844 deferred function is contained in another deferred
4845 function later in deferred_fns varray,
4846 rest_of_compilation would skip this function and we
4847 really cannot expand the same function twice. */
4848 import_export_decl (decl);
4849 if (DECL_NOT_REALLY_EXTERN (decl)
4850 && DECL_INITIAL (decl)
4851 && decl_needed_p (decl))
4853 if (node->cpp_implicit_alias)
4854 node = node->get_alias_target ();
4856 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
4857 NULL, true);
4858 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4859 group, we need to mark all symbols in the same comdat group
4860 that way. */
4861 if (node->same_comdat_group)
4862 for (cgraph_node *next
4863 = dyn_cast<cgraph_node *> (node->same_comdat_group);
4864 next != node;
4865 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
4866 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
4867 NULL, true);
4870 /* If we're going to need to write this function out, and
4871 there's already a body for it, create RTL for it now.
4872 (There might be no body if this is a method we haven't
4873 gotten around to synthesizing yet.) */
4874 if (!DECL_EXTERNAL (decl)
4875 && decl_needed_p (decl)
4876 && !TREE_ASM_WRITTEN (decl)
4877 && !node->definition)
4879 /* We will output the function; no longer consider it in this
4880 loop. */
4881 DECL_DEFER_OUTPUT (decl) = 0;
4882 /* Generate RTL for this function now that we know we
4883 need it. */
4884 expand_or_defer_fn (decl);
4885 /* If we're compiling -fsyntax-only pretend that this
4886 function has been written out so that we don't try to
4887 expand it again. */
4888 if (flag_syntax_only)
4889 TREE_ASM_WRITTEN (decl) = 1;
4890 reconsider = true;
4894 if (wrapup_namespace_globals ())
4895 reconsider = true;
4897 /* Static data members are just like namespace-scope globals. */
4898 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4900 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4901 /* Don't write it out if we haven't seen a definition. */
4902 || (DECL_IN_AGGR_P (decl) && !DECL_INLINE_VAR_P (decl)))
4903 continue;
4904 import_export_decl (decl);
4905 /* If this static data member is needed, provide it to the
4906 back end. */
4907 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4908 DECL_EXTERNAL (decl) = 0;
4910 if (vec_safe_length (pending_statics) != 0
4911 && wrapup_global_declarations (pending_statics->address (),
4912 pending_statics->length ()))
4913 reconsider = true;
4915 retries++;
4917 while (reconsider);
4919 lower_var_init ();
4921 generate_mangling_aliases ();
4923 /* All used inline functions must have a definition at this point. */
4924 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4926 if (/* Check online inline functions that were actually used. */
4927 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4928 /* If the definition actually was available here, then the
4929 fact that the function was not defined merely represents
4930 that for some reason (use of a template repository,
4931 #pragma interface, etc.) we decided not to emit the
4932 definition here. */
4933 && !DECL_INITIAL (decl)
4934 /* Don't complain if the template was defined. */
4935 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4936 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4937 (template_for_substitution (decl)))))
4939 warning_at (DECL_SOURCE_LOCATION (decl), 0,
4940 "inline function %qD used but never defined", decl);
4941 /* Avoid a duplicate warning from check_global_declaration. */
4942 TREE_NO_WARNING (decl) = 1;
4946 /* So must decls that use a type with no linkage. */
4947 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4948 no_linkage_error (decl);
4950 maybe_warn_sized_delete ();
4952 /* Then, do the Objective-C stuff. This is where all the
4953 Objective-C module stuff gets generated (symtab,
4954 class/protocol/selector lists etc). This must be done after C++
4955 templates, destructors etc. so that selectors used in C++
4956 templates are properly allocated. */
4957 if (c_dialect_objc ())
4958 objc_write_global_declarations ();
4960 /* We give C linkage to static constructors and destructors. */
4961 push_lang_context (lang_name_c);
4963 /* Generate initialization and destruction functions for all
4964 priorities for which they are required. */
4965 if (priority_info_map)
4966 splay_tree_foreach (priority_info_map,
4967 generate_ctor_and_dtor_functions_for_priority,
4968 /*data=*/&locus_at_end_of_parsing);
4969 else if (c_dialect_objc () && objc_static_init_needed_p ())
4970 /* If this is obj-c++ and we need a static init, call
4971 generate_ctor_or_dtor_function. */
4972 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4973 DEFAULT_INIT_PRIORITY,
4974 &locus_at_end_of_parsing);
4976 /* We're done with the splay-tree now. */
4977 if (priority_info_map)
4978 splay_tree_delete (priority_info_map);
4980 /* Generate any missing aliases. */
4981 maybe_apply_pending_pragma_weaks ();
4983 /* We're done with static constructors, so we can go back to "C++"
4984 linkage now. */
4985 pop_lang_context ();
4987 if (flag_vtable_verify)
4989 vtv_recover_class_info ();
4990 vtv_compute_class_hierarchy_transitive_closure ();
4991 vtv_build_vtable_verify_fndecl ();
4994 perform_deferred_noexcept_checks ();
4996 finish_repo ();
4997 fini_constexpr ();
4999 /* The entire file is now complete. If requested, dump everything
5000 to a file. */
5001 dump_tu ();
5003 if (flag_detailed_statistics)
5005 dump_tree_statistics ();
5006 dump_time_statistics ();
5009 timevar_stop (TV_PHASE_DEFERRED);
5010 timevar_start (TV_PHASE_PARSING);
5012 /* Indicate that we're done with front end processing. */
5013 at_eof = 2;
5016 /* Perform any post compilation-proper cleanups for the C++ front-end.
5017 This should really go away. No front-end should need to do
5018 anything past the compilation process. */
5020 void
5021 cxx_post_compilation_parsing_cleanups (void)
5023 timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS);
5025 if (flag_vtable_verify)
5027 /* Generate the special constructor initialization function that
5028 calls __VLTRegisterPairs, and give it a very high
5029 initialization priority. This must be done after
5030 finalize_compilation_unit so that we have accurate
5031 information about which vtable will actually be emitted. */
5032 vtv_generate_init_routine ();
5035 input_location = locus_at_end_of_parsing;
5037 if (flag_checking)
5038 validate_conversion_obstack ();
5040 timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS);
5043 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
5044 function to call in parse-tree form; it has not yet been
5045 semantically analyzed. ARGS are the arguments to the function.
5046 They have already been semantically analyzed. This may change
5047 ARGS. */
5049 tree
5050 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
5051 tsubst_flags_t complain)
5053 tree orig_fn;
5054 vec<tree, va_gc> *orig_args = NULL;
5055 tree expr;
5056 tree object;
5058 orig_fn = fn;
5059 object = TREE_OPERAND (fn, 0);
5061 if (processing_template_decl)
5063 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
5064 || TREE_CODE (fn) == MEMBER_REF);
5065 if (type_dependent_expression_p (fn)
5066 || any_type_dependent_arguments_p (*args))
5067 return build_min_nt_call_vec (fn, *args);
5069 orig_args = make_tree_vector_copy (*args);
5071 /* Transform the arguments and add the implicit "this"
5072 parameter. That must be done before the FN is transformed
5073 because we depend on the form of FN. */
5074 make_args_non_dependent (*args);
5075 object = build_non_dependent_expr (object);
5076 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5078 if (TREE_CODE (fn) == DOTSTAR_EXPR)
5079 object = cp_build_addr_expr (object, complain);
5080 vec_safe_insert (*args, 0, object);
5082 /* Now that the arguments are done, transform FN. */
5083 fn = build_non_dependent_expr (fn);
5086 /* A qualified name corresponding to a bound pointer-to-member is
5087 represented as an OFFSET_REF:
5089 struct B { void g(); };
5090 void (B::*p)();
5091 void B::g() { (this->*p)(); } */
5092 if (TREE_CODE (fn) == OFFSET_REF)
5094 tree object_addr = cp_build_addr_expr (object, complain);
5095 fn = TREE_OPERAND (fn, 1);
5096 fn = get_member_function_from_ptrfunc (&object_addr, fn,
5097 complain);
5098 vec_safe_insert (*args, 0, object_addr);
5101 if (CLASS_TYPE_P (TREE_TYPE (fn)))
5102 expr = build_op_call (fn, args, complain);
5103 else
5104 expr = cp_build_function_call_vec (fn, args, complain);
5105 if (processing_template_decl && expr != error_mark_node)
5106 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
5108 if (orig_args != NULL)
5109 release_tree_vector (orig_args);
5111 return expr;
5115 void
5116 check_default_args (tree x)
5118 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
5119 bool saw_def = false;
5120 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
5121 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
5123 if (TREE_PURPOSE (arg))
5124 saw_def = true;
5125 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
5127 error ("default argument missing for parameter %P of %q+#D", i, x);
5128 TREE_PURPOSE (arg) = error_mark_node;
5133 /* Return true if function DECL can be inlined. This is used to force
5134 instantiation of methods that might be interesting for inlining. */
5135 bool
5136 possibly_inlined_p (tree decl)
5138 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
5139 if (DECL_UNINLINABLE (decl))
5140 return false;
5141 if (!optimize)
5142 return DECL_DECLARED_INLINE_P (decl);
5143 /* When optimizing, we might inline everything when flatten
5144 attribute or heuristics inlining for size or autoinlining
5145 is used. */
5146 return true;
5149 /* Normally, we can wait until instantiation-time to synthesize DECL.
5150 However, if DECL is a static data member initialized with a constant
5151 or a constexpr function, we need it right now because a reference to
5152 such a data member or a call to such function is not value-dependent.
5153 For a function that uses auto in the return type, we need to instantiate
5154 it to find out its type. For OpenMP user defined reductions, we need
5155 them instantiated for reduction clauses which inline them by hand
5156 directly. */
5158 static void
5159 maybe_instantiate_decl (tree decl)
5161 if (DECL_LANG_SPECIFIC (decl)
5162 && DECL_TEMPLATE_INFO (decl)
5163 && (decl_maybe_constant_var_p (decl)
5164 || (TREE_CODE (decl) == FUNCTION_DECL
5165 && DECL_OMP_DECLARE_REDUCTION_P (decl))
5166 || undeduced_auto_decl (decl))
5167 && !DECL_DECLARED_CONCEPT_P (decl)
5168 && !uses_template_parms (DECL_TI_ARGS (decl)))
5170 /* Instantiating a function will result in garbage collection. We
5171 must treat this situation as if we were within the body of a
5172 function so as to avoid collecting live data only referenced from
5173 the stack (such as overload resolution candidates). */
5174 ++function_depth;
5175 instantiate_decl (decl, /*defer_ok=*/false,
5176 /*expl_inst_class_mem_p=*/false);
5177 --function_depth;
5181 /* Maybe warn if DECL is deprecated, subject to COMPLAIN. Returns whether or
5182 not a warning was emitted. */
5184 bool
5185 cp_warn_deprecated_use (tree decl, tsubst_flags_t complain)
5187 if (!(complain & tf_warning) || !decl
5188 || deprecated_state == DEPRECATED_SUPPRESS)
5189 return false;
5191 if (!TREE_DEPRECATED (decl))
5193 /* Perhaps this is a deprecated typedef. */
5194 if (TYPE_P (decl) && TYPE_NAME (decl))
5195 decl = TYPE_NAME (decl);
5197 if (!TREE_DEPRECATED (decl))
5198 return false;
5201 /* Don't warn within members of a deprecated type. */
5202 if (TYPE_P (decl)
5203 && currently_open_class (decl))
5204 return false;
5206 bool warned = false;
5207 if (cxx_dialect >= cxx11
5208 && DECL_P (decl)
5209 && DECL_ARTIFICIAL (decl)
5210 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5211 && copy_fn_p (decl))
5213 auto_diagnostic_group d;
5214 /* Don't warn about system library classes (c++/86342). */
5215 if (!DECL_IN_SYSTEM_HEADER (decl))
5216 warned = warning (OPT_Wdeprecated_copy,
5217 "implicitly-declared %qD is deprecated", decl);
5218 if (warned)
5220 tree ctx = DECL_CONTEXT (decl);
5221 tree other = classtype_has_user_copy_or_dtor (ctx);
5222 inform (DECL_SOURCE_LOCATION (other),
5223 "because %qT has user-provided %qD",
5224 ctx, other);
5227 else
5228 warned = warn_deprecated_use (decl, NULL_TREE);
5230 return warned;
5233 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
5234 If DECL is a specialization or implicitly declared class member,
5235 generate the actual definition. Return false if something goes
5236 wrong, true otherwise. */
5238 bool
5239 mark_used (tree decl, tsubst_flags_t complain)
5241 /* If we're just testing conversions or resolving overloads, we
5242 don't want any permanent effects like forcing functions to be
5243 output or instantiating templates. */
5244 if ((complain & tf_conv))
5245 return true;
5247 /* If DECL is a BASELINK for a single function, then treat it just
5248 like the DECL for the function. Otherwise, if the BASELINK is
5249 for an overloaded function, we don't know which function was
5250 actually used until after overload resolution. */
5251 if (BASELINK_P (decl))
5253 decl = BASELINK_FUNCTIONS (decl);
5254 if (really_overloaded_fn (decl))
5255 return true;
5256 decl = OVL_FIRST (decl);
5259 /* Set TREE_USED for the benefit of -Wunused. */
5260 TREE_USED (decl) = 1;
5261 /* And for structured bindings also the underlying decl. */
5262 if (DECL_DECOMPOSITION_P (decl) && DECL_DECOMP_BASE (decl))
5263 TREE_USED (DECL_DECOMP_BASE (decl)) = 1;
5265 if (TREE_CODE (decl) == TEMPLATE_DECL)
5266 return true;
5268 if (DECL_CLONED_FUNCTION_P (decl))
5269 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5271 /* Mark enumeration types as used. */
5272 if (TREE_CODE (decl) == CONST_DECL)
5273 used_types_insert (DECL_CONTEXT (decl));
5275 if (TREE_CODE (decl) == FUNCTION_DECL
5276 && !maybe_instantiate_noexcept (decl, complain))
5277 return false;
5279 if (TREE_CODE (decl) == FUNCTION_DECL
5280 && DECL_DELETED_FN (decl))
5282 if (DECL_ARTIFICIAL (decl)
5283 && DECL_CONV_FN_P (decl)
5284 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
5285 /* We mark a lambda conversion op as deleted if we can't
5286 generate it properly; see maybe_add_lambda_conv_op. */
5287 sorry ("converting lambda that uses %<...%> to function pointer");
5288 else if (complain & tf_error)
5290 error ("use of deleted function %qD", decl);
5291 if (!maybe_explain_implicit_delete (decl))
5292 inform (DECL_SOURCE_LOCATION (decl), "declared here");
5294 return false;
5297 cp_warn_deprecated_use (decl, complain);
5299 /* We can only check DECL_ODR_USED on variables or functions with
5300 DECL_LANG_SPECIFIC set, and these are also the only decls that we
5301 might need special handling for. */
5302 if (!VAR_OR_FUNCTION_DECL_P (decl)
5303 || DECL_LANG_SPECIFIC (decl) == NULL
5304 || DECL_THUNK_P (decl))
5306 if (!processing_template_decl
5307 && !require_deduced_type (decl, complain))
5308 return false;
5309 return true;
5312 /* We only want to do this processing once. We don't need to keep trying
5313 to instantiate inline templates, because unit-at-a-time will make sure
5314 we get them compiled before functions that want to inline them. */
5315 if (DECL_ODR_USED (decl))
5316 return true;
5318 /* Normally, we can wait until instantiation-time to synthesize DECL.
5319 However, if DECL is a static data member initialized with a constant
5320 or a constexpr function, we need it right now because a reference to
5321 such a data member or a call to such function is not value-dependent.
5322 For a function that uses auto in the return type, we need to instantiate
5323 it to find out its type. For OpenMP user defined reductions, we need
5324 them instantiated for reduction clauses which inline them by hand
5325 directly. */
5326 maybe_instantiate_decl (decl);
5328 if (processing_template_decl || in_template_function ())
5329 return true;
5331 /* Check this too in case we're within instantiate_non_dependent_expr. */
5332 if (DECL_TEMPLATE_INFO (decl)
5333 && uses_template_parms (DECL_TI_ARGS (decl)))
5334 return true;
5336 if (!require_deduced_type (decl, complain))
5337 return false;
5339 if (builtin_pack_fn_p (decl))
5341 error ("use of built-in parameter pack %qD outside of a template",
5342 DECL_NAME (decl));
5343 return false;
5346 /* If we don't need a value, then we don't need to synthesize DECL. */
5347 if (cp_unevaluated_operand || in_discarded_stmt)
5348 return true;
5350 DECL_ODR_USED (decl) = 1;
5351 if (DECL_CLONED_FUNCTION_P (decl))
5352 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5354 /* DR 757: A type without linkage shall not be used as the type of a
5355 variable or function with linkage, unless
5356 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5357 o the variable or function is not used (3.2 [basic.def.odr]) or is
5358 defined in the same translation unit. */
5359 if (cxx_dialect > cxx98
5360 && decl_linkage (decl) != lk_none
5361 && !DECL_EXTERN_C_P (decl)
5362 && !DECL_ARTIFICIAL (decl)
5363 && !decl_defined_p (decl)
5364 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
5366 if (is_local_extern (decl))
5367 /* There's no way to define a local extern, and adding it to
5368 the vector interferes with GC, so give an error now. */
5369 no_linkage_error (decl);
5370 else
5371 vec_safe_push (no_linkage_decls, decl);
5374 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
5375 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
5376 /* Remember it, so we can check it was defined. */
5377 note_vague_linkage_fn (decl);
5379 /* Is it a synthesized method that needs to be synthesized? */
5380 if (TREE_CODE (decl) == FUNCTION_DECL
5381 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5382 && DECL_DEFAULTED_FN (decl)
5383 /* A function defaulted outside the class is synthesized either by
5384 cp_finish_decl or instantiate_decl. */
5385 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
5386 && ! DECL_INITIAL (decl))
5388 /* Defer virtual destructors so that thunks get the right
5389 linkage. */
5390 if (DECL_VIRTUAL_P (decl) && !at_eof)
5392 note_vague_linkage_fn (decl);
5393 return true;
5396 /* Remember the current location for a function we will end up
5397 synthesizing. Then we can inform the user where it was
5398 required in the case of error. */
5399 DECL_SOURCE_LOCATION (decl) = input_location;
5401 /* Synthesizing an implicitly defined member function will result in
5402 garbage collection. We must treat this situation as if we were
5403 within the body of a function so as to avoid collecting live data
5404 on the stack (such as overload resolution candidates).
5406 We could just let cp_write_global_declarations handle synthesizing
5407 this function by adding it to deferred_fns, but doing
5408 it at the use site produces better error messages. */
5409 ++function_depth;
5410 synthesize_method (decl);
5411 --function_depth;
5412 /* If this is a synthesized method we don't need to
5413 do the instantiation test below. */
5415 else if (VAR_OR_FUNCTION_DECL_P (decl)
5416 && DECL_TEMPLATE_INFO (decl)
5417 && !DECL_DECLARED_CONCEPT_P (decl)
5418 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5419 || always_instantiate_p (decl)))
5420 /* If this is a function or variable that is an instance of some
5421 template, we now know that we will need to actually do the
5422 instantiation. We check that DECL is not an explicit
5423 instantiation because that is not checked in instantiate_decl.
5425 We put off instantiating functions in order to improve compile
5426 times. Maintaining a stack of active functions is expensive,
5427 and the inliner knows to instantiate any functions it might
5428 need. Therefore, we always try to defer instantiation. */
5430 ++function_depth;
5431 instantiate_decl (decl, /*defer_ok=*/true,
5432 /*expl_inst_class_mem_p=*/false);
5433 --function_depth;
5436 return true;
5439 bool
5440 mark_used (tree decl)
5442 return mark_used (decl, tf_warning_or_error);
5445 tree
5446 vtv_start_verification_constructor_init_function (void)
5448 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5451 tree
5452 vtv_finish_verification_constructor_init_function (tree function_body)
5454 tree fn;
5456 finish_compound_stmt (function_body);
5457 fn = finish_function (/*inline_p=*/false);
5458 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5459 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5461 return fn;
5464 #include "gt-cp-decl2.h"