PR c++/86728 - C variadic generic lambda.
[official-gcc.git] / gcc / cp / decl2.c
blobd67ced097daf019911dca9d7f0c572b68215fda5
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 if (permerror (DECL_SOURCE_LOCATION (decl),
768 "static data member %qD in unnamed class", decl))
769 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
770 "unnamed class defined here");
771 break;
774 DECL_IN_AGGR_P (decl) = 1;
776 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
777 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
778 SET_VAR_HAD_UNKNOWN_BOUND (decl);
780 if (init)
782 /* Similarly to start_decl_1, we want to complete the type in order
783 to do the right thing in cp_apply_type_quals_to_decl, possibly
784 clear TYPE_QUAL_CONST (c++/65579). */
785 tree type = TREE_TYPE (decl) = complete_type (TREE_TYPE (decl));
786 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
789 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
792 /* DECLARATOR and DECLSPECS correspond to a class member. The other
793 parameters are as for cp_finish_decl. Return the DECL for the
794 class member declared. */
796 tree
797 grokfield (const cp_declarator *declarator,
798 cp_decl_specifier_seq *declspecs,
799 tree init, bool init_const_expr_p,
800 tree asmspec_tree,
801 tree attrlist)
803 tree value;
804 const char *asmspec = 0;
805 int flags;
806 tree name;
808 if (init
809 && TREE_CODE (init) == TREE_LIST
810 && TREE_VALUE (init) == error_mark_node
811 && TREE_CHAIN (init) == NULL_TREE)
812 init = NULL_TREE;
814 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
815 if (! value || value == error_mark_node)
816 /* friend or constructor went bad. */
817 return error_mark_node;
818 if (TREE_TYPE (value) == error_mark_node)
819 return value;
821 if (TREE_CODE (value) == TYPE_DECL && init)
823 error ("typedef %qD is initialized (use decltype instead)", value);
824 init = NULL_TREE;
827 /* Pass friendly classes back. */
828 if (value == void_type_node)
829 return value;
832 name = DECL_NAME (value);
834 if (name != NULL_TREE)
836 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
838 error ("explicit template argument list not allowed");
839 return error_mark_node;
842 if (IDENTIFIER_POINTER (name)[0] == '_'
843 && id_equal (name, "_vptr"))
844 error ("member %qD conflicts with virtual function table field name",
845 value);
848 /* Stash away type declarations. */
849 if (TREE_CODE (value) == TYPE_DECL)
851 DECL_NONLOCAL (value) = 1;
852 DECL_CONTEXT (value) = current_class_type;
854 if (attrlist)
856 int attrflags = 0;
858 /* If this is a typedef that names the class for linkage purposes
859 (7.1.3p8), apply any attributes directly to the type. */
860 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
861 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
862 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
864 cplus_decl_attributes (&value, attrlist, attrflags);
867 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
868 && TREE_TYPE (value) != error_mark_node
869 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
870 set_underlying_type (value);
872 /* It's important that push_template_decl below follows
873 set_underlying_type above so that the created template
874 carries the properly set type of VALUE. */
875 if (processing_template_decl)
876 value = push_template_decl (value);
878 record_locally_defined_typedef (value);
879 return value;
882 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
884 if (!friendp && DECL_IN_AGGR_P (value))
886 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
887 return void_type_node;
890 if (asmspec_tree && asmspec_tree != error_mark_node)
891 asmspec = TREE_STRING_POINTER (asmspec_tree);
893 if (init)
895 if (TREE_CODE (value) == FUNCTION_DECL)
897 if (init == ridpointers[(int)RID_DELETE])
899 if (friendp && decl_defined_p (value))
901 error ("redefinition of %q#D", value);
902 inform (DECL_SOURCE_LOCATION (value),
903 "%q#D previously defined here", value);
905 else
907 DECL_DELETED_FN (value) = 1;
908 DECL_DECLARED_INLINE_P (value) = 1;
909 DECL_INITIAL (value) = error_mark_node;
912 else if (init == ridpointers[(int)RID_DEFAULT])
914 if (defaultable_fn_check (value))
916 DECL_DEFAULTED_FN (value) = 1;
917 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
918 DECL_DECLARED_INLINE_P (value) = 1;
921 else if (TREE_CODE (init) == DEFAULT_ARG)
922 error ("invalid initializer for member function %qD", value);
923 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
925 if (integer_zerop (init))
926 DECL_PURE_VIRTUAL_P (value) = 1;
927 else if (error_operand_p (init))
928 ; /* An error has already been reported. */
929 else
930 error ("invalid initializer for member function %qD",
931 value);
933 else
935 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
936 if (friendp)
937 error ("initializer specified for friend function %qD",
938 value);
939 else
940 error ("initializer specified for static member function %qD",
941 value);
944 else if (TREE_CODE (value) == FIELD_DECL)
945 /* C++11 NSDMI, keep going. */;
946 else if (!VAR_P (value))
947 gcc_unreachable ();
950 /* Pass friend decls back. */
951 if ((TREE_CODE (value) == FUNCTION_DECL
952 || TREE_CODE (value) == TEMPLATE_DECL)
953 && DECL_CONTEXT (value) != current_class_type)
954 return value;
956 /* Need to set this before push_template_decl. */
957 if (VAR_P (value))
958 DECL_CONTEXT (value) = current_class_type;
960 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
962 value = push_template_decl (value);
963 if (error_operand_p (value))
964 return error_mark_node;
967 if (attrlist)
968 cplus_decl_attributes (&value, attrlist, 0);
970 if (init && DIRECT_LIST_INIT_P (init))
971 flags = LOOKUP_NORMAL;
972 else
973 flags = LOOKUP_IMPLICIT;
975 switch (TREE_CODE (value))
977 case VAR_DECL:
978 finish_static_data_member_decl (value, init, init_const_expr_p,
979 asmspec_tree, flags);
980 return value;
982 case FIELD_DECL:
983 if (asmspec)
984 error ("%<asm%> specifiers are not permitted on non-static data members");
985 if (DECL_INITIAL (value) == error_mark_node)
986 init = error_mark_node;
987 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
988 NULL_TREE, flags);
989 DECL_IN_AGGR_P (value) = 1;
990 return value;
992 case FUNCTION_DECL:
993 if (asmspec)
994 set_user_assembler_name (value, asmspec);
996 cp_finish_decl (value,
997 /*init=*/NULL_TREE,
998 /*init_const_expr_p=*/false,
999 asmspec_tree, flags);
1001 /* Pass friends back this way. */
1002 if (DECL_FRIEND_P (value))
1003 return void_type_node;
1005 DECL_IN_AGGR_P (value) = 1;
1006 return value;
1008 default:
1009 gcc_unreachable ();
1011 return NULL_TREE;
1014 /* Like `grokfield', but for bitfields.
1015 WIDTH is the width of the bitfield, a constant expression.
1016 The other parameters are as for grokfield. */
1018 tree
1019 grokbitfield (const cp_declarator *declarator,
1020 cp_decl_specifier_seq *declspecs, tree width, tree init,
1021 tree attrlist)
1023 tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1024 init != NULL_TREE, &attrlist);
1026 if (value == error_mark_node)
1027 return NULL_TREE; /* friends went bad. */
1028 if (TREE_TYPE (value) == error_mark_node)
1029 return value;
1031 /* Pass friendly classes back. */
1032 if (VOID_TYPE_P (value))
1033 return void_type_node;
1035 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
1036 && (INDIRECT_TYPE_P (value)
1037 || !dependent_type_p (TREE_TYPE (value))))
1039 error ("bit-field %qD with non-integral type", value);
1040 return error_mark_node;
1043 if (TREE_CODE (value) == TYPE_DECL)
1045 error ("cannot declare %qD to be a bit-field type", value);
1046 return NULL_TREE;
1049 /* Usually, finish_struct_1 catches bitfields with invalid types.
1050 But, in the case of bitfields with function type, we confuse
1051 ourselves into thinking they are member functions, so we must
1052 check here. */
1053 if (TREE_CODE (value) == FUNCTION_DECL)
1055 error ("cannot declare bit-field %qD with function type",
1056 DECL_NAME (value));
1057 return NULL_TREE;
1060 if (width && TYPE_WARN_IF_NOT_ALIGN (TREE_TYPE (value)))
1062 error ("cannot declare bit-field %qD with %<warn_if_not_aligned%> type",
1063 DECL_NAME (value));
1064 return NULL_TREE;
1067 if (DECL_IN_AGGR_P (value))
1069 error ("%qD is already defined in the class %qT", value,
1070 DECL_CONTEXT (value));
1071 return void_type_node;
1074 if (TREE_STATIC (value))
1076 error ("static member %qD cannot be a bit-field", value);
1077 return NULL_TREE;
1080 int flags = LOOKUP_IMPLICIT;
1081 if (init && DIRECT_LIST_INIT_P (init))
1082 flags = LOOKUP_NORMAL;
1083 cp_finish_decl (value, init, false, NULL_TREE, flags);
1085 if (width != error_mark_node)
1087 /* The width must be an integer type. */
1088 if (!type_dependent_expression_p (width)
1089 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1090 error ("width of bit-field %qD has non-integral type %qT", value,
1091 TREE_TYPE (width));
1092 else
1094 /* Temporarily stash the width in DECL_BIT_FIELD_REPRESENTATIVE.
1095 check_bitfield_decl picks it from there later and sets DECL_SIZE
1096 accordingly. */
1097 DECL_BIT_FIELD_REPRESENTATIVE (value) = width;
1098 SET_DECL_C_BIT_FIELD (value);
1102 DECL_IN_AGGR_P (value) = 1;
1104 if (attrlist)
1105 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1107 return value;
1111 /* Returns true iff ATTR is an attribute which needs to be applied at
1112 instantiation time rather than template definition time. */
1114 static bool
1115 is_late_template_attribute (tree attr, tree decl)
1117 tree name = get_attribute_name (attr);
1118 tree args = TREE_VALUE (attr);
1119 const struct attribute_spec *spec = lookup_attribute_spec (name);
1120 tree arg;
1122 if (!spec)
1123 /* Unknown attribute. */
1124 return false;
1126 /* Attribute weak handling wants to write out assembly right away. */
1127 if (is_attribute_p ("weak", name))
1128 return true;
1130 /* Attributes used and unused are applied directly to typedefs for the
1131 benefit of maybe_warn_unused_local_typedefs. */
1132 if (TREE_CODE (decl) == TYPE_DECL
1133 && (is_attribute_p ("unused", name)
1134 || is_attribute_p ("used", name)))
1135 return false;
1137 /* Attribute tls_model wants to modify the symtab. */
1138 if (is_attribute_p ("tls_model", name))
1139 return true;
1141 /* #pragma omp declare simd attribute needs to be always deferred. */
1142 if (flag_openmp
1143 && is_attribute_p ("omp declare simd", name))
1144 return true;
1146 /* An attribute pack is clearly dependent. */
1147 if (args && PACK_EXPANSION_P (args))
1148 return true;
1150 /* If any of the arguments are dependent expressions, we can't evaluate
1151 the attribute until instantiation time. */
1152 for (arg = args; arg; arg = TREE_CHAIN (arg))
1154 tree t = TREE_VALUE (arg);
1156 /* If the first attribute argument is an identifier, only consider
1157 second and following arguments. Attributes like mode, format,
1158 cleanup and several target specific attributes aren't late
1159 just because they have an IDENTIFIER_NODE as first argument. */
1160 if (arg == args && attribute_takes_identifier_p (name)
1161 && identifier_p (t))
1162 continue;
1164 if (value_dependent_expression_p (t)
1165 || type_dependent_expression_p (t))
1166 return true;
1169 if (TREE_CODE (decl) == TYPE_DECL
1170 || TYPE_P (decl)
1171 || spec->type_required)
1173 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1175 /* We can't apply any attributes to a completely unknown type until
1176 instantiation time. */
1177 enum tree_code code = TREE_CODE (type);
1178 if (code == TEMPLATE_TYPE_PARM
1179 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1180 || code == TYPENAME_TYPE)
1181 return true;
1182 /* Also defer most attributes on dependent types. This is not
1183 necessary in all cases, but is the better default. */
1184 else if (dependent_type_p (type)
1185 /* But some attributes specifically apply to templates. */
1186 && !is_attribute_p ("abi_tag", name)
1187 && !is_attribute_p ("deprecated", name)
1188 && !is_attribute_p ("visibility", name))
1189 return true;
1190 else
1191 return false;
1193 else
1194 return false;
1197 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1198 applied at instantiation time and return them. If IS_DEPENDENT is true,
1199 the declaration itself is dependent, so all attributes should be applied
1200 at instantiation time. */
1202 static tree
1203 splice_template_attributes (tree *attr_p, tree decl)
1205 tree *p = attr_p;
1206 tree late_attrs = NULL_TREE;
1207 tree *q = &late_attrs;
1209 if (!p)
1210 return NULL_TREE;
1212 for (; *p; )
1214 if (is_late_template_attribute (*p, decl))
1216 ATTR_IS_DEPENDENT (*p) = 1;
1217 *q = *p;
1218 *p = TREE_CHAIN (*p);
1219 q = &TREE_CHAIN (*q);
1220 *q = NULL_TREE;
1222 else
1223 p = &TREE_CHAIN (*p);
1226 return late_attrs;
1229 /* Remove any late attributes from the list in ATTR_P and attach them to
1230 DECL_P. */
1232 static void
1233 save_template_attributes (tree *attr_p, tree *decl_p, int flags)
1235 tree *q;
1237 if (attr_p && *attr_p == error_mark_node)
1238 return;
1240 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1241 if (!late_attrs)
1242 return;
1244 if (DECL_P (*decl_p))
1245 q = &DECL_ATTRIBUTES (*decl_p);
1246 else
1247 q = &TYPE_ATTRIBUTES (*decl_p);
1249 tree old_attrs = *q;
1251 /* Merge the late attributes at the beginning with the attribute
1252 list. */
1253 late_attrs = merge_attributes (late_attrs, *q);
1254 if (*q != late_attrs
1255 && !DECL_P (*decl_p)
1256 && !(flags & ATTR_FLAG_TYPE_IN_PLACE))
1258 if (!dependent_type_p (*decl_p))
1259 *decl_p = cp_build_type_attribute_variant (*decl_p, late_attrs);
1260 else
1262 *decl_p = build_variant_type_copy (*decl_p);
1263 TYPE_ATTRIBUTES (*decl_p) = late_attrs;
1266 else
1267 *q = late_attrs;
1269 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1271 /* We've added new attributes directly to the main variant, so
1272 now we need to update all of the other variants to include
1273 these new attributes. */
1274 tree variant;
1275 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1276 variant = TYPE_NEXT_VARIANT (variant))
1278 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1279 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1284 /* True if ATTRS contains any dependent attributes that affect type
1285 identity. */
1287 bool
1288 any_dependent_type_attributes_p (tree attrs)
1290 for (tree a = attrs; a; a = TREE_CHAIN (a))
1291 if (ATTR_IS_DEPENDENT (a))
1293 const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (a));
1294 if (as && as->affects_type_identity)
1295 return true;
1297 return false;
1300 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1301 to a typedef which gives a previously unnamed class or enum a name for
1302 linkage purposes. */
1304 bool
1305 attributes_naming_typedef_ok (tree attrs)
1307 for (; attrs; attrs = TREE_CHAIN (attrs))
1309 tree name = get_attribute_name (attrs);
1310 if (is_attribute_p ("vector_size", name))
1311 return false;
1313 return true;
1316 /* Like reconstruct_complex_type, but handle also template trees. */
1318 tree
1319 cp_reconstruct_complex_type (tree type, tree bottom)
1321 tree inner, outer;
1323 if (TYPE_PTR_P (type))
1325 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1326 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1327 TYPE_REF_CAN_ALIAS_ALL (type));
1329 else if (TYPE_REF_P (type))
1331 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1332 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1333 TYPE_REF_CAN_ALIAS_ALL (type));
1335 else if (TREE_CODE (type) == ARRAY_TYPE)
1337 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1338 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1339 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1340 element type qualification will be handled by the recursive
1341 cp_reconstruct_complex_type call and cp_build_qualified_type
1342 for ARRAY_TYPEs changes the element type. */
1343 return outer;
1345 else if (TREE_CODE (type) == FUNCTION_TYPE)
1347 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1348 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1349 outer = apply_memfn_quals (outer, type_memfn_quals (type));
1351 else if (TREE_CODE (type) == METHOD_TYPE)
1353 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1354 /* The build_method_type_directly() routine prepends 'this' to argument list,
1355 so we must compensate by getting rid of it. */
1356 outer
1357 = build_method_type_directly
1358 (class_of_this_parm (type), inner,
1359 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1361 else if (TREE_CODE (type) == OFFSET_TYPE)
1363 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1364 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1366 else
1367 return bottom;
1369 if (TYPE_ATTRIBUTES (type))
1370 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1371 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1372 outer = cxx_copy_lang_qualifiers (outer, type);
1374 return outer;
1377 /* Replaces any constexpr expression that may be into the attributes
1378 arguments with their reduced value. */
1380 static void
1381 cp_check_const_attributes (tree attributes)
1383 if (attributes == error_mark_node)
1384 return;
1386 tree attr;
1387 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1389 tree arg;
1390 for (arg = TREE_VALUE (attr); arg && TREE_CODE (arg) == TREE_LIST;
1391 arg = TREE_CHAIN (arg))
1393 tree expr = TREE_VALUE (arg);
1394 if (EXPR_P (expr))
1395 TREE_VALUE (arg) = fold_non_dependent_expr (expr);
1400 /* Return true if TYPE is an OpenMP mappable type. */
1401 bool
1402 cp_omp_mappable_type (tree type)
1404 /* Mappable type has to be complete. */
1405 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1406 return false;
1407 /* Arrays have mappable type if the elements have mappable type. */
1408 while (TREE_CODE (type) == ARRAY_TYPE)
1409 type = TREE_TYPE (type);
1410 /* A mappable type cannot contain virtual members. */
1411 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1412 return false;
1413 /* All data members must be non-static. */
1414 if (CLASS_TYPE_P (type))
1416 tree field;
1417 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1418 if (VAR_P (field))
1419 return false;
1420 /* All fields must have mappable types. */
1421 else if (TREE_CODE (field) == FIELD_DECL
1422 && !cp_omp_mappable_type (TREE_TYPE (field)))
1423 return false;
1425 return true;
1428 /* Return the last pushed declaration for the symbol DECL or NULL
1429 when no such declaration exists. */
1431 static tree
1432 find_last_decl (tree decl)
1434 tree last_decl = NULL_TREE;
1436 if (tree name = DECL_P (decl) ? DECL_NAME (decl) : NULL_TREE)
1438 /* Look up the declaration in its scope. */
1439 tree pushed_scope = NULL_TREE;
1440 if (tree ctype = DECL_CONTEXT (decl))
1441 pushed_scope = push_scope (ctype);
1443 last_decl = lookup_name (name);
1445 if (pushed_scope)
1446 pop_scope (pushed_scope);
1448 /* The declaration may be a member conversion operator
1449 or a bunch of overfloads (handle the latter below). */
1450 if (last_decl && BASELINK_P (last_decl))
1451 last_decl = BASELINK_FUNCTIONS (last_decl);
1454 if (!last_decl)
1455 return NULL_TREE;
1457 if (DECL_P (last_decl) || TREE_CODE (last_decl) == OVERLOAD)
1459 /* A set of overloads of the same function. */
1460 for (lkp_iterator iter (last_decl); iter; ++iter)
1462 if (TREE_CODE (*iter) == OVERLOAD)
1463 continue;
1465 if (decls_match (decl, *iter, /*record_decls=*/false))
1466 return *iter;
1468 return NULL_TREE;
1471 return NULL_TREE;
1474 /* Like decl_attributes, but handle C++ complexity. */
1476 void
1477 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1479 if (*decl == NULL_TREE || *decl == void_type_node
1480 || *decl == error_mark_node)
1481 return;
1483 /* Add implicit "omp declare target" attribute if requested. */
1484 if (scope_chain->omp_declare_target_attribute
1485 && ((VAR_P (*decl)
1486 && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
1487 || TREE_CODE (*decl) == FUNCTION_DECL))
1489 if (VAR_P (*decl)
1490 && DECL_CLASS_SCOPE_P (*decl))
1491 error ("%q+D static data member inside of declare target directive",
1492 *decl);
1493 else if (VAR_P (*decl)
1494 && (processing_template_decl
1495 || !cp_omp_mappable_type (TREE_TYPE (*decl))))
1496 attributes = tree_cons (get_identifier ("omp declare target implicit"),
1497 NULL_TREE, attributes);
1498 else
1499 attributes = tree_cons (get_identifier ("omp declare target"),
1500 NULL_TREE, attributes);
1503 if (processing_template_decl)
1505 if (check_for_bare_parameter_packs (attributes))
1506 return;
1508 save_template_attributes (&attributes, decl, flags);
1511 cp_check_const_attributes (attributes);
1513 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1514 decl = &DECL_TEMPLATE_RESULT (*decl);
1516 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1518 attributes
1519 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1520 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1521 attributes, flags);
1523 else
1525 tree last_decl = find_last_decl (*decl);
1526 decl_attributes (decl, attributes, flags, last_decl);
1529 if (TREE_CODE (*decl) == TYPE_DECL)
1530 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1532 /* Propagate deprecation out to the template. */
1533 if (TREE_DEPRECATED (*decl))
1534 if (tree ti = get_template_info (*decl))
1536 tree tmpl = TI_TEMPLATE (ti);
1537 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1538 : DECL_TEMPLATE_RESULT (tmpl));
1539 if (*decl == pattern)
1540 TREE_DEPRECATED (tmpl) = true;
1544 /* Walks through the namespace- or function-scope anonymous union
1545 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1546 Returns one of the fields for use in the mangled name. */
1548 static tree
1549 build_anon_union_vars (tree type, tree object)
1551 tree main_decl = NULL_TREE;
1552 tree field;
1554 /* Rather than write the code to handle the non-union case,
1555 just give an error. */
1556 if (TREE_CODE (type) != UNION_TYPE)
1558 error ("anonymous struct not inside named type");
1559 return error_mark_node;
1562 for (field = TYPE_FIELDS (type);
1563 field != NULL_TREE;
1564 field = DECL_CHAIN (field))
1566 tree decl;
1567 tree ref;
1569 if (DECL_ARTIFICIAL (field))
1570 continue;
1571 if (TREE_CODE (field) != FIELD_DECL)
1573 permerror (DECL_SOURCE_LOCATION (field),
1574 "%q#D invalid; an anonymous union can only "
1575 "have non-static data members", field);
1576 continue;
1579 if (TREE_PRIVATE (field))
1580 permerror (DECL_SOURCE_LOCATION (field),
1581 "private member %q#D in anonymous union", field);
1582 else if (TREE_PROTECTED (field))
1583 permerror (DECL_SOURCE_LOCATION (field),
1584 "protected member %q#D in anonymous union", field);
1586 if (processing_template_decl)
1587 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1588 DECL_NAME (field), NULL_TREE);
1589 else
1590 ref = build_class_member_access_expr (object, field, NULL_TREE,
1591 false, tf_warning_or_error);
1593 if (DECL_NAME (field))
1595 tree base;
1597 decl = build_decl (input_location,
1598 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1599 DECL_ANON_UNION_VAR_P (decl) = 1;
1600 DECL_ARTIFICIAL (decl) = 1;
1602 base = get_base_address (object);
1603 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1604 TREE_STATIC (decl) = TREE_STATIC (base);
1605 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1607 SET_DECL_VALUE_EXPR (decl, ref);
1608 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1610 decl = pushdecl (decl);
1612 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1613 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1614 else
1615 decl = 0;
1617 if (main_decl == NULL_TREE)
1618 main_decl = decl;
1621 return main_decl;
1624 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1625 anonymous union, then all members must be laid out together. PUBLIC_P
1626 is nonzero if this union is not declared static. */
1628 void
1629 finish_anon_union (tree anon_union_decl)
1631 tree type;
1632 tree main_decl;
1633 bool public_p;
1635 if (anon_union_decl == error_mark_node)
1636 return;
1638 type = TREE_TYPE (anon_union_decl);
1639 public_p = TREE_PUBLIC (anon_union_decl);
1641 /* The VAR_DECL's context is the same as the TYPE's context. */
1642 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1644 if (TYPE_FIELDS (type) == NULL_TREE)
1645 return;
1647 if (public_p)
1649 error ("namespace-scope anonymous aggregates must be static");
1650 return;
1653 main_decl = build_anon_union_vars (type, anon_union_decl);
1654 if (main_decl == error_mark_node)
1655 return;
1656 if (main_decl == NULL_TREE)
1658 pedwarn (input_location, 0, "anonymous union with no members");
1659 return;
1662 if (!processing_template_decl)
1664 /* Use main_decl to set the mangled name. */
1665 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1666 maybe_commonize_var (anon_union_decl);
1667 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1668 mangle_decl (anon_union_decl);
1669 DECL_NAME (anon_union_decl) = NULL_TREE;
1672 pushdecl (anon_union_decl);
1673 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1676 /* Auxiliary functions to make type signatures for
1677 `operator new' and `operator delete' correspond to
1678 what compiler will be expecting. */
1680 tree
1681 coerce_new_type (tree type, location_t loc)
1683 int e = 0;
1684 tree args = TYPE_ARG_TYPES (type);
1686 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1688 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1690 e = 1;
1691 error_at (loc, "%<operator new%> must return type %qT",
1692 ptr_type_node);
1695 if (args && args != void_list_node)
1697 if (TREE_PURPOSE (args))
1699 /* [basic.stc.dynamic.allocation]
1701 The first parameter shall not have an associated default
1702 argument. */
1703 error_at (loc, "the first parameter of %<operator new%> cannot "
1704 "have a default argument");
1705 /* Throw away the default argument. */
1706 TREE_PURPOSE (args) = NULL_TREE;
1709 if (!same_type_p (TREE_VALUE (args), size_type_node))
1711 e = 2;
1712 args = TREE_CHAIN (args);
1715 else
1716 e = 2;
1718 if (e == 2)
1719 permerror (loc, "%<operator new%> takes type %<size_t%> (%qT) "
1720 "as first parameter", size_type_node);
1722 switch (e)
1724 case 2:
1725 args = tree_cons (NULL_TREE, size_type_node, args);
1726 /* Fall through. */
1727 case 1:
1728 type = (cxx_copy_lang_qualifiers
1729 (build_function_type (ptr_type_node, args),
1730 type));
1731 /* Fall through. */
1732 default:;
1734 return type;
1737 tree
1738 coerce_delete_type (tree type, location_t loc)
1740 int e = 0;
1741 tree args = TYPE_ARG_TYPES (type);
1743 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1745 if (!same_type_p (TREE_TYPE (type), void_type_node))
1747 e = 1;
1748 error_at (loc, "%<operator delete%> must return type %qT",
1749 void_type_node);
1752 if (!args || args == void_list_node
1753 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1755 e = 2;
1756 if (args && args != void_list_node)
1757 args = TREE_CHAIN (args);
1758 error_at (loc, "%<operator delete%> takes type %qT as first parameter",
1759 ptr_type_node);
1761 switch (e)
1763 case 2:
1764 args = tree_cons (NULL_TREE, ptr_type_node, args);
1765 /* Fall through. */
1766 case 1:
1767 type = (cxx_copy_lang_qualifiers
1768 (build_function_type (void_type_node, args),
1769 type));
1770 /* Fall through. */
1771 default:;
1774 return type;
1777 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1778 and mark them as needed. */
1780 static void
1781 mark_vtable_entries (tree decl)
1783 tree fnaddr;
1784 unsigned HOST_WIDE_INT idx;
1786 /* It's OK for the vtable to refer to deprecated virtual functions. */
1787 warning_sentinel w(warn_deprecated_decl);
1789 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1790 idx, fnaddr)
1792 tree fn;
1794 STRIP_NOPS (fnaddr);
1796 if (TREE_CODE (fnaddr) != ADDR_EXPR
1797 && TREE_CODE (fnaddr) != FDESC_EXPR)
1798 /* This entry is an offset: a virtual base class offset, a
1799 virtual call offset, an RTTI offset, etc. */
1800 continue;
1802 fn = TREE_OPERAND (fnaddr, 0);
1803 TREE_ADDRESSABLE (fn) = 1;
1804 /* When we don't have vcall offsets, we output thunks whenever
1805 we output the vtables that contain them. With vcall offsets,
1806 we know all the thunks we'll need when we emit a virtual
1807 function, so we emit the thunks there instead. */
1808 if (DECL_THUNK_P (fn))
1809 use_thunk (fn, /*emit_p=*/0);
1810 /* Set the location, as marking the function could cause
1811 instantiation. We do not need to preserve the incoming
1812 location, as we're called from c_parse_final_cleanups, which
1813 takes care of that. */
1814 input_location = DECL_SOURCE_LOCATION (fn);
1815 mark_used (fn);
1819 /* Adjust the TLS model on variable DECL if need be, typically after
1820 the linkage of DECL has been modified. */
1822 static void
1823 adjust_var_decl_tls_model (tree decl)
1825 if (CP_DECL_THREAD_LOCAL_P (decl)
1826 && !lookup_attribute ("tls_model", DECL_ATTRIBUTES (decl)))
1827 set_decl_tls_model (decl, decl_default_tls_model (decl));
1830 /* Set DECL up to have the closest approximation of "initialized common"
1831 linkage available. */
1833 void
1834 comdat_linkage (tree decl)
1836 if (flag_weak)
1837 make_decl_one_only (decl, cxx_comdat_group (decl));
1838 else if (TREE_CODE (decl) == FUNCTION_DECL
1839 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1840 /* We can just emit function and compiler-generated variables
1841 statically; having multiple copies is (for the most part) only
1842 a waste of space.
1844 There are two correctness issues, however: the address of a
1845 template instantiation with external linkage should be the
1846 same, independent of what translation unit asks for the
1847 address, and this will not hold when we emit multiple copies of
1848 the function. However, there's little else we can do.
1850 Also, by default, the typeinfo implementation assumes that
1851 there will be only one copy of the string used as the name for
1852 each type. Therefore, if weak symbols are unavailable, the
1853 run-time library should perform a more conservative check; it
1854 should perform a string comparison, rather than an address
1855 comparison. */
1856 TREE_PUBLIC (decl) = 0;
1857 else
1859 /* Static data member template instantiations, however, cannot
1860 have multiple copies. */
1861 if (DECL_INITIAL (decl) == 0
1862 || DECL_INITIAL (decl) == error_mark_node)
1863 DECL_COMMON (decl) = 1;
1864 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1866 DECL_COMMON (decl) = 1;
1867 DECL_INITIAL (decl) = error_mark_node;
1869 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1871 /* We can't do anything useful; leave vars for explicit
1872 instantiation. */
1873 DECL_EXTERNAL (decl) = 1;
1874 DECL_NOT_REALLY_EXTERN (decl) = 0;
1878 if (TREE_PUBLIC (decl))
1879 DECL_COMDAT (decl) = 1;
1881 if (VAR_P (decl))
1882 adjust_var_decl_tls_model (decl);
1885 /* For win32 we also want to put explicit instantiations in
1886 linkonce sections, so that they will be merged with implicit
1887 instantiations; otherwise we get duplicate symbol errors.
1888 For Darwin we do not want explicit instantiations to be
1889 linkonce. */
1891 void
1892 maybe_make_one_only (tree decl)
1894 /* We used to say that this was not necessary on targets that support weak
1895 symbols, because the implicit instantiations will defer to the explicit
1896 one. However, that's not actually the case in SVR4; a strong definition
1897 after a weak one is an error. Also, not making explicit
1898 instantiations one_only means that we can end up with two copies of
1899 some template instantiations. */
1900 if (! flag_weak)
1901 return;
1903 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1904 we can get away with not emitting them if they aren't used. We need
1905 to for variables so that cp_finish_decl will update their linkage,
1906 because their DECL_INITIAL may not have been set properly yet. */
1908 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1909 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1910 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1912 make_decl_one_only (decl, cxx_comdat_group (decl));
1914 if (VAR_P (decl))
1916 varpool_node *node = varpool_node::get_create (decl);
1917 DECL_COMDAT (decl) = 1;
1918 /* Mark it needed so we don't forget to emit it. */
1919 node->forced_by_abi = true;
1920 TREE_USED (decl) = 1;
1922 adjust_var_decl_tls_model (decl);
1927 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1928 This predicate will give the right answer during parsing of the
1929 function, which other tests may not. */
1931 bool
1932 vague_linkage_p (tree decl)
1934 if (!TREE_PUBLIC (decl))
1936 /* maybe_thunk_body clears TREE_PUBLIC and DECL_ABSTRACT_P on the
1937 maybe-in-charge 'tor variants; in that case we need to check one of
1938 the "clones" for the real linkage. But only in that case; before
1939 maybe_clone_body we haven't yet copied the linkage to the clones. */
1940 if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl)
1941 && !DECL_ABSTRACT_P (decl)
1942 && DECL_CHAIN (decl)
1943 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)))
1944 return vague_linkage_p (DECL_CHAIN (decl));
1946 gcc_checking_assert (!DECL_COMDAT (decl));
1947 return false;
1949 /* Unfortunately, import_export_decl has not always been called
1950 before the function is processed, so we cannot simply check
1951 DECL_COMDAT. */
1952 if (DECL_COMDAT (decl)
1953 || (TREE_CODE (decl) == FUNCTION_DECL
1954 && DECL_DECLARED_INLINE_P (decl))
1955 || (DECL_LANG_SPECIFIC (decl)
1956 && DECL_TEMPLATE_INSTANTIATION (decl))
1957 || (VAR_P (decl) && DECL_INLINE_VAR_P (decl)))
1958 return true;
1959 else if (DECL_FUNCTION_SCOPE_P (decl))
1960 /* A local static in an inline effectively has vague linkage. */
1961 return (TREE_STATIC (decl)
1962 && vague_linkage_p (DECL_CONTEXT (decl)));
1963 else
1964 return false;
1967 /* Determine whether or not we want to specifically import or export CTYPE,
1968 using various heuristics. */
1970 static void
1971 import_export_class (tree ctype)
1973 /* -1 for imported, 1 for exported. */
1974 int import_export = 0;
1976 /* It only makes sense to call this function at EOF. The reason is
1977 that this function looks at whether or not the first non-inline
1978 non-abstract virtual member function has been defined in this
1979 translation unit. But, we can't possibly know that until we've
1980 seen the entire translation unit. */
1981 gcc_assert (at_eof);
1983 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1984 return;
1986 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1987 we will have CLASSTYPE_INTERFACE_ONLY set but not
1988 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1989 heuristic because someone will supply a #pragma implementation
1990 elsewhere, and deducing it here would produce a conflict. */
1991 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1992 return;
1994 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1995 import_export = -1;
1996 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1997 import_export = 1;
1998 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1999 && !flag_implicit_templates)
2000 /* For a template class, without -fimplicit-templates, check the
2001 repository. If the virtual table is assigned to this
2002 translation unit, then export the class; otherwise, import
2003 it. */
2004 import_export = repo_export_class_p (ctype) ? 1 : -1;
2005 else if (TYPE_POLYMORPHIC_P (ctype))
2007 /* The ABI specifies that the virtual table and associated
2008 information are emitted with the key method, if any. */
2009 tree method = CLASSTYPE_KEY_METHOD (ctype);
2010 /* If weak symbol support is not available, then we must be
2011 careful not to emit the vtable when the key function is
2012 inline. An inline function can be defined in multiple
2013 translation units. If we were to emit the vtable in each
2014 translation unit containing a definition, we would get
2015 multiple definition errors at link-time. */
2016 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
2017 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
2020 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
2021 a definition anywhere else. */
2022 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
2023 import_export = 0;
2025 /* Allow back ends the chance to overrule the decision. */
2026 if (targetm.cxx.import_export_class)
2027 import_export = targetm.cxx.import_export_class (ctype, import_export);
2029 if (import_export)
2031 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
2032 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
2036 /* Return true if VAR has already been provided to the back end; in that
2037 case VAR should not be modified further by the front end. */
2038 static bool
2039 var_finalized_p (tree var)
2041 return varpool_node::get_create (var)->definition;
2044 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
2045 must be emitted in this translation unit. Mark it as such. */
2047 void
2048 mark_needed (tree decl)
2050 TREE_USED (decl) = 1;
2051 if (TREE_CODE (decl) == FUNCTION_DECL)
2053 /* Extern inline functions don't become needed when referenced.
2054 If we know a method will be emitted in other TU and no new
2055 functions can be marked reachable, just use the external
2056 definition. */
2057 struct cgraph_node *node = cgraph_node::get_create (decl);
2058 node->forced_by_abi = true;
2060 /* #pragma interface and -frepo code can call mark_needed for
2061 maybe-in-charge 'tors; mark the clones as well. */
2062 tree clone;
2063 FOR_EACH_CLONE (clone, decl)
2064 mark_needed (clone);
2066 else if (VAR_P (decl))
2068 varpool_node *node = varpool_node::get_create (decl);
2069 /* C++ frontend use mark_decl_references to force COMDAT variables
2070 to be output that might appear dead otherwise. */
2071 node->forced_by_abi = true;
2075 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
2076 returns true if a definition of this entity should be provided in
2077 this object file. Callers use this function to determine whether
2078 or not to let the back end know that a definition of DECL is
2079 available in this translation unit. */
2081 bool
2082 decl_needed_p (tree decl)
2084 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2085 /* This function should only be called at the end of the translation
2086 unit. We cannot be sure of whether or not something will be
2087 COMDAT until that point. */
2088 gcc_assert (at_eof);
2090 /* All entities with external linkage that are not COMDAT/EXTERN should be
2091 emitted; they may be referred to from other object files. */
2092 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
2093 return true;
2094 /* Functions marked "dllexport" must be emitted so that they are
2095 visible to other DLLs. */
2096 if (flag_keep_inline_dllexport
2097 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
2098 return true;
2100 /* When not optimizing, do not bother to produce definitions for extern
2101 symbols. */
2102 if (DECL_REALLY_EXTERN (decl)
2103 && ((TREE_CODE (decl) != FUNCTION_DECL
2104 && !optimize)
2105 || (TREE_CODE (decl) == FUNCTION_DECL
2106 && !opt_for_fn (decl, optimize)))
2107 && !lookup_attribute ("always_inline", decl))
2108 return false;
2110 /* If this entity was used, let the back end see it; it will decide
2111 whether or not to emit it into the object file. */
2112 if (TREE_USED (decl))
2113 return true;
2115 /* Virtual functions might be needed for devirtualization. */
2116 if (flag_devirtualize
2117 && TREE_CODE (decl) == FUNCTION_DECL
2118 && DECL_VIRTUAL_P (decl))
2119 return true;
2121 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2122 reference to DECL might cause it to be emitted later. */
2123 return false;
2126 /* If necessary, write out the vtables for the dynamic class CTYPE.
2127 Returns true if any vtables were emitted. */
2129 static bool
2130 maybe_emit_vtables (tree ctype)
2132 tree vtbl;
2133 tree primary_vtbl;
2134 int needed = 0;
2135 varpool_node *current = NULL, *last = NULL;
2137 /* If the vtables for this class have already been emitted there is
2138 nothing more to do. */
2139 primary_vtbl = CLASSTYPE_VTABLES (ctype);
2140 if (var_finalized_p (primary_vtbl))
2141 return false;
2142 /* Ignore dummy vtables made by get_vtable_decl. */
2143 if (TREE_TYPE (primary_vtbl) == void_type_node)
2144 return false;
2146 /* On some targets, we cannot determine the key method until the end
2147 of the translation unit -- which is when this function is
2148 called. */
2149 if (!targetm.cxx.key_method_may_be_inline ())
2150 determine_key_method (ctype);
2152 /* See if any of the vtables are needed. */
2153 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2155 import_export_decl (vtbl);
2156 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2157 needed = 1;
2159 if (!needed)
2161 /* If the references to this class' vtables are optimized away,
2162 still emit the appropriate debugging information. See
2163 dfs_debug_mark. */
2164 if (DECL_COMDAT (primary_vtbl)
2165 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2166 note_debug_info_needed (ctype);
2167 return false;
2170 /* The ABI requires that we emit all of the vtables if we emit any
2171 of them. */
2172 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2174 /* Mark entities references from the virtual table as used. */
2175 mark_vtable_entries (vtbl);
2177 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2179 vec<tree, va_gc> *cleanups = NULL;
2180 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2181 LOOKUP_NORMAL);
2183 /* It had better be all done at compile-time. */
2184 gcc_assert (!expr && !cleanups);
2187 /* Write it out. */
2188 DECL_EXTERNAL (vtbl) = 0;
2189 rest_of_decl_compilation (vtbl, 1, 1);
2191 /* Because we're only doing syntax-checking, we'll never end up
2192 actually marking the variable as written. */
2193 if (flag_syntax_only)
2194 TREE_ASM_WRITTEN (vtbl) = 1;
2195 else if (DECL_ONE_ONLY (vtbl))
2197 current = varpool_node::get_create (vtbl);
2198 if (last)
2199 current->add_to_same_comdat_group (last);
2200 last = current;
2204 /* Since we're writing out the vtable here, also write the debug
2205 info. */
2206 note_debug_info_needed (ctype);
2208 return true;
2211 /* A special return value from type_visibility meaning internal
2212 linkage. */
2214 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2216 /* walk_tree helper function for type_visibility. */
2218 static tree
2219 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2221 int *vis_p = (int *)data;
2222 if (! TYPE_P (*tp))
2224 *walk_subtrees = 0;
2226 else if (OVERLOAD_TYPE_P (*tp)
2227 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2229 *vis_p = VISIBILITY_ANON;
2230 return *tp;
2232 else if (CLASS_TYPE_P (*tp)
2233 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2234 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2235 return NULL;
2238 /* Returns the visibility of TYPE, which is the minimum visibility of its
2239 component types. */
2241 static int
2242 type_visibility (tree type)
2244 int vis = VISIBILITY_DEFAULT;
2245 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2246 return vis;
2249 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2250 specified (or if VISIBILITY is static). If TMPL is true, this
2251 constraint is for a template argument, and takes precedence
2252 over explicitly-specified visibility on the template. */
2254 static void
2255 constrain_visibility (tree decl, int visibility, bool tmpl)
2257 if (visibility == VISIBILITY_ANON)
2259 /* extern "C" declarations aren't affected by the anonymous
2260 namespace. */
2261 if (!DECL_EXTERN_C_P (decl))
2263 TREE_PUBLIC (decl) = 0;
2264 DECL_WEAK (decl) = 0;
2265 DECL_COMMON (decl) = 0;
2266 DECL_COMDAT (decl) = false;
2267 if (VAR_OR_FUNCTION_DECL_P (decl))
2269 struct symtab_node *snode = symtab_node::get (decl);
2271 if (snode)
2272 snode->set_comdat_group (NULL);
2274 DECL_INTERFACE_KNOWN (decl) = 1;
2275 if (DECL_LANG_SPECIFIC (decl))
2276 DECL_NOT_REALLY_EXTERN (decl) = 1;
2279 else if (visibility > DECL_VISIBILITY (decl)
2280 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2282 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2283 /* This visibility was not specified. */
2284 DECL_VISIBILITY_SPECIFIED (decl) = false;
2288 /* Constrain the visibility of DECL based on the visibility of its template
2289 arguments. */
2291 static void
2292 constrain_visibility_for_template (tree decl, tree targs)
2294 /* If this is a template instantiation, check the innermost
2295 template args for visibility constraints. The outer template
2296 args are covered by the class check. */
2297 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2298 int i;
2299 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2301 int vis = 0;
2303 tree arg = TREE_VEC_ELT (args, i-1);
2304 if (TYPE_P (arg))
2305 vis = type_visibility (arg);
2306 else
2308 if (REFERENCE_REF_P (arg))
2309 arg = TREE_OPERAND (arg, 0);
2310 if (TREE_TYPE (arg))
2311 STRIP_NOPS (arg);
2312 if (TREE_CODE (arg) == ADDR_EXPR)
2313 arg = TREE_OPERAND (arg, 0);
2314 if (VAR_OR_FUNCTION_DECL_P (arg))
2316 if (! TREE_PUBLIC (arg))
2317 vis = VISIBILITY_ANON;
2318 else
2319 vis = DECL_VISIBILITY (arg);
2322 if (vis)
2323 constrain_visibility (decl, vis, true);
2327 /* Like c_determine_visibility, but with additional C++-specific
2328 behavior.
2330 Function-scope entities can rely on the function's visibility because
2331 it is set in start_preparsed_function.
2333 Class-scope entities cannot rely on the class's visibility until the end
2334 of the enclosing class definition.
2336 Note that because namespaces have multiple independent definitions,
2337 namespace visibility is handled elsewhere using the #pragma visibility
2338 machinery rather than by decorating the namespace declaration.
2340 The goal is for constraints from the type to give a diagnostic, and
2341 other constraints to be applied silently. */
2343 void
2344 determine_visibility (tree decl)
2346 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2348 /* Only relevant for names with external linkage. */
2349 if (!TREE_PUBLIC (decl))
2350 return;
2352 /* Cloned constructors and destructors get the same visibility as
2353 the underlying function. That should be set up in
2354 maybe_clone_body. */
2355 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2357 bool orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2358 enum symbol_visibility orig_visibility = DECL_VISIBILITY (decl);
2360 /* The decl may be a template instantiation, which could influence
2361 visibilty. */
2362 tree template_decl = NULL_TREE;
2363 if (TREE_CODE (decl) == TYPE_DECL)
2365 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2367 if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
2368 template_decl = decl;
2370 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2371 template_decl = decl;
2373 else if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
2374 template_decl = decl;
2376 /* If DECL is a member of a class, visibility specifiers on the
2377 class can influence the visibility of the DECL. */
2378 tree class_type = NULL_TREE;
2379 if (DECL_CLASS_SCOPE_P (decl))
2380 class_type = DECL_CONTEXT (decl);
2381 else
2383 /* Not a class member. */
2385 /* Virtual tables have DECL_CONTEXT set to their associated class,
2386 so they are automatically handled above. */
2387 gcc_assert (!VAR_P (decl)
2388 || !DECL_VTABLE_OR_VTT_P (decl));
2390 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2392 /* Local statics and classes get the visibility of their
2393 containing function by default, except that
2394 -fvisibility-inlines-hidden doesn't affect them. */
2395 tree fn = DECL_CONTEXT (decl);
2396 if (DECL_VISIBILITY_SPECIFIED (fn))
2398 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2399 DECL_VISIBILITY_SPECIFIED (decl) =
2400 DECL_VISIBILITY_SPECIFIED (fn);
2402 else
2404 if (DECL_CLASS_SCOPE_P (fn))
2405 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2406 else if (determine_hidden_inline (fn))
2408 DECL_VISIBILITY (decl) = default_visibility;
2409 DECL_VISIBILITY_SPECIFIED (decl) =
2410 visibility_options.inpragma;
2412 else
2414 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2415 DECL_VISIBILITY_SPECIFIED (decl) =
2416 DECL_VISIBILITY_SPECIFIED (fn);
2420 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2421 but have no TEMPLATE_INFO, so don't try to check it. */
2422 template_decl = NULL_TREE;
2424 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2425 && flag_visibility_ms_compat)
2427 /* Under -fvisibility-ms-compat, types are visible by default,
2428 even though their contents aren't. */
2429 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2430 int underlying_vis = type_visibility (underlying_type);
2431 if (underlying_vis == VISIBILITY_ANON
2432 || (CLASS_TYPE_P (underlying_type)
2433 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2434 constrain_visibility (decl, underlying_vis, false);
2435 else
2436 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2438 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2440 /* tinfo visibility is based on the type it's for. */
2441 constrain_visibility
2442 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2444 /* Give the target a chance to override the visibility associated
2445 with DECL. */
2446 if (TREE_PUBLIC (decl)
2447 && !DECL_REALLY_EXTERN (decl)
2448 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2449 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2450 targetm.cxx.determine_class_data_visibility (decl);
2452 else if (template_decl)
2453 /* Template instantiations and specializations get visibility based
2454 on their template unless they override it with an attribute. */;
2455 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2457 if (determine_hidden_inline (decl))
2458 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2459 else
2461 /* Set default visibility to whatever the user supplied with
2462 #pragma GCC visibility or a namespace visibility attribute. */
2463 DECL_VISIBILITY (decl) = default_visibility;
2464 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2469 if (template_decl)
2471 /* If the specialization doesn't specify visibility, use the
2472 visibility from the template. */
2473 tree tinfo = get_template_info (template_decl);
2474 tree args = TI_ARGS (tinfo);
2475 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2476 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2477 : DECL_ATTRIBUTES (decl));
2479 if (args != error_mark_node)
2481 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2483 if (!DECL_VISIBILITY_SPECIFIED (decl))
2485 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2486 && determine_hidden_inline (decl))
2487 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2488 else
2490 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2491 DECL_VISIBILITY_SPECIFIED (decl)
2492 = DECL_VISIBILITY_SPECIFIED (pattern);
2496 if (args
2497 /* Template argument visibility outweighs #pragma or namespace
2498 visibility, but not an explicit attribute. */
2499 && !lookup_attribute ("visibility", attribs))
2501 int depth = TMPL_ARGS_DEPTH (args);
2502 if (DECL_VISIBILITY_SPECIFIED (decl))
2504 /* A class template member with explicit visibility
2505 overrides the class visibility, so we need to apply
2506 all the levels of template args directly. */
2507 int i;
2508 for (i = 1; i <= depth; ++i)
2510 tree lev = TMPL_ARGS_LEVEL (args, i);
2511 constrain_visibility_for_template (decl, lev);
2514 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2515 /* Limit visibility based on its template arguments. */
2516 constrain_visibility_for_template (decl, args);
2521 if (class_type)
2522 determine_visibility_from_class (decl, class_type);
2524 if (decl_anon_ns_mem_p (decl))
2525 /* Names in an anonymous namespace get internal linkage.
2526 This might change once we implement export. */
2527 constrain_visibility (decl, VISIBILITY_ANON, false);
2528 else if (TREE_CODE (decl) != TYPE_DECL)
2530 /* Propagate anonymity from type to decl. */
2531 int tvis = type_visibility (TREE_TYPE (decl));
2532 if (tvis == VISIBILITY_ANON
2533 || ! DECL_VISIBILITY_SPECIFIED (decl))
2534 constrain_visibility (decl, tvis, false);
2536 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2537 /* DR 757: A type without linkage shall not be used as the type of a
2538 variable or function with linkage, unless
2539 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2540 o the variable or function is not used (3.2 [basic.def.odr]) or is
2541 defined in the same translation unit.
2543 Since non-extern "C" decls need to be defined in the same
2544 translation unit, we can make the type internal. */
2545 constrain_visibility (decl, VISIBILITY_ANON, false);
2547 /* If visibility changed and DECL already has DECL_RTL, ensure
2548 symbol flags are updated. */
2549 if ((DECL_VISIBILITY (decl) != orig_visibility
2550 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2551 && ((VAR_P (decl) && TREE_STATIC (decl))
2552 || TREE_CODE (decl) == FUNCTION_DECL)
2553 && DECL_RTL_SET_P (decl))
2554 make_decl_rtl (decl);
2557 /* By default, static data members and function members receive
2558 the visibility of their containing class. */
2560 static void
2561 determine_visibility_from_class (tree decl, tree class_type)
2563 if (DECL_VISIBILITY_SPECIFIED (decl))
2564 return;
2566 if (determine_hidden_inline (decl))
2567 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2568 else
2570 /* Default to the class visibility. */
2571 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2572 DECL_VISIBILITY_SPECIFIED (decl)
2573 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2576 /* Give the target a chance to override the visibility associated
2577 with DECL. */
2578 if (VAR_P (decl)
2579 && (DECL_TINFO_P (decl)
2580 || (DECL_VTABLE_OR_VTT_P (decl)
2581 /* Construction virtual tables are not exported because
2582 they cannot be referred to from other object files;
2583 their name is not standardized by the ABI. */
2584 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2585 && TREE_PUBLIC (decl)
2586 && !DECL_REALLY_EXTERN (decl)
2587 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2588 targetm.cxx.determine_class_data_visibility (decl);
2591 /* Returns true iff DECL is an inline that should get hidden visibility
2592 because of -fvisibility-inlines-hidden. */
2594 static bool
2595 determine_hidden_inline (tree decl)
2597 return (visibility_options.inlines_hidden
2598 /* Don't do this for inline templates; specializations might not be
2599 inline, and we don't want them to inherit the hidden
2600 visibility. We'll set it here for all inline instantiations. */
2601 && !processing_template_decl
2602 && TREE_CODE (decl) == FUNCTION_DECL
2603 && DECL_DECLARED_INLINE_P (decl)
2604 && (! DECL_LANG_SPECIFIC (decl)
2605 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2608 /* Constrain the visibility of a class TYPE based on the visibility of its
2609 field types. Warn if any fields require lesser visibility. */
2611 void
2612 constrain_class_visibility (tree type)
2614 tree binfo;
2615 tree t;
2616 int i;
2618 int vis = type_visibility (type);
2620 if (vis == VISIBILITY_ANON
2621 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2622 return;
2624 /* Don't warn about visibility if the class has explicit visibility. */
2625 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2626 vis = VISIBILITY_INTERNAL;
2628 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2629 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node
2630 && !DECL_ARTIFICIAL (t))
2632 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2633 int subvis = type_visibility (ftype);
2635 if (subvis == VISIBILITY_ANON)
2637 if (!in_main_input_context())
2639 tree nlt = no_linkage_check (ftype, /*relaxed_p=*/false);
2640 if (nlt)
2642 if (same_type_p (TREE_TYPE (t), nlt))
2643 warning (OPT_Wsubobject_linkage, "\
2644 %qT has a field %qD whose type has no linkage",
2645 type, t);
2646 else
2647 warning (OPT_Wsubobject_linkage, "\
2648 %qT has a field %qD whose type depends on the type %qT which has no linkage",
2649 type, t, nlt);
2651 else
2652 warning (OPT_Wsubobject_linkage, "\
2653 %qT has a field %qD whose type uses the anonymous namespace",
2654 type, t);
2657 else if (MAYBE_CLASS_TYPE_P (ftype)
2658 && vis < VISIBILITY_HIDDEN
2659 && subvis >= VISIBILITY_HIDDEN)
2660 warning (OPT_Wattributes, "\
2661 %qT declared with greater visibility than the type of its field %qD",
2662 type, t);
2665 binfo = TYPE_BINFO (type);
2666 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2668 int subvis = type_visibility (TREE_TYPE (t));
2670 if (subvis == VISIBILITY_ANON)
2672 if (!in_main_input_context())
2674 tree nlt = no_linkage_check (TREE_TYPE (t), /*relaxed_p=*/false);
2675 if (nlt)
2677 if (same_type_p (TREE_TYPE (t), nlt))
2678 warning (OPT_Wsubobject_linkage, "\
2679 %qT has a base %qT whose type has no linkage",
2680 type, TREE_TYPE (t));
2681 else
2682 warning (OPT_Wsubobject_linkage, "\
2683 %qT has a base %qT whose type depends on the type %qT which has no linkage",
2684 type, TREE_TYPE (t), nlt);
2686 else
2687 warning (OPT_Wsubobject_linkage, "\
2688 %qT has a base %qT whose type uses the anonymous namespace",
2689 type, TREE_TYPE (t));
2692 else if (vis < VISIBILITY_HIDDEN
2693 && subvis >= VISIBILITY_HIDDEN)
2694 warning (OPT_Wattributes, "\
2695 %qT declared with greater visibility than its base %qT",
2696 type, TREE_TYPE (t));
2700 /* Functions for adjusting the visibility of a tagged type and its nested
2701 types and declarations when it gets a name for linkage purposes from a
2702 typedef. */
2704 static void bt_reset_linkage_1 (binding_entry, void *);
2705 static void bt_reset_linkage_2 (binding_entry, void *);
2707 /* First reset the visibility of all the types. */
2709 static void
2710 reset_type_linkage_1 (tree type)
2712 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2713 if (CLASS_TYPE_P (type))
2714 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2715 bt_reset_linkage_1, NULL);
2717 static void
2718 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2720 reset_type_linkage_1 (b->type);
2723 /* Then reset the visibility of any static data members or member
2724 functions that use those types. */
2726 static void
2727 reset_decl_linkage (tree decl)
2729 if (TREE_PUBLIC (decl))
2730 return;
2731 if (DECL_CLONED_FUNCTION_P (decl))
2732 return;
2733 TREE_PUBLIC (decl) = true;
2734 DECL_INTERFACE_KNOWN (decl) = false;
2735 determine_visibility (decl);
2736 tentative_decl_linkage (decl);
2739 static void
2740 reset_type_linkage_2 (tree type)
2742 if (CLASS_TYPE_P (type))
2744 if (tree vt = CLASSTYPE_VTABLES (type))
2746 tree name = mangle_vtbl_for_type (type);
2747 DECL_NAME (vt) = name;
2748 SET_DECL_ASSEMBLER_NAME (vt, name);
2749 reset_decl_linkage (vt);
2751 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2753 tree name = mangle_typeinfo_for_type (type);
2754 DECL_NAME (ti) = name;
2755 SET_DECL_ASSEMBLER_NAME (ti, name);
2756 TREE_TYPE (name) = type;
2757 reset_decl_linkage (ti);
2759 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2761 tree mem = STRIP_TEMPLATE (m);
2762 if (TREE_CODE (mem) == VAR_DECL || TREE_CODE (mem) == FUNCTION_DECL)
2763 reset_decl_linkage (mem);
2765 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2766 bt_reset_linkage_2, NULL);
2770 static void
2771 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2773 reset_type_linkage_2 (b->type);
2775 void
2776 reset_type_linkage (tree type)
2778 reset_type_linkage_1 (type);
2779 reset_type_linkage_2 (type);
2782 /* Set up our initial idea of what the linkage of DECL should be. */
2784 void
2785 tentative_decl_linkage (tree decl)
2787 if (DECL_INTERFACE_KNOWN (decl))
2788 /* We've already made a decision as to how this function will
2789 be handled. */;
2790 else if (vague_linkage_p (decl))
2792 if (TREE_CODE (decl) == FUNCTION_DECL
2793 && decl_defined_p (decl))
2795 DECL_EXTERNAL (decl) = 1;
2796 DECL_NOT_REALLY_EXTERN (decl) = 1;
2797 note_vague_linkage_fn (decl);
2798 /* A non-template inline function with external linkage will
2799 always be COMDAT. As we must eventually determine the
2800 linkage of all functions, and as that causes writes to
2801 the data mapped in from the PCH file, it's advantageous
2802 to mark the functions at this point. */
2803 if (DECL_DECLARED_INLINE_P (decl)
2804 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2805 || DECL_DEFAULTED_FN (decl)))
2807 /* This function must have external linkage, as
2808 otherwise DECL_INTERFACE_KNOWN would have been
2809 set. */
2810 gcc_assert (TREE_PUBLIC (decl));
2811 comdat_linkage (decl);
2812 DECL_INTERFACE_KNOWN (decl) = 1;
2815 else if (VAR_P (decl))
2816 maybe_commonize_var (decl);
2820 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2821 for DECL has not already been determined, do so now by setting
2822 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2823 function is called entities with vague linkage whose definitions
2824 are available must have TREE_PUBLIC set.
2826 If this function decides to place DECL in COMDAT, it will set
2827 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2828 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2829 callers defer that decision until it is clear that DECL is actually
2830 required. */
2832 void
2833 import_export_decl (tree decl)
2835 int emit_p;
2836 bool comdat_p;
2837 bool import_p;
2838 tree class_type = NULL_TREE;
2840 if (DECL_INTERFACE_KNOWN (decl))
2841 return;
2843 /* We cannot determine what linkage to give to an entity with vague
2844 linkage until the end of the file. For example, a virtual table
2845 for a class will be defined if and only if the key method is
2846 defined in this translation unit. As a further example, consider
2847 that when compiling a translation unit that uses PCH file with
2848 "-frepo" it would be incorrect to make decisions about what
2849 entities to emit when building the PCH; those decisions must be
2850 delayed until the repository information has been processed. */
2851 gcc_assert (at_eof);
2852 /* Object file linkage for explicit instantiations is handled in
2853 mark_decl_instantiated. For static variables in functions with
2854 vague linkage, maybe_commonize_var is used.
2856 Therefore, the only declarations that should be provided to this
2857 function are those with external linkage that are:
2859 * implicit instantiations of function templates
2861 * inline function
2863 * implicit instantiations of static data members of class
2864 templates
2866 * virtual tables
2868 * typeinfo objects
2870 Furthermore, all entities that reach this point must have a
2871 definition available in this translation unit.
2873 The following assertions check these conditions. */
2874 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2875 /* Any code that creates entities with TREE_PUBLIC cleared should
2876 also set DECL_INTERFACE_KNOWN. */
2877 gcc_assert (TREE_PUBLIC (decl));
2878 if (TREE_CODE (decl) == FUNCTION_DECL)
2879 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2880 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2881 || DECL_DECLARED_INLINE_P (decl));
2882 else
2883 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2884 || DECL_VTABLE_OR_VTT_P (decl)
2885 || DECL_TINFO_P (decl));
2886 /* Check that a definition of DECL is available in this translation
2887 unit. */
2888 gcc_assert (!DECL_REALLY_EXTERN (decl));
2890 /* Assume that DECL will not have COMDAT linkage. */
2891 comdat_p = false;
2892 /* Assume that DECL will not be imported into this translation
2893 unit. */
2894 import_p = false;
2896 /* See if the repository tells us whether or not to emit DECL in
2897 this translation unit. */
2898 emit_p = repo_emit_p (decl);
2899 if (emit_p == 0)
2900 import_p = true;
2901 else if (emit_p == 1)
2903 /* The repository indicates that this entity should be defined
2904 here. Make sure the back end honors that request. */
2905 mark_needed (decl);
2906 /* Output the definition as an ordinary strong definition. */
2907 DECL_EXTERNAL (decl) = 0;
2908 DECL_INTERFACE_KNOWN (decl) = 1;
2909 return;
2912 if (import_p)
2913 /* We have already decided what to do with this DECL; there is no
2914 need to check anything further. */
2916 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2918 class_type = DECL_CONTEXT (decl);
2919 import_export_class (class_type);
2920 if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2921 && CLASSTYPE_INTERFACE_ONLY (class_type))
2922 import_p = true;
2923 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2924 && !CLASSTYPE_USE_TEMPLATE (class_type)
2925 && CLASSTYPE_KEY_METHOD (class_type)
2926 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2927 /* The ABI requires that all virtual tables be emitted with
2928 COMDAT linkage. However, on systems where COMDAT symbols
2929 don't show up in the table of contents for a static
2930 archive, or on systems without weak symbols (where we
2931 approximate COMDAT linkage by using internal linkage), the
2932 linker will report errors about undefined symbols because
2933 it will not see the virtual table definition. Therefore,
2934 in the case that we know that the virtual table will be
2935 emitted in only one translation unit, we make the virtual
2936 table an ordinary definition with external linkage. */
2937 DECL_EXTERNAL (decl) = 0;
2938 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2940 /* CLASS_TYPE is being exported from this translation unit,
2941 so DECL should be defined here. */
2942 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2943 /* If a class is declared in a header with the "extern
2944 template" extension, then it will not be instantiated,
2945 even in translation units that would normally require
2946 it. Often such classes are explicitly instantiated in
2947 one translation unit. Therefore, the explicit
2948 instantiation must be made visible to other translation
2949 units. */
2950 DECL_EXTERNAL (decl) = 0;
2951 else
2953 /* The generic C++ ABI says that class data is always
2954 COMDAT, even if there is a key function. Some
2955 variants (e.g., the ARM EABI) says that class data
2956 only has COMDAT linkage if the class data might be
2957 emitted in more than one translation unit. When the
2958 key method can be inline and is inline, we still have
2959 to arrange for comdat even though
2960 class_data_always_comdat is false. */
2961 if (!CLASSTYPE_KEY_METHOD (class_type)
2962 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2963 || targetm.cxx.class_data_always_comdat ())
2965 /* The ABI requires COMDAT linkage. Normally, we
2966 only emit COMDAT things when they are needed;
2967 make sure that we realize that this entity is
2968 indeed needed. */
2969 comdat_p = true;
2970 mark_needed (decl);
2974 else if (!flag_implicit_templates
2975 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2976 import_p = true;
2977 else
2978 comdat_p = true;
2980 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2982 tree type = TREE_TYPE (DECL_NAME (decl));
2983 if (CLASS_TYPE_P (type))
2985 class_type = type;
2986 import_export_class (type);
2987 if (CLASSTYPE_INTERFACE_KNOWN (type)
2988 && TYPE_POLYMORPHIC_P (type)
2989 && CLASSTYPE_INTERFACE_ONLY (type)
2990 /* If -fno-rtti was specified, then we cannot be sure
2991 that RTTI information will be emitted with the
2992 virtual table of the class, so we must emit it
2993 wherever it is used. */
2994 && flag_rtti)
2995 import_p = true;
2996 else
2998 if (CLASSTYPE_INTERFACE_KNOWN (type)
2999 && !CLASSTYPE_INTERFACE_ONLY (type))
3001 comdat_p = (targetm.cxx.class_data_always_comdat ()
3002 || (CLASSTYPE_KEY_METHOD (type)
3003 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
3004 mark_needed (decl);
3005 if (!flag_weak)
3007 comdat_p = false;
3008 DECL_EXTERNAL (decl) = 0;
3011 else
3012 comdat_p = true;
3015 else
3016 comdat_p = true;
3018 else if (DECL_TEMPLOID_INSTANTIATION (decl))
3020 /* DECL is an implicit instantiation of a function or static
3021 data member. */
3022 if ((flag_implicit_templates
3023 && !flag_use_repository)
3024 || (flag_implicit_inline_templates
3025 && TREE_CODE (decl) == FUNCTION_DECL
3026 && DECL_DECLARED_INLINE_P (decl)))
3027 comdat_p = true;
3028 else
3029 /* If we are not implicitly generating templates, then mark
3030 this entity as undefined in this translation unit. */
3031 import_p = true;
3033 else if (DECL_FUNCTION_MEMBER_P (decl))
3035 if (!DECL_DECLARED_INLINE_P (decl))
3037 tree ctype = DECL_CONTEXT (decl);
3038 import_export_class (ctype);
3039 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
3041 DECL_NOT_REALLY_EXTERN (decl)
3042 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
3043 || (DECL_DECLARED_INLINE_P (decl)
3044 && ! flag_implement_inlines
3045 && !DECL_VINDEX (decl)));
3047 if (!DECL_NOT_REALLY_EXTERN (decl))
3048 DECL_EXTERNAL (decl) = 1;
3050 /* Always make artificials weak. */
3051 if (DECL_ARTIFICIAL (decl) && flag_weak)
3052 comdat_p = true;
3053 else
3054 maybe_make_one_only (decl);
3057 else
3058 comdat_p = true;
3060 else
3061 comdat_p = true;
3063 if (import_p)
3065 /* If we are importing DECL into this translation unit, mark is
3066 an undefined here. */
3067 DECL_EXTERNAL (decl) = 1;
3068 DECL_NOT_REALLY_EXTERN (decl) = 0;
3070 else if (comdat_p)
3072 /* If we decided to put DECL in COMDAT, mark it accordingly at
3073 this point. */
3074 comdat_linkage (decl);
3077 DECL_INTERFACE_KNOWN (decl) = 1;
3080 /* Return an expression that performs the destruction of DECL, which
3081 must be a VAR_DECL whose type has a non-trivial destructor, or is
3082 an array whose (innermost) elements have a non-trivial destructor. */
3084 tree
3085 build_cleanup (tree decl)
3087 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
3088 gcc_assert (clean != NULL_TREE);
3089 return clean;
3092 /* Returns the initialization guard variable for the variable DECL,
3093 which has static storage duration. */
3095 tree
3096 get_guard (tree decl)
3098 tree sname;
3099 tree guard;
3101 sname = mangle_guard_variable (decl);
3102 guard = get_global_binding (sname);
3103 if (! guard)
3105 tree guard_type;
3107 /* We use a type that is big enough to contain a mutex as well
3108 as an integer counter. */
3109 guard_type = targetm.cxx.guard_type ();
3110 guard = build_decl (DECL_SOURCE_LOCATION (decl),
3111 VAR_DECL, sname, guard_type);
3113 /* The guard should have the same linkage as what it guards. */
3114 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
3115 TREE_STATIC (guard) = TREE_STATIC (decl);
3116 DECL_COMMON (guard) = DECL_COMMON (decl);
3117 DECL_COMDAT (guard) = DECL_COMDAT (decl);
3118 CP_DECL_THREAD_LOCAL_P (guard) = CP_DECL_THREAD_LOCAL_P (decl);
3119 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
3120 if (DECL_ONE_ONLY (decl))
3121 make_decl_one_only (guard, cxx_comdat_group (guard));
3122 if (TREE_PUBLIC (decl))
3123 DECL_WEAK (guard) = DECL_WEAK (decl);
3124 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
3125 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
3127 DECL_ARTIFICIAL (guard) = 1;
3128 DECL_IGNORED_P (guard) = 1;
3129 TREE_USED (guard) = 1;
3130 pushdecl_top_level_and_finish (guard, NULL_TREE);
3132 return guard;
3135 /* Return an atomic load of src with the appropriate memory model. */
3137 static tree
3138 build_atomic_load_byte (tree src, HOST_WIDE_INT model)
3140 tree ptr_type = build_pointer_type (char_type_node);
3141 tree mem_model = build_int_cst (integer_type_node, model);
3142 tree t, addr, val;
3143 unsigned int size;
3144 int fncode;
3146 size = tree_to_uhwi (TYPE_SIZE_UNIT (char_type_node));
3148 fncode = BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1;
3149 t = builtin_decl_implicit ((enum built_in_function) fncode);
3151 addr = build1 (ADDR_EXPR, ptr_type, src);
3152 val = build_call_expr (t, 2, addr, mem_model);
3153 return val;
3156 /* Return those bits of the GUARD variable that should be set when the
3157 guarded entity is actually initialized. */
3159 static tree
3160 get_guard_bits (tree guard)
3162 if (!targetm.cxx.guard_mask_bit ())
3164 /* We only set the first byte of the guard, in order to leave room
3165 for a mutex in the high-order bits. */
3166 guard = build1 (ADDR_EXPR,
3167 build_pointer_type (TREE_TYPE (guard)),
3168 guard);
3169 guard = build1 (NOP_EXPR,
3170 build_pointer_type (char_type_node),
3171 guard);
3172 guard = build1 (INDIRECT_REF, char_type_node, guard);
3175 return guard;
3178 /* Return an expression which determines whether or not the GUARD
3179 variable has already been initialized. */
3181 tree
3182 get_guard_cond (tree guard, bool thread_safe)
3184 tree guard_value;
3186 if (!thread_safe)
3187 guard = get_guard_bits (guard);
3188 else
3189 guard = build_atomic_load_byte (guard, MEMMODEL_ACQUIRE);
3191 /* Mask off all but the low bit. */
3192 if (targetm.cxx.guard_mask_bit ())
3194 guard_value = integer_one_node;
3195 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3196 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3197 guard = cp_build_binary_op (input_location,
3198 BIT_AND_EXPR, guard, guard_value,
3199 tf_warning_or_error);
3202 guard_value = integer_zero_node;
3203 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3204 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3205 return cp_build_binary_op (input_location,
3206 EQ_EXPR, guard, guard_value,
3207 tf_warning_or_error);
3210 /* Return an expression which sets the GUARD variable, indicating that
3211 the variable being guarded has been initialized. */
3213 tree
3214 set_guard (tree guard)
3216 tree guard_init;
3218 /* Set the GUARD to one. */
3219 guard = get_guard_bits (guard);
3220 guard_init = integer_one_node;
3221 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3222 guard_init = fold_convert (TREE_TYPE (guard), guard_init);
3223 return cp_build_modify_expr (input_location, guard, NOP_EXPR, guard_init,
3224 tf_warning_or_error);
3227 /* Returns true iff we can tell that VAR does not have a dynamic
3228 initializer. */
3230 static bool
3231 var_defined_without_dynamic_init (tree var)
3233 /* If it's defined in another TU, we can't tell. */
3234 if (DECL_EXTERNAL (var))
3235 return false;
3236 /* If it has a non-trivial destructor, registering the destructor
3237 counts as dynamic initialization. */
3238 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3239 return false;
3240 /* If it's in this TU, its initializer has been processed, unless
3241 it's a case of self-initialization, then DECL_INITIALIZED_P is
3242 false while the initializer is handled by finish_id_expression. */
3243 if (!DECL_INITIALIZED_P (var))
3244 return false;
3245 /* If it has no initializer or a constant one, it's not dynamic. */
3246 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3247 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3250 /* Returns true iff VAR is a variable that needs uses to be
3251 wrapped for possible dynamic initialization. */
3253 static bool
3254 var_needs_tls_wrapper (tree var)
3256 return (!error_operand_p (var)
3257 && CP_DECL_THREAD_LOCAL_P (var)
3258 && !DECL_GNU_TLS_P (var)
3259 && !DECL_FUNCTION_SCOPE_P (var)
3260 && !var_defined_without_dynamic_init (var));
3263 /* Get the FUNCTION_DECL for the shared TLS init function for this
3264 translation unit. */
3266 static tree
3267 get_local_tls_init_fn (void)
3269 tree sname = get_identifier ("__tls_init");
3270 tree fn = get_global_binding (sname);
3271 if (!fn)
3273 fn = build_lang_decl (FUNCTION_DECL, sname,
3274 build_function_type (void_type_node,
3275 void_list_node));
3276 SET_DECL_LANGUAGE (fn, lang_c);
3277 TREE_PUBLIC (fn) = false;
3278 DECL_ARTIFICIAL (fn) = true;
3279 mark_used (fn);
3280 set_global_binding (fn);
3282 return fn;
3285 /* Get a FUNCTION_DECL for the init function for the thread_local
3286 variable VAR. The init function will be an alias to the function
3287 that initializes all the non-local TLS variables in the translation
3288 unit. The init function is only used by the wrapper function. */
3290 static tree
3291 get_tls_init_fn (tree var)
3293 /* Only C++11 TLS vars need this init fn. */
3294 if (!var_needs_tls_wrapper (var))
3295 return NULL_TREE;
3297 /* If -fno-extern-tls-init, assume that we don't need to call
3298 a tls init function for a variable defined in another TU. */
3299 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3300 return NULL_TREE;
3302 /* If the variable is internal, or if we can't generate aliases,
3303 call the local init function directly. */
3304 if (!TREE_PUBLIC (var) || !TARGET_SUPPORTS_ALIASES)
3305 return get_local_tls_init_fn ();
3307 tree sname = mangle_tls_init_fn (var);
3308 tree fn = get_global_binding (sname);
3309 if (!fn)
3311 fn = build_lang_decl (FUNCTION_DECL, sname,
3312 build_function_type (void_type_node,
3313 void_list_node));
3314 SET_DECL_LANGUAGE (fn, lang_c);
3315 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3316 DECL_ARTIFICIAL (fn) = true;
3317 DECL_COMDAT (fn) = DECL_COMDAT (var);
3318 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3319 if (DECL_ONE_ONLY (var))
3320 make_decl_one_only (fn, cxx_comdat_group (fn));
3321 if (TREE_PUBLIC (var))
3323 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3324 /* If the variable is defined somewhere else and might have static
3325 initialization, make the init function a weak reference. */
3326 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3327 || TYPE_HAS_CONSTEXPR_CTOR (obtype)
3328 || TYPE_HAS_TRIVIAL_DFLT (obtype))
3329 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3330 && DECL_EXTERNAL (var))
3331 declare_weak (fn);
3332 else
3333 DECL_WEAK (fn) = DECL_WEAK (var);
3335 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3336 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3337 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3338 DECL_IGNORED_P (fn) = 1;
3339 mark_used (fn);
3341 DECL_BEFRIENDING_CLASSES (fn) = var;
3343 set_global_binding (fn);
3345 return fn;
3348 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3349 variable VAR. The wrapper function calls the init function (if any) for
3350 VAR and then returns a reference to VAR. The wrapper function is used
3351 in place of VAR everywhere VAR is mentioned. */
3353 tree
3354 get_tls_wrapper_fn (tree var)
3356 /* Only C++11 TLS vars need this wrapper fn. */
3357 if (!var_needs_tls_wrapper (var))
3358 return NULL_TREE;
3360 tree sname = mangle_tls_wrapper_fn (var);
3361 tree fn = get_global_binding (sname);
3362 if (!fn)
3364 /* A named rvalue reference is an lvalue, so the wrapper should
3365 always return an lvalue reference. */
3366 tree type = non_reference (TREE_TYPE (var));
3367 type = build_reference_type (type);
3368 tree fntype = build_function_type (type, void_list_node);
3369 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3370 SET_DECL_LANGUAGE (fn, lang_c);
3371 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3372 DECL_ARTIFICIAL (fn) = true;
3373 DECL_IGNORED_P (fn) = 1;
3374 /* The wrapper is inline and emitted everywhere var is used. */
3375 DECL_DECLARED_INLINE_P (fn) = true;
3376 if (TREE_PUBLIC (var))
3378 comdat_linkage (fn);
3379 #ifdef HAVE_GAS_HIDDEN
3380 /* Make the wrapper bind locally; there's no reason to share
3381 the wrapper between multiple shared objects. */
3382 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3383 DECL_VISIBILITY_SPECIFIED (fn) = true;
3384 #endif
3386 if (!TREE_PUBLIC (fn))
3387 DECL_INTERFACE_KNOWN (fn) = true;
3388 mark_used (fn);
3389 note_vague_linkage_fn (fn);
3391 #if 0
3392 /* We want CSE to commonize calls to the wrapper, but marking it as
3393 pure is unsafe since it has side-effects. I guess we need a new
3394 ECF flag even weaker than ECF_PURE. FIXME! */
3395 DECL_PURE_P (fn) = true;
3396 #endif
3398 DECL_BEFRIENDING_CLASSES (fn) = var;
3400 set_global_binding (fn);
3402 return fn;
3405 /* At EOF, generate the definition for the TLS wrapper function FN:
3407 T& var_wrapper() {
3408 if (init_fn) init_fn();
3409 return var;
3410 } */
3412 static void
3413 generate_tls_wrapper (tree fn)
3415 tree var = DECL_BEFRIENDING_CLASSES (fn);
3417 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3418 tree body = begin_function_body ();
3419 /* Only call the init fn if there might be one. */
3420 if (tree init_fn = get_tls_init_fn (var))
3422 tree if_stmt = NULL_TREE;
3423 /* If init_fn is a weakref, make sure it exists before calling. */
3424 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3426 if_stmt = begin_if_stmt ();
3427 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3428 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3429 NE_EXPR, addr, nullptr_node,
3430 tf_warning_or_error);
3431 finish_if_stmt_cond (cond, if_stmt);
3433 finish_expr_stmt (build_cxx_call
3434 (init_fn, 0, NULL, tf_warning_or_error));
3435 if (if_stmt)
3437 finish_then_clause (if_stmt);
3438 finish_if_stmt (if_stmt);
3441 else
3442 /* If there's no initialization, the wrapper is a constant function. */
3443 TREE_READONLY (fn) = true;
3444 finish_return_stmt (convert_from_reference (var));
3445 finish_function_body (body);
3446 expand_or_defer_fn (finish_function (/*inline_p=*/false));
3449 /* Start the process of running a particular set of global constructors
3450 or destructors. Subroutine of do_[cd]tors. Also called from
3451 vtv_start_verification_constructor_init_function. */
3453 static tree
3454 start_objects (int method_type, int initp)
3456 tree body;
3457 tree fndecl;
3458 char type[14];
3460 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3462 if (initp != DEFAULT_INIT_PRIORITY)
3464 char joiner;
3466 #ifdef JOINER
3467 joiner = JOINER;
3468 #else
3469 joiner = '_';
3470 #endif
3472 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3474 else
3475 sprintf (type, "sub_%c", method_type);
3477 fndecl = build_lang_decl (FUNCTION_DECL,
3478 get_file_function_name (type),
3479 build_function_type_list (void_type_node,
3480 NULL_TREE));
3481 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3483 TREE_PUBLIC (current_function_decl) = 0;
3485 /* Mark as artificial because it's not explicitly in the user's
3486 source code. */
3487 DECL_ARTIFICIAL (current_function_decl) = 1;
3489 /* Mark this declaration as used to avoid spurious warnings. */
3490 TREE_USED (current_function_decl) = 1;
3492 /* Mark this function as a global constructor or destructor. */
3493 if (method_type == 'I')
3494 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3495 else
3496 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3498 body = begin_compound_stmt (BCS_FN_BODY);
3500 return body;
3503 /* Finish the process of running a particular set of global constructors
3504 or destructors. Subroutine of do_[cd]tors. */
3506 static void
3507 finish_objects (int method_type, int initp, tree body)
3509 tree fn;
3511 /* Finish up. */
3512 finish_compound_stmt (body);
3513 fn = finish_function (/*inline_p=*/false);
3515 if (method_type == 'I')
3517 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3518 decl_init_priority_insert (fn, initp);
3520 else
3522 DECL_STATIC_DESTRUCTOR (fn) = 1;
3523 decl_fini_priority_insert (fn, initp);
3526 expand_or_defer_fn (fn);
3529 /* The names of the parameters to the function created to handle
3530 initializations and destructions for objects with static storage
3531 duration. */
3532 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3533 #define PRIORITY_IDENTIFIER "__priority"
3535 /* The name of the function we create to handle initializations and
3536 destructions for objects with static storage duration. */
3537 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3539 /* The declaration for the __INITIALIZE_P argument. */
3540 static GTY(()) tree initialize_p_decl;
3542 /* The declaration for the __PRIORITY argument. */
3543 static GTY(()) tree priority_decl;
3545 /* The declaration for the static storage duration function. */
3546 static GTY(()) tree ssdf_decl;
3548 /* All the static storage duration functions created in this
3549 translation unit. */
3550 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3552 /* A map from priority levels to information about that priority
3553 level. There may be many such levels, so efficient lookup is
3554 important. */
3555 static splay_tree priority_info_map;
3557 /* Begins the generation of the function that will handle all
3558 initialization and destruction of objects with static storage
3559 duration. The function generated takes two parameters of type
3560 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3561 nonzero, it performs initializations. Otherwise, it performs
3562 destructions. It only performs those initializations or
3563 destructions with the indicated __PRIORITY. The generated function
3564 returns no value.
3566 It is assumed that this function will only be called once per
3567 translation unit. */
3569 static tree
3570 start_static_storage_duration_function (unsigned count)
3572 tree type;
3573 tree body;
3574 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3576 /* Create the identifier for this function. It will be of the form
3577 SSDF_IDENTIFIER_<number>. */
3578 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3580 type = build_function_type_list (void_type_node,
3581 integer_type_node, integer_type_node,
3582 NULL_TREE);
3584 /* Create the FUNCTION_DECL itself. */
3585 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3586 get_identifier (id),
3587 type);
3588 TREE_PUBLIC (ssdf_decl) = 0;
3589 DECL_ARTIFICIAL (ssdf_decl) = 1;
3591 /* Put this function in the list of functions to be called from the
3592 static constructors and destructors. */
3593 if (!ssdf_decls)
3595 vec_alloc (ssdf_decls, 32);
3597 /* Take this opportunity to initialize the map from priority
3598 numbers to information about that priority level. */
3599 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3600 /*delete_key_fn=*/0,
3601 /*delete_value_fn=*/
3602 splay_tree_delete_pointers);
3604 /* We always need to generate functions for the
3605 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3606 priorities later, we'll be sure to find the
3607 DEFAULT_INIT_PRIORITY. */
3608 get_priority_info (DEFAULT_INIT_PRIORITY);
3611 vec_safe_push (ssdf_decls, ssdf_decl);
3613 /* Create the argument list. */
3614 initialize_p_decl = cp_build_parm_decl
3615 (ssdf_decl, get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3616 TREE_USED (initialize_p_decl) = 1;
3617 priority_decl = cp_build_parm_decl
3618 (ssdf_decl, get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3619 TREE_USED (priority_decl) = 1;
3621 DECL_CHAIN (initialize_p_decl) = priority_decl;
3622 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3624 /* Put the function in the global scope. */
3625 pushdecl (ssdf_decl);
3627 /* Start the function itself. This is equivalent to declaring the
3628 function as:
3630 static void __ssdf (int __initialize_p, init __priority_p);
3632 It is static because we only need to call this function from the
3633 various constructor and destructor functions for this module. */
3634 start_preparsed_function (ssdf_decl,
3635 /*attrs=*/NULL_TREE,
3636 SF_PRE_PARSED);
3638 /* Set up the scope of the outermost block in the function. */
3639 body = begin_compound_stmt (BCS_FN_BODY);
3641 return body;
3644 /* Finish the generation of the function which performs initialization
3645 and destruction of objects with static storage duration. After
3646 this point, no more such objects can be created. */
3648 static void
3649 finish_static_storage_duration_function (tree body)
3651 /* Close out the function. */
3652 finish_compound_stmt (body);
3653 expand_or_defer_fn (finish_function (/*inline_p=*/false));
3656 /* Return the information about the indicated PRIORITY level. If no
3657 code to handle this level has yet been generated, generate the
3658 appropriate prologue. */
3660 static priority_info
3661 get_priority_info (int priority)
3663 priority_info pi;
3664 splay_tree_node n;
3666 n = splay_tree_lookup (priority_info_map,
3667 (splay_tree_key) priority);
3668 if (!n)
3670 /* Create a new priority information structure, and insert it
3671 into the map. */
3672 pi = XNEW (struct priority_info_s);
3673 pi->initializations_p = 0;
3674 pi->destructions_p = 0;
3675 splay_tree_insert (priority_info_map,
3676 (splay_tree_key) priority,
3677 (splay_tree_value) pi);
3679 else
3680 pi = (priority_info) n->value;
3682 return pi;
3685 /* The effective initialization priority of a DECL. */
3687 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3688 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3689 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3691 /* Whether a DECL needs a guard to protect it against multiple
3692 initialization. */
3694 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3695 || DECL_ONE_ONLY (decl) \
3696 || DECL_WEAK (decl)))
3698 /* Called from one_static_initialization_or_destruction(),
3699 via walk_tree.
3700 Walks the initializer list of a global variable and looks for
3701 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3702 and that have their DECL_CONTEXT() == NULL.
3703 For each such temporary variable, set their DECL_CONTEXT() to
3704 the current function. This is necessary because otherwise
3705 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3706 when trying to refer to a temporary variable that does not have
3707 it's DECL_CONTECT() properly set. */
3708 static tree
3709 fix_temporary_vars_context_r (tree *node,
3710 int * /*unused*/,
3711 void * /*unused1*/)
3713 gcc_assert (current_function_decl);
3715 if (TREE_CODE (*node) == BIND_EXPR)
3717 tree var;
3719 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3720 if (VAR_P (var)
3721 && !DECL_NAME (var)
3722 && DECL_ARTIFICIAL (var)
3723 && !DECL_CONTEXT (var))
3724 DECL_CONTEXT (var) = current_function_decl;
3727 return NULL_TREE;
3730 /* Set up to handle the initialization or destruction of DECL. If
3731 INITP is nonzero, we are initializing the variable. Otherwise, we
3732 are destroying it. */
3734 static void
3735 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3737 tree guard_if_stmt = NULL_TREE;
3738 tree guard;
3740 /* If we are supposed to destruct and there's a trivial destructor,
3741 nothing has to be done. */
3742 if (!initp
3743 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3744 return;
3746 /* Trick the compiler into thinking we are at the file and line
3747 where DECL was declared so that error-messages make sense, and so
3748 that the debugger will show somewhat sensible file and line
3749 information. */
3750 input_location = DECL_SOURCE_LOCATION (decl);
3752 /* Make sure temporary variables in the initialiser all have
3753 their DECL_CONTEXT() set to a value different from NULL_TREE.
3754 This can happen when global variables initializers are built.
3755 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3756 the temporary variables that might have been generated in the
3757 accompanying initializers is NULL_TREE, meaning the variables have been
3758 declared in the global namespace.
3759 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3760 of the temporaries are set to the current function decl. */
3761 cp_walk_tree_without_duplicates (&init,
3762 fix_temporary_vars_context_r,
3763 NULL);
3765 /* Because of:
3767 [class.access.spec]
3769 Access control for implicit calls to the constructors,
3770 the conversion functions, or the destructor called to
3771 create and destroy a static data member is performed as
3772 if these calls appeared in the scope of the member's
3773 class.
3775 we pretend we are in a static member function of the class of
3776 which the DECL is a member. */
3777 if (member_p (decl))
3779 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3780 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3783 /* Assume we don't need a guard. */
3784 guard = NULL_TREE;
3785 /* We need a guard if this is an object with external linkage that
3786 might be initialized in more than one place. (For example, a
3787 static data member of a template, when the data member requires
3788 construction.) */
3789 if (NEEDS_GUARD_P (decl))
3791 tree guard_cond;
3793 guard = get_guard (decl);
3795 /* When using __cxa_atexit, we just check the GUARD as we would
3796 for a local static. */
3797 if (flag_use_cxa_atexit)
3799 /* When using __cxa_atexit, we never try to destroy
3800 anything from a static destructor. */
3801 gcc_assert (initp);
3802 guard_cond = get_guard_cond (guard, false);
3804 /* If we don't have __cxa_atexit, then we will be running
3805 destructors from .fini sections, or their equivalents. So,
3806 we need to know how many times we've tried to initialize this
3807 object. We do initializations only if the GUARD is zero,
3808 i.e., if we are the first to initialize the variable. We do
3809 destructions only if the GUARD is one, i.e., if we are the
3810 last to destroy the variable. */
3811 else if (initp)
3812 guard_cond
3813 = cp_build_binary_op (input_location,
3814 EQ_EXPR,
3815 cp_build_unary_op (PREINCREMENT_EXPR,
3816 guard,
3817 /*noconvert=*/true,
3818 tf_warning_or_error),
3819 integer_one_node,
3820 tf_warning_or_error);
3821 else
3822 guard_cond
3823 = cp_build_binary_op (input_location,
3824 EQ_EXPR,
3825 cp_build_unary_op (PREDECREMENT_EXPR,
3826 guard,
3827 /*noconvert=*/true,
3828 tf_warning_or_error),
3829 integer_zero_node,
3830 tf_warning_or_error);
3832 guard_if_stmt = begin_if_stmt ();
3833 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3837 /* If we're using __cxa_atexit, we have not already set the GUARD,
3838 so we must do so now. */
3839 if (guard && initp && flag_use_cxa_atexit)
3840 finish_expr_stmt (set_guard (guard));
3842 /* Perform the initialization or destruction. */
3843 if (initp)
3845 if (init)
3847 finish_expr_stmt (init);
3848 if (sanitize_flags_p (SANITIZE_ADDRESS, decl))
3850 varpool_node *vnode = varpool_node::get (decl);
3851 if (vnode)
3852 vnode->dynamically_initialized = 1;
3856 /* If we're using __cxa_atexit, register a function that calls the
3857 destructor for the object. */
3858 if (flag_use_cxa_atexit)
3859 finish_expr_stmt (register_dtor_fn (decl));
3861 else
3862 finish_expr_stmt (build_cleanup (decl));
3864 /* Finish the guard if-stmt, if necessary. */
3865 if (guard)
3867 finish_then_clause (guard_if_stmt);
3868 finish_if_stmt (guard_if_stmt);
3871 /* Now that we're done with DECL we don't need to pretend to be a
3872 member of its class any longer. */
3873 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3874 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3877 /* Generate code to do the initialization or destruction of the decls in VARS,
3878 a TREE_LIST of VAR_DECL with static storage duration.
3879 Whether initialization or destruction is performed is specified by INITP. */
3881 static void
3882 do_static_initialization_or_destruction (tree vars, bool initp)
3884 tree node, init_if_stmt, cond;
3886 /* Build the outer if-stmt to check for initialization or destruction. */
3887 init_if_stmt = begin_if_stmt ();
3888 cond = initp ? integer_one_node : integer_zero_node;
3889 cond = cp_build_binary_op (input_location,
3890 EQ_EXPR,
3891 initialize_p_decl,
3892 cond,
3893 tf_warning_or_error);
3894 finish_if_stmt_cond (cond, init_if_stmt);
3896 /* To make sure dynamic construction doesn't access globals from other
3897 compilation units where they might not be yet constructed, for
3898 -fsanitize=address insert __asan_before_dynamic_init call that
3899 prevents access to either all global variables that need construction
3900 in other compilation units, or at least those that haven't been
3901 initialized yet. Variables that need dynamic construction in
3902 the current compilation unit are kept accessible. */
3903 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
3904 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3906 node = vars;
3907 do {
3908 tree decl = TREE_VALUE (node);
3909 tree priority_if_stmt;
3910 int priority;
3911 priority_info pi;
3913 /* If we don't need a destructor, there's nothing to do. Avoid
3914 creating a possibly empty if-stmt. */
3915 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3917 node = TREE_CHAIN (node);
3918 continue;
3921 /* Remember that we had an initialization or finalization at this
3922 priority. */
3923 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3924 pi = get_priority_info (priority);
3925 if (initp)
3926 pi->initializations_p = 1;
3927 else
3928 pi->destructions_p = 1;
3930 /* Conditionalize this initialization on being in the right priority
3931 and being initializing/finalizing appropriately. */
3932 priority_if_stmt = begin_if_stmt ();
3933 cond = cp_build_binary_op (input_location,
3934 EQ_EXPR,
3935 priority_decl,
3936 build_int_cst (NULL_TREE, priority),
3937 tf_warning_or_error);
3938 finish_if_stmt_cond (cond, priority_if_stmt);
3940 /* Process initializers with same priority. */
3941 for (; node
3942 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3943 node = TREE_CHAIN (node))
3944 /* Do one initialization or destruction. */
3945 one_static_initialization_or_destruction (TREE_VALUE (node),
3946 TREE_PURPOSE (node), initp);
3948 /* Finish up the priority if-stmt body. */
3949 finish_then_clause (priority_if_stmt);
3950 finish_if_stmt (priority_if_stmt);
3952 } while (node);
3954 /* Revert what __asan_before_dynamic_init did by calling
3955 __asan_after_dynamic_init. */
3956 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
3957 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3959 /* Finish up the init/destruct if-stmt body. */
3960 finish_then_clause (init_if_stmt);
3961 finish_if_stmt (init_if_stmt);
3964 /* VARS is a list of variables with static storage duration which may
3965 need initialization and/or finalization. Remove those variables
3966 that don't really need to be initialized or finalized, and return
3967 the resulting list. The order in which the variables appear in
3968 VARS is in reverse order of the order in which they should actually
3969 be initialized. The list we return is in the unreversed order;
3970 i.e., the first variable should be initialized first. */
3972 static tree
3973 prune_vars_needing_no_initialization (tree *vars)
3975 tree *var = vars;
3976 tree result = NULL_TREE;
3978 while (*var)
3980 tree t = *var;
3981 tree decl = TREE_VALUE (t);
3982 tree init = TREE_PURPOSE (t);
3984 /* Deal gracefully with error. */
3985 if (error_operand_p (decl))
3987 var = &TREE_CHAIN (t);
3988 continue;
3991 /* The only things that can be initialized are variables. */
3992 gcc_assert (VAR_P (decl));
3994 /* If this object is not defined, we don't need to do anything
3995 here. */
3996 if (DECL_EXTERNAL (decl))
3998 var = &TREE_CHAIN (t);
3999 continue;
4002 /* Also, if the initializer already contains errors, we can bail
4003 out now. */
4004 if (init && TREE_CODE (init) == TREE_LIST
4005 && value_member (error_mark_node, init))
4007 var = &TREE_CHAIN (t);
4008 continue;
4011 /* This variable is going to need initialization and/or
4012 finalization, so we add it to the list. */
4013 *var = TREE_CHAIN (t);
4014 TREE_CHAIN (t) = result;
4015 result = t;
4018 return result;
4021 /* Make sure we have told the back end about all the variables in
4022 VARS. */
4024 static void
4025 write_out_vars (tree vars)
4027 tree v;
4029 for (v = vars; v; v = TREE_CHAIN (v))
4031 tree var = TREE_VALUE (v);
4032 if (!var_finalized_p (var))
4034 import_export_decl (var);
4035 rest_of_decl_compilation (var, 1, 1);
4040 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
4041 (otherwise) that will initialize all global objects with static
4042 storage duration having the indicated PRIORITY. */
4044 static void
4045 generate_ctor_or_dtor_function (bool constructor_p, int priority,
4046 location_t *locus)
4048 char function_key;
4049 tree fndecl;
4050 tree body;
4051 size_t i;
4053 input_location = *locus;
4054 /* ??? */
4055 /* Was: locus->line++; */
4057 /* We use `I' to indicate initialization and `D' to indicate
4058 destruction. */
4059 function_key = constructor_p ? 'I' : 'D';
4061 /* We emit the function lazily, to avoid generating empty
4062 global constructors and destructors. */
4063 body = NULL_TREE;
4065 /* For Objective-C++, we may need to initialize metadata found in this module.
4066 This must be done _before_ any other static initializations. */
4067 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
4068 && constructor_p && objc_static_init_needed_p ())
4070 body = start_objects (function_key, priority);
4071 objc_generate_static_init_call (NULL_TREE);
4074 /* Call the static storage duration function with appropriate
4075 arguments. */
4076 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
4078 /* Calls to pure or const functions will expand to nothing. */
4079 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
4081 tree call;
4083 if (! body)
4084 body = start_objects (function_key, priority);
4086 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
4087 build_int_cst (NULL_TREE,
4088 constructor_p),
4089 build_int_cst (NULL_TREE,
4090 priority),
4091 NULL_TREE);
4092 finish_expr_stmt (call);
4096 /* Close out the function. */
4097 if (body)
4098 finish_objects (function_key, priority, body);
4101 /* Generate constructor and destructor functions for the priority
4102 indicated by N. */
4104 static int
4105 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
4107 location_t *locus = (location_t *) data;
4108 int priority = (int) n->key;
4109 priority_info pi = (priority_info) n->value;
4111 /* Generate the functions themselves, but only if they are really
4112 needed. */
4113 if (pi->initializations_p)
4114 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
4115 if (pi->destructions_p)
4116 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
4118 /* Keep iterating. */
4119 return 0;
4122 /* Return C++ property of T, based on given operation OP. */
4124 static int
4125 cpp_check (tree t, cpp_operation op)
4127 switch (op)
4129 case HAS_DEPENDENT_TEMPLATE_ARGS:
4131 tree ti = CLASSTYPE_TEMPLATE_INFO (t);
4132 if (!ti)
4133 return 0;
4134 ++processing_template_decl;
4135 const bool dep = any_dependent_template_arguments_p (TI_ARGS (ti));
4136 --processing_template_decl;
4137 return dep;
4139 case IS_ABSTRACT:
4140 return DECL_PURE_VIRTUAL_P (t);
4141 case IS_CONSTRUCTOR:
4142 return DECL_CONSTRUCTOR_P (t);
4143 case IS_DESTRUCTOR:
4144 return DECL_DESTRUCTOR_P (t);
4145 case IS_COPY_CONSTRUCTOR:
4146 return DECL_COPY_CONSTRUCTOR_P (t);
4147 case IS_MOVE_CONSTRUCTOR:
4148 return DECL_MOVE_CONSTRUCTOR_P (t);
4149 case IS_TEMPLATE:
4150 return TREE_CODE (t) == TEMPLATE_DECL;
4151 case IS_TRIVIAL:
4152 return trivial_type_p (t);
4153 default:
4154 return 0;
4158 /* Collect source file references recursively, starting from NAMESPC. */
4160 static void
4161 collect_source_refs (tree namespc)
4163 /* Iterate over names in this name space. */
4164 for (tree t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4165 if (DECL_IS_BUILTIN (t))
4167 else if (TREE_CODE (t) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (t))
4168 collect_source_refs (t);
4169 else
4170 collect_source_ref (DECL_SOURCE_FILE (t));
4173 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4174 starting from NAMESPC. */
4176 static void
4177 collect_ada_namespace (tree namespc, const char *source_file)
4179 tree decl = NAMESPACE_LEVEL (namespc)->names;
4181 /* Collect decls from this namespace. This will skip
4182 NAMESPACE_DECLs (both aliases and regular, it cannot tell). */
4183 collect_ada_nodes (decl, source_file);
4185 /* Now scan for namespace children, and dump them. */
4186 for (; decl; decl = TREE_CHAIN (decl))
4187 if (TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl))
4188 collect_ada_namespace (decl, source_file);
4191 /* Returns true iff there is a definition available for variable or
4192 function DECL. */
4194 bool
4195 decl_defined_p (tree decl)
4197 if (TREE_CODE (decl) == FUNCTION_DECL)
4198 return (DECL_INITIAL (decl) != NULL_TREE
4199 /* A pending instantiation of a friend temploid is defined. */
4200 || (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
4201 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4202 (DECL_TI_TEMPLATE (decl)))));
4203 else
4205 gcc_assert (VAR_P (decl));
4206 return !DECL_EXTERNAL (decl);
4210 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4212 [expr.const]
4214 An integral constant-expression can only involve ... const
4215 variables of integral or enumeration types initialized with
4216 constant expressions ...
4218 C++0x also allows constexpr variables and temporaries initialized
4219 with constant expressions. We handle the former here, but the latter
4220 are just folded away in cxx_eval_constant_expression.
4222 The standard does not require that the expression be non-volatile.
4223 G++ implements the proposed correction in DR 457. */
4225 bool
4226 decl_constant_var_p (tree decl)
4228 if (!decl_maybe_constant_var_p (decl))
4229 return false;
4231 /* We don't know if a template static data member is initialized with
4232 a constant expression until we instantiate its initializer. Even
4233 in the case of a constexpr variable, we can't treat it as a
4234 constant until its initializer is complete in case it's used in
4235 its own initializer. */
4236 maybe_instantiate_decl (decl);
4237 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4240 /* Returns true if DECL could be a symbolic constant variable, depending on
4241 its initializer. */
4243 bool
4244 decl_maybe_constant_var_p (tree decl)
4246 tree type = TREE_TYPE (decl);
4247 if (!VAR_P (decl))
4248 return false;
4249 if (DECL_DECLARED_CONSTEXPR_P (decl))
4250 return true;
4251 if (DECL_HAS_VALUE_EXPR_P (decl))
4252 /* A proxy isn't constant. */
4253 return false;
4254 if (TYPE_REF_P (type))
4255 /* References can be constant. */;
4256 else if (CP_TYPE_CONST_NON_VOLATILE_P (type)
4257 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4258 /* And const integers. */;
4259 else
4260 return false;
4262 if (DECL_INITIAL (decl)
4263 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
4264 /* We know the initializer, and it isn't constant. */
4265 return false;
4266 else
4267 return true;
4270 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4271 called from grokfndecl and grokvardecl; in all modes it is called from
4272 cp_write_global_declarations. */
4274 void
4275 no_linkage_error (tree decl)
4277 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4278 /* In C++11 it's ok if the decl is defined. */
4279 return;
4280 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4281 if (t == NULL_TREE)
4282 /* The type that got us on no_linkage_decls must have gotten a name for
4283 linkage purposes. */;
4284 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4285 /* The type might end up having a typedef name for linkage purposes. */
4286 vec_safe_push (no_linkage_decls, decl);
4287 else if (TYPE_UNNAMED_P (t))
4289 bool d = false;
4290 if (cxx_dialect >= cxx11)
4291 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4292 "unnamed type, is used but never defined", decl);
4293 else if (DECL_EXTERN_C_P (decl))
4294 /* Allow this; it's pretty common in C. */;
4295 else if (VAR_P (decl))
4296 /* DRs 132, 319 and 389 seem to indicate types with
4297 no linkage can only be used to declare extern "C"
4298 entities. Since it's not always an error in the
4299 ISO C++ 90 Standard, we only issue a warning. */
4300 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "unnamed type "
4301 "with no linkage used to declare variable %q#D with "
4302 "linkage", decl);
4303 else
4304 d = permerror (DECL_SOURCE_LOCATION (decl), "unnamed type with no "
4305 "linkage used to declare function %q#D with linkage",
4306 decl);
4307 if (d && is_typedef_decl (TYPE_NAME (t)))
4308 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4309 "to the unqualified type, so it is not used for linkage",
4310 TYPE_NAME (t));
4312 else if (cxx_dialect >= cxx11)
4314 if (VAR_P (decl) || !DECL_PURE_VIRTUAL_P (decl))
4315 permerror (DECL_SOURCE_LOCATION (decl),
4316 "%q#D, declared using local type "
4317 "%qT, is used but never defined", decl, t);
4319 else if (VAR_P (decl))
4320 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4321 "used to declare variable %q#D with linkage", t, decl);
4322 else
4323 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4324 "to declare function %q#D with linkage", t, decl);
4327 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4329 static void
4330 collect_all_refs (const char *source_file)
4332 collect_ada_namespace (global_namespace, source_file);
4335 /* Clear DECL_EXTERNAL for NODE. */
4337 static bool
4338 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4340 DECL_EXTERNAL (node->decl) = 0;
4341 return false;
4344 /* Build up the function to run dynamic initializers for thread_local
4345 variables in this translation unit and alias the init functions for the
4346 individual variables to it. */
4348 static void
4349 handle_tls_init (void)
4351 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4352 if (vars == NULL_TREE)
4353 return;
4355 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4357 write_out_vars (vars);
4359 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4360 boolean_type_node);
4361 TREE_PUBLIC (guard) = false;
4362 TREE_STATIC (guard) = true;
4363 DECL_ARTIFICIAL (guard) = true;
4364 DECL_IGNORED_P (guard) = true;
4365 TREE_USED (guard) = true;
4366 CP_DECL_THREAD_LOCAL_P (guard) = true;
4367 set_decl_tls_model (guard, decl_default_tls_model (guard));
4368 pushdecl_top_level_and_finish (guard, NULL_TREE);
4370 tree fn = get_local_tls_init_fn ();
4371 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4372 tree body = begin_function_body ();
4373 tree if_stmt = begin_if_stmt ();
4374 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4375 tf_warning_or_error);
4376 finish_if_stmt_cond (cond, if_stmt);
4377 finish_expr_stmt (cp_build_modify_expr (loc, guard, NOP_EXPR,
4378 boolean_true_node,
4379 tf_warning_or_error));
4380 for (; vars; vars = TREE_CHAIN (vars))
4382 tree var = TREE_VALUE (vars);
4383 tree init = TREE_PURPOSE (vars);
4384 one_static_initialization_or_destruction (var, init, true);
4386 /* Output init aliases even with -fno-extern-tls-init. */
4387 if (TARGET_SUPPORTS_ALIASES && TREE_PUBLIC (var))
4389 tree single_init_fn = get_tls_init_fn (var);
4390 if (single_init_fn == NULL_TREE)
4391 continue;
4392 cgraph_node *alias
4393 = cgraph_node::get_create (fn)->create_same_body_alias
4394 (single_init_fn, fn);
4395 gcc_assert (alias != NULL);
4399 finish_then_clause (if_stmt);
4400 finish_if_stmt (if_stmt);
4401 finish_function_body (body);
4402 expand_or_defer_fn (finish_function (/*inline_p=*/false));
4405 /* We're at the end of compilation, so generate any mangling aliases that
4406 we've been saving up, if DECL is going to be output and ID2 isn't
4407 already taken by another declaration. */
4409 static void
4410 generate_mangling_alias (tree decl, tree id2)
4412 struct cgraph_node *n = NULL;
4414 if (TREE_CODE (decl) == FUNCTION_DECL)
4416 n = cgraph_node::get (decl);
4417 if (!n)
4418 /* Don't create an alias to an unreferenced function. */
4419 return;
4422 tree *slot
4423 = mangled_decls->find_slot_with_hash (id2, IDENTIFIER_HASH_VALUE (id2),
4424 INSERT);
4426 /* If there's a declaration already using this mangled name,
4427 don't create a compatibility alias that conflicts. */
4428 if (*slot)
4429 return;
4431 tree alias = make_alias_for (decl, id2);
4432 *slot = alias;
4434 DECL_IGNORED_P (alias) = 1;
4435 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
4436 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
4437 if (vague_linkage_p (decl))
4438 DECL_WEAK (alias) = 1;
4440 if (n)
4441 n->create_same_body_alias (alias, decl);
4442 else
4443 varpool_node::create_extra_name_alias (alias, decl);
4446 /* Note that we might want to emit an alias with the symbol ID2 for DECL at
4447 the end of translation, for compatibility across bugs in the mangling
4448 implementation. */
4450 void
4451 note_mangling_alias (tree decl, tree id2)
4453 if (TARGET_SUPPORTS_ALIASES)
4455 if (!defer_mangling_aliases)
4456 generate_mangling_alias (decl, id2);
4457 else
4459 vec_safe_push (mangling_aliases, decl);
4460 vec_safe_push (mangling_aliases, id2);
4465 /* Emit all mangling aliases that were deferred up to this point. */
4467 void
4468 generate_mangling_aliases ()
4470 while (!vec_safe_is_empty (mangling_aliases))
4472 tree id2 = mangling_aliases->pop();
4473 tree decl = mangling_aliases->pop();
4474 generate_mangling_alias (decl, id2);
4476 defer_mangling_aliases = false;
4479 /* Record a mangling of DECL, whose DECL_ASSEMBLER_NAME has just been
4480 set. NEED_WARNING is true if we must warn about collisions. We do
4481 this to spot changes in mangling that may require compatibility
4482 aliases. */
4484 void
4485 record_mangling (tree decl, bool need_warning)
4487 if (!mangled_decls)
4488 mangled_decls = hash_table<mangled_decl_hash>::create_ggc (499);
4490 gcc_checking_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
4491 tree id = DECL_ASSEMBLER_NAME_RAW (decl);
4492 tree *slot
4493 = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id),
4494 INSERT);
4496 /* If this is already an alias, remove the alias, because the real
4497 decl takes precedence. */
4498 if (*slot && DECL_ARTIFICIAL (*slot) && DECL_IGNORED_P (*slot))
4499 if (symtab_node *n = symtab_node::get (*slot))
4500 if (n->cpp_implicit_alias)
4502 n->remove ();
4503 *slot = NULL_TREE;
4506 if (!*slot)
4507 *slot = decl;
4508 else if (need_warning)
4510 error_at (DECL_SOURCE_LOCATION (decl),
4511 "mangling of %q#D as %qE conflicts with a previous mangle",
4512 decl, id);
4513 inform (DECL_SOURCE_LOCATION (*slot),
4514 "previous mangling %q#D", *slot);
4515 inform (DECL_SOURCE_LOCATION (decl),
4516 "a later -fabi-version= (or =0)"
4517 " avoids this error with a change in mangling");
4518 *slot = decl;
4522 /* The mangled name of DECL is being forcibly changed to NAME. Remove
4523 any existing knowledge of DECL's mangled name meaning DECL. */
4525 void
4526 overwrite_mangling (tree decl, tree name)
4528 if (tree id = DECL_ASSEMBLER_NAME_RAW (decl))
4529 if ((TREE_CODE (decl) == VAR_DECL
4530 || TREE_CODE (decl) == FUNCTION_DECL)
4531 && mangled_decls)
4532 if (tree *slot
4533 = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id),
4534 NO_INSERT))
4535 if (*slot == decl)
4537 mangled_decls->clear_slot (slot);
4539 /* If this is an alias, remove it from the symbol table. */
4540 if (DECL_ARTIFICIAL (decl) && DECL_IGNORED_P (decl))
4541 if (symtab_node *n = symtab_node::get (decl))
4542 if (n->cpp_implicit_alias)
4543 n->remove ();
4546 DECL_ASSEMBLER_NAME_RAW (decl) = name;
4549 /* The entire file is now complete. If requested, dump everything
4550 to a file. */
4552 static void
4553 dump_tu (void)
4555 dump_flags_t flags;
4556 if (FILE *stream = dump_begin (raw_dump_id, &flags))
4558 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4559 dump_end (raw_dump_id, stream);
4563 static location_t locus_at_end_of_parsing;
4565 /* Check the deallocation functions for CODE to see if we want to warn that
4566 only one was defined. */
4568 static void
4569 maybe_warn_sized_delete (enum tree_code code)
4571 tree sized = NULL_TREE;
4572 tree unsized = NULL_TREE;
4574 for (ovl_iterator iter (get_global_binding (ovl_op_identifier (false, code)));
4575 iter; ++iter)
4577 tree fn = *iter;
4578 /* We're only interested in usual deallocation functions. */
4579 if (!usual_deallocation_fn_p (fn))
4580 continue;
4581 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
4582 unsized = fn;
4583 else
4584 sized = fn;
4586 if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
4587 warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
4588 "the program should also define %qD", sized);
4589 else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
4590 warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
4591 "the program should also define %qD", unsized);
4594 /* Check the global deallocation functions to see if we want to warn about
4595 defining unsized without sized (or vice versa). */
4597 static void
4598 maybe_warn_sized_delete ()
4600 if (!flag_sized_deallocation || !warn_sized_deallocation)
4601 return;
4602 maybe_warn_sized_delete (DELETE_EXPR);
4603 maybe_warn_sized_delete (VEC_DELETE_EXPR);
4606 /* Earlier we left PTRMEM_CST in variable initializers alone so that we could
4607 look them up when evaluating non-type template parameters. Now we need to
4608 lower them to something the back end can understand. */
4610 static void
4611 lower_var_init ()
4613 varpool_node *node;
4614 FOR_EACH_VARIABLE (node)
4616 tree d = node->decl;
4617 if (tree init = DECL_INITIAL (d))
4618 DECL_INITIAL (d) = cplus_expand_constant (init);
4622 /* This routine is called at the end of compilation.
4623 Its job is to create all the code needed to initialize and
4624 destroy the global aggregates. We do the destruction
4625 first, since that way we only need to reverse the decls once. */
4627 void
4628 c_parse_final_cleanups (void)
4630 tree vars;
4631 bool reconsider;
4632 size_t i;
4633 unsigned ssdf_count = 0;
4634 int retries = 0;
4635 tree decl;
4637 locus_at_end_of_parsing = input_location;
4638 at_eof = 1;
4640 /* Bad parse errors. Just forget about it. */
4641 if (! global_bindings_p () || current_class_type
4642 || !vec_safe_is_empty (decl_namespace_list))
4643 return;
4645 /* This is the point to write out a PCH if we're doing that.
4646 In that case we do not want to do anything else. */
4647 if (pch_file)
4649 /* Mangle all symbols at PCH creation time. */
4650 symtab_node *node;
4651 FOR_EACH_SYMBOL (node)
4652 if (! is_a <varpool_node *> (node)
4653 || ! DECL_HARD_REGISTER (node->decl))
4654 DECL_ASSEMBLER_NAME (node->decl);
4655 c_common_write_pch ();
4656 dump_tu ();
4657 /* Ensure even the callers don't try to finalize the CU. */
4658 flag_syntax_only = 1;
4659 return;
4662 timevar_stop (TV_PHASE_PARSING);
4663 timevar_start (TV_PHASE_DEFERRED);
4665 symtab->process_same_body_aliases ();
4667 /* Handle -fdump-ada-spec[-slim] */
4668 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4670 if (flag_dump_ada_spec_slim)
4671 collect_source_ref (main_input_filename);
4672 else
4673 collect_source_refs (global_namespace);
4675 dump_ada_specs (collect_all_refs, cpp_check);
4678 /* FIXME - huh? was input_line -= 1;*/
4680 /* We now have to write out all the stuff we put off writing out.
4681 These include:
4683 o Template specializations that we have not yet instantiated,
4684 but which are needed.
4685 o Initialization and destruction for non-local objects with
4686 static storage duration. (Local objects with static storage
4687 duration are initialized when their scope is first entered,
4688 and are cleaned up via atexit.)
4689 o Virtual function tables.
4691 All of these may cause others to be needed. For example,
4692 instantiating one function may cause another to be needed, and
4693 generating the initializer for an object may cause templates to be
4694 instantiated, etc., etc. */
4696 emit_support_tinfos ();
4700 tree t;
4701 tree decl;
4703 reconsider = false;
4705 /* If there are templates that we've put off instantiating, do
4706 them now. */
4707 instantiate_pending_templates (retries);
4708 ggc_collect ();
4710 /* Write out virtual tables as required. Writing out the
4711 virtual table for a template class may cause the
4712 instantiation of members of that class. If we write out
4713 vtables then we remove the class from our list so we don't
4714 have to look at it again. */
4715 for (i = keyed_classes->length ();
4716 keyed_classes->iterate (--i, &t);)
4717 if (maybe_emit_vtables (t))
4719 reconsider = true;
4720 keyed_classes->unordered_remove (i);
4722 /* The input_location may have been changed during marking of
4723 vtable entries. */
4724 input_location = locus_at_end_of_parsing;
4726 /* Write out needed type info variables. We have to be careful
4727 looping through unemitted decls, because emit_tinfo_decl may
4728 cause other variables to be needed. New elements will be
4729 appended, and we remove from the vector those that actually
4730 get emitted. */
4731 for (i = unemitted_tinfo_decls->length ();
4732 unemitted_tinfo_decls->iterate (--i, &t);)
4733 if (emit_tinfo_decl (t))
4735 reconsider = true;
4736 unemitted_tinfo_decls->unordered_remove (i);
4739 /* The list of objects with static storage duration is built up
4740 in reverse order. We clear STATIC_AGGREGATES so that any new
4741 aggregates added during the initialization of these will be
4742 initialized in the correct order when we next come around the
4743 loop. */
4744 vars = prune_vars_needing_no_initialization (&static_aggregates);
4746 if (vars)
4748 /* We need to start a new initialization function each time
4749 through the loop. That's because we need to know which
4750 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4751 isn't computed until a function is finished, and written
4752 out. That's a deficiency in the back end. When this is
4753 fixed, these initialization functions could all become
4754 inline, with resulting performance improvements. */
4755 tree ssdf_body;
4757 /* Make sure the back end knows about all the variables. */
4758 write_out_vars (vars);
4760 /* Set the line and file, so that it is obviously not from
4761 the source file. */
4762 input_location = locus_at_end_of_parsing;
4763 ssdf_body = start_static_storage_duration_function (ssdf_count);
4765 /* First generate code to do all the initializations. */
4766 if (vars)
4767 do_static_initialization_or_destruction (vars, /*initp=*/true);
4769 /* Then, generate code to do all the destructions. Do these
4770 in reverse order so that the most recently constructed
4771 variable is the first destroyed. If we're using
4772 __cxa_atexit, then we don't need to do this; functions
4773 were registered at initialization time to destroy the
4774 local statics. */
4775 if (!flag_use_cxa_atexit && vars)
4777 vars = nreverse (vars);
4778 do_static_initialization_or_destruction (vars, /*initp=*/false);
4780 else
4781 vars = NULL_TREE;
4783 /* Finish up the static storage duration function for this
4784 round. */
4785 input_location = locus_at_end_of_parsing;
4786 finish_static_storage_duration_function (ssdf_body);
4788 /* All those initializations and finalizations might cause
4789 us to need more inline functions, more template
4790 instantiations, etc. */
4791 reconsider = true;
4792 ssdf_count++;
4793 /* ??? was: locus_at_end_of_parsing.line++; */
4796 /* Now do the same for thread_local variables. */
4797 handle_tls_init ();
4799 /* Go through the set of inline functions whose bodies have not
4800 been emitted yet. If out-of-line copies of these functions
4801 are required, emit them. */
4802 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4804 /* Does it need synthesizing? */
4805 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4806 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4808 /* Even though we're already at the top-level, we push
4809 there again. That way, when we pop back a few lines
4810 hence, all of our state is restored. Otherwise,
4811 finish_function doesn't clean things up, and we end
4812 up with CURRENT_FUNCTION_DECL set. */
4813 push_to_top_level ();
4814 /* The decl's location will mark where it was first
4815 needed. Save that so synthesize method can indicate
4816 where it was needed from, in case of error */
4817 input_location = DECL_SOURCE_LOCATION (decl);
4818 synthesize_method (decl);
4819 pop_from_top_level ();
4820 reconsider = true;
4823 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4824 generate_tls_wrapper (decl);
4826 if (!DECL_SAVED_TREE (decl))
4827 continue;
4829 cgraph_node *node = cgraph_node::get_create (decl);
4831 /* We lie to the back end, pretending that some functions
4832 are not defined when they really are. This keeps these
4833 functions from being put out unnecessarily. But, we must
4834 stop lying when the functions are referenced, or if they
4835 are not comdat since they need to be put out now. If
4836 DECL_INTERFACE_KNOWN, then we have already set
4837 DECL_EXTERNAL appropriately, so there's no need to check
4838 again, and we do not want to clear DECL_EXTERNAL if a
4839 previous call to import_export_decl set it.
4841 This is done in a separate for cycle, because if some
4842 deferred function is contained in another deferred
4843 function later in deferred_fns varray,
4844 rest_of_compilation would skip this function and we
4845 really cannot expand the same function twice. */
4846 import_export_decl (decl);
4847 if (DECL_NOT_REALLY_EXTERN (decl)
4848 && DECL_INITIAL (decl)
4849 && decl_needed_p (decl))
4851 if (node->cpp_implicit_alias)
4852 node = node->get_alias_target ();
4854 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
4855 NULL, true);
4856 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4857 group, we need to mark all symbols in the same comdat group
4858 that way. */
4859 if (node->same_comdat_group)
4860 for (cgraph_node *next
4861 = dyn_cast<cgraph_node *> (node->same_comdat_group);
4862 next != node;
4863 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
4864 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
4865 NULL, true);
4868 /* If we're going to need to write this function out, and
4869 there's already a body for it, create RTL for it now.
4870 (There might be no body if this is a method we haven't
4871 gotten around to synthesizing yet.) */
4872 if (!DECL_EXTERNAL (decl)
4873 && decl_needed_p (decl)
4874 && !TREE_ASM_WRITTEN (decl)
4875 && !node->definition)
4877 /* We will output the function; no longer consider it in this
4878 loop. */
4879 DECL_DEFER_OUTPUT (decl) = 0;
4880 /* Generate RTL for this function now that we know we
4881 need it. */
4882 expand_or_defer_fn (decl);
4883 /* If we're compiling -fsyntax-only pretend that this
4884 function has been written out so that we don't try to
4885 expand it again. */
4886 if (flag_syntax_only)
4887 TREE_ASM_WRITTEN (decl) = 1;
4888 reconsider = true;
4892 if (wrapup_namespace_globals ())
4893 reconsider = true;
4895 /* Static data members are just like namespace-scope globals. */
4896 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4898 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4899 /* Don't write it out if we haven't seen a definition. */
4900 || (DECL_IN_AGGR_P (decl) && !DECL_INLINE_VAR_P (decl)))
4901 continue;
4902 import_export_decl (decl);
4903 /* If this static data member is needed, provide it to the
4904 back end. */
4905 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4906 DECL_EXTERNAL (decl) = 0;
4908 if (vec_safe_length (pending_statics) != 0
4909 && wrapup_global_declarations (pending_statics->address (),
4910 pending_statics->length ()))
4911 reconsider = true;
4913 retries++;
4915 while (reconsider);
4917 lower_var_init ();
4919 generate_mangling_aliases ();
4921 /* All used inline functions must have a definition at this point. */
4922 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4924 if (/* Check online inline functions that were actually used. */
4925 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4926 /* If the definition actually was available here, then the
4927 fact that the function was not defined merely represents
4928 that for some reason (use of a template repository,
4929 #pragma interface, etc.) we decided not to emit the
4930 definition here. */
4931 && !DECL_INITIAL (decl)
4932 /* Don't complain if the template was defined. */
4933 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4934 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4935 (template_for_substitution (decl)))))
4937 warning_at (DECL_SOURCE_LOCATION (decl), 0,
4938 "inline function %qD used but never defined", decl);
4939 /* Avoid a duplicate warning from check_global_declaration. */
4940 TREE_NO_WARNING (decl) = 1;
4944 /* So must decls that use a type with no linkage. */
4945 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4946 no_linkage_error (decl);
4948 maybe_warn_sized_delete ();
4950 /* Then, do the Objective-C stuff. This is where all the
4951 Objective-C module stuff gets generated (symtab,
4952 class/protocol/selector lists etc). This must be done after C++
4953 templates, destructors etc. so that selectors used in C++
4954 templates are properly allocated. */
4955 if (c_dialect_objc ())
4956 objc_write_global_declarations ();
4958 /* We give C linkage to static constructors and destructors. */
4959 push_lang_context (lang_name_c);
4961 /* Generate initialization and destruction functions for all
4962 priorities for which they are required. */
4963 if (priority_info_map)
4964 splay_tree_foreach (priority_info_map,
4965 generate_ctor_and_dtor_functions_for_priority,
4966 /*data=*/&locus_at_end_of_parsing);
4967 else if (c_dialect_objc () && objc_static_init_needed_p ())
4968 /* If this is obj-c++ and we need a static init, call
4969 generate_ctor_or_dtor_function. */
4970 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4971 DEFAULT_INIT_PRIORITY,
4972 &locus_at_end_of_parsing);
4974 /* We're done with the splay-tree now. */
4975 if (priority_info_map)
4976 splay_tree_delete (priority_info_map);
4978 /* Generate any missing aliases. */
4979 maybe_apply_pending_pragma_weaks ();
4981 /* We're done with static constructors, so we can go back to "C++"
4982 linkage now. */
4983 pop_lang_context ();
4985 if (flag_vtable_verify)
4987 vtv_recover_class_info ();
4988 vtv_compute_class_hierarchy_transitive_closure ();
4989 vtv_build_vtable_verify_fndecl ();
4992 perform_deferred_noexcept_checks ();
4994 finish_repo ();
4995 fini_constexpr ();
4997 /* The entire file is now complete. If requested, dump everything
4998 to a file. */
4999 dump_tu ();
5001 if (flag_detailed_statistics)
5003 dump_tree_statistics ();
5004 dump_time_statistics ();
5007 timevar_stop (TV_PHASE_DEFERRED);
5008 timevar_start (TV_PHASE_PARSING);
5010 /* Indicate that we're done with front end processing. */
5011 at_eof = 2;
5014 /* Perform any post compilation-proper cleanups for the C++ front-end.
5015 This should really go away. No front-end should need to do
5016 anything past the compilation process. */
5018 void
5019 cxx_post_compilation_parsing_cleanups (void)
5021 timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS);
5023 if (flag_vtable_verify)
5025 /* Generate the special constructor initialization function that
5026 calls __VLTRegisterPairs, and give it a very high
5027 initialization priority. This must be done after
5028 finalize_compilation_unit so that we have accurate
5029 information about which vtable will actually be emitted. */
5030 vtv_generate_init_routine ();
5033 input_location = locus_at_end_of_parsing;
5035 if (flag_checking)
5036 validate_conversion_obstack ();
5038 timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS);
5041 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
5042 function to call in parse-tree form; it has not yet been
5043 semantically analyzed. ARGS are the arguments to the function.
5044 They have already been semantically analyzed. This may change
5045 ARGS. */
5047 tree
5048 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
5049 tsubst_flags_t complain)
5051 tree orig_fn;
5052 vec<tree, va_gc> *orig_args = NULL;
5053 tree expr;
5054 tree object;
5056 orig_fn = fn;
5057 object = TREE_OPERAND (fn, 0);
5059 if (processing_template_decl)
5061 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
5062 || TREE_CODE (fn) == MEMBER_REF);
5063 if (type_dependent_expression_p (fn)
5064 || any_type_dependent_arguments_p (*args))
5065 return build_min_nt_call_vec (fn, *args);
5067 orig_args = make_tree_vector_copy (*args);
5069 /* Transform the arguments and add the implicit "this"
5070 parameter. That must be done before the FN is transformed
5071 because we depend on the form of FN. */
5072 make_args_non_dependent (*args);
5073 object = build_non_dependent_expr (object);
5074 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5076 if (TREE_CODE (fn) == DOTSTAR_EXPR)
5077 object = cp_build_addr_expr (object, complain);
5078 vec_safe_insert (*args, 0, object);
5080 /* Now that the arguments are done, transform FN. */
5081 fn = build_non_dependent_expr (fn);
5084 /* A qualified name corresponding to a bound pointer-to-member is
5085 represented as an OFFSET_REF:
5087 struct B { void g(); };
5088 void (B::*p)();
5089 void B::g() { (this->*p)(); } */
5090 if (TREE_CODE (fn) == OFFSET_REF)
5092 tree object_addr = cp_build_addr_expr (object, complain);
5093 fn = TREE_OPERAND (fn, 1);
5094 fn = get_member_function_from_ptrfunc (&object_addr, fn,
5095 complain);
5096 vec_safe_insert (*args, 0, object_addr);
5099 if (CLASS_TYPE_P (TREE_TYPE (fn)))
5100 expr = build_op_call (fn, args, complain);
5101 else
5102 expr = cp_build_function_call_vec (fn, args, complain);
5103 if (processing_template_decl && expr != error_mark_node)
5104 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
5106 if (orig_args != NULL)
5107 release_tree_vector (orig_args);
5109 return expr;
5113 void
5114 check_default_args (tree x)
5116 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
5117 bool saw_def = false;
5118 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
5119 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
5121 if (TREE_PURPOSE (arg))
5122 saw_def = true;
5123 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
5125 error ("default argument missing for parameter %P of %q+#D", i, x);
5126 TREE_PURPOSE (arg) = error_mark_node;
5131 /* Return true if function DECL can be inlined. This is used to force
5132 instantiation of methods that might be interesting for inlining. */
5133 bool
5134 possibly_inlined_p (tree decl)
5136 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
5137 if (DECL_UNINLINABLE (decl))
5138 return false;
5139 if (!optimize)
5140 return DECL_DECLARED_INLINE_P (decl);
5141 /* When optimizing, we might inline everything when flatten
5142 attribute or heuristics inlining for size or autoinlining
5143 is used. */
5144 return true;
5147 /* Normally, we can wait until instantiation-time to synthesize DECL.
5148 However, if DECL is a static data member initialized with a constant
5149 or a constexpr function, we need it right now because a reference to
5150 such a data member or a call to such function is not value-dependent.
5151 For a function that uses auto in the return type, we need to instantiate
5152 it to find out its type. For OpenMP user defined reductions, we need
5153 them instantiated for reduction clauses which inline them by hand
5154 directly. */
5156 static void
5157 maybe_instantiate_decl (tree decl)
5159 if (DECL_LANG_SPECIFIC (decl)
5160 && DECL_TEMPLATE_INFO (decl)
5161 && (decl_maybe_constant_var_p (decl)
5162 || (TREE_CODE (decl) == FUNCTION_DECL
5163 && DECL_OMP_DECLARE_REDUCTION_P (decl))
5164 || undeduced_auto_decl (decl))
5165 && !DECL_DECLARED_CONCEPT_P (decl)
5166 && !uses_template_parms (DECL_TI_ARGS (decl)))
5168 /* Instantiating a function will result in garbage collection. We
5169 must treat this situation as if we were within the body of a
5170 function so as to avoid collecting live data only referenced from
5171 the stack (such as overload resolution candidates). */
5172 ++function_depth;
5173 instantiate_decl (decl, /*defer_ok=*/false,
5174 /*expl_inst_class_mem_p=*/false);
5175 --function_depth;
5179 /* Maybe warn if DECL is deprecated, subject to COMPLAIN. Returns whether or
5180 not a warning was emitted. */
5182 bool
5183 cp_warn_deprecated_use (tree decl, tsubst_flags_t complain)
5185 if (!(complain & tf_warning) || !decl
5186 || deprecated_state == DEPRECATED_SUPPRESS)
5187 return false;
5189 if (!TREE_DEPRECATED (decl))
5191 /* Perhaps this is a deprecated typedef. */
5192 if (TYPE_P (decl) && TYPE_NAME (decl))
5193 decl = TYPE_NAME (decl);
5195 if (!TREE_DEPRECATED (decl))
5196 return false;
5199 /* Don't warn within members of a deprecated type. */
5200 if (TYPE_P (decl)
5201 && currently_open_class (decl))
5202 return false;
5204 bool warned = false;
5205 if (cxx_dialect >= cxx11
5206 && DECL_P (decl)
5207 && DECL_ARTIFICIAL (decl)
5208 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5209 && copy_fn_p (decl))
5211 /* Don't warn about system library classes (c++/86342). */
5212 if (!DECL_IN_SYSTEM_HEADER (decl))
5213 warned = warning (OPT_Wdeprecated_copy,
5214 "implicitly-declared %qD is deprecated", decl);
5215 if (warned)
5217 tree ctx = DECL_CONTEXT (decl);
5218 tree other = classtype_has_user_copy_or_dtor (ctx);
5219 inform (DECL_SOURCE_LOCATION (other),
5220 "because %qT has user-provided %qD",
5221 ctx, other);
5224 else
5225 warned = warn_deprecated_use (decl, NULL_TREE);
5227 return warned;
5230 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
5231 If DECL is a specialization or implicitly declared class member,
5232 generate the actual definition. Return false if something goes
5233 wrong, true otherwise. */
5235 bool
5236 mark_used (tree decl, tsubst_flags_t complain)
5238 /* If we're just testing conversions or resolving overloads, we
5239 don't want any permanent effects like forcing functions to be
5240 output or instantiating templates. */
5241 if ((complain & tf_conv))
5242 return true;
5244 /* If DECL is a BASELINK for a single function, then treat it just
5245 like the DECL for the function. Otherwise, if the BASELINK is
5246 for an overloaded function, we don't know which function was
5247 actually used until after overload resolution. */
5248 if (BASELINK_P (decl))
5250 decl = BASELINK_FUNCTIONS (decl);
5251 if (really_overloaded_fn (decl))
5252 return true;
5253 decl = OVL_FIRST (decl);
5256 /* Set TREE_USED for the benefit of -Wunused. */
5257 TREE_USED (decl) = 1;
5258 /* And for structured bindings also the underlying decl. */
5259 if (DECL_DECOMPOSITION_P (decl) && DECL_DECOMP_BASE (decl))
5260 TREE_USED (DECL_DECOMP_BASE (decl)) = 1;
5262 if (TREE_CODE (decl) == TEMPLATE_DECL)
5263 return true;
5265 if (DECL_CLONED_FUNCTION_P (decl))
5266 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5268 /* Mark enumeration types as used. */
5269 if (TREE_CODE (decl) == CONST_DECL)
5270 used_types_insert (DECL_CONTEXT (decl));
5272 if (TREE_CODE (decl) == FUNCTION_DECL
5273 && !maybe_instantiate_noexcept (decl, complain))
5274 return false;
5276 if (TREE_CODE (decl) == FUNCTION_DECL
5277 && DECL_DELETED_FN (decl))
5279 if (DECL_ARTIFICIAL (decl)
5280 && DECL_CONV_FN_P (decl)
5281 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
5282 /* We mark a lambda conversion op as deleted if we can't
5283 generate it properly; see maybe_add_lambda_conv_op. */
5284 sorry ("converting lambda that uses %<...%> to function pointer");
5285 else if (complain & tf_error)
5287 error ("use of deleted function %qD", decl);
5288 if (!maybe_explain_implicit_delete (decl))
5289 inform (DECL_SOURCE_LOCATION (decl), "declared here");
5291 return false;
5294 cp_warn_deprecated_use (decl, complain);
5296 /* We can only check DECL_ODR_USED on variables or functions with
5297 DECL_LANG_SPECIFIC set, and these are also the only decls that we
5298 might need special handling for. */
5299 if (!VAR_OR_FUNCTION_DECL_P (decl)
5300 || DECL_LANG_SPECIFIC (decl) == NULL
5301 || DECL_THUNK_P (decl))
5303 if (!processing_template_decl
5304 && !require_deduced_type (decl, complain))
5305 return false;
5306 return true;
5309 /* We only want to do this processing once. We don't need to keep trying
5310 to instantiate inline templates, because unit-at-a-time will make sure
5311 we get them compiled before functions that want to inline them. */
5312 if (DECL_ODR_USED (decl))
5313 return true;
5315 /* Normally, we can wait until instantiation-time to synthesize DECL.
5316 However, if DECL is a static data member initialized with a constant
5317 or a constexpr function, we need it right now because a reference to
5318 such a data member or a call to such function is not value-dependent.
5319 For a function that uses auto in the return type, we need to instantiate
5320 it to find out its type. For OpenMP user defined reductions, we need
5321 them instantiated for reduction clauses which inline them by hand
5322 directly. */
5323 maybe_instantiate_decl (decl);
5325 if (processing_template_decl || in_template_function ())
5326 return true;
5328 /* Check this too in case we're within instantiate_non_dependent_expr. */
5329 if (DECL_TEMPLATE_INFO (decl)
5330 && uses_template_parms (DECL_TI_ARGS (decl)))
5331 return true;
5333 if (!require_deduced_type (decl, complain))
5334 return false;
5336 if (builtin_pack_fn_p (decl))
5338 error ("use of built-in parameter pack %qD outside of a template",
5339 DECL_NAME (decl));
5340 return false;
5343 /* If we don't need a value, then we don't need to synthesize DECL. */
5344 if (cp_unevaluated_operand || in_discarded_stmt)
5345 return true;
5347 DECL_ODR_USED (decl) = 1;
5348 if (DECL_CLONED_FUNCTION_P (decl))
5349 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5351 /* DR 757: A type without linkage shall not be used as the type of a
5352 variable or function with linkage, unless
5353 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5354 o the variable or function is not used (3.2 [basic.def.odr]) or is
5355 defined in the same translation unit. */
5356 if (cxx_dialect > cxx98
5357 && decl_linkage (decl) != lk_none
5358 && !DECL_EXTERN_C_P (decl)
5359 && !DECL_ARTIFICIAL (decl)
5360 && !decl_defined_p (decl)
5361 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
5363 if (is_local_extern (decl))
5364 /* There's no way to define a local extern, and adding it to
5365 the vector interferes with GC, so give an error now. */
5366 no_linkage_error (decl);
5367 else
5368 vec_safe_push (no_linkage_decls, decl);
5371 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
5372 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
5373 /* Remember it, so we can check it was defined. */
5374 note_vague_linkage_fn (decl);
5376 /* Is it a synthesized method that needs to be synthesized? */
5377 if (TREE_CODE (decl) == FUNCTION_DECL
5378 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5379 && DECL_DEFAULTED_FN (decl)
5380 /* A function defaulted outside the class is synthesized either by
5381 cp_finish_decl or instantiate_decl. */
5382 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
5383 && ! DECL_INITIAL (decl))
5385 /* Defer virtual destructors so that thunks get the right
5386 linkage. */
5387 if (DECL_VIRTUAL_P (decl) && !at_eof)
5389 note_vague_linkage_fn (decl);
5390 return true;
5393 /* Remember the current location for a function we will end up
5394 synthesizing. Then we can inform the user where it was
5395 required in the case of error. */
5396 DECL_SOURCE_LOCATION (decl) = input_location;
5398 /* Synthesizing an implicitly defined member function will result in
5399 garbage collection. We must treat this situation as if we were
5400 within the body of a function so as to avoid collecting live data
5401 on the stack (such as overload resolution candidates).
5403 We could just let cp_write_global_declarations handle synthesizing
5404 this function by adding it to deferred_fns, but doing
5405 it at the use site produces better error messages. */
5406 ++function_depth;
5407 synthesize_method (decl);
5408 --function_depth;
5409 /* If this is a synthesized method we don't need to
5410 do the instantiation test below. */
5412 else if (VAR_OR_FUNCTION_DECL_P (decl)
5413 && DECL_TEMPLATE_INFO (decl)
5414 && !DECL_DECLARED_CONCEPT_P (decl)
5415 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5416 || always_instantiate_p (decl)))
5417 /* If this is a function or variable that is an instance of some
5418 template, we now know that we will need to actually do the
5419 instantiation. We check that DECL is not an explicit
5420 instantiation because that is not checked in instantiate_decl.
5422 We put off instantiating functions in order to improve compile
5423 times. Maintaining a stack of active functions is expensive,
5424 and the inliner knows to instantiate any functions it might
5425 need. Therefore, we always try to defer instantiation. */
5427 ++function_depth;
5428 instantiate_decl (decl, /*defer_ok=*/true,
5429 /*expl_inst_class_mem_p=*/false);
5430 --function_depth;
5433 return true;
5436 bool
5437 mark_used (tree decl)
5439 return mark_used (decl, tf_warning_or_error);
5442 tree
5443 vtv_start_verification_constructor_init_function (void)
5445 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5448 tree
5449 vtv_finish_verification_constructor_init_function (tree function_body)
5451 tree fn;
5453 finish_compound_stmt (function_body);
5454 fn = finish_function (/*inline_p=*/false);
5455 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5456 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5458 return fn;
5461 #include "gt-cp-decl2.h"