Fix typo in last ChangeLog entry
[official-gcc.git] / gcc / cp / decl2.c
blobfa753749e1a6b63f2497f529e35cdb211fca883c
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 if (processing_template_decl)
743 return;
745 DECL_DEFER_OUTPUT (decl) = 1;
746 vec_safe_push (deferred_fns, decl);
749 /* As above, but for variable template instantiations. */
751 void
752 note_variable_template_instantiation (tree decl)
754 vec_safe_push (pending_statics, decl);
757 /* We have just processed the DECL, which is a static data member.
758 The other parameters are as for cp_finish_decl. */
760 void
761 finish_static_data_member_decl (tree decl,
762 tree init, bool init_const_expr_p,
763 tree asmspec_tree,
764 int flags)
766 DECL_CONTEXT (decl) = current_class_type;
768 /* We cannot call pushdecl here, because that would fill in the
769 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
770 the right thing, namely, to put this decl out straight away. */
772 if (! processing_template_decl)
773 vec_safe_push (pending_statics, decl);
775 if (LOCAL_CLASS_P (current_class_type)
776 /* We already complained about the template definition. */
777 && !DECL_TEMPLATE_INSTANTIATION (decl))
778 permerror (input_location, "local class %q#T shall not have static data member %q#D",
779 current_class_type, decl);
780 else
781 for (tree t = current_class_type; TYPE_P (t);
782 t = CP_TYPE_CONTEXT (t))
783 if (TYPE_UNNAMED_P (t))
785 if (permerror (DECL_SOURCE_LOCATION (decl),
786 "static data member %qD in unnamed class", decl))
787 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
788 "unnamed class defined here");
789 break;
792 DECL_IN_AGGR_P (decl) = 1;
794 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
795 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
796 SET_VAR_HAD_UNKNOWN_BOUND (decl);
798 if (init)
800 /* Similarly to start_decl_1, we want to complete the type in order
801 to do the right thing in cp_apply_type_quals_to_decl, possibly
802 clear TYPE_QUAL_CONST (c++/65579). */
803 tree type = TREE_TYPE (decl) = complete_type (TREE_TYPE (decl));
804 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
807 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
810 /* DECLARATOR and DECLSPECS correspond to a class member. The other
811 parameters are as for cp_finish_decl. Return the DECL for the
812 class member declared. */
814 tree
815 grokfield (const cp_declarator *declarator,
816 cp_decl_specifier_seq *declspecs,
817 tree init, bool init_const_expr_p,
818 tree asmspec_tree,
819 tree attrlist)
821 tree value;
822 const char *asmspec = 0;
823 int flags;
824 tree name;
826 if (init
827 && TREE_CODE (init) == TREE_LIST
828 && TREE_VALUE (init) == error_mark_node
829 && TREE_CHAIN (init) == NULL_TREE)
830 init = NULL_TREE;
832 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
833 if (! value || value == error_mark_node)
834 /* friend or constructor went bad. */
835 return error_mark_node;
836 if (TREE_TYPE (value) == error_mark_node)
837 return value;
839 if (TREE_CODE (value) == TYPE_DECL && init)
841 error ("typedef %qD is initialized (use decltype instead)", value);
842 init = NULL_TREE;
845 /* Pass friendly classes back. */
846 if (value == void_type_node)
847 return value;
850 name = DECL_NAME (value);
852 if (name != NULL_TREE)
854 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
856 error ("explicit template argument list not allowed");
857 return error_mark_node;
860 if (IDENTIFIER_POINTER (name)[0] == '_'
861 && id_equal (name, "_vptr"))
862 error ("member %qD conflicts with virtual function table field name",
863 value);
866 /* Stash away type declarations. */
867 if (TREE_CODE (value) == TYPE_DECL)
869 DECL_NONLOCAL (value) = 1;
870 DECL_CONTEXT (value) = current_class_type;
872 if (attrlist)
874 int attrflags = 0;
876 /* If this is a typedef that names the class for linkage purposes
877 (7.1.3p8), apply any attributes directly to the type. */
878 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
879 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
880 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
882 cplus_decl_attributes (&value, attrlist, attrflags);
885 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
886 && TREE_TYPE (value) != error_mark_node
887 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
888 set_underlying_type (value);
890 /* It's important that push_template_decl below follows
891 set_underlying_type above so that the created template
892 carries the properly set type of VALUE. */
893 if (processing_template_decl)
894 value = push_template_decl (value);
896 record_locally_defined_typedef (value);
897 return value;
900 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
902 if (!friendp && DECL_IN_AGGR_P (value))
904 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
905 return void_type_node;
908 if (asmspec_tree && asmspec_tree != error_mark_node)
909 asmspec = TREE_STRING_POINTER (asmspec_tree);
911 if (init)
913 if (TREE_CODE (value) == FUNCTION_DECL)
915 if (init == ridpointers[(int)RID_DELETE])
917 if (friendp && decl_defined_p (value))
919 error ("redefinition of %q#D", value);
920 inform (DECL_SOURCE_LOCATION (value),
921 "%q#D previously defined here", value);
923 else
925 DECL_DELETED_FN (value) = 1;
926 DECL_DECLARED_INLINE_P (value) = 1;
927 DECL_INITIAL (value) = error_mark_node;
930 else if (init == ridpointers[(int)RID_DEFAULT])
932 if (defaultable_fn_check (value))
934 DECL_DEFAULTED_FN (value) = 1;
935 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
936 DECL_DECLARED_INLINE_P (value) = 1;
939 else if (TREE_CODE (init) == DEFAULT_ARG)
940 error ("invalid initializer for member function %qD", value);
941 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
943 if (integer_zerop (init))
944 DECL_PURE_VIRTUAL_P (value) = 1;
945 else if (error_operand_p (init))
946 ; /* An error has already been reported. */
947 else
948 error ("invalid initializer for member function %qD",
949 value);
951 else
953 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
954 if (friendp)
955 error ("initializer specified for friend function %qD",
956 value);
957 else
958 error ("initializer specified for static member function %qD",
959 value);
962 else if (TREE_CODE (value) == FIELD_DECL)
963 /* C++11 NSDMI, keep going. */;
964 else if (!VAR_P (value))
965 gcc_unreachable ();
968 /* Pass friend decls back. */
969 if ((TREE_CODE (value) == FUNCTION_DECL
970 || TREE_CODE (value) == TEMPLATE_DECL)
971 && DECL_CONTEXT (value) != current_class_type)
972 return value;
974 /* Need to set this before push_template_decl. */
975 if (VAR_P (value))
976 DECL_CONTEXT (value) = current_class_type;
978 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
980 value = push_template_decl (value);
981 if (error_operand_p (value))
982 return error_mark_node;
985 if (attrlist)
986 cplus_decl_attributes (&value, attrlist, 0);
988 if (init && DIRECT_LIST_INIT_P (init))
989 flags = LOOKUP_NORMAL;
990 else
991 flags = LOOKUP_IMPLICIT;
993 switch (TREE_CODE (value))
995 case VAR_DECL:
996 finish_static_data_member_decl (value, init, init_const_expr_p,
997 asmspec_tree, flags);
998 return value;
1000 case FIELD_DECL:
1001 if (asmspec)
1002 error ("%<asm%> specifiers are not permitted on non-static data members");
1003 if (DECL_INITIAL (value) == error_mark_node)
1004 init = error_mark_node;
1005 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
1006 NULL_TREE, flags);
1007 DECL_IN_AGGR_P (value) = 1;
1008 return value;
1010 case FUNCTION_DECL:
1011 if (asmspec)
1012 set_user_assembler_name (value, asmspec);
1014 cp_finish_decl (value,
1015 /*init=*/NULL_TREE,
1016 /*init_const_expr_p=*/false,
1017 asmspec_tree, flags);
1019 /* Pass friends back this way. */
1020 if (DECL_FRIEND_P (value))
1021 return void_type_node;
1023 DECL_IN_AGGR_P (value) = 1;
1024 return value;
1026 default:
1027 gcc_unreachable ();
1029 return NULL_TREE;
1032 /* Like `grokfield', but for bitfields.
1033 WIDTH is the width of the bitfield, a constant expression.
1034 The other parameters are as for grokfield. */
1036 tree
1037 grokbitfield (const cp_declarator *declarator,
1038 cp_decl_specifier_seq *declspecs, tree width, tree init,
1039 tree attrlist)
1041 tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1042 init != NULL_TREE, &attrlist);
1044 if (value == error_mark_node)
1045 return NULL_TREE; /* friends went bad. */
1046 if (TREE_TYPE (value) == error_mark_node)
1047 return value;
1049 /* Pass friendly classes back. */
1050 if (VOID_TYPE_P (value))
1051 return void_type_node;
1053 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
1054 && (POINTER_TYPE_P (value)
1055 || !dependent_type_p (TREE_TYPE (value))))
1057 error ("bit-field %qD with non-integral type", value);
1058 return error_mark_node;
1061 if (TREE_CODE (value) == TYPE_DECL)
1063 error ("cannot declare %qD to be a bit-field type", value);
1064 return NULL_TREE;
1067 /* Usually, finish_struct_1 catches bitfields with invalid types.
1068 But, in the case of bitfields with function type, we confuse
1069 ourselves into thinking they are member functions, so we must
1070 check here. */
1071 if (TREE_CODE (value) == FUNCTION_DECL)
1073 error ("cannot declare bit-field %qD with function type",
1074 DECL_NAME (value));
1075 return NULL_TREE;
1078 if (width && TYPE_WARN_IF_NOT_ALIGN (TREE_TYPE (value)))
1080 error ("cannot declare bit-field %qD with %<warn_if_not_aligned%> type",
1081 DECL_NAME (value));
1082 return NULL_TREE;
1085 if (DECL_IN_AGGR_P (value))
1087 error ("%qD is already defined in the class %qT", value,
1088 DECL_CONTEXT (value));
1089 return void_type_node;
1092 if (TREE_STATIC (value))
1094 error ("static member %qD cannot be a bit-field", value);
1095 return NULL_TREE;
1098 int flags = LOOKUP_IMPLICIT;
1099 if (init && DIRECT_LIST_INIT_P (init))
1100 flags = LOOKUP_NORMAL;
1101 cp_finish_decl (value, init, false, NULL_TREE, flags);
1103 if (width != error_mark_node)
1105 /* The width must be an integer type. */
1106 if (!type_dependent_expression_p (width)
1107 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1108 error ("width of bit-field %qD has non-integral type %qT", value,
1109 TREE_TYPE (width));
1110 else
1112 /* Temporarily stash the width in DECL_BIT_FIELD_REPRESENTATIVE.
1113 check_bitfield_decl picks it from there later and sets DECL_SIZE
1114 accordingly. */
1115 DECL_BIT_FIELD_REPRESENTATIVE (value) = width;
1116 SET_DECL_C_BIT_FIELD (value);
1120 DECL_IN_AGGR_P (value) = 1;
1122 if (attrlist)
1123 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1125 return value;
1129 /* Returns true iff ATTR is an attribute which needs to be applied at
1130 instantiation time rather than template definition time. */
1132 static bool
1133 is_late_template_attribute (tree attr, tree decl)
1135 tree name = get_attribute_name (attr);
1136 tree args = TREE_VALUE (attr);
1137 const struct attribute_spec *spec = lookup_attribute_spec (name);
1138 tree arg;
1140 if (!spec)
1141 /* Unknown attribute. */
1142 return false;
1144 /* Attribute weak handling wants to write out assembly right away. */
1145 if (is_attribute_p ("weak", name))
1146 return true;
1148 /* Attributes used and unused are applied directly, as they appertain to
1149 decls. */
1150 if (is_attribute_p ("unused", name)
1151 || is_attribute_p ("used", name))
1152 return false;
1154 /* Attribute tls_model wants to modify the symtab. */
1155 if (is_attribute_p ("tls_model", name))
1156 return true;
1158 /* #pragma omp declare simd attribute needs to be always deferred. */
1159 if (flag_openmp
1160 && is_attribute_p ("omp declare simd", name))
1161 return true;
1163 /* An attribute pack is clearly dependent. */
1164 if (args && PACK_EXPANSION_P (args))
1165 return true;
1167 /* If any of the arguments are dependent expressions, we can't evaluate
1168 the attribute until instantiation time. */
1169 for (arg = args; arg; arg = TREE_CHAIN (arg))
1171 tree t = TREE_VALUE (arg);
1173 /* If the first attribute argument is an identifier, only consider
1174 second and following arguments. Attributes like mode, format,
1175 cleanup and several target specific attributes aren't late
1176 just because they have an IDENTIFIER_NODE as first argument. */
1177 if (arg == args && attribute_takes_identifier_p (name)
1178 && identifier_p (t))
1179 continue;
1181 if (value_dependent_expression_p (t)
1182 || type_dependent_expression_p (t))
1183 return true;
1186 if (TREE_CODE (decl) == TYPE_DECL
1187 || TYPE_P (decl)
1188 || spec->type_required)
1190 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1192 /* We can't apply any attributes to a completely unknown type until
1193 instantiation time. */
1194 enum tree_code code = TREE_CODE (type);
1195 if (code == TEMPLATE_TYPE_PARM
1196 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1197 || code == TYPENAME_TYPE)
1198 return true;
1199 /* Also defer most attributes on dependent types. This is not
1200 necessary in all cases, but is the better default. */
1201 else if (dependent_type_p (type)
1202 /* But some attributes specifically apply to templates. */
1203 && !is_attribute_p ("abi_tag", name)
1204 && !is_attribute_p ("deprecated", name)
1205 && !is_attribute_p ("visibility", name))
1206 return true;
1207 else
1208 return false;
1210 else
1211 return false;
1214 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1215 applied at instantiation time and return them. If IS_DEPENDENT is true,
1216 the declaration itself is dependent, so all attributes should be applied
1217 at instantiation time. */
1219 static tree
1220 splice_template_attributes (tree *attr_p, tree decl)
1222 tree *p = attr_p;
1223 tree late_attrs = NULL_TREE;
1224 tree *q = &late_attrs;
1226 if (!p)
1227 return NULL_TREE;
1229 for (; *p; )
1231 if (is_late_template_attribute (*p, decl))
1233 ATTR_IS_DEPENDENT (*p) = 1;
1234 *q = *p;
1235 *p = TREE_CHAIN (*p);
1236 q = &TREE_CHAIN (*q);
1237 *q = NULL_TREE;
1239 else
1240 p = &TREE_CHAIN (*p);
1243 return late_attrs;
1246 /* Remove any late attributes from the list in ATTR_P and attach them to
1247 DECL_P. */
1249 static void
1250 save_template_attributes (tree *attr_p, tree *decl_p, int flags)
1252 tree *q;
1254 if (attr_p && *attr_p == error_mark_node)
1255 return;
1257 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1258 if (!late_attrs)
1259 return;
1261 if (DECL_P (*decl_p))
1262 q = &DECL_ATTRIBUTES (*decl_p);
1263 else
1264 q = &TYPE_ATTRIBUTES (*decl_p);
1266 tree old_attrs = *q;
1268 /* Merge the late attributes at the beginning with the attribute
1269 list. */
1270 late_attrs = merge_attributes (late_attrs, *q);
1271 if (*q != late_attrs
1272 && !DECL_P (*decl_p)
1273 && !(flags & ATTR_FLAG_TYPE_IN_PLACE))
1275 if (!dependent_type_p (*decl_p))
1276 *decl_p = cp_build_type_attribute_variant (*decl_p, late_attrs);
1277 else
1279 *decl_p = build_variant_type_copy (*decl_p);
1280 TYPE_ATTRIBUTES (*decl_p) = late_attrs;
1283 else
1284 *q = late_attrs;
1286 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1288 /* We've added new attributes directly to the main variant, so
1289 now we need to update all of the other variants to include
1290 these new attributes. */
1291 tree variant;
1292 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1293 variant = TYPE_NEXT_VARIANT (variant))
1295 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1296 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1301 /* True if ATTRS contains any dependent attributes that affect type
1302 identity. */
1304 bool
1305 any_dependent_type_attributes_p (tree attrs)
1307 for (tree a = attrs; a; a = TREE_CHAIN (a))
1308 if (ATTR_IS_DEPENDENT (a))
1310 const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (a));
1311 if (as && as->affects_type_identity)
1312 return true;
1314 return false;
1317 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1318 to a typedef which gives a previously unnamed class or enum a name for
1319 linkage purposes. */
1321 bool
1322 attributes_naming_typedef_ok (tree attrs)
1324 for (; attrs; attrs = TREE_CHAIN (attrs))
1326 tree name = get_attribute_name (attrs);
1327 if (is_attribute_p ("vector_size", name))
1328 return false;
1330 return true;
1333 /* Like reconstruct_complex_type, but handle also template trees. */
1335 tree
1336 cp_reconstruct_complex_type (tree type, tree bottom)
1338 tree inner, outer;
1339 bool late_return_type_p = false;
1341 if (TYPE_PTR_P (type))
1343 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1344 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1345 TYPE_REF_CAN_ALIAS_ALL (type));
1347 else if (TREE_CODE (type) == REFERENCE_TYPE)
1349 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1350 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1351 TYPE_REF_CAN_ALIAS_ALL (type));
1353 else if (TREE_CODE (type) == ARRAY_TYPE)
1355 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1356 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1357 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1358 element type qualification will be handled by the recursive
1359 cp_reconstruct_complex_type call and cp_build_qualified_type
1360 for ARRAY_TYPEs changes the element type. */
1361 return outer;
1363 else if (TREE_CODE (type) == FUNCTION_TYPE)
1365 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1366 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1367 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1368 outer = apply_memfn_quals (outer,
1369 type_memfn_quals (type),
1370 type_memfn_rqual (type));
1372 else if (TREE_CODE (type) == METHOD_TYPE)
1374 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1375 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1376 /* The build_method_type_directly() routine prepends 'this' to argument list,
1377 so we must compensate by getting rid of it. */
1378 outer
1379 = build_method_type_directly
1380 (class_of_this_parm (type), inner,
1381 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1383 else if (TREE_CODE (type) == OFFSET_TYPE)
1385 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1386 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1388 else
1389 return bottom;
1391 if (TYPE_ATTRIBUTES (type))
1392 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1393 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1395 if (late_return_type_p)
1396 TYPE_HAS_LATE_RETURN_TYPE (outer) = 1;
1398 return outer;
1401 /* Replaces any constexpr expression that may be into the attributes
1402 arguments with their reduced value. */
1404 static void
1405 cp_check_const_attributes (tree attributes)
1407 if (attributes == error_mark_node)
1408 return;
1410 tree attr;
1411 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1413 tree arg;
1414 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1416 tree expr = TREE_VALUE (arg);
1417 if (EXPR_P (expr))
1418 TREE_VALUE (arg) = maybe_constant_value (expr);
1423 /* Return true if TYPE is an OpenMP mappable type. */
1424 bool
1425 cp_omp_mappable_type (tree type)
1427 /* Mappable type has to be complete. */
1428 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1429 return false;
1430 /* Arrays have mappable type if the elements have mappable type. */
1431 while (TREE_CODE (type) == ARRAY_TYPE)
1432 type = TREE_TYPE (type);
1433 /* A mappable type cannot contain virtual members. */
1434 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1435 return false;
1436 /* All data members must be non-static. */
1437 if (CLASS_TYPE_P (type))
1439 tree field;
1440 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1441 if (VAR_P (field))
1442 return false;
1443 /* All fields must have mappable types. */
1444 else if (TREE_CODE (field) == FIELD_DECL
1445 && !cp_omp_mappable_type (TREE_TYPE (field)))
1446 return false;
1448 return true;
1451 /* Return the last pushed declaration for the symbol DECL or NULL
1452 when no such declaration exists. */
1454 static tree
1455 find_last_decl (tree decl)
1457 tree last_decl = NULL_TREE;
1459 if (tree name = DECL_P (decl) ? DECL_NAME (decl) : NULL_TREE)
1461 /* Look up the declaration in its scope. */
1462 tree pushed_scope = NULL_TREE;
1463 if (tree ctype = DECL_CONTEXT (decl))
1464 pushed_scope = push_scope (ctype);
1466 last_decl = lookup_name (name);
1468 if (pushed_scope)
1469 pop_scope (pushed_scope);
1471 /* The declaration may be a member conversion operator
1472 or a bunch of overfloads (handle the latter below). */
1473 if (last_decl && BASELINK_P (last_decl))
1474 last_decl = BASELINK_FUNCTIONS (last_decl);
1477 if (!last_decl)
1478 return NULL_TREE;
1480 if (DECL_P (last_decl) || TREE_CODE (last_decl) == OVERLOAD)
1482 /* A set of overloads of the same function. */
1483 for (lkp_iterator iter (last_decl); iter; ++iter)
1485 if (TREE_CODE (*iter) == OVERLOAD)
1486 continue;
1488 if (decls_match (decl, *iter, /*record_decls=*/false))
1489 return *iter;
1491 return NULL_TREE;
1494 return NULL_TREE;
1497 /* Like decl_attributes, but handle C++ complexity. */
1499 void
1500 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1502 if (*decl == NULL_TREE || *decl == void_type_node
1503 || *decl == error_mark_node)
1504 return;
1506 /* Add implicit "omp declare target" attribute if requested. */
1507 if (scope_chain->omp_declare_target_attribute
1508 && ((VAR_P (*decl)
1509 && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
1510 || TREE_CODE (*decl) == FUNCTION_DECL))
1512 if (VAR_P (*decl)
1513 && DECL_CLASS_SCOPE_P (*decl))
1514 error ("%q+D static data member inside of declare target directive",
1515 *decl);
1516 else if (!processing_template_decl
1517 && VAR_P (*decl)
1518 && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1519 error ("%q+D in declare target directive does not have mappable type",
1520 *decl);
1521 else
1522 attributes = tree_cons (get_identifier ("omp declare target"),
1523 NULL_TREE, attributes);
1526 if (processing_template_decl)
1528 if (check_for_bare_parameter_packs (attributes))
1529 return;
1531 save_template_attributes (&attributes, decl, flags);
1534 cp_check_const_attributes (attributes);
1536 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1537 decl = &DECL_TEMPLATE_RESULT (*decl);
1539 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1541 attributes
1542 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1543 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1544 attributes, flags);
1546 else
1548 tree last_decl = find_last_decl (*decl);
1549 decl_attributes (decl, attributes, flags, last_decl);
1552 if (TREE_CODE (*decl) == TYPE_DECL)
1553 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1555 /* Propagate deprecation out to the template. */
1556 if (TREE_DEPRECATED (*decl))
1557 if (tree ti = get_template_info (*decl))
1559 tree tmpl = TI_TEMPLATE (ti);
1560 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1561 : DECL_TEMPLATE_RESULT (tmpl));
1562 if (*decl == pattern)
1563 TREE_DEPRECATED (tmpl) = true;
1567 /* Walks through the namespace- or function-scope anonymous union
1568 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1569 Returns one of the fields for use in the mangled name. */
1571 static tree
1572 build_anon_union_vars (tree type, tree object)
1574 tree main_decl = NULL_TREE;
1575 tree field;
1577 /* Rather than write the code to handle the non-union case,
1578 just give an error. */
1579 if (TREE_CODE (type) != UNION_TYPE)
1581 error ("anonymous struct not inside named type");
1582 return error_mark_node;
1585 for (field = TYPE_FIELDS (type);
1586 field != NULL_TREE;
1587 field = DECL_CHAIN (field))
1589 tree decl;
1590 tree ref;
1592 if (DECL_ARTIFICIAL (field))
1593 continue;
1594 if (TREE_CODE (field) != FIELD_DECL)
1596 permerror (DECL_SOURCE_LOCATION (field),
1597 "%q#D invalid; an anonymous union can only "
1598 "have non-static data members", field);
1599 continue;
1602 if (TREE_PRIVATE (field))
1603 permerror (DECL_SOURCE_LOCATION (field),
1604 "private member %q#D in anonymous union", field);
1605 else if (TREE_PROTECTED (field))
1606 permerror (DECL_SOURCE_LOCATION (field),
1607 "protected member %q#D in anonymous union", field);
1609 if (processing_template_decl)
1610 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1611 DECL_NAME (field), NULL_TREE);
1612 else
1613 ref = build_class_member_access_expr (object, field, NULL_TREE,
1614 false, tf_warning_or_error);
1616 if (DECL_NAME (field))
1618 tree base;
1620 decl = build_decl (input_location,
1621 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1622 DECL_ANON_UNION_VAR_P (decl) = 1;
1623 DECL_ARTIFICIAL (decl) = 1;
1625 base = get_base_address (object);
1626 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1627 TREE_STATIC (decl) = TREE_STATIC (base);
1628 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1630 SET_DECL_VALUE_EXPR (decl, ref);
1631 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1633 decl = pushdecl (decl);
1635 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1636 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1637 else
1638 decl = 0;
1640 if (main_decl == NULL_TREE)
1641 main_decl = decl;
1644 return main_decl;
1647 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1648 anonymous union, then all members must be laid out together. PUBLIC_P
1649 is nonzero if this union is not declared static. */
1651 void
1652 finish_anon_union (tree anon_union_decl)
1654 tree type;
1655 tree main_decl;
1656 bool public_p;
1658 if (anon_union_decl == error_mark_node)
1659 return;
1661 type = TREE_TYPE (anon_union_decl);
1662 public_p = TREE_PUBLIC (anon_union_decl);
1664 /* The VAR_DECL's context is the same as the TYPE's context. */
1665 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1667 if (TYPE_FIELDS (type) == NULL_TREE)
1668 return;
1670 if (public_p)
1672 error ("namespace-scope anonymous aggregates must be static");
1673 return;
1676 main_decl = build_anon_union_vars (type, anon_union_decl);
1677 if (main_decl == error_mark_node)
1678 return;
1679 if (main_decl == NULL_TREE)
1681 pedwarn (input_location, 0, "anonymous union with no members");
1682 return;
1685 if (!processing_template_decl)
1687 /* Use main_decl to set the mangled name. */
1688 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1689 maybe_commonize_var (anon_union_decl);
1690 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1691 mangle_decl (anon_union_decl);
1692 DECL_NAME (anon_union_decl) = NULL_TREE;
1695 pushdecl (anon_union_decl);
1696 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1699 /* Auxiliary functions to make type signatures for
1700 `operator new' and `operator delete' correspond to
1701 what compiler will be expecting. */
1703 tree
1704 coerce_new_type (tree type)
1706 int e = 0;
1707 tree args = TYPE_ARG_TYPES (type);
1709 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1711 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1713 e = 1;
1714 error ("%<operator new%> must return type %qT", ptr_type_node);
1717 if (args && args != void_list_node)
1719 if (TREE_PURPOSE (args))
1721 /* [basic.stc.dynamic.allocation]
1723 The first parameter shall not have an associated default
1724 argument. */
1725 error ("the first parameter of %<operator new%> cannot "
1726 "have a default argument");
1727 /* Throw away the default argument. */
1728 TREE_PURPOSE (args) = NULL_TREE;
1731 if (!same_type_p (TREE_VALUE (args), size_type_node))
1733 e = 2;
1734 args = TREE_CHAIN (args);
1737 else
1738 e = 2;
1740 if (e == 2)
1741 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1742 "as first parameter", size_type_node);
1744 switch (e)
1746 case 2:
1747 args = tree_cons (NULL_TREE, size_type_node, args);
1748 /* Fall through. */
1749 case 1:
1750 type = build_exception_variant
1751 (build_function_type (ptr_type_node, args),
1752 TYPE_RAISES_EXCEPTIONS (type));
1753 /* Fall through. */
1754 default:;
1756 return type;
1759 tree
1760 coerce_delete_type (tree type)
1762 int e = 0;
1763 tree args = TYPE_ARG_TYPES (type);
1765 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1767 if (!same_type_p (TREE_TYPE (type), void_type_node))
1769 e = 1;
1770 error ("%<operator delete%> must return type %qT", void_type_node);
1773 if (!args || args == void_list_node
1774 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1776 e = 2;
1777 if (args && args != void_list_node)
1778 args = TREE_CHAIN (args);
1779 error ("%<operator delete%> takes type %qT as first parameter",
1780 ptr_type_node);
1782 switch (e)
1784 case 2:
1785 args = tree_cons (NULL_TREE, ptr_type_node, args);
1786 /* Fall through. */
1787 case 1:
1788 type = build_exception_variant
1789 (build_function_type (void_type_node, args),
1790 TYPE_RAISES_EXCEPTIONS (type));
1791 /* Fall through. */
1792 default:;
1795 return type;
1798 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1799 and mark them as needed. */
1801 static void
1802 mark_vtable_entries (tree decl)
1804 tree fnaddr;
1805 unsigned HOST_WIDE_INT idx;
1807 /* It's OK for the vtable to refer to deprecated virtual functions. */
1808 warning_sentinel w(warn_deprecated_decl);
1810 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1811 idx, fnaddr)
1813 tree fn;
1815 STRIP_NOPS (fnaddr);
1817 if (TREE_CODE (fnaddr) != ADDR_EXPR
1818 && TREE_CODE (fnaddr) != FDESC_EXPR)
1819 /* This entry is an offset: a virtual base class offset, a
1820 virtual call offset, an RTTI offset, etc. */
1821 continue;
1823 fn = TREE_OPERAND (fnaddr, 0);
1824 TREE_ADDRESSABLE (fn) = 1;
1825 /* When we don't have vcall offsets, we output thunks whenever
1826 we output the vtables that contain them. With vcall offsets,
1827 we know all the thunks we'll need when we emit a virtual
1828 function, so we emit the thunks there instead. */
1829 if (DECL_THUNK_P (fn))
1830 use_thunk (fn, /*emit_p=*/0);
1831 /* Set the location, as marking the function could cause
1832 instantiation. We do not need to preserve the incoming
1833 location, as we're called from c_parse_final_cleanups, which
1834 takes care of that. */
1835 input_location = DECL_SOURCE_LOCATION (fn);
1836 mark_used (fn);
1840 /* Set DECL up to have the closest approximation of "initialized common"
1841 linkage available. */
1843 void
1844 comdat_linkage (tree decl)
1846 if (flag_weak)
1847 make_decl_one_only (decl, cxx_comdat_group (decl));
1848 else if (TREE_CODE (decl) == FUNCTION_DECL
1849 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1850 /* We can just emit function and compiler-generated variables
1851 statically; having multiple copies is (for the most part) only
1852 a waste of space.
1854 There are two correctness issues, however: the address of a
1855 template instantiation with external linkage should be the
1856 same, independent of what translation unit asks for the
1857 address, and this will not hold when we emit multiple copies of
1858 the function. However, there's little else we can do.
1860 Also, by default, the typeinfo implementation assumes that
1861 there will be only one copy of the string used as the name for
1862 each type. Therefore, if weak symbols are unavailable, the
1863 run-time library should perform a more conservative check; it
1864 should perform a string comparison, rather than an address
1865 comparison. */
1866 TREE_PUBLIC (decl) = 0;
1867 else
1869 /* Static data member template instantiations, however, cannot
1870 have multiple copies. */
1871 if (DECL_INITIAL (decl) == 0
1872 || DECL_INITIAL (decl) == error_mark_node)
1873 DECL_COMMON (decl) = 1;
1874 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1876 DECL_COMMON (decl) = 1;
1877 DECL_INITIAL (decl) = error_mark_node;
1879 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1881 /* We can't do anything useful; leave vars for explicit
1882 instantiation. */
1883 DECL_EXTERNAL (decl) = 1;
1884 DECL_NOT_REALLY_EXTERN (decl) = 0;
1888 if (TREE_PUBLIC (decl))
1889 DECL_COMDAT (decl) = 1;
1892 /* For win32 we also want to put explicit instantiations in
1893 linkonce sections, so that they will be merged with implicit
1894 instantiations; otherwise we get duplicate symbol errors.
1895 For Darwin we do not want explicit instantiations to be
1896 linkonce. */
1898 void
1899 maybe_make_one_only (tree decl)
1901 /* We used to say that this was not necessary on targets that support weak
1902 symbols, because the implicit instantiations will defer to the explicit
1903 one. However, that's not actually the case in SVR4; a strong definition
1904 after a weak one is an error. Also, not making explicit
1905 instantiations one_only means that we can end up with two copies of
1906 some template instantiations. */
1907 if (! flag_weak)
1908 return;
1910 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1911 we can get away with not emitting them if they aren't used. We need
1912 to for variables so that cp_finish_decl will update their linkage,
1913 because their DECL_INITIAL may not have been set properly yet. */
1915 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1916 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1917 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1919 make_decl_one_only (decl, cxx_comdat_group (decl));
1921 if (VAR_P (decl))
1923 varpool_node *node = varpool_node::get_create (decl);
1924 DECL_COMDAT (decl) = 1;
1925 /* Mark it needed so we don't forget to emit it. */
1926 node->forced_by_abi = true;
1927 TREE_USED (decl) = 1;
1932 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1933 This predicate will give the right answer during parsing of the
1934 function, which other tests may not. */
1936 bool
1937 vague_linkage_p (tree decl)
1939 if (!TREE_PUBLIC (decl))
1941 /* maybe_thunk_body clears TREE_PUBLIC on the maybe-in-charge 'tor
1942 variants, check one of the "clones" for the real linkage. */
1943 if ((DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
1944 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl))
1945 && DECL_CHAIN (decl)
1946 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)))
1947 return vague_linkage_p (DECL_CHAIN (decl));
1949 gcc_checking_assert (!DECL_COMDAT (decl));
1950 return false;
1952 /* Unfortunately, import_export_decl has not always been called
1953 before the function is processed, so we cannot simply check
1954 DECL_COMDAT. */
1955 if (DECL_COMDAT (decl)
1956 || (TREE_CODE (decl) == FUNCTION_DECL
1957 && DECL_DECLARED_INLINE_P (decl))
1958 || (DECL_LANG_SPECIFIC (decl)
1959 && DECL_TEMPLATE_INSTANTIATION (decl))
1960 || (VAR_P (decl) && DECL_INLINE_VAR_P (decl)))
1961 return true;
1962 else if (DECL_FUNCTION_SCOPE_P (decl))
1963 /* A local static in an inline effectively has vague linkage. */
1964 return (TREE_STATIC (decl)
1965 && vague_linkage_p (DECL_CONTEXT (decl)));
1966 else
1967 return false;
1970 /* Determine whether or not we want to specifically import or export CTYPE,
1971 using various heuristics. */
1973 static void
1974 import_export_class (tree ctype)
1976 /* -1 for imported, 1 for exported. */
1977 int import_export = 0;
1979 /* It only makes sense to call this function at EOF. The reason is
1980 that this function looks at whether or not the first non-inline
1981 non-abstract virtual member function has been defined in this
1982 translation unit. But, we can't possibly know that until we've
1983 seen the entire translation unit. */
1984 gcc_assert (at_eof);
1986 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1987 return;
1989 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1990 we will have CLASSTYPE_INTERFACE_ONLY set but not
1991 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1992 heuristic because someone will supply a #pragma implementation
1993 elsewhere, and deducing it here would produce a conflict. */
1994 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1995 return;
1997 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1998 import_export = -1;
1999 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
2000 import_export = 1;
2001 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
2002 && !flag_implicit_templates)
2003 /* For a template class, without -fimplicit-templates, check the
2004 repository. If the virtual table is assigned to this
2005 translation unit, then export the class; otherwise, import
2006 it. */
2007 import_export = repo_export_class_p (ctype) ? 1 : -1;
2008 else if (TYPE_POLYMORPHIC_P (ctype))
2010 /* The ABI specifies that the virtual table and associated
2011 information are emitted with the key method, if any. */
2012 tree method = CLASSTYPE_KEY_METHOD (ctype);
2013 /* If weak symbol support is not available, then we must be
2014 careful not to emit the vtable when the key function is
2015 inline. An inline function can be defined in multiple
2016 translation units. If we were to emit the vtable in each
2017 translation unit containing a definition, we would get
2018 multiple definition errors at link-time. */
2019 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
2020 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
2023 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
2024 a definition anywhere else. */
2025 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
2026 import_export = 0;
2028 /* Allow back ends the chance to overrule the decision. */
2029 if (targetm.cxx.import_export_class)
2030 import_export = targetm.cxx.import_export_class (ctype, import_export);
2032 if (import_export)
2034 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
2035 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
2039 /* Return true if VAR has already been provided to the back end; in that
2040 case VAR should not be modified further by the front end. */
2041 static bool
2042 var_finalized_p (tree var)
2044 return varpool_node::get_create (var)->definition;
2047 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
2048 must be emitted in this translation unit. Mark it as such. */
2050 void
2051 mark_needed (tree decl)
2053 TREE_USED (decl) = 1;
2054 if (TREE_CODE (decl) == FUNCTION_DECL)
2056 /* Extern inline functions don't become needed when referenced.
2057 If we know a method will be emitted in other TU and no new
2058 functions can be marked reachable, just use the external
2059 definition. */
2060 struct cgraph_node *node = cgraph_node::get_create (decl);
2061 node->forced_by_abi = true;
2063 /* #pragma interface and -frepo code can call mark_needed for
2064 maybe-in-charge 'tors; mark the clones as well. */
2065 tree clone;
2066 FOR_EACH_CLONE (clone, decl)
2067 mark_needed (clone);
2069 else if (VAR_P (decl))
2071 varpool_node *node = varpool_node::get_create (decl);
2072 /* C++ frontend use mark_decl_references to force COMDAT variables
2073 to be output that might appear dead otherwise. */
2074 node->forced_by_abi = true;
2078 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
2079 returns true if a definition of this entity should be provided in
2080 this object file. Callers use this function to determine whether
2081 or not to let the back end know that a definition of DECL is
2082 available in this translation unit. */
2084 bool
2085 decl_needed_p (tree decl)
2087 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2088 /* This function should only be called at the end of the translation
2089 unit. We cannot be sure of whether or not something will be
2090 COMDAT until that point. */
2091 gcc_assert (at_eof);
2093 /* All entities with external linkage that are not COMDAT/EXTERN should be
2094 emitted; they may be referred to from other object files. */
2095 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
2096 return true;
2097 /* Functions marked "dllexport" must be emitted so that they are
2098 visible to other DLLs. */
2099 if (flag_keep_inline_dllexport
2100 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
2101 return true;
2103 /* When not optimizing, do not bother to produce definitions for extern
2104 symbols. */
2105 if (DECL_REALLY_EXTERN (decl)
2106 && ((TREE_CODE (decl) != FUNCTION_DECL
2107 && !optimize)
2108 || (TREE_CODE (decl) == FUNCTION_DECL
2109 && !opt_for_fn (decl, optimize)))
2110 && !lookup_attribute ("always_inline", decl))
2111 return false;
2113 /* If this entity was used, let the back end see it; it will decide
2114 whether or not to emit it into the object file. */
2115 if (TREE_USED (decl))
2116 return true;
2118 /* Virtual functions might be needed for devirtualization. */
2119 if (flag_devirtualize
2120 && TREE_CODE (decl) == FUNCTION_DECL
2121 && DECL_VIRTUAL_P (decl))
2122 return true;
2124 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2125 reference to DECL might cause it to be emitted later. */
2126 return false;
2129 /* If necessary, write out the vtables for the dynamic class CTYPE.
2130 Returns true if any vtables were emitted. */
2132 static bool
2133 maybe_emit_vtables (tree ctype)
2135 tree vtbl;
2136 tree primary_vtbl;
2137 int needed = 0;
2138 varpool_node *current = NULL, *last = NULL;
2140 /* If the vtables for this class have already been emitted there is
2141 nothing more to do. */
2142 primary_vtbl = CLASSTYPE_VTABLES (ctype);
2143 if (var_finalized_p (primary_vtbl))
2144 return false;
2145 /* Ignore dummy vtables made by get_vtable_decl. */
2146 if (TREE_TYPE (primary_vtbl) == void_type_node)
2147 return false;
2149 /* On some targets, we cannot determine the key method until the end
2150 of the translation unit -- which is when this function is
2151 called. */
2152 if (!targetm.cxx.key_method_may_be_inline ())
2153 determine_key_method (ctype);
2155 /* See if any of the vtables are needed. */
2156 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2158 import_export_decl (vtbl);
2159 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2160 needed = 1;
2162 if (!needed)
2164 /* If the references to this class' vtables are optimized away,
2165 still emit the appropriate debugging information. See
2166 dfs_debug_mark. */
2167 if (DECL_COMDAT (primary_vtbl)
2168 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2169 note_debug_info_needed (ctype);
2170 return false;
2173 /* The ABI requires that we emit all of the vtables if we emit any
2174 of them. */
2175 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2177 /* Mark entities references from the virtual table as used. */
2178 mark_vtable_entries (vtbl);
2180 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2182 vec<tree, va_gc> *cleanups = NULL;
2183 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2184 LOOKUP_NORMAL);
2186 /* It had better be all done at compile-time. */
2187 gcc_assert (!expr && !cleanups);
2190 /* Write it out. */
2191 DECL_EXTERNAL (vtbl) = 0;
2192 rest_of_decl_compilation (vtbl, 1, 1);
2194 /* Because we're only doing syntax-checking, we'll never end up
2195 actually marking the variable as written. */
2196 if (flag_syntax_only)
2197 TREE_ASM_WRITTEN (vtbl) = 1;
2198 else if (DECL_ONE_ONLY (vtbl))
2200 current = varpool_node::get_create (vtbl);
2201 if (last)
2202 current->add_to_same_comdat_group (last);
2203 last = current;
2207 /* Since we're writing out the vtable here, also write the debug
2208 info. */
2209 note_debug_info_needed (ctype);
2211 return true;
2214 /* A special return value from type_visibility meaning internal
2215 linkage. */
2217 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2219 /* walk_tree helper function for type_visibility. */
2221 static tree
2222 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2224 int *vis_p = (int *)data;
2225 if (! TYPE_P (*tp))
2227 *walk_subtrees = 0;
2229 else if (OVERLOAD_TYPE_P (*tp)
2230 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2232 *vis_p = VISIBILITY_ANON;
2233 return *tp;
2235 else if (CLASS_TYPE_P (*tp)
2236 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2237 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2238 return NULL;
2241 /* Returns the visibility of TYPE, which is the minimum visibility of its
2242 component types. */
2244 static int
2245 type_visibility (tree type)
2247 int vis = VISIBILITY_DEFAULT;
2248 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2249 return vis;
2252 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2253 specified (or if VISIBILITY is static). If TMPL is true, this
2254 constraint is for a template argument, and takes precedence
2255 over explicitly-specified visibility on the template. */
2257 static void
2258 constrain_visibility (tree decl, int visibility, bool tmpl)
2260 if (visibility == VISIBILITY_ANON)
2262 /* extern "C" declarations aren't affected by the anonymous
2263 namespace. */
2264 if (!DECL_EXTERN_C_P (decl))
2266 TREE_PUBLIC (decl) = 0;
2267 DECL_WEAK (decl) = 0;
2268 DECL_COMMON (decl) = 0;
2269 DECL_COMDAT (decl) = false;
2270 if (VAR_OR_FUNCTION_DECL_P (decl))
2272 struct symtab_node *snode = symtab_node::get (decl);
2274 if (snode)
2275 snode->set_comdat_group (NULL);
2277 DECL_INTERFACE_KNOWN (decl) = 1;
2278 if (DECL_LANG_SPECIFIC (decl))
2279 DECL_NOT_REALLY_EXTERN (decl) = 1;
2282 else if (visibility > DECL_VISIBILITY (decl)
2283 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2285 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2286 /* This visibility was not specified. */
2287 DECL_VISIBILITY_SPECIFIED (decl) = false;
2291 /* Constrain the visibility of DECL based on the visibility of its template
2292 arguments. */
2294 static void
2295 constrain_visibility_for_template (tree decl, tree targs)
2297 /* If this is a template instantiation, check the innermost
2298 template args for visibility constraints. The outer template
2299 args are covered by the class check. */
2300 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2301 int i;
2302 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2304 int vis = 0;
2306 tree arg = TREE_VEC_ELT (args, i-1);
2307 if (TYPE_P (arg))
2308 vis = type_visibility (arg);
2309 else
2311 if (REFERENCE_REF_P (arg))
2312 arg = TREE_OPERAND (arg, 0);
2313 if (TREE_TYPE (arg))
2314 STRIP_NOPS (arg);
2315 if (TREE_CODE (arg) == ADDR_EXPR)
2316 arg = TREE_OPERAND (arg, 0);
2317 if (VAR_OR_FUNCTION_DECL_P (arg))
2319 if (! TREE_PUBLIC (arg))
2320 vis = VISIBILITY_ANON;
2321 else
2322 vis = DECL_VISIBILITY (arg);
2325 if (vis)
2326 constrain_visibility (decl, vis, true);
2330 /* Like c_determine_visibility, but with additional C++-specific
2331 behavior.
2333 Function-scope entities can rely on the function's visibility because
2334 it is set in start_preparsed_function.
2336 Class-scope entities cannot rely on the class's visibility until the end
2337 of the enclosing class definition.
2339 Note that because namespaces have multiple independent definitions,
2340 namespace visibility is handled elsewhere using the #pragma visibility
2341 machinery rather than by decorating the namespace declaration.
2343 The goal is for constraints from the type to give a diagnostic, and
2344 other constraints to be applied silently. */
2346 void
2347 determine_visibility (tree decl)
2349 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2351 /* Only relevant for names with external linkage. */
2352 if (!TREE_PUBLIC (decl))
2353 return;
2355 /* Cloned constructors and destructors get the same visibility as
2356 the underlying function. That should be set up in
2357 maybe_clone_body. */
2358 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2360 bool orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2361 enum symbol_visibility orig_visibility = DECL_VISIBILITY (decl);
2363 /* The decl may be a template instantiation, which could influence
2364 visibilty. */
2365 tree template_decl = NULL_TREE;
2366 if (TREE_CODE (decl) == TYPE_DECL)
2368 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2370 if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
2371 template_decl = decl;
2373 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2374 template_decl = decl;
2376 else if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
2377 template_decl = decl;
2379 /* If DECL is a member of a class, visibility specifiers on the
2380 class can influence the visibility of the DECL. */
2381 tree class_type = NULL_TREE;
2382 if (DECL_CLASS_SCOPE_P (decl))
2383 class_type = DECL_CONTEXT (decl);
2384 else
2386 /* Not a class member. */
2388 /* Virtual tables have DECL_CONTEXT set to their associated class,
2389 so they are automatically handled above. */
2390 gcc_assert (!VAR_P (decl)
2391 || !DECL_VTABLE_OR_VTT_P (decl));
2393 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2395 /* Local statics and classes get the visibility of their
2396 containing function by default, except that
2397 -fvisibility-inlines-hidden doesn't affect them. */
2398 tree fn = DECL_CONTEXT (decl);
2399 if (DECL_VISIBILITY_SPECIFIED (fn))
2401 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2402 DECL_VISIBILITY_SPECIFIED (decl) =
2403 DECL_VISIBILITY_SPECIFIED (fn);
2405 else
2407 if (DECL_CLASS_SCOPE_P (fn))
2408 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2409 else if (determine_hidden_inline (fn))
2411 DECL_VISIBILITY (decl) = default_visibility;
2412 DECL_VISIBILITY_SPECIFIED (decl) =
2413 visibility_options.inpragma;
2415 else
2417 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2418 DECL_VISIBILITY_SPECIFIED (decl) =
2419 DECL_VISIBILITY_SPECIFIED (fn);
2423 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2424 but have no TEMPLATE_INFO. Their containing template
2425 function does, and the local class could be constrained
2426 by that. */
2427 if (DECL_LANG_SPECIFIC (fn) && DECL_USE_TEMPLATE (fn))
2428 template_decl = fn;
2429 else if (template_decl)
2431 /* FN must be a regenerated lambda function, since they don't
2432 have template arguments. Find a containing non-lambda
2433 template instantiation. */
2434 tree ctx = fn;
2435 while (ctx && !get_template_info (ctx))
2436 ctx = get_containing_scope (ctx);
2437 template_decl = ctx;
2440 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2441 && flag_visibility_ms_compat)
2443 /* Under -fvisibility-ms-compat, types are visible by default,
2444 even though their contents aren't. */
2445 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2446 int underlying_vis = type_visibility (underlying_type);
2447 if (underlying_vis == VISIBILITY_ANON
2448 || (CLASS_TYPE_P (underlying_type)
2449 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2450 constrain_visibility (decl, underlying_vis, false);
2451 else
2452 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2454 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2456 /* tinfo visibility is based on the type it's for. */
2457 constrain_visibility
2458 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2460 /* Give the target a chance to override the visibility associated
2461 with DECL. */
2462 if (TREE_PUBLIC (decl)
2463 && !DECL_REALLY_EXTERN (decl)
2464 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2465 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2466 targetm.cxx.determine_class_data_visibility (decl);
2468 else if (template_decl)
2469 /* Template instantiations and specializations get visibility based
2470 on their template unless they override it with an attribute. */;
2471 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2473 if (determine_hidden_inline (decl))
2474 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2475 else
2477 /* Set default visibility to whatever the user supplied with
2478 #pragma GCC visibility or a namespace visibility attribute. */
2479 DECL_VISIBILITY (decl) = default_visibility;
2480 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2485 if (template_decl)
2487 /* If the specialization doesn't specify visibility, use the
2488 visibility from the template. */
2489 tree tinfo = get_template_info (template_decl);
2490 tree args = TI_ARGS (tinfo);
2491 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2492 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2493 : DECL_ATTRIBUTES (decl));
2495 if (args != error_mark_node)
2497 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2499 if (!DECL_VISIBILITY_SPECIFIED (decl))
2501 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2502 && determine_hidden_inline (decl))
2503 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2504 else
2506 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2507 DECL_VISIBILITY_SPECIFIED (decl)
2508 = DECL_VISIBILITY_SPECIFIED (pattern);
2512 if (args
2513 /* Template argument visibility outweighs #pragma or namespace
2514 visibility, but not an explicit attribute. */
2515 && !lookup_attribute ("visibility", attribs))
2517 int depth = TMPL_ARGS_DEPTH (args);
2518 if (DECL_VISIBILITY_SPECIFIED (decl))
2520 /* A class template member with explicit visibility
2521 overrides the class visibility, so we need to apply
2522 all the levels of template args directly. */
2523 int i;
2524 for (i = 1; i <= depth; ++i)
2526 tree lev = TMPL_ARGS_LEVEL (args, i);
2527 constrain_visibility_for_template (decl, lev);
2530 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2531 /* Limit visibility based on its template arguments. */
2532 constrain_visibility_for_template (decl, args);
2537 if (class_type)
2538 determine_visibility_from_class (decl, class_type);
2540 if (decl_anon_ns_mem_p (decl))
2541 /* Names in an anonymous namespace get internal linkage.
2542 This might change once we implement export. */
2543 constrain_visibility (decl, VISIBILITY_ANON, false);
2544 else if (TREE_CODE (decl) != TYPE_DECL)
2546 /* Propagate anonymity from type to decl. */
2547 int tvis = type_visibility (TREE_TYPE (decl));
2548 if (tvis == VISIBILITY_ANON
2549 || ! DECL_VISIBILITY_SPECIFIED (decl))
2550 constrain_visibility (decl, tvis, false);
2552 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2553 /* DR 757: A type without linkage shall not be used as the type of a
2554 variable or function with linkage, unless
2555 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2556 o the variable or function is not used (3.2 [basic.def.odr]) or is
2557 defined in the same translation unit.
2559 Since non-extern "C" decls need to be defined in the same
2560 translation unit, we can make the type internal. */
2561 constrain_visibility (decl, VISIBILITY_ANON, false);
2563 /* If visibility changed and DECL already has DECL_RTL, ensure
2564 symbol flags are updated. */
2565 if ((DECL_VISIBILITY (decl) != orig_visibility
2566 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2567 && ((VAR_P (decl) && TREE_STATIC (decl))
2568 || TREE_CODE (decl) == FUNCTION_DECL)
2569 && DECL_RTL_SET_P (decl))
2570 make_decl_rtl (decl);
2573 /* By default, static data members and function members receive
2574 the visibility of their containing class. */
2576 static void
2577 determine_visibility_from_class (tree decl, tree class_type)
2579 if (DECL_VISIBILITY_SPECIFIED (decl))
2580 return;
2582 if (determine_hidden_inline (decl))
2583 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2584 else
2586 /* Default to the class visibility. */
2587 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2588 DECL_VISIBILITY_SPECIFIED (decl)
2589 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2592 /* Give the target a chance to override the visibility associated
2593 with DECL. */
2594 if (VAR_P (decl)
2595 && (DECL_TINFO_P (decl)
2596 || (DECL_VTABLE_OR_VTT_P (decl)
2597 /* Construction virtual tables are not exported because
2598 they cannot be referred to from other object files;
2599 their name is not standardized by the ABI. */
2600 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2601 && TREE_PUBLIC (decl)
2602 && !DECL_REALLY_EXTERN (decl)
2603 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2604 targetm.cxx.determine_class_data_visibility (decl);
2607 /* Returns true iff DECL is an inline that should get hidden visibility
2608 because of -fvisibility-inlines-hidden. */
2610 static bool
2611 determine_hidden_inline (tree decl)
2613 return (visibility_options.inlines_hidden
2614 /* Don't do this for inline templates; specializations might not be
2615 inline, and we don't want them to inherit the hidden
2616 visibility. We'll set it here for all inline instantiations. */
2617 && !processing_template_decl
2618 && TREE_CODE (decl) == FUNCTION_DECL
2619 && DECL_DECLARED_INLINE_P (decl)
2620 && (! DECL_LANG_SPECIFIC (decl)
2621 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2624 /* Constrain the visibility of a class TYPE based on the visibility of its
2625 field types. Warn if any fields require lesser visibility. */
2627 void
2628 constrain_class_visibility (tree type)
2630 tree binfo;
2631 tree t;
2632 int i;
2634 int vis = type_visibility (type);
2636 if (vis == VISIBILITY_ANON
2637 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2638 return;
2640 /* Don't warn about visibility if the class has explicit visibility. */
2641 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2642 vis = VISIBILITY_INTERNAL;
2644 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2645 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node
2646 && !DECL_ARTIFICIAL (t))
2648 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2649 int subvis = type_visibility (ftype);
2651 if (subvis == VISIBILITY_ANON)
2653 if (!in_main_input_context())
2655 tree nlt = no_linkage_check (ftype, /*relaxed_p=*/false);
2656 if (nlt)
2658 if (same_type_p (TREE_TYPE (t), nlt))
2659 warning (OPT_Wsubobject_linkage, "\
2660 %qT has a field %qD whose type has no linkage",
2661 type, t);
2662 else
2663 warning (OPT_Wsubobject_linkage, "\
2664 %qT has a field %qD whose type depends on the type %qT which has no linkage",
2665 type, t, nlt);
2667 else
2668 warning (OPT_Wsubobject_linkage, "\
2669 %qT has a field %qD whose type uses the anonymous namespace",
2670 type, t);
2673 else if (MAYBE_CLASS_TYPE_P (ftype)
2674 && vis < VISIBILITY_HIDDEN
2675 && subvis >= VISIBILITY_HIDDEN)
2676 warning (OPT_Wattributes, "\
2677 %qT declared with greater visibility than the type of its field %qD",
2678 type, t);
2681 binfo = TYPE_BINFO (type);
2682 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2684 int subvis = type_visibility (TREE_TYPE (t));
2686 if (subvis == VISIBILITY_ANON)
2688 if (!in_main_input_context())
2690 tree nlt = no_linkage_check (TREE_TYPE (t), /*relaxed_p=*/false);
2691 if (nlt)
2693 if (same_type_p (TREE_TYPE (t), nlt))
2694 warning (OPT_Wsubobject_linkage, "\
2695 %qT has a base %qT whose type has no linkage",
2696 type, TREE_TYPE (t));
2697 else
2698 warning (OPT_Wsubobject_linkage, "\
2699 %qT has a base %qT whose type depends on the type %qT which has no linkage",
2700 type, TREE_TYPE (t), nlt);
2702 else
2703 warning (OPT_Wsubobject_linkage, "\
2704 %qT has a base %qT whose type uses the anonymous namespace",
2705 type, TREE_TYPE (t));
2708 else if (vis < VISIBILITY_HIDDEN
2709 && subvis >= VISIBILITY_HIDDEN)
2710 warning (OPT_Wattributes, "\
2711 %qT declared with greater visibility than its base %qT",
2712 type, TREE_TYPE (t));
2716 /* Functions for adjusting the visibility of a tagged type and its nested
2717 types and declarations when it gets a name for linkage purposes from a
2718 typedef. */
2720 static void bt_reset_linkage_1 (binding_entry, void *);
2721 static void bt_reset_linkage_2 (binding_entry, void *);
2723 /* First reset the visibility of all the types. */
2725 static void
2726 reset_type_linkage_1 (tree type)
2728 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2729 if (CLASS_TYPE_P (type))
2730 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2731 bt_reset_linkage_1, NULL);
2733 static void
2734 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2736 reset_type_linkage_1 (b->type);
2739 /* Then reset the visibility of any static data members or member
2740 functions that use those types. */
2742 static void
2743 reset_decl_linkage (tree decl)
2745 if (TREE_PUBLIC (decl))
2746 return;
2747 if (DECL_CLONED_FUNCTION_P (decl))
2748 return;
2749 TREE_PUBLIC (decl) = true;
2750 DECL_INTERFACE_KNOWN (decl) = false;
2751 determine_visibility (decl);
2752 tentative_decl_linkage (decl);
2755 static void
2756 reset_type_linkage_2 (tree type)
2758 if (CLASS_TYPE_P (type))
2760 if (tree vt = CLASSTYPE_VTABLES (type))
2762 tree name = mangle_vtbl_for_type (type);
2763 DECL_NAME (vt) = name;
2764 SET_DECL_ASSEMBLER_NAME (vt, name);
2765 reset_decl_linkage (vt);
2767 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2769 tree name = mangle_typeinfo_for_type (type);
2770 DECL_NAME (ti) = name;
2771 SET_DECL_ASSEMBLER_NAME (ti, name);
2772 TREE_TYPE (name) = type;
2773 reset_decl_linkage (ti);
2775 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2777 tree mem = STRIP_TEMPLATE (m);
2778 if (TREE_CODE (mem) == VAR_DECL || TREE_CODE (mem) == FUNCTION_DECL)
2779 reset_decl_linkage (mem);
2781 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2782 bt_reset_linkage_2, NULL);
2786 static void
2787 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2789 reset_type_linkage_2 (b->type);
2791 void
2792 reset_type_linkage (tree type)
2794 reset_type_linkage_1 (type);
2795 reset_type_linkage_2 (type);
2798 /* Set up our initial idea of what the linkage of DECL should be. */
2800 void
2801 tentative_decl_linkage (tree decl)
2803 if (DECL_INTERFACE_KNOWN (decl))
2804 /* We've already made a decision as to how this function will
2805 be handled. */;
2806 else if (vague_linkage_p (decl))
2808 if (TREE_CODE (decl) == FUNCTION_DECL
2809 && decl_defined_p (decl))
2811 DECL_EXTERNAL (decl) = 1;
2812 DECL_NOT_REALLY_EXTERN (decl) = 1;
2813 note_vague_linkage_fn (decl);
2814 /* A non-template inline function with external linkage will
2815 always be COMDAT. As we must eventually determine the
2816 linkage of all functions, and as that causes writes to
2817 the data mapped in from the PCH file, it's advantageous
2818 to mark the functions at this point. */
2819 if (DECL_DECLARED_INLINE_P (decl)
2820 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2821 || DECL_DEFAULTED_FN (decl)))
2823 /* This function must have external linkage, as
2824 otherwise DECL_INTERFACE_KNOWN would have been
2825 set. */
2826 gcc_assert (TREE_PUBLIC (decl));
2827 comdat_linkage (decl);
2828 DECL_INTERFACE_KNOWN (decl) = 1;
2831 else if (VAR_P (decl))
2832 maybe_commonize_var (decl);
2836 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2837 for DECL has not already been determined, do so now by setting
2838 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2839 function is called entities with vague linkage whose definitions
2840 are available must have TREE_PUBLIC set.
2842 If this function decides to place DECL in COMDAT, it will set
2843 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2844 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2845 callers defer that decision until it is clear that DECL is actually
2846 required. */
2848 void
2849 import_export_decl (tree decl)
2851 int emit_p;
2852 bool comdat_p;
2853 bool import_p;
2854 tree class_type = NULL_TREE;
2856 if (DECL_INTERFACE_KNOWN (decl))
2857 return;
2859 /* We cannot determine what linkage to give to an entity with vague
2860 linkage until the end of the file. For example, a virtual table
2861 for a class will be defined if and only if the key method is
2862 defined in this translation unit. As a further example, consider
2863 that when compiling a translation unit that uses PCH file with
2864 "-frepo" it would be incorrect to make decisions about what
2865 entities to emit when building the PCH; those decisions must be
2866 delayed until the repository information has been processed. */
2867 gcc_assert (at_eof);
2868 /* Object file linkage for explicit instantiations is handled in
2869 mark_decl_instantiated. For static variables in functions with
2870 vague linkage, maybe_commonize_var is used.
2872 Therefore, the only declarations that should be provided to this
2873 function are those with external linkage that are:
2875 * implicit instantiations of function templates
2877 * inline function
2879 * implicit instantiations of static data members of class
2880 templates
2882 * virtual tables
2884 * typeinfo objects
2886 Furthermore, all entities that reach this point must have a
2887 definition available in this translation unit.
2889 The following assertions check these conditions. */
2890 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2891 /* Any code that creates entities with TREE_PUBLIC cleared should
2892 also set DECL_INTERFACE_KNOWN. */
2893 gcc_assert (TREE_PUBLIC (decl));
2894 if (TREE_CODE (decl) == FUNCTION_DECL)
2895 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2896 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2897 || DECL_DECLARED_INLINE_P (decl));
2898 else
2899 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2900 || DECL_VTABLE_OR_VTT_P (decl)
2901 || DECL_TINFO_P (decl));
2902 /* Check that a definition of DECL is available in this translation
2903 unit. */
2904 gcc_assert (!DECL_REALLY_EXTERN (decl));
2906 /* Assume that DECL will not have COMDAT linkage. */
2907 comdat_p = false;
2908 /* Assume that DECL will not be imported into this translation
2909 unit. */
2910 import_p = false;
2912 /* See if the repository tells us whether or not to emit DECL in
2913 this translation unit. */
2914 emit_p = repo_emit_p (decl);
2915 if (emit_p == 0)
2916 import_p = true;
2917 else if (emit_p == 1)
2919 /* The repository indicates that this entity should be defined
2920 here. Make sure the back end honors that request. */
2921 mark_needed (decl);
2922 /* Output the definition as an ordinary strong definition. */
2923 DECL_EXTERNAL (decl) = 0;
2924 DECL_INTERFACE_KNOWN (decl) = 1;
2925 return;
2928 if (import_p)
2929 /* We have already decided what to do with this DECL; there is no
2930 need to check anything further. */
2932 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2934 class_type = DECL_CONTEXT (decl);
2935 import_export_class (class_type);
2936 if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2937 && CLASSTYPE_INTERFACE_ONLY (class_type))
2938 import_p = true;
2939 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2940 && !CLASSTYPE_USE_TEMPLATE (class_type)
2941 && CLASSTYPE_KEY_METHOD (class_type)
2942 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2943 /* The ABI requires that all virtual tables be emitted with
2944 COMDAT linkage. However, on systems where COMDAT symbols
2945 don't show up in the table of contents for a static
2946 archive, or on systems without weak symbols (where we
2947 approximate COMDAT linkage by using internal linkage), the
2948 linker will report errors about undefined symbols because
2949 it will not see the virtual table definition. Therefore,
2950 in the case that we know that the virtual table will be
2951 emitted in only one translation unit, we make the virtual
2952 table an ordinary definition with external linkage. */
2953 DECL_EXTERNAL (decl) = 0;
2954 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2956 /* CLASS_TYPE is being exported from this translation unit,
2957 so DECL should be defined here. */
2958 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2959 /* If a class is declared in a header with the "extern
2960 template" extension, then it will not be instantiated,
2961 even in translation units that would normally require
2962 it. Often such classes are explicitly instantiated in
2963 one translation unit. Therefore, the explicit
2964 instantiation must be made visible to other translation
2965 units. */
2966 DECL_EXTERNAL (decl) = 0;
2967 else
2969 /* The generic C++ ABI says that class data is always
2970 COMDAT, even if there is a key function. Some
2971 variants (e.g., the ARM EABI) says that class data
2972 only has COMDAT linkage if the class data might be
2973 emitted in more than one translation unit. When the
2974 key method can be inline and is inline, we still have
2975 to arrange for comdat even though
2976 class_data_always_comdat is false. */
2977 if (!CLASSTYPE_KEY_METHOD (class_type)
2978 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2979 || targetm.cxx.class_data_always_comdat ())
2981 /* The ABI requires COMDAT linkage. Normally, we
2982 only emit COMDAT things when they are needed;
2983 make sure that we realize that this entity is
2984 indeed needed. */
2985 comdat_p = true;
2986 mark_needed (decl);
2990 else if (!flag_implicit_templates
2991 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2992 import_p = true;
2993 else
2994 comdat_p = true;
2996 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2998 tree type = TREE_TYPE (DECL_NAME (decl));
2999 if (CLASS_TYPE_P (type))
3001 class_type = type;
3002 import_export_class (type);
3003 if (CLASSTYPE_INTERFACE_KNOWN (type)
3004 && TYPE_POLYMORPHIC_P (type)
3005 && CLASSTYPE_INTERFACE_ONLY (type)
3006 /* If -fno-rtti was specified, then we cannot be sure
3007 that RTTI information will be emitted with the
3008 virtual table of the class, so we must emit it
3009 wherever it is used. */
3010 && flag_rtti)
3011 import_p = true;
3012 else
3014 if (CLASSTYPE_INTERFACE_KNOWN (type)
3015 && !CLASSTYPE_INTERFACE_ONLY (type))
3017 comdat_p = (targetm.cxx.class_data_always_comdat ()
3018 || (CLASSTYPE_KEY_METHOD (type)
3019 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
3020 mark_needed (decl);
3021 if (!flag_weak)
3023 comdat_p = false;
3024 DECL_EXTERNAL (decl) = 0;
3027 else
3028 comdat_p = true;
3031 else
3032 comdat_p = true;
3034 else if (DECL_TEMPLOID_INSTANTIATION (decl))
3036 /* DECL is an implicit instantiation of a function or static
3037 data member. */
3038 if ((flag_implicit_templates
3039 && !flag_use_repository)
3040 || (flag_implicit_inline_templates
3041 && TREE_CODE (decl) == FUNCTION_DECL
3042 && DECL_DECLARED_INLINE_P (decl)))
3043 comdat_p = true;
3044 else
3045 /* If we are not implicitly generating templates, then mark
3046 this entity as undefined in this translation unit. */
3047 import_p = true;
3049 else if (DECL_FUNCTION_MEMBER_P (decl))
3051 if (!DECL_DECLARED_INLINE_P (decl))
3053 tree ctype = DECL_CONTEXT (decl);
3054 import_export_class (ctype);
3055 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
3057 DECL_NOT_REALLY_EXTERN (decl)
3058 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
3059 || (DECL_DECLARED_INLINE_P (decl)
3060 && ! flag_implement_inlines
3061 && !DECL_VINDEX (decl)));
3063 if (!DECL_NOT_REALLY_EXTERN (decl))
3064 DECL_EXTERNAL (decl) = 1;
3066 /* Always make artificials weak. */
3067 if (DECL_ARTIFICIAL (decl) && flag_weak)
3068 comdat_p = true;
3069 else
3070 maybe_make_one_only (decl);
3073 else
3074 comdat_p = true;
3076 else
3077 comdat_p = true;
3079 if (import_p)
3081 /* If we are importing DECL into this translation unit, mark is
3082 an undefined here. */
3083 DECL_EXTERNAL (decl) = 1;
3084 DECL_NOT_REALLY_EXTERN (decl) = 0;
3086 else if (comdat_p)
3088 /* If we decided to put DECL in COMDAT, mark it accordingly at
3089 this point. */
3090 comdat_linkage (decl);
3093 DECL_INTERFACE_KNOWN (decl) = 1;
3096 /* Return an expression that performs the destruction of DECL, which
3097 must be a VAR_DECL whose type has a non-trivial destructor, or is
3098 an array whose (innermost) elements have a non-trivial destructor. */
3100 tree
3101 build_cleanup (tree decl)
3103 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
3104 gcc_assert (clean != NULL_TREE);
3105 return clean;
3108 /* Returns the initialization guard variable for the variable DECL,
3109 which has static storage duration. */
3111 tree
3112 get_guard (tree decl)
3114 tree sname;
3115 tree guard;
3117 sname = mangle_guard_variable (decl);
3118 guard = get_global_binding (sname);
3119 if (! guard)
3121 tree guard_type;
3123 /* We use a type that is big enough to contain a mutex as well
3124 as an integer counter. */
3125 guard_type = targetm.cxx.guard_type ();
3126 guard = build_decl (DECL_SOURCE_LOCATION (decl),
3127 VAR_DECL, sname, guard_type);
3129 /* The guard should have the same linkage as what it guards. */
3130 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
3131 TREE_STATIC (guard) = TREE_STATIC (decl);
3132 DECL_COMMON (guard) = DECL_COMMON (decl);
3133 DECL_COMDAT (guard) = DECL_COMDAT (decl);
3134 CP_DECL_THREAD_LOCAL_P (guard) = CP_DECL_THREAD_LOCAL_P (decl);
3135 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
3136 if (DECL_ONE_ONLY (decl))
3137 make_decl_one_only (guard, cxx_comdat_group (guard));
3138 if (TREE_PUBLIC (decl))
3139 DECL_WEAK (guard) = DECL_WEAK (decl);
3140 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
3141 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
3143 DECL_ARTIFICIAL (guard) = 1;
3144 DECL_IGNORED_P (guard) = 1;
3145 TREE_USED (guard) = 1;
3146 pushdecl_top_level_and_finish (guard, NULL_TREE);
3148 return guard;
3151 /* Return an atomic load of src with the appropriate memory model. */
3153 static tree
3154 build_atomic_load_byte (tree src, HOST_WIDE_INT model)
3156 tree ptr_type = build_pointer_type (char_type_node);
3157 tree mem_model = build_int_cst (integer_type_node, model);
3158 tree t, addr, val;
3159 unsigned int size;
3160 int fncode;
3162 size = tree_to_uhwi (TYPE_SIZE_UNIT (char_type_node));
3164 fncode = BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1;
3165 t = builtin_decl_implicit ((enum built_in_function) fncode);
3167 addr = build1 (ADDR_EXPR, ptr_type, src);
3168 val = build_call_expr (t, 2, addr, mem_model);
3169 return val;
3172 /* Return those bits of the GUARD variable that should be set when the
3173 guarded entity is actually initialized. */
3175 static tree
3176 get_guard_bits (tree guard)
3178 if (!targetm.cxx.guard_mask_bit ())
3180 /* We only set the first byte of the guard, in order to leave room
3181 for a mutex in the high-order bits. */
3182 guard = build1 (ADDR_EXPR,
3183 build_pointer_type (TREE_TYPE (guard)),
3184 guard);
3185 guard = build1 (NOP_EXPR,
3186 build_pointer_type (char_type_node),
3187 guard);
3188 guard = build1 (INDIRECT_REF, char_type_node, guard);
3191 return guard;
3194 /* Return an expression which determines whether or not the GUARD
3195 variable has already been initialized. */
3197 tree
3198 get_guard_cond (tree guard, bool thread_safe)
3200 tree guard_value;
3202 if (!thread_safe)
3203 guard = get_guard_bits (guard);
3204 else
3205 guard = build_atomic_load_byte (guard, MEMMODEL_ACQUIRE);
3207 /* Mask off all but the low bit. */
3208 if (targetm.cxx.guard_mask_bit ())
3210 guard_value = integer_one_node;
3211 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3212 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3213 guard = cp_build_binary_op (input_location,
3214 BIT_AND_EXPR, guard, guard_value,
3215 tf_warning_or_error);
3218 guard_value = integer_zero_node;
3219 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3220 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3221 return cp_build_binary_op (input_location,
3222 EQ_EXPR, guard, guard_value,
3223 tf_warning_or_error);
3226 /* Return an expression which sets the GUARD variable, indicating that
3227 the variable being guarded has been initialized. */
3229 tree
3230 set_guard (tree guard)
3232 tree guard_init;
3234 /* Set the GUARD to one. */
3235 guard = get_guard_bits (guard);
3236 guard_init = integer_one_node;
3237 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3238 guard_init = fold_convert (TREE_TYPE (guard), guard_init);
3239 return cp_build_modify_expr (input_location, guard, NOP_EXPR, guard_init,
3240 tf_warning_or_error);
3243 /* Returns true iff we can tell that VAR does not have a dynamic
3244 initializer. */
3246 static bool
3247 var_defined_without_dynamic_init (tree var)
3249 /* If it's defined in another TU, we can't tell. */
3250 if (DECL_EXTERNAL (var))
3251 return false;
3252 /* If it has a non-trivial destructor, registering the destructor
3253 counts as dynamic initialization. */
3254 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3255 return false;
3256 /* If it's in this TU, its initializer has been processed, unless
3257 it's a case of self-initialization, then DECL_INITIALIZED_P is
3258 false while the initializer is handled by finish_id_expression. */
3259 if (!DECL_INITIALIZED_P (var))
3260 return false;
3261 /* If it has no initializer or a constant one, it's not dynamic. */
3262 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3263 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3266 /* Returns true iff VAR is a variable that needs uses to be
3267 wrapped for possible dynamic initialization. */
3269 static bool
3270 var_needs_tls_wrapper (tree var)
3272 return (!error_operand_p (var)
3273 && CP_DECL_THREAD_LOCAL_P (var)
3274 && !DECL_GNU_TLS_P (var)
3275 && !DECL_FUNCTION_SCOPE_P (var)
3276 && !var_defined_without_dynamic_init (var));
3279 /* Get the FUNCTION_DECL for the shared TLS init function for this
3280 translation unit. */
3282 static tree
3283 get_local_tls_init_fn (void)
3285 tree sname = get_identifier ("__tls_init");
3286 tree fn = get_global_binding (sname);
3287 if (!fn)
3289 fn = build_lang_decl (FUNCTION_DECL, sname,
3290 build_function_type (void_type_node,
3291 void_list_node));
3292 SET_DECL_LANGUAGE (fn, lang_c);
3293 TREE_PUBLIC (fn) = false;
3294 DECL_ARTIFICIAL (fn) = true;
3295 mark_used (fn);
3296 set_global_binding (fn);
3298 return fn;
3301 /* Get a FUNCTION_DECL for the init function for the thread_local
3302 variable VAR. The init function will be an alias to the function
3303 that initializes all the non-local TLS variables in the translation
3304 unit. The init function is only used by the wrapper function. */
3306 static tree
3307 get_tls_init_fn (tree var)
3309 /* Only C++11 TLS vars need this init fn. */
3310 if (!var_needs_tls_wrapper (var))
3311 return NULL_TREE;
3313 /* If -fno-extern-tls-init, assume that we don't need to call
3314 a tls init function for a variable defined in another TU. */
3315 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3316 return NULL_TREE;
3318 /* If the variable is internal, or if we can't generate aliases,
3319 call the local init function directly. */
3320 if (!TREE_PUBLIC (var) || !TARGET_SUPPORTS_ALIASES)
3321 return get_local_tls_init_fn ();
3323 tree sname = mangle_tls_init_fn (var);
3324 tree fn = get_global_binding (sname);
3325 if (!fn)
3327 fn = build_lang_decl (FUNCTION_DECL, sname,
3328 build_function_type (void_type_node,
3329 void_list_node));
3330 SET_DECL_LANGUAGE (fn, lang_c);
3331 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3332 DECL_ARTIFICIAL (fn) = true;
3333 DECL_COMDAT (fn) = DECL_COMDAT (var);
3334 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3335 if (DECL_ONE_ONLY (var))
3336 make_decl_one_only (fn, cxx_comdat_group (fn));
3337 if (TREE_PUBLIC (var))
3339 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3340 /* If the variable is defined somewhere else and might have static
3341 initialization, make the init function a weak reference. */
3342 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3343 || TYPE_HAS_CONSTEXPR_CTOR (obtype)
3344 || TYPE_HAS_TRIVIAL_DFLT (obtype))
3345 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3346 && DECL_EXTERNAL (var))
3347 declare_weak (fn);
3348 else
3349 DECL_WEAK (fn) = DECL_WEAK (var);
3351 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3352 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3353 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3354 DECL_IGNORED_P (fn) = 1;
3355 mark_used (fn);
3357 DECL_BEFRIENDING_CLASSES (fn) = var;
3359 set_global_binding (fn);
3361 return fn;
3364 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3365 variable VAR. The wrapper function calls the init function (if any) for
3366 VAR and then returns a reference to VAR. The wrapper function is used
3367 in place of VAR everywhere VAR is mentioned. */
3369 tree
3370 get_tls_wrapper_fn (tree var)
3372 /* Only C++11 TLS vars need this wrapper fn. */
3373 if (!var_needs_tls_wrapper (var))
3374 return NULL_TREE;
3376 tree sname = mangle_tls_wrapper_fn (var);
3377 tree fn = get_global_binding (sname);
3378 if (!fn)
3380 /* A named rvalue reference is an lvalue, so the wrapper should
3381 always return an lvalue reference. */
3382 tree type = non_reference (TREE_TYPE (var));
3383 type = build_reference_type (type);
3384 tree fntype = build_function_type (type, void_list_node);
3385 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3386 SET_DECL_LANGUAGE (fn, lang_c);
3387 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3388 DECL_ARTIFICIAL (fn) = true;
3389 DECL_IGNORED_P (fn) = 1;
3390 /* The wrapper is inline and emitted everywhere var is used. */
3391 DECL_DECLARED_INLINE_P (fn) = true;
3392 if (TREE_PUBLIC (var))
3394 comdat_linkage (fn);
3395 #ifdef HAVE_GAS_HIDDEN
3396 /* Make the wrapper bind locally; there's no reason to share
3397 the wrapper between multiple shared objects. */
3398 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3399 DECL_VISIBILITY_SPECIFIED (fn) = true;
3400 #endif
3402 if (!TREE_PUBLIC (fn))
3403 DECL_INTERFACE_KNOWN (fn) = true;
3404 mark_used (fn);
3405 note_vague_linkage_fn (fn);
3407 #if 0
3408 /* We want CSE to commonize calls to the wrapper, but marking it as
3409 pure is unsafe since it has side-effects. I guess we need a new
3410 ECF flag even weaker than ECF_PURE. FIXME! */
3411 DECL_PURE_P (fn) = true;
3412 #endif
3414 DECL_BEFRIENDING_CLASSES (fn) = var;
3416 set_global_binding (fn);
3418 return fn;
3421 /* At EOF, generate the definition for the TLS wrapper function FN:
3423 T& var_wrapper() {
3424 if (init_fn) init_fn();
3425 return var;
3426 } */
3428 static void
3429 generate_tls_wrapper (tree fn)
3431 tree var = DECL_BEFRIENDING_CLASSES (fn);
3433 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3434 tree body = begin_function_body ();
3435 /* Only call the init fn if there might be one. */
3436 if (tree init_fn = get_tls_init_fn (var))
3438 tree if_stmt = NULL_TREE;
3439 /* If init_fn is a weakref, make sure it exists before calling. */
3440 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3442 if_stmt = begin_if_stmt ();
3443 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3444 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3445 NE_EXPR, addr, nullptr_node,
3446 tf_warning_or_error);
3447 finish_if_stmt_cond (cond, if_stmt);
3449 finish_expr_stmt (build_cxx_call
3450 (init_fn, 0, NULL, tf_warning_or_error));
3451 if (if_stmt)
3453 finish_then_clause (if_stmt);
3454 finish_if_stmt (if_stmt);
3457 else
3458 /* If there's no initialization, the wrapper is a constant function. */
3459 TREE_READONLY (fn) = true;
3460 finish_return_stmt (convert_from_reference (var));
3461 finish_function_body (body);
3462 expand_or_defer_fn (finish_function (/*inline_p=*/false));
3465 /* Start the process of running a particular set of global constructors
3466 or destructors. Subroutine of do_[cd]tors. Also called from
3467 vtv_start_verification_constructor_init_function. */
3469 static tree
3470 start_objects (int method_type, int initp)
3472 tree body;
3473 tree fndecl;
3474 char type[14];
3476 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3478 if (initp != DEFAULT_INIT_PRIORITY)
3480 char joiner;
3482 #ifdef JOINER
3483 joiner = JOINER;
3484 #else
3485 joiner = '_';
3486 #endif
3488 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3490 else
3491 sprintf (type, "sub_%c", method_type);
3493 fndecl = build_lang_decl (FUNCTION_DECL,
3494 get_file_function_name (type),
3495 build_function_type_list (void_type_node,
3496 NULL_TREE));
3497 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3499 TREE_PUBLIC (current_function_decl) = 0;
3501 /* Mark as artificial because it's not explicitly in the user's
3502 source code. */
3503 DECL_ARTIFICIAL (current_function_decl) = 1;
3505 /* Mark this declaration as used to avoid spurious warnings. */
3506 TREE_USED (current_function_decl) = 1;
3508 /* Mark this function as a global constructor or destructor. */
3509 if (method_type == 'I')
3510 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3511 else
3512 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3514 body = begin_compound_stmt (BCS_FN_BODY);
3516 return body;
3519 /* Finish the process of running a particular set of global constructors
3520 or destructors. Subroutine of do_[cd]tors. */
3522 static void
3523 finish_objects (int method_type, int initp, tree body)
3525 tree fn;
3527 /* Finish up. */
3528 finish_compound_stmt (body);
3529 fn = finish_function (/*inline_p=*/false);
3531 if (method_type == 'I')
3533 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3534 decl_init_priority_insert (fn, initp);
3536 else
3538 DECL_STATIC_DESTRUCTOR (fn) = 1;
3539 decl_fini_priority_insert (fn, initp);
3542 expand_or_defer_fn (fn);
3545 /* The names of the parameters to the function created to handle
3546 initializations and destructions for objects with static storage
3547 duration. */
3548 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3549 #define PRIORITY_IDENTIFIER "__priority"
3551 /* The name of the function we create to handle initializations and
3552 destructions for objects with static storage duration. */
3553 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3555 /* The declaration for the __INITIALIZE_P argument. */
3556 static GTY(()) tree initialize_p_decl;
3558 /* The declaration for the __PRIORITY argument. */
3559 static GTY(()) tree priority_decl;
3561 /* The declaration for the static storage duration function. */
3562 static GTY(()) tree ssdf_decl;
3564 /* All the static storage duration functions created in this
3565 translation unit. */
3566 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3568 /* A map from priority levels to information about that priority
3569 level. There may be many such levels, so efficient lookup is
3570 important. */
3571 static splay_tree priority_info_map;
3573 /* Begins the generation of the function that will handle all
3574 initialization and destruction of objects with static storage
3575 duration. The function generated takes two parameters of type
3576 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3577 nonzero, it performs initializations. Otherwise, it performs
3578 destructions. It only performs those initializations or
3579 destructions with the indicated __PRIORITY. The generated function
3580 returns no value.
3582 It is assumed that this function will only be called once per
3583 translation unit. */
3585 static tree
3586 start_static_storage_duration_function (unsigned count)
3588 tree type;
3589 tree body;
3590 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3592 /* Create the identifier for this function. It will be of the form
3593 SSDF_IDENTIFIER_<number>. */
3594 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3596 type = build_function_type_list (void_type_node,
3597 integer_type_node, integer_type_node,
3598 NULL_TREE);
3600 /* Create the FUNCTION_DECL itself. */
3601 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3602 get_identifier (id),
3603 type);
3604 TREE_PUBLIC (ssdf_decl) = 0;
3605 DECL_ARTIFICIAL (ssdf_decl) = 1;
3607 /* Put this function in the list of functions to be called from the
3608 static constructors and destructors. */
3609 if (!ssdf_decls)
3611 vec_alloc (ssdf_decls, 32);
3613 /* Take this opportunity to initialize the map from priority
3614 numbers to information about that priority level. */
3615 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3616 /*delete_key_fn=*/0,
3617 /*delete_value_fn=*/
3618 (splay_tree_delete_value_fn)
3619 (void (*) (void)) free);
3621 /* We always need to generate functions for the
3622 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3623 priorities later, we'll be sure to find the
3624 DEFAULT_INIT_PRIORITY. */
3625 get_priority_info (DEFAULT_INIT_PRIORITY);
3628 vec_safe_push (ssdf_decls, ssdf_decl);
3630 /* Create the argument list. */
3631 initialize_p_decl = cp_build_parm_decl
3632 (ssdf_decl, get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3633 TREE_USED (initialize_p_decl) = 1;
3634 priority_decl = cp_build_parm_decl
3635 (ssdf_decl, get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3636 TREE_USED (priority_decl) = 1;
3638 DECL_CHAIN (initialize_p_decl) = priority_decl;
3639 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3641 /* Put the function in the global scope. */
3642 pushdecl (ssdf_decl);
3644 /* Start the function itself. This is equivalent to declaring the
3645 function as:
3647 static void __ssdf (int __initialize_p, init __priority_p);
3649 It is static because we only need to call this function from the
3650 various constructor and destructor functions for this module. */
3651 start_preparsed_function (ssdf_decl,
3652 /*attrs=*/NULL_TREE,
3653 SF_PRE_PARSED);
3655 /* Set up the scope of the outermost block in the function. */
3656 body = begin_compound_stmt (BCS_FN_BODY);
3658 return body;
3661 /* Finish the generation of the function which performs initialization
3662 and destruction of objects with static storage duration. After
3663 this point, no more such objects can be created. */
3665 static void
3666 finish_static_storage_duration_function (tree body)
3668 /* Close out the function. */
3669 finish_compound_stmt (body);
3670 expand_or_defer_fn (finish_function (/*inline_p=*/false));
3673 /* Return the information about the indicated PRIORITY level. If no
3674 code to handle this level has yet been generated, generate the
3675 appropriate prologue. */
3677 static priority_info
3678 get_priority_info (int priority)
3680 priority_info pi;
3681 splay_tree_node n;
3683 n = splay_tree_lookup (priority_info_map,
3684 (splay_tree_key) priority);
3685 if (!n)
3687 /* Create a new priority information structure, and insert it
3688 into the map. */
3689 pi = XNEW (struct priority_info_s);
3690 pi->initializations_p = 0;
3691 pi->destructions_p = 0;
3692 splay_tree_insert (priority_info_map,
3693 (splay_tree_key) priority,
3694 (splay_tree_value) pi);
3696 else
3697 pi = (priority_info) n->value;
3699 return pi;
3702 /* The effective initialization priority of a DECL. */
3704 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3705 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3706 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3708 /* Whether a DECL needs a guard to protect it against multiple
3709 initialization. */
3711 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3712 || DECL_ONE_ONLY (decl) \
3713 || DECL_WEAK (decl)))
3715 /* Called from one_static_initialization_or_destruction(),
3716 via walk_tree.
3717 Walks the initializer list of a global variable and looks for
3718 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3719 and that have their DECL_CONTEXT() == NULL.
3720 For each such temporary variable, set their DECL_CONTEXT() to
3721 the current function. This is necessary because otherwise
3722 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3723 when trying to refer to a temporary variable that does not have
3724 it's DECL_CONTECT() properly set. */
3725 static tree
3726 fix_temporary_vars_context_r (tree *node,
3727 int * /*unused*/,
3728 void * /*unused1*/)
3730 gcc_assert (current_function_decl);
3732 if (TREE_CODE (*node) == BIND_EXPR)
3734 tree var;
3736 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3737 if (VAR_P (var)
3738 && !DECL_NAME (var)
3739 && DECL_ARTIFICIAL (var)
3740 && !DECL_CONTEXT (var))
3741 DECL_CONTEXT (var) = current_function_decl;
3744 return NULL_TREE;
3747 /* Set up to handle the initialization or destruction of DECL. If
3748 INITP is nonzero, we are initializing the variable. Otherwise, we
3749 are destroying it. */
3751 static void
3752 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3754 tree guard_if_stmt = NULL_TREE;
3755 tree guard;
3757 /* If we are supposed to destruct and there's a trivial destructor,
3758 nothing has to be done. */
3759 if (!initp
3760 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3761 return;
3763 /* Trick the compiler into thinking we are at the file and line
3764 where DECL was declared so that error-messages make sense, and so
3765 that the debugger will show somewhat sensible file and line
3766 information. */
3767 input_location = DECL_SOURCE_LOCATION (decl);
3769 /* Make sure temporary variables in the initialiser all have
3770 their DECL_CONTEXT() set to a value different from NULL_TREE.
3771 This can happen when global variables initializers are built.
3772 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3773 the temporary variables that might have been generated in the
3774 accompanying initializers is NULL_TREE, meaning the variables have been
3775 declared in the global namespace.
3776 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3777 of the temporaries are set to the current function decl. */
3778 cp_walk_tree_without_duplicates (&init,
3779 fix_temporary_vars_context_r,
3780 NULL);
3782 /* Because of:
3784 [class.access.spec]
3786 Access control for implicit calls to the constructors,
3787 the conversion functions, or the destructor called to
3788 create and destroy a static data member is performed as
3789 if these calls appeared in the scope of the member's
3790 class.
3792 we pretend we are in a static member function of the class of
3793 which the DECL is a member. */
3794 if (member_p (decl))
3796 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3797 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3800 /* Assume we don't need a guard. */
3801 guard = NULL_TREE;
3802 /* We need a guard if this is an object with external linkage that
3803 might be initialized in more than one place. (For example, a
3804 static data member of a template, when the data member requires
3805 construction.) */
3806 if (NEEDS_GUARD_P (decl))
3808 tree guard_cond;
3810 guard = get_guard (decl);
3812 /* When using __cxa_atexit, we just check the GUARD as we would
3813 for a local static. */
3814 if (flag_use_cxa_atexit)
3816 /* When using __cxa_atexit, we never try to destroy
3817 anything from a static destructor. */
3818 gcc_assert (initp);
3819 guard_cond = get_guard_cond (guard, false);
3821 /* If we don't have __cxa_atexit, then we will be running
3822 destructors from .fini sections, or their equivalents. So,
3823 we need to know how many times we've tried to initialize this
3824 object. We do initializations only if the GUARD is zero,
3825 i.e., if we are the first to initialize the variable. We do
3826 destructions only if the GUARD is one, i.e., if we are the
3827 last to destroy the variable. */
3828 else if (initp)
3829 guard_cond
3830 = cp_build_binary_op (input_location,
3831 EQ_EXPR,
3832 cp_build_unary_op (PREINCREMENT_EXPR,
3833 guard,
3834 /*noconvert=*/true,
3835 tf_warning_or_error),
3836 integer_one_node,
3837 tf_warning_or_error);
3838 else
3839 guard_cond
3840 = cp_build_binary_op (input_location,
3841 EQ_EXPR,
3842 cp_build_unary_op (PREDECREMENT_EXPR,
3843 guard,
3844 /*noconvert=*/true,
3845 tf_warning_or_error),
3846 integer_zero_node,
3847 tf_warning_or_error);
3849 guard_if_stmt = begin_if_stmt ();
3850 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3854 /* If we're using __cxa_atexit, we have not already set the GUARD,
3855 so we must do so now. */
3856 if (guard && initp && flag_use_cxa_atexit)
3857 finish_expr_stmt (set_guard (guard));
3859 /* Perform the initialization or destruction. */
3860 if (initp)
3862 if (init)
3864 finish_expr_stmt (init);
3865 if (sanitize_flags_p (SANITIZE_ADDRESS, decl))
3867 varpool_node *vnode = varpool_node::get (decl);
3868 if (vnode)
3869 vnode->dynamically_initialized = 1;
3873 /* If we're using __cxa_atexit, register a function that calls the
3874 destructor for the object. */
3875 if (flag_use_cxa_atexit)
3876 finish_expr_stmt (register_dtor_fn (decl));
3878 else
3879 finish_expr_stmt (build_cleanup (decl));
3881 /* Finish the guard if-stmt, if necessary. */
3882 if (guard)
3884 finish_then_clause (guard_if_stmt);
3885 finish_if_stmt (guard_if_stmt);
3888 /* Now that we're done with DECL we don't need to pretend to be a
3889 member of its class any longer. */
3890 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3891 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3894 /* Generate code to do the initialization or destruction of the decls in VARS,
3895 a TREE_LIST of VAR_DECL with static storage duration.
3896 Whether initialization or destruction is performed is specified by INITP. */
3898 static void
3899 do_static_initialization_or_destruction (tree vars, bool initp)
3901 tree node, init_if_stmt, cond;
3903 /* Build the outer if-stmt to check for initialization or destruction. */
3904 init_if_stmt = begin_if_stmt ();
3905 cond = initp ? integer_one_node : integer_zero_node;
3906 cond = cp_build_binary_op (input_location,
3907 EQ_EXPR,
3908 initialize_p_decl,
3909 cond,
3910 tf_warning_or_error);
3911 finish_if_stmt_cond (cond, init_if_stmt);
3913 /* To make sure dynamic construction doesn't access globals from other
3914 compilation units where they might not be yet constructed, for
3915 -fsanitize=address insert __asan_before_dynamic_init call that
3916 prevents access to either all global variables that need construction
3917 in other compilation units, or at least those that haven't been
3918 initialized yet. Variables that need dynamic construction in
3919 the current compilation unit are kept accessible. */
3920 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
3921 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3923 node = vars;
3924 do {
3925 tree decl = TREE_VALUE (node);
3926 tree priority_if_stmt;
3927 int priority;
3928 priority_info pi;
3930 /* If we don't need a destructor, there's nothing to do. Avoid
3931 creating a possibly empty if-stmt. */
3932 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3934 node = TREE_CHAIN (node);
3935 continue;
3938 /* Remember that we had an initialization or finalization at this
3939 priority. */
3940 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3941 pi = get_priority_info (priority);
3942 if (initp)
3943 pi->initializations_p = 1;
3944 else
3945 pi->destructions_p = 1;
3947 /* Conditionalize this initialization on being in the right priority
3948 and being initializing/finalizing appropriately. */
3949 priority_if_stmt = begin_if_stmt ();
3950 cond = cp_build_binary_op (input_location,
3951 EQ_EXPR,
3952 priority_decl,
3953 build_int_cst (NULL_TREE, priority),
3954 tf_warning_or_error);
3955 finish_if_stmt_cond (cond, priority_if_stmt);
3957 /* Process initializers with same priority. */
3958 for (; node
3959 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3960 node = TREE_CHAIN (node))
3961 /* Do one initialization or destruction. */
3962 one_static_initialization_or_destruction (TREE_VALUE (node),
3963 TREE_PURPOSE (node), initp);
3965 /* Finish up the priority if-stmt body. */
3966 finish_then_clause (priority_if_stmt);
3967 finish_if_stmt (priority_if_stmt);
3969 } while (node);
3971 /* Revert what __asan_before_dynamic_init did by calling
3972 __asan_after_dynamic_init. */
3973 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
3974 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3976 /* Finish up the init/destruct if-stmt body. */
3977 finish_then_clause (init_if_stmt);
3978 finish_if_stmt (init_if_stmt);
3981 /* VARS is a list of variables with static storage duration which may
3982 need initialization and/or finalization. Remove those variables
3983 that don't really need to be initialized or finalized, and return
3984 the resulting list. The order in which the variables appear in
3985 VARS is in reverse order of the order in which they should actually
3986 be initialized. The list we return is in the unreversed order;
3987 i.e., the first variable should be initialized first. */
3989 static tree
3990 prune_vars_needing_no_initialization (tree *vars)
3992 tree *var = vars;
3993 tree result = NULL_TREE;
3995 while (*var)
3997 tree t = *var;
3998 tree decl = TREE_VALUE (t);
3999 tree init = TREE_PURPOSE (t);
4001 /* Deal gracefully with error. */
4002 if (error_operand_p (decl))
4004 var = &TREE_CHAIN (t);
4005 continue;
4008 /* The only things that can be initialized are variables. */
4009 gcc_assert (VAR_P (decl));
4011 /* If this object is not defined, we don't need to do anything
4012 here. */
4013 if (DECL_EXTERNAL (decl))
4015 var = &TREE_CHAIN (t);
4016 continue;
4019 /* Also, if the initializer already contains errors, we can bail
4020 out now. */
4021 if (init && TREE_CODE (init) == TREE_LIST
4022 && value_member (error_mark_node, init))
4024 var = &TREE_CHAIN (t);
4025 continue;
4028 /* This variable is going to need initialization and/or
4029 finalization, so we add it to the list. */
4030 *var = TREE_CHAIN (t);
4031 TREE_CHAIN (t) = result;
4032 result = t;
4035 return result;
4038 /* Make sure we have told the back end about all the variables in
4039 VARS. */
4041 static void
4042 write_out_vars (tree vars)
4044 tree v;
4046 for (v = vars; v; v = TREE_CHAIN (v))
4048 tree var = TREE_VALUE (v);
4049 if (!var_finalized_p (var))
4051 import_export_decl (var);
4052 rest_of_decl_compilation (var, 1, 1);
4057 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
4058 (otherwise) that will initialize all global objects with static
4059 storage duration having the indicated PRIORITY. */
4061 static void
4062 generate_ctor_or_dtor_function (bool constructor_p, int priority,
4063 location_t *locus)
4065 char function_key;
4066 tree fndecl;
4067 tree body;
4068 size_t i;
4070 input_location = *locus;
4071 /* ??? */
4072 /* Was: locus->line++; */
4074 /* We use `I' to indicate initialization and `D' to indicate
4075 destruction. */
4076 function_key = constructor_p ? 'I' : 'D';
4078 /* We emit the function lazily, to avoid generating empty
4079 global constructors and destructors. */
4080 body = NULL_TREE;
4082 /* For Objective-C++, we may need to initialize metadata found in this module.
4083 This must be done _before_ any other static initializations. */
4084 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
4085 && constructor_p && objc_static_init_needed_p ())
4087 body = start_objects (function_key, priority);
4088 objc_generate_static_init_call (NULL_TREE);
4091 /* Call the static storage duration function with appropriate
4092 arguments. */
4093 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
4095 /* Calls to pure or const functions will expand to nothing. */
4096 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
4098 tree call;
4100 if (! body)
4101 body = start_objects (function_key, priority);
4103 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
4104 build_int_cst (NULL_TREE,
4105 constructor_p),
4106 build_int_cst (NULL_TREE,
4107 priority),
4108 NULL_TREE);
4109 finish_expr_stmt (call);
4113 /* Close out the function. */
4114 if (body)
4115 finish_objects (function_key, priority, body);
4118 /* Generate constructor and destructor functions for the priority
4119 indicated by N. */
4121 static int
4122 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
4124 location_t *locus = (location_t *) data;
4125 int priority = (int) n->key;
4126 priority_info pi = (priority_info) n->value;
4128 /* Generate the functions themselves, but only if they are really
4129 needed. */
4130 if (pi->initializations_p)
4131 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
4132 if (pi->destructions_p)
4133 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
4135 /* Keep iterating. */
4136 return 0;
4139 /* Return C++ property of T, based on given operation OP. */
4141 static int
4142 cpp_check (tree t, cpp_operation op)
4144 switch (op)
4146 case HAS_DEPENDENT_TEMPLATE_ARGS:
4148 tree ti = CLASSTYPE_TEMPLATE_INFO (t);
4149 if (!ti)
4150 return 0;
4151 ++processing_template_decl;
4152 const bool dep = any_dependent_template_arguments_p (TI_ARGS (ti));
4153 --processing_template_decl;
4154 return dep;
4156 case IS_ABSTRACT:
4157 return DECL_PURE_VIRTUAL_P (t);
4158 case IS_CONSTRUCTOR:
4159 return DECL_CONSTRUCTOR_P (t);
4160 case IS_DESTRUCTOR:
4161 return DECL_DESTRUCTOR_P (t);
4162 case IS_COPY_CONSTRUCTOR:
4163 return DECL_COPY_CONSTRUCTOR_P (t);
4164 case IS_MOVE_CONSTRUCTOR:
4165 return DECL_MOVE_CONSTRUCTOR_P (t);
4166 case IS_TEMPLATE:
4167 return TREE_CODE (t) == TEMPLATE_DECL;
4168 case IS_TRIVIAL:
4169 return trivial_type_p (t);
4170 default:
4171 return 0;
4175 /* Collect source file references recursively, starting from NAMESPC. */
4177 static void
4178 collect_source_refs (tree namespc)
4180 /* Iterate over names in this name space. */
4181 for (tree t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4182 if (DECL_IS_BUILTIN (t))
4184 else if (TREE_CODE (t) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (t))
4185 collect_source_refs (t);
4186 else
4187 collect_source_ref (DECL_SOURCE_FILE (t));
4190 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4191 starting from NAMESPC. */
4193 static void
4194 collect_ada_namespace (tree namespc, const char *source_file)
4196 tree decl = NAMESPACE_LEVEL (namespc)->names;
4198 /* Collect decls from this namespace. This will skip
4199 NAMESPACE_DECLs (both aliases and regular, it cannot tell). */
4200 collect_ada_nodes (decl, source_file);
4202 /* Now scan for namespace children, and dump them. */
4203 for (; decl; decl = TREE_CHAIN (decl))
4204 if (TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl))
4205 collect_ada_namespace (decl, source_file);
4208 /* Returns true iff there is a definition available for variable or
4209 function DECL. */
4211 bool
4212 decl_defined_p (tree decl)
4214 if (TREE_CODE (decl) == FUNCTION_DECL)
4215 return (DECL_INITIAL (decl) != NULL_TREE
4216 /* A pending instantiation of a friend temploid is defined. */
4217 || (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
4218 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4219 (DECL_TI_TEMPLATE (decl)))));
4220 else
4222 gcc_assert (VAR_P (decl));
4223 return !DECL_EXTERNAL (decl);
4227 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4229 [expr.const]
4231 An integral constant-expression can only involve ... const
4232 variables of integral or enumeration types initialized with
4233 constant expressions ...
4235 C++0x also allows constexpr variables and temporaries initialized
4236 with constant expressions. We handle the former here, but the latter
4237 are just folded away in cxx_eval_constant_expression.
4239 The standard does not require that the expression be non-volatile.
4240 G++ implements the proposed correction in DR 457. */
4242 bool
4243 decl_constant_var_p (tree decl)
4245 if (!decl_maybe_constant_var_p (decl))
4246 return false;
4248 /* We don't know if a template static data member is initialized with
4249 a constant expression until we instantiate its initializer. Even
4250 in the case of a constexpr variable, we can't treat it as a
4251 constant until its initializer is complete in case it's used in
4252 its own initializer. */
4253 maybe_instantiate_decl (decl);
4254 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4257 /* Returns true if DECL could be a symbolic constant variable, depending on
4258 its initializer. */
4260 bool
4261 decl_maybe_constant_var_p (tree decl)
4263 tree type = TREE_TYPE (decl);
4264 if (!VAR_P (decl))
4265 return false;
4266 if (DECL_DECLARED_CONSTEXPR_P (decl))
4267 return true;
4268 if (DECL_HAS_VALUE_EXPR_P (decl))
4269 /* A proxy isn't constant. */
4270 return false;
4271 if (TREE_CODE (type) == REFERENCE_TYPE)
4272 /* References can be constant. */;
4273 else if (CP_TYPE_CONST_NON_VOLATILE_P (type)
4274 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4275 /* And const integers. */;
4276 else
4277 return false;
4279 if (DECL_INITIAL (decl)
4280 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
4281 /* We know the initializer, and it isn't constant. */
4282 return false;
4283 else
4284 return true;
4287 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4288 called from grokfndecl and grokvardecl; in all modes it is called from
4289 cp_write_global_declarations. */
4291 void
4292 no_linkage_error (tree decl)
4294 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4295 /* In C++11 it's ok if the decl is defined. */
4296 return;
4297 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4298 if (t == NULL_TREE)
4299 /* The type that got us on no_linkage_decls must have gotten a name for
4300 linkage purposes. */;
4301 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4302 /* The type might end up having a typedef name for linkage purposes. */
4303 vec_safe_push (no_linkage_decls, decl);
4304 else if (TYPE_UNNAMED_P (t))
4306 bool d = false;
4307 if (cxx_dialect >= cxx11)
4308 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4309 "unnamed type, is used but never defined", decl);
4310 else if (DECL_EXTERN_C_P (decl))
4311 /* Allow this; it's pretty common in C. */;
4312 else if (VAR_P (decl))
4313 /* DRs 132, 319 and 389 seem to indicate types with
4314 no linkage can only be used to declare extern "C"
4315 entities. Since it's not always an error in the
4316 ISO C++ 90 Standard, we only issue a warning. */
4317 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "unnamed type "
4318 "with no linkage used to declare variable %q#D with "
4319 "linkage", decl);
4320 else
4321 d = permerror (DECL_SOURCE_LOCATION (decl), "unnamed type with no "
4322 "linkage used to declare function %q#D with linkage",
4323 decl);
4324 if (d && is_typedef_decl (TYPE_NAME (t)))
4325 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4326 "to the unqualified type, so it is not used for linkage",
4327 TYPE_NAME (t));
4329 else if (cxx_dialect >= cxx11)
4331 if (VAR_P (decl) || !DECL_PURE_VIRTUAL_P (decl))
4332 permerror (DECL_SOURCE_LOCATION (decl),
4333 "%q#D, declared using local type "
4334 "%qT, is used but never defined", decl, t);
4336 else if (VAR_P (decl))
4337 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4338 "used to declare variable %q#D with linkage", t, decl);
4339 else
4340 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4341 "to declare function %q#D with linkage", t, decl);
4344 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4346 static void
4347 collect_all_refs (const char *source_file)
4349 collect_ada_namespace (global_namespace, source_file);
4352 /* Clear DECL_EXTERNAL for NODE. */
4354 static bool
4355 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4357 DECL_EXTERNAL (node->decl) = 0;
4358 return false;
4361 /* Build up the function to run dynamic initializers for thread_local
4362 variables in this translation unit and alias the init functions for the
4363 individual variables to it. */
4365 static void
4366 handle_tls_init (void)
4368 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4369 if (vars == NULL_TREE)
4370 return;
4372 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4374 write_out_vars (vars);
4376 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4377 boolean_type_node);
4378 TREE_PUBLIC (guard) = false;
4379 TREE_STATIC (guard) = true;
4380 DECL_ARTIFICIAL (guard) = true;
4381 DECL_IGNORED_P (guard) = true;
4382 TREE_USED (guard) = true;
4383 CP_DECL_THREAD_LOCAL_P (guard) = true;
4384 set_decl_tls_model (guard, decl_default_tls_model (guard));
4385 pushdecl_top_level_and_finish (guard, NULL_TREE);
4387 tree fn = get_local_tls_init_fn ();
4388 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4389 tree body = begin_function_body ();
4390 tree if_stmt = begin_if_stmt ();
4391 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4392 tf_warning_or_error);
4393 finish_if_stmt_cond (cond, if_stmt);
4394 finish_expr_stmt (cp_build_modify_expr (loc, guard, NOP_EXPR,
4395 boolean_true_node,
4396 tf_warning_or_error));
4397 for (; vars; vars = TREE_CHAIN (vars))
4399 tree var = TREE_VALUE (vars);
4400 tree init = TREE_PURPOSE (vars);
4401 one_static_initialization_or_destruction (var, init, true);
4403 /* Output init aliases even with -fno-extern-tls-init. */
4404 if (TARGET_SUPPORTS_ALIASES && TREE_PUBLIC (var))
4406 tree single_init_fn = get_tls_init_fn (var);
4407 if (single_init_fn == NULL_TREE)
4408 continue;
4409 cgraph_node *alias
4410 = cgraph_node::get_create (fn)->create_same_body_alias
4411 (single_init_fn, fn);
4412 gcc_assert (alias != NULL);
4416 finish_then_clause (if_stmt);
4417 finish_if_stmt (if_stmt);
4418 finish_function_body (body);
4419 expand_or_defer_fn (finish_function (/*inline_p=*/false));
4422 /* We're at the end of compilation, so generate any mangling aliases that
4423 we've been saving up, if DECL is going to be output and ID2 isn't
4424 already taken by another declaration. */
4426 static void
4427 generate_mangling_alias (tree decl, tree id2)
4429 struct cgraph_node *n = NULL;
4431 if (TREE_CODE (decl) == FUNCTION_DECL)
4433 n = cgraph_node::get (decl);
4434 if (!n)
4435 /* Don't create an alias to an unreferenced function. */
4436 return;
4439 tree *slot
4440 = mangled_decls->find_slot_with_hash (id2, IDENTIFIER_HASH_VALUE (id2),
4441 INSERT);
4443 /* If there's a declaration already using this mangled name,
4444 don't create a compatibility alias that conflicts. */
4445 if (*slot)
4446 return;
4448 tree alias = make_alias_for (decl, id2);
4449 *slot = alias;
4451 DECL_IGNORED_P (alias) = 1;
4452 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
4453 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
4454 if (vague_linkage_p (decl))
4455 DECL_WEAK (alias) = 1;
4457 if (n)
4458 n->create_same_body_alias (alias, decl);
4459 else
4460 varpool_node::create_extra_name_alias (alias, decl);
4463 /* Note that we might want to emit an alias with the symbol ID2 for DECL at
4464 the end of translation, for compatibility across bugs in the mangling
4465 implementation. */
4467 void
4468 note_mangling_alias (tree decl, tree id2)
4470 if (TARGET_SUPPORTS_ALIASES)
4472 if (!defer_mangling_aliases)
4473 generate_mangling_alias (decl, id2);
4474 else
4476 vec_safe_push (mangling_aliases, decl);
4477 vec_safe_push (mangling_aliases, id2);
4482 /* Emit all mangling aliases that were deferred up to this point. */
4484 void
4485 generate_mangling_aliases ()
4487 while (!vec_safe_is_empty (mangling_aliases))
4489 tree id2 = mangling_aliases->pop();
4490 tree decl = mangling_aliases->pop();
4491 generate_mangling_alias (decl, id2);
4493 defer_mangling_aliases = false;
4496 /* Record a mangling of DECL, whose DECL_ASSEMBLER_NAME has just been
4497 set. NEED_WARNING is true if we must warn about collisions. We do
4498 this to spot changes in mangling that may require compatibility
4499 aliases. */
4501 void
4502 record_mangling (tree decl, bool need_warning)
4504 if (!mangled_decls)
4505 mangled_decls = hash_table<mangled_decl_hash>::create_ggc (499);
4507 gcc_checking_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
4508 tree id = DECL_ASSEMBLER_NAME_RAW (decl);
4509 tree *slot
4510 = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id),
4511 INSERT);
4513 /* If this is already an alias, remove the alias, because the real
4514 decl takes precedence. */
4515 if (*slot && DECL_ARTIFICIAL (*slot) && DECL_IGNORED_P (*slot))
4516 if (symtab_node *n = symtab_node::get (*slot))
4517 if (n->cpp_implicit_alias)
4519 n->remove ();
4520 *slot = NULL_TREE;
4523 if (!*slot)
4524 *slot = decl;
4525 else if (need_warning)
4527 error_at (DECL_SOURCE_LOCATION (decl),
4528 "mangling of %q#D as %qE conflicts with a previous mangle",
4529 decl, id);
4530 inform (DECL_SOURCE_LOCATION (*slot),
4531 "previous mangling %q#D", *slot);
4532 inform (DECL_SOURCE_LOCATION (decl),
4533 "a later -fabi-version= (or =0)"
4534 " avoids this error with a change in mangling");
4535 *slot = decl;
4539 /* The mangled name of DECL is being forcibly changed to NAME. Remove
4540 any existing knowledge of DECL's mangled name meaning DECL. */
4542 void
4543 overwrite_mangling (tree decl, tree name)
4545 if (tree id = DECL_ASSEMBLER_NAME_RAW (decl))
4546 if ((TREE_CODE (decl) == VAR_DECL
4547 || TREE_CODE (decl) == FUNCTION_DECL)
4548 && mangled_decls)
4549 if (tree *slot
4550 = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id),
4551 NO_INSERT))
4552 if (*slot == decl)
4554 mangled_decls->clear_slot (slot);
4556 /* If this is an alias, remove it from the symbol table. */
4557 if (DECL_ARTIFICIAL (decl) && DECL_IGNORED_P (decl))
4558 if (symtab_node *n = symtab_node::get (decl))
4559 if (n->cpp_implicit_alias)
4560 n->remove ();
4563 DECL_ASSEMBLER_NAME_RAW (decl) = name;
4566 /* The entire file is now complete. If requested, dump everything
4567 to a file. */
4569 static void
4570 dump_tu (void)
4572 dump_flags_t flags;
4573 if (FILE *stream = dump_begin (raw_dump_id, &flags))
4575 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4576 dump_end (raw_dump_id, stream);
4580 static location_t locus_at_end_of_parsing;
4582 /* Check the deallocation functions for CODE to see if we want to warn that
4583 only one was defined. */
4585 static void
4586 maybe_warn_sized_delete (enum tree_code code)
4588 tree sized = NULL_TREE;
4589 tree unsized = NULL_TREE;
4591 for (ovl_iterator iter (get_global_binding (ovl_op_identifier (false, code)));
4592 iter; ++iter)
4594 tree fn = *iter;
4595 /* We're only interested in usual deallocation functions. */
4596 if (!usual_deallocation_fn_p (fn))
4597 continue;
4598 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
4599 unsized = fn;
4600 else
4601 sized = fn;
4603 if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
4604 warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
4605 "the program should also define %qD", sized);
4606 else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
4607 warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
4608 "the program should also define %qD", unsized);
4611 /* Check the global deallocation functions to see if we want to warn about
4612 defining unsized without sized (or vice versa). */
4614 static void
4615 maybe_warn_sized_delete ()
4617 if (!flag_sized_deallocation || !warn_sized_deallocation)
4618 return;
4619 maybe_warn_sized_delete (DELETE_EXPR);
4620 maybe_warn_sized_delete (VEC_DELETE_EXPR);
4623 /* Earlier we left PTRMEM_CST in variable initializers alone so that we could
4624 look them up when evaluating non-type template parameters. Now we need to
4625 lower them to something the back end can understand. */
4627 static void
4628 lower_var_init ()
4630 varpool_node *node;
4631 FOR_EACH_VARIABLE (node)
4633 tree d = node->decl;
4634 if (tree init = DECL_INITIAL (d))
4635 DECL_INITIAL (d) = cplus_expand_constant (init);
4639 /* This routine is called at the end of compilation.
4640 Its job is to create all the code needed to initialize and
4641 destroy the global aggregates. We do the destruction
4642 first, since that way we only need to reverse the decls once. */
4644 void
4645 c_parse_final_cleanups (void)
4647 tree vars;
4648 bool reconsider;
4649 size_t i;
4650 unsigned ssdf_count = 0;
4651 int retries = 0;
4652 tree decl;
4654 locus_at_end_of_parsing = input_location;
4655 at_eof = 1;
4657 /* Bad parse errors. Just forget about it. */
4658 if (! global_bindings_p () || current_class_type
4659 || !vec_safe_is_empty (decl_namespace_list))
4660 return;
4662 /* This is the point to write out a PCH if we're doing that.
4663 In that case we do not want to do anything else. */
4664 if (pch_file)
4666 /* Mangle all symbols at PCH creation time. */
4667 symtab_node *node;
4668 FOR_EACH_SYMBOL (node)
4669 if (! is_a <varpool_node *> (node)
4670 || ! DECL_HARD_REGISTER (node->decl))
4671 DECL_ASSEMBLER_NAME (node->decl);
4672 c_common_write_pch ();
4673 dump_tu ();
4674 /* Ensure even the callers don't try to finalize the CU. */
4675 flag_syntax_only = 1;
4676 return;
4679 timevar_stop (TV_PHASE_PARSING);
4680 timevar_start (TV_PHASE_DEFERRED);
4682 symtab->process_same_body_aliases ();
4684 /* Handle -fdump-ada-spec[-slim] */
4685 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4687 if (flag_dump_ada_spec_slim)
4688 collect_source_ref (main_input_filename);
4689 else
4690 collect_source_refs (global_namespace);
4692 dump_ada_specs (collect_all_refs, cpp_check);
4695 /* FIXME - huh? was input_line -= 1;*/
4697 /* We now have to write out all the stuff we put off writing out.
4698 These include:
4700 o Template specializations that we have not yet instantiated,
4701 but which are needed.
4702 o Initialization and destruction for non-local objects with
4703 static storage duration. (Local objects with static storage
4704 duration are initialized when their scope is first entered,
4705 and are cleaned up via atexit.)
4706 o Virtual function tables.
4708 All of these may cause others to be needed. For example,
4709 instantiating one function may cause another to be needed, and
4710 generating the initializer for an object may cause templates to be
4711 instantiated, etc., etc. */
4713 emit_support_tinfos ();
4717 tree t;
4718 tree decl;
4720 reconsider = false;
4722 /* If there are templates that we've put off instantiating, do
4723 them now. */
4724 instantiate_pending_templates (retries);
4725 ggc_collect ();
4727 /* Write out virtual tables as required. Writing out the
4728 virtual table for a template class may cause the
4729 instantiation of members of that class. If we write out
4730 vtables then we remove the class from our list so we don't
4731 have to look at it again. */
4732 for (i = keyed_classes->length ();
4733 keyed_classes->iterate (--i, &t);)
4734 if (maybe_emit_vtables (t))
4736 reconsider = true;
4737 keyed_classes->unordered_remove (i);
4739 /* The input_location may have been changed during marking of
4740 vtable entries. */
4741 input_location = locus_at_end_of_parsing;
4743 /* Write out needed type info variables. We have to be careful
4744 looping through unemitted decls, because emit_tinfo_decl may
4745 cause other variables to be needed. New elements will be
4746 appended, and we remove from the vector those that actually
4747 get emitted. */
4748 for (i = unemitted_tinfo_decls->length ();
4749 unemitted_tinfo_decls->iterate (--i, &t);)
4750 if (emit_tinfo_decl (t))
4752 reconsider = true;
4753 unemitted_tinfo_decls->unordered_remove (i);
4756 /* The list of objects with static storage duration is built up
4757 in reverse order. We clear STATIC_AGGREGATES so that any new
4758 aggregates added during the initialization of these will be
4759 initialized in the correct order when we next come around the
4760 loop. */
4761 vars = prune_vars_needing_no_initialization (&static_aggregates);
4763 if (vars)
4765 /* We need to start a new initialization function each time
4766 through the loop. That's because we need to know which
4767 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4768 isn't computed until a function is finished, and written
4769 out. That's a deficiency in the back end. When this is
4770 fixed, these initialization functions could all become
4771 inline, with resulting performance improvements. */
4772 tree ssdf_body;
4774 /* Set the line and file, so that it is obviously not from
4775 the source file. */
4776 input_location = locus_at_end_of_parsing;
4777 ssdf_body = start_static_storage_duration_function (ssdf_count);
4779 /* Make sure the back end knows about all the variables. */
4780 write_out_vars (vars);
4782 /* First generate code to do all the initializations. */
4783 if (vars)
4784 do_static_initialization_or_destruction (vars, /*initp=*/true);
4786 /* Then, generate code to do all the destructions. Do these
4787 in reverse order so that the most recently constructed
4788 variable is the first destroyed. If we're using
4789 __cxa_atexit, then we don't need to do this; functions
4790 were registered at initialization time to destroy the
4791 local statics. */
4792 if (!flag_use_cxa_atexit && vars)
4794 vars = nreverse (vars);
4795 do_static_initialization_or_destruction (vars, /*initp=*/false);
4797 else
4798 vars = NULL_TREE;
4800 /* Finish up the static storage duration function for this
4801 round. */
4802 input_location = locus_at_end_of_parsing;
4803 finish_static_storage_duration_function (ssdf_body);
4805 /* All those initializations and finalizations might cause
4806 us to need more inline functions, more template
4807 instantiations, etc. */
4808 reconsider = true;
4809 ssdf_count++;
4810 /* ??? was: locus_at_end_of_parsing.line++; */
4813 /* Now do the same for thread_local variables. */
4814 handle_tls_init ();
4816 /* Go through the set of inline functions whose bodies have not
4817 been emitted yet. If out-of-line copies of these functions
4818 are required, emit them. */
4819 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4821 /* Does it need synthesizing? */
4822 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4823 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4825 /* Even though we're already at the top-level, we push
4826 there again. That way, when we pop back a few lines
4827 hence, all of our state is restored. Otherwise,
4828 finish_function doesn't clean things up, and we end
4829 up with CURRENT_FUNCTION_DECL set. */
4830 push_to_top_level ();
4831 /* The decl's location will mark where it was first
4832 needed. Save that so synthesize method can indicate
4833 where it was needed from, in case of error */
4834 input_location = DECL_SOURCE_LOCATION (decl);
4835 synthesize_method (decl);
4836 pop_from_top_level ();
4837 reconsider = true;
4840 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4841 generate_tls_wrapper (decl);
4843 if (!DECL_SAVED_TREE (decl))
4844 continue;
4846 cgraph_node *node = cgraph_node::get_create (decl);
4848 /* We lie to the back end, pretending that some functions
4849 are not defined when they really are. This keeps these
4850 functions from being put out unnecessarily. But, we must
4851 stop lying when the functions are referenced, or if they
4852 are not comdat since they need to be put out now. If
4853 DECL_INTERFACE_KNOWN, then we have already set
4854 DECL_EXTERNAL appropriately, so there's no need to check
4855 again, and we do not want to clear DECL_EXTERNAL if a
4856 previous call to import_export_decl set it.
4858 This is done in a separate for cycle, because if some
4859 deferred function is contained in another deferred
4860 function later in deferred_fns varray,
4861 rest_of_compilation would skip this function and we
4862 really cannot expand the same function twice. */
4863 import_export_decl (decl);
4864 if (DECL_NOT_REALLY_EXTERN (decl)
4865 && DECL_INITIAL (decl)
4866 && decl_needed_p (decl))
4868 if (node->cpp_implicit_alias)
4869 node = node->get_alias_target ();
4871 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
4872 NULL, true);
4873 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4874 group, we need to mark all symbols in the same comdat group
4875 that way. */
4876 if (node->same_comdat_group)
4877 for (cgraph_node *next
4878 = dyn_cast<cgraph_node *> (node->same_comdat_group);
4879 next != node;
4880 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
4881 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
4882 NULL, true);
4885 /* If we're going to need to write this function out, and
4886 there's already a body for it, create RTL for it now.
4887 (There might be no body if this is a method we haven't
4888 gotten around to synthesizing yet.) */
4889 if (!DECL_EXTERNAL (decl)
4890 && decl_needed_p (decl)
4891 && !TREE_ASM_WRITTEN (decl)
4892 && !node->definition)
4894 /* We will output the function; no longer consider it in this
4895 loop. */
4896 DECL_DEFER_OUTPUT (decl) = 0;
4897 /* Generate RTL for this function now that we know we
4898 need it. */
4899 expand_or_defer_fn (decl);
4900 /* If we're compiling -fsyntax-only pretend that this
4901 function has been written out so that we don't try to
4902 expand it again. */
4903 if (flag_syntax_only)
4904 TREE_ASM_WRITTEN (decl) = 1;
4905 reconsider = true;
4909 if (wrapup_namespace_globals ())
4910 reconsider = true;
4912 /* Static data members are just like namespace-scope globals. */
4913 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4915 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4916 /* Don't write it out if we haven't seen a definition. */
4917 || (DECL_IN_AGGR_P (decl) && !DECL_INLINE_VAR_P (decl)))
4918 continue;
4919 import_export_decl (decl);
4920 /* If this static data member is needed, provide it to the
4921 back end. */
4922 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4923 DECL_EXTERNAL (decl) = 0;
4925 if (vec_safe_length (pending_statics) != 0
4926 && wrapup_global_declarations (pending_statics->address (),
4927 pending_statics->length ()))
4928 reconsider = true;
4930 retries++;
4932 while (reconsider);
4934 lower_var_init ();
4936 generate_mangling_aliases ();
4938 /* All used inline functions must have a definition at this point. */
4939 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4941 if (/* Check online inline functions that were actually used. */
4942 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4943 /* If the definition actually was available here, then the
4944 fact that the function was not defined merely represents
4945 that for some reason (use of a template repository,
4946 #pragma interface, etc.) we decided not to emit the
4947 definition here. */
4948 && !DECL_INITIAL (decl)
4949 /* Don't complain if the template was defined. */
4950 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4951 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4952 (template_for_substitution (decl)))))
4954 warning_at (DECL_SOURCE_LOCATION (decl), 0,
4955 "inline function %qD used but never defined", decl);
4956 /* Avoid a duplicate warning from check_global_declaration. */
4957 TREE_NO_WARNING (decl) = 1;
4961 /* So must decls that use a type with no linkage. */
4962 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4963 no_linkage_error (decl);
4965 maybe_warn_sized_delete ();
4967 /* Then, do the Objective-C stuff. This is where all the
4968 Objective-C module stuff gets generated (symtab,
4969 class/protocol/selector lists etc). This must be done after C++
4970 templates, destructors etc. so that selectors used in C++
4971 templates are properly allocated. */
4972 if (c_dialect_objc ())
4973 objc_write_global_declarations ();
4975 /* We give C linkage to static constructors and destructors. */
4976 push_lang_context (lang_name_c);
4978 /* Generate initialization and destruction functions for all
4979 priorities for which they are required. */
4980 if (priority_info_map)
4981 splay_tree_foreach (priority_info_map,
4982 generate_ctor_and_dtor_functions_for_priority,
4983 /*data=*/&locus_at_end_of_parsing);
4984 else if (c_dialect_objc () && objc_static_init_needed_p ())
4985 /* If this is obj-c++ and we need a static init, call
4986 generate_ctor_or_dtor_function. */
4987 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4988 DEFAULT_INIT_PRIORITY,
4989 &locus_at_end_of_parsing);
4991 /* We're done with the splay-tree now. */
4992 if (priority_info_map)
4993 splay_tree_delete (priority_info_map);
4995 /* Generate any missing aliases. */
4996 maybe_apply_pending_pragma_weaks ();
4998 /* We're done with static constructors, so we can go back to "C++"
4999 linkage now. */
5000 pop_lang_context ();
5002 if (flag_vtable_verify)
5004 vtv_recover_class_info ();
5005 vtv_compute_class_hierarchy_transitive_closure ();
5006 vtv_build_vtable_verify_fndecl ();
5009 perform_deferred_noexcept_checks ();
5011 finish_repo ();
5012 fini_constexpr ();
5014 /* The entire file is now complete. If requested, dump everything
5015 to a file. */
5016 dump_tu ();
5018 if (flag_detailed_statistics)
5020 dump_tree_statistics ();
5021 dump_time_statistics ();
5024 timevar_stop (TV_PHASE_DEFERRED);
5025 timevar_start (TV_PHASE_PARSING);
5027 /* Indicate that we're done with front end processing. */
5028 at_eof = 2;
5031 /* Perform any post compilation-proper cleanups for the C++ front-end.
5032 This should really go away. No front-end should need to do
5033 anything past the compilation process. */
5035 void
5036 cxx_post_compilation_parsing_cleanups (void)
5038 timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS);
5040 if (flag_vtable_verify)
5042 /* Generate the special constructor initialization function that
5043 calls __VLTRegisterPairs, and give it a very high
5044 initialization priority. This must be done after
5045 finalize_compilation_unit so that we have accurate
5046 information about which vtable will actually be emitted. */
5047 vtv_generate_init_routine ();
5050 input_location = locus_at_end_of_parsing;
5052 if (flag_checking)
5053 validate_conversion_obstack ();
5055 timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS);
5058 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
5059 function to call in parse-tree form; it has not yet been
5060 semantically analyzed. ARGS are the arguments to the function.
5061 They have already been semantically analyzed. This may change
5062 ARGS. */
5064 tree
5065 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
5066 tsubst_flags_t complain)
5068 tree orig_fn;
5069 vec<tree, va_gc> *orig_args = NULL;
5070 tree expr;
5071 tree object;
5073 orig_fn = fn;
5074 object = TREE_OPERAND (fn, 0);
5076 if (processing_template_decl)
5078 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
5079 || TREE_CODE (fn) == MEMBER_REF);
5080 if (type_dependent_expression_p (fn)
5081 || any_type_dependent_arguments_p (*args))
5082 return build_min_nt_call_vec (fn, *args);
5084 orig_args = make_tree_vector_copy (*args);
5086 /* Transform the arguments and add the implicit "this"
5087 parameter. That must be done before the FN is transformed
5088 because we depend on the form of FN. */
5089 make_args_non_dependent (*args);
5090 object = build_non_dependent_expr (object);
5091 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5093 if (TREE_CODE (fn) == DOTSTAR_EXPR)
5094 object = cp_build_addr_expr (object, complain);
5095 vec_safe_insert (*args, 0, object);
5097 /* Now that the arguments are done, transform FN. */
5098 fn = build_non_dependent_expr (fn);
5101 /* A qualified name corresponding to a bound pointer-to-member is
5102 represented as an OFFSET_REF:
5104 struct B { void g(); };
5105 void (B::*p)();
5106 void B::g() { (this->*p)(); } */
5107 if (TREE_CODE (fn) == OFFSET_REF)
5109 tree object_addr = cp_build_addr_expr (object, complain);
5110 fn = TREE_OPERAND (fn, 1);
5111 fn = get_member_function_from_ptrfunc (&object_addr, fn,
5112 complain);
5113 vec_safe_insert (*args, 0, object_addr);
5116 if (CLASS_TYPE_P (TREE_TYPE (fn)))
5117 expr = build_op_call (fn, args, complain);
5118 else
5119 expr = cp_build_function_call_vec (fn, args, complain);
5120 if (processing_template_decl && expr != error_mark_node)
5121 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
5123 if (orig_args != NULL)
5124 release_tree_vector (orig_args);
5126 return expr;
5130 void
5131 check_default_args (tree x)
5133 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
5134 bool saw_def = false;
5135 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
5136 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
5138 if (TREE_PURPOSE (arg))
5139 saw_def = true;
5140 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
5142 error ("default argument missing for parameter %P of %q+#D", i, x);
5143 TREE_PURPOSE (arg) = error_mark_node;
5148 /* Return true if function DECL can be inlined. This is used to force
5149 instantiation of methods that might be interesting for inlining. */
5150 bool
5151 possibly_inlined_p (tree decl)
5153 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
5154 if (DECL_UNINLINABLE (decl))
5155 return false;
5156 if (!optimize)
5157 return DECL_DECLARED_INLINE_P (decl);
5158 /* When optimizing, we might inline everything when flatten
5159 attribute or heuristics inlining for size or autoinlining
5160 is used. */
5161 return true;
5164 /* Normally, we can wait until instantiation-time to synthesize DECL.
5165 However, if DECL is a static data member initialized with a constant
5166 or a constexpr function, we need it right now because a reference to
5167 such a data member or a call to such function is not value-dependent.
5168 For a function that uses auto in the return type, we need to instantiate
5169 it to find out its type. For OpenMP user defined reductions, we need
5170 them instantiated for reduction clauses which inline them by hand
5171 directly. */
5173 static void
5174 maybe_instantiate_decl (tree decl)
5176 if (DECL_LANG_SPECIFIC (decl)
5177 && DECL_TEMPLATE_INFO (decl)
5178 && (decl_maybe_constant_var_p (decl)
5179 || (TREE_CODE (decl) == FUNCTION_DECL
5180 && DECL_OMP_DECLARE_REDUCTION_P (decl))
5181 || undeduced_auto_decl (decl))
5182 && !DECL_DECLARED_CONCEPT_P (decl)
5183 && !uses_template_parms (DECL_TI_ARGS (decl)))
5185 /* Instantiating a function will result in garbage collection. We
5186 must treat this situation as if we were within the body of a
5187 function so as to avoid collecting live data only referenced from
5188 the stack (such as overload resolution candidates). */
5189 ++function_depth;
5190 instantiate_decl (decl, /*defer_ok=*/false,
5191 /*expl_inst_class_mem_p=*/false);
5192 --function_depth;
5196 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
5197 If DECL is a specialization or implicitly declared class member,
5198 generate the actual definition. Return false if something goes
5199 wrong, true otherwise. */
5201 bool
5202 mark_used (tree decl, tsubst_flags_t complain)
5204 /* If DECL is a BASELINK for a single function, then treat it just
5205 like the DECL for the function. Otherwise, if the BASELINK is
5206 for an overloaded function, we don't know which function was
5207 actually used until after overload resolution. */
5208 if (BASELINK_P (decl))
5210 decl = BASELINK_FUNCTIONS (decl);
5211 if (really_overloaded_fn (decl))
5212 return true;
5213 decl = OVL_FIRST (decl);
5216 /* Set TREE_USED for the benefit of -Wunused. */
5217 TREE_USED (decl) = 1;
5218 /* And for structured bindings also the underlying decl. */
5219 if (DECL_DECOMPOSITION_P (decl) && DECL_DECOMP_BASE (decl))
5220 TREE_USED (DECL_DECOMP_BASE (decl)) = 1;
5222 if (TREE_CODE (decl) == TEMPLATE_DECL)
5223 return true;
5225 if (DECL_CLONED_FUNCTION_P (decl))
5226 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5228 /* Mark enumeration types as used. */
5229 if (TREE_CODE (decl) == CONST_DECL)
5230 used_types_insert (DECL_CONTEXT (decl));
5232 if (TREE_CODE (decl) == FUNCTION_DECL
5233 && !maybe_instantiate_noexcept (decl, complain))
5234 return false;
5236 if (TREE_CODE (decl) == FUNCTION_DECL
5237 && DECL_DELETED_FN (decl))
5239 if (DECL_ARTIFICIAL (decl)
5240 && DECL_CONV_FN_P (decl)
5241 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
5242 /* We mark a lambda conversion op as deleted if we can't
5243 generate it properly; see maybe_add_lambda_conv_op. */
5244 sorry ("converting lambda that uses %<...%> to function pointer");
5245 else if (complain & tf_error)
5247 error ("use of deleted function %qD", decl);
5248 if (!maybe_explain_implicit_delete (decl))
5249 inform (DECL_SOURCE_LOCATION (decl), "declared here");
5251 return false;
5254 if (TREE_DEPRECATED (decl) && (complain & tf_warning)
5255 && deprecated_state != DEPRECATED_SUPPRESS)
5256 warn_deprecated_use (decl, NULL_TREE);
5258 /* We can only check DECL_ODR_USED on variables or functions with
5259 DECL_LANG_SPECIFIC set, and these are also the only decls that we
5260 might need special handling for. */
5261 if (!VAR_OR_FUNCTION_DECL_P (decl)
5262 || DECL_LANG_SPECIFIC (decl) == NULL
5263 || DECL_THUNK_P (decl))
5265 if (!processing_template_decl
5266 && !require_deduced_type (decl, complain))
5267 return false;
5268 return true;
5271 /* We only want to do this processing once. We don't need to keep trying
5272 to instantiate inline templates, because unit-at-a-time will make sure
5273 we get them compiled before functions that want to inline them. */
5274 if (DECL_ODR_USED (decl))
5275 return true;
5277 /* Normally, we can wait until instantiation-time to synthesize DECL.
5278 However, if DECL is a static data member initialized with a constant
5279 or a constexpr function, we need it right now because a reference to
5280 such a data member or a call to such function is not value-dependent.
5281 For a function that uses auto in the return type, we need to instantiate
5282 it to find out its type. For OpenMP user defined reductions, we need
5283 them instantiated for reduction clauses which inline them by hand
5284 directly. */
5285 maybe_instantiate_decl (decl);
5287 if (processing_template_decl || in_template_function ())
5288 return true;
5290 /* Check this too in case we're within instantiate_non_dependent_expr. */
5291 if (DECL_TEMPLATE_INFO (decl)
5292 && uses_template_parms (DECL_TI_ARGS (decl)))
5293 return true;
5295 if (!require_deduced_type (decl, complain))
5296 return false;
5298 if (builtin_pack_fn_p (decl))
5300 error ("use of built-in parameter pack %qD outside of a template",
5301 DECL_NAME (decl));
5302 return false;
5305 /* If we don't need a value, then we don't need to synthesize DECL. */
5306 if (cp_unevaluated_operand || in_discarded_stmt)
5307 return true;
5309 DECL_ODR_USED (decl) = 1;
5310 if (DECL_CLONED_FUNCTION_P (decl))
5311 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5313 /* DR 757: A type without linkage shall not be used as the type of a
5314 variable or function with linkage, unless
5315 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5316 o the variable or function is not used (3.2 [basic.def.odr]) or is
5317 defined in the same translation unit. */
5318 if (cxx_dialect > cxx98
5319 && decl_linkage (decl) != lk_none
5320 && !DECL_EXTERN_C_P (decl)
5321 && !DECL_ARTIFICIAL (decl)
5322 && !decl_defined_p (decl)
5323 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
5325 if (is_local_extern (decl))
5326 /* There's no way to define a local extern, and adding it to
5327 the vector interferes with GC, so give an error now. */
5328 no_linkage_error (decl);
5329 else
5330 vec_safe_push (no_linkage_decls, decl);
5333 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
5334 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
5335 /* Remember it, so we can check it was defined. */
5336 note_vague_linkage_fn (decl);
5338 /* Is it a synthesized method that needs to be synthesized? */
5339 if (TREE_CODE (decl) == FUNCTION_DECL
5340 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5341 && DECL_DEFAULTED_FN (decl)
5342 /* A function defaulted outside the class is synthesized either by
5343 cp_finish_decl or instantiate_decl. */
5344 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
5345 && ! DECL_INITIAL (decl))
5347 /* Defer virtual destructors so that thunks get the right
5348 linkage. */
5349 if (DECL_VIRTUAL_P (decl) && !at_eof)
5351 note_vague_linkage_fn (decl);
5352 return true;
5355 /* Remember the current location for a function we will end up
5356 synthesizing. Then we can inform the user where it was
5357 required in the case of error. */
5358 DECL_SOURCE_LOCATION (decl) = input_location;
5360 /* Synthesizing an implicitly defined member function will result in
5361 garbage collection. We must treat this situation as if we were
5362 within the body of a function so as to avoid collecting live data
5363 on the stack (such as overload resolution candidates).
5365 We could just let cp_write_global_declarations handle synthesizing
5366 this function by adding it to deferred_fns, but doing
5367 it at the use site produces better error messages. */
5368 ++function_depth;
5369 synthesize_method (decl);
5370 --function_depth;
5371 /* If this is a synthesized method we don't need to
5372 do the instantiation test below. */
5374 else if (VAR_OR_FUNCTION_DECL_P (decl)
5375 && DECL_TEMPLATE_INFO (decl)
5376 && !DECL_DECLARED_CONCEPT_P (decl)
5377 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5378 || always_instantiate_p (decl)))
5379 /* If this is a function or variable that is an instance of some
5380 template, we now know that we will need to actually do the
5381 instantiation. We check that DECL is not an explicit
5382 instantiation because that is not checked in instantiate_decl.
5384 We put off instantiating functions in order to improve compile
5385 times. Maintaining a stack of active functions is expensive,
5386 and the inliner knows to instantiate any functions it might
5387 need. Therefore, we always try to defer instantiation. */
5389 ++function_depth;
5390 instantiate_decl (decl, /*defer_ok=*/true,
5391 /*expl_inst_class_mem_p=*/false);
5392 --function_depth;
5395 return true;
5398 bool
5399 mark_used (tree decl)
5401 return mark_used (decl, tf_warning_or_error);
5404 tree
5405 vtv_start_verification_constructor_init_function (void)
5407 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5410 tree
5411 vtv_finish_verification_constructor_init_function (tree function_body)
5413 tree fn;
5415 finish_compound_stmt (function_body);
5416 fn = finish_function (/*inline_p=*/false);
5417 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5418 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5420 return fn;
5423 #include "gt-cp-decl2.h"