re PR c++/79790 ([C++17] ICE class template argument deduction failed)
[official-gcc.git] / gcc / cp / decl2.c
blob2a52f8ca3e206fd74f927569c59a7c4fa2d518dc
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2017 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 /* Nonzero if we're done parsing and into end-of-file activities. */
107 int at_eof;
109 /* True if note_mangling_alias should enqueue mangling aliases for
110 later generation, rather than emitting them right away. */
112 bool defer_mangling_aliases = true;
115 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
116 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
117 that apply to the function). */
119 tree
120 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
121 cp_ref_qualifier rqual)
123 tree raises;
124 tree attrs;
125 int type_quals;
126 bool late_return_type_p;
128 if (fntype == error_mark_node || ctype == error_mark_node)
129 return error_mark_node;
131 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
132 || TREE_CODE (fntype) == METHOD_TYPE);
134 type_quals = quals & ~TYPE_QUAL_RESTRICT;
135 ctype = cp_build_qualified_type (ctype, type_quals);
136 raises = TYPE_RAISES_EXCEPTIONS (fntype);
137 attrs = TYPE_ATTRIBUTES (fntype);
138 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
139 fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
140 (TREE_CODE (fntype) == METHOD_TYPE
141 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
142 : TYPE_ARG_TYPES (fntype)));
143 if (attrs)
144 fntype = cp_build_type_attribute_variant (fntype, attrs);
145 if (rqual)
146 fntype = build_ref_qualified_type (fntype, rqual);
147 if (raises)
148 fntype = build_exception_variant (fntype, raises);
149 if (late_return_type_p)
150 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
152 return fntype;
155 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
156 return type changed to NEW_RET. */
158 tree
159 change_return_type (tree new_ret, tree fntype)
161 tree newtype;
162 tree args = TYPE_ARG_TYPES (fntype);
163 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
164 tree attrs = TYPE_ATTRIBUTES (fntype);
165 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
167 if (new_ret == error_mark_node)
168 return fntype;
170 if (same_type_p (new_ret, TREE_TYPE (fntype)))
171 return fntype;
173 if (TREE_CODE (fntype) == FUNCTION_TYPE)
175 newtype = build_function_type (new_ret, args);
176 newtype = apply_memfn_quals (newtype,
177 type_memfn_quals (fntype),
178 type_memfn_rqual (fntype));
180 else
181 newtype = build_method_type_directly
182 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
183 if (FUNCTION_REF_QUALIFIED (fntype))
184 newtype = build_ref_qualified_type (newtype, type_memfn_rqual (fntype));
185 if (raises)
186 newtype = build_exception_variant (newtype, raises);
187 if (attrs)
188 newtype = cp_build_type_attribute_variant (newtype, attrs);
189 if (late_return_type_p)
190 TYPE_HAS_LATE_RETURN_TYPE (newtype) = 1;
192 return newtype;
195 /* Build a PARM_DECL of FN with NAME and TYPE, and set DECL_ARG_TYPE
196 appropriately. */
198 tree
199 cp_build_parm_decl (tree fn, tree name, tree type)
201 tree parm = build_decl (input_location,
202 PARM_DECL, name, type);
203 DECL_CONTEXT (parm) = fn;
205 /* DECL_ARG_TYPE is only used by the back end and the back end never
206 sees templates. */
207 if (!processing_template_decl)
208 DECL_ARG_TYPE (parm) = type_passed_as (type);
210 return parm;
213 /* Returns a PARM_DECL of FN for a parameter of the indicated TYPE, with the
214 indicated NAME. */
216 tree
217 build_artificial_parm (tree fn, tree name, tree type)
219 tree parm = cp_build_parm_decl (fn, name, type);
220 DECL_ARTIFICIAL (parm) = 1;
221 /* All our artificial parms are implicitly `const'; they cannot be
222 assigned to. */
223 TREE_READONLY (parm) = 1;
224 return parm;
227 /* Constructors for types with virtual baseclasses need an "in-charge" flag
228 saying whether this constructor is responsible for initialization of
229 virtual baseclasses or not. All destructors also need this "in-charge"
230 flag, which additionally determines whether or not the destructor should
231 free the memory for the object.
233 This function adds the "in-charge" flag to member function FN if
234 appropriate. It is called from grokclassfn and tsubst.
235 FN must be either a constructor or destructor.
237 The in-charge flag follows the 'this' parameter, and is followed by the
238 VTT parm (if any), then the user-written parms. */
240 void
241 maybe_retrofit_in_chrg (tree fn)
243 tree basetype, arg_types, parms, parm, fntype;
245 /* If we've already add the in-charge parameter don't do it again. */
246 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
247 return;
249 /* When processing templates we can't know, in general, whether or
250 not we're going to have virtual baseclasses. */
251 if (processing_template_decl)
252 return;
254 /* We don't need an in-charge parameter for constructors that don't
255 have virtual bases. */
256 if (DECL_CONSTRUCTOR_P (fn)
257 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
258 return;
260 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
261 basetype = TREE_TYPE (TREE_VALUE (arg_types));
262 arg_types = TREE_CHAIN (arg_types);
264 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
266 /* If this is a subobject constructor or destructor, our caller will
267 pass us a pointer to our VTT. */
268 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
270 parm = build_artificial_parm (fn, vtt_parm_identifier, vtt_parm_type);
272 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
273 DECL_CHAIN (parm) = parms;
274 parms = parm;
276 /* ...and then to TYPE_ARG_TYPES. */
277 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
279 DECL_HAS_VTT_PARM_P (fn) = 1;
282 /* Then add the in-charge parm (before the VTT parm). */
283 parm = build_artificial_parm (fn, in_charge_identifier, integer_type_node);
284 DECL_CHAIN (parm) = parms;
285 parms = parm;
286 arg_types = hash_tree_chain (integer_type_node, arg_types);
288 /* Insert our new parameter(s) into the list. */
289 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
291 /* And rebuild the function type. */
292 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
293 arg_types);
294 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
295 fntype = build_exception_variant (fntype,
296 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
297 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
298 fntype = (cp_build_type_attribute_variant
299 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
300 TREE_TYPE (fn) = fntype;
302 /* Now we've got the in-charge parameter. */
303 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
306 /* Classes overload their constituent function names automatically.
307 When a function name is declared in a record structure,
308 its name is changed to it overloaded name. Since names for
309 constructors and destructors can conflict, we place a leading
310 '$' for destructors.
312 CNAME is the name of the class we are grokking for.
314 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
316 FLAGS contains bits saying what's special about today's
317 arguments. DTOR_FLAG == DESTRUCTOR.
319 If FUNCTION is a destructor, then we must add the `auto-delete' field
320 as a second parameter. There is some hair associated with the fact
321 that we must "declare" this variable in the manner consistent with the
322 way the rest of the arguments were declared.
324 QUALS are the qualifiers for the this pointer. */
326 void
327 grokclassfn (tree ctype, tree function, enum overload_flags flags)
329 tree fn_name = DECL_NAME (function);
331 /* Even within an `extern "C"' block, members get C++ linkage. See
332 [dcl.link] for details. */
333 SET_DECL_LANGUAGE (function, lang_cplusplus);
335 if (fn_name == NULL_TREE)
337 error ("name missing for member function");
338 fn_name = get_identifier ("<anonymous>");
339 DECL_NAME (function) = fn_name;
342 DECL_CONTEXT (function) = ctype;
344 if (flags == DTOR_FLAG)
345 DECL_CXX_DESTRUCTOR_P (function) = 1;
347 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
348 maybe_retrofit_in_chrg (function);
351 /* Create an ARRAY_REF, checking for the user doing things backwards
352 along the way. DECLTYPE_P is for N3276, as in the parser. */
354 tree
355 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
356 bool decltype_p)
358 tree type;
359 tree expr;
360 tree orig_array_expr = array_expr;
361 tree orig_index_exp = index_exp;
362 tree overload = NULL_TREE;
364 if (error_operand_p (array_expr) || error_operand_p (index_exp))
365 return error_mark_node;
367 if (processing_template_decl)
369 if (type_dependent_expression_p (array_expr)
370 || type_dependent_expression_p (index_exp))
371 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
372 NULL_TREE, NULL_TREE);
373 array_expr = build_non_dependent_expr (array_expr);
374 index_exp = build_non_dependent_expr (index_exp);
377 type = TREE_TYPE (array_expr);
378 gcc_assert (type);
379 type = non_reference (type);
381 /* If they have an `operator[]', use that. */
382 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
384 tsubst_flags_t complain = tf_warning_or_error;
385 if (decltype_p)
386 complain |= tf_decltype;
387 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
388 index_exp, NULL_TREE, &overload, complain);
390 else
392 tree p1, p2, i1, i2;
394 /* Otherwise, create an ARRAY_REF for a pointer or array type.
395 It is a little-known fact that, if `a' is an array and `i' is
396 an int, you can write `i[a]', which means the same thing as
397 `a[i]'. */
398 if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
399 p1 = array_expr;
400 else
401 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
403 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
404 p2 = index_exp;
405 else
406 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
408 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
409 false);
410 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
411 false);
413 if ((p1 && i2) && (i1 && p2))
414 error ("ambiguous conversion for array subscript");
416 if (p1 && i2)
417 array_expr = p1, index_exp = i2;
418 else if (i1 && p2)
419 array_expr = p2, index_exp = i1;
420 else
422 error ("invalid types %<%T[%T]%> for array subscript",
423 type, TREE_TYPE (index_exp));
424 return error_mark_node;
427 if (array_expr == error_mark_node || index_exp == error_mark_node)
428 error ("ambiguous conversion for array subscript");
430 expr = build_array_ref (input_location, array_expr, index_exp);
432 if (processing_template_decl && expr != error_mark_node)
434 if (overload != NULL_TREE)
435 return (build_min_non_dep_op_overload
436 (ARRAY_REF, expr, overload, orig_array_expr, orig_index_exp));
438 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
439 NULL_TREE, NULL_TREE);
441 return expr;
444 /* Given the cast expression EXP, checking out its validity. Either return
445 an error_mark_node if there was an unavoidable error, return a cast to
446 void for trying to delete a pointer w/ the value 0, or return the
447 call to delete. If DOING_VEC is true, we handle things differently
448 for doing an array delete.
449 Implements ARM $5.3.4. This is called from the parser. */
451 tree
452 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
453 tsubst_flags_t complain)
455 tree t, type;
457 if (exp == error_mark_node)
458 return exp;
460 if (processing_template_decl)
462 t = build_min (DELETE_EXPR, void_type_node, exp, size);
463 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
464 DELETE_EXPR_USE_VEC (t) = doing_vec;
465 TREE_SIDE_EFFECTS (t) = 1;
466 return t;
469 /* An array can't have been allocated by new, so complain. */
470 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
471 warning (0, "deleting array %q#E", exp);
473 t = build_expr_type_conversion (WANT_POINTER, exp, true);
475 if (t == NULL_TREE || t == error_mark_node)
477 error ("type %q#T argument given to %<delete%>, expected pointer",
478 TREE_TYPE (exp));
479 return error_mark_node;
482 type = TREE_TYPE (t);
484 /* As of Valley Forge, you can delete a pointer to const. */
486 /* You can't delete functions. */
487 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
489 error ("cannot delete a function. Only pointer-to-objects are "
490 "valid arguments to %<delete%>");
491 return error_mark_node;
494 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
495 if (VOID_TYPE_P (TREE_TYPE (type)))
497 warning (OPT_Wdelete_incomplete, "deleting %qT is undefined", type);
498 doing_vec = 0;
501 /* Deleting a pointer with the value zero is valid and has no effect. */
502 if (integer_zerop (t))
503 return build1 (NOP_EXPR, void_type_node, t);
505 if (doing_vec)
506 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
507 sfk_deleting_destructor,
508 use_global_delete, complain);
509 else
510 return build_delete (type, t, sfk_deleting_destructor,
511 LOOKUP_NORMAL, use_global_delete,
512 complain);
515 /* Report an error if the indicated template declaration is not the
516 sort of thing that should be a member template. */
518 void
519 check_member_template (tree tmpl)
521 tree decl;
523 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
524 decl = DECL_TEMPLATE_RESULT (tmpl);
526 if (TREE_CODE (decl) == FUNCTION_DECL
527 || DECL_ALIAS_TEMPLATE_P (tmpl)
528 || (TREE_CODE (decl) == TYPE_DECL
529 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
531 /* The parser rejects template declarations in local classes
532 (with the exception of generic lambdas). */
533 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
534 /* The parser rejects any use of virtual in a function template. */
535 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
536 && DECL_VIRTUAL_P (decl)));
538 /* The debug-information generating code doesn't know what to do
539 with member templates. */
540 DECL_IGNORED_P (tmpl) = 1;
542 else if (variable_template_p (tmpl))
543 /* OK */;
544 else
545 error ("template declaration of %q#D", decl);
548 /* Sanity check: report error if this function FUNCTION is not
549 really a member of the class (CTYPE) it is supposed to belong to.
550 TEMPLATE_PARMS is used to specify the template parameters of a member
551 template passed as FUNCTION_DECL. If the member template is passed as a
552 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
553 from the declaration. If the function is not a function template, it
554 must be NULL.
555 It returns the original declaration for the function, NULL_TREE if
556 no declaration was found, error_mark_node if an error was emitted. */
558 tree
559 check_classfn (tree ctype, tree function, tree template_parms)
561 if (DECL_USE_TEMPLATE (function)
562 && !(TREE_CODE (function) == TEMPLATE_DECL
563 && DECL_TEMPLATE_SPECIALIZATION (function))
564 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
565 /* Since this is a specialization of a member template,
566 we're not going to find the declaration in the class.
567 For example, in:
569 struct S { template <typename T> void f(T); };
570 template <> void S::f(int);
572 we're not going to find `S::f(int)', but there's no
573 reason we should, either. We let our callers know we didn't
574 find the method, but we don't complain. */
575 return NULL_TREE;
577 /* Basic sanity check: for a template function, the template parameters
578 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
579 if (TREE_CODE (function) == TEMPLATE_DECL)
581 if (template_parms
582 && !comp_template_parms (template_parms,
583 DECL_TEMPLATE_PARMS (function)))
585 error ("template parameter lists provided don%'t match the "
586 "template parameters of %qD", function);
587 return error_mark_node;
589 template_parms = DECL_TEMPLATE_PARMS (function);
592 /* OK, is this a definition of a member template? */
593 bool is_template = (template_parms != NULL_TREE);
595 /* [temp.mem]
597 A destructor shall not be a member template. */
598 if (DECL_DESTRUCTOR_P (function) && is_template)
600 error ("destructor %qD declared as member template", function);
601 return error_mark_node;
604 /* We must enter the scope here, because conversion operators are
605 named by target type, and type equivalence relies on typenames
606 resolving within the scope of CTYPE. */
607 tree pushed_scope = push_scope (ctype);
608 tree matched = NULL_TREE;
609 tree fns = lookup_fnfields_slot (ctype, DECL_NAME (function));
611 for (ovl_iterator iter (fns); !matched && iter; ++iter)
613 tree fndecl = *iter;
614 tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
615 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
617 /* We cannot simply call decls_match because this doesn't work
618 for static member functions that are pretending to be
619 methods, and because the name may have been changed by
620 asm("new_name"). */
622 /* Get rid of the this parameter on functions that become
623 static. */
624 if (DECL_STATIC_FUNCTION_P (fndecl)
625 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
626 p1 = TREE_CHAIN (p1);
628 /* A member template definition only matches a member template
629 declaration. */
630 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
631 continue;
633 /* ref-qualifier or absence of same must match. */
634 if (type_memfn_rqual (TREE_TYPE (function))
635 != type_memfn_rqual (TREE_TYPE (fndecl)))
636 continue;
638 // Include constraints in the match.
639 tree c1 = get_constraints (function);
640 tree c2 = get_constraints (fndecl);
642 /* While finding a match, same types and params are not enough
643 if the function is versioned. Also check version ("target")
644 attributes. */
645 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
646 TREE_TYPE (TREE_TYPE (fndecl)))
647 && compparms (p1, p2)
648 && !targetm.target_option.function_versions (function, fndecl)
649 && (!is_template
650 || comp_template_parms (template_parms,
651 DECL_TEMPLATE_PARMS (fndecl)))
652 && equivalent_constraints (c1, c2)
653 && (DECL_TEMPLATE_SPECIALIZATION (function)
654 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
655 && (!DECL_TEMPLATE_SPECIALIZATION (function)
656 || (DECL_TI_TEMPLATE (function) == DECL_TI_TEMPLATE (fndecl))))
657 matched = fndecl;
660 if (!matched)
662 if (!COMPLETE_TYPE_P (ctype))
663 cxx_incomplete_type_error (function, ctype);
664 else
666 if (DECL_CONV_FN_P (function))
667 fns = lookup_all_conversions (ctype);
669 error_at (DECL_SOURCE_LOCATION (function),
670 "no declaration matches %q#D", function);
671 if (fns)
672 print_candidates (fns);
673 else if (DECL_CONV_FN_P (function))
674 inform (DECL_SOURCE_LOCATION (function),
675 "no conversion operators declared");
676 else
677 inform (DECL_SOURCE_LOCATION (function),
678 "no functions named %qD", function);
679 inform (DECL_SOURCE_LOCATION (TYPE_NAME (ctype)),
680 "%#qT defined here", ctype);
682 matched = error_mark_node;
685 if (pushed_scope)
686 pop_scope (pushed_scope);
688 return matched;
691 /* DECL is a function with vague linkage. Remember it so that at the
692 end of the translation unit we can decide whether or not to emit
693 it. */
695 void
696 note_vague_linkage_fn (tree decl)
698 DECL_DEFER_OUTPUT (decl) = 1;
699 vec_safe_push (deferred_fns, decl);
702 /* As above, but for variable template instantiations. */
704 void
705 note_variable_template_instantiation (tree decl)
707 vec_safe_push (pending_statics, decl);
710 /* We have just processed the DECL, which is a static data member.
711 The other parameters are as for cp_finish_decl. */
713 void
714 finish_static_data_member_decl (tree decl,
715 tree init, bool init_const_expr_p,
716 tree asmspec_tree,
717 int flags)
719 DECL_CONTEXT (decl) = current_class_type;
721 /* We cannot call pushdecl here, because that would fill in the
722 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
723 the right thing, namely, to put this decl out straight away. */
725 if (! processing_template_decl)
726 vec_safe_push (pending_statics, decl);
728 if (LOCAL_CLASS_P (current_class_type)
729 /* We already complained about the template definition. */
730 && !DECL_TEMPLATE_INSTANTIATION (decl))
731 permerror (input_location, "local class %q#T shall not have static data member %q#D",
732 current_class_type, decl);
733 else
734 for (tree t = current_class_type; TYPE_P (t);
735 t = CP_TYPE_CONTEXT (t))
736 if (TYPE_UNNAMED_P (t))
738 if (permerror (DECL_SOURCE_LOCATION (decl),
739 "static data member %qD in unnamed class", decl))
740 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
741 "unnamed class defined here");
742 break;
745 DECL_IN_AGGR_P (decl) = 1;
747 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
748 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
749 SET_VAR_HAD_UNKNOWN_BOUND (decl);
751 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
754 /* DECLARATOR and DECLSPECS correspond to a class member. The other
755 parameters are as for cp_finish_decl. Return the DECL for the
756 class member declared. */
758 tree
759 grokfield (const cp_declarator *declarator,
760 cp_decl_specifier_seq *declspecs,
761 tree init, bool init_const_expr_p,
762 tree asmspec_tree,
763 tree attrlist)
765 tree value;
766 const char *asmspec = 0;
767 int flags;
768 tree name;
770 if (init
771 && TREE_CODE (init) == TREE_LIST
772 && TREE_VALUE (init) == error_mark_node
773 && TREE_CHAIN (init) == NULL_TREE)
774 init = NULL_TREE;
776 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
777 if (! value || value == error_mark_node)
778 /* friend or constructor went bad. */
779 return error_mark_node;
780 if (TREE_TYPE (value) == error_mark_node)
781 return value;
783 if (TREE_CODE (value) == TYPE_DECL && init)
785 error ("typedef %qD is initialized (use decltype instead)", value);
786 init = NULL_TREE;
789 /* Pass friendly classes back. */
790 if (value == void_type_node)
791 return value;
794 name = DECL_NAME (value);
796 if (name != NULL_TREE)
798 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
800 error ("explicit template argument list not allowed");
801 return error_mark_node;
804 if (IDENTIFIER_POINTER (name)[0] == '_'
805 && id_equal (name, "_vptr"))
806 error ("member %qD conflicts with virtual function table field name",
807 value);
810 /* Stash away type declarations. */
811 if (TREE_CODE (value) == TYPE_DECL)
813 DECL_NONLOCAL (value) = 1;
814 DECL_CONTEXT (value) = current_class_type;
816 if (attrlist)
818 int attrflags = 0;
820 /* If this is a typedef that names the class for linkage purposes
821 (7.1.3p8), apply any attributes directly to the type. */
822 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
823 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
824 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
826 cplus_decl_attributes (&value, attrlist, attrflags);
829 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
830 && TREE_TYPE (value) != error_mark_node
831 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
832 set_underlying_type (value);
834 /* It's important that push_template_decl below follows
835 set_underlying_type above so that the created template
836 carries the properly set type of VALUE. */
837 if (processing_template_decl)
838 value = push_template_decl (value);
840 record_locally_defined_typedef (value);
841 return value;
844 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
846 if (!friendp && DECL_IN_AGGR_P (value))
848 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
849 return void_type_node;
852 if (asmspec_tree && asmspec_tree != error_mark_node)
853 asmspec = TREE_STRING_POINTER (asmspec_tree);
855 if (init)
857 if (TREE_CODE (value) == FUNCTION_DECL)
859 if (init == ridpointers[(int)RID_DELETE])
861 DECL_DELETED_FN (value) = 1;
862 DECL_DECLARED_INLINE_P (value) = 1;
863 DECL_INITIAL (value) = error_mark_node;
865 else if (init == ridpointers[(int)RID_DEFAULT])
867 if (defaultable_fn_check (value))
869 DECL_DEFAULTED_FN (value) = 1;
870 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
871 DECL_DECLARED_INLINE_P (value) = 1;
874 else if (TREE_CODE (init) == DEFAULT_ARG)
875 error ("invalid initializer for member function %qD", value);
876 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
878 if (integer_zerop (init))
879 DECL_PURE_VIRTUAL_P (value) = 1;
880 else if (error_operand_p (init))
881 ; /* An error has already been reported. */
882 else
883 error ("invalid initializer for member function %qD",
884 value);
886 else
888 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
889 if (friendp)
890 error ("initializer specified for friend function %qD",
891 value);
892 else
893 error ("initializer specified for static member function %qD",
894 value);
897 else if (TREE_CODE (value) == FIELD_DECL)
898 /* C++11 NSDMI, keep going. */;
899 else if (!VAR_P (value))
900 gcc_unreachable ();
903 /* Pass friend decls back. */
904 if ((TREE_CODE (value) == FUNCTION_DECL
905 || TREE_CODE (value) == TEMPLATE_DECL)
906 && DECL_CONTEXT (value) != current_class_type)
907 return value;
909 /* Need to set this before push_template_decl. */
910 if (VAR_P (value))
911 DECL_CONTEXT (value) = current_class_type;
913 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
915 value = push_template_decl (value);
916 if (error_operand_p (value))
917 return error_mark_node;
920 if (attrlist)
921 cplus_decl_attributes (&value, attrlist, 0);
923 if (init && DIRECT_LIST_INIT_P (init))
924 flags = LOOKUP_NORMAL;
925 else
926 flags = LOOKUP_IMPLICIT;
928 switch (TREE_CODE (value))
930 case VAR_DECL:
931 finish_static_data_member_decl (value, init, init_const_expr_p,
932 asmspec_tree, flags);
933 return value;
935 case FIELD_DECL:
936 if (asmspec)
937 error ("%<asm%> specifiers are not permitted on non-static data members");
938 if (DECL_INITIAL (value) == error_mark_node)
939 init = error_mark_node;
940 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
941 NULL_TREE, flags);
942 DECL_IN_AGGR_P (value) = 1;
943 return value;
945 case FUNCTION_DECL:
946 if (asmspec)
947 set_user_assembler_name (value, asmspec);
949 cp_finish_decl (value,
950 /*init=*/NULL_TREE,
951 /*init_const_expr_p=*/false,
952 asmspec_tree, flags);
954 /* Pass friends back this way. */
955 if (DECL_FRIEND_P (value))
956 return void_type_node;
958 DECL_IN_AGGR_P (value) = 1;
959 return value;
961 default:
962 gcc_unreachable ();
964 return NULL_TREE;
967 /* Like `grokfield', but for bitfields.
968 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
970 tree
971 grokbitfield (const cp_declarator *declarator,
972 cp_decl_specifier_seq *declspecs, tree width,
973 tree attrlist)
975 tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, &attrlist);
977 if (value == error_mark_node)
978 return NULL_TREE; /* friends went bad. */
979 if (TREE_TYPE (value) == error_mark_node)
980 return value;
982 /* Pass friendly classes back. */
983 if (VOID_TYPE_P (value))
984 return void_type_node;
986 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
987 && (POINTER_TYPE_P (value)
988 || !dependent_type_p (TREE_TYPE (value))))
990 error ("bit-field %qD with non-integral type", value);
991 return error_mark_node;
994 if (TREE_CODE (value) == TYPE_DECL)
996 error ("cannot declare %qD to be a bit-field type", value);
997 return NULL_TREE;
1000 /* Usually, finish_struct_1 catches bitfields with invalid types.
1001 But, in the case of bitfields with function type, we confuse
1002 ourselves into thinking they are member functions, so we must
1003 check here. */
1004 if (TREE_CODE (value) == FUNCTION_DECL)
1006 error ("cannot declare bit-field %qD with function type",
1007 DECL_NAME (value));
1008 return NULL_TREE;
1011 if (DECL_IN_AGGR_P (value))
1013 error ("%qD is already defined in the class %qT", value,
1014 DECL_CONTEXT (value));
1015 return void_type_node;
1018 if (TREE_STATIC (value))
1020 error ("static member %qD cannot be a bit-field", value);
1021 return NULL_TREE;
1023 cp_finish_decl (value, NULL_TREE, false, NULL_TREE, 0);
1025 if (width != error_mark_node)
1027 /* The width must be an integer type. */
1028 if (!type_dependent_expression_p (width)
1029 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1030 error ("width of bit-field %qD has non-integral type %qT", value,
1031 TREE_TYPE (width));
1032 else
1034 DECL_INITIAL (value) = width;
1035 SET_DECL_C_BIT_FIELD (value);
1039 DECL_IN_AGGR_P (value) = 1;
1041 if (attrlist)
1042 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1044 return value;
1048 /* Returns true iff ATTR is an attribute which needs to be applied at
1049 instantiation time rather than template definition time. */
1051 static bool
1052 is_late_template_attribute (tree attr, tree decl)
1054 tree name = get_attribute_name (attr);
1055 tree args = TREE_VALUE (attr);
1056 const struct attribute_spec *spec = lookup_attribute_spec (name);
1057 tree arg;
1059 if (!spec)
1060 /* Unknown attribute. */
1061 return false;
1063 /* Attribute weak handling wants to write out assembly right away. */
1064 if (is_attribute_p ("weak", name))
1065 return true;
1067 /* Attributes used and unused are applied directly, as they appertain to
1068 decls. */
1069 if (is_attribute_p ("unused", name)
1070 || is_attribute_p ("used", name))
1071 return false;
1073 /* Attribute tls_model wants to modify the symtab. */
1074 if (is_attribute_p ("tls_model", name))
1075 return true;
1077 /* #pragma omp declare simd attribute needs to be always deferred. */
1078 if (flag_openmp
1079 && is_attribute_p ("omp declare simd", name))
1080 return true;
1082 /* An attribute pack is clearly dependent. */
1083 if (args && PACK_EXPANSION_P (args))
1084 return true;
1086 /* If any of the arguments are dependent expressions, we can't evaluate
1087 the attribute until instantiation time. */
1088 for (arg = args; arg; arg = TREE_CHAIN (arg))
1090 tree t = TREE_VALUE (arg);
1092 /* If the first attribute argument is an identifier, only consider
1093 second and following arguments. Attributes like mode, format,
1094 cleanup and several target specific attributes aren't late
1095 just because they have an IDENTIFIER_NODE as first argument. */
1096 if (arg == args && attribute_takes_identifier_p (name)
1097 && identifier_p (t))
1098 continue;
1100 if (value_dependent_expression_p (t)
1101 || type_dependent_expression_p (t))
1102 return true;
1105 if (TREE_CODE (decl) == TYPE_DECL
1106 || TYPE_P (decl)
1107 || spec->type_required)
1109 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1111 /* We can't apply any attributes to a completely unknown type until
1112 instantiation time. */
1113 enum tree_code code = TREE_CODE (type);
1114 if (code == TEMPLATE_TYPE_PARM
1115 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1116 || code == TYPENAME_TYPE)
1117 return true;
1118 /* Also defer most attributes on dependent types. This is not
1119 necessary in all cases, but is the better default. */
1120 else if (dependent_type_p (type)
1121 /* But some attributes specifically apply to templates. */
1122 && !is_attribute_p ("abi_tag", name)
1123 && !is_attribute_p ("deprecated", name)
1124 && !is_attribute_p ("visibility", name))
1125 return true;
1126 else
1127 return false;
1129 else
1130 return false;
1133 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1134 applied at instantiation time and return them. If IS_DEPENDENT is true,
1135 the declaration itself is dependent, so all attributes should be applied
1136 at instantiation time. */
1138 static tree
1139 splice_template_attributes (tree *attr_p, tree decl)
1141 tree *p = attr_p;
1142 tree late_attrs = NULL_TREE;
1143 tree *q = &late_attrs;
1145 if (!p)
1146 return NULL_TREE;
1148 for (; *p; )
1150 if (is_late_template_attribute (*p, decl))
1152 ATTR_IS_DEPENDENT (*p) = 1;
1153 *q = *p;
1154 *p = TREE_CHAIN (*p);
1155 q = &TREE_CHAIN (*q);
1156 *q = NULL_TREE;
1158 else
1159 p = &TREE_CHAIN (*p);
1162 return late_attrs;
1165 /* Remove any late attributes from the list in ATTR_P and attach them to
1166 DECL_P. */
1168 static void
1169 save_template_attributes (tree *attr_p, tree *decl_p)
1171 tree *q;
1173 if (attr_p && *attr_p == error_mark_node)
1174 return;
1176 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1177 if (!late_attrs)
1178 return;
1180 if (DECL_P (*decl_p))
1181 q = &DECL_ATTRIBUTES (*decl_p);
1182 else
1183 q = &TYPE_ATTRIBUTES (*decl_p);
1185 tree old_attrs = *q;
1187 /* Merge the late attributes at the beginning with the attribute
1188 list. */
1189 late_attrs = merge_attributes (late_attrs, *q);
1190 *q = late_attrs;
1192 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1194 /* We've added new attributes directly to the main variant, so
1195 now we need to update all of the other variants to include
1196 these new attributes. */
1197 tree variant;
1198 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1199 variant = TYPE_NEXT_VARIANT (variant))
1201 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1202 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1207 /* True if ATTRS contains any dependent attributes that affect type
1208 identity. */
1210 bool
1211 any_dependent_type_attributes_p (tree attrs)
1213 for (tree a = attrs; a; a = TREE_CHAIN (a))
1214 if (ATTR_IS_DEPENDENT (a))
1216 const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (a));
1217 if (as && as->affects_type_identity)
1218 return true;
1220 return false;
1223 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1224 to a typedef which gives a previously unnamed class or enum a name for
1225 linkage purposes. */
1227 bool
1228 attributes_naming_typedef_ok (tree attrs)
1230 for (; attrs; attrs = TREE_CHAIN (attrs))
1232 tree name = get_attribute_name (attrs);
1233 if (is_attribute_p ("vector_size", name))
1234 return false;
1236 return true;
1239 /* Like reconstruct_complex_type, but handle also template trees. */
1241 tree
1242 cp_reconstruct_complex_type (tree type, tree bottom)
1244 tree inner, outer;
1245 bool late_return_type_p = false;
1247 if (TYPE_PTR_P (type))
1249 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1250 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1251 TYPE_REF_CAN_ALIAS_ALL (type));
1253 else if (TREE_CODE (type) == REFERENCE_TYPE)
1255 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1256 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1257 TYPE_REF_CAN_ALIAS_ALL (type));
1259 else if (TREE_CODE (type) == ARRAY_TYPE)
1261 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1262 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1263 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1264 element type qualification will be handled by the recursive
1265 cp_reconstruct_complex_type call and cp_build_qualified_type
1266 for ARRAY_TYPEs changes the element type. */
1267 return outer;
1269 else if (TREE_CODE (type) == FUNCTION_TYPE)
1271 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1272 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1273 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1274 outer = apply_memfn_quals (outer,
1275 type_memfn_quals (type),
1276 type_memfn_rqual (type));
1278 else if (TREE_CODE (type) == METHOD_TYPE)
1280 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1281 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1282 /* The build_method_type_directly() routine prepends 'this' to argument list,
1283 so we must compensate by getting rid of it. */
1284 outer
1285 = build_method_type_directly
1286 (class_of_this_parm (type), inner,
1287 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1289 else if (TREE_CODE (type) == OFFSET_TYPE)
1291 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1292 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1294 else
1295 return bottom;
1297 if (TYPE_ATTRIBUTES (type))
1298 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1299 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1301 if (late_return_type_p)
1302 TYPE_HAS_LATE_RETURN_TYPE (outer) = 1;
1304 return outer;
1307 /* Replaces any constexpr expression that may be into the attributes
1308 arguments with their reduced value. */
1310 static void
1311 cp_check_const_attributes (tree attributes)
1313 if (attributes == error_mark_node)
1314 return;
1316 tree attr;
1317 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1319 tree arg;
1320 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1322 tree expr = TREE_VALUE (arg);
1323 if (EXPR_P (expr))
1324 TREE_VALUE (arg) = maybe_constant_value (expr);
1329 /* Return true if TYPE is an OpenMP mappable type. */
1330 bool
1331 cp_omp_mappable_type (tree type)
1333 /* Mappable type has to be complete. */
1334 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1335 return false;
1336 /* Arrays have mappable type if the elements have mappable type. */
1337 while (TREE_CODE (type) == ARRAY_TYPE)
1338 type = TREE_TYPE (type);
1339 /* A mappable type cannot contain virtual members. */
1340 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1341 return false;
1342 /* All data members must be non-static. */
1343 if (CLASS_TYPE_P (type))
1345 tree field;
1346 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1347 if (VAR_P (field))
1348 return false;
1349 /* All fields must have mappable types. */
1350 else if (TREE_CODE (field) == FIELD_DECL
1351 && !cp_omp_mappable_type (TREE_TYPE (field)))
1352 return false;
1354 return true;
1357 /* Like decl_attributes, but handle C++ complexity. */
1359 void
1360 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1362 if (*decl == NULL_TREE || *decl == void_type_node
1363 || *decl == error_mark_node)
1364 return;
1366 /* Add implicit "omp declare target" attribute if requested. */
1367 if (scope_chain->omp_declare_target_attribute
1368 && ((VAR_P (*decl)
1369 && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
1370 || TREE_CODE (*decl) == FUNCTION_DECL))
1372 if (VAR_P (*decl)
1373 && DECL_CLASS_SCOPE_P (*decl))
1374 error ("%q+D static data member inside of declare target directive",
1375 *decl);
1376 else if (!processing_template_decl
1377 && VAR_P (*decl)
1378 && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1379 error ("%q+D in declare target directive does not have mappable type",
1380 *decl);
1381 else
1382 attributes = tree_cons (get_identifier ("omp declare target"),
1383 NULL_TREE, attributes);
1386 if (processing_template_decl)
1388 if (check_for_bare_parameter_packs (attributes))
1389 return;
1391 save_template_attributes (&attributes, decl);
1394 cp_check_const_attributes (attributes);
1396 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1397 decl = &DECL_TEMPLATE_RESULT (*decl);
1399 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1401 attributes
1402 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1403 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1404 attributes, flags);
1406 else
1407 decl_attributes (decl, attributes, flags);
1409 if (TREE_CODE (*decl) == TYPE_DECL)
1410 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1412 /* Propagate deprecation out to the template. */
1413 if (TREE_DEPRECATED (*decl))
1414 if (tree ti = get_template_info (*decl))
1416 tree tmpl = TI_TEMPLATE (ti);
1417 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1418 : DECL_TEMPLATE_RESULT (tmpl));
1419 if (*decl == pattern)
1420 TREE_DEPRECATED (tmpl) = true;
1424 /* Walks through the namespace- or function-scope anonymous union
1425 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1426 Returns one of the fields for use in the mangled name. */
1428 static tree
1429 build_anon_union_vars (tree type, tree object)
1431 tree main_decl = NULL_TREE;
1432 tree field;
1434 /* Rather than write the code to handle the non-union case,
1435 just give an error. */
1436 if (TREE_CODE (type) != UNION_TYPE)
1438 error ("anonymous struct not inside named type");
1439 return error_mark_node;
1442 for (field = TYPE_FIELDS (type);
1443 field != NULL_TREE;
1444 field = DECL_CHAIN (field))
1446 tree decl;
1447 tree ref;
1449 if (DECL_ARTIFICIAL (field))
1450 continue;
1451 if (TREE_CODE (field) != FIELD_DECL)
1453 permerror (DECL_SOURCE_LOCATION (field),
1454 "%q#D invalid; an anonymous union can only "
1455 "have non-static data members", field);
1456 continue;
1459 if (TREE_PRIVATE (field))
1460 permerror (DECL_SOURCE_LOCATION (field),
1461 "private member %q#D in anonymous union", field);
1462 else if (TREE_PROTECTED (field))
1463 permerror (DECL_SOURCE_LOCATION (field),
1464 "protected member %q#D in anonymous union", field);
1466 if (processing_template_decl)
1467 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1468 DECL_NAME (field), NULL_TREE);
1469 else
1470 ref = build_class_member_access_expr (object, field, NULL_TREE,
1471 false, tf_warning_or_error);
1473 if (DECL_NAME (field))
1475 tree base;
1477 decl = build_decl (input_location,
1478 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1479 DECL_ANON_UNION_VAR_P (decl) = 1;
1480 DECL_ARTIFICIAL (decl) = 1;
1482 base = get_base_address (object);
1483 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1484 TREE_STATIC (decl) = TREE_STATIC (base);
1485 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1487 SET_DECL_VALUE_EXPR (decl, ref);
1488 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1490 decl = pushdecl (decl);
1492 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1493 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1494 else
1495 decl = 0;
1497 if (main_decl == NULL_TREE)
1498 main_decl = decl;
1501 return main_decl;
1504 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1505 anonymous union, then all members must be laid out together. PUBLIC_P
1506 is nonzero if this union is not declared static. */
1508 void
1509 finish_anon_union (tree anon_union_decl)
1511 tree type;
1512 tree main_decl;
1513 bool public_p;
1515 if (anon_union_decl == error_mark_node)
1516 return;
1518 type = TREE_TYPE (anon_union_decl);
1519 public_p = TREE_PUBLIC (anon_union_decl);
1521 /* The VAR_DECL's context is the same as the TYPE's context. */
1522 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1524 if (TYPE_FIELDS (type) == NULL_TREE)
1525 return;
1527 if (public_p)
1529 error ("namespace-scope anonymous aggregates must be static");
1530 return;
1533 main_decl = build_anon_union_vars (type, anon_union_decl);
1534 if (main_decl == error_mark_node)
1535 return;
1536 if (main_decl == NULL_TREE)
1538 warning (0, "anonymous union with no members");
1539 return;
1542 if (!processing_template_decl)
1544 /* Use main_decl to set the mangled name. */
1545 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1546 maybe_commonize_var (anon_union_decl);
1547 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1548 mangle_decl (anon_union_decl);
1549 DECL_NAME (anon_union_decl) = NULL_TREE;
1552 pushdecl (anon_union_decl);
1553 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1556 /* Auxiliary functions to make type signatures for
1557 `operator new' and `operator delete' correspond to
1558 what compiler will be expecting. */
1560 tree
1561 coerce_new_type (tree type)
1563 int e = 0;
1564 tree args = TYPE_ARG_TYPES (type);
1566 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1568 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1570 e = 1;
1571 error ("%<operator new%> must return type %qT", ptr_type_node);
1574 if (args && args != void_list_node)
1576 if (TREE_PURPOSE (args))
1578 /* [basic.stc.dynamic.allocation]
1580 The first parameter shall not have an associated default
1581 argument. */
1582 error ("the first parameter of %<operator new%> cannot "
1583 "have a default argument");
1584 /* Throw away the default argument. */
1585 TREE_PURPOSE (args) = NULL_TREE;
1588 if (!same_type_p (TREE_VALUE (args), size_type_node))
1590 e = 2;
1591 args = TREE_CHAIN (args);
1594 else
1595 e = 2;
1597 if (e == 2)
1598 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1599 "as first parameter", size_type_node);
1601 switch (e)
1603 case 2:
1604 args = tree_cons (NULL_TREE, size_type_node, args);
1605 /* Fall through. */
1606 case 1:
1607 type = build_exception_variant
1608 (build_function_type (ptr_type_node, args),
1609 TYPE_RAISES_EXCEPTIONS (type));
1610 /* Fall through. */
1611 default:;
1613 return type;
1616 tree
1617 coerce_delete_type (tree type)
1619 int e = 0;
1620 tree args = TYPE_ARG_TYPES (type);
1622 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1624 if (!same_type_p (TREE_TYPE (type), void_type_node))
1626 e = 1;
1627 error ("%<operator delete%> must return type %qT", void_type_node);
1630 if (!args || args == void_list_node
1631 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1633 e = 2;
1634 if (args && args != void_list_node)
1635 args = TREE_CHAIN (args);
1636 error ("%<operator delete%> takes type %qT as first parameter",
1637 ptr_type_node);
1639 switch (e)
1641 case 2:
1642 args = tree_cons (NULL_TREE, ptr_type_node, args);
1643 /* Fall through. */
1644 case 1:
1645 type = build_exception_variant
1646 (build_function_type (void_type_node, args),
1647 TYPE_RAISES_EXCEPTIONS (type));
1648 /* Fall through. */
1649 default:;
1652 return type;
1655 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1656 and mark them as needed. */
1658 static void
1659 mark_vtable_entries (tree decl)
1661 tree fnaddr;
1662 unsigned HOST_WIDE_INT idx;
1664 /* It's OK for the vtable to refer to deprecated virtual functions. */
1665 warning_sentinel w(warn_deprecated_decl);
1667 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1668 idx, fnaddr)
1670 tree fn;
1672 STRIP_NOPS (fnaddr);
1674 if (TREE_CODE (fnaddr) != ADDR_EXPR
1675 && TREE_CODE (fnaddr) != FDESC_EXPR)
1676 /* This entry is an offset: a virtual base class offset, a
1677 virtual call offset, an RTTI offset, etc. */
1678 continue;
1680 fn = TREE_OPERAND (fnaddr, 0);
1681 TREE_ADDRESSABLE (fn) = 1;
1682 /* When we don't have vcall offsets, we output thunks whenever
1683 we output the vtables that contain them. With vcall offsets,
1684 we know all the thunks we'll need when we emit a virtual
1685 function, so we emit the thunks there instead. */
1686 if (DECL_THUNK_P (fn))
1687 use_thunk (fn, /*emit_p=*/0);
1688 mark_used (fn);
1692 /* Set DECL up to have the closest approximation of "initialized common"
1693 linkage available. */
1695 void
1696 comdat_linkage (tree decl)
1698 if (flag_weak)
1699 make_decl_one_only (decl, cxx_comdat_group (decl));
1700 else if (TREE_CODE (decl) == FUNCTION_DECL
1701 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1702 /* We can just emit function and compiler-generated variables
1703 statically; having multiple copies is (for the most part) only
1704 a waste of space.
1706 There are two correctness issues, however: the address of a
1707 template instantiation with external linkage should be the
1708 same, independent of what translation unit asks for the
1709 address, and this will not hold when we emit multiple copies of
1710 the function. However, there's little else we can do.
1712 Also, by default, the typeinfo implementation assumes that
1713 there will be only one copy of the string used as the name for
1714 each type. Therefore, if weak symbols are unavailable, the
1715 run-time library should perform a more conservative check; it
1716 should perform a string comparison, rather than an address
1717 comparison. */
1718 TREE_PUBLIC (decl) = 0;
1719 else
1721 /* Static data member template instantiations, however, cannot
1722 have multiple copies. */
1723 if (DECL_INITIAL (decl) == 0
1724 || DECL_INITIAL (decl) == error_mark_node)
1725 DECL_COMMON (decl) = 1;
1726 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1728 DECL_COMMON (decl) = 1;
1729 DECL_INITIAL (decl) = error_mark_node;
1731 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1733 /* We can't do anything useful; leave vars for explicit
1734 instantiation. */
1735 DECL_EXTERNAL (decl) = 1;
1736 DECL_NOT_REALLY_EXTERN (decl) = 0;
1740 if (TREE_PUBLIC (decl))
1741 DECL_COMDAT (decl) = 1;
1744 /* For win32 we also want to put explicit instantiations in
1745 linkonce sections, so that they will be merged with implicit
1746 instantiations; otherwise we get duplicate symbol errors.
1747 For Darwin we do not want explicit instantiations to be
1748 linkonce. */
1750 void
1751 maybe_make_one_only (tree decl)
1753 /* We used to say that this was not necessary on targets that support weak
1754 symbols, because the implicit instantiations will defer to the explicit
1755 one. However, that's not actually the case in SVR4; a strong definition
1756 after a weak one is an error. Also, not making explicit
1757 instantiations one_only means that we can end up with two copies of
1758 some template instantiations. */
1759 if (! flag_weak)
1760 return;
1762 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1763 we can get away with not emitting them if they aren't used. We need
1764 to for variables so that cp_finish_decl will update their linkage,
1765 because their DECL_INITIAL may not have been set properly yet. */
1767 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1768 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1769 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1771 make_decl_one_only (decl, cxx_comdat_group (decl));
1773 if (VAR_P (decl))
1775 varpool_node *node = varpool_node::get_create (decl);
1776 DECL_COMDAT (decl) = 1;
1777 /* Mark it needed so we don't forget to emit it. */
1778 node->forced_by_abi = true;
1779 TREE_USED (decl) = 1;
1784 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1785 This predicate will give the right answer during parsing of the
1786 function, which other tests may not. */
1788 bool
1789 vague_linkage_p (tree decl)
1791 if (!TREE_PUBLIC (decl))
1793 /* maybe_thunk_body clears TREE_PUBLIC on the maybe-in-charge 'tor
1794 variants, check one of the "clones" for the real linkage. */
1795 if ((DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
1796 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl))
1797 && DECL_CHAIN (decl)
1798 && DECL_CLONED_FUNCTION (DECL_CHAIN (decl)))
1799 return vague_linkage_p (DECL_CHAIN (decl));
1801 gcc_checking_assert (!DECL_COMDAT (decl));
1802 return false;
1804 /* Unfortunately, import_export_decl has not always been called
1805 before the function is processed, so we cannot simply check
1806 DECL_COMDAT. */
1807 if (DECL_COMDAT (decl)
1808 || (TREE_CODE (decl) == FUNCTION_DECL
1809 && DECL_DECLARED_INLINE_P (decl))
1810 || (DECL_LANG_SPECIFIC (decl)
1811 && DECL_TEMPLATE_INSTANTIATION (decl))
1812 || (VAR_P (decl) && DECL_INLINE_VAR_P (decl)))
1813 return true;
1814 else if (DECL_FUNCTION_SCOPE_P (decl))
1815 /* A local static in an inline effectively has vague linkage. */
1816 return (TREE_STATIC (decl)
1817 && vague_linkage_p (DECL_CONTEXT (decl)));
1818 else
1819 return false;
1822 /* Determine whether or not we want to specifically import or export CTYPE,
1823 using various heuristics. */
1825 static void
1826 import_export_class (tree ctype)
1828 /* -1 for imported, 1 for exported. */
1829 int import_export = 0;
1831 /* It only makes sense to call this function at EOF. The reason is
1832 that this function looks at whether or not the first non-inline
1833 non-abstract virtual member function has been defined in this
1834 translation unit. But, we can't possibly know that until we've
1835 seen the entire translation unit. */
1836 gcc_assert (at_eof);
1838 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1839 return;
1841 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1842 we will have CLASSTYPE_INTERFACE_ONLY set but not
1843 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1844 heuristic because someone will supply a #pragma implementation
1845 elsewhere, and deducing it here would produce a conflict. */
1846 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1847 return;
1849 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1850 import_export = -1;
1851 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1852 import_export = 1;
1853 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1854 && !flag_implicit_templates)
1855 /* For a template class, without -fimplicit-templates, check the
1856 repository. If the virtual table is assigned to this
1857 translation unit, then export the class; otherwise, import
1858 it. */
1859 import_export = repo_export_class_p (ctype) ? 1 : -1;
1860 else if (TYPE_POLYMORPHIC_P (ctype))
1862 /* The ABI specifies that the virtual table and associated
1863 information are emitted with the key method, if any. */
1864 tree method = CLASSTYPE_KEY_METHOD (ctype);
1865 /* If weak symbol support is not available, then we must be
1866 careful not to emit the vtable when the key function is
1867 inline. An inline function can be defined in multiple
1868 translation units. If we were to emit the vtable in each
1869 translation unit containing a definition, we would get
1870 multiple definition errors at link-time. */
1871 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1872 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1875 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1876 a definition anywhere else. */
1877 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1878 import_export = 0;
1880 /* Allow back ends the chance to overrule the decision. */
1881 if (targetm.cxx.import_export_class)
1882 import_export = targetm.cxx.import_export_class (ctype, import_export);
1884 if (import_export)
1886 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1887 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1891 /* Return true if VAR has already been provided to the back end; in that
1892 case VAR should not be modified further by the front end. */
1893 static bool
1894 var_finalized_p (tree var)
1896 return varpool_node::get_create (var)->definition;
1899 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1900 must be emitted in this translation unit. Mark it as such. */
1902 void
1903 mark_needed (tree decl)
1905 TREE_USED (decl) = 1;
1906 if (TREE_CODE (decl) == FUNCTION_DECL)
1908 /* Extern inline functions don't become needed when referenced.
1909 If we know a method will be emitted in other TU and no new
1910 functions can be marked reachable, just use the external
1911 definition. */
1912 struct cgraph_node *node = cgraph_node::get_create (decl);
1913 node->forced_by_abi = true;
1915 /* #pragma interface and -frepo code can call mark_needed for
1916 maybe-in-charge 'tors; mark the clones as well. */
1917 tree clone;
1918 FOR_EACH_CLONE (clone, decl)
1919 mark_needed (clone);
1921 else if (VAR_P (decl))
1923 varpool_node *node = varpool_node::get_create (decl);
1924 /* C++ frontend use mark_decl_references to force COMDAT variables
1925 to be output that might appear dead otherwise. */
1926 node->forced_by_abi = true;
1930 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1931 returns true if a definition of this entity should be provided in
1932 this object file. Callers use this function to determine whether
1933 or not to let the back end know that a definition of DECL is
1934 available in this translation unit. */
1936 bool
1937 decl_needed_p (tree decl)
1939 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
1940 /* This function should only be called at the end of the translation
1941 unit. We cannot be sure of whether or not something will be
1942 COMDAT until that point. */
1943 gcc_assert (at_eof);
1945 /* All entities with external linkage that are not COMDAT/EXTERN should be
1946 emitted; they may be referred to from other object files. */
1947 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
1948 return true;
1949 /* Functions marked "dllexport" must be emitted so that they are
1950 visible to other DLLs. */
1951 if (flag_keep_inline_dllexport
1952 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
1953 return true;
1955 /* When not optimizing, do not bother to produce definitions for extern
1956 symbols. */
1957 if (DECL_REALLY_EXTERN (decl)
1958 && ((TREE_CODE (decl) != FUNCTION_DECL
1959 && !optimize)
1960 || (TREE_CODE (decl) == FUNCTION_DECL
1961 && !opt_for_fn (decl, optimize)))
1962 && !lookup_attribute ("always_inline", decl))
1963 return false;
1965 /* If this entity was used, let the back end see it; it will decide
1966 whether or not to emit it into the object file. */
1967 if (TREE_USED (decl))
1968 return true;
1970 /* Virtual functions might be needed for devirtualization. */
1971 if (flag_devirtualize
1972 && TREE_CODE (decl) == FUNCTION_DECL
1973 && DECL_VIRTUAL_P (decl))
1974 return true;
1976 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
1977 reference to DECL might cause it to be emitted later. */
1978 return false;
1981 /* If necessary, write out the vtables for the dynamic class CTYPE.
1982 Returns true if any vtables were emitted. */
1984 static bool
1985 maybe_emit_vtables (tree ctype)
1987 tree vtbl;
1988 tree primary_vtbl;
1989 int needed = 0;
1990 varpool_node *current = NULL, *last = NULL;
1992 /* If the vtables for this class have already been emitted there is
1993 nothing more to do. */
1994 primary_vtbl = CLASSTYPE_VTABLES (ctype);
1995 if (var_finalized_p (primary_vtbl))
1996 return false;
1997 /* Ignore dummy vtables made by get_vtable_decl. */
1998 if (TREE_TYPE (primary_vtbl) == void_type_node)
1999 return false;
2001 /* On some targets, we cannot determine the key method until the end
2002 of the translation unit -- which is when this function is
2003 called. */
2004 if (!targetm.cxx.key_method_may_be_inline ())
2005 determine_key_method (ctype);
2007 /* See if any of the vtables are needed. */
2008 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2010 import_export_decl (vtbl);
2011 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2012 needed = 1;
2014 if (!needed)
2016 /* If the references to this class' vtables are optimized away,
2017 still emit the appropriate debugging information. See
2018 dfs_debug_mark. */
2019 if (DECL_COMDAT (primary_vtbl)
2020 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2021 note_debug_info_needed (ctype);
2022 return false;
2025 /* The ABI requires that we emit all of the vtables if we emit any
2026 of them. */
2027 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2029 /* Mark entities references from the virtual table as used. */
2030 mark_vtable_entries (vtbl);
2032 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2034 vec<tree, va_gc> *cleanups = NULL;
2035 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2036 LOOKUP_NORMAL);
2038 /* It had better be all done at compile-time. */
2039 gcc_assert (!expr && !cleanups);
2042 /* Write it out. */
2043 DECL_EXTERNAL (vtbl) = 0;
2044 rest_of_decl_compilation (vtbl, 1, 1);
2046 /* Because we're only doing syntax-checking, we'll never end up
2047 actually marking the variable as written. */
2048 if (flag_syntax_only)
2049 TREE_ASM_WRITTEN (vtbl) = 1;
2050 else if (DECL_ONE_ONLY (vtbl))
2052 current = varpool_node::get_create (vtbl);
2053 if (last)
2054 current->add_to_same_comdat_group (last);
2055 last = current;
2059 /* Since we're writing out the vtable here, also write the debug
2060 info. */
2061 note_debug_info_needed (ctype);
2063 return true;
2066 /* A special return value from type_visibility meaning internal
2067 linkage. */
2069 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2071 /* walk_tree helper function for type_visibility. */
2073 static tree
2074 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2076 int *vis_p = (int *)data;
2077 if (! TYPE_P (*tp))
2079 *walk_subtrees = 0;
2081 else if (OVERLOAD_TYPE_P (*tp)
2082 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2084 *vis_p = VISIBILITY_ANON;
2085 return *tp;
2087 else if (CLASS_TYPE_P (*tp)
2088 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2089 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2090 return NULL;
2093 /* Returns the visibility of TYPE, which is the minimum visibility of its
2094 component types. */
2096 static int
2097 type_visibility (tree type)
2099 int vis = VISIBILITY_DEFAULT;
2100 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2101 return vis;
2104 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2105 specified (or if VISIBILITY is static). If TMPL is true, this
2106 constraint is for a template argument, and takes precedence
2107 over explicitly-specified visibility on the template. */
2109 static void
2110 constrain_visibility (tree decl, int visibility, bool tmpl)
2112 if (visibility == VISIBILITY_ANON)
2114 /* extern "C" declarations aren't affected by the anonymous
2115 namespace. */
2116 if (!DECL_EXTERN_C_P (decl))
2118 TREE_PUBLIC (decl) = 0;
2119 DECL_WEAK (decl) = 0;
2120 DECL_COMMON (decl) = 0;
2121 DECL_COMDAT (decl) = false;
2122 if (VAR_OR_FUNCTION_DECL_P (decl))
2124 struct symtab_node *snode = symtab_node::get (decl);
2126 if (snode)
2127 snode->set_comdat_group (NULL);
2129 DECL_INTERFACE_KNOWN (decl) = 1;
2130 if (DECL_LANG_SPECIFIC (decl))
2131 DECL_NOT_REALLY_EXTERN (decl) = 1;
2134 else if (visibility > DECL_VISIBILITY (decl)
2135 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2137 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2138 /* This visibility was not specified. */
2139 DECL_VISIBILITY_SPECIFIED (decl) = false;
2143 /* Constrain the visibility of DECL based on the visibility of its template
2144 arguments. */
2146 static void
2147 constrain_visibility_for_template (tree decl, tree targs)
2149 /* If this is a template instantiation, check the innermost
2150 template args for visibility constraints. The outer template
2151 args are covered by the class check. */
2152 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2153 int i;
2154 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2156 int vis = 0;
2158 tree arg = TREE_VEC_ELT (args, i-1);
2159 if (TYPE_P (arg))
2160 vis = type_visibility (arg);
2161 else
2163 if (REFERENCE_REF_P (arg))
2164 arg = TREE_OPERAND (arg, 0);
2165 if (TREE_TYPE (arg))
2166 STRIP_NOPS (arg);
2167 if (TREE_CODE (arg) == ADDR_EXPR)
2168 arg = TREE_OPERAND (arg, 0);
2169 if (VAR_OR_FUNCTION_DECL_P (arg))
2171 if (! TREE_PUBLIC (arg))
2172 vis = VISIBILITY_ANON;
2173 else
2174 vis = DECL_VISIBILITY (arg);
2177 if (vis)
2178 constrain_visibility (decl, vis, true);
2182 /* Like c_determine_visibility, but with additional C++-specific
2183 behavior.
2185 Function-scope entities can rely on the function's visibility because
2186 it is set in start_preparsed_function.
2188 Class-scope entities cannot rely on the class's visibility until the end
2189 of the enclosing class definition.
2191 Note that because namespaces have multiple independent definitions,
2192 namespace visibility is handled elsewhere using the #pragma visibility
2193 machinery rather than by decorating the namespace declaration.
2195 The goal is for constraints from the type to give a diagnostic, and
2196 other constraints to be applied silently. */
2198 void
2199 determine_visibility (tree decl)
2201 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2203 /* Only relevant for names with external linkage. */
2204 if (!TREE_PUBLIC (decl))
2205 return;
2207 /* Cloned constructors and destructors get the same visibility as
2208 the underlying function. That should be set up in
2209 maybe_clone_body. */
2210 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2212 bool orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2213 enum symbol_visibility orig_visibility = DECL_VISIBILITY (decl);
2215 /* The decl may be a template instantiation, which could influence
2216 visibilty. */
2217 tree template_decl = NULL_TREE;
2218 if (TREE_CODE (decl) == TYPE_DECL)
2220 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2222 if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
2223 template_decl = decl;
2225 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2226 template_decl = decl;
2228 else if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
2229 template_decl = decl;
2231 /* If DECL is a member of a class, visibility specifiers on the
2232 class can influence the visibility of the DECL. */
2233 tree class_type = NULL_TREE;
2234 if (DECL_CLASS_SCOPE_P (decl))
2235 class_type = DECL_CONTEXT (decl);
2236 else
2238 /* Not a class member. */
2240 /* Virtual tables have DECL_CONTEXT set to their associated class,
2241 so they are automatically handled above. */
2242 gcc_assert (!VAR_P (decl)
2243 || !DECL_VTABLE_OR_VTT_P (decl));
2245 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2247 /* Local statics and classes get the visibility of their
2248 containing function by default, except that
2249 -fvisibility-inlines-hidden doesn't affect them. */
2250 tree fn = DECL_CONTEXT (decl);
2251 if (DECL_VISIBILITY_SPECIFIED (fn))
2253 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2254 DECL_VISIBILITY_SPECIFIED (decl) =
2255 DECL_VISIBILITY_SPECIFIED (fn);
2257 else
2259 if (DECL_CLASS_SCOPE_P (fn))
2260 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2261 else if (determine_hidden_inline (fn))
2263 DECL_VISIBILITY (decl) = default_visibility;
2264 DECL_VISIBILITY_SPECIFIED (decl) =
2265 visibility_options.inpragma;
2267 else
2269 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2270 DECL_VISIBILITY_SPECIFIED (decl) =
2271 DECL_VISIBILITY_SPECIFIED (fn);
2275 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2276 but have no TEMPLATE_INFO. Their containing template
2277 function does, and the local class could be constrained
2278 by that. */
2279 if (template_decl)
2280 template_decl = fn;
2282 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2283 && flag_visibility_ms_compat)
2285 /* Under -fvisibility-ms-compat, types are visible by default,
2286 even though their contents aren't. */
2287 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2288 int underlying_vis = type_visibility (underlying_type);
2289 if (underlying_vis == VISIBILITY_ANON
2290 || (CLASS_TYPE_P (underlying_type)
2291 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2292 constrain_visibility (decl, underlying_vis, false);
2293 else
2294 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2296 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2298 /* tinfo visibility is based on the type it's for. */
2299 constrain_visibility
2300 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2302 /* Give the target a chance to override the visibility associated
2303 with DECL. */
2304 if (TREE_PUBLIC (decl)
2305 && !DECL_REALLY_EXTERN (decl)
2306 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2307 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2308 targetm.cxx.determine_class_data_visibility (decl);
2310 else if (template_decl)
2311 /* Template instantiations and specializations get visibility based
2312 on their template unless they override it with an attribute. */;
2313 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2315 if (determine_hidden_inline (decl))
2316 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2317 else
2319 /* Set default visibility to whatever the user supplied with
2320 #pragma GCC visibility or a namespace visibility attribute. */
2321 DECL_VISIBILITY (decl) = default_visibility;
2322 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2327 if (template_decl)
2329 /* If the specialization doesn't specify visibility, use the
2330 visibility from the template. */
2331 tree tinfo = get_template_info (template_decl);
2332 tree args = TI_ARGS (tinfo);
2333 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2334 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2335 : DECL_ATTRIBUTES (decl));
2337 if (args != error_mark_node)
2339 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2341 if (!DECL_VISIBILITY_SPECIFIED (decl))
2343 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2344 && determine_hidden_inline (decl))
2345 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2346 else
2348 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2349 DECL_VISIBILITY_SPECIFIED (decl)
2350 = DECL_VISIBILITY_SPECIFIED (pattern);
2354 if (args
2355 /* Template argument visibility outweighs #pragma or namespace
2356 visibility, but not an explicit attribute. */
2357 && !lookup_attribute ("visibility", attribs))
2359 int depth = TMPL_ARGS_DEPTH (args);
2360 if (DECL_VISIBILITY_SPECIFIED (decl))
2362 /* A class template member with explicit visibility
2363 overrides the class visibility, so we need to apply
2364 all the levels of template args directly. */
2365 int i;
2366 for (i = 1; i <= depth; ++i)
2368 tree lev = TMPL_ARGS_LEVEL (args, i);
2369 constrain_visibility_for_template (decl, lev);
2372 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2373 /* Limit visibility based on its template arguments. */
2374 constrain_visibility_for_template (decl, args);
2379 if (class_type)
2380 determine_visibility_from_class (decl, class_type);
2382 if (decl_anon_ns_mem_p (decl))
2383 /* Names in an anonymous namespace get internal linkage.
2384 This might change once we implement export. */
2385 constrain_visibility (decl, VISIBILITY_ANON, false);
2386 else if (TREE_CODE (decl) != TYPE_DECL)
2388 /* Propagate anonymity from type to decl. */
2389 int tvis = type_visibility (TREE_TYPE (decl));
2390 if (tvis == VISIBILITY_ANON
2391 || ! DECL_VISIBILITY_SPECIFIED (decl))
2392 constrain_visibility (decl, tvis, false);
2394 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2395 /* DR 757: A type without linkage shall not be used as the type of a
2396 variable or function with linkage, unless
2397 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2398 o the variable or function is not used (3.2 [basic.def.odr]) or is
2399 defined in the same translation unit.
2401 Since non-extern "C" decls need to be defined in the same
2402 translation unit, we can make the type internal. */
2403 constrain_visibility (decl, VISIBILITY_ANON, false);
2405 /* If visibility changed and DECL already has DECL_RTL, ensure
2406 symbol flags are updated. */
2407 if ((DECL_VISIBILITY (decl) != orig_visibility
2408 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2409 && ((VAR_P (decl) && TREE_STATIC (decl))
2410 || TREE_CODE (decl) == FUNCTION_DECL)
2411 && DECL_RTL_SET_P (decl))
2412 make_decl_rtl (decl);
2415 /* By default, static data members and function members receive
2416 the visibility of their containing class. */
2418 static void
2419 determine_visibility_from_class (tree decl, tree class_type)
2421 if (DECL_VISIBILITY_SPECIFIED (decl))
2422 return;
2424 if (determine_hidden_inline (decl))
2425 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2426 else
2428 /* Default to the class visibility. */
2429 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2430 DECL_VISIBILITY_SPECIFIED (decl)
2431 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2434 /* Give the target a chance to override the visibility associated
2435 with DECL. */
2436 if (VAR_P (decl)
2437 && (DECL_TINFO_P (decl)
2438 || (DECL_VTABLE_OR_VTT_P (decl)
2439 /* Construction virtual tables are not exported because
2440 they cannot be referred to from other object files;
2441 their name is not standardized by the ABI. */
2442 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2443 && TREE_PUBLIC (decl)
2444 && !DECL_REALLY_EXTERN (decl)
2445 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2446 targetm.cxx.determine_class_data_visibility (decl);
2449 /* Returns true iff DECL is an inline that should get hidden visibility
2450 because of -fvisibility-inlines-hidden. */
2452 static bool
2453 determine_hidden_inline (tree decl)
2455 return (visibility_options.inlines_hidden
2456 /* Don't do this for inline templates; specializations might not be
2457 inline, and we don't want them to inherit the hidden
2458 visibility. We'll set it here for all inline instantiations. */
2459 && !processing_template_decl
2460 && TREE_CODE (decl) == FUNCTION_DECL
2461 && DECL_DECLARED_INLINE_P (decl)
2462 && (! DECL_LANG_SPECIFIC (decl)
2463 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2466 /* Constrain the visibility of a class TYPE based on the visibility of its
2467 field types. Warn if any fields require lesser visibility. */
2469 void
2470 constrain_class_visibility (tree type)
2472 tree binfo;
2473 tree t;
2474 int i;
2476 int vis = type_visibility (type);
2478 if (vis == VISIBILITY_ANON
2479 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2480 return;
2482 /* Don't warn about visibility if the class has explicit visibility. */
2483 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2484 vis = VISIBILITY_INTERNAL;
2486 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2487 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2489 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2490 int subvis = type_visibility (ftype);
2492 if (subvis == VISIBILITY_ANON)
2494 if (!in_main_input_context())
2496 tree nlt = no_linkage_check (ftype, /*relaxed_p=*/false);
2497 if (nlt)
2499 if (same_type_p (TREE_TYPE (t), nlt))
2500 warning (OPT_Wsubobject_linkage, "\
2501 %qT has a field %qD whose type has no linkage",
2502 type, t);
2503 else
2504 warning (OPT_Wsubobject_linkage, "\
2505 %qT has a field %qD whose type depends on the type %qT which has no linkage",
2506 type, t, nlt);
2508 else
2509 warning (OPT_Wsubobject_linkage, "\
2510 %qT has a field %qD whose type uses the anonymous namespace",
2511 type, t);
2514 else if (MAYBE_CLASS_TYPE_P (ftype)
2515 && vis < VISIBILITY_HIDDEN
2516 && subvis >= VISIBILITY_HIDDEN)
2517 warning (OPT_Wattributes, "\
2518 %qT declared with greater visibility than the type of its field %qD",
2519 type, t);
2522 binfo = TYPE_BINFO (type);
2523 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2525 int subvis = type_visibility (TREE_TYPE (t));
2527 if (subvis == VISIBILITY_ANON)
2529 if (!in_main_input_context())
2531 tree nlt = no_linkage_check (TREE_TYPE (t), /*relaxed_p=*/false);
2532 if (nlt)
2534 if (same_type_p (TREE_TYPE (t), nlt))
2535 warning (OPT_Wsubobject_linkage, "\
2536 %qT has a base %qT whose type has no linkage",
2537 type, TREE_TYPE (t));
2538 else
2539 warning (OPT_Wsubobject_linkage, "\
2540 %qT has a base %qT whose type depends on the type %qT which has no linkage",
2541 type, TREE_TYPE (t), nlt);
2543 else
2544 warning (OPT_Wsubobject_linkage, "\
2545 %qT has a base %qT whose type uses the anonymous namespace",
2546 type, TREE_TYPE (t));
2549 else if (vis < VISIBILITY_HIDDEN
2550 && subvis >= VISIBILITY_HIDDEN)
2551 warning (OPT_Wattributes, "\
2552 %qT declared with greater visibility than its base %qT",
2553 type, TREE_TYPE (t));
2557 /* Functions for adjusting the visibility of a tagged type and its nested
2558 types and declarations when it gets a name for linkage purposes from a
2559 typedef. */
2561 static void bt_reset_linkage_1 (binding_entry, void *);
2562 static void bt_reset_linkage_2 (binding_entry, void *);
2564 /* First reset the visibility of all the types. */
2566 static void
2567 reset_type_linkage_1 (tree type)
2569 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2570 if (CLASS_TYPE_P (type))
2571 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2572 bt_reset_linkage_1, NULL);
2574 static void
2575 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2577 reset_type_linkage_1 (b->type);
2580 /* Then reset the visibility of any static data members or member
2581 functions that use those types. */
2583 static void
2584 reset_decl_linkage (tree decl)
2586 if (TREE_PUBLIC (decl))
2587 return;
2588 if (DECL_CLONED_FUNCTION_P (decl))
2589 return;
2590 TREE_PUBLIC (decl) = true;
2591 DECL_INTERFACE_KNOWN (decl) = false;
2592 determine_visibility (decl);
2593 tentative_decl_linkage (decl);
2596 static void
2597 reset_type_linkage_2 (tree type)
2599 if (CLASS_TYPE_P (type))
2601 if (tree vt = CLASSTYPE_VTABLES (type))
2603 tree name = mangle_vtbl_for_type (type);
2604 DECL_NAME (vt) = name;
2605 SET_DECL_ASSEMBLER_NAME (vt, name);
2606 reset_decl_linkage (vt);
2608 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2610 tree name = mangle_typeinfo_for_type (type);
2611 DECL_NAME (ti) = name;
2612 SET_DECL_ASSEMBLER_NAME (ti, name);
2613 TREE_TYPE (name) = type;
2614 reset_decl_linkage (ti);
2616 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2618 tree mem = STRIP_TEMPLATE (m);
2619 if (TREE_CODE (mem) == VAR_DECL || TREE_CODE (mem) == FUNCTION_DECL)
2620 reset_decl_linkage (mem);
2622 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2623 bt_reset_linkage_2, NULL);
2627 static void
2628 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2630 reset_type_linkage_2 (b->type);
2632 void
2633 reset_type_linkage (tree type)
2635 reset_type_linkage_1 (type);
2636 reset_type_linkage_2 (type);
2639 /* Set up our initial idea of what the linkage of DECL should be. */
2641 void
2642 tentative_decl_linkage (tree decl)
2644 if (DECL_INTERFACE_KNOWN (decl))
2645 /* We've already made a decision as to how this function will
2646 be handled. */;
2647 else if (vague_linkage_p (decl))
2649 if (TREE_CODE (decl) == FUNCTION_DECL
2650 && decl_defined_p (decl))
2652 DECL_EXTERNAL (decl) = 1;
2653 DECL_NOT_REALLY_EXTERN (decl) = 1;
2654 note_vague_linkage_fn (decl);
2655 /* A non-template inline function with external linkage will
2656 always be COMDAT. As we must eventually determine the
2657 linkage of all functions, and as that causes writes to
2658 the data mapped in from the PCH file, it's advantageous
2659 to mark the functions at this point. */
2660 if (DECL_DECLARED_INLINE_P (decl)
2661 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2662 || DECL_DEFAULTED_FN (decl)))
2664 /* This function must have external linkage, as
2665 otherwise DECL_INTERFACE_KNOWN would have been
2666 set. */
2667 gcc_assert (TREE_PUBLIC (decl));
2668 comdat_linkage (decl);
2669 DECL_INTERFACE_KNOWN (decl) = 1;
2672 else if (VAR_P (decl))
2673 maybe_commonize_var (decl);
2677 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2678 for DECL has not already been determined, do so now by setting
2679 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2680 function is called entities with vague linkage whose definitions
2681 are available must have TREE_PUBLIC set.
2683 If this function decides to place DECL in COMDAT, it will set
2684 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2685 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2686 callers defer that decision until it is clear that DECL is actually
2687 required. */
2689 void
2690 import_export_decl (tree decl)
2692 int emit_p;
2693 bool comdat_p;
2694 bool import_p;
2695 tree class_type = NULL_TREE;
2697 if (DECL_INTERFACE_KNOWN (decl))
2698 return;
2700 /* We cannot determine what linkage to give to an entity with vague
2701 linkage until the end of the file. For example, a virtual table
2702 for a class will be defined if and only if the key method is
2703 defined in this translation unit. As a further example, consider
2704 that when compiling a translation unit that uses PCH file with
2705 "-frepo" it would be incorrect to make decisions about what
2706 entities to emit when building the PCH; those decisions must be
2707 delayed until the repository information has been processed. */
2708 gcc_assert (at_eof);
2709 /* Object file linkage for explicit instantiations is handled in
2710 mark_decl_instantiated. For static variables in functions with
2711 vague linkage, maybe_commonize_var is used.
2713 Therefore, the only declarations that should be provided to this
2714 function are those with external linkage that are:
2716 * implicit instantiations of function templates
2718 * inline function
2720 * implicit instantiations of static data members of class
2721 templates
2723 * virtual tables
2725 * typeinfo objects
2727 Furthermore, all entities that reach this point must have a
2728 definition available in this translation unit.
2730 The following assertions check these conditions. */
2731 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2732 /* Any code that creates entities with TREE_PUBLIC cleared should
2733 also set DECL_INTERFACE_KNOWN. */
2734 gcc_assert (TREE_PUBLIC (decl));
2735 if (TREE_CODE (decl) == FUNCTION_DECL)
2736 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2737 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2738 || DECL_DECLARED_INLINE_P (decl));
2739 else
2740 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2741 || DECL_VTABLE_OR_VTT_P (decl)
2742 || DECL_TINFO_P (decl));
2743 /* Check that a definition of DECL is available in this translation
2744 unit. */
2745 gcc_assert (!DECL_REALLY_EXTERN (decl));
2747 /* Assume that DECL will not have COMDAT linkage. */
2748 comdat_p = false;
2749 /* Assume that DECL will not be imported into this translation
2750 unit. */
2751 import_p = false;
2753 /* See if the repository tells us whether or not to emit DECL in
2754 this translation unit. */
2755 emit_p = repo_emit_p (decl);
2756 if (emit_p == 0)
2757 import_p = true;
2758 else if (emit_p == 1)
2760 /* The repository indicates that this entity should be defined
2761 here. Make sure the back end honors that request. */
2762 mark_needed (decl);
2763 /* Output the definition as an ordinary strong definition. */
2764 DECL_EXTERNAL (decl) = 0;
2765 DECL_INTERFACE_KNOWN (decl) = 1;
2766 return;
2769 if (import_p)
2770 /* We have already decided what to do with this DECL; there is no
2771 need to check anything further. */
2773 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2775 class_type = DECL_CONTEXT (decl);
2776 import_export_class (class_type);
2777 if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2778 && CLASSTYPE_INTERFACE_ONLY (class_type))
2779 import_p = true;
2780 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2781 && !CLASSTYPE_USE_TEMPLATE (class_type)
2782 && CLASSTYPE_KEY_METHOD (class_type)
2783 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2784 /* The ABI requires that all virtual tables be emitted with
2785 COMDAT linkage. However, on systems where COMDAT symbols
2786 don't show up in the table of contents for a static
2787 archive, or on systems without weak symbols (where we
2788 approximate COMDAT linkage by using internal linkage), the
2789 linker will report errors about undefined symbols because
2790 it will not see the virtual table definition. Therefore,
2791 in the case that we know that the virtual table will be
2792 emitted in only one translation unit, we make the virtual
2793 table an ordinary definition with external linkage. */
2794 DECL_EXTERNAL (decl) = 0;
2795 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2797 /* CLASS_TYPE is being exported from this translation unit,
2798 so DECL should be defined here. */
2799 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2800 /* If a class is declared in a header with the "extern
2801 template" extension, then it will not be instantiated,
2802 even in translation units that would normally require
2803 it. Often such classes are explicitly instantiated in
2804 one translation unit. Therefore, the explicit
2805 instantiation must be made visible to other translation
2806 units. */
2807 DECL_EXTERNAL (decl) = 0;
2808 else
2810 /* The generic C++ ABI says that class data is always
2811 COMDAT, even if there is a key function. Some
2812 variants (e.g., the ARM EABI) says that class data
2813 only has COMDAT linkage if the class data might be
2814 emitted in more than one translation unit. When the
2815 key method can be inline and is inline, we still have
2816 to arrange for comdat even though
2817 class_data_always_comdat is false. */
2818 if (!CLASSTYPE_KEY_METHOD (class_type)
2819 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2820 || targetm.cxx.class_data_always_comdat ())
2822 /* The ABI requires COMDAT linkage. Normally, we
2823 only emit COMDAT things when they are needed;
2824 make sure that we realize that this entity is
2825 indeed needed. */
2826 comdat_p = true;
2827 mark_needed (decl);
2831 else if (!flag_implicit_templates
2832 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2833 import_p = true;
2834 else
2835 comdat_p = true;
2837 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2839 tree type = TREE_TYPE (DECL_NAME (decl));
2840 if (CLASS_TYPE_P (type))
2842 class_type = type;
2843 import_export_class (type);
2844 if (CLASSTYPE_INTERFACE_KNOWN (type)
2845 && TYPE_POLYMORPHIC_P (type)
2846 && CLASSTYPE_INTERFACE_ONLY (type)
2847 /* If -fno-rtti was specified, then we cannot be sure
2848 that RTTI information will be emitted with the
2849 virtual table of the class, so we must emit it
2850 wherever it is used. */
2851 && flag_rtti)
2852 import_p = true;
2853 else
2855 if (CLASSTYPE_INTERFACE_KNOWN (type)
2856 && !CLASSTYPE_INTERFACE_ONLY (type))
2858 comdat_p = (targetm.cxx.class_data_always_comdat ()
2859 || (CLASSTYPE_KEY_METHOD (type)
2860 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2861 mark_needed (decl);
2862 if (!flag_weak)
2864 comdat_p = false;
2865 DECL_EXTERNAL (decl) = 0;
2868 else
2869 comdat_p = true;
2872 else
2873 comdat_p = true;
2875 else if (DECL_TEMPLOID_INSTANTIATION (decl))
2877 /* DECL is an implicit instantiation of a function or static
2878 data member. */
2879 if ((flag_implicit_templates
2880 && !flag_use_repository)
2881 || (flag_implicit_inline_templates
2882 && TREE_CODE (decl) == FUNCTION_DECL
2883 && DECL_DECLARED_INLINE_P (decl)))
2884 comdat_p = true;
2885 else
2886 /* If we are not implicitly generating templates, then mark
2887 this entity as undefined in this translation unit. */
2888 import_p = true;
2890 else if (DECL_FUNCTION_MEMBER_P (decl))
2892 if (!DECL_DECLARED_INLINE_P (decl))
2894 tree ctype = DECL_CONTEXT (decl);
2895 import_export_class (ctype);
2896 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2898 DECL_NOT_REALLY_EXTERN (decl)
2899 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2900 || (DECL_DECLARED_INLINE_P (decl)
2901 && ! flag_implement_inlines
2902 && !DECL_VINDEX (decl)));
2904 if (!DECL_NOT_REALLY_EXTERN (decl))
2905 DECL_EXTERNAL (decl) = 1;
2907 /* Always make artificials weak. */
2908 if (DECL_ARTIFICIAL (decl) && flag_weak)
2909 comdat_p = true;
2910 else
2911 maybe_make_one_only (decl);
2914 else
2915 comdat_p = true;
2917 else
2918 comdat_p = true;
2920 if (import_p)
2922 /* If we are importing DECL into this translation unit, mark is
2923 an undefined here. */
2924 DECL_EXTERNAL (decl) = 1;
2925 DECL_NOT_REALLY_EXTERN (decl) = 0;
2927 else if (comdat_p)
2929 /* If we decided to put DECL in COMDAT, mark it accordingly at
2930 this point. */
2931 comdat_linkage (decl);
2934 DECL_INTERFACE_KNOWN (decl) = 1;
2937 /* Return an expression that performs the destruction of DECL, which
2938 must be a VAR_DECL whose type has a non-trivial destructor, or is
2939 an array whose (innermost) elements have a non-trivial destructor. */
2941 tree
2942 build_cleanup (tree decl)
2944 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
2945 gcc_assert (clean != NULL_TREE);
2946 return clean;
2949 /* Returns the initialization guard variable for the variable DECL,
2950 which has static storage duration. */
2952 tree
2953 get_guard (tree decl)
2955 tree sname;
2956 tree guard;
2958 sname = mangle_guard_variable (decl);
2959 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2960 if (! guard)
2962 tree guard_type;
2964 /* We use a type that is big enough to contain a mutex as well
2965 as an integer counter. */
2966 guard_type = targetm.cxx.guard_type ();
2967 guard = build_decl (DECL_SOURCE_LOCATION (decl),
2968 VAR_DECL, sname, guard_type);
2970 /* The guard should have the same linkage as what it guards. */
2971 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
2972 TREE_STATIC (guard) = TREE_STATIC (decl);
2973 DECL_COMMON (guard) = DECL_COMMON (decl);
2974 DECL_COMDAT (guard) = DECL_COMDAT (decl);
2975 CP_DECL_THREAD_LOCAL_P (guard) = CP_DECL_THREAD_LOCAL_P (decl);
2976 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
2977 if (DECL_ONE_ONLY (decl))
2978 make_decl_one_only (guard, cxx_comdat_group (guard));
2979 if (TREE_PUBLIC (decl))
2980 DECL_WEAK (guard) = DECL_WEAK (decl);
2981 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
2982 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
2984 DECL_ARTIFICIAL (guard) = 1;
2985 DECL_IGNORED_P (guard) = 1;
2986 TREE_USED (guard) = 1;
2987 pushdecl_top_level_and_finish (guard, NULL_TREE);
2989 return guard;
2992 /* Return an atomic load of src with the appropriate memory model. */
2994 static tree
2995 build_atomic_load_byte (tree src, HOST_WIDE_INT model)
2997 tree ptr_type = build_pointer_type (char_type_node);
2998 tree mem_model = build_int_cst (integer_type_node, model);
2999 tree t, addr, val;
3000 unsigned int size;
3001 int fncode;
3003 size = tree_to_uhwi (TYPE_SIZE_UNIT (char_type_node));
3005 fncode = BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1;
3006 t = builtin_decl_implicit ((enum built_in_function) fncode);
3008 addr = build1 (ADDR_EXPR, ptr_type, src);
3009 val = build_call_expr (t, 2, addr, mem_model);
3010 return val;
3013 /* Return those bits of the GUARD variable that should be set when the
3014 guarded entity is actually initialized. */
3016 static tree
3017 get_guard_bits (tree guard)
3019 if (!targetm.cxx.guard_mask_bit ())
3021 /* We only set the first byte of the guard, in order to leave room
3022 for a mutex in the high-order bits. */
3023 guard = build1 (ADDR_EXPR,
3024 build_pointer_type (TREE_TYPE (guard)),
3025 guard);
3026 guard = build1 (NOP_EXPR,
3027 build_pointer_type (char_type_node),
3028 guard);
3029 guard = build1 (INDIRECT_REF, char_type_node, guard);
3032 return guard;
3035 /* Return an expression which determines whether or not the GUARD
3036 variable has already been initialized. */
3038 tree
3039 get_guard_cond (tree guard, bool thread_safe)
3041 tree guard_value;
3043 if (!thread_safe)
3044 guard = get_guard_bits (guard);
3045 else
3046 guard = build_atomic_load_byte (guard, MEMMODEL_ACQUIRE);
3048 /* Mask off all but the low bit. */
3049 if (targetm.cxx.guard_mask_bit ())
3051 guard_value = integer_one_node;
3052 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3053 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3054 guard = cp_build_binary_op (input_location,
3055 BIT_AND_EXPR, guard, guard_value,
3056 tf_warning_or_error);
3059 guard_value = integer_zero_node;
3060 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3061 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3062 return cp_build_binary_op (input_location,
3063 EQ_EXPR, guard, guard_value,
3064 tf_warning_or_error);
3067 /* Return an expression which sets the GUARD variable, indicating that
3068 the variable being guarded has been initialized. */
3070 tree
3071 set_guard (tree guard)
3073 tree guard_init;
3075 /* Set the GUARD to one. */
3076 guard = get_guard_bits (guard);
3077 guard_init = integer_one_node;
3078 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3079 guard_init = fold_convert (TREE_TYPE (guard), guard_init);
3080 return cp_build_modify_expr (input_location, guard, NOP_EXPR, guard_init,
3081 tf_warning_or_error);
3084 /* Returns true iff we can tell that VAR does not have a dynamic
3085 initializer. */
3087 static bool
3088 var_defined_without_dynamic_init (tree var)
3090 /* If it's defined in another TU, we can't tell. */
3091 if (DECL_EXTERNAL (var))
3092 return false;
3093 /* If it has a non-trivial destructor, registering the destructor
3094 counts as dynamic initialization. */
3095 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3096 return false;
3097 /* If it's in this TU, its initializer has been processed, unless
3098 it's a case of self-initialization, then DECL_INITIALIZED_P is
3099 false while the initializer is handled by finish_id_expression. */
3100 if (!DECL_INITIALIZED_P (var))
3101 return false;
3102 /* If it has no initializer or a constant one, it's not dynamic. */
3103 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3104 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3107 /* Returns true iff VAR is a variable that needs uses to be
3108 wrapped for possible dynamic initialization. */
3110 static bool
3111 var_needs_tls_wrapper (tree var)
3113 return (!error_operand_p (var)
3114 && CP_DECL_THREAD_LOCAL_P (var)
3115 && !DECL_GNU_TLS_P (var)
3116 && !DECL_FUNCTION_SCOPE_P (var)
3117 && !var_defined_without_dynamic_init (var));
3120 /* Get the FUNCTION_DECL for the shared TLS init function for this
3121 translation unit. */
3123 static tree
3124 get_local_tls_init_fn (void)
3126 tree sname = get_identifier ("__tls_init");
3127 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3128 if (!fn)
3130 fn = build_lang_decl (FUNCTION_DECL, sname,
3131 build_function_type (void_type_node,
3132 void_list_node));
3133 SET_DECL_LANGUAGE (fn, lang_c);
3134 TREE_PUBLIC (fn) = false;
3135 DECL_ARTIFICIAL (fn) = true;
3136 mark_used (fn);
3137 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3139 return fn;
3142 /* Get a FUNCTION_DECL for the init function for the thread_local
3143 variable VAR. The init function will be an alias to the function
3144 that initializes all the non-local TLS variables in the translation
3145 unit. The init function is only used by the wrapper function. */
3147 static tree
3148 get_tls_init_fn (tree var)
3150 /* Only C++11 TLS vars need this init fn. */
3151 if (!var_needs_tls_wrapper (var))
3152 return NULL_TREE;
3154 /* If -fno-extern-tls-init, assume that we don't need to call
3155 a tls init function for a variable defined in another TU. */
3156 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3157 return NULL_TREE;
3159 #ifdef ASM_OUTPUT_DEF
3160 /* If the variable is internal, or if we can't generate aliases,
3161 call the local init function directly. */
3162 if (!TREE_PUBLIC (var))
3163 #endif
3164 return get_local_tls_init_fn ();
3166 tree sname = mangle_tls_init_fn (var);
3167 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3168 if (!fn)
3170 fn = build_lang_decl (FUNCTION_DECL, sname,
3171 build_function_type (void_type_node,
3172 void_list_node));
3173 SET_DECL_LANGUAGE (fn, lang_c);
3174 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3175 DECL_ARTIFICIAL (fn) = true;
3176 DECL_COMDAT (fn) = DECL_COMDAT (var);
3177 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3178 if (DECL_ONE_ONLY (var))
3179 make_decl_one_only (fn, cxx_comdat_group (fn));
3180 if (TREE_PUBLIC (var))
3182 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3183 /* If the variable is defined somewhere else and might have static
3184 initialization, make the init function a weak reference. */
3185 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3186 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3187 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3188 && DECL_EXTERNAL (var))
3189 declare_weak (fn);
3190 else
3191 DECL_WEAK (fn) = DECL_WEAK (var);
3193 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3194 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3195 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3196 DECL_IGNORED_P (fn) = 1;
3197 mark_used (fn);
3199 DECL_BEFRIENDING_CLASSES (fn) = var;
3201 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3203 return fn;
3206 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3207 variable VAR. The wrapper function calls the init function (if any) for
3208 VAR and then returns a reference to VAR. The wrapper function is used
3209 in place of VAR everywhere VAR is mentioned. */
3211 tree
3212 get_tls_wrapper_fn (tree var)
3214 /* Only C++11 TLS vars need this wrapper fn. */
3215 if (!var_needs_tls_wrapper (var))
3216 return NULL_TREE;
3218 tree sname = mangle_tls_wrapper_fn (var);
3219 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3220 if (!fn)
3222 /* A named rvalue reference is an lvalue, so the wrapper should
3223 always return an lvalue reference. */
3224 tree type = non_reference (TREE_TYPE (var));
3225 type = build_reference_type (type);
3226 tree fntype = build_function_type (type, void_list_node);
3227 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3228 SET_DECL_LANGUAGE (fn, lang_c);
3229 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3230 DECL_ARTIFICIAL (fn) = true;
3231 DECL_IGNORED_P (fn) = 1;
3232 /* The wrapper is inline and emitted everywhere var is used. */
3233 DECL_DECLARED_INLINE_P (fn) = true;
3234 if (TREE_PUBLIC (var))
3236 comdat_linkage (fn);
3237 #ifdef HAVE_GAS_HIDDEN
3238 /* Make the wrapper bind locally; there's no reason to share
3239 the wrapper between multiple shared objects. */
3240 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3241 DECL_VISIBILITY_SPECIFIED (fn) = true;
3242 #endif
3244 if (!TREE_PUBLIC (fn))
3245 DECL_INTERFACE_KNOWN (fn) = true;
3246 mark_used (fn);
3247 note_vague_linkage_fn (fn);
3249 #if 0
3250 /* We want CSE to commonize calls to the wrapper, but marking it as
3251 pure is unsafe since it has side-effects. I guess we need a new
3252 ECF flag even weaker than ECF_PURE. FIXME! */
3253 DECL_PURE_P (fn) = true;
3254 #endif
3256 DECL_BEFRIENDING_CLASSES (fn) = var;
3258 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3260 return fn;
3263 /* At EOF, generate the definition for the TLS wrapper function FN:
3265 T& var_wrapper() {
3266 if (init_fn) init_fn();
3267 return var;
3268 } */
3270 static void
3271 generate_tls_wrapper (tree fn)
3273 tree var = DECL_BEFRIENDING_CLASSES (fn);
3275 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3276 tree body = begin_function_body ();
3277 /* Only call the init fn if there might be one. */
3278 if (tree init_fn = get_tls_init_fn (var))
3280 tree if_stmt = NULL_TREE;
3281 /* If init_fn is a weakref, make sure it exists before calling. */
3282 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3284 if_stmt = begin_if_stmt ();
3285 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3286 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3287 NE_EXPR, addr, nullptr_node,
3288 tf_warning_or_error);
3289 finish_if_stmt_cond (cond, if_stmt);
3291 finish_expr_stmt (build_cxx_call
3292 (init_fn, 0, NULL, tf_warning_or_error));
3293 if (if_stmt)
3295 finish_then_clause (if_stmt);
3296 finish_if_stmt (if_stmt);
3299 else
3300 /* If there's no initialization, the wrapper is a constant function. */
3301 TREE_READONLY (fn) = true;
3302 finish_return_stmt (convert_from_reference (var));
3303 finish_function_body (body);
3304 expand_or_defer_fn (finish_function (0));
3307 /* Start the process of running a particular set of global constructors
3308 or destructors. Subroutine of do_[cd]tors. Also called from
3309 vtv_start_verification_constructor_init_function. */
3311 static tree
3312 start_objects (int method_type, int initp)
3314 tree body;
3315 tree fndecl;
3316 char type[14];
3318 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3320 if (initp != DEFAULT_INIT_PRIORITY)
3322 char joiner;
3324 #ifdef JOINER
3325 joiner = JOINER;
3326 #else
3327 joiner = '_';
3328 #endif
3330 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3332 else
3333 sprintf (type, "sub_%c", method_type);
3335 fndecl = build_lang_decl (FUNCTION_DECL,
3336 get_file_function_name (type),
3337 build_function_type_list (void_type_node,
3338 NULL_TREE));
3339 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3341 TREE_PUBLIC (current_function_decl) = 0;
3343 /* Mark as artificial because it's not explicitly in the user's
3344 source code. */
3345 DECL_ARTIFICIAL (current_function_decl) = 1;
3347 /* Mark this declaration as used to avoid spurious warnings. */
3348 TREE_USED (current_function_decl) = 1;
3350 /* Mark this function as a global constructor or destructor. */
3351 if (method_type == 'I')
3352 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3353 else
3354 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3356 body = begin_compound_stmt (BCS_FN_BODY);
3358 return body;
3361 /* Finish the process of running a particular set of global constructors
3362 or destructors. Subroutine of do_[cd]tors. */
3364 static void
3365 finish_objects (int method_type, int initp, tree body)
3367 tree fn;
3369 /* Finish up. */
3370 finish_compound_stmt (body);
3371 fn = finish_function (0);
3373 if (method_type == 'I')
3375 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3376 decl_init_priority_insert (fn, initp);
3378 else
3380 DECL_STATIC_DESTRUCTOR (fn) = 1;
3381 decl_fini_priority_insert (fn, initp);
3384 expand_or_defer_fn (fn);
3387 /* The names of the parameters to the function created to handle
3388 initializations and destructions for objects with static storage
3389 duration. */
3390 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3391 #define PRIORITY_IDENTIFIER "__priority"
3393 /* The name of the function we create to handle initializations and
3394 destructions for objects with static storage duration. */
3395 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3397 /* The declaration for the __INITIALIZE_P argument. */
3398 static GTY(()) tree initialize_p_decl;
3400 /* The declaration for the __PRIORITY argument. */
3401 static GTY(()) tree priority_decl;
3403 /* The declaration for the static storage duration function. */
3404 static GTY(()) tree ssdf_decl;
3406 /* All the static storage duration functions created in this
3407 translation unit. */
3408 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3410 /* A map from priority levels to information about that priority
3411 level. There may be many such levels, so efficient lookup is
3412 important. */
3413 static splay_tree priority_info_map;
3415 /* Begins the generation of the function that will handle all
3416 initialization and destruction of objects with static storage
3417 duration. The function generated takes two parameters of type
3418 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3419 nonzero, it performs initializations. Otherwise, it performs
3420 destructions. It only performs those initializations or
3421 destructions with the indicated __PRIORITY. The generated function
3422 returns no value.
3424 It is assumed that this function will only be called once per
3425 translation unit. */
3427 static tree
3428 start_static_storage_duration_function (unsigned count)
3430 tree type;
3431 tree body;
3432 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3434 /* Create the identifier for this function. It will be of the form
3435 SSDF_IDENTIFIER_<number>. */
3436 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3438 type = build_function_type_list (void_type_node,
3439 integer_type_node, integer_type_node,
3440 NULL_TREE);
3442 /* Create the FUNCTION_DECL itself. */
3443 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3444 get_identifier (id),
3445 type);
3446 TREE_PUBLIC (ssdf_decl) = 0;
3447 DECL_ARTIFICIAL (ssdf_decl) = 1;
3449 /* Put this function in the list of functions to be called from the
3450 static constructors and destructors. */
3451 if (!ssdf_decls)
3453 vec_alloc (ssdf_decls, 32);
3455 /* Take this opportunity to initialize the map from priority
3456 numbers to information about that priority level. */
3457 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3458 /*delete_key_fn=*/0,
3459 /*delete_value_fn=*/
3460 (splay_tree_delete_value_fn) &free);
3462 /* We always need to generate functions for the
3463 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3464 priorities later, we'll be sure to find the
3465 DEFAULT_INIT_PRIORITY. */
3466 get_priority_info (DEFAULT_INIT_PRIORITY);
3469 vec_safe_push (ssdf_decls, ssdf_decl);
3471 /* Create the argument list. */
3472 initialize_p_decl = cp_build_parm_decl
3473 (ssdf_decl, get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3474 TREE_USED (initialize_p_decl) = 1;
3475 priority_decl = cp_build_parm_decl
3476 (ssdf_decl, get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3477 TREE_USED (priority_decl) = 1;
3479 DECL_CHAIN (initialize_p_decl) = priority_decl;
3480 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3482 /* Put the function in the global scope. */
3483 pushdecl (ssdf_decl);
3485 /* Start the function itself. This is equivalent to declaring the
3486 function as:
3488 static void __ssdf (int __initialize_p, init __priority_p);
3490 It is static because we only need to call this function from the
3491 various constructor and destructor functions for this module. */
3492 start_preparsed_function (ssdf_decl,
3493 /*attrs=*/NULL_TREE,
3494 SF_PRE_PARSED);
3496 /* Set up the scope of the outermost block in the function. */
3497 body = begin_compound_stmt (BCS_FN_BODY);
3499 return body;
3502 /* Finish the generation of the function which performs initialization
3503 and destruction of objects with static storage duration. After
3504 this point, no more such objects can be created. */
3506 static void
3507 finish_static_storage_duration_function (tree body)
3509 /* Close out the function. */
3510 finish_compound_stmt (body);
3511 expand_or_defer_fn (finish_function (0));
3514 /* Return the information about the indicated PRIORITY level. If no
3515 code to handle this level has yet been generated, generate the
3516 appropriate prologue. */
3518 static priority_info
3519 get_priority_info (int priority)
3521 priority_info pi;
3522 splay_tree_node n;
3524 n = splay_tree_lookup (priority_info_map,
3525 (splay_tree_key) priority);
3526 if (!n)
3528 /* Create a new priority information structure, and insert it
3529 into the map. */
3530 pi = XNEW (struct priority_info_s);
3531 pi->initializations_p = 0;
3532 pi->destructions_p = 0;
3533 splay_tree_insert (priority_info_map,
3534 (splay_tree_key) priority,
3535 (splay_tree_value) pi);
3537 else
3538 pi = (priority_info) n->value;
3540 return pi;
3543 /* The effective initialization priority of a DECL. */
3545 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3546 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3547 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3549 /* Whether a DECL needs a guard to protect it against multiple
3550 initialization. */
3552 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3553 || DECL_ONE_ONLY (decl) \
3554 || DECL_WEAK (decl)))
3556 /* Called from one_static_initialization_or_destruction(),
3557 via walk_tree.
3558 Walks the initializer list of a global variable and looks for
3559 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3560 and that have their DECL_CONTEXT() == NULL.
3561 For each such temporary variable, set their DECL_CONTEXT() to
3562 the current function. This is necessary because otherwise
3563 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3564 when trying to refer to a temporary variable that does not have
3565 it's DECL_CONTECT() properly set. */
3566 static tree
3567 fix_temporary_vars_context_r (tree *node,
3568 int * /*unused*/,
3569 void * /*unused1*/)
3571 gcc_assert (current_function_decl);
3573 if (TREE_CODE (*node) == BIND_EXPR)
3575 tree var;
3577 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3578 if (VAR_P (var)
3579 && !DECL_NAME (var)
3580 && DECL_ARTIFICIAL (var)
3581 && !DECL_CONTEXT (var))
3582 DECL_CONTEXT (var) = current_function_decl;
3585 return NULL_TREE;
3588 /* Set up to handle the initialization or destruction of DECL. If
3589 INITP is nonzero, we are initializing the variable. Otherwise, we
3590 are destroying it. */
3592 static void
3593 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3595 tree guard_if_stmt = NULL_TREE;
3596 tree guard;
3598 /* If we are supposed to destruct and there's a trivial destructor,
3599 nothing has to be done. */
3600 if (!initp
3601 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3602 return;
3604 /* Trick the compiler into thinking we are at the file and line
3605 where DECL was declared so that error-messages make sense, and so
3606 that the debugger will show somewhat sensible file and line
3607 information. */
3608 input_location = DECL_SOURCE_LOCATION (decl);
3610 /* Make sure temporary variables in the initialiser all have
3611 their DECL_CONTEXT() set to a value different from NULL_TREE.
3612 This can happen when global variables initializers are built.
3613 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3614 the temporary variables that might have been generated in the
3615 accompanying initializers is NULL_TREE, meaning the variables have been
3616 declared in the global namespace.
3617 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3618 of the temporaries are set to the current function decl. */
3619 cp_walk_tree_without_duplicates (&init,
3620 fix_temporary_vars_context_r,
3621 NULL);
3623 /* Because of:
3625 [class.access.spec]
3627 Access control for implicit calls to the constructors,
3628 the conversion functions, or the destructor called to
3629 create and destroy a static data member is performed as
3630 if these calls appeared in the scope of the member's
3631 class.
3633 we pretend we are in a static member function of the class of
3634 which the DECL is a member. */
3635 if (member_p (decl))
3637 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3638 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3641 /* Assume we don't need a guard. */
3642 guard = NULL_TREE;
3643 /* We need a guard if this is an object with external linkage that
3644 might be initialized in more than one place. (For example, a
3645 static data member of a template, when the data member requires
3646 construction.) */
3647 if (NEEDS_GUARD_P (decl))
3649 tree guard_cond;
3651 guard = get_guard (decl);
3653 /* When using __cxa_atexit, we just check the GUARD as we would
3654 for a local static. */
3655 if (flag_use_cxa_atexit)
3657 /* When using __cxa_atexit, we never try to destroy
3658 anything from a static destructor. */
3659 gcc_assert (initp);
3660 guard_cond = get_guard_cond (guard, false);
3662 /* If we don't have __cxa_atexit, then we will be running
3663 destructors from .fini sections, or their equivalents. So,
3664 we need to know how many times we've tried to initialize this
3665 object. We do initializations only if the GUARD is zero,
3666 i.e., if we are the first to initialize the variable. We do
3667 destructions only if the GUARD is one, i.e., if we are the
3668 last to destroy the variable. */
3669 else if (initp)
3670 guard_cond
3671 = cp_build_binary_op (input_location,
3672 EQ_EXPR,
3673 cp_build_unary_op (PREINCREMENT_EXPR,
3674 guard,
3675 /*noconvert=*/true,
3676 tf_warning_or_error),
3677 integer_one_node,
3678 tf_warning_or_error);
3679 else
3680 guard_cond
3681 = cp_build_binary_op (input_location,
3682 EQ_EXPR,
3683 cp_build_unary_op (PREDECREMENT_EXPR,
3684 guard,
3685 /*noconvert=*/true,
3686 tf_warning_or_error),
3687 integer_zero_node,
3688 tf_warning_or_error);
3690 guard_if_stmt = begin_if_stmt ();
3691 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3695 /* If we're using __cxa_atexit, we have not already set the GUARD,
3696 so we must do so now. */
3697 if (guard && initp && flag_use_cxa_atexit)
3698 finish_expr_stmt (set_guard (guard));
3700 /* Perform the initialization or destruction. */
3701 if (initp)
3703 if (init)
3705 finish_expr_stmt (init);
3706 if (sanitize_flags_p (SANITIZE_ADDRESS, decl))
3708 varpool_node *vnode = varpool_node::get (decl);
3709 if (vnode)
3710 vnode->dynamically_initialized = 1;
3714 /* If we're using __cxa_atexit, register a function that calls the
3715 destructor for the object. */
3716 if (flag_use_cxa_atexit)
3717 finish_expr_stmt (register_dtor_fn (decl));
3719 else
3720 finish_expr_stmt (build_cleanup (decl));
3722 /* Finish the guard if-stmt, if necessary. */
3723 if (guard)
3725 finish_then_clause (guard_if_stmt);
3726 finish_if_stmt (guard_if_stmt);
3729 /* Now that we're done with DECL we don't need to pretend to be a
3730 member of its class any longer. */
3731 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3732 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3735 /* Generate code to do the initialization or destruction of the decls in VARS,
3736 a TREE_LIST of VAR_DECL with static storage duration.
3737 Whether initialization or destruction is performed is specified by INITP. */
3739 static void
3740 do_static_initialization_or_destruction (tree vars, bool initp)
3742 tree node, init_if_stmt, cond;
3744 /* Build the outer if-stmt to check for initialization or destruction. */
3745 init_if_stmt = begin_if_stmt ();
3746 cond = initp ? integer_one_node : integer_zero_node;
3747 cond = cp_build_binary_op (input_location,
3748 EQ_EXPR,
3749 initialize_p_decl,
3750 cond,
3751 tf_warning_or_error);
3752 finish_if_stmt_cond (cond, init_if_stmt);
3754 /* To make sure dynamic construction doesn't access globals from other
3755 compilation units where they might not be yet constructed, for
3756 -fsanitize=address insert __asan_before_dynamic_init call that
3757 prevents access to either all global variables that need construction
3758 in other compilation units, or at least those that haven't been
3759 initialized yet. Variables that need dynamic construction in
3760 the current compilation unit are kept accessible. */
3761 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
3762 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3764 node = vars;
3765 do {
3766 tree decl = TREE_VALUE (node);
3767 tree priority_if_stmt;
3768 int priority;
3769 priority_info pi;
3771 /* If we don't need a destructor, there's nothing to do. Avoid
3772 creating a possibly empty if-stmt. */
3773 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3775 node = TREE_CHAIN (node);
3776 continue;
3779 /* Remember that we had an initialization or finalization at this
3780 priority. */
3781 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3782 pi = get_priority_info (priority);
3783 if (initp)
3784 pi->initializations_p = 1;
3785 else
3786 pi->destructions_p = 1;
3788 /* Conditionalize this initialization on being in the right priority
3789 and being initializing/finalizing appropriately. */
3790 priority_if_stmt = begin_if_stmt ();
3791 cond = cp_build_binary_op (input_location,
3792 EQ_EXPR,
3793 priority_decl,
3794 build_int_cst (NULL_TREE, priority),
3795 tf_warning_or_error);
3796 finish_if_stmt_cond (cond, priority_if_stmt);
3798 /* Process initializers with same priority. */
3799 for (; node
3800 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3801 node = TREE_CHAIN (node))
3802 /* Do one initialization or destruction. */
3803 one_static_initialization_or_destruction (TREE_VALUE (node),
3804 TREE_PURPOSE (node), initp);
3806 /* Finish up the priority if-stmt body. */
3807 finish_then_clause (priority_if_stmt);
3808 finish_if_stmt (priority_if_stmt);
3810 } while (node);
3812 /* Revert what __asan_before_dynamic_init did by calling
3813 __asan_after_dynamic_init. */
3814 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
3815 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3817 /* Finish up the init/destruct if-stmt body. */
3818 finish_then_clause (init_if_stmt);
3819 finish_if_stmt (init_if_stmt);
3822 /* VARS is a list of variables with static storage duration which may
3823 need initialization and/or finalization. Remove those variables
3824 that don't really need to be initialized or finalized, and return
3825 the resulting list. The order in which the variables appear in
3826 VARS is in reverse order of the order in which they should actually
3827 be initialized. The list we return is in the unreversed order;
3828 i.e., the first variable should be initialized first. */
3830 static tree
3831 prune_vars_needing_no_initialization (tree *vars)
3833 tree *var = vars;
3834 tree result = NULL_TREE;
3836 while (*var)
3838 tree t = *var;
3839 tree decl = TREE_VALUE (t);
3840 tree init = TREE_PURPOSE (t);
3842 /* Deal gracefully with error. */
3843 if (error_operand_p (decl))
3845 var = &TREE_CHAIN (t);
3846 continue;
3849 /* The only things that can be initialized are variables. */
3850 gcc_assert (VAR_P (decl));
3852 /* If this object is not defined, we don't need to do anything
3853 here. */
3854 if (DECL_EXTERNAL (decl))
3856 var = &TREE_CHAIN (t);
3857 continue;
3860 /* Also, if the initializer already contains errors, we can bail
3861 out now. */
3862 if (init && TREE_CODE (init) == TREE_LIST
3863 && value_member (error_mark_node, init))
3865 var = &TREE_CHAIN (t);
3866 continue;
3869 /* This variable is going to need initialization and/or
3870 finalization, so we add it to the list. */
3871 *var = TREE_CHAIN (t);
3872 TREE_CHAIN (t) = result;
3873 result = t;
3876 return result;
3879 /* Make sure we have told the back end about all the variables in
3880 VARS. */
3882 static void
3883 write_out_vars (tree vars)
3885 tree v;
3887 for (v = vars; v; v = TREE_CHAIN (v))
3889 tree var = TREE_VALUE (v);
3890 if (!var_finalized_p (var))
3892 import_export_decl (var);
3893 rest_of_decl_compilation (var, 1, 1);
3898 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3899 (otherwise) that will initialize all global objects with static
3900 storage duration having the indicated PRIORITY. */
3902 static void
3903 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3904 location_t *locus)
3906 char function_key;
3907 tree fndecl;
3908 tree body;
3909 size_t i;
3911 input_location = *locus;
3912 /* ??? */
3913 /* Was: locus->line++; */
3915 /* We use `I' to indicate initialization and `D' to indicate
3916 destruction. */
3917 function_key = constructor_p ? 'I' : 'D';
3919 /* We emit the function lazily, to avoid generating empty
3920 global constructors and destructors. */
3921 body = NULL_TREE;
3923 /* For Objective-C++, we may need to initialize metadata found in this module.
3924 This must be done _before_ any other static initializations. */
3925 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3926 && constructor_p && objc_static_init_needed_p ())
3928 body = start_objects (function_key, priority);
3929 objc_generate_static_init_call (NULL_TREE);
3932 /* Call the static storage duration function with appropriate
3933 arguments. */
3934 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
3936 /* Calls to pure or const functions will expand to nothing. */
3937 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3939 tree call;
3941 if (! body)
3942 body = start_objects (function_key, priority);
3944 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
3945 build_int_cst (NULL_TREE,
3946 constructor_p),
3947 build_int_cst (NULL_TREE,
3948 priority),
3949 NULL_TREE);
3950 finish_expr_stmt (call);
3954 /* Close out the function. */
3955 if (body)
3956 finish_objects (function_key, priority, body);
3959 /* Generate constructor and destructor functions for the priority
3960 indicated by N. */
3962 static int
3963 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
3965 location_t *locus = (location_t *) data;
3966 int priority = (int) n->key;
3967 priority_info pi = (priority_info) n->value;
3969 /* Generate the functions themselves, but only if they are really
3970 needed. */
3971 if (pi->initializations_p)
3972 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
3973 if (pi->destructions_p)
3974 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
3976 /* Keep iterating. */
3977 return 0;
3980 /* Return C++ property of T, based on given operation OP. */
3982 static int
3983 cpp_check (tree t, cpp_operation op)
3985 switch (op)
3987 case HAS_DEPENDENT_TEMPLATE_ARGS:
3989 tree ti = CLASSTYPE_TEMPLATE_INFO (t);
3990 if (!ti)
3991 return 0;
3992 ++processing_template_decl;
3993 const bool dep = any_dependent_template_arguments_p (TI_ARGS (ti));
3994 --processing_template_decl;
3995 return dep;
3997 case IS_ABSTRACT:
3998 return DECL_PURE_VIRTUAL_P (t);
3999 case IS_CONSTRUCTOR:
4000 return DECL_CONSTRUCTOR_P (t);
4001 case IS_DESTRUCTOR:
4002 return DECL_DESTRUCTOR_P (t);
4003 case IS_COPY_CONSTRUCTOR:
4004 return DECL_COPY_CONSTRUCTOR_P (t);
4005 case IS_MOVE_CONSTRUCTOR:
4006 return DECL_MOVE_CONSTRUCTOR_P (t);
4007 case IS_TEMPLATE:
4008 return TREE_CODE (t) == TEMPLATE_DECL;
4009 case IS_TRIVIAL:
4010 return trivial_type_p (t);
4011 default:
4012 return 0;
4016 /* Collect source file references recursively, starting from NAMESPC. */
4018 static void
4019 collect_source_refs (tree namespc)
4021 /* Iterate over names in this name space. */
4022 for (tree t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4023 if (DECL_IS_BUILTIN (t))
4025 else if (TREE_CODE (t) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (t))
4026 collect_source_refs (t);
4027 else
4028 collect_source_ref (DECL_SOURCE_FILE (t));
4031 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4032 starting from NAMESPC. */
4034 static void
4035 collect_ada_namespace (tree namespc, const char *source_file)
4037 tree decl = NAMESPACE_LEVEL (namespc)->names;
4039 /* Collect decls from this namespace. This will skip
4040 NAMESPACE_DECLs (both aliases and regular, it cannot tell). */
4041 collect_ada_nodes (decl, source_file);
4043 /* Now scan for namespace children, and dump them. */
4044 for (; decl; decl = TREE_CHAIN (decl))
4045 if (TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl))
4046 collect_ada_namespace (decl, source_file);
4049 /* Returns true iff there is a definition available for variable or
4050 function DECL. */
4052 bool
4053 decl_defined_p (tree decl)
4055 if (TREE_CODE (decl) == FUNCTION_DECL)
4056 return (DECL_INITIAL (decl) != NULL_TREE
4057 /* A pending instantiation of a friend temploid is defined. */
4058 || (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
4059 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4060 (DECL_TI_TEMPLATE (decl)))));
4061 else
4063 gcc_assert (VAR_P (decl));
4064 return !DECL_EXTERNAL (decl);
4068 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4070 [expr.const]
4072 An integral constant-expression can only involve ... const
4073 variables of integral or enumeration types initialized with
4074 constant expressions ...
4076 C++0x also allows constexpr variables and temporaries initialized
4077 with constant expressions. We handle the former here, but the latter
4078 are just folded away in cxx_eval_constant_expression.
4080 The standard does not require that the expression be non-volatile.
4081 G++ implements the proposed correction in DR 457. */
4083 bool
4084 decl_constant_var_p (tree decl)
4086 if (!decl_maybe_constant_var_p (decl))
4087 return false;
4089 /* We don't know if a template static data member is initialized with
4090 a constant expression until we instantiate its initializer. Even
4091 in the case of a constexpr variable, we can't treat it as a
4092 constant until its initializer is complete in case it's used in
4093 its own initializer. */
4094 maybe_instantiate_decl (decl);
4095 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4098 /* Returns true if DECL could be a symbolic constant variable, depending on
4099 its initializer. */
4101 bool
4102 decl_maybe_constant_var_p (tree decl)
4104 tree type = TREE_TYPE (decl);
4105 if (!VAR_P (decl))
4106 return false;
4107 if (DECL_DECLARED_CONSTEXPR_P (decl))
4108 return true;
4109 if (DECL_HAS_VALUE_EXPR_P (decl))
4110 /* A proxy isn't constant. */
4111 return false;
4112 if (TREE_CODE (type) == REFERENCE_TYPE)
4113 /* References can be constant. */;
4114 else if (CP_TYPE_CONST_NON_VOLATILE_P (type)
4115 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4116 /* And const integers. */;
4117 else
4118 return false;
4120 if (DECL_INITIAL (decl)
4121 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
4122 /* We know the initializer, and it isn't constant. */
4123 return false;
4124 else
4125 return true;
4128 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4129 called from grokfndecl and grokvardecl; in all modes it is called from
4130 cp_write_global_declarations. */
4132 void
4133 no_linkage_error (tree decl)
4135 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4136 /* In C++11 it's ok if the decl is defined. */
4137 return;
4138 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4139 if (t == NULL_TREE)
4140 /* The type that got us on no_linkage_decls must have gotten a name for
4141 linkage purposes. */;
4142 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4143 /* The type might end up having a typedef name for linkage purposes. */
4144 vec_safe_push (no_linkage_decls, decl);
4145 else if (TYPE_UNNAMED_P (t))
4147 bool d = false;
4148 if (cxx_dialect >= cxx11)
4149 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4150 "unnamed type, is used but never defined", decl);
4151 else if (DECL_EXTERN_C_P (decl))
4152 /* Allow this; it's pretty common in C. */;
4153 else if (VAR_P (decl))
4154 /* DRs 132, 319 and 389 seem to indicate types with
4155 no linkage can only be used to declare extern "C"
4156 entities. Since it's not always an error in the
4157 ISO C++ 90 Standard, we only issue a warning. */
4158 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "unnamed type "
4159 "with no linkage used to declare variable %q#D with "
4160 "linkage", decl);
4161 else
4162 d = permerror (DECL_SOURCE_LOCATION (decl), "unnamed type with no "
4163 "linkage used to declare function %q#D with linkage",
4164 decl);
4165 if (d && is_typedef_decl (TYPE_NAME (t)))
4166 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4167 "to the unqualified type, so it is not used for linkage",
4168 TYPE_NAME (t));
4170 else if (cxx_dialect >= cxx11)
4172 if (VAR_P (decl) || !DECL_PURE_VIRTUAL_P (decl))
4173 permerror (DECL_SOURCE_LOCATION (decl),
4174 "%q#D, declared using local type "
4175 "%qT, is used but never defined", decl, t);
4177 else if (VAR_P (decl))
4178 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4179 "used to declare variable %q#D with linkage", t, decl);
4180 else
4181 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4182 "to declare function %q#D with linkage", t, decl);
4185 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4187 static void
4188 collect_all_refs (const char *source_file)
4190 collect_ada_namespace (global_namespace, source_file);
4193 /* Clear DECL_EXTERNAL for NODE. */
4195 static bool
4196 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4198 DECL_EXTERNAL (node->decl) = 0;
4199 return false;
4202 /* Build up the function to run dynamic initializers for thread_local
4203 variables in this translation unit and alias the init functions for the
4204 individual variables to it. */
4206 static void
4207 handle_tls_init (void)
4209 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4210 if (vars == NULL_TREE)
4211 return;
4213 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4215 write_out_vars (vars);
4217 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4218 boolean_type_node);
4219 TREE_PUBLIC (guard) = false;
4220 TREE_STATIC (guard) = true;
4221 DECL_ARTIFICIAL (guard) = true;
4222 DECL_IGNORED_P (guard) = true;
4223 TREE_USED (guard) = true;
4224 CP_DECL_THREAD_LOCAL_P (guard) = true;
4225 set_decl_tls_model (guard, decl_default_tls_model (guard));
4226 pushdecl_top_level_and_finish (guard, NULL_TREE);
4228 tree fn = get_local_tls_init_fn ();
4229 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4230 tree body = begin_function_body ();
4231 tree if_stmt = begin_if_stmt ();
4232 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4233 tf_warning_or_error);
4234 finish_if_stmt_cond (cond, if_stmt);
4235 finish_expr_stmt (cp_build_modify_expr (loc, guard, NOP_EXPR,
4236 boolean_true_node,
4237 tf_warning_or_error));
4238 for (; vars; vars = TREE_CHAIN (vars))
4240 tree var = TREE_VALUE (vars);
4241 tree init = TREE_PURPOSE (vars);
4242 one_static_initialization_or_destruction (var, init, true);
4244 #ifdef ASM_OUTPUT_DEF
4245 /* Output init aliases even with -fno-extern-tls-init. */
4246 if (TREE_PUBLIC (var))
4248 tree single_init_fn = get_tls_init_fn (var);
4249 if (single_init_fn == NULL_TREE)
4250 continue;
4251 cgraph_node *alias
4252 = cgraph_node::get_create (fn)->create_same_body_alias
4253 (single_init_fn, fn);
4254 gcc_assert (alias != NULL);
4256 #endif
4259 finish_then_clause (if_stmt);
4260 finish_if_stmt (if_stmt);
4261 finish_function_body (body);
4262 expand_or_defer_fn (finish_function (0));
4265 /* We're at the end of compilation, so generate any mangling aliases that
4266 we've been saving up, if DECL is going to be output and ID2 isn't
4267 already taken by another declaration. */
4269 static void
4270 generate_mangling_alias (tree decl, tree id2)
4272 /* If there's a declaration already using this mangled name,
4273 don't create a compatibility alias that conflicts. */
4274 if (IDENTIFIER_GLOBAL_VALUE (id2))
4275 return;
4277 struct cgraph_node *n = NULL;
4278 if (TREE_CODE (decl) == FUNCTION_DECL
4279 && !(n = cgraph_node::get (decl)))
4280 /* Don't create an alias to an unreferenced function. */
4281 return;
4283 tree alias = make_alias_for (decl, id2);
4284 SET_IDENTIFIER_GLOBAL_VALUE (id2, alias);
4285 DECL_IGNORED_P (alias) = 1;
4286 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
4287 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
4288 if (vague_linkage_p (decl))
4289 DECL_WEAK (alias) = 1;
4290 if (TREE_CODE (decl) == FUNCTION_DECL)
4291 n->create_same_body_alias (alias, decl);
4292 else
4293 varpool_node::create_extra_name_alias (alias, decl);
4296 /* Note that we might want to emit an alias with the symbol ID2 for DECL at
4297 the end of translation, for compatibility across bugs in the mangling
4298 implementation. */
4300 void
4301 note_mangling_alias (tree decl ATTRIBUTE_UNUSED, tree id2 ATTRIBUTE_UNUSED)
4303 #ifdef ASM_OUTPUT_DEF
4304 if (!defer_mangling_aliases)
4305 generate_mangling_alias (decl, id2);
4306 else
4308 vec_safe_push (mangling_aliases, decl);
4309 vec_safe_push (mangling_aliases, id2);
4311 #endif
4314 /* Emit all mangling aliases that were deferred up to this point. */
4316 void
4317 generate_mangling_aliases ()
4319 while (!vec_safe_is_empty (mangling_aliases))
4321 tree id2 = mangling_aliases->pop();
4322 tree decl = mangling_aliases->pop();
4323 generate_mangling_alias (decl, id2);
4325 defer_mangling_aliases = false;
4328 /* The entire file is now complete. If requested, dump everything
4329 to a file. */
4331 static void
4332 dump_tu (void)
4334 dump_flags_t flags;
4335 if (FILE *stream = dump_begin (raw_dump_id, &flags))
4337 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4338 dump_end (raw_dump_id, stream);
4342 static location_t locus_at_end_of_parsing;
4344 /* Check the deallocation functions for CODE to see if we want to warn that
4345 only one was defined. */
4347 static void
4348 maybe_warn_sized_delete (enum tree_code code)
4350 tree sized = NULL_TREE;
4351 tree unsized = NULL_TREE;
4353 for (ovl_iterator iter (IDENTIFIER_GLOBAL_VALUE (cp_operator_id (code)));
4354 iter; ++iter)
4356 tree fn = *iter;
4357 /* We're only interested in usual deallocation functions. */
4358 if (!usual_deallocation_fn_p (fn))
4359 continue;
4360 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
4361 unsized = fn;
4362 else
4363 sized = fn;
4365 if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
4366 warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
4367 "the program should also define %qD", sized);
4368 else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
4369 warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
4370 "the program should also define %qD", unsized);
4373 /* Check the global deallocation functions to see if we want to warn about
4374 defining unsized without sized (or vice versa). */
4376 static void
4377 maybe_warn_sized_delete ()
4379 if (!flag_sized_deallocation || !warn_sized_deallocation)
4380 return;
4381 maybe_warn_sized_delete (DELETE_EXPR);
4382 maybe_warn_sized_delete (VEC_DELETE_EXPR);
4385 /* Earlier we left PTRMEM_CST in variable initializers alone so that we could
4386 look them up when evaluating non-type template parameters. Now we need to
4387 lower them to something the back end can understand. */
4389 static void
4390 lower_var_init ()
4392 varpool_node *node;
4393 FOR_EACH_VARIABLE (node)
4395 tree d = node->decl;
4396 if (tree init = DECL_INITIAL (d))
4397 DECL_INITIAL (d) = cplus_expand_constant (init);
4401 /* This routine is called at the end of compilation.
4402 Its job is to create all the code needed to initialize and
4403 destroy the global aggregates. We do the destruction
4404 first, since that way we only need to reverse the decls once. */
4406 void
4407 c_parse_final_cleanups (void)
4409 tree vars;
4410 bool reconsider;
4411 size_t i;
4412 unsigned ssdf_count = 0;
4413 int retries = 0;
4414 tree decl;
4416 locus_at_end_of_parsing = input_location;
4417 at_eof = 1;
4419 /* Bad parse errors. Just forget about it. */
4420 if (! global_bindings_p () || current_class_type
4421 || !vec_safe_is_empty (decl_namespace_list))
4422 return;
4424 /* This is the point to write out a PCH if we're doing that.
4425 In that case we do not want to do anything else. */
4426 if (pch_file)
4428 /* Mangle all symbols at PCH creation time. */
4429 symtab_node *node;
4430 FOR_EACH_SYMBOL (node)
4431 if (! is_a <varpool_node *> (node)
4432 || ! DECL_HARD_REGISTER (node->decl))
4433 DECL_ASSEMBLER_NAME (node->decl);
4434 c_common_write_pch ();
4435 dump_tu ();
4436 /* Ensure even the callers don't try to finalize the CU. */
4437 flag_syntax_only = 1;
4438 return;
4441 timevar_stop (TV_PHASE_PARSING);
4442 timevar_start (TV_PHASE_DEFERRED);
4444 symtab->process_same_body_aliases ();
4446 /* Handle -fdump-ada-spec[-slim] */
4447 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4449 if (flag_dump_ada_spec_slim)
4450 collect_source_ref (main_input_filename);
4451 else
4452 collect_source_refs (global_namespace);
4454 dump_ada_specs (collect_all_refs, cpp_check);
4457 /* FIXME - huh? was input_line -= 1;*/
4459 /* We now have to write out all the stuff we put off writing out.
4460 These include:
4462 o Template specializations that we have not yet instantiated,
4463 but which are needed.
4464 o Initialization and destruction for non-local objects with
4465 static storage duration. (Local objects with static storage
4466 duration are initialized when their scope is first entered,
4467 and are cleaned up via atexit.)
4468 o Virtual function tables.
4470 All of these may cause others to be needed. For example,
4471 instantiating one function may cause another to be needed, and
4472 generating the initializer for an object may cause templates to be
4473 instantiated, etc., etc. */
4475 emit_support_tinfos ();
4479 tree t;
4480 tree decl;
4482 reconsider = false;
4484 /* If there are templates that we've put off instantiating, do
4485 them now. */
4486 instantiate_pending_templates (retries);
4487 ggc_collect ();
4489 /* Write out virtual tables as required. Writing out the
4490 virtual table for a template class may cause the
4491 instantiation of members of that class. If we write out
4492 vtables then we remove the class from our list so we don't
4493 have to look at it again. */
4494 for (i = keyed_classes->length ();
4495 keyed_classes->iterate (--i, &t);)
4496 if (maybe_emit_vtables (t))
4498 reconsider = true;
4499 keyed_classes->unordered_remove (i);
4502 /* Write out needed type info variables. We have to be careful
4503 looping through unemitted decls, because emit_tinfo_decl may
4504 cause other variables to be needed. New elements will be
4505 appended, and we remove from the vector those that actually
4506 get emitted. */
4507 for (i = unemitted_tinfo_decls->length ();
4508 unemitted_tinfo_decls->iterate (--i, &t);)
4509 if (emit_tinfo_decl (t))
4511 reconsider = true;
4512 unemitted_tinfo_decls->unordered_remove (i);
4515 /* The list of objects with static storage duration is built up
4516 in reverse order. We clear STATIC_AGGREGATES so that any new
4517 aggregates added during the initialization of these will be
4518 initialized in the correct order when we next come around the
4519 loop. */
4520 vars = prune_vars_needing_no_initialization (&static_aggregates);
4522 if (vars)
4524 /* We need to start a new initialization function each time
4525 through the loop. That's because we need to know which
4526 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4527 isn't computed until a function is finished, and written
4528 out. That's a deficiency in the back end. When this is
4529 fixed, these initialization functions could all become
4530 inline, with resulting performance improvements. */
4531 tree ssdf_body;
4533 /* Set the line and file, so that it is obviously not from
4534 the source file. */
4535 input_location = locus_at_end_of_parsing;
4536 ssdf_body = start_static_storage_duration_function (ssdf_count);
4538 /* Make sure the back end knows about all the variables. */
4539 write_out_vars (vars);
4541 /* First generate code to do all the initializations. */
4542 if (vars)
4543 do_static_initialization_or_destruction (vars, /*initp=*/true);
4545 /* Then, generate code to do all the destructions. Do these
4546 in reverse order so that the most recently constructed
4547 variable is the first destroyed. If we're using
4548 __cxa_atexit, then we don't need to do this; functions
4549 were registered at initialization time to destroy the
4550 local statics. */
4551 if (!flag_use_cxa_atexit && vars)
4553 vars = nreverse (vars);
4554 do_static_initialization_or_destruction (vars, /*initp=*/false);
4556 else
4557 vars = NULL_TREE;
4559 /* Finish up the static storage duration function for this
4560 round. */
4561 input_location = locus_at_end_of_parsing;
4562 finish_static_storage_duration_function (ssdf_body);
4564 /* All those initializations and finalizations might cause
4565 us to need more inline functions, more template
4566 instantiations, etc. */
4567 reconsider = true;
4568 ssdf_count++;
4569 /* ??? was: locus_at_end_of_parsing.line++; */
4572 /* Now do the same for thread_local variables. */
4573 handle_tls_init ();
4575 /* Go through the set of inline functions whose bodies have not
4576 been emitted yet. If out-of-line copies of these functions
4577 are required, emit them. */
4578 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4580 /* Does it need synthesizing? */
4581 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4582 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4584 /* Even though we're already at the top-level, we push
4585 there again. That way, when we pop back a few lines
4586 hence, all of our state is restored. Otherwise,
4587 finish_function doesn't clean things up, and we end
4588 up with CURRENT_FUNCTION_DECL set. */
4589 push_to_top_level ();
4590 /* The decl's location will mark where it was first
4591 needed. Save that so synthesize method can indicate
4592 where it was needed from, in case of error */
4593 input_location = DECL_SOURCE_LOCATION (decl);
4594 synthesize_method (decl);
4595 pop_from_top_level ();
4596 reconsider = true;
4599 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4600 generate_tls_wrapper (decl);
4602 if (!DECL_SAVED_TREE (decl))
4603 continue;
4605 cgraph_node *node = cgraph_node::get_create (decl);
4607 /* We lie to the back end, pretending that some functions
4608 are not defined when they really are. This keeps these
4609 functions from being put out unnecessarily. But, we must
4610 stop lying when the functions are referenced, or if they
4611 are not comdat since they need to be put out now. If
4612 DECL_INTERFACE_KNOWN, then we have already set
4613 DECL_EXTERNAL appropriately, so there's no need to check
4614 again, and we do not want to clear DECL_EXTERNAL if a
4615 previous call to import_export_decl set it.
4617 This is done in a separate for cycle, because if some
4618 deferred function is contained in another deferred
4619 function later in deferred_fns varray,
4620 rest_of_compilation would skip this function and we
4621 really cannot expand the same function twice. */
4622 import_export_decl (decl);
4623 if (DECL_NOT_REALLY_EXTERN (decl)
4624 && DECL_INITIAL (decl)
4625 && decl_needed_p (decl))
4627 if (node->cpp_implicit_alias)
4628 node = node->get_alias_target ();
4630 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
4631 NULL, true);
4632 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4633 group, we need to mark all symbols in the same comdat group
4634 that way. */
4635 if (node->same_comdat_group)
4636 for (cgraph_node *next
4637 = dyn_cast<cgraph_node *> (node->same_comdat_group);
4638 next != node;
4639 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
4640 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
4641 NULL, true);
4644 /* If we're going to need to write this function out, and
4645 there's already a body for it, create RTL for it now.
4646 (There might be no body if this is a method we haven't
4647 gotten around to synthesizing yet.) */
4648 if (!DECL_EXTERNAL (decl)
4649 && decl_needed_p (decl)
4650 && !TREE_ASM_WRITTEN (decl)
4651 && !node->definition)
4653 /* We will output the function; no longer consider it in this
4654 loop. */
4655 DECL_DEFER_OUTPUT (decl) = 0;
4656 /* Generate RTL for this function now that we know we
4657 need it. */
4658 expand_or_defer_fn (decl);
4659 /* If we're compiling -fsyntax-only pretend that this
4660 function has been written out so that we don't try to
4661 expand it again. */
4662 if (flag_syntax_only)
4663 TREE_ASM_WRITTEN (decl) = 1;
4664 reconsider = true;
4668 if (wrapup_namespace_globals ())
4669 reconsider = true;
4671 /* Static data members are just like namespace-scope globals. */
4672 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4674 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4675 /* Don't write it out if we haven't seen a definition. */
4676 || (DECL_IN_AGGR_P (decl) && !DECL_INLINE_VAR_P (decl)))
4677 continue;
4678 import_export_decl (decl);
4679 /* If this static data member is needed, provide it to the
4680 back end. */
4681 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4682 DECL_EXTERNAL (decl) = 0;
4684 if (vec_safe_length (pending_statics) != 0
4685 && wrapup_global_declarations (pending_statics->address (),
4686 pending_statics->length ()))
4687 reconsider = true;
4689 retries++;
4691 while (reconsider);
4693 lower_var_init ();
4695 generate_mangling_aliases ();
4697 /* All used inline functions must have a definition at this point. */
4698 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4700 if (/* Check online inline functions that were actually used. */
4701 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4702 /* If the definition actually was available here, then the
4703 fact that the function was not defined merely represents
4704 that for some reason (use of a template repository,
4705 #pragma interface, etc.) we decided not to emit the
4706 definition here. */
4707 && !DECL_INITIAL (decl)
4708 /* Don't complain if the template was defined. */
4709 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4710 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4711 (template_for_substitution (decl)))))
4713 warning_at (DECL_SOURCE_LOCATION (decl), 0,
4714 "inline function %qD used but never defined", decl);
4715 /* Avoid a duplicate warning from check_global_declaration. */
4716 TREE_NO_WARNING (decl) = 1;
4720 /* So must decls that use a type with no linkage. */
4721 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4722 no_linkage_error (decl);
4724 maybe_warn_sized_delete ();
4726 /* Then, do the Objective-C stuff. This is where all the
4727 Objective-C module stuff gets generated (symtab,
4728 class/protocol/selector lists etc). This must be done after C++
4729 templates, destructors etc. so that selectors used in C++
4730 templates are properly allocated. */
4731 if (c_dialect_objc ())
4732 objc_write_global_declarations ();
4734 /* We give C linkage to static constructors and destructors. */
4735 push_lang_context (lang_name_c);
4737 /* Generate initialization and destruction functions for all
4738 priorities for which they are required. */
4739 if (priority_info_map)
4740 splay_tree_foreach (priority_info_map,
4741 generate_ctor_and_dtor_functions_for_priority,
4742 /*data=*/&locus_at_end_of_parsing);
4743 else if (c_dialect_objc () && objc_static_init_needed_p ())
4744 /* If this is obj-c++ and we need a static init, call
4745 generate_ctor_or_dtor_function. */
4746 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4747 DEFAULT_INIT_PRIORITY,
4748 &locus_at_end_of_parsing);
4750 /* We're done with the splay-tree now. */
4751 if (priority_info_map)
4752 splay_tree_delete (priority_info_map);
4754 /* Generate any missing aliases. */
4755 maybe_apply_pending_pragma_weaks ();
4757 /* We're done with static constructors, so we can go back to "C++"
4758 linkage now. */
4759 pop_lang_context ();
4761 if (flag_vtable_verify)
4763 vtv_recover_class_info ();
4764 vtv_compute_class_hierarchy_transitive_closure ();
4765 vtv_build_vtable_verify_fndecl ();
4768 perform_deferred_noexcept_checks ();
4770 finish_repo ();
4771 fini_constexpr ();
4773 /* The entire file is now complete. If requested, dump everything
4774 to a file. */
4775 dump_tu ();
4777 if (flag_detailed_statistics)
4779 dump_tree_statistics ();
4780 dump_time_statistics ();
4783 timevar_stop (TV_PHASE_DEFERRED);
4784 timevar_start (TV_PHASE_PARSING);
4786 /* Indicate that we're done with front end processing. */
4787 at_eof = 2;
4790 /* Perform any post compilation-proper cleanups for the C++ front-end.
4791 This should really go away. No front-end should need to do
4792 anything past the compilation process. */
4794 void
4795 cxx_post_compilation_parsing_cleanups (void)
4797 timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS);
4799 if (flag_vtable_verify)
4801 /* Generate the special constructor initialization function that
4802 calls __VLTRegisterPairs, and give it a very high
4803 initialization priority. This must be done after
4804 finalize_compilation_unit so that we have accurate
4805 information about which vtable will actually be emitted. */
4806 vtv_generate_init_routine ();
4809 input_location = locus_at_end_of_parsing;
4811 if (flag_checking)
4812 validate_conversion_obstack ();
4814 timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS);
4817 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4818 function to call in parse-tree form; it has not yet been
4819 semantically analyzed. ARGS are the arguments to the function.
4820 They have already been semantically analyzed. This may change
4821 ARGS. */
4823 tree
4824 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4825 tsubst_flags_t complain)
4827 tree orig_fn;
4828 vec<tree, va_gc> *orig_args = NULL;
4829 tree expr;
4830 tree object;
4832 orig_fn = fn;
4833 object = TREE_OPERAND (fn, 0);
4835 if (processing_template_decl)
4837 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4838 || TREE_CODE (fn) == MEMBER_REF);
4839 if (type_dependent_expression_p (fn)
4840 || any_type_dependent_arguments_p (*args))
4841 return build_min_nt_call_vec (fn, *args);
4843 orig_args = make_tree_vector_copy (*args);
4845 /* Transform the arguments and add the implicit "this"
4846 parameter. That must be done before the FN is transformed
4847 because we depend on the form of FN. */
4848 make_args_non_dependent (*args);
4849 object = build_non_dependent_expr (object);
4850 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4852 if (TREE_CODE (fn) == DOTSTAR_EXPR)
4853 object = cp_build_addr_expr (object, complain);
4854 vec_safe_insert (*args, 0, object);
4856 /* Now that the arguments are done, transform FN. */
4857 fn = build_non_dependent_expr (fn);
4860 /* A qualified name corresponding to a bound pointer-to-member is
4861 represented as an OFFSET_REF:
4863 struct B { void g(); };
4864 void (B::*p)();
4865 void B::g() { (this->*p)(); } */
4866 if (TREE_CODE (fn) == OFFSET_REF)
4868 tree object_addr = cp_build_addr_expr (object, complain);
4869 fn = TREE_OPERAND (fn, 1);
4870 fn = get_member_function_from_ptrfunc (&object_addr, fn,
4871 complain);
4872 vec_safe_insert (*args, 0, object_addr);
4875 if (CLASS_TYPE_P (TREE_TYPE (fn)))
4876 expr = build_op_call (fn, args, complain);
4877 else
4878 expr = cp_build_function_call_vec (fn, args, complain);
4879 if (processing_template_decl && expr != error_mark_node)
4880 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
4882 if (orig_args != NULL)
4883 release_tree_vector (orig_args);
4885 return expr;
4889 void
4890 check_default_args (tree x)
4892 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4893 bool saw_def = false;
4894 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4895 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4897 if (TREE_PURPOSE (arg))
4898 saw_def = true;
4899 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
4901 error ("default argument missing for parameter %P of %q+#D", i, x);
4902 TREE_PURPOSE (arg) = error_mark_node;
4907 /* Return true if function DECL can be inlined. This is used to force
4908 instantiation of methods that might be interesting for inlining. */
4909 bool
4910 possibly_inlined_p (tree decl)
4912 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4913 if (DECL_UNINLINABLE (decl))
4914 return false;
4915 if (!optimize)
4916 return DECL_DECLARED_INLINE_P (decl);
4917 /* When optimizing, we might inline everything when flatten
4918 attribute or heuristics inlining for size or autoinlining
4919 is used. */
4920 return true;
4923 /* Normally, we can wait until instantiation-time to synthesize DECL.
4924 However, if DECL is a static data member initialized with a constant
4925 or a constexpr function, we need it right now because a reference to
4926 such a data member or a call to such function is not value-dependent.
4927 For a function that uses auto in the return type, we need to instantiate
4928 it to find out its type. For OpenMP user defined reductions, we need
4929 them instantiated for reduction clauses which inline them by hand
4930 directly. */
4932 static void
4933 maybe_instantiate_decl (tree decl)
4935 if (DECL_LANG_SPECIFIC (decl)
4936 && DECL_TEMPLATE_INFO (decl)
4937 && (decl_maybe_constant_var_p (decl)
4938 || (TREE_CODE (decl) == FUNCTION_DECL
4939 && DECL_OMP_DECLARE_REDUCTION_P (decl))
4940 || undeduced_auto_decl (decl))
4941 && !DECL_DECLARED_CONCEPT_P (decl)
4942 && !uses_template_parms (DECL_TI_ARGS (decl)))
4944 /* Instantiating a function will result in garbage collection. We
4945 must treat this situation as if we were within the body of a
4946 function so as to avoid collecting live data only referenced from
4947 the stack (such as overload resolution candidates). */
4948 ++function_depth;
4949 instantiate_decl (decl, /*defer_ok=*/false,
4950 /*expl_inst_class_mem_p=*/false);
4951 --function_depth;
4955 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
4956 If DECL is a specialization or implicitly declared class member,
4957 generate the actual definition. Return false if something goes
4958 wrong, true otherwise. */
4960 bool
4961 mark_used (tree decl, tsubst_flags_t complain)
4963 /* If DECL is a BASELINK for a single function, then treat it just
4964 like the DECL for the function. Otherwise, if the BASELINK is
4965 for an overloaded function, we don't know which function was
4966 actually used until after overload resolution. */
4967 if (BASELINK_P (decl))
4969 decl = BASELINK_FUNCTIONS (decl);
4970 if (really_overloaded_fn (decl))
4971 return true;
4972 decl = OVL_FIRST (decl);
4975 /* Set TREE_USED for the benefit of -Wunused. */
4976 TREE_USED (decl) = 1;
4977 /* And for structured bindings also the underlying decl. */
4978 if (DECL_DECOMPOSITION_P (decl) && DECL_DECOMP_BASE (decl))
4979 TREE_USED (DECL_DECOMP_BASE (decl)) = 1;
4981 if (TREE_CODE (decl) == TEMPLATE_DECL)
4982 return true;
4984 if (DECL_CLONED_FUNCTION_P (decl))
4985 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4987 /* Mark enumeration types as used. */
4988 if (TREE_CODE (decl) == CONST_DECL)
4989 used_types_insert (DECL_CONTEXT (decl));
4991 if (TREE_CODE (decl) == FUNCTION_DECL)
4992 maybe_instantiate_noexcept (decl);
4994 if (TREE_CODE (decl) == FUNCTION_DECL
4995 && DECL_DELETED_FN (decl))
4997 if (DECL_ARTIFICIAL (decl)
4998 && DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
4999 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
5000 /* We mark a lambda conversion op as deleted if we can't
5001 generate it properly; see maybe_add_lambda_conv_op. */
5002 sorry ("converting lambda which uses %<...%> to function pointer");
5003 else if (complain & tf_error)
5005 error ("use of deleted function %qD", decl);
5006 if (!maybe_explain_implicit_delete (decl))
5007 inform (DECL_SOURCE_LOCATION (decl), "declared here");
5009 return false;
5012 if (TREE_DEPRECATED (decl) && (complain & tf_warning)
5013 && deprecated_state != DEPRECATED_SUPPRESS)
5014 warn_deprecated_use (decl, NULL_TREE);
5016 /* We can only check DECL_ODR_USED on variables or functions with
5017 DECL_LANG_SPECIFIC set, and these are also the only decls that we
5018 might need special handling for. */
5019 if (!VAR_OR_FUNCTION_DECL_P (decl)
5020 || DECL_LANG_SPECIFIC (decl) == NULL
5021 || DECL_THUNK_P (decl))
5023 if (!processing_template_decl
5024 && !require_deduced_type (decl, complain))
5025 return false;
5026 return true;
5029 /* We only want to do this processing once. We don't need to keep trying
5030 to instantiate inline templates, because unit-at-a-time will make sure
5031 we get them compiled before functions that want to inline them. */
5032 if (DECL_ODR_USED (decl))
5033 return true;
5035 /* Normally, we can wait until instantiation-time to synthesize DECL.
5036 However, if DECL is a static data member initialized with a constant
5037 or a constexpr function, we need it right now because a reference to
5038 such a data member or a call to such function is not value-dependent.
5039 For a function that uses auto in the return type, we need to instantiate
5040 it to find out its type. For OpenMP user defined reductions, we need
5041 them instantiated for reduction clauses which inline them by hand
5042 directly. */
5043 maybe_instantiate_decl (decl);
5045 if (processing_template_decl || in_template_function ())
5046 return true;
5048 /* Check this too in case we're within instantiate_non_dependent_expr. */
5049 if (DECL_TEMPLATE_INFO (decl)
5050 && uses_template_parms (DECL_TI_ARGS (decl)))
5051 return true;
5053 if (!require_deduced_type (decl, complain))
5054 return false;
5056 if (builtin_pack_fn_p (decl))
5058 error ("use of built-in parameter pack %qD outside of a template",
5059 DECL_NAME (decl));
5060 return false;
5063 /* If we don't need a value, then we don't need to synthesize DECL. */
5064 if (cp_unevaluated_operand || in_discarded_stmt)
5065 return true;
5067 DECL_ODR_USED (decl) = 1;
5068 if (DECL_CLONED_FUNCTION_P (decl))
5069 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5071 /* DR 757: A type without linkage shall not be used as the type of a
5072 variable or function with linkage, unless
5073 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5074 o the variable or function is not used (3.2 [basic.def.odr]) or is
5075 defined in the same translation unit. */
5076 if (cxx_dialect > cxx98
5077 && decl_linkage (decl) != lk_none
5078 && !DECL_EXTERN_C_P (decl)
5079 && !DECL_ARTIFICIAL (decl)
5080 && !decl_defined_p (decl)
5081 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
5083 if (is_local_extern (decl))
5084 /* There's no way to define a local extern, and adding it to
5085 the vector interferes with GC, so give an error now. */
5086 no_linkage_error (decl);
5087 else
5088 vec_safe_push (no_linkage_decls, decl);
5091 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
5092 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
5093 /* Remember it, so we can check it was defined. */
5094 note_vague_linkage_fn (decl);
5096 /* Is it a synthesized method that needs to be synthesized? */
5097 if (TREE_CODE (decl) == FUNCTION_DECL
5098 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5099 && DECL_DEFAULTED_FN (decl)
5100 /* A function defaulted outside the class is synthesized either by
5101 cp_finish_decl or instantiate_decl. */
5102 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
5103 && ! DECL_INITIAL (decl))
5105 /* Defer virtual destructors so that thunks get the right
5106 linkage. */
5107 if (DECL_VIRTUAL_P (decl) && !at_eof)
5109 note_vague_linkage_fn (decl);
5110 return true;
5113 /* Remember the current location for a function we will end up
5114 synthesizing. Then we can inform the user where it was
5115 required in the case of error. */
5116 DECL_SOURCE_LOCATION (decl) = input_location;
5118 /* Synthesizing an implicitly defined member function will result in
5119 garbage collection. We must treat this situation as if we were
5120 within the body of a function so as to avoid collecting live data
5121 on the stack (such as overload resolution candidates).
5123 We could just let cp_write_global_declarations handle synthesizing
5124 this function by adding it to deferred_fns, but doing
5125 it at the use site produces better error messages. */
5126 ++function_depth;
5127 synthesize_method (decl);
5128 --function_depth;
5129 /* If this is a synthesized method we don't need to
5130 do the instantiation test below. */
5132 else if (VAR_OR_FUNCTION_DECL_P (decl)
5133 && DECL_TEMPLATE_INFO (decl)
5134 && !DECL_DECLARED_CONCEPT_P (decl)
5135 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5136 || always_instantiate_p (decl)))
5137 /* If this is a function or variable that is an instance of some
5138 template, we now know that we will need to actually do the
5139 instantiation. We check that DECL is not an explicit
5140 instantiation because that is not checked in instantiate_decl.
5142 We put off instantiating functions in order to improve compile
5143 times. Maintaining a stack of active functions is expensive,
5144 and the inliner knows to instantiate any functions it might
5145 need. Therefore, we always try to defer instantiation. */
5147 ++function_depth;
5148 instantiate_decl (decl, /*defer_ok=*/true,
5149 /*expl_inst_class_mem_p=*/false);
5150 --function_depth;
5153 return true;
5156 bool
5157 mark_used (tree decl)
5159 return mark_used (decl, tf_warning_or_error);
5162 tree
5163 vtv_start_verification_constructor_init_function (void)
5165 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5168 tree
5169 vtv_finish_verification_constructor_init_function (tree function_body)
5171 tree fn;
5173 finish_compound_stmt (function_body);
5174 fn = finish_function (0);
5175 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5176 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5178 return fn;
5181 #include "gt-cp-decl2.h"