PR c++/56973, DR 696 - capture constant variables only as needed.
[official-gcc.git] / gcc / cp / decl2.c
blob29d6c59f549a0292ffa94f04308f065b462d132d
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2017 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* Process declarations and symbol lookup for C++ front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "memmodel.h"
33 #include "target.h"
34 #include "cp-tree.h"
35 #include "c-family/c-common.h"
36 #include "timevar.h"
37 #include "stringpool.h"
38 #include "cgraph.h"
39 #include "varasm.h"
40 #include "attribs.h"
41 #include "stor-layout.h"
42 #include "calls.h"
43 #include "decl.h"
44 #include "toplev.h"
45 #include "c-family/c-objc.h"
46 #include "c-family/c-pragma.h"
47 #include "dumpfile.h"
48 #include "intl.h"
49 #include "c-family/c-ada-spec.h"
50 #include "asan.h"
52 /* Id for dumping the raw trees. */
53 int raw_dump_id;
55 extern cpp_reader *parse_in;
57 /* This structure contains information about the initializations
58 and/or destructions required for a particular priority level. */
59 typedef struct priority_info_s {
60 /* Nonzero if there have been any initializations at this priority
61 throughout the translation unit. */
62 int initializations_p;
63 /* Nonzero if there have been any destructions at this priority
64 throughout the translation unit. */
65 int destructions_p;
66 } *priority_info;
68 static void mark_vtable_entries (tree);
69 static bool maybe_emit_vtables (tree);
70 static tree start_objects (int, int);
71 static void finish_objects (int, int, tree);
72 static tree start_static_storage_duration_function (unsigned);
73 static void finish_static_storage_duration_function (tree);
74 static priority_info get_priority_info (int);
75 static void do_static_initialization_or_destruction (tree, bool);
76 static void one_static_initialization_or_destruction (tree, tree, bool);
77 static void generate_ctor_or_dtor_function (bool, int, location_t *);
78 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
79 void *);
80 static tree prune_vars_needing_no_initialization (tree *);
81 static void write_out_vars (tree);
82 static void import_export_class (tree);
83 static tree get_guard_bits (tree);
84 static void determine_visibility_from_class (tree, tree);
85 static bool determine_hidden_inline (tree);
86 static void maybe_instantiate_decl (tree);
88 /* A list of static class variables. This is needed, because a
89 static class variable can be declared inside the class without
90 an initializer, and then initialized, statically, outside the class. */
91 static GTY(()) vec<tree, va_gc> *pending_statics;
93 /* A list of functions which were declared inline, but which we
94 may need to emit outline anyway. */
95 static GTY(()) vec<tree, va_gc> *deferred_fns;
97 /* A list of decls that use types with no linkage, which we need to make
98 sure are defined. */
99 static GTY(()) vec<tree, va_gc> *no_linkage_decls;
101 /* A vector of alternating decls and identifiers, where the latter
102 is to be an alias for the former if the former is defined. */
103 static GTY(()) vec<tree, va_gc> *mangling_aliases;
105 /* Nonzero if we're done parsing and into end-of-file activities. */
107 int at_eof;
109 /* True if note_mangling_alias should enqueue mangling aliases for
110 later generation, rather than emitting them right away. */
112 bool defer_mangling_aliases = true;
115 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
116 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
117 that apply to the function). */
119 tree
120 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
121 cp_ref_qualifier rqual)
123 tree raises;
124 tree attrs;
125 int type_quals;
126 bool late_return_type_p;
128 if (fntype == error_mark_node || ctype == error_mark_node)
129 return error_mark_node;
131 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
132 || TREE_CODE (fntype) == METHOD_TYPE);
134 type_quals = quals & ~TYPE_QUAL_RESTRICT;
135 ctype = cp_build_qualified_type (ctype, type_quals);
136 raises = TYPE_RAISES_EXCEPTIONS (fntype);
137 attrs = TYPE_ATTRIBUTES (fntype);
138 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
139 fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
140 (TREE_CODE (fntype) == METHOD_TYPE
141 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
142 : TYPE_ARG_TYPES (fntype)));
143 if (attrs)
144 fntype = cp_build_type_attribute_variant (fntype, attrs);
145 if (rqual)
146 fntype = build_ref_qualified_type (fntype, rqual);
147 if (raises)
148 fntype = build_exception_variant (fntype, raises);
149 if (late_return_type_p)
150 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
152 return fntype;
155 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
156 return type changed to NEW_RET. */
158 tree
159 change_return_type (tree new_ret, tree fntype)
161 tree newtype;
162 tree args = TYPE_ARG_TYPES (fntype);
163 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
164 tree attrs = TYPE_ATTRIBUTES (fntype);
165 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
167 if (new_ret == error_mark_node)
168 return fntype;
170 if (same_type_p (new_ret, TREE_TYPE (fntype)))
171 return fntype;
173 if (TREE_CODE (fntype) == FUNCTION_TYPE)
175 newtype = build_function_type (new_ret, args);
176 newtype = apply_memfn_quals (newtype,
177 type_memfn_quals (fntype),
178 type_memfn_rqual (fntype));
180 else
181 newtype = build_method_type_directly
182 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
183 if (FUNCTION_REF_QUALIFIED (fntype))
184 newtype = build_ref_qualified_type (newtype, type_memfn_rqual (fntype));
185 if (raises)
186 newtype = build_exception_variant (newtype, raises);
187 if (attrs)
188 newtype = cp_build_type_attribute_variant (newtype, attrs);
189 if (late_return_type_p)
190 TYPE_HAS_LATE_RETURN_TYPE (newtype) = 1;
192 return newtype;
195 /* Build a PARM_DECL of FN with NAME and TYPE, and set DECL_ARG_TYPE
196 appropriately. */
198 tree
199 cp_build_parm_decl (tree fn, tree name, tree type)
201 tree parm = build_decl (input_location,
202 PARM_DECL, name, type);
203 DECL_CONTEXT (parm) = fn;
205 /* DECL_ARG_TYPE is only used by the back end and the back end never
206 sees templates. */
207 if (!processing_template_decl)
208 DECL_ARG_TYPE (parm) = type_passed_as (type);
210 return parm;
213 /* Returns a PARM_DECL of FN for a parameter of the indicated TYPE, with the
214 indicated NAME. */
216 tree
217 build_artificial_parm (tree fn, tree name, tree type)
219 tree parm = cp_build_parm_decl (fn, name, type);
220 DECL_ARTIFICIAL (parm) = 1;
221 /* All our artificial parms are implicitly `const'; they cannot be
222 assigned to. */
223 TREE_READONLY (parm) = 1;
224 return parm;
227 /* Constructors for types with virtual baseclasses need an "in-charge" flag
228 saying whether this constructor is responsible for initialization of
229 virtual baseclasses or not. All destructors also need this "in-charge"
230 flag, which additionally determines whether or not the destructor should
231 free the memory for the object.
233 This function adds the "in-charge" flag to member function FN if
234 appropriate. It is called from grokclassfn and tsubst.
235 FN must be either a constructor or destructor.
237 The in-charge flag follows the 'this' parameter, and is followed by the
238 VTT parm (if any), then the user-written parms. */
240 void
241 maybe_retrofit_in_chrg (tree fn)
243 tree basetype, arg_types, parms, parm, fntype;
245 /* If we've already add the in-charge parameter don't do it again. */
246 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
247 return;
249 /* When processing templates we can't know, in general, whether or
250 not we're going to have virtual baseclasses. */
251 if (processing_template_decl)
252 return;
254 /* We don't need an in-charge parameter for constructors that don't
255 have virtual bases. */
256 if (DECL_CONSTRUCTOR_P (fn)
257 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
258 return;
260 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
261 basetype = TREE_TYPE (TREE_VALUE (arg_types));
262 arg_types = TREE_CHAIN (arg_types);
264 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
266 /* If this is a subobject constructor or destructor, our caller will
267 pass us a pointer to our VTT. */
268 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
270 parm = build_artificial_parm (fn, vtt_parm_identifier, vtt_parm_type);
272 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
273 DECL_CHAIN (parm) = parms;
274 parms = parm;
276 /* ...and then to TYPE_ARG_TYPES. */
277 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
279 DECL_HAS_VTT_PARM_P (fn) = 1;
282 /* Then add the in-charge parm (before the VTT parm). */
283 parm = build_artificial_parm (fn, in_charge_identifier, integer_type_node);
284 DECL_CHAIN (parm) = parms;
285 parms = parm;
286 arg_types = hash_tree_chain (integer_type_node, arg_types);
288 /* Insert our new parameter(s) into the list. */
289 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
291 /* And rebuild the function type. */
292 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
293 arg_types);
294 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
295 fntype = build_exception_variant (fntype,
296 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
297 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
298 fntype = (cp_build_type_attribute_variant
299 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
300 TREE_TYPE (fn) = fntype;
302 /* Now we've got the in-charge parameter. */
303 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
306 /* Classes overload their constituent function names automatically.
307 When a function name is declared in a record structure,
308 its name is changed to it overloaded name. Since names for
309 constructors and destructors can conflict, we place a leading
310 '$' for destructors.
312 CNAME is the name of the class we are grokking for.
314 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
316 FLAGS contains bits saying what's special about today's
317 arguments. DTOR_FLAG == DESTRUCTOR.
319 If FUNCTION is a destructor, then we must add the `auto-delete' field
320 as a second parameter. There is some hair associated with the fact
321 that we must "declare" this variable in the manner consistent with the
322 way the rest of the arguments were declared.
324 QUALS are the qualifiers for the this pointer. */
326 void
327 grokclassfn (tree ctype, tree function, enum overload_flags flags)
329 tree fn_name = DECL_NAME (function);
331 /* Even within an `extern "C"' block, members get C++ linkage. See
332 [dcl.link] for details. */
333 SET_DECL_LANGUAGE (function, lang_cplusplus);
335 if (fn_name == NULL_TREE)
337 error ("name missing for member function");
338 fn_name = get_identifier ("<anonymous>");
339 DECL_NAME (function) = fn_name;
342 DECL_CONTEXT (function) = ctype;
344 if (flags == DTOR_FLAG)
345 DECL_CXX_DESTRUCTOR_P (function) = 1;
347 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
348 maybe_retrofit_in_chrg (function);
351 /* Create an ARRAY_REF, checking for the user doing things backwards
352 along the way. DECLTYPE_P is for N3276, as in the parser. */
354 tree
355 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
356 bool decltype_p)
358 tree type;
359 tree expr;
360 tree orig_array_expr = array_expr;
361 tree orig_index_exp = index_exp;
362 tree overload = NULL_TREE;
364 if (error_operand_p (array_expr) || error_operand_p (index_exp))
365 return error_mark_node;
367 if (processing_template_decl)
369 if (type_dependent_expression_p (array_expr)
370 || type_dependent_expression_p (index_exp))
371 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
372 NULL_TREE, NULL_TREE);
373 array_expr = build_non_dependent_expr (array_expr);
374 index_exp = build_non_dependent_expr (index_exp);
377 type = TREE_TYPE (array_expr);
378 gcc_assert (type);
379 type = non_reference (type);
381 /* If they have an `operator[]', use that. */
382 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
384 tsubst_flags_t complain = tf_warning_or_error;
385 if (decltype_p)
386 complain |= tf_decltype;
387 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
388 index_exp, NULL_TREE, &overload, complain);
390 else
392 tree p1, p2, i1, i2;
394 /* Otherwise, create an ARRAY_REF for a pointer or array type.
395 It is a little-known fact that, if `a' is an array and `i' is
396 an int, you can write `i[a]', which means the same thing as
397 `a[i]'. */
398 if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
399 p1 = array_expr;
400 else
401 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
403 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
404 p2 = index_exp;
405 else
406 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
408 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
409 false);
410 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
411 false);
413 if ((p1 && i2) && (i1 && p2))
414 error ("ambiguous conversion for array subscript");
416 if (p1 && i2)
417 array_expr = p1, index_exp = i2;
418 else if (i1 && p2)
419 array_expr = p2, index_exp = i1;
420 else
422 error ("invalid types %<%T[%T]%> for array subscript",
423 type, TREE_TYPE (index_exp));
424 return error_mark_node;
427 if (array_expr == error_mark_node || index_exp == error_mark_node)
428 error ("ambiguous conversion for array subscript");
430 if (TREE_CODE (TREE_TYPE (array_expr)) == POINTER_TYPE)
431 array_expr = mark_rvalue_use (array_expr);
432 else
433 array_expr = mark_lvalue_use_nonread (array_expr);
434 index_exp = mark_rvalue_use (index_exp);
435 expr = build_array_ref (input_location, array_expr, index_exp);
437 if (processing_template_decl && expr != error_mark_node)
439 if (overload != NULL_TREE)
440 return (build_min_non_dep_op_overload
441 (ARRAY_REF, expr, overload, orig_array_expr, orig_index_exp));
443 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
444 NULL_TREE, NULL_TREE);
446 return expr;
449 /* Given the cast expression EXP, checking out its validity. Either return
450 an error_mark_node if there was an unavoidable error, return a cast to
451 void for trying to delete a pointer w/ the value 0, or return the
452 call to delete. If DOING_VEC is true, we handle things differently
453 for doing an array delete.
454 Implements ARM $5.3.4. This is called from the parser. */
456 tree
457 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
458 tsubst_flags_t complain)
460 tree t, type;
462 if (exp == error_mark_node)
463 return exp;
465 if (processing_template_decl)
467 t = build_min (DELETE_EXPR, void_type_node, exp, size);
468 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
469 DELETE_EXPR_USE_VEC (t) = doing_vec;
470 TREE_SIDE_EFFECTS (t) = 1;
471 return t;
474 /* An array can't have been allocated by new, so complain. */
475 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
476 warning (0, "deleting array %q#E", exp);
478 t = build_expr_type_conversion (WANT_POINTER, exp, true);
480 if (t == NULL_TREE || t == error_mark_node)
482 error ("type %q#T argument given to %<delete%>, expected pointer",
483 TREE_TYPE (exp));
484 return error_mark_node;
487 type = TREE_TYPE (t);
489 /* As of Valley Forge, you can delete a pointer to const. */
491 /* You can't delete functions. */
492 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
494 error ("cannot delete a function. Only pointer-to-objects are "
495 "valid arguments to %<delete%>");
496 return error_mark_node;
499 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
500 if (VOID_TYPE_P (TREE_TYPE (type)))
502 warning (OPT_Wdelete_incomplete, "deleting %qT is undefined", type);
503 doing_vec = 0;
506 /* Deleting a pointer with the value zero is valid and has no effect. */
507 if (integer_zerop (t))
508 return build1 (NOP_EXPR, void_type_node, t);
510 if (doing_vec)
511 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
512 sfk_deleting_destructor,
513 use_global_delete, complain);
514 else
515 return build_delete (type, t, sfk_deleting_destructor,
516 LOOKUP_NORMAL, use_global_delete,
517 complain);
520 /* Report an error if the indicated template declaration is not the
521 sort of thing that should be a member template. */
523 void
524 check_member_template (tree tmpl)
526 tree decl;
528 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
529 decl = DECL_TEMPLATE_RESULT (tmpl);
531 if (TREE_CODE (decl) == FUNCTION_DECL
532 || DECL_ALIAS_TEMPLATE_P (tmpl)
533 || (TREE_CODE (decl) == TYPE_DECL
534 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
536 /* The parser rejects template declarations in local classes
537 (with the exception of generic lambdas). */
538 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
539 /* The parser rejects any use of virtual in a function template. */
540 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
541 && DECL_VIRTUAL_P (decl)));
543 /* The debug-information generating code doesn't know what to do
544 with member templates. */
545 DECL_IGNORED_P (tmpl) = 1;
547 else if (variable_template_p (tmpl))
548 /* OK */;
549 else
550 error ("template declaration of %q#D", decl);
553 /* Sanity check: report error if this function FUNCTION is not
554 really a member of the class (CTYPE) it is supposed to belong to.
555 TEMPLATE_PARMS is used to specify the template parameters of a member
556 template passed as FUNCTION_DECL. If the member template is passed as a
557 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
558 from the declaration. If the function is not a function template, it
559 must be NULL.
560 It returns the original declaration for the function, NULL_TREE if
561 no declaration was found, error_mark_node if an error was emitted. */
563 tree
564 check_classfn (tree ctype, tree function, tree template_parms)
566 if (DECL_USE_TEMPLATE (function)
567 && !(TREE_CODE (function) == TEMPLATE_DECL
568 && DECL_TEMPLATE_SPECIALIZATION (function))
569 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
570 /* Since this is a specialization of a member template,
571 we're not going to find the declaration in the class.
572 For example, in:
574 struct S { template <typename T> void f(T); };
575 template <> void S::f(int);
577 we're not going to find `S::f(int)', but there's no
578 reason we should, either. We let our callers know we didn't
579 find the method, but we don't complain. */
580 return NULL_TREE;
582 /* Basic sanity check: for a template function, the template parameters
583 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
584 if (TREE_CODE (function) == TEMPLATE_DECL)
586 if (template_parms
587 && !comp_template_parms (template_parms,
588 DECL_TEMPLATE_PARMS (function)))
590 error ("template parameter lists provided don%'t match the "
591 "template parameters of %qD", function);
592 return error_mark_node;
594 template_parms = DECL_TEMPLATE_PARMS (function);
597 /* OK, is this a definition of a member template? */
598 bool is_template = (template_parms != NULL_TREE);
600 /* [temp.mem]
602 A destructor shall not be a member template. */
603 if (DECL_DESTRUCTOR_P (function) && is_template)
605 error ("destructor %qD declared as member template", function);
606 return error_mark_node;
609 /* We must enter the scope here, because conversion operators are
610 named by target type, and type equivalence relies on typenames
611 resolving within the scope of CTYPE. */
612 tree pushed_scope = push_scope (ctype);
613 tree matched = NULL_TREE;
614 tree fns = get_class_binding (ctype, DECL_NAME (function));
616 for (ovl_iterator iter (fns); !matched && iter; ++iter)
618 tree fndecl = *iter;
620 /* A member template definition only matches a member template
621 declaration. */
622 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
623 continue;
625 if (!DECL_DECLARES_FUNCTION_P (fndecl))
626 continue;
628 tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
629 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
631 /* We cannot simply call decls_match because this doesn't work
632 for static member functions that are pretending to be
633 methods, and because the name may have been changed by
634 asm("new_name"). */
636 /* Get rid of the this parameter on functions that become
637 static. */
638 if (DECL_STATIC_FUNCTION_P (fndecl)
639 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
640 p1 = TREE_CHAIN (p1);
642 /* ref-qualifier or absence of same must match. */
643 if (type_memfn_rqual (TREE_TYPE (function))
644 != type_memfn_rqual (TREE_TYPE (fndecl)))
645 continue;
647 // Include constraints in the match.
648 tree c1 = get_constraints (function);
649 tree c2 = get_constraints (fndecl);
651 /* While finding a match, same types and params are not enough
652 if the function is versioned. Also check version ("target")
653 attributes. */
654 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
655 TREE_TYPE (TREE_TYPE (fndecl)))
656 && compparms (p1, p2)
657 && !targetm.target_option.function_versions (function, fndecl)
658 && (!is_template
659 || comp_template_parms (template_parms,
660 DECL_TEMPLATE_PARMS (fndecl)))
661 && equivalent_constraints (c1, c2)
662 && (DECL_TEMPLATE_SPECIALIZATION (function)
663 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
664 && (!DECL_TEMPLATE_SPECIALIZATION (function)
665 || (DECL_TI_TEMPLATE (function) == DECL_TI_TEMPLATE (fndecl))))
666 matched = fndecl;
669 if (!matched)
671 if (!COMPLETE_TYPE_P (ctype))
672 cxx_incomplete_type_error (function, ctype);
673 else
675 if (DECL_CONV_FN_P (function))
676 fns = get_class_binding (ctype, conv_op_identifier);
678 error_at (DECL_SOURCE_LOCATION (function),
679 "no declaration matches %q#D", function);
680 if (fns)
681 print_candidates (fns);
682 else if (DECL_CONV_FN_P (function))
683 inform (DECL_SOURCE_LOCATION (function),
684 "no conversion operators declared");
685 else
686 inform (DECL_SOURCE_LOCATION (function),
687 "no functions named %qD", function);
688 inform (DECL_SOURCE_LOCATION (TYPE_NAME (ctype)),
689 "%#qT defined here", ctype);
691 matched = error_mark_node;
694 if (pushed_scope)
695 pop_scope (pushed_scope);
697 return matched;
700 /* DECL is a function with vague linkage. Remember it so that at the
701 end of the translation unit we can decide whether or not to emit
702 it. */
704 void
705 note_vague_linkage_fn (tree decl)
707 DECL_DEFER_OUTPUT (decl) = 1;
708 vec_safe_push (deferred_fns, decl);
711 /* As above, but for variable template instantiations. */
713 void
714 note_variable_template_instantiation (tree decl)
716 vec_safe_push (pending_statics, decl);
719 /* We have just processed the DECL, which is a static data member.
720 The other parameters are as for cp_finish_decl. */
722 void
723 finish_static_data_member_decl (tree decl,
724 tree init, bool init_const_expr_p,
725 tree asmspec_tree,
726 int flags)
728 DECL_CONTEXT (decl) = current_class_type;
730 /* We cannot call pushdecl here, because that would fill in the
731 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
732 the right thing, namely, to put this decl out straight away. */
734 if (! processing_template_decl)
735 vec_safe_push (pending_statics, decl);
737 if (LOCAL_CLASS_P (current_class_type)
738 /* We already complained about the template definition. */
739 && !DECL_TEMPLATE_INSTANTIATION (decl))
740 permerror (input_location, "local class %q#T shall not have static data member %q#D",
741 current_class_type, decl);
742 else
743 for (tree t = current_class_type; TYPE_P (t);
744 t = CP_TYPE_CONTEXT (t))
745 if (TYPE_UNNAMED_P (t))
747 if (permerror (DECL_SOURCE_LOCATION (decl),
748 "static data member %qD in unnamed class", decl))
749 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
750 "unnamed class defined here");
751 break;
754 DECL_IN_AGGR_P (decl) = 1;
756 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
757 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
758 SET_VAR_HAD_UNKNOWN_BOUND (decl);
760 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
763 /* DECLARATOR and DECLSPECS correspond to a class member. The other
764 parameters are as for cp_finish_decl. Return the DECL for the
765 class member declared. */
767 tree
768 grokfield (const cp_declarator *declarator,
769 cp_decl_specifier_seq *declspecs,
770 tree init, bool init_const_expr_p,
771 tree asmspec_tree,
772 tree attrlist)
774 tree value;
775 const char *asmspec = 0;
776 int flags;
777 tree name;
779 if (init
780 && TREE_CODE (init) == TREE_LIST
781 && TREE_VALUE (init) == error_mark_node
782 && TREE_CHAIN (init) == NULL_TREE)
783 init = NULL_TREE;
785 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
786 if (! value || value == error_mark_node)
787 /* friend or constructor went bad. */
788 return error_mark_node;
789 if (TREE_TYPE (value) == error_mark_node)
790 return value;
792 if (TREE_CODE (value) == TYPE_DECL && init)
794 error ("typedef %qD is initialized (use decltype instead)", value);
795 init = NULL_TREE;
798 /* Pass friendly classes back. */
799 if (value == void_type_node)
800 return value;
803 name = DECL_NAME (value);
805 if (name != NULL_TREE)
807 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
809 error ("explicit template argument list not allowed");
810 return error_mark_node;
813 if (IDENTIFIER_POINTER (name)[0] == '_'
814 && id_equal (name, "_vptr"))
815 error ("member %qD conflicts with virtual function table field name",
816 value);
819 /* Stash away type declarations. */
820 if (TREE_CODE (value) == TYPE_DECL)
822 DECL_NONLOCAL (value) = 1;
823 DECL_CONTEXT (value) = current_class_type;
825 if (attrlist)
827 int attrflags = 0;
829 /* If this is a typedef that names the class for linkage purposes
830 (7.1.3p8), apply any attributes directly to the type. */
831 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
832 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
833 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
835 cplus_decl_attributes (&value, attrlist, attrflags);
838 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
839 && TREE_TYPE (value) != error_mark_node
840 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
841 set_underlying_type (value);
843 /* It's important that push_template_decl below follows
844 set_underlying_type above so that the created template
845 carries the properly set type of VALUE. */
846 if (processing_template_decl)
847 value = push_template_decl (value);
849 record_locally_defined_typedef (value);
850 return value;
853 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
855 if (!friendp && DECL_IN_AGGR_P (value))
857 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
858 return void_type_node;
861 if (asmspec_tree && asmspec_tree != error_mark_node)
862 asmspec = TREE_STRING_POINTER (asmspec_tree);
864 if (init)
866 if (TREE_CODE (value) == FUNCTION_DECL)
868 if (init == ridpointers[(int)RID_DELETE])
870 DECL_DELETED_FN (value) = 1;
871 DECL_DECLARED_INLINE_P (value) = 1;
872 DECL_INITIAL (value) = error_mark_node;
874 else if (init == ridpointers[(int)RID_DEFAULT])
876 if (defaultable_fn_check (value))
878 DECL_DEFAULTED_FN (value) = 1;
879 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
880 DECL_DECLARED_INLINE_P (value) = 1;
883 else if (TREE_CODE (init) == DEFAULT_ARG)
884 error ("invalid initializer for member function %qD", value);
885 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
887 if (integer_zerop (init))
888 DECL_PURE_VIRTUAL_P (value) = 1;
889 else if (error_operand_p (init))
890 ; /* An error has already been reported. */
891 else
892 error ("invalid initializer for member function %qD",
893 value);
895 else
897 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
898 if (friendp)
899 error ("initializer specified for friend function %qD",
900 value);
901 else
902 error ("initializer specified for static member function %qD",
903 value);
906 else if (TREE_CODE (value) == FIELD_DECL)
907 /* C++11 NSDMI, keep going. */;
908 else if (!VAR_P (value))
909 gcc_unreachable ();
912 /* Pass friend decls back. */
913 if ((TREE_CODE (value) == FUNCTION_DECL
914 || TREE_CODE (value) == TEMPLATE_DECL)
915 && DECL_CONTEXT (value) != current_class_type)
916 return value;
918 /* Need to set this before push_template_decl. */
919 if (VAR_P (value))
920 DECL_CONTEXT (value) = current_class_type;
922 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
924 value = push_template_decl (value);
925 if (error_operand_p (value))
926 return error_mark_node;
929 if (attrlist)
930 cplus_decl_attributes (&value, attrlist, 0);
932 if (init && DIRECT_LIST_INIT_P (init))
933 flags = LOOKUP_NORMAL;
934 else
935 flags = LOOKUP_IMPLICIT;
937 switch (TREE_CODE (value))
939 case VAR_DECL:
940 finish_static_data_member_decl (value, init, init_const_expr_p,
941 asmspec_tree, flags);
942 return value;
944 case FIELD_DECL:
945 if (asmspec)
946 error ("%<asm%> specifiers are not permitted on non-static data members");
947 if (DECL_INITIAL (value) == error_mark_node)
948 init = error_mark_node;
949 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
950 NULL_TREE, flags);
951 DECL_IN_AGGR_P (value) = 1;
952 return value;
954 case FUNCTION_DECL:
955 if (asmspec)
956 set_user_assembler_name (value, asmspec);
958 cp_finish_decl (value,
959 /*init=*/NULL_TREE,
960 /*init_const_expr_p=*/false,
961 asmspec_tree, flags);
963 /* Pass friends back this way. */
964 if (DECL_FRIEND_P (value))
965 return void_type_node;
967 DECL_IN_AGGR_P (value) = 1;
968 return value;
970 default:
971 gcc_unreachable ();
973 return NULL_TREE;
976 /* Like `grokfield', but for bitfields.
977 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
979 tree
980 grokbitfield (const cp_declarator *declarator,
981 cp_decl_specifier_seq *declspecs, tree width,
982 tree attrlist)
984 tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, &attrlist);
986 if (value == error_mark_node)
987 return NULL_TREE; /* friends went bad. */
988 if (TREE_TYPE (value) == error_mark_node)
989 return value;
991 /* Pass friendly classes back. */
992 if (VOID_TYPE_P (value))
993 return void_type_node;
995 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
996 && (POINTER_TYPE_P (value)
997 || !dependent_type_p (TREE_TYPE (value))))
999 error ("bit-field %qD with non-integral type", value);
1000 return error_mark_node;
1003 if (TREE_CODE (value) == TYPE_DECL)
1005 error ("cannot declare %qD to be a bit-field type", value);
1006 return NULL_TREE;
1009 /* Usually, finish_struct_1 catches bitfields with invalid types.
1010 But, in the case of bitfields with function type, we confuse
1011 ourselves into thinking they are member functions, so we must
1012 check here. */
1013 if (TREE_CODE (value) == FUNCTION_DECL)
1015 error ("cannot declare bit-field %qD with function type",
1016 DECL_NAME (value));
1017 return NULL_TREE;
1020 if (width && TYPE_WARN_IF_NOT_ALIGN (TREE_TYPE (value)))
1022 error ("cannot declare bit-field %qD with %<warn_if_not_aligned%> type",
1023 DECL_NAME (value));
1024 return NULL_TREE;
1027 if (DECL_IN_AGGR_P (value))
1029 error ("%qD is already defined in the class %qT", value,
1030 DECL_CONTEXT (value));
1031 return void_type_node;
1034 if (TREE_STATIC (value))
1036 error ("static member %qD cannot be a bit-field", value);
1037 return NULL_TREE;
1039 cp_finish_decl (value, NULL_TREE, false, NULL_TREE, 0);
1041 if (width != error_mark_node)
1043 /* The width must be an integer type. */
1044 if (!type_dependent_expression_p (width)
1045 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1046 error ("width of bit-field %qD has non-integral type %qT", value,
1047 TREE_TYPE (width));
1048 else
1050 DECL_INITIAL (value) = width;
1051 SET_DECL_C_BIT_FIELD (value);
1055 DECL_IN_AGGR_P (value) = 1;
1057 if (attrlist)
1058 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1060 return value;
1064 /* Returns true iff ATTR is an attribute which needs to be applied at
1065 instantiation time rather than template definition time. */
1067 static bool
1068 is_late_template_attribute (tree attr, tree decl)
1070 tree name = get_attribute_name (attr);
1071 tree args = TREE_VALUE (attr);
1072 const struct attribute_spec *spec = lookup_attribute_spec (name);
1073 tree arg;
1075 if (!spec)
1076 /* Unknown attribute. */
1077 return false;
1079 /* Attribute weak handling wants to write out assembly right away. */
1080 if (is_attribute_p ("weak", name))
1081 return true;
1083 /* Attributes used and unused are applied directly, as they appertain to
1084 decls. */
1085 if (is_attribute_p ("unused", name)
1086 || is_attribute_p ("used", name))
1087 return false;
1089 /* Attribute tls_model wants to modify the symtab. */
1090 if (is_attribute_p ("tls_model", name))
1091 return true;
1093 /* #pragma omp declare simd attribute needs to be always deferred. */
1094 if (flag_openmp
1095 && is_attribute_p ("omp declare simd", name))
1096 return true;
1098 /* An attribute pack is clearly dependent. */
1099 if (args && PACK_EXPANSION_P (args))
1100 return true;
1102 /* If any of the arguments are dependent expressions, we can't evaluate
1103 the attribute until instantiation time. */
1104 for (arg = args; arg; arg = TREE_CHAIN (arg))
1106 tree t = TREE_VALUE (arg);
1108 /* If the first attribute argument is an identifier, only consider
1109 second and following arguments. Attributes like mode, format,
1110 cleanup and several target specific attributes aren't late
1111 just because they have an IDENTIFIER_NODE as first argument. */
1112 if (arg == args && attribute_takes_identifier_p (name)
1113 && identifier_p (t))
1114 continue;
1116 if (value_dependent_expression_p (t)
1117 || type_dependent_expression_p (t))
1118 return true;
1121 if (TREE_CODE (decl) == TYPE_DECL
1122 || TYPE_P (decl)
1123 || spec->type_required)
1125 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1127 /* We can't apply any attributes to a completely unknown type until
1128 instantiation time. */
1129 enum tree_code code = TREE_CODE (type);
1130 if (code == TEMPLATE_TYPE_PARM
1131 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1132 || code == TYPENAME_TYPE)
1133 return true;
1134 /* Also defer most attributes on dependent types. This is not
1135 necessary in all cases, but is the better default. */
1136 else if (dependent_type_p (type)
1137 /* But some attributes specifically apply to templates. */
1138 && !is_attribute_p ("abi_tag", name)
1139 && !is_attribute_p ("deprecated", name)
1140 && !is_attribute_p ("visibility", name))
1141 return true;
1142 else
1143 return false;
1145 else
1146 return false;
1149 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1150 applied at instantiation time and return them. If IS_DEPENDENT is true,
1151 the declaration itself is dependent, so all attributes should be applied
1152 at instantiation time. */
1154 static tree
1155 splice_template_attributes (tree *attr_p, tree decl)
1157 tree *p = attr_p;
1158 tree late_attrs = NULL_TREE;
1159 tree *q = &late_attrs;
1161 if (!p)
1162 return NULL_TREE;
1164 for (; *p; )
1166 if (is_late_template_attribute (*p, decl))
1168 ATTR_IS_DEPENDENT (*p) = 1;
1169 *q = *p;
1170 *p = TREE_CHAIN (*p);
1171 q = &TREE_CHAIN (*q);
1172 *q = NULL_TREE;
1174 else
1175 p = &TREE_CHAIN (*p);
1178 return late_attrs;
1181 /* Remove any late attributes from the list in ATTR_P and attach them to
1182 DECL_P. */
1184 static void
1185 save_template_attributes (tree *attr_p, tree *decl_p)
1187 tree *q;
1189 if (attr_p && *attr_p == error_mark_node)
1190 return;
1192 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1193 if (!late_attrs)
1194 return;
1196 if (DECL_P (*decl_p))
1197 q = &DECL_ATTRIBUTES (*decl_p);
1198 else
1199 q = &TYPE_ATTRIBUTES (*decl_p);
1201 tree old_attrs = *q;
1203 /* Merge the late attributes at the beginning with the attribute
1204 list. */
1205 late_attrs = merge_attributes (late_attrs, *q);
1206 *q = late_attrs;
1208 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1210 /* We've added new attributes directly to the main variant, so
1211 now we need to update all of the other variants to include
1212 these new attributes. */
1213 tree variant;
1214 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1215 variant = TYPE_NEXT_VARIANT (variant))
1217 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1218 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1223 /* True if ATTRS contains any dependent attributes that affect type
1224 identity. */
1226 bool
1227 any_dependent_type_attributes_p (tree attrs)
1229 for (tree a = attrs; a; a = TREE_CHAIN (a))
1230 if (ATTR_IS_DEPENDENT (a))
1232 const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (a));
1233 if (as && as->affects_type_identity)
1234 return true;
1236 return false;
1239 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1240 to a typedef which gives a previously unnamed class or enum a name for
1241 linkage purposes. */
1243 bool
1244 attributes_naming_typedef_ok (tree attrs)
1246 for (; attrs; attrs = TREE_CHAIN (attrs))
1248 tree name = get_attribute_name (attrs);
1249 if (is_attribute_p ("vector_size", name))
1250 return false;
1252 return true;
1255 /* Like reconstruct_complex_type, but handle also template trees. */
1257 tree
1258 cp_reconstruct_complex_type (tree type, tree bottom)
1260 tree inner, outer;
1261 bool late_return_type_p = false;
1263 if (TYPE_PTR_P (type))
1265 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1266 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1267 TYPE_REF_CAN_ALIAS_ALL (type));
1269 else if (TREE_CODE (type) == REFERENCE_TYPE)
1271 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1272 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1273 TYPE_REF_CAN_ALIAS_ALL (type));
1275 else if (TREE_CODE (type) == ARRAY_TYPE)
1277 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1278 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1279 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1280 element type qualification will be handled by the recursive
1281 cp_reconstruct_complex_type call and cp_build_qualified_type
1282 for ARRAY_TYPEs changes the element type. */
1283 return outer;
1285 else if (TREE_CODE (type) == FUNCTION_TYPE)
1287 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1288 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1289 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1290 outer = apply_memfn_quals (outer,
1291 type_memfn_quals (type),
1292 type_memfn_rqual (type));
1294 else if (TREE_CODE (type) == METHOD_TYPE)
1296 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1297 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1298 /* The build_method_type_directly() routine prepends 'this' to argument list,
1299 so we must compensate by getting rid of it. */
1300 outer
1301 = build_method_type_directly
1302 (class_of_this_parm (type), inner,
1303 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1305 else if (TREE_CODE (type) == OFFSET_TYPE)
1307 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1308 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1310 else
1311 return bottom;
1313 if (TYPE_ATTRIBUTES (type))
1314 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1315 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1317 if (late_return_type_p)
1318 TYPE_HAS_LATE_RETURN_TYPE (outer) = 1;
1320 return outer;
1323 /* Replaces any constexpr expression that may be into the attributes
1324 arguments with their reduced value. */
1326 static void
1327 cp_check_const_attributes (tree attributes)
1329 if (attributes == error_mark_node)
1330 return;
1332 tree attr;
1333 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1335 tree arg;
1336 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1338 tree expr = TREE_VALUE (arg);
1339 if (EXPR_P (expr))
1340 TREE_VALUE (arg) = maybe_constant_value (expr);
1345 /* Return true if TYPE is an OpenMP mappable type. */
1346 bool
1347 cp_omp_mappable_type (tree type)
1349 /* Mappable type has to be complete. */
1350 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1351 return false;
1352 /* Arrays have mappable type if the elements have mappable type. */
1353 while (TREE_CODE (type) == ARRAY_TYPE)
1354 type = TREE_TYPE (type);
1355 /* A mappable type cannot contain virtual members. */
1356 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1357 return false;
1358 /* All data members must be non-static. */
1359 if (CLASS_TYPE_P (type))
1361 tree field;
1362 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1363 if (VAR_P (field))
1364 return false;
1365 /* All fields must have mappable types. */
1366 else if (TREE_CODE (field) == FIELD_DECL
1367 && !cp_omp_mappable_type (TREE_TYPE (field)))
1368 return false;
1370 return true;
1373 /* Like decl_attributes, but handle C++ complexity. */
1375 void
1376 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1378 if (*decl == NULL_TREE || *decl == void_type_node
1379 || *decl == error_mark_node)
1380 return;
1382 /* Add implicit "omp declare target" attribute if requested. */
1383 if (scope_chain->omp_declare_target_attribute
1384 && ((VAR_P (*decl)
1385 && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
1386 || TREE_CODE (*decl) == FUNCTION_DECL))
1388 if (VAR_P (*decl)
1389 && DECL_CLASS_SCOPE_P (*decl))
1390 error ("%q+D static data member inside of declare target directive",
1391 *decl);
1392 else if (!processing_template_decl
1393 && VAR_P (*decl)
1394 && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1395 error ("%q+D in declare target directive does not have mappable type",
1396 *decl);
1397 else
1398 attributes = tree_cons (get_identifier ("omp declare target"),
1399 NULL_TREE, attributes);
1402 if (processing_template_decl)
1404 if (check_for_bare_parameter_packs (attributes))
1405 return;
1407 save_template_attributes (&attributes, decl);
1410 cp_check_const_attributes (attributes);
1412 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1413 decl = &DECL_TEMPLATE_RESULT (*decl);
1415 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1417 attributes
1418 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1419 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1420 attributes, flags);
1422 else
1423 decl_attributes (decl, attributes, flags);
1425 if (TREE_CODE (*decl) == TYPE_DECL)
1426 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1428 /* Propagate deprecation out to the template. */
1429 if (TREE_DEPRECATED (*decl))
1430 if (tree ti = get_template_info (*decl))
1432 tree tmpl = TI_TEMPLATE (ti);
1433 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1434 : DECL_TEMPLATE_RESULT (tmpl));
1435 if (*decl == pattern)
1436 TREE_DEPRECATED (tmpl) = true;
1440 /* Walks through the namespace- or function-scope anonymous union
1441 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1442 Returns one of the fields for use in the mangled name. */
1444 static tree
1445 build_anon_union_vars (tree type, tree object)
1447 tree main_decl = NULL_TREE;
1448 tree field;
1450 /* Rather than write the code to handle the non-union case,
1451 just give an error. */
1452 if (TREE_CODE (type) != UNION_TYPE)
1454 error ("anonymous struct not inside named type");
1455 return error_mark_node;
1458 for (field = TYPE_FIELDS (type);
1459 field != NULL_TREE;
1460 field = DECL_CHAIN (field))
1462 tree decl;
1463 tree ref;
1465 if (DECL_ARTIFICIAL (field))
1466 continue;
1467 if (TREE_CODE (field) != FIELD_DECL)
1469 permerror (DECL_SOURCE_LOCATION (field),
1470 "%q#D invalid; an anonymous union can only "
1471 "have non-static data members", field);
1472 continue;
1475 if (TREE_PRIVATE (field))
1476 permerror (DECL_SOURCE_LOCATION (field),
1477 "private member %q#D in anonymous union", field);
1478 else if (TREE_PROTECTED (field))
1479 permerror (DECL_SOURCE_LOCATION (field),
1480 "protected member %q#D in anonymous union", field);
1482 if (processing_template_decl)
1483 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1484 DECL_NAME (field), NULL_TREE);
1485 else
1486 ref = build_class_member_access_expr (object, field, NULL_TREE,
1487 false, tf_warning_or_error);
1489 if (DECL_NAME (field))
1491 tree base;
1493 decl = build_decl (input_location,
1494 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1495 DECL_ANON_UNION_VAR_P (decl) = 1;
1496 DECL_ARTIFICIAL (decl) = 1;
1498 base = get_base_address (object);
1499 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1500 TREE_STATIC (decl) = TREE_STATIC (base);
1501 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1503 SET_DECL_VALUE_EXPR (decl, ref);
1504 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1506 decl = pushdecl (decl);
1508 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1509 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1510 else
1511 decl = 0;
1513 if (main_decl == NULL_TREE)
1514 main_decl = decl;
1517 return main_decl;
1520 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1521 anonymous union, then all members must be laid out together. PUBLIC_P
1522 is nonzero if this union is not declared static. */
1524 void
1525 finish_anon_union (tree anon_union_decl)
1527 tree type;
1528 tree main_decl;
1529 bool public_p;
1531 if (anon_union_decl == error_mark_node)
1532 return;
1534 type = TREE_TYPE (anon_union_decl);
1535 public_p = TREE_PUBLIC (anon_union_decl);
1537 /* The VAR_DECL's context is the same as the TYPE's context. */
1538 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1540 if (TYPE_FIELDS (type) == NULL_TREE)
1541 return;
1543 if (public_p)
1545 error ("namespace-scope anonymous aggregates must be static");
1546 return;
1549 main_decl = build_anon_union_vars (type, anon_union_decl);
1550 if (main_decl == error_mark_node)
1551 return;
1552 if (main_decl == NULL_TREE)
1554 pedwarn (input_location, 0, "anonymous union with no members");
1555 return;
1558 if (!processing_template_decl)
1560 /* Use main_decl to set the mangled name. */
1561 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1562 maybe_commonize_var (anon_union_decl);
1563 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1564 mangle_decl (anon_union_decl);
1565 DECL_NAME (anon_union_decl) = NULL_TREE;
1568 pushdecl (anon_union_decl);
1569 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1572 /* Auxiliary functions to make type signatures for
1573 `operator new' and `operator delete' correspond to
1574 what compiler will be expecting. */
1576 tree
1577 coerce_new_type (tree type)
1579 int e = 0;
1580 tree args = TYPE_ARG_TYPES (type);
1582 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1584 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1586 e = 1;
1587 error ("%<operator new%> must return type %qT", ptr_type_node);
1590 if (args && args != void_list_node)
1592 if (TREE_PURPOSE (args))
1594 /* [basic.stc.dynamic.allocation]
1596 The first parameter shall not have an associated default
1597 argument. */
1598 error ("the first parameter of %<operator new%> cannot "
1599 "have a default argument");
1600 /* Throw away the default argument. */
1601 TREE_PURPOSE (args) = NULL_TREE;
1604 if (!same_type_p (TREE_VALUE (args), size_type_node))
1606 e = 2;
1607 args = TREE_CHAIN (args);
1610 else
1611 e = 2;
1613 if (e == 2)
1614 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1615 "as first parameter", size_type_node);
1617 switch (e)
1619 case 2:
1620 args = tree_cons (NULL_TREE, size_type_node, args);
1621 /* Fall through. */
1622 case 1:
1623 type = build_exception_variant
1624 (build_function_type (ptr_type_node, args),
1625 TYPE_RAISES_EXCEPTIONS (type));
1626 /* Fall through. */
1627 default:;
1629 return type;
1632 tree
1633 coerce_delete_type (tree type)
1635 int e = 0;
1636 tree args = TYPE_ARG_TYPES (type);
1638 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1640 if (!same_type_p (TREE_TYPE (type), void_type_node))
1642 e = 1;
1643 error ("%<operator delete%> must return type %qT", void_type_node);
1646 if (!args || args == void_list_node
1647 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1649 e = 2;
1650 if (args && args != void_list_node)
1651 args = TREE_CHAIN (args);
1652 error ("%<operator delete%> takes type %qT as first parameter",
1653 ptr_type_node);
1655 switch (e)
1657 case 2:
1658 args = tree_cons (NULL_TREE, ptr_type_node, args);
1659 /* Fall through. */
1660 case 1:
1661 type = build_exception_variant
1662 (build_function_type (void_type_node, args),
1663 TYPE_RAISES_EXCEPTIONS (type));
1664 /* Fall through. */
1665 default:;
1668 return type;
1671 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1672 and mark them as needed. */
1674 static void
1675 mark_vtable_entries (tree decl)
1677 tree fnaddr;
1678 unsigned HOST_WIDE_INT idx;
1680 /* It's OK for the vtable to refer to deprecated virtual functions. */
1681 warning_sentinel w(warn_deprecated_decl);
1683 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1684 idx, fnaddr)
1686 tree fn;
1688 STRIP_NOPS (fnaddr);
1690 if (TREE_CODE (fnaddr) != ADDR_EXPR
1691 && TREE_CODE (fnaddr) != FDESC_EXPR)
1692 /* This entry is an offset: a virtual base class offset, a
1693 virtual call offset, an RTTI offset, etc. */
1694 continue;
1696 fn = TREE_OPERAND (fnaddr, 0);
1697 TREE_ADDRESSABLE (fn) = 1;
1698 /* When we don't have vcall offsets, we output thunks whenever
1699 we output the vtables that contain them. With vcall offsets,
1700 we know all the thunks we'll need when we emit a virtual
1701 function, so we emit the thunks there instead. */
1702 if (DECL_THUNK_P (fn))
1703 use_thunk (fn, /*emit_p=*/0);
1704 mark_used (fn);
1708 /* Set DECL up to have the closest approximation of "initialized common"
1709 linkage available. */
1711 void
1712 comdat_linkage (tree decl)
1714 if (flag_weak)
1715 make_decl_one_only (decl, cxx_comdat_group (decl));
1716 else if (TREE_CODE (decl) == FUNCTION_DECL
1717 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1718 /* We can just emit function and compiler-generated variables
1719 statically; having multiple copies is (for the most part) only
1720 a waste of space.
1722 There are two correctness issues, however: the address of a
1723 template instantiation with external linkage should be the
1724 same, independent of what translation unit asks for the
1725 address, and this will not hold when we emit multiple copies of
1726 the function. However, there's little else we can do.
1728 Also, by default, the typeinfo implementation assumes that
1729 there will be only one copy of the string used as the name for
1730 each type. Therefore, if weak symbols are unavailable, the
1731 run-time library should perform a more conservative check; it
1732 should perform a string comparison, rather than an address
1733 comparison. */
1734 TREE_PUBLIC (decl) = 0;
1735 else
1737 /* Static data member template instantiations, however, cannot
1738 have multiple copies. */
1739 if (DECL_INITIAL (decl) == 0
1740 || DECL_INITIAL (decl) == error_mark_node)
1741 DECL_COMMON (decl) = 1;
1742 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1744 DECL_COMMON (decl) = 1;
1745 DECL_INITIAL (decl) = error_mark_node;
1747 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1749 /* We can't do anything useful; leave vars for explicit
1750 instantiation. */
1751 DECL_EXTERNAL (decl) = 1;
1752 DECL_NOT_REALLY_EXTERN (decl) = 0;
1756 if (TREE_PUBLIC (decl))
1757 DECL_COMDAT (decl) = 1;
1760 /* For win32 we also want to put explicit instantiations in
1761 linkonce sections, so that they will be merged with implicit
1762 instantiations; otherwise we get duplicate symbol errors.
1763 For Darwin we do not want explicit instantiations to be
1764 linkonce. */
1766 void
1767 maybe_make_one_only (tree decl)
1769 /* We used to say that this was not necessary on targets that support weak
1770 symbols, because the implicit instantiations will defer to the explicit
1771 one. However, that's not actually the case in SVR4; a strong definition
1772 after a weak one is an error. Also, not making explicit
1773 instantiations one_only means that we can end up with two copies of
1774 some template instantiations. */
1775 if (! flag_weak)
1776 return;
1778 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1779 we can get away with not emitting them if they aren't used. We need
1780 to for variables so that cp_finish_decl will update their linkage,
1781 because their DECL_INITIAL may not have been set properly yet. */
1783 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1784 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1785 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1787 make_decl_one_only (decl, cxx_comdat_group (decl));
1789 if (VAR_P (decl))
1791 varpool_node *node = varpool_node::get_create (decl);
1792 DECL_COMDAT (decl) = 1;
1793 /* Mark it needed so we don't forget to emit it. */
1794 node->forced_by_abi = true;
1795 TREE_USED (decl) = 1;
1800 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1801 This predicate will give the right answer during parsing of the
1802 function, which other tests may not. */
1804 bool
1805 vague_linkage_p (tree decl)
1807 if (!TREE_PUBLIC (decl))
1809 /* maybe_thunk_body clears TREE_PUBLIC on the maybe-in-charge 'tor
1810 variants, check one of the "clones" for the real linkage. */
1811 if ((DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
1812 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl))
1813 && DECL_CHAIN (decl)
1814 && DECL_CLONED_FUNCTION (DECL_CHAIN (decl)))
1815 return vague_linkage_p (DECL_CHAIN (decl));
1817 gcc_checking_assert (!DECL_COMDAT (decl));
1818 return false;
1820 /* Unfortunately, import_export_decl has not always been called
1821 before the function is processed, so we cannot simply check
1822 DECL_COMDAT. */
1823 if (DECL_COMDAT (decl)
1824 || (TREE_CODE (decl) == FUNCTION_DECL
1825 && DECL_DECLARED_INLINE_P (decl))
1826 || (DECL_LANG_SPECIFIC (decl)
1827 && DECL_TEMPLATE_INSTANTIATION (decl))
1828 || (VAR_P (decl) && DECL_INLINE_VAR_P (decl)))
1829 return true;
1830 else if (DECL_FUNCTION_SCOPE_P (decl))
1831 /* A local static in an inline effectively has vague linkage. */
1832 return (TREE_STATIC (decl)
1833 && vague_linkage_p (DECL_CONTEXT (decl)));
1834 else
1835 return false;
1838 /* Determine whether or not we want to specifically import or export CTYPE,
1839 using various heuristics. */
1841 static void
1842 import_export_class (tree ctype)
1844 /* -1 for imported, 1 for exported. */
1845 int import_export = 0;
1847 /* It only makes sense to call this function at EOF. The reason is
1848 that this function looks at whether or not the first non-inline
1849 non-abstract virtual member function has been defined in this
1850 translation unit. But, we can't possibly know that until we've
1851 seen the entire translation unit. */
1852 gcc_assert (at_eof);
1854 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1855 return;
1857 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1858 we will have CLASSTYPE_INTERFACE_ONLY set but not
1859 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1860 heuristic because someone will supply a #pragma implementation
1861 elsewhere, and deducing it here would produce a conflict. */
1862 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1863 return;
1865 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1866 import_export = -1;
1867 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1868 import_export = 1;
1869 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1870 && !flag_implicit_templates)
1871 /* For a template class, without -fimplicit-templates, check the
1872 repository. If the virtual table is assigned to this
1873 translation unit, then export the class; otherwise, import
1874 it. */
1875 import_export = repo_export_class_p (ctype) ? 1 : -1;
1876 else if (TYPE_POLYMORPHIC_P (ctype))
1878 /* The ABI specifies that the virtual table and associated
1879 information are emitted with the key method, if any. */
1880 tree method = CLASSTYPE_KEY_METHOD (ctype);
1881 /* If weak symbol support is not available, then we must be
1882 careful not to emit the vtable when the key function is
1883 inline. An inline function can be defined in multiple
1884 translation units. If we were to emit the vtable in each
1885 translation unit containing a definition, we would get
1886 multiple definition errors at link-time. */
1887 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1888 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1891 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1892 a definition anywhere else. */
1893 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1894 import_export = 0;
1896 /* Allow back ends the chance to overrule the decision. */
1897 if (targetm.cxx.import_export_class)
1898 import_export = targetm.cxx.import_export_class (ctype, import_export);
1900 if (import_export)
1902 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1903 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1907 /* Return true if VAR has already been provided to the back end; in that
1908 case VAR should not be modified further by the front end. */
1909 static bool
1910 var_finalized_p (tree var)
1912 return varpool_node::get_create (var)->definition;
1915 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1916 must be emitted in this translation unit. Mark it as such. */
1918 void
1919 mark_needed (tree decl)
1921 TREE_USED (decl) = 1;
1922 if (TREE_CODE (decl) == FUNCTION_DECL)
1924 /* Extern inline functions don't become needed when referenced.
1925 If we know a method will be emitted in other TU and no new
1926 functions can be marked reachable, just use the external
1927 definition. */
1928 struct cgraph_node *node = cgraph_node::get_create (decl);
1929 node->forced_by_abi = true;
1931 /* #pragma interface and -frepo code can call mark_needed for
1932 maybe-in-charge 'tors; mark the clones as well. */
1933 tree clone;
1934 FOR_EACH_CLONE (clone, decl)
1935 mark_needed (clone);
1937 else if (VAR_P (decl))
1939 varpool_node *node = varpool_node::get_create (decl);
1940 /* C++ frontend use mark_decl_references to force COMDAT variables
1941 to be output that might appear dead otherwise. */
1942 node->forced_by_abi = true;
1946 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1947 returns true if a definition of this entity should be provided in
1948 this object file. Callers use this function to determine whether
1949 or not to let the back end know that a definition of DECL is
1950 available in this translation unit. */
1952 bool
1953 decl_needed_p (tree decl)
1955 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
1956 /* This function should only be called at the end of the translation
1957 unit. We cannot be sure of whether or not something will be
1958 COMDAT until that point. */
1959 gcc_assert (at_eof);
1961 /* All entities with external linkage that are not COMDAT/EXTERN should be
1962 emitted; they may be referred to from other object files. */
1963 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
1964 return true;
1965 /* Functions marked "dllexport" must be emitted so that they are
1966 visible to other DLLs. */
1967 if (flag_keep_inline_dllexport
1968 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
1969 return true;
1971 /* When not optimizing, do not bother to produce definitions for extern
1972 symbols. */
1973 if (DECL_REALLY_EXTERN (decl)
1974 && ((TREE_CODE (decl) != FUNCTION_DECL
1975 && !optimize)
1976 || (TREE_CODE (decl) == FUNCTION_DECL
1977 && !opt_for_fn (decl, optimize)))
1978 && !lookup_attribute ("always_inline", decl))
1979 return false;
1981 /* If this entity was used, let the back end see it; it will decide
1982 whether or not to emit it into the object file. */
1983 if (TREE_USED (decl))
1984 return true;
1986 /* Virtual functions might be needed for devirtualization. */
1987 if (flag_devirtualize
1988 && TREE_CODE (decl) == FUNCTION_DECL
1989 && DECL_VIRTUAL_P (decl))
1990 return true;
1992 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
1993 reference to DECL might cause it to be emitted later. */
1994 return false;
1997 /* If necessary, write out the vtables for the dynamic class CTYPE.
1998 Returns true if any vtables were emitted. */
2000 static bool
2001 maybe_emit_vtables (tree ctype)
2003 tree vtbl;
2004 tree primary_vtbl;
2005 int needed = 0;
2006 varpool_node *current = NULL, *last = NULL;
2008 /* If the vtables for this class have already been emitted there is
2009 nothing more to do. */
2010 primary_vtbl = CLASSTYPE_VTABLES (ctype);
2011 if (var_finalized_p (primary_vtbl))
2012 return false;
2013 /* Ignore dummy vtables made by get_vtable_decl. */
2014 if (TREE_TYPE (primary_vtbl) == void_type_node)
2015 return false;
2017 /* On some targets, we cannot determine the key method until the end
2018 of the translation unit -- which is when this function is
2019 called. */
2020 if (!targetm.cxx.key_method_may_be_inline ())
2021 determine_key_method (ctype);
2023 /* See if any of the vtables are needed. */
2024 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2026 import_export_decl (vtbl);
2027 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2028 needed = 1;
2030 if (!needed)
2032 /* If the references to this class' vtables are optimized away,
2033 still emit the appropriate debugging information. See
2034 dfs_debug_mark. */
2035 if (DECL_COMDAT (primary_vtbl)
2036 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2037 note_debug_info_needed (ctype);
2038 return false;
2041 /* The ABI requires that we emit all of the vtables if we emit any
2042 of them. */
2043 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2045 /* Mark entities references from the virtual table as used. */
2046 mark_vtable_entries (vtbl);
2048 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2050 vec<tree, va_gc> *cleanups = NULL;
2051 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2052 LOOKUP_NORMAL);
2054 /* It had better be all done at compile-time. */
2055 gcc_assert (!expr && !cleanups);
2058 /* Write it out. */
2059 DECL_EXTERNAL (vtbl) = 0;
2060 rest_of_decl_compilation (vtbl, 1, 1);
2062 /* Because we're only doing syntax-checking, we'll never end up
2063 actually marking the variable as written. */
2064 if (flag_syntax_only)
2065 TREE_ASM_WRITTEN (vtbl) = 1;
2066 else if (DECL_ONE_ONLY (vtbl))
2068 current = varpool_node::get_create (vtbl);
2069 if (last)
2070 current->add_to_same_comdat_group (last);
2071 last = current;
2075 /* Since we're writing out the vtable here, also write the debug
2076 info. */
2077 note_debug_info_needed (ctype);
2079 return true;
2082 /* A special return value from type_visibility meaning internal
2083 linkage. */
2085 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2087 /* walk_tree helper function for type_visibility. */
2089 static tree
2090 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2092 int *vis_p = (int *)data;
2093 if (! TYPE_P (*tp))
2095 *walk_subtrees = 0;
2097 else if (OVERLOAD_TYPE_P (*tp)
2098 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2100 *vis_p = VISIBILITY_ANON;
2101 return *tp;
2103 else if (CLASS_TYPE_P (*tp)
2104 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2105 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2106 return NULL;
2109 /* Returns the visibility of TYPE, which is the minimum visibility of its
2110 component types. */
2112 static int
2113 type_visibility (tree type)
2115 int vis = VISIBILITY_DEFAULT;
2116 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2117 return vis;
2120 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2121 specified (or if VISIBILITY is static). If TMPL is true, this
2122 constraint is for a template argument, and takes precedence
2123 over explicitly-specified visibility on the template. */
2125 static void
2126 constrain_visibility (tree decl, int visibility, bool tmpl)
2128 if (visibility == VISIBILITY_ANON)
2130 /* extern "C" declarations aren't affected by the anonymous
2131 namespace. */
2132 if (!DECL_EXTERN_C_P (decl))
2134 TREE_PUBLIC (decl) = 0;
2135 DECL_WEAK (decl) = 0;
2136 DECL_COMMON (decl) = 0;
2137 DECL_COMDAT (decl) = false;
2138 if (VAR_OR_FUNCTION_DECL_P (decl))
2140 struct symtab_node *snode = symtab_node::get (decl);
2142 if (snode)
2143 snode->set_comdat_group (NULL);
2145 DECL_INTERFACE_KNOWN (decl) = 1;
2146 if (DECL_LANG_SPECIFIC (decl))
2147 DECL_NOT_REALLY_EXTERN (decl) = 1;
2150 else if (visibility > DECL_VISIBILITY (decl)
2151 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2153 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2154 /* This visibility was not specified. */
2155 DECL_VISIBILITY_SPECIFIED (decl) = false;
2159 /* Constrain the visibility of DECL based on the visibility of its template
2160 arguments. */
2162 static void
2163 constrain_visibility_for_template (tree decl, tree targs)
2165 /* If this is a template instantiation, check the innermost
2166 template args for visibility constraints. The outer template
2167 args are covered by the class check. */
2168 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2169 int i;
2170 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2172 int vis = 0;
2174 tree arg = TREE_VEC_ELT (args, i-1);
2175 if (TYPE_P (arg))
2176 vis = type_visibility (arg);
2177 else
2179 if (REFERENCE_REF_P (arg))
2180 arg = TREE_OPERAND (arg, 0);
2181 if (TREE_TYPE (arg))
2182 STRIP_NOPS (arg);
2183 if (TREE_CODE (arg) == ADDR_EXPR)
2184 arg = TREE_OPERAND (arg, 0);
2185 if (VAR_OR_FUNCTION_DECL_P (arg))
2187 if (! TREE_PUBLIC (arg))
2188 vis = VISIBILITY_ANON;
2189 else
2190 vis = DECL_VISIBILITY (arg);
2193 if (vis)
2194 constrain_visibility (decl, vis, true);
2198 /* Like c_determine_visibility, but with additional C++-specific
2199 behavior.
2201 Function-scope entities can rely on the function's visibility because
2202 it is set in start_preparsed_function.
2204 Class-scope entities cannot rely on the class's visibility until the end
2205 of the enclosing class definition.
2207 Note that because namespaces have multiple independent definitions,
2208 namespace visibility is handled elsewhere using the #pragma visibility
2209 machinery rather than by decorating the namespace declaration.
2211 The goal is for constraints from the type to give a diagnostic, and
2212 other constraints to be applied silently. */
2214 void
2215 determine_visibility (tree decl)
2217 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2219 /* Only relevant for names with external linkage. */
2220 if (!TREE_PUBLIC (decl))
2221 return;
2223 /* Cloned constructors and destructors get the same visibility as
2224 the underlying function. That should be set up in
2225 maybe_clone_body. */
2226 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2228 bool orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2229 enum symbol_visibility orig_visibility = DECL_VISIBILITY (decl);
2231 /* The decl may be a template instantiation, which could influence
2232 visibilty. */
2233 tree template_decl = NULL_TREE;
2234 if (TREE_CODE (decl) == TYPE_DECL)
2236 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2238 if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
2239 template_decl = decl;
2241 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2242 template_decl = decl;
2244 else if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
2245 template_decl = decl;
2247 /* If DECL is a member of a class, visibility specifiers on the
2248 class can influence the visibility of the DECL. */
2249 tree class_type = NULL_TREE;
2250 if (DECL_CLASS_SCOPE_P (decl))
2251 class_type = DECL_CONTEXT (decl);
2252 else
2254 /* Not a class member. */
2256 /* Virtual tables have DECL_CONTEXT set to their associated class,
2257 so they are automatically handled above. */
2258 gcc_assert (!VAR_P (decl)
2259 || !DECL_VTABLE_OR_VTT_P (decl));
2261 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2263 /* Local statics and classes get the visibility of their
2264 containing function by default, except that
2265 -fvisibility-inlines-hidden doesn't affect them. */
2266 tree fn = DECL_CONTEXT (decl);
2267 if (DECL_VISIBILITY_SPECIFIED (fn))
2269 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2270 DECL_VISIBILITY_SPECIFIED (decl) =
2271 DECL_VISIBILITY_SPECIFIED (fn);
2273 else
2275 if (DECL_CLASS_SCOPE_P (fn))
2276 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2277 else if (determine_hidden_inline (fn))
2279 DECL_VISIBILITY (decl) = default_visibility;
2280 DECL_VISIBILITY_SPECIFIED (decl) =
2281 visibility_options.inpragma;
2283 else
2285 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2286 DECL_VISIBILITY_SPECIFIED (decl) =
2287 DECL_VISIBILITY_SPECIFIED (fn);
2291 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2292 but have no TEMPLATE_INFO. Their containing template
2293 function does, and the local class could be constrained
2294 by that. */
2295 if (template_decl)
2296 template_decl = fn;
2298 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2299 && flag_visibility_ms_compat)
2301 /* Under -fvisibility-ms-compat, types are visible by default,
2302 even though their contents aren't. */
2303 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2304 int underlying_vis = type_visibility (underlying_type);
2305 if (underlying_vis == VISIBILITY_ANON
2306 || (CLASS_TYPE_P (underlying_type)
2307 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2308 constrain_visibility (decl, underlying_vis, false);
2309 else
2310 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2312 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2314 /* tinfo visibility is based on the type it's for. */
2315 constrain_visibility
2316 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2318 /* Give the target a chance to override the visibility associated
2319 with DECL. */
2320 if (TREE_PUBLIC (decl)
2321 && !DECL_REALLY_EXTERN (decl)
2322 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2323 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2324 targetm.cxx.determine_class_data_visibility (decl);
2326 else if (template_decl)
2327 /* Template instantiations and specializations get visibility based
2328 on their template unless they override it with an attribute. */;
2329 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2331 if (determine_hidden_inline (decl))
2332 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2333 else
2335 /* Set default visibility to whatever the user supplied with
2336 #pragma GCC visibility or a namespace visibility attribute. */
2337 DECL_VISIBILITY (decl) = default_visibility;
2338 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2343 if (template_decl)
2345 /* If the specialization doesn't specify visibility, use the
2346 visibility from the template. */
2347 tree tinfo = get_template_info (template_decl);
2348 tree args = TI_ARGS (tinfo);
2349 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2350 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2351 : DECL_ATTRIBUTES (decl));
2353 if (args != error_mark_node)
2355 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2357 if (!DECL_VISIBILITY_SPECIFIED (decl))
2359 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2360 && determine_hidden_inline (decl))
2361 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2362 else
2364 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2365 DECL_VISIBILITY_SPECIFIED (decl)
2366 = DECL_VISIBILITY_SPECIFIED (pattern);
2370 if (args
2371 /* Template argument visibility outweighs #pragma or namespace
2372 visibility, but not an explicit attribute. */
2373 && !lookup_attribute ("visibility", attribs))
2375 int depth = TMPL_ARGS_DEPTH (args);
2376 if (DECL_VISIBILITY_SPECIFIED (decl))
2378 /* A class template member with explicit visibility
2379 overrides the class visibility, so we need to apply
2380 all the levels of template args directly. */
2381 int i;
2382 for (i = 1; i <= depth; ++i)
2384 tree lev = TMPL_ARGS_LEVEL (args, i);
2385 constrain_visibility_for_template (decl, lev);
2388 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2389 /* Limit visibility based on its template arguments. */
2390 constrain_visibility_for_template (decl, args);
2395 if (class_type)
2396 determine_visibility_from_class (decl, class_type);
2398 if (decl_anon_ns_mem_p (decl))
2399 /* Names in an anonymous namespace get internal linkage.
2400 This might change once we implement export. */
2401 constrain_visibility (decl, VISIBILITY_ANON, false);
2402 else if (TREE_CODE (decl) != TYPE_DECL)
2404 /* Propagate anonymity from type to decl. */
2405 int tvis = type_visibility (TREE_TYPE (decl));
2406 if (tvis == VISIBILITY_ANON
2407 || ! DECL_VISIBILITY_SPECIFIED (decl))
2408 constrain_visibility (decl, tvis, false);
2410 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2411 /* DR 757: A type without linkage shall not be used as the type of a
2412 variable or function with linkage, unless
2413 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2414 o the variable or function is not used (3.2 [basic.def.odr]) or is
2415 defined in the same translation unit.
2417 Since non-extern "C" decls need to be defined in the same
2418 translation unit, we can make the type internal. */
2419 constrain_visibility (decl, VISIBILITY_ANON, false);
2421 /* If visibility changed and DECL already has DECL_RTL, ensure
2422 symbol flags are updated. */
2423 if ((DECL_VISIBILITY (decl) != orig_visibility
2424 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2425 && ((VAR_P (decl) && TREE_STATIC (decl))
2426 || TREE_CODE (decl) == FUNCTION_DECL)
2427 && DECL_RTL_SET_P (decl))
2428 make_decl_rtl (decl);
2431 /* By default, static data members and function members receive
2432 the visibility of their containing class. */
2434 static void
2435 determine_visibility_from_class (tree decl, tree class_type)
2437 if (DECL_VISIBILITY_SPECIFIED (decl))
2438 return;
2440 if (determine_hidden_inline (decl))
2441 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2442 else
2444 /* Default to the class visibility. */
2445 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2446 DECL_VISIBILITY_SPECIFIED (decl)
2447 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2450 /* Give the target a chance to override the visibility associated
2451 with DECL. */
2452 if (VAR_P (decl)
2453 && (DECL_TINFO_P (decl)
2454 || (DECL_VTABLE_OR_VTT_P (decl)
2455 /* Construction virtual tables are not exported because
2456 they cannot be referred to from other object files;
2457 their name is not standardized by the ABI. */
2458 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2459 && TREE_PUBLIC (decl)
2460 && !DECL_REALLY_EXTERN (decl)
2461 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2462 targetm.cxx.determine_class_data_visibility (decl);
2465 /* Returns true iff DECL is an inline that should get hidden visibility
2466 because of -fvisibility-inlines-hidden. */
2468 static bool
2469 determine_hidden_inline (tree decl)
2471 return (visibility_options.inlines_hidden
2472 /* Don't do this for inline templates; specializations might not be
2473 inline, and we don't want them to inherit the hidden
2474 visibility. We'll set it here for all inline instantiations. */
2475 && !processing_template_decl
2476 && TREE_CODE (decl) == FUNCTION_DECL
2477 && DECL_DECLARED_INLINE_P (decl)
2478 && (! DECL_LANG_SPECIFIC (decl)
2479 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2482 /* Constrain the visibility of a class TYPE based on the visibility of its
2483 field types. Warn if any fields require lesser visibility. */
2485 void
2486 constrain_class_visibility (tree type)
2488 tree binfo;
2489 tree t;
2490 int i;
2492 int vis = type_visibility (type);
2494 if (vis == VISIBILITY_ANON
2495 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2496 return;
2498 /* Don't warn about visibility if the class has explicit visibility. */
2499 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2500 vis = VISIBILITY_INTERNAL;
2502 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2503 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2505 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2506 int subvis = type_visibility (ftype);
2508 if (subvis == VISIBILITY_ANON)
2510 if (!in_main_input_context())
2512 tree nlt = no_linkage_check (ftype, /*relaxed_p=*/false);
2513 if (nlt)
2515 if (same_type_p (TREE_TYPE (t), nlt))
2516 warning (OPT_Wsubobject_linkage, "\
2517 %qT has a field %qD whose type has no linkage",
2518 type, t);
2519 else
2520 warning (OPT_Wsubobject_linkage, "\
2521 %qT has a field %qD whose type depends on the type %qT which has no linkage",
2522 type, t, nlt);
2524 else
2525 warning (OPT_Wsubobject_linkage, "\
2526 %qT has a field %qD whose type uses the anonymous namespace",
2527 type, t);
2530 else if (MAYBE_CLASS_TYPE_P (ftype)
2531 && vis < VISIBILITY_HIDDEN
2532 && subvis >= VISIBILITY_HIDDEN)
2533 warning (OPT_Wattributes, "\
2534 %qT declared with greater visibility than the type of its field %qD",
2535 type, t);
2538 binfo = TYPE_BINFO (type);
2539 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2541 int subvis = type_visibility (TREE_TYPE (t));
2543 if (subvis == VISIBILITY_ANON)
2545 if (!in_main_input_context())
2547 tree nlt = no_linkage_check (TREE_TYPE (t), /*relaxed_p=*/false);
2548 if (nlt)
2550 if (same_type_p (TREE_TYPE (t), nlt))
2551 warning (OPT_Wsubobject_linkage, "\
2552 %qT has a base %qT whose type has no linkage",
2553 type, TREE_TYPE (t));
2554 else
2555 warning (OPT_Wsubobject_linkage, "\
2556 %qT has a base %qT whose type depends on the type %qT which has no linkage",
2557 type, TREE_TYPE (t), nlt);
2559 else
2560 warning (OPT_Wsubobject_linkage, "\
2561 %qT has a base %qT whose type uses the anonymous namespace",
2562 type, TREE_TYPE (t));
2565 else if (vis < VISIBILITY_HIDDEN
2566 && subvis >= VISIBILITY_HIDDEN)
2567 warning (OPT_Wattributes, "\
2568 %qT declared with greater visibility than its base %qT",
2569 type, TREE_TYPE (t));
2573 /* Functions for adjusting the visibility of a tagged type and its nested
2574 types and declarations when it gets a name for linkage purposes from a
2575 typedef. */
2577 static void bt_reset_linkage_1 (binding_entry, void *);
2578 static void bt_reset_linkage_2 (binding_entry, void *);
2580 /* First reset the visibility of all the types. */
2582 static void
2583 reset_type_linkage_1 (tree type)
2585 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2586 if (CLASS_TYPE_P (type))
2587 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2588 bt_reset_linkage_1, NULL);
2590 static void
2591 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2593 reset_type_linkage_1 (b->type);
2596 /* Then reset the visibility of any static data members or member
2597 functions that use those types. */
2599 static void
2600 reset_decl_linkage (tree decl)
2602 if (TREE_PUBLIC (decl))
2603 return;
2604 if (DECL_CLONED_FUNCTION_P (decl))
2605 return;
2606 TREE_PUBLIC (decl) = true;
2607 DECL_INTERFACE_KNOWN (decl) = false;
2608 determine_visibility (decl);
2609 tentative_decl_linkage (decl);
2612 static void
2613 reset_type_linkage_2 (tree type)
2615 if (CLASS_TYPE_P (type))
2617 if (tree vt = CLASSTYPE_VTABLES (type))
2619 tree name = mangle_vtbl_for_type (type);
2620 DECL_NAME (vt) = name;
2621 SET_DECL_ASSEMBLER_NAME (vt, name);
2622 reset_decl_linkage (vt);
2624 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2626 tree name = mangle_typeinfo_for_type (type);
2627 DECL_NAME (ti) = name;
2628 SET_DECL_ASSEMBLER_NAME (ti, name);
2629 TREE_TYPE (name) = type;
2630 reset_decl_linkage (ti);
2632 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2634 tree mem = STRIP_TEMPLATE (m);
2635 if (TREE_CODE (mem) == VAR_DECL || TREE_CODE (mem) == FUNCTION_DECL)
2636 reset_decl_linkage (mem);
2638 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2639 bt_reset_linkage_2, NULL);
2643 static void
2644 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2646 reset_type_linkage_2 (b->type);
2648 void
2649 reset_type_linkage (tree type)
2651 reset_type_linkage_1 (type);
2652 reset_type_linkage_2 (type);
2655 /* Set up our initial idea of what the linkage of DECL should be. */
2657 void
2658 tentative_decl_linkage (tree decl)
2660 if (DECL_INTERFACE_KNOWN (decl))
2661 /* We've already made a decision as to how this function will
2662 be handled. */;
2663 else if (vague_linkage_p (decl))
2665 if (TREE_CODE (decl) == FUNCTION_DECL
2666 && decl_defined_p (decl))
2668 DECL_EXTERNAL (decl) = 1;
2669 DECL_NOT_REALLY_EXTERN (decl) = 1;
2670 note_vague_linkage_fn (decl);
2671 /* A non-template inline function with external linkage will
2672 always be COMDAT. As we must eventually determine the
2673 linkage of all functions, and as that causes writes to
2674 the data mapped in from the PCH file, it's advantageous
2675 to mark the functions at this point. */
2676 if (DECL_DECLARED_INLINE_P (decl)
2677 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2678 || DECL_DEFAULTED_FN (decl)))
2680 /* This function must have external linkage, as
2681 otherwise DECL_INTERFACE_KNOWN would have been
2682 set. */
2683 gcc_assert (TREE_PUBLIC (decl));
2684 comdat_linkage (decl);
2685 DECL_INTERFACE_KNOWN (decl) = 1;
2688 else if (VAR_P (decl))
2689 maybe_commonize_var (decl);
2693 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2694 for DECL has not already been determined, do so now by setting
2695 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2696 function is called entities with vague linkage whose definitions
2697 are available must have TREE_PUBLIC set.
2699 If this function decides to place DECL in COMDAT, it will set
2700 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2701 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2702 callers defer that decision until it is clear that DECL is actually
2703 required. */
2705 void
2706 import_export_decl (tree decl)
2708 int emit_p;
2709 bool comdat_p;
2710 bool import_p;
2711 tree class_type = NULL_TREE;
2713 if (DECL_INTERFACE_KNOWN (decl))
2714 return;
2716 /* We cannot determine what linkage to give to an entity with vague
2717 linkage until the end of the file. For example, a virtual table
2718 for a class will be defined if and only if the key method is
2719 defined in this translation unit. As a further example, consider
2720 that when compiling a translation unit that uses PCH file with
2721 "-frepo" it would be incorrect to make decisions about what
2722 entities to emit when building the PCH; those decisions must be
2723 delayed until the repository information has been processed. */
2724 gcc_assert (at_eof);
2725 /* Object file linkage for explicit instantiations is handled in
2726 mark_decl_instantiated. For static variables in functions with
2727 vague linkage, maybe_commonize_var is used.
2729 Therefore, the only declarations that should be provided to this
2730 function are those with external linkage that are:
2732 * implicit instantiations of function templates
2734 * inline function
2736 * implicit instantiations of static data members of class
2737 templates
2739 * virtual tables
2741 * typeinfo objects
2743 Furthermore, all entities that reach this point must have a
2744 definition available in this translation unit.
2746 The following assertions check these conditions. */
2747 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2748 /* Any code that creates entities with TREE_PUBLIC cleared should
2749 also set DECL_INTERFACE_KNOWN. */
2750 gcc_assert (TREE_PUBLIC (decl));
2751 if (TREE_CODE (decl) == FUNCTION_DECL)
2752 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2753 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2754 || DECL_DECLARED_INLINE_P (decl));
2755 else
2756 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2757 || DECL_VTABLE_OR_VTT_P (decl)
2758 || DECL_TINFO_P (decl));
2759 /* Check that a definition of DECL is available in this translation
2760 unit. */
2761 gcc_assert (!DECL_REALLY_EXTERN (decl));
2763 /* Assume that DECL will not have COMDAT linkage. */
2764 comdat_p = false;
2765 /* Assume that DECL will not be imported into this translation
2766 unit. */
2767 import_p = false;
2769 /* See if the repository tells us whether or not to emit DECL in
2770 this translation unit. */
2771 emit_p = repo_emit_p (decl);
2772 if (emit_p == 0)
2773 import_p = true;
2774 else if (emit_p == 1)
2776 /* The repository indicates that this entity should be defined
2777 here. Make sure the back end honors that request. */
2778 mark_needed (decl);
2779 /* Output the definition as an ordinary strong definition. */
2780 DECL_EXTERNAL (decl) = 0;
2781 DECL_INTERFACE_KNOWN (decl) = 1;
2782 return;
2785 if (import_p)
2786 /* We have already decided what to do with this DECL; there is no
2787 need to check anything further. */
2789 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2791 class_type = DECL_CONTEXT (decl);
2792 import_export_class (class_type);
2793 if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2794 && CLASSTYPE_INTERFACE_ONLY (class_type))
2795 import_p = true;
2796 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2797 && !CLASSTYPE_USE_TEMPLATE (class_type)
2798 && CLASSTYPE_KEY_METHOD (class_type)
2799 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2800 /* The ABI requires that all virtual tables be emitted with
2801 COMDAT linkage. However, on systems where COMDAT symbols
2802 don't show up in the table of contents for a static
2803 archive, or on systems without weak symbols (where we
2804 approximate COMDAT linkage by using internal linkage), the
2805 linker will report errors about undefined symbols because
2806 it will not see the virtual table definition. Therefore,
2807 in the case that we know that the virtual table will be
2808 emitted in only one translation unit, we make the virtual
2809 table an ordinary definition with external linkage. */
2810 DECL_EXTERNAL (decl) = 0;
2811 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2813 /* CLASS_TYPE is being exported from this translation unit,
2814 so DECL should be defined here. */
2815 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2816 /* If a class is declared in a header with the "extern
2817 template" extension, then it will not be instantiated,
2818 even in translation units that would normally require
2819 it. Often such classes are explicitly instantiated in
2820 one translation unit. Therefore, the explicit
2821 instantiation must be made visible to other translation
2822 units. */
2823 DECL_EXTERNAL (decl) = 0;
2824 else
2826 /* The generic C++ ABI says that class data is always
2827 COMDAT, even if there is a key function. Some
2828 variants (e.g., the ARM EABI) says that class data
2829 only has COMDAT linkage if the class data might be
2830 emitted in more than one translation unit. When the
2831 key method can be inline and is inline, we still have
2832 to arrange for comdat even though
2833 class_data_always_comdat is false. */
2834 if (!CLASSTYPE_KEY_METHOD (class_type)
2835 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2836 || targetm.cxx.class_data_always_comdat ())
2838 /* The ABI requires COMDAT linkage. Normally, we
2839 only emit COMDAT things when they are needed;
2840 make sure that we realize that this entity is
2841 indeed needed. */
2842 comdat_p = true;
2843 mark_needed (decl);
2847 else if (!flag_implicit_templates
2848 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2849 import_p = true;
2850 else
2851 comdat_p = true;
2853 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2855 tree type = TREE_TYPE (DECL_NAME (decl));
2856 if (CLASS_TYPE_P (type))
2858 class_type = type;
2859 import_export_class (type);
2860 if (CLASSTYPE_INTERFACE_KNOWN (type)
2861 && TYPE_POLYMORPHIC_P (type)
2862 && CLASSTYPE_INTERFACE_ONLY (type)
2863 /* If -fno-rtti was specified, then we cannot be sure
2864 that RTTI information will be emitted with the
2865 virtual table of the class, so we must emit it
2866 wherever it is used. */
2867 && flag_rtti)
2868 import_p = true;
2869 else
2871 if (CLASSTYPE_INTERFACE_KNOWN (type)
2872 && !CLASSTYPE_INTERFACE_ONLY (type))
2874 comdat_p = (targetm.cxx.class_data_always_comdat ()
2875 || (CLASSTYPE_KEY_METHOD (type)
2876 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2877 mark_needed (decl);
2878 if (!flag_weak)
2880 comdat_p = false;
2881 DECL_EXTERNAL (decl) = 0;
2884 else
2885 comdat_p = true;
2888 else
2889 comdat_p = true;
2891 else if (DECL_TEMPLOID_INSTANTIATION (decl))
2893 /* DECL is an implicit instantiation of a function or static
2894 data member. */
2895 if ((flag_implicit_templates
2896 && !flag_use_repository)
2897 || (flag_implicit_inline_templates
2898 && TREE_CODE (decl) == FUNCTION_DECL
2899 && DECL_DECLARED_INLINE_P (decl)))
2900 comdat_p = true;
2901 else
2902 /* If we are not implicitly generating templates, then mark
2903 this entity as undefined in this translation unit. */
2904 import_p = true;
2906 else if (DECL_FUNCTION_MEMBER_P (decl))
2908 if (!DECL_DECLARED_INLINE_P (decl))
2910 tree ctype = DECL_CONTEXT (decl);
2911 import_export_class (ctype);
2912 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2914 DECL_NOT_REALLY_EXTERN (decl)
2915 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2916 || (DECL_DECLARED_INLINE_P (decl)
2917 && ! flag_implement_inlines
2918 && !DECL_VINDEX (decl)));
2920 if (!DECL_NOT_REALLY_EXTERN (decl))
2921 DECL_EXTERNAL (decl) = 1;
2923 /* Always make artificials weak. */
2924 if (DECL_ARTIFICIAL (decl) && flag_weak)
2925 comdat_p = true;
2926 else
2927 maybe_make_one_only (decl);
2930 else
2931 comdat_p = true;
2933 else
2934 comdat_p = true;
2936 if (import_p)
2938 /* If we are importing DECL into this translation unit, mark is
2939 an undefined here. */
2940 DECL_EXTERNAL (decl) = 1;
2941 DECL_NOT_REALLY_EXTERN (decl) = 0;
2943 else if (comdat_p)
2945 /* If we decided to put DECL in COMDAT, mark it accordingly at
2946 this point. */
2947 comdat_linkage (decl);
2950 DECL_INTERFACE_KNOWN (decl) = 1;
2953 /* Return an expression that performs the destruction of DECL, which
2954 must be a VAR_DECL whose type has a non-trivial destructor, or is
2955 an array whose (innermost) elements have a non-trivial destructor. */
2957 tree
2958 build_cleanup (tree decl)
2960 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
2961 gcc_assert (clean != NULL_TREE);
2962 return clean;
2965 /* Returns the initialization guard variable for the variable DECL,
2966 which has static storage duration. */
2968 tree
2969 get_guard (tree decl)
2971 tree sname;
2972 tree guard;
2974 sname = mangle_guard_variable (decl);
2975 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2976 if (! guard)
2978 tree guard_type;
2980 /* We use a type that is big enough to contain a mutex as well
2981 as an integer counter. */
2982 guard_type = targetm.cxx.guard_type ();
2983 guard = build_decl (DECL_SOURCE_LOCATION (decl),
2984 VAR_DECL, sname, guard_type);
2986 /* The guard should have the same linkage as what it guards. */
2987 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
2988 TREE_STATIC (guard) = TREE_STATIC (decl);
2989 DECL_COMMON (guard) = DECL_COMMON (decl);
2990 DECL_COMDAT (guard) = DECL_COMDAT (decl);
2991 CP_DECL_THREAD_LOCAL_P (guard) = CP_DECL_THREAD_LOCAL_P (decl);
2992 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
2993 if (DECL_ONE_ONLY (decl))
2994 make_decl_one_only (guard, cxx_comdat_group (guard));
2995 if (TREE_PUBLIC (decl))
2996 DECL_WEAK (guard) = DECL_WEAK (decl);
2997 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
2998 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
3000 DECL_ARTIFICIAL (guard) = 1;
3001 DECL_IGNORED_P (guard) = 1;
3002 TREE_USED (guard) = 1;
3003 pushdecl_top_level_and_finish (guard, NULL_TREE);
3005 return guard;
3008 /* Return an atomic load of src with the appropriate memory model. */
3010 static tree
3011 build_atomic_load_byte (tree src, HOST_WIDE_INT model)
3013 tree ptr_type = build_pointer_type (char_type_node);
3014 tree mem_model = build_int_cst (integer_type_node, model);
3015 tree t, addr, val;
3016 unsigned int size;
3017 int fncode;
3019 size = tree_to_uhwi (TYPE_SIZE_UNIT (char_type_node));
3021 fncode = BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1;
3022 t = builtin_decl_implicit ((enum built_in_function) fncode);
3024 addr = build1 (ADDR_EXPR, ptr_type, src);
3025 val = build_call_expr (t, 2, addr, mem_model);
3026 return val;
3029 /* Return those bits of the GUARD variable that should be set when the
3030 guarded entity is actually initialized. */
3032 static tree
3033 get_guard_bits (tree guard)
3035 if (!targetm.cxx.guard_mask_bit ())
3037 /* We only set the first byte of the guard, in order to leave room
3038 for a mutex in the high-order bits. */
3039 guard = build1 (ADDR_EXPR,
3040 build_pointer_type (TREE_TYPE (guard)),
3041 guard);
3042 guard = build1 (NOP_EXPR,
3043 build_pointer_type (char_type_node),
3044 guard);
3045 guard = build1 (INDIRECT_REF, char_type_node, guard);
3048 return guard;
3051 /* Return an expression which determines whether or not the GUARD
3052 variable has already been initialized. */
3054 tree
3055 get_guard_cond (tree guard, bool thread_safe)
3057 tree guard_value;
3059 if (!thread_safe)
3060 guard = get_guard_bits (guard);
3061 else
3062 guard = build_atomic_load_byte (guard, MEMMODEL_ACQUIRE);
3064 /* Mask off all but the low bit. */
3065 if (targetm.cxx.guard_mask_bit ())
3067 guard_value = integer_one_node;
3068 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3069 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3070 guard = cp_build_binary_op (input_location,
3071 BIT_AND_EXPR, guard, guard_value,
3072 tf_warning_or_error);
3075 guard_value = integer_zero_node;
3076 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3077 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3078 return cp_build_binary_op (input_location,
3079 EQ_EXPR, guard, guard_value,
3080 tf_warning_or_error);
3083 /* Return an expression which sets the GUARD variable, indicating that
3084 the variable being guarded has been initialized. */
3086 tree
3087 set_guard (tree guard)
3089 tree guard_init;
3091 /* Set the GUARD to one. */
3092 guard = get_guard_bits (guard);
3093 guard_init = integer_one_node;
3094 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3095 guard_init = fold_convert (TREE_TYPE (guard), guard_init);
3096 return cp_build_modify_expr (input_location, guard, NOP_EXPR, guard_init,
3097 tf_warning_or_error);
3100 /* Returns true iff we can tell that VAR does not have a dynamic
3101 initializer. */
3103 static bool
3104 var_defined_without_dynamic_init (tree var)
3106 /* If it's defined in another TU, we can't tell. */
3107 if (DECL_EXTERNAL (var))
3108 return false;
3109 /* If it has a non-trivial destructor, registering the destructor
3110 counts as dynamic initialization. */
3111 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3112 return false;
3113 /* If it's in this TU, its initializer has been processed, unless
3114 it's a case of self-initialization, then DECL_INITIALIZED_P is
3115 false while the initializer is handled by finish_id_expression. */
3116 if (!DECL_INITIALIZED_P (var))
3117 return false;
3118 /* If it has no initializer or a constant one, it's not dynamic. */
3119 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3120 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3123 /* Returns true iff VAR is a variable that needs uses to be
3124 wrapped for possible dynamic initialization. */
3126 static bool
3127 var_needs_tls_wrapper (tree var)
3129 return (!error_operand_p (var)
3130 && CP_DECL_THREAD_LOCAL_P (var)
3131 && !DECL_GNU_TLS_P (var)
3132 && !DECL_FUNCTION_SCOPE_P (var)
3133 && !var_defined_without_dynamic_init (var));
3136 /* Get the FUNCTION_DECL for the shared TLS init function for this
3137 translation unit. */
3139 static tree
3140 get_local_tls_init_fn (void)
3142 tree sname = get_identifier ("__tls_init");
3143 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3144 if (!fn)
3146 fn = build_lang_decl (FUNCTION_DECL, sname,
3147 build_function_type (void_type_node,
3148 void_list_node));
3149 SET_DECL_LANGUAGE (fn, lang_c);
3150 TREE_PUBLIC (fn) = false;
3151 DECL_ARTIFICIAL (fn) = true;
3152 mark_used (fn);
3153 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3155 return fn;
3158 /* Get a FUNCTION_DECL for the init function for the thread_local
3159 variable VAR. The init function will be an alias to the function
3160 that initializes all the non-local TLS variables in the translation
3161 unit. The init function is only used by the wrapper function. */
3163 static tree
3164 get_tls_init_fn (tree var)
3166 /* Only C++11 TLS vars need this init fn. */
3167 if (!var_needs_tls_wrapper (var))
3168 return NULL_TREE;
3170 /* If -fno-extern-tls-init, assume that we don't need to call
3171 a tls init function for a variable defined in another TU. */
3172 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3173 return NULL_TREE;
3175 /* If the variable is internal, or if we can't generate aliases,
3176 call the local init function directly. */
3177 if (!TREE_PUBLIC (var) || !TARGET_SUPPORTS_ALIASES)
3178 return get_local_tls_init_fn ();
3180 tree sname = mangle_tls_init_fn (var);
3181 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3182 if (!fn)
3184 fn = build_lang_decl (FUNCTION_DECL, sname,
3185 build_function_type (void_type_node,
3186 void_list_node));
3187 SET_DECL_LANGUAGE (fn, lang_c);
3188 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3189 DECL_ARTIFICIAL (fn) = true;
3190 DECL_COMDAT (fn) = DECL_COMDAT (var);
3191 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3192 if (DECL_ONE_ONLY (var))
3193 make_decl_one_only (fn, cxx_comdat_group (fn));
3194 if (TREE_PUBLIC (var))
3196 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3197 /* If the variable is defined somewhere else and might have static
3198 initialization, make the init function a weak reference. */
3199 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3200 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3201 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3202 && DECL_EXTERNAL (var))
3203 declare_weak (fn);
3204 else
3205 DECL_WEAK (fn) = DECL_WEAK (var);
3207 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3208 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3209 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3210 DECL_IGNORED_P (fn) = 1;
3211 mark_used (fn);
3213 DECL_BEFRIENDING_CLASSES (fn) = var;
3215 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3217 return fn;
3220 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3221 variable VAR. The wrapper function calls the init function (if any) for
3222 VAR and then returns a reference to VAR. The wrapper function is used
3223 in place of VAR everywhere VAR is mentioned. */
3225 tree
3226 get_tls_wrapper_fn (tree var)
3228 /* Only C++11 TLS vars need this wrapper fn. */
3229 if (!var_needs_tls_wrapper (var))
3230 return NULL_TREE;
3232 tree sname = mangle_tls_wrapper_fn (var);
3233 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3234 if (!fn)
3236 /* A named rvalue reference is an lvalue, so the wrapper should
3237 always return an lvalue reference. */
3238 tree type = non_reference (TREE_TYPE (var));
3239 type = build_reference_type (type);
3240 tree fntype = build_function_type (type, void_list_node);
3241 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3242 SET_DECL_LANGUAGE (fn, lang_c);
3243 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3244 DECL_ARTIFICIAL (fn) = true;
3245 DECL_IGNORED_P (fn) = 1;
3246 /* The wrapper is inline and emitted everywhere var is used. */
3247 DECL_DECLARED_INLINE_P (fn) = true;
3248 if (TREE_PUBLIC (var))
3250 comdat_linkage (fn);
3251 #ifdef HAVE_GAS_HIDDEN
3252 /* Make the wrapper bind locally; there's no reason to share
3253 the wrapper between multiple shared objects. */
3254 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3255 DECL_VISIBILITY_SPECIFIED (fn) = true;
3256 #endif
3258 if (!TREE_PUBLIC (fn))
3259 DECL_INTERFACE_KNOWN (fn) = true;
3260 mark_used (fn);
3261 note_vague_linkage_fn (fn);
3263 #if 0
3264 /* We want CSE to commonize calls to the wrapper, but marking it as
3265 pure is unsafe since it has side-effects. I guess we need a new
3266 ECF flag even weaker than ECF_PURE. FIXME! */
3267 DECL_PURE_P (fn) = true;
3268 #endif
3270 DECL_BEFRIENDING_CLASSES (fn) = var;
3272 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3274 return fn;
3277 /* At EOF, generate the definition for the TLS wrapper function FN:
3279 T& var_wrapper() {
3280 if (init_fn) init_fn();
3281 return var;
3282 } */
3284 static void
3285 generate_tls_wrapper (tree fn)
3287 tree var = DECL_BEFRIENDING_CLASSES (fn);
3289 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3290 tree body = begin_function_body ();
3291 /* Only call the init fn if there might be one. */
3292 if (tree init_fn = get_tls_init_fn (var))
3294 tree if_stmt = NULL_TREE;
3295 /* If init_fn is a weakref, make sure it exists before calling. */
3296 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3298 if_stmt = begin_if_stmt ();
3299 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3300 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3301 NE_EXPR, addr, nullptr_node,
3302 tf_warning_or_error);
3303 finish_if_stmt_cond (cond, if_stmt);
3305 finish_expr_stmt (build_cxx_call
3306 (init_fn, 0, NULL, tf_warning_or_error));
3307 if (if_stmt)
3309 finish_then_clause (if_stmt);
3310 finish_if_stmt (if_stmt);
3313 else
3314 /* If there's no initialization, the wrapper is a constant function. */
3315 TREE_READONLY (fn) = true;
3316 finish_return_stmt (convert_from_reference (var));
3317 finish_function_body (body);
3318 expand_or_defer_fn (finish_function (0));
3321 /* Start the process of running a particular set of global constructors
3322 or destructors. Subroutine of do_[cd]tors. Also called from
3323 vtv_start_verification_constructor_init_function. */
3325 static tree
3326 start_objects (int method_type, int initp)
3328 tree body;
3329 tree fndecl;
3330 char type[14];
3332 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3334 if (initp != DEFAULT_INIT_PRIORITY)
3336 char joiner;
3338 #ifdef JOINER
3339 joiner = JOINER;
3340 #else
3341 joiner = '_';
3342 #endif
3344 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3346 else
3347 sprintf (type, "sub_%c", method_type);
3349 fndecl = build_lang_decl (FUNCTION_DECL,
3350 get_file_function_name (type),
3351 build_function_type_list (void_type_node,
3352 NULL_TREE));
3353 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3355 TREE_PUBLIC (current_function_decl) = 0;
3357 /* Mark as artificial because it's not explicitly in the user's
3358 source code. */
3359 DECL_ARTIFICIAL (current_function_decl) = 1;
3361 /* Mark this declaration as used to avoid spurious warnings. */
3362 TREE_USED (current_function_decl) = 1;
3364 /* Mark this function as a global constructor or destructor. */
3365 if (method_type == 'I')
3366 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3367 else
3368 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3370 body = begin_compound_stmt (BCS_FN_BODY);
3372 return body;
3375 /* Finish the process of running a particular set of global constructors
3376 or destructors. Subroutine of do_[cd]tors. */
3378 static void
3379 finish_objects (int method_type, int initp, tree body)
3381 tree fn;
3383 /* Finish up. */
3384 finish_compound_stmt (body);
3385 fn = finish_function (0);
3387 if (method_type == 'I')
3389 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3390 decl_init_priority_insert (fn, initp);
3392 else
3394 DECL_STATIC_DESTRUCTOR (fn) = 1;
3395 decl_fini_priority_insert (fn, initp);
3398 expand_or_defer_fn (fn);
3401 /* The names of the parameters to the function created to handle
3402 initializations and destructions for objects with static storage
3403 duration. */
3404 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3405 #define PRIORITY_IDENTIFIER "__priority"
3407 /* The name of the function we create to handle initializations and
3408 destructions for objects with static storage duration. */
3409 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3411 /* The declaration for the __INITIALIZE_P argument. */
3412 static GTY(()) tree initialize_p_decl;
3414 /* The declaration for the __PRIORITY argument. */
3415 static GTY(()) tree priority_decl;
3417 /* The declaration for the static storage duration function. */
3418 static GTY(()) tree ssdf_decl;
3420 /* All the static storage duration functions created in this
3421 translation unit. */
3422 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3424 /* A map from priority levels to information about that priority
3425 level. There may be many such levels, so efficient lookup is
3426 important. */
3427 static splay_tree priority_info_map;
3429 /* Begins the generation of the function that will handle all
3430 initialization and destruction of objects with static storage
3431 duration. The function generated takes two parameters of type
3432 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3433 nonzero, it performs initializations. Otherwise, it performs
3434 destructions. It only performs those initializations or
3435 destructions with the indicated __PRIORITY. The generated function
3436 returns no value.
3438 It is assumed that this function will only be called once per
3439 translation unit. */
3441 static tree
3442 start_static_storage_duration_function (unsigned count)
3444 tree type;
3445 tree body;
3446 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3448 /* Create the identifier for this function. It will be of the form
3449 SSDF_IDENTIFIER_<number>. */
3450 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3452 type = build_function_type_list (void_type_node,
3453 integer_type_node, integer_type_node,
3454 NULL_TREE);
3456 /* Create the FUNCTION_DECL itself. */
3457 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3458 get_identifier (id),
3459 type);
3460 TREE_PUBLIC (ssdf_decl) = 0;
3461 DECL_ARTIFICIAL (ssdf_decl) = 1;
3463 /* Put this function in the list of functions to be called from the
3464 static constructors and destructors. */
3465 if (!ssdf_decls)
3467 vec_alloc (ssdf_decls, 32);
3469 /* Take this opportunity to initialize the map from priority
3470 numbers to information about that priority level. */
3471 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3472 /*delete_key_fn=*/0,
3473 /*delete_value_fn=*/
3474 (splay_tree_delete_value_fn) &free);
3476 /* We always need to generate functions for the
3477 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3478 priorities later, we'll be sure to find the
3479 DEFAULT_INIT_PRIORITY. */
3480 get_priority_info (DEFAULT_INIT_PRIORITY);
3483 vec_safe_push (ssdf_decls, ssdf_decl);
3485 /* Create the argument list. */
3486 initialize_p_decl = cp_build_parm_decl
3487 (ssdf_decl, get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3488 TREE_USED (initialize_p_decl) = 1;
3489 priority_decl = cp_build_parm_decl
3490 (ssdf_decl, get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3491 TREE_USED (priority_decl) = 1;
3493 DECL_CHAIN (initialize_p_decl) = priority_decl;
3494 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3496 /* Put the function in the global scope. */
3497 pushdecl (ssdf_decl);
3499 /* Start the function itself. This is equivalent to declaring the
3500 function as:
3502 static void __ssdf (int __initialize_p, init __priority_p);
3504 It is static because we only need to call this function from the
3505 various constructor and destructor functions for this module. */
3506 start_preparsed_function (ssdf_decl,
3507 /*attrs=*/NULL_TREE,
3508 SF_PRE_PARSED);
3510 /* Set up the scope of the outermost block in the function. */
3511 body = begin_compound_stmt (BCS_FN_BODY);
3513 return body;
3516 /* Finish the generation of the function which performs initialization
3517 and destruction of objects with static storage duration. After
3518 this point, no more such objects can be created. */
3520 static void
3521 finish_static_storage_duration_function (tree body)
3523 /* Close out the function. */
3524 finish_compound_stmt (body);
3525 expand_or_defer_fn (finish_function (0));
3528 /* Return the information about the indicated PRIORITY level. If no
3529 code to handle this level has yet been generated, generate the
3530 appropriate prologue. */
3532 static priority_info
3533 get_priority_info (int priority)
3535 priority_info pi;
3536 splay_tree_node n;
3538 n = splay_tree_lookup (priority_info_map,
3539 (splay_tree_key) priority);
3540 if (!n)
3542 /* Create a new priority information structure, and insert it
3543 into the map. */
3544 pi = XNEW (struct priority_info_s);
3545 pi->initializations_p = 0;
3546 pi->destructions_p = 0;
3547 splay_tree_insert (priority_info_map,
3548 (splay_tree_key) priority,
3549 (splay_tree_value) pi);
3551 else
3552 pi = (priority_info) n->value;
3554 return pi;
3557 /* The effective initialization priority of a DECL. */
3559 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3560 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3561 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3563 /* Whether a DECL needs a guard to protect it against multiple
3564 initialization. */
3566 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3567 || DECL_ONE_ONLY (decl) \
3568 || DECL_WEAK (decl)))
3570 /* Called from one_static_initialization_or_destruction(),
3571 via walk_tree.
3572 Walks the initializer list of a global variable and looks for
3573 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3574 and that have their DECL_CONTEXT() == NULL.
3575 For each such temporary variable, set their DECL_CONTEXT() to
3576 the current function. This is necessary because otherwise
3577 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3578 when trying to refer to a temporary variable that does not have
3579 it's DECL_CONTECT() properly set. */
3580 static tree
3581 fix_temporary_vars_context_r (tree *node,
3582 int * /*unused*/,
3583 void * /*unused1*/)
3585 gcc_assert (current_function_decl);
3587 if (TREE_CODE (*node) == BIND_EXPR)
3589 tree var;
3591 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3592 if (VAR_P (var)
3593 && !DECL_NAME (var)
3594 && DECL_ARTIFICIAL (var)
3595 && !DECL_CONTEXT (var))
3596 DECL_CONTEXT (var) = current_function_decl;
3599 return NULL_TREE;
3602 /* Set up to handle the initialization or destruction of DECL. If
3603 INITP is nonzero, we are initializing the variable. Otherwise, we
3604 are destroying it. */
3606 static void
3607 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3609 tree guard_if_stmt = NULL_TREE;
3610 tree guard;
3612 /* If we are supposed to destruct and there's a trivial destructor,
3613 nothing has to be done. */
3614 if (!initp
3615 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3616 return;
3618 /* Trick the compiler into thinking we are at the file and line
3619 where DECL was declared so that error-messages make sense, and so
3620 that the debugger will show somewhat sensible file and line
3621 information. */
3622 input_location = DECL_SOURCE_LOCATION (decl);
3624 /* Make sure temporary variables in the initialiser all have
3625 their DECL_CONTEXT() set to a value different from NULL_TREE.
3626 This can happen when global variables initializers are built.
3627 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3628 the temporary variables that might have been generated in the
3629 accompanying initializers is NULL_TREE, meaning the variables have been
3630 declared in the global namespace.
3631 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3632 of the temporaries are set to the current function decl. */
3633 cp_walk_tree_without_duplicates (&init,
3634 fix_temporary_vars_context_r,
3635 NULL);
3637 /* Because of:
3639 [class.access.spec]
3641 Access control for implicit calls to the constructors,
3642 the conversion functions, or the destructor called to
3643 create and destroy a static data member is performed as
3644 if these calls appeared in the scope of the member's
3645 class.
3647 we pretend we are in a static member function of the class of
3648 which the DECL is a member. */
3649 if (member_p (decl))
3651 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3652 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3655 /* Assume we don't need a guard. */
3656 guard = NULL_TREE;
3657 /* We need a guard if this is an object with external linkage that
3658 might be initialized in more than one place. (For example, a
3659 static data member of a template, when the data member requires
3660 construction.) */
3661 if (NEEDS_GUARD_P (decl))
3663 tree guard_cond;
3665 guard = get_guard (decl);
3667 /* When using __cxa_atexit, we just check the GUARD as we would
3668 for a local static. */
3669 if (flag_use_cxa_atexit)
3671 /* When using __cxa_atexit, we never try to destroy
3672 anything from a static destructor. */
3673 gcc_assert (initp);
3674 guard_cond = get_guard_cond (guard, false);
3676 /* If we don't have __cxa_atexit, then we will be running
3677 destructors from .fini sections, or their equivalents. So,
3678 we need to know how many times we've tried to initialize this
3679 object. We do initializations only if the GUARD is zero,
3680 i.e., if we are the first to initialize the variable. We do
3681 destructions only if the GUARD is one, i.e., if we are the
3682 last to destroy the variable. */
3683 else if (initp)
3684 guard_cond
3685 = cp_build_binary_op (input_location,
3686 EQ_EXPR,
3687 cp_build_unary_op (PREINCREMENT_EXPR,
3688 guard,
3689 /*noconvert=*/true,
3690 tf_warning_or_error),
3691 integer_one_node,
3692 tf_warning_or_error);
3693 else
3694 guard_cond
3695 = cp_build_binary_op (input_location,
3696 EQ_EXPR,
3697 cp_build_unary_op (PREDECREMENT_EXPR,
3698 guard,
3699 /*noconvert=*/true,
3700 tf_warning_or_error),
3701 integer_zero_node,
3702 tf_warning_or_error);
3704 guard_if_stmt = begin_if_stmt ();
3705 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3709 /* If we're using __cxa_atexit, we have not already set the GUARD,
3710 so we must do so now. */
3711 if (guard && initp && flag_use_cxa_atexit)
3712 finish_expr_stmt (set_guard (guard));
3714 /* Perform the initialization or destruction. */
3715 if (initp)
3717 if (init)
3719 finish_expr_stmt (init);
3720 if (sanitize_flags_p (SANITIZE_ADDRESS, decl))
3722 varpool_node *vnode = varpool_node::get (decl);
3723 if (vnode)
3724 vnode->dynamically_initialized = 1;
3728 /* If we're using __cxa_atexit, register a function that calls the
3729 destructor for the object. */
3730 if (flag_use_cxa_atexit)
3731 finish_expr_stmt (register_dtor_fn (decl));
3733 else
3734 finish_expr_stmt (build_cleanup (decl));
3736 /* Finish the guard if-stmt, if necessary. */
3737 if (guard)
3739 finish_then_clause (guard_if_stmt);
3740 finish_if_stmt (guard_if_stmt);
3743 /* Now that we're done with DECL we don't need to pretend to be a
3744 member of its class any longer. */
3745 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3746 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3749 /* Generate code to do the initialization or destruction of the decls in VARS,
3750 a TREE_LIST of VAR_DECL with static storage duration.
3751 Whether initialization or destruction is performed is specified by INITP. */
3753 static void
3754 do_static_initialization_or_destruction (tree vars, bool initp)
3756 tree node, init_if_stmt, cond;
3758 /* Build the outer if-stmt to check for initialization or destruction. */
3759 init_if_stmt = begin_if_stmt ();
3760 cond = initp ? integer_one_node : integer_zero_node;
3761 cond = cp_build_binary_op (input_location,
3762 EQ_EXPR,
3763 initialize_p_decl,
3764 cond,
3765 tf_warning_or_error);
3766 finish_if_stmt_cond (cond, init_if_stmt);
3768 /* To make sure dynamic construction doesn't access globals from other
3769 compilation units where they might not be yet constructed, for
3770 -fsanitize=address insert __asan_before_dynamic_init call that
3771 prevents access to either all global variables that need construction
3772 in other compilation units, or at least those that haven't been
3773 initialized yet. Variables that need dynamic construction in
3774 the current compilation unit are kept accessible. */
3775 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
3776 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3778 node = vars;
3779 do {
3780 tree decl = TREE_VALUE (node);
3781 tree priority_if_stmt;
3782 int priority;
3783 priority_info pi;
3785 /* If we don't need a destructor, there's nothing to do. Avoid
3786 creating a possibly empty if-stmt. */
3787 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3789 node = TREE_CHAIN (node);
3790 continue;
3793 /* Remember that we had an initialization or finalization at this
3794 priority. */
3795 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3796 pi = get_priority_info (priority);
3797 if (initp)
3798 pi->initializations_p = 1;
3799 else
3800 pi->destructions_p = 1;
3802 /* Conditionalize this initialization on being in the right priority
3803 and being initializing/finalizing appropriately. */
3804 priority_if_stmt = begin_if_stmt ();
3805 cond = cp_build_binary_op (input_location,
3806 EQ_EXPR,
3807 priority_decl,
3808 build_int_cst (NULL_TREE, priority),
3809 tf_warning_or_error);
3810 finish_if_stmt_cond (cond, priority_if_stmt);
3812 /* Process initializers with same priority. */
3813 for (; node
3814 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3815 node = TREE_CHAIN (node))
3816 /* Do one initialization or destruction. */
3817 one_static_initialization_or_destruction (TREE_VALUE (node),
3818 TREE_PURPOSE (node), initp);
3820 /* Finish up the priority if-stmt body. */
3821 finish_then_clause (priority_if_stmt);
3822 finish_if_stmt (priority_if_stmt);
3824 } while (node);
3826 /* Revert what __asan_before_dynamic_init did by calling
3827 __asan_after_dynamic_init. */
3828 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
3829 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3831 /* Finish up the init/destruct if-stmt body. */
3832 finish_then_clause (init_if_stmt);
3833 finish_if_stmt (init_if_stmt);
3836 /* VARS is a list of variables with static storage duration which may
3837 need initialization and/or finalization. Remove those variables
3838 that don't really need to be initialized or finalized, and return
3839 the resulting list. The order in which the variables appear in
3840 VARS is in reverse order of the order in which they should actually
3841 be initialized. The list we return is in the unreversed order;
3842 i.e., the first variable should be initialized first. */
3844 static tree
3845 prune_vars_needing_no_initialization (tree *vars)
3847 tree *var = vars;
3848 tree result = NULL_TREE;
3850 while (*var)
3852 tree t = *var;
3853 tree decl = TREE_VALUE (t);
3854 tree init = TREE_PURPOSE (t);
3856 /* Deal gracefully with error. */
3857 if (error_operand_p (decl))
3859 var = &TREE_CHAIN (t);
3860 continue;
3863 /* The only things that can be initialized are variables. */
3864 gcc_assert (VAR_P (decl));
3866 /* If this object is not defined, we don't need to do anything
3867 here. */
3868 if (DECL_EXTERNAL (decl))
3870 var = &TREE_CHAIN (t);
3871 continue;
3874 /* Also, if the initializer already contains errors, we can bail
3875 out now. */
3876 if (init && TREE_CODE (init) == TREE_LIST
3877 && value_member (error_mark_node, init))
3879 var = &TREE_CHAIN (t);
3880 continue;
3883 /* This variable is going to need initialization and/or
3884 finalization, so we add it to the list. */
3885 *var = TREE_CHAIN (t);
3886 TREE_CHAIN (t) = result;
3887 result = t;
3890 return result;
3893 /* Make sure we have told the back end about all the variables in
3894 VARS. */
3896 static void
3897 write_out_vars (tree vars)
3899 tree v;
3901 for (v = vars; v; v = TREE_CHAIN (v))
3903 tree var = TREE_VALUE (v);
3904 if (!var_finalized_p (var))
3906 import_export_decl (var);
3907 rest_of_decl_compilation (var, 1, 1);
3912 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3913 (otherwise) that will initialize all global objects with static
3914 storage duration having the indicated PRIORITY. */
3916 static void
3917 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3918 location_t *locus)
3920 char function_key;
3921 tree fndecl;
3922 tree body;
3923 size_t i;
3925 input_location = *locus;
3926 /* ??? */
3927 /* Was: locus->line++; */
3929 /* We use `I' to indicate initialization and `D' to indicate
3930 destruction. */
3931 function_key = constructor_p ? 'I' : 'D';
3933 /* We emit the function lazily, to avoid generating empty
3934 global constructors and destructors. */
3935 body = NULL_TREE;
3937 /* For Objective-C++, we may need to initialize metadata found in this module.
3938 This must be done _before_ any other static initializations. */
3939 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3940 && constructor_p && objc_static_init_needed_p ())
3942 body = start_objects (function_key, priority);
3943 objc_generate_static_init_call (NULL_TREE);
3946 /* Call the static storage duration function with appropriate
3947 arguments. */
3948 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
3950 /* Calls to pure or const functions will expand to nothing. */
3951 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3953 tree call;
3955 if (! body)
3956 body = start_objects (function_key, priority);
3958 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
3959 build_int_cst (NULL_TREE,
3960 constructor_p),
3961 build_int_cst (NULL_TREE,
3962 priority),
3963 NULL_TREE);
3964 finish_expr_stmt (call);
3968 /* Close out the function. */
3969 if (body)
3970 finish_objects (function_key, priority, body);
3973 /* Generate constructor and destructor functions for the priority
3974 indicated by N. */
3976 static int
3977 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
3979 location_t *locus = (location_t *) data;
3980 int priority = (int) n->key;
3981 priority_info pi = (priority_info) n->value;
3983 /* Generate the functions themselves, but only if they are really
3984 needed. */
3985 if (pi->initializations_p)
3986 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
3987 if (pi->destructions_p)
3988 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
3990 /* Keep iterating. */
3991 return 0;
3994 /* Return C++ property of T, based on given operation OP. */
3996 static int
3997 cpp_check (tree t, cpp_operation op)
3999 switch (op)
4001 case HAS_DEPENDENT_TEMPLATE_ARGS:
4003 tree ti = CLASSTYPE_TEMPLATE_INFO (t);
4004 if (!ti)
4005 return 0;
4006 ++processing_template_decl;
4007 const bool dep = any_dependent_template_arguments_p (TI_ARGS (ti));
4008 --processing_template_decl;
4009 return dep;
4011 case IS_ABSTRACT:
4012 return DECL_PURE_VIRTUAL_P (t);
4013 case IS_CONSTRUCTOR:
4014 return DECL_CONSTRUCTOR_P (t);
4015 case IS_DESTRUCTOR:
4016 return DECL_DESTRUCTOR_P (t);
4017 case IS_COPY_CONSTRUCTOR:
4018 return DECL_COPY_CONSTRUCTOR_P (t);
4019 case IS_MOVE_CONSTRUCTOR:
4020 return DECL_MOVE_CONSTRUCTOR_P (t);
4021 case IS_TEMPLATE:
4022 return TREE_CODE (t) == TEMPLATE_DECL;
4023 case IS_TRIVIAL:
4024 return trivial_type_p (t);
4025 default:
4026 return 0;
4030 /* Collect source file references recursively, starting from NAMESPC. */
4032 static void
4033 collect_source_refs (tree namespc)
4035 /* Iterate over names in this name space. */
4036 for (tree t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4037 if (DECL_IS_BUILTIN (t))
4039 else if (TREE_CODE (t) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (t))
4040 collect_source_refs (t);
4041 else
4042 collect_source_ref (DECL_SOURCE_FILE (t));
4045 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4046 starting from NAMESPC. */
4048 static void
4049 collect_ada_namespace (tree namespc, const char *source_file)
4051 tree decl = NAMESPACE_LEVEL (namespc)->names;
4053 /* Collect decls from this namespace. This will skip
4054 NAMESPACE_DECLs (both aliases and regular, it cannot tell). */
4055 collect_ada_nodes (decl, source_file);
4057 /* Now scan for namespace children, and dump them. */
4058 for (; decl; decl = TREE_CHAIN (decl))
4059 if (TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl))
4060 collect_ada_namespace (decl, source_file);
4063 /* Returns true iff there is a definition available for variable or
4064 function DECL. */
4066 bool
4067 decl_defined_p (tree decl)
4069 if (TREE_CODE (decl) == FUNCTION_DECL)
4070 return (DECL_INITIAL (decl) != NULL_TREE
4071 /* A pending instantiation of a friend temploid is defined. */
4072 || (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
4073 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4074 (DECL_TI_TEMPLATE (decl)))));
4075 else
4077 gcc_assert (VAR_P (decl));
4078 return !DECL_EXTERNAL (decl);
4082 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4084 [expr.const]
4086 An integral constant-expression can only involve ... const
4087 variables of integral or enumeration types initialized with
4088 constant expressions ...
4090 C++0x also allows constexpr variables and temporaries initialized
4091 with constant expressions. We handle the former here, but the latter
4092 are just folded away in cxx_eval_constant_expression.
4094 The standard does not require that the expression be non-volatile.
4095 G++ implements the proposed correction in DR 457. */
4097 bool
4098 decl_constant_var_p (tree decl)
4100 if (!decl_maybe_constant_var_p (decl))
4101 return false;
4103 /* We don't know if a template static data member is initialized with
4104 a constant expression until we instantiate its initializer. Even
4105 in the case of a constexpr variable, we can't treat it as a
4106 constant until its initializer is complete in case it's used in
4107 its own initializer. */
4108 maybe_instantiate_decl (decl);
4109 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4112 /* Returns true if DECL could be a symbolic constant variable, depending on
4113 its initializer. */
4115 bool
4116 decl_maybe_constant_var_p (tree decl)
4118 tree type = TREE_TYPE (decl);
4119 if (!VAR_P (decl))
4120 return false;
4121 if (DECL_DECLARED_CONSTEXPR_P (decl))
4122 return true;
4123 if (DECL_HAS_VALUE_EXPR_P (decl))
4124 /* A proxy isn't constant. */
4125 return false;
4126 if (TREE_CODE (type) == REFERENCE_TYPE)
4127 /* References can be constant. */;
4128 else if (CP_TYPE_CONST_NON_VOLATILE_P (type)
4129 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4130 /* And const integers. */;
4131 else
4132 return false;
4134 if (DECL_INITIAL (decl)
4135 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
4136 /* We know the initializer, and it isn't constant. */
4137 return false;
4138 else
4139 return true;
4142 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4143 called from grokfndecl and grokvardecl; in all modes it is called from
4144 cp_write_global_declarations. */
4146 void
4147 no_linkage_error (tree decl)
4149 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4150 /* In C++11 it's ok if the decl is defined. */
4151 return;
4152 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4153 if (t == NULL_TREE)
4154 /* The type that got us on no_linkage_decls must have gotten a name for
4155 linkage purposes. */;
4156 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4157 /* The type might end up having a typedef name for linkage purposes. */
4158 vec_safe_push (no_linkage_decls, decl);
4159 else if (TYPE_UNNAMED_P (t))
4161 bool d = false;
4162 if (cxx_dialect >= cxx11)
4163 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4164 "unnamed type, is used but never defined", decl);
4165 else if (DECL_EXTERN_C_P (decl))
4166 /* Allow this; it's pretty common in C. */;
4167 else if (VAR_P (decl))
4168 /* DRs 132, 319 and 389 seem to indicate types with
4169 no linkage can only be used to declare extern "C"
4170 entities. Since it's not always an error in the
4171 ISO C++ 90 Standard, we only issue a warning. */
4172 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "unnamed type "
4173 "with no linkage used to declare variable %q#D with "
4174 "linkage", decl);
4175 else
4176 d = permerror (DECL_SOURCE_LOCATION (decl), "unnamed type with no "
4177 "linkage used to declare function %q#D with linkage",
4178 decl);
4179 if (d && is_typedef_decl (TYPE_NAME (t)))
4180 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4181 "to the unqualified type, so it is not used for linkage",
4182 TYPE_NAME (t));
4184 else if (cxx_dialect >= cxx11)
4186 if (VAR_P (decl) || !DECL_PURE_VIRTUAL_P (decl))
4187 permerror (DECL_SOURCE_LOCATION (decl),
4188 "%q#D, declared using local type "
4189 "%qT, is used but never defined", decl, t);
4191 else if (VAR_P (decl))
4192 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4193 "used to declare variable %q#D with linkage", t, decl);
4194 else
4195 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4196 "to declare function %q#D with linkage", t, decl);
4199 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4201 static void
4202 collect_all_refs (const char *source_file)
4204 collect_ada_namespace (global_namespace, source_file);
4207 /* Clear DECL_EXTERNAL for NODE. */
4209 static bool
4210 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4212 DECL_EXTERNAL (node->decl) = 0;
4213 return false;
4216 /* Build up the function to run dynamic initializers for thread_local
4217 variables in this translation unit and alias the init functions for the
4218 individual variables to it. */
4220 static void
4221 handle_tls_init (void)
4223 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4224 if (vars == NULL_TREE)
4225 return;
4227 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4229 write_out_vars (vars);
4231 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4232 boolean_type_node);
4233 TREE_PUBLIC (guard) = false;
4234 TREE_STATIC (guard) = true;
4235 DECL_ARTIFICIAL (guard) = true;
4236 DECL_IGNORED_P (guard) = true;
4237 TREE_USED (guard) = true;
4238 CP_DECL_THREAD_LOCAL_P (guard) = true;
4239 set_decl_tls_model (guard, decl_default_tls_model (guard));
4240 pushdecl_top_level_and_finish (guard, NULL_TREE);
4242 tree fn = get_local_tls_init_fn ();
4243 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4244 tree body = begin_function_body ();
4245 tree if_stmt = begin_if_stmt ();
4246 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4247 tf_warning_or_error);
4248 finish_if_stmt_cond (cond, if_stmt);
4249 finish_expr_stmt (cp_build_modify_expr (loc, guard, NOP_EXPR,
4250 boolean_true_node,
4251 tf_warning_or_error));
4252 for (; vars; vars = TREE_CHAIN (vars))
4254 tree var = TREE_VALUE (vars);
4255 tree init = TREE_PURPOSE (vars);
4256 one_static_initialization_or_destruction (var, init, true);
4258 /* Output init aliases even with -fno-extern-tls-init. */
4259 if (TARGET_SUPPORTS_ALIASES && TREE_PUBLIC (var))
4261 tree single_init_fn = get_tls_init_fn (var);
4262 if (single_init_fn == NULL_TREE)
4263 continue;
4264 cgraph_node *alias
4265 = cgraph_node::get_create (fn)->create_same_body_alias
4266 (single_init_fn, fn);
4267 gcc_assert (alias != NULL);
4271 finish_then_clause (if_stmt);
4272 finish_if_stmt (if_stmt);
4273 finish_function_body (body);
4274 expand_or_defer_fn (finish_function (0));
4277 /* We're at the end of compilation, so generate any mangling aliases that
4278 we've been saving up, if DECL is going to be output and ID2 isn't
4279 already taken by another declaration. */
4281 static void
4282 generate_mangling_alias (tree decl, tree id2)
4284 /* If there's a declaration already using this mangled name,
4285 don't create a compatibility alias that conflicts. */
4286 if (IDENTIFIER_GLOBAL_VALUE (id2))
4287 return;
4289 struct cgraph_node *n = NULL;
4290 if (TREE_CODE (decl) == FUNCTION_DECL
4291 && !(n = cgraph_node::get (decl)))
4292 /* Don't create an alias to an unreferenced function. */
4293 return;
4295 tree alias = make_alias_for (decl, id2);
4296 SET_IDENTIFIER_GLOBAL_VALUE (id2, alias);
4297 DECL_IGNORED_P (alias) = 1;
4298 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
4299 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
4300 if (vague_linkage_p (decl))
4301 DECL_WEAK (alias) = 1;
4302 if (TREE_CODE (decl) == FUNCTION_DECL)
4303 n->create_same_body_alias (alias, decl);
4304 else
4305 varpool_node::create_extra_name_alias (alias, decl);
4308 /* Note that we might want to emit an alias with the symbol ID2 for DECL at
4309 the end of translation, for compatibility across bugs in the mangling
4310 implementation. */
4312 void
4313 note_mangling_alias (tree decl, tree id2)
4315 if (TARGET_SUPPORTS_ALIASES)
4317 if (!defer_mangling_aliases)
4318 generate_mangling_alias (decl, id2);
4319 else
4321 vec_safe_push (mangling_aliases, decl);
4322 vec_safe_push (mangling_aliases, id2);
4327 /* Emit all mangling aliases that were deferred up to this point. */
4329 void
4330 generate_mangling_aliases ()
4332 while (!vec_safe_is_empty (mangling_aliases))
4334 tree id2 = mangling_aliases->pop();
4335 tree decl = mangling_aliases->pop();
4336 generate_mangling_alias (decl, id2);
4338 defer_mangling_aliases = false;
4341 /* The entire file is now complete. If requested, dump everything
4342 to a file. */
4344 static void
4345 dump_tu (void)
4347 dump_flags_t flags;
4348 if (FILE *stream = dump_begin (raw_dump_id, &flags))
4350 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4351 dump_end (raw_dump_id, stream);
4355 static location_t locus_at_end_of_parsing;
4357 /* Check the deallocation functions for CODE to see if we want to warn that
4358 only one was defined. */
4360 static void
4361 maybe_warn_sized_delete (enum tree_code code)
4363 tree sized = NULL_TREE;
4364 tree unsized = NULL_TREE;
4366 for (ovl_iterator iter (IDENTIFIER_GLOBAL_VALUE (cp_operator_id (code)));
4367 iter; ++iter)
4369 tree fn = *iter;
4370 /* We're only interested in usual deallocation functions. */
4371 if (!usual_deallocation_fn_p (fn))
4372 continue;
4373 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
4374 unsized = fn;
4375 else
4376 sized = fn;
4378 if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
4379 warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
4380 "the program should also define %qD", sized);
4381 else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
4382 warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
4383 "the program should also define %qD", unsized);
4386 /* Check the global deallocation functions to see if we want to warn about
4387 defining unsized without sized (or vice versa). */
4389 static void
4390 maybe_warn_sized_delete ()
4392 if (!flag_sized_deallocation || !warn_sized_deallocation)
4393 return;
4394 maybe_warn_sized_delete (DELETE_EXPR);
4395 maybe_warn_sized_delete (VEC_DELETE_EXPR);
4398 /* Earlier we left PTRMEM_CST in variable initializers alone so that we could
4399 look them up when evaluating non-type template parameters. Now we need to
4400 lower them to something the back end can understand. */
4402 static void
4403 lower_var_init ()
4405 varpool_node *node;
4406 FOR_EACH_VARIABLE (node)
4408 tree d = node->decl;
4409 if (tree init = DECL_INITIAL (d))
4410 DECL_INITIAL (d) = cplus_expand_constant (init);
4414 /* This routine is called at the end of compilation.
4415 Its job is to create all the code needed to initialize and
4416 destroy the global aggregates. We do the destruction
4417 first, since that way we only need to reverse the decls once. */
4419 void
4420 c_parse_final_cleanups (void)
4422 tree vars;
4423 bool reconsider;
4424 size_t i;
4425 unsigned ssdf_count = 0;
4426 int retries = 0;
4427 tree decl;
4429 locus_at_end_of_parsing = input_location;
4430 at_eof = 1;
4432 /* Bad parse errors. Just forget about it. */
4433 if (! global_bindings_p () || current_class_type
4434 || !vec_safe_is_empty (decl_namespace_list))
4435 return;
4437 /* This is the point to write out a PCH if we're doing that.
4438 In that case we do not want to do anything else. */
4439 if (pch_file)
4441 /* Mangle all symbols at PCH creation time. */
4442 symtab_node *node;
4443 FOR_EACH_SYMBOL (node)
4444 if (! is_a <varpool_node *> (node)
4445 || ! DECL_HARD_REGISTER (node->decl))
4446 DECL_ASSEMBLER_NAME (node->decl);
4447 c_common_write_pch ();
4448 dump_tu ();
4449 /* Ensure even the callers don't try to finalize the CU. */
4450 flag_syntax_only = 1;
4451 return;
4454 timevar_stop (TV_PHASE_PARSING);
4455 timevar_start (TV_PHASE_DEFERRED);
4457 symtab->process_same_body_aliases ();
4459 /* Handle -fdump-ada-spec[-slim] */
4460 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4462 if (flag_dump_ada_spec_slim)
4463 collect_source_ref (main_input_filename);
4464 else
4465 collect_source_refs (global_namespace);
4467 dump_ada_specs (collect_all_refs, cpp_check);
4470 /* FIXME - huh? was input_line -= 1;*/
4472 /* We now have to write out all the stuff we put off writing out.
4473 These include:
4475 o Template specializations that we have not yet instantiated,
4476 but which are needed.
4477 o Initialization and destruction for non-local objects with
4478 static storage duration. (Local objects with static storage
4479 duration are initialized when their scope is first entered,
4480 and are cleaned up via atexit.)
4481 o Virtual function tables.
4483 All of these may cause others to be needed. For example,
4484 instantiating one function may cause another to be needed, and
4485 generating the initializer for an object may cause templates to be
4486 instantiated, etc., etc. */
4488 emit_support_tinfos ();
4492 tree t;
4493 tree decl;
4495 reconsider = false;
4497 /* If there are templates that we've put off instantiating, do
4498 them now. */
4499 instantiate_pending_templates (retries);
4500 ggc_collect ();
4502 /* Write out virtual tables as required. Writing out the
4503 virtual table for a template class may cause the
4504 instantiation of members of that class. If we write out
4505 vtables then we remove the class from our list so we don't
4506 have to look at it again. */
4507 for (i = keyed_classes->length ();
4508 keyed_classes->iterate (--i, &t);)
4509 if (maybe_emit_vtables (t))
4511 reconsider = true;
4512 keyed_classes->unordered_remove (i);
4515 /* Write out needed type info variables. We have to be careful
4516 looping through unemitted decls, because emit_tinfo_decl may
4517 cause other variables to be needed. New elements will be
4518 appended, and we remove from the vector those that actually
4519 get emitted. */
4520 for (i = unemitted_tinfo_decls->length ();
4521 unemitted_tinfo_decls->iterate (--i, &t);)
4522 if (emit_tinfo_decl (t))
4524 reconsider = true;
4525 unemitted_tinfo_decls->unordered_remove (i);
4528 /* The list of objects with static storage duration is built up
4529 in reverse order. We clear STATIC_AGGREGATES so that any new
4530 aggregates added during the initialization of these will be
4531 initialized in the correct order when we next come around the
4532 loop. */
4533 vars = prune_vars_needing_no_initialization (&static_aggregates);
4535 if (vars)
4537 /* We need to start a new initialization function each time
4538 through the loop. That's because we need to know which
4539 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4540 isn't computed until a function is finished, and written
4541 out. That's a deficiency in the back end. When this is
4542 fixed, these initialization functions could all become
4543 inline, with resulting performance improvements. */
4544 tree ssdf_body;
4546 /* Set the line and file, so that it is obviously not from
4547 the source file. */
4548 input_location = locus_at_end_of_parsing;
4549 ssdf_body = start_static_storage_duration_function (ssdf_count);
4551 /* Make sure the back end knows about all the variables. */
4552 write_out_vars (vars);
4554 /* First generate code to do all the initializations. */
4555 if (vars)
4556 do_static_initialization_or_destruction (vars, /*initp=*/true);
4558 /* Then, generate code to do all the destructions. Do these
4559 in reverse order so that the most recently constructed
4560 variable is the first destroyed. If we're using
4561 __cxa_atexit, then we don't need to do this; functions
4562 were registered at initialization time to destroy the
4563 local statics. */
4564 if (!flag_use_cxa_atexit && vars)
4566 vars = nreverse (vars);
4567 do_static_initialization_or_destruction (vars, /*initp=*/false);
4569 else
4570 vars = NULL_TREE;
4572 /* Finish up the static storage duration function for this
4573 round. */
4574 input_location = locus_at_end_of_parsing;
4575 finish_static_storage_duration_function (ssdf_body);
4577 /* All those initializations and finalizations might cause
4578 us to need more inline functions, more template
4579 instantiations, etc. */
4580 reconsider = true;
4581 ssdf_count++;
4582 /* ??? was: locus_at_end_of_parsing.line++; */
4585 /* Now do the same for thread_local variables. */
4586 handle_tls_init ();
4588 /* Go through the set of inline functions whose bodies have not
4589 been emitted yet. If out-of-line copies of these functions
4590 are required, emit them. */
4591 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4593 /* Does it need synthesizing? */
4594 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4595 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4597 /* Even though we're already at the top-level, we push
4598 there again. That way, when we pop back a few lines
4599 hence, all of our state is restored. Otherwise,
4600 finish_function doesn't clean things up, and we end
4601 up with CURRENT_FUNCTION_DECL set. */
4602 push_to_top_level ();
4603 /* The decl's location will mark where it was first
4604 needed. Save that so synthesize method can indicate
4605 where it was needed from, in case of error */
4606 input_location = DECL_SOURCE_LOCATION (decl);
4607 synthesize_method (decl);
4608 pop_from_top_level ();
4609 reconsider = true;
4612 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4613 generate_tls_wrapper (decl);
4615 if (!DECL_SAVED_TREE (decl))
4616 continue;
4618 cgraph_node *node = cgraph_node::get_create (decl);
4620 /* We lie to the back end, pretending that some functions
4621 are not defined when they really are. This keeps these
4622 functions from being put out unnecessarily. But, we must
4623 stop lying when the functions are referenced, or if they
4624 are not comdat since they need to be put out now. If
4625 DECL_INTERFACE_KNOWN, then we have already set
4626 DECL_EXTERNAL appropriately, so there's no need to check
4627 again, and we do not want to clear DECL_EXTERNAL if a
4628 previous call to import_export_decl set it.
4630 This is done in a separate for cycle, because if some
4631 deferred function is contained in another deferred
4632 function later in deferred_fns varray,
4633 rest_of_compilation would skip this function and we
4634 really cannot expand the same function twice. */
4635 import_export_decl (decl);
4636 if (DECL_NOT_REALLY_EXTERN (decl)
4637 && DECL_INITIAL (decl)
4638 && decl_needed_p (decl))
4640 if (node->cpp_implicit_alias)
4641 node = node->get_alias_target ();
4643 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
4644 NULL, true);
4645 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4646 group, we need to mark all symbols in the same comdat group
4647 that way. */
4648 if (node->same_comdat_group)
4649 for (cgraph_node *next
4650 = dyn_cast<cgraph_node *> (node->same_comdat_group);
4651 next != node;
4652 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
4653 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
4654 NULL, true);
4657 /* If we're going to need to write this function out, and
4658 there's already a body for it, create RTL for it now.
4659 (There might be no body if this is a method we haven't
4660 gotten around to synthesizing yet.) */
4661 if (!DECL_EXTERNAL (decl)
4662 && decl_needed_p (decl)
4663 && !TREE_ASM_WRITTEN (decl)
4664 && !node->definition)
4666 /* We will output the function; no longer consider it in this
4667 loop. */
4668 DECL_DEFER_OUTPUT (decl) = 0;
4669 /* Generate RTL for this function now that we know we
4670 need it. */
4671 expand_or_defer_fn (decl);
4672 /* If we're compiling -fsyntax-only pretend that this
4673 function has been written out so that we don't try to
4674 expand it again. */
4675 if (flag_syntax_only)
4676 TREE_ASM_WRITTEN (decl) = 1;
4677 reconsider = true;
4681 if (wrapup_namespace_globals ())
4682 reconsider = true;
4684 /* Static data members are just like namespace-scope globals. */
4685 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4687 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4688 /* Don't write it out if we haven't seen a definition. */
4689 || (DECL_IN_AGGR_P (decl) && !DECL_INLINE_VAR_P (decl)))
4690 continue;
4691 import_export_decl (decl);
4692 /* If this static data member is needed, provide it to the
4693 back end. */
4694 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4695 DECL_EXTERNAL (decl) = 0;
4697 if (vec_safe_length (pending_statics) != 0
4698 && wrapup_global_declarations (pending_statics->address (),
4699 pending_statics->length ()))
4700 reconsider = true;
4702 retries++;
4704 while (reconsider);
4706 lower_var_init ();
4708 generate_mangling_aliases ();
4710 /* All used inline functions must have a definition at this point. */
4711 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4713 if (/* Check online inline functions that were actually used. */
4714 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4715 /* If the definition actually was available here, then the
4716 fact that the function was not defined merely represents
4717 that for some reason (use of a template repository,
4718 #pragma interface, etc.) we decided not to emit the
4719 definition here. */
4720 && !DECL_INITIAL (decl)
4721 /* Don't complain if the template was defined. */
4722 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4723 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4724 (template_for_substitution (decl)))))
4726 warning_at (DECL_SOURCE_LOCATION (decl), 0,
4727 "inline function %qD used but never defined", decl);
4728 /* Avoid a duplicate warning from check_global_declaration. */
4729 TREE_NO_WARNING (decl) = 1;
4733 /* So must decls that use a type with no linkage. */
4734 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4735 no_linkage_error (decl);
4737 maybe_warn_sized_delete ();
4739 /* Then, do the Objective-C stuff. This is where all the
4740 Objective-C module stuff gets generated (symtab,
4741 class/protocol/selector lists etc). This must be done after C++
4742 templates, destructors etc. so that selectors used in C++
4743 templates are properly allocated. */
4744 if (c_dialect_objc ())
4745 objc_write_global_declarations ();
4747 /* We give C linkage to static constructors and destructors. */
4748 push_lang_context (lang_name_c);
4750 /* Generate initialization and destruction functions for all
4751 priorities for which they are required. */
4752 if (priority_info_map)
4753 splay_tree_foreach (priority_info_map,
4754 generate_ctor_and_dtor_functions_for_priority,
4755 /*data=*/&locus_at_end_of_parsing);
4756 else if (c_dialect_objc () && objc_static_init_needed_p ())
4757 /* If this is obj-c++ and we need a static init, call
4758 generate_ctor_or_dtor_function. */
4759 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4760 DEFAULT_INIT_PRIORITY,
4761 &locus_at_end_of_parsing);
4763 /* We're done with the splay-tree now. */
4764 if (priority_info_map)
4765 splay_tree_delete (priority_info_map);
4767 /* Generate any missing aliases. */
4768 maybe_apply_pending_pragma_weaks ();
4770 /* We're done with static constructors, so we can go back to "C++"
4771 linkage now. */
4772 pop_lang_context ();
4774 if (flag_vtable_verify)
4776 vtv_recover_class_info ();
4777 vtv_compute_class_hierarchy_transitive_closure ();
4778 vtv_build_vtable_verify_fndecl ();
4781 perform_deferred_noexcept_checks ();
4783 finish_repo ();
4784 fini_constexpr ();
4786 /* The entire file is now complete. If requested, dump everything
4787 to a file. */
4788 dump_tu ();
4790 if (flag_detailed_statistics)
4792 dump_tree_statistics ();
4793 dump_time_statistics ();
4796 timevar_stop (TV_PHASE_DEFERRED);
4797 timevar_start (TV_PHASE_PARSING);
4799 /* Indicate that we're done with front end processing. */
4800 at_eof = 2;
4803 /* Perform any post compilation-proper cleanups for the C++ front-end.
4804 This should really go away. No front-end should need to do
4805 anything past the compilation process. */
4807 void
4808 cxx_post_compilation_parsing_cleanups (void)
4810 timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS);
4812 if (flag_vtable_verify)
4814 /* Generate the special constructor initialization function that
4815 calls __VLTRegisterPairs, and give it a very high
4816 initialization priority. This must be done after
4817 finalize_compilation_unit so that we have accurate
4818 information about which vtable will actually be emitted. */
4819 vtv_generate_init_routine ();
4822 input_location = locus_at_end_of_parsing;
4824 if (flag_checking)
4825 validate_conversion_obstack ();
4827 timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS);
4830 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4831 function to call in parse-tree form; it has not yet been
4832 semantically analyzed. ARGS are the arguments to the function.
4833 They have already been semantically analyzed. This may change
4834 ARGS. */
4836 tree
4837 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4838 tsubst_flags_t complain)
4840 tree orig_fn;
4841 vec<tree, va_gc> *orig_args = NULL;
4842 tree expr;
4843 tree object;
4845 orig_fn = fn;
4846 object = TREE_OPERAND (fn, 0);
4848 if (processing_template_decl)
4850 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4851 || TREE_CODE (fn) == MEMBER_REF);
4852 if (type_dependent_expression_p (fn)
4853 || any_type_dependent_arguments_p (*args))
4854 return build_min_nt_call_vec (fn, *args);
4856 orig_args = make_tree_vector_copy (*args);
4858 /* Transform the arguments and add the implicit "this"
4859 parameter. That must be done before the FN is transformed
4860 because we depend on the form of FN. */
4861 make_args_non_dependent (*args);
4862 object = build_non_dependent_expr (object);
4863 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4865 if (TREE_CODE (fn) == DOTSTAR_EXPR)
4866 object = cp_build_addr_expr (object, complain);
4867 vec_safe_insert (*args, 0, object);
4869 /* Now that the arguments are done, transform FN. */
4870 fn = build_non_dependent_expr (fn);
4873 /* A qualified name corresponding to a bound pointer-to-member is
4874 represented as an OFFSET_REF:
4876 struct B { void g(); };
4877 void (B::*p)();
4878 void B::g() { (this->*p)(); } */
4879 if (TREE_CODE (fn) == OFFSET_REF)
4881 tree object_addr = cp_build_addr_expr (object, complain);
4882 fn = TREE_OPERAND (fn, 1);
4883 fn = get_member_function_from_ptrfunc (&object_addr, fn,
4884 complain);
4885 vec_safe_insert (*args, 0, object_addr);
4888 if (CLASS_TYPE_P (TREE_TYPE (fn)))
4889 expr = build_op_call (fn, args, complain);
4890 else
4891 expr = cp_build_function_call_vec (fn, args, complain);
4892 if (processing_template_decl && expr != error_mark_node)
4893 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
4895 if (orig_args != NULL)
4896 release_tree_vector (orig_args);
4898 return expr;
4902 void
4903 check_default_args (tree x)
4905 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4906 bool saw_def = false;
4907 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4908 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4910 if (TREE_PURPOSE (arg))
4911 saw_def = true;
4912 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
4914 error ("default argument missing for parameter %P of %q+#D", i, x);
4915 TREE_PURPOSE (arg) = error_mark_node;
4920 /* Return true if function DECL can be inlined. This is used to force
4921 instantiation of methods that might be interesting for inlining. */
4922 bool
4923 possibly_inlined_p (tree decl)
4925 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4926 if (DECL_UNINLINABLE (decl))
4927 return false;
4928 if (!optimize)
4929 return DECL_DECLARED_INLINE_P (decl);
4930 /* When optimizing, we might inline everything when flatten
4931 attribute or heuristics inlining for size or autoinlining
4932 is used. */
4933 return true;
4936 /* Normally, we can wait until instantiation-time to synthesize DECL.
4937 However, if DECL is a static data member initialized with a constant
4938 or a constexpr function, we need it right now because a reference to
4939 such a data member or a call to such function is not value-dependent.
4940 For a function that uses auto in the return type, we need to instantiate
4941 it to find out its type. For OpenMP user defined reductions, we need
4942 them instantiated for reduction clauses which inline them by hand
4943 directly. */
4945 static void
4946 maybe_instantiate_decl (tree decl)
4948 if (DECL_LANG_SPECIFIC (decl)
4949 && DECL_TEMPLATE_INFO (decl)
4950 && (decl_maybe_constant_var_p (decl)
4951 || (TREE_CODE (decl) == FUNCTION_DECL
4952 && DECL_OMP_DECLARE_REDUCTION_P (decl))
4953 || undeduced_auto_decl (decl))
4954 && !DECL_DECLARED_CONCEPT_P (decl)
4955 && !uses_template_parms (DECL_TI_ARGS (decl)))
4957 /* Instantiating a function will result in garbage collection. We
4958 must treat this situation as if we were within the body of a
4959 function so as to avoid collecting live data only referenced from
4960 the stack (such as overload resolution candidates). */
4961 ++function_depth;
4962 instantiate_decl (decl, /*defer_ok=*/false,
4963 /*expl_inst_class_mem_p=*/false);
4964 --function_depth;
4968 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
4969 If DECL is a specialization or implicitly declared class member,
4970 generate the actual definition. Return false if something goes
4971 wrong, true otherwise. */
4973 bool
4974 mark_used (tree decl, tsubst_flags_t complain)
4976 /* If DECL is a BASELINK for a single function, then treat it just
4977 like the DECL for the function. Otherwise, if the BASELINK is
4978 for an overloaded function, we don't know which function was
4979 actually used until after overload resolution. */
4980 if (BASELINK_P (decl))
4982 decl = BASELINK_FUNCTIONS (decl);
4983 if (really_overloaded_fn (decl))
4984 return true;
4985 decl = OVL_FIRST (decl);
4988 /* Set TREE_USED for the benefit of -Wunused. */
4989 TREE_USED (decl) = 1;
4990 /* And for structured bindings also the underlying decl. */
4991 if (DECL_DECOMPOSITION_P (decl) && DECL_DECOMP_BASE (decl))
4992 TREE_USED (DECL_DECOMP_BASE (decl)) = 1;
4994 if (TREE_CODE (decl) == TEMPLATE_DECL)
4995 return true;
4997 if (DECL_CLONED_FUNCTION_P (decl))
4998 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5000 /* Mark enumeration types as used. */
5001 if (TREE_CODE (decl) == CONST_DECL)
5002 used_types_insert (DECL_CONTEXT (decl));
5004 if (TREE_CODE (decl) == FUNCTION_DECL
5005 && !maybe_instantiate_noexcept (decl, complain))
5006 return false;
5008 if (TREE_CODE (decl) == FUNCTION_DECL
5009 && DECL_DELETED_FN (decl))
5011 if (DECL_ARTIFICIAL (decl)
5012 && DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
5013 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
5014 /* We mark a lambda conversion op as deleted if we can't
5015 generate it properly; see maybe_add_lambda_conv_op. */
5016 sorry ("converting lambda which uses %<...%> to function pointer");
5017 else if (complain & tf_error)
5019 error ("use of deleted function %qD", decl);
5020 if (!maybe_explain_implicit_delete (decl))
5021 inform (DECL_SOURCE_LOCATION (decl), "declared here");
5023 return false;
5026 if (TREE_DEPRECATED (decl) && (complain & tf_warning)
5027 && deprecated_state != DEPRECATED_SUPPRESS)
5028 warn_deprecated_use (decl, NULL_TREE);
5030 /* We can only check DECL_ODR_USED on variables or functions with
5031 DECL_LANG_SPECIFIC set, and these are also the only decls that we
5032 might need special handling for. */
5033 if (!VAR_OR_FUNCTION_DECL_P (decl)
5034 || DECL_LANG_SPECIFIC (decl) == NULL
5035 || DECL_THUNK_P (decl))
5037 if (!processing_template_decl
5038 && !require_deduced_type (decl, complain))
5039 return false;
5040 return true;
5043 /* We only want to do this processing once. We don't need to keep trying
5044 to instantiate inline templates, because unit-at-a-time will make sure
5045 we get them compiled before functions that want to inline them. */
5046 if (DECL_ODR_USED (decl))
5047 return true;
5049 /* Normally, we can wait until instantiation-time to synthesize DECL.
5050 However, if DECL is a static data member initialized with a constant
5051 or a constexpr function, we need it right now because a reference to
5052 such a data member or a call to such function is not value-dependent.
5053 For a function that uses auto in the return type, we need to instantiate
5054 it to find out its type. For OpenMP user defined reductions, we need
5055 them instantiated for reduction clauses which inline them by hand
5056 directly. */
5057 maybe_instantiate_decl (decl);
5059 if (processing_template_decl || in_template_function ())
5060 return true;
5062 /* Check this too in case we're within instantiate_non_dependent_expr. */
5063 if (DECL_TEMPLATE_INFO (decl)
5064 && uses_template_parms (DECL_TI_ARGS (decl)))
5065 return true;
5067 if (!require_deduced_type (decl, complain))
5068 return false;
5070 if (builtin_pack_fn_p (decl))
5072 error ("use of built-in parameter pack %qD outside of a template",
5073 DECL_NAME (decl));
5074 return false;
5077 /* If we don't need a value, then we don't need to synthesize DECL. */
5078 if (cp_unevaluated_operand || in_discarded_stmt)
5079 return true;
5081 DECL_ODR_USED (decl) = 1;
5082 if (DECL_CLONED_FUNCTION_P (decl))
5083 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5085 /* DR 757: A type without linkage shall not be used as the type of a
5086 variable or function with linkage, unless
5087 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5088 o the variable or function is not used (3.2 [basic.def.odr]) or is
5089 defined in the same translation unit. */
5090 if (cxx_dialect > cxx98
5091 && decl_linkage (decl) != lk_none
5092 && !DECL_EXTERN_C_P (decl)
5093 && !DECL_ARTIFICIAL (decl)
5094 && !decl_defined_p (decl)
5095 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
5097 if (is_local_extern (decl))
5098 /* There's no way to define a local extern, and adding it to
5099 the vector interferes with GC, so give an error now. */
5100 no_linkage_error (decl);
5101 else
5102 vec_safe_push (no_linkage_decls, decl);
5105 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
5106 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
5107 /* Remember it, so we can check it was defined. */
5108 note_vague_linkage_fn (decl);
5110 /* Is it a synthesized method that needs to be synthesized? */
5111 if (TREE_CODE (decl) == FUNCTION_DECL
5112 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5113 && DECL_DEFAULTED_FN (decl)
5114 /* A function defaulted outside the class is synthesized either by
5115 cp_finish_decl or instantiate_decl. */
5116 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
5117 && ! DECL_INITIAL (decl))
5119 /* Defer virtual destructors so that thunks get the right
5120 linkage. */
5121 if (DECL_VIRTUAL_P (decl) && !at_eof)
5123 note_vague_linkage_fn (decl);
5124 return true;
5127 /* Remember the current location for a function we will end up
5128 synthesizing. Then we can inform the user where it was
5129 required in the case of error. */
5130 DECL_SOURCE_LOCATION (decl) = input_location;
5132 /* Synthesizing an implicitly defined member function will result in
5133 garbage collection. We must treat this situation as if we were
5134 within the body of a function so as to avoid collecting live data
5135 on the stack (such as overload resolution candidates).
5137 We could just let cp_write_global_declarations handle synthesizing
5138 this function by adding it to deferred_fns, but doing
5139 it at the use site produces better error messages. */
5140 ++function_depth;
5141 synthesize_method (decl);
5142 --function_depth;
5143 /* If this is a synthesized method we don't need to
5144 do the instantiation test below. */
5146 else if (VAR_OR_FUNCTION_DECL_P (decl)
5147 && DECL_TEMPLATE_INFO (decl)
5148 && !DECL_DECLARED_CONCEPT_P (decl)
5149 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5150 || always_instantiate_p (decl)))
5151 /* If this is a function or variable that is an instance of some
5152 template, we now know that we will need to actually do the
5153 instantiation. We check that DECL is not an explicit
5154 instantiation because that is not checked in instantiate_decl.
5156 We put off instantiating functions in order to improve compile
5157 times. Maintaining a stack of active functions is expensive,
5158 and the inliner knows to instantiate any functions it might
5159 need. Therefore, we always try to defer instantiation. */
5161 ++function_depth;
5162 instantiate_decl (decl, /*defer_ok=*/true,
5163 /*expl_inst_class_mem_p=*/false);
5164 --function_depth;
5167 return true;
5170 bool
5171 mark_used (tree decl)
5173 return mark_used (decl, tf_warning_or_error);
5176 tree
5177 vtv_start_verification_constructor_init_function (void)
5179 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5182 tree
5183 vtv_finish_verification_constructor_init_function (tree function_body)
5185 tree fn;
5187 finish_compound_stmt (function_body);
5188 fn = finish_function (0);
5189 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5190 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5192 return fn;
5195 #include "gt-cp-decl2.h"