PR rtl-optimization/82913
[official-gcc.git] / gcc / cp / decl2.c
blob0b183085e0e3b605df8aa5ee88402d22792c8fbb
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2017 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* Process declarations and symbol lookup for C++ front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "memmodel.h"
33 #include "target.h"
34 #include "cp-tree.h"
35 #include "c-family/c-common.h"
36 #include "timevar.h"
37 #include "stringpool.h"
38 #include "cgraph.h"
39 #include "varasm.h"
40 #include "attribs.h"
41 #include "stor-layout.h"
42 #include "calls.h"
43 #include "decl.h"
44 #include "toplev.h"
45 #include "c-family/c-objc.h"
46 #include "c-family/c-pragma.h"
47 #include "dumpfile.h"
48 #include "intl.h"
49 #include "c-family/c-ada-spec.h"
50 #include "asan.h"
52 /* Id for dumping the raw trees. */
53 int raw_dump_id;
55 extern cpp_reader *parse_in;
57 /* This structure contains information about the initializations
58 and/or destructions required for a particular priority level. */
59 typedef struct priority_info_s {
60 /* Nonzero if there have been any initializations at this priority
61 throughout the translation unit. */
62 int initializations_p;
63 /* Nonzero if there have been any destructions at this priority
64 throughout the translation unit. */
65 int destructions_p;
66 } *priority_info;
68 static void mark_vtable_entries (tree);
69 static bool maybe_emit_vtables (tree);
70 static tree start_objects (int, int);
71 static void finish_objects (int, int, tree);
72 static tree start_static_storage_duration_function (unsigned);
73 static void finish_static_storage_duration_function (tree);
74 static priority_info get_priority_info (int);
75 static void do_static_initialization_or_destruction (tree, bool);
76 static void one_static_initialization_or_destruction (tree, tree, bool);
77 static void generate_ctor_or_dtor_function (bool, int, location_t *);
78 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
79 void *);
80 static tree prune_vars_needing_no_initialization (tree *);
81 static void write_out_vars (tree);
82 static void import_export_class (tree);
83 static tree get_guard_bits (tree);
84 static void determine_visibility_from_class (tree, tree);
85 static bool determine_hidden_inline (tree);
86 static void maybe_instantiate_decl (tree);
88 /* A list of static class variables. This is needed, because a
89 static class variable can be declared inside the class without
90 an initializer, and then initialized, statically, outside the class. */
91 static GTY(()) vec<tree, va_gc> *pending_statics;
93 /* A list of functions which were declared inline, but which we
94 may need to emit outline anyway. */
95 static GTY(()) vec<tree, va_gc> *deferred_fns;
97 /* A list of decls that use types with no linkage, which we need to make
98 sure are defined. */
99 static GTY(()) vec<tree, va_gc> *no_linkage_decls;
101 /* A vector of alternating decls and identifiers, where the latter
102 is to be an alias for the former if the former is defined. */
103 static GTY(()) vec<tree, va_gc> *mangling_aliases;
105 /* 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 /* Nothing is deletable. Everything is insertable. */
127 static bool is_deleted (value_type) { return false; }
128 static void mark_deleted (value_type) { gcc_unreachable (); }
131 /* A hash table of decls keyed by mangled name. Used to figure out if
132 we need compatibility aliases. */
133 static GTY(()) hash_table<mangled_decl_hash> *mangled_decls;
135 /* Nonzero if we're done parsing and into end-of-file activities. */
137 int at_eof;
139 /* True if note_mangling_alias should enqueue mangling aliases for
140 later generation, rather than emitting them right away. */
142 bool defer_mangling_aliases = true;
145 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
146 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
147 that apply to the function). */
149 tree
150 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
151 cp_ref_qualifier rqual)
153 tree raises;
154 tree attrs;
155 int type_quals;
156 bool late_return_type_p;
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 type_quals = quals & ~TYPE_QUAL_RESTRICT;
165 ctype = cp_build_qualified_type (ctype, type_quals);
166 raises = TYPE_RAISES_EXCEPTIONS (fntype);
167 attrs = TYPE_ATTRIBUTES (fntype);
168 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
169 fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
170 (TREE_CODE (fntype) == METHOD_TYPE
171 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
172 : TYPE_ARG_TYPES (fntype)));
173 if (attrs)
174 fntype = cp_build_type_attribute_variant (fntype, attrs);
175 if (rqual)
176 fntype = build_ref_qualified_type (fntype, rqual);
177 if (raises)
178 fntype = build_exception_variant (fntype, raises);
179 if (late_return_type_p)
180 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
182 return fntype;
185 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
186 return type changed to NEW_RET. */
188 tree
189 change_return_type (tree new_ret, tree fntype)
191 tree newtype;
192 tree args = TYPE_ARG_TYPES (fntype);
193 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
194 tree attrs = TYPE_ATTRIBUTES (fntype);
195 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
197 if (new_ret == error_mark_node)
198 return fntype;
200 if (same_type_p (new_ret, TREE_TYPE (fntype)))
201 return fntype;
203 if (TREE_CODE (fntype) == FUNCTION_TYPE)
205 newtype = build_function_type (new_ret, args);
206 newtype = apply_memfn_quals (newtype,
207 type_memfn_quals (fntype),
208 type_memfn_rqual (fntype));
210 else
211 newtype = build_method_type_directly
212 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
213 if (FUNCTION_REF_QUALIFIED (fntype))
214 newtype = build_ref_qualified_type (newtype, type_memfn_rqual (fntype));
215 if (raises)
216 newtype = build_exception_variant (newtype, raises);
217 if (attrs)
218 newtype = cp_build_type_attribute_variant (newtype, attrs);
219 if (late_return_type_p)
220 TYPE_HAS_LATE_RETURN_TYPE (newtype) = 1;
222 return newtype;
225 /* Build a PARM_DECL of FN with NAME and TYPE, and set DECL_ARG_TYPE
226 appropriately. */
228 tree
229 cp_build_parm_decl (tree fn, tree name, tree type)
231 tree parm = build_decl (input_location,
232 PARM_DECL, name, type);
233 DECL_CONTEXT (parm) = fn;
235 /* DECL_ARG_TYPE is only used by the back end and the back end never
236 sees templates. */
237 if (!processing_template_decl)
238 DECL_ARG_TYPE (parm) = type_passed_as (type);
240 return parm;
243 /* Returns a PARM_DECL of FN for a parameter of the indicated TYPE, with the
244 indicated NAME. */
246 tree
247 build_artificial_parm (tree fn, tree name, tree type)
249 tree parm = cp_build_parm_decl (fn, name, type);
250 DECL_ARTIFICIAL (parm) = 1;
251 /* All our artificial parms are implicitly `const'; they cannot be
252 assigned to. */
253 TREE_READONLY (parm) = 1;
254 return parm;
257 /* Constructors for types with virtual baseclasses need an "in-charge" flag
258 saying whether this constructor is responsible for initialization of
259 virtual baseclasses or not. All destructors also need this "in-charge"
260 flag, which additionally determines whether or not the destructor should
261 free the memory for the object.
263 This function adds the "in-charge" flag to member function FN if
264 appropriate. It is called from grokclassfn and tsubst.
265 FN must be either a constructor or destructor.
267 The in-charge flag follows the 'this' parameter, and is followed by the
268 VTT parm (if any), then the user-written parms. */
270 void
271 maybe_retrofit_in_chrg (tree fn)
273 tree basetype, arg_types, parms, parm, fntype;
275 /* If we've already add the in-charge parameter don't do it again. */
276 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
277 return;
279 /* When processing templates we can't know, in general, whether or
280 not we're going to have virtual baseclasses. */
281 if (processing_template_decl)
282 return;
284 /* We don't need an in-charge parameter for constructors that don't
285 have virtual bases. */
286 if (DECL_CONSTRUCTOR_P (fn)
287 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
288 return;
290 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
291 basetype = TREE_TYPE (TREE_VALUE (arg_types));
292 arg_types = TREE_CHAIN (arg_types);
294 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
296 /* If this is a subobject constructor or destructor, our caller will
297 pass us a pointer to our VTT. */
298 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
300 parm = build_artificial_parm (fn, vtt_parm_identifier, vtt_parm_type);
302 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
303 DECL_CHAIN (parm) = parms;
304 parms = parm;
306 /* ...and then to TYPE_ARG_TYPES. */
307 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
309 DECL_HAS_VTT_PARM_P (fn) = 1;
312 /* Then add the in-charge parm (before the VTT parm). */
313 parm = build_artificial_parm (fn, in_charge_identifier, integer_type_node);
314 DECL_CHAIN (parm) = parms;
315 parms = parm;
316 arg_types = hash_tree_chain (integer_type_node, arg_types);
318 /* Insert our new parameter(s) into the list. */
319 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
321 /* And rebuild the function type. */
322 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
323 arg_types);
324 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
325 fntype = build_exception_variant (fntype,
326 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
327 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
328 fntype = (cp_build_type_attribute_variant
329 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
330 TREE_TYPE (fn) = fntype;
332 /* Now we've got the in-charge parameter. */
333 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
336 /* Classes overload their constituent function names automatically.
337 When a function name is declared in a record structure,
338 its name is changed to it overloaded name. Since names for
339 constructors and destructors can conflict, we place a leading
340 '$' for destructors.
342 CNAME is the name of the class we are grokking for.
344 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
346 FLAGS contains bits saying what's special about today's
347 arguments. DTOR_FLAG == DESTRUCTOR.
349 If FUNCTION is a destructor, then we must add the `auto-delete' field
350 as a second parameter. There is some hair associated with the fact
351 that we must "declare" this variable in the manner consistent with the
352 way the rest of the arguments were declared.
354 QUALS are the qualifiers for the this pointer. */
356 void
357 grokclassfn (tree ctype, tree function, enum overload_flags flags)
359 tree fn_name = DECL_NAME (function);
361 /* Even within an `extern "C"' block, members get C++ linkage. See
362 [dcl.link] for details. */
363 SET_DECL_LANGUAGE (function, lang_cplusplus);
365 if (fn_name == NULL_TREE)
367 error ("name missing for member function");
368 fn_name = get_identifier ("<anonymous>");
369 DECL_NAME (function) = fn_name;
372 DECL_CONTEXT (function) = ctype;
374 if (flags == DTOR_FLAG)
375 DECL_CXX_DESTRUCTOR_P (function) = 1;
377 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
378 maybe_retrofit_in_chrg (function);
381 /* Create an ARRAY_REF, checking for the user doing things backwards
382 along the way. DECLTYPE_P is for N3276, as in the parser. */
384 tree
385 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
386 bool decltype_p)
388 tree type;
389 tree expr;
390 tree orig_array_expr = array_expr;
391 tree orig_index_exp = index_exp;
392 tree overload = NULL_TREE;
394 if (error_operand_p (array_expr) || error_operand_p (index_exp))
395 return error_mark_node;
397 if (processing_template_decl)
399 if (type_dependent_expression_p (array_expr)
400 || type_dependent_expression_p (index_exp))
401 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
402 NULL_TREE, NULL_TREE);
403 array_expr = build_non_dependent_expr (array_expr);
404 index_exp = build_non_dependent_expr (index_exp);
407 type = TREE_TYPE (array_expr);
408 gcc_assert (type);
409 type = non_reference (type);
411 /* If they have an `operator[]', use that. */
412 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
414 tsubst_flags_t complain = tf_warning_or_error;
415 if (decltype_p)
416 complain |= tf_decltype;
417 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
418 index_exp, NULL_TREE, &overload, complain);
420 else
422 tree p1, p2, i1, i2;
424 /* Otherwise, create an ARRAY_REF for a pointer or array type.
425 It is a little-known fact that, if `a' is an array and `i' is
426 an int, you can write `i[a]', which means the same thing as
427 `a[i]'. */
428 if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
429 p1 = array_expr;
430 else
431 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
433 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
434 p2 = index_exp;
435 else
436 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
438 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
439 false);
440 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
441 false);
443 if ((p1 && i2) && (i1 && p2))
444 error ("ambiguous conversion for array subscript");
446 if (p1 && i2)
447 array_expr = p1, index_exp = i2;
448 else if (i1 && p2)
449 array_expr = p2, index_exp = i1;
450 else
452 error ("invalid types %<%T[%T]%> for array subscript",
453 type, TREE_TYPE (index_exp));
454 return error_mark_node;
457 if (array_expr == error_mark_node || index_exp == error_mark_node)
458 error ("ambiguous conversion for array subscript");
460 if (TREE_CODE (TREE_TYPE (array_expr)) == POINTER_TYPE)
461 array_expr = mark_rvalue_use (array_expr);
462 else
463 array_expr = mark_lvalue_use_nonread (array_expr);
464 index_exp = mark_rvalue_use (index_exp);
465 expr = build_array_ref (input_location, array_expr, index_exp);
467 if (processing_template_decl && expr != error_mark_node)
469 if (overload != NULL_TREE)
470 return (build_min_non_dep_op_overload
471 (ARRAY_REF, expr, overload, orig_array_expr, orig_index_exp));
473 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
474 NULL_TREE, NULL_TREE);
476 return expr;
479 /* Given the cast expression EXP, checking out its validity. Either return
480 an error_mark_node if there was an unavoidable error, return a cast to
481 void for trying to delete a pointer w/ the value 0, or return the
482 call to delete. If DOING_VEC is true, we handle things differently
483 for doing an array delete.
484 Implements ARM $5.3.4. This is called from the parser. */
486 tree
487 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
488 tsubst_flags_t complain)
490 tree t, type;
492 if (exp == error_mark_node)
493 return exp;
495 if (processing_template_decl)
497 t = build_min (DELETE_EXPR, void_type_node, exp, size);
498 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
499 DELETE_EXPR_USE_VEC (t) = doing_vec;
500 TREE_SIDE_EFFECTS (t) = 1;
501 return t;
504 /* An array can't have been allocated by new, so complain. */
505 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
506 warning (0, "deleting array %q#E", exp);
508 t = build_expr_type_conversion (WANT_POINTER, exp, true);
510 if (t == NULL_TREE || t == error_mark_node)
512 error ("type %q#T argument given to %<delete%>, expected pointer",
513 TREE_TYPE (exp));
514 return error_mark_node;
517 type = TREE_TYPE (t);
519 /* As of Valley Forge, you can delete a pointer to const. */
521 /* You can't delete functions. */
522 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
524 error ("cannot delete a function. Only pointer-to-objects are "
525 "valid arguments to %<delete%>");
526 return error_mark_node;
529 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
530 if (VOID_TYPE_P (TREE_TYPE (type)))
532 warning (OPT_Wdelete_incomplete, "deleting %qT is undefined", type);
533 doing_vec = 0;
536 /* Deleting a pointer with the value zero is valid and has no effect. */
537 if (integer_zerop (t))
538 return build1 (NOP_EXPR, void_type_node, t);
540 if (doing_vec)
541 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
542 sfk_deleting_destructor,
543 use_global_delete, complain);
544 else
545 return build_delete (type, t, sfk_deleting_destructor,
546 LOOKUP_NORMAL, use_global_delete,
547 complain);
550 /* Report an error if the indicated template declaration is not the
551 sort of thing that should be a member template. */
553 void
554 check_member_template (tree tmpl)
556 tree decl;
558 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
559 decl = DECL_TEMPLATE_RESULT (tmpl);
561 if (TREE_CODE (decl) == FUNCTION_DECL
562 || DECL_ALIAS_TEMPLATE_P (tmpl)
563 || (TREE_CODE (decl) == TYPE_DECL
564 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
566 /* The parser rejects template declarations in local classes
567 (with the exception of generic lambdas). */
568 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
569 /* The parser rejects any use of virtual in a function template. */
570 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
571 && DECL_VIRTUAL_P (decl)));
573 /* The debug-information generating code doesn't know what to do
574 with member templates. */
575 DECL_IGNORED_P (tmpl) = 1;
577 else if (variable_template_p (tmpl))
578 /* OK */;
579 else
580 error ("template declaration of %q#D", decl);
583 /* Sanity check: report error if this function FUNCTION is not
584 really a member of the class (CTYPE) it is supposed to belong to.
585 TEMPLATE_PARMS is used to specify the template parameters of a member
586 template passed as FUNCTION_DECL. If the member template is passed as a
587 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
588 from the declaration. If the function is not a function template, it
589 must be NULL.
590 It returns the original declaration for the function, NULL_TREE if
591 no declaration was found, error_mark_node if an error was emitted. */
593 tree
594 check_classfn (tree ctype, tree function, tree template_parms)
596 if (DECL_USE_TEMPLATE (function)
597 && !(TREE_CODE (function) == TEMPLATE_DECL
598 && DECL_TEMPLATE_SPECIALIZATION (function))
599 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
600 /* Since this is a specialization of a member template,
601 we're not going to find the declaration in the class.
602 For example, in:
604 struct S { template <typename T> void f(T); };
605 template <> void S::f(int);
607 we're not going to find `S::f(int)', but there's no
608 reason we should, either. We let our callers know we didn't
609 find the method, but we don't complain. */
610 return NULL_TREE;
612 /* Basic sanity check: for a template function, the template parameters
613 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
614 if (TREE_CODE (function) == TEMPLATE_DECL)
616 if (template_parms
617 && !comp_template_parms (template_parms,
618 DECL_TEMPLATE_PARMS (function)))
620 error ("template parameter lists provided don%'t match the "
621 "template parameters of %qD", function);
622 return error_mark_node;
624 template_parms = DECL_TEMPLATE_PARMS (function);
627 /* OK, is this a definition of a member template? */
628 bool is_template = (template_parms != NULL_TREE);
630 /* [temp.mem]
632 A destructor shall not be a member template. */
633 if (DECL_DESTRUCTOR_P (function) && is_template)
635 error ("destructor %qD declared as member template", function);
636 return error_mark_node;
639 /* We must enter the scope here, because conversion operators are
640 named by target type, and type equivalence relies on typenames
641 resolving within the scope of CTYPE. */
642 tree pushed_scope = push_scope (ctype);
643 tree matched = NULL_TREE;
644 tree fns = get_class_binding (ctype, DECL_NAME (function));
646 for (ovl_iterator iter (fns); !matched && iter; ++iter)
648 tree fndecl = *iter;
650 /* A member template definition only matches a member template
651 declaration. */
652 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
653 continue;
655 if (!DECL_DECLARES_FUNCTION_P (fndecl))
656 continue;
658 tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
659 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
661 /* We cannot simply call decls_match because this doesn't work
662 for static member functions that are pretending to be
663 methods, and because the name may have been changed by
664 asm("new_name"). */
666 /* Get rid of the this parameter on functions that become
667 static. */
668 if (DECL_STATIC_FUNCTION_P (fndecl)
669 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
670 p1 = TREE_CHAIN (p1);
672 /* ref-qualifier or absence of same must match. */
673 if (type_memfn_rqual (TREE_TYPE (function))
674 != type_memfn_rqual (TREE_TYPE (fndecl)))
675 continue;
677 // Include constraints in the match.
678 tree c1 = get_constraints (function);
679 tree c2 = get_constraints (fndecl);
681 /* While finding a match, same types and params are not enough
682 if the function is versioned. Also check version ("target")
683 attributes. */
684 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
685 TREE_TYPE (TREE_TYPE (fndecl)))
686 && compparms (p1, p2)
687 && !targetm.target_option.function_versions (function, fndecl)
688 && (!is_template
689 || comp_template_parms (template_parms,
690 DECL_TEMPLATE_PARMS (fndecl)))
691 && equivalent_constraints (c1, c2)
692 && (DECL_TEMPLATE_SPECIALIZATION (function)
693 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
694 && (!DECL_TEMPLATE_SPECIALIZATION (function)
695 || (DECL_TI_TEMPLATE (function) == DECL_TI_TEMPLATE (fndecl))))
696 matched = fndecl;
699 if (!matched)
701 if (!COMPLETE_TYPE_P (ctype))
702 cxx_incomplete_type_error (function, ctype);
703 else
705 if (DECL_CONV_FN_P (function))
706 fns = get_class_binding (ctype, conv_op_identifier);
708 error_at (DECL_SOURCE_LOCATION (function),
709 "no declaration matches %q#D", function);
710 if (fns)
711 print_candidates (fns);
712 else if (DECL_CONV_FN_P (function))
713 inform (DECL_SOURCE_LOCATION (function),
714 "no conversion operators declared");
715 else
716 inform (DECL_SOURCE_LOCATION (function),
717 "no functions named %qD", function);
718 inform (DECL_SOURCE_LOCATION (TYPE_NAME (ctype)),
719 "%#qT defined here", ctype);
721 matched = error_mark_node;
724 if (pushed_scope)
725 pop_scope (pushed_scope);
727 return matched;
730 /* DECL is a function with vague linkage. Remember it so that at the
731 end of the translation unit we can decide whether or not to emit
732 it. */
734 void
735 note_vague_linkage_fn (tree decl)
737 DECL_DEFER_OUTPUT (decl) = 1;
738 vec_safe_push (deferred_fns, decl);
741 /* As above, but for variable template instantiations. */
743 void
744 note_variable_template_instantiation (tree decl)
746 vec_safe_push (pending_statics, decl);
749 /* We have just processed the DECL, which is a static data member.
750 The other parameters are as for cp_finish_decl. */
752 void
753 finish_static_data_member_decl (tree decl,
754 tree init, bool init_const_expr_p,
755 tree asmspec_tree,
756 int flags)
758 DECL_CONTEXT (decl) = current_class_type;
760 /* We cannot call pushdecl here, because that would fill in the
761 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
762 the right thing, namely, to put this decl out straight away. */
764 if (! processing_template_decl)
765 vec_safe_push (pending_statics, decl);
767 if (LOCAL_CLASS_P (current_class_type)
768 /* We already complained about the template definition. */
769 && !DECL_TEMPLATE_INSTANTIATION (decl))
770 permerror (input_location, "local class %q#T shall not have static data member %q#D",
771 current_class_type, decl);
772 else
773 for (tree t = current_class_type; TYPE_P (t);
774 t = CP_TYPE_CONTEXT (t))
775 if (TYPE_UNNAMED_P (t))
777 if (permerror (DECL_SOURCE_LOCATION (decl),
778 "static data member %qD in unnamed class", decl))
779 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
780 "unnamed class defined here");
781 break;
784 DECL_IN_AGGR_P (decl) = 1;
786 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
787 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
788 SET_VAR_HAD_UNKNOWN_BOUND (decl);
790 if (init)
792 /* Similarly to start_decl_1, we want to complete the type in order
793 to do the right thing in cp_apply_type_quals_to_decl, possibly
794 clear TYPE_QUAL_CONST (c++/65579). */
795 tree type = TREE_TYPE (decl) = complete_type (TREE_TYPE (decl));
796 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
799 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
802 /* DECLARATOR and DECLSPECS correspond to a class member. The other
803 parameters are as for cp_finish_decl. Return the DECL for the
804 class member declared. */
806 tree
807 grokfield (const cp_declarator *declarator,
808 cp_decl_specifier_seq *declspecs,
809 tree init, bool init_const_expr_p,
810 tree asmspec_tree,
811 tree attrlist)
813 tree value;
814 const char *asmspec = 0;
815 int flags;
816 tree name;
818 if (init
819 && TREE_CODE (init) == TREE_LIST
820 && TREE_VALUE (init) == error_mark_node
821 && TREE_CHAIN (init) == NULL_TREE)
822 init = NULL_TREE;
824 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
825 if (! value || value == error_mark_node)
826 /* friend or constructor went bad. */
827 return error_mark_node;
828 if (TREE_TYPE (value) == error_mark_node)
829 return value;
831 if (TREE_CODE (value) == TYPE_DECL && init)
833 error ("typedef %qD is initialized (use decltype instead)", value);
834 init = NULL_TREE;
837 /* Pass friendly classes back. */
838 if (value == void_type_node)
839 return value;
842 name = DECL_NAME (value);
844 if (name != NULL_TREE)
846 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
848 error ("explicit template argument list not allowed");
849 return error_mark_node;
852 if (IDENTIFIER_POINTER (name)[0] == '_'
853 && id_equal (name, "_vptr"))
854 error ("member %qD conflicts with virtual function table field name",
855 value);
858 /* Stash away type declarations. */
859 if (TREE_CODE (value) == TYPE_DECL)
861 DECL_NONLOCAL (value) = 1;
862 DECL_CONTEXT (value) = current_class_type;
864 if (attrlist)
866 int attrflags = 0;
868 /* If this is a typedef that names the class for linkage purposes
869 (7.1.3p8), apply any attributes directly to the type. */
870 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
871 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
872 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
874 cplus_decl_attributes (&value, attrlist, attrflags);
877 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
878 && TREE_TYPE (value) != error_mark_node
879 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
880 set_underlying_type (value);
882 /* It's important that push_template_decl below follows
883 set_underlying_type above so that the created template
884 carries the properly set type of VALUE. */
885 if (processing_template_decl)
886 value = push_template_decl (value);
888 record_locally_defined_typedef (value);
889 return value;
892 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
894 if (!friendp && DECL_IN_AGGR_P (value))
896 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
897 return void_type_node;
900 if (asmspec_tree && asmspec_tree != error_mark_node)
901 asmspec = TREE_STRING_POINTER (asmspec_tree);
903 if (init)
905 if (TREE_CODE (value) == FUNCTION_DECL)
907 if (init == ridpointers[(int)RID_DELETE])
909 DECL_DELETED_FN (value) = 1;
910 DECL_DECLARED_INLINE_P (value) = 1;
911 DECL_INITIAL (value) = error_mark_node;
913 else if (init == ridpointers[(int)RID_DEFAULT])
915 if (defaultable_fn_check (value))
917 DECL_DEFAULTED_FN (value) = 1;
918 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
919 DECL_DECLARED_INLINE_P (value) = 1;
922 else if (TREE_CODE (init) == DEFAULT_ARG)
923 error ("invalid initializer for member function %qD", value);
924 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
926 if (integer_zerop (init))
927 DECL_PURE_VIRTUAL_P (value) = 1;
928 else if (error_operand_p (init))
929 ; /* An error has already been reported. */
930 else
931 error ("invalid initializer for member function %qD",
932 value);
934 else
936 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
937 if (friendp)
938 error ("initializer specified for friend function %qD",
939 value);
940 else
941 error ("initializer specified for static member function %qD",
942 value);
945 else if (TREE_CODE (value) == FIELD_DECL)
946 /* C++11 NSDMI, keep going. */;
947 else if (!VAR_P (value))
948 gcc_unreachable ();
951 /* Pass friend decls back. */
952 if ((TREE_CODE (value) == FUNCTION_DECL
953 || TREE_CODE (value) == TEMPLATE_DECL)
954 && DECL_CONTEXT (value) != current_class_type)
955 return value;
957 /* Need to set this before push_template_decl. */
958 if (VAR_P (value))
959 DECL_CONTEXT (value) = current_class_type;
961 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
963 value = push_template_decl (value);
964 if (error_operand_p (value))
965 return error_mark_node;
968 if (attrlist)
969 cplus_decl_attributes (&value, attrlist, 0);
971 if (init && DIRECT_LIST_INIT_P (init))
972 flags = LOOKUP_NORMAL;
973 else
974 flags = LOOKUP_IMPLICIT;
976 switch (TREE_CODE (value))
978 case VAR_DECL:
979 finish_static_data_member_decl (value, init, init_const_expr_p,
980 asmspec_tree, flags);
981 return value;
983 case FIELD_DECL:
984 if (asmspec)
985 error ("%<asm%> specifiers are not permitted on non-static data members");
986 if (DECL_INITIAL (value) == error_mark_node)
987 init = error_mark_node;
988 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
989 NULL_TREE, flags);
990 DECL_IN_AGGR_P (value) = 1;
991 return value;
993 case FUNCTION_DECL:
994 if (asmspec)
995 set_user_assembler_name (value, asmspec);
997 cp_finish_decl (value,
998 /*init=*/NULL_TREE,
999 /*init_const_expr_p=*/false,
1000 asmspec_tree, flags);
1002 /* Pass friends back this way. */
1003 if (DECL_FRIEND_P (value))
1004 return void_type_node;
1006 DECL_IN_AGGR_P (value) = 1;
1007 return value;
1009 default:
1010 gcc_unreachable ();
1012 return NULL_TREE;
1015 /* Like `grokfield', but for bitfields.
1016 WIDTH is the width of the bitfield, a constant expression.
1017 The other parameters are as for grokfield. */
1019 tree
1020 grokbitfield (const cp_declarator *declarator,
1021 cp_decl_specifier_seq *declspecs, tree width, tree init,
1022 tree attrlist)
1024 tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1025 init != NULL_TREE, &attrlist);
1027 if (value == error_mark_node)
1028 return NULL_TREE; /* friends went bad. */
1029 if (TREE_TYPE (value) == error_mark_node)
1030 return value;
1032 /* Pass friendly classes back. */
1033 if (VOID_TYPE_P (value))
1034 return void_type_node;
1036 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
1037 && (POINTER_TYPE_P (value)
1038 || !dependent_type_p (TREE_TYPE (value))))
1040 error ("bit-field %qD with non-integral type", value);
1041 return error_mark_node;
1044 if (TREE_CODE (value) == TYPE_DECL)
1046 error ("cannot declare %qD to be a bit-field type", value);
1047 return NULL_TREE;
1050 /* Usually, finish_struct_1 catches bitfields with invalid types.
1051 But, in the case of bitfields with function type, we confuse
1052 ourselves into thinking they are member functions, so we must
1053 check here. */
1054 if (TREE_CODE (value) == FUNCTION_DECL)
1056 error ("cannot declare bit-field %qD with function type",
1057 DECL_NAME (value));
1058 return NULL_TREE;
1061 if (width && TYPE_WARN_IF_NOT_ALIGN (TREE_TYPE (value)))
1063 error ("cannot declare bit-field %qD with %<warn_if_not_aligned%> type",
1064 DECL_NAME (value));
1065 return NULL_TREE;
1068 if (DECL_IN_AGGR_P (value))
1070 error ("%qD is already defined in the class %qT", value,
1071 DECL_CONTEXT (value));
1072 return void_type_node;
1075 if (TREE_STATIC (value))
1077 error ("static member %qD cannot be a bit-field", value);
1078 return NULL_TREE;
1081 int flags = LOOKUP_IMPLICIT;
1082 if (init && DIRECT_LIST_INIT_P (init))
1083 flags = LOOKUP_NORMAL;
1084 cp_finish_decl (value, init, false, NULL_TREE, flags);
1086 if (width != error_mark_node)
1088 /* The width must be an integer type. */
1089 if (!type_dependent_expression_p (width)
1090 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1091 error ("width of bit-field %qD has non-integral type %qT", value,
1092 TREE_TYPE (width));
1093 else
1095 /* Temporarily stash the width in DECL_BIT_FIELD_REPRESENTATIVE.
1096 check_bitfield_decl picks it from there later and sets DECL_SIZE
1097 accordingly. */
1098 DECL_BIT_FIELD_REPRESENTATIVE (value) = width;
1099 SET_DECL_C_BIT_FIELD (value);
1103 DECL_IN_AGGR_P (value) = 1;
1105 if (attrlist)
1106 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1108 return value;
1112 /* Returns true iff ATTR is an attribute which needs to be applied at
1113 instantiation time rather than template definition time. */
1115 static bool
1116 is_late_template_attribute (tree attr, tree decl)
1118 tree name = get_attribute_name (attr);
1119 tree args = TREE_VALUE (attr);
1120 const struct attribute_spec *spec = lookup_attribute_spec (name);
1121 tree arg;
1123 if (!spec)
1124 /* Unknown attribute. */
1125 return false;
1127 /* Attribute weak handling wants to write out assembly right away. */
1128 if (is_attribute_p ("weak", name))
1129 return true;
1131 /* Attributes used and unused are applied directly, as they appertain to
1132 decls. */
1133 if (is_attribute_p ("unused", name)
1134 || is_attribute_p ("used", name))
1135 return false;
1137 /* Attribute tls_model wants to modify the symtab. */
1138 if (is_attribute_p ("tls_model", name))
1139 return true;
1141 /* #pragma omp declare simd attribute needs to be always deferred. */
1142 if (flag_openmp
1143 && is_attribute_p ("omp declare simd", name))
1144 return true;
1146 /* An attribute pack is clearly dependent. */
1147 if (args && PACK_EXPANSION_P (args))
1148 return true;
1150 /* If any of the arguments are dependent expressions, we can't evaluate
1151 the attribute until instantiation time. */
1152 for (arg = args; arg; arg = TREE_CHAIN (arg))
1154 tree t = TREE_VALUE (arg);
1156 /* If the first attribute argument is an identifier, only consider
1157 second and following arguments. Attributes like mode, format,
1158 cleanup and several target specific attributes aren't late
1159 just because they have an IDENTIFIER_NODE as first argument. */
1160 if (arg == args && attribute_takes_identifier_p (name)
1161 && identifier_p (t))
1162 continue;
1164 if (value_dependent_expression_p (t)
1165 || type_dependent_expression_p (t))
1166 return true;
1169 if (TREE_CODE (decl) == TYPE_DECL
1170 || TYPE_P (decl)
1171 || spec->type_required)
1173 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1175 /* We can't apply any attributes to a completely unknown type until
1176 instantiation time. */
1177 enum tree_code code = TREE_CODE (type);
1178 if (code == TEMPLATE_TYPE_PARM
1179 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1180 || code == TYPENAME_TYPE)
1181 return true;
1182 /* Also defer most attributes on dependent types. This is not
1183 necessary in all cases, but is the better default. */
1184 else if (dependent_type_p (type)
1185 /* But some attributes specifically apply to templates. */
1186 && !is_attribute_p ("abi_tag", name)
1187 && !is_attribute_p ("deprecated", name)
1188 && !is_attribute_p ("visibility", name))
1189 return true;
1190 else
1191 return false;
1193 else
1194 return false;
1197 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1198 applied at instantiation time and return them. If IS_DEPENDENT is true,
1199 the declaration itself is dependent, so all attributes should be applied
1200 at instantiation time. */
1202 static tree
1203 splice_template_attributes (tree *attr_p, tree decl)
1205 tree *p = attr_p;
1206 tree late_attrs = NULL_TREE;
1207 tree *q = &late_attrs;
1209 if (!p)
1210 return NULL_TREE;
1212 for (; *p; )
1214 if (is_late_template_attribute (*p, decl))
1216 ATTR_IS_DEPENDENT (*p) = 1;
1217 *q = *p;
1218 *p = TREE_CHAIN (*p);
1219 q = &TREE_CHAIN (*q);
1220 *q = NULL_TREE;
1222 else
1223 p = &TREE_CHAIN (*p);
1226 return late_attrs;
1229 /* Remove any late attributes from the list in ATTR_P and attach them to
1230 DECL_P. */
1232 static void
1233 save_template_attributes (tree *attr_p, tree *decl_p)
1235 tree *q;
1237 if (attr_p && *attr_p == error_mark_node)
1238 return;
1240 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1241 if (!late_attrs)
1242 return;
1244 if (DECL_P (*decl_p))
1245 q = &DECL_ATTRIBUTES (*decl_p);
1246 else
1247 q = &TYPE_ATTRIBUTES (*decl_p);
1249 tree old_attrs = *q;
1251 /* Merge the late attributes at the beginning with the attribute
1252 list. */
1253 late_attrs = merge_attributes (late_attrs, *q);
1254 *q = late_attrs;
1256 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1258 /* We've added new attributes directly to the main variant, so
1259 now we need to update all of the other variants to include
1260 these new attributes. */
1261 tree variant;
1262 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1263 variant = TYPE_NEXT_VARIANT (variant))
1265 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1266 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1271 /* True if ATTRS contains any dependent attributes that affect type
1272 identity. */
1274 bool
1275 any_dependent_type_attributes_p (tree attrs)
1277 for (tree a = attrs; a; a = TREE_CHAIN (a))
1278 if (ATTR_IS_DEPENDENT (a))
1280 const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (a));
1281 if (as && as->affects_type_identity)
1282 return true;
1284 return false;
1287 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1288 to a typedef which gives a previously unnamed class or enum a name for
1289 linkage purposes. */
1291 bool
1292 attributes_naming_typedef_ok (tree attrs)
1294 for (; attrs; attrs = TREE_CHAIN (attrs))
1296 tree name = get_attribute_name (attrs);
1297 if (is_attribute_p ("vector_size", name))
1298 return false;
1300 return true;
1303 /* Like reconstruct_complex_type, but handle also template trees. */
1305 tree
1306 cp_reconstruct_complex_type (tree type, tree bottom)
1308 tree inner, outer;
1309 bool late_return_type_p = false;
1311 if (TYPE_PTR_P (type))
1313 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1314 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1315 TYPE_REF_CAN_ALIAS_ALL (type));
1317 else if (TREE_CODE (type) == REFERENCE_TYPE)
1319 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1320 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1321 TYPE_REF_CAN_ALIAS_ALL (type));
1323 else if (TREE_CODE (type) == ARRAY_TYPE)
1325 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1326 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1327 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1328 element type qualification will be handled by the recursive
1329 cp_reconstruct_complex_type call and cp_build_qualified_type
1330 for ARRAY_TYPEs changes the element type. */
1331 return outer;
1333 else if (TREE_CODE (type) == FUNCTION_TYPE)
1335 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1336 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1337 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1338 outer = apply_memfn_quals (outer,
1339 type_memfn_quals (type),
1340 type_memfn_rqual (type));
1342 else if (TREE_CODE (type) == METHOD_TYPE)
1344 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1345 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1346 /* The build_method_type_directly() routine prepends 'this' to argument list,
1347 so we must compensate by getting rid of it. */
1348 outer
1349 = build_method_type_directly
1350 (class_of_this_parm (type), inner,
1351 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1353 else if (TREE_CODE (type) == OFFSET_TYPE)
1355 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1356 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1358 else
1359 return bottom;
1361 if (TYPE_ATTRIBUTES (type))
1362 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1363 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1365 if (late_return_type_p)
1366 TYPE_HAS_LATE_RETURN_TYPE (outer) = 1;
1368 return outer;
1371 /* Replaces any constexpr expression that may be into the attributes
1372 arguments with their reduced value. */
1374 static void
1375 cp_check_const_attributes (tree attributes)
1377 if (attributes == error_mark_node)
1378 return;
1380 tree attr;
1381 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1383 tree arg;
1384 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1386 tree expr = TREE_VALUE (arg);
1387 if (EXPR_P (expr))
1388 TREE_VALUE (arg) = maybe_constant_value (expr);
1393 /* Return true if TYPE is an OpenMP mappable type. */
1394 bool
1395 cp_omp_mappable_type (tree type)
1397 /* Mappable type has to be complete. */
1398 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1399 return false;
1400 /* Arrays have mappable type if the elements have mappable type. */
1401 while (TREE_CODE (type) == ARRAY_TYPE)
1402 type = TREE_TYPE (type);
1403 /* A mappable type cannot contain virtual members. */
1404 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1405 return false;
1406 /* All data members must be non-static. */
1407 if (CLASS_TYPE_P (type))
1409 tree field;
1410 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1411 if (VAR_P (field))
1412 return false;
1413 /* All fields must have mappable types. */
1414 else if (TREE_CODE (field) == FIELD_DECL
1415 && !cp_omp_mappable_type (TREE_TYPE (field)))
1416 return false;
1418 return true;
1421 /* Like decl_attributes, but handle C++ complexity. */
1423 void
1424 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1426 if (*decl == NULL_TREE || *decl == void_type_node
1427 || *decl == error_mark_node)
1428 return;
1430 /* Add implicit "omp declare target" attribute if requested. */
1431 if (scope_chain->omp_declare_target_attribute
1432 && ((VAR_P (*decl)
1433 && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
1434 || TREE_CODE (*decl) == FUNCTION_DECL))
1436 if (VAR_P (*decl)
1437 && DECL_CLASS_SCOPE_P (*decl))
1438 error ("%q+D static data member inside of declare target directive",
1439 *decl);
1440 else if (!processing_template_decl
1441 && VAR_P (*decl)
1442 && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1443 error ("%q+D in declare target directive does not have mappable type",
1444 *decl);
1445 else
1446 attributes = tree_cons (get_identifier ("omp declare target"),
1447 NULL_TREE, attributes);
1450 if (processing_template_decl)
1452 if (check_for_bare_parameter_packs (attributes))
1453 return;
1455 save_template_attributes (&attributes, decl);
1458 cp_check_const_attributes (attributes);
1460 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1461 decl = &DECL_TEMPLATE_RESULT (*decl);
1463 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1465 attributes
1466 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1467 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1468 attributes, flags);
1470 else
1471 decl_attributes (decl, attributes, flags);
1473 if (TREE_CODE (*decl) == TYPE_DECL)
1474 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1476 /* Propagate deprecation out to the template. */
1477 if (TREE_DEPRECATED (*decl))
1478 if (tree ti = get_template_info (*decl))
1480 tree tmpl = TI_TEMPLATE (ti);
1481 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1482 : DECL_TEMPLATE_RESULT (tmpl));
1483 if (*decl == pattern)
1484 TREE_DEPRECATED (tmpl) = true;
1488 /* Walks through the namespace- or function-scope anonymous union
1489 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1490 Returns one of the fields for use in the mangled name. */
1492 static tree
1493 build_anon_union_vars (tree type, tree object)
1495 tree main_decl = NULL_TREE;
1496 tree field;
1498 /* Rather than write the code to handle the non-union case,
1499 just give an error. */
1500 if (TREE_CODE (type) != UNION_TYPE)
1502 error ("anonymous struct not inside named type");
1503 return error_mark_node;
1506 for (field = TYPE_FIELDS (type);
1507 field != NULL_TREE;
1508 field = DECL_CHAIN (field))
1510 tree decl;
1511 tree ref;
1513 if (DECL_ARTIFICIAL (field))
1514 continue;
1515 if (TREE_CODE (field) != FIELD_DECL)
1517 permerror (DECL_SOURCE_LOCATION (field),
1518 "%q#D invalid; an anonymous union can only "
1519 "have non-static data members", field);
1520 continue;
1523 if (TREE_PRIVATE (field))
1524 permerror (DECL_SOURCE_LOCATION (field),
1525 "private member %q#D in anonymous union", field);
1526 else if (TREE_PROTECTED (field))
1527 permerror (DECL_SOURCE_LOCATION (field),
1528 "protected member %q#D in anonymous union", field);
1530 if (processing_template_decl)
1531 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1532 DECL_NAME (field), NULL_TREE);
1533 else
1534 ref = build_class_member_access_expr (object, field, NULL_TREE,
1535 false, tf_warning_or_error);
1537 if (DECL_NAME (field))
1539 tree base;
1541 decl = build_decl (input_location,
1542 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1543 DECL_ANON_UNION_VAR_P (decl) = 1;
1544 DECL_ARTIFICIAL (decl) = 1;
1546 base = get_base_address (object);
1547 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1548 TREE_STATIC (decl) = TREE_STATIC (base);
1549 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1551 SET_DECL_VALUE_EXPR (decl, ref);
1552 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1554 decl = pushdecl (decl);
1556 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1557 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1558 else
1559 decl = 0;
1561 if (main_decl == NULL_TREE)
1562 main_decl = decl;
1565 return main_decl;
1568 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1569 anonymous union, then all members must be laid out together. PUBLIC_P
1570 is nonzero if this union is not declared static. */
1572 void
1573 finish_anon_union (tree anon_union_decl)
1575 tree type;
1576 tree main_decl;
1577 bool public_p;
1579 if (anon_union_decl == error_mark_node)
1580 return;
1582 type = TREE_TYPE (anon_union_decl);
1583 public_p = TREE_PUBLIC (anon_union_decl);
1585 /* The VAR_DECL's context is the same as the TYPE's context. */
1586 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1588 if (TYPE_FIELDS (type) == NULL_TREE)
1589 return;
1591 if (public_p)
1593 error ("namespace-scope anonymous aggregates must be static");
1594 return;
1597 main_decl = build_anon_union_vars (type, anon_union_decl);
1598 if (main_decl == error_mark_node)
1599 return;
1600 if (main_decl == NULL_TREE)
1602 pedwarn (input_location, 0, "anonymous union with no members");
1603 return;
1606 if (!processing_template_decl)
1608 /* Use main_decl to set the mangled name. */
1609 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1610 maybe_commonize_var (anon_union_decl);
1611 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1612 mangle_decl (anon_union_decl);
1613 DECL_NAME (anon_union_decl) = NULL_TREE;
1616 pushdecl (anon_union_decl);
1617 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1620 /* Auxiliary functions to make type signatures for
1621 `operator new' and `operator delete' correspond to
1622 what compiler will be expecting. */
1624 tree
1625 coerce_new_type (tree type)
1627 int e = 0;
1628 tree args = TYPE_ARG_TYPES (type);
1630 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1632 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1634 e = 1;
1635 error ("%<operator new%> must return type %qT", ptr_type_node);
1638 if (args && args != void_list_node)
1640 if (TREE_PURPOSE (args))
1642 /* [basic.stc.dynamic.allocation]
1644 The first parameter shall not have an associated default
1645 argument. */
1646 error ("the first parameter of %<operator new%> cannot "
1647 "have a default argument");
1648 /* Throw away the default argument. */
1649 TREE_PURPOSE (args) = NULL_TREE;
1652 if (!same_type_p (TREE_VALUE (args), size_type_node))
1654 e = 2;
1655 args = TREE_CHAIN (args);
1658 else
1659 e = 2;
1661 if (e == 2)
1662 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1663 "as first parameter", size_type_node);
1665 switch (e)
1667 case 2:
1668 args = tree_cons (NULL_TREE, size_type_node, args);
1669 /* Fall through. */
1670 case 1:
1671 type = build_exception_variant
1672 (build_function_type (ptr_type_node, args),
1673 TYPE_RAISES_EXCEPTIONS (type));
1674 /* Fall through. */
1675 default:;
1677 return type;
1680 tree
1681 coerce_delete_type (tree type)
1683 int e = 0;
1684 tree args = TYPE_ARG_TYPES (type);
1686 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1688 if (!same_type_p (TREE_TYPE (type), void_type_node))
1690 e = 1;
1691 error ("%<operator delete%> must return type %qT", void_type_node);
1694 if (!args || args == void_list_node
1695 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1697 e = 2;
1698 if (args && args != void_list_node)
1699 args = TREE_CHAIN (args);
1700 error ("%<operator delete%> takes type %qT as first parameter",
1701 ptr_type_node);
1703 switch (e)
1705 case 2:
1706 args = tree_cons (NULL_TREE, ptr_type_node, args);
1707 /* Fall through. */
1708 case 1:
1709 type = build_exception_variant
1710 (build_function_type (void_type_node, args),
1711 TYPE_RAISES_EXCEPTIONS (type));
1712 /* Fall through. */
1713 default:;
1716 return type;
1719 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1720 and mark them as needed. */
1722 static void
1723 mark_vtable_entries (tree decl)
1725 tree fnaddr;
1726 unsigned HOST_WIDE_INT idx;
1728 /* It's OK for the vtable to refer to deprecated virtual functions. */
1729 warning_sentinel w(warn_deprecated_decl);
1731 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1732 idx, fnaddr)
1734 tree fn;
1736 STRIP_NOPS (fnaddr);
1738 if (TREE_CODE (fnaddr) != ADDR_EXPR
1739 && TREE_CODE (fnaddr) != FDESC_EXPR)
1740 /* This entry is an offset: a virtual base class offset, a
1741 virtual call offset, an RTTI offset, etc. */
1742 continue;
1744 fn = TREE_OPERAND (fnaddr, 0);
1745 TREE_ADDRESSABLE (fn) = 1;
1746 /* When we don't have vcall offsets, we output thunks whenever
1747 we output the vtables that contain them. With vcall offsets,
1748 we know all the thunks we'll need when we emit a virtual
1749 function, so we emit the thunks there instead. */
1750 if (DECL_THUNK_P (fn))
1751 use_thunk (fn, /*emit_p=*/0);
1752 mark_used (fn);
1756 /* Set DECL up to have the closest approximation of "initialized common"
1757 linkage available. */
1759 void
1760 comdat_linkage (tree decl)
1762 if (flag_weak)
1763 make_decl_one_only (decl, cxx_comdat_group (decl));
1764 else if (TREE_CODE (decl) == FUNCTION_DECL
1765 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1766 /* We can just emit function and compiler-generated variables
1767 statically; having multiple copies is (for the most part) only
1768 a waste of space.
1770 There are two correctness issues, however: the address of a
1771 template instantiation with external linkage should be the
1772 same, independent of what translation unit asks for the
1773 address, and this will not hold when we emit multiple copies of
1774 the function. However, there's little else we can do.
1776 Also, by default, the typeinfo implementation assumes that
1777 there will be only one copy of the string used as the name for
1778 each type. Therefore, if weak symbols are unavailable, the
1779 run-time library should perform a more conservative check; it
1780 should perform a string comparison, rather than an address
1781 comparison. */
1782 TREE_PUBLIC (decl) = 0;
1783 else
1785 /* Static data member template instantiations, however, cannot
1786 have multiple copies. */
1787 if (DECL_INITIAL (decl) == 0
1788 || DECL_INITIAL (decl) == error_mark_node)
1789 DECL_COMMON (decl) = 1;
1790 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1792 DECL_COMMON (decl) = 1;
1793 DECL_INITIAL (decl) = error_mark_node;
1795 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1797 /* We can't do anything useful; leave vars for explicit
1798 instantiation. */
1799 DECL_EXTERNAL (decl) = 1;
1800 DECL_NOT_REALLY_EXTERN (decl) = 0;
1804 if (TREE_PUBLIC (decl))
1805 DECL_COMDAT (decl) = 1;
1808 /* For win32 we also want to put explicit instantiations in
1809 linkonce sections, so that they will be merged with implicit
1810 instantiations; otherwise we get duplicate symbol errors.
1811 For Darwin we do not want explicit instantiations to be
1812 linkonce. */
1814 void
1815 maybe_make_one_only (tree decl)
1817 /* We used to say that this was not necessary on targets that support weak
1818 symbols, because the implicit instantiations will defer to the explicit
1819 one. However, that's not actually the case in SVR4; a strong definition
1820 after a weak one is an error. Also, not making explicit
1821 instantiations one_only means that we can end up with two copies of
1822 some template instantiations. */
1823 if (! flag_weak)
1824 return;
1826 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1827 we can get away with not emitting them if they aren't used. We need
1828 to for variables so that cp_finish_decl will update their linkage,
1829 because their DECL_INITIAL may not have been set properly yet. */
1831 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1832 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1833 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1835 make_decl_one_only (decl, cxx_comdat_group (decl));
1837 if (VAR_P (decl))
1839 varpool_node *node = varpool_node::get_create (decl);
1840 DECL_COMDAT (decl) = 1;
1841 /* Mark it needed so we don't forget to emit it. */
1842 node->forced_by_abi = true;
1843 TREE_USED (decl) = 1;
1848 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1849 This predicate will give the right answer during parsing of the
1850 function, which other tests may not. */
1852 bool
1853 vague_linkage_p (tree decl)
1855 if (!TREE_PUBLIC (decl))
1857 /* maybe_thunk_body clears TREE_PUBLIC on the maybe-in-charge 'tor
1858 variants, check one of the "clones" for the real linkage. */
1859 if ((DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
1860 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl))
1861 && DECL_CHAIN (decl)
1862 && DECL_CLONED_FUNCTION (DECL_CHAIN (decl)))
1863 return vague_linkage_p (DECL_CHAIN (decl));
1865 gcc_checking_assert (!DECL_COMDAT (decl));
1866 return false;
1868 /* Unfortunately, import_export_decl has not always been called
1869 before the function is processed, so we cannot simply check
1870 DECL_COMDAT. */
1871 if (DECL_COMDAT (decl)
1872 || (TREE_CODE (decl) == FUNCTION_DECL
1873 && DECL_DECLARED_INLINE_P (decl))
1874 || (DECL_LANG_SPECIFIC (decl)
1875 && DECL_TEMPLATE_INSTANTIATION (decl))
1876 || (VAR_P (decl) && DECL_INLINE_VAR_P (decl)))
1877 return true;
1878 else if (DECL_FUNCTION_SCOPE_P (decl))
1879 /* A local static in an inline effectively has vague linkage. */
1880 return (TREE_STATIC (decl)
1881 && vague_linkage_p (DECL_CONTEXT (decl)));
1882 else
1883 return false;
1886 /* Determine whether or not we want to specifically import or export CTYPE,
1887 using various heuristics. */
1889 static void
1890 import_export_class (tree ctype)
1892 /* -1 for imported, 1 for exported. */
1893 int import_export = 0;
1895 /* It only makes sense to call this function at EOF. The reason is
1896 that this function looks at whether or not the first non-inline
1897 non-abstract virtual member function has been defined in this
1898 translation unit. But, we can't possibly know that until we've
1899 seen the entire translation unit. */
1900 gcc_assert (at_eof);
1902 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1903 return;
1905 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1906 we will have CLASSTYPE_INTERFACE_ONLY set but not
1907 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1908 heuristic because someone will supply a #pragma implementation
1909 elsewhere, and deducing it here would produce a conflict. */
1910 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1911 return;
1913 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1914 import_export = -1;
1915 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1916 import_export = 1;
1917 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1918 && !flag_implicit_templates)
1919 /* For a template class, without -fimplicit-templates, check the
1920 repository. If the virtual table is assigned to this
1921 translation unit, then export the class; otherwise, import
1922 it. */
1923 import_export = repo_export_class_p (ctype) ? 1 : -1;
1924 else if (TYPE_POLYMORPHIC_P (ctype))
1926 /* The ABI specifies that the virtual table and associated
1927 information are emitted with the key method, if any. */
1928 tree method = CLASSTYPE_KEY_METHOD (ctype);
1929 /* If weak symbol support is not available, then we must be
1930 careful not to emit the vtable when the key function is
1931 inline. An inline function can be defined in multiple
1932 translation units. If we were to emit the vtable in each
1933 translation unit containing a definition, we would get
1934 multiple definition errors at link-time. */
1935 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1936 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1939 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1940 a definition anywhere else. */
1941 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1942 import_export = 0;
1944 /* Allow back ends the chance to overrule the decision. */
1945 if (targetm.cxx.import_export_class)
1946 import_export = targetm.cxx.import_export_class (ctype, import_export);
1948 if (import_export)
1950 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1951 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1955 /* Return true if VAR has already been provided to the back end; in that
1956 case VAR should not be modified further by the front end. */
1957 static bool
1958 var_finalized_p (tree var)
1960 return varpool_node::get_create (var)->definition;
1963 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1964 must be emitted in this translation unit. Mark it as such. */
1966 void
1967 mark_needed (tree decl)
1969 TREE_USED (decl) = 1;
1970 if (TREE_CODE (decl) == FUNCTION_DECL)
1972 /* Extern inline functions don't become needed when referenced.
1973 If we know a method will be emitted in other TU and no new
1974 functions can be marked reachable, just use the external
1975 definition. */
1976 struct cgraph_node *node = cgraph_node::get_create (decl);
1977 node->forced_by_abi = true;
1979 /* #pragma interface and -frepo code can call mark_needed for
1980 maybe-in-charge 'tors; mark the clones as well. */
1981 tree clone;
1982 FOR_EACH_CLONE (clone, decl)
1983 mark_needed (clone);
1985 else if (VAR_P (decl))
1987 varpool_node *node = varpool_node::get_create (decl);
1988 /* C++ frontend use mark_decl_references to force COMDAT variables
1989 to be output that might appear dead otherwise. */
1990 node->forced_by_abi = true;
1994 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1995 returns true if a definition of this entity should be provided in
1996 this object file. Callers use this function to determine whether
1997 or not to let the back end know that a definition of DECL is
1998 available in this translation unit. */
2000 bool
2001 decl_needed_p (tree decl)
2003 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2004 /* This function should only be called at the end of the translation
2005 unit. We cannot be sure of whether or not something will be
2006 COMDAT until that point. */
2007 gcc_assert (at_eof);
2009 /* All entities with external linkage that are not COMDAT/EXTERN should be
2010 emitted; they may be referred to from other object files. */
2011 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
2012 return true;
2013 /* Functions marked "dllexport" must be emitted so that they are
2014 visible to other DLLs. */
2015 if (flag_keep_inline_dllexport
2016 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
2017 return true;
2019 /* When not optimizing, do not bother to produce definitions for extern
2020 symbols. */
2021 if (DECL_REALLY_EXTERN (decl)
2022 && ((TREE_CODE (decl) != FUNCTION_DECL
2023 && !optimize)
2024 || (TREE_CODE (decl) == FUNCTION_DECL
2025 && !opt_for_fn (decl, optimize)))
2026 && !lookup_attribute ("always_inline", decl))
2027 return false;
2029 /* If this entity was used, let the back end see it; it will decide
2030 whether or not to emit it into the object file. */
2031 if (TREE_USED (decl))
2032 return true;
2034 /* Virtual functions might be needed for devirtualization. */
2035 if (flag_devirtualize
2036 && TREE_CODE (decl) == FUNCTION_DECL
2037 && DECL_VIRTUAL_P (decl))
2038 return true;
2040 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2041 reference to DECL might cause it to be emitted later. */
2042 return false;
2045 /* If necessary, write out the vtables for the dynamic class CTYPE.
2046 Returns true if any vtables were emitted. */
2048 static bool
2049 maybe_emit_vtables (tree ctype)
2051 tree vtbl;
2052 tree primary_vtbl;
2053 int needed = 0;
2054 varpool_node *current = NULL, *last = NULL;
2056 /* If the vtables for this class have already been emitted there is
2057 nothing more to do. */
2058 primary_vtbl = CLASSTYPE_VTABLES (ctype);
2059 if (var_finalized_p (primary_vtbl))
2060 return false;
2061 /* Ignore dummy vtables made by get_vtable_decl. */
2062 if (TREE_TYPE (primary_vtbl) == void_type_node)
2063 return false;
2065 /* On some targets, we cannot determine the key method until the end
2066 of the translation unit -- which is when this function is
2067 called. */
2068 if (!targetm.cxx.key_method_may_be_inline ())
2069 determine_key_method (ctype);
2071 /* See if any of the vtables are needed. */
2072 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2074 import_export_decl (vtbl);
2075 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2076 needed = 1;
2078 if (!needed)
2080 /* If the references to this class' vtables are optimized away,
2081 still emit the appropriate debugging information. See
2082 dfs_debug_mark. */
2083 if (DECL_COMDAT (primary_vtbl)
2084 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2085 note_debug_info_needed (ctype);
2086 return false;
2089 /* The ABI requires that we emit all of the vtables if we emit any
2090 of them. */
2091 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2093 /* Mark entities references from the virtual table as used. */
2094 mark_vtable_entries (vtbl);
2096 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2098 vec<tree, va_gc> *cleanups = NULL;
2099 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2100 LOOKUP_NORMAL);
2102 /* It had better be all done at compile-time. */
2103 gcc_assert (!expr && !cleanups);
2106 /* Write it out. */
2107 DECL_EXTERNAL (vtbl) = 0;
2108 rest_of_decl_compilation (vtbl, 1, 1);
2110 /* Because we're only doing syntax-checking, we'll never end up
2111 actually marking the variable as written. */
2112 if (flag_syntax_only)
2113 TREE_ASM_WRITTEN (vtbl) = 1;
2114 else if (DECL_ONE_ONLY (vtbl))
2116 current = varpool_node::get_create (vtbl);
2117 if (last)
2118 current->add_to_same_comdat_group (last);
2119 last = current;
2123 /* Since we're writing out the vtable here, also write the debug
2124 info. */
2125 note_debug_info_needed (ctype);
2127 return true;
2130 /* A special return value from type_visibility meaning internal
2131 linkage. */
2133 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2135 /* walk_tree helper function for type_visibility. */
2137 static tree
2138 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2140 int *vis_p = (int *)data;
2141 if (! TYPE_P (*tp))
2143 *walk_subtrees = 0;
2145 else if (OVERLOAD_TYPE_P (*tp)
2146 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2148 *vis_p = VISIBILITY_ANON;
2149 return *tp;
2151 else if (CLASS_TYPE_P (*tp)
2152 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2153 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2154 return NULL;
2157 /* Returns the visibility of TYPE, which is the minimum visibility of its
2158 component types. */
2160 static int
2161 type_visibility (tree type)
2163 int vis = VISIBILITY_DEFAULT;
2164 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2165 return vis;
2168 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2169 specified (or if VISIBILITY is static). If TMPL is true, this
2170 constraint is for a template argument, and takes precedence
2171 over explicitly-specified visibility on the template. */
2173 static void
2174 constrain_visibility (tree decl, int visibility, bool tmpl)
2176 if (visibility == VISIBILITY_ANON)
2178 /* extern "C" declarations aren't affected by the anonymous
2179 namespace. */
2180 if (!DECL_EXTERN_C_P (decl))
2182 TREE_PUBLIC (decl) = 0;
2183 DECL_WEAK (decl) = 0;
2184 DECL_COMMON (decl) = 0;
2185 DECL_COMDAT (decl) = false;
2186 if (VAR_OR_FUNCTION_DECL_P (decl))
2188 struct symtab_node *snode = symtab_node::get (decl);
2190 if (snode)
2191 snode->set_comdat_group (NULL);
2193 DECL_INTERFACE_KNOWN (decl) = 1;
2194 if (DECL_LANG_SPECIFIC (decl))
2195 DECL_NOT_REALLY_EXTERN (decl) = 1;
2198 else if (visibility > DECL_VISIBILITY (decl)
2199 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2201 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2202 /* This visibility was not specified. */
2203 DECL_VISIBILITY_SPECIFIED (decl) = false;
2207 /* Constrain the visibility of DECL based on the visibility of its template
2208 arguments. */
2210 static void
2211 constrain_visibility_for_template (tree decl, tree targs)
2213 /* If this is a template instantiation, check the innermost
2214 template args for visibility constraints. The outer template
2215 args are covered by the class check. */
2216 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2217 int i;
2218 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2220 int vis = 0;
2222 tree arg = TREE_VEC_ELT (args, i-1);
2223 if (TYPE_P (arg))
2224 vis = type_visibility (arg);
2225 else
2227 if (REFERENCE_REF_P (arg))
2228 arg = TREE_OPERAND (arg, 0);
2229 if (TREE_TYPE (arg))
2230 STRIP_NOPS (arg);
2231 if (TREE_CODE (arg) == ADDR_EXPR)
2232 arg = TREE_OPERAND (arg, 0);
2233 if (VAR_OR_FUNCTION_DECL_P (arg))
2235 if (! TREE_PUBLIC (arg))
2236 vis = VISIBILITY_ANON;
2237 else
2238 vis = DECL_VISIBILITY (arg);
2241 if (vis)
2242 constrain_visibility (decl, vis, true);
2246 /* Like c_determine_visibility, but with additional C++-specific
2247 behavior.
2249 Function-scope entities can rely on the function's visibility because
2250 it is set in start_preparsed_function.
2252 Class-scope entities cannot rely on the class's visibility until the end
2253 of the enclosing class definition.
2255 Note that because namespaces have multiple independent definitions,
2256 namespace visibility is handled elsewhere using the #pragma visibility
2257 machinery rather than by decorating the namespace declaration.
2259 The goal is for constraints from the type to give a diagnostic, and
2260 other constraints to be applied silently. */
2262 void
2263 determine_visibility (tree decl)
2265 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2267 /* Only relevant for names with external linkage. */
2268 if (!TREE_PUBLIC (decl))
2269 return;
2271 /* Cloned constructors and destructors get the same visibility as
2272 the underlying function. That should be set up in
2273 maybe_clone_body. */
2274 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2276 bool orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2277 enum symbol_visibility orig_visibility = DECL_VISIBILITY (decl);
2279 /* The decl may be a template instantiation, which could influence
2280 visibilty. */
2281 tree template_decl = NULL_TREE;
2282 if (TREE_CODE (decl) == TYPE_DECL)
2284 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2286 if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
2287 template_decl = decl;
2289 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2290 template_decl = decl;
2292 else if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
2293 template_decl = decl;
2295 /* If DECL is a member of a class, visibility specifiers on the
2296 class can influence the visibility of the DECL. */
2297 tree class_type = NULL_TREE;
2298 if (DECL_CLASS_SCOPE_P (decl))
2299 class_type = DECL_CONTEXT (decl);
2300 else
2302 /* Not a class member. */
2304 /* Virtual tables have DECL_CONTEXT set to their associated class,
2305 so they are automatically handled above. */
2306 gcc_assert (!VAR_P (decl)
2307 || !DECL_VTABLE_OR_VTT_P (decl));
2309 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2311 /* Local statics and classes get the visibility of their
2312 containing function by default, except that
2313 -fvisibility-inlines-hidden doesn't affect them. */
2314 tree fn = DECL_CONTEXT (decl);
2315 if (DECL_VISIBILITY_SPECIFIED (fn))
2317 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2318 DECL_VISIBILITY_SPECIFIED (decl) =
2319 DECL_VISIBILITY_SPECIFIED (fn);
2321 else
2323 if (DECL_CLASS_SCOPE_P (fn))
2324 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2325 else if (determine_hidden_inline (fn))
2327 DECL_VISIBILITY (decl) = default_visibility;
2328 DECL_VISIBILITY_SPECIFIED (decl) =
2329 visibility_options.inpragma;
2331 else
2333 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2334 DECL_VISIBILITY_SPECIFIED (decl) =
2335 DECL_VISIBILITY_SPECIFIED (fn);
2339 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2340 but have no TEMPLATE_INFO. Their containing template
2341 function does, and the local class could be constrained
2342 by that. */
2343 if (template_decl)
2344 template_decl = fn;
2346 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2347 && flag_visibility_ms_compat)
2349 /* Under -fvisibility-ms-compat, types are visible by default,
2350 even though their contents aren't. */
2351 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2352 int underlying_vis = type_visibility (underlying_type);
2353 if (underlying_vis == VISIBILITY_ANON
2354 || (CLASS_TYPE_P (underlying_type)
2355 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2356 constrain_visibility (decl, underlying_vis, false);
2357 else
2358 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2360 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2362 /* tinfo visibility is based on the type it's for. */
2363 constrain_visibility
2364 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2366 /* Give the target a chance to override the visibility associated
2367 with DECL. */
2368 if (TREE_PUBLIC (decl)
2369 && !DECL_REALLY_EXTERN (decl)
2370 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2371 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2372 targetm.cxx.determine_class_data_visibility (decl);
2374 else if (template_decl)
2375 /* Template instantiations and specializations get visibility based
2376 on their template unless they override it with an attribute. */;
2377 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2379 if (determine_hidden_inline (decl))
2380 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2381 else
2383 /* Set default visibility to whatever the user supplied with
2384 #pragma GCC visibility or a namespace visibility attribute. */
2385 DECL_VISIBILITY (decl) = default_visibility;
2386 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2391 if (template_decl)
2393 /* If the specialization doesn't specify visibility, use the
2394 visibility from the template. */
2395 tree tinfo = get_template_info (template_decl);
2396 tree args = TI_ARGS (tinfo);
2397 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2398 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2399 : DECL_ATTRIBUTES (decl));
2401 if (args != error_mark_node)
2403 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2405 if (!DECL_VISIBILITY_SPECIFIED (decl))
2407 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2408 && determine_hidden_inline (decl))
2409 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2410 else
2412 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2413 DECL_VISIBILITY_SPECIFIED (decl)
2414 = DECL_VISIBILITY_SPECIFIED (pattern);
2418 if (args
2419 /* Template argument visibility outweighs #pragma or namespace
2420 visibility, but not an explicit attribute. */
2421 && !lookup_attribute ("visibility", attribs))
2423 int depth = TMPL_ARGS_DEPTH (args);
2424 if (DECL_VISIBILITY_SPECIFIED (decl))
2426 /* A class template member with explicit visibility
2427 overrides the class visibility, so we need to apply
2428 all the levels of template args directly. */
2429 int i;
2430 for (i = 1; i <= depth; ++i)
2432 tree lev = TMPL_ARGS_LEVEL (args, i);
2433 constrain_visibility_for_template (decl, lev);
2436 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2437 /* Limit visibility based on its template arguments. */
2438 constrain_visibility_for_template (decl, args);
2443 if (class_type)
2444 determine_visibility_from_class (decl, class_type);
2446 if (decl_anon_ns_mem_p (decl))
2447 /* Names in an anonymous namespace get internal linkage.
2448 This might change once we implement export. */
2449 constrain_visibility (decl, VISIBILITY_ANON, false);
2450 else if (TREE_CODE (decl) != TYPE_DECL)
2452 /* Propagate anonymity from type to decl. */
2453 int tvis = type_visibility (TREE_TYPE (decl));
2454 if (tvis == VISIBILITY_ANON
2455 || ! DECL_VISIBILITY_SPECIFIED (decl))
2456 constrain_visibility (decl, tvis, false);
2458 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2459 /* DR 757: A type without linkage shall not be used as the type of a
2460 variable or function with linkage, unless
2461 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2462 o the variable or function is not used (3.2 [basic.def.odr]) or is
2463 defined in the same translation unit.
2465 Since non-extern "C" decls need to be defined in the same
2466 translation unit, we can make the type internal. */
2467 constrain_visibility (decl, VISIBILITY_ANON, false);
2469 /* If visibility changed and DECL already has DECL_RTL, ensure
2470 symbol flags are updated. */
2471 if ((DECL_VISIBILITY (decl) != orig_visibility
2472 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2473 && ((VAR_P (decl) && TREE_STATIC (decl))
2474 || TREE_CODE (decl) == FUNCTION_DECL)
2475 && DECL_RTL_SET_P (decl))
2476 make_decl_rtl (decl);
2479 /* By default, static data members and function members receive
2480 the visibility of their containing class. */
2482 static void
2483 determine_visibility_from_class (tree decl, tree class_type)
2485 if (DECL_VISIBILITY_SPECIFIED (decl))
2486 return;
2488 if (determine_hidden_inline (decl))
2489 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2490 else
2492 /* Default to the class visibility. */
2493 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2494 DECL_VISIBILITY_SPECIFIED (decl)
2495 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2498 /* Give the target a chance to override the visibility associated
2499 with DECL. */
2500 if (VAR_P (decl)
2501 && (DECL_TINFO_P (decl)
2502 || (DECL_VTABLE_OR_VTT_P (decl)
2503 /* Construction virtual tables are not exported because
2504 they cannot be referred to from other object files;
2505 their name is not standardized by the ABI. */
2506 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2507 && TREE_PUBLIC (decl)
2508 && !DECL_REALLY_EXTERN (decl)
2509 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2510 targetm.cxx.determine_class_data_visibility (decl);
2513 /* Returns true iff DECL is an inline that should get hidden visibility
2514 because of -fvisibility-inlines-hidden. */
2516 static bool
2517 determine_hidden_inline (tree decl)
2519 return (visibility_options.inlines_hidden
2520 /* Don't do this for inline templates; specializations might not be
2521 inline, and we don't want them to inherit the hidden
2522 visibility. We'll set it here for all inline instantiations. */
2523 && !processing_template_decl
2524 && TREE_CODE (decl) == FUNCTION_DECL
2525 && DECL_DECLARED_INLINE_P (decl)
2526 && (! DECL_LANG_SPECIFIC (decl)
2527 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2530 /* Constrain the visibility of a class TYPE based on the visibility of its
2531 field types. Warn if any fields require lesser visibility. */
2533 void
2534 constrain_class_visibility (tree type)
2536 tree binfo;
2537 tree t;
2538 int i;
2540 int vis = type_visibility (type);
2542 if (vis == VISIBILITY_ANON
2543 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2544 return;
2546 /* Don't warn about visibility if the class has explicit visibility. */
2547 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2548 vis = VISIBILITY_INTERNAL;
2550 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2551 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2553 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2554 int subvis = type_visibility (ftype);
2556 if (subvis == VISIBILITY_ANON)
2558 if (!in_main_input_context())
2560 tree nlt = no_linkage_check (ftype, /*relaxed_p=*/false);
2561 if (nlt)
2563 if (same_type_p (TREE_TYPE (t), nlt))
2564 warning (OPT_Wsubobject_linkage, "\
2565 %qT has a field %qD whose type has no linkage",
2566 type, t);
2567 else
2568 warning (OPT_Wsubobject_linkage, "\
2569 %qT has a field %qD whose type depends on the type %qT which has no linkage",
2570 type, t, nlt);
2572 else
2573 warning (OPT_Wsubobject_linkage, "\
2574 %qT has a field %qD whose type uses the anonymous namespace",
2575 type, t);
2578 else if (MAYBE_CLASS_TYPE_P (ftype)
2579 && vis < VISIBILITY_HIDDEN
2580 && subvis >= VISIBILITY_HIDDEN)
2581 warning (OPT_Wattributes, "\
2582 %qT declared with greater visibility than the type of its field %qD",
2583 type, t);
2586 binfo = TYPE_BINFO (type);
2587 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2589 int subvis = type_visibility (TREE_TYPE (t));
2591 if (subvis == VISIBILITY_ANON)
2593 if (!in_main_input_context())
2595 tree nlt = no_linkage_check (TREE_TYPE (t), /*relaxed_p=*/false);
2596 if (nlt)
2598 if (same_type_p (TREE_TYPE (t), nlt))
2599 warning (OPT_Wsubobject_linkage, "\
2600 %qT has a base %qT whose type has no linkage",
2601 type, TREE_TYPE (t));
2602 else
2603 warning (OPT_Wsubobject_linkage, "\
2604 %qT has a base %qT whose type depends on the type %qT which has no linkage",
2605 type, TREE_TYPE (t), nlt);
2607 else
2608 warning (OPT_Wsubobject_linkage, "\
2609 %qT has a base %qT whose type uses the anonymous namespace",
2610 type, TREE_TYPE (t));
2613 else if (vis < VISIBILITY_HIDDEN
2614 && subvis >= VISIBILITY_HIDDEN)
2615 warning (OPT_Wattributes, "\
2616 %qT declared with greater visibility than its base %qT",
2617 type, TREE_TYPE (t));
2621 /* Functions for adjusting the visibility of a tagged type and its nested
2622 types and declarations when it gets a name for linkage purposes from a
2623 typedef. */
2625 static void bt_reset_linkage_1 (binding_entry, void *);
2626 static void bt_reset_linkage_2 (binding_entry, void *);
2628 /* First reset the visibility of all the types. */
2630 static void
2631 reset_type_linkage_1 (tree type)
2633 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2634 if (CLASS_TYPE_P (type))
2635 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2636 bt_reset_linkage_1, NULL);
2638 static void
2639 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2641 reset_type_linkage_1 (b->type);
2644 /* Then reset the visibility of any static data members or member
2645 functions that use those types. */
2647 static void
2648 reset_decl_linkage (tree decl)
2650 if (TREE_PUBLIC (decl))
2651 return;
2652 if (DECL_CLONED_FUNCTION_P (decl))
2653 return;
2654 TREE_PUBLIC (decl) = true;
2655 DECL_INTERFACE_KNOWN (decl) = false;
2656 determine_visibility (decl);
2657 tentative_decl_linkage (decl);
2660 static void
2661 reset_type_linkage_2 (tree type)
2663 if (CLASS_TYPE_P (type))
2665 if (tree vt = CLASSTYPE_VTABLES (type))
2667 tree name = mangle_vtbl_for_type (type);
2668 DECL_NAME (vt) = name;
2669 SET_DECL_ASSEMBLER_NAME (vt, name);
2670 reset_decl_linkage (vt);
2672 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2674 tree name = mangle_typeinfo_for_type (type);
2675 DECL_NAME (ti) = name;
2676 SET_DECL_ASSEMBLER_NAME (ti, name);
2677 TREE_TYPE (name) = type;
2678 reset_decl_linkage (ti);
2680 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2682 tree mem = STRIP_TEMPLATE (m);
2683 if (TREE_CODE (mem) == VAR_DECL || TREE_CODE (mem) == FUNCTION_DECL)
2684 reset_decl_linkage (mem);
2686 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2687 bt_reset_linkage_2, NULL);
2691 static void
2692 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2694 reset_type_linkage_2 (b->type);
2696 void
2697 reset_type_linkage (tree type)
2699 reset_type_linkage_1 (type);
2700 reset_type_linkage_2 (type);
2703 /* Set up our initial idea of what the linkage of DECL should be. */
2705 void
2706 tentative_decl_linkage (tree decl)
2708 if (DECL_INTERFACE_KNOWN (decl))
2709 /* We've already made a decision as to how this function will
2710 be handled. */;
2711 else if (vague_linkage_p (decl))
2713 if (TREE_CODE (decl) == FUNCTION_DECL
2714 && decl_defined_p (decl))
2716 DECL_EXTERNAL (decl) = 1;
2717 DECL_NOT_REALLY_EXTERN (decl) = 1;
2718 note_vague_linkage_fn (decl);
2719 /* A non-template inline function with external linkage will
2720 always be COMDAT. As we must eventually determine the
2721 linkage of all functions, and as that causes writes to
2722 the data mapped in from the PCH file, it's advantageous
2723 to mark the functions at this point. */
2724 if (DECL_DECLARED_INLINE_P (decl)
2725 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2726 || DECL_DEFAULTED_FN (decl)))
2728 /* This function must have external linkage, as
2729 otherwise DECL_INTERFACE_KNOWN would have been
2730 set. */
2731 gcc_assert (TREE_PUBLIC (decl));
2732 comdat_linkage (decl);
2733 DECL_INTERFACE_KNOWN (decl) = 1;
2736 else if (VAR_P (decl))
2737 maybe_commonize_var (decl);
2741 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2742 for DECL has not already been determined, do so now by setting
2743 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2744 function is called entities with vague linkage whose definitions
2745 are available must have TREE_PUBLIC set.
2747 If this function decides to place DECL in COMDAT, it will set
2748 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2749 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2750 callers defer that decision until it is clear that DECL is actually
2751 required. */
2753 void
2754 import_export_decl (tree decl)
2756 int emit_p;
2757 bool comdat_p;
2758 bool import_p;
2759 tree class_type = NULL_TREE;
2761 if (DECL_INTERFACE_KNOWN (decl))
2762 return;
2764 /* We cannot determine what linkage to give to an entity with vague
2765 linkage until the end of the file. For example, a virtual table
2766 for a class will be defined if and only if the key method is
2767 defined in this translation unit. As a further example, consider
2768 that when compiling a translation unit that uses PCH file with
2769 "-frepo" it would be incorrect to make decisions about what
2770 entities to emit when building the PCH; those decisions must be
2771 delayed until the repository information has been processed. */
2772 gcc_assert (at_eof);
2773 /* Object file linkage for explicit instantiations is handled in
2774 mark_decl_instantiated. For static variables in functions with
2775 vague linkage, maybe_commonize_var is used.
2777 Therefore, the only declarations that should be provided to this
2778 function are those with external linkage that are:
2780 * implicit instantiations of function templates
2782 * inline function
2784 * implicit instantiations of static data members of class
2785 templates
2787 * virtual tables
2789 * typeinfo objects
2791 Furthermore, all entities that reach this point must have a
2792 definition available in this translation unit.
2794 The following assertions check these conditions. */
2795 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2796 /* Any code that creates entities with TREE_PUBLIC cleared should
2797 also set DECL_INTERFACE_KNOWN. */
2798 gcc_assert (TREE_PUBLIC (decl));
2799 if (TREE_CODE (decl) == FUNCTION_DECL)
2800 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2801 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2802 || DECL_DECLARED_INLINE_P (decl));
2803 else
2804 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2805 || DECL_VTABLE_OR_VTT_P (decl)
2806 || DECL_TINFO_P (decl));
2807 /* Check that a definition of DECL is available in this translation
2808 unit. */
2809 gcc_assert (!DECL_REALLY_EXTERN (decl));
2811 /* Assume that DECL will not have COMDAT linkage. */
2812 comdat_p = false;
2813 /* Assume that DECL will not be imported into this translation
2814 unit. */
2815 import_p = false;
2817 /* See if the repository tells us whether or not to emit DECL in
2818 this translation unit. */
2819 emit_p = repo_emit_p (decl);
2820 if (emit_p == 0)
2821 import_p = true;
2822 else if (emit_p == 1)
2824 /* The repository indicates that this entity should be defined
2825 here. Make sure the back end honors that request. */
2826 mark_needed (decl);
2827 /* Output the definition as an ordinary strong definition. */
2828 DECL_EXTERNAL (decl) = 0;
2829 DECL_INTERFACE_KNOWN (decl) = 1;
2830 return;
2833 if (import_p)
2834 /* We have already decided what to do with this DECL; there is no
2835 need to check anything further. */
2837 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2839 class_type = DECL_CONTEXT (decl);
2840 import_export_class (class_type);
2841 if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2842 && CLASSTYPE_INTERFACE_ONLY (class_type))
2843 import_p = true;
2844 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2845 && !CLASSTYPE_USE_TEMPLATE (class_type)
2846 && CLASSTYPE_KEY_METHOD (class_type)
2847 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2848 /* The ABI requires that all virtual tables be emitted with
2849 COMDAT linkage. However, on systems where COMDAT symbols
2850 don't show up in the table of contents for a static
2851 archive, or on systems without weak symbols (where we
2852 approximate COMDAT linkage by using internal linkage), the
2853 linker will report errors about undefined symbols because
2854 it will not see the virtual table definition. Therefore,
2855 in the case that we know that the virtual table will be
2856 emitted in only one translation unit, we make the virtual
2857 table an ordinary definition with external linkage. */
2858 DECL_EXTERNAL (decl) = 0;
2859 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2861 /* CLASS_TYPE is being exported from this translation unit,
2862 so DECL should be defined here. */
2863 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2864 /* If a class is declared in a header with the "extern
2865 template" extension, then it will not be instantiated,
2866 even in translation units that would normally require
2867 it. Often such classes are explicitly instantiated in
2868 one translation unit. Therefore, the explicit
2869 instantiation must be made visible to other translation
2870 units. */
2871 DECL_EXTERNAL (decl) = 0;
2872 else
2874 /* The generic C++ ABI says that class data is always
2875 COMDAT, even if there is a key function. Some
2876 variants (e.g., the ARM EABI) says that class data
2877 only has COMDAT linkage if the class data might be
2878 emitted in more than one translation unit. When the
2879 key method can be inline and is inline, we still have
2880 to arrange for comdat even though
2881 class_data_always_comdat is false. */
2882 if (!CLASSTYPE_KEY_METHOD (class_type)
2883 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2884 || targetm.cxx.class_data_always_comdat ())
2886 /* The ABI requires COMDAT linkage. Normally, we
2887 only emit COMDAT things when they are needed;
2888 make sure that we realize that this entity is
2889 indeed needed. */
2890 comdat_p = true;
2891 mark_needed (decl);
2895 else if (!flag_implicit_templates
2896 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2897 import_p = true;
2898 else
2899 comdat_p = true;
2901 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2903 tree type = TREE_TYPE (DECL_NAME (decl));
2904 if (CLASS_TYPE_P (type))
2906 class_type = type;
2907 import_export_class (type);
2908 if (CLASSTYPE_INTERFACE_KNOWN (type)
2909 && TYPE_POLYMORPHIC_P (type)
2910 && CLASSTYPE_INTERFACE_ONLY (type)
2911 /* If -fno-rtti was specified, then we cannot be sure
2912 that RTTI information will be emitted with the
2913 virtual table of the class, so we must emit it
2914 wherever it is used. */
2915 && flag_rtti)
2916 import_p = true;
2917 else
2919 if (CLASSTYPE_INTERFACE_KNOWN (type)
2920 && !CLASSTYPE_INTERFACE_ONLY (type))
2922 comdat_p = (targetm.cxx.class_data_always_comdat ()
2923 || (CLASSTYPE_KEY_METHOD (type)
2924 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2925 mark_needed (decl);
2926 if (!flag_weak)
2928 comdat_p = false;
2929 DECL_EXTERNAL (decl) = 0;
2932 else
2933 comdat_p = true;
2936 else
2937 comdat_p = true;
2939 else if (DECL_TEMPLOID_INSTANTIATION (decl))
2941 /* DECL is an implicit instantiation of a function or static
2942 data member. */
2943 if ((flag_implicit_templates
2944 && !flag_use_repository)
2945 || (flag_implicit_inline_templates
2946 && TREE_CODE (decl) == FUNCTION_DECL
2947 && DECL_DECLARED_INLINE_P (decl)))
2948 comdat_p = true;
2949 else
2950 /* If we are not implicitly generating templates, then mark
2951 this entity as undefined in this translation unit. */
2952 import_p = true;
2954 else if (DECL_FUNCTION_MEMBER_P (decl))
2956 if (!DECL_DECLARED_INLINE_P (decl))
2958 tree ctype = DECL_CONTEXT (decl);
2959 import_export_class (ctype);
2960 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2962 DECL_NOT_REALLY_EXTERN (decl)
2963 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2964 || (DECL_DECLARED_INLINE_P (decl)
2965 && ! flag_implement_inlines
2966 && !DECL_VINDEX (decl)));
2968 if (!DECL_NOT_REALLY_EXTERN (decl))
2969 DECL_EXTERNAL (decl) = 1;
2971 /* Always make artificials weak. */
2972 if (DECL_ARTIFICIAL (decl) && flag_weak)
2973 comdat_p = true;
2974 else
2975 maybe_make_one_only (decl);
2978 else
2979 comdat_p = true;
2981 else
2982 comdat_p = true;
2984 if (import_p)
2986 /* If we are importing DECL into this translation unit, mark is
2987 an undefined here. */
2988 DECL_EXTERNAL (decl) = 1;
2989 DECL_NOT_REALLY_EXTERN (decl) = 0;
2991 else if (comdat_p)
2993 /* If we decided to put DECL in COMDAT, mark it accordingly at
2994 this point. */
2995 comdat_linkage (decl);
2998 DECL_INTERFACE_KNOWN (decl) = 1;
3001 /* Return an expression that performs the destruction of DECL, which
3002 must be a VAR_DECL whose type has a non-trivial destructor, or is
3003 an array whose (innermost) elements have a non-trivial destructor. */
3005 tree
3006 build_cleanup (tree decl)
3008 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
3009 gcc_assert (clean != NULL_TREE);
3010 return clean;
3013 /* Returns the initialization guard variable for the variable DECL,
3014 which has static storage duration. */
3016 tree
3017 get_guard (tree decl)
3019 tree sname;
3020 tree guard;
3022 sname = mangle_guard_variable (decl);
3023 guard = get_global_binding (sname);
3024 if (! guard)
3026 tree guard_type;
3028 /* We use a type that is big enough to contain a mutex as well
3029 as an integer counter. */
3030 guard_type = targetm.cxx.guard_type ();
3031 guard = build_decl (DECL_SOURCE_LOCATION (decl),
3032 VAR_DECL, sname, guard_type);
3034 /* The guard should have the same linkage as what it guards. */
3035 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
3036 TREE_STATIC (guard) = TREE_STATIC (decl);
3037 DECL_COMMON (guard) = DECL_COMMON (decl);
3038 DECL_COMDAT (guard) = DECL_COMDAT (decl);
3039 CP_DECL_THREAD_LOCAL_P (guard) = CP_DECL_THREAD_LOCAL_P (decl);
3040 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
3041 if (DECL_ONE_ONLY (decl))
3042 make_decl_one_only (guard, cxx_comdat_group (guard));
3043 if (TREE_PUBLIC (decl))
3044 DECL_WEAK (guard) = DECL_WEAK (decl);
3045 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
3046 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
3048 DECL_ARTIFICIAL (guard) = 1;
3049 DECL_IGNORED_P (guard) = 1;
3050 TREE_USED (guard) = 1;
3051 pushdecl_top_level_and_finish (guard, NULL_TREE);
3053 return guard;
3056 /* Return an atomic load of src with the appropriate memory model. */
3058 static tree
3059 build_atomic_load_byte (tree src, HOST_WIDE_INT model)
3061 tree ptr_type = build_pointer_type (char_type_node);
3062 tree mem_model = build_int_cst (integer_type_node, model);
3063 tree t, addr, val;
3064 unsigned int size;
3065 int fncode;
3067 size = tree_to_uhwi (TYPE_SIZE_UNIT (char_type_node));
3069 fncode = BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1;
3070 t = builtin_decl_implicit ((enum built_in_function) fncode);
3072 addr = build1 (ADDR_EXPR, ptr_type, src);
3073 val = build_call_expr (t, 2, addr, mem_model);
3074 return val;
3077 /* Return those bits of the GUARD variable that should be set when the
3078 guarded entity is actually initialized. */
3080 static tree
3081 get_guard_bits (tree guard)
3083 if (!targetm.cxx.guard_mask_bit ())
3085 /* We only set the first byte of the guard, in order to leave room
3086 for a mutex in the high-order bits. */
3087 guard = build1 (ADDR_EXPR,
3088 build_pointer_type (TREE_TYPE (guard)),
3089 guard);
3090 guard = build1 (NOP_EXPR,
3091 build_pointer_type (char_type_node),
3092 guard);
3093 guard = build1 (INDIRECT_REF, char_type_node, guard);
3096 return guard;
3099 /* Return an expression which determines whether or not the GUARD
3100 variable has already been initialized. */
3102 tree
3103 get_guard_cond (tree guard, bool thread_safe)
3105 tree guard_value;
3107 if (!thread_safe)
3108 guard = get_guard_bits (guard);
3109 else
3110 guard = build_atomic_load_byte (guard, MEMMODEL_ACQUIRE);
3112 /* Mask off all but the low bit. */
3113 if (targetm.cxx.guard_mask_bit ())
3115 guard_value = integer_one_node;
3116 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3117 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3118 guard = cp_build_binary_op (input_location,
3119 BIT_AND_EXPR, guard, guard_value,
3120 tf_warning_or_error);
3123 guard_value = integer_zero_node;
3124 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3125 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3126 return cp_build_binary_op (input_location,
3127 EQ_EXPR, guard, guard_value,
3128 tf_warning_or_error);
3131 /* Return an expression which sets the GUARD variable, indicating that
3132 the variable being guarded has been initialized. */
3134 tree
3135 set_guard (tree guard)
3137 tree guard_init;
3139 /* Set the GUARD to one. */
3140 guard = get_guard_bits (guard);
3141 guard_init = integer_one_node;
3142 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3143 guard_init = fold_convert (TREE_TYPE (guard), guard_init);
3144 return cp_build_modify_expr (input_location, guard, NOP_EXPR, guard_init,
3145 tf_warning_or_error);
3148 /* Returns true iff we can tell that VAR does not have a dynamic
3149 initializer. */
3151 static bool
3152 var_defined_without_dynamic_init (tree var)
3154 /* If it's defined in another TU, we can't tell. */
3155 if (DECL_EXTERNAL (var))
3156 return false;
3157 /* If it has a non-trivial destructor, registering the destructor
3158 counts as dynamic initialization. */
3159 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3160 return false;
3161 /* If it's in this TU, its initializer has been processed, unless
3162 it's a case of self-initialization, then DECL_INITIALIZED_P is
3163 false while the initializer is handled by finish_id_expression. */
3164 if (!DECL_INITIALIZED_P (var))
3165 return false;
3166 /* If it has no initializer or a constant one, it's not dynamic. */
3167 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3168 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3171 /* Returns true iff VAR is a variable that needs uses to be
3172 wrapped for possible dynamic initialization. */
3174 static bool
3175 var_needs_tls_wrapper (tree var)
3177 return (!error_operand_p (var)
3178 && CP_DECL_THREAD_LOCAL_P (var)
3179 && !DECL_GNU_TLS_P (var)
3180 && !DECL_FUNCTION_SCOPE_P (var)
3181 && !var_defined_without_dynamic_init (var));
3184 /* Get the FUNCTION_DECL for the shared TLS init function for this
3185 translation unit. */
3187 static tree
3188 get_local_tls_init_fn (void)
3190 tree sname = get_identifier ("__tls_init");
3191 tree fn = get_global_binding (sname);
3192 if (!fn)
3194 fn = build_lang_decl (FUNCTION_DECL, sname,
3195 build_function_type (void_type_node,
3196 void_list_node));
3197 SET_DECL_LANGUAGE (fn, lang_c);
3198 TREE_PUBLIC (fn) = false;
3199 DECL_ARTIFICIAL (fn) = true;
3200 mark_used (fn);
3201 set_global_binding (fn);
3203 return fn;
3206 /* Get a FUNCTION_DECL for the init function for the thread_local
3207 variable VAR. The init function will be an alias to the function
3208 that initializes all the non-local TLS variables in the translation
3209 unit. The init function is only used by the wrapper function. */
3211 static tree
3212 get_tls_init_fn (tree var)
3214 /* Only C++11 TLS vars need this init fn. */
3215 if (!var_needs_tls_wrapper (var))
3216 return NULL_TREE;
3218 /* If -fno-extern-tls-init, assume that we don't need to call
3219 a tls init function for a variable defined in another TU. */
3220 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3221 return NULL_TREE;
3223 /* If the variable is internal, or if we can't generate aliases,
3224 call the local init function directly. */
3225 if (!TREE_PUBLIC (var) || !TARGET_SUPPORTS_ALIASES)
3226 return get_local_tls_init_fn ();
3228 tree sname = mangle_tls_init_fn (var);
3229 tree fn = get_global_binding (sname);
3230 if (!fn)
3232 fn = build_lang_decl (FUNCTION_DECL, sname,
3233 build_function_type (void_type_node,
3234 void_list_node));
3235 SET_DECL_LANGUAGE (fn, lang_c);
3236 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3237 DECL_ARTIFICIAL (fn) = true;
3238 DECL_COMDAT (fn) = DECL_COMDAT (var);
3239 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3240 if (DECL_ONE_ONLY (var))
3241 make_decl_one_only (fn, cxx_comdat_group (fn));
3242 if (TREE_PUBLIC (var))
3244 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3245 /* If the variable is defined somewhere else and might have static
3246 initialization, make the init function a weak reference. */
3247 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3248 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3249 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3250 && DECL_EXTERNAL (var))
3251 declare_weak (fn);
3252 else
3253 DECL_WEAK (fn) = DECL_WEAK (var);
3255 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3256 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3257 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3258 DECL_IGNORED_P (fn) = 1;
3259 mark_used (fn);
3261 DECL_BEFRIENDING_CLASSES (fn) = var;
3263 set_global_binding (fn);
3265 return fn;
3268 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3269 variable VAR. The wrapper function calls the init function (if any) for
3270 VAR and then returns a reference to VAR. The wrapper function is used
3271 in place of VAR everywhere VAR is mentioned. */
3273 tree
3274 get_tls_wrapper_fn (tree var)
3276 /* Only C++11 TLS vars need this wrapper fn. */
3277 if (!var_needs_tls_wrapper (var))
3278 return NULL_TREE;
3280 tree sname = mangle_tls_wrapper_fn (var);
3281 tree fn = get_global_binding (sname);
3282 if (!fn)
3284 /* A named rvalue reference is an lvalue, so the wrapper should
3285 always return an lvalue reference. */
3286 tree type = non_reference (TREE_TYPE (var));
3287 type = build_reference_type (type);
3288 tree fntype = build_function_type (type, void_list_node);
3289 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3290 SET_DECL_LANGUAGE (fn, lang_c);
3291 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3292 DECL_ARTIFICIAL (fn) = true;
3293 DECL_IGNORED_P (fn) = 1;
3294 /* The wrapper is inline and emitted everywhere var is used. */
3295 DECL_DECLARED_INLINE_P (fn) = true;
3296 if (TREE_PUBLIC (var))
3298 comdat_linkage (fn);
3299 #ifdef HAVE_GAS_HIDDEN
3300 /* Make the wrapper bind locally; there's no reason to share
3301 the wrapper between multiple shared objects. */
3302 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3303 DECL_VISIBILITY_SPECIFIED (fn) = true;
3304 #endif
3306 if (!TREE_PUBLIC (fn))
3307 DECL_INTERFACE_KNOWN (fn) = true;
3308 mark_used (fn);
3309 note_vague_linkage_fn (fn);
3311 #if 0
3312 /* We want CSE to commonize calls to the wrapper, but marking it as
3313 pure is unsafe since it has side-effects. I guess we need a new
3314 ECF flag even weaker than ECF_PURE. FIXME! */
3315 DECL_PURE_P (fn) = true;
3316 #endif
3318 DECL_BEFRIENDING_CLASSES (fn) = var;
3320 set_global_binding (fn);
3322 return fn;
3325 /* At EOF, generate the definition for the TLS wrapper function FN:
3327 T& var_wrapper() {
3328 if (init_fn) init_fn();
3329 return var;
3330 } */
3332 static void
3333 generate_tls_wrapper (tree fn)
3335 tree var = DECL_BEFRIENDING_CLASSES (fn);
3337 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3338 tree body = begin_function_body ();
3339 /* Only call the init fn if there might be one. */
3340 if (tree init_fn = get_tls_init_fn (var))
3342 tree if_stmt = NULL_TREE;
3343 /* If init_fn is a weakref, make sure it exists before calling. */
3344 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3346 if_stmt = begin_if_stmt ();
3347 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3348 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3349 NE_EXPR, addr, nullptr_node,
3350 tf_warning_or_error);
3351 finish_if_stmt_cond (cond, if_stmt);
3353 finish_expr_stmt (build_cxx_call
3354 (init_fn, 0, NULL, tf_warning_or_error));
3355 if (if_stmt)
3357 finish_then_clause (if_stmt);
3358 finish_if_stmt (if_stmt);
3361 else
3362 /* If there's no initialization, the wrapper is a constant function. */
3363 TREE_READONLY (fn) = true;
3364 finish_return_stmt (convert_from_reference (var));
3365 finish_function_body (body);
3366 expand_or_defer_fn (finish_function (/*inline_p=*/false));
3369 /* Start the process of running a particular set of global constructors
3370 or destructors. Subroutine of do_[cd]tors. Also called from
3371 vtv_start_verification_constructor_init_function. */
3373 static tree
3374 start_objects (int method_type, int initp)
3376 tree body;
3377 tree fndecl;
3378 char type[14];
3380 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3382 if (initp != DEFAULT_INIT_PRIORITY)
3384 char joiner;
3386 #ifdef JOINER
3387 joiner = JOINER;
3388 #else
3389 joiner = '_';
3390 #endif
3392 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3394 else
3395 sprintf (type, "sub_%c", method_type);
3397 fndecl = build_lang_decl (FUNCTION_DECL,
3398 get_file_function_name (type),
3399 build_function_type_list (void_type_node,
3400 NULL_TREE));
3401 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3403 TREE_PUBLIC (current_function_decl) = 0;
3405 /* Mark as artificial because it's not explicitly in the user's
3406 source code. */
3407 DECL_ARTIFICIAL (current_function_decl) = 1;
3409 /* Mark this declaration as used to avoid spurious warnings. */
3410 TREE_USED (current_function_decl) = 1;
3412 /* Mark this function as a global constructor or destructor. */
3413 if (method_type == 'I')
3414 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3415 else
3416 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3418 body = begin_compound_stmt (BCS_FN_BODY);
3420 return body;
3423 /* Finish the process of running a particular set of global constructors
3424 or destructors. Subroutine of do_[cd]tors. */
3426 static void
3427 finish_objects (int method_type, int initp, tree body)
3429 tree fn;
3431 /* Finish up. */
3432 finish_compound_stmt (body);
3433 fn = finish_function (/*inline_p=*/false);
3435 if (method_type == 'I')
3437 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3438 decl_init_priority_insert (fn, initp);
3440 else
3442 DECL_STATIC_DESTRUCTOR (fn) = 1;
3443 decl_fini_priority_insert (fn, initp);
3446 expand_or_defer_fn (fn);
3449 /* The names of the parameters to the function created to handle
3450 initializations and destructions for objects with static storage
3451 duration. */
3452 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3453 #define PRIORITY_IDENTIFIER "__priority"
3455 /* The name of the function we create to handle initializations and
3456 destructions for objects with static storage duration. */
3457 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3459 /* The declaration for the __INITIALIZE_P argument. */
3460 static GTY(()) tree initialize_p_decl;
3462 /* The declaration for the __PRIORITY argument. */
3463 static GTY(()) tree priority_decl;
3465 /* The declaration for the static storage duration function. */
3466 static GTY(()) tree ssdf_decl;
3468 /* All the static storage duration functions created in this
3469 translation unit. */
3470 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3472 /* A map from priority levels to information about that priority
3473 level. There may be many such levels, so efficient lookup is
3474 important. */
3475 static splay_tree priority_info_map;
3477 /* Begins the generation of the function that will handle all
3478 initialization and destruction of objects with static storage
3479 duration. The function generated takes two parameters of type
3480 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3481 nonzero, it performs initializations. Otherwise, it performs
3482 destructions. It only performs those initializations or
3483 destructions with the indicated __PRIORITY. The generated function
3484 returns no value.
3486 It is assumed that this function will only be called once per
3487 translation unit. */
3489 static tree
3490 start_static_storage_duration_function (unsigned count)
3492 tree type;
3493 tree body;
3494 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3496 /* Create the identifier for this function. It will be of the form
3497 SSDF_IDENTIFIER_<number>. */
3498 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3500 type = build_function_type_list (void_type_node,
3501 integer_type_node, integer_type_node,
3502 NULL_TREE);
3504 /* Create the FUNCTION_DECL itself. */
3505 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3506 get_identifier (id),
3507 type);
3508 TREE_PUBLIC (ssdf_decl) = 0;
3509 DECL_ARTIFICIAL (ssdf_decl) = 1;
3511 /* Put this function in the list of functions to be called from the
3512 static constructors and destructors. */
3513 if (!ssdf_decls)
3515 vec_alloc (ssdf_decls, 32);
3517 /* Take this opportunity to initialize the map from priority
3518 numbers to information about that priority level. */
3519 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3520 /*delete_key_fn=*/0,
3521 /*delete_value_fn=*/
3522 (splay_tree_delete_value_fn) &free);
3524 /* We always need to generate functions for the
3525 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3526 priorities later, we'll be sure to find the
3527 DEFAULT_INIT_PRIORITY. */
3528 get_priority_info (DEFAULT_INIT_PRIORITY);
3531 vec_safe_push (ssdf_decls, ssdf_decl);
3533 /* Create the argument list. */
3534 initialize_p_decl = cp_build_parm_decl
3535 (ssdf_decl, get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3536 TREE_USED (initialize_p_decl) = 1;
3537 priority_decl = cp_build_parm_decl
3538 (ssdf_decl, get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3539 TREE_USED (priority_decl) = 1;
3541 DECL_CHAIN (initialize_p_decl) = priority_decl;
3542 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3544 /* Put the function in the global scope. */
3545 pushdecl (ssdf_decl);
3547 /* Start the function itself. This is equivalent to declaring the
3548 function as:
3550 static void __ssdf (int __initialize_p, init __priority_p);
3552 It is static because we only need to call this function from the
3553 various constructor and destructor functions for this module. */
3554 start_preparsed_function (ssdf_decl,
3555 /*attrs=*/NULL_TREE,
3556 SF_PRE_PARSED);
3558 /* Set up the scope of the outermost block in the function. */
3559 body = begin_compound_stmt (BCS_FN_BODY);
3561 return body;
3564 /* Finish the generation of the function which performs initialization
3565 and destruction of objects with static storage duration. After
3566 this point, no more such objects can be created. */
3568 static void
3569 finish_static_storage_duration_function (tree body)
3571 /* Close out the function. */
3572 finish_compound_stmt (body);
3573 expand_or_defer_fn (finish_function (/*inline_p=*/false));
3576 /* Return the information about the indicated PRIORITY level. If no
3577 code to handle this level has yet been generated, generate the
3578 appropriate prologue. */
3580 static priority_info
3581 get_priority_info (int priority)
3583 priority_info pi;
3584 splay_tree_node n;
3586 n = splay_tree_lookup (priority_info_map,
3587 (splay_tree_key) priority);
3588 if (!n)
3590 /* Create a new priority information structure, and insert it
3591 into the map. */
3592 pi = XNEW (struct priority_info_s);
3593 pi->initializations_p = 0;
3594 pi->destructions_p = 0;
3595 splay_tree_insert (priority_info_map,
3596 (splay_tree_key) priority,
3597 (splay_tree_value) pi);
3599 else
3600 pi = (priority_info) n->value;
3602 return pi;
3605 /* The effective initialization priority of a DECL. */
3607 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3608 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3609 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3611 /* Whether a DECL needs a guard to protect it against multiple
3612 initialization. */
3614 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3615 || DECL_ONE_ONLY (decl) \
3616 || DECL_WEAK (decl)))
3618 /* Called from one_static_initialization_or_destruction(),
3619 via walk_tree.
3620 Walks the initializer list of a global variable and looks for
3621 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3622 and that have their DECL_CONTEXT() == NULL.
3623 For each such temporary variable, set their DECL_CONTEXT() to
3624 the current function. This is necessary because otherwise
3625 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3626 when trying to refer to a temporary variable that does not have
3627 it's DECL_CONTECT() properly set. */
3628 static tree
3629 fix_temporary_vars_context_r (tree *node,
3630 int * /*unused*/,
3631 void * /*unused1*/)
3633 gcc_assert (current_function_decl);
3635 if (TREE_CODE (*node) == BIND_EXPR)
3637 tree var;
3639 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3640 if (VAR_P (var)
3641 && !DECL_NAME (var)
3642 && DECL_ARTIFICIAL (var)
3643 && !DECL_CONTEXT (var))
3644 DECL_CONTEXT (var) = current_function_decl;
3647 return NULL_TREE;
3650 /* Set up to handle the initialization or destruction of DECL. If
3651 INITP is nonzero, we are initializing the variable. Otherwise, we
3652 are destroying it. */
3654 static void
3655 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3657 tree guard_if_stmt = NULL_TREE;
3658 tree guard;
3660 /* If we are supposed to destruct and there's a trivial destructor,
3661 nothing has to be done. */
3662 if (!initp
3663 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3664 return;
3666 /* Trick the compiler into thinking we are at the file and line
3667 where DECL was declared so that error-messages make sense, and so
3668 that the debugger will show somewhat sensible file and line
3669 information. */
3670 input_location = DECL_SOURCE_LOCATION (decl);
3672 /* Make sure temporary variables in the initialiser all have
3673 their DECL_CONTEXT() set to a value different from NULL_TREE.
3674 This can happen when global variables initializers are built.
3675 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3676 the temporary variables that might have been generated in the
3677 accompanying initializers is NULL_TREE, meaning the variables have been
3678 declared in the global namespace.
3679 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3680 of the temporaries are set to the current function decl. */
3681 cp_walk_tree_without_duplicates (&init,
3682 fix_temporary_vars_context_r,
3683 NULL);
3685 /* Because of:
3687 [class.access.spec]
3689 Access control for implicit calls to the constructors,
3690 the conversion functions, or the destructor called to
3691 create and destroy a static data member is performed as
3692 if these calls appeared in the scope of the member's
3693 class.
3695 we pretend we are in a static member function of the class of
3696 which the DECL is a member. */
3697 if (member_p (decl))
3699 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3700 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3703 /* Assume we don't need a guard. */
3704 guard = NULL_TREE;
3705 /* We need a guard if this is an object with external linkage that
3706 might be initialized in more than one place. (For example, a
3707 static data member of a template, when the data member requires
3708 construction.) */
3709 if (NEEDS_GUARD_P (decl))
3711 tree guard_cond;
3713 guard = get_guard (decl);
3715 /* When using __cxa_atexit, we just check the GUARD as we would
3716 for a local static. */
3717 if (flag_use_cxa_atexit)
3719 /* When using __cxa_atexit, we never try to destroy
3720 anything from a static destructor. */
3721 gcc_assert (initp);
3722 guard_cond = get_guard_cond (guard, false);
3724 /* If we don't have __cxa_atexit, then we will be running
3725 destructors from .fini sections, or their equivalents. So,
3726 we need to know how many times we've tried to initialize this
3727 object. We do initializations only if the GUARD is zero,
3728 i.e., if we are the first to initialize the variable. We do
3729 destructions only if the GUARD is one, i.e., if we are the
3730 last to destroy the variable. */
3731 else if (initp)
3732 guard_cond
3733 = cp_build_binary_op (input_location,
3734 EQ_EXPR,
3735 cp_build_unary_op (PREINCREMENT_EXPR,
3736 guard,
3737 /*noconvert=*/true,
3738 tf_warning_or_error),
3739 integer_one_node,
3740 tf_warning_or_error);
3741 else
3742 guard_cond
3743 = cp_build_binary_op (input_location,
3744 EQ_EXPR,
3745 cp_build_unary_op (PREDECREMENT_EXPR,
3746 guard,
3747 /*noconvert=*/true,
3748 tf_warning_or_error),
3749 integer_zero_node,
3750 tf_warning_or_error);
3752 guard_if_stmt = begin_if_stmt ();
3753 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3757 /* If we're using __cxa_atexit, we have not already set the GUARD,
3758 so we must do so now. */
3759 if (guard && initp && flag_use_cxa_atexit)
3760 finish_expr_stmt (set_guard (guard));
3762 /* Perform the initialization or destruction. */
3763 if (initp)
3765 if (init)
3767 finish_expr_stmt (init);
3768 if (sanitize_flags_p (SANITIZE_ADDRESS, decl))
3770 varpool_node *vnode = varpool_node::get (decl);
3771 if (vnode)
3772 vnode->dynamically_initialized = 1;
3776 /* If we're using __cxa_atexit, register a function that calls the
3777 destructor for the object. */
3778 if (flag_use_cxa_atexit)
3779 finish_expr_stmt (register_dtor_fn (decl));
3781 else
3782 finish_expr_stmt (build_cleanup (decl));
3784 /* Finish the guard if-stmt, if necessary. */
3785 if (guard)
3787 finish_then_clause (guard_if_stmt);
3788 finish_if_stmt (guard_if_stmt);
3791 /* Now that we're done with DECL we don't need to pretend to be a
3792 member of its class any longer. */
3793 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3794 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3797 /* Generate code to do the initialization or destruction of the decls in VARS,
3798 a TREE_LIST of VAR_DECL with static storage duration.
3799 Whether initialization or destruction is performed is specified by INITP. */
3801 static void
3802 do_static_initialization_or_destruction (tree vars, bool initp)
3804 tree node, init_if_stmt, cond;
3806 /* Build the outer if-stmt to check for initialization or destruction. */
3807 init_if_stmt = begin_if_stmt ();
3808 cond = initp ? integer_one_node : integer_zero_node;
3809 cond = cp_build_binary_op (input_location,
3810 EQ_EXPR,
3811 initialize_p_decl,
3812 cond,
3813 tf_warning_or_error);
3814 finish_if_stmt_cond (cond, init_if_stmt);
3816 /* To make sure dynamic construction doesn't access globals from other
3817 compilation units where they might not be yet constructed, for
3818 -fsanitize=address insert __asan_before_dynamic_init call that
3819 prevents access to either all global variables that need construction
3820 in other compilation units, or at least those that haven't been
3821 initialized yet. Variables that need dynamic construction in
3822 the current compilation unit are kept accessible. */
3823 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
3824 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3826 node = vars;
3827 do {
3828 tree decl = TREE_VALUE (node);
3829 tree priority_if_stmt;
3830 int priority;
3831 priority_info pi;
3833 /* If we don't need a destructor, there's nothing to do. Avoid
3834 creating a possibly empty if-stmt. */
3835 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3837 node = TREE_CHAIN (node);
3838 continue;
3841 /* Remember that we had an initialization or finalization at this
3842 priority. */
3843 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3844 pi = get_priority_info (priority);
3845 if (initp)
3846 pi->initializations_p = 1;
3847 else
3848 pi->destructions_p = 1;
3850 /* Conditionalize this initialization on being in the right priority
3851 and being initializing/finalizing appropriately. */
3852 priority_if_stmt = begin_if_stmt ();
3853 cond = cp_build_binary_op (input_location,
3854 EQ_EXPR,
3855 priority_decl,
3856 build_int_cst (NULL_TREE, priority),
3857 tf_warning_or_error);
3858 finish_if_stmt_cond (cond, priority_if_stmt);
3860 /* Process initializers with same priority. */
3861 for (; node
3862 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3863 node = TREE_CHAIN (node))
3864 /* Do one initialization or destruction. */
3865 one_static_initialization_or_destruction (TREE_VALUE (node),
3866 TREE_PURPOSE (node), initp);
3868 /* Finish up the priority if-stmt body. */
3869 finish_then_clause (priority_if_stmt);
3870 finish_if_stmt (priority_if_stmt);
3872 } while (node);
3874 /* Revert what __asan_before_dynamic_init did by calling
3875 __asan_after_dynamic_init. */
3876 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
3877 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3879 /* Finish up the init/destruct if-stmt body. */
3880 finish_then_clause (init_if_stmt);
3881 finish_if_stmt (init_if_stmt);
3884 /* VARS is a list of variables with static storage duration which may
3885 need initialization and/or finalization. Remove those variables
3886 that don't really need to be initialized or finalized, and return
3887 the resulting list. The order in which the variables appear in
3888 VARS is in reverse order of the order in which they should actually
3889 be initialized. The list we return is in the unreversed order;
3890 i.e., the first variable should be initialized first. */
3892 static tree
3893 prune_vars_needing_no_initialization (tree *vars)
3895 tree *var = vars;
3896 tree result = NULL_TREE;
3898 while (*var)
3900 tree t = *var;
3901 tree decl = TREE_VALUE (t);
3902 tree init = TREE_PURPOSE (t);
3904 /* Deal gracefully with error. */
3905 if (error_operand_p (decl))
3907 var = &TREE_CHAIN (t);
3908 continue;
3911 /* The only things that can be initialized are variables. */
3912 gcc_assert (VAR_P (decl));
3914 /* If this object is not defined, we don't need to do anything
3915 here. */
3916 if (DECL_EXTERNAL (decl))
3918 var = &TREE_CHAIN (t);
3919 continue;
3922 /* Also, if the initializer already contains errors, we can bail
3923 out now. */
3924 if (init && TREE_CODE (init) == TREE_LIST
3925 && value_member (error_mark_node, init))
3927 var = &TREE_CHAIN (t);
3928 continue;
3931 /* This variable is going to need initialization and/or
3932 finalization, so we add it to the list. */
3933 *var = TREE_CHAIN (t);
3934 TREE_CHAIN (t) = result;
3935 result = t;
3938 return result;
3941 /* Make sure we have told the back end about all the variables in
3942 VARS. */
3944 static void
3945 write_out_vars (tree vars)
3947 tree v;
3949 for (v = vars; v; v = TREE_CHAIN (v))
3951 tree var = TREE_VALUE (v);
3952 if (!var_finalized_p (var))
3954 import_export_decl (var);
3955 rest_of_decl_compilation (var, 1, 1);
3960 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3961 (otherwise) that will initialize all global objects with static
3962 storage duration having the indicated PRIORITY. */
3964 static void
3965 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3966 location_t *locus)
3968 char function_key;
3969 tree fndecl;
3970 tree body;
3971 size_t i;
3973 input_location = *locus;
3974 /* ??? */
3975 /* Was: locus->line++; */
3977 /* We use `I' to indicate initialization and `D' to indicate
3978 destruction. */
3979 function_key = constructor_p ? 'I' : 'D';
3981 /* We emit the function lazily, to avoid generating empty
3982 global constructors and destructors. */
3983 body = NULL_TREE;
3985 /* For Objective-C++, we may need to initialize metadata found in this module.
3986 This must be done _before_ any other static initializations. */
3987 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3988 && constructor_p && objc_static_init_needed_p ())
3990 body = start_objects (function_key, priority);
3991 objc_generate_static_init_call (NULL_TREE);
3994 /* Call the static storage duration function with appropriate
3995 arguments. */
3996 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
3998 /* Calls to pure or const functions will expand to nothing. */
3999 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
4001 tree call;
4003 if (! body)
4004 body = start_objects (function_key, priority);
4006 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
4007 build_int_cst (NULL_TREE,
4008 constructor_p),
4009 build_int_cst (NULL_TREE,
4010 priority),
4011 NULL_TREE);
4012 finish_expr_stmt (call);
4016 /* Close out the function. */
4017 if (body)
4018 finish_objects (function_key, priority, body);
4021 /* Generate constructor and destructor functions for the priority
4022 indicated by N. */
4024 static int
4025 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
4027 location_t *locus = (location_t *) data;
4028 int priority = (int) n->key;
4029 priority_info pi = (priority_info) n->value;
4031 /* Generate the functions themselves, but only if they are really
4032 needed. */
4033 if (pi->initializations_p)
4034 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
4035 if (pi->destructions_p)
4036 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
4038 /* Keep iterating. */
4039 return 0;
4042 /* Return C++ property of T, based on given operation OP. */
4044 static int
4045 cpp_check (tree t, cpp_operation op)
4047 switch (op)
4049 case HAS_DEPENDENT_TEMPLATE_ARGS:
4051 tree ti = CLASSTYPE_TEMPLATE_INFO (t);
4052 if (!ti)
4053 return 0;
4054 ++processing_template_decl;
4055 const bool dep = any_dependent_template_arguments_p (TI_ARGS (ti));
4056 --processing_template_decl;
4057 return dep;
4059 case IS_ABSTRACT:
4060 return DECL_PURE_VIRTUAL_P (t);
4061 case IS_CONSTRUCTOR:
4062 return DECL_CONSTRUCTOR_P (t);
4063 case IS_DESTRUCTOR:
4064 return DECL_DESTRUCTOR_P (t);
4065 case IS_COPY_CONSTRUCTOR:
4066 return DECL_COPY_CONSTRUCTOR_P (t);
4067 case IS_MOVE_CONSTRUCTOR:
4068 return DECL_MOVE_CONSTRUCTOR_P (t);
4069 case IS_TEMPLATE:
4070 return TREE_CODE (t) == TEMPLATE_DECL;
4071 case IS_TRIVIAL:
4072 return trivial_type_p (t);
4073 default:
4074 return 0;
4078 /* Collect source file references recursively, starting from NAMESPC. */
4080 static void
4081 collect_source_refs (tree namespc)
4083 /* Iterate over names in this name space. */
4084 for (tree t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4085 if (DECL_IS_BUILTIN (t))
4087 else if (TREE_CODE (t) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (t))
4088 collect_source_refs (t);
4089 else
4090 collect_source_ref (DECL_SOURCE_FILE (t));
4093 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4094 starting from NAMESPC. */
4096 static void
4097 collect_ada_namespace (tree namespc, const char *source_file)
4099 tree decl = NAMESPACE_LEVEL (namespc)->names;
4101 /* Collect decls from this namespace. This will skip
4102 NAMESPACE_DECLs (both aliases and regular, it cannot tell). */
4103 collect_ada_nodes (decl, source_file);
4105 /* Now scan for namespace children, and dump them. */
4106 for (; decl; decl = TREE_CHAIN (decl))
4107 if (TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl))
4108 collect_ada_namespace (decl, source_file);
4111 /* Returns true iff there is a definition available for variable or
4112 function DECL. */
4114 bool
4115 decl_defined_p (tree decl)
4117 if (TREE_CODE (decl) == FUNCTION_DECL)
4118 return (DECL_INITIAL (decl) != NULL_TREE
4119 /* A pending instantiation of a friend temploid is defined. */
4120 || (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
4121 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4122 (DECL_TI_TEMPLATE (decl)))));
4123 else
4125 gcc_assert (VAR_P (decl));
4126 return !DECL_EXTERNAL (decl);
4130 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4132 [expr.const]
4134 An integral constant-expression can only involve ... const
4135 variables of integral or enumeration types initialized with
4136 constant expressions ...
4138 C++0x also allows constexpr variables and temporaries initialized
4139 with constant expressions. We handle the former here, but the latter
4140 are just folded away in cxx_eval_constant_expression.
4142 The standard does not require that the expression be non-volatile.
4143 G++ implements the proposed correction in DR 457. */
4145 bool
4146 decl_constant_var_p (tree decl)
4148 if (!decl_maybe_constant_var_p (decl))
4149 return false;
4151 /* We don't know if a template static data member is initialized with
4152 a constant expression until we instantiate its initializer. Even
4153 in the case of a constexpr variable, we can't treat it as a
4154 constant until its initializer is complete in case it's used in
4155 its own initializer. */
4156 maybe_instantiate_decl (decl);
4157 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4160 /* Returns true if DECL could be a symbolic constant variable, depending on
4161 its initializer. */
4163 bool
4164 decl_maybe_constant_var_p (tree decl)
4166 tree type = TREE_TYPE (decl);
4167 if (!VAR_P (decl))
4168 return false;
4169 if (DECL_DECLARED_CONSTEXPR_P (decl))
4170 return true;
4171 if (DECL_HAS_VALUE_EXPR_P (decl))
4172 /* A proxy isn't constant. */
4173 return false;
4174 if (TREE_CODE (type) == REFERENCE_TYPE)
4175 /* References can be constant. */;
4176 else if (CP_TYPE_CONST_NON_VOLATILE_P (type)
4177 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4178 /* And const integers. */;
4179 else
4180 return false;
4182 if (DECL_INITIAL (decl)
4183 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
4184 /* We know the initializer, and it isn't constant. */
4185 return false;
4186 else
4187 return true;
4190 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4191 called from grokfndecl and grokvardecl; in all modes it is called from
4192 cp_write_global_declarations. */
4194 void
4195 no_linkage_error (tree decl)
4197 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4198 /* In C++11 it's ok if the decl is defined. */
4199 return;
4200 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4201 if (t == NULL_TREE)
4202 /* The type that got us on no_linkage_decls must have gotten a name for
4203 linkage purposes. */;
4204 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4205 /* The type might end up having a typedef name for linkage purposes. */
4206 vec_safe_push (no_linkage_decls, decl);
4207 else if (TYPE_UNNAMED_P (t))
4209 bool d = false;
4210 if (cxx_dialect >= cxx11)
4211 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4212 "unnamed type, is used but never defined", decl);
4213 else if (DECL_EXTERN_C_P (decl))
4214 /* Allow this; it's pretty common in C. */;
4215 else if (VAR_P (decl))
4216 /* DRs 132, 319 and 389 seem to indicate types with
4217 no linkage can only be used to declare extern "C"
4218 entities. Since it's not always an error in the
4219 ISO C++ 90 Standard, we only issue a warning. */
4220 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "unnamed type "
4221 "with no linkage used to declare variable %q#D with "
4222 "linkage", decl);
4223 else
4224 d = permerror (DECL_SOURCE_LOCATION (decl), "unnamed type with no "
4225 "linkage used to declare function %q#D with linkage",
4226 decl);
4227 if (d && is_typedef_decl (TYPE_NAME (t)))
4228 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4229 "to the unqualified type, so it is not used for linkage",
4230 TYPE_NAME (t));
4232 else if (cxx_dialect >= cxx11)
4234 if (VAR_P (decl) || !DECL_PURE_VIRTUAL_P (decl))
4235 permerror (DECL_SOURCE_LOCATION (decl),
4236 "%q#D, declared using local type "
4237 "%qT, is used but never defined", decl, t);
4239 else if (VAR_P (decl))
4240 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4241 "used to declare variable %q#D with linkage", t, decl);
4242 else
4243 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4244 "to declare function %q#D with linkage", t, decl);
4247 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4249 static void
4250 collect_all_refs (const char *source_file)
4252 collect_ada_namespace (global_namespace, source_file);
4255 /* Clear DECL_EXTERNAL for NODE. */
4257 static bool
4258 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4260 DECL_EXTERNAL (node->decl) = 0;
4261 return false;
4264 /* Build up the function to run dynamic initializers for thread_local
4265 variables in this translation unit and alias the init functions for the
4266 individual variables to it. */
4268 static void
4269 handle_tls_init (void)
4271 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4272 if (vars == NULL_TREE)
4273 return;
4275 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4277 write_out_vars (vars);
4279 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4280 boolean_type_node);
4281 TREE_PUBLIC (guard) = false;
4282 TREE_STATIC (guard) = true;
4283 DECL_ARTIFICIAL (guard) = true;
4284 DECL_IGNORED_P (guard) = true;
4285 TREE_USED (guard) = true;
4286 CP_DECL_THREAD_LOCAL_P (guard) = true;
4287 set_decl_tls_model (guard, decl_default_tls_model (guard));
4288 pushdecl_top_level_and_finish (guard, NULL_TREE);
4290 tree fn = get_local_tls_init_fn ();
4291 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4292 tree body = begin_function_body ();
4293 tree if_stmt = begin_if_stmt ();
4294 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4295 tf_warning_or_error);
4296 finish_if_stmt_cond (cond, if_stmt);
4297 finish_expr_stmt (cp_build_modify_expr (loc, guard, NOP_EXPR,
4298 boolean_true_node,
4299 tf_warning_or_error));
4300 for (; vars; vars = TREE_CHAIN (vars))
4302 tree var = TREE_VALUE (vars);
4303 tree init = TREE_PURPOSE (vars);
4304 one_static_initialization_or_destruction (var, init, true);
4306 /* Output init aliases even with -fno-extern-tls-init. */
4307 if (TARGET_SUPPORTS_ALIASES && TREE_PUBLIC (var))
4309 tree single_init_fn = get_tls_init_fn (var);
4310 if (single_init_fn == NULL_TREE)
4311 continue;
4312 cgraph_node *alias
4313 = cgraph_node::get_create (fn)->create_same_body_alias
4314 (single_init_fn, fn);
4315 gcc_assert (alias != NULL);
4319 finish_then_clause (if_stmt);
4320 finish_if_stmt (if_stmt);
4321 finish_function_body (body);
4322 expand_or_defer_fn (finish_function (/*inline_p=*/false));
4325 /* We're at the end of compilation, so generate any mangling aliases that
4326 we've been saving up, if DECL is going to be output and ID2 isn't
4327 already taken by another declaration. */
4329 static void
4330 generate_mangling_alias (tree decl, tree id2)
4332 struct cgraph_node *n = NULL;
4334 if (TREE_CODE (decl) == FUNCTION_DECL)
4336 n = cgraph_node::get (decl);
4337 if (!n)
4338 /* Don't create an alias to an unreferenced function. */
4339 return;
4342 tree *slot
4343 = mangled_decls->find_slot_with_hash (id2, IDENTIFIER_HASH_VALUE (id2),
4344 INSERT);
4346 /* If there's a declaration already using this mangled name,
4347 don't create a compatibility alias that conflicts. */
4348 if (*slot)
4349 return;
4351 tree alias = make_alias_for (decl, id2);
4352 *slot = alias;
4354 DECL_IGNORED_P (alias) = 1;
4355 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
4356 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
4357 if (vague_linkage_p (decl))
4358 DECL_WEAK (alias) = 1;
4360 if (n)
4361 n->create_same_body_alias (alias, decl);
4362 else
4363 varpool_node::create_extra_name_alias (alias, decl);
4366 /* Note that we might want to emit an alias with the symbol ID2 for DECL at
4367 the end of translation, for compatibility across bugs in the mangling
4368 implementation. */
4370 void
4371 note_mangling_alias (tree decl, tree id2)
4373 if (TARGET_SUPPORTS_ALIASES)
4375 if (!defer_mangling_aliases)
4376 generate_mangling_alias (decl, id2);
4377 else
4379 vec_safe_push (mangling_aliases, decl);
4380 vec_safe_push (mangling_aliases, id2);
4385 /* Emit all mangling aliases that were deferred up to this point. */
4387 void
4388 generate_mangling_aliases ()
4390 while (!vec_safe_is_empty (mangling_aliases))
4392 tree id2 = mangling_aliases->pop();
4393 tree decl = mangling_aliases->pop();
4394 generate_mangling_alias (decl, id2);
4396 defer_mangling_aliases = false;
4399 /* Record a mangling of DECL, whose DECL_ASSEMBLER_NAME has just been
4400 set. NEED_WARNING is true if we must warn about collisions. We do
4401 this to spot changes in mangling that may require compatibility
4402 aliases. */
4404 void
4405 record_mangling (tree decl, bool need_warning)
4407 if (!mangled_decls)
4408 mangled_decls = hash_table<mangled_decl_hash>::create_ggc (499);
4410 gcc_checking_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
4411 tree id = DECL_ASSEMBLER_NAME_RAW (decl);
4412 tree *slot
4413 = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id),
4414 INSERT);
4416 /* If this is already an alias, remove the alias, because the real
4417 decl takes precedence. */
4418 if (*slot && DECL_ARTIFICIAL (*slot) && DECL_IGNORED_P (*slot))
4419 if (symtab_node *n = symtab_node::get (*slot))
4420 if (n->cpp_implicit_alias)
4422 n->remove ();
4423 *slot = NULL_TREE;
4426 if (!*slot)
4427 *slot = decl;
4428 else if (need_warning)
4430 error_at (DECL_SOURCE_LOCATION (decl),
4431 "mangling of %q#D as %qE conflicts with a previous mangle",
4432 decl, id);
4433 inform (DECL_SOURCE_LOCATION (*slot),
4434 "previous mangling %q#D", *slot);
4435 inform (DECL_SOURCE_LOCATION (decl),
4436 "a later -fabi-version= (or =0)"
4437 " avoids this error with a change in mangling");
4438 *slot = decl;
4442 /* The entire file is now complete. If requested, dump everything
4443 to a file. */
4445 static void
4446 dump_tu (void)
4448 dump_flags_t flags;
4449 if (FILE *stream = dump_begin (raw_dump_id, &flags))
4451 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4452 dump_end (raw_dump_id, stream);
4456 static location_t locus_at_end_of_parsing;
4458 /* Check the deallocation functions for CODE to see if we want to warn that
4459 only one was defined. */
4461 static void
4462 maybe_warn_sized_delete (enum tree_code code)
4464 tree sized = NULL_TREE;
4465 tree unsized = NULL_TREE;
4467 for (ovl_iterator iter (get_global_binding (ovl_op_identifier (false, code)));
4468 iter; ++iter)
4470 tree fn = *iter;
4471 /* We're only interested in usual deallocation functions. */
4472 if (!usual_deallocation_fn_p (fn))
4473 continue;
4474 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
4475 unsized = fn;
4476 else
4477 sized = fn;
4479 if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
4480 warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
4481 "the program should also define %qD", sized);
4482 else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
4483 warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
4484 "the program should also define %qD", unsized);
4487 /* Check the global deallocation functions to see if we want to warn about
4488 defining unsized without sized (or vice versa). */
4490 static void
4491 maybe_warn_sized_delete ()
4493 if (!flag_sized_deallocation || !warn_sized_deallocation)
4494 return;
4495 maybe_warn_sized_delete (DELETE_EXPR);
4496 maybe_warn_sized_delete (VEC_DELETE_EXPR);
4499 /* Earlier we left PTRMEM_CST in variable initializers alone so that we could
4500 look them up when evaluating non-type template parameters. Now we need to
4501 lower them to something the back end can understand. */
4503 static void
4504 lower_var_init ()
4506 varpool_node *node;
4507 FOR_EACH_VARIABLE (node)
4509 tree d = node->decl;
4510 if (tree init = DECL_INITIAL (d))
4511 DECL_INITIAL (d) = cplus_expand_constant (init);
4515 /* This routine is called at the end of compilation.
4516 Its job is to create all the code needed to initialize and
4517 destroy the global aggregates. We do the destruction
4518 first, since that way we only need to reverse the decls once. */
4520 void
4521 c_parse_final_cleanups (void)
4523 tree vars;
4524 bool reconsider;
4525 size_t i;
4526 unsigned ssdf_count = 0;
4527 int retries = 0;
4528 tree decl;
4530 locus_at_end_of_parsing = input_location;
4531 at_eof = 1;
4533 /* Bad parse errors. Just forget about it. */
4534 if (! global_bindings_p () || current_class_type
4535 || !vec_safe_is_empty (decl_namespace_list))
4536 return;
4538 /* This is the point to write out a PCH if we're doing that.
4539 In that case we do not want to do anything else. */
4540 if (pch_file)
4542 /* Mangle all symbols at PCH creation time. */
4543 symtab_node *node;
4544 FOR_EACH_SYMBOL (node)
4545 if (! is_a <varpool_node *> (node)
4546 || ! DECL_HARD_REGISTER (node->decl))
4547 DECL_ASSEMBLER_NAME (node->decl);
4548 c_common_write_pch ();
4549 dump_tu ();
4550 /* Ensure even the callers don't try to finalize the CU. */
4551 flag_syntax_only = 1;
4552 return;
4555 timevar_stop (TV_PHASE_PARSING);
4556 timevar_start (TV_PHASE_DEFERRED);
4558 symtab->process_same_body_aliases ();
4560 /* Handle -fdump-ada-spec[-slim] */
4561 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4563 if (flag_dump_ada_spec_slim)
4564 collect_source_ref (main_input_filename);
4565 else
4566 collect_source_refs (global_namespace);
4568 dump_ada_specs (collect_all_refs, cpp_check);
4571 /* FIXME - huh? was input_line -= 1;*/
4573 /* We now have to write out all the stuff we put off writing out.
4574 These include:
4576 o Template specializations that we have not yet instantiated,
4577 but which are needed.
4578 o Initialization and destruction for non-local objects with
4579 static storage duration. (Local objects with static storage
4580 duration are initialized when their scope is first entered,
4581 and are cleaned up via atexit.)
4582 o Virtual function tables.
4584 All of these may cause others to be needed. For example,
4585 instantiating one function may cause another to be needed, and
4586 generating the initializer for an object may cause templates to be
4587 instantiated, etc., etc. */
4589 emit_support_tinfos ();
4593 tree t;
4594 tree decl;
4596 reconsider = false;
4598 /* If there are templates that we've put off instantiating, do
4599 them now. */
4600 instantiate_pending_templates (retries);
4601 ggc_collect ();
4603 /* Write out virtual tables as required. Writing out the
4604 virtual table for a template class may cause the
4605 instantiation of members of that class. If we write out
4606 vtables then we remove the class from our list so we don't
4607 have to look at it again. */
4608 for (i = keyed_classes->length ();
4609 keyed_classes->iterate (--i, &t);)
4610 if (maybe_emit_vtables (t))
4612 reconsider = true;
4613 keyed_classes->unordered_remove (i);
4616 /* Write out needed type info variables. We have to be careful
4617 looping through unemitted decls, because emit_tinfo_decl may
4618 cause other variables to be needed. New elements will be
4619 appended, and we remove from the vector those that actually
4620 get emitted. */
4621 for (i = unemitted_tinfo_decls->length ();
4622 unemitted_tinfo_decls->iterate (--i, &t);)
4623 if (emit_tinfo_decl (t))
4625 reconsider = true;
4626 unemitted_tinfo_decls->unordered_remove (i);
4629 /* The list of objects with static storage duration is built up
4630 in reverse order. We clear STATIC_AGGREGATES so that any new
4631 aggregates added during the initialization of these will be
4632 initialized in the correct order when we next come around the
4633 loop. */
4634 vars = prune_vars_needing_no_initialization (&static_aggregates);
4636 if (vars)
4638 /* We need to start a new initialization function each time
4639 through the loop. That's because we need to know which
4640 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4641 isn't computed until a function is finished, and written
4642 out. That's a deficiency in the back end. When this is
4643 fixed, these initialization functions could all become
4644 inline, with resulting performance improvements. */
4645 tree ssdf_body;
4647 /* Set the line and file, so that it is obviously not from
4648 the source file. */
4649 input_location = locus_at_end_of_parsing;
4650 ssdf_body = start_static_storage_duration_function (ssdf_count);
4652 /* Make sure the back end knows about all the variables. */
4653 write_out_vars (vars);
4655 /* First generate code to do all the initializations. */
4656 if (vars)
4657 do_static_initialization_or_destruction (vars, /*initp=*/true);
4659 /* Then, generate code to do all the destructions. Do these
4660 in reverse order so that the most recently constructed
4661 variable is the first destroyed. If we're using
4662 __cxa_atexit, then we don't need to do this; functions
4663 were registered at initialization time to destroy the
4664 local statics. */
4665 if (!flag_use_cxa_atexit && vars)
4667 vars = nreverse (vars);
4668 do_static_initialization_or_destruction (vars, /*initp=*/false);
4670 else
4671 vars = NULL_TREE;
4673 /* Finish up the static storage duration function for this
4674 round. */
4675 input_location = locus_at_end_of_parsing;
4676 finish_static_storage_duration_function (ssdf_body);
4678 /* All those initializations and finalizations might cause
4679 us to need more inline functions, more template
4680 instantiations, etc. */
4681 reconsider = true;
4682 ssdf_count++;
4683 /* ??? was: locus_at_end_of_parsing.line++; */
4686 /* Now do the same for thread_local variables. */
4687 handle_tls_init ();
4689 /* Go through the set of inline functions whose bodies have not
4690 been emitted yet. If out-of-line copies of these functions
4691 are required, emit them. */
4692 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4694 /* Does it need synthesizing? */
4695 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4696 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4698 /* Even though we're already at the top-level, we push
4699 there again. That way, when we pop back a few lines
4700 hence, all of our state is restored. Otherwise,
4701 finish_function doesn't clean things up, and we end
4702 up with CURRENT_FUNCTION_DECL set. */
4703 push_to_top_level ();
4704 /* The decl's location will mark where it was first
4705 needed. Save that so synthesize method can indicate
4706 where it was needed from, in case of error */
4707 input_location = DECL_SOURCE_LOCATION (decl);
4708 synthesize_method (decl);
4709 pop_from_top_level ();
4710 reconsider = true;
4713 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4714 generate_tls_wrapper (decl);
4716 if (!DECL_SAVED_TREE (decl))
4717 continue;
4719 cgraph_node *node = cgraph_node::get_create (decl);
4721 /* We lie to the back end, pretending that some functions
4722 are not defined when they really are. This keeps these
4723 functions from being put out unnecessarily. But, we must
4724 stop lying when the functions are referenced, or if they
4725 are not comdat since they need to be put out now. If
4726 DECL_INTERFACE_KNOWN, then we have already set
4727 DECL_EXTERNAL appropriately, so there's no need to check
4728 again, and we do not want to clear DECL_EXTERNAL if a
4729 previous call to import_export_decl set it.
4731 This is done in a separate for cycle, because if some
4732 deferred function is contained in another deferred
4733 function later in deferred_fns varray,
4734 rest_of_compilation would skip this function and we
4735 really cannot expand the same function twice. */
4736 import_export_decl (decl);
4737 if (DECL_NOT_REALLY_EXTERN (decl)
4738 && DECL_INITIAL (decl)
4739 && decl_needed_p (decl))
4741 if (node->cpp_implicit_alias)
4742 node = node->get_alias_target ();
4744 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
4745 NULL, true);
4746 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4747 group, we need to mark all symbols in the same comdat group
4748 that way. */
4749 if (node->same_comdat_group)
4750 for (cgraph_node *next
4751 = dyn_cast<cgraph_node *> (node->same_comdat_group);
4752 next != node;
4753 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
4754 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
4755 NULL, true);
4758 /* If we're going to need to write this function out, and
4759 there's already a body for it, create RTL for it now.
4760 (There might be no body if this is a method we haven't
4761 gotten around to synthesizing yet.) */
4762 if (!DECL_EXTERNAL (decl)
4763 && decl_needed_p (decl)
4764 && !TREE_ASM_WRITTEN (decl)
4765 && !node->definition)
4767 /* We will output the function; no longer consider it in this
4768 loop. */
4769 DECL_DEFER_OUTPUT (decl) = 0;
4770 /* Generate RTL for this function now that we know we
4771 need it. */
4772 expand_or_defer_fn (decl);
4773 /* If we're compiling -fsyntax-only pretend that this
4774 function has been written out so that we don't try to
4775 expand it again. */
4776 if (flag_syntax_only)
4777 TREE_ASM_WRITTEN (decl) = 1;
4778 reconsider = true;
4782 if (wrapup_namespace_globals ())
4783 reconsider = true;
4785 /* Static data members are just like namespace-scope globals. */
4786 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4788 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4789 /* Don't write it out if we haven't seen a definition. */
4790 || (DECL_IN_AGGR_P (decl) && !DECL_INLINE_VAR_P (decl)))
4791 continue;
4792 import_export_decl (decl);
4793 /* If this static data member is needed, provide it to the
4794 back end. */
4795 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4796 DECL_EXTERNAL (decl) = 0;
4798 if (vec_safe_length (pending_statics) != 0
4799 && wrapup_global_declarations (pending_statics->address (),
4800 pending_statics->length ()))
4801 reconsider = true;
4803 retries++;
4805 while (reconsider);
4807 lower_var_init ();
4809 generate_mangling_aliases ();
4811 /* All used inline functions must have a definition at this point. */
4812 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4814 if (/* Check online inline functions that were actually used. */
4815 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4816 /* If the definition actually was available here, then the
4817 fact that the function was not defined merely represents
4818 that for some reason (use of a template repository,
4819 #pragma interface, etc.) we decided not to emit the
4820 definition here. */
4821 && !DECL_INITIAL (decl)
4822 /* Don't complain if the template was defined. */
4823 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4824 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4825 (template_for_substitution (decl)))))
4827 warning_at (DECL_SOURCE_LOCATION (decl), 0,
4828 "inline function %qD used but never defined", decl);
4829 /* Avoid a duplicate warning from check_global_declaration. */
4830 TREE_NO_WARNING (decl) = 1;
4834 /* So must decls that use a type with no linkage. */
4835 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4836 no_linkage_error (decl);
4838 maybe_warn_sized_delete ();
4840 /* Then, do the Objective-C stuff. This is where all the
4841 Objective-C module stuff gets generated (symtab,
4842 class/protocol/selector lists etc). This must be done after C++
4843 templates, destructors etc. so that selectors used in C++
4844 templates are properly allocated. */
4845 if (c_dialect_objc ())
4846 objc_write_global_declarations ();
4848 /* We give C linkage to static constructors and destructors. */
4849 push_lang_context (lang_name_c);
4851 /* Generate initialization and destruction functions for all
4852 priorities for which they are required. */
4853 if (priority_info_map)
4854 splay_tree_foreach (priority_info_map,
4855 generate_ctor_and_dtor_functions_for_priority,
4856 /*data=*/&locus_at_end_of_parsing);
4857 else if (c_dialect_objc () && objc_static_init_needed_p ())
4858 /* If this is obj-c++ and we need a static init, call
4859 generate_ctor_or_dtor_function. */
4860 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4861 DEFAULT_INIT_PRIORITY,
4862 &locus_at_end_of_parsing);
4864 /* We're done with the splay-tree now. */
4865 if (priority_info_map)
4866 splay_tree_delete (priority_info_map);
4868 /* Generate any missing aliases. */
4869 maybe_apply_pending_pragma_weaks ();
4871 /* We're done with static constructors, so we can go back to "C++"
4872 linkage now. */
4873 pop_lang_context ();
4875 if (flag_vtable_verify)
4877 vtv_recover_class_info ();
4878 vtv_compute_class_hierarchy_transitive_closure ();
4879 vtv_build_vtable_verify_fndecl ();
4882 perform_deferred_noexcept_checks ();
4884 finish_repo ();
4885 fini_constexpr ();
4887 /* The entire file is now complete. If requested, dump everything
4888 to a file. */
4889 dump_tu ();
4891 if (flag_detailed_statistics)
4893 dump_tree_statistics ();
4894 dump_time_statistics ();
4897 timevar_stop (TV_PHASE_DEFERRED);
4898 timevar_start (TV_PHASE_PARSING);
4900 /* Indicate that we're done with front end processing. */
4901 at_eof = 2;
4904 /* Perform any post compilation-proper cleanups for the C++ front-end.
4905 This should really go away. No front-end should need to do
4906 anything past the compilation process. */
4908 void
4909 cxx_post_compilation_parsing_cleanups (void)
4911 timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS);
4913 if (flag_vtable_verify)
4915 /* Generate the special constructor initialization function that
4916 calls __VLTRegisterPairs, and give it a very high
4917 initialization priority. This must be done after
4918 finalize_compilation_unit so that we have accurate
4919 information about which vtable will actually be emitted. */
4920 vtv_generate_init_routine ();
4923 input_location = locus_at_end_of_parsing;
4925 if (flag_checking)
4926 validate_conversion_obstack ();
4928 timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS);
4931 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4932 function to call in parse-tree form; it has not yet been
4933 semantically analyzed. ARGS are the arguments to the function.
4934 They have already been semantically analyzed. This may change
4935 ARGS. */
4937 tree
4938 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4939 tsubst_flags_t complain)
4941 tree orig_fn;
4942 vec<tree, va_gc> *orig_args = NULL;
4943 tree expr;
4944 tree object;
4946 orig_fn = fn;
4947 object = TREE_OPERAND (fn, 0);
4949 if (processing_template_decl)
4951 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4952 || TREE_CODE (fn) == MEMBER_REF);
4953 if (type_dependent_expression_p (fn)
4954 || any_type_dependent_arguments_p (*args))
4955 return build_min_nt_call_vec (fn, *args);
4957 orig_args = make_tree_vector_copy (*args);
4959 /* Transform the arguments and add the implicit "this"
4960 parameter. That must be done before the FN is transformed
4961 because we depend on the form of FN. */
4962 make_args_non_dependent (*args);
4963 object = build_non_dependent_expr (object);
4964 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4966 if (TREE_CODE (fn) == DOTSTAR_EXPR)
4967 object = cp_build_addr_expr (object, complain);
4968 vec_safe_insert (*args, 0, object);
4970 /* Now that the arguments are done, transform FN. */
4971 fn = build_non_dependent_expr (fn);
4974 /* A qualified name corresponding to a bound pointer-to-member is
4975 represented as an OFFSET_REF:
4977 struct B { void g(); };
4978 void (B::*p)();
4979 void B::g() { (this->*p)(); } */
4980 if (TREE_CODE (fn) == OFFSET_REF)
4982 tree object_addr = cp_build_addr_expr (object, complain);
4983 fn = TREE_OPERAND (fn, 1);
4984 fn = get_member_function_from_ptrfunc (&object_addr, fn,
4985 complain);
4986 vec_safe_insert (*args, 0, object_addr);
4989 if (CLASS_TYPE_P (TREE_TYPE (fn)))
4990 expr = build_op_call (fn, args, complain);
4991 else
4992 expr = cp_build_function_call_vec (fn, args, complain);
4993 if (processing_template_decl && expr != error_mark_node)
4994 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
4996 if (orig_args != NULL)
4997 release_tree_vector (orig_args);
4999 return expr;
5003 void
5004 check_default_args (tree x)
5006 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
5007 bool saw_def = false;
5008 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
5009 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
5011 if (TREE_PURPOSE (arg))
5012 saw_def = true;
5013 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
5015 error ("default argument missing for parameter %P of %q+#D", i, x);
5016 TREE_PURPOSE (arg) = error_mark_node;
5021 /* Return true if function DECL can be inlined. This is used to force
5022 instantiation of methods that might be interesting for inlining. */
5023 bool
5024 possibly_inlined_p (tree decl)
5026 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
5027 if (DECL_UNINLINABLE (decl))
5028 return false;
5029 if (!optimize)
5030 return DECL_DECLARED_INLINE_P (decl);
5031 /* When optimizing, we might inline everything when flatten
5032 attribute or heuristics inlining for size or autoinlining
5033 is used. */
5034 return true;
5037 /* Normally, we can wait until instantiation-time to synthesize DECL.
5038 However, if DECL is a static data member initialized with a constant
5039 or a constexpr function, we need it right now because a reference to
5040 such a data member or a call to such function is not value-dependent.
5041 For a function that uses auto in the return type, we need to instantiate
5042 it to find out its type. For OpenMP user defined reductions, we need
5043 them instantiated for reduction clauses which inline them by hand
5044 directly. */
5046 static void
5047 maybe_instantiate_decl (tree decl)
5049 if (DECL_LANG_SPECIFIC (decl)
5050 && DECL_TEMPLATE_INFO (decl)
5051 && (decl_maybe_constant_var_p (decl)
5052 || (TREE_CODE (decl) == FUNCTION_DECL
5053 && DECL_OMP_DECLARE_REDUCTION_P (decl))
5054 || undeduced_auto_decl (decl))
5055 && !DECL_DECLARED_CONCEPT_P (decl)
5056 && !uses_template_parms (DECL_TI_ARGS (decl)))
5058 /* Instantiating a function will result in garbage collection. We
5059 must treat this situation as if we were within the body of a
5060 function so as to avoid collecting live data only referenced from
5061 the stack (such as overload resolution candidates). */
5062 ++function_depth;
5063 instantiate_decl (decl, /*defer_ok=*/false,
5064 /*expl_inst_class_mem_p=*/false);
5065 --function_depth;
5069 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
5070 If DECL is a specialization or implicitly declared class member,
5071 generate the actual definition. Return false if something goes
5072 wrong, true otherwise. */
5074 bool
5075 mark_used (tree decl, tsubst_flags_t complain)
5077 /* If DECL is a BASELINK for a single function, then treat it just
5078 like the DECL for the function. Otherwise, if the BASELINK is
5079 for an overloaded function, we don't know which function was
5080 actually used until after overload resolution. */
5081 if (BASELINK_P (decl))
5083 decl = BASELINK_FUNCTIONS (decl);
5084 if (really_overloaded_fn (decl))
5085 return true;
5086 decl = OVL_FIRST (decl);
5089 /* Set TREE_USED for the benefit of -Wunused. */
5090 TREE_USED (decl) = 1;
5091 /* And for structured bindings also the underlying decl. */
5092 if (DECL_DECOMPOSITION_P (decl) && DECL_DECOMP_BASE (decl))
5093 TREE_USED (DECL_DECOMP_BASE (decl)) = 1;
5095 if (TREE_CODE (decl) == TEMPLATE_DECL)
5096 return true;
5098 if (DECL_CLONED_FUNCTION_P (decl))
5099 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5101 /* Mark enumeration types as used. */
5102 if (TREE_CODE (decl) == CONST_DECL)
5103 used_types_insert (DECL_CONTEXT (decl));
5105 if (TREE_CODE (decl) == FUNCTION_DECL
5106 && !maybe_instantiate_noexcept (decl, complain))
5107 return false;
5109 if (TREE_CODE (decl) == FUNCTION_DECL
5110 && DECL_DELETED_FN (decl))
5112 if (DECL_ARTIFICIAL (decl)
5113 && DECL_CONV_FN_P (decl)
5114 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
5115 /* We mark a lambda conversion op as deleted if we can't
5116 generate it properly; see maybe_add_lambda_conv_op. */
5117 sorry ("converting lambda that uses %<...%> to function pointer");
5118 else if (complain & tf_error)
5120 error ("use of deleted function %qD", decl);
5121 if (!maybe_explain_implicit_delete (decl))
5122 inform (DECL_SOURCE_LOCATION (decl), "declared here");
5124 return false;
5127 if (TREE_DEPRECATED (decl) && (complain & tf_warning)
5128 && deprecated_state != DEPRECATED_SUPPRESS)
5129 warn_deprecated_use (decl, NULL_TREE);
5131 /* We can only check DECL_ODR_USED on variables or functions with
5132 DECL_LANG_SPECIFIC set, and these are also the only decls that we
5133 might need special handling for. */
5134 if (!VAR_OR_FUNCTION_DECL_P (decl)
5135 || DECL_LANG_SPECIFIC (decl) == NULL
5136 || DECL_THUNK_P (decl))
5138 if (!processing_template_decl
5139 && !require_deduced_type (decl, complain))
5140 return false;
5141 return true;
5144 /* We only want to do this processing once. We don't need to keep trying
5145 to instantiate inline templates, because unit-at-a-time will make sure
5146 we get them compiled before functions that want to inline them. */
5147 if (DECL_ODR_USED (decl))
5148 return true;
5150 /* Normally, we can wait until instantiation-time to synthesize DECL.
5151 However, if DECL is a static data member initialized with a constant
5152 or a constexpr function, we need it right now because a reference to
5153 such a data member or a call to such function is not value-dependent.
5154 For a function that uses auto in the return type, we need to instantiate
5155 it to find out its type. For OpenMP user defined reductions, we need
5156 them instantiated for reduction clauses which inline them by hand
5157 directly. */
5158 maybe_instantiate_decl (decl);
5160 if (processing_template_decl || in_template_function ())
5161 return true;
5163 /* Check this too in case we're within instantiate_non_dependent_expr. */
5164 if (DECL_TEMPLATE_INFO (decl)
5165 && uses_template_parms (DECL_TI_ARGS (decl)))
5166 return true;
5168 if (!require_deduced_type (decl, complain))
5169 return false;
5171 if (builtin_pack_fn_p (decl))
5173 error ("use of built-in parameter pack %qD outside of a template",
5174 DECL_NAME (decl));
5175 return false;
5178 /* If we don't need a value, then we don't need to synthesize DECL. */
5179 if (cp_unevaluated_operand || in_discarded_stmt)
5180 return true;
5182 DECL_ODR_USED (decl) = 1;
5183 if (DECL_CLONED_FUNCTION_P (decl))
5184 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5186 /* DR 757: A type without linkage shall not be used as the type of a
5187 variable or function with linkage, unless
5188 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5189 o the variable or function is not used (3.2 [basic.def.odr]) or is
5190 defined in the same translation unit. */
5191 if (cxx_dialect > cxx98
5192 && decl_linkage (decl) != lk_none
5193 && !DECL_EXTERN_C_P (decl)
5194 && !DECL_ARTIFICIAL (decl)
5195 && !decl_defined_p (decl)
5196 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
5198 if (is_local_extern (decl))
5199 /* There's no way to define a local extern, and adding it to
5200 the vector interferes with GC, so give an error now. */
5201 no_linkage_error (decl);
5202 else
5203 vec_safe_push (no_linkage_decls, decl);
5206 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
5207 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
5208 /* Remember it, so we can check it was defined. */
5209 note_vague_linkage_fn (decl);
5211 /* Is it a synthesized method that needs to be synthesized? */
5212 if (TREE_CODE (decl) == FUNCTION_DECL
5213 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5214 && DECL_DEFAULTED_FN (decl)
5215 /* A function defaulted outside the class is synthesized either by
5216 cp_finish_decl or instantiate_decl. */
5217 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
5218 && ! DECL_INITIAL (decl))
5220 /* Defer virtual destructors so that thunks get the right
5221 linkage. */
5222 if (DECL_VIRTUAL_P (decl) && !at_eof)
5224 note_vague_linkage_fn (decl);
5225 return true;
5228 /* Remember the current location for a function we will end up
5229 synthesizing. Then we can inform the user where it was
5230 required in the case of error. */
5231 DECL_SOURCE_LOCATION (decl) = input_location;
5233 /* Synthesizing an implicitly defined member function will result in
5234 garbage collection. We must treat this situation as if we were
5235 within the body of a function so as to avoid collecting live data
5236 on the stack (such as overload resolution candidates).
5238 We could just let cp_write_global_declarations handle synthesizing
5239 this function by adding it to deferred_fns, but doing
5240 it at the use site produces better error messages. */
5241 ++function_depth;
5242 synthesize_method (decl);
5243 --function_depth;
5244 /* If this is a synthesized method we don't need to
5245 do the instantiation test below. */
5247 else if (VAR_OR_FUNCTION_DECL_P (decl)
5248 && DECL_TEMPLATE_INFO (decl)
5249 && !DECL_DECLARED_CONCEPT_P (decl)
5250 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5251 || always_instantiate_p (decl)))
5252 /* If this is a function or variable that is an instance of some
5253 template, we now know that we will need to actually do the
5254 instantiation. We check that DECL is not an explicit
5255 instantiation because that is not checked in instantiate_decl.
5257 We put off instantiating functions in order to improve compile
5258 times. Maintaining a stack of active functions is expensive,
5259 and the inliner knows to instantiate any functions it might
5260 need. Therefore, we always try to defer instantiation. */
5262 ++function_depth;
5263 instantiate_decl (decl, /*defer_ok=*/true,
5264 /*expl_inst_class_mem_p=*/false);
5265 --function_depth;
5268 return true;
5271 bool
5272 mark_used (tree decl)
5274 return mark_used (decl, tf_warning_or_error);
5277 tree
5278 vtv_start_verification_constructor_init_function (void)
5280 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5283 tree
5284 vtv_finish_verification_constructor_init_function (tree function_body)
5286 tree fn;
5288 finish_compound_stmt (function_body);
5289 fn = finish_function (/*inline_p=*/false);
5290 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5291 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5293 return fn;
5296 #include "gt-cp-decl2.h"