[PR c++/85008] ICE looking for clone
[official-gcc.git] / gcc / cp / decl2.c
blobe522b9ebe55a708ec97adc163f050fb63294191d
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2018 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* Process declarations and symbol lookup for C++ front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "memmodel.h"
33 #include "target.h"
34 #include "cp-tree.h"
35 #include "c-family/c-common.h"
36 #include "timevar.h"
37 #include "stringpool.h"
38 #include "cgraph.h"
39 #include "varasm.h"
40 #include "attribs.h"
41 #include "stor-layout.h"
42 #include "calls.h"
43 #include "decl.h"
44 #include "toplev.h"
45 #include "c-family/c-objc.h"
46 #include "c-family/c-pragma.h"
47 #include "dumpfile.h"
48 #include "intl.h"
49 #include "c-family/c-ada-spec.h"
50 #include "asan.h"
52 /* Id for dumping the raw trees. */
53 int raw_dump_id;
55 extern cpp_reader *parse_in;
57 /* This structure contains information about the initializations
58 and/or destructions required for a particular priority level. */
59 typedef struct priority_info_s {
60 /* Nonzero if there have been any initializations at this priority
61 throughout the translation unit. */
62 int initializations_p;
63 /* Nonzero if there have been any destructions at this priority
64 throughout the translation unit. */
65 int destructions_p;
66 } *priority_info;
68 static void mark_vtable_entries (tree);
69 static bool maybe_emit_vtables (tree);
70 static tree start_objects (int, int);
71 static void finish_objects (int, int, tree);
72 static tree start_static_storage_duration_function (unsigned);
73 static void finish_static_storage_duration_function (tree);
74 static priority_info get_priority_info (int);
75 static void do_static_initialization_or_destruction (tree, bool);
76 static void one_static_initialization_or_destruction (tree, tree, bool);
77 static void generate_ctor_or_dtor_function (bool, int, location_t *);
78 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
79 void *);
80 static tree prune_vars_needing_no_initialization (tree *);
81 static void write_out_vars (tree);
82 static void import_export_class (tree);
83 static tree get_guard_bits (tree);
84 static void determine_visibility_from_class (tree, tree);
85 static bool determine_hidden_inline (tree);
86 static void maybe_instantiate_decl (tree);
88 /* A list of static class variables. This is needed, because a
89 static class variable can be declared inside the class without
90 an initializer, and then initialized, statically, outside the class. */
91 static GTY(()) vec<tree, va_gc> *pending_statics;
93 /* A list of functions which were declared inline, but which we
94 may need to emit outline anyway. */
95 static GTY(()) vec<tree, va_gc> *deferred_fns;
97 /* A list of decls that use types with no linkage, which we need to make
98 sure are defined. */
99 static GTY(()) vec<tree, va_gc> *no_linkage_decls;
101 /* A vector of alternating decls and identifiers, where the latter
102 is to be an alias for the former if the former is defined. */
103 static GTY(()) vec<tree, va_gc> *mangling_aliases;
105 /* hash traits for declarations. Hashes single decls via
106 DECL_ASSEMBLER_NAME_RAW. */
108 struct mangled_decl_hash : ggc_remove <tree>
110 typedef tree value_type; /* A DECL. */
111 typedef tree compare_type; /* An identifier. */
113 static hashval_t hash (const value_type decl)
115 return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME_RAW (decl));
117 static bool equal (const value_type existing, compare_type candidate)
119 tree name = DECL_ASSEMBLER_NAME_RAW (existing);
120 return candidate == name;
123 static inline void mark_empty (value_type &p) {p = NULL_TREE;}
124 static inline bool is_empty (value_type p) {return !p;}
126 static bool is_deleted (value_type e)
128 return e == reinterpret_cast <value_type> (1);
130 static void mark_deleted (value_type &e)
132 e = reinterpret_cast <value_type> (1);
136 /* A hash table of decls keyed by mangled name. Used to figure out if
137 we need compatibility aliases. */
138 static GTY(()) hash_table<mangled_decl_hash> *mangled_decls;
140 /* Nonzero if we're done parsing and into end-of-file activities. */
142 int at_eof;
144 /* True if note_mangling_alias should enqueue mangling aliases for
145 later generation, rather than emitting them right away. */
147 bool defer_mangling_aliases = true;
150 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
151 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
152 that apply to the function). */
154 tree
155 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
156 cp_ref_qualifier rqual)
158 tree raises;
159 tree attrs;
160 int type_quals;
161 bool late_return_type_p;
163 if (fntype == error_mark_node || ctype == error_mark_node)
164 return error_mark_node;
166 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
167 || TREE_CODE (fntype) == METHOD_TYPE);
169 type_quals = quals & ~TYPE_QUAL_RESTRICT;
170 ctype = cp_build_qualified_type (ctype, type_quals);
171 raises = TYPE_RAISES_EXCEPTIONS (fntype);
172 attrs = TYPE_ATTRIBUTES (fntype);
173 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
174 fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
175 (TREE_CODE (fntype) == METHOD_TYPE
176 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
177 : TYPE_ARG_TYPES (fntype)));
178 if (attrs)
179 fntype = cp_build_type_attribute_variant (fntype, attrs);
180 if (rqual)
181 fntype = build_ref_qualified_type (fntype, rqual);
182 if (raises)
183 fntype = build_exception_variant (fntype, raises);
184 if (late_return_type_p)
185 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
187 return fntype;
190 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
191 return type changed to NEW_RET. */
193 tree
194 change_return_type (tree new_ret, tree fntype)
196 tree newtype;
197 tree args = TYPE_ARG_TYPES (fntype);
198 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
199 tree attrs = TYPE_ATTRIBUTES (fntype);
200 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
202 if (new_ret == error_mark_node)
203 return fntype;
205 if (same_type_p (new_ret, TREE_TYPE (fntype)))
206 return fntype;
208 if (TREE_CODE (fntype) == FUNCTION_TYPE)
210 newtype = build_function_type (new_ret, args);
211 newtype = apply_memfn_quals (newtype,
212 type_memfn_quals (fntype),
213 type_memfn_rqual (fntype));
215 else
216 newtype = build_method_type_directly
217 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
218 if (FUNCTION_REF_QUALIFIED (fntype))
219 newtype = build_ref_qualified_type (newtype, type_memfn_rqual (fntype));
220 if (raises)
221 newtype = build_exception_variant (newtype, raises);
222 if (attrs)
223 newtype = cp_build_type_attribute_variant (newtype, attrs);
224 if (late_return_type_p)
225 TYPE_HAS_LATE_RETURN_TYPE (newtype) = 1;
227 return newtype;
230 /* Build a PARM_DECL of FN with NAME and TYPE, and set DECL_ARG_TYPE
231 appropriately. */
233 tree
234 cp_build_parm_decl (tree fn, tree name, tree type)
236 tree parm = build_decl (input_location,
237 PARM_DECL, name, type);
238 DECL_CONTEXT (parm) = fn;
240 /* DECL_ARG_TYPE is only used by the back end and the back end never
241 sees templates. */
242 if (!processing_template_decl)
243 DECL_ARG_TYPE (parm) = type_passed_as (type);
245 return parm;
248 /* Returns a PARM_DECL of FN for a parameter of the indicated TYPE, with the
249 indicated NAME. */
251 tree
252 build_artificial_parm (tree fn, tree name, tree type)
254 tree parm = cp_build_parm_decl (fn, name, type);
255 DECL_ARTIFICIAL (parm) = 1;
256 /* All our artificial parms are implicitly `const'; they cannot be
257 assigned to. */
258 TREE_READONLY (parm) = 1;
259 return parm;
262 /* Constructors for types with virtual baseclasses need an "in-charge" flag
263 saying whether this constructor is responsible for initialization of
264 virtual baseclasses or not. All destructors also need this "in-charge"
265 flag, which additionally determines whether or not the destructor should
266 free the memory for the object.
268 This function adds the "in-charge" flag to member function FN if
269 appropriate. It is called from grokclassfn and tsubst.
270 FN must be either a constructor or destructor.
272 The in-charge flag follows the 'this' parameter, and is followed by the
273 VTT parm (if any), then the user-written parms. */
275 void
276 maybe_retrofit_in_chrg (tree fn)
278 tree basetype, arg_types, parms, parm, fntype;
280 /* If we've already add the in-charge parameter don't do it again. */
281 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
282 return;
284 /* When processing templates we can't know, in general, whether or
285 not we're going to have virtual baseclasses. */
286 if (processing_template_decl)
287 return;
289 /* We don't need an in-charge parameter for constructors that don't
290 have virtual bases. */
291 if (DECL_CONSTRUCTOR_P (fn)
292 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
293 return;
295 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
296 basetype = TREE_TYPE (TREE_VALUE (arg_types));
297 arg_types = TREE_CHAIN (arg_types);
299 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
301 /* If this is a subobject constructor or destructor, our caller will
302 pass us a pointer to our VTT. */
303 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
305 parm = build_artificial_parm (fn, vtt_parm_identifier, vtt_parm_type);
307 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
308 DECL_CHAIN (parm) = parms;
309 parms = parm;
311 /* ...and then to TYPE_ARG_TYPES. */
312 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
314 DECL_HAS_VTT_PARM_P (fn) = 1;
317 /* Then add the in-charge parm (before the VTT parm). */
318 parm = build_artificial_parm (fn, in_charge_identifier, integer_type_node);
319 DECL_CHAIN (parm) = parms;
320 parms = parm;
321 arg_types = hash_tree_chain (integer_type_node, arg_types);
323 /* Insert our new parameter(s) into the list. */
324 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
326 /* And rebuild the function type. */
327 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
328 arg_types);
329 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
330 fntype = build_exception_variant (fntype,
331 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
332 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
333 fntype = (cp_build_type_attribute_variant
334 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
335 TREE_TYPE (fn) = fntype;
337 /* Now we've got the in-charge parameter. */
338 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
341 /* Classes overload their constituent function names automatically.
342 When a function name is declared in a record structure,
343 its name is changed to it overloaded name. Since names for
344 constructors and destructors can conflict, we place a leading
345 '$' for destructors.
347 CNAME is the name of the class we are grokking for.
349 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
351 FLAGS contains bits saying what's special about today's
352 arguments. DTOR_FLAG == DESTRUCTOR.
354 If FUNCTION is a destructor, then we must add the `auto-delete' field
355 as a second parameter. There is some hair associated with the fact
356 that we must "declare" this variable in the manner consistent with the
357 way the rest of the arguments were declared.
359 QUALS are the qualifiers for the this pointer. */
361 void
362 grokclassfn (tree ctype, tree function, enum overload_flags flags)
364 tree fn_name = DECL_NAME (function);
366 /* Even within an `extern "C"' block, members get C++ linkage. See
367 [dcl.link] for details. */
368 SET_DECL_LANGUAGE (function, lang_cplusplus);
370 if (fn_name == NULL_TREE)
372 error ("name missing for member function");
373 fn_name = get_identifier ("<anonymous>");
374 DECL_NAME (function) = fn_name;
377 DECL_CONTEXT (function) = ctype;
379 if (flags == DTOR_FLAG)
380 DECL_CXX_DESTRUCTOR_P (function) = 1;
382 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
383 maybe_retrofit_in_chrg (function);
386 /* Create an ARRAY_REF, checking for the user doing things backwards
387 along the way. DECLTYPE_P is for N3276, as in the parser. */
389 tree
390 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
391 bool decltype_p)
393 tree type;
394 tree expr;
395 tree orig_array_expr = array_expr;
396 tree orig_index_exp = index_exp;
397 tree overload = NULL_TREE;
399 if (error_operand_p (array_expr) || error_operand_p (index_exp))
400 return error_mark_node;
402 if (processing_template_decl)
404 if (type_dependent_expression_p (array_expr)
405 || type_dependent_expression_p (index_exp))
406 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
407 NULL_TREE, NULL_TREE);
408 array_expr = build_non_dependent_expr (array_expr);
409 index_exp = build_non_dependent_expr (index_exp);
412 type = TREE_TYPE (array_expr);
413 gcc_assert (type);
414 type = non_reference (type);
416 /* If they have an `operator[]', use that. */
417 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
419 tsubst_flags_t complain = tf_warning_or_error;
420 if (decltype_p)
421 complain |= tf_decltype;
422 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
423 index_exp, NULL_TREE, &overload, complain);
425 else
427 tree p1, p2, i1, i2;
429 /* Otherwise, create an ARRAY_REF for a pointer or array type.
430 It is a little-known fact that, if `a' is an array and `i' is
431 an int, you can write `i[a]', which means the same thing as
432 `a[i]'. */
433 if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
434 p1 = array_expr;
435 else
436 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
438 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
439 p2 = index_exp;
440 else
441 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
443 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
444 false);
445 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
446 false);
448 if ((p1 && i2) && (i1 && p2))
449 error ("ambiguous conversion for array subscript");
451 if (p1 && i2)
452 array_expr = p1, index_exp = i2;
453 else if (i1 && p2)
454 array_expr = p2, index_exp = i1;
455 else
457 error ("invalid types %<%T[%T]%> for array subscript",
458 type, TREE_TYPE (index_exp));
459 return error_mark_node;
462 if (array_expr == error_mark_node || index_exp == error_mark_node)
463 error ("ambiguous conversion for array subscript");
465 if (TREE_CODE (TREE_TYPE (array_expr)) == POINTER_TYPE)
466 array_expr = mark_rvalue_use (array_expr);
467 else
468 array_expr = mark_lvalue_use_nonread (array_expr);
469 index_exp = mark_rvalue_use (index_exp);
470 expr = build_array_ref (input_location, array_expr, index_exp);
472 if (processing_template_decl && expr != error_mark_node)
474 if (overload != NULL_TREE)
475 return (build_min_non_dep_op_overload
476 (ARRAY_REF, expr, overload, orig_array_expr, orig_index_exp));
478 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
479 NULL_TREE, NULL_TREE);
481 return expr;
484 /* Given the cast expression EXP, checking out its validity. Either return
485 an error_mark_node if there was an unavoidable error, return a cast to
486 void for trying to delete a pointer w/ the value 0, or return the
487 call to delete. If DOING_VEC is true, we handle things differently
488 for doing an array delete.
489 Implements ARM $5.3.4. This is called from the parser. */
491 tree
492 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
493 tsubst_flags_t complain)
495 tree t, type;
497 if (exp == error_mark_node)
498 return exp;
500 if (processing_template_decl)
502 t = build_min (DELETE_EXPR, void_type_node, exp, size);
503 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
504 DELETE_EXPR_USE_VEC (t) = doing_vec;
505 TREE_SIDE_EFFECTS (t) = 1;
506 return t;
509 /* An array can't have been allocated by new, so complain. */
510 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
511 warning (0, "deleting array %q#E", exp);
513 t = build_expr_type_conversion (WANT_POINTER, exp, true);
515 if (t == NULL_TREE || t == error_mark_node)
517 error ("type %q#T argument given to %<delete%>, expected pointer",
518 TREE_TYPE (exp));
519 return error_mark_node;
522 type = TREE_TYPE (t);
524 /* As of Valley Forge, you can delete a pointer to const. */
526 /* You can't delete functions. */
527 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
529 error ("cannot delete a function. Only pointer-to-objects are "
530 "valid arguments to %<delete%>");
531 return error_mark_node;
534 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
535 if (VOID_TYPE_P (TREE_TYPE (type)))
537 warning (OPT_Wdelete_incomplete, "deleting %qT is undefined", type);
538 doing_vec = 0;
541 /* Deleting a pointer with the value zero is valid and has no effect. */
542 if (integer_zerop (t))
543 return build1 (NOP_EXPR, void_type_node, t);
545 if (doing_vec)
546 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
547 sfk_deleting_destructor,
548 use_global_delete, complain);
549 else
550 return build_delete (type, t, sfk_deleting_destructor,
551 LOOKUP_NORMAL, use_global_delete,
552 complain);
555 /* Report an error if the indicated template declaration is not the
556 sort of thing that should be a member template. */
558 void
559 check_member_template (tree tmpl)
561 tree decl;
563 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
564 decl = DECL_TEMPLATE_RESULT (tmpl);
566 if (TREE_CODE (decl) == FUNCTION_DECL
567 || DECL_ALIAS_TEMPLATE_P (tmpl)
568 || (TREE_CODE (decl) == TYPE_DECL
569 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
571 /* The parser rejects template declarations in local classes
572 (with the exception of generic lambdas). */
573 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
574 /* The parser rejects any use of virtual in a function template. */
575 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
576 && DECL_VIRTUAL_P (decl)));
578 /* The debug-information generating code doesn't know what to do
579 with member templates. */
580 DECL_IGNORED_P (tmpl) = 1;
582 else if (variable_template_p (tmpl))
583 /* OK */;
584 else
585 error ("template declaration of %q#D", decl);
588 /* Sanity check: report error if this function FUNCTION is not
589 really a member of the class (CTYPE) it is supposed to belong to.
590 TEMPLATE_PARMS is used to specify the template parameters of a member
591 template passed as FUNCTION_DECL. If the member template is passed as a
592 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
593 from the declaration. If the function is not a function template, it
594 must be NULL.
595 It returns the original declaration for the function, NULL_TREE if
596 no declaration was found, error_mark_node if an error was emitted. */
598 tree
599 check_classfn (tree ctype, tree function, tree template_parms)
601 if (DECL_USE_TEMPLATE (function)
602 && !(TREE_CODE (function) == TEMPLATE_DECL
603 && DECL_TEMPLATE_SPECIALIZATION (function))
604 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
605 /* Since this is a specialization of a member template,
606 we're not going to find the declaration in the class.
607 For example, in:
609 struct S { template <typename T> void f(T); };
610 template <> void S::f(int);
612 we're not going to find `S::f(int)', but there's no
613 reason we should, either. We let our callers know we didn't
614 find the method, but we don't complain. */
615 return NULL_TREE;
617 /* Basic sanity check: for a template function, the template parameters
618 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
619 if (TREE_CODE (function) == TEMPLATE_DECL)
621 if (template_parms
622 && !comp_template_parms (template_parms,
623 DECL_TEMPLATE_PARMS (function)))
625 error ("template parameter lists provided don%'t match the "
626 "template parameters of %qD", function);
627 return error_mark_node;
629 template_parms = DECL_TEMPLATE_PARMS (function);
632 /* OK, is this a definition of a member template? */
633 bool is_template = (template_parms != NULL_TREE);
635 /* [temp.mem]
637 A destructor shall not be a member template. */
638 if (DECL_DESTRUCTOR_P (function) && is_template)
640 error ("destructor %qD declared as member template", function);
641 return error_mark_node;
644 /* We must enter the scope here, because conversion operators are
645 named by target type, and type equivalence relies on typenames
646 resolving within the scope of CTYPE. */
647 tree pushed_scope = push_scope (ctype);
648 tree matched = NULL_TREE;
649 tree fns = get_class_binding (ctype, DECL_NAME (function));
651 for (ovl_iterator iter (fns); !matched && iter; ++iter)
653 tree fndecl = *iter;
655 /* A member template definition only matches a member template
656 declaration. */
657 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
658 continue;
660 if (!DECL_DECLARES_FUNCTION_P (fndecl))
661 continue;
663 tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
664 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
666 /* We cannot simply call decls_match because this doesn't work
667 for static member functions that are pretending to be
668 methods, and because the name may have been changed by
669 asm("new_name"). */
671 /* Get rid of the this parameter on functions that become
672 static. */
673 if (DECL_STATIC_FUNCTION_P (fndecl)
674 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
675 p1 = TREE_CHAIN (p1);
677 /* ref-qualifier or absence of same must match. */
678 if (type_memfn_rqual (TREE_TYPE (function))
679 != type_memfn_rqual (TREE_TYPE (fndecl)))
680 continue;
682 // Include constraints in the match.
683 tree c1 = get_constraints (function);
684 tree c2 = get_constraints (fndecl);
686 /* While finding a match, same types and params are not enough
687 if the function is versioned. Also check version ("target")
688 attributes. */
689 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
690 TREE_TYPE (TREE_TYPE (fndecl)))
691 && compparms (p1, p2)
692 && !targetm.target_option.function_versions (function, fndecl)
693 && (!is_template
694 || comp_template_parms (template_parms,
695 DECL_TEMPLATE_PARMS (fndecl)))
696 && equivalent_constraints (c1, c2)
697 && (DECL_TEMPLATE_SPECIALIZATION (function)
698 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
699 && (!DECL_TEMPLATE_SPECIALIZATION (function)
700 || (DECL_TI_TEMPLATE (function) == DECL_TI_TEMPLATE (fndecl))))
701 matched = fndecl;
704 if (!matched)
706 if (!COMPLETE_TYPE_P (ctype))
707 cxx_incomplete_type_error (function, ctype);
708 else
710 if (DECL_CONV_FN_P (function))
711 fns = get_class_binding (ctype, conv_op_identifier);
713 error_at (DECL_SOURCE_LOCATION (function),
714 "no declaration matches %q#D", function);
715 if (fns)
716 print_candidates (fns);
717 else if (DECL_CONV_FN_P (function))
718 inform (DECL_SOURCE_LOCATION (function),
719 "no conversion operators declared");
720 else
721 inform (DECL_SOURCE_LOCATION (function),
722 "no functions named %qD", function);
723 inform (DECL_SOURCE_LOCATION (TYPE_NAME (ctype)),
724 "%#qT defined here", ctype);
726 matched = error_mark_node;
729 if (pushed_scope)
730 pop_scope (pushed_scope);
732 return matched;
735 /* DECL is a function with vague linkage. Remember it so that at the
736 end of the translation unit we can decide whether or not to emit
737 it. */
739 void
740 note_vague_linkage_fn (tree decl)
742 DECL_DEFER_OUTPUT (decl) = 1;
743 vec_safe_push (deferred_fns, decl);
746 /* As above, but for variable template instantiations. */
748 void
749 note_variable_template_instantiation (tree decl)
751 vec_safe_push (pending_statics, decl);
754 /* We have just processed the DECL, which is a static data member.
755 The other parameters are as for cp_finish_decl. */
757 void
758 finish_static_data_member_decl (tree decl,
759 tree init, bool init_const_expr_p,
760 tree asmspec_tree,
761 int flags)
763 DECL_CONTEXT (decl) = current_class_type;
765 /* We cannot call pushdecl here, because that would fill in the
766 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
767 the right thing, namely, to put this decl out straight away. */
769 if (! processing_template_decl)
770 vec_safe_push (pending_statics, decl);
772 if (LOCAL_CLASS_P (current_class_type)
773 /* We already complained about the template definition. */
774 && !DECL_TEMPLATE_INSTANTIATION (decl))
775 permerror (input_location, "local class %q#T shall not have static data member %q#D",
776 current_class_type, decl);
777 else
778 for (tree t = current_class_type; TYPE_P (t);
779 t = CP_TYPE_CONTEXT (t))
780 if (TYPE_UNNAMED_P (t))
782 if (permerror (DECL_SOURCE_LOCATION (decl),
783 "static data member %qD in unnamed class", decl))
784 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
785 "unnamed class defined here");
786 break;
789 DECL_IN_AGGR_P (decl) = 1;
791 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
792 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
793 SET_VAR_HAD_UNKNOWN_BOUND (decl);
795 if (init)
797 /* Similarly to start_decl_1, we want to complete the type in order
798 to do the right thing in cp_apply_type_quals_to_decl, possibly
799 clear TYPE_QUAL_CONST (c++/65579). */
800 tree type = TREE_TYPE (decl) = complete_type (TREE_TYPE (decl));
801 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
804 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
807 /* DECLARATOR and DECLSPECS correspond to a class member. The other
808 parameters are as for cp_finish_decl. Return the DECL for the
809 class member declared. */
811 tree
812 grokfield (const cp_declarator *declarator,
813 cp_decl_specifier_seq *declspecs,
814 tree init, bool init_const_expr_p,
815 tree asmspec_tree,
816 tree attrlist)
818 tree value;
819 const char *asmspec = 0;
820 int flags;
821 tree name;
823 if (init
824 && TREE_CODE (init) == TREE_LIST
825 && TREE_VALUE (init) == error_mark_node
826 && TREE_CHAIN (init) == NULL_TREE)
827 init = NULL_TREE;
829 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
830 if (! value || value == error_mark_node)
831 /* friend or constructor went bad. */
832 return error_mark_node;
833 if (TREE_TYPE (value) == error_mark_node)
834 return value;
836 if (TREE_CODE (value) == TYPE_DECL && init)
838 error ("typedef %qD is initialized (use decltype instead)", value);
839 init = NULL_TREE;
842 /* Pass friendly classes back. */
843 if (value == void_type_node)
844 return value;
847 name = DECL_NAME (value);
849 if (name != NULL_TREE)
851 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
853 error ("explicit template argument list not allowed");
854 return error_mark_node;
857 if (IDENTIFIER_POINTER (name)[0] == '_'
858 && id_equal (name, "_vptr"))
859 error ("member %qD conflicts with virtual function table field name",
860 value);
863 /* Stash away type declarations. */
864 if (TREE_CODE (value) == TYPE_DECL)
866 DECL_NONLOCAL (value) = 1;
867 DECL_CONTEXT (value) = current_class_type;
869 if (attrlist)
871 int attrflags = 0;
873 /* If this is a typedef that names the class for linkage purposes
874 (7.1.3p8), apply any attributes directly to the type. */
875 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
876 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
877 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
879 cplus_decl_attributes (&value, attrlist, attrflags);
882 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
883 && TREE_TYPE (value) != error_mark_node
884 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
885 set_underlying_type (value);
887 /* It's important that push_template_decl below follows
888 set_underlying_type above so that the created template
889 carries the properly set type of VALUE. */
890 if (processing_template_decl)
891 value = push_template_decl (value);
893 record_locally_defined_typedef (value);
894 return value;
897 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
899 if (!friendp && DECL_IN_AGGR_P (value))
901 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
902 return void_type_node;
905 if (asmspec_tree && asmspec_tree != error_mark_node)
906 asmspec = TREE_STRING_POINTER (asmspec_tree);
908 if (init)
910 if (TREE_CODE (value) == FUNCTION_DECL)
912 if (init == ridpointers[(int)RID_DELETE])
914 if (friendp && decl_defined_p (value))
916 error ("redefinition of %q#D", value);
917 inform (DECL_SOURCE_LOCATION (value),
918 "%q#D previously defined here", value);
920 else
922 DECL_DELETED_FN (value) = 1;
923 DECL_DECLARED_INLINE_P (value) = 1;
924 DECL_INITIAL (value) = error_mark_node;
927 else if (init == ridpointers[(int)RID_DEFAULT])
929 if (defaultable_fn_check (value))
931 DECL_DEFAULTED_FN (value) = 1;
932 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
933 DECL_DECLARED_INLINE_P (value) = 1;
936 else if (TREE_CODE (init) == DEFAULT_ARG)
937 error ("invalid initializer for member function %qD", value);
938 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
940 if (integer_zerop (init))
941 DECL_PURE_VIRTUAL_P (value) = 1;
942 else if (error_operand_p (init))
943 ; /* An error has already been reported. */
944 else
945 error ("invalid initializer for member function %qD",
946 value);
948 else
950 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
951 if (friendp)
952 error ("initializer specified for friend function %qD",
953 value);
954 else
955 error ("initializer specified for static member function %qD",
956 value);
959 else if (TREE_CODE (value) == FIELD_DECL)
960 /* C++11 NSDMI, keep going. */;
961 else if (!VAR_P (value))
962 gcc_unreachable ();
965 /* Pass friend decls back. */
966 if ((TREE_CODE (value) == FUNCTION_DECL
967 || TREE_CODE (value) == TEMPLATE_DECL)
968 && DECL_CONTEXT (value) != current_class_type)
969 return value;
971 /* Need to set this before push_template_decl. */
972 if (VAR_P (value))
973 DECL_CONTEXT (value) = current_class_type;
975 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
977 value = push_template_decl (value);
978 if (error_operand_p (value))
979 return error_mark_node;
982 if (attrlist)
983 cplus_decl_attributes (&value, attrlist, 0);
985 if (init && DIRECT_LIST_INIT_P (init))
986 flags = LOOKUP_NORMAL;
987 else
988 flags = LOOKUP_IMPLICIT;
990 switch (TREE_CODE (value))
992 case VAR_DECL:
993 finish_static_data_member_decl (value, init, init_const_expr_p,
994 asmspec_tree, flags);
995 return value;
997 case FIELD_DECL:
998 if (asmspec)
999 error ("%<asm%> specifiers are not permitted on non-static data members");
1000 if (DECL_INITIAL (value) == error_mark_node)
1001 init = error_mark_node;
1002 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
1003 NULL_TREE, flags);
1004 DECL_IN_AGGR_P (value) = 1;
1005 return value;
1007 case FUNCTION_DECL:
1008 if (asmspec)
1009 set_user_assembler_name (value, asmspec);
1011 cp_finish_decl (value,
1012 /*init=*/NULL_TREE,
1013 /*init_const_expr_p=*/false,
1014 asmspec_tree, flags);
1016 /* Pass friends back this way. */
1017 if (DECL_FRIEND_P (value))
1018 return void_type_node;
1020 DECL_IN_AGGR_P (value) = 1;
1021 return value;
1023 default:
1024 gcc_unreachable ();
1026 return NULL_TREE;
1029 /* Like `grokfield', but for bitfields.
1030 WIDTH is the width of the bitfield, a constant expression.
1031 The other parameters are as for grokfield. */
1033 tree
1034 grokbitfield (const cp_declarator *declarator,
1035 cp_decl_specifier_seq *declspecs, tree width, tree init,
1036 tree attrlist)
1038 tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1039 init != NULL_TREE, &attrlist);
1041 if (value == error_mark_node)
1042 return NULL_TREE; /* friends went bad. */
1043 if (TREE_TYPE (value) == error_mark_node)
1044 return value;
1046 /* Pass friendly classes back. */
1047 if (VOID_TYPE_P (value))
1048 return void_type_node;
1050 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
1051 && (POINTER_TYPE_P (value)
1052 || !dependent_type_p (TREE_TYPE (value))))
1054 error ("bit-field %qD with non-integral type", value);
1055 return error_mark_node;
1058 if (TREE_CODE (value) == TYPE_DECL)
1060 error ("cannot declare %qD to be a bit-field type", value);
1061 return NULL_TREE;
1064 /* Usually, finish_struct_1 catches bitfields with invalid types.
1065 But, in the case of bitfields with function type, we confuse
1066 ourselves into thinking they are member functions, so we must
1067 check here. */
1068 if (TREE_CODE (value) == FUNCTION_DECL)
1070 error ("cannot declare bit-field %qD with function type",
1071 DECL_NAME (value));
1072 return NULL_TREE;
1075 if (width && TYPE_WARN_IF_NOT_ALIGN (TREE_TYPE (value)))
1077 error ("cannot declare bit-field %qD with %<warn_if_not_aligned%> type",
1078 DECL_NAME (value));
1079 return NULL_TREE;
1082 if (DECL_IN_AGGR_P (value))
1084 error ("%qD is already defined in the class %qT", value,
1085 DECL_CONTEXT (value));
1086 return void_type_node;
1089 if (TREE_STATIC (value))
1091 error ("static member %qD cannot be a bit-field", value);
1092 return NULL_TREE;
1095 int flags = LOOKUP_IMPLICIT;
1096 if (init && DIRECT_LIST_INIT_P (init))
1097 flags = LOOKUP_NORMAL;
1098 cp_finish_decl (value, init, false, NULL_TREE, flags);
1100 if (width != error_mark_node)
1102 /* The width must be an integer type. */
1103 if (!type_dependent_expression_p (width)
1104 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1105 error ("width of bit-field %qD has non-integral type %qT", value,
1106 TREE_TYPE (width));
1107 else
1109 /* Temporarily stash the width in DECL_BIT_FIELD_REPRESENTATIVE.
1110 check_bitfield_decl picks it from there later and sets DECL_SIZE
1111 accordingly. */
1112 DECL_BIT_FIELD_REPRESENTATIVE (value) = width;
1113 SET_DECL_C_BIT_FIELD (value);
1117 DECL_IN_AGGR_P (value) = 1;
1119 if (attrlist)
1120 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1122 return value;
1126 /* Returns true iff ATTR is an attribute which needs to be applied at
1127 instantiation time rather than template definition time. */
1129 static bool
1130 is_late_template_attribute (tree attr, tree decl)
1132 tree name = get_attribute_name (attr);
1133 tree args = TREE_VALUE (attr);
1134 const struct attribute_spec *spec = lookup_attribute_spec (name);
1135 tree arg;
1137 if (!spec)
1138 /* Unknown attribute. */
1139 return false;
1141 /* Attribute weak handling wants to write out assembly right away. */
1142 if (is_attribute_p ("weak", name))
1143 return true;
1145 /* Attributes used and unused are applied directly, as they appertain to
1146 decls. */
1147 if (is_attribute_p ("unused", name)
1148 || is_attribute_p ("used", name))
1149 return false;
1151 /* Attribute tls_model wants to modify the symtab. */
1152 if (is_attribute_p ("tls_model", name))
1153 return true;
1155 /* #pragma omp declare simd attribute needs to be always deferred. */
1156 if (flag_openmp
1157 && is_attribute_p ("omp declare simd", name))
1158 return true;
1160 /* An attribute pack is clearly dependent. */
1161 if (args && PACK_EXPANSION_P (args))
1162 return true;
1164 /* If any of the arguments are dependent expressions, we can't evaluate
1165 the attribute until instantiation time. */
1166 for (arg = args; arg; arg = TREE_CHAIN (arg))
1168 tree t = TREE_VALUE (arg);
1170 /* If the first attribute argument is an identifier, only consider
1171 second and following arguments. Attributes like mode, format,
1172 cleanup and several target specific attributes aren't late
1173 just because they have an IDENTIFIER_NODE as first argument. */
1174 if (arg == args && attribute_takes_identifier_p (name)
1175 && identifier_p (t))
1176 continue;
1178 if (value_dependent_expression_p (t)
1179 || type_dependent_expression_p (t))
1180 return true;
1183 if (TREE_CODE (decl) == TYPE_DECL
1184 || TYPE_P (decl)
1185 || spec->type_required)
1187 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1189 /* We can't apply any attributes to a completely unknown type until
1190 instantiation time. */
1191 enum tree_code code = TREE_CODE (type);
1192 if (code == TEMPLATE_TYPE_PARM
1193 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1194 || code == TYPENAME_TYPE)
1195 return true;
1196 /* Also defer most attributes on dependent types. This is not
1197 necessary in all cases, but is the better default. */
1198 else if (dependent_type_p (type)
1199 /* But some attributes specifically apply to templates. */
1200 && !is_attribute_p ("abi_tag", name)
1201 && !is_attribute_p ("deprecated", name)
1202 && !is_attribute_p ("visibility", name))
1203 return true;
1204 else
1205 return false;
1207 else
1208 return false;
1211 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1212 applied at instantiation time and return them. If IS_DEPENDENT is true,
1213 the declaration itself is dependent, so all attributes should be applied
1214 at instantiation time. */
1216 static tree
1217 splice_template_attributes (tree *attr_p, tree decl)
1219 tree *p = attr_p;
1220 tree late_attrs = NULL_TREE;
1221 tree *q = &late_attrs;
1223 if (!p)
1224 return NULL_TREE;
1226 for (; *p; )
1228 if (is_late_template_attribute (*p, decl))
1230 ATTR_IS_DEPENDENT (*p) = 1;
1231 *q = *p;
1232 *p = TREE_CHAIN (*p);
1233 q = &TREE_CHAIN (*q);
1234 *q = NULL_TREE;
1236 else
1237 p = &TREE_CHAIN (*p);
1240 return late_attrs;
1243 /* Remove any late attributes from the list in ATTR_P and attach them to
1244 DECL_P. */
1246 static void
1247 save_template_attributes (tree *attr_p, tree *decl_p, int flags)
1249 tree *q;
1251 if (attr_p && *attr_p == error_mark_node)
1252 return;
1254 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1255 if (!late_attrs)
1256 return;
1258 if (DECL_P (*decl_p))
1259 q = &DECL_ATTRIBUTES (*decl_p);
1260 else
1261 q = &TYPE_ATTRIBUTES (*decl_p);
1263 tree old_attrs = *q;
1265 /* Merge the late attributes at the beginning with the attribute
1266 list. */
1267 late_attrs = merge_attributes (late_attrs, *q);
1268 if (*q != late_attrs
1269 && !DECL_P (*decl_p)
1270 && !(flags & ATTR_FLAG_TYPE_IN_PLACE))
1272 if (!dependent_type_p (*decl_p))
1273 *decl_p = cp_build_type_attribute_variant (*decl_p, late_attrs);
1274 else
1276 *decl_p = build_variant_type_copy (*decl_p);
1277 TYPE_ATTRIBUTES (*decl_p) = late_attrs;
1280 else
1281 *q = late_attrs;
1283 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1285 /* We've added new attributes directly to the main variant, so
1286 now we need to update all of the other variants to include
1287 these new attributes. */
1288 tree variant;
1289 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1290 variant = TYPE_NEXT_VARIANT (variant))
1292 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1293 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1298 /* True if ATTRS contains any dependent attributes that affect type
1299 identity. */
1301 bool
1302 any_dependent_type_attributes_p (tree attrs)
1304 for (tree a = attrs; a; a = TREE_CHAIN (a))
1305 if (ATTR_IS_DEPENDENT (a))
1307 const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (a));
1308 if (as && as->affects_type_identity)
1309 return true;
1311 return false;
1314 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1315 to a typedef which gives a previously unnamed class or enum a name for
1316 linkage purposes. */
1318 bool
1319 attributes_naming_typedef_ok (tree attrs)
1321 for (; attrs; attrs = TREE_CHAIN (attrs))
1323 tree name = get_attribute_name (attrs);
1324 if (is_attribute_p ("vector_size", name))
1325 return false;
1327 return true;
1330 /* Like reconstruct_complex_type, but handle also template trees. */
1332 tree
1333 cp_reconstruct_complex_type (tree type, tree bottom)
1335 tree inner, outer;
1336 bool late_return_type_p = false;
1338 if (TYPE_PTR_P (type))
1340 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1341 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1342 TYPE_REF_CAN_ALIAS_ALL (type));
1344 else if (TREE_CODE (type) == REFERENCE_TYPE)
1346 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1347 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1348 TYPE_REF_CAN_ALIAS_ALL (type));
1350 else if (TREE_CODE (type) == ARRAY_TYPE)
1352 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1353 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1354 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1355 element type qualification will be handled by the recursive
1356 cp_reconstruct_complex_type call and cp_build_qualified_type
1357 for ARRAY_TYPEs changes the element type. */
1358 return outer;
1360 else if (TREE_CODE (type) == FUNCTION_TYPE)
1362 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1363 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1364 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1365 outer = apply_memfn_quals (outer,
1366 type_memfn_quals (type),
1367 type_memfn_rqual (type));
1369 else if (TREE_CODE (type) == METHOD_TYPE)
1371 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1372 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1373 /* The build_method_type_directly() routine prepends 'this' to argument list,
1374 so we must compensate by getting rid of it. */
1375 outer
1376 = build_method_type_directly
1377 (class_of_this_parm (type), inner,
1378 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1380 else if (TREE_CODE (type) == OFFSET_TYPE)
1382 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1383 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1385 else
1386 return bottom;
1388 if (TYPE_ATTRIBUTES (type))
1389 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1390 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1392 if (late_return_type_p)
1393 TYPE_HAS_LATE_RETURN_TYPE (outer) = 1;
1395 return outer;
1398 /* Replaces any constexpr expression that may be into the attributes
1399 arguments with their reduced value. */
1401 static void
1402 cp_check_const_attributes (tree attributes)
1404 if (attributes == error_mark_node)
1405 return;
1407 tree attr;
1408 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1410 tree arg;
1411 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1413 tree expr = TREE_VALUE (arg);
1414 if (EXPR_P (expr))
1415 TREE_VALUE (arg) = maybe_constant_value (expr);
1420 /* Return true if TYPE is an OpenMP mappable type. */
1421 bool
1422 cp_omp_mappable_type (tree type)
1424 /* Mappable type has to be complete. */
1425 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1426 return false;
1427 /* Arrays have mappable type if the elements have mappable type. */
1428 while (TREE_CODE (type) == ARRAY_TYPE)
1429 type = TREE_TYPE (type);
1430 /* A mappable type cannot contain virtual members. */
1431 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1432 return false;
1433 /* All data members must be non-static. */
1434 if (CLASS_TYPE_P (type))
1436 tree field;
1437 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1438 if (VAR_P (field))
1439 return false;
1440 /* All fields must have mappable types. */
1441 else if (TREE_CODE (field) == FIELD_DECL
1442 && !cp_omp_mappable_type (TREE_TYPE (field)))
1443 return false;
1445 return true;
1448 /* Return the last pushed declaration for the symbol DECL or NULL
1449 when no such declaration exists. */
1451 static tree
1452 find_last_decl (tree decl)
1454 tree last_decl = NULL_TREE;
1456 if (tree name = DECL_P (decl) ? DECL_NAME (decl) : NULL_TREE)
1458 /* Look up the declaration in its scope. */
1459 tree pushed_scope = NULL_TREE;
1460 if (tree ctype = DECL_CONTEXT (decl))
1461 pushed_scope = push_scope (ctype);
1463 last_decl = lookup_name (name);
1465 if (pushed_scope)
1466 pop_scope (pushed_scope);
1468 /* The declaration may be a member conversion operator
1469 or a bunch of overfloads (handle the latter below). */
1470 if (last_decl && BASELINK_P (last_decl))
1471 last_decl = BASELINK_FUNCTIONS (last_decl);
1474 if (!last_decl)
1475 return NULL_TREE;
1477 if (DECL_P (last_decl) || TREE_CODE (last_decl) == OVERLOAD)
1479 /* A set of overloads of the same function. */
1480 for (lkp_iterator iter (last_decl); iter; ++iter)
1482 if (TREE_CODE (*iter) == OVERLOAD)
1483 continue;
1485 if (decls_match (decl, *iter, /*record_decls=*/false))
1486 return *iter;
1488 return NULL_TREE;
1491 return NULL_TREE;
1494 /* Like decl_attributes, but handle C++ complexity. */
1496 void
1497 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1499 if (*decl == NULL_TREE || *decl == void_type_node
1500 || *decl == error_mark_node)
1501 return;
1503 /* Add implicit "omp declare target" attribute if requested. */
1504 if (scope_chain->omp_declare_target_attribute
1505 && ((VAR_P (*decl)
1506 && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
1507 || TREE_CODE (*decl) == FUNCTION_DECL))
1509 if (VAR_P (*decl)
1510 && DECL_CLASS_SCOPE_P (*decl))
1511 error ("%q+D static data member inside of declare target directive",
1512 *decl);
1513 else if (!processing_template_decl
1514 && VAR_P (*decl)
1515 && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1516 error ("%q+D in declare target directive does not have mappable type",
1517 *decl);
1518 else
1519 attributes = tree_cons (get_identifier ("omp declare target"),
1520 NULL_TREE, attributes);
1523 if (processing_template_decl)
1525 if (check_for_bare_parameter_packs (attributes))
1526 return;
1528 save_template_attributes (&attributes, decl, flags);
1531 cp_check_const_attributes (attributes);
1533 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1534 decl = &DECL_TEMPLATE_RESULT (*decl);
1536 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1538 attributes
1539 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1540 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1541 attributes, flags);
1543 else
1545 tree last_decl = find_last_decl (*decl);
1546 decl_attributes (decl, attributes, flags, last_decl);
1549 if (TREE_CODE (*decl) == TYPE_DECL)
1550 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1552 /* Propagate deprecation out to the template. */
1553 if (TREE_DEPRECATED (*decl))
1554 if (tree ti = get_template_info (*decl))
1556 tree tmpl = TI_TEMPLATE (ti);
1557 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1558 : DECL_TEMPLATE_RESULT (tmpl));
1559 if (*decl == pattern)
1560 TREE_DEPRECATED (tmpl) = true;
1564 /* Walks through the namespace- or function-scope anonymous union
1565 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1566 Returns one of the fields for use in the mangled name. */
1568 static tree
1569 build_anon_union_vars (tree type, tree object)
1571 tree main_decl = NULL_TREE;
1572 tree field;
1574 /* Rather than write the code to handle the non-union case,
1575 just give an error. */
1576 if (TREE_CODE (type) != UNION_TYPE)
1578 error ("anonymous struct not inside named type");
1579 return error_mark_node;
1582 for (field = TYPE_FIELDS (type);
1583 field != NULL_TREE;
1584 field = DECL_CHAIN (field))
1586 tree decl;
1587 tree ref;
1589 if (DECL_ARTIFICIAL (field))
1590 continue;
1591 if (TREE_CODE (field) != FIELD_DECL)
1593 permerror (DECL_SOURCE_LOCATION (field),
1594 "%q#D invalid; an anonymous union can only "
1595 "have non-static data members", field);
1596 continue;
1599 if (TREE_PRIVATE (field))
1600 permerror (DECL_SOURCE_LOCATION (field),
1601 "private member %q#D in anonymous union", field);
1602 else if (TREE_PROTECTED (field))
1603 permerror (DECL_SOURCE_LOCATION (field),
1604 "protected member %q#D in anonymous union", field);
1606 if (processing_template_decl)
1607 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1608 DECL_NAME (field), NULL_TREE);
1609 else
1610 ref = build_class_member_access_expr (object, field, NULL_TREE,
1611 false, tf_warning_or_error);
1613 if (DECL_NAME (field))
1615 tree base;
1617 decl = build_decl (input_location,
1618 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1619 DECL_ANON_UNION_VAR_P (decl) = 1;
1620 DECL_ARTIFICIAL (decl) = 1;
1622 base = get_base_address (object);
1623 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1624 TREE_STATIC (decl) = TREE_STATIC (base);
1625 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1627 SET_DECL_VALUE_EXPR (decl, ref);
1628 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1630 decl = pushdecl (decl);
1632 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1633 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1634 else
1635 decl = 0;
1637 if (main_decl == NULL_TREE)
1638 main_decl = decl;
1641 return main_decl;
1644 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1645 anonymous union, then all members must be laid out together. PUBLIC_P
1646 is nonzero if this union is not declared static. */
1648 void
1649 finish_anon_union (tree anon_union_decl)
1651 tree type;
1652 tree main_decl;
1653 bool public_p;
1655 if (anon_union_decl == error_mark_node)
1656 return;
1658 type = TREE_TYPE (anon_union_decl);
1659 public_p = TREE_PUBLIC (anon_union_decl);
1661 /* The VAR_DECL's context is the same as the TYPE's context. */
1662 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1664 if (TYPE_FIELDS (type) == NULL_TREE)
1665 return;
1667 if (public_p)
1669 error ("namespace-scope anonymous aggregates must be static");
1670 return;
1673 main_decl = build_anon_union_vars (type, anon_union_decl);
1674 if (main_decl == error_mark_node)
1675 return;
1676 if (main_decl == NULL_TREE)
1678 pedwarn (input_location, 0, "anonymous union with no members");
1679 return;
1682 if (!processing_template_decl)
1684 /* Use main_decl to set the mangled name. */
1685 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1686 maybe_commonize_var (anon_union_decl);
1687 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1688 mangle_decl (anon_union_decl);
1689 DECL_NAME (anon_union_decl) = NULL_TREE;
1692 pushdecl (anon_union_decl);
1693 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1696 /* Auxiliary functions to make type signatures for
1697 `operator new' and `operator delete' correspond to
1698 what compiler will be expecting. */
1700 tree
1701 coerce_new_type (tree type)
1703 int e = 0;
1704 tree args = TYPE_ARG_TYPES (type);
1706 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1708 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1710 e = 1;
1711 error ("%<operator new%> must return type %qT", ptr_type_node);
1714 if (args && args != void_list_node)
1716 if (TREE_PURPOSE (args))
1718 /* [basic.stc.dynamic.allocation]
1720 The first parameter shall not have an associated default
1721 argument. */
1722 error ("the first parameter of %<operator new%> cannot "
1723 "have a default argument");
1724 /* Throw away the default argument. */
1725 TREE_PURPOSE (args) = NULL_TREE;
1728 if (!same_type_p (TREE_VALUE (args), size_type_node))
1730 e = 2;
1731 args = TREE_CHAIN (args);
1734 else
1735 e = 2;
1737 if (e == 2)
1738 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1739 "as first parameter", size_type_node);
1741 switch (e)
1743 case 2:
1744 args = tree_cons (NULL_TREE, size_type_node, args);
1745 /* Fall through. */
1746 case 1:
1747 type = build_exception_variant
1748 (build_function_type (ptr_type_node, args),
1749 TYPE_RAISES_EXCEPTIONS (type));
1750 /* Fall through. */
1751 default:;
1753 return type;
1756 tree
1757 coerce_delete_type (tree type)
1759 int e = 0;
1760 tree args = TYPE_ARG_TYPES (type);
1762 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1764 if (!same_type_p (TREE_TYPE (type), void_type_node))
1766 e = 1;
1767 error ("%<operator delete%> must return type %qT", void_type_node);
1770 if (!args || args == void_list_node
1771 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1773 e = 2;
1774 if (args && args != void_list_node)
1775 args = TREE_CHAIN (args);
1776 error ("%<operator delete%> takes type %qT as first parameter",
1777 ptr_type_node);
1779 switch (e)
1781 case 2:
1782 args = tree_cons (NULL_TREE, ptr_type_node, args);
1783 /* Fall through. */
1784 case 1:
1785 type = build_exception_variant
1786 (build_function_type (void_type_node, args),
1787 TYPE_RAISES_EXCEPTIONS (type));
1788 /* Fall through. */
1789 default:;
1792 return type;
1795 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1796 and mark them as needed. */
1798 static void
1799 mark_vtable_entries (tree decl)
1801 tree fnaddr;
1802 unsigned HOST_WIDE_INT idx;
1804 /* It's OK for the vtable to refer to deprecated virtual functions. */
1805 warning_sentinel w(warn_deprecated_decl);
1807 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1808 idx, fnaddr)
1810 tree fn;
1812 STRIP_NOPS (fnaddr);
1814 if (TREE_CODE (fnaddr) != ADDR_EXPR
1815 && TREE_CODE (fnaddr) != FDESC_EXPR)
1816 /* This entry is an offset: a virtual base class offset, a
1817 virtual call offset, an RTTI offset, etc. */
1818 continue;
1820 fn = TREE_OPERAND (fnaddr, 0);
1821 TREE_ADDRESSABLE (fn) = 1;
1822 /* When we don't have vcall offsets, we output thunks whenever
1823 we output the vtables that contain them. With vcall offsets,
1824 we know all the thunks we'll need when we emit a virtual
1825 function, so we emit the thunks there instead. */
1826 if (DECL_THUNK_P (fn))
1827 use_thunk (fn, /*emit_p=*/0);
1828 /* Set the location, as marking the function could cause
1829 instantiation. We do not need to preserve the incoming
1830 location, as we're called from c_parse_final_cleanups, which
1831 takes care of that. */
1832 input_location = DECL_SOURCE_LOCATION (fn);
1833 mark_used (fn);
1837 /* Set DECL up to have the closest approximation of "initialized common"
1838 linkage available. */
1840 void
1841 comdat_linkage (tree decl)
1843 if (flag_weak)
1844 make_decl_one_only (decl, cxx_comdat_group (decl));
1845 else if (TREE_CODE (decl) == FUNCTION_DECL
1846 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1847 /* We can just emit function and compiler-generated variables
1848 statically; having multiple copies is (for the most part) only
1849 a waste of space.
1851 There are two correctness issues, however: the address of a
1852 template instantiation with external linkage should be the
1853 same, independent of what translation unit asks for the
1854 address, and this will not hold when we emit multiple copies of
1855 the function. However, there's little else we can do.
1857 Also, by default, the typeinfo implementation assumes that
1858 there will be only one copy of the string used as the name for
1859 each type. Therefore, if weak symbols are unavailable, the
1860 run-time library should perform a more conservative check; it
1861 should perform a string comparison, rather than an address
1862 comparison. */
1863 TREE_PUBLIC (decl) = 0;
1864 else
1866 /* Static data member template instantiations, however, cannot
1867 have multiple copies. */
1868 if (DECL_INITIAL (decl) == 0
1869 || DECL_INITIAL (decl) == error_mark_node)
1870 DECL_COMMON (decl) = 1;
1871 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1873 DECL_COMMON (decl) = 1;
1874 DECL_INITIAL (decl) = error_mark_node;
1876 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1878 /* We can't do anything useful; leave vars for explicit
1879 instantiation. */
1880 DECL_EXTERNAL (decl) = 1;
1881 DECL_NOT_REALLY_EXTERN (decl) = 0;
1885 if (TREE_PUBLIC (decl))
1886 DECL_COMDAT (decl) = 1;
1889 /* For win32 we also want to put explicit instantiations in
1890 linkonce sections, so that they will be merged with implicit
1891 instantiations; otherwise we get duplicate symbol errors.
1892 For Darwin we do not want explicit instantiations to be
1893 linkonce. */
1895 void
1896 maybe_make_one_only (tree decl)
1898 /* We used to say that this was not necessary on targets that support weak
1899 symbols, because the implicit instantiations will defer to the explicit
1900 one. However, that's not actually the case in SVR4; a strong definition
1901 after a weak one is an error. Also, not making explicit
1902 instantiations one_only means that we can end up with two copies of
1903 some template instantiations. */
1904 if (! flag_weak)
1905 return;
1907 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1908 we can get away with not emitting them if they aren't used. We need
1909 to for variables so that cp_finish_decl will update their linkage,
1910 because their DECL_INITIAL may not have been set properly yet. */
1912 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1913 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1914 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1916 make_decl_one_only (decl, cxx_comdat_group (decl));
1918 if (VAR_P (decl))
1920 varpool_node *node = varpool_node::get_create (decl);
1921 DECL_COMDAT (decl) = 1;
1922 /* Mark it needed so we don't forget to emit it. */
1923 node->forced_by_abi = true;
1924 TREE_USED (decl) = 1;
1929 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1930 This predicate will give the right answer during parsing of the
1931 function, which other tests may not. */
1933 bool
1934 vague_linkage_p (tree decl)
1936 if (!TREE_PUBLIC (decl))
1938 /* maybe_thunk_body clears TREE_PUBLIC on the maybe-in-charge 'tor
1939 variants, check one of the "clones" for the real linkage. */
1940 if ((DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
1941 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl))
1942 && DECL_CHAIN (decl)
1943 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)))
1944 return vague_linkage_p (DECL_CHAIN (decl));
1946 gcc_checking_assert (!DECL_COMDAT (decl));
1947 return false;
1949 /* Unfortunately, import_export_decl has not always been called
1950 before the function is processed, so we cannot simply check
1951 DECL_COMDAT. */
1952 if (DECL_COMDAT (decl)
1953 || (TREE_CODE (decl) == FUNCTION_DECL
1954 && DECL_DECLARED_INLINE_P (decl))
1955 || (DECL_LANG_SPECIFIC (decl)
1956 && DECL_TEMPLATE_INSTANTIATION (decl))
1957 || (VAR_P (decl) && DECL_INLINE_VAR_P (decl)))
1958 return true;
1959 else if (DECL_FUNCTION_SCOPE_P (decl))
1960 /* A local static in an inline effectively has vague linkage. */
1961 return (TREE_STATIC (decl)
1962 && vague_linkage_p (DECL_CONTEXT (decl)));
1963 else
1964 return false;
1967 /* Determine whether or not we want to specifically import or export CTYPE,
1968 using various heuristics. */
1970 static void
1971 import_export_class (tree ctype)
1973 /* -1 for imported, 1 for exported. */
1974 int import_export = 0;
1976 /* It only makes sense to call this function at EOF. The reason is
1977 that this function looks at whether or not the first non-inline
1978 non-abstract virtual member function has been defined in this
1979 translation unit. But, we can't possibly know that until we've
1980 seen the entire translation unit. */
1981 gcc_assert (at_eof);
1983 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1984 return;
1986 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1987 we will have CLASSTYPE_INTERFACE_ONLY set but not
1988 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1989 heuristic because someone will supply a #pragma implementation
1990 elsewhere, and deducing it here would produce a conflict. */
1991 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1992 return;
1994 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1995 import_export = -1;
1996 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1997 import_export = 1;
1998 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1999 && !flag_implicit_templates)
2000 /* For a template class, without -fimplicit-templates, check the
2001 repository. If the virtual table is assigned to this
2002 translation unit, then export the class; otherwise, import
2003 it. */
2004 import_export = repo_export_class_p (ctype) ? 1 : -1;
2005 else if (TYPE_POLYMORPHIC_P (ctype))
2007 /* The ABI specifies that the virtual table and associated
2008 information are emitted with the key method, if any. */
2009 tree method = CLASSTYPE_KEY_METHOD (ctype);
2010 /* If weak symbol support is not available, then we must be
2011 careful not to emit the vtable when the key function is
2012 inline. An inline function can be defined in multiple
2013 translation units. If we were to emit the vtable in each
2014 translation unit containing a definition, we would get
2015 multiple definition errors at link-time. */
2016 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
2017 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
2020 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
2021 a definition anywhere else. */
2022 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
2023 import_export = 0;
2025 /* Allow back ends the chance to overrule the decision. */
2026 if (targetm.cxx.import_export_class)
2027 import_export = targetm.cxx.import_export_class (ctype, import_export);
2029 if (import_export)
2031 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
2032 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
2036 /* Return true if VAR has already been provided to the back end; in that
2037 case VAR should not be modified further by the front end. */
2038 static bool
2039 var_finalized_p (tree var)
2041 return varpool_node::get_create (var)->definition;
2044 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
2045 must be emitted in this translation unit. Mark it as such. */
2047 void
2048 mark_needed (tree decl)
2050 TREE_USED (decl) = 1;
2051 if (TREE_CODE (decl) == FUNCTION_DECL)
2053 /* Extern inline functions don't become needed when referenced.
2054 If we know a method will be emitted in other TU and no new
2055 functions can be marked reachable, just use the external
2056 definition. */
2057 struct cgraph_node *node = cgraph_node::get_create (decl);
2058 node->forced_by_abi = true;
2060 /* #pragma interface and -frepo code can call mark_needed for
2061 maybe-in-charge 'tors; mark the clones as well. */
2062 tree clone;
2063 FOR_EACH_CLONE (clone, decl)
2064 mark_needed (clone);
2066 else if (VAR_P (decl))
2068 varpool_node *node = varpool_node::get_create (decl);
2069 /* C++ frontend use mark_decl_references to force COMDAT variables
2070 to be output that might appear dead otherwise. */
2071 node->forced_by_abi = true;
2075 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
2076 returns true if a definition of this entity should be provided in
2077 this object file. Callers use this function to determine whether
2078 or not to let the back end know that a definition of DECL is
2079 available in this translation unit. */
2081 bool
2082 decl_needed_p (tree decl)
2084 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2085 /* This function should only be called at the end of the translation
2086 unit. We cannot be sure of whether or not something will be
2087 COMDAT until that point. */
2088 gcc_assert (at_eof);
2090 /* All entities with external linkage that are not COMDAT/EXTERN should be
2091 emitted; they may be referred to from other object files. */
2092 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
2093 return true;
2094 /* Functions marked "dllexport" must be emitted so that they are
2095 visible to other DLLs. */
2096 if (flag_keep_inline_dllexport
2097 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
2098 return true;
2100 /* When not optimizing, do not bother to produce definitions for extern
2101 symbols. */
2102 if (DECL_REALLY_EXTERN (decl)
2103 && ((TREE_CODE (decl) != FUNCTION_DECL
2104 && !optimize)
2105 || (TREE_CODE (decl) == FUNCTION_DECL
2106 && !opt_for_fn (decl, optimize)))
2107 && !lookup_attribute ("always_inline", decl))
2108 return false;
2110 /* If this entity was used, let the back end see it; it will decide
2111 whether or not to emit it into the object file. */
2112 if (TREE_USED (decl))
2113 return true;
2115 /* Virtual functions might be needed for devirtualization. */
2116 if (flag_devirtualize
2117 && TREE_CODE (decl) == FUNCTION_DECL
2118 && DECL_VIRTUAL_P (decl))
2119 return true;
2121 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2122 reference to DECL might cause it to be emitted later. */
2123 return false;
2126 /* If necessary, write out the vtables for the dynamic class CTYPE.
2127 Returns true if any vtables were emitted. */
2129 static bool
2130 maybe_emit_vtables (tree ctype)
2132 tree vtbl;
2133 tree primary_vtbl;
2134 int needed = 0;
2135 varpool_node *current = NULL, *last = NULL;
2137 /* If the vtables for this class have already been emitted there is
2138 nothing more to do. */
2139 primary_vtbl = CLASSTYPE_VTABLES (ctype);
2140 if (var_finalized_p (primary_vtbl))
2141 return false;
2142 /* Ignore dummy vtables made by get_vtable_decl. */
2143 if (TREE_TYPE (primary_vtbl) == void_type_node)
2144 return false;
2146 /* On some targets, we cannot determine the key method until the end
2147 of the translation unit -- which is when this function is
2148 called. */
2149 if (!targetm.cxx.key_method_may_be_inline ())
2150 determine_key_method (ctype);
2152 /* See if any of the vtables are needed. */
2153 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2155 import_export_decl (vtbl);
2156 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2157 needed = 1;
2159 if (!needed)
2161 /* If the references to this class' vtables are optimized away,
2162 still emit the appropriate debugging information. See
2163 dfs_debug_mark. */
2164 if (DECL_COMDAT (primary_vtbl)
2165 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2166 note_debug_info_needed (ctype);
2167 return false;
2170 /* The ABI requires that we emit all of the vtables if we emit any
2171 of them. */
2172 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2174 /* Mark entities references from the virtual table as used. */
2175 mark_vtable_entries (vtbl);
2177 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2179 vec<tree, va_gc> *cleanups = NULL;
2180 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2181 LOOKUP_NORMAL);
2183 /* It had better be all done at compile-time. */
2184 gcc_assert (!expr && !cleanups);
2187 /* Write it out. */
2188 DECL_EXTERNAL (vtbl) = 0;
2189 rest_of_decl_compilation (vtbl, 1, 1);
2191 /* Because we're only doing syntax-checking, we'll never end up
2192 actually marking the variable as written. */
2193 if (flag_syntax_only)
2194 TREE_ASM_WRITTEN (vtbl) = 1;
2195 else if (DECL_ONE_ONLY (vtbl))
2197 current = varpool_node::get_create (vtbl);
2198 if (last)
2199 current->add_to_same_comdat_group (last);
2200 last = current;
2204 /* Since we're writing out the vtable here, also write the debug
2205 info. */
2206 note_debug_info_needed (ctype);
2208 return true;
2211 /* A special return value from type_visibility meaning internal
2212 linkage. */
2214 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2216 /* walk_tree helper function for type_visibility. */
2218 static tree
2219 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2221 int *vis_p = (int *)data;
2222 if (! TYPE_P (*tp))
2224 *walk_subtrees = 0;
2226 else if (OVERLOAD_TYPE_P (*tp)
2227 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2229 *vis_p = VISIBILITY_ANON;
2230 return *tp;
2232 else if (CLASS_TYPE_P (*tp)
2233 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2234 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2235 return NULL;
2238 /* Returns the visibility of TYPE, which is the minimum visibility of its
2239 component types. */
2241 static int
2242 type_visibility (tree type)
2244 int vis = VISIBILITY_DEFAULT;
2245 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2246 return vis;
2249 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2250 specified (or if VISIBILITY is static). If TMPL is true, this
2251 constraint is for a template argument, and takes precedence
2252 over explicitly-specified visibility on the template. */
2254 static void
2255 constrain_visibility (tree decl, int visibility, bool tmpl)
2257 if (visibility == VISIBILITY_ANON)
2259 /* extern "C" declarations aren't affected by the anonymous
2260 namespace. */
2261 if (!DECL_EXTERN_C_P (decl))
2263 TREE_PUBLIC (decl) = 0;
2264 DECL_WEAK (decl) = 0;
2265 DECL_COMMON (decl) = 0;
2266 DECL_COMDAT (decl) = false;
2267 if (VAR_OR_FUNCTION_DECL_P (decl))
2269 struct symtab_node *snode = symtab_node::get (decl);
2271 if (snode)
2272 snode->set_comdat_group (NULL);
2274 DECL_INTERFACE_KNOWN (decl) = 1;
2275 if (DECL_LANG_SPECIFIC (decl))
2276 DECL_NOT_REALLY_EXTERN (decl) = 1;
2279 else if (visibility > DECL_VISIBILITY (decl)
2280 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2282 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2283 /* This visibility was not specified. */
2284 DECL_VISIBILITY_SPECIFIED (decl) = false;
2288 /* Constrain the visibility of DECL based on the visibility of its template
2289 arguments. */
2291 static void
2292 constrain_visibility_for_template (tree decl, tree targs)
2294 /* If this is a template instantiation, check the innermost
2295 template args for visibility constraints. The outer template
2296 args are covered by the class check. */
2297 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2298 int i;
2299 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2301 int vis = 0;
2303 tree arg = TREE_VEC_ELT (args, i-1);
2304 if (TYPE_P (arg))
2305 vis = type_visibility (arg);
2306 else
2308 if (REFERENCE_REF_P (arg))
2309 arg = TREE_OPERAND (arg, 0);
2310 if (TREE_TYPE (arg))
2311 STRIP_NOPS (arg);
2312 if (TREE_CODE (arg) == ADDR_EXPR)
2313 arg = TREE_OPERAND (arg, 0);
2314 if (VAR_OR_FUNCTION_DECL_P (arg))
2316 if (! TREE_PUBLIC (arg))
2317 vis = VISIBILITY_ANON;
2318 else
2319 vis = DECL_VISIBILITY (arg);
2322 if (vis)
2323 constrain_visibility (decl, vis, true);
2327 /* Like c_determine_visibility, but with additional C++-specific
2328 behavior.
2330 Function-scope entities can rely on the function's visibility because
2331 it is set in start_preparsed_function.
2333 Class-scope entities cannot rely on the class's visibility until the end
2334 of the enclosing class definition.
2336 Note that because namespaces have multiple independent definitions,
2337 namespace visibility is handled elsewhere using the #pragma visibility
2338 machinery rather than by decorating the namespace declaration.
2340 The goal is for constraints from the type to give a diagnostic, and
2341 other constraints to be applied silently. */
2343 void
2344 determine_visibility (tree decl)
2346 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2348 /* Only relevant for names with external linkage. */
2349 if (!TREE_PUBLIC (decl))
2350 return;
2352 /* Cloned constructors and destructors get the same visibility as
2353 the underlying function. That should be set up in
2354 maybe_clone_body. */
2355 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2357 bool orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2358 enum symbol_visibility orig_visibility = DECL_VISIBILITY (decl);
2360 /* The decl may be a template instantiation, which could influence
2361 visibilty. */
2362 tree template_decl = NULL_TREE;
2363 if (TREE_CODE (decl) == TYPE_DECL)
2365 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2367 if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
2368 template_decl = decl;
2370 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2371 template_decl = decl;
2373 else if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
2374 template_decl = decl;
2376 /* If DECL is a member of a class, visibility specifiers on the
2377 class can influence the visibility of the DECL. */
2378 tree class_type = NULL_TREE;
2379 if (DECL_CLASS_SCOPE_P (decl))
2380 class_type = DECL_CONTEXT (decl);
2381 else
2383 /* Not a class member. */
2385 /* Virtual tables have DECL_CONTEXT set to their associated class,
2386 so they are automatically handled above. */
2387 gcc_assert (!VAR_P (decl)
2388 || !DECL_VTABLE_OR_VTT_P (decl));
2390 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2392 /* Local statics and classes get the visibility of their
2393 containing function by default, except that
2394 -fvisibility-inlines-hidden doesn't affect them. */
2395 tree fn = DECL_CONTEXT (decl);
2396 if (DECL_VISIBILITY_SPECIFIED (fn))
2398 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2399 DECL_VISIBILITY_SPECIFIED (decl) =
2400 DECL_VISIBILITY_SPECIFIED (fn);
2402 else
2404 if (DECL_CLASS_SCOPE_P (fn))
2405 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2406 else if (determine_hidden_inline (fn))
2408 DECL_VISIBILITY (decl) = default_visibility;
2409 DECL_VISIBILITY_SPECIFIED (decl) =
2410 visibility_options.inpragma;
2412 else
2414 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2415 DECL_VISIBILITY_SPECIFIED (decl) =
2416 DECL_VISIBILITY_SPECIFIED (fn);
2420 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2421 but have no TEMPLATE_INFO. Their containing template
2422 function does, and the local class could be constrained
2423 by that. */
2424 if (DECL_LANG_SPECIFIC (fn) && DECL_USE_TEMPLATE (fn))
2425 template_decl = fn;
2426 else if (template_decl)
2428 /* FN must be a regenerated lambda function, since they don't
2429 have template arguments. Find a containing non-lambda
2430 template instantiation. */
2431 tree ctx = fn;
2432 while (ctx && !get_template_info (ctx))
2433 ctx = get_containing_scope (ctx);
2434 template_decl = ctx;
2437 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2438 && flag_visibility_ms_compat)
2440 /* Under -fvisibility-ms-compat, types are visible by default,
2441 even though their contents aren't. */
2442 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2443 int underlying_vis = type_visibility (underlying_type);
2444 if (underlying_vis == VISIBILITY_ANON
2445 || (CLASS_TYPE_P (underlying_type)
2446 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2447 constrain_visibility (decl, underlying_vis, false);
2448 else
2449 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2451 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2453 /* tinfo visibility is based on the type it's for. */
2454 constrain_visibility
2455 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2457 /* Give the target a chance to override the visibility associated
2458 with DECL. */
2459 if (TREE_PUBLIC (decl)
2460 && !DECL_REALLY_EXTERN (decl)
2461 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2462 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2463 targetm.cxx.determine_class_data_visibility (decl);
2465 else if (template_decl)
2466 /* Template instantiations and specializations get visibility based
2467 on their template unless they override it with an attribute. */;
2468 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2470 if (determine_hidden_inline (decl))
2471 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2472 else
2474 /* Set default visibility to whatever the user supplied with
2475 #pragma GCC visibility or a namespace visibility attribute. */
2476 DECL_VISIBILITY (decl) = default_visibility;
2477 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2482 if (template_decl)
2484 /* If the specialization doesn't specify visibility, use the
2485 visibility from the template. */
2486 tree tinfo = get_template_info (template_decl);
2487 tree args = TI_ARGS (tinfo);
2488 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2489 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2490 : DECL_ATTRIBUTES (decl));
2492 if (args != error_mark_node)
2494 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2496 if (!DECL_VISIBILITY_SPECIFIED (decl))
2498 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2499 && determine_hidden_inline (decl))
2500 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2501 else
2503 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2504 DECL_VISIBILITY_SPECIFIED (decl)
2505 = DECL_VISIBILITY_SPECIFIED (pattern);
2509 if (args
2510 /* Template argument visibility outweighs #pragma or namespace
2511 visibility, but not an explicit attribute. */
2512 && !lookup_attribute ("visibility", attribs))
2514 int depth = TMPL_ARGS_DEPTH (args);
2515 if (DECL_VISIBILITY_SPECIFIED (decl))
2517 /* A class template member with explicit visibility
2518 overrides the class visibility, so we need to apply
2519 all the levels of template args directly. */
2520 int i;
2521 for (i = 1; i <= depth; ++i)
2523 tree lev = TMPL_ARGS_LEVEL (args, i);
2524 constrain_visibility_for_template (decl, lev);
2527 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2528 /* Limit visibility based on its template arguments. */
2529 constrain_visibility_for_template (decl, args);
2534 if (class_type)
2535 determine_visibility_from_class (decl, class_type);
2537 if (decl_anon_ns_mem_p (decl))
2538 /* Names in an anonymous namespace get internal linkage.
2539 This might change once we implement export. */
2540 constrain_visibility (decl, VISIBILITY_ANON, false);
2541 else if (TREE_CODE (decl) != TYPE_DECL)
2543 /* Propagate anonymity from type to decl. */
2544 int tvis = type_visibility (TREE_TYPE (decl));
2545 if (tvis == VISIBILITY_ANON
2546 || ! DECL_VISIBILITY_SPECIFIED (decl))
2547 constrain_visibility (decl, tvis, false);
2549 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2550 /* DR 757: A type without linkage shall not be used as the type of a
2551 variable or function with linkage, unless
2552 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2553 o the variable or function is not used (3.2 [basic.def.odr]) or is
2554 defined in the same translation unit.
2556 Since non-extern "C" decls need to be defined in the same
2557 translation unit, we can make the type internal. */
2558 constrain_visibility (decl, VISIBILITY_ANON, false);
2560 /* If visibility changed and DECL already has DECL_RTL, ensure
2561 symbol flags are updated. */
2562 if ((DECL_VISIBILITY (decl) != orig_visibility
2563 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2564 && ((VAR_P (decl) && TREE_STATIC (decl))
2565 || TREE_CODE (decl) == FUNCTION_DECL)
2566 && DECL_RTL_SET_P (decl))
2567 make_decl_rtl (decl);
2570 /* By default, static data members and function members receive
2571 the visibility of their containing class. */
2573 static void
2574 determine_visibility_from_class (tree decl, tree class_type)
2576 if (DECL_VISIBILITY_SPECIFIED (decl))
2577 return;
2579 if (determine_hidden_inline (decl))
2580 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2581 else
2583 /* Default to the class visibility. */
2584 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2585 DECL_VISIBILITY_SPECIFIED (decl)
2586 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2589 /* Give the target a chance to override the visibility associated
2590 with DECL. */
2591 if (VAR_P (decl)
2592 && (DECL_TINFO_P (decl)
2593 || (DECL_VTABLE_OR_VTT_P (decl)
2594 /* Construction virtual tables are not exported because
2595 they cannot be referred to from other object files;
2596 their name is not standardized by the ABI. */
2597 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2598 && TREE_PUBLIC (decl)
2599 && !DECL_REALLY_EXTERN (decl)
2600 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2601 targetm.cxx.determine_class_data_visibility (decl);
2604 /* Returns true iff DECL is an inline that should get hidden visibility
2605 because of -fvisibility-inlines-hidden. */
2607 static bool
2608 determine_hidden_inline (tree decl)
2610 return (visibility_options.inlines_hidden
2611 /* Don't do this for inline templates; specializations might not be
2612 inline, and we don't want them to inherit the hidden
2613 visibility. We'll set it here for all inline instantiations. */
2614 && !processing_template_decl
2615 && TREE_CODE (decl) == FUNCTION_DECL
2616 && DECL_DECLARED_INLINE_P (decl)
2617 && (! DECL_LANG_SPECIFIC (decl)
2618 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2621 /* Constrain the visibility of a class TYPE based on the visibility of its
2622 field types. Warn if any fields require lesser visibility. */
2624 void
2625 constrain_class_visibility (tree type)
2627 tree binfo;
2628 tree t;
2629 int i;
2631 int vis = type_visibility (type);
2633 if (vis == VISIBILITY_ANON
2634 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2635 return;
2637 /* Don't warn about visibility if the class has explicit visibility. */
2638 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2639 vis = VISIBILITY_INTERNAL;
2641 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2642 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node
2643 && !DECL_ARTIFICIAL (t))
2645 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2646 int subvis = type_visibility (ftype);
2648 if (subvis == VISIBILITY_ANON)
2650 if (!in_main_input_context())
2652 tree nlt = no_linkage_check (ftype, /*relaxed_p=*/false);
2653 if (nlt)
2655 if (same_type_p (TREE_TYPE (t), nlt))
2656 warning (OPT_Wsubobject_linkage, "\
2657 %qT has a field %qD whose type has no linkage",
2658 type, t);
2659 else
2660 warning (OPT_Wsubobject_linkage, "\
2661 %qT has a field %qD whose type depends on the type %qT which has no linkage",
2662 type, t, nlt);
2664 else
2665 warning (OPT_Wsubobject_linkage, "\
2666 %qT has a field %qD whose type uses the anonymous namespace",
2667 type, t);
2670 else if (MAYBE_CLASS_TYPE_P (ftype)
2671 && vis < VISIBILITY_HIDDEN
2672 && subvis >= VISIBILITY_HIDDEN)
2673 warning (OPT_Wattributes, "\
2674 %qT declared with greater visibility than the type of its field %qD",
2675 type, t);
2678 binfo = TYPE_BINFO (type);
2679 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2681 int subvis = type_visibility (TREE_TYPE (t));
2683 if (subvis == VISIBILITY_ANON)
2685 if (!in_main_input_context())
2687 tree nlt = no_linkage_check (TREE_TYPE (t), /*relaxed_p=*/false);
2688 if (nlt)
2690 if (same_type_p (TREE_TYPE (t), nlt))
2691 warning (OPT_Wsubobject_linkage, "\
2692 %qT has a base %qT whose type has no linkage",
2693 type, TREE_TYPE (t));
2694 else
2695 warning (OPT_Wsubobject_linkage, "\
2696 %qT has a base %qT whose type depends on the type %qT which has no linkage",
2697 type, TREE_TYPE (t), nlt);
2699 else
2700 warning (OPT_Wsubobject_linkage, "\
2701 %qT has a base %qT whose type uses the anonymous namespace",
2702 type, TREE_TYPE (t));
2705 else if (vis < VISIBILITY_HIDDEN
2706 && subvis >= VISIBILITY_HIDDEN)
2707 warning (OPT_Wattributes, "\
2708 %qT declared with greater visibility than its base %qT",
2709 type, TREE_TYPE (t));
2713 /* Functions for adjusting the visibility of a tagged type and its nested
2714 types and declarations when it gets a name for linkage purposes from a
2715 typedef. */
2717 static void bt_reset_linkage_1 (binding_entry, void *);
2718 static void bt_reset_linkage_2 (binding_entry, void *);
2720 /* First reset the visibility of all the types. */
2722 static void
2723 reset_type_linkage_1 (tree type)
2725 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2726 if (CLASS_TYPE_P (type))
2727 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2728 bt_reset_linkage_1, NULL);
2730 static void
2731 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2733 reset_type_linkage_1 (b->type);
2736 /* Then reset the visibility of any static data members or member
2737 functions that use those types. */
2739 static void
2740 reset_decl_linkage (tree decl)
2742 if (TREE_PUBLIC (decl))
2743 return;
2744 if (DECL_CLONED_FUNCTION_P (decl))
2745 return;
2746 TREE_PUBLIC (decl) = true;
2747 DECL_INTERFACE_KNOWN (decl) = false;
2748 determine_visibility (decl);
2749 tentative_decl_linkage (decl);
2752 static void
2753 reset_type_linkage_2 (tree type)
2755 if (CLASS_TYPE_P (type))
2757 if (tree vt = CLASSTYPE_VTABLES (type))
2759 tree name = mangle_vtbl_for_type (type);
2760 DECL_NAME (vt) = name;
2761 SET_DECL_ASSEMBLER_NAME (vt, name);
2762 reset_decl_linkage (vt);
2764 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2766 tree name = mangle_typeinfo_for_type (type);
2767 DECL_NAME (ti) = name;
2768 SET_DECL_ASSEMBLER_NAME (ti, name);
2769 TREE_TYPE (name) = type;
2770 reset_decl_linkage (ti);
2772 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2774 tree mem = STRIP_TEMPLATE (m);
2775 if (TREE_CODE (mem) == VAR_DECL || TREE_CODE (mem) == FUNCTION_DECL)
2776 reset_decl_linkage (mem);
2778 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2779 bt_reset_linkage_2, NULL);
2783 static void
2784 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2786 reset_type_linkage_2 (b->type);
2788 void
2789 reset_type_linkage (tree type)
2791 reset_type_linkage_1 (type);
2792 reset_type_linkage_2 (type);
2795 /* Set up our initial idea of what the linkage of DECL should be. */
2797 void
2798 tentative_decl_linkage (tree decl)
2800 if (DECL_INTERFACE_KNOWN (decl))
2801 /* We've already made a decision as to how this function will
2802 be handled. */;
2803 else if (vague_linkage_p (decl))
2805 if (TREE_CODE (decl) == FUNCTION_DECL
2806 && decl_defined_p (decl))
2808 DECL_EXTERNAL (decl) = 1;
2809 DECL_NOT_REALLY_EXTERN (decl) = 1;
2810 note_vague_linkage_fn (decl);
2811 /* A non-template inline function with external linkage will
2812 always be COMDAT. As we must eventually determine the
2813 linkage of all functions, and as that causes writes to
2814 the data mapped in from the PCH file, it's advantageous
2815 to mark the functions at this point. */
2816 if (DECL_DECLARED_INLINE_P (decl)
2817 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2818 || DECL_DEFAULTED_FN (decl)))
2820 /* This function must have external linkage, as
2821 otherwise DECL_INTERFACE_KNOWN would have been
2822 set. */
2823 gcc_assert (TREE_PUBLIC (decl));
2824 comdat_linkage (decl);
2825 DECL_INTERFACE_KNOWN (decl) = 1;
2828 else if (VAR_P (decl))
2829 maybe_commonize_var (decl);
2833 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2834 for DECL has not already been determined, do so now by setting
2835 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2836 function is called entities with vague linkage whose definitions
2837 are available must have TREE_PUBLIC set.
2839 If this function decides to place DECL in COMDAT, it will set
2840 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2841 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2842 callers defer that decision until it is clear that DECL is actually
2843 required. */
2845 void
2846 import_export_decl (tree decl)
2848 int emit_p;
2849 bool comdat_p;
2850 bool import_p;
2851 tree class_type = NULL_TREE;
2853 if (DECL_INTERFACE_KNOWN (decl))
2854 return;
2856 /* We cannot determine what linkage to give to an entity with vague
2857 linkage until the end of the file. For example, a virtual table
2858 for a class will be defined if and only if the key method is
2859 defined in this translation unit. As a further example, consider
2860 that when compiling a translation unit that uses PCH file with
2861 "-frepo" it would be incorrect to make decisions about what
2862 entities to emit when building the PCH; those decisions must be
2863 delayed until the repository information has been processed. */
2864 gcc_assert (at_eof);
2865 /* Object file linkage for explicit instantiations is handled in
2866 mark_decl_instantiated. For static variables in functions with
2867 vague linkage, maybe_commonize_var is used.
2869 Therefore, the only declarations that should be provided to this
2870 function are those with external linkage that are:
2872 * implicit instantiations of function templates
2874 * inline function
2876 * implicit instantiations of static data members of class
2877 templates
2879 * virtual tables
2881 * typeinfo objects
2883 Furthermore, all entities that reach this point must have a
2884 definition available in this translation unit.
2886 The following assertions check these conditions. */
2887 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2888 /* Any code that creates entities with TREE_PUBLIC cleared should
2889 also set DECL_INTERFACE_KNOWN. */
2890 gcc_assert (TREE_PUBLIC (decl));
2891 if (TREE_CODE (decl) == FUNCTION_DECL)
2892 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2893 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2894 || DECL_DECLARED_INLINE_P (decl));
2895 else
2896 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2897 || DECL_VTABLE_OR_VTT_P (decl)
2898 || DECL_TINFO_P (decl));
2899 /* Check that a definition of DECL is available in this translation
2900 unit. */
2901 gcc_assert (!DECL_REALLY_EXTERN (decl));
2903 /* Assume that DECL will not have COMDAT linkage. */
2904 comdat_p = false;
2905 /* Assume that DECL will not be imported into this translation
2906 unit. */
2907 import_p = false;
2909 /* See if the repository tells us whether or not to emit DECL in
2910 this translation unit. */
2911 emit_p = repo_emit_p (decl);
2912 if (emit_p == 0)
2913 import_p = true;
2914 else if (emit_p == 1)
2916 /* The repository indicates that this entity should be defined
2917 here. Make sure the back end honors that request. */
2918 mark_needed (decl);
2919 /* Output the definition as an ordinary strong definition. */
2920 DECL_EXTERNAL (decl) = 0;
2921 DECL_INTERFACE_KNOWN (decl) = 1;
2922 return;
2925 if (import_p)
2926 /* We have already decided what to do with this DECL; there is no
2927 need to check anything further. */
2929 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2931 class_type = DECL_CONTEXT (decl);
2932 import_export_class (class_type);
2933 if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2934 && CLASSTYPE_INTERFACE_ONLY (class_type))
2935 import_p = true;
2936 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2937 && !CLASSTYPE_USE_TEMPLATE (class_type)
2938 && CLASSTYPE_KEY_METHOD (class_type)
2939 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2940 /* The ABI requires that all virtual tables be emitted with
2941 COMDAT linkage. However, on systems where COMDAT symbols
2942 don't show up in the table of contents for a static
2943 archive, or on systems without weak symbols (where we
2944 approximate COMDAT linkage by using internal linkage), the
2945 linker will report errors about undefined symbols because
2946 it will not see the virtual table definition. Therefore,
2947 in the case that we know that the virtual table will be
2948 emitted in only one translation unit, we make the virtual
2949 table an ordinary definition with external linkage. */
2950 DECL_EXTERNAL (decl) = 0;
2951 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2953 /* CLASS_TYPE is being exported from this translation unit,
2954 so DECL should be defined here. */
2955 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2956 /* If a class is declared in a header with the "extern
2957 template" extension, then it will not be instantiated,
2958 even in translation units that would normally require
2959 it. Often such classes are explicitly instantiated in
2960 one translation unit. Therefore, the explicit
2961 instantiation must be made visible to other translation
2962 units. */
2963 DECL_EXTERNAL (decl) = 0;
2964 else
2966 /* The generic C++ ABI says that class data is always
2967 COMDAT, even if there is a key function. Some
2968 variants (e.g., the ARM EABI) says that class data
2969 only has COMDAT linkage if the class data might be
2970 emitted in more than one translation unit. When the
2971 key method can be inline and is inline, we still have
2972 to arrange for comdat even though
2973 class_data_always_comdat is false. */
2974 if (!CLASSTYPE_KEY_METHOD (class_type)
2975 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2976 || targetm.cxx.class_data_always_comdat ())
2978 /* The ABI requires COMDAT linkage. Normally, we
2979 only emit COMDAT things when they are needed;
2980 make sure that we realize that this entity is
2981 indeed needed. */
2982 comdat_p = true;
2983 mark_needed (decl);
2987 else if (!flag_implicit_templates
2988 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2989 import_p = true;
2990 else
2991 comdat_p = true;
2993 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2995 tree type = TREE_TYPE (DECL_NAME (decl));
2996 if (CLASS_TYPE_P (type))
2998 class_type = type;
2999 import_export_class (type);
3000 if (CLASSTYPE_INTERFACE_KNOWN (type)
3001 && TYPE_POLYMORPHIC_P (type)
3002 && CLASSTYPE_INTERFACE_ONLY (type)
3003 /* If -fno-rtti was specified, then we cannot be sure
3004 that RTTI information will be emitted with the
3005 virtual table of the class, so we must emit it
3006 wherever it is used. */
3007 && flag_rtti)
3008 import_p = true;
3009 else
3011 if (CLASSTYPE_INTERFACE_KNOWN (type)
3012 && !CLASSTYPE_INTERFACE_ONLY (type))
3014 comdat_p = (targetm.cxx.class_data_always_comdat ()
3015 || (CLASSTYPE_KEY_METHOD (type)
3016 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
3017 mark_needed (decl);
3018 if (!flag_weak)
3020 comdat_p = false;
3021 DECL_EXTERNAL (decl) = 0;
3024 else
3025 comdat_p = true;
3028 else
3029 comdat_p = true;
3031 else if (DECL_TEMPLOID_INSTANTIATION (decl))
3033 /* DECL is an implicit instantiation of a function or static
3034 data member. */
3035 if ((flag_implicit_templates
3036 && !flag_use_repository)
3037 || (flag_implicit_inline_templates
3038 && TREE_CODE (decl) == FUNCTION_DECL
3039 && DECL_DECLARED_INLINE_P (decl)))
3040 comdat_p = true;
3041 else
3042 /* If we are not implicitly generating templates, then mark
3043 this entity as undefined in this translation unit. */
3044 import_p = true;
3046 else if (DECL_FUNCTION_MEMBER_P (decl))
3048 if (!DECL_DECLARED_INLINE_P (decl))
3050 tree ctype = DECL_CONTEXT (decl);
3051 import_export_class (ctype);
3052 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
3054 DECL_NOT_REALLY_EXTERN (decl)
3055 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
3056 || (DECL_DECLARED_INLINE_P (decl)
3057 && ! flag_implement_inlines
3058 && !DECL_VINDEX (decl)));
3060 if (!DECL_NOT_REALLY_EXTERN (decl))
3061 DECL_EXTERNAL (decl) = 1;
3063 /* Always make artificials weak. */
3064 if (DECL_ARTIFICIAL (decl) && flag_weak)
3065 comdat_p = true;
3066 else
3067 maybe_make_one_only (decl);
3070 else
3071 comdat_p = true;
3073 else
3074 comdat_p = true;
3076 if (import_p)
3078 /* If we are importing DECL into this translation unit, mark is
3079 an undefined here. */
3080 DECL_EXTERNAL (decl) = 1;
3081 DECL_NOT_REALLY_EXTERN (decl) = 0;
3083 else if (comdat_p)
3085 /* If we decided to put DECL in COMDAT, mark it accordingly at
3086 this point. */
3087 comdat_linkage (decl);
3090 DECL_INTERFACE_KNOWN (decl) = 1;
3093 /* Return an expression that performs the destruction of DECL, which
3094 must be a VAR_DECL whose type has a non-trivial destructor, or is
3095 an array whose (innermost) elements have a non-trivial destructor. */
3097 tree
3098 build_cleanup (tree decl)
3100 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
3101 gcc_assert (clean != NULL_TREE);
3102 return clean;
3105 /* Returns the initialization guard variable for the variable DECL,
3106 which has static storage duration. */
3108 tree
3109 get_guard (tree decl)
3111 tree sname;
3112 tree guard;
3114 sname = mangle_guard_variable (decl);
3115 guard = get_global_binding (sname);
3116 if (! guard)
3118 tree guard_type;
3120 /* We use a type that is big enough to contain a mutex as well
3121 as an integer counter. */
3122 guard_type = targetm.cxx.guard_type ();
3123 guard = build_decl (DECL_SOURCE_LOCATION (decl),
3124 VAR_DECL, sname, guard_type);
3126 /* The guard should have the same linkage as what it guards. */
3127 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
3128 TREE_STATIC (guard) = TREE_STATIC (decl);
3129 DECL_COMMON (guard) = DECL_COMMON (decl);
3130 DECL_COMDAT (guard) = DECL_COMDAT (decl);
3131 CP_DECL_THREAD_LOCAL_P (guard) = CP_DECL_THREAD_LOCAL_P (decl);
3132 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
3133 if (DECL_ONE_ONLY (decl))
3134 make_decl_one_only (guard, cxx_comdat_group (guard));
3135 if (TREE_PUBLIC (decl))
3136 DECL_WEAK (guard) = DECL_WEAK (decl);
3137 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
3138 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
3140 DECL_ARTIFICIAL (guard) = 1;
3141 DECL_IGNORED_P (guard) = 1;
3142 TREE_USED (guard) = 1;
3143 pushdecl_top_level_and_finish (guard, NULL_TREE);
3145 return guard;
3148 /* Return an atomic load of src with the appropriate memory model. */
3150 static tree
3151 build_atomic_load_byte (tree src, HOST_WIDE_INT model)
3153 tree ptr_type = build_pointer_type (char_type_node);
3154 tree mem_model = build_int_cst (integer_type_node, model);
3155 tree t, addr, val;
3156 unsigned int size;
3157 int fncode;
3159 size = tree_to_uhwi (TYPE_SIZE_UNIT (char_type_node));
3161 fncode = BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1;
3162 t = builtin_decl_implicit ((enum built_in_function) fncode);
3164 addr = build1 (ADDR_EXPR, ptr_type, src);
3165 val = build_call_expr (t, 2, addr, mem_model);
3166 return val;
3169 /* Return those bits of the GUARD variable that should be set when the
3170 guarded entity is actually initialized. */
3172 static tree
3173 get_guard_bits (tree guard)
3175 if (!targetm.cxx.guard_mask_bit ())
3177 /* We only set the first byte of the guard, in order to leave room
3178 for a mutex in the high-order bits. */
3179 guard = build1 (ADDR_EXPR,
3180 build_pointer_type (TREE_TYPE (guard)),
3181 guard);
3182 guard = build1 (NOP_EXPR,
3183 build_pointer_type (char_type_node),
3184 guard);
3185 guard = build1 (INDIRECT_REF, char_type_node, guard);
3188 return guard;
3191 /* Return an expression which determines whether or not the GUARD
3192 variable has already been initialized. */
3194 tree
3195 get_guard_cond (tree guard, bool thread_safe)
3197 tree guard_value;
3199 if (!thread_safe)
3200 guard = get_guard_bits (guard);
3201 else
3202 guard = build_atomic_load_byte (guard, MEMMODEL_ACQUIRE);
3204 /* Mask off all but the low bit. */
3205 if (targetm.cxx.guard_mask_bit ())
3207 guard_value = integer_one_node;
3208 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3209 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3210 guard = cp_build_binary_op (input_location,
3211 BIT_AND_EXPR, guard, guard_value,
3212 tf_warning_or_error);
3215 guard_value = integer_zero_node;
3216 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3217 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3218 return cp_build_binary_op (input_location,
3219 EQ_EXPR, guard, guard_value,
3220 tf_warning_or_error);
3223 /* Return an expression which sets the GUARD variable, indicating that
3224 the variable being guarded has been initialized. */
3226 tree
3227 set_guard (tree guard)
3229 tree guard_init;
3231 /* Set the GUARD to one. */
3232 guard = get_guard_bits (guard);
3233 guard_init = integer_one_node;
3234 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3235 guard_init = fold_convert (TREE_TYPE (guard), guard_init);
3236 return cp_build_modify_expr (input_location, guard, NOP_EXPR, guard_init,
3237 tf_warning_or_error);
3240 /* Returns true iff we can tell that VAR does not have a dynamic
3241 initializer. */
3243 static bool
3244 var_defined_without_dynamic_init (tree var)
3246 /* If it's defined in another TU, we can't tell. */
3247 if (DECL_EXTERNAL (var))
3248 return false;
3249 /* If it has a non-trivial destructor, registering the destructor
3250 counts as dynamic initialization. */
3251 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3252 return false;
3253 /* If it's in this TU, its initializer has been processed, unless
3254 it's a case of self-initialization, then DECL_INITIALIZED_P is
3255 false while the initializer is handled by finish_id_expression. */
3256 if (!DECL_INITIALIZED_P (var))
3257 return false;
3258 /* If it has no initializer or a constant one, it's not dynamic. */
3259 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3260 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3263 /* Returns true iff VAR is a variable that needs uses to be
3264 wrapped for possible dynamic initialization. */
3266 static bool
3267 var_needs_tls_wrapper (tree var)
3269 return (!error_operand_p (var)
3270 && CP_DECL_THREAD_LOCAL_P (var)
3271 && !DECL_GNU_TLS_P (var)
3272 && !DECL_FUNCTION_SCOPE_P (var)
3273 && !var_defined_without_dynamic_init (var));
3276 /* Get the FUNCTION_DECL for the shared TLS init function for this
3277 translation unit. */
3279 static tree
3280 get_local_tls_init_fn (void)
3282 tree sname = get_identifier ("__tls_init");
3283 tree fn = get_global_binding (sname);
3284 if (!fn)
3286 fn = build_lang_decl (FUNCTION_DECL, sname,
3287 build_function_type (void_type_node,
3288 void_list_node));
3289 SET_DECL_LANGUAGE (fn, lang_c);
3290 TREE_PUBLIC (fn) = false;
3291 DECL_ARTIFICIAL (fn) = true;
3292 mark_used (fn);
3293 set_global_binding (fn);
3295 return fn;
3298 /* Get a FUNCTION_DECL for the init function for the thread_local
3299 variable VAR. The init function will be an alias to the function
3300 that initializes all the non-local TLS variables in the translation
3301 unit. The init function is only used by the wrapper function. */
3303 static tree
3304 get_tls_init_fn (tree var)
3306 /* Only C++11 TLS vars need this init fn. */
3307 if (!var_needs_tls_wrapper (var))
3308 return NULL_TREE;
3310 /* If -fno-extern-tls-init, assume that we don't need to call
3311 a tls init function for a variable defined in another TU. */
3312 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3313 return NULL_TREE;
3315 /* If the variable is internal, or if we can't generate aliases,
3316 call the local init function directly. */
3317 if (!TREE_PUBLIC (var) || !TARGET_SUPPORTS_ALIASES)
3318 return get_local_tls_init_fn ();
3320 tree sname = mangle_tls_init_fn (var);
3321 tree fn = get_global_binding (sname);
3322 if (!fn)
3324 fn = build_lang_decl (FUNCTION_DECL, sname,
3325 build_function_type (void_type_node,
3326 void_list_node));
3327 SET_DECL_LANGUAGE (fn, lang_c);
3328 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3329 DECL_ARTIFICIAL (fn) = true;
3330 DECL_COMDAT (fn) = DECL_COMDAT (var);
3331 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3332 if (DECL_ONE_ONLY (var))
3333 make_decl_one_only (fn, cxx_comdat_group (fn));
3334 if (TREE_PUBLIC (var))
3336 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3337 /* If the variable is defined somewhere else and might have static
3338 initialization, make the init function a weak reference. */
3339 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3340 || TYPE_HAS_CONSTEXPR_CTOR (obtype)
3341 || TYPE_HAS_TRIVIAL_DFLT (obtype))
3342 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3343 && DECL_EXTERNAL (var))
3344 declare_weak (fn);
3345 else
3346 DECL_WEAK (fn) = DECL_WEAK (var);
3348 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3349 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3350 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3351 DECL_IGNORED_P (fn) = 1;
3352 mark_used (fn);
3354 DECL_BEFRIENDING_CLASSES (fn) = var;
3356 set_global_binding (fn);
3358 return fn;
3361 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3362 variable VAR. The wrapper function calls the init function (if any) for
3363 VAR and then returns a reference to VAR. The wrapper function is used
3364 in place of VAR everywhere VAR is mentioned. */
3366 tree
3367 get_tls_wrapper_fn (tree var)
3369 /* Only C++11 TLS vars need this wrapper fn. */
3370 if (!var_needs_tls_wrapper (var))
3371 return NULL_TREE;
3373 tree sname = mangle_tls_wrapper_fn (var);
3374 tree fn = get_global_binding (sname);
3375 if (!fn)
3377 /* A named rvalue reference is an lvalue, so the wrapper should
3378 always return an lvalue reference. */
3379 tree type = non_reference (TREE_TYPE (var));
3380 type = build_reference_type (type);
3381 tree fntype = build_function_type (type, void_list_node);
3382 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3383 SET_DECL_LANGUAGE (fn, lang_c);
3384 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3385 DECL_ARTIFICIAL (fn) = true;
3386 DECL_IGNORED_P (fn) = 1;
3387 /* The wrapper is inline and emitted everywhere var is used. */
3388 DECL_DECLARED_INLINE_P (fn) = true;
3389 if (TREE_PUBLIC (var))
3391 comdat_linkage (fn);
3392 #ifdef HAVE_GAS_HIDDEN
3393 /* Make the wrapper bind locally; there's no reason to share
3394 the wrapper between multiple shared objects. */
3395 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3396 DECL_VISIBILITY_SPECIFIED (fn) = true;
3397 #endif
3399 if (!TREE_PUBLIC (fn))
3400 DECL_INTERFACE_KNOWN (fn) = true;
3401 mark_used (fn);
3402 note_vague_linkage_fn (fn);
3404 #if 0
3405 /* We want CSE to commonize calls to the wrapper, but marking it as
3406 pure is unsafe since it has side-effects. I guess we need a new
3407 ECF flag even weaker than ECF_PURE. FIXME! */
3408 DECL_PURE_P (fn) = true;
3409 #endif
3411 DECL_BEFRIENDING_CLASSES (fn) = var;
3413 set_global_binding (fn);
3415 return fn;
3418 /* At EOF, generate the definition for the TLS wrapper function FN:
3420 T& var_wrapper() {
3421 if (init_fn) init_fn();
3422 return var;
3423 } */
3425 static void
3426 generate_tls_wrapper (tree fn)
3428 tree var = DECL_BEFRIENDING_CLASSES (fn);
3430 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3431 tree body = begin_function_body ();
3432 /* Only call the init fn if there might be one. */
3433 if (tree init_fn = get_tls_init_fn (var))
3435 tree if_stmt = NULL_TREE;
3436 /* If init_fn is a weakref, make sure it exists before calling. */
3437 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3439 if_stmt = begin_if_stmt ();
3440 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3441 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3442 NE_EXPR, addr, nullptr_node,
3443 tf_warning_or_error);
3444 finish_if_stmt_cond (cond, if_stmt);
3446 finish_expr_stmt (build_cxx_call
3447 (init_fn, 0, NULL, tf_warning_or_error));
3448 if (if_stmt)
3450 finish_then_clause (if_stmt);
3451 finish_if_stmt (if_stmt);
3454 else
3455 /* If there's no initialization, the wrapper is a constant function. */
3456 TREE_READONLY (fn) = true;
3457 finish_return_stmt (convert_from_reference (var));
3458 finish_function_body (body);
3459 expand_or_defer_fn (finish_function (/*inline_p=*/false));
3462 /* Start the process of running a particular set of global constructors
3463 or destructors. Subroutine of do_[cd]tors. Also called from
3464 vtv_start_verification_constructor_init_function. */
3466 static tree
3467 start_objects (int method_type, int initp)
3469 tree body;
3470 tree fndecl;
3471 char type[14];
3473 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3475 if (initp != DEFAULT_INIT_PRIORITY)
3477 char joiner;
3479 #ifdef JOINER
3480 joiner = JOINER;
3481 #else
3482 joiner = '_';
3483 #endif
3485 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3487 else
3488 sprintf (type, "sub_%c", method_type);
3490 fndecl = build_lang_decl (FUNCTION_DECL,
3491 get_file_function_name (type),
3492 build_function_type_list (void_type_node,
3493 NULL_TREE));
3494 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3496 TREE_PUBLIC (current_function_decl) = 0;
3498 /* Mark as artificial because it's not explicitly in the user's
3499 source code. */
3500 DECL_ARTIFICIAL (current_function_decl) = 1;
3502 /* Mark this declaration as used to avoid spurious warnings. */
3503 TREE_USED (current_function_decl) = 1;
3505 /* Mark this function as a global constructor or destructor. */
3506 if (method_type == 'I')
3507 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3508 else
3509 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3511 body = begin_compound_stmt (BCS_FN_BODY);
3513 return body;
3516 /* Finish the process of running a particular set of global constructors
3517 or destructors. Subroutine of do_[cd]tors. */
3519 static void
3520 finish_objects (int method_type, int initp, tree body)
3522 tree fn;
3524 /* Finish up. */
3525 finish_compound_stmt (body);
3526 fn = finish_function (/*inline_p=*/false);
3528 if (method_type == 'I')
3530 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3531 decl_init_priority_insert (fn, initp);
3533 else
3535 DECL_STATIC_DESTRUCTOR (fn) = 1;
3536 decl_fini_priority_insert (fn, initp);
3539 expand_or_defer_fn (fn);
3542 /* The names of the parameters to the function created to handle
3543 initializations and destructions for objects with static storage
3544 duration. */
3545 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3546 #define PRIORITY_IDENTIFIER "__priority"
3548 /* The name of the function we create to handle initializations and
3549 destructions for objects with static storage duration. */
3550 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3552 /* The declaration for the __INITIALIZE_P argument. */
3553 static GTY(()) tree initialize_p_decl;
3555 /* The declaration for the __PRIORITY argument. */
3556 static GTY(()) tree priority_decl;
3558 /* The declaration for the static storage duration function. */
3559 static GTY(()) tree ssdf_decl;
3561 /* All the static storage duration functions created in this
3562 translation unit. */
3563 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3565 /* A map from priority levels to information about that priority
3566 level. There may be many such levels, so efficient lookup is
3567 important. */
3568 static splay_tree priority_info_map;
3570 /* Begins the generation of the function that will handle all
3571 initialization and destruction of objects with static storage
3572 duration. The function generated takes two parameters of type
3573 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3574 nonzero, it performs initializations. Otherwise, it performs
3575 destructions. It only performs those initializations or
3576 destructions with the indicated __PRIORITY. The generated function
3577 returns no value.
3579 It is assumed that this function will only be called once per
3580 translation unit. */
3582 static tree
3583 start_static_storage_duration_function (unsigned count)
3585 tree type;
3586 tree body;
3587 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3589 /* Create the identifier for this function. It will be of the form
3590 SSDF_IDENTIFIER_<number>. */
3591 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3593 type = build_function_type_list (void_type_node,
3594 integer_type_node, integer_type_node,
3595 NULL_TREE);
3597 /* Create the FUNCTION_DECL itself. */
3598 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3599 get_identifier (id),
3600 type);
3601 TREE_PUBLIC (ssdf_decl) = 0;
3602 DECL_ARTIFICIAL (ssdf_decl) = 1;
3604 /* Put this function in the list of functions to be called from the
3605 static constructors and destructors. */
3606 if (!ssdf_decls)
3608 vec_alloc (ssdf_decls, 32);
3610 /* Take this opportunity to initialize the map from priority
3611 numbers to information about that priority level. */
3612 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3613 /*delete_key_fn=*/0,
3614 /*delete_value_fn=*/
3615 (splay_tree_delete_value_fn)
3616 (void (*) (void)) free);
3618 /* We always need to generate functions for the
3619 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3620 priorities later, we'll be sure to find the
3621 DEFAULT_INIT_PRIORITY. */
3622 get_priority_info (DEFAULT_INIT_PRIORITY);
3625 vec_safe_push (ssdf_decls, ssdf_decl);
3627 /* Create the argument list. */
3628 initialize_p_decl = cp_build_parm_decl
3629 (ssdf_decl, get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3630 TREE_USED (initialize_p_decl) = 1;
3631 priority_decl = cp_build_parm_decl
3632 (ssdf_decl, get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3633 TREE_USED (priority_decl) = 1;
3635 DECL_CHAIN (initialize_p_decl) = priority_decl;
3636 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3638 /* Put the function in the global scope. */
3639 pushdecl (ssdf_decl);
3641 /* Start the function itself. This is equivalent to declaring the
3642 function as:
3644 static void __ssdf (int __initialize_p, init __priority_p);
3646 It is static because we only need to call this function from the
3647 various constructor and destructor functions for this module. */
3648 start_preparsed_function (ssdf_decl,
3649 /*attrs=*/NULL_TREE,
3650 SF_PRE_PARSED);
3652 /* Set up the scope of the outermost block in the function. */
3653 body = begin_compound_stmt (BCS_FN_BODY);
3655 return body;
3658 /* Finish the generation of the function which performs initialization
3659 and destruction of objects with static storage duration. After
3660 this point, no more such objects can be created. */
3662 static void
3663 finish_static_storage_duration_function (tree body)
3665 /* Close out the function. */
3666 finish_compound_stmt (body);
3667 expand_or_defer_fn (finish_function (/*inline_p=*/false));
3670 /* Return the information about the indicated PRIORITY level. If no
3671 code to handle this level has yet been generated, generate the
3672 appropriate prologue. */
3674 static priority_info
3675 get_priority_info (int priority)
3677 priority_info pi;
3678 splay_tree_node n;
3680 n = splay_tree_lookup (priority_info_map,
3681 (splay_tree_key) priority);
3682 if (!n)
3684 /* Create a new priority information structure, and insert it
3685 into the map. */
3686 pi = XNEW (struct priority_info_s);
3687 pi->initializations_p = 0;
3688 pi->destructions_p = 0;
3689 splay_tree_insert (priority_info_map,
3690 (splay_tree_key) priority,
3691 (splay_tree_value) pi);
3693 else
3694 pi = (priority_info) n->value;
3696 return pi;
3699 /* The effective initialization priority of a DECL. */
3701 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3702 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3703 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3705 /* Whether a DECL needs a guard to protect it against multiple
3706 initialization. */
3708 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3709 || DECL_ONE_ONLY (decl) \
3710 || DECL_WEAK (decl)))
3712 /* Called from one_static_initialization_or_destruction(),
3713 via walk_tree.
3714 Walks the initializer list of a global variable and looks for
3715 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3716 and that have their DECL_CONTEXT() == NULL.
3717 For each such temporary variable, set their DECL_CONTEXT() to
3718 the current function. This is necessary because otherwise
3719 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3720 when trying to refer to a temporary variable that does not have
3721 it's DECL_CONTECT() properly set. */
3722 static tree
3723 fix_temporary_vars_context_r (tree *node,
3724 int * /*unused*/,
3725 void * /*unused1*/)
3727 gcc_assert (current_function_decl);
3729 if (TREE_CODE (*node) == BIND_EXPR)
3731 tree var;
3733 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3734 if (VAR_P (var)
3735 && !DECL_NAME (var)
3736 && DECL_ARTIFICIAL (var)
3737 && !DECL_CONTEXT (var))
3738 DECL_CONTEXT (var) = current_function_decl;
3741 return NULL_TREE;
3744 /* Set up to handle the initialization or destruction of DECL. If
3745 INITP is nonzero, we are initializing the variable. Otherwise, we
3746 are destroying it. */
3748 static void
3749 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3751 tree guard_if_stmt = NULL_TREE;
3752 tree guard;
3754 /* If we are supposed to destruct and there's a trivial destructor,
3755 nothing has to be done. */
3756 if (!initp
3757 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3758 return;
3760 /* Trick the compiler into thinking we are at the file and line
3761 where DECL was declared so that error-messages make sense, and so
3762 that the debugger will show somewhat sensible file and line
3763 information. */
3764 input_location = DECL_SOURCE_LOCATION (decl);
3766 /* Make sure temporary variables in the initialiser all have
3767 their DECL_CONTEXT() set to a value different from NULL_TREE.
3768 This can happen when global variables initializers are built.
3769 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3770 the temporary variables that might have been generated in the
3771 accompanying initializers is NULL_TREE, meaning the variables have been
3772 declared in the global namespace.
3773 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3774 of the temporaries are set to the current function decl. */
3775 cp_walk_tree_without_duplicates (&init,
3776 fix_temporary_vars_context_r,
3777 NULL);
3779 /* Because of:
3781 [class.access.spec]
3783 Access control for implicit calls to the constructors,
3784 the conversion functions, or the destructor called to
3785 create and destroy a static data member is performed as
3786 if these calls appeared in the scope of the member's
3787 class.
3789 we pretend we are in a static member function of the class of
3790 which the DECL is a member. */
3791 if (member_p (decl))
3793 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3794 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3797 /* Assume we don't need a guard. */
3798 guard = NULL_TREE;
3799 /* We need a guard if this is an object with external linkage that
3800 might be initialized in more than one place. (For example, a
3801 static data member of a template, when the data member requires
3802 construction.) */
3803 if (NEEDS_GUARD_P (decl))
3805 tree guard_cond;
3807 guard = get_guard (decl);
3809 /* When using __cxa_atexit, we just check the GUARD as we would
3810 for a local static. */
3811 if (flag_use_cxa_atexit)
3813 /* When using __cxa_atexit, we never try to destroy
3814 anything from a static destructor. */
3815 gcc_assert (initp);
3816 guard_cond = get_guard_cond (guard, false);
3818 /* If we don't have __cxa_atexit, then we will be running
3819 destructors from .fini sections, or their equivalents. So,
3820 we need to know how many times we've tried to initialize this
3821 object. We do initializations only if the GUARD is zero,
3822 i.e., if we are the first to initialize the variable. We do
3823 destructions only if the GUARD is one, i.e., if we are the
3824 last to destroy the variable. */
3825 else if (initp)
3826 guard_cond
3827 = cp_build_binary_op (input_location,
3828 EQ_EXPR,
3829 cp_build_unary_op (PREINCREMENT_EXPR,
3830 guard,
3831 /*noconvert=*/true,
3832 tf_warning_or_error),
3833 integer_one_node,
3834 tf_warning_or_error);
3835 else
3836 guard_cond
3837 = cp_build_binary_op (input_location,
3838 EQ_EXPR,
3839 cp_build_unary_op (PREDECREMENT_EXPR,
3840 guard,
3841 /*noconvert=*/true,
3842 tf_warning_or_error),
3843 integer_zero_node,
3844 tf_warning_or_error);
3846 guard_if_stmt = begin_if_stmt ();
3847 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3851 /* If we're using __cxa_atexit, we have not already set the GUARD,
3852 so we must do so now. */
3853 if (guard && initp && flag_use_cxa_atexit)
3854 finish_expr_stmt (set_guard (guard));
3856 /* Perform the initialization or destruction. */
3857 if (initp)
3859 if (init)
3861 finish_expr_stmt (init);
3862 if (sanitize_flags_p (SANITIZE_ADDRESS, decl))
3864 varpool_node *vnode = varpool_node::get (decl);
3865 if (vnode)
3866 vnode->dynamically_initialized = 1;
3870 /* If we're using __cxa_atexit, register a function that calls the
3871 destructor for the object. */
3872 if (flag_use_cxa_atexit)
3873 finish_expr_stmt (register_dtor_fn (decl));
3875 else
3876 finish_expr_stmt (build_cleanup (decl));
3878 /* Finish the guard if-stmt, if necessary. */
3879 if (guard)
3881 finish_then_clause (guard_if_stmt);
3882 finish_if_stmt (guard_if_stmt);
3885 /* Now that we're done with DECL we don't need to pretend to be a
3886 member of its class any longer. */
3887 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3888 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3891 /* Generate code to do the initialization or destruction of the decls in VARS,
3892 a TREE_LIST of VAR_DECL with static storage duration.
3893 Whether initialization or destruction is performed is specified by INITP. */
3895 static void
3896 do_static_initialization_or_destruction (tree vars, bool initp)
3898 tree node, init_if_stmt, cond;
3900 /* Build the outer if-stmt to check for initialization or destruction. */
3901 init_if_stmt = begin_if_stmt ();
3902 cond = initp ? integer_one_node : integer_zero_node;
3903 cond = cp_build_binary_op (input_location,
3904 EQ_EXPR,
3905 initialize_p_decl,
3906 cond,
3907 tf_warning_or_error);
3908 finish_if_stmt_cond (cond, init_if_stmt);
3910 /* To make sure dynamic construction doesn't access globals from other
3911 compilation units where they might not be yet constructed, for
3912 -fsanitize=address insert __asan_before_dynamic_init call that
3913 prevents access to either all global variables that need construction
3914 in other compilation units, or at least those that haven't been
3915 initialized yet. Variables that need dynamic construction in
3916 the current compilation unit are kept accessible. */
3917 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
3918 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3920 node = vars;
3921 do {
3922 tree decl = TREE_VALUE (node);
3923 tree priority_if_stmt;
3924 int priority;
3925 priority_info pi;
3927 /* If we don't need a destructor, there's nothing to do. Avoid
3928 creating a possibly empty if-stmt. */
3929 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3931 node = TREE_CHAIN (node);
3932 continue;
3935 /* Remember that we had an initialization or finalization at this
3936 priority. */
3937 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3938 pi = get_priority_info (priority);
3939 if (initp)
3940 pi->initializations_p = 1;
3941 else
3942 pi->destructions_p = 1;
3944 /* Conditionalize this initialization on being in the right priority
3945 and being initializing/finalizing appropriately. */
3946 priority_if_stmt = begin_if_stmt ();
3947 cond = cp_build_binary_op (input_location,
3948 EQ_EXPR,
3949 priority_decl,
3950 build_int_cst (NULL_TREE, priority),
3951 tf_warning_or_error);
3952 finish_if_stmt_cond (cond, priority_if_stmt);
3954 /* Process initializers with same priority. */
3955 for (; node
3956 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3957 node = TREE_CHAIN (node))
3958 /* Do one initialization or destruction. */
3959 one_static_initialization_or_destruction (TREE_VALUE (node),
3960 TREE_PURPOSE (node), initp);
3962 /* Finish up the priority if-stmt body. */
3963 finish_then_clause (priority_if_stmt);
3964 finish_if_stmt (priority_if_stmt);
3966 } while (node);
3968 /* Revert what __asan_before_dynamic_init did by calling
3969 __asan_after_dynamic_init. */
3970 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
3971 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3973 /* Finish up the init/destruct if-stmt body. */
3974 finish_then_clause (init_if_stmt);
3975 finish_if_stmt (init_if_stmt);
3978 /* VARS is a list of variables with static storage duration which may
3979 need initialization and/or finalization. Remove those variables
3980 that don't really need to be initialized or finalized, and return
3981 the resulting list. The order in which the variables appear in
3982 VARS is in reverse order of the order in which they should actually
3983 be initialized. The list we return is in the unreversed order;
3984 i.e., the first variable should be initialized first. */
3986 static tree
3987 prune_vars_needing_no_initialization (tree *vars)
3989 tree *var = vars;
3990 tree result = NULL_TREE;
3992 while (*var)
3994 tree t = *var;
3995 tree decl = TREE_VALUE (t);
3996 tree init = TREE_PURPOSE (t);
3998 /* Deal gracefully with error. */
3999 if (error_operand_p (decl))
4001 var = &TREE_CHAIN (t);
4002 continue;
4005 /* The only things that can be initialized are variables. */
4006 gcc_assert (VAR_P (decl));
4008 /* If this object is not defined, we don't need to do anything
4009 here. */
4010 if (DECL_EXTERNAL (decl))
4012 var = &TREE_CHAIN (t);
4013 continue;
4016 /* Also, if the initializer already contains errors, we can bail
4017 out now. */
4018 if (init && TREE_CODE (init) == TREE_LIST
4019 && value_member (error_mark_node, init))
4021 var = &TREE_CHAIN (t);
4022 continue;
4025 /* This variable is going to need initialization and/or
4026 finalization, so we add it to the list. */
4027 *var = TREE_CHAIN (t);
4028 TREE_CHAIN (t) = result;
4029 result = t;
4032 return result;
4035 /* Make sure we have told the back end about all the variables in
4036 VARS. */
4038 static void
4039 write_out_vars (tree vars)
4041 tree v;
4043 for (v = vars; v; v = TREE_CHAIN (v))
4045 tree var = TREE_VALUE (v);
4046 if (!var_finalized_p (var))
4048 import_export_decl (var);
4049 rest_of_decl_compilation (var, 1, 1);
4054 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
4055 (otherwise) that will initialize all global objects with static
4056 storage duration having the indicated PRIORITY. */
4058 static void
4059 generate_ctor_or_dtor_function (bool constructor_p, int priority,
4060 location_t *locus)
4062 char function_key;
4063 tree fndecl;
4064 tree body;
4065 size_t i;
4067 input_location = *locus;
4068 /* ??? */
4069 /* Was: locus->line++; */
4071 /* We use `I' to indicate initialization and `D' to indicate
4072 destruction. */
4073 function_key = constructor_p ? 'I' : 'D';
4075 /* We emit the function lazily, to avoid generating empty
4076 global constructors and destructors. */
4077 body = NULL_TREE;
4079 /* For Objective-C++, we may need to initialize metadata found in this module.
4080 This must be done _before_ any other static initializations. */
4081 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
4082 && constructor_p && objc_static_init_needed_p ())
4084 body = start_objects (function_key, priority);
4085 objc_generate_static_init_call (NULL_TREE);
4088 /* Call the static storage duration function with appropriate
4089 arguments. */
4090 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
4092 /* Calls to pure or const functions will expand to nothing. */
4093 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
4095 tree call;
4097 if (! body)
4098 body = start_objects (function_key, priority);
4100 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
4101 build_int_cst (NULL_TREE,
4102 constructor_p),
4103 build_int_cst (NULL_TREE,
4104 priority),
4105 NULL_TREE);
4106 finish_expr_stmt (call);
4110 /* Close out the function. */
4111 if (body)
4112 finish_objects (function_key, priority, body);
4115 /* Generate constructor and destructor functions for the priority
4116 indicated by N. */
4118 static int
4119 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
4121 location_t *locus = (location_t *) data;
4122 int priority = (int) n->key;
4123 priority_info pi = (priority_info) n->value;
4125 /* Generate the functions themselves, but only if they are really
4126 needed. */
4127 if (pi->initializations_p)
4128 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
4129 if (pi->destructions_p)
4130 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
4132 /* Keep iterating. */
4133 return 0;
4136 /* Return C++ property of T, based on given operation OP. */
4138 static int
4139 cpp_check (tree t, cpp_operation op)
4141 switch (op)
4143 case HAS_DEPENDENT_TEMPLATE_ARGS:
4145 tree ti = CLASSTYPE_TEMPLATE_INFO (t);
4146 if (!ti)
4147 return 0;
4148 ++processing_template_decl;
4149 const bool dep = any_dependent_template_arguments_p (TI_ARGS (ti));
4150 --processing_template_decl;
4151 return dep;
4153 case IS_ABSTRACT:
4154 return DECL_PURE_VIRTUAL_P (t);
4155 case IS_CONSTRUCTOR:
4156 return DECL_CONSTRUCTOR_P (t);
4157 case IS_DESTRUCTOR:
4158 return DECL_DESTRUCTOR_P (t);
4159 case IS_COPY_CONSTRUCTOR:
4160 return DECL_COPY_CONSTRUCTOR_P (t);
4161 case IS_MOVE_CONSTRUCTOR:
4162 return DECL_MOVE_CONSTRUCTOR_P (t);
4163 case IS_TEMPLATE:
4164 return TREE_CODE (t) == TEMPLATE_DECL;
4165 case IS_TRIVIAL:
4166 return trivial_type_p (t);
4167 default:
4168 return 0;
4172 /* Collect source file references recursively, starting from NAMESPC. */
4174 static void
4175 collect_source_refs (tree namespc)
4177 /* Iterate over names in this name space. */
4178 for (tree t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4179 if (DECL_IS_BUILTIN (t))
4181 else if (TREE_CODE (t) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (t))
4182 collect_source_refs (t);
4183 else
4184 collect_source_ref (DECL_SOURCE_FILE (t));
4187 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4188 starting from NAMESPC. */
4190 static void
4191 collect_ada_namespace (tree namespc, const char *source_file)
4193 tree decl = NAMESPACE_LEVEL (namespc)->names;
4195 /* Collect decls from this namespace. This will skip
4196 NAMESPACE_DECLs (both aliases and regular, it cannot tell). */
4197 collect_ada_nodes (decl, source_file);
4199 /* Now scan for namespace children, and dump them. */
4200 for (; decl; decl = TREE_CHAIN (decl))
4201 if (TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl))
4202 collect_ada_namespace (decl, source_file);
4205 /* Returns true iff there is a definition available for variable or
4206 function DECL. */
4208 bool
4209 decl_defined_p (tree decl)
4211 if (TREE_CODE (decl) == FUNCTION_DECL)
4212 return (DECL_INITIAL (decl) != NULL_TREE
4213 /* A pending instantiation of a friend temploid is defined. */
4214 || (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
4215 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4216 (DECL_TI_TEMPLATE (decl)))));
4217 else
4219 gcc_assert (VAR_P (decl));
4220 return !DECL_EXTERNAL (decl);
4224 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4226 [expr.const]
4228 An integral constant-expression can only involve ... const
4229 variables of integral or enumeration types initialized with
4230 constant expressions ...
4232 C++0x also allows constexpr variables and temporaries initialized
4233 with constant expressions. We handle the former here, but the latter
4234 are just folded away in cxx_eval_constant_expression.
4236 The standard does not require that the expression be non-volatile.
4237 G++ implements the proposed correction in DR 457. */
4239 bool
4240 decl_constant_var_p (tree decl)
4242 if (!decl_maybe_constant_var_p (decl))
4243 return false;
4245 /* We don't know if a template static data member is initialized with
4246 a constant expression until we instantiate its initializer. Even
4247 in the case of a constexpr variable, we can't treat it as a
4248 constant until its initializer is complete in case it's used in
4249 its own initializer. */
4250 maybe_instantiate_decl (decl);
4251 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4254 /* Returns true if DECL could be a symbolic constant variable, depending on
4255 its initializer. */
4257 bool
4258 decl_maybe_constant_var_p (tree decl)
4260 tree type = TREE_TYPE (decl);
4261 if (!VAR_P (decl))
4262 return false;
4263 if (DECL_DECLARED_CONSTEXPR_P (decl))
4264 return true;
4265 if (DECL_HAS_VALUE_EXPR_P (decl))
4266 /* A proxy isn't constant. */
4267 return false;
4268 if (TREE_CODE (type) == REFERENCE_TYPE)
4269 /* References can be constant. */;
4270 else if (CP_TYPE_CONST_NON_VOLATILE_P (type)
4271 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4272 /* And const integers. */;
4273 else
4274 return false;
4276 if (DECL_INITIAL (decl)
4277 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
4278 /* We know the initializer, and it isn't constant. */
4279 return false;
4280 else
4281 return true;
4284 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4285 called from grokfndecl and grokvardecl; in all modes it is called from
4286 cp_write_global_declarations. */
4288 void
4289 no_linkage_error (tree decl)
4291 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4292 /* In C++11 it's ok if the decl is defined. */
4293 return;
4294 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4295 if (t == NULL_TREE)
4296 /* The type that got us on no_linkage_decls must have gotten a name for
4297 linkage purposes. */;
4298 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4299 /* The type might end up having a typedef name for linkage purposes. */
4300 vec_safe_push (no_linkage_decls, decl);
4301 else if (TYPE_UNNAMED_P (t))
4303 bool d = false;
4304 if (cxx_dialect >= cxx11)
4305 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4306 "unnamed type, is used but never defined", decl);
4307 else if (DECL_EXTERN_C_P (decl))
4308 /* Allow this; it's pretty common in C. */;
4309 else if (VAR_P (decl))
4310 /* DRs 132, 319 and 389 seem to indicate types with
4311 no linkage can only be used to declare extern "C"
4312 entities. Since it's not always an error in the
4313 ISO C++ 90 Standard, we only issue a warning. */
4314 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "unnamed type "
4315 "with no linkage used to declare variable %q#D with "
4316 "linkage", decl);
4317 else
4318 d = permerror (DECL_SOURCE_LOCATION (decl), "unnamed type with no "
4319 "linkage used to declare function %q#D with linkage",
4320 decl);
4321 if (d && is_typedef_decl (TYPE_NAME (t)))
4322 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4323 "to the unqualified type, so it is not used for linkage",
4324 TYPE_NAME (t));
4326 else if (cxx_dialect >= cxx11)
4328 if (VAR_P (decl) || !DECL_PURE_VIRTUAL_P (decl))
4329 permerror (DECL_SOURCE_LOCATION (decl),
4330 "%q#D, declared using local type "
4331 "%qT, is used but never defined", decl, t);
4333 else if (VAR_P (decl))
4334 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4335 "used to declare variable %q#D with linkage", t, decl);
4336 else
4337 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4338 "to declare function %q#D with linkage", t, decl);
4341 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4343 static void
4344 collect_all_refs (const char *source_file)
4346 collect_ada_namespace (global_namespace, source_file);
4349 /* Clear DECL_EXTERNAL for NODE. */
4351 static bool
4352 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4354 DECL_EXTERNAL (node->decl) = 0;
4355 return false;
4358 /* Build up the function to run dynamic initializers for thread_local
4359 variables in this translation unit and alias the init functions for the
4360 individual variables to it. */
4362 static void
4363 handle_tls_init (void)
4365 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4366 if (vars == NULL_TREE)
4367 return;
4369 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4371 write_out_vars (vars);
4373 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4374 boolean_type_node);
4375 TREE_PUBLIC (guard) = false;
4376 TREE_STATIC (guard) = true;
4377 DECL_ARTIFICIAL (guard) = true;
4378 DECL_IGNORED_P (guard) = true;
4379 TREE_USED (guard) = true;
4380 CP_DECL_THREAD_LOCAL_P (guard) = true;
4381 set_decl_tls_model (guard, decl_default_tls_model (guard));
4382 pushdecl_top_level_and_finish (guard, NULL_TREE);
4384 tree fn = get_local_tls_init_fn ();
4385 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4386 tree body = begin_function_body ();
4387 tree if_stmt = begin_if_stmt ();
4388 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4389 tf_warning_or_error);
4390 finish_if_stmt_cond (cond, if_stmt);
4391 finish_expr_stmt (cp_build_modify_expr (loc, guard, NOP_EXPR,
4392 boolean_true_node,
4393 tf_warning_or_error));
4394 for (; vars; vars = TREE_CHAIN (vars))
4396 tree var = TREE_VALUE (vars);
4397 tree init = TREE_PURPOSE (vars);
4398 one_static_initialization_or_destruction (var, init, true);
4400 /* Output init aliases even with -fno-extern-tls-init. */
4401 if (TARGET_SUPPORTS_ALIASES && TREE_PUBLIC (var))
4403 tree single_init_fn = get_tls_init_fn (var);
4404 if (single_init_fn == NULL_TREE)
4405 continue;
4406 cgraph_node *alias
4407 = cgraph_node::get_create (fn)->create_same_body_alias
4408 (single_init_fn, fn);
4409 gcc_assert (alias != NULL);
4413 finish_then_clause (if_stmt);
4414 finish_if_stmt (if_stmt);
4415 finish_function_body (body);
4416 expand_or_defer_fn (finish_function (/*inline_p=*/false));
4419 /* We're at the end of compilation, so generate any mangling aliases that
4420 we've been saving up, if DECL is going to be output and ID2 isn't
4421 already taken by another declaration. */
4423 static void
4424 generate_mangling_alias (tree decl, tree id2)
4426 struct cgraph_node *n = NULL;
4428 if (TREE_CODE (decl) == FUNCTION_DECL)
4430 n = cgraph_node::get (decl);
4431 if (!n)
4432 /* Don't create an alias to an unreferenced function. */
4433 return;
4436 tree *slot
4437 = mangled_decls->find_slot_with_hash (id2, IDENTIFIER_HASH_VALUE (id2),
4438 INSERT);
4440 /* If there's a declaration already using this mangled name,
4441 don't create a compatibility alias that conflicts. */
4442 if (*slot)
4443 return;
4445 tree alias = make_alias_for (decl, id2);
4446 *slot = alias;
4448 DECL_IGNORED_P (alias) = 1;
4449 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
4450 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
4451 if (vague_linkage_p (decl))
4452 DECL_WEAK (alias) = 1;
4454 if (n)
4455 n->create_same_body_alias (alias, decl);
4456 else
4457 varpool_node::create_extra_name_alias (alias, decl);
4460 /* Note that we might want to emit an alias with the symbol ID2 for DECL at
4461 the end of translation, for compatibility across bugs in the mangling
4462 implementation. */
4464 void
4465 note_mangling_alias (tree decl, tree id2)
4467 if (TARGET_SUPPORTS_ALIASES)
4469 if (!defer_mangling_aliases)
4470 generate_mangling_alias (decl, id2);
4471 else
4473 vec_safe_push (mangling_aliases, decl);
4474 vec_safe_push (mangling_aliases, id2);
4479 /* Emit all mangling aliases that were deferred up to this point. */
4481 void
4482 generate_mangling_aliases ()
4484 while (!vec_safe_is_empty (mangling_aliases))
4486 tree id2 = mangling_aliases->pop();
4487 tree decl = mangling_aliases->pop();
4488 generate_mangling_alias (decl, id2);
4490 defer_mangling_aliases = false;
4493 /* Record a mangling of DECL, whose DECL_ASSEMBLER_NAME has just been
4494 set. NEED_WARNING is true if we must warn about collisions. We do
4495 this to spot changes in mangling that may require compatibility
4496 aliases. */
4498 void
4499 record_mangling (tree decl, bool need_warning)
4501 if (!mangled_decls)
4502 mangled_decls = hash_table<mangled_decl_hash>::create_ggc (499);
4504 gcc_checking_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
4505 tree id = DECL_ASSEMBLER_NAME_RAW (decl);
4506 tree *slot
4507 = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id),
4508 INSERT);
4510 /* If this is already an alias, remove the alias, because the real
4511 decl takes precedence. */
4512 if (*slot && DECL_ARTIFICIAL (*slot) && DECL_IGNORED_P (*slot))
4513 if (symtab_node *n = symtab_node::get (*slot))
4514 if (n->cpp_implicit_alias)
4516 n->remove ();
4517 *slot = NULL_TREE;
4520 if (!*slot)
4521 *slot = decl;
4522 else if (need_warning)
4524 error_at (DECL_SOURCE_LOCATION (decl),
4525 "mangling of %q#D as %qE conflicts with a previous mangle",
4526 decl, id);
4527 inform (DECL_SOURCE_LOCATION (*slot),
4528 "previous mangling %q#D", *slot);
4529 inform (DECL_SOURCE_LOCATION (decl),
4530 "a later -fabi-version= (or =0)"
4531 " avoids this error with a change in mangling");
4532 *slot = decl;
4536 /* The mangled name of DECL is being forcibly changed to NAME. Remove
4537 any existing knowledge of DECL's mangled name meaning DECL. */
4539 void
4540 overwrite_mangling (tree decl, tree name)
4542 if (tree id = DECL_ASSEMBLER_NAME_RAW (decl))
4543 if ((TREE_CODE (decl) == VAR_DECL
4544 || TREE_CODE (decl) == FUNCTION_DECL)
4545 && mangled_decls)
4546 if (tree *slot
4547 = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id),
4548 NO_INSERT))
4549 if (*slot == decl)
4551 mangled_decls->clear_slot (slot);
4553 /* If this is an alias, remove it from the symbol table. */
4554 if (DECL_ARTIFICIAL (decl) && DECL_IGNORED_P (decl))
4555 if (symtab_node *n = symtab_node::get (decl))
4556 if (n->cpp_implicit_alias)
4557 n->remove ();
4560 DECL_ASSEMBLER_NAME_RAW (decl) = name;
4563 /* The entire file is now complete. If requested, dump everything
4564 to a file. */
4566 static void
4567 dump_tu (void)
4569 dump_flags_t flags;
4570 if (FILE *stream = dump_begin (raw_dump_id, &flags))
4572 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4573 dump_end (raw_dump_id, stream);
4577 static location_t locus_at_end_of_parsing;
4579 /* Check the deallocation functions for CODE to see if we want to warn that
4580 only one was defined. */
4582 static void
4583 maybe_warn_sized_delete (enum tree_code code)
4585 tree sized = NULL_TREE;
4586 tree unsized = NULL_TREE;
4588 for (ovl_iterator iter (get_global_binding (ovl_op_identifier (false, code)));
4589 iter; ++iter)
4591 tree fn = *iter;
4592 /* We're only interested in usual deallocation functions. */
4593 if (!usual_deallocation_fn_p (fn))
4594 continue;
4595 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
4596 unsized = fn;
4597 else
4598 sized = fn;
4600 if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
4601 warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
4602 "the program should also define %qD", sized);
4603 else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
4604 warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
4605 "the program should also define %qD", unsized);
4608 /* Check the global deallocation functions to see if we want to warn about
4609 defining unsized without sized (or vice versa). */
4611 static void
4612 maybe_warn_sized_delete ()
4614 if (!flag_sized_deallocation || !warn_sized_deallocation)
4615 return;
4616 maybe_warn_sized_delete (DELETE_EXPR);
4617 maybe_warn_sized_delete (VEC_DELETE_EXPR);
4620 /* Earlier we left PTRMEM_CST in variable initializers alone so that we could
4621 look them up when evaluating non-type template parameters. Now we need to
4622 lower them to something the back end can understand. */
4624 static void
4625 lower_var_init ()
4627 varpool_node *node;
4628 FOR_EACH_VARIABLE (node)
4630 tree d = node->decl;
4631 if (tree init = DECL_INITIAL (d))
4632 DECL_INITIAL (d) = cplus_expand_constant (init);
4636 /* This routine is called at the end of compilation.
4637 Its job is to create all the code needed to initialize and
4638 destroy the global aggregates. We do the destruction
4639 first, since that way we only need to reverse the decls once. */
4641 void
4642 c_parse_final_cleanups (void)
4644 tree vars;
4645 bool reconsider;
4646 size_t i;
4647 unsigned ssdf_count = 0;
4648 int retries = 0;
4649 tree decl;
4651 locus_at_end_of_parsing = input_location;
4652 at_eof = 1;
4654 /* Bad parse errors. Just forget about it. */
4655 if (! global_bindings_p () || current_class_type
4656 || !vec_safe_is_empty (decl_namespace_list))
4657 return;
4659 /* This is the point to write out a PCH if we're doing that.
4660 In that case we do not want to do anything else. */
4661 if (pch_file)
4663 /* Mangle all symbols at PCH creation time. */
4664 symtab_node *node;
4665 FOR_EACH_SYMBOL (node)
4666 if (! is_a <varpool_node *> (node)
4667 || ! DECL_HARD_REGISTER (node->decl))
4668 DECL_ASSEMBLER_NAME (node->decl);
4669 c_common_write_pch ();
4670 dump_tu ();
4671 /* Ensure even the callers don't try to finalize the CU. */
4672 flag_syntax_only = 1;
4673 return;
4676 timevar_stop (TV_PHASE_PARSING);
4677 timevar_start (TV_PHASE_DEFERRED);
4679 symtab->process_same_body_aliases ();
4681 /* Handle -fdump-ada-spec[-slim] */
4682 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4684 if (flag_dump_ada_spec_slim)
4685 collect_source_ref (main_input_filename);
4686 else
4687 collect_source_refs (global_namespace);
4689 dump_ada_specs (collect_all_refs, cpp_check);
4692 /* FIXME - huh? was input_line -= 1;*/
4694 /* We now have to write out all the stuff we put off writing out.
4695 These include:
4697 o Template specializations that we have not yet instantiated,
4698 but which are needed.
4699 o Initialization and destruction for non-local objects with
4700 static storage duration. (Local objects with static storage
4701 duration are initialized when their scope is first entered,
4702 and are cleaned up via atexit.)
4703 o Virtual function tables.
4705 All of these may cause others to be needed. For example,
4706 instantiating one function may cause another to be needed, and
4707 generating the initializer for an object may cause templates to be
4708 instantiated, etc., etc. */
4710 emit_support_tinfos ();
4714 tree t;
4715 tree decl;
4717 reconsider = false;
4719 /* If there are templates that we've put off instantiating, do
4720 them now. */
4721 instantiate_pending_templates (retries);
4722 ggc_collect ();
4724 /* Write out virtual tables as required. Writing out the
4725 virtual table for a template class may cause the
4726 instantiation of members of that class. If we write out
4727 vtables then we remove the class from our list so we don't
4728 have to look at it again. */
4729 for (i = keyed_classes->length ();
4730 keyed_classes->iterate (--i, &t);)
4731 if (maybe_emit_vtables (t))
4733 reconsider = true;
4734 keyed_classes->unordered_remove (i);
4736 /* The input_location may have been changed during marking of
4737 vtable entries. */
4738 input_location = locus_at_end_of_parsing;
4740 /* Write out needed type info variables. We have to be careful
4741 looping through unemitted decls, because emit_tinfo_decl may
4742 cause other variables to be needed. New elements will be
4743 appended, and we remove from the vector those that actually
4744 get emitted. */
4745 for (i = unemitted_tinfo_decls->length ();
4746 unemitted_tinfo_decls->iterate (--i, &t);)
4747 if (emit_tinfo_decl (t))
4749 reconsider = true;
4750 unemitted_tinfo_decls->unordered_remove (i);
4753 /* The list of objects with static storage duration is built up
4754 in reverse order. We clear STATIC_AGGREGATES so that any new
4755 aggregates added during the initialization of these will be
4756 initialized in the correct order when we next come around the
4757 loop. */
4758 vars = prune_vars_needing_no_initialization (&static_aggregates);
4760 if (vars)
4762 /* We need to start a new initialization function each time
4763 through the loop. That's because we need to know which
4764 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4765 isn't computed until a function is finished, and written
4766 out. That's a deficiency in the back end. When this is
4767 fixed, these initialization functions could all become
4768 inline, with resulting performance improvements. */
4769 tree ssdf_body;
4771 /* Set the line and file, so that it is obviously not from
4772 the source file. */
4773 input_location = locus_at_end_of_parsing;
4774 ssdf_body = start_static_storage_duration_function (ssdf_count);
4776 /* Make sure the back end knows about all the variables. */
4777 write_out_vars (vars);
4779 /* First generate code to do all the initializations. */
4780 if (vars)
4781 do_static_initialization_or_destruction (vars, /*initp=*/true);
4783 /* Then, generate code to do all the destructions. Do these
4784 in reverse order so that the most recently constructed
4785 variable is the first destroyed. If we're using
4786 __cxa_atexit, then we don't need to do this; functions
4787 were registered at initialization time to destroy the
4788 local statics. */
4789 if (!flag_use_cxa_atexit && vars)
4791 vars = nreverse (vars);
4792 do_static_initialization_or_destruction (vars, /*initp=*/false);
4794 else
4795 vars = NULL_TREE;
4797 /* Finish up the static storage duration function for this
4798 round. */
4799 input_location = locus_at_end_of_parsing;
4800 finish_static_storage_duration_function (ssdf_body);
4802 /* All those initializations and finalizations might cause
4803 us to need more inline functions, more template
4804 instantiations, etc. */
4805 reconsider = true;
4806 ssdf_count++;
4807 /* ??? was: locus_at_end_of_parsing.line++; */
4810 /* Now do the same for thread_local variables. */
4811 handle_tls_init ();
4813 /* Go through the set of inline functions whose bodies have not
4814 been emitted yet. If out-of-line copies of these functions
4815 are required, emit them. */
4816 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4818 /* Does it need synthesizing? */
4819 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4820 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4822 /* Even though we're already at the top-level, we push
4823 there again. That way, when we pop back a few lines
4824 hence, all of our state is restored. Otherwise,
4825 finish_function doesn't clean things up, and we end
4826 up with CURRENT_FUNCTION_DECL set. */
4827 push_to_top_level ();
4828 /* The decl's location will mark where it was first
4829 needed. Save that so synthesize method can indicate
4830 where it was needed from, in case of error */
4831 input_location = DECL_SOURCE_LOCATION (decl);
4832 synthesize_method (decl);
4833 pop_from_top_level ();
4834 reconsider = true;
4837 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4838 generate_tls_wrapper (decl);
4840 if (!DECL_SAVED_TREE (decl))
4841 continue;
4843 cgraph_node *node = cgraph_node::get_create (decl);
4845 /* We lie to the back end, pretending that some functions
4846 are not defined when they really are. This keeps these
4847 functions from being put out unnecessarily. But, we must
4848 stop lying when the functions are referenced, or if they
4849 are not comdat since they need to be put out now. If
4850 DECL_INTERFACE_KNOWN, then we have already set
4851 DECL_EXTERNAL appropriately, so there's no need to check
4852 again, and we do not want to clear DECL_EXTERNAL if a
4853 previous call to import_export_decl set it.
4855 This is done in a separate for cycle, because if some
4856 deferred function is contained in another deferred
4857 function later in deferred_fns varray,
4858 rest_of_compilation would skip this function and we
4859 really cannot expand the same function twice. */
4860 import_export_decl (decl);
4861 if (DECL_NOT_REALLY_EXTERN (decl)
4862 && DECL_INITIAL (decl)
4863 && decl_needed_p (decl))
4865 if (node->cpp_implicit_alias)
4866 node = node->get_alias_target ();
4868 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
4869 NULL, true);
4870 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4871 group, we need to mark all symbols in the same comdat group
4872 that way. */
4873 if (node->same_comdat_group)
4874 for (cgraph_node *next
4875 = dyn_cast<cgraph_node *> (node->same_comdat_group);
4876 next != node;
4877 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
4878 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
4879 NULL, true);
4882 /* If we're going to need to write this function out, and
4883 there's already a body for it, create RTL for it now.
4884 (There might be no body if this is a method we haven't
4885 gotten around to synthesizing yet.) */
4886 if (!DECL_EXTERNAL (decl)
4887 && decl_needed_p (decl)
4888 && !TREE_ASM_WRITTEN (decl)
4889 && !node->definition)
4891 /* We will output the function; no longer consider it in this
4892 loop. */
4893 DECL_DEFER_OUTPUT (decl) = 0;
4894 /* Generate RTL for this function now that we know we
4895 need it. */
4896 expand_or_defer_fn (decl);
4897 /* If we're compiling -fsyntax-only pretend that this
4898 function has been written out so that we don't try to
4899 expand it again. */
4900 if (flag_syntax_only)
4901 TREE_ASM_WRITTEN (decl) = 1;
4902 reconsider = true;
4906 if (wrapup_namespace_globals ())
4907 reconsider = true;
4909 /* Static data members are just like namespace-scope globals. */
4910 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4912 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4913 /* Don't write it out if we haven't seen a definition. */
4914 || (DECL_IN_AGGR_P (decl) && !DECL_INLINE_VAR_P (decl)))
4915 continue;
4916 import_export_decl (decl);
4917 /* If this static data member is needed, provide it to the
4918 back end. */
4919 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4920 DECL_EXTERNAL (decl) = 0;
4922 if (vec_safe_length (pending_statics) != 0
4923 && wrapup_global_declarations (pending_statics->address (),
4924 pending_statics->length ()))
4925 reconsider = true;
4927 retries++;
4929 while (reconsider);
4931 lower_var_init ();
4933 generate_mangling_aliases ();
4935 /* All used inline functions must have a definition at this point. */
4936 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4938 if (/* Check online inline functions that were actually used. */
4939 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4940 /* If the definition actually was available here, then the
4941 fact that the function was not defined merely represents
4942 that for some reason (use of a template repository,
4943 #pragma interface, etc.) we decided not to emit the
4944 definition here. */
4945 && !DECL_INITIAL (decl)
4946 /* Don't complain if the template was defined. */
4947 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4948 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4949 (template_for_substitution (decl)))))
4951 warning_at (DECL_SOURCE_LOCATION (decl), 0,
4952 "inline function %qD used but never defined", decl);
4953 /* Avoid a duplicate warning from check_global_declaration. */
4954 TREE_NO_WARNING (decl) = 1;
4958 /* So must decls that use a type with no linkage. */
4959 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4960 no_linkage_error (decl);
4962 maybe_warn_sized_delete ();
4964 /* Then, do the Objective-C stuff. This is where all the
4965 Objective-C module stuff gets generated (symtab,
4966 class/protocol/selector lists etc). This must be done after C++
4967 templates, destructors etc. so that selectors used in C++
4968 templates are properly allocated. */
4969 if (c_dialect_objc ())
4970 objc_write_global_declarations ();
4972 /* We give C linkage to static constructors and destructors. */
4973 push_lang_context (lang_name_c);
4975 /* Generate initialization and destruction functions for all
4976 priorities for which they are required. */
4977 if (priority_info_map)
4978 splay_tree_foreach (priority_info_map,
4979 generate_ctor_and_dtor_functions_for_priority,
4980 /*data=*/&locus_at_end_of_parsing);
4981 else if (c_dialect_objc () && objc_static_init_needed_p ())
4982 /* If this is obj-c++ and we need a static init, call
4983 generate_ctor_or_dtor_function. */
4984 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4985 DEFAULT_INIT_PRIORITY,
4986 &locus_at_end_of_parsing);
4988 /* We're done with the splay-tree now. */
4989 if (priority_info_map)
4990 splay_tree_delete (priority_info_map);
4992 /* Generate any missing aliases. */
4993 maybe_apply_pending_pragma_weaks ();
4995 /* We're done with static constructors, so we can go back to "C++"
4996 linkage now. */
4997 pop_lang_context ();
4999 if (flag_vtable_verify)
5001 vtv_recover_class_info ();
5002 vtv_compute_class_hierarchy_transitive_closure ();
5003 vtv_build_vtable_verify_fndecl ();
5006 perform_deferred_noexcept_checks ();
5008 finish_repo ();
5009 fini_constexpr ();
5011 /* The entire file is now complete. If requested, dump everything
5012 to a file. */
5013 dump_tu ();
5015 if (flag_detailed_statistics)
5017 dump_tree_statistics ();
5018 dump_time_statistics ();
5021 timevar_stop (TV_PHASE_DEFERRED);
5022 timevar_start (TV_PHASE_PARSING);
5024 /* Indicate that we're done with front end processing. */
5025 at_eof = 2;
5028 /* Perform any post compilation-proper cleanups for the C++ front-end.
5029 This should really go away. No front-end should need to do
5030 anything past the compilation process. */
5032 void
5033 cxx_post_compilation_parsing_cleanups (void)
5035 timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS);
5037 if (flag_vtable_verify)
5039 /* Generate the special constructor initialization function that
5040 calls __VLTRegisterPairs, and give it a very high
5041 initialization priority. This must be done after
5042 finalize_compilation_unit so that we have accurate
5043 information about which vtable will actually be emitted. */
5044 vtv_generate_init_routine ();
5047 input_location = locus_at_end_of_parsing;
5049 if (flag_checking)
5050 validate_conversion_obstack ();
5052 timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS);
5055 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
5056 function to call in parse-tree form; it has not yet been
5057 semantically analyzed. ARGS are the arguments to the function.
5058 They have already been semantically analyzed. This may change
5059 ARGS. */
5061 tree
5062 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
5063 tsubst_flags_t complain)
5065 tree orig_fn;
5066 vec<tree, va_gc> *orig_args = NULL;
5067 tree expr;
5068 tree object;
5070 orig_fn = fn;
5071 object = TREE_OPERAND (fn, 0);
5073 if (processing_template_decl)
5075 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
5076 || TREE_CODE (fn) == MEMBER_REF);
5077 if (type_dependent_expression_p (fn)
5078 || any_type_dependent_arguments_p (*args))
5079 return build_min_nt_call_vec (fn, *args);
5081 orig_args = make_tree_vector_copy (*args);
5083 /* Transform the arguments and add the implicit "this"
5084 parameter. That must be done before the FN is transformed
5085 because we depend on the form of FN. */
5086 make_args_non_dependent (*args);
5087 object = build_non_dependent_expr (object);
5088 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5090 if (TREE_CODE (fn) == DOTSTAR_EXPR)
5091 object = cp_build_addr_expr (object, complain);
5092 vec_safe_insert (*args, 0, object);
5094 /* Now that the arguments are done, transform FN. */
5095 fn = build_non_dependent_expr (fn);
5098 /* A qualified name corresponding to a bound pointer-to-member is
5099 represented as an OFFSET_REF:
5101 struct B { void g(); };
5102 void (B::*p)();
5103 void B::g() { (this->*p)(); } */
5104 if (TREE_CODE (fn) == OFFSET_REF)
5106 tree object_addr = cp_build_addr_expr (object, complain);
5107 fn = TREE_OPERAND (fn, 1);
5108 fn = get_member_function_from_ptrfunc (&object_addr, fn,
5109 complain);
5110 vec_safe_insert (*args, 0, object_addr);
5113 if (CLASS_TYPE_P (TREE_TYPE (fn)))
5114 expr = build_op_call (fn, args, complain);
5115 else
5116 expr = cp_build_function_call_vec (fn, args, complain);
5117 if (processing_template_decl && expr != error_mark_node)
5118 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
5120 if (orig_args != NULL)
5121 release_tree_vector (orig_args);
5123 return expr;
5127 void
5128 check_default_args (tree x)
5130 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
5131 bool saw_def = false;
5132 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
5133 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
5135 if (TREE_PURPOSE (arg))
5136 saw_def = true;
5137 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
5139 error ("default argument missing for parameter %P of %q+#D", i, x);
5140 TREE_PURPOSE (arg) = error_mark_node;
5145 /* Return true if function DECL can be inlined. This is used to force
5146 instantiation of methods that might be interesting for inlining. */
5147 bool
5148 possibly_inlined_p (tree decl)
5150 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
5151 if (DECL_UNINLINABLE (decl))
5152 return false;
5153 if (!optimize)
5154 return DECL_DECLARED_INLINE_P (decl);
5155 /* When optimizing, we might inline everything when flatten
5156 attribute or heuristics inlining for size or autoinlining
5157 is used. */
5158 return true;
5161 /* Normally, we can wait until instantiation-time to synthesize DECL.
5162 However, if DECL is a static data member initialized with a constant
5163 or a constexpr function, we need it right now because a reference to
5164 such a data member or a call to such function is not value-dependent.
5165 For a function that uses auto in the return type, we need to instantiate
5166 it to find out its type. For OpenMP user defined reductions, we need
5167 them instantiated for reduction clauses which inline them by hand
5168 directly. */
5170 static void
5171 maybe_instantiate_decl (tree decl)
5173 if (DECL_LANG_SPECIFIC (decl)
5174 && DECL_TEMPLATE_INFO (decl)
5175 && (decl_maybe_constant_var_p (decl)
5176 || (TREE_CODE (decl) == FUNCTION_DECL
5177 && DECL_OMP_DECLARE_REDUCTION_P (decl))
5178 || undeduced_auto_decl (decl))
5179 && !DECL_DECLARED_CONCEPT_P (decl)
5180 && !uses_template_parms (DECL_TI_ARGS (decl)))
5182 /* Instantiating a function will result in garbage collection. We
5183 must treat this situation as if we were within the body of a
5184 function so as to avoid collecting live data only referenced from
5185 the stack (such as overload resolution candidates). */
5186 ++function_depth;
5187 instantiate_decl (decl, /*defer_ok=*/false,
5188 /*expl_inst_class_mem_p=*/false);
5189 --function_depth;
5193 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
5194 If DECL is a specialization or implicitly declared class member,
5195 generate the actual definition. Return false if something goes
5196 wrong, true otherwise. */
5198 bool
5199 mark_used (tree decl, tsubst_flags_t complain)
5201 /* If DECL is a BASELINK for a single function, then treat it just
5202 like the DECL for the function. Otherwise, if the BASELINK is
5203 for an overloaded function, we don't know which function was
5204 actually used until after overload resolution. */
5205 if (BASELINK_P (decl))
5207 decl = BASELINK_FUNCTIONS (decl);
5208 if (really_overloaded_fn (decl))
5209 return true;
5210 decl = OVL_FIRST (decl);
5213 /* Set TREE_USED for the benefit of -Wunused. */
5214 TREE_USED (decl) = 1;
5215 /* And for structured bindings also the underlying decl. */
5216 if (DECL_DECOMPOSITION_P (decl) && DECL_DECOMP_BASE (decl))
5217 TREE_USED (DECL_DECOMP_BASE (decl)) = 1;
5219 if (TREE_CODE (decl) == TEMPLATE_DECL)
5220 return true;
5222 if (DECL_CLONED_FUNCTION_P (decl))
5223 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5225 /* Mark enumeration types as used. */
5226 if (TREE_CODE (decl) == CONST_DECL)
5227 used_types_insert (DECL_CONTEXT (decl));
5229 if (TREE_CODE (decl) == FUNCTION_DECL
5230 && !maybe_instantiate_noexcept (decl, complain))
5231 return false;
5233 if (TREE_CODE (decl) == FUNCTION_DECL
5234 && DECL_DELETED_FN (decl))
5236 if (DECL_ARTIFICIAL (decl)
5237 && DECL_CONV_FN_P (decl)
5238 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
5239 /* We mark a lambda conversion op as deleted if we can't
5240 generate it properly; see maybe_add_lambda_conv_op. */
5241 sorry ("converting lambda that uses %<...%> to function pointer");
5242 else if (complain & tf_error)
5244 error ("use of deleted function %qD", decl);
5245 if (!maybe_explain_implicit_delete (decl))
5246 inform (DECL_SOURCE_LOCATION (decl), "declared here");
5248 return false;
5251 if (TREE_DEPRECATED (decl) && (complain & tf_warning)
5252 && deprecated_state != DEPRECATED_SUPPRESS)
5253 warn_deprecated_use (decl, NULL_TREE);
5255 /* We can only check DECL_ODR_USED on variables or functions with
5256 DECL_LANG_SPECIFIC set, and these are also the only decls that we
5257 might need special handling for. */
5258 if (!VAR_OR_FUNCTION_DECL_P (decl)
5259 || DECL_LANG_SPECIFIC (decl) == NULL
5260 || DECL_THUNK_P (decl))
5262 if (!processing_template_decl
5263 && !require_deduced_type (decl, complain))
5264 return false;
5265 return true;
5268 /* We only want to do this processing once. We don't need to keep trying
5269 to instantiate inline templates, because unit-at-a-time will make sure
5270 we get them compiled before functions that want to inline them. */
5271 if (DECL_ODR_USED (decl))
5272 return true;
5274 /* Normally, we can wait until instantiation-time to synthesize DECL.
5275 However, if DECL is a static data member initialized with a constant
5276 or a constexpr function, we need it right now because a reference to
5277 such a data member or a call to such function is not value-dependent.
5278 For a function that uses auto in the return type, we need to instantiate
5279 it to find out its type. For OpenMP user defined reductions, we need
5280 them instantiated for reduction clauses which inline them by hand
5281 directly. */
5282 maybe_instantiate_decl (decl);
5284 if (processing_template_decl || in_template_function ())
5285 return true;
5287 /* Check this too in case we're within instantiate_non_dependent_expr. */
5288 if (DECL_TEMPLATE_INFO (decl)
5289 && uses_template_parms (DECL_TI_ARGS (decl)))
5290 return true;
5292 if (!require_deduced_type (decl, complain))
5293 return false;
5295 if (builtin_pack_fn_p (decl))
5297 error ("use of built-in parameter pack %qD outside of a template",
5298 DECL_NAME (decl));
5299 return false;
5302 /* If we don't need a value, then we don't need to synthesize DECL. */
5303 if (cp_unevaluated_operand || in_discarded_stmt)
5304 return true;
5306 DECL_ODR_USED (decl) = 1;
5307 if (DECL_CLONED_FUNCTION_P (decl))
5308 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5310 /* DR 757: A type without linkage shall not be used as the type of a
5311 variable or function with linkage, unless
5312 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5313 o the variable or function is not used (3.2 [basic.def.odr]) or is
5314 defined in the same translation unit. */
5315 if (cxx_dialect > cxx98
5316 && decl_linkage (decl) != lk_none
5317 && !DECL_EXTERN_C_P (decl)
5318 && !DECL_ARTIFICIAL (decl)
5319 && !decl_defined_p (decl)
5320 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
5322 if (is_local_extern (decl))
5323 /* There's no way to define a local extern, and adding it to
5324 the vector interferes with GC, so give an error now. */
5325 no_linkage_error (decl);
5326 else
5327 vec_safe_push (no_linkage_decls, decl);
5330 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
5331 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
5332 /* Remember it, so we can check it was defined. */
5333 note_vague_linkage_fn (decl);
5335 /* Is it a synthesized method that needs to be synthesized? */
5336 if (TREE_CODE (decl) == FUNCTION_DECL
5337 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5338 && DECL_DEFAULTED_FN (decl)
5339 /* A function defaulted outside the class is synthesized either by
5340 cp_finish_decl or instantiate_decl. */
5341 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
5342 && ! DECL_INITIAL (decl))
5344 /* Defer virtual destructors so that thunks get the right
5345 linkage. */
5346 if (DECL_VIRTUAL_P (decl) && !at_eof)
5348 note_vague_linkage_fn (decl);
5349 return true;
5352 /* Remember the current location for a function we will end up
5353 synthesizing. Then we can inform the user where it was
5354 required in the case of error. */
5355 DECL_SOURCE_LOCATION (decl) = input_location;
5357 /* Synthesizing an implicitly defined member function will result in
5358 garbage collection. We must treat this situation as if we were
5359 within the body of a function so as to avoid collecting live data
5360 on the stack (such as overload resolution candidates).
5362 We could just let cp_write_global_declarations handle synthesizing
5363 this function by adding it to deferred_fns, but doing
5364 it at the use site produces better error messages. */
5365 ++function_depth;
5366 synthesize_method (decl);
5367 --function_depth;
5368 /* If this is a synthesized method we don't need to
5369 do the instantiation test below. */
5371 else if (VAR_OR_FUNCTION_DECL_P (decl)
5372 && DECL_TEMPLATE_INFO (decl)
5373 && !DECL_DECLARED_CONCEPT_P (decl)
5374 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5375 || always_instantiate_p (decl)))
5376 /* If this is a function or variable that is an instance of some
5377 template, we now know that we will need to actually do the
5378 instantiation. We check that DECL is not an explicit
5379 instantiation because that is not checked in instantiate_decl.
5381 We put off instantiating functions in order to improve compile
5382 times. Maintaining a stack of active functions is expensive,
5383 and the inliner knows to instantiate any functions it might
5384 need. Therefore, we always try to defer instantiation. */
5386 ++function_depth;
5387 instantiate_decl (decl, /*defer_ok=*/true,
5388 /*expl_inst_class_mem_p=*/false);
5389 --function_depth;
5392 return true;
5395 bool
5396 mark_used (tree decl)
5398 return mark_used (decl, tf_warning_or_error);
5401 tree
5402 vtv_start_verification_constructor_init_function (void)
5404 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5407 tree
5408 vtv_finish_verification_constructor_init_function (tree function_body)
5410 tree fn;
5412 finish_compound_stmt (function_body);
5413 fn = finish_function (/*inline_p=*/false);
5414 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5415 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5417 return fn;
5420 #include "gt-cp-decl2.h"