ChangeLog fix
[official-gcc.git] / gcc / cp / decl2.c
blob2cef9c750edc72ce657497dc8dcf2e2a8925b5a6
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2018 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* Process declarations and symbol lookup for C++ front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "memmodel.h"
33 #include "target.h"
34 #include "cp-tree.h"
35 #include "c-family/c-common.h"
36 #include "timevar.h"
37 #include "stringpool.h"
38 #include "cgraph.h"
39 #include "varasm.h"
40 #include "attribs.h"
41 #include "stor-layout.h"
42 #include "calls.h"
43 #include "decl.h"
44 #include "toplev.h"
45 #include "c-family/c-objc.h"
46 #include "c-family/c-pragma.h"
47 #include "dumpfile.h"
48 #include "intl.h"
49 #include "c-family/c-ada-spec.h"
50 #include "asan.h"
52 /* Id for dumping the raw trees. */
53 int raw_dump_id;
55 extern cpp_reader *parse_in;
57 /* This structure contains information about the initializations
58 and/or destructions required for a particular priority level. */
59 typedef struct priority_info_s {
60 /* Nonzero if there have been any initializations at this priority
61 throughout the translation unit. */
62 int initializations_p;
63 /* Nonzero if there have been any destructions at this priority
64 throughout the translation unit. */
65 int destructions_p;
66 } *priority_info;
68 static void mark_vtable_entries (tree);
69 static bool maybe_emit_vtables (tree);
70 static tree start_objects (int, int);
71 static void finish_objects (int, int, tree);
72 static tree start_static_storage_duration_function (unsigned);
73 static void finish_static_storage_duration_function (tree);
74 static priority_info get_priority_info (int);
75 static void do_static_initialization_or_destruction (tree, bool);
76 static void one_static_initialization_or_destruction (tree, tree, bool);
77 static void generate_ctor_or_dtor_function (bool, int, location_t *);
78 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
79 void *);
80 static tree prune_vars_needing_no_initialization (tree *);
81 static void write_out_vars (tree);
82 static void import_export_class (tree);
83 static tree get_guard_bits (tree);
84 static void determine_visibility_from_class (tree, tree);
85 static bool determine_hidden_inline (tree);
86 static void maybe_instantiate_decl (tree);
88 /* A list of static class variables. This is needed, because a
89 static class variable can be declared inside the class without
90 an initializer, and then initialized, statically, outside the class. */
91 static GTY(()) vec<tree, va_gc> *pending_statics;
93 /* A list of functions which were declared inline, but which we
94 may need to emit outline anyway. */
95 static GTY(()) vec<tree, va_gc> *deferred_fns;
97 /* A list of decls that use types with no linkage, which we need to make
98 sure are defined. */
99 static GTY(()) vec<tree, va_gc> *no_linkage_decls;
101 /* A vector of alternating decls and identifiers, where the latter
102 is to be an alias for the former if the former is defined. */
103 static GTY(()) vec<tree, va_gc> *mangling_aliases;
105 /* hash traits for declarations. Hashes single decls via
106 DECL_ASSEMBLER_NAME_RAW. */
108 struct mangled_decl_hash : ggc_remove <tree>
110 typedef tree value_type; /* A DECL. */
111 typedef tree compare_type; /* An identifier. */
113 static hashval_t hash (const value_type decl)
115 return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME_RAW (decl));
117 static bool equal (const value_type existing, compare_type candidate)
119 tree name = DECL_ASSEMBLER_NAME_RAW (existing);
120 return candidate == name;
123 static inline void mark_empty (value_type &p) {p = NULL_TREE;}
124 static inline bool is_empty (value_type p) {return !p;}
126 static bool is_deleted (value_type e)
128 return e == reinterpret_cast <value_type> (1);
130 static void mark_deleted (value_type &e)
132 e = reinterpret_cast <value_type> (1);
136 /* A hash table of decls keyed by mangled name. Used to figure out if
137 we need compatibility aliases. */
138 static GTY(()) hash_table<mangled_decl_hash> *mangled_decls;
140 /* Nonzero if we're done parsing and into end-of-file activities. */
142 int at_eof;
144 /* True if note_mangling_alias should enqueue mangling aliases for
145 later generation, rather than emitting them right away. */
147 bool defer_mangling_aliases = true;
150 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
151 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
152 that apply to the function). */
154 tree
155 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
156 cp_ref_qualifier rqual)
158 if (fntype == error_mark_node || ctype == error_mark_node)
159 return error_mark_node;
161 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
162 || TREE_CODE (fntype) == METHOD_TYPE);
164 cp_cv_quals type_quals = quals & ~TYPE_QUAL_RESTRICT;
165 ctype = cp_build_qualified_type (ctype, type_quals);
167 tree newtype
168 = build_method_type_directly (ctype, TREE_TYPE (fntype),
169 (TREE_CODE (fntype) == METHOD_TYPE
170 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
171 : TYPE_ARG_TYPES (fntype)));
172 if (tree attrs = TYPE_ATTRIBUTES (fntype))
173 newtype = cp_build_type_attribute_variant (newtype, attrs);
174 newtype = build_cp_fntype_variant (newtype, rqual,
175 TYPE_RAISES_EXCEPTIONS (fntype),
176 TYPE_HAS_LATE_RETURN_TYPE (fntype));
178 return newtype;
181 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
182 return type changed to NEW_RET. */
184 tree
185 change_return_type (tree new_ret, tree fntype)
187 if (new_ret == error_mark_node)
188 return fntype;
190 if (same_type_p (new_ret, TREE_TYPE (fntype)))
191 return fntype;
193 tree newtype;
194 tree args = TYPE_ARG_TYPES (fntype);
196 if (TREE_CODE (fntype) == FUNCTION_TYPE)
198 newtype = build_function_type (new_ret, args);
199 newtype = apply_memfn_quals (newtype,
200 type_memfn_quals (fntype));
202 else
203 newtype = build_method_type_directly
204 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
206 if (tree attrs = TYPE_ATTRIBUTES (fntype))
207 newtype = cp_build_type_attribute_variant (newtype, attrs);
208 newtype = cxx_copy_lang_qualifiers (newtype, fntype);
210 return newtype;
213 /* Build a PARM_DECL of FN with NAME and TYPE, and set DECL_ARG_TYPE
214 appropriately. */
216 tree
217 cp_build_parm_decl (tree fn, tree name, tree type)
219 tree parm = build_decl (input_location,
220 PARM_DECL, name, type);
221 DECL_CONTEXT (parm) = fn;
223 /* DECL_ARG_TYPE is only used by the back end and the back end never
224 sees templates. */
225 if (!processing_template_decl)
226 DECL_ARG_TYPE (parm) = type_passed_as (type);
228 return parm;
231 /* Returns a PARM_DECL of FN for a parameter of the indicated TYPE, with the
232 indicated NAME. */
234 tree
235 build_artificial_parm (tree fn, tree name, tree type)
237 tree parm = cp_build_parm_decl (fn, name, type);
238 DECL_ARTIFICIAL (parm) = 1;
239 /* All our artificial parms are implicitly `const'; they cannot be
240 assigned to. */
241 TREE_READONLY (parm) = 1;
242 return parm;
245 /* Constructors for types with virtual baseclasses need an "in-charge" flag
246 saying whether this constructor is responsible for initialization of
247 virtual baseclasses or not. All destructors also need this "in-charge"
248 flag, which additionally determines whether or not the destructor should
249 free the memory for the object.
251 This function adds the "in-charge" flag to member function FN if
252 appropriate. It is called from grokclassfn and tsubst.
253 FN must be either a constructor or destructor.
255 The in-charge flag follows the 'this' parameter, and is followed by the
256 VTT parm (if any), then the user-written parms. */
258 void
259 maybe_retrofit_in_chrg (tree fn)
261 tree basetype, arg_types, parms, parm, fntype;
263 /* If we've already add the in-charge parameter don't do it again. */
264 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
265 return;
267 /* When processing templates we can't know, in general, whether or
268 not we're going to have virtual baseclasses. */
269 if (processing_template_decl)
270 return;
272 /* We don't need an in-charge parameter for constructors that don't
273 have virtual bases. */
274 if (DECL_CONSTRUCTOR_P (fn)
275 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
276 return;
278 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
279 basetype = TREE_TYPE (TREE_VALUE (arg_types));
280 arg_types = TREE_CHAIN (arg_types);
282 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
284 /* If this is a subobject constructor or destructor, our caller will
285 pass us a pointer to our VTT. */
286 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
288 parm = build_artificial_parm (fn, vtt_parm_identifier, vtt_parm_type);
290 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
291 DECL_CHAIN (parm) = parms;
292 parms = parm;
294 /* ...and then to TYPE_ARG_TYPES. */
295 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
297 DECL_HAS_VTT_PARM_P (fn) = 1;
300 /* Then add the in-charge parm (before the VTT parm). */
301 parm = build_artificial_parm (fn, in_charge_identifier, integer_type_node);
302 DECL_CHAIN (parm) = parms;
303 parms = parm;
304 arg_types = hash_tree_chain (integer_type_node, arg_types);
306 /* Insert our new parameter(s) into the list. */
307 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
309 /* And rebuild the function type. */
310 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
311 arg_types);
312 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
313 fntype = (cp_build_type_attribute_variant
314 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
315 fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (fn));
316 TREE_TYPE (fn) = fntype;
318 /* Now we've got the in-charge parameter. */
319 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
322 /* Classes overload their constituent function names automatically.
323 When a function name is declared in a record structure,
324 its name is changed to it overloaded name. Since names for
325 constructors and destructors can conflict, we place a leading
326 '$' for destructors.
328 CNAME is the name of the class we are grokking for.
330 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
332 FLAGS contains bits saying what's special about today's
333 arguments. DTOR_FLAG == DESTRUCTOR.
335 If FUNCTION is a destructor, then we must add the `auto-delete' field
336 as a second parameter. There is some hair associated with the fact
337 that we must "declare" this variable in the manner consistent with the
338 way the rest of the arguments were declared.
340 QUALS are the qualifiers for the this pointer. */
342 void
343 grokclassfn (tree ctype, tree function, enum overload_flags flags)
345 tree fn_name = DECL_NAME (function);
347 /* Even within an `extern "C"' block, members get C++ linkage. See
348 [dcl.link] for details. */
349 SET_DECL_LANGUAGE (function, lang_cplusplus);
351 if (fn_name == NULL_TREE)
353 error ("name missing for member function");
354 fn_name = get_identifier ("<anonymous>");
355 DECL_NAME (function) = fn_name;
358 DECL_CONTEXT (function) = ctype;
360 if (flags == DTOR_FLAG)
361 DECL_CXX_DESTRUCTOR_P (function) = 1;
363 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
364 maybe_retrofit_in_chrg (function);
367 /* Create an ARRAY_REF, checking for the user doing things backwards
368 along the way. DECLTYPE_P is for N3276, as in the parser. */
370 tree
371 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
372 bool decltype_p)
374 tree type;
375 tree expr;
376 tree orig_array_expr = array_expr;
377 tree orig_index_exp = index_exp;
378 tree overload = NULL_TREE;
380 if (error_operand_p (array_expr) || error_operand_p (index_exp))
381 return error_mark_node;
383 if (processing_template_decl)
385 if (type_dependent_expression_p (array_expr)
386 || type_dependent_expression_p (index_exp))
387 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
388 NULL_TREE, NULL_TREE);
389 array_expr = build_non_dependent_expr (array_expr);
390 index_exp = build_non_dependent_expr (index_exp);
393 type = TREE_TYPE (array_expr);
394 gcc_assert (type);
395 type = non_reference (type);
397 /* If they have an `operator[]', use that. */
398 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
400 tsubst_flags_t complain = tf_warning_or_error;
401 if (decltype_p)
402 complain |= tf_decltype;
403 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
404 index_exp, NULL_TREE, &overload, complain);
406 else
408 tree p1, p2, i1, i2;
410 /* Otherwise, create an ARRAY_REF for a pointer or array type.
411 It is a little-known fact that, if `a' is an array and `i' is
412 an int, you can write `i[a]', which means the same thing as
413 `a[i]'. */
414 if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
415 p1 = array_expr;
416 else
417 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
419 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
420 p2 = index_exp;
421 else
422 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
424 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
425 false);
426 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
427 false);
429 if ((p1 && i2) && (i1 && p2))
430 error ("ambiguous conversion for array subscript");
432 if (p1 && i2)
433 array_expr = p1, index_exp = i2;
434 else if (i1 && p2)
435 array_expr = p2, index_exp = i1;
436 else
438 error ("invalid types %<%T[%T]%> for array subscript",
439 type, TREE_TYPE (index_exp));
440 return error_mark_node;
443 if (array_expr == error_mark_node || index_exp == error_mark_node)
444 error ("ambiguous conversion for array subscript");
446 if (TYPE_PTR_P (TREE_TYPE (array_expr)))
447 array_expr = mark_rvalue_use (array_expr);
448 else
449 array_expr = mark_lvalue_use_nonread (array_expr);
450 index_exp = mark_rvalue_use (index_exp);
451 expr = build_array_ref (input_location, array_expr, index_exp);
453 if (processing_template_decl && expr != error_mark_node)
455 if (overload != NULL_TREE)
456 return (build_min_non_dep_op_overload
457 (ARRAY_REF, expr, overload, orig_array_expr, orig_index_exp));
459 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
460 NULL_TREE, NULL_TREE);
462 return expr;
465 /* Given the cast expression EXP, checking out its validity. Either return
466 an error_mark_node if there was an unavoidable error, return a cast to
467 void for trying to delete a pointer w/ the value 0, or return the
468 call to delete. If DOING_VEC is true, we handle things differently
469 for doing an array delete.
470 Implements ARM $5.3.4. This is called from the parser. */
472 tree
473 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
474 tsubst_flags_t complain)
476 tree t, type;
478 if (exp == error_mark_node)
479 return exp;
481 if (processing_template_decl)
483 t = build_min (DELETE_EXPR, void_type_node, exp, size);
484 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
485 DELETE_EXPR_USE_VEC (t) = doing_vec;
486 TREE_SIDE_EFFECTS (t) = 1;
487 return t;
490 /* An array can't have been allocated by new, so complain. */
491 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
492 warning (0, "deleting array %q#E", exp);
494 t = build_expr_type_conversion (WANT_POINTER, exp, true);
496 if (t == NULL_TREE || t == error_mark_node)
498 error ("type %q#T argument given to %<delete%>, expected pointer",
499 TREE_TYPE (exp));
500 return error_mark_node;
503 type = TREE_TYPE (t);
505 /* As of Valley Forge, you can delete a pointer to const. */
507 /* You can't delete functions. */
508 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
510 error ("cannot delete a function. Only pointer-to-objects are "
511 "valid arguments to %<delete%>");
512 return error_mark_node;
515 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
516 if (VOID_TYPE_P (TREE_TYPE (type)))
518 warning (OPT_Wdelete_incomplete, "deleting %qT is undefined", type);
519 doing_vec = 0;
522 /* Deleting a pointer with the value zero is valid and has no effect. */
523 if (integer_zerop (t))
524 return build1 (NOP_EXPR, void_type_node, t);
526 if (doing_vec)
527 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
528 sfk_deleting_destructor,
529 use_global_delete, complain);
530 else
531 return build_delete (type, t, sfk_deleting_destructor,
532 LOOKUP_NORMAL, use_global_delete,
533 complain);
536 /* Report an error if the indicated template declaration is not the
537 sort of thing that should be a member template. */
539 void
540 check_member_template (tree tmpl)
542 tree decl;
544 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
545 decl = DECL_TEMPLATE_RESULT (tmpl);
547 if (TREE_CODE (decl) == FUNCTION_DECL
548 || DECL_ALIAS_TEMPLATE_P (tmpl)
549 || (TREE_CODE (decl) == TYPE_DECL
550 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
552 /* The parser rejects template declarations in local classes
553 (with the exception of generic lambdas). */
554 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
555 /* The parser rejects any use of virtual in a function template. */
556 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
557 && DECL_VIRTUAL_P (decl)));
559 /* The debug-information generating code doesn't know what to do
560 with member templates. */
561 DECL_IGNORED_P (tmpl) = 1;
563 else if (variable_template_p (tmpl))
564 /* OK */;
565 else
566 error ("template declaration of %q#D", decl);
569 /* Sanity check: report error if this function FUNCTION is not
570 really a member of the class (CTYPE) it is supposed to belong to.
571 TEMPLATE_PARMS is used to specify the template parameters of a member
572 template passed as FUNCTION_DECL. If the member template is passed as a
573 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
574 from the declaration. If the function is not a function template, it
575 must be NULL.
576 It returns the original declaration for the function, NULL_TREE if
577 no declaration was found, error_mark_node if an error was emitted. */
579 tree
580 check_classfn (tree ctype, tree function, tree template_parms)
582 if (DECL_USE_TEMPLATE (function)
583 && !(TREE_CODE (function) == TEMPLATE_DECL
584 && DECL_TEMPLATE_SPECIALIZATION (function))
585 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
586 /* Since this is a specialization of a member template,
587 we're not going to find the declaration in the class.
588 For example, in:
590 struct S { template <typename T> void f(T); };
591 template <> void S::f(int);
593 we're not going to find `S::f(int)', but there's no
594 reason we should, either. We let our callers know we didn't
595 find the method, but we don't complain. */
596 return NULL_TREE;
598 /* Basic sanity check: for a template function, the template parameters
599 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
600 if (TREE_CODE (function) == TEMPLATE_DECL)
602 if (template_parms
603 && !comp_template_parms (template_parms,
604 DECL_TEMPLATE_PARMS (function)))
606 error ("template parameter lists provided don%'t match the "
607 "template parameters of %qD", function);
608 return error_mark_node;
610 template_parms = DECL_TEMPLATE_PARMS (function);
613 /* OK, is this a definition of a member template? */
614 bool is_template = (template_parms != NULL_TREE);
616 /* [temp.mem]
618 A destructor shall not be a member template. */
619 if (DECL_DESTRUCTOR_P (function) && is_template)
621 error ("destructor %qD declared as member template", function);
622 return error_mark_node;
625 /* We must enter the scope here, because conversion operators are
626 named by target type, and type equivalence relies on typenames
627 resolving within the scope of CTYPE. */
628 tree pushed_scope = push_scope (ctype);
629 tree matched = NULL_TREE;
630 tree fns = get_class_binding (ctype, DECL_NAME (function));
632 for (ovl_iterator iter (fns); !matched && iter; ++iter)
634 tree fndecl = *iter;
636 /* A member template definition only matches a member template
637 declaration. */
638 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
639 continue;
641 if (!DECL_DECLARES_FUNCTION_P (fndecl))
642 continue;
644 tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
645 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
647 /* We cannot simply call decls_match because this doesn't work
648 for static member functions that are pretending to be
649 methods, and because the name may have been changed by
650 asm("new_name"). */
652 /* Get rid of the this parameter on functions that become
653 static. */
654 if (DECL_STATIC_FUNCTION_P (fndecl)
655 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
656 p1 = TREE_CHAIN (p1);
658 /* ref-qualifier or absence of same must match. */
659 if (type_memfn_rqual (TREE_TYPE (function))
660 != type_memfn_rqual (TREE_TYPE (fndecl)))
661 continue;
663 // Include constraints in the match.
664 tree c1 = get_constraints (function);
665 tree c2 = get_constraints (fndecl);
667 /* While finding a match, same types and params are not enough
668 if the function is versioned. Also check version ("target")
669 attributes. */
670 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
671 TREE_TYPE (TREE_TYPE (fndecl)))
672 && compparms (p1, p2)
673 && !targetm.target_option.function_versions (function, fndecl)
674 && (!is_template
675 || comp_template_parms (template_parms,
676 DECL_TEMPLATE_PARMS (fndecl)))
677 && equivalent_constraints (c1, c2)
678 && (DECL_TEMPLATE_SPECIALIZATION (function)
679 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
680 && (!DECL_TEMPLATE_SPECIALIZATION (function)
681 || (DECL_TI_TEMPLATE (function) == DECL_TI_TEMPLATE (fndecl))))
682 matched = fndecl;
685 if (!matched)
687 if (!COMPLETE_TYPE_P (ctype))
688 cxx_incomplete_type_error (function, ctype);
689 else
691 if (DECL_CONV_FN_P (function))
692 fns = get_class_binding (ctype, conv_op_identifier);
694 error_at (DECL_SOURCE_LOCATION (function),
695 "no declaration matches %q#D", function);
696 if (fns)
697 print_candidates (fns);
698 else if (DECL_CONV_FN_P (function))
699 inform (DECL_SOURCE_LOCATION (function),
700 "no conversion operators declared");
701 else
702 inform (DECL_SOURCE_LOCATION (function),
703 "no functions named %qD", function);
704 inform (DECL_SOURCE_LOCATION (TYPE_NAME (ctype)),
705 "%#qT defined here", ctype);
707 matched = error_mark_node;
710 if (pushed_scope)
711 pop_scope (pushed_scope);
713 return matched;
716 /* DECL is a function with vague linkage. Remember it so that at the
717 end of the translation unit we can decide whether or not to emit
718 it. */
720 void
721 note_vague_linkage_fn (tree decl)
723 if (processing_template_decl)
724 return;
726 DECL_DEFER_OUTPUT (decl) = 1;
727 vec_safe_push (deferred_fns, decl);
730 /* As above, but for variable template instantiations. */
732 void
733 note_variable_template_instantiation (tree decl)
735 vec_safe_push (pending_statics, decl);
738 /* We have just processed the DECL, which is a static data member.
739 The other parameters are as for cp_finish_decl. */
741 void
742 finish_static_data_member_decl (tree decl,
743 tree init, bool init_const_expr_p,
744 tree asmspec_tree,
745 int flags)
747 DECL_CONTEXT (decl) = current_class_type;
749 /* We cannot call pushdecl here, because that would fill in the
750 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
751 the right thing, namely, to put this decl out straight away. */
753 if (! processing_template_decl)
754 vec_safe_push (pending_statics, decl);
756 if (LOCAL_CLASS_P (current_class_type)
757 /* We already complained about the template definition. */
758 && !DECL_TEMPLATE_INSTANTIATION (decl))
759 permerror (input_location, "local class %q#T shall not have static data member %q#D",
760 current_class_type, decl);
761 else
762 for (tree t = current_class_type; TYPE_P (t);
763 t = CP_TYPE_CONTEXT (t))
764 if (TYPE_UNNAMED_P (t))
766 if (permerror (DECL_SOURCE_LOCATION (decl),
767 "static data member %qD in unnamed class", decl))
768 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
769 "unnamed class defined here");
770 break;
773 DECL_IN_AGGR_P (decl) = 1;
775 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
776 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
777 SET_VAR_HAD_UNKNOWN_BOUND (decl);
779 if (init)
781 /* Similarly to start_decl_1, we want to complete the type in order
782 to do the right thing in cp_apply_type_quals_to_decl, possibly
783 clear TYPE_QUAL_CONST (c++/65579). */
784 tree type = TREE_TYPE (decl) = complete_type (TREE_TYPE (decl));
785 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
788 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
791 /* DECLARATOR and DECLSPECS correspond to a class member. The other
792 parameters are as for cp_finish_decl. Return the DECL for the
793 class member declared. */
795 tree
796 grokfield (const cp_declarator *declarator,
797 cp_decl_specifier_seq *declspecs,
798 tree init, bool init_const_expr_p,
799 tree asmspec_tree,
800 tree attrlist)
802 tree value;
803 const char *asmspec = 0;
804 int flags;
805 tree name;
807 if (init
808 && TREE_CODE (init) == TREE_LIST
809 && TREE_VALUE (init) == error_mark_node
810 && TREE_CHAIN (init) == NULL_TREE)
811 init = NULL_TREE;
813 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
814 if (! value || value == error_mark_node)
815 /* friend or constructor went bad. */
816 return error_mark_node;
817 if (TREE_TYPE (value) == error_mark_node)
818 return value;
820 if (TREE_CODE (value) == TYPE_DECL && init)
822 error ("typedef %qD is initialized (use decltype instead)", value);
823 init = NULL_TREE;
826 /* Pass friendly classes back. */
827 if (value == void_type_node)
828 return value;
831 name = DECL_NAME (value);
833 if (name != NULL_TREE)
835 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
837 error ("explicit template argument list not allowed");
838 return error_mark_node;
841 if (IDENTIFIER_POINTER (name)[0] == '_'
842 && id_equal (name, "_vptr"))
843 error ("member %qD conflicts with virtual function table field name",
844 value);
847 /* Stash away type declarations. */
848 if (TREE_CODE (value) == TYPE_DECL)
850 DECL_NONLOCAL (value) = 1;
851 DECL_CONTEXT (value) = current_class_type;
853 if (attrlist)
855 int attrflags = 0;
857 /* If this is a typedef that names the class for linkage purposes
858 (7.1.3p8), apply any attributes directly to the type. */
859 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
860 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
861 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
863 cplus_decl_attributes (&value, attrlist, attrflags);
866 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
867 && TREE_TYPE (value) != error_mark_node
868 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
869 set_underlying_type (value);
871 /* It's important that push_template_decl below follows
872 set_underlying_type above so that the created template
873 carries the properly set type of VALUE. */
874 if (processing_template_decl)
875 value = push_template_decl (value);
877 record_locally_defined_typedef (value);
878 return value;
881 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
883 if (!friendp && DECL_IN_AGGR_P (value))
885 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
886 return void_type_node;
889 if (asmspec_tree && asmspec_tree != error_mark_node)
890 asmspec = TREE_STRING_POINTER (asmspec_tree);
892 if (init)
894 if (TREE_CODE (value) == FUNCTION_DECL)
896 if (init == ridpointers[(int)RID_DELETE])
898 if (friendp && decl_defined_p (value))
900 error ("redefinition of %q#D", value);
901 inform (DECL_SOURCE_LOCATION (value),
902 "%q#D previously defined here", value);
904 else
906 DECL_DELETED_FN (value) = 1;
907 DECL_DECLARED_INLINE_P (value) = 1;
908 DECL_INITIAL (value) = error_mark_node;
911 else if (init == ridpointers[(int)RID_DEFAULT])
913 if (defaultable_fn_check (value))
915 DECL_DEFAULTED_FN (value) = 1;
916 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
917 DECL_DECLARED_INLINE_P (value) = 1;
920 else if (TREE_CODE (init) == DEFAULT_ARG)
921 error ("invalid initializer for member function %qD", value);
922 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
924 if (integer_zerop (init))
925 DECL_PURE_VIRTUAL_P (value) = 1;
926 else if (error_operand_p (init))
927 ; /* An error has already been reported. */
928 else
929 error ("invalid initializer for member function %qD",
930 value);
932 else
934 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
935 if (friendp)
936 error ("initializer specified for friend function %qD",
937 value);
938 else
939 error ("initializer specified for static member function %qD",
940 value);
943 else if (TREE_CODE (value) == FIELD_DECL)
944 /* C++11 NSDMI, keep going. */;
945 else if (!VAR_P (value))
946 gcc_unreachable ();
949 /* Pass friend decls back. */
950 if ((TREE_CODE (value) == FUNCTION_DECL
951 || TREE_CODE (value) == TEMPLATE_DECL)
952 && DECL_CONTEXT (value) != current_class_type)
953 return value;
955 /* Need to set this before push_template_decl. */
956 if (VAR_P (value))
957 DECL_CONTEXT (value) = current_class_type;
959 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
961 value = push_template_decl (value);
962 if (error_operand_p (value))
963 return error_mark_node;
966 if (attrlist)
967 cplus_decl_attributes (&value, attrlist, 0);
969 if (init && DIRECT_LIST_INIT_P (init))
970 flags = LOOKUP_NORMAL;
971 else
972 flags = LOOKUP_IMPLICIT;
974 switch (TREE_CODE (value))
976 case VAR_DECL:
977 finish_static_data_member_decl (value, init, init_const_expr_p,
978 asmspec_tree, flags);
979 return value;
981 case FIELD_DECL:
982 if (asmspec)
983 error ("%<asm%> specifiers are not permitted on non-static data members");
984 if (DECL_INITIAL (value) == error_mark_node)
985 init = error_mark_node;
986 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
987 NULL_TREE, flags);
988 DECL_IN_AGGR_P (value) = 1;
989 return value;
991 case FUNCTION_DECL:
992 if (asmspec)
993 set_user_assembler_name (value, asmspec);
995 cp_finish_decl (value,
996 /*init=*/NULL_TREE,
997 /*init_const_expr_p=*/false,
998 asmspec_tree, flags);
1000 /* Pass friends back this way. */
1001 if (DECL_FRIEND_P (value))
1002 return void_type_node;
1004 DECL_IN_AGGR_P (value) = 1;
1005 return value;
1007 default:
1008 gcc_unreachable ();
1010 return NULL_TREE;
1013 /* Like `grokfield', but for bitfields.
1014 WIDTH is the width of the bitfield, a constant expression.
1015 The other parameters are as for grokfield. */
1017 tree
1018 grokbitfield (const cp_declarator *declarator,
1019 cp_decl_specifier_seq *declspecs, tree width, tree init,
1020 tree attrlist)
1022 tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1023 init != NULL_TREE, &attrlist);
1025 if (value == error_mark_node)
1026 return NULL_TREE; /* friends went bad. */
1027 if (TREE_TYPE (value) == error_mark_node)
1028 return value;
1030 /* Pass friendly classes back. */
1031 if (VOID_TYPE_P (value))
1032 return void_type_node;
1034 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
1035 && (INDIRECT_TYPE_P (value)
1036 || !dependent_type_p (TREE_TYPE (value))))
1038 error ("bit-field %qD with non-integral type", value);
1039 return error_mark_node;
1042 if (TREE_CODE (value) == TYPE_DECL)
1044 error ("cannot declare %qD to be a bit-field type", value);
1045 return NULL_TREE;
1048 /* Usually, finish_struct_1 catches bitfields with invalid types.
1049 But, in the case of bitfields with function type, we confuse
1050 ourselves into thinking they are member functions, so we must
1051 check here. */
1052 if (TREE_CODE (value) == FUNCTION_DECL)
1054 error ("cannot declare bit-field %qD with function type",
1055 DECL_NAME (value));
1056 return NULL_TREE;
1059 if (width && TYPE_WARN_IF_NOT_ALIGN (TREE_TYPE (value)))
1061 error ("cannot declare bit-field %qD with %<warn_if_not_aligned%> type",
1062 DECL_NAME (value));
1063 return NULL_TREE;
1066 if (DECL_IN_AGGR_P (value))
1068 error ("%qD is already defined in the class %qT", value,
1069 DECL_CONTEXT (value));
1070 return void_type_node;
1073 if (TREE_STATIC (value))
1075 error ("static member %qD cannot be a bit-field", value);
1076 return NULL_TREE;
1079 int flags = LOOKUP_IMPLICIT;
1080 if (init && DIRECT_LIST_INIT_P (init))
1081 flags = LOOKUP_NORMAL;
1082 cp_finish_decl (value, init, false, NULL_TREE, flags);
1084 if (width != error_mark_node)
1086 /* The width must be an integer type. */
1087 if (!type_dependent_expression_p (width)
1088 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1089 error ("width of bit-field %qD has non-integral type %qT", value,
1090 TREE_TYPE (width));
1091 else
1093 /* Temporarily stash the width in DECL_BIT_FIELD_REPRESENTATIVE.
1094 check_bitfield_decl picks it from there later and sets DECL_SIZE
1095 accordingly. */
1096 DECL_BIT_FIELD_REPRESENTATIVE (value) = width;
1097 SET_DECL_C_BIT_FIELD (value);
1101 DECL_IN_AGGR_P (value) = 1;
1103 if (attrlist)
1104 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1106 return value;
1110 /* Returns true iff ATTR is an attribute which needs to be applied at
1111 instantiation time rather than template definition time. */
1113 static bool
1114 is_late_template_attribute (tree attr, tree decl)
1116 tree name = get_attribute_name (attr);
1117 tree args = TREE_VALUE (attr);
1118 const struct attribute_spec *spec = lookup_attribute_spec (name);
1119 tree arg;
1121 if (!spec)
1122 /* Unknown attribute. */
1123 return false;
1125 /* Attribute weak handling wants to write out assembly right away. */
1126 if (is_attribute_p ("weak", name))
1127 return true;
1129 /* Attributes used and unused are applied directly to typedefs for the
1130 benefit of maybe_warn_unused_local_typedefs. */
1131 if (TREE_CODE (decl) == TYPE_DECL
1132 && (is_attribute_p ("unused", name)
1133 || is_attribute_p ("used", name)))
1134 return false;
1136 /* Attribute tls_model wants to modify the symtab. */
1137 if (is_attribute_p ("tls_model", name))
1138 return true;
1140 /* #pragma omp declare simd attribute needs to be always deferred. */
1141 if (flag_openmp
1142 && is_attribute_p ("omp declare simd", name))
1143 return true;
1145 /* An attribute pack is clearly dependent. */
1146 if (args && PACK_EXPANSION_P (args))
1147 return true;
1149 /* If any of the arguments are dependent expressions, we can't evaluate
1150 the attribute until instantiation time. */
1151 for (arg = args; arg; arg = TREE_CHAIN (arg))
1153 tree t = TREE_VALUE (arg);
1155 /* If the first attribute argument is an identifier, only consider
1156 second and following arguments. Attributes like mode, format,
1157 cleanup and several target specific attributes aren't late
1158 just because they have an IDENTIFIER_NODE as first argument. */
1159 if (arg == args && attribute_takes_identifier_p (name)
1160 && identifier_p (t))
1161 continue;
1163 if (value_dependent_expression_p (t)
1164 || type_dependent_expression_p (t))
1165 return true;
1168 if (TREE_CODE (decl) == TYPE_DECL
1169 || TYPE_P (decl)
1170 || spec->type_required)
1172 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1174 /* We can't apply any attributes to a completely unknown type until
1175 instantiation time. */
1176 enum tree_code code = TREE_CODE (type);
1177 if (code == TEMPLATE_TYPE_PARM
1178 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1179 || code == TYPENAME_TYPE)
1180 return true;
1181 /* Also defer most attributes on dependent types. This is not
1182 necessary in all cases, but is the better default. */
1183 else if (dependent_type_p (type)
1184 /* But some attributes specifically apply to templates. */
1185 && !is_attribute_p ("abi_tag", name)
1186 && !is_attribute_p ("deprecated", name)
1187 && !is_attribute_p ("visibility", name))
1188 return true;
1189 else
1190 return false;
1192 else
1193 return false;
1196 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1197 applied at instantiation time and return them. If IS_DEPENDENT is true,
1198 the declaration itself is dependent, so all attributes should be applied
1199 at instantiation time. */
1201 static tree
1202 splice_template_attributes (tree *attr_p, tree decl)
1204 tree *p = attr_p;
1205 tree late_attrs = NULL_TREE;
1206 tree *q = &late_attrs;
1208 if (!p)
1209 return NULL_TREE;
1211 for (; *p; )
1213 if (is_late_template_attribute (*p, decl))
1215 ATTR_IS_DEPENDENT (*p) = 1;
1216 *q = *p;
1217 *p = TREE_CHAIN (*p);
1218 q = &TREE_CHAIN (*q);
1219 *q = NULL_TREE;
1221 else
1222 p = &TREE_CHAIN (*p);
1225 return late_attrs;
1228 /* Remove any late attributes from the list in ATTR_P and attach them to
1229 DECL_P. */
1231 static void
1232 save_template_attributes (tree *attr_p, tree *decl_p, int flags)
1234 tree *q;
1236 if (attr_p && *attr_p == error_mark_node)
1237 return;
1239 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1240 if (!late_attrs)
1241 return;
1243 if (DECL_P (*decl_p))
1244 q = &DECL_ATTRIBUTES (*decl_p);
1245 else
1246 q = &TYPE_ATTRIBUTES (*decl_p);
1248 tree old_attrs = *q;
1250 /* Merge the late attributes at the beginning with the attribute
1251 list. */
1252 late_attrs = merge_attributes (late_attrs, *q);
1253 if (*q != late_attrs
1254 && !DECL_P (*decl_p)
1255 && !(flags & ATTR_FLAG_TYPE_IN_PLACE))
1257 if (!dependent_type_p (*decl_p))
1258 *decl_p = cp_build_type_attribute_variant (*decl_p, late_attrs);
1259 else
1261 *decl_p = build_variant_type_copy (*decl_p);
1262 TYPE_ATTRIBUTES (*decl_p) = late_attrs;
1265 else
1266 *q = late_attrs;
1268 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1270 /* We've added new attributes directly to the main variant, so
1271 now we need to update all of the other variants to include
1272 these new attributes. */
1273 tree variant;
1274 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1275 variant = TYPE_NEXT_VARIANT (variant))
1277 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1278 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1283 /* True if ATTRS contains any dependent attributes that affect type
1284 identity. */
1286 bool
1287 any_dependent_type_attributes_p (tree attrs)
1289 for (tree a = attrs; a; a = TREE_CHAIN (a))
1290 if (ATTR_IS_DEPENDENT (a))
1292 const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (a));
1293 if (as && as->affects_type_identity)
1294 return true;
1296 return false;
1299 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1300 to a typedef which gives a previously unnamed class or enum a name for
1301 linkage purposes. */
1303 bool
1304 attributes_naming_typedef_ok (tree attrs)
1306 for (; attrs; attrs = TREE_CHAIN (attrs))
1308 tree name = get_attribute_name (attrs);
1309 if (is_attribute_p ("vector_size", name))
1310 return false;
1312 return true;
1315 /* Like reconstruct_complex_type, but handle also template trees. */
1317 tree
1318 cp_reconstruct_complex_type (tree type, tree bottom)
1320 tree inner, outer;
1322 if (TYPE_PTR_P (type))
1324 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1325 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1326 TYPE_REF_CAN_ALIAS_ALL (type));
1328 else if (TYPE_REF_P (type))
1330 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1331 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1332 TYPE_REF_CAN_ALIAS_ALL (type));
1334 else if (TREE_CODE (type) == ARRAY_TYPE)
1336 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1337 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1338 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1339 element type qualification will be handled by the recursive
1340 cp_reconstruct_complex_type call and cp_build_qualified_type
1341 for ARRAY_TYPEs changes the element type. */
1342 return outer;
1344 else if (TREE_CODE (type) == FUNCTION_TYPE)
1346 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1347 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1348 outer = apply_memfn_quals (outer, type_memfn_quals (type));
1350 else if (TREE_CODE (type) == METHOD_TYPE)
1352 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1353 /* The build_method_type_directly() routine prepends 'this' to argument list,
1354 so we must compensate by getting rid of it. */
1355 outer
1356 = build_method_type_directly
1357 (class_of_this_parm (type), inner,
1358 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1360 else if (TREE_CODE (type) == OFFSET_TYPE)
1362 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1363 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1365 else
1366 return bottom;
1368 if (TYPE_ATTRIBUTES (type))
1369 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1370 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1371 outer = cxx_copy_lang_qualifiers (outer, type);
1373 return outer;
1376 /* Replaces any constexpr expression that may be into the attributes
1377 arguments with their reduced value. */
1379 static void
1380 cp_check_const_attributes (tree attributes)
1382 if (attributes == error_mark_node)
1383 return;
1385 tree attr;
1386 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1388 tree arg;
1389 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1391 tree expr = TREE_VALUE (arg);
1392 if (EXPR_P (expr))
1393 TREE_VALUE (arg) = fold_non_dependent_expr (expr);
1398 /* Return true if TYPE is an OpenMP mappable type. */
1399 bool
1400 cp_omp_mappable_type (tree type)
1402 /* Mappable type has to be complete. */
1403 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1404 return false;
1405 /* Arrays have mappable type if the elements have mappable type. */
1406 while (TREE_CODE (type) == ARRAY_TYPE)
1407 type = TREE_TYPE (type);
1408 /* A mappable type cannot contain virtual members. */
1409 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1410 return false;
1411 /* All data members must be non-static. */
1412 if (CLASS_TYPE_P (type))
1414 tree field;
1415 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1416 if (VAR_P (field))
1417 return false;
1418 /* All fields must have mappable types. */
1419 else if (TREE_CODE (field) == FIELD_DECL
1420 && !cp_omp_mappable_type (TREE_TYPE (field)))
1421 return false;
1423 return true;
1426 /* Return the last pushed declaration for the symbol DECL or NULL
1427 when no such declaration exists. */
1429 static tree
1430 find_last_decl (tree decl)
1432 tree last_decl = NULL_TREE;
1434 if (tree name = DECL_P (decl) ? DECL_NAME (decl) : NULL_TREE)
1436 /* Look up the declaration in its scope. */
1437 tree pushed_scope = NULL_TREE;
1438 if (tree ctype = DECL_CONTEXT (decl))
1439 pushed_scope = push_scope (ctype);
1441 last_decl = lookup_name (name);
1443 if (pushed_scope)
1444 pop_scope (pushed_scope);
1446 /* The declaration may be a member conversion operator
1447 or a bunch of overfloads (handle the latter below). */
1448 if (last_decl && BASELINK_P (last_decl))
1449 last_decl = BASELINK_FUNCTIONS (last_decl);
1452 if (!last_decl)
1453 return NULL_TREE;
1455 if (DECL_P (last_decl) || TREE_CODE (last_decl) == OVERLOAD)
1457 /* A set of overloads of the same function. */
1458 for (lkp_iterator iter (last_decl); iter; ++iter)
1460 if (TREE_CODE (*iter) == OVERLOAD)
1461 continue;
1463 if (decls_match (decl, *iter, /*record_decls=*/false))
1464 return *iter;
1466 return NULL_TREE;
1469 return NULL_TREE;
1472 /* Like decl_attributes, but handle C++ complexity. */
1474 void
1475 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1477 if (*decl == NULL_TREE || *decl == void_type_node
1478 || *decl == error_mark_node)
1479 return;
1481 /* Add implicit "omp declare target" attribute if requested. */
1482 if (scope_chain->omp_declare_target_attribute
1483 && ((VAR_P (*decl)
1484 && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
1485 || TREE_CODE (*decl) == FUNCTION_DECL))
1487 if (VAR_P (*decl)
1488 && DECL_CLASS_SCOPE_P (*decl))
1489 error ("%q+D static data member inside of declare target directive",
1490 *decl);
1491 else if (!processing_template_decl
1492 && VAR_P (*decl)
1493 && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1494 error ("%q+D in declare target directive does not have mappable type",
1495 *decl);
1496 else
1497 attributes = tree_cons (get_identifier ("omp declare target"),
1498 NULL_TREE, attributes);
1501 if (processing_template_decl)
1503 if (check_for_bare_parameter_packs (attributes))
1504 return;
1506 save_template_attributes (&attributes, decl, flags);
1509 cp_check_const_attributes (attributes);
1511 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1512 decl = &DECL_TEMPLATE_RESULT (*decl);
1514 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1516 attributes
1517 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1518 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1519 attributes, flags);
1521 else
1523 tree last_decl = find_last_decl (*decl);
1524 decl_attributes (decl, attributes, flags, last_decl);
1527 if (TREE_CODE (*decl) == TYPE_DECL)
1528 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1530 /* Propagate deprecation out to the template. */
1531 if (TREE_DEPRECATED (*decl))
1532 if (tree ti = get_template_info (*decl))
1534 tree tmpl = TI_TEMPLATE (ti);
1535 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1536 : DECL_TEMPLATE_RESULT (tmpl));
1537 if (*decl == pattern)
1538 TREE_DEPRECATED (tmpl) = true;
1542 /* Walks through the namespace- or function-scope anonymous union
1543 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1544 Returns one of the fields for use in the mangled name. */
1546 static tree
1547 build_anon_union_vars (tree type, tree object)
1549 tree main_decl = NULL_TREE;
1550 tree field;
1552 /* Rather than write the code to handle the non-union case,
1553 just give an error. */
1554 if (TREE_CODE (type) != UNION_TYPE)
1556 error ("anonymous struct not inside named type");
1557 return error_mark_node;
1560 for (field = TYPE_FIELDS (type);
1561 field != NULL_TREE;
1562 field = DECL_CHAIN (field))
1564 tree decl;
1565 tree ref;
1567 if (DECL_ARTIFICIAL (field))
1568 continue;
1569 if (TREE_CODE (field) != FIELD_DECL)
1571 permerror (DECL_SOURCE_LOCATION (field),
1572 "%q#D invalid; an anonymous union can only "
1573 "have non-static data members", field);
1574 continue;
1577 if (TREE_PRIVATE (field))
1578 permerror (DECL_SOURCE_LOCATION (field),
1579 "private member %q#D in anonymous union", field);
1580 else if (TREE_PROTECTED (field))
1581 permerror (DECL_SOURCE_LOCATION (field),
1582 "protected member %q#D in anonymous union", field);
1584 if (processing_template_decl)
1585 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1586 DECL_NAME (field), NULL_TREE);
1587 else
1588 ref = build_class_member_access_expr (object, field, NULL_TREE,
1589 false, tf_warning_or_error);
1591 if (DECL_NAME (field))
1593 tree base;
1595 decl = build_decl (input_location,
1596 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1597 DECL_ANON_UNION_VAR_P (decl) = 1;
1598 DECL_ARTIFICIAL (decl) = 1;
1600 base = get_base_address (object);
1601 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1602 TREE_STATIC (decl) = TREE_STATIC (base);
1603 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1605 SET_DECL_VALUE_EXPR (decl, ref);
1606 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1608 decl = pushdecl (decl);
1610 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1611 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1612 else
1613 decl = 0;
1615 if (main_decl == NULL_TREE)
1616 main_decl = decl;
1619 return main_decl;
1622 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1623 anonymous union, then all members must be laid out together. PUBLIC_P
1624 is nonzero if this union is not declared static. */
1626 void
1627 finish_anon_union (tree anon_union_decl)
1629 tree type;
1630 tree main_decl;
1631 bool public_p;
1633 if (anon_union_decl == error_mark_node)
1634 return;
1636 type = TREE_TYPE (anon_union_decl);
1637 public_p = TREE_PUBLIC (anon_union_decl);
1639 /* The VAR_DECL's context is the same as the TYPE's context. */
1640 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1642 if (TYPE_FIELDS (type) == NULL_TREE)
1643 return;
1645 if (public_p)
1647 error ("namespace-scope anonymous aggregates must be static");
1648 return;
1651 main_decl = build_anon_union_vars (type, anon_union_decl);
1652 if (main_decl == error_mark_node)
1653 return;
1654 if (main_decl == NULL_TREE)
1656 pedwarn (input_location, 0, "anonymous union with no members");
1657 return;
1660 if (!processing_template_decl)
1662 /* Use main_decl to set the mangled name. */
1663 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1664 maybe_commonize_var (anon_union_decl);
1665 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1666 mangle_decl (anon_union_decl);
1667 DECL_NAME (anon_union_decl) = NULL_TREE;
1670 pushdecl (anon_union_decl);
1671 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1674 /* Auxiliary functions to make type signatures for
1675 `operator new' and `operator delete' correspond to
1676 what compiler will be expecting. */
1678 tree
1679 coerce_new_type (tree type)
1681 int e = 0;
1682 tree args = TYPE_ARG_TYPES (type);
1684 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1686 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1688 e = 1;
1689 error ("%<operator new%> must return type %qT", ptr_type_node);
1692 if (args && args != void_list_node)
1694 if (TREE_PURPOSE (args))
1696 /* [basic.stc.dynamic.allocation]
1698 The first parameter shall not have an associated default
1699 argument. */
1700 error ("the first parameter of %<operator new%> cannot "
1701 "have a default argument");
1702 /* Throw away the default argument. */
1703 TREE_PURPOSE (args) = NULL_TREE;
1706 if (!same_type_p (TREE_VALUE (args), size_type_node))
1708 e = 2;
1709 args = TREE_CHAIN (args);
1712 else
1713 e = 2;
1715 if (e == 2)
1716 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1717 "as first parameter", size_type_node);
1719 switch (e)
1721 case 2:
1722 args = tree_cons (NULL_TREE, size_type_node, args);
1723 /* Fall through. */
1724 case 1:
1725 type = (cxx_copy_lang_qualifiers
1726 (build_function_type (ptr_type_node, args),
1727 type));
1728 /* Fall through. */
1729 default:;
1731 return type;
1734 tree
1735 coerce_delete_type (tree type)
1737 int e = 0;
1738 tree args = TYPE_ARG_TYPES (type);
1740 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1742 if (!same_type_p (TREE_TYPE (type), void_type_node))
1744 e = 1;
1745 error ("%<operator delete%> must return type %qT", void_type_node);
1748 if (!args || args == void_list_node
1749 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1751 e = 2;
1752 if (args && args != void_list_node)
1753 args = TREE_CHAIN (args);
1754 error ("%<operator delete%> takes type %qT as first parameter",
1755 ptr_type_node);
1757 switch (e)
1759 case 2:
1760 args = tree_cons (NULL_TREE, ptr_type_node, args);
1761 /* Fall through. */
1762 case 1:
1763 type = (cxx_copy_lang_qualifiers
1764 (build_function_type (void_type_node, args),
1765 type));
1766 /* Fall through. */
1767 default:;
1770 return type;
1773 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1774 and mark them as needed. */
1776 static void
1777 mark_vtable_entries (tree decl)
1779 tree fnaddr;
1780 unsigned HOST_WIDE_INT idx;
1782 /* It's OK for the vtable to refer to deprecated virtual functions. */
1783 warning_sentinel w(warn_deprecated_decl);
1785 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1786 idx, fnaddr)
1788 tree fn;
1790 STRIP_NOPS (fnaddr);
1792 if (TREE_CODE (fnaddr) != ADDR_EXPR
1793 && TREE_CODE (fnaddr) != FDESC_EXPR)
1794 /* This entry is an offset: a virtual base class offset, a
1795 virtual call offset, an RTTI offset, etc. */
1796 continue;
1798 fn = TREE_OPERAND (fnaddr, 0);
1799 TREE_ADDRESSABLE (fn) = 1;
1800 /* When we don't have vcall offsets, we output thunks whenever
1801 we output the vtables that contain them. With vcall offsets,
1802 we know all the thunks we'll need when we emit a virtual
1803 function, so we emit the thunks there instead. */
1804 if (DECL_THUNK_P (fn))
1805 use_thunk (fn, /*emit_p=*/0);
1806 /* Set the location, as marking the function could cause
1807 instantiation. We do not need to preserve the incoming
1808 location, as we're called from c_parse_final_cleanups, which
1809 takes care of that. */
1810 input_location = DECL_SOURCE_LOCATION (fn);
1811 mark_used (fn);
1815 /* Adjust the TLS model on variable DECL if need be, typically after
1816 the linkage of DECL has been modified. */
1818 static void
1819 adjust_var_decl_tls_model (tree decl)
1821 if (CP_DECL_THREAD_LOCAL_P (decl)
1822 && !lookup_attribute ("tls_model", DECL_ATTRIBUTES (decl)))
1823 set_decl_tls_model (decl, decl_default_tls_model (decl));
1826 /* Set DECL up to have the closest approximation of "initialized common"
1827 linkage available. */
1829 void
1830 comdat_linkage (tree decl)
1832 if (flag_weak)
1833 make_decl_one_only (decl, cxx_comdat_group (decl));
1834 else if (TREE_CODE (decl) == FUNCTION_DECL
1835 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1836 /* We can just emit function and compiler-generated variables
1837 statically; having multiple copies is (for the most part) only
1838 a waste of space.
1840 There are two correctness issues, however: the address of a
1841 template instantiation with external linkage should be the
1842 same, independent of what translation unit asks for the
1843 address, and this will not hold when we emit multiple copies of
1844 the function. However, there's little else we can do.
1846 Also, by default, the typeinfo implementation assumes that
1847 there will be only one copy of the string used as the name for
1848 each type. Therefore, if weak symbols are unavailable, the
1849 run-time library should perform a more conservative check; it
1850 should perform a string comparison, rather than an address
1851 comparison. */
1852 TREE_PUBLIC (decl) = 0;
1853 else
1855 /* Static data member template instantiations, however, cannot
1856 have multiple copies. */
1857 if (DECL_INITIAL (decl) == 0
1858 || DECL_INITIAL (decl) == error_mark_node)
1859 DECL_COMMON (decl) = 1;
1860 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1862 DECL_COMMON (decl) = 1;
1863 DECL_INITIAL (decl) = error_mark_node;
1865 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1867 /* We can't do anything useful; leave vars for explicit
1868 instantiation. */
1869 DECL_EXTERNAL (decl) = 1;
1870 DECL_NOT_REALLY_EXTERN (decl) = 0;
1874 if (TREE_PUBLIC (decl))
1875 DECL_COMDAT (decl) = 1;
1877 if (VAR_P (decl))
1878 adjust_var_decl_tls_model (decl);
1881 /* For win32 we also want to put explicit instantiations in
1882 linkonce sections, so that they will be merged with implicit
1883 instantiations; otherwise we get duplicate symbol errors.
1884 For Darwin we do not want explicit instantiations to be
1885 linkonce. */
1887 void
1888 maybe_make_one_only (tree decl)
1890 /* We used to say that this was not necessary on targets that support weak
1891 symbols, because the implicit instantiations will defer to the explicit
1892 one. However, that's not actually the case in SVR4; a strong definition
1893 after a weak one is an error. Also, not making explicit
1894 instantiations one_only means that we can end up with two copies of
1895 some template instantiations. */
1896 if (! flag_weak)
1897 return;
1899 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1900 we can get away with not emitting them if they aren't used. We need
1901 to for variables so that cp_finish_decl will update their linkage,
1902 because their DECL_INITIAL may not have been set properly yet. */
1904 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1905 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1906 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1908 make_decl_one_only (decl, cxx_comdat_group (decl));
1910 if (VAR_P (decl))
1912 varpool_node *node = varpool_node::get_create (decl);
1913 DECL_COMDAT (decl) = 1;
1914 /* Mark it needed so we don't forget to emit it. */
1915 node->forced_by_abi = true;
1916 TREE_USED (decl) = 1;
1918 adjust_var_decl_tls_model (decl);
1923 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1924 This predicate will give the right answer during parsing of the
1925 function, which other tests may not. */
1927 bool
1928 vague_linkage_p (tree decl)
1930 if (!TREE_PUBLIC (decl))
1932 /* maybe_thunk_body clears TREE_PUBLIC and DECL_ABSTRACT_P on the
1933 maybe-in-charge 'tor variants; in that case we need to check one of
1934 the "clones" for the real linkage. But only in that case; before
1935 maybe_clone_body we haven't yet copied the linkage to the clones. */
1936 if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl)
1937 && !DECL_ABSTRACT_P (decl)
1938 && DECL_CHAIN (decl)
1939 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)))
1940 return vague_linkage_p (DECL_CHAIN (decl));
1942 gcc_checking_assert (!DECL_COMDAT (decl));
1943 return false;
1945 /* Unfortunately, import_export_decl has not always been called
1946 before the function is processed, so we cannot simply check
1947 DECL_COMDAT. */
1948 if (DECL_COMDAT (decl)
1949 || (TREE_CODE (decl) == FUNCTION_DECL
1950 && DECL_DECLARED_INLINE_P (decl))
1951 || (DECL_LANG_SPECIFIC (decl)
1952 && DECL_TEMPLATE_INSTANTIATION (decl))
1953 || (VAR_P (decl) && DECL_INLINE_VAR_P (decl)))
1954 return true;
1955 else if (DECL_FUNCTION_SCOPE_P (decl))
1956 /* A local static in an inline effectively has vague linkage. */
1957 return (TREE_STATIC (decl)
1958 && vague_linkage_p (DECL_CONTEXT (decl)));
1959 else
1960 return false;
1963 /* Determine whether or not we want to specifically import or export CTYPE,
1964 using various heuristics. */
1966 static void
1967 import_export_class (tree ctype)
1969 /* -1 for imported, 1 for exported. */
1970 int import_export = 0;
1972 /* It only makes sense to call this function at EOF. The reason is
1973 that this function looks at whether or not the first non-inline
1974 non-abstract virtual member function has been defined in this
1975 translation unit. But, we can't possibly know that until we've
1976 seen the entire translation unit. */
1977 gcc_assert (at_eof);
1979 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1980 return;
1982 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1983 we will have CLASSTYPE_INTERFACE_ONLY set but not
1984 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1985 heuristic because someone will supply a #pragma implementation
1986 elsewhere, and deducing it here would produce a conflict. */
1987 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1988 return;
1990 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1991 import_export = -1;
1992 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1993 import_export = 1;
1994 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1995 && !flag_implicit_templates)
1996 /* For a template class, without -fimplicit-templates, check the
1997 repository. If the virtual table is assigned to this
1998 translation unit, then export the class; otherwise, import
1999 it. */
2000 import_export = repo_export_class_p (ctype) ? 1 : -1;
2001 else if (TYPE_POLYMORPHIC_P (ctype))
2003 /* The ABI specifies that the virtual table and associated
2004 information are emitted with the key method, if any. */
2005 tree method = CLASSTYPE_KEY_METHOD (ctype);
2006 /* If weak symbol support is not available, then we must be
2007 careful not to emit the vtable when the key function is
2008 inline. An inline function can be defined in multiple
2009 translation units. If we were to emit the vtable in each
2010 translation unit containing a definition, we would get
2011 multiple definition errors at link-time. */
2012 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
2013 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
2016 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
2017 a definition anywhere else. */
2018 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
2019 import_export = 0;
2021 /* Allow back ends the chance to overrule the decision. */
2022 if (targetm.cxx.import_export_class)
2023 import_export = targetm.cxx.import_export_class (ctype, import_export);
2025 if (import_export)
2027 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
2028 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
2032 /* Return true if VAR has already been provided to the back end; in that
2033 case VAR should not be modified further by the front end. */
2034 static bool
2035 var_finalized_p (tree var)
2037 return varpool_node::get_create (var)->definition;
2040 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
2041 must be emitted in this translation unit. Mark it as such. */
2043 void
2044 mark_needed (tree decl)
2046 TREE_USED (decl) = 1;
2047 if (TREE_CODE (decl) == FUNCTION_DECL)
2049 /* Extern inline functions don't become needed when referenced.
2050 If we know a method will be emitted in other TU and no new
2051 functions can be marked reachable, just use the external
2052 definition. */
2053 struct cgraph_node *node = cgraph_node::get_create (decl);
2054 node->forced_by_abi = true;
2056 /* #pragma interface and -frepo code can call mark_needed for
2057 maybe-in-charge 'tors; mark the clones as well. */
2058 tree clone;
2059 FOR_EACH_CLONE (clone, decl)
2060 mark_needed (clone);
2062 else if (VAR_P (decl))
2064 varpool_node *node = varpool_node::get_create (decl);
2065 /* C++ frontend use mark_decl_references to force COMDAT variables
2066 to be output that might appear dead otherwise. */
2067 node->forced_by_abi = true;
2071 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
2072 returns true if a definition of this entity should be provided in
2073 this object file. Callers use this function to determine whether
2074 or not to let the back end know that a definition of DECL is
2075 available in this translation unit. */
2077 bool
2078 decl_needed_p (tree decl)
2080 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2081 /* This function should only be called at the end of the translation
2082 unit. We cannot be sure of whether or not something will be
2083 COMDAT until that point. */
2084 gcc_assert (at_eof);
2086 /* All entities with external linkage that are not COMDAT/EXTERN should be
2087 emitted; they may be referred to from other object files. */
2088 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
2089 return true;
2090 /* Functions marked "dllexport" must be emitted so that they are
2091 visible to other DLLs. */
2092 if (flag_keep_inline_dllexport
2093 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
2094 return true;
2096 /* When not optimizing, do not bother to produce definitions for extern
2097 symbols. */
2098 if (DECL_REALLY_EXTERN (decl)
2099 && ((TREE_CODE (decl) != FUNCTION_DECL
2100 && !optimize)
2101 || (TREE_CODE (decl) == FUNCTION_DECL
2102 && !opt_for_fn (decl, optimize)))
2103 && !lookup_attribute ("always_inline", decl))
2104 return false;
2106 /* If this entity was used, let the back end see it; it will decide
2107 whether or not to emit it into the object file. */
2108 if (TREE_USED (decl))
2109 return true;
2111 /* Virtual functions might be needed for devirtualization. */
2112 if (flag_devirtualize
2113 && TREE_CODE (decl) == FUNCTION_DECL
2114 && DECL_VIRTUAL_P (decl))
2115 return true;
2117 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2118 reference to DECL might cause it to be emitted later. */
2119 return false;
2122 /* If necessary, write out the vtables for the dynamic class CTYPE.
2123 Returns true if any vtables were emitted. */
2125 static bool
2126 maybe_emit_vtables (tree ctype)
2128 tree vtbl;
2129 tree primary_vtbl;
2130 int needed = 0;
2131 varpool_node *current = NULL, *last = NULL;
2133 /* If the vtables for this class have already been emitted there is
2134 nothing more to do. */
2135 primary_vtbl = CLASSTYPE_VTABLES (ctype);
2136 if (var_finalized_p (primary_vtbl))
2137 return false;
2138 /* Ignore dummy vtables made by get_vtable_decl. */
2139 if (TREE_TYPE (primary_vtbl) == void_type_node)
2140 return false;
2142 /* On some targets, we cannot determine the key method until the end
2143 of the translation unit -- which is when this function is
2144 called. */
2145 if (!targetm.cxx.key_method_may_be_inline ())
2146 determine_key_method (ctype);
2148 /* See if any of the vtables are needed. */
2149 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2151 import_export_decl (vtbl);
2152 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2153 needed = 1;
2155 if (!needed)
2157 /* If the references to this class' vtables are optimized away,
2158 still emit the appropriate debugging information. See
2159 dfs_debug_mark. */
2160 if (DECL_COMDAT (primary_vtbl)
2161 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2162 note_debug_info_needed (ctype);
2163 return false;
2166 /* The ABI requires that we emit all of the vtables if we emit any
2167 of them. */
2168 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2170 /* Mark entities references from the virtual table as used. */
2171 mark_vtable_entries (vtbl);
2173 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2175 vec<tree, va_gc> *cleanups = NULL;
2176 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2177 LOOKUP_NORMAL);
2179 /* It had better be all done at compile-time. */
2180 gcc_assert (!expr && !cleanups);
2183 /* Write it out. */
2184 DECL_EXTERNAL (vtbl) = 0;
2185 rest_of_decl_compilation (vtbl, 1, 1);
2187 /* Because we're only doing syntax-checking, we'll never end up
2188 actually marking the variable as written. */
2189 if (flag_syntax_only)
2190 TREE_ASM_WRITTEN (vtbl) = 1;
2191 else if (DECL_ONE_ONLY (vtbl))
2193 current = varpool_node::get_create (vtbl);
2194 if (last)
2195 current->add_to_same_comdat_group (last);
2196 last = current;
2200 /* Since we're writing out the vtable here, also write the debug
2201 info. */
2202 note_debug_info_needed (ctype);
2204 return true;
2207 /* A special return value from type_visibility meaning internal
2208 linkage. */
2210 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2212 /* walk_tree helper function for type_visibility. */
2214 static tree
2215 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2217 int *vis_p = (int *)data;
2218 if (! TYPE_P (*tp))
2220 *walk_subtrees = 0;
2222 else if (OVERLOAD_TYPE_P (*tp)
2223 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2225 *vis_p = VISIBILITY_ANON;
2226 return *tp;
2228 else if (CLASS_TYPE_P (*tp)
2229 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2230 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2231 return NULL;
2234 /* Returns the visibility of TYPE, which is the minimum visibility of its
2235 component types. */
2237 static int
2238 type_visibility (tree type)
2240 int vis = VISIBILITY_DEFAULT;
2241 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2242 return vis;
2245 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2246 specified (or if VISIBILITY is static). If TMPL is true, this
2247 constraint is for a template argument, and takes precedence
2248 over explicitly-specified visibility on the template. */
2250 static void
2251 constrain_visibility (tree decl, int visibility, bool tmpl)
2253 if (visibility == VISIBILITY_ANON)
2255 /* extern "C" declarations aren't affected by the anonymous
2256 namespace. */
2257 if (!DECL_EXTERN_C_P (decl))
2259 TREE_PUBLIC (decl) = 0;
2260 DECL_WEAK (decl) = 0;
2261 DECL_COMMON (decl) = 0;
2262 DECL_COMDAT (decl) = false;
2263 if (VAR_OR_FUNCTION_DECL_P (decl))
2265 struct symtab_node *snode = symtab_node::get (decl);
2267 if (snode)
2268 snode->set_comdat_group (NULL);
2270 DECL_INTERFACE_KNOWN (decl) = 1;
2271 if (DECL_LANG_SPECIFIC (decl))
2272 DECL_NOT_REALLY_EXTERN (decl) = 1;
2275 else if (visibility > DECL_VISIBILITY (decl)
2276 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2278 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2279 /* This visibility was not specified. */
2280 DECL_VISIBILITY_SPECIFIED (decl) = false;
2284 /* Constrain the visibility of DECL based on the visibility of its template
2285 arguments. */
2287 static void
2288 constrain_visibility_for_template (tree decl, tree targs)
2290 /* If this is a template instantiation, check the innermost
2291 template args for visibility constraints. The outer template
2292 args are covered by the class check. */
2293 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2294 int i;
2295 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2297 int vis = 0;
2299 tree arg = TREE_VEC_ELT (args, i-1);
2300 if (TYPE_P (arg))
2301 vis = type_visibility (arg);
2302 else
2304 if (REFERENCE_REF_P (arg))
2305 arg = TREE_OPERAND (arg, 0);
2306 if (TREE_TYPE (arg))
2307 STRIP_NOPS (arg);
2308 if (TREE_CODE (arg) == ADDR_EXPR)
2309 arg = TREE_OPERAND (arg, 0);
2310 if (VAR_OR_FUNCTION_DECL_P (arg))
2312 if (! TREE_PUBLIC (arg))
2313 vis = VISIBILITY_ANON;
2314 else
2315 vis = DECL_VISIBILITY (arg);
2318 if (vis)
2319 constrain_visibility (decl, vis, true);
2323 /* Like c_determine_visibility, but with additional C++-specific
2324 behavior.
2326 Function-scope entities can rely on the function's visibility because
2327 it is set in start_preparsed_function.
2329 Class-scope entities cannot rely on the class's visibility until the end
2330 of the enclosing class definition.
2332 Note that because namespaces have multiple independent definitions,
2333 namespace visibility is handled elsewhere using the #pragma visibility
2334 machinery rather than by decorating the namespace declaration.
2336 The goal is for constraints from the type to give a diagnostic, and
2337 other constraints to be applied silently. */
2339 void
2340 determine_visibility (tree decl)
2342 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2344 /* Only relevant for names with external linkage. */
2345 if (!TREE_PUBLIC (decl))
2346 return;
2348 /* Cloned constructors and destructors get the same visibility as
2349 the underlying function. That should be set up in
2350 maybe_clone_body. */
2351 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2353 bool orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2354 enum symbol_visibility orig_visibility = DECL_VISIBILITY (decl);
2356 /* The decl may be a template instantiation, which could influence
2357 visibilty. */
2358 tree template_decl = NULL_TREE;
2359 if (TREE_CODE (decl) == TYPE_DECL)
2361 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2363 if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
2364 template_decl = decl;
2366 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2367 template_decl = decl;
2369 else if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
2370 template_decl = decl;
2372 /* If DECL is a member of a class, visibility specifiers on the
2373 class can influence the visibility of the DECL. */
2374 tree class_type = NULL_TREE;
2375 if (DECL_CLASS_SCOPE_P (decl))
2376 class_type = DECL_CONTEXT (decl);
2377 else
2379 /* Not a class member. */
2381 /* Virtual tables have DECL_CONTEXT set to their associated class,
2382 so they are automatically handled above. */
2383 gcc_assert (!VAR_P (decl)
2384 || !DECL_VTABLE_OR_VTT_P (decl));
2386 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2388 /* Local statics and classes get the visibility of their
2389 containing function by default, except that
2390 -fvisibility-inlines-hidden doesn't affect them. */
2391 tree fn = DECL_CONTEXT (decl);
2392 if (DECL_VISIBILITY_SPECIFIED (fn))
2394 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2395 DECL_VISIBILITY_SPECIFIED (decl) =
2396 DECL_VISIBILITY_SPECIFIED (fn);
2398 else
2400 if (DECL_CLASS_SCOPE_P (fn))
2401 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2402 else if (determine_hidden_inline (fn))
2404 DECL_VISIBILITY (decl) = default_visibility;
2405 DECL_VISIBILITY_SPECIFIED (decl) =
2406 visibility_options.inpragma;
2408 else
2410 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2411 DECL_VISIBILITY_SPECIFIED (decl) =
2412 DECL_VISIBILITY_SPECIFIED (fn);
2416 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2417 but have no TEMPLATE_INFO, so don't try to check it. */
2418 template_decl = NULL_TREE;
2420 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2421 && flag_visibility_ms_compat)
2423 /* Under -fvisibility-ms-compat, types are visible by default,
2424 even though their contents aren't. */
2425 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2426 int underlying_vis = type_visibility (underlying_type);
2427 if (underlying_vis == VISIBILITY_ANON
2428 || (CLASS_TYPE_P (underlying_type)
2429 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2430 constrain_visibility (decl, underlying_vis, false);
2431 else
2432 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2434 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2436 /* tinfo visibility is based on the type it's for. */
2437 constrain_visibility
2438 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2440 /* Give the target a chance to override the visibility associated
2441 with DECL. */
2442 if (TREE_PUBLIC (decl)
2443 && !DECL_REALLY_EXTERN (decl)
2444 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2445 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2446 targetm.cxx.determine_class_data_visibility (decl);
2448 else if (template_decl)
2449 /* Template instantiations and specializations get visibility based
2450 on their template unless they override it with an attribute. */;
2451 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2453 if (determine_hidden_inline (decl))
2454 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2455 else
2457 /* Set default visibility to whatever the user supplied with
2458 #pragma GCC visibility or a namespace visibility attribute. */
2459 DECL_VISIBILITY (decl) = default_visibility;
2460 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2465 if (template_decl)
2467 /* If the specialization doesn't specify visibility, use the
2468 visibility from the template. */
2469 tree tinfo = get_template_info (template_decl);
2470 tree args = TI_ARGS (tinfo);
2471 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2472 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2473 : DECL_ATTRIBUTES (decl));
2475 if (args != error_mark_node)
2477 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2479 if (!DECL_VISIBILITY_SPECIFIED (decl))
2481 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2482 && determine_hidden_inline (decl))
2483 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2484 else
2486 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2487 DECL_VISIBILITY_SPECIFIED (decl)
2488 = DECL_VISIBILITY_SPECIFIED (pattern);
2492 if (args
2493 /* Template argument visibility outweighs #pragma or namespace
2494 visibility, but not an explicit attribute. */
2495 && !lookup_attribute ("visibility", attribs))
2497 int depth = TMPL_ARGS_DEPTH (args);
2498 if (DECL_VISIBILITY_SPECIFIED (decl))
2500 /* A class template member with explicit visibility
2501 overrides the class visibility, so we need to apply
2502 all the levels of template args directly. */
2503 int i;
2504 for (i = 1; i <= depth; ++i)
2506 tree lev = TMPL_ARGS_LEVEL (args, i);
2507 constrain_visibility_for_template (decl, lev);
2510 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2511 /* Limit visibility based on its template arguments. */
2512 constrain_visibility_for_template (decl, args);
2517 if (class_type)
2518 determine_visibility_from_class (decl, class_type);
2520 if (decl_anon_ns_mem_p (decl))
2521 /* Names in an anonymous namespace get internal linkage.
2522 This might change once we implement export. */
2523 constrain_visibility (decl, VISIBILITY_ANON, false);
2524 else if (TREE_CODE (decl) != TYPE_DECL)
2526 /* Propagate anonymity from type to decl. */
2527 int tvis = type_visibility (TREE_TYPE (decl));
2528 if (tvis == VISIBILITY_ANON
2529 || ! DECL_VISIBILITY_SPECIFIED (decl))
2530 constrain_visibility (decl, tvis, false);
2532 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2533 /* DR 757: A type without linkage shall not be used as the type of a
2534 variable or function with linkage, unless
2535 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2536 o the variable or function is not used (3.2 [basic.def.odr]) or is
2537 defined in the same translation unit.
2539 Since non-extern "C" decls need to be defined in the same
2540 translation unit, we can make the type internal. */
2541 constrain_visibility (decl, VISIBILITY_ANON, false);
2543 /* If visibility changed and DECL already has DECL_RTL, ensure
2544 symbol flags are updated. */
2545 if ((DECL_VISIBILITY (decl) != orig_visibility
2546 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2547 && ((VAR_P (decl) && TREE_STATIC (decl))
2548 || TREE_CODE (decl) == FUNCTION_DECL)
2549 && DECL_RTL_SET_P (decl))
2550 make_decl_rtl (decl);
2553 /* By default, static data members and function members receive
2554 the visibility of their containing class. */
2556 static void
2557 determine_visibility_from_class (tree decl, tree class_type)
2559 if (DECL_VISIBILITY_SPECIFIED (decl))
2560 return;
2562 if (determine_hidden_inline (decl))
2563 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2564 else
2566 /* Default to the class visibility. */
2567 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2568 DECL_VISIBILITY_SPECIFIED (decl)
2569 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2572 /* Give the target a chance to override the visibility associated
2573 with DECL. */
2574 if (VAR_P (decl)
2575 && (DECL_TINFO_P (decl)
2576 || (DECL_VTABLE_OR_VTT_P (decl)
2577 /* Construction virtual tables are not exported because
2578 they cannot be referred to from other object files;
2579 their name is not standardized by the ABI. */
2580 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2581 && TREE_PUBLIC (decl)
2582 && !DECL_REALLY_EXTERN (decl)
2583 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2584 targetm.cxx.determine_class_data_visibility (decl);
2587 /* Returns true iff DECL is an inline that should get hidden visibility
2588 because of -fvisibility-inlines-hidden. */
2590 static bool
2591 determine_hidden_inline (tree decl)
2593 return (visibility_options.inlines_hidden
2594 /* Don't do this for inline templates; specializations might not be
2595 inline, and we don't want them to inherit the hidden
2596 visibility. We'll set it here for all inline instantiations. */
2597 && !processing_template_decl
2598 && TREE_CODE (decl) == FUNCTION_DECL
2599 && DECL_DECLARED_INLINE_P (decl)
2600 && (! DECL_LANG_SPECIFIC (decl)
2601 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2604 /* Constrain the visibility of a class TYPE based on the visibility of its
2605 field types. Warn if any fields require lesser visibility. */
2607 void
2608 constrain_class_visibility (tree type)
2610 tree binfo;
2611 tree t;
2612 int i;
2614 int vis = type_visibility (type);
2616 if (vis == VISIBILITY_ANON
2617 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2618 return;
2620 /* Don't warn about visibility if the class has explicit visibility. */
2621 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2622 vis = VISIBILITY_INTERNAL;
2624 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2625 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node
2626 && !DECL_ARTIFICIAL (t))
2628 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2629 int subvis = type_visibility (ftype);
2631 if (subvis == VISIBILITY_ANON)
2633 if (!in_main_input_context())
2635 tree nlt = no_linkage_check (ftype, /*relaxed_p=*/false);
2636 if (nlt)
2638 if (same_type_p (TREE_TYPE (t), nlt))
2639 warning (OPT_Wsubobject_linkage, "\
2640 %qT has a field %qD whose type has no linkage",
2641 type, t);
2642 else
2643 warning (OPT_Wsubobject_linkage, "\
2644 %qT has a field %qD whose type depends on the type %qT which has no linkage",
2645 type, t, nlt);
2647 else
2648 warning (OPT_Wsubobject_linkage, "\
2649 %qT has a field %qD whose type uses the anonymous namespace",
2650 type, t);
2653 else if (MAYBE_CLASS_TYPE_P (ftype)
2654 && vis < VISIBILITY_HIDDEN
2655 && subvis >= VISIBILITY_HIDDEN)
2656 warning (OPT_Wattributes, "\
2657 %qT declared with greater visibility than the type of its field %qD",
2658 type, t);
2661 binfo = TYPE_BINFO (type);
2662 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2664 int subvis = type_visibility (TREE_TYPE (t));
2666 if (subvis == VISIBILITY_ANON)
2668 if (!in_main_input_context())
2670 tree nlt = no_linkage_check (TREE_TYPE (t), /*relaxed_p=*/false);
2671 if (nlt)
2673 if (same_type_p (TREE_TYPE (t), nlt))
2674 warning (OPT_Wsubobject_linkage, "\
2675 %qT has a base %qT whose type has no linkage",
2676 type, TREE_TYPE (t));
2677 else
2678 warning (OPT_Wsubobject_linkage, "\
2679 %qT has a base %qT whose type depends on the type %qT which has no linkage",
2680 type, TREE_TYPE (t), nlt);
2682 else
2683 warning (OPT_Wsubobject_linkage, "\
2684 %qT has a base %qT whose type uses the anonymous namespace",
2685 type, TREE_TYPE (t));
2688 else if (vis < VISIBILITY_HIDDEN
2689 && subvis >= VISIBILITY_HIDDEN)
2690 warning (OPT_Wattributes, "\
2691 %qT declared with greater visibility than its base %qT",
2692 type, TREE_TYPE (t));
2696 /* Functions for adjusting the visibility of a tagged type and its nested
2697 types and declarations when it gets a name for linkage purposes from a
2698 typedef. */
2700 static void bt_reset_linkage_1 (binding_entry, void *);
2701 static void bt_reset_linkage_2 (binding_entry, void *);
2703 /* First reset the visibility of all the types. */
2705 static void
2706 reset_type_linkage_1 (tree type)
2708 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2709 if (CLASS_TYPE_P (type))
2710 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2711 bt_reset_linkage_1, NULL);
2713 static void
2714 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2716 reset_type_linkage_1 (b->type);
2719 /* Then reset the visibility of any static data members or member
2720 functions that use those types. */
2722 static void
2723 reset_decl_linkage (tree decl)
2725 if (TREE_PUBLIC (decl))
2726 return;
2727 if (DECL_CLONED_FUNCTION_P (decl))
2728 return;
2729 TREE_PUBLIC (decl) = true;
2730 DECL_INTERFACE_KNOWN (decl) = false;
2731 determine_visibility (decl);
2732 tentative_decl_linkage (decl);
2735 static void
2736 reset_type_linkage_2 (tree type)
2738 if (CLASS_TYPE_P (type))
2740 if (tree vt = CLASSTYPE_VTABLES (type))
2742 tree name = mangle_vtbl_for_type (type);
2743 DECL_NAME (vt) = name;
2744 SET_DECL_ASSEMBLER_NAME (vt, name);
2745 reset_decl_linkage (vt);
2747 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2749 tree name = mangle_typeinfo_for_type (type);
2750 DECL_NAME (ti) = name;
2751 SET_DECL_ASSEMBLER_NAME (ti, name);
2752 TREE_TYPE (name) = type;
2753 reset_decl_linkage (ti);
2755 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2757 tree mem = STRIP_TEMPLATE (m);
2758 if (TREE_CODE (mem) == VAR_DECL || TREE_CODE (mem) == FUNCTION_DECL)
2759 reset_decl_linkage (mem);
2761 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2762 bt_reset_linkage_2, NULL);
2766 static void
2767 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2769 reset_type_linkage_2 (b->type);
2771 void
2772 reset_type_linkage (tree type)
2774 reset_type_linkage_1 (type);
2775 reset_type_linkage_2 (type);
2778 /* Set up our initial idea of what the linkage of DECL should be. */
2780 void
2781 tentative_decl_linkage (tree decl)
2783 if (DECL_INTERFACE_KNOWN (decl))
2784 /* We've already made a decision as to how this function will
2785 be handled. */;
2786 else if (vague_linkage_p (decl))
2788 if (TREE_CODE (decl) == FUNCTION_DECL
2789 && decl_defined_p (decl))
2791 DECL_EXTERNAL (decl) = 1;
2792 DECL_NOT_REALLY_EXTERN (decl) = 1;
2793 note_vague_linkage_fn (decl);
2794 /* A non-template inline function with external linkage will
2795 always be COMDAT. As we must eventually determine the
2796 linkage of all functions, and as that causes writes to
2797 the data mapped in from the PCH file, it's advantageous
2798 to mark the functions at this point. */
2799 if (DECL_DECLARED_INLINE_P (decl)
2800 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2801 || DECL_DEFAULTED_FN (decl)))
2803 /* This function must have external linkage, as
2804 otherwise DECL_INTERFACE_KNOWN would have been
2805 set. */
2806 gcc_assert (TREE_PUBLIC (decl));
2807 comdat_linkage (decl);
2808 DECL_INTERFACE_KNOWN (decl) = 1;
2811 else if (VAR_P (decl))
2812 maybe_commonize_var (decl);
2816 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2817 for DECL has not already been determined, do so now by setting
2818 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2819 function is called entities with vague linkage whose definitions
2820 are available must have TREE_PUBLIC set.
2822 If this function decides to place DECL in COMDAT, it will set
2823 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2824 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2825 callers defer that decision until it is clear that DECL is actually
2826 required. */
2828 void
2829 import_export_decl (tree decl)
2831 int emit_p;
2832 bool comdat_p;
2833 bool import_p;
2834 tree class_type = NULL_TREE;
2836 if (DECL_INTERFACE_KNOWN (decl))
2837 return;
2839 /* We cannot determine what linkage to give to an entity with vague
2840 linkage until the end of the file. For example, a virtual table
2841 for a class will be defined if and only if the key method is
2842 defined in this translation unit. As a further example, consider
2843 that when compiling a translation unit that uses PCH file with
2844 "-frepo" it would be incorrect to make decisions about what
2845 entities to emit when building the PCH; those decisions must be
2846 delayed until the repository information has been processed. */
2847 gcc_assert (at_eof);
2848 /* Object file linkage for explicit instantiations is handled in
2849 mark_decl_instantiated. For static variables in functions with
2850 vague linkage, maybe_commonize_var is used.
2852 Therefore, the only declarations that should be provided to this
2853 function are those with external linkage that are:
2855 * implicit instantiations of function templates
2857 * inline function
2859 * implicit instantiations of static data members of class
2860 templates
2862 * virtual tables
2864 * typeinfo objects
2866 Furthermore, all entities that reach this point must have a
2867 definition available in this translation unit.
2869 The following assertions check these conditions. */
2870 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2871 /* Any code that creates entities with TREE_PUBLIC cleared should
2872 also set DECL_INTERFACE_KNOWN. */
2873 gcc_assert (TREE_PUBLIC (decl));
2874 if (TREE_CODE (decl) == FUNCTION_DECL)
2875 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2876 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2877 || DECL_DECLARED_INLINE_P (decl));
2878 else
2879 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2880 || DECL_VTABLE_OR_VTT_P (decl)
2881 || DECL_TINFO_P (decl));
2882 /* Check that a definition of DECL is available in this translation
2883 unit. */
2884 gcc_assert (!DECL_REALLY_EXTERN (decl));
2886 /* Assume that DECL will not have COMDAT linkage. */
2887 comdat_p = false;
2888 /* Assume that DECL will not be imported into this translation
2889 unit. */
2890 import_p = false;
2892 /* See if the repository tells us whether or not to emit DECL in
2893 this translation unit. */
2894 emit_p = repo_emit_p (decl);
2895 if (emit_p == 0)
2896 import_p = true;
2897 else if (emit_p == 1)
2899 /* The repository indicates that this entity should be defined
2900 here. Make sure the back end honors that request. */
2901 mark_needed (decl);
2902 /* Output the definition as an ordinary strong definition. */
2903 DECL_EXTERNAL (decl) = 0;
2904 DECL_INTERFACE_KNOWN (decl) = 1;
2905 return;
2908 if (import_p)
2909 /* We have already decided what to do with this DECL; there is no
2910 need to check anything further. */
2912 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2914 class_type = DECL_CONTEXT (decl);
2915 import_export_class (class_type);
2916 if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2917 && CLASSTYPE_INTERFACE_ONLY (class_type))
2918 import_p = true;
2919 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2920 && !CLASSTYPE_USE_TEMPLATE (class_type)
2921 && CLASSTYPE_KEY_METHOD (class_type)
2922 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2923 /* The ABI requires that all virtual tables be emitted with
2924 COMDAT linkage. However, on systems where COMDAT symbols
2925 don't show up in the table of contents for a static
2926 archive, or on systems without weak symbols (where we
2927 approximate COMDAT linkage by using internal linkage), the
2928 linker will report errors about undefined symbols because
2929 it will not see the virtual table definition. Therefore,
2930 in the case that we know that the virtual table will be
2931 emitted in only one translation unit, we make the virtual
2932 table an ordinary definition with external linkage. */
2933 DECL_EXTERNAL (decl) = 0;
2934 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2936 /* CLASS_TYPE is being exported from this translation unit,
2937 so DECL should be defined here. */
2938 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2939 /* If a class is declared in a header with the "extern
2940 template" extension, then it will not be instantiated,
2941 even in translation units that would normally require
2942 it. Often such classes are explicitly instantiated in
2943 one translation unit. Therefore, the explicit
2944 instantiation must be made visible to other translation
2945 units. */
2946 DECL_EXTERNAL (decl) = 0;
2947 else
2949 /* The generic C++ ABI says that class data is always
2950 COMDAT, even if there is a key function. Some
2951 variants (e.g., the ARM EABI) says that class data
2952 only has COMDAT linkage if the class data might be
2953 emitted in more than one translation unit. When the
2954 key method can be inline and is inline, we still have
2955 to arrange for comdat even though
2956 class_data_always_comdat is false. */
2957 if (!CLASSTYPE_KEY_METHOD (class_type)
2958 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2959 || targetm.cxx.class_data_always_comdat ())
2961 /* The ABI requires COMDAT linkage. Normally, we
2962 only emit COMDAT things when they are needed;
2963 make sure that we realize that this entity is
2964 indeed needed. */
2965 comdat_p = true;
2966 mark_needed (decl);
2970 else if (!flag_implicit_templates
2971 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2972 import_p = true;
2973 else
2974 comdat_p = true;
2976 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2978 tree type = TREE_TYPE (DECL_NAME (decl));
2979 if (CLASS_TYPE_P (type))
2981 class_type = type;
2982 import_export_class (type);
2983 if (CLASSTYPE_INTERFACE_KNOWN (type)
2984 && TYPE_POLYMORPHIC_P (type)
2985 && CLASSTYPE_INTERFACE_ONLY (type)
2986 /* If -fno-rtti was specified, then we cannot be sure
2987 that RTTI information will be emitted with the
2988 virtual table of the class, so we must emit it
2989 wherever it is used. */
2990 && flag_rtti)
2991 import_p = true;
2992 else
2994 if (CLASSTYPE_INTERFACE_KNOWN (type)
2995 && !CLASSTYPE_INTERFACE_ONLY (type))
2997 comdat_p = (targetm.cxx.class_data_always_comdat ()
2998 || (CLASSTYPE_KEY_METHOD (type)
2999 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
3000 mark_needed (decl);
3001 if (!flag_weak)
3003 comdat_p = false;
3004 DECL_EXTERNAL (decl) = 0;
3007 else
3008 comdat_p = true;
3011 else
3012 comdat_p = true;
3014 else if (DECL_TEMPLOID_INSTANTIATION (decl))
3016 /* DECL is an implicit instantiation of a function or static
3017 data member. */
3018 if ((flag_implicit_templates
3019 && !flag_use_repository)
3020 || (flag_implicit_inline_templates
3021 && TREE_CODE (decl) == FUNCTION_DECL
3022 && DECL_DECLARED_INLINE_P (decl)))
3023 comdat_p = true;
3024 else
3025 /* If we are not implicitly generating templates, then mark
3026 this entity as undefined in this translation unit. */
3027 import_p = true;
3029 else if (DECL_FUNCTION_MEMBER_P (decl))
3031 if (!DECL_DECLARED_INLINE_P (decl))
3033 tree ctype = DECL_CONTEXT (decl);
3034 import_export_class (ctype);
3035 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
3037 DECL_NOT_REALLY_EXTERN (decl)
3038 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
3039 || (DECL_DECLARED_INLINE_P (decl)
3040 && ! flag_implement_inlines
3041 && !DECL_VINDEX (decl)));
3043 if (!DECL_NOT_REALLY_EXTERN (decl))
3044 DECL_EXTERNAL (decl) = 1;
3046 /* Always make artificials weak. */
3047 if (DECL_ARTIFICIAL (decl) && flag_weak)
3048 comdat_p = true;
3049 else
3050 maybe_make_one_only (decl);
3053 else
3054 comdat_p = true;
3056 else
3057 comdat_p = true;
3059 if (import_p)
3061 /* If we are importing DECL into this translation unit, mark is
3062 an undefined here. */
3063 DECL_EXTERNAL (decl) = 1;
3064 DECL_NOT_REALLY_EXTERN (decl) = 0;
3066 else if (comdat_p)
3068 /* If we decided to put DECL in COMDAT, mark it accordingly at
3069 this point. */
3070 comdat_linkage (decl);
3073 DECL_INTERFACE_KNOWN (decl) = 1;
3076 /* Return an expression that performs the destruction of DECL, which
3077 must be a VAR_DECL whose type has a non-trivial destructor, or is
3078 an array whose (innermost) elements have a non-trivial destructor. */
3080 tree
3081 build_cleanup (tree decl)
3083 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
3084 gcc_assert (clean != NULL_TREE);
3085 return clean;
3088 /* Returns the initialization guard variable for the variable DECL,
3089 which has static storage duration. */
3091 tree
3092 get_guard (tree decl)
3094 tree sname;
3095 tree guard;
3097 sname = mangle_guard_variable (decl);
3098 guard = get_global_binding (sname);
3099 if (! guard)
3101 tree guard_type;
3103 /* We use a type that is big enough to contain a mutex as well
3104 as an integer counter. */
3105 guard_type = targetm.cxx.guard_type ();
3106 guard = build_decl (DECL_SOURCE_LOCATION (decl),
3107 VAR_DECL, sname, guard_type);
3109 /* The guard should have the same linkage as what it guards. */
3110 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
3111 TREE_STATIC (guard) = TREE_STATIC (decl);
3112 DECL_COMMON (guard) = DECL_COMMON (decl);
3113 DECL_COMDAT (guard) = DECL_COMDAT (decl);
3114 CP_DECL_THREAD_LOCAL_P (guard) = CP_DECL_THREAD_LOCAL_P (decl);
3115 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
3116 if (DECL_ONE_ONLY (decl))
3117 make_decl_one_only (guard, cxx_comdat_group (guard));
3118 if (TREE_PUBLIC (decl))
3119 DECL_WEAK (guard) = DECL_WEAK (decl);
3120 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
3121 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
3123 DECL_ARTIFICIAL (guard) = 1;
3124 DECL_IGNORED_P (guard) = 1;
3125 TREE_USED (guard) = 1;
3126 pushdecl_top_level_and_finish (guard, NULL_TREE);
3128 return guard;
3131 /* Return an atomic load of src with the appropriate memory model. */
3133 static tree
3134 build_atomic_load_byte (tree src, HOST_WIDE_INT model)
3136 tree ptr_type = build_pointer_type (char_type_node);
3137 tree mem_model = build_int_cst (integer_type_node, model);
3138 tree t, addr, val;
3139 unsigned int size;
3140 int fncode;
3142 size = tree_to_uhwi (TYPE_SIZE_UNIT (char_type_node));
3144 fncode = BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1;
3145 t = builtin_decl_implicit ((enum built_in_function) fncode);
3147 addr = build1 (ADDR_EXPR, ptr_type, src);
3148 val = build_call_expr (t, 2, addr, mem_model);
3149 return val;
3152 /* Return those bits of the GUARD variable that should be set when the
3153 guarded entity is actually initialized. */
3155 static tree
3156 get_guard_bits (tree guard)
3158 if (!targetm.cxx.guard_mask_bit ())
3160 /* We only set the first byte of the guard, in order to leave room
3161 for a mutex in the high-order bits. */
3162 guard = build1 (ADDR_EXPR,
3163 build_pointer_type (TREE_TYPE (guard)),
3164 guard);
3165 guard = build1 (NOP_EXPR,
3166 build_pointer_type (char_type_node),
3167 guard);
3168 guard = build1 (INDIRECT_REF, char_type_node, guard);
3171 return guard;
3174 /* Return an expression which determines whether or not the GUARD
3175 variable has already been initialized. */
3177 tree
3178 get_guard_cond (tree guard, bool thread_safe)
3180 tree guard_value;
3182 if (!thread_safe)
3183 guard = get_guard_bits (guard);
3184 else
3185 guard = build_atomic_load_byte (guard, MEMMODEL_ACQUIRE);
3187 /* Mask off all but the low bit. */
3188 if (targetm.cxx.guard_mask_bit ())
3190 guard_value = integer_one_node;
3191 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3192 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3193 guard = cp_build_binary_op (input_location,
3194 BIT_AND_EXPR, guard, guard_value,
3195 tf_warning_or_error);
3198 guard_value = integer_zero_node;
3199 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3200 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3201 return cp_build_binary_op (input_location,
3202 EQ_EXPR, guard, guard_value,
3203 tf_warning_or_error);
3206 /* Return an expression which sets the GUARD variable, indicating that
3207 the variable being guarded has been initialized. */
3209 tree
3210 set_guard (tree guard)
3212 tree guard_init;
3214 /* Set the GUARD to one. */
3215 guard = get_guard_bits (guard);
3216 guard_init = integer_one_node;
3217 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3218 guard_init = fold_convert (TREE_TYPE (guard), guard_init);
3219 return cp_build_modify_expr (input_location, guard, NOP_EXPR, guard_init,
3220 tf_warning_or_error);
3223 /* Returns true iff we can tell that VAR does not have a dynamic
3224 initializer. */
3226 static bool
3227 var_defined_without_dynamic_init (tree var)
3229 /* If it's defined in another TU, we can't tell. */
3230 if (DECL_EXTERNAL (var))
3231 return false;
3232 /* If it has a non-trivial destructor, registering the destructor
3233 counts as dynamic initialization. */
3234 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3235 return false;
3236 /* If it's in this TU, its initializer has been processed, unless
3237 it's a case of self-initialization, then DECL_INITIALIZED_P is
3238 false while the initializer is handled by finish_id_expression. */
3239 if (!DECL_INITIALIZED_P (var))
3240 return false;
3241 /* If it has no initializer or a constant one, it's not dynamic. */
3242 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3243 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3246 /* Returns true iff VAR is a variable that needs uses to be
3247 wrapped for possible dynamic initialization. */
3249 static bool
3250 var_needs_tls_wrapper (tree var)
3252 return (!error_operand_p (var)
3253 && CP_DECL_THREAD_LOCAL_P (var)
3254 && !DECL_GNU_TLS_P (var)
3255 && !DECL_FUNCTION_SCOPE_P (var)
3256 && !var_defined_without_dynamic_init (var));
3259 /* Get the FUNCTION_DECL for the shared TLS init function for this
3260 translation unit. */
3262 static tree
3263 get_local_tls_init_fn (void)
3265 tree sname = get_identifier ("__tls_init");
3266 tree fn = get_global_binding (sname);
3267 if (!fn)
3269 fn = build_lang_decl (FUNCTION_DECL, sname,
3270 build_function_type (void_type_node,
3271 void_list_node));
3272 SET_DECL_LANGUAGE (fn, lang_c);
3273 TREE_PUBLIC (fn) = false;
3274 DECL_ARTIFICIAL (fn) = true;
3275 mark_used (fn);
3276 set_global_binding (fn);
3278 return fn;
3281 /* Get a FUNCTION_DECL for the init function for the thread_local
3282 variable VAR. The init function will be an alias to the function
3283 that initializes all the non-local TLS variables in the translation
3284 unit. The init function is only used by the wrapper function. */
3286 static tree
3287 get_tls_init_fn (tree var)
3289 /* Only C++11 TLS vars need this init fn. */
3290 if (!var_needs_tls_wrapper (var))
3291 return NULL_TREE;
3293 /* If -fno-extern-tls-init, assume that we don't need to call
3294 a tls init function for a variable defined in another TU. */
3295 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3296 return NULL_TREE;
3298 /* If the variable is internal, or if we can't generate aliases,
3299 call the local init function directly. */
3300 if (!TREE_PUBLIC (var) || !TARGET_SUPPORTS_ALIASES)
3301 return get_local_tls_init_fn ();
3303 tree sname = mangle_tls_init_fn (var);
3304 tree fn = get_global_binding (sname);
3305 if (!fn)
3307 fn = build_lang_decl (FUNCTION_DECL, sname,
3308 build_function_type (void_type_node,
3309 void_list_node));
3310 SET_DECL_LANGUAGE (fn, lang_c);
3311 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3312 DECL_ARTIFICIAL (fn) = true;
3313 DECL_COMDAT (fn) = DECL_COMDAT (var);
3314 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3315 if (DECL_ONE_ONLY (var))
3316 make_decl_one_only (fn, cxx_comdat_group (fn));
3317 if (TREE_PUBLIC (var))
3319 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3320 /* If the variable is defined somewhere else and might have static
3321 initialization, make the init function a weak reference. */
3322 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3323 || TYPE_HAS_CONSTEXPR_CTOR (obtype)
3324 || TYPE_HAS_TRIVIAL_DFLT (obtype))
3325 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3326 && DECL_EXTERNAL (var))
3327 declare_weak (fn);
3328 else
3329 DECL_WEAK (fn) = DECL_WEAK (var);
3331 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3332 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3333 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3334 DECL_IGNORED_P (fn) = 1;
3335 mark_used (fn);
3337 DECL_BEFRIENDING_CLASSES (fn) = var;
3339 set_global_binding (fn);
3341 return fn;
3344 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3345 variable VAR. The wrapper function calls the init function (if any) for
3346 VAR and then returns a reference to VAR. The wrapper function is used
3347 in place of VAR everywhere VAR is mentioned. */
3349 tree
3350 get_tls_wrapper_fn (tree var)
3352 /* Only C++11 TLS vars need this wrapper fn. */
3353 if (!var_needs_tls_wrapper (var))
3354 return NULL_TREE;
3356 tree sname = mangle_tls_wrapper_fn (var);
3357 tree fn = get_global_binding (sname);
3358 if (!fn)
3360 /* A named rvalue reference is an lvalue, so the wrapper should
3361 always return an lvalue reference. */
3362 tree type = non_reference (TREE_TYPE (var));
3363 type = build_reference_type (type);
3364 tree fntype = build_function_type (type, void_list_node);
3365 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3366 SET_DECL_LANGUAGE (fn, lang_c);
3367 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3368 DECL_ARTIFICIAL (fn) = true;
3369 DECL_IGNORED_P (fn) = 1;
3370 /* The wrapper is inline and emitted everywhere var is used. */
3371 DECL_DECLARED_INLINE_P (fn) = true;
3372 if (TREE_PUBLIC (var))
3374 comdat_linkage (fn);
3375 #ifdef HAVE_GAS_HIDDEN
3376 /* Make the wrapper bind locally; there's no reason to share
3377 the wrapper between multiple shared objects. */
3378 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3379 DECL_VISIBILITY_SPECIFIED (fn) = true;
3380 #endif
3382 if (!TREE_PUBLIC (fn))
3383 DECL_INTERFACE_KNOWN (fn) = true;
3384 mark_used (fn);
3385 note_vague_linkage_fn (fn);
3387 #if 0
3388 /* We want CSE to commonize calls to the wrapper, but marking it as
3389 pure is unsafe since it has side-effects. I guess we need a new
3390 ECF flag even weaker than ECF_PURE. FIXME! */
3391 DECL_PURE_P (fn) = true;
3392 #endif
3394 DECL_BEFRIENDING_CLASSES (fn) = var;
3396 set_global_binding (fn);
3398 return fn;
3401 /* At EOF, generate the definition for the TLS wrapper function FN:
3403 T& var_wrapper() {
3404 if (init_fn) init_fn();
3405 return var;
3406 } */
3408 static void
3409 generate_tls_wrapper (tree fn)
3411 tree var = DECL_BEFRIENDING_CLASSES (fn);
3413 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3414 tree body = begin_function_body ();
3415 /* Only call the init fn if there might be one. */
3416 if (tree init_fn = get_tls_init_fn (var))
3418 tree if_stmt = NULL_TREE;
3419 /* If init_fn is a weakref, make sure it exists before calling. */
3420 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3422 if_stmt = begin_if_stmt ();
3423 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3424 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3425 NE_EXPR, addr, nullptr_node,
3426 tf_warning_or_error);
3427 finish_if_stmt_cond (cond, if_stmt);
3429 finish_expr_stmt (build_cxx_call
3430 (init_fn, 0, NULL, tf_warning_or_error));
3431 if (if_stmt)
3433 finish_then_clause (if_stmt);
3434 finish_if_stmt (if_stmt);
3437 else
3438 /* If there's no initialization, the wrapper is a constant function. */
3439 TREE_READONLY (fn) = true;
3440 finish_return_stmt (convert_from_reference (var));
3441 finish_function_body (body);
3442 expand_or_defer_fn (finish_function (/*inline_p=*/false));
3445 /* Start the process of running a particular set of global constructors
3446 or destructors. Subroutine of do_[cd]tors. Also called from
3447 vtv_start_verification_constructor_init_function. */
3449 static tree
3450 start_objects (int method_type, int initp)
3452 tree body;
3453 tree fndecl;
3454 char type[14];
3456 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3458 if (initp != DEFAULT_INIT_PRIORITY)
3460 char joiner;
3462 #ifdef JOINER
3463 joiner = JOINER;
3464 #else
3465 joiner = '_';
3466 #endif
3468 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3470 else
3471 sprintf (type, "sub_%c", method_type);
3473 fndecl = build_lang_decl (FUNCTION_DECL,
3474 get_file_function_name (type),
3475 build_function_type_list (void_type_node,
3476 NULL_TREE));
3477 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3479 TREE_PUBLIC (current_function_decl) = 0;
3481 /* Mark as artificial because it's not explicitly in the user's
3482 source code. */
3483 DECL_ARTIFICIAL (current_function_decl) = 1;
3485 /* Mark this declaration as used to avoid spurious warnings. */
3486 TREE_USED (current_function_decl) = 1;
3488 /* Mark this function as a global constructor or destructor. */
3489 if (method_type == 'I')
3490 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3491 else
3492 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3494 body = begin_compound_stmt (BCS_FN_BODY);
3496 return body;
3499 /* Finish the process of running a particular set of global constructors
3500 or destructors. Subroutine of do_[cd]tors. */
3502 static void
3503 finish_objects (int method_type, int initp, tree body)
3505 tree fn;
3507 /* Finish up. */
3508 finish_compound_stmt (body);
3509 fn = finish_function (/*inline_p=*/false);
3511 if (method_type == 'I')
3513 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3514 decl_init_priority_insert (fn, initp);
3516 else
3518 DECL_STATIC_DESTRUCTOR (fn) = 1;
3519 decl_fini_priority_insert (fn, initp);
3522 expand_or_defer_fn (fn);
3525 /* The names of the parameters to the function created to handle
3526 initializations and destructions for objects with static storage
3527 duration. */
3528 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3529 #define PRIORITY_IDENTIFIER "__priority"
3531 /* The name of the function we create to handle initializations and
3532 destructions for objects with static storage duration. */
3533 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3535 /* The declaration for the __INITIALIZE_P argument. */
3536 static GTY(()) tree initialize_p_decl;
3538 /* The declaration for the __PRIORITY argument. */
3539 static GTY(()) tree priority_decl;
3541 /* The declaration for the static storage duration function. */
3542 static GTY(()) tree ssdf_decl;
3544 /* All the static storage duration functions created in this
3545 translation unit. */
3546 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3548 /* A map from priority levels to information about that priority
3549 level. There may be many such levels, so efficient lookup is
3550 important. */
3551 static splay_tree priority_info_map;
3553 /* Begins the generation of the function that will handle all
3554 initialization and destruction of objects with static storage
3555 duration. The function generated takes two parameters of type
3556 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3557 nonzero, it performs initializations. Otherwise, it performs
3558 destructions. It only performs those initializations or
3559 destructions with the indicated __PRIORITY. The generated function
3560 returns no value.
3562 It is assumed that this function will only be called once per
3563 translation unit. */
3565 static tree
3566 start_static_storage_duration_function (unsigned count)
3568 tree type;
3569 tree body;
3570 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3572 /* Create the identifier for this function. It will be of the form
3573 SSDF_IDENTIFIER_<number>. */
3574 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3576 type = build_function_type_list (void_type_node,
3577 integer_type_node, integer_type_node,
3578 NULL_TREE);
3580 /* Create the FUNCTION_DECL itself. */
3581 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3582 get_identifier (id),
3583 type);
3584 TREE_PUBLIC (ssdf_decl) = 0;
3585 DECL_ARTIFICIAL (ssdf_decl) = 1;
3587 /* Put this function in the list of functions to be called from the
3588 static constructors and destructors. */
3589 if (!ssdf_decls)
3591 vec_alloc (ssdf_decls, 32);
3593 /* Take this opportunity to initialize the map from priority
3594 numbers to information about that priority level. */
3595 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3596 /*delete_key_fn=*/0,
3597 /*delete_value_fn=*/
3598 splay_tree_delete_pointers);
3600 /* We always need to generate functions for the
3601 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3602 priorities later, we'll be sure to find the
3603 DEFAULT_INIT_PRIORITY. */
3604 get_priority_info (DEFAULT_INIT_PRIORITY);
3607 vec_safe_push (ssdf_decls, ssdf_decl);
3609 /* Create the argument list. */
3610 initialize_p_decl = cp_build_parm_decl
3611 (ssdf_decl, get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3612 TREE_USED (initialize_p_decl) = 1;
3613 priority_decl = cp_build_parm_decl
3614 (ssdf_decl, get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3615 TREE_USED (priority_decl) = 1;
3617 DECL_CHAIN (initialize_p_decl) = priority_decl;
3618 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3620 /* Put the function in the global scope. */
3621 pushdecl (ssdf_decl);
3623 /* Start the function itself. This is equivalent to declaring the
3624 function as:
3626 static void __ssdf (int __initialize_p, init __priority_p);
3628 It is static because we only need to call this function from the
3629 various constructor and destructor functions for this module. */
3630 start_preparsed_function (ssdf_decl,
3631 /*attrs=*/NULL_TREE,
3632 SF_PRE_PARSED);
3634 /* Set up the scope of the outermost block in the function. */
3635 body = begin_compound_stmt (BCS_FN_BODY);
3637 return body;
3640 /* Finish the generation of the function which performs initialization
3641 and destruction of objects with static storage duration. After
3642 this point, no more such objects can be created. */
3644 static void
3645 finish_static_storage_duration_function (tree body)
3647 /* Close out the function. */
3648 finish_compound_stmt (body);
3649 expand_or_defer_fn (finish_function (/*inline_p=*/false));
3652 /* Return the information about the indicated PRIORITY level. If no
3653 code to handle this level has yet been generated, generate the
3654 appropriate prologue. */
3656 static priority_info
3657 get_priority_info (int priority)
3659 priority_info pi;
3660 splay_tree_node n;
3662 n = splay_tree_lookup (priority_info_map,
3663 (splay_tree_key) priority);
3664 if (!n)
3666 /* Create a new priority information structure, and insert it
3667 into the map. */
3668 pi = XNEW (struct priority_info_s);
3669 pi->initializations_p = 0;
3670 pi->destructions_p = 0;
3671 splay_tree_insert (priority_info_map,
3672 (splay_tree_key) priority,
3673 (splay_tree_value) pi);
3675 else
3676 pi = (priority_info) n->value;
3678 return pi;
3681 /* The effective initialization priority of a DECL. */
3683 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3684 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3685 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3687 /* Whether a DECL needs a guard to protect it against multiple
3688 initialization. */
3690 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3691 || DECL_ONE_ONLY (decl) \
3692 || DECL_WEAK (decl)))
3694 /* Called from one_static_initialization_or_destruction(),
3695 via walk_tree.
3696 Walks the initializer list of a global variable and looks for
3697 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3698 and that have their DECL_CONTEXT() == NULL.
3699 For each such temporary variable, set their DECL_CONTEXT() to
3700 the current function. This is necessary because otherwise
3701 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3702 when trying to refer to a temporary variable that does not have
3703 it's DECL_CONTECT() properly set. */
3704 static tree
3705 fix_temporary_vars_context_r (tree *node,
3706 int * /*unused*/,
3707 void * /*unused1*/)
3709 gcc_assert (current_function_decl);
3711 if (TREE_CODE (*node) == BIND_EXPR)
3713 tree var;
3715 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3716 if (VAR_P (var)
3717 && !DECL_NAME (var)
3718 && DECL_ARTIFICIAL (var)
3719 && !DECL_CONTEXT (var))
3720 DECL_CONTEXT (var) = current_function_decl;
3723 return NULL_TREE;
3726 /* Set up to handle the initialization or destruction of DECL. If
3727 INITP is nonzero, we are initializing the variable. Otherwise, we
3728 are destroying it. */
3730 static void
3731 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3733 tree guard_if_stmt = NULL_TREE;
3734 tree guard;
3736 /* If we are supposed to destruct and there's a trivial destructor,
3737 nothing has to be done. */
3738 if (!initp
3739 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3740 return;
3742 /* Trick the compiler into thinking we are at the file and line
3743 where DECL was declared so that error-messages make sense, and so
3744 that the debugger will show somewhat sensible file and line
3745 information. */
3746 input_location = DECL_SOURCE_LOCATION (decl);
3748 /* Make sure temporary variables in the initialiser all have
3749 their DECL_CONTEXT() set to a value different from NULL_TREE.
3750 This can happen when global variables initializers are built.
3751 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3752 the temporary variables that might have been generated in the
3753 accompanying initializers is NULL_TREE, meaning the variables have been
3754 declared in the global namespace.
3755 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3756 of the temporaries are set to the current function decl. */
3757 cp_walk_tree_without_duplicates (&init,
3758 fix_temporary_vars_context_r,
3759 NULL);
3761 /* Because of:
3763 [class.access.spec]
3765 Access control for implicit calls to the constructors,
3766 the conversion functions, or the destructor called to
3767 create and destroy a static data member is performed as
3768 if these calls appeared in the scope of the member's
3769 class.
3771 we pretend we are in a static member function of the class of
3772 which the DECL is a member. */
3773 if (member_p (decl))
3775 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3776 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3779 /* Assume we don't need a guard. */
3780 guard = NULL_TREE;
3781 /* We need a guard if this is an object with external linkage that
3782 might be initialized in more than one place. (For example, a
3783 static data member of a template, when the data member requires
3784 construction.) */
3785 if (NEEDS_GUARD_P (decl))
3787 tree guard_cond;
3789 guard = get_guard (decl);
3791 /* When using __cxa_atexit, we just check the GUARD as we would
3792 for a local static. */
3793 if (flag_use_cxa_atexit)
3795 /* When using __cxa_atexit, we never try to destroy
3796 anything from a static destructor. */
3797 gcc_assert (initp);
3798 guard_cond = get_guard_cond (guard, false);
3800 /* If we don't have __cxa_atexit, then we will be running
3801 destructors from .fini sections, or their equivalents. So,
3802 we need to know how many times we've tried to initialize this
3803 object. We do initializations only if the GUARD is zero,
3804 i.e., if we are the first to initialize the variable. We do
3805 destructions only if the GUARD is one, i.e., if we are the
3806 last to destroy the variable. */
3807 else if (initp)
3808 guard_cond
3809 = cp_build_binary_op (input_location,
3810 EQ_EXPR,
3811 cp_build_unary_op (PREINCREMENT_EXPR,
3812 guard,
3813 /*noconvert=*/true,
3814 tf_warning_or_error),
3815 integer_one_node,
3816 tf_warning_or_error);
3817 else
3818 guard_cond
3819 = cp_build_binary_op (input_location,
3820 EQ_EXPR,
3821 cp_build_unary_op (PREDECREMENT_EXPR,
3822 guard,
3823 /*noconvert=*/true,
3824 tf_warning_or_error),
3825 integer_zero_node,
3826 tf_warning_or_error);
3828 guard_if_stmt = begin_if_stmt ();
3829 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3833 /* If we're using __cxa_atexit, we have not already set the GUARD,
3834 so we must do so now. */
3835 if (guard && initp && flag_use_cxa_atexit)
3836 finish_expr_stmt (set_guard (guard));
3838 /* Perform the initialization or destruction. */
3839 if (initp)
3841 if (init)
3843 finish_expr_stmt (init);
3844 if (sanitize_flags_p (SANITIZE_ADDRESS, decl))
3846 varpool_node *vnode = varpool_node::get (decl);
3847 if (vnode)
3848 vnode->dynamically_initialized = 1;
3852 /* If we're using __cxa_atexit, register a function that calls the
3853 destructor for the object. */
3854 if (flag_use_cxa_atexit)
3855 finish_expr_stmt (register_dtor_fn (decl));
3857 else
3858 finish_expr_stmt (build_cleanup (decl));
3860 /* Finish the guard if-stmt, if necessary. */
3861 if (guard)
3863 finish_then_clause (guard_if_stmt);
3864 finish_if_stmt (guard_if_stmt);
3867 /* Now that we're done with DECL we don't need to pretend to be a
3868 member of its class any longer. */
3869 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3870 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3873 /* Generate code to do the initialization or destruction of the decls in VARS,
3874 a TREE_LIST of VAR_DECL with static storage duration.
3875 Whether initialization or destruction is performed is specified by INITP. */
3877 static void
3878 do_static_initialization_or_destruction (tree vars, bool initp)
3880 tree node, init_if_stmt, cond;
3882 /* Build the outer if-stmt to check for initialization or destruction. */
3883 init_if_stmt = begin_if_stmt ();
3884 cond = initp ? integer_one_node : integer_zero_node;
3885 cond = cp_build_binary_op (input_location,
3886 EQ_EXPR,
3887 initialize_p_decl,
3888 cond,
3889 tf_warning_or_error);
3890 finish_if_stmt_cond (cond, init_if_stmt);
3892 /* To make sure dynamic construction doesn't access globals from other
3893 compilation units where they might not be yet constructed, for
3894 -fsanitize=address insert __asan_before_dynamic_init call that
3895 prevents access to either all global variables that need construction
3896 in other compilation units, or at least those that haven't been
3897 initialized yet. Variables that need dynamic construction in
3898 the current compilation unit are kept accessible. */
3899 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
3900 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3902 node = vars;
3903 do {
3904 tree decl = TREE_VALUE (node);
3905 tree priority_if_stmt;
3906 int priority;
3907 priority_info pi;
3909 /* If we don't need a destructor, there's nothing to do. Avoid
3910 creating a possibly empty if-stmt. */
3911 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3913 node = TREE_CHAIN (node);
3914 continue;
3917 /* Remember that we had an initialization or finalization at this
3918 priority. */
3919 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3920 pi = get_priority_info (priority);
3921 if (initp)
3922 pi->initializations_p = 1;
3923 else
3924 pi->destructions_p = 1;
3926 /* Conditionalize this initialization on being in the right priority
3927 and being initializing/finalizing appropriately. */
3928 priority_if_stmt = begin_if_stmt ();
3929 cond = cp_build_binary_op (input_location,
3930 EQ_EXPR,
3931 priority_decl,
3932 build_int_cst (NULL_TREE, priority),
3933 tf_warning_or_error);
3934 finish_if_stmt_cond (cond, priority_if_stmt);
3936 /* Process initializers with same priority. */
3937 for (; node
3938 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3939 node = TREE_CHAIN (node))
3940 /* Do one initialization or destruction. */
3941 one_static_initialization_or_destruction (TREE_VALUE (node),
3942 TREE_PURPOSE (node), initp);
3944 /* Finish up the priority if-stmt body. */
3945 finish_then_clause (priority_if_stmt);
3946 finish_if_stmt (priority_if_stmt);
3948 } while (node);
3950 /* Revert what __asan_before_dynamic_init did by calling
3951 __asan_after_dynamic_init. */
3952 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
3953 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3955 /* Finish up the init/destruct if-stmt body. */
3956 finish_then_clause (init_if_stmt);
3957 finish_if_stmt (init_if_stmt);
3960 /* VARS is a list of variables with static storage duration which may
3961 need initialization and/or finalization. Remove those variables
3962 that don't really need to be initialized or finalized, and return
3963 the resulting list. The order in which the variables appear in
3964 VARS is in reverse order of the order in which they should actually
3965 be initialized. The list we return is in the unreversed order;
3966 i.e., the first variable should be initialized first. */
3968 static tree
3969 prune_vars_needing_no_initialization (tree *vars)
3971 tree *var = vars;
3972 tree result = NULL_TREE;
3974 while (*var)
3976 tree t = *var;
3977 tree decl = TREE_VALUE (t);
3978 tree init = TREE_PURPOSE (t);
3980 /* Deal gracefully with error. */
3981 if (error_operand_p (decl))
3983 var = &TREE_CHAIN (t);
3984 continue;
3987 /* The only things that can be initialized are variables. */
3988 gcc_assert (VAR_P (decl));
3990 /* If this object is not defined, we don't need to do anything
3991 here. */
3992 if (DECL_EXTERNAL (decl))
3994 var = &TREE_CHAIN (t);
3995 continue;
3998 /* Also, if the initializer already contains errors, we can bail
3999 out now. */
4000 if (init && TREE_CODE (init) == TREE_LIST
4001 && value_member (error_mark_node, init))
4003 var = &TREE_CHAIN (t);
4004 continue;
4007 /* This variable is going to need initialization and/or
4008 finalization, so we add it to the list. */
4009 *var = TREE_CHAIN (t);
4010 TREE_CHAIN (t) = result;
4011 result = t;
4014 return result;
4017 /* Make sure we have told the back end about all the variables in
4018 VARS. */
4020 static void
4021 write_out_vars (tree vars)
4023 tree v;
4025 for (v = vars; v; v = TREE_CHAIN (v))
4027 tree var = TREE_VALUE (v);
4028 if (!var_finalized_p (var))
4030 import_export_decl (var);
4031 rest_of_decl_compilation (var, 1, 1);
4036 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
4037 (otherwise) that will initialize all global objects with static
4038 storage duration having the indicated PRIORITY. */
4040 static void
4041 generate_ctor_or_dtor_function (bool constructor_p, int priority,
4042 location_t *locus)
4044 char function_key;
4045 tree fndecl;
4046 tree body;
4047 size_t i;
4049 input_location = *locus;
4050 /* ??? */
4051 /* Was: locus->line++; */
4053 /* We use `I' to indicate initialization and `D' to indicate
4054 destruction. */
4055 function_key = constructor_p ? 'I' : 'D';
4057 /* We emit the function lazily, to avoid generating empty
4058 global constructors and destructors. */
4059 body = NULL_TREE;
4061 /* For Objective-C++, we may need to initialize metadata found in this module.
4062 This must be done _before_ any other static initializations. */
4063 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
4064 && constructor_p && objc_static_init_needed_p ())
4066 body = start_objects (function_key, priority);
4067 objc_generate_static_init_call (NULL_TREE);
4070 /* Call the static storage duration function with appropriate
4071 arguments. */
4072 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
4074 /* Calls to pure or const functions will expand to nothing. */
4075 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
4077 tree call;
4079 if (! body)
4080 body = start_objects (function_key, priority);
4082 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
4083 build_int_cst (NULL_TREE,
4084 constructor_p),
4085 build_int_cst (NULL_TREE,
4086 priority),
4087 NULL_TREE);
4088 finish_expr_stmt (call);
4092 /* Close out the function. */
4093 if (body)
4094 finish_objects (function_key, priority, body);
4097 /* Generate constructor and destructor functions for the priority
4098 indicated by N. */
4100 static int
4101 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
4103 location_t *locus = (location_t *) data;
4104 int priority = (int) n->key;
4105 priority_info pi = (priority_info) n->value;
4107 /* Generate the functions themselves, but only if they are really
4108 needed. */
4109 if (pi->initializations_p)
4110 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
4111 if (pi->destructions_p)
4112 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
4114 /* Keep iterating. */
4115 return 0;
4118 /* Return C++ property of T, based on given operation OP. */
4120 static int
4121 cpp_check (tree t, cpp_operation op)
4123 switch (op)
4125 case HAS_DEPENDENT_TEMPLATE_ARGS:
4127 tree ti = CLASSTYPE_TEMPLATE_INFO (t);
4128 if (!ti)
4129 return 0;
4130 ++processing_template_decl;
4131 const bool dep = any_dependent_template_arguments_p (TI_ARGS (ti));
4132 --processing_template_decl;
4133 return dep;
4135 case IS_ABSTRACT:
4136 return DECL_PURE_VIRTUAL_P (t);
4137 case IS_CONSTRUCTOR:
4138 return DECL_CONSTRUCTOR_P (t);
4139 case IS_DESTRUCTOR:
4140 return DECL_DESTRUCTOR_P (t);
4141 case IS_COPY_CONSTRUCTOR:
4142 return DECL_COPY_CONSTRUCTOR_P (t);
4143 case IS_MOVE_CONSTRUCTOR:
4144 return DECL_MOVE_CONSTRUCTOR_P (t);
4145 case IS_TEMPLATE:
4146 return TREE_CODE (t) == TEMPLATE_DECL;
4147 case IS_TRIVIAL:
4148 return trivial_type_p (t);
4149 default:
4150 return 0;
4154 /* Collect source file references recursively, starting from NAMESPC. */
4156 static void
4157 collect_source_refs (tree namespc)
4159 /* Iterate over names in this name space. */
4160 for (tree t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4161 if (DECL_IS_BUILTIN (t))
4163 else if (TREE_CODE (t) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (t))
4164 collect_source_refs (t);
4165 else
4166 collect_source_ref (DECL_SOURCE_FILE (t));
4169 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4170 starting from NAMESPC. */
4172 static void
4173 collect_ada_namespace (tree namespc, const char *source_file)
4175 tree decl = NAMESPACE_LEVEL (namespc)->names;
4177 /* Collect decls from this namespace. This will skip
4178 NAMESPACE_DECLs (both aliases and regular, it cannot tell). */
4179 collect_ada_nodes (decl, source_file);
4181 /* Now scan for namespace children, and dump them. */
4182 for (; decl; decl = TREE_CHAIN (decl))
4183 if (TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl))
4184 collect_ada_namespace (decl, source_file);
4187 /* Returns true iff there is a definition available for variable or
4188 function DECL. */
4190 bool
4191 decl_defined_p (tree decl)
4193 if (TREE_CODE (decl) == FUNCTION_DECL)
4194 return (DECL_INITIAL (decl) != NULL_TREE
4195 /* A pending instantiation of a friend temploid is defined. */
4196 || (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
4197 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4198 (DECL_TI_TEMPLATE (decl)))));
4199 else
4201 gcc_assert (VAR_P (decl));
4202 return !DECL_EXTERNAL (decl);
4206 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4208 [expr.const]
4210 An integral constant-expression can only involve ... const
4211 variables of integral or enumeration types initialized with
4212 constant expressions ...
4214 C++0x also allows constexpr variables and temporaries initialized
4215 with constant expressions. We handle the former here, but the latter
4216 are just folded away in cxx_eval_constant_expression.
4218 The standard does not require that the expression be non-volatile.
4219 G++ implements the proposed correction in DR 457. */
4221 bool
4222 decl_constant_var_p (tree decl)
4224 if (!decl_maybe_constant_var_p (decl))
4225 return false;
4227 /* We don't know if a template static data member is initialized with
4228 a constant expression until we instantiate its initializer. Even
4229 in the case of a constexpr variable, we can't treat it as a
4230 constant until its initializer is complete in case it's used in
4231 its own initializer. */
4232 maybe_instantiate_decl (decl);
4233 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4236 /* Returns true if DECL could be a symbolic constant variable, depending on
4237 its initializer. */
4239 bool
4240 decl_maybe_constant_var_p (tree decl)
4242 tree type = TREE_TYPE (decl);
4243 if (!VAR_P (decl))
4244 return false;
4245 if (DECL_DECLARED_CONSTEXPR_P (decl))
4246 return true;
4247 if (DECL_HAS_VALUE_EXPR_P (decl))
4248 /* A proxy isn't constant. */
4249 return false;
4250 if (TYPE_REF_P (type))
4251 /* References can be constant. */;
4252 else if (CP_TYPE_CONST_NON_VOLATILE_P (type)
4253 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4254 /* And const integers. */;
4255 else
4256 return false;
4258 if (DECL_INITIAL (decl)
4259 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
4260 /* We know the initializer, and it isn't constant. */
4261 return false;
4262 else
4263 return true;
4266 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4267 called from grokfndecl and grokvardecl; in all modes it is called from
4268 cp_write_global_declarations. */
4270 void
4271 no_linkage_error (tree decl)
4273 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4274 /* In C++11 it's ok if the decl is defined. */
4275 return;
4276 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4277 if (t == NULL_TREE)
4278 /* The type that got us on no_linkage_decls must have gotten a name for
4279 linkage purposes. */;
4280 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4281 /* The type might end up having a typedef name for linkage purposes. */
4282 vec_safe_push (no_linkage_decls, decl);
4283 else if (TYPE_UNNAMED_P (t))
4285 bool d = false;
4286 if (cxx_dialect >= cxx11)
4287 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4288 "unnamed type, is used but never defined", decl);
4289 else if (DECL_EXTERN_C_P (decl))
4290 /* Allow this; it's pretty common in C. */;
4291 else if (VAR_P (decl))
4292 /* DRs 132, 319 and 389 seem to indicate types with
4293 no linkage can only be used to declare extern "C"
4294 entities. Since it's not always an error in the
4295 ISO C++ 90 Standard, we only issue a warning. */
4296 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "unnamed type "
4297 "with no linkage used to declare variable %q#D with "
4298 "linkage", decl);
4299 else
4300 d = permerror (DECL_SOURCE_LOCATION (decl), "unnamed type with no "
4301 "linkage used to declare function %q#D with linkage",
4302 decl);
4303 if (d && is_typedef_decl (TYPE_NAME (t)))
4304 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4305 "to the unqualified type, so it is not used for linkage",
4306 TYPE_NAME (t));
4308 else if (cxx_dialect >= cxx11)
4310 if (VAR_P (decl) || !DECL_PURE_VIRTUAL_P (decl))
4311 permerror (DECL_SOURCE_LOCATION (decl),
4312 "%q#D, declared using local type "
4313 "%qT, is used but never defined", decl, t);
4315 else if (VAR_P (decl))
4316 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4317 "used to declare variable %q#D with linkage", t, decl);
4318 else
4319 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4320 "to declare function %q#D with linkage", t, decl);
4323 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4325 static void
4326 collect_all_refs (const char *source_file)
4328 collect_ada_namespace (global_namespace, source_file);
4331 /* Clear DECL_EXTERNAL for NODE. */
4333 static bool
4334 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4336 DECL_EXTERNAL (node->decl) = 0;
4337 return false;
4340 /* Build up the function to run dynamic initializers for thread_local
4341 variables in this translation unit and alias the init functions for the
4342 individual variables to it. */
4344 static void
4345 handle_tls_init (void)
4347 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4348 if (vars == NULL_TREE)
4349 return;
4351 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4353 write_out_vars (vars);
4355 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4356 boolean_type_node);
4357 TREE_PUBLIC (guard) = false;
4358 TREE_STATIC (guard) = true;
4359 DECL_ARTIFICIAL (guard) = true;
4360 DECL_IGNORED_P (guard) = true;
4361 TREE_USED (guard) = true;
4362 CP_DECL_THREAD_LOCAL_P (guard) = true;
4363 set_decl_tls_model (guard, decl_default_tls_model (guard));
4364 pushdecl_top_level_and_finish (guard, NULL_TREE);
4366 tree fn = get_local_tls_init_fn ();
4367 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4368 tree body = begin_function_body ();
4369 tree if_stmt = begin_if_stmt ();
4370 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4371 tf_warning_or_error);
4372 finish_if_stmt_cond (cond, if_stmt);
4373 finish_expr_stmt (cp_build_modify_expr (loc, guard, NOP_EXPR,
4374 boolean_true_node,
4375 tf_warning_or_error));
4376 for (; vars; vars = TREE_CHAIN (vars))
4378 tree var = TREE_VALUE (vars);
4379 tree init = TREE_PURPOSE (vars);
4380 one_static_initialization_or_destruction (var, init, true);
4382 /* Output init aliases even with -fno-extern-tls-init. */
4383 if (TARGET_SUPPORTS_ALIASES && TREE_PUBLIC (var))
4385 tree single_init_fn = get_tls_init_fn (var);
4386 if (single_init_fn == NULL_TREE)
4387 continue;
4388 cgraph_node *alias
4389 = cgraph_node::get_create (fn)->create_same_body_alias
4390 (single_init_fn, fn);
4391 gcc_assert (alias != NULL);
4395 finish_then_clause (if_stmt);
4396 finish_if_stmt (if_stmt);
4397 finish_function_body (body);
4398 expand_or_defer_fn (finish_function (/*inline_p=*/false));
4401 /* We're at the end of compilation, so generate any mangling aliases that
4402 we've been saving up, if DECL is going to be output and ID2 isn't
4403 already taken by another declaration. */
4405 static void
4406 generate_mangling_alias (tree decl, tree id2)
4408 struct cgraph_node *n = NULL;
4410 if (TREE_CODE (decl) == FUNCTION_DECL)
4412 n = cgraph_node::get (decl);
4413 if (!n)
4414 /* Don't create an alias to an unreferenced function. */
4415 return;
4418 tree *slot
4419 = mangled_decls->find_slot_with_hash (id2, IDENTIFIER_HASH_VALUE (id2),
4420 INSERT);
4422 /* If there's a declaration already using this mangled name,
4423 don't create a compatibility alias that conflicts. */
4424 if (*slot)
4425 return;
4427 tree alias = make_alias_for (decl, id2);
4428 *slot = alias;
4430 DECL_IGNORED_P (alias) = 1;
4431 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
4432 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
4433 if (vague_linkage_p (decl))
4434 DECL_WEAK (alias) = 1;
4436 if (n)
4437 n->create_same_body_alias (alias, decl);
4438 else
4439 varpool_node::create_extra_name_alias (alias, decl);
4442 /* Note that we might want to emit an alias with the symbol ID2 for DECL at
4443 the end of translation, for compatibility across bugs in the mangling
4444 implementation. */
4446 void
4447 note_mangling_alias (tree decl, tree id2)
4449 if (TARGET_SUPPORTS_ALIASES)
4451 if (!defer_mangling_aliases)
4452 generate_mangling_alias (decl, id2);
4453 else
4455 vec_safe_push (mangling_aliases, decl);
4456 vec_safe_push (mangling_aliases, id2);
4461 /* Emit all mangling aliases that were deferred up to this point. */
4463 void
4464 generate_mangling_aliases ()
4466 while (!vec_safe_is_empty (mangling_aliases))
4468 tree id2 = mangling_aliases->pop();
4469 tree decl = mangling_aliases->pop();
4470 generate_mangling_alias (decl, id2);
4472 defer_mangling_aliases = false;
4475 /* Record a mangling of DECL, whose DECL_ASSEMBLER_NAME has just been
4476 set. NEED_WARNING is true if we must warn about collisions. We do
4477 this to spot changes in mangling that may require compatibility
4478 aliases. */
4480 void
4481 record_mangling (tree decl, bool need_warning)
4483 if (!mangled_decls)
4484 mangled_decls = hash_table<mangled_decl_hash>::create_ggc (499);
4486 gcc_checking_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
4487 tree id = DECL_ASSEMBLER_NAME_RAW (decl);
4488 tree *slot
4489 = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id),
4490 INSERT);
4492 /* If this is already an alias, remove the alias, because the real
4493 decl takes precedence. */
4494 if (*slot && DECL_ARTIFICIAL (*slot) && DECL_IGNORED_P (*slot))
4495 if (symtab_node *n = symtab_node::get (*slot))
4496 if (n->cpp_implicit_alias)
4498 n->remove ();
4499 *slot = NULL_TREE;
4502 if (!*slot)
4503 *slot = decl;
4504 else if (need_warning)
4506 error_at (DECL_SOURCE_LOCATION (decl),
4507 "mangling of %q#D as %qE conflicts with a previous mangle",
4508 decl, id);
4509 inform (DECL_SOURCE_LOCATION (*slot),
4510 "previous mangling %q#D", *slot);
4511 inform (DECL_SOURCE_LOCATION (decl),
4512 "a later -fabi-version= (or =0)"
4513 " avoids this error with a change in mangling");
4514 *slot = decl;
4518 /* The mangled name of DECL is being forcibly changed to NAME. Remove
4519 any existing knowledge of DECL's mangled name meaning DECL. */
4521 void
4522 overwrite_mangling (tree decl, tree name)
4524 if (tree id = DECL_ASSEMBLER_NAME_RAW (decl))
4525 if ((TREE_CODE (decl) == VAR_DECL
4526 || TREE_CODE (decl) == FUNCTION_DECL)
4527 && mangled_decls)
4528 if (tree *slot
4529 = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id),
4530 NO_INSERT))
4531 if (*slot == decl)
4533 mangled_decls->clear_slot (slot);
4535 /* If this is an alias, remove it from the symbol table. */
4536 if (DECL_ARTIFICIAL (decl) && DECL_IGNORED_P (decl))
4537 if (symtab_node *n = symtab_node::get (decl))
4538 if (n->cpp_implicit_alias)
4539 n->remove ();
4542 DECL_ASSEMBLER_NAME_RAW (decl) = name;
4545 /* The entire file is now complete. If requested, dump everything
4546 to a file. */
4548 static void
4549 dump_tu (void)
4551 dump_flags_t flags;
4552 if (FILE *stream = dump_begin (raw_dump_id, &flags))
4554 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4555 dump_end (raw_dump_id, stream);
4559 static location_t locus_at_end_of_parsing;
4561 /* Check the deallocation functions for CODE to see if we want to warn that
4562 only one was defined. */
4564 static void
4565 maybe_warn_sized_delete (enum tree_code code)
4567 tree sized = NULL_TREE;
4568 tree unsized = NULL_TREE;
4570 for (ovl_iterator iter (get_global_binding (ovl_op_identifier (false, code)));
4571 iter; ++iter)
4573 tree fn = *iter;
4574 /* We're only interested in usual deallocation functions. */
4575 if (!usual_deallocation_fn_p (fn))
4576 continue;
4577 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
4578 unsized = fn;
4579 else
4580 sized = fn;
4582 if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
4583 warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
4584 "the program should also define %qD", sized);
4585 else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
4586 warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
4587 "the program should also define %qD", unsized);
4590 /* Check the global deallocation functions to see if we want to warn about
4591 defining unsized without sized (or vice versa). */
4593 static void
4594 maybe_warn_sized_delete ()
4596 if (!flag_sized_deallocation || !warn_sized_deallocation)
4597 return;
4598 maybe_warn_sized_delete (DELETE_EXPR);
4599 maybe_warn_sized_delete (VEC_DELETE_EXPR);
4602 /* Earlier we left PTRMEM_CST in variable initializers alone so that we could
4603 look them up when evaluating non-type template parameters. Now we need to
4604 lower them to something the back end can understand. */
4606 static void
4607 lower_var_init ()
4609 varpool_node *node;
4610 FOR_EACH_VARIABLE (node)
4612 tree d = node->decl;
4613 if (tree init = DECL_INITIAL (d))
4614 DECL_INITIAL (d) = cplus_expand_constant (init);
4618 /* This routine is called at the end of compilation.
4619 Its job is to create all the code needed to initialize and
4620 destroy the global aggregates. We do the destruction
4621 first, since that way we only need to reverse the decls once. */
4623 void
4624 c_parse_final_cleanups (void)
4626 tree vars;
4627 bool reconsider;
4628 size_t i;
4629 unsigned ssdf_count = 0;
4630 int retries = 0;
4631 tree decl;
4633 locus_at_end_of_parsing = input_location;
4634 at_eof = 1;
4636 /* Bad parse errors. Just forget about it. */
4637 if (! global_bindings_p () || current_class_type
4638 || !vec_safe_is_empty (decl_namespace_list))
4639 return;
4641 /* This is the point to write out a PCH if we're doing that.
4642 In that case we do not want to do anything else. */
4643 if (pch_file)
4645 /* Mangle all symbols at PCH creation time. */
4646 symtab_node *node;
4647 FOR_EACH_SYMBOL (node)
4648 if (! is_a <varpool_node *> (node)
4649 || ! DECL_HARD_REGISTER (node->decl))
4650 DECL_ASSEMBLER_NAME (node->decl);
4651 c_common_write_pch ();
4652 dump_tu ();
4653 /* Ensure even the callers don't try to finalize the CU. */
4654 flag_syntax_only = 1;
4655 return;
4658 timevar_stop (TV_PHASE_PARSING);
4659 timevar_start (TV_PHASE_DEFERRED);
4661 symtab->process_same_body_aliases ();
4663 /* Handle -fdump-ada-spec[-slim] */
4664 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4666 if (flag_dump_ada_spec_slim)
4667 collect_source_ref (main_input_filename);
4668 else
4669 collect_source_refs (global_namespace);
4671 dump_ada_specs (collect_all_refs, cpp_check);
4674 /* FIXME - huh? was input_line -= 1;*/
4676 /* We now have to write out all the stuff we put off writing out.
4677 These include:
4679 o Template specializations that we have not yet instantiated,
4680 but which are needed.
4681 o Initialization and destruction for non-local objects with
4682 static storage duration. (Local objects with static storage
4683 duration are initialized when their scope is first entered,
4684 and are cleaned up via atexit.)
4685 o Virtual function tables.
4687 All of these may cause others to be needed. For example,
4688 instantiating one function may cause another to be needed, and
4689 generating the initializer for an object may cause templates to be
4690 instantiated, etc., etc. */
4692 emit_support_tinfos ();
4696 tree t;
4697 tree decl;
4699 reconsider = false;
4701 /* If there are templates that we've put off instantiating, do
4702 them now. */
4703 instantiate_pending_templates (retries);
4704 ggc_collect ();
4706 /* Write out virtual tables as required. Writing out the
4707 virtual table for a template class may cause the
4708 instantiation of members of that class. If we write out
4709 vtables then we remove the class from our list so we don't
4710 have to look at it again. */
4711 for (i = keyed_classes->length ();
4712 keyed_classes->iterate (--i, &t);)
4713 if (maybe_emit_vtables (t))
4715 reconsider = true;
4716 keyed_classes->unordered_remove (i);
4718 /* The input_location may have been changed during marking of
4719 vtable entries. */
4720 input_location = locus_at_end_of_parsing;
4722 /* Write out needed type info variables. We have to be careful
4723 looping through unemitted decls, because emit_tinfo_decl may
4724 cause other variables to be needed. New elements will be
4725 appended, and we remove from the vector those that actually
4726 get emitted. */
4727 for (i = unemitted_tinfo_decls->length ();
4728 unemitted_tinfo_decls->iterate (--i, &t);)
4729 if (emit_tinfo_decl (t))
4731 reconsider = true;
4732 unemitted_tinfo_decls->unordered_remove (i);
4735 /* The list of objects with static storage duration is built up
4736 in reverse order. We clear STATIC_AGGREGATES so that any new
4737 aggregates added during the initialization of these will be
4738 initialized in the correct order when we next come around the
4739 loop. */
4740 vars = prune_vars_needing_no_initialization (&static_aggregates);
4742 if (vars)
4744 /* We need to start a new initialization function each time
4745 through the loop. That's because we need to know which
4746 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4747 isn't computed until a function is finished, and written
4748 out. That's a deficiency in the back end. When this is
4749 fixed, these initialization functions could all become
4750 inline, with resulting performance improvements. */
4751 tree ssdf_body;
4753 /* Set the line and file, so that it is obviously not from
4754 the source file. */
4755 input_location = locus_at_end_of_parsing;
4756 ssdf_body = start_static_storage_duration_function (ssdf_count);
4758 /* Make sure the back end knows about all the variables. */
4759 write_out_vars (vars);
4761 /* First generate code to do all the initializations. */
4762 if (vars)
4763 do_static_initialization_or_destruction (vars, /*initp=*/true);
4765 /* Then, generate code to do all the destructions. Do these
4766 in reverse order so that the most recently constructed
4767 variable is the first destroyed. If we're using
4768 __cxa_atexit, then we don't need to do this; functions
4769 were registered at initialization time to destroy the
4770 local statics. */
4771 if (!flag_use_cxa_atexit && vars)
4773 vars = nreverse (vars);
4774 do_static_initialization_or_destruction (vars, /*initp=*/false);
4776 else
4777 vars = NULL_TREE;
4779 /* Finish up the static storage duration function for this
4780 round. */
4781 input_location = locus_at_end_of_parsing;
4782 finish_static_storage_duration_function (ssdf_body);
4784 /* All those initializations and finalizations might cause
4785 us to need more inline functions, more template
4786 instantiations, etc. */
4787 reconsider = true;
4788 ssdf_count++;
4789 /* ??? was: locus_at_end_of_parsing.line++; */
4792 /* Now do the same for thread_local variables. */
4793 handle_tls_init ();
4795 /* Go through the set of inline functions whose bodies have not
4796 been emitted yet. If out-of-line copies of these functions
4797 are required, emit them. */
4798 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4800 /* Does it need synthesizing? */
4801 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4802 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4804 /* Even though we're already at the top-level, we push
4805 there again. That way, when we pop back a few lines
4806 hence, all of our state is restored. Otherwise,
4807 finish_function doesn't clean things up, and we end
4808 up with CURRENT_FUNCTION_DECL set. */
4809 push_to_top_level ();
4810 /* The decl's location will mark where it was first
4811 needed. Save that so synthesize method can indicate
4812 where it was needed from, in case of error */
4813 input_location = DECL_SOURCE_LOCATION (decl);
4814 synthesize_method (decl);
4815 pop_from_top_level ();
4816 reconsider = true;
4819 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4820 generate_tls_wrapper (decl);
4822 if (!DECL_SAVED_TREE (decl))
4823 continue;
4825 cgraph_node *node = cgraph_node::get_create (decl);
4827 /* We lie to the back end, pretending that some functions
4828 are not defined when they really are. This keeps these
4829 functions from being put out unnecessarily. But, we must
4830 stop lying when the functions are referenced, or if they
4831 are not comdat since they need to be put out now. If
4832 DECL_INTERFACE_KNOWN, then we have already set
4833 DECL_EXTERNAL appropriately, so there's no need to check
4834 again, and we do not want to clear DECL_EXTERNAL if a
4835 previous call to import_export_decl set it.
4837 This is done in a separate for cycle, because if some
4838 deferred function is contained in another deferred
4839 function later in deferred_fns varray,
4840 rest_of_compilation would skip this function and we
4841 really cannot expand the same function twice. */
4842 import_export_decl (decl);
4843 if (DECL_NOT_REALLY_EXTERN (decl)
4844 && DECL_INITIAL (decl)
4845 && decl_needed_p (decl))
4847 if (node->cpp_implicit_alias)
4848 node = node->get_alias_target ();
4850 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
4851 NULL, true);
4852 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4853 group, we need to mark all symbols in the same comdat group
4854 that way. */
4855 if (node->same_comdat_group)
4856 for (cgraph_node *next
4857 = dyn_cast<cgraph_node *> (node->same_comdat_group);
4858 next != node;
4859 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
4860 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
4861 NULL, true);
4864 /* If we're going to need to write this function out, and
4865 there's already a body for it, create RTL for it now.
4866 (There might be no body if this is a method we haven't
4867 gotten around to synthesizing yet.) */
4868 if (!DECL_EXTERNAL (decl)
4869 && decl_needed_p (decl)
4870 && !TREE_ASM_WRITTEN (decl)
4871 && !node->definition)
4873 /* We will output the function; no longer consider it in this
4874 loop. */
4875 DECL_DEFER_OUTPUT (decl) = 0;
4876 /* Generate RTL for this function now that we know we
4877 need it. */
4878 expand_or_defer_fn (decl);
4879 /* If we're compiling -fsyntax-only pretend that this
4880 function has been written out so that we don't try to
4881 expand it again. */
4882 if (flag_syntax_only)
4883 TREE_ASM_WRITTEN (decl) = 1;
4884 reconsider = true;
4888 if (wrapup_namespace_globals ())
4889 reconsider = true;
4891 /* Static data members are just like namespace-scope globals. */
4892 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4894 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4895 /* Don't write it out if we haven't seen a definition. */
4896 || (DECL_IN_AGGR_P (decl) && !DECL_INLINE_VAR_P (decl)))
4897 continue;
4898 import_export_decl (decl);
4899 /* If this static data member is needed, provide it to the
4900 back end. */
4901 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4902 DECL_EXTERNAL (decl) = 0;
4904 if (vec_safe_length (pending_statics) != 0
4905 && wrapup_global_declarations (pending_statics->address (),
4906 pending_statics->length ()))
4907 reconsider = true;
4909 retries++;
4911 while (reconsider);
4913 lower_var_init ();
4915 generate_mangling_aliases ();
4917 /* All used inline functions must have a definition at this point. */
4918 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4920 if (/* Check online inline functions that were actually used. */
4921 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4922 /* If the definition actually was available here, then the
4923 fact that the function was not defined merely represents
4924 that for some reason (use of a template repository,
4925 #pragma interface, etc.) we decided not to emit the
4926 definition here. */
4927 && !DECL_INITIAL (decl)
4928 /* Don't complain if the template was defined. */
4929 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4930 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4931 (template_for_substitution (decl)))))
4933 warning_at (DECL_SOURCE_LOCATION (decl), 0,
4934 "inline function %qD used but never defined", decl);
4935 /* Avoid a duplicate warning from check_global_declaration. */
4936 TREE_NO_WARNING (decl) = 1;
4940 /* So must decls that use a type with no linkage. */
4941 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4942 no_linkage_error (decl);
4944 maybe_warn_sized_delete ();
4946 /* Then, do the Objective-C stuff. This is where all the
4947 Objective-C module stuff gets generated (symtab,
4948 class/protocol/selector lists etc). This must be done after C++
4949 templates, destructors etc. so that selectors used in C++
4950 templates are properly allocated. */
4951 if (c_dialect_objc ())
4952 objc_write_global_declarations ();
4954 /* We give C linkage to static constructors and destructors. */
4955 push_lang_context (lang_name_c);
4957 /* Generate initialization and destruction functions for all
4958 priorities for which they are required. */
4959 if (priority_info_map)
4960 splay_tree_foreach (priority_info_map,
4961 generate_ctor_and_dtor_functions_for_priority,
4962 /*data=*/&locus_at_end_of_parsing);
4963 else if (c_dialect_objc () && objc_static_init_needed_p ())
4964 /* If this is obj-c++ and we need a static init, call
4965 generate_ctor_or_dtor_function. */
4966 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4967 DEFAULT_INIT_PRIORITY,
4968 &locus_at_end_of_parsing);
4970 /* We're done with the splay-tree now. */
4971 if (priority_info_map)
4972 splay_tree_delete (priority_info_map);
4974 /* Generate any missing aliases. */
4975 maybe_apply_pending_pragma_weaks ();
4977 /* We're done with static constructors, so we can go back to "C++"
4978 linkage now. */
4979 pop_lang_context ();
4981 if (flag_vtable_verify)
4983 vtv_recover_class_info ();
4984 vtv_compute_class_hierarchy_transitive_closure ();
4985 vtv_build_vtable_verify_fndecl ();
4988 perform_deferred_noexcept_checks ();
4990 finish_repo ();
4991 fini_constexpr ();
4993 /* The entire file is now complete. If requested, dump everything
4994 to a file. */
4995 dump_tu ();
4997 if (flag_detailed_statistics)
4999 dump_tree_statistics ();
5000 dump_time_statistics ();
5003 timevar_stop (TV_PHASE_DEFERRED);
5004 timevar_start (TV_PHASE_PARSING);
5006 /* Indicate that we're done with front end processing. */
5007 at_eof = 2;
5010 /* Perform any post compilation-proper cleanups for the C++ front-end.
5011 This should really go away. No front-end should need to do
5012 anything past the compilation process. */
5014 void
5015 cxx_post_compilation_parsing_cleanups (void)
5017 timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS);
5019 if (flag_vtable_verify)
5021 /* Generate the special constructor initialization function that
5022 calls __VLTRegisterPairs, and give it a very high
5023 initialization priority. This must be done after
5024 finalize_compilation_unit so that we have accurate
5025 information about which vtable will actually be emitted. */
5026 vtv_generate_init_routine ();
5029 input_location = locus_at_end_of_parsing;
5031 if (flag_checking)
5032 validate_conversion_obstack ();
5034 timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS);
5037 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
5038 function to call in parse-tree form; it has not yet been
5039 semantically analyzed. ARGS are the arguments to the function.
5040 They have already been semantically analyzed. This may change
5041 ARGS. */
5043 tree
5044 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
5045 tsubst_flags_t complain)
5047 tree orig_fn;
5048 vec<tree, va_gc> *orig_args = NULL;
5049 tree expr;
5050 tree object;
5052 orig_fn = fn;
5053 object = TREE_OPERAND (fn, 0);
5055 if (processing_template_decl)
5057 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
5058 || TREE_CODE (fn) == MEMBER_REF);
5059 if (type_dependent_expression_p (fn)
5060 || any_type_dependent_arguments_p (*args))
5061 return build_min_nt_call_vec (fn, *args);
5063 orig_args = make_tree_vector_copy (*args);
5065 /* Transform the arguments and add the implicit "this"
5066 parameter. That must be done before the FN is transformed
5067 because we depend on the form of FN. */
5068 make_args_non_dependent (*args);
5069 object = build_non_dependent_expr (object);
5070 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5072 if (TREE_CODE (fn) == DOTSTAR_EXPR)
5073 object = cp_build_addr_expr (object, complain);
5074 vec_safe_insert (*args, 0, object);
5076 /* Now that the arguments are done, transform FN. */
5077 fn = build_non_dependent_expr (fn);
5080 /* A qualified name corresponding to a bound pointer-to-member is
5081 represented as an OFFSET_REF:
5083 struct B { void g(); };
5084 void (B::*p)();
5085 void B::g() { (this->*p)(); } */
5086 if (TREE_CODE (fn) == OFFSET_REF)
5088 tree object_addr = cp_build_addr_expr (object, complain);
5089 fn = TREE_OPERAND (fn, 1);
5090 fn = get_member_function_from_ptrfunc (&object_addr, fn,
5091 complain);
5092 vec_safe_insert (*args, 0, object_addr);
5095 if (CLASS_TYPE_P (TREE_TYPE (fn)))
5096 expr = build_op_call (fn, args, complain);
5097 else
5098 expr = cp_build_function_call_vec (fn, args, complain);
5099 if (processing_template_decl && expr != error_mark_node)
5100 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
5102 if (orig_args != NULL)
5103 release_tree_vector (orig_args);
5105 return expr;
5109 void
5110 check_default_args (tree x)
5112 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
5113 bool saw_def = false;
5114 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
5115 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
5117 if (TREE_PURPOSE (arg))
5118 saw_def = true;
5119 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
5121 error ("default argument missing for parameter %P of %q+#D", i, x);
5122 TREE_PURPOSE (arg) = error_mark_node;
5127 /* Return true if function DECL can be inlined. This is used to force
5128 instantiation of methods that might be interesting for inlining. */
5129 bool
5130 possibly_inlined_p (tree decl)
5132 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
5133 if (DECL_UNINLINABLE (decl))
5134 return false;
5135 if (!optimize)
5136 return DECL_DECLARED_INLINE_P (decl);
5137 /* When optimizing, we might inline everything when flatten
5138 attribute or heuristics inlining for size or autoinlining
5139 is used. */
5140 return true;
5143 /* Normally, we can wait until instantiation-time to synthesize DECL.
5144 However, if DECL is a static data member initialized with a constant
5145 or a constexpr function, we need it right now because a reference to
5146 such a data member or a call to such function is not value-dependent.
5147 For a function that uses auto in the return type, we need to instantiate
5148 it to find out its type. For OpenMP user defined reductions, we need
5149 them instantiated for reduction clauses which inline them by hand
5150 directly. */
5152 static void
5153 maybe_instantiate_decl (tree decl)
5155 if (DECL_LANG_SPECIFIC (decl)
5156 && DECL_TEMPLATE_INFO (decl)
5157 && (decl_maybe_constant_var_p (decl)
5158 || (TREE_CODE (decl) == FUNCTION_DECL
5159 && DECL_OMP_DECLARE_REDUCTION_P (decl))
5160 || undeduced_auto_decl (decl))
5161 && !DECL_DECLARED_CONCEPT_P (decl)
5162 && !uses_template_parms (DECL_TI_ARGS (decl)))
5164 /* Instantiating a function will result in garbage collection. We
5165 must treat this situation as if we were within the body of a
5166 function so as to avoid collecting live data only referenced from
5167 the stack (such as overload resolution candidates). */
5168 ++function_depth;
5169 instantiate_decl (decl, /*defer_ok=*/false,
5170 /*expl_inst_class_mem_p=*/false);
5171 --function_depth;
5175 /* Maybe warn if DECL is deprecated, subject to COMPLAIN. Returns whether or
5176 not a warning was emitted. */
5178 bool
5179 cp_warn_deprecated_use (tree decl, tsubst_flags_t complain)
5181 if (!(complain & tf_warning) || !decl
5182 || deprecated_state == DEPRECATED_SUPPRESS)
5183 return false;
5185 if (!TREE_DEPRECATED (decl))
5187 /* Perhaps this is a deprecated typedef. */
5188 if (TYPE_P (decl) && TYPE_NAME (decl))
5189 decl = TYPE_NAME (decl);
5191 if (!TREE_DEPRECATED (decl))
5192 return false;
5195 /* Don't warn within members of a deprecated type. */
5196 if (TYPE_P (decl)
5197 && currently_open_class (decl))
5198 return false;
5200 bool warned = false;
5201 if (cxx_dialect >= cxx11
5202 && DECL_P (decl)
5203 && DECL_ARTIFICIAL (decl)
5204 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5205 && copy_fn_p (decl))
5207 warned = warning (OPT_Wdeprecated_copy,
5208 "implicitly-declared %qD is deprecated", decl);
5209 if (warned)
5211 tree ctx = DECL_CONTEXT (decl);
5212 tree other = classtype_has_user_copy_or_dtor (ctx);
5213 inform (DECL_SOURCE_LOCATION (other),
5214 "because %qT has user-provided %qD",
5215 ctx, other);
5218 else
5219 warned = warn_deprecated_use (decl, NULL_TREE);
5221 return warned;
5224 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
5225 If DECL is a specialization or implicitly declared class member,
5226 generate the actual definition. Return false if something goes
5227 wrong, true otherwise. */
5229 bool
5230 mark_used (tree decl, tsubst_flags_t complain)
5232 /* If we're just testing conversions or resolving overloads, we
5233 don't want any permanent effects like forcing functions to be
5234 output or instantiating templates. */
5235 if ((complain & tf_conv))
5236 return true;
5238 /* If DECL is a BASELINK for a single function, then treat it just
5239 like the DECL for the function. Otherwise, if the BASELINK is
5240 for an overloaded function, we don't know which function was
5241 actually used until after overload resolution. */
5242 if (BASELINK_P (decl))
5244 decl = BASELINK_FUNCTIONS (decl);
5245 if (really_overloaded_fn (decl))
5246 return true;
5247 decl = OVL_FIRST (decl);
5250 /* Set TREE_USED for the benefit of -Wunused. */
5251 TREE_USED (decl) = 1;
5252 /* And for structured bindings also the underlying decl. */
5253 if (DECL_DECOMPOSITION_P (decl) && DECL_DECOMP_BASE (decl))
5254 TREE_USED (DECL_DECOMP_BASE (decl)) = 1;
5256 if (TREE_CODE (decl) == TEMPLATE_DECL)
5257 return true;
5259 if (DECL_CLONED_FUNCTION_P (decl))
5260 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5262 /* Mark enumeration types as used. */
5263 if (TREE_CODE (decl) == CONST_DECL)
5264 used_types_insert (DECL_CONTEXT (decl));
5266 if (TREE_CODE (decl) == FUNCTION_DECL
5267 && !maybe_instantiate_noexcept (decl, complain))
5268 return false;
5270 if (TREE_CODE (decl) == FUNCTION_DECL
5271 && DECL_DELETED_FN (decl))
5273 if (DECL_ARTIFICIAL (decl)
5274 && DECL_CONV_FN_P (decl)
5275 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
5276 /* We mark a lambda conversion op as deleted if we can't
5277 generate it properly; see maybe_add_lambda_conv_op. */
5278 sorry ("converting lambda that uses %<...%> to function pointer");
5279 else if (complain & tf_error)
5281 error ("use of deleted function %qD", decl);
5282 if (!maybe_explain_implicit_delete (decl))
5283 inform (DECL_SOURCE_LOCATION (decl), "declared here");
5285 return false;
5288 cp_warn_deprecated_use (decl, complain);
5290 /* We can only check DECL_ODR_USED on variables or functions with
5291 DECL_LANG_SPECIFIC set, and these are also the only decls that we
5292 might need special handling for. */
5293 if (!VAR_OR_FUNCTION_DECL_P (decl)
5294 || DECL_LANG_SPECIFIC (decl) == NULL
5295 || DECL_THUNK_P (decl))
5297 if (!processing_template_decl
5298 && !require_deduced_type (decl, complain))
5299 return false;
5300 return true;
5303 /* We only want to do this processing once. We don't need to keep trying
5304 to instantiate inline templates, because unit-at-a-time will make sure
5305 we get them compiled before functions that want to inline them. */
5306 if (DECL_ODR_USED (decl))
5307 return true;
5309 /* Normally, we can wait until instantiation-time to synthesize DECL.
5310 However, if DECL is a static data member initialized with a constant
5311 or a constexpr function, we need it right now because a reference to
5312 such a data member or a call to such function is not value-dependent.
5313 For a function that uses auto in the return type, we need to instantiate
5314 it to find out its type. For OpenMP user defined reductions, we need
5315 them instantiated for reduction clauses which inline them by hand
5316 directly. */
5317 maybe_instantiate_decl (decl);
5319 if (processing_template_decl || in_template_function ())
5320 return true;
5322 /* Check this too in case we're within instantiate_non_dependent_expr. */
5323 if (DECL_TEMPLATE_INFO (decl)
5324 && uses_template_parms (DECL_TI_ARGS (decl)))
5325 return true;
5327 if (!require_deduced_type (decl, complain))
5328 return false;
5330 if (builtin_pack_fn_p (decl))
5332 error ("use of built-in parameter pack %qD outside of a template",
5333 DECL_NAME (decl));
5334 return false;
5337 /* If we don't need a value, then we don't need to synthesize DECL. */
5338 if (cp_unevaluated_operand || in_discarded_stmt)
5339 return true;
5341 DECL_ODR_USED (decl) = 1;
5342 if (DECL_CLONED_FUNCTION_P (decl))
5343 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5345 /* DR 757: A type without linkage shall not be used as the type of a
5346 variable or function with linkage, unless
5347 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5348 o the variable or function is not used (3.2 [basic.def.odr]) or is
5349 defined in the same translation unit. */
5350 if (cxx_dialect > cxx98
5351 && decl_linkage (decl) != lk_none
5352 && !DECL_EXTERN_C_P (decl)
5353 && !DECL_ARTIFICIAL (decl)
5354 && !decl_defined_p (decl)
5355 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
5357 if (is_local_extern (decl))
5358 /* There's no way to define a local extern, and adding it to
5359 the vector interferes with GC, so give an error now. */
5360 no_linkage_error (decl);
5361 else
5362 vec_safe_push (no_linkage_decls, decl);
5365 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
5366 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
5367 /* Remember it, so we can check it was defined. */
5368 note_vague_linkage_fn (decl);
5370 /* Is it a synthesized method that needs to be synthesized? */
5371 if (TREE_CODE (decl) == FUNCTION_DECL
5372 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5373 && DECL_DEFAULTED_FN (decl)
5374 /* A function defaulted outside the class is synthesized either by
5375 cp_finish_decl or instantiate_decl. */
5376 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
5377 && ! DECL_INITIAL (decl))
5379 /* Defer virtual destructors so that thunks get the right
5380 linkage. */
5381 if (DECL_VIRTUAL_P (decl) && !at_eof)
5383 note_vague_linkage_fn (decl);
5384 return true;
5387 /* Remember the current location for a function we will end up
5388 synthesizing. Then we can inform the user where it was
5389 required in the case of error. */
5390 DECL_SOURCE_LOCATION (decl) = input_location;
5392 /* Synthesizing an implicitly defined member function will result in
5393 garbage collection. We must treat this situation as if we were
5394 within the body of a function so as to avoid collecting live data
5395 on the stack (such as overload resolution candidates).
5397 We could just let cp_write_global_declarations handle synthesizing
5398 this function by adding it to deferred_fns, but doing
5399 it at the use site produces better error messages. */
5400 ++function_depth;
5401 synthesize_method (decl);
5402 --function_depth;
5403 /* If this is a synthesized method we don't need to
5404 do the instantiation test below. */
5406 else if (VAR_OR_FUNCTION_DECL_P (decl)
5407 && DECL_TEMPLATE_INFO (decl)
5408 && !DECL_DECLARED_CONCEPT_P (decl)
5409 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5410 || always_instantiate_p (decl)))
5411 /* If this is a function or variable that is an instance of some
5412 template, we now know that we will need to actually do the
5413 instantiation. We check that DECL is not an explicit
5414 instantiation because that is not checked in instantiate_decl.
5416 We put off instantiating functions in order to improve compile
5417 times. Maintaining a stack of active functions is expensive,
5418 and the inliner knows to instantiate any functions it might
5419 need. Therefore, we always try to defer instantiation. */
5421 ++function_depth;
5422 instantiate_decl (decl, /*defer_ok=*/true,
5423 /*expl_inst_class_mem_p=*/false);
5424 --function_depth;
5427 return true;
5430 bool
5431 mark_used (tree decl)
5433 return mark_used (decl, tf_warning_or_error);
5436 tree
5437 vtv_start_verification_constructor_init_function (void)
5439 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5442 tree
5443 vtv_finish_verification_constructor_init_function (tree function_body)
5445 tree fn;
5447 finish_compound_stmt (function_body);
5448 fn = finish_function (/*inline_p=*/false);
5449 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5450 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5452 return fn;
5455 #include "gt-cp-decl2.h"