libgo: add misc/cgo files
[official-gcc.git] / gcc / cp / decl2.c
bloba4751460e065172821a49d4d0611d54068b2fd42
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_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 expr = build_array_ref (input_location, array_expr, index_exp);
432 if (processing_template_decl && expr != error_mark_node)
434 if (overload != NULL_TREE)
435 return (build_min_non_dep_op_overload
436 (ARRAY_REF, expr, overload, orig_array_expr, orig_index_exp));
438 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
439 NULL_TREE, NULL_TREE);
441 return expr;
444 /* Given the cast expression EXP, checking out its validity. Either return
445 an error_mark_node if there was an unavoidable error, return a cast to
446 void for trying to delete a pointer w/ the value 0, or return the
447 call to delete. If DOING_VEC is true, we handle things differently
448 for doing an array delete.
449 Implements ARM $5.3.4. This is called from the parser. */
451 tree
452 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
453 tsubst_flags_t complain)
455 tree t, type;
457 if (exp == error_mark_node)
458 return exp;
460 if (processing_template_decl)
462 t = build_min (DELETE_EXPR, void_type_node, exp, size);
463 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
464 DELETE_EXPR_USE_VEC (t) = doing_vec;
465 TREE_SIDE_EFFECTS (t) = 1;
466 return t;
469 /* An array can't have been allocated by new, so complain. */
470 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
471 warning (0, "deleting array %q#E", exp);
473 t = build_expr_type_conversion (WANT_POINTER, exp, true);
475 if (t == NULL_TREE || t == error_mark_node)
477 error ("type %q#T argument given to %<delete%>, expected pointer",
478 TREE_TYPE (exp));
479 return error_mark_node;
482 type = TREE_TYPE (t);
484 /* As of Valley Forge, you can delete a pointer to const. */
486 /* You can't delete functions. */
487 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
489 error ("cannot delete a function. Only pointer-to-objects are "
490 "valid arguments to %<delete%>");
491 return error_mark_node;
494 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
495 if (VOID_TYPE_P (TREE_TYPE (type)))
497 warning (OPT_Wdelete_incomplete, "deleting %qT is undefined", type);
498 doing_vec = 0;
501 /* Deleting a pointer with the value zero is valid and has no effect. */
502 if (integer_zerop (t))
503 return build1 (NOP_EXPR, void_type_node, t);
505 if (doing_vec)
506 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
507 sfk_deleting_destructor,
508 use_global_delete, complain);
509 else
510 return build_delete (type, t, sfk_deleting_destructor,
511 LOOKUP_NORMAL, use_global_delete,
512 complain);
515 /* Report an error if the indicated template declaration is not the
516 sort of thing that should be a member template. */
518 void
519 check_member_template (tree tmpl)
521 tree decl;
523 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
524 decl = DECL_TEMPLATE_RESULT (tmpl);
526 if (TREE_CODE (decl) == FUNCTION_DECL
527 || DECL_ALIAS_TEMPLATE_P (tmpl)
528 || (TREE_CODE (decl) == TYPE_DECL
529 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
531 /* The parser rejects template declarations in local classes
532 (with the exception of generic lambdas). */
533 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
534 /* The parser rejects any use of virtual in a function template. */
535 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
536 && DECL_VIRTUAL_P (decl)));
538 /* The debug-information generating code doesn't know what to do
539 with member templates. */
540 DECL_IGNORED_P (tmpl) = 1;
542 else if (variable_template_p (tmpl))
543 /* OK */;
544 else
545 error ("template declaration of %q#D", decl);
548 /* Sanity check: report error if this function FUNCTION is not
549 really a member of the class (CTYPE) it is supposed to belong to.
550 TEMPLATE_PARMS is used to specify the template parameters of a member
551 template passed as FUNCTION_DECL. If the member template is passed as a
552 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
553 from the declaration. If the function is not a function template, it
554 must be NULL.
555 It returns the original declaration for the function, NULL_TREE if
556 no declaration was found, error_mark_node if an error was emitted. */
558 tree
559 check_classfn (tree ctype, tree function, tree template_parms)
561 int ix;
562 bool is_template;
563 tree pushed_scope;
565 if (DECL_USE_TEMPLATE (function)
566 && !(TREE_CODE (function) == TEMPLATE_DECL
567 && DECL_TEMPLATE_SPECIALIZATION (function))
568 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
569 /* Since this is a specialization of a member template,
570 we're not going to find the declaration in the class.
571 For example, in:
573 struct S { template <typename T> void f(T); };
574 template <> void S::f(int);
576 we're not going to find `S::f(int)', but there's no
577 reason we should, either. We let our callers know we didn't
578 find the method, but we don't complain. */
579 return NULL_TREE;
581 /* Basic sanity check: for a template function, the template parameters
582 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
583 if (TREE_CODE (function) == TEMPLATE_DECL)
585 if (template_parms
586 && !comp_template_parms (template_parms,
587 DECL_TEMPLATE_PARMS (function)))
589 error ("template parameter lists provided don%'t match the "
590 "template parameters of %qD", function);
591 return error_mark_node;
593 template_parms = DECL_TEMPLATE_PARMS (function);
596 /* OK, is this a definition of a member template? */
597 is_template = (template_parms != NULL_TREE);
599 /* [temp.mem]
601 A destructor shall not be a member template. */
602 if (DECL_DESTRUCTOR_P (function) && is_template)
604 error ("destructor %qD declared as member template", function);
605 return error_mark_node;
608 /* We must enter the scope here, because conversion operators are
609 named by target type, and type equivalence relies on typenames
610 resolving within the scope of CTYPE. */
611 pushed_scope = push_scope (ctype);
612 ix = class_method_index_for_fn (complete_type (ctype), function);
613 if (ix >= 0)
615 vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (ctype);
617 for (ovl_iterator iter ((*methods)[ix]); iter; ++iter)
619 tree fndecl = *iter;
620 tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
621 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
623 /* We cannot simply call decls_match because this doesn't
624 work for static member functions that are pretending to
625 be methods, and because the name may have been changed by
626 asm("new_name"). */
628 /* Get rid of the this parameter on functions that become
629 static. */
630 if (DECL_STATIC_FUNCTION_P (fndecl)
631 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
632 p1 = TREE_CHAIN (p1);
634 /* A member template definition only matches a member template
635 declaration. */
636 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
637 continue;
639 /* ref-qualifier or absence of same must match. */
640 if (type_memfn_rqual (TREE_TYPE (function))
641 != type_memfn_rqual (TREE_TYPE (fndecl)))
642 continue;
644 // Include constraints in the match.
645 tree c1 = get_constraints (function);
646 tree c2 = get_constraints (fndecl);
648 /* While finding a match, same types and params are not enough
649 if the function is versioned. Also check version ("target")
650 attributes. */
651 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
652 TREE_TYPE (TREE_TYPE (fndecl)))
653 && compparms (p1, p2)
654 && !targetm.target_option.function_versions (function, fndecl)
655 && (!is_template
656 || comp_template_parms (template_parms,
657 DECL_TEMPLATE_PARMS (fndecl)))
658 && equivalent_constraints (c1, c2)
659 && (DECL_TEMPLATE_SPECIALIZATION (function)
660 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
661 && (!DECL_TEMPLATE_SPECIALIZATION (function)
662 || (DECL_TI_TEMPLATE (function)
663 == DECL_TI_TEMPLATE (fndecl))))
665 if (pushed_scope)
666 pop_scope (pushed_scope);
667 return fndecl;
671 error_at (DECL_SOURCE_LOCATION (function),
672 "prototype for %q#D does not match any in class %qT",
673 function, ctype);
675 const char *format = NULL;
676 tree first = OVL_FIRST ((*methods)[ix]);
677 bool is_conv_op = DECL_CONV_FN_P (first);
678 tree prev = NULL_TREE;
680 if (is_conv_op)
681 ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
684 ovl_iterator iter ((*methods)[ix++]);
685 if (is_conv_op && !DECL_CONV_FN_P (*iter))
686 break;
687 for (; iter; ++iter)
689 if (prev)
691 if (!format)
692 format = N_("candidates are: %+#D");
693 error (format, prev);
694 format = " %+#D";
696 prev = *iter;
699 while (is_conv_op && size_t (ix) < methods->length ());
700 if (prev)
702 if (!format)
703 format = N_("candidate is: %+#D");
704 error (format, prev);
707 else if (!COMPLETE_TYPE_P (ctype))
708 cxx_incomplete_type_error (function, ctype);
709 else
710 error ("no %q#D member function declared in class %qT",
711 function, ctype);
713 if (pushed_scope)
714 pop_scope (pushed_scope);
715 return error_mark_node;
718 /* DECL is a function with vague linkage. Remember it so that at the
719 end of the translation unit we can decide whether or not to emit
720 it. */
722 void
723 note_vague_linkage_fn (tree decl)
725 DECL_DEFER_OUTPUT (decl) = 1;
726 vec_safe_push (deferred_fns, decl);
729 /* As above, but for variable template instantiations. */
731 void
732 note_variable_template_instantiation (tree decl)
734 vec_safe_push (pending_statics, decl);
737 /* We have just processed the DECL, which is a static data member.
738 The other parameters are as for cp_finish_decl. */
740 void
741 finish_static_data_member_decl (tree decl,
742 tree init, bool init_const_expr_p,
743 tree asmspec_tree,
744 int flags)
746 DECL_CONTEXT (decl) = current_class_type;
748 /* We cannot call pushdecl here, because that would fill in the
749 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
750 the right thing, namely, to put this decl out straight away. */
752 if (! processing_template_decl)
753 vec_safe_push (pending_statics, decl);
755 if (LOCAL_CLASS_P (current_class_type)
756 /* We already complained about the template definition. */
757 && !DECL_TEMPLATE_INSTANTIATION (decl))
758 permerror (input_location, "local class %q#T shall not have static data member %q#D",
759 current_class_type, decl);
760 else
761 for (tree t = current_class_type; TYPE_P (t);
762 t = CP_TYPE_CONTEXT (t))
763 if (TYPE_UNNAMED_P (t))
765 if (permerror (DECL_SOURCE_LOCATION (decl),
766 "static data member %qD in unnamed class", decl))
767 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
768 "unnamed class defined here");
769 break;
772 DECL_IN_AGGR_P (decl) = 1;
774 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
775 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
776 SET_VAR_HAD_UNKNOWN_BOUND (decl);
778 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
781 /* DECLARATOR and DECLSPECS correspond to a class member. The other
782 parameters are as for cp_finish_decl. Return the DECL for the
783 class member declared. */
785 tree
786 grokfield (const cp_declarator *declarator,
787 cp_decl_specifier_seq *declspecs,
788 tree init, bool init_const_expr_p,
789 tree asmspec_tree,
790 tree attrlist)
792 tree value;
793 const char *asmspec = 0;
794 int flags;
795 tree name;
797 if (init
798 && TREE_CODE (init) == TREE_LIST
799 && TREE_VALUE (init) == error_mark_node
800 && TREE_CHAIN (init) == NULL_TREE)
801 init = NULL_TREE;
803 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
804 if (! value || value == error_mark_node)
805 /* friend or constructor went bad. */
806 return error_mark_node;
807 if (TREE_TYPE (value) == error_mark_node)
808 return value;
810 if (TREE_CODE (value) == TYPE_DECL && init)
812 error ("typedef %qD is initialized (use decltype instead)", value);
813 init = NULL_TREE;
816 /* Pass friendly classes back. */
817 if (value == void_type_node)
818 return value;
821 name = DECL_NAME (value);
823 if (name != NULL_TREE)
825 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
827 error ("explicit template argument list not allowed");
828 return error_mark_node;
831 if (IDENTIFIER_POINTER (name)[0] == '_'
832 && id_equal (name, "_vptr"))
833 error ("member %qD conflicts with virtual function table field name",
834 value);
837 /* Stash away type declarations. */
838 if (TREE_CODE (value) == TYPE_DECL)
840 DECL_NONLOCAL (value) = 1;
841 DECL_CONTEXT (value) = current_class_type;
843 if (attrlist)
845 int attrflags = 0;
847 /* If this is a typedef that names the class for linkage purposes
848 (7.1.3p8), apply any attributes directly to the type. */
849 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
850 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
851 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
853 cplus_decl_attributes (&value, attrlist, attrflags);
856 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
857 && TREE_TYPE (value) != error_mark_node
858 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
859 set_underlying_type (value);
861 /* It's important that push_template_decl below follows
862 set_underlying_type above so that the created template
863 carries the properly set type of VALUE. */
864 if (processing_template_decl)
865 value = push_template_decl (value);
867 record_locally_defined_typedef (value);
868 return value;
871 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
873 if (!friendp && DECL_IN_AGGR_P (value))
875 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
876 return void_type_node;
879 if (asmspec_tree && asmspec_tree != error_mark_node)
880 asmspec = TREE_STRING_POINTER (asmspec_tree);
882 if (init)
884 if (TREE_CODE (value) == FUNCTION_DECL)
886 if (init == ridpointers[(int)RID_DELETE])
888 DECL_DELETED_FN (value) = 1;
889 DECL_DECLARED_INLINE_P (value) = 1;
890 DECL_INITIAL (value) = error_mark_node;
892 else if (init == ridpointers[(int)RID_DEFAULT])
894 if (defaultable_fn_check (value))
896 DECL_DEFAULTED_FN (value) = 1;
897 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
898 DECL_DECLARED_INLINE_P (value) = 1;
901 else if (TREE_CODE (init) == DEFAULT_ARG)
902 error ("invalid initializer for member function %qD", value);
903 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
905 if (integer_zerop (init))
906 DECL_PURE_VIRTUAL_P (value) = 1;
907 else if (error_operand_p (init))
908 ; /* An error has already been reported. */
909 else
910 error ("invalid initializer for member function %qD",
911 value);
913 else
915 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
916 if (friendp)
917 error ("initializer specified for friend function %qD",
918 value);
919 else
920 error ("initializer specified for static member function %qD",
921 value);
924 else if (TREE_CODE (value) == FIELD_DECL)
925 /* C++11 NSDMI, keep going. */;
926 else if (!VAR_P (value))
927 gcc_unreachable ();
930 /* Pass friend decls back. */
931 if ((TREE_CODE (value) == FUNCTION_DECL
932 || TREE_CODE (value) == TEMPLATE_DECL)
933 && DECL_CONTEXT (value) != current_class_type)
934 return value;
936 /* Need to set this before push_template_decl. */
937 if (VAR_P (value))
938 DECL_CONTEXT (value) = current_class_type;
940 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
942 value = push_template_decl (value);
943 if (error_operand_p (value))
944 return error_mark_node;
947 if (attrlist)
948 cplus_decl_attributes (&value, attrlist, 0);
950 if (init && DIRECT_LIST_INIT_P (init))
951 flags = LOOKUP_NORMAL;
952 else
953 flags = LOOKUP_IMPLICIT;
955 switch (TREE_CODE (value))
957 case VAR_DECL:
958 finish_static_data_member_decl (value, init, init_const_expr_p,
959 asmspec_tree, flags);
960 return value;
962 case FIELD_DECL:
963 if (asmspec)
964 error ("%<asm%> specifiers are not permitted on non-static data members");
965 if (DECL_INITIAL (value) == error_mark_node)
966 init = error_mark_node;
967 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
968 NULL_TREE, flags);
969 DECL_IN_AGGR_P (value) = 1;
970 return value;
972 case FUNCTION_DECL:
973 if (asmspec)
974 set_user_assembler_name (value, asmspec);
976 cp_finish_decl (value,
977 /*init=*/NULL_TREE,
978 /*init_const_expr_p=*/false,
979 asmspec_tree, flags);
981 /* Pass friends back this way. */
982 if (DECL_FRIEND_P (value))
983 return void_type_node;
985 DECL_IN_AGGR_P (value) = 1;
986 return value;
988 default:
989 gcc_unreachable ();
991 return NULL_TREE;
994 /* Like `grokfield', but for bitfields.
995 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
997 tree
998 grokbitfield (const cp_declarator *declarator,
999 cp_decl_specifier_seq *declspecs, tree width,
1000 tree attrlist)
1002 tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, &attrlist);
1004 if (value == error_mark_node)
1005 return NULL_TREE; /* friends went bad. */
1006 if (TREE_TYPE (value) == error_mark_node)
1007 return value;
1009 /* Pass friendly classes back. */
1010 if (VOID_TYPE_P (value))
1011 return void_type_node;
1013 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
1014 && (POINTER_TYPE_P (value)
1015 || !dependent_type_p (TREE_TYPE (value))))
1017 error ("bit-field %qD with non-integral type", value);
1018 return error_mark_node;
1021 if (TREE_CODE (value) == TYPE_DECL)
1023 error ("cannot declare %qD to be a bit-field type", value);
1024 return NULL_TREE;
1027 /* Usually, finish_struct_1 catches bitfields with invalid types.
1028 But, in the case of bitfields with function type, we confuse
1029 ourselves into thinking they are member functions, so we must
1030 check here. */
1031 if (TREE_CODE (value) == FUNCTION_DECL)
1033 error ("cannot declare bit-field %qD with function type",
1034 DECL_NAME (value));
1035 return NULL_TREE;
1038 if (DECL_IN_AGGR_P (value))
1040 error ("%qD is already defined in the class %qT", value,
1041 DECL_CONTEXT (value));
1042 return void_type_node;
1045 if (TREE_STATIC (value))
1047 error ("static member %qD cannot be a bit-field", value);
1048 return NULL_TREE;
1050 cp_finish_decl (value, NULL_TREE, false, NULL_TREE, 0);
1052 if (width != error_mark_node)
1054 /* The width must be an integer type. */
1055 if (!type_dependent_expression_p (width)
1056 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1057 error ("width of bit-field %qD has non-integral type %qT", value,
1058 TREE_TYPE (width));
1059 else
1061 DECL_INITIAL (value) = width;
1062 SET_DECL_C_BIT_FIELD (value);
1066 DECL_IN_AGGR_P (value) = 1;
1068 if (attrlist)
1069 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1071 return value;
1075 /* Returns true iff ATTR is an attribute which needs to be applied at
1076 instantiation time rather than template definition time. */
1078 static bool
1079 is_late_template_attribute (tree attr, tree decl)
1081 tree name = get_attribute_name (attr);
1082 tree args = TREE_VALUE (attr);
1083 const struct attribute_spec *spec = lookup_attribute_spec (name);
1084 tree arg;
1086 if (!spec)
1087 /* Unknown attribute. */
1088 return false;
1090 /* Attribute weak handling wants to write out assembly right away. */
1091 if (is_attribute_p ("weak", name))
1092 return true;
1094 /* Attributes used and unused are applied directly, as they appertain to
1095 decls. */
1096 if (is_attribute_p ("unused", name)
1097 || is_attribute_p ("used", name))
1098 return false;
1100 /* Attribute tls_model wants to modify the symtab. */
1101 if (is_attribute_p ("tls_model", name))
1102 return true;
1104 /* #pragma omp declare simd attribute needs to be always deferred. */
1105 if (flag_openmp
1106 && is_attribute_p ("omp declare simd", name))
1107 return true;
1109 /* An attribute pack is clearly dependent. */
1110 if (args && PACK_EXPANSION_P (args))
1111 return true;
1113 /* If any of the arguments are dependent expressions, we can't evaluate
1114 the attribute until instantiation time. */
1115 for (arg = args; arg; arg = TREE_CHAIN (arg))
1117 tree t = TREE_VALUE (arg);
1119 /* If the first attribute argument is an identifier, only consider
1120 second and following arguments. Attributes like mode, format,
1121 cleanup and several target specific attributes aren't late
1122 just because they have an IDENTIFIER_NODE as first argument. */
1123 if (arg == args && attribute_takes_identifier_p (name)
1124 && identifier_p (t))
1125 continue;
1127 if (value_dependent_expression_p (t)
1128 || type_dependent_expression_p (t))
1129 return true;
1132 if (TREE_CODE (decl) == TYPE_DECL
1133 || TYPE_P (decl)
1134 || spec->type_required)
1136 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1138 /* We can't apply any attributes to a completely unknown type until
1139 instantiation time. */
1140 enum tree_code code = TREE_CODE (type);
1141 if (code == TEMPLATE_TYPE_PARM
1142 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1143 || code == TYPENAME_TYPE)
1144 return true;
1145 /* Also defer most attributes on dependent types. This is not
1146 necessary in all cases, but is the better default. */
1147 else if (dependent_type_p (type)
1148 /* But some attributes specifically apply to templates. */
1149 && !is_attribute_p ("abi_tag", name)
1150 && !is_attribute_p ("deprecated", name)
1151 && !is_attribute_p ("visibility", name))
1152 return true;
1153 else
1154 return false;
1156 else
1157 return false;
1160 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1161 applied at instantiation time and return them. If IS_DEPENDENT is true,
1162 the declaration itself is dependent, so all attributes should be applied
1163 at instantiation time. */
1165 static tree
1166 splice_template_attributes (tree *attr_p, tree decl)
1168 tree *p = attr_p;
1169 tree late_attrs = NULL_TREE;
1170 tree *q = &late_attrs;
1172 if (!p)
1173 return NULL_TREE;
1175 for (; *p; )
1177 if (is_late_template_attribute (*p, decl))
1179 ATTR_IS_DEPENDENT (*p) = 1;
1180 *q = *p;
1181 *p = TREE_CHAIN (*p);
1182 q = &TREE_CHAIN (*q);
1183 *q = NULL_TREE;
1185 else
1186 p = &TREE_CHAIN (*p);
1189 return late_attrs;
1192 /* Remove any late attributes from the list in ATTR_P and attach them to
1193 DECL_P. */
1195 static void
1196 save_template_attributes (tree *attr_p, tree *decl_p)
1198 tree *q;
1200 if (attr_p && *attr_p == error_mark_node)
1201 return;
1203 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1204 if (!late_attrs)
1205 return;
1207 if (DECL_P (*decl_p))
1208 q = &DECL_ATTRIBUTES (*decl_p);
1209 else
1210 q = &TYPE_ATTRIBUTES (*decl_p);
1212 tree old_attrs = *q;
1214 /* Merge the late attributes at the beginning with the attribute
1215 list. */
1216 late_attrs = merge_attributes (late_attrs, *q);
1217 *q = late_attrs;
1219 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1221 /* We've added new attributes directly to the main variant, so
1222 now we need to update all of the other variants to include
1223 these new attributes. */
1224 tree variant;
1225 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1226 variant = TYPE_NEXT_VARIANT (variant))
1228 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1229 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1234 /* True if ATTRS contains any dependent attributes that affect type
1235 identity. */
1237 bool
1238 any_dependent_type_attributes_p (tree attrs)
1240 for (tree a = attrs; a; a = TREE_CHAIN (a))
1241 if (ATTR_IS_DEPENDENT (a))
1243 const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (a));
1244 if (as && as->affects_type_identity)
1245 return true;
1247 return false;
1250 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1251 to a typedef which gives a previously unnamed class or enum a name for
1252 linkage purposes. */
1254 bool
1255 attributes_naming_typedef_ok (tree attrs)
1257 for (; attrs; attrs = TREE_CHAIN (attrs))
1259 tree name = get_attribute_name (attrs);
1260 if (is_attribute_p ("vector_size", name))
1261 return false;
1263 return true;
1266 /* Like reconstruct_complex_type, but handle also template trees. */
1268 tree
1269 cp_reconstruct_complex_type (tree type, tree bottom)
1271 tree inner, outer;
1272 bool late_return_type_p = false;
1274 if (TYPE_PTR_P (type))
1276 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1277 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1278 TYPE_REF_CAN_ALIAS_ALL (type));
1280 else if (TREE_CODE (type) == REFERENCE_TYPE)
1282 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1283 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1284 TYPE_REF_CAN_ALIAS_ALL (type));
1286 else if (TREE_CODE (type) == ARRAY_TYPE)
1288 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1289 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1290 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1291 element type qualification will be handled by the recursive
1292 cp_reconstruct_complex_type call and cp_build_qualified_type
1293 for ARRAY_TYPEs changes the element type. */
1294 return outer;
1296 else if (TREE_CODE (type) == FUNCTION_TYPE)
1298 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1299 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1300 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1301 outer = apply_memfn_quals (outer,
1302 type_memfn_quals (type),
1303 type_memfn_rqual (type));
1305 else if (TREE_CODE (type) == METHOD_TYPE)
1307 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1308 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1309 /* The build_method_type_directly() routine prepends 'this' to argument list,
1310 so we must compensate by getting rid of it. */
1311 outer
1312 = build_method_type_directly
1313 (class_of_this_parm (type), inner,
1314 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1316 else if (TREE_CODE (type) == OFFSET_TYPE)
1318 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1319 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1321 else
1322 return bottom;
1324 if (TYPE_ATTRIBUTES (type))
1325 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1326 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1328 if (late_return_type_p)
1329 TYPE_HAS_LATE_RETURN_TYPE (outer) = 1;
1331 return outer;
1334 /* Replaces any constexpr expression that may be into the attributes
1335 arguments with their reduced value. */
1337 static void
1338 cp_check_const_attributes (tree attributes)
1340 if (attributes == error_mark_node)
1341 return;
1343 tree attr;
1344 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1346 tree arg;
1347 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1349 tree expr = TREE_VALUE (arg);
1350 if (EXPR_P (expr))
1351 TREE_VALUE (arg) = maybe_constant_value (expr);
1356 /* Return true if TYPE is an OpenMP mappable type. */
1357 bool
1358 cp_omp_mappable_type (tree type)
1360 /* Mappable type has to be complete. */
1361 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1362 return false;
1363 /* Arrays have mappable type if the elements have mappable type. */
1364 while (TREE_CODE (type) == ARRAY_TYPE)
1365 type = TREE_TYPE (type);
1366 /* A mappable type cannot contain virtual members. */
1367 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1368 return false;
1369 /* All data members must be non-static. */
1370 if (CLASS_TYPE_P (type))
1372 tree field;
1373 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1374 if (VAR_P (field))
1375 return false;
1376 /* All fields must have mappable types. */
1377 else if (TREE_CODE (field) == FIELD_DECL
1378 && !cp_omp_mappable_type (TREE_TYPE (field)))
1379 return false;
1381 return true;
1384 /* Like decl_attributes, but handle C++ complexity. */
1386 void
1387 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1389 if (*decl == NULL_TREE || *decl == void_type_node
1390 || *decl == error_mark_node)
1391 return;
1393 /* Add implicit "omp declare target" attribute if requested. */
1394 if (scope_chain->omp_declare_target_attribute
1395 && ((VAR_P (*decl)
1396 && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
1397 || TREE_CODE (*decl) == FUNCTION_DECL))
1399 if (VAR_P (*decl)
1400 && DECL_CLASS_SCOPE_P (*decl))
1401 error ("%q+D static data member inside of declare target directive",
1402 *decl);
1403 else if (!processing_template_decl
1404 && VAR_P (*decl)
1405 && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1406 error ("%q+D in declare target directive does not have mappable type",
1407 *decl);
1408 else
1409 attributes = tree_cons (get_identifier ("omp declare target"),
1410 NULL_TREE, attributes);
1413 if (processing_template_decl)
1415 if (check_for_bare_parameter_packs (attributes))
1416 return;
1418 save_template_attributes (&attributes, decl);
1421 cp_check_const_attributes (attributes);
1423 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1424 decl = &DECL_TEMPLATE_RESULT (*decl);
1426 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1428 attributes
1429 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1430 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1431 attributes, flags);
1433 else
1434 decl_attributes (decl, attributes, flags);
1436 if (TREE_CODE (*decl) == TYPE_DECL)
1437 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1439 /* Propagate deprecation out to the template. */
1440 if (TREE_DEPRECATED (*decl))
1441 if (tree ti = get_template_info (*decl))
1443 tree tmpl = TI_TEMPLATE (ti);
1444 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1445 : DECL_TEMPLATE_RESULT (tmpl));
1446 if (*decl == pattern)
1447 TREE_DEPRECATED (tmpl) = true;
1451 /* Walks through the namespace- or function-scope anonymous union
1452 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1453 Returns one of the fields for use in the mangled name. */
1455 static tree
1456 build_anon_union_vars (tree type, tree object)
1458 tree main_decl = NULL_TREE;
1459 tree field;
1461 /* Rather than write the code to handle the non-union case,
1462 just give an error. */
1463 if (TREE_CODE (type) != UNION_TYPE)
1465 error ("anonymous struct not inside named type");
1466 return error_mark_node;
1469 for (field = TYPE_FIELDS (type);
1470 field != NULL_TREE;
1471 field = DECL_CHAIN (field))
1473 tree decl;
1474 tree ref;
1476 if (DECL_ARTIFICIAL (field))
1477 continue;
1478 if (TREE_CODE (field) != FIELD_DECL)
1480 permerror (DECL_SOURCE_LOCATION (field),
1481 "%q#D invalid; an anonymous union can only "
1482 "have non-static data members", field);
1483 continue;
1486 if (TREE_PRIVATE (field))
1487 permerror (DECL_SOURCE_LOCATION (field),
1488 "private member %q#D in anonymous union", field);
1489 else if (TREE_PROTECTED (field))
1490 permerror (DECL_SOURCE_LOCATION (field),
1491 "protected member %q#D in anonymous union", field);
1493 if (processing_template_decl)
1494 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1495 DECL_NAME (field), NULL_TREE);
1496 else
1497 ref = build_class_member_access_expr (object, field, NULL_TREE,
1498 false, tf_warning_or_error);
1500 if (DECL_NAME (field))
1502 tree base;
1504 decl = build_decl (input_location,
1505 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1506 DECL_ANON_UNION_VAR_P (decl) = 1;
1507 DECL_ARTIFICIAL (decl) = 1;
1509 base = get_base_address (object);
1510 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1511 TREE_STATIC (decl) = TREE_STATIC (base);
1512 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1514 SET_DECL_VALUE_EXPR (decl, ref);
1515 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1517 decl = pushdecl (decl);
1519 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1520 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1521 else
1522 decl = 0;
1524 if (main_decl == NULL_TREE)
1525 main_decl = decl;
1528 return main_decl;
1531 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1532 anonymous union, then all members must be laid out together. PUBLIC_P
1533 is nonzero if this union is not declared static. */
1535 void
1536 finish_anon_union (tree anon_union_decl)
1538 tree type;
1539 tree main_decl;
1540 bool public_p;
1542 if (anon_union_decl == error_mark_node)
1543 return;
1545 type = TREE_TYPE (anon_union_decl);
1546 public_p = TREE_PUBLIC (anon_union_decl);
1548 /* The VAR_DECL's context is the same as the TYPE's context. */
1549 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1551 if (TYPE_FIELDS (type) == NULL_TREE)
1552 return;
1554 if (public_p)
1556 error ("namespace-scope anonymous aggregates must be static");
1557 return;
1560 main_decl = build_anon_union_vars (type, anon_union_decl);
1561 if (main_decl == error_mark_node)
1562 return;
1563 if (main_decl == NULL_TREE)
1565 warning (0, "anonymous union with no members");
1566 return;
1569 if (!processing_template_decl)
1571 /* Use main_decl to set the mangled name. */
1572 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1573 maybe_commonize_var (anon_union_decl);
1574 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1575 mangle_decl (anon_union_decl);
1576 DECL_NAME (anon_union_decl) = NULL_TREE;
1579 pushdecl (anon_union_decl);
1580 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1583 /* Auxiliary functions to make type signatures for
1584 `operator new' and `operator delete' correspond to
1585 what compiler will be expecting. */
1587 tree
1588 coerce_new_type (tree type)
1590 int e = 0;
1591 tree args = TYPE_ARG_TYPES (type);
1593 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1595 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1597 e = 1;
1598 error ("%<operator new%> must return type %qT", ptr_type_node);
1601 if (args && args != void_list_node)
1603 if (TREE_PURPOSE (args))
1605 /* [basic.stc.dynamic.allocation]
1607 The first parameter shall not have an associated default
1608 argument. */
1609 error ("the first parameter of %<operator new%> cannot "
1610 "have a default argument");
1611 /* Throw away the default argument. */
1612 TREE_PURPOSE (args) = NULL_TREE;
1615 if (!same_type_p (TREE_VALUE (args), size_type_node))
1617 e = 2;
1618 args = TREE_CHAIN (args);
1621 else
1622 e = 2;
1624 if (e == 2)
1625 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1626 "as first parameter", size_type_node);
1628 switch (e)
1630 case 2:
1631 args = tree_cons (NULL_TREE, size_type_node, args);
1632 /* Fall through. */
1633 case 1:
1634 type = build_exception_variant
1635 (build_function_type (ptr_type_node, args),
1636 TYPE_RAISES_EXCEPTIONS (type));
1637 /* Fall through. */
1638 default:;
1640 return type;
1643 tree
1644 coerce_delete_type (tree type)
1646 int e = 0;
1647 tree args = TYPE_ARG_TYPES (type);
1649 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1651 if (!same_type_p (TREE_TYPE (type), void_type_node))
1653 e = 1;
1654 error ("%<operator delete%> must return type %qT", void_type_node);
1657 if (!args || args == void_list_node
1658 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1660 e = 2;
1661 if (args && args != void_list_node)
1662 args = TREE_CHAIN (args);
1663 error ("%<operator delete%> takes type %qT as first parameter",
1664 ptr_type_node);
1666 switch (e)
1668 case 2:
1669 args = tree_cons (NULL_TREE, ptr_type_node, args);
1670 /* Fall through. */
1671 case 1:
1672 type = build_exception_variant
1673 (build_function_type (void_type_node, args),
1674 TYPE_RAISES_EXCEPTIONS (type));
1675 /* Fall through. */
1676 default:;
1679 return type;
1682 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1683 and mark them as needed. */
1685 static void
1686 mark_vtable_entries (tree decl)
1688 tree fnaddr;
1689 unsigned HOST_WIDE_INT idx;
1691 /* It's OK for the vtable to refer to deprecated virtual functions. */
1692 warning_sentinel w(warn_deprecated_decl);
1694 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1695 idx, fnaddr)
1697 tree fn;
1699 STRIP_NOPS (fnaddr);
1701 if (TREE_CODE (fnaddr) != ADDR_EXPR
1702 && TREE_CODE (fnaddr) != FDESC_EXPR)
1703 /* This entry is an offset: a virtual base class offset, a
1704 virtual call offset, an RTTI offset, etc. */
1705 continue;
1707 fn = TREE_OPERAND (fnaddr, 0);
1708 TREE_ADDRESSABLE (fn) = 1;
1709 /* When we don't have vcall offsets, we output thunks whenever
1710 we output the vtables that contain them. With vcall offsets,
1711 we know all the thunks we'll need when we emit a virtual
1712 function, so we emit the thunks there instead. */
1713 if (DECL_THUNK_P (fn))
1714 use_thunk (fn, /*emit_p=*/0);
1715 mark_used (fn);
1719 /* Set DECL up to have the closest approximation of "initialized common"
1720 linkage available. */
1722 void
1723 comdat_linkage (tree decl)
1725 if (flag_weak)
1726 make_decl_one_only (decl, cxx_comdat_group (decl));
1727 else if (TREE_CODE (decl) == FUNCTION_DECL
1728 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1729 /* We can just emit function and compiler-generated variables
1730 statically; having multiple copies is (for the most part) only
1731 a waste of space.
1733 There are two correctness issues, however: the address of a
1734 template instantiation with external linkage should be the
1735 same, independent of what translation unit asks for the
1736 address, and this will not hold when we emit multiple copies of
1737 the function. However, there's little else we can do.
1739 Also, by default, the typeinfo implementation assumes that
1740 there will be only one copy of the string used as the name for
1741 each type. Therefore, if weak symbols are unavailable, the
1742 run-time library should perform a more conservative check; it
1743 should perform a string comparison, rather than an address
1744 comparison. */
1745 TREE_PUBLIC (decl) = 0;
1746 else
1748 /* Static data member template instantiations, however, cannot
1749 have multiple copies. */
1750 if (DECL_INITIAL (decl) == 0
1751 || DECL_INITIAL (decl) == error_mark_node)
1752 DECL_COMMON (decl) = 1;
1753 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1755 DECL_COMMON (decl) = 1;
1756 DECL_INITIAL (decl) = error_mark_node;
1758 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1760 /* We can't do anything useful; leave vars for explicit
1761 instantiation. */
1762 DECL_EXTERNAL (decl) = 1;
1763 DECL_NOT_REALLY_EXTERN (decl) = 0;
1767 if (TREE_PUBLIC (decl))
1768 DECL_COMDAT (decl) = 1;
1771 /* For win32 we also want to put explicit instantiations in
1772 linkonce sections, so that they will be merged with implicit
1773 instantiations; otherwise we get duplicate symbol errors.
1774 For Darwin we do not want explicit instantiations to be
1775 linkonce. */
1777 void
1778 maybe_make_one_only (tree decl)
1780 /* We used to say that this was not necessary on targets that support weak
1781 symbols, because the implicit instantiations will defer to the explicit
1782 one. However, that's not actually the case in SVR4; a strong definition
1783 after a weak one is an error. Also, not making explicit
1784 instantiations one_only means that we can end up with two copies of
1785 some template instantiations. */
1786 if (! flag_weak)
1787 return;
1789 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1790 we can get away with not emitting them if they aren't used. We need
1791 to for variables so that cp_finish_decl will update their linkage,
1792 because their DECL_INITIAL may not have been set properly yet. */
1794 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1795 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1796 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1798 make_decl_one_only (decl, cxx_comdat_group (decl));
1800 if (VAR_P (decl))
1802 varpool_node *node = varpool_node::get_create (decl);
1803 DECL_COMDAT (decl) = 1;
1804 /* Mark it needed so we don't forget to emit it. */
1805 node->forced_by_abi = true;
1806 TREE_USED (decl) = 1;
1811 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1812 This predicate will give the right answer during parsing of the
1813 function, which other tests may not. */
1815 bool
1816 vague_linkage_p (tree decl)
1818 if (!TREE_PUBLIC (decl))
1820 /* maybe_thunk_body clears TREE_PUBLIC on the maybe-in-charge 'tor
1821 variants, check one of the "clones" for the real linkage. */
1822 if ((DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
1823 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl))
1824 && DECL_CHAIN (decl)
1825 && DECL_CLONED_FUNCTION (DECL_CHAIN (decl)))
1826 return vague_linkage_p (DECL_CHAIN (decl));
1828 gcc_checking_assert (!DECL_COMDAT (decl));
1829 return false;
1831 /* Unfortunately, import_export_decl has not always been called
1832 before the function is processed, so we cannot simply check
1833 DECL_COMDAT. */
1834 if (DECL_COMDAT (decl)
1835 || (TREE_CODE (decl) == FUNCTION_DECL
1836 && DECL_DECLARED_INLINE_P (decl))
1837 || (DECL_LANG_SPECIFIC (decl)
1838 && DECL_TEMPLATE_INSTANTIATION (decl))
1839 || (VAR_P (decl) && DECL_INLINE_VAR_P (decl)))
1840 return true;
1841 else if (DECL_FUNCTION_SCOPE_P (decl))
1842 /* A local static in an inline effectively has vague linkage. */
1843 return (TREE_STATIC (decl)
1844 && vague_linkage_p (DECL_CONTEXT (decl)));
1845 else
1846 return false;
1849 /* Determine whether or not we want to specifically import or export CTYPE,
1850 using various heuristics. */
1852 static void
1853 import_export_class (tree ctype)
1855 /* -1 for imported, 1 for exported. */
1856 int import_export = 0;
1858 /* It only makes sense to call this function at EOF. The reason is
1859 that this function looks at whether or not the first non-inline
1860 non-abstract virtual member function has been defined in this
1861 translation unit. But, we can't possibly know that until we've
1862 seen the entire translation unit. */
1863 gcc_assert (at_eof);
1865 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1866 return;
1868 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1869 we will have CLASSTYPE_INTERFACE_ONLY set but not
1870 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1871 heuristic because someone will supply a #pragma implementation
1872 elsewhere, and deducing it here would produce a conflict. */
1873 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1874 return;
1876 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1877 import_export = -1;
1878 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1879 import_export = 1;
1880 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1881 && !flag_implicit_templates)
1882 /* For a template class, without -fimplicit-templates, check the
1883 repository. If the virtual table is assigned to this
1884 translation unit, then export the class; otherwise, import
1885 it. */
1886 import_export = repo_export_class_p (ctype) ? 1 : -1;
1887 else if (TYPE_POLYMORPHIC_P (ctype))
1889 /* The ABI specifies that the virtual table and associated
1890 information are emitted with the key method, if any. */
1891 tree method = CLASSTYPE_KEY_METHOD (ctype);
1892 /* If weak symbol support is not available, then we must be
1893 careful not to emit the vtable when the key function is
1894 inline. An inline function can be defined in multiple
1895 translation units. If we were to emit the vtable in each
1896 translation unit containing a definition, we would get
1897 multiple definition errors at link-time. */
1898 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1899 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1902 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1903 a definition anywhere else. */
1904 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1905 import_export = 0;
1907 /* Allow back ends the chance to overrule the decision. */
1908 if (targetm.cxx.import_export_class)
1909 import_export = targetm.cxx.import_export_class (ctype, import_export);
1911 if (import_export)
1913 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1914 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1918 /* Return true if VAR has already been provided to the back end; in that
1919 case VAR should not be modified further by the front end. */
1920 static bool
1921 var_finalized_p (tree var)
1923 return varpool_node::get_create (var)->definition;
1926 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1927 must be emitted in this translation unit. Mark it as such. */
1929 void
1930 mark_needed (tree decl)
1932 TREE_USED (decl) = 1;
1933 if (TREE_CODE (decl) == FUNCTION_DECL)
1935 /* Extern inline functions don't become needed when referenced.
1936 If we know a method will be emitted in other TU and no new
1937 functions can be marked reachable, just use the external
1938 definition. */
1939 struct cgraph_node *node = cgraph_node::get_create (decl);
1940 node->forced_by_abi = true;
1942 /* #pragma interface and -frepo code can call mark_needed for
1943 maybe-in-charge 'tors; mark the clones as well. */
1944 tree clone;
1945 FOR_EACH_CLONE (clone, decl)
1946 mark_needed (clone);
1948 else if (VAR_P (decl))
1950 varpool_node *node = varpool_node::get_create (decl);
1951 /* C++ frontend use mark_decl_references to force COMDAT variables
1952 to be output that might appear dead otherwise. */
1953 node->forced_by_abi = true;
1957 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1958 returns true if a definition of this entity should be provided in
1959 this object file. Callers use this function to determine whether
1960 or not to let the back end know that a definition of DECL is
1961 available in this translation unit. */
1963 bool
1964 decl_needed_p (tree decl)
1966 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
1967 /* This function should only be called at the end of the translation
1968 unit. We cannot be sure of whether or not something will be
1969 COMDAT until that point. */
1970 gcc_assert (at_eof);
1972 /* All entities with external linkage that are not COMDAT/EXTERN should be
1973 emitted; they may be referred to from other object files. */
1974 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
1975 return true;
1976 /* Functions marked "dllexport" must be emitted so that they are
1977 visible to other DLLs. */
1978 if (flag_keep_inline_dllexport
1979 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
1980 return true;
1982 /* When not optimizing, do not bother to produce definitions for extern
1983 symbols. */
1984 if (DECL_REALLY_EXTERN (decl)
1985 && ((TREE_CODE (decl) != FUNCTION_DECL
1986 && !optimize)
1987 || (TREE_CODE (decl) == FUNCTION_DECL
1988 && !opt_for_fn (decl, optimize)))
1989 && !lookup_attribute ("always_inline", decl))
1990 return false;
1992 /* If this entity was used, let the back end see it; it will decide
1993 whether or not to emit it into the object file. */
1994 if (TREE_USED (decl))
1995 return true;
1996 /* Virtual functions might be needed for devirtualization. */
1997 if (flag_devirtualize
1998 && TREE_CODE (decl) == FUNCTION_DECL
1999 && DECL_VIRTUAL_P (decl))
2000 return true;
2001 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2002 reference to DECL might cause it to be emitted later. */
2003 return false;
2006 /* If necessary, write out the vtables for the dynamic class CTYPE.
2007 Returns true if any vtables were emitted. */
2009 static bool
2010 maybe_emit_vtables (tree ctype)
2012 tree vtbl;
2013 tree primary_vtbl;
2014 int needed = 0;
2015 varpool_node *current = NULL, *last = NULL;
2017 /* If the vtables for this class have already been emitted there is
2018 nothing more to do. */
2019 primary_vtbl = CLASSTYPE_VTABLES (ctype);
2020 if (var_finalized_p (primary_vtbl))
2021 return false;
2022 /* Ignore dummy vtables made by get_vtable_decl. */
2023 if (TREE_TYPE (primary_vtbl) == void_type_node)
2024 return false;
2026 /* On some targets, we cannot determine the key method until the end
2027 of the translation unit -- which is when this function is
2028 called. */
2029 if (!targetm.cxx.key_method_may_be_inline ())
2030 determine_key_method (ctype);
2032 /* See if any of the vtables are needed. */
2033 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2035 import_export_decl (vtbl);
2036 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2037 needed = 1;
2039 if (!needed)
2041 /* If the references to this class' vtables are optimized away,
2042 still emit the appropriate debugging information. See
2043 dfs_debug_mark. */
2044 if (DECL_COMDAT (primary_vtbl)
2045 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2046 note_debug_info_needed (ctype);
2047 return false;
2050 /* The ABI requires that we emit all of the vtables if we emit any
2051 of them. */
2052 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2054 /* Mark entities references from the virtual table as used. */
2055 mark_vtable_entries (vtbl);
2057 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2059 vec<tree, va_gc> *cleanups = NULL;
2060 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2061 LOOKUP_NORMAL);
2063 /* It had better be all done at compile-time. */
2064 gcc_assert (!expr && !cleanups);
2067 /* Write it out. */
2068 DECL_EXTERNAL (vtbl) = 0;
2069 rest_of_decl_compilation (vtbl, 1, 1);
2071 /* Because we're only doing syntax-checking, we'll never end up
2072 actually marking the variable as written. */
2073 if (flag_syntax_only)
2074 TREE_ASM_WRITTEN (vtbl) = 1;
2075 else if (DECL_ONE_ONLY (vtbl))
2077 current = varpool_node::get_create (vtbl);
2078 if (last)
2079 current->add_to_same_comdat_group (last);
2080 last = current;
2084 /* Since we're writing out the vtable here, also write the debug
2085 info. */
2086 note_debug_info_needed (ctype);
2088 return true;
2091 /* A special return value from type_visibility meaning internal
2092 linkage. */
2094 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2096 /* walk_tree helper function for type_visibility. */
2098 static tree
2099 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2101 int *vis_p = (int *)data;
2102 if (! TYPE_P (*tp))
2104 *walk_subtrees = 0;
2106 else if (OVERLOAD_TYPE_P (*tp)
2107 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2109 *vis_p = VISIBILITY_ANON;
2110 return *tp;
2112 else if (CLASS_TYPE_P (*tp)
2113 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2114 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2115 return NULL;
2118 /* Returns the visibility of TYPE, which is the minimum visibility of its
2119 component types. */
2121 static int
2122 type_visibility (tree type)
2124 int vis = VISIBILITY_DEFAULT;
2125 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2126 return vis;
2129 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2130 specified (or if VISIBILITY is static). If TMPL is true, this
2131 constraint is for a template argument, and takes precedence
2132 over explicitly-specified visibility on the template. */
2134 static void
2135 constrain_visibility (tree decl, int visibility, bool tmpl)
2137 if (visibility == VISIBILITY_ANON)
2139 /* extern "C" declarations aren't affected by the anonymous
2140 namespace. */
2141 if (!DECL_EXTERN_C_P (decl))
2143 TREE_PUBLIC (decl) = 0;
2144 DECL_WEAK (decl) = 0;
2145 DECL_COMMON (decl) = 0;
2146 DECL_COMDAT (decl) = false;
2147 if (VAR_OR_FUNCTION_DECL_P (decl))
2149 struct symtab_node *snode = symtab_node::get (decl);
2151 if (snode)
2152 snode->set_comdat_group (NULL);
2154 DECL_INTERFACE_KNOWN (decl) = 1;
2155 if (DECL_LANG_SPECIFIC (decl))
2156 DECL_NOT_REALLY_EXTERN (decl) = 1;
2159 else if (visibility > DECL_VISIBILITY (decl)
2160 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2162 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2163 /* This visibility was not specified. */
2164 DECL_VISIBILITY_SPECIFIED (decl) = false;
2168 /* Constrain the visibility of DECL based on the visibility of its template
2169 arguments. */
2171 static void
2172 constrain_visibility_for_template (tree decl, tree targs)
2174 /* If this is a template instantiation, check the innermost
2175 template args for visibility constraints. The outer template
2176 args are covered by the class check. */
2177 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2178 int i;
2179 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2181 int vis = 0;
2183 tree arg = TREE_VEC_ELT (args, i-1);
2184 if (TYPE_P (arg))
2185 vis = type_visibility (arg);
2186 else
2188 if (REFERENCE_REF_P (arg))
2189 arg = TREE_OPERAND (arg, 0);
2190 if (TREE_TYPE (arg))
2191 STRIP_NOPS (arg);
2192 if (TREE_CODE (arg) == ADDR_EXPR)
2193 arg = TREE_OPERAND (arg, 0);
2194 if (VAR_OR_FUNCTION_DECL_P (arg))
2196 if (! TREE_PUBLIC (arg))
2197 vis = VISIBILITY_ANON;
2198 else
2199 vis = DECL_VISIBILITY (arg);
2202 if (vis)
2203 constrain_visibility (decl, vis, true);
2207 /* Like c_determine_visibility, but with additional C++-specific
2208 behavior.
2210 Function-scope entities can rely on the function's visibility because
2211 it is set in start_preparsed_function.
2213 Class-scope entities cannot rely on the class's visibility until the end
2214 of the enclosing class definition.
2216 Note that because namespaces have multiple independent definitions,
2217 namespace visibility is handled elsewhere using the #pragma visibility
2218 machinery rather than by decorating the namespace declaration.
2220 The goal is for constraints from the type to give a diagnostic, and
2221 other constraints to be applied silently. */
2223 void
2224 determine_visibility (tree decl)
2226 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2228 /* Only relevant for names with external linkage. */
2229 if (!TREE_PUBLIC (decl))
2230 return;
2232 /* Cloned constructors and destructors get the same visibility as
2233 the underlying function. That should be set up in
2234 maybe_clone_body. */
2235 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2237 bool orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2238 enum symbol_visibility orig_visibility = DECL_VISIBILITY (decl);
2240 /* The decl may be a template instantiation, which could influence
2241 visibilty. */
2242 tree template_decl = NULL_TREE;
2243 if (TREE_CODE (decl) == TYPE_DECL)
2245 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2247 if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
2248 template_decl = decl;
2250 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2251 template_decl = decl;
2253 else if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
2254 template_decl = decl;
2256 /* If DECL is a member of a class, visibility specifiers on the
2257 class can influence the visibility of the DECL. */
2258 tree class_type = NULL_TREE;
2259 if (DECL_CLASS_SCOPE_P (decl))
2260 class_type = DECL_CONTEXT (decl);
2261 else
2263 /* Not a class member. */
2265 /* Virtual tables have DECL_CONTEXT set to their associated class,
2266 so they are automatically handled above. */
2267 gcc_assert (!VAR_P (decl)
2268 || !DECL_VTABLE_OR_VTT_P (decl));
2270 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2272 /* Local statics and classes get the visibility of their
2273 containing function by default, except that
2274 -fvisibility-inlines-hidden doesn't affect them. */
2275 tree fn = DECL_CONTEXT (decl);
2276 if (DECL_VISIBILITY_SPECIFIED (fn))
2278 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2279 DECL_VISIBILITY_SPECIFIED (decl) =
2280 DECL_VISIBILITY_SPECIFIED (fn);
2282 else
2284 if (DECL_CLASS_SCOPE_P (fn))
2285 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2286 else if (determine_hidden_inline (fn))
2288 DECL_VISIBILITY (decl) = default_visibility;
2289 DECL_VISIBILITY_SPECIFIED (decl) =
2290 visibility_options.inpragma;
2292 else
2294 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2295 DECL_VISIBILITY_SPECIFIED (decl) =
2296 DECL_VISIBILITY_SPECIFIED (fn);
2300 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2301 but have no TEMPLATE_INFO. Their containing template
2302 function does, and the local class could be constrained
2303 by that. */
2304 if (template_decl)
2305 template_decl = fn;
2307 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2308 && flag_visibility_ms_compat)
2310 /* Under -fvisibility-ms-compat, types are visible by default,
2311 even though their contents aren't. */
2312 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2313 int underlying_vis = type_visibility (underlying_type);
2314 if (underlying_vis == VISIBILITY_ANON
2315 || (CLASS_TYPE_P (underlying_type)
2316 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2317 constrain_visibility (decl, underlying_vis, false);
2318 else
2319 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2321 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2323 /* tinfo visibility is based on the type it's for. */
2324 constrain_visibility
2325 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2327 /* Give the target a chance to override the visibility associated
2328 with DECL. */
2329 if (TREE_PUBLIC (decl)
2330 && !DECL_REALLY_EXTERN (decl)
2331 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2332 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2333 targetm.cxx.determine_class_data_visibility (decl);
2335 else if (template_decl)
2336 /* Template instantiations and specializations get visibility based
2337 on their template unless they override it with an attribute. */;
2338 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2340 if (determine_hidden_inline (decl))
2341 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2342 else
2344 /* Set default visibility to whatever the user supplied with
2345 #pragma GCC visibility or a namespace visibility attribute. */
2346 DECL_VISIBILITY (decl) = default_visibility;
2347 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2352 if (template_decl)
2354 /* If the specialization doesn't specify visibility, use the
2355 visibility from the template. */
2356 tree tinfo = get_template_info (template_decl);
2357 tree args = TI_ARGS (tinfo);
2358 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2359 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2360 : DECL_ATTRIBUTES (decl));
2362 if (args != error_mark_node)
2364 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2366 if (!DECL_VISIBILITY_SPECIFIED (decl))
2368 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2369 && determine_hidden_inline (decl))
2370 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2371 else
2373 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2374 DECL_VISIBILITY_SPECIFIED (decl)
2375 = DECL_VISIBILITY_SPECIFIED (pattern);
2379 if (args
2380 /* Template argument visibility outweighs #pragma or namespace
2381 visibility, but not an explicit attribute. */
2382 && !lookup_attribute ("visibility", attribs))
2384 int depth = TMPL_ARGS_DEPTH (args);
2385 if (DECL_VISIBILITY_SPECIFIED (decl))
2387 /* A class template member with explicit visibility
2388 overrides the class visibility, so we need to apply
2389 all the levels of template args directly. */
2390 int i;
2391 for (i = 1; i <= depth; ++i)
2393 tree lev = TMPL_ARGS_LEVEL (args, i);
2394 constrain_visibility_for_template (decl, lev);
2397 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2398 /* Limit visibility based on its template arguments. */
2399 constrain_visibility_for_template (decl, args);
2404 if (class_type)
2405 determine_visibility_from_class (decl, class_type);
2407 if (decl_anon_ns_mem_p (decl))
2408 /* Names in an anonymous namespace get internal linkage.
2409 This might change once we implement export. */
2410 constrain_visibility (decl, VISIBILITY_ANON, false);
2411 else if (TREE_CODE (decl) != TYPE_DECL)
2413 /* Propagate anonymity from type to decl. */
2414 int tvis = type_visibility (TREE_TYPE (decl));
2415 if (tvis == VISIBILITY_ANON
2416 || ! DECL_VISIBILITY_SPECIFIED (decl))
2417 constrain_visibility (decl, tvis, false);
2419 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2420 /* DR 757: A type without linkage shall not be used as the type of a
2421 variable or function with linkage, unless
2422 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2423 o the variable or function is not used (3.2 [basic.def.odr]) or is
2424 defined in the same translation unit.
2426 Since non-extern "C" decls need to be defined in the same
2427 translation unit, we can make the type internal. */
2428 constrain_visibility (decl, VISIBILITY_ANON, false);
2430 /* If visibility changed and DECL already has DECL_RTL, ensure
2431 symbol flags are updated. */
2432 if ((DECL_VISIBILITY (decl) != orig_visibility
2433 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2434 && ((VAR_P (decl) && TREE_STATIC (decl))
2435 || TREE_CODE (decl) == FUNCTION_DECL)
2436 && DECL_RTL_SET_P (decl))
2437 make_decl_rtl (decl);
2440 /* By default, static data members and function members receive
2441 the visibility of their containing class. */
2443 static void
2444 determine_visibility_from_class (tree decl, tree class_type)
2446 if (DECL_VISIBILITY_SPECIFIED (decl))
2447 return;
2449 if (determine_hidden_inline (decl))
2450 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2451 else
2453 /* Default to the class visibility. */
2454 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2455 DECL_VISIBILITY_SPECIFIED (decl)
2456 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2459 /* Give the target a chance to override the visibility associated
2460 with DECL. */
2461 if (VAR_P (decl)
2462 && (DECL_TINFO_P (decl)
2463 || (DECL_VTABLE_OR_VTT_P (decl)
2464 /* Construction virtual tables are not exported because
2465 they cannot be referred to from other object files;
2466 their name is not standardized by the ABI. */
2467 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2468 && TREE_PUBLIC (decl)
2469 && !DECL_REALLY_EXTERN (decl)
2470 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2471 targetm.cxx.determine_class_data_visibility (decl);
2474 /* Returns true iff DECL is an inline that should get hidden visibility
2475 because of -fvisibility-inlines-hidden. */
2477 static bool
2478 determine_hidden_inline (tree decl)
2480 return (visibility_options.inlines_hidden
2481 /* Don't do this for inline templates; specializations might not be
2482 inline, and we don't want them to inherit the hidden
2483 visibility. We'll set it here for all inline instantiations. */
2484 && !processing_template_decl
2485 && TREE_CODE (decl) == FUNCTION_DECL
2486 && DECL_DECLARED_INLINE_P (decl)
2487 && (! DECL_LANG_SPECIFIC (decl)
2488 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2491 /* Constrain the visibility of a class TYPE based on the visibility of its
2492 field types. Warn if any fields require lesser visibility. */
2494 void
2495 constrain_class_visibility (tree type)
2497 tree binfo;
2498 tree t;
2499 int i;
2501 int vis = type_visibility (type);
2503 if (vis == VISIBILITY_ANON
2504 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2505 return;
2507 /* Don't warn about visibility if the class has explicit visibility. */
2508 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2509 vis = VISIBILITY_INTERNAL;
2511 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2512 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2514 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2515 int subvis = type_visibility (ftype);
2517 if (subvis == VISIBILITY_ANON)
2519 if (!in_main_input_context())
2521 tree nlt = no_linkage_check (ftype, /*relaxed_p=*/false);
2522 if (nlt)
2524 if (same_type_p (TREE_TYPE (t), nlt))
2525 warning (OPT_Wsubobject_linkage, "\
2526 %qT has a field %qD whose type has no linkage",
2527 type, t);
2528 else
2529 warning (OPT_Wsubobject_linkage, "\
2530 %qT has a field %qD whose type depends on the type %qT which has no linkage",
2531 type, t, nlt);
2533 else
2534 warning (OPT_Wsubobject_linkage, "\
2535 %qT has a field %qD whose type uses the anonymous namespace",
2536 type, t);
2539 else if (MAYBE_CLASS_TYPE_P (ftype)
2540 && vis < VISIBILITY_HIDDEN
2541 && subvis >= VISIBILITY_HIDDEN)
2542 warning (OPT_Wattributes, "\
2543 %qT declared with greater visibility than the type of its field %qD",
2544 type, t);
2547 binfo = TYPE_BINFO (type);
2548 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2550 int subvis = type_visibility (TREE_TYPE (t));
2552 if (subvis == VISIBILITY_ANON)
2554 if (!in_main_input_context())
2556 tree nlt = no_linkage_check (TREE_TYPE (t), /*relaxed_p=*/false);
2557 if (nlt)
2559 if (same_type_p (TREE_TYPE (t), nlt))
2560 warning (OPT_Wsubobject_linkage, "\
2561 %qT has a base %qT whose type has no linkage",
2562 type, TREE_TYPE (t));
2563 else
2564 warning (OPT_Wsubobject_linkage, "\
2565 %qT has a base %qT whose type depends on the type %qT which has no linkage",
2566 type, TREE_TYPE (t), nlt);
2568 else
2569 warning (OPT_Wsubobject_linkage, "\
2570 %qT has a base %qT whose type uses the anonymous namespace",
2571 type, TREE_TYPE (t));
2574 else if (vis < VISIBILITY_HIDDEN
2575 && subvis >= VISIBILITY_HIDDEN)
2576 warning (OPT_Wattributes, "\
2577 %qT declared with greater visibility than its base %qT",
2578 type, TREE_TYPE (t));
2582 /* Functions for adjusting the visibility of a tagged type and its nested
2583 types and declarations when it gets a name for linkage purposes from a
2584 typedef. */
2586 static void bt_reset_linkage_1 (binding_entry, void *);
2587 static void bt_reset_linkage_2 (binding_entry, void *);
2589 /* First reset the visibility of all the types. */
2591 static void
2592 reset_type_linkage_1 (tree type)
2594 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2595 if (CLASS_TYPE_P (type))
2596 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2597 bt_reset_linkage_1, NULL);
2599 static void
2600 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2602 reset_type_linkage_1 (b->type);
2605 /* Then reset the visibility of any static data members or member
2606 functions that use those types. */
2608 static void
2609 reset_decl_linkage (tree decl)
2611 if (TREE_PUBLIC (decl))
2612 return;
2613 if (DECL_CLONED_FUNCTION_P (decl))
2614 return;
2615 TREE_PUBLIC (decl) = true;
2616 DECL_INTERFACE_KNOWN (decl) = false;
2617 determine_visibility (decl);
2618 tentative_decl_linkage (decl);
2620 static void
2621 reset_type_linkage_2 (tree type)
2623 if (CLASS_TYPE_P (type))
2625 if (tree vt = CLASSTYPE_VTABLES (type))
2627 tree name = mangle_vtbl_for_type (type);
2628 DECL_NAME (vt) = name;
2629 SET_DECL_ASSEMBLER_NAME (vt, name);
2630 reset_decl_linkage (vt);
2632 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2634 tree name = mangle_typeinfo_for_type (type);
2635 DECL_NAME (ti) = name;
2636 SET_DECL_ASSEMBLER_NAME (ti, name);
2637 TREE_TYPE (name) = type;
2638 reset_decl_linkage (ti);
2640 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2642 tree mem = STRIP_TEMPLATE (m);
2643 if (VAR_P (mem))
2644 reset_decl_linkage (mem);
2646 for (tree m = TYPE_METHODS (type); m; m = DECL_CHAIN (m))
2648 tree mem = STRIP_TEMPLATE (m);
2649 reset_decl_linkage (mem);
2650 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (mem))
2652 /* Also update its name, for cxx_dwarf_name. */
2653 DECL_NAME (mem) = TYPE_IDENTIFIER (type);
2654 if (m != mem)
2655 DECL_NAME (m) = TYPE_IDENTIFIER (type);
2658 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2659 bt_reset_linkage_2, NULL);
2662 static void
2663 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2665 reset_type_linkage_2 (b->type);
2667 void
2668 reset_type_linkage (tree type)
2670 reset_type_linkage_1 (type);
2671 reset_type_linkage_2 (type);
2674 /* Set up our initial idea of what the linkage of DECL should be. */
2676 void
2677 tentative_decl_linkage (tree decl)
2679 if (DECL_INTERFACE_KNOWN (decl))
2680 /* We've already made a decision as to how this function will
2681 be handled. */;
2682 else if (vague_linkage_p (decl))
2684 if (TREE_CODE (decl) == FUNCTION_DECL
2685 && decl_defined_p (decl))
2687 DECL_EXTERNAL (decl) = 1;
2688 DECL_NOT_REALLY_EXTERN (decl) = 1;
2689 note_vague_linkage_fn (decl);
2690 /* A non-template inline function with external linkage will
2691 always be COMDAT. As we must eventually determine the
2692 linkage of all functions, and as that causes writes to
2693 the data mapped in from the PCH file, it's advantageous
2694 to mark the functions at this point. */
2695 if (DECL_DECLARED_INLINE_P (decl)
2696 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2697 || DECL_DEFAULTED_FN (decl)))
2699 /* This function must have external linkage, as
2700 otherwise DECL_INTERFACE_KNOWN would have been
2701 set. */
2702 gcc_assert (TREE_PUBLIC (decl));
2703 comdat_linkage (decl);
2704 DECL_INTERFACE_KNOWN (decl) = 1;
2707 else if (VAR_P (decl))
2708 maybe_commonize_var (decl);
2712 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2713 for DECL has not already been determined, do so now by setting
2714 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2715 function is called entities with vague linkage whose definitions
2716 are available must have TREE_PUBLIC set.
2718 If this function decides to place DECL in COMDAT, it will set
2719 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2720 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2721 callers defer that decision until it is clear that DECL is actually
2722 required. */
2724 void
2725 import_export_decl (tree decl)
2727 int emit_p;
2728 bool comdat_p;
2729 bool import_p;
2730 tree class_type = NULL_TREE;
2732 if (DECL_INTERFACE_KNOWN (decl))
2733 return;
2735 /* We cannot determine what linkage to give to an entity with vague
2736 linkage until the end of the file. For example, a virtual table
2737 for a class will be defined if and only if the key method is
2738 defined in this translation unit. As a further example, consider
2739 that when compiling a translation unit that uses PCH file with
2740 "-frepo" it would be incorrect to make decisions about what
2741 entities to emit when building the PCH; those decisions must be
2742 delayed until the repository information has been processed. */
2743 gcc_assert (at_eof);
2744 /* Object file linkage for explicit instantiations is handled in
2745 mark_decl_instantiated. For static variables in functions with
2746 vague linkage, maybe_commonize_var is used.
2748 Therefore, the only declarations that should be provided to this
2749 function are those with external linkage that are:
2751 * implicit instantiations of function templates
2753 * inline function
2755 * implicit instantiations of static data members of class
2756 templates
2758 * virtual tables
2760 * typeinfo objects
2762 Furthermore, all entities that reach this point must have a
2763 definition available in this translation unit.
2765 The following assertions check these conditions. */
2766 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2767 /* Any code that creates entities with TREE_PUBLIC cleared should
2768 also set DECL_INTERFACE_KNOWN. */
2769 gcc_assert (TREE_PUBLIC (decl));
2770 if (TREE_CODE (decl) == FUNCTION_DECL)
2771 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2772 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2773 || DECL_DECLARED_INLINE_P (decl));
2774 else
2775 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2776 || DECL_VTABLE_OR_VTT_P (decl)
2777 || DECL_TINFO_P (decl));
2778 /* Check that a definition of DECL is available in this translation
2779 unit. */
2780 gcc_assert (!DECL_REALLY_EXTERN (decl));
2782 /* Assume that DECL will not have COMDAT linkage. */
2783 comdat_p = false;
2784 /* Assume that DECL will not be imported into this translation
2785 unit. */
2786 import_p = false;
2788 /* See if the repository tells us whether or not to emit DECL in
2789 this translation unit. */
2790 emit_p = repo_emit_p (decl);
2791 if (emit_p == 0)
2792 import_p = true;
2793 else if (emit_p == 1)
2795 /* The repository indicates that this entity should be defined
2796 here. Make sure the back end honors that request. */
2797 mark_needed (decl);
2798 /* Output the definition as an ordinary strong definition. */
2799 DECL_EXTERNAL (decl) = 0;
2800 DECL_INTERFACE_KNOWN (decl) = 1;
2801 return;
2804 if (import_p)
2805 /* We have already decided what to do with this DECL; there is no
2806 need to check anything further. */
2808 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2810 class_type = DECL_CONTEXT (decl);
2811 import_export_class (class_type);
2812 if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2813 && CLASSTYPE_INTERFACE_ONLY (class_type))
2814 import_p = true;
2815 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2816 && !CLASSTYPE_USE_TEMPLATE (class_type)
2817 && CLASSTYPE_KEY_METHOD (class_type)
2818 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2819 /* The ABI requires that all virtual tables be emitted with
2820 COMDAT linkage. However, on systems where COMDAT symbols
2821 don't show up in the table of contents for a static
2822 archive, or on systems without weak symbols (where we
2823 approximate COMDAT linkage by using internal linkage), the
2824 linker will report errors about undefined symbols because
2825 it will not see the virtual table definition. Therefore,
2826 in the case that we know that the virtual table will be
2827 emitted in only one translation unit, we make the virtual
2828 table an ordinary definition with external linkage. */
2829 DECL_EXTERNAL (decl) = 0;
2830 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2832 /* CLASS_TYPE is being exported from this translation unit,
2833 so DECL should be defined here. */
2834 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2835 /* If a class is declared in a header with the "extern
2836 template" extension, then it will not be instantiated,
2837 even in translation units that would normally require
2838 it. Often such classes are explicitly instantiated in
2839 one translation unit. Therefore, the explicit
2840 instantiation must be made visible to other translation
2841 units. */
2842 DECL_EXTERNAL (decl) = 0;
2843 else
2845 /* The generic C++ ABI says that class data is always
2846 COMDAT, even if there is a key function. Some
2847 variants (e.g., the ARM EABI) says that class data
2848 only has COMDAT linkage if the class data might be
2849 emitted in more than one translation unit. When the
2850 key method can be inline and is inline, we still have
2851 to arrange for comdat even though
2852 class_data_always_comdat is false. */
2853 if (!CLASSTYPE_KEY_METHOD (class_type)
2854 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2855 || targetm.cxx.class_data_always_comdat ())
2857 /* The ABI requires COMDAT linkage. Normally, we
2858 only emit COMDAT things when they are needed;
2859 make sure that we realize that this entity is
2860 indeed needed. */
2861 comdat_p = true;
2862 mark_needed (decl);
2866 else if (!flag_implicit_templates
2867 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2868 import_p = true;
2869 else
2870 comdat_p = true;
2872 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2874 tree type = TREE_TYPE (DECL_NAME (decl));
2875 if (CLASS_TYPE_P (type))
2877 class_type = type;
2878 import_export_class (type);
2879 if (CLASSTYPE_INTERFACE_KNOWN (type)
2880 && TYPE_POLYMORPHIC_P (type)
2881 && CLASSTYPE_INTERFACE_ONLY (type)
2882 /* If -fno-rtti was specified, then we cannot be sure
2883 that RTTI information will be emitted with the
2884 virtual table of the class, so we must emit it
2885 wherever it is used. */
2886 && flag_rtti)
2887 import_p = true;
2888 else
2890 if (CLASSTYPE_INTERFACE_KNOWN (type)
2891 && !CLASSTYPE_INTERFACE_ONLY (type))
2893 comdat_p = (targetm.cxx.class_data_always_comdat ()
2894 || (CLASSTYPE_KEY_METHOD (type)
2895 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2896 mark_needed (decl);
2897 if (!flag_weak)
2899 comdat_p = false;
2900 DECL_EXTERNAL (decl) = 0;
2903 else
2904 comdat_p = true;
2907 else
2908 comdat_p = true;
2910 else if (DECL_TEMPLOID_INSTANTIATION (decl))
2912 /* DECL is an implicit instantiation of a function or static
2913 data member. */
2914 if ((flag_implicit_templates
2915 && !flag_use_repository)
2916 || (flag_implicit_inline_templates
2917 && TREE_CODE (decl) == FUNCTION_DECL
2918 && DECL_DECLARED_INLINE_P (decl)))
2919 comdat_p = true;
2920 else
2921 /* If we are not implicitly generating templates, then mark
2922 this entity as undefined in this translation unit. */
2923 import_p = true;
2925 else if (DECL_FUNCTION_MEMBER_P (decl))
2927 if (!DECL_DECLARED_INLINE_P (decl))
2929 tree ctype = DECL_CONTEXT (decl);
2930 import_export_class (ctype);
2931 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2933 DECL_NOT_REALLY_EXTERN (decl)
2934 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2935 || (DECL_DECLARED_INLINE_P (decl)
2936 && ! flag_implement_inlines
2937 && !DECL_VINDEX (decl)));
2939 if (!DECL_NOT_REALLY_EXTERN (decl))
2940 DECL_EXTERNAL (decl) = 1;
2942 /* Always make artificials weak. */
2943 if (DECL_ARTIFICIAL (decl) && flag_weak)
2944 comdat_p = true;
2945 else
2946 maybe_make_one_only (decl);
2949 else
2950 comdat_p = true;
2952 else
2953 comdat_p = true;
2955 if (import_p)
2957 /* If we are importing DECL into this translation unit, mark is
2958 an undefined here. */
2959 DECL_EXTERNAL (decl) = 1;
2960 DECL_NOT_REALLY_EXTERN (decl) = 0;
2962 else if (comdat_p)
2964 /* If we decided to put DECL in COMDAT, mark it accordingly at
2965 this point. */
2966 comdat_linkage (decl);
2969 DECL_INTERFACE_KNOWN (decl) = 1;
2972 /* Return an expression that performs the destruction of DECL, which
2973 must be a VAR_DECL whose type has a non-trivial destructor, or is
2974 an array whose (innermost) elements have a non-trivial destructor. */
2976 tree
2977 build_cleanup (tree decl)
2979 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
2980 gcc_assert (clean != NULL_TREE);
2981 return clean;
2984 /* Returns the initialization guard variable for the variable DECL,
2985 which has static storage duration. */
2987 tree
2988 get_guard (tree decl)
2990 tree sname;
2991 tree guard;
2993 sname = mangle_guard_variable (decl);
2994 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2995 if (! guard)
2997 tree guard_type;
2999 /* We use a type that is big enough to contain a mutex as well
3000 as an integer counter. */
3001 guard_type = targetm.cxx.guard_type ();
3002 guard = build_decl (DECL_SOURCE_LOCATION (decl),
3003 VAR_DECL, sname, guard_type);
3005 /* The guard should have the same linkage as what it guards. */
3006 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
3007 TREE_STATIC (guard) = TREE_STATIC (decl);
3008 DECL_COMMON (guard) = DECL_COMMON (decl);
3009 DECL_COMDAT (guard) = DECL_COMDAT (decl);
3010 CP_DECL_THREAD_LOCAL_P (guard) = CP_DECL_THREAD_LOCAL_P (decl);
3011 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
3012 if (DECL_ONE_ONLY (decl))
3013 make_decl_one_only (guard, cxx_comdat_group (guard));
3014 if (TREE_PUBLIC (decl))
3015 DECL_WEAK (guard) = DECL_WEAK (decl);
3016 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
3017 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
3019 DECL_ARTIFICIAL (guard) = 1;
3020 DECL_IGNORED_P (guard) = 1;
3021 TREE_USED (guard) = 1;
3022 pushdecl_top_level_and_finish (guard, NULL_TREE);
3024 return guard;
3027 /* Return an atomic load of src with the appropriate memory model. */
3029 static tree
3030 build_atomic_load_byte (tree src, HOST_WIDE_INT model)
3032 tree ptr_type = build_pointer_type (char_type_node);
3033 tree mem_model = build_int_cst (integer_type_node, model);
3034 tree t, addr, val;
3035 unsigned int size;
3036 int fncode;
3038 size = tree_to_uhwi (TYPE_SIZE_UNIT (char_type_node));
3040 fncode = BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1;
3041 t = builtin_decl_implicit ((enum built_in_function) fncode);
3043 addr = build1 (ADDR_EXPR, ptr_type, src);
3044 val = build_call_expr (t, 2, addr, mem_model);
3045 return val;
3048 /* Return those bits of the GUARD variable that should be set when the
3049 guarded entity is actually initialized. */
3051 static tree
3052 get_guard_bits (tree guard)
3054 if (!targetm.cxx.guard_mask_bit ())
3056 /* We only set the first byte of the guard, in order to leave room
3057 for a mutex in the high-order bits. */
3058 guard = build1 (ADDR_EXPR,
3059 build_pointer_type (TREE_TYPE (guard)),
3060 guard);
3061 guard = build1 (NOP_EXPR,
3062 build_pointer_type (char_type_node),
3063 guard);
3064 guard = build1 (INDIRECT_REF, char_type_node, guard);
3067 return guard;
3070 /* Return an expression which determines whether or not the GUARD
3071 variable has already been initialized. */
3073 tree
3074 get_guard_cond (tree guard, bool thread_safe)
3076 tree guard_value;
3078 if (!thread_safe)
3079 guard = get_guard_bits (guard);
3080 else
3081 guard = build_atomic_load_byte (guard, MEMMODEL_ACQUIRE);
3083 /* Mask off all but the low bit. */
3084 if (targetm.cxx.guard_mask_bit ())
3086 guard_value = integer_one_node;
3087 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3088 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3089 guard = cp_build_binary_op (input_location,
3090 BIT_AND_EXPR, guard, guard_value,
3091 tf_warning_or_error);
3094 guard_value = integer_zero_node;
3095 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3096 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3097 return cp_build_binary_op (input_location,
3098 EQ_EXPR, guard, guard_value,
3099 tf_warning_or_error);
3102 /* Return an expression which sets the GUARD variable, indicating that
3103 the variable being guarded has been initialized. */
3105 tree
3106 set_guard (tree guard)
3108 tree guard_init;
3110 /* Set the GUARD to one. */
3111 guard = get_guard_bits (guard);
3112 guard_init = integer_one_node;
3113 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3114 guard_init = fold_convert (TREE_TYPE (guard), guard_init);
3115 return cp_build_modify_expr (input_location, guard, NOP_EXPR, guard_init,
3116 tf_warning_or_error);
3119 /* Returns true iff we can tell that VAR does not have a dynamic
3120 initializer. */
3122 static bool
3123 var_defined_without_dynamic_init (tree var)
3125 /* If it's defined in another TU, we can't tell. */
3126 if (DECL_EXTERNAL (var))
3127 return false;
3128 /* If it has a non-trivial destructor, registering the destructor
3129 counts as dynamic initialization. */
3130 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3131 return false;
3132 /* If it's in this TU, its initializer has been processed, unless
3133 it's a case of self-initialization, then DECL_INITIALIZED_P is
3134 false while the initializer is handled by finish_id_expression. */
3135 if (!DECL_INITIALIZED_P (var))
3136 return false;
3137 /* If it has no initializer or a constant one, it's not dynamic. */
3138 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3139 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3142 /* Returns true iff VAR is a variable that needs uses to be
3143 wrapped for possible dynamic initialization. */
3145 static bool
3146 var_needs_tls_wrapper (tree var)
3148 return (!error_operand_p (var)
3149 && CP_DECL_THREAD_LOCAL_P (var)
3150 && !DECL_GNU_TLS_P (var)
3151 && !DECL_FUNCTION_SCOPE_P (var)
3152 && !var_defined_without_dynamic_init (var));
3155 /* Get the FUNCTION_DECL for the shared TLS init function for this
3156 translation unit. */
3158 static tree
3159 get_local_tls_init_fn (void)
3161 tree sname = get_identifier ("__tls_init");
3162 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3163 if (!fn)
3165 fn = build_lang_decl (FUNCTION_DECL, sname,
3166 build_function_type (void_type_node,
3167 void_list_node));
3168 SET_DECL_LANGUAGE (fn, lang_c);
3169 TREE_PUBLIC (fn) = false;
3170 DECL_ARTIFICIAL (fn) = true;
3171 mark_used (fn);
3172 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3174 return fn;
3177 /* Get a FUNCTION_DECL for the init function for the thread_local
3178 variable VAR. The init function will be an alias to the function
3179 that initializes all the non-local TLS variables in the translation
3180 unit. The init function is only used by the wrapper function. */
3182 static tree
3183 get_tls_init_fn (tree var)
3185 /* Only C++11 TLS vars need this init fn. */
3186 if (!var_needs_tls_wrapper (var))
3187 return NULL_TREE;
3189 /* If -fno-extern-tls-init, assume that we don't need to call
3190 a tls init function for a variable defined in another TU. */
3191 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3192 return NULL_TREE;
3194 #ifdef ASM_OUTPUT_DEF
3195 /* If the variable is internal, or if we can't generate aliases,
3196 call the local init function directly. */
3197 if (!TREE_PUBLIC (var))
3198 #endif
3199 return get_local_tls_init_fn ();
3201 tree sname = mangle_tls_init_fn (var);
3202 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3203 if (!fn)
3205 fn = build_lang_decl (FUNCTION_DECL, sname,
3206 build_function_type (void_type_node,
3207 void_list_node));
3208 SET_DECL_LANGUAGE (fn, lang_c);
3209 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3210 DECL_ARTIFICIAL (fn) = true;
3211 DECL_COMDAT (fn) = DECL_COMDAT (var);
3212 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3213 if (DECL_ONE_ONLY (var))
3214 make_decl_one_only (fn, cxx_comdat_group (fn));
3215 if (TREE_PUBLIC (var))
3217 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3218 /* If the variable is defined somewhere else and might have static
3219 initialization, make the init function a weak reference. */
3220 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3221 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3222 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3223 && DECL_EXTERNAL (var))
3224 declare_weak (fn);
3225 else
3226 DECL_WEAK (fn) = DECL_WEAK (var);
3228 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3229 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3230 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3231 DECL_IGNORED_P (fn) = 1;
3232 mark_used (fn);
3234 DECL_BEFRIENDING_CLASSES (fn) = var;
3236 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3238 return fn;
3241 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3242 variable VAR. The wrapper function calls the init function (if any) for
3243 VAR and then returns a reference to VAR. The wrapper function is used
3244 in place of VAR everywhere VAR is mentioned. */
3246 tree
3247 get_tls_wrapper_fn (tree var)
3249 /* Only C++11 TLS vars need this wrapper fn. */
3250 if (!var_needs_tls_wrapper (var))
3251 return NULL_TREE;
3253 tree sname = mangle_tls_wrapper_fn (var);
3254 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3255 if (!fn)
3257 /* A named rvalue reference is an lvalue, so the wrapper should
3258 always return an lvalue reference. */
3259 tree type = non_reference (TREE_TYPE (var));
3260 type = build_reference_type (type);
3261 tree fntype = build_function_type (type, void_list_node);
3262 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3263 SET_DECL_LANGUAGE (fn, lang_c);
3264 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3265 DECL_ARTIFICIAL (fn) = true;
3266 DECL_IGNORED_P (fn) = 1;
3267 /* The wrapper is inline and emitted everywhere var is used. */
3268 DECL_DECLARED_INLINE_P (fn) = true;
3269 if (TREE_PUBLIC (var))
3271 comdat_linkage (fn);
3272 #ifdef HAVE_GAS_HIDDEN
3273 /* Make the wrapper bind locally; there's no reason to share
3274 the wrapper between multiple shared objects. */
3275 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3276 DECL_VISIBILITY_SPECIFIED (fn) = true;
3277 #endif
3279 if (!TREE_PUBLIC (fn))
3280 DECL_INTERFACE_KNOWN (fn) = true;
3281 mark_used (fn);
3282 note_vague_linkage_fn (fn);
3284 #if 0
3285 /* We want CSE to commonize calls to the wrapper, but marking it as
3286 pure is unsafe since it has side-effects. I guess we need a new
3287 ECF flag even weaker than ECF_PURE. FIXME! */
3288 DECL_PURE_P (fn) = true;
3289 #endif
3291 DECL_BEFRIENDING_CLASSES (fn) = var;
3293 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3295 return fn;
3298 /* At EOF, generate the definition for the TLS wrapper function FN:
3300 T& var_wrapper() {
3301 if (init_fn) init_fn();
3302 return var;
3303 } */
3305 static void
3306 generate_tls_wrapper (tree fn)
3308 tree var = DECL_BEFRIENDING_CLASSES (fn);
3310 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3311 tree body = begin_function_body ();
3312 /* Only call the init fn if there might be one. */
3313 if (tree init_fn = get_tls_init_fn (var))
3315 tree if_stmt = NULL_TREE;
3316 /* If init_fn is a weakref, make sure it exists before calling. */
3317 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3319 if_stmt = begin_if_stmt ();
3320 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3321 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3322 NE_EXPR, addr, nullptr_node,
3323 tf_warning_or_error);
3324 finish_if_stmt_cond (cond, if_stmt);
3326 finish_expr_stmt (build_cxx_call
3327 (init_fn, 0, NULL, tf_warning_or_error));
3328 if (if_stmt)
3330 finish_then_clause (if_stmt);
3331 finish_if_stmt (if_stmt);
3334 else
3335 /* If there's no initialization, the wrapper is a constant function. */
3336 TREE_READONLY (fn) = true;
3337 finish_return_stmt (convert_from_reference (var));
3338 finish_function_body (body);
3339 expand_or_defer_fn (finish_function (0));
3342 /* Start the process of running a particular set of global constructors
3343 or destructors. Subroutine of do_[cd]tors. Also called from
3344 vtv_start_verification_constructor_init_function. */
3346 static tree
3347 start_objects (int method_type, int initp)
3349 tree body;
3350 tree fndecl;
3351 char type[14];
3353 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3355 if (initp != DEFAULT_INIT_PRIORITY)
3357 char joiner;
3359 #ifdef JOINER
3360 joiner = JOINER;
3361 #else
3362 joiner = '_';
3363 #endif
3365 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3367 else
3368 sprintf (type, "sub_%c", method_type);
3370 fndecl = build_lang_decl (FUNCTION_DECL,
3371 get_file_function_name (type),
3372 build_function_type_list (void_type_node,
3373 NULL_TREE));
3374 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3376 TREE_PUBLIC (current_function_decl) = 0;
3378 /* Mark as artificial because it's not explicitly in the user's
3379 source code. */
3380 DECL_ARTIFICIAL (current_function_decl) = 1;
3382 /* Mark this declaration as used to avoid spurious warnings. */
3383 TREE_USED (current_function_decl) = 1;
3385 /* Mark this function as a global constructor or destructor. */
3386 if (method_type == 'I')
3387 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3388 else
3389 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3391 body = begin_compound_stmt (BCS_FN_BODY);
3393 return body;
3396 /* Finish the process of running a particular set of global constructors
3397 or destructors. Subroutine of do_[cd]tors. */
3399 static void
3400 finish_objects (int method_type, int initp, tree body)
3402 tree fn;
3404 /* Finish up. */
3405 finish_compound_stmt (body);
3406 fn = finish_function (0);
3408 if (method_type == 'I')
3410 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3411 decl_init_priority_insert (fn, initp);
3413 else
3415 DECL_STATIC_DESTRUCTOR (fn) = 1;
3416 decl_fini_priority_insert (fn, initp);
3419 expand_or_defer_fn (fn);
3422 /* The names of the parameters to the function created to handle
3423 initializations and destructions for objects with static storage
3424 duration. */
3425 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3426 #define PRIORITY_IDENTIFIER "__priority"
3428 /* The name of the function we create to handle initializations and
3429 destructions for objects with static storage duration. */
3430 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3432 /* The declaration for the __INITIALIZE_P argument. */
3433 static GTY(()) tree initialize_p_decl;
3435 /* The declaration for the __PRIORITY argument. */
3436 static GTY(()) tree priority_decl;
3438 /* The declaration for the static storage duration function. */
3439 static GTY(()) tree ssdf_decl;
3441 /* All the static storage duration functions created in this
3442 translation unit. */
3443 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3445 /* A map from priority levels to information about that priority
3446 level. There may be many such levels, so efficient lookup is
3447 important. */
3448 static splay_tree priority_info_map;
3450 /* Begins the generation of the function that will handle all
3451 initialization and destruction of objects with static storage
3452 duration. The function generated takes two parameters of type
3453 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3454 nonzero, it performs initializations. Otherwise, it performs
3455 destructions. It only performs those initializations or
3456 destructions with the indicated __PRIORITY. The generated function
3457 returns no value.
3459 It is assumed that this function will only be called once per
3460 translation unit. */
3462 static tree
3463 start_static_storage_duration_function (unsigned count)
3465 tree type;
3466 tree body;
3467 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3469 /* Create the identifier for this function. It will be of the form
3470 SSDF_IDENTIFIER_<number>. */
3471 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3473 type = build_function_type_list (void_type_node,
3474 integer_type_node, integer_type_node,
3475 NULL_TREE);
3477 /* Create the FUNCTION_DECL itself. */
3478 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3479 get_identifier (id),
3480 type);
3481 TREE_PUBLIC (ssdf_decl) = 0;
3482 DECL_ARTIFICIAL (ssdf_decl) = 1;
3484 /* Put this function in the list of functions to be called from the
3485 static constructors and destructors. */
3486 if (!ssdf_decls)
3488 vec_alloc (ssdf_decls, 32);
3490 /* Take this opportunity to initialize the map from priority
3491 numbers to information about that priority level. */
3492 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3493 /*delete_key_fn=*/0,
3494 /*delete_value_fn=*/
3495 (splay_tree_delete_value_fn) &free);
3497 /* We always need to generate functions for the
3498 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3499 priorities later, we'll be sure to find the
3500 DEFAULT_INIT_PRIORITY. */
3501 get_priority_info (DEFAULT_INIT_PRIORITY);
3504 vec_safe_push (ssdf_decls, ssdf_decl);
3506 /* Create the argument list. */
3507 initialize_p_decl = cp_build_parm_decl
3508 (ssdf_decl, get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3509 TREE_USED (initialize_p_decl) = 1;
3510 priority_decl = cp_build_parm_decl
3511 (ssdf_decl, get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3512 TREE_USED (priority_decl) = 1;
3514 DECL_CHAIN (initialize_p_decl) = priority_decl;
3515 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3517 /* Put the function in the global scope. */
3518 pushdecl (ssdf_decl);
3520 /* Start the function itself. This is equivalent to declaring the
3521 function as:
3523 static void __ssdf (int __initialize_p, init __priority_p);
3525 It is static because we only need to call this function from the
3526 various constructor and destructor functions for this module. */
3527 start_preparsed_function (ssdf_decl,
3528 /*attrs=*/NULL_TREE,
3529 SF_PRE_PARSED);
3531 /* Set up the scope of the outermost block in the function. */
3532 body = begin_compound_stmt (BCS_FN_BODY);
3534 return body;
3537 /* Finish the generation of the function which performs initialization
3538 and destruction of objects with static storage duration. After
3539 this point, no more such objects can be created. */
3541 static void
3542 finish_static_storage_duration_function (tree body)
3544 /* Close out the function. */
3545 finish_compound_stmt (body);
3546 expand_or_defer_fn (finish_function (0));
3549 /* Return the information about the indicated PRIORITY level. If no
3550 code to handle this level has yet been generated, generate the
3551 appropriate prologue. */
3553 static priority_info
3554 get_priority_info (int priority)
3556 priority_info pi;
3557 splay_tree_node n;
3559 n = splay_tree_lookup (priority_info_map,
3560 (splay_tree_key) priority);
3561 if (!n)
3563 /* Create a new priority information structure, and insert it
3564 into the map. */
3565 pi = XNEW (struct priority_info_s);
3566 pi->initializations_p = 0;
3567 pi->destructions_p = 0;
3568 splay_tree_insert (priority_info_map,
3569 (splay_tree_key) priority,
3570 (splay_tree_value) pi);
3572 else
3573 pi = (priority_info) n->value;
3575 return pi;
3578 /* The effective initialization priority of a DECL. */
3580 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3581 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3582 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3584 /* Whether a DECL needs a guard to protect it against multiple
3585 initialization. */
3587 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3588 || DECL_ONE_ONLY (decl) \
3589 || DECL_WEAK (decl)))
3591 /* Called from one_static_initialization_or_destruction(),
3592 via walk_tree.
3593 Walks the initializer list of a global variable and looks for
3594 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3595 and that have their DECL_CONTEXT() == NULL.
3596 For each such temporary variable, set their DECL_CONTEXT() to
3597 the current function. This is necessary because otherwise
3598 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3599 when trying to refer to a temporary variable that does not have
3600 it's DECL_CONTECT() properly set. */
3601 static tree
3602 fix_temporary_vars_context_r (tree *node,
3603 int * /*unused*/,
3604 void * /*unused1*/)
3606 gcc_assert (current_function_decl);
3608 if (TREE_CODE (*node) == BIND_EXPR)
3610 tree var;
3612 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3613 if (VAR_P (var)
3614 && !DECL_NAME (var)
3615 && DECL_ARTIFICIAL (var)
3616 && !DECL_CONTEXT (var))
3617 DECL_CONTEXT (var) = current_function_decl;
3620 return NULL_TREE;
3623 /* Set up to handle the initialization or destruction of DECL. If
3624 INITP is nonzero, we are initializing the variable. Otherwise, we
3625 are destroying it. */
3627 static void
3628 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3630 tree guard_if_stmt = NULL_TREE;
3631 tree guard;
3633 /* If we are supposed to destruct and there's a trivial destructor,
3634 nothing has to be done. */
3635 if (!initp
3636 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3637 return;
3639 /* Trick the compiler into thinking we are at the file and line
3640 where DECL was declared so that error-messages make sense, and so
3641 that the debugger will show somewhat sensible file and line
3642 information. */
3643 input_location = DECL_SOURCE_LOCATION (decl);
3645 /* Make sure temporary variables in the initialiser all have
3646 their DECL_CONTEXT() set to a value different from NULL_TREE.
3647 This can happen when global variables initializers are built.
3648 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3649 the temporary variables that might have been generated in the
3650 accompanying initializers is NULL_TREE, meaning the variables have been
3651 declared in the global namespace.
3652 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3653 of the temporaries are set to the current function decl. */
3654 cp_walk_tree_without_duplicates (&init,
3655 fix_temporary_vars_context_r,
3656 NULL);
3658 /* Because of:
3660 [class.access.spec]
3662 Access control for implicit calls to the constructors,
3663 the conversion functions, or the destructor called to
3664 create and destroy a static data member is performed as
3665 if these calls appeared in the scope of the member's
3666 class.
3668 we pretend we are in a static member function of the class of
3669 which the DECL is a member. */
3670 if (member_p (decl))
3672 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3673 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3676 /* Assume we don't need a guard. */
3677 guard = NULL_TREE;
3678 /* We need a guard if this is an object with external linkage that
3679 might be initialized in more than one place. (For example, a
3680 static data member of a template, when the data member requires
3681 construction.) */
3682 if (NEEDS_GUARD_P (decl))
3684 tree guard_cond;
3686 guard = get_guard (decl);
3688 /* When using __cxa_atexit, we just check the GUARD as we would
3689 for a local static. */
3690 if (flag_use_cxa_atexit)
3692 /* When using __cxa_atexit, we never try to destroy
3693 anything from a static destructor. */
3694 gcc_assert (initp);
3695 guard_cond = get_guard_cond (guard, false);
3697 /* If we don't have __cxa_atexit, then we will be running
3698 destructors from .fini sections, or their equivalents. So,
3699 we need to know how many times we've tried to initialize this
3700 object. We do initializations only if the GUARD is zero,
3701 i.e., if we are the first to initialize the variable. We do
3702 destructions only if the GUARD is one, i.e., if we are the
3703 last to destroy the variable. */
3704 else if (initp)
3705 guard_cond
3706 = cp_build_binary_op (input_location,
3707 EQ_EXPR,
3708 cp_build_unary_op (PREINCREMENT_EXPR,
3709 guard,
3710 /*noconvert=*/true,
3711 tf_warning_or_error),
3712 integer_one_node,
3713 tf_warning_or_error);
3714 else
3715 guard_cond
3716 = cp_build_binary_op (input_location,
3717 EQ_EXPR,
3718 cp_build_unary_op (PREDECREMENT_EXPR,
3719 guard,
3720 /*noconvert=*/true,
3721 tf_warning_or_error),
3722 integer_zero_node,
3723 tf_warning_or_error);
3725 guard_if_stmt = begin_if_stmt ();
3726 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3730 /* If we're using __cxa_atexit, we have not already set the GUARD,
3731 so we must do so now. */
3732 if (guard && initp && flag_use_cxa_atexit)
3733 finish_expr_stmt (set_guard (guard));
3735 /* Perform the initialization or destruction. */
3736 if (initp)
3738 if (init)
3740 finish_expr_stmt (init);
3741 if (sanitize_flags_p (SANITIZE_ADDRESS, decl))
3743 varpool_node *vnode = varpool_node::get (decl);
3744 if (vnode)
3745 vnode->dynamically_initialized = 1;
3749 /* If we're using __cxa_atexit, register a function that calls the
3750 destructor for the object. */
3751 if (flag_use_cxa_atexit)
3752 finish_expr_stmt (register_dtor_fn (decl));
3754 else
3755 finish_expr_stmt (build_cleanup (decl));
3757 /* Finish the guard if-stmt, if necessary. */
3758 if (guard)
3760 finish_then_clause (guard_if_stmt);
3761 finish_if_stmt (guard_if_stmt);
3764 /* Now that we're done with DECL we don't need to pretend to be a
3765 member of its class any longer. */
3766 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3767 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3770 /* Generate code to do the initialization or destruction of the decls in VARS,
3771 a TREE_LIST of VAR_DECL with static storage duration.
3772 Whether initialization or destruction is performed is specified by INITP. */
3774 static void
3775 do_static_initialization_or_destruction (tree vars, bool initp)
3777 tree node, init_if_stmt, cond;
3779 /* Build the outer if-stmt to check for initialization or destruction. */
3780 init_if_stmt = begin_if_stmt ();
3781 cond = initp ? integer_one_node : integer_zero_node;
3782 cond = cp_build_binary_op (input_location,
3783 EQ_EXPR,
3784 initialize_p_decl,
3785 cond,
3786 tf_warning_or_error);
3787 finish_if_stmt_cond (cond, init_if_stmt);
3789 /* To make sure dynamic construction doesn't access globals from other
3790 compilation units where they might not be yet constructed, for
3791 -fsanitize=address insert __asan_before_dynamic_init call that
3792 prevents access to either all global variables that need construction
3793 in other compilation units, or at least those that haven't been
3794 initialized yet. Variables that need dynamic construction in
3795 the current compilation unit are kept accessible. */
3796 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
3797 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3799 node = vars;
3800 do {
3801 tree decl = TREE_VALUE (node);
3802 tree priority_if_stmt;
3803 int priority;
3804 priority_info pi;
3806 /* If we don't need a destructor, there's nothing to do. Avoid
3807 creating a possibly empty if-stmt. */
3808 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3810 node = TREE_CHAIN (node);
3811 continue;
3814 /* Remember that we had an initialization or finalization at this
3815 priority. */
3816 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3817 pi = get_priority_info (priority);
3818 if (initp)
3819 pi->initializations_p = 1;
3820 else
3821 pi->destructions_p = 1;
3823 /* Conditionalize this initialization on being in the right priority
3824 and being initializing/finalizing appropriately. */
3825 priority_if_stmt = begin_if_stmt ();
3826 cond = cp_build_binary_op (input_location,
3827 EQ_EXPR,
3828 priority_decl,
3829 build_int_cst (NULL_TREE, priority),
3830 tf_warning_or_error);
3831 finish_if_stmt_cond (cond, priority_if_stmt);
3833 /* Process initializers with same priority. */
3834 for (; node
3835 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3836 node = TREE_CHAIN (node))
3837 /* Do one initialization or destruction. */
3838 one_static_initialization_or_destruction (TREE_VALUE (node),
3839 TREE_PURPOSE (node), initp);
3841 /* Finish up the priority if-stmt body. */
3842 finish_then_clause (priority_if_stmt);
3843 finish_if_stmt (priority_if_stmt);
3845 } while (node);
3847 /* Revert what __asan_before_dynamic_init did by calling
3848 __asan_after_dynamic_init. */
3849 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
3850 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3852 /* Finish up the init/destruct if-stmt body. */
3853 finish_then_clause (init_if_stmt);
3854 finish_if_stmt (init_if_stmt);
3857 /* VARS is a list of variables with static storage duration which may
3858 need initialization and/or finalization. Remove those variables
3859 that don't really need to be initialized or finalized, and return
3860 the resulting list. The order in which the variables appear in
3861 VARS is in reverse order of the order in which they should actually
3862 be initialized. The list we return is in the unreversed order;
3863 i.e., the first variable should be initialized first. */
3865 static tree
3866 prune_vars_needing_no_initialization (tree *vars)
3868 tree *var = vars;
3869 tree result = NULL_TREE;
3871 while (*var)
3873 tree t = *var;
3874 tree decl = TREE_VALUE (t);
3875 tree init = TREE_PURPOSE (t);
3877 /* Deal gracefully with error. */
3878 if (error_operand_p (decl))
3880 var = &TREE_CHAIN (t);
3881 continue;
3884 /* The only things that can be initialized are variables. */
3885 gcc_assert (VAR_P (decl));
3887 /* If this object is not defined, we don't need to do anything
3888 here. */
3889 if (DECL_EXTERNAL (decl))
3891 var = &TREE_CHAIN (t);
3892 continue;
3895 /* Also, if the initializer already contains errors, we can bail
3896 out now. */
3897 if (init && TREE_CODE (init) == TREE_LIST
3898 && value_member (error_mark_node, init))
3900 var = &TREE_CHAIN (t);
3901 continue;
3904 /* This variable is going to need initialization and/or
3905 finalization, so we add it to the list. */
3906 *var = TREE_CHAIN (t);
3907 TREE_CHAIN (t) = result;
3908 result = t;
3911 return result;
3914 /* Make sure we have told the back end about all the variables in
3915 VARS. */
3917 static void
3918 write_out_vars (tree vars)
3920 tree v;
3922 for (v = vars; v; v = TREE_CHAIN (v))
3924 tree var = TREE_VALUE (v);
3925 if (!var_finalized_p (var))
3927 import_export_decl (var);
3928 rest_of_decl_compilation (var, 1, 1);
3933 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3934 (otherwise) that will initialize all global objects with static
3935 storage duration having the indicated PRIORITY. */
3937 static void
3938 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3939 location_t *locus)
3941 char function_key;
3942 tree fndecl;
3943 tree body;
3944 size_t i;
3946 input_location = *locus;
3947 /* ??? */
3948 /* Was: locus->line++; */
3950 /* We use `I' to indicate initialization and `D' to indicate
3951 destruction. */
3952 function_key = constructor_p ? 'I' : 'D';
3954 /* We emit the function lazily, to avoid generating empty
3955 global constructors and destructors. */
3956 body = NULL_TREE;
3958 /* For Objective-C++, we may need to initialize metadata found in this module.
3959 This must be done _before_ any other static initializations. */
3960 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3961 && constructor_p && objc_static_init_needed_p ())
3963 body = start_objects (function_key, priority);
3964 objc_generate_static_init_call (NULL_TREE);
3967 /* Call the static storage duration function with appropriate
3968 arguments. */
3969 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
3971 /* Calls to pure or const functions will expand to nothing. */
3972 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3974 tree call;
3976 if (! body)
3977 body = start_objects (function_key, priority);
3979 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
3980 build_int_cst (NULL_TREE,
3981 constructor_p),
3982 build_int_cst (NULL_TREE,
3983 priority),
3984 NULL_TREE);
3985 finish_expr_stmt (call);
3989 /* Close out the function. */
3990 if (body)
3991 finish_objects (function_key, priority, body);
3994 /* Generate constructor and destructor functions for the priority
3995 indicated by N. */
3997 static int
3998 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
4000 location_t *locus = (location_t *) data;
4001 int priority = (int) n->key;
4002 priority_info pi = (priority_info) n->value;
4004 /* Generate the functions themselves, but only if they are really
4005 needed. */
4006 if (pi->initializations_p)
4007 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
4008 if (pi->destructions_p)
4009 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
4011 /* Keep iterating. */
4012 return 0;
4015 /* Return C++ property of T, based on given operation OP. */
4017 static int
4018 cpp_check (tree t, cpp_operation op)
4020 switch (op)
4022 case HAS_DEPENDENT_TEMPLATE_ARGS:
4024 tree ti = CLASSTYPE_TEMPLATE_INFO (t);
4025 if (!ti)
4026 return 0;
4027 ++processing_template_decl;
4028 const bool dep = any_dependent_template_arguments_p (TI_ARGS (ti));
4029 --processing_template_decl;
4030 return dep;
4032 case IS_ABSTRACT:
4033 return DECL_PURE_VIRTUAL_P (t);
4034 case IS_CONSTRUCTOR:
4035 return DECL_CONSTRUCTOR_P (t);
4036 case IS_DESTRUCTOR:
4037 return DECL_DESTRUCTOR_P (t);
4038 case IS_COPY_CONSTRUCTOR:
4039 return DECL_COPY_CONSTRUCTOR_P (t);
4040 case IS_MOVE_CONSTRUCTOR:
4041 return DECL_MOVE_CONSTRUCTOR_P (t);
4042 case IS_TEMPLATE:
4043 return TREE_CODE (t) == TEMPLATE_DECL;
4044 case IS_TRIVIAL:
4045 return trivial_type_p (t);
4046 default:
4047 return 0;
4051 /* Collect source file references recursively, starting from NAMESPC. */
4053 static void
4054 collect_source_refs (tree namespc)
4056 /* Iterate over names in this name space. */
4057 for (tree t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4058 if (DECL_IS_BUILTIN (t))
4060 else if (TREE_CODE (t) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (t))
4061 collect_source_refs (t);
4062 else
4063 collect_source_ref (DECL_SOURCE_FILE (t));
4066 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4067 starting from NAMESPC. */
4069 static void
4070 collect_ada_namespace (tree namespc, const char *source_file)
4072 tree decl = NAMESPACE_LEVEL (namespc)->names;
4074 /* Collect decls from this namespace. This will skip
4075 NAMESPACE_DECLs (both aliases and regular, it cannot tell). */
4076 collect_ada_nodes (decl, source_file);
4078 /* Now scan for namespace children, and dump them. */
4079 for (; decl; decl = TREE_CHAIN (decl))
4080 if (TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl))
4081 collect_ada_namespace (decl, source_file);
4084 /* Returns true iff there is a definition available for variable or
4085 function DECL. */
4087 bool
4088 decl_defined_p (tree decl)
4090 if (TREE_CODE (decl) == FUNCTION_DECL)
4091 return (DECL_INITIAL (decl) != NULL_TREE
4092 /* A pending instantiation of a friend temploid is defined. */
4093 || (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
4094 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4095 (DECL_TI_TEMPLATE (decl)))));
4096 else
4098 gcc_assert (VAR_P (decl));
4099 return !DECL_EXTERNAL (decl);
4103 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4105 [expr.const]
4107 An integral constant-expression can only involve ... const
4108 variables of integral or enumeration types initialized with
4109 constant expressions ...
4111 C++0x also allows constexpr variables and temporaries initialized
4112 with constant expressions. We handle the former here, but the latter
4113 are just folded away in cxx_eval_constant_expression.
4115 The standard does not require that the expression be non-volatile.
4116 G++ implements the proposed correction in DR 457. */
4118 bool
4119 decl_constant_var_p (tree decl)
4121 if (!decl_maybe_constant_var_p (decl))
4122 return false;
4124 /* We don't know if a template static data member is initialized with
4125 a constant expression until we instantiate its initializer. Even
4126 in the case of a constexpr variable, we can't treat it as a
4127 constant until its initializer is complete in case it's used in
4128 its own initializer. */
4129 maybe_instantiate_decl (decl);
4130 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4133 /* Returns true if DECL could be a symbolic constant variable, depending on
4134 its initializer. */
4136 bool
4137 decl_maybe_constant_var_p (tree decl)
4139 tree type = TREE_TYPE (decl);
4140 if (!VAR_P (decl))
4141 return false;
4142 if (DECL_DECLARED_CONSTEXPR_P (decl))
4143 return true;
4144 if (DECL_HAS_VALUE_EXPR_P (decl))
4145 /* A proxy isn't constant. */
4146 return false;
4147 if (TREE_CODE (type) == REFERENCE_TYPE)
4148 /* References can be constant. */;
4149 else if (CP_TYPE_CONST_NON_VOLATILE_P (type)
4150 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4151 /* And const integers. */;
4152 else
4153 return false;
4155 if (DECL_INITIAL (decl)
4156 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
4157 /* We know the initializer, and it isn't constant. */
4158 return false;
4159 else
4160 return true;
4163 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4164 called from grokfndecl and grokvardecl; in all modes it is called from
4165 cp_write_global_declarations. */
4167 void
4168 no_linkage_error (tree decl)
4170 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4171 /* In C++11 it's ok if the decl is defined. */
4172 return;
4173 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4174 if (t == NULL_TREE)
4175 /* The type that got us on no_linkage_decls must have gotten a name for
4176 linkage purposes. */;
4177 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4178 /* The type might end up having a typedef name for linkage purposes. */
4179 vec_safe_push (no_linkage_decls, decl);
4180 else if (TYPE_UNNAMED_P (t))
4182 bool d = false;
4183 if (cxx_dialect >= cxx11)
4184 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4185 "unnamed type, is used but never defined", decl);
4186 else if (DECL_EXTERN_C_P (decl))
4187 /* Allow this; it's pretty common in C. */;
4188 else if (VAR_P (decl))
4189 /* DRs 132, 319 and 389 seem to indicate types with
4190 no linkage can only be used to declare extern "C"
4191 entities. Since it's not always an error in the
4192 ISO C++ 90 Standard, we only issue a warning. */
4193 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "unnamed type "
4194 "with no linkage used to declare variable %q#D with "
4195 "linkage", decl);
4196 else
4197 d = permerror (DECL_SOURCE_LOCATION (decl), "unnamed type with no "
4198 "linkage used to declare function %q#D with linkage",
4199 decl);
4200 if (d && is_typedef_decl (TYPE_NAME (t)))
4201 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4202 "to the unqualified type, so it is not used for linkage",
4203 TYPE_NAME (t));
4205 else if (cxx_dialect >= cxx11)
4207 if (VAR_P (decl) || !DECL_PURE_VIRTUAL_P (decl))
4208 permerror (DECL_SOURCE_LOCATION (decl),
4209 "%q#D, declared using local type "
4210 "%qT, is used but never defined", decl, t);
4212 else if (VAR_P (decl))
4213 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4214 "used to declare variable %q#D with linkage", t, decl);
4215 else
4216 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4217 "to declare function %q#D with linkage", t, decl);
4220 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4222 static void
4223 collect_all_refs (const char *source_file)
4225 collect_ada_namespace (global_namespace, source_file);
4228 /* Clear DECL_EXTERNAL for NODE. */
4230 static bool
4231 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4233 DECL_EXTERNAL (node->decl) = 0;
4234 return false;
4237 /* Build up the function to run dynamic initializers for thread_local
4238 variables in this translation unit and alias the init functions for the
4239 individual variables to it. */
4241 static void
4242 handle_tls_init (void)
4244 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4245 if (vars == NULL_TREE)
4246 return;
4248 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4250 write_out_vars (vars);
4252 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4253 boolean_type_node);
4254 TREE_PUBLIC (guard) = false;
4255 TREE_STATIC (guard) = true;
4256 DECL_ARTIFICIAL (guard) = true;
4257 DECL_IGNORED_P (guard) = true;
4258 TREE_USED (guard) = true;
4259 CP_DECL_THREAD_LOCAL_P (guard) = true;
4260 set_decl_tls_model (guard, decl_default_tls_model (guard));
4261 pushdecl_top_level_and_finish (guard, NULL_TREE);
4263 tree fn = get_local_tls_init_fn ();
4264 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4265 tree body = begin_function_body ();
4266 tree if_stmt = begin_if_stmt ();
4267 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4268 tf_warning_or_error);
4269 finish_if_stmt_cond (cond, if_stmt);
4270 finish_expr_stmt (cp_build_modify_expr (loc, guard, NOP_EXPR,
4271 boolean_true_node,
4272 tf_warning_or_error));
4273 for (; vars; vars = TREE_CHAIN (vars))
4275 tree var = TREE_VALUE (vars);
4276 tree init = TREE_PURPOSE (vars);
4277 one_static_initialization_or_destruction (var, init, true);
4279 #ifdef ASM_OUTPUT_DEF
4280 /* Output init aliases even with -fno-extern-tls-init. */
4281 if (TREE_PUBLIC (var))
4283 tree single_init_fn = get_tls_init_fn (var);
4284 if (single_init_fn == NULL_TREE)
4285 continue;
4286 cgraph_node *alias
4287 = cgraph_node::get_create (fn)->create_same_body_alias
4288 (single_init_fn, fn);
4289 gcc_assert (alias != NULL);
4291 #endif
4294 finish_then_clause (if_stmt);
4295 finish_if_stmt (if_stmt);
4296 finish_function_body (body);
4297 expand_or_defer_fn (finish_function (0));
4300 /* We're at the end of compilation, so generate any mangling aliases that
4301 we've been saving up, if DECL is going to be output and ID2 isn't
4302 already taken by another declaration. */
4304 static void
4305 generate_mangling_alias (tree decl, tree id2)
4307 /* If there's a declaration already using this mangled name,
4308 don't create a compatibility alias that conflicts. */
4309 if (IDENTIFIER_GLOBAL_VALUE (id2))
4310 return;
4312 struct cgraph_node *n = NULL;
4313 if (TREE_CODE (decl) == FUNCTION_DECL
4314 && !(n = cgraph_node::get (decl)))
4315 /* Don't create an alias to an unreferenced function. */
4316 return;
4318 tree alias = make_alias_for (decl, id2);
4319 SET_IDENTIFIER_GLOBAL_VALUE (id2, alias);
4320 DECL_IGNORED_P (alias) = 1;
4321 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
4322 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
4323 if (vague_linkage_p (decl))
4324 DECL_WEAK (alias) = 1;
4325 if (TREE_CODE (decl) == FUNCTION_DECL)
4326 n->create_same_body_alias (alias, decl);
4327 else
4328 varpool_node::create_extra_name_alias (alias, decl);
4331 /* Note that we might want to emit an alias with the symbol ID2 for DECL at
4332 the end of translation, for compatibility across bugs in the mangling
4333 implementation. */
4335 void
4336 note_mangling_alias (tree decl ATTRIBUTE_UNUSED, tree id2 ATTRIBUTE_UNUSED)
4338 #ifdef ASM_OUTPUT_DEF
4339 if (!defer_mangling_aliases)
4340 generate_mangling_alias (decl, id2);
4341 else
4343 vec_safe_push (mangling_aliases, decl);
4344 vec_safe_push (mangling_aliases, id2);
4346 #endif
4349 /* Emit all mangling aliases that were deferred up to this point. */
4351 void
4352 generate_mangling_aliases ()
4354 while (!vec_safe_is_empty (mangling_aliases))
4356 tree id2 = mangling_aliases->pop();
4357 tree decl = mangling_aliases->pop();
4358 generate_mangling_alias (decl, id2);
4360 defer_mangling_aliases = false;
4363 /* The entire file is now complete. If requested, dump everything
4364 to a file. */
4366 static void
4367 dump_tu (void)
4369 dump_flags_t flags;
4370 if (FILE *stream = dump_begin (raw_dump_id, &flags))
4372 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4373 dump_end (raw_dump_id, stream);
4377 static location_t locus_at_end_of_parsing;
4379 /* Check the deallocation functions for CODE to see if we want to warn that
4380 only one was defined. */
4382 static void
4383 maybe_warn_sized_delete (enum tree_code code)
4385 tree sized = NULL_TREE;
4386 tree unsized = NULL_TREE;
4388 for (ovl_iterator iter (IDENTIFIER_GLOBAL_VALUE (cp_operator_id (code)));
4389 iter; ++iter)
4391 tree fn = *iter;
4392 /* We're only interested in usual deallocation functions. */
4393 if (!usual_deallocation_fn_p (fn))
4394 continue;
4395 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
4396 unsized = fn;
4397 else
4398 sized = fn;
4400 if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
4401 warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
4402 "the program should also define %qD", sized);
4403 else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
4404 warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
4405 "the program should also define %qD", unsized);
4408 /* Check the global deallocation functions to see if we want to warn about
4409 defining unsized without sized (or vice versa). */
4411 static void
4412 maybe_warn_sized_delete ()
4414 if (!flag_sized_deallocation || !warn_sized_deallocation)
4415 return;
4416 maybe_warn_sized_delete (DELETE_EXPR);
4417 maybe_warn_sized_delete (VEC_DELETE_EXPR);
4420 /* Earlier we left PTRMEM_CST in variable initializers alone so that we could
4421 look them up when evaluating non-type template parameters. Now we need to
4422 lower them to something the back end can understand. */
4424 static void
4425 lower_var_init ()
4427 varpool_node *node;
4428 FOR_EACH_VARIABLE (node)
4430 tree d = node->decl;
4431 if (tree init = DECL_INITIAL (d))
4432 DECL_INITIAL (d) = cplus_expand_constant (init);
4436 /* This routine is called at the end of compilation.
4437 Its job is to create all the code needed to initialize and
4438 destroy the global aggregates. We do the destruction
4439 first, since that way we only need to reverse the decls once. */
4441 void
4442 c_parse_final_cleanups (void)
4444 tree vars;
4445 bool reconsider;
4446 size_t i;
4447 unsigned ssdf_count = 0;
4448 int retries = 0;
4449 tree decl;
4451 locus_at_end_of_parsing = input_location;
4452 at_eof = 1;
4454 /* Bad parse errors. Just forget about it. */
4455 if (! global_bindings_p () || current_class_type
4456 || !vec_safe_is_empty (decl_namespace_list))
4457 return;
4459 /* This is the point to write out a PCH if we're doing that.
4460 In that case we do not want to do anything else. */
4461 if (pch_file)
4463 /* Mangle all symbols at PCH creation time. */
4464 symtab_node *node;
4465 FOR_EACH_SYMBOL (node)
4466 if (! is_a <varpool_node *> (node)
4467 || ! DECL_HARD_REGISTER (node->decl))
4468 DECL_ASSEMBLER_NAME (node->decl);
4469 c_common_write_pch ();
4470 dump_tu ();
4471 /* Ensure even the callers don't try to finalize the CU. */
4472 flag_syntax_only = 1;
4473 return;
4476 timevar_stop (TV_PHASE_PARSING);
4477 timevar_start (TV_PHASE_DEFERRED);
4479 symtab->process_same_body_aliases ();
4481 /* Handle -fdump-ada-spec[-slim] */
4482 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4484 if (flag_dump_ada_spec_slim)
4485 collect_source_ref (main_input_filename);
4486 else
4487 collect_source_refs (global_namespace);
4489 dump_ada_specs (collect_all_refs, cpp_check);
4492 /* FIXME - huh? was input_line -= 1;*/
4494 /* We now have to write out all the stuff we put off writing out.
4495 These include:
4497 o Template specializations that we have not yet instantiated,
4498 but which are needed.
4499 o Initialization and destruction for non-local objects with
4500 static storage duration. (Local objects with static storage
4501 duration are initialized when their scope is first entered,
4502 and are cleaned up via atexit.)
4503 o Virtual function tables.
4505 All of these may cause others to be needed. For example,
4506 instantiating one function may cause another to be needed, and
4507 generating the initializer for an object may cause templates to be
4508 instantiated, etc., etc. */
4510 emit_support_tinfos ();
4514 tree t;
4515 tree decl;
4517 reconsider = false;
4519 /* If there are templates that we've put off instantiating, do
4520 them now. */
4521 instantiate_pending_templates (retries);
4522 ggc_collect ();
4524 /* Write out virtual tables as required. Writing out the
4525 virtual table for a template class may cause the
4526 instantiation of members of that class. If we write out
4527 vtables then we remove the class from our list so we don't
4528 have to look at it again. */
4529 for (i = keyed_classes->length ();
4530 keyed_classes->iterate (--i, &t);)
4531 if (maybe_emit_vtables (t))
4533 reconsider = true;
4534 keyed_classes->unordered_remove (i);
4537 /* Write out needed type info variables. We have to be careful
4538 looping through unemitted decls, because emit_tinfo_decl may
4539 cause other variables to be needed. New elements will be
4540 appended, and we remove from the vector those that actually
4541 get emitted. */
4542 for (i = unemitted_tinfo_decls->length ();
4543 unemitted_tinfo_decls->iterate (--i, &t);)
4544 if (emit_tinfo_decl (t))
4546 reconsider = true;
4547 unemitted_tinfo_decls->unordered_remove (i);
4550 /* The list of objects with static storage duration is built up
4551 in reverse order. We clear STATIC_AGGREGATES so that any new
4552 aggregates added during the initialization of these will be
4553 initialized in the correct order when we next come around the
4554 loop. */
4555 vars = prune_vars_needing_no_initialization (&static_aggregates);
4557 if (vars)
4559 /* We need to start a new initialization function each time
4560 through the loop. That's because we need to know which
4561 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4562 isn't computed until a function is finished, and written
4563 out. That's a deficiency in the back end. When this is
4564 fixed, these initialization functions could all become
4565 inline, with resulting performance improvements. */
4566 tree ssdf_body;
4568 /* Set the line and file, so that it is obviously not from
4569 the source file. */
4570 input_location = locus_at_end_of_parsing;
4571 ssdf_body = start_static_storage_duration_function (ssdf_count);
4573 /* Make sure the back end knows about all the variables. */
4574 write_out_vars (vars);
4576 /* First generate code to do all the initializations. */
4577 if (vars)
4578 do_static_initialization_or_destruction (vars, /*initp=*/true);
4580 /* Then, generate code to do all the destructions. Do these
4581 in reverse order so that the most recently constructed
4582 variable is the first destroyed. If we're using
4583 __cxa_atexit, then we don't need to do this; functions
4584 were registered at initialization time to destroy the
4585 local statics. */
4586 if (!flag_use_cxa_atexit && vars)
4588 vars = nreverse (vars);
4589 do_static_initialization_or_destruction (vars, /*initp=*/false);
4591 else
4592 vars = NULL_TREE;
4594 /* Finish up the static storage duration function for this
4595 round. */
4596 input_location = locus_at_end_of_parsing;
4597 finish_static_storage_duration_function (ssdf_body);
4599 /* All those initializations and finalizations might cause
4600 us to need more inline functions, more template
4601 instantiations, etc. */
4602 reconsider = true;
4603 ssdf_count++;
4604 /* ??? was: locus_at_end_of_parsing.line++; */
4607 /* Now do the same for thread_local variables. */
4608 handle_tls_init ();
4610 /* Go through the set of inline functions whose bodies have not
4611 been emitted yet. If out-of-line copies of these functions
4612 are required, emit them. */
4613 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4615 /* Does it need synthesizing? */
4616 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4617 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4619 /* Even though we're already at the top-level, we push
4620 there again. That way, when we pop back a few lines
4621 hence, all of our state is restored. Otherwise,
4622 finish_function doesn't clean things up, and we end
4623 up with CURRENT_FUNCTION_DECL set. */
4624 push_to_top_level ();
4625 /* The decl's location will mark where it was first
4626 needed. Save that so synthesize method can indicate
4627 where it was needed from, in case of error */
4628 input_location = DECL_SOURCE_LOCATION (decl);
4629 synthesize_method (decl);
4630 pop_from_top_level ();
4631 reconsider = true;
4634 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4635 generate_tls_wrapper (decl);
4637 if (!DECL_SAVED_TREE (decl))
4638 continue;
4640 cgraph_node *node = cgraph_node::get_create (decl);
4642 /* We lie to the back end, pretending that some functions
4643 are not defined when they really are. This keeps these
4644 functions from being put out unnecessarily. But, we must
4645 stop lying when the functions are referenced, or if they
4646 are not comdat since they need to be put out now. If
4647 DECL_INTERFACE_KNOWN, then we have already set
4648 DECL_EXTERNAL appropriately, so there's no need to check
4649 again, and we do not want to clear DECL_EXTERNAL if a
4650 previous call to import_export_decl set it.
4652 This is done in a separate for cycle, because if some
4653 deferred function is contained in another deferred
4654 function later in deferred_fns varray,
4655 rest_of_compilation would skip this function and we
4656 really cannot expand the same function twice. */
4657 import_export_decl (decl);
4658 if (DECL_NOT_REALLY_EXTERN (decl)
4659 && DECL_INITIAL (decl)
4660 && decl_needed_p (decl))
4662 if (node->cpp_implicit_alias)
4663 node = node->get_alias_target ();
4665 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
4666 NULL, true);
4667 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4668 group, we need to mark all symbols in the same comdat group
4669 that way. */
4670 if (node->same_comdat_group)
4671 for (cgraph_node *next
4672 = dyn_cast<cgraph_node *> (node->same_comdat_group);
4673 next != node;
4674 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
4675 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
4676 NULL, true);
4679 /* If we're going to need to write this function out, and
4680 there's already a body for it, create RTL for it now.
4681 (There might be no body if this is a method we haven't
4682 gotten around to synthesizing yet.) */
4683 if (!DECL_EXTERNAL (decl)
4684 && decl_needed_p (decl)
4685 && !TREE_ASM_WRITTEN (decl)
4686 && !node->definition)
4688 /* We will output the function; no longer consider it in this
4689 loop. */
4690 DECL_DEFER_OUTPUT (decl) = 0;
4691 /* Generate RTL for this function now that we know we
4692 need it. */
4693 expand_or_defer_fn (decl);
4694 /* If we're compiling -fsyntax-only pretend that this
4695 function has been written out so that we don't try to
4696 expand it again. */
4697 if (flag_syntax_only)
4698 TREE_ASM_WRITTEN (decl) = 1;
4699 reconsider = true;
4703 if (wrapup_namespace_globals ())
4704 reconsider = true;
4706 /* Static data members are just like namespace-scope globals. */
4707 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4709 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4710 /* Don't write it out if we haven't seen a definition. */
4711 || (DECL_IN_AGGR_P (decl) && !DECL_INLINE_VAR_P (decl)))
4712 continue;
4713 import_export_decl (decl);
4714 /* If this static data member is needed, provide it to the
4715 back end. */
4716 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4717 DECL_EXTERNAL (decl) = 0;
4719 if (vec_safe_length (pending_statics) != 0
4720 && wrapup_global_declarations (pending_statics->address (),
4721 pending_statics->length ()))
4722 reconsider = true;
4724 retries++;
4726 while (reconsider);
4728 lower_var_init ();
4730 generate_mangling_aliases ();
4732 /* All used inline functions must have a definition at this point. */
4733 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4735 if (/* Check online inline functions that were actually used. */
4736 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4737 /* If the definition actually was available here, then the
4738 fact that the function was not defined merely represents
4739 that for some reason (use of a template repository,
4740 #pragma interface, etc.) we decided not to emit the
4741 definition here. */
4742 && !DECL_INITIAL (decl)
4743 /* Don't complain if the template was defined. */
4744 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4745 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4746 (template_for_substitution (decl)))))
4748 warning_at (DECL_SOURCE_LOCATION (decl), 0,
4749 "inline function %qD used but never defined", decl);
4750 /* Avoid a duplicate warning from check_global_declaration. */
4751 TREE_NO_WARNING (decl) = 1;
4755 /* So must decls that use a type with no linkage. */
4756 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4757 no_linkage_error (decl);
4759 maybe_warn_sized_delete ();
4761 /* Then, do the Objective-C stuff. This is where all the
4762 Objective-C module stuff gets generated (symtab,
4763 class/protocol/selector lists etc). This must be done after C++
4764 templates, destructors etc. so that selectors used in C++
4765 templates are properly allocated. */
4766 if (c_dialect_objc ())
4767 objc_write_global_declarations ();
4769 /* We give C linkage to static constructors and destructors. */
4770 push_lang_context (lang_name_c);
4772 /* Generate initialization and destruction functions for all
4773 priorities for which they are required. */
4774 if (priority_info_map)
4775 splay_tree_foreach (priority_info_map,
4776 generate_ctor_and_dtor_functions_for_priority,
4777 /*data=*/&locus_at_end_of_parsing);
4778 else if (c_dialect_objc () && objc_static_init_needed_p ())
4779 /* If this is obj-c++ and we need a static init, call
4780 generate_ctor_or_dtor_function. */
4781 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4782 DEFAULT_INIT_PRIORITY,
4783 &locus_at_end_of_parsing);
4785 /* We're done with the splay-tree now. */
4786 if (priority_info_map)
4787 splay_tree_delete (priority_info_map);
4789 /* Generate any missing aliases. */
4790 maybe_apply_pending_pragma_weaks ();
4792 /* We're done with static constructors, so we can go back to "C++"
4793 linkage now. */
4794 pop_lang_context ();
4796 if (flag_vtable_verify)
4798 vtv_recover_class_info ();
4799 vtv_compute_class_hierarchy_transitive_closure ();
4800 vtv_build_vtable_verify_fndecl ();
4803 perform_deferred_noexcept_checks ();
4805 finish_repo ();
4807 fini_constexpr ();
4809 /* The entire file is now complete. If requested, dump everything
4810 to a file. */
4811 dump_tu ();
4813 if (flag_detailed_statistics)
4815 dump_tree_statistics ();
4816 dump_time_statistics ();
4819 timevar_stop (TV_PHASE_DEFERRED);
4820 timevar_start (TV_PHASE_PARSING);
4822 /* Indicate that we're done with front end processing. */
4823 at_eof = 2;
4826 /* Perform any post compilation-proper cleanups for the C++ front-end.
4827 This should really go away. No front-end should need to do
4828 anything past the compilation process. */
4830 void
4831 cxx_post_compilation_parsing_cleanups (void)
4833 timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS);
4835 if (flag_vtable_verify)
4837 /* Generate the special constructor initialization function that
4838 calls __VLTRegisterPairs, and give it a very high
4839 initialization priority. This must be done after
4840 finalize_compilation_unit so that we have accurate
4841 information about which vtable will actually be emitted. */
4842 vtv_generate_init_routine ();
4845 input_location = locus_at_end_of_parsing;
4847 if (flag_checking)
4848 validate_conversion_obstack ();
4850 timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS);
4853 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4854 function to call in parse-tree form; it has not yet been
4855 semantically analyzed. ARGS are the arguments to the function.
4856 They have already been semantically analyzed. This may change
4857 ARGS. */
4859 tree
4860 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4861 tsubst_flags_t complain)
4863 tree orig_fn;
4864 vec<tree, va_gc> *orig_args = NULL;
4865 tree expr;
4866 tree object;
4868 orig_fn = fn;
4869 object = TREE_OPERAND (fn, 0);
4871 if (processing_template_decl)
4873 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4874 || TREE_CODE (fn) == MEMBER_REF);
4875 if (type_dependent_expression_p (fn)
4876 || any_type_dependent_arguments_p (*args))
4877 return build_min_nt_call_vec (fn, *args);
4879 orig_args = make_tree_vector_copy (*args);
4881 /* Transform the arguments and add the implicit "this"
4882 parameter. That must be done before the FN is transformed
4883 because we depend on the form of FN. */
4884 make_args_non_dependent (*args);
4885 object = build_non_dependent_expr (object);
4886 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4888 if (TREE_CODE (fn) == DOTSTAR_EXPR)
4889 object = cp_build_addr_expr (object, complain);
4890 vec_safe_insert (*args, 0, object);
4892 /* Now that the arguments are done, transform FN. */
4893 fn = build_non_dependent_expr (fn);
4896 /* A qualified name corresponding to a bound pointer-to-member is
4897 represented as an OFFSET_REF:
4899 struct B { void g(); };
4900 void (B::*p)();
4901 void B::g() { (this->*p)(); } */
4902 if (TREE_CODE (fn) == OFFSET_REF)
4904 tree object_addr = cp_build_addr_expr (object, complain);
4905 fn = TREE_OPERAND (fn, 1);
4906 fn = get_member_function_from_ptrfunc (&object_addr, fn,
4907 complain);
4908 vec_safe_insert (*args, 0, object_addr);
4911 if (CLASS_TYPE_P (TREE_TYPE (fn)))
4912 expr = build_op_call (fn, args, complain);
4913 else
4914 expr = cp_build_function_call_vec (fn, args, complain);
4915 if (processing_template_decl && expr != error_mark_node)
4916 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
4918 if (orig_args != NULL)
4919 release_tree_vector (orig_args);
4921 return expr;
4925 void
4926 check_default_args (tree x)
4928 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4929 bool saw_def = false;
4930 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4931 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4933 if (TREE_PURPOSE (arg))
4934 saw_def = true;
4935 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
4937 error ("default argument missing for parameter %P of %q+#D", i, x);
4938 TREE_PURPOSE (arg) = error_mark_node;
4943 /* Return true if function DECL can be inlined. This is used to force
4944 instantiation of methods that might be interesting for inlining. */
4945 bool
4946 possibly_inlined_p (tree decl)
4948 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4949 if (DECL_UNINLINABLE (decl))
4950 return false;
4951 if (!optimize)
4952 return DECL_DECLARED_INLINE_P (decl);
4953 /* When optimizing, we might inline everything when flatten
4954 attribute or heuristics inlining for size or autoinlining
4955 is used. */
4956 return true;
4959 /* Normally, we can wait until instantiation-time to synthesize DECL.
4960 However, if DECL is a static data member initialized with a constant
4961 or a constexpr function, we need it right now because a reference to
4962 such a data member or a call to such function is not value-dependent.
4963 For a function that uses auto in the return type, we need to instantiate
4964 it to find out its type. For OpenMP user defined reductions, we need
4965 them instantiated for reduction clauses which inline them by hand
4966 directly. */
4968 static void
4969 maybe_instantiate_decl (tree decl)
4971 if (DECL_LANG_SPECIFIC (decl)
4972 && DECL_TEMPLATE_INFO (decl)
4973 && (decl_maybe_constant_var_p (decl)
4974 || (TREE_CODE (decl) == FUNCTION_DECL
4975 && DECL_OMP_DECLARE_REDUCTION_P (decl))
4976 || undeduced_auto_decl (decl))
4977 && !DECL_DECLARED_CONCEPT_P (decl)
4978 && !uses_template_parms (DECL_TI_ARGS (decl)))
4980 /* Instantiating a function will result in garbage collection. We
4981 must treat this situation as if we were within the body of a
4982 function so as to avoid collecting live data only referenced from
4983 the stack (such as overload resolution candidates). */
4984 ++function_depth;
4985 instantiate_decl (decl, /*defer_ok=*/false,
4986 /*expl_inst_class_mem_p=*/false);
4987 --function_depth;
4991 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
4992 If DECL is a specialization or implicitly declared class member,
4993 generate the actual definition. Return false if something goes
4994 wrong, true otherwise. */
4996 bool
4997 mark_used (tree decl, tsubst_flags_t complain)
4999 /* If DECL is a BASELINK for a single function, then treat it just
5000 like the DECL for the function. Otherwise, if the BASELINK is
5001 for an overloaded function, we don't know which function was
5002 actually used until after overload resolution. */
5003 if (BASELINK_P (decl))
5005 decl = BASELINK_FUNCTIONS (decl);
5006 if (really_overloaded_fn (decl))
5007 return true;
5008 decl = OVL_FIRST (decl);
5011 /* Set TREE_USED for the benefit of -Wunused. */
5012 TREE_USED (decl) = 1;
5013 /* And for structured bindings also the underlying decl. */
5014 if (DECL_DECOMPOSITION_P (decl) && DECL_DECOMP_BASE (decl))
5015 TREE_USED (DECL_DECOMP_BASE (decl)) = 1;
5017 if (TREE_CODE (decl) == TEMPLATE_DECL)
5018 return true;
5020 if (DECL_CLONED_FUNCTION_P (decl))
5021 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5023 /* Mark enumeration types as used. */
5024 if (TREE_CODE (decl) == CONST_DECL)
5025 used_types_insert (DECL_CONTEXT (decl));
5027 if (TREE_CODE (decl) == FUNCTION_DECL)
5028 maybe_instantiate_noexcept (decl);
5030 if (TREE_CODE (decl) == FUNCTION_DECL
5031 && DECL_DELETED_FN (decl))
5033 if (DECL_ARTIFICIAL (decl))
5035 if (DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
5036 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
5038 /* We mark a lambda conversion op as deleted if we can't
5039 generate it properly; see maybe_add_lambda_conv_op. */
5040 sorry ("converting lambda which uses %<...%> to "
5041 "function pointer");
5042 return false;
5045 if (complain & tf_error)
5047 error ("use of deleted function %qD", decl);
5048 if (!maybe_explain_implicit_delete (decl))
5049 inform (DECL_SOURCE_LOCATION (decl), "declared here");
5051 return false;
5054 if (TREE_DEPRECATED (decl) && (complain & tf_warning)
5055 && deprecated_state != DEPRECATED_SUPPRESS)
5056 warn_deprecated_use (decl, NULL_TREE);
5058 /* We can only check DECL_ODR_USED on variables or functions with
5059 DECL_LANG_SPECIFIC set, and these are also the only decls that we
5060 might need special handling for. */
5061 if (!VAR_OR_FUNCTION_DECL_P (decl)
5062 || DECL_LANG_SPECIFIC (decl) == NULL
5063 || DECL_THUNK_P (decl))
5065 if (!processing_template_decl
5066 && !require_deduced_type (decl, complain))
5067 return false;
5068 return true;
5071 /* We only want to do this processing once. We don't need to keep trying
5072 to instantiate inline templates, because unit-at-a-time will make sure
5073 we get them compiled before functions that want to inline them. */
5074 if (DECL_ODR_USED (decl))
5075 return true;
5077 /* Normally, we can wait until instantiation-time to synthesize DECL.
5078 However, if DECL is a static data member initialized with a constant
5079 or a constexpr function, we need it right now because a reference to
5080 such a data member or a call to such function is not value-dependent.
5081 For a function that uses auto in the return type, we need to instantiate
5082 it to find out its type. For OpenMP user defined reductions, we need
5083 them instantiated for reduction clauses which inline them by hand
5084 directly. */
5085 maybe_instantiate_decl (decl);
5087 if (processing_template_decl || in_template_function ())
5088 return true;
5090 /* Check this too in case we're within instantiate_non_dependent_expr. */
5091 if (DECL_TEMPLATE_INFO (decl)
5092 && uses_template_parms (DECL_TI_ARGS (decl)))
5093 return true;
5095 if (!require_deduced_type (decl, complain))
5096 return false;
5098 if (builtin_pack_fn_p (decl))
5100 error ("use of built-in parameter pack %qD outside of a template",
5101 DECL_NAME (decl));
5102 return false;
5105 /* If we don't need a value, then we don't need to synthesize DECL. */
5106 if (cp_unevaluated_operand || in_discarded_stmt)
5107 return true;
5109 DECL_ODR_USED (decl) = 1;
5110 if (DECL_CLONED_FUNCTION_P (decl))
5111 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5113 /* DR 757: A type without linkage shall not be used as the type of a
5114 variable or function with linkage, unless
5115 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5116 o the variable or function is not used (3.2 [basic.def.odr]) or is
5117 defined in the same translation unit. */
5118 if (cxx_dialect > cxx98
5119 && decl_linkage (decl) != lk_none
5120 && !DECL_EXTERN_C_P (decl)
5121 && !DECL_ARTIFICIAL (decl)
5122 && !decl_defined_p (decl)
5123 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
5125 if (is_local_extern (decl))
5126 /* There's no way to define a local extern, and adding it to
5127 the vector interferes with GC, so give an error now. */
5128 no_linkage_error (decl);
5129 else
5130 vec_safe_push (no_linkage_decls, decl);
5133 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
5134 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
5135 /* Remember it, so we can check it was defined. */
5136 note_vague_linkage_fn (decl);
5138 /* Is it a synthesized method that needs to be synthesized? */
5139 if (TREE_CODE (decl) == FUNCTION_DECL
5140 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5141 && DECL_DEFAULTED_FN (decl)
5142 /* A function defaulted outside the class is synthesized either by
5143 cp_finish_decl or instantiate_decl. */
5144 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
5145 && ! DECL_INITIAL (decl))
5147 /* Defer virtual destructors so that thunks get the right
5148 linkage. */
5149 if (DECL_VIRTUAL_P (decl) && !at_eof)
5151 note_vague_linkage_fn (decl);
5152 return true;
5155 /* Remember the current location for a function we will end up
5156 synthesizing. Then we can inform the user where it was
5157 required in the case of error. */
5158 DECL_SOURCE_LOCATION (decl) = input_location;
5160 /* Synthesizing an implicitly defined member function will result in
5161 garbage collection. We must treat this situation as if we were
5162 within the body of a function so as to avoid collecting live data
5163 on the stack (such as overload resolution candidates).
5165 We could just let cp_write_global_declarations handle synthesizing
5166 this function by adding it to deferred_fns, but doing
5167 it at the use site produces better error messages. */
5168 ++function_depth;
5169 synthesize_method (decl);
5170 --function_depth;
5171 /* If this is a synthesized method we don't need to
5172 do the instantiation test below. */
5174 else if (VAR_OR_FUNCTION_DECL_P (decl)
5175 && DECL_TEMPLATE_INFO (decl)
5176 && !DECL_DECLARED_CONCEPT_P (decl)
5177 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5178 || always_instantiate_p (decl)))
5179 /* If this is a function or variable that is an instance of some
5180 template, we now know that we will need to actually do the
5181 instantiation. We check that DECL is not an explicit
5182 instantiation because that is not checked in instantiate_decl.
5184 We put off instantiating functions in order to improve compile
5185 times. Maintaining a stack of active functions is expensive,
5186 and the inliner knows to instantiate any functions it might
5187 need. Therefore, we always try to defer instantiation. */
5189 ++function_depth;
5190 instantiate_decl (decl, /*defer_ok=*/true,
5191 /*expl_inst_class_mem_p=*/false);
5192 --function_depth;
5195 return true;
5198 bool
5199 mark_used (tree decl)
5201 return mark_used (decl, tf_warning_or_error);
5204 tree
5205 vtv_start_verification_constructor_init_function (void)
5207 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5210 tree
5211 vtv_finish_verification_constructor_init_function (tree function_body)
5213 tree fn;
5215 finish_compound_stmt (function_body);
5216 fn = finish_function (0);
5217 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5218 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5220 return fn;
5223 #include "gt-cp-decl2.h"