* decl2.c (reset_type_linkage_2): Dont't change ctor name.
[official-gcc.git] / gcc / cp / decl2.c
blob62a50acc93d86eced013d2e950139accb4b13677
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 if (DECL_USE_TEMPLATE (function)
562 && !(TREE_CODE (function) == TEMPLATE_DECL
563 && DECL_TEMPLATE_SPECIALIZATION (function))
564 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
565 /* Since this is a specialization of a member template,
566 we're not going to find the declaration in the class.
567 For example, in:
569 struct S { template <typename T> void f(T); };
570 template <> void S::f(int);
572 we're not going to find `S::f(int)', but there's no
573 reason we should, either. We let our callers know we didn't
574 find the method, but we don't complain. */
575 return NULL_TREE;
577 /* Basic sanity check: for a template function, the template parameters
578 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
579 if (TREE_CODE (function) == TEMPLATE_DECL)
581 if (template_parms
582 && !comp_template_parms (template_parms,
583 DECL_TEMPLATE_PARMS (function)))
585 error ("template parameter lists provided don%'t match the "
586 "template parameters of %qD", function);
587 return error_mark_node;
589 template_parms = DECL_TEMPLATE_PARMS (function);
592 /* OK, is this a definition of a member template? */
593 bool is_template = (template_parms != NULL_TREE);
595 /* [temp.mem]
597 A destructor shall not be a member template. */
598 if (DECL_DESTRUCTOR_P (function) && is_template)
600 error ("destructor %qD declared as member template", function);
601 return error_mark_node;
604 /* We must enter the scope here, because conversion operators are
605 named by target type, and type equivalence relies on typenames
606 resolving within the scope of CTYPE. */
607 tree pushed_scope = push_scope (ctype);
608 tree matched = NULL_TREE;
609 tree fns = lookup_fnfields_slot (ctype, DECL_NAME (function));
611 for (ovl_iterator iter (fns); !matched && iter; ++iter)
613 tree fndecl = *iter;
614 tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
615 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
617 /* We cannot simply call decls_match because this doesn't work
618 for static member functions that are pretending to be
619 methods, and because the name may have been changed by
620 asm("new_name"). */
622 /* Get rid of the this parameter on functions that become
623 static. */
624 if (DECL_STATIC_FUNCTION_P (fndecl)
625 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
626 p1 = TREE_CHAIN (p1);
628 /* A member template definition only matches a member template
629 declaration. */
630 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
631 continue;
633 /* ref-qualifier or absence of same must match. */
634 if (type_memfn_rqual (TREE_TYPE (function))
635 != type_memfn_rqual (TREE_TYPE (fndecl)))
636 continue;
638 // Include constraints in the match.
639 tree c1 = get_constraints (function);
640 tree c2 = get_constraints (fndecl);
642 /* While finding a match, same types and params are not enough
643 if the function is versioned. Also check version ("target")
644 attributes. */
645 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
646 TREE_TYPE (TREE_TYPE (fndecl)))
647 && compparms (p1, p2)
648 && !targetm.target_option.function_versions (function, fndecl)
649 && (!is_template
650 || comp_template_parms (template_parms,
651 DECL_TEMPLATE_PARMS (fndecl)))
652 && equivalent_constraints (c1, c2)
653 && (DECL_TEMPLATE_SPECIALIZATION (function)
654 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
655 && (!DECL_TEMPLATE_SPECIALIZATION (function)
656 || (DECL_TI_TEMPLATE (function) == DECL_TI_TEMPLATE (fndecl))))
657 matched = fndecl;
660 if (!matched)
662 if (!COMPLETE_TYPE_P (ctype))
663 cxx_incomplete_type_error (function, ctype);
664 else
666 if (DECL_CONV_FN_P (function))
667 fns = lookup_all_conversions (ctype);
669 error_at (DECL_SOURCE_LOCATION (function),
670 "no declaration matches %q#D", function);
671 if (fns)
672 print_candidates (fns);
673 else if (DECL_CONV_FN_P (function))
674 inform (DECL_SOURCE_LOCATION (function),
675 "no conversion operators declared");
676 else
677 inform (DECL_SOURCE_LOCATION (function),
678 "no functions named %qD", function);
679 inform (DECL_SOURCE_LOCATION (TYPE_NAME (ctype)),
680 "%#qT defined here", ctype);
682 matched = error_mark_node;
685 if (pushed_scope)
686 pop_scope (pushed_scope);
688 return matched;
691 /* DECL is a function with vague linkage. Remember it so that at the
692 end of the translation unit we can decide whether or not to emit
693 it. */
695 void
696 note_vague_linkage_fn (tree decl)
698 DECL_DEFER_OUTPUT (decl) = 1;
699 vec_safe_push (deferred_fns, decl);
702 /* As above, but for variable template instantiations. */
704 void
705 note_variable_template_instantiation (tree decl)
707 vec_safe_push (pending_statics, decl);
710 /* We have just processed the DECL, which is a static data member.
711 The other parameters are as for cp_finish_decl. */
713 void
714 finish_static_data_member_decl (tree decl,
715 tree init, bool init_const_expr_p,
716 tree asmspec_tree,
717 int flags)
719 DECL_CONTEXT (decl) = current_class_type;
721 /* We cannot call pushdecl here, because that would fill in the
722 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
723 the right thing, namely, to put this decl out straight away. */
725 if (! processing_template_decl)
726 vec_safe_push (pending_statics, decl);
728 if (LOCAL_CLASS_P (current_class_type)
729 /* We already complained about the template definition. */
730 && !DECL_TEMPLATE_INSTANTIATION (decl))
731 permerror (input_location, "local class %q#T shall not have static data member %q#D",
732 current_class_type, decl);
733 else
734 for (tree t = current_class_type; TYPE_P (t);
735 t = CP_TYPE_CONTEXT (t))
736 if (TYPE_UNNAMED_P (t))
738 if (permerror (DECL_SOURCE_LOCATION (decl),
739 "static data member %qD in unnamed class", decl))
740 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
741 "unnamed class defined here");
742 break;
745 DECL_IN_AGGR_P (decl) = 1;
747 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
748 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
749 SET_VAR_HAD_UNKNOWN_BOUND (decl);
751 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
754 /* DECLARATOR and DECLSPECS correspond to a class member. The other
755 parameters are as for cp_finish_decl. Return the DECL for the
756 class member declared. */
758 tree
759 grokfield (const cp_declarator *declarator,
760 cp_decl_specifier_seq *declspecs,
761 tree init, bool init_const_expr_p,
762 tree asmspec_tree,
763 tree attrlist)
765 tree value;
766 const char *asmspec = 0;
767 int flags;
768 tree name;
770 if (init
771 && TREE_CODE (init) == TREE_LIST
772 && TREE_VALUE (init) == error_mark_node
773 && TREE_CHAIN (init) == NULL_TREE)
774 init = NULL_TREE;
776 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
777 if (! value || value == error_mark_node)
778 /* friend or constructor went bad. */
779 return error_mark_node;
780 if (TREE_TYPE (value) == error_mark_node)
781 return value;
783 if (TREE_CODE (value) == TYPE_DECL && init)
785 error ("typedef %qD is initialized (use decltype instead)", value);
786 init = NULL_TREE;
789 /* Pass friendly classes back. */
790 if (value == void_type_node)
791 return value;
794 name = DECL_NAME (value);
796 if (name != NULL_TREE)
798 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
800 error ("explicit template argument list not allowed");
801 return error_mark_node;
804 if (IDENTIFIER_POINTER (name)[0] == '_'
805 && id_equal (name, "_vptr"))
806 error ("member %qD conflicts with virtual function table field name",
807 value);
810 /* Stash away type declarations. */
811 if (TREE_CODE (value) == TYPE_DECL)
813 DECL_NONLOCAL (value) = 1;
814 DECL_CONTEXT (value) = current_class_type;
816 if (attrlist)
818 int attrflags = 0;
820 /* If this is a typedef that names the class for linkage purposes
821 (7.1.3p8), apply any attributes directly to the type. */
822 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
823 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
824 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
826 cplus_decl_attributes (&value, attrlist, attrflags);
829 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
830 && TREE_TYPE (value) != error_mark_node
831 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
832 set_underlying_type (value);
834 /* It's important that push_template_decl below follows
835 set_underlying_type above so that the created template
836 carries the properly set type of VALUE. */
837 if (processing_template_decl)
838 value = push_template_decl (value);
840 record_locally_defined_typedef (value);
841 return value;
844 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
846 if (!friendp && DECL_IN_AGGR_P (value))
848 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
849 return void_type_node;
852 if (asmspec_tree && asmspec_tree != error_mark_node)
853 asmspec = TREE_STRING_POINTER (asmspec_tree);
855 if (init)
857 if (TREE_CODE (value) == FUNCTION_DECL)
859 if (init == ridpointers[(int)RID_DELETE])
861 DECL_DELETED_FN (value) = 1;
862 DECL_DECLARED_INLINE_P (value) = 1;
863 DECL_INITIAL (value) = error_mark_node;
865 else if (init == ridpointers[(int)RID_DEFAULT])
867 if (defaultable_fn_check (value))
869 DECL_DEFAULTED_FN (value) = 1;
870 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
871 DECL_DECLARED_INLINE_P (value) = 1;
874 else if (TREE_CODE (init) == DEFAULT_ARG)
875 error ("invalid initializer for member function %qD", value);
876 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
878 if (integer_zerop (init))
879 DECL_PURE_VIRTUAL_P (value) = 1;
880 else if (error_operand_p (init))
881 ; /* An error has already been reported. */
882 else
883 error ("invalid initializer for member function %qD",
884 value);
886 else
888 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
889 if (friendp)
890 error ("initializer specified for friend function %qD",
891 value);
892 else
893 error ("initializer specified for static member function %qD",
894 value);
897 else if (TREE_CODE (value) == FIELD_DECL)
898 /* C++11 NSDMI, keep going. */;
899 else if (!VAR_P (value))
900 gcc_unreachable ();
903 /* Pass friend decls back. */
904 if ((TREE_CODE (value) == FUNCTION_DECL
905 || TREE_CODE (value) == TEMPLATE_DECL)
906 && DECL_CONTEXT (value) != current_class_type)
907 return value;
909 /* Need to set this before push_template_decl. */
910 if (VAR_P (value))
911 DECL_CONTEXT (value) = current_class_type;
913 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
915 value = push_template_decl (value);
916 if (error_operand_p (value))
917 return error_mark_node;
920 if (attrlist)
921 cplus_decl_attributes (&value, attrlist, 0);
923 if (init && DIRECT_LIST_INIT_P (init))
924 flags = LOOKUP_NORMAL;
925 else
926 flags = LOOKUP_IMPLICIT;
928 switch (TREE_CODE (value))
930 case VAR_DECL:
931 finish_static_data_member_decl (value, init, init_const_expr_p,
932 asmspec_tree, flags);
933 return value;
935 case FIELD_DECL:
936 if (asmspec)
937 error ("%<asm%> specifiers are not permitted on non-static data members");
938 if (DECL_INITIAL (value) == error_mark_node)
939 init = error_mark_node;
940 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
941 NULL_TREE, flags);
942 DECL_IN_AGGR_P (value) = 1;
943 return value;
945 case FUNCTION_DECL:
946 if (asmspec)
947 set_user_assembler_name (value, asmspec);
949 cp_finish_decl (value,
950 /*init=*/NULL_TREE,
951 /*init_const_expr_p=*/false,
952 asmspec_tree, flags);
954 /* Pass friends back this way. */
955 if (DECL_FRIEND_P (value))
956 return void_type_node;
958 DECL_IN_AGGR_P (value) = 1;
959 return value;
961 default:
962 gcc_unreachable ();
964 return NULL_TREE;
967 /* Like `grokfield', but for bitfields.
968 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
970 tree
971 grokbitfield (const cp_declarator *declarator,
972 cp_decl_specifier_seq *declspecs, tree width,
973 tree attrlist)
975 tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, &attrlist);
977 if (value == error_mark_node)
978 return NULL_TREE; /* friends went bad. */
979 if (TREE_TYPE (value) == error_mark_node)
980 return value;
982 /* Pass friendly classes back. */
983 if (VOID_TYPE_P (value))
984 return void_type_node;
986 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
987 && (POINTER_TYPE_P (value)
988 || !dependent_type_p (TREE_TYPE (value))))
990 error ("bit-field %qD with non-integral type", value);
991 return error_mark_node;
994 if (TREE_CODE (value) == TYPE_DECL)
996 error ("cannot declare %qD to be a bit-field type", value);
997 return NULL_TREE;
1000 /* Usually, finish_struct_1 catches bitfields with invalid types.
1001 But, in the case of bitfields with function type, we confuse
1002 ourselves into thinking they are member functions, so we must
1003 check here. */
1004 if (TREE_CODE (value) == FUNCTION_DECL)
1006 error ("cannot declare bit-field %qD with function type",
1007 DECL_NAME (value));
1008 return NULL_TREE;
1011 if (DECL_IN_AGGR_P (value))
1013 error ("%qD is already defined in the class %qT", value,
1014 DECL_CONTEXT (value));
1015 return void_type_node;
1018 if (TREE_STATIC (value))
1020 error ("static member %qD cannot be a bit-field", value);
1021 return NULL_TREE;
1023 cp_finish_decl (value, NULL_TREE, false, NULL_TREE, 0);
1025 if (width != error_mark_node)
1027 /* The width must be an integer type. */
1028 if (!type_dependent_expression_p (width)
1029 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1030 error ("width of bit-field %qD has non-integral type %qT", value,
1031 TREE_TYPE (width));
1032 else
1034 DECL_INITIAL (value) = width;
1035 SET_DECL_C_BIT_FIELD (value);
1039 DECL_IN_AGGR_P (value) = 1;
1041 if (attrlist)
1042 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1044 return value;
1048 /* Returns true iff ATTR is an attribute which needs to be applied at
1049 instantiation time rather than template definition time. */
1051 static bool
1052 is_late_template_attribute (tree attr, tree decl)
1054 tree name = get_attribute_name (attr);
1055 tree args = TREE_VALUE (attr);
1056 const struct attribute_spec *spec = lookup_attribute_spec (name);
1057 tree arg;
1059 if (!spec)
1060 /* Unknown attribute. */
1061 return false;
1063 /* Attribute weak handling wants to write out assembly right away. */
1064 if (is_attribute_p ("weak", name))
1065 return true;
1067 /* Attributes used and unused are applied directly, as they appertain to
1068 decls. */
1069 if (is_attribute_p ("unused", name)
1070 || is_attribute_p ("used", name))
1071 return false;
1073 /* Attribute tls_model wants to modify the symtab. */
1074 if (is_attribute_p ("tls_model", name))
1075 return true;
1077 /* #pragma omp declare simd attribute needs to be always deferred. */
1078 if (flag_openmp
1079 && is_attribute_p ("omp declare simd", name))
1080 return true;
1082 /* An attribute pack is clearly dependent. */
1083 if (args && PACK_EXPANSION_P (args))
1084 return true;
1086 /* If any of the arguments are dependent expressions, we can't evaluate
1087 the attribute until instantiation time. */
1088 for (arg = args; arg; arg = TREE_CHAIN (arg))
1090 tree t = TREE_VALUE (arg);
1092 /* If the first attribute argument is an identifier, only consider
1093 second and following arguments. Attributes like mode, format,
1094 cleanup and several target specific attributes aren't late
1095 just because they have an IDENTIFIER_NODE as first argument. */
1096 if (arg == args && attribute_takes_identifier_p (name)
1097 && identifier_p (t))
1098 continue;
1100 if (value_dependent_expression_p (t)
1101 || type_dependent_expression_p (t))
1102 return true;
1105 if (TREE_CODE (decl) == TYPE_DECL
1106 || TYPE_P (decl)
1107 || spec->type_required)
1109 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1111 /* We can't apply any attributes to a completely unknown type until
1112 instantiation time. */
1113 enum tree_code code = TREE_CODE (type);
1114 if (code == TEMPLATE_TYPE_PARM
1115 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1116 || code == TYPENAME_TYPE)
1117 return true;
1118 /* Also defer most attributes on dependent types. This is not
1119 necessary in all cases, but is the better default. */
1120 else if (dependent_type_p (type)
1121 /* But some attributes specifically apply to templates. */
1122 && !is_attribute_p ("abi_tag", name)
1123 && !is_attribute_p ("deprecated", name)
1124 && !is_attribute_p ("visibility", name))
1125 return true;
1126 else
1127 return false;
1129 else
1130 return false;
1133 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1134 applied at instantiation time and return them. If IS_DEPENDENT is true,
1135 the declaration itself is dependent, so all attributes should be applied
1136 at instantiation time. */
1138 static tree
1139 splice_template_attributes (tree *attr_p, tree decl)
1141 tree *p = attr_p;
1142 tree late_attrs = NULL_TREE;
1143 tree *q = &late_attrs;
1145 if (!p)
1146 return NULL_TREE;
1148 for (; *p; )
1150 if (is_late_template_attribute (*p, decl))
1152 ATTR_IS_DEPENDENT (*p) = 1;
1153 *q = *p;
1154 *p = TREE_CHAIN (*p);
1155 q = &TREE_CHAIN (*q);
1156 *q = NULL_TREE;
1158 else
1159 p = &TREE_CHAIN (*p);
1162 return late_attrs;
1165 /* Remove any late attributes from the list in ATTR_P and attach them to
1166 DECL_P. */
1168 static void
1169 save_template_attributes (tree *attr_p, tree *decl_p)
1171 tree *q;
1173 if (attr_p && *attr_p == error_mark_node)
1174 return;
1176 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1177 if (!late_attrs)
1178 return;
1180 if (DECL_P (*decl_p))
1181 q = &DECL_ATTRIBUTES (*decl_p);
1182 else
1183 q = &TYPE_ATTRIBUTES (*decl_p);
1185 tree old_attrs = *q;
1187 /* Merge the late attributes at the beginning with the attribute
1188 list. */
1189 late_attrs = merge_attributes (late_attrs, *q);
1190 *q = late_attrs;
1192 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1194 /* We've added new attributes directly to the main variant, so
1195 now we need to update all of the other variants to include
1196 these new attributes. */
1197 tree variant;
1198 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1199 variant = TYPE_NEXT_VARIANT (variant))
1201 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1202 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1207 /* True if ATTRS contains any dependent attributes that affect type
1208 identity. */
1210 bool
1211 any_dependent_type_attributes_p (tree attrs)
1213 for (tree a = attrs; a; a = TREE_CHAIN (a))
1214 if (ATTR_IS_DEPENDENT (a))
1216 const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (a));
1217 if (as && as->affects_type_identity)
1218 return true;
1220 return false;
1223 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1224 to a typedef which gives a previously unnamed class or enum a name for
1225 linkage purposes. */
1227 bool
1228 attributes_naming_typedef_ok (tree attrs)
1230 for (; attrs; attrs = TREE_CHAIN (attrs))
1232 tree name = get_attribute_name (attrs);
1233 if (is_attribute_p ("vector_size", name))
1234 return false;
1236 return true;
1239 /* Like reconstruct_complex_type, but handle also template trees. */
1241 tree
1242 cp_reconstruct_complex_type (tree type, tree bottom)
1244 tree inner, outer;
1245 bool late_return_type_p = false;
1247 if (TYPE_PTR_P (type))
1249 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1250 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1251 TYPE_REF_CAN_ALIAS_ALL (type));
1253 else if (TREE_CODE (type) == REFERENCE_TYPE)
1255 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1256 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1257 TYPE_REF_CAN_ALIAS_ALL (type));
1259 else if (TREE_CODE (type) == ARRAY_TYPE)
1261 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1262 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1263 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1264 element type qualification will be handled by the recursive
1265 cp_reconstruct_complex_type call and cp_build_qualified_type
1266 for ARRAY_TYPEs changes the element type. */
1267 return outer;
1269 else if (TREE_CODE (type) == FUNCTION_TYPE)
1271 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1272 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1273 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1274 outer = apply_memfn_quals (outer,
1275 type_memfn_quals (type),
1276 type_memfn_rqual (type));
1278 else if (TREE_CODE (type) == METHOD_TYPE)
1280 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1281 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1282 /* The build_method_type_directly() routine prepends 'this' to argument list,
1283 so we must compensate by getting rid of it. */
1284 outer
1285 = build_method_type_directly
1286 (class_of_this_parm (type), inner,
1287 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1289 else if (TREE_CODE (type) == OFFSET_TYPE)
1291 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1292 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1294 else
1295 return bottom;
1297 if (TYPE_ATTRIBUTES (type))
1298 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1299 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1301 if (late_return_type_p)
1302 TYPE_HAS_LATE_RETURN_TYPE (outer) = 1;
1304 return outer;
1307 /* Replaces any constexpr expression that may be into the attributes
1308 arguments with their reduced value. */
1310 static void
1311 cp_check_const_attributes (tree attributes)
1313 if (attributes == error_mark_node)
1314 return;
1316 tree attr;
1317 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1319 tree arg;
1320 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1322 tree expr = TREE_VALUE (arg);
1323 if (EXPR_P (expr))
1324 TREE_VALUE (arg) = maybe_constant_value (expr);
1329 /* Return true if TYPE is an OpenMP mappable type. */
1330 bool
1331 cp_omp_mappable_type (tree type)
1333 /* Mappable type has to be complete. */
1334 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1335 return false;
1336 /* Arrays have mappable type if the elements have mappable type. */
1337 while (TREE_CODE (type) == ARRAY_TYPE)
1338 type = TREE_TYPE (type);
1339 /* A mappable type cannot contain virtual members. */
1340 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1341 return false;
1342 /* All data members must be non-static. */
1343 if (CLASS_TYPE_P (type))
1345 tree field;
1346 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1347 if (VAR_P (field))
1348 return false;
1349 /* All fields must have mappable types. */
1350 else if (TREE_CODE (field) == FIELD_DECL
1351 && !cp_omp_mappable_type (TREE_TYPE (field)))
1352 return false;
1354 return true;
1357 /* Like decl_attributes, but handle C++ complexity. */
1359 void
1360 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1362 if (*decl == NULL_TREE || *decl == void_type_node
1363 || *decl == error_mark_node)
1364 return;
1366 /* Add implicit "omp declare target" attribute if requested. */
1367 if (scope_chain->omp_declare_target_attribute
1368 && ((VAR_P (*decl)
1369 && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
1370 || TREE_CODE (*decl) == FUNCTION_DECL))
1372 if (VAR_P (*decl)
1373 && DECL_CLASS_SCOPE_P (*decl))
1374 error ("%q+D static data member inside of declare target directive",
1375 *decl);
1376 else if (!processing_template_decl
1377 && VAR_P (*decl)
1378 && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1379 error ("%q+D in declare target directive does not have mappable type",
1380 *decl);
1381 else
1382 attributes = tree_cons (get_identifier ("omp declare target"),
1383 NULL_TREE, attributes);
1386 if (processing_template_decl)
1388 if (check_for_bare_parameter_packs (attributes))
1389 return;
1391 save_template_attributes (&attributes, decl);
1394 cp_check_const_attributes (attributes);
1396 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1397 decl = &DECL_TEMPLATE_RESULT (*decl);
1399 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1401 attributes
1402 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1403 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1404 attributes, flags);
1406 else
1407 decl_attributes (decl, attributes, flags);
1409 if (TREE_CODE (*decl) == TYPE_DECL)
1410 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1412 /* Propagate deprecation out to the template. */
1413 if (TREE_DEPRECATED (*decl))
1414 if (tree ti = get_template_info (*decl))
1416 tree tmpl = TI_TEMPLATE (ti);
1417 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1418 : DECL_TEMPLATE_RESULT (tmpl));
1419 if (*decl == pattern)
1420 TREE_DEPRECATED (tmpl) = true;
1424 /* Walks through the namespace- or function-scope anonymous union
1425 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1426 Returns one of the fields for use in the mangled name. */
1428 static tree
1429 build_anon_union_vars (tree type, tree object)
1431 tree main_decl = NULL_TREE;
1432 tree field;
1434 /* Rather than write the code to handle the non-union case,
1435 just give an error. */
1436 if (TREE_CODE (type) != UNION_TYPE)
1438 error ("anonymous struct not inside named type");
1439 return error_mark_node;
1442 for (field = TYPE_FIELDS (type);
1443 field != NULL_TREE;
1444 field = DECL_CHAIN (field))
1446 tree decl;
1447 tree ref;
1449 if (DECL_ARTIFICIAL (field))
1450 continue;
1451 if (TREE_CODE (field) != FIELD_DECL)
1453 permerror (DECL_SOURCE_LOCATION (field),
1454 "%q#D invalid; an anonymous union can only "
1455 "have non-static data members", field);
1456 continue;
1459 if (TREE_PRIVATE (field))
1460 permerror (DECL_SOURCE_LOCATION (field),
1461 "private member %q#D in anonymous union", field);
1462 else if (TREE_PROTECTED (field))
1463 permerror (DECL_SOURCE_LOCATION (field),
1464 "protected member %q#D in anonymous union", field);
1466 if (processing_template_decl)
1467 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1468 DECL_NAME (field), NULL_TREE);
1469 else
1470 ref = build_class_member_access_expr (object, field, NULL_TREE,
1471 false, tf_warning_or_error);
1473 if (DECL_NAME (field))
1475 tree base;
1477 decl = build_decl (input_location,
1478 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1479 DECL_ANON_UNION_VAR_P (decl) = 1;
1480 DECL_ARTIFICIAL (decl) = 1;
1482 base = get_base_address (object);
1483 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1484 TREE_STATIC (decl) = TREE_STATIC (base);
1485 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1487 SET_DECL_VALUE_EXPR (decl, ref);
1488 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1490 decl = pushdecl (decl);
1492 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1493 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1494 else
1495 decl = 0;
1497 if (main_decl == NULL_TREE)
1498 main_decl = decl;
1501 return main_decl;
1504 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1505 anonymous union, then all members must be laid out together. PUBLIC_P
1506 is nonzero if this union is not declared static. */
1508 void
1509 finish_anon_union (tree anon_union_decl)
1511 tree type;
1512 tree main_decl;
1513 bool public_p;
1515 if (anon_union_decl == error_mark_node)
1516 return;
1518 type = TREE_TYPE (anon_union_decl);
1519 public_p = TREE_PUBLIC (anon_union_decl);
1521 /* The VAR_DECL's context is the same as the TYPE's context. */
1522 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1524 if (TYPE_FIELDS (type) == NULL_TREE)
1525 return;
1527 if (public_p)
1529 error ("namespace-scope anonymous aggregates must be static");
1530 return;
1533 main_decl = build_anon_union_vars (type, anon_union_decl);
1534 if (main_decl == error_mark_node)
1535 return;
1536 if (main_decl == NULL_TREE)
1538 warning (0, "anonymous union with no members");
1539 return;
1542 if (!processing_template_decl)
1544 /* Use main_decl to set the mangled name. */
1545 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1546 maybe_commonize_var (anon_union_decl);
1547 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1548 mangle_decl (anon_union_decl);
1549 DECL_NAME (anon_union_decl) = NULL_TREE;
1552 pushdecl (anon_union_decl);
1553 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1556 /* Auxiliary functions to make type signatures for
1557 `operator new' and `operator delete' correspond to
1558 what compiler will be expecting. */
1560 tree
1561 coerce_new_type (tree type)
1563 int e = 0;
1564 tree args = TYPE_ARG_TYPES (type);
1566 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1568 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1570 e = 1;
1571 error ("%<operator new%> must return type %qT", ptr_type_node);
1574 if (args && args != void_list_node)
1576 if (TREE_PURPOSE (args))
1578 /* [basic.stc.dynamic.allocation]
1580 The first parameter shall not have an associated default
1581 argument. */
1582 error ("the first parameter of %<operator new%> cannot "
1583 "have a default argument");
1584 /* Throw away the default argument. */
1585 TREE_PURPOSE (args) = NULL_TREE;
1588 if (!same_type_p (TREE_VALUE (args), size_type_node))
1590 e = 2;
1591 args = TREE_CHAIN (args);
1594 else
1595 e = 2;
1597 if (e == 2)
1598 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1599 "as first parameter", size_type_node);
1601 switch (e)
1603 case 2:
1604 args = tree_cons (NULL_TREE, size_type_node, args);
1605 /* Fall through. */
1606 case 1:
1607 type = build_exception_variant
1608 (build_function_type (ptr_type_node, args),
1609 TYPE_RAISES_EXCEPTIONS (type));
1610 /* Fall through. */
1611 default:;
1613 return type;
1616 tree
1617 coerce_delete_type (tree type)
1619 int e = 0;
1620 tree args = TYPE_ARG_TYPES (type);
1622 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1624 if (!same_type_p (TREE_TYPE (type), void_type_node))
1626 e = 1;
1627 error ("%<operator delete%> must return type %qT", void_type_node);
1630 if (!args || args == void_list_node
1631 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1633 e = 2;
1634 if (args && args != void_list_node)
1635 args = TREE_CHAIN (args);
1636 error ("%<operator delete%> takes type %qT as first parameter",
1637 ptr_type_node);
1639 switch (e)
1641 case 2:
1642 args = tree_cons (NULL_TREE, ptr_type_node, args);
1643 /* Fall through. */
1644 case 1:
1645 type = build_exception_variant
1646 (build_function_type (void_type_node, args),
1647 TYPE_RAISES_EXCEPTIONS (type));
1648 /* Fall through. */
1649 default:;
1652 return type;
1655 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1656 and mark them as needed. */
1658 static void
1659 mark_vtable_entries (tree decl)
1661 tree fnaddr;
1662 unsigned HOST_WIDE_INT idx;
1664 /* It's OK for the vtable to refer to deprecated virtual functions. */
1665 warning_sentinel w(warn_deprecated_decl);
1667 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1668 idx, fnaddr)
1670 tree fn;
1672 STRIP_NOPS (fnaddr);
1674 if (TREE_CODE (fnaddr) != ADDR_EXPR
1675 && TREE_CODE (fnaddr) != FDESC_EXPR)
1676 /* This entry is an offset: a virtual base class offset, a
1677 virtual call offset, an RTTI offset, etc. */
1678 continue;
1680 fn = TREE_OPERAND (fnaddr, 0);
1681 TREE_ADDRESSABLE (fn) = 1;
1682 /* When we don't have vcall offsets, we output thunks whenever
1683 we output the vtables that contain them. With vcall offsets,
1684 we know all the thunks we'll need when we emit a virtual
1685 function, so we emit the thunks there instead. */
1686 if (DECL_THUNK_P (fn))
1687 use_thunk (fn, /*emit_p=*/0);
1688 mark_used (fn);
1692 /* Set DECL up to have the closest approximation of "initialized common"
1693 linkage available. */
1695 void
1696 comdat_linkage (tree decl)
1698 if (flag_weak)
1699 make_decl_one_only (decl, cxx_comdat_group (decl));
1700 else if (TREE_CODE (decl) == FUNCTION_DECL
1701 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1702 /* We can just emit function and compiler-generated variables
1703 statically; having multiple copies is (for the most part) only
1704 a waste of space.
1706 There are two correctness issues, however: the address of a
1707 template instantiation with external linkage should be the
1708 same, independent of what translation unit asks for the
1709 address, and this will not hold when we emit multiple copies of
1710 the function. However, there's little else we can do.
1712 Also, by default, the typeinfo implementation assumes that
1713 there will be only one copy of the string used as the name for
1714 each type. Therefore, if weak symbols are unavailable, the
1715 run-time library should perform a more conservative check; it
1716 should perform a string comparison, rather than an address
1717 comparison. */
1718 TREE_PUBLIC (decl) = 0;
1719 else
1721 /* Static data member template instantiations, however, cannot
1722 have multiple copies. */
1723 if (DECL_INITIAL (decl) == 0
1724 || DECL_INITIAL (decl) == error_mark_node)
1725 DECL_COMMON (decl) = 1;
1726 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1728 DECL_COMMON (decl) = 1;
1729 DECL_INITIAL (decl) = error_mark_node;
1731 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1733 /* We can't do anything useful; leave vars for explicit
1734 instantiation. */
1735 DECL_EXTERNAL (decl) = 1;
1736 DECL_NOT_REALLY_EXTERN (decl) = 0;
1740 if (TREE_PUBLIC (decl))
1741 DECL_COMDAT (decl) = 1;
1744 /* For win32 we also want to put explicit instantiations in
1745 linkonce sections, so that they will be merged with implicit
1746 instantiations; otherwise we get duplicate symbol errors.
1747 For Darwin we do not want explicit instantiations to be
1748 linkonce. */
1750 void
1751 maybe_make_one_only (tree decl)
1753 /* We used to say that this was not necessary on targets that support weak
1754 symbols, because the implicit instantiations will defer to the explicit
1755 one. However, that's not actually the case in SVR4; a strong definition
1756 after a weak one is an error. Also, not making explicit
1757 instantiations one_only means that we can end up with two copies of
1758 some template instantiations. */
1759 if (! flag_weak)
1760 return;
1762 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1763 we can get away with not emitting them if they aren't used. We need
1764 to for variables so that cp_finish_decl will update their linkage,
1765 because their DECL_INITIAL may not have been set properly yet. */
1767 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1768 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1769 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1771 make_decl_one_only (decl, cxx_comdat_group (decl));
1773 if (VAR_P (decl))
1775 varpool_node *node = varpool_node::get_create (decl);
1776 DECL_COMDAT (decl) = 1;
1777 /* Mark it needed so we don't forget to emit it. */
1778 node->forced_by_abi = true;
1779 TREE_USED (decl) = 1;
1784 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1785 This predicate will give the right answer during parsing of the
1786 function, which other tests may not. */
1788 bool
1789 vague_linkage_p (tree decl)
1791 if (!TREE_PUBLIC (decl))
1793 /* maybe_thunk_body clears TREE_PUBLIC on the maybe-in-charge 'tor
1794 variants, check one of the "clones" for the real linkage. */
1795 if ((DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
1796 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl))
1797 && DECL_CHAIN (decl)
1798 && DECL_CLONED_FUNCTION (DECL_CHAIN (decl)))
1799 return vague_linkage_p (DECL_CHAIN (decl));
1801 gcc_checking_assert (!DECL_COMDAT (decl));
1802 return false;
1804 /* Unfortunately, import_export_decl has not always been called
1805 before the function is processed, so we cannot simply check
1806 DECL_COMDAT. */
1807 if (DECL_COMDAT (decl)
1808 || (TREE_CODE (decl) == FUNCTION_DECL
1809 && DECL_DECLARED_INLINE_P (decl))
1810 || (DECL_LANG_SPECIFIC (decl)
1811 && DECL_TEMPLATE_INSTANTIATION (decl))
1812 || (VAR_P (decl) && DECL_INLINE_VAR_P (decl)))
1813 return true;
1814 else if (DECL_FUNCTION_SCOPE_P (decl))
1815 /* A local static in an inline effectively has vague linkage. */
1816 return (TREE_STATIC (decl)
1817 && vague_linkage_p (DECL_CONTEXT (decl)));
1818 else
1819 return false;
1822 /* Determine whether or not we want to specifically import or export CTYPE,
1823 using various heuristics. */
1825 static void
1826 import_export_class (tree ctype)
1828 /* -1 for imported, 1 for exported. */
1829 int import_export = 0;
1831 /* It only makes sense to call this function at EOF. The reason is
1832 that this function looks at whether or not the first non-inline
1833 non-abstract virtual member function has been defined in this
1834 translation unit. But, we can't possibly know that until we've
1835 seen the entire translation unit. */
1836 gcc_assert (at_eof);
1838 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1839 return;
1841 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1842 we will have CLASSTYPE_INTERFACE_ONLY set but not
1843 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1844 heuristic because someone will supply a #pragma implementation
1845 elsewhere, and deducing it here would produce a conflict. */
1846 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1847 return;
1849 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1850 import_export = -1;
1851 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1852 import_export = 1;
1853 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1854 && !flag_implicit_templates)
1855 /* For a template class, without -fimplicit-templates, check the
1856 repository. If the virtual table is assigned to this
1857 translation unit, then export the class; otherwise, import
1858 it. */
1859 import_export = repo_export_class_p (ctype) ? 1 : -1;
1860 else if (TYPE_POLYMORPHIC_P (ctype))
1862 /* The ABI specifies that the virtual table and associated
1863 information are emitted with the key method, if any. */
1864 tree method = CLASSTYPE_KEY_METHOD (ctype);
1865 /* If weak symbol support is not available, then we must be
1866 careful not to emit the vtable when the key function is
1867 inline. An inline function can be defined in multiple
1868 translation units. If we were to emit the vtable in each
1869 translation unit containing a definition, we would get
1870 multiple definition errors at link-time. */
1871 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1872 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1875 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1876 a definition anywhere else. */
1877 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1878 import_export = 0;
1880 /* Allow back ends the chance to overrule the decision. */
1881 if (targetm.cxx.import_export_class)
1882 import_export = targetm.cxx.import_export_class (ctype, import_export);
1884 if (import_export)
1886 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1887 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1891 /* Return true if VAR has already been provided to the back end; in that
1892 case VAR should not be modified further by the front end. */
1893 static bool
1894 var_finalized_p (tree var)
1896 return varpool_node::get_create (var)->definition;
1899 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1900 must be emitted in this translation unit. Mark it as such. */
1902 void
1903 mark_needed (tree decl)
1905 TREE_USED (decl) = 1;
1906 if (TREE_CODE (decl) == FUNCTION_DECL)
1908 /* Extern inline functions don't become needed when referenced.
1909 If we know a method will be emitted in other TU and no new
1910 functions can be marked reachable, just use the external
1911 definition. */
1912 struct cgraph_node *node = cgraph_node::get_create (decl);
1913 node->forced_by_abi = true;
1915 /* #pragma interface and -frepo code can call mark_needed for
1916 maybe-in-charge 'tors; mark the clones as well. */
1917 tree clone;
1918 FOR_EACH_CLONE (clone, decl)
1919 mark_needed (clone);
1921 else if (VAR_P (decl))
1923 varpool_node *node = varpool_node::get_create (decl);
1924 /* C++ frontend use mark_decl_references to force COMDAT variables
1925 to be output that might appear dead otherwise. */
1926 node->forced_by_abi = true;
1930 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1931 returns true if a definition of this entity should be provided in
1932 this object file. Callers use this function to determine whether
1933 or not to let the back end know that a definition of DECL is
1934 available in this translation unit. */
1936 bool
1937 decl_needed_p (tree decl)
1939 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
1940 /* This function should only be called at the end of the translation
1941 unit. We cannot be sure of whether or not something will be
1942 COMDAT until that point. */
1943 gcc_assert (at_eof);
1945 /* All entities with external linkage that are not COMDAT/EXTERN should be
1946 emitted; they may be referred to from other object files. */
1947 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
1948 return true;
1949 /* Functions marked "dllexport" must be emitted so that they are
1950 visible to other DLLs. */
1951 if (flag_keep_inline_dllexport
1952 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
1953 return true;
1955 /* When not optimizing, do not bother to produce definitions for extern
1956 symbols. */
1957 if (DECL_REALLY_EXTERN (decl)
1958 && ((TREE_CODE (decl) != FUNCTION_DECL
1959 && !optimize)
1960 || (TREE_CODE (decl) == FUNCTION_DECL
1961 && !opt_for_fn (decl, optimize)))
1962 && !lookup_attribute ("always_inline", decl))
1963 return false;
1965 /* If this entity was used, let the back end see it; it will decide
1966 whether or not to emit it into the object file. */
1967 if (TREE_USED (decl))
1968 return true;
1970 /* Virtual functions might be needed for devirtualization. */
1971 if (flag_devirtualize
1972 && TREE_CODE (decl) == FUNCTION_DECL
1973 && DECL_VIRTUAL_P (decl))
1974 return true;
1976 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
1977 reference to DECL might cause it to be emitted later. */
1978 return false;
1981 /* If necessary, write out the vtables for the dynamic class CTYPE.
1982 Returns true if any vtables were emitted. */
1984 static bool
1985 maybe_emit_vtables (tree ctype)
1987 tree vtbl;
1988 tree primary_vtbl;
1989 int needed = 0;
1990 varpool_node *current = NULL, *last = NULL;
1992 /* If the vtables for this class have already been emitted there is
1993 nothing more to do. */
1994 primary_vtbl = CLASSTYPE_VTABLES (ctype);
1995 if (var_finalized_p (primary_vtbl))
1996 return false;
1997 /* Ignore dummy vtables made by get_vtable_decl. */
1998 if (TREE_TYPE (primary_vtbl) == void_type_node)
1999 return false;
2001 /* On some targets, we cannot determine the key method until the end
2002 of the translation unit -- which is when this function is
2003 called. */
2004 if (!targetm.cxx.key_method_may_be_inline ())
2005 determine_key_method (ctype);
2007 /* See if any of the vtables are needed. */
2008 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2010 import_export_decl (vtbl);
2011 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2012 needed = 1;
2014 if (!needed)
2016 /* If the references to this class' vtables are optimized away,
2017 still emit the appropriate debugging information. See
2018 dfs_debug_mark. */
2019 if (DECL_COMDAT (primary_vtbl)
2020 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2021 note_debug_info_needed (ctype);
2022 return false;
2025 /* The ABI requires that we emit all of the vtables if we emit any
2026 of them. */
2027 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2029 /* Mark entities references from the virtual table as used. */
2030 mark_vtable_entries (vtbl);
2032 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2034 vec<tree, va_gc> *cleanups = NULL;
2035 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2036 LOOKUP_NORMAL);
2038 /* It had better be all done at compile-time. */
2039 gcc_assert (!expr && !cleanups);
2042 /* Write it out. */
2043 DECL_EXTERNAL (vtbl) = 0;
2044 rest_of_decl_compilation (vtbl, 1, 1);
2046 /* Because we're only doing syntax-checking, we'll never end up
2047 actually marking the variable as written. */
2048 if (flag_syntax_only)
2049 TREE_ASM_WRITTEN (vtbl) = 1;
2050 else if (DECL_ONE_ONLY (vtbl))
2052 current = varpool_node::get_create (vtbl);
2053 if (last)
2054 current->add_to_same_comdat_group (last);
2055 last = current;
2059 /* Since we're writing out the vtable here, also write the debug
2060 info. */
2061 note_debug_info_needed (ctype);
2063 return true;
2066 /* A special return value from type_visibility meaning internal
2067 linkage. */
2069 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2071 /* walk_tree helper function for type_visibility. */
2073 static tree
2074 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2076 int *vis_p = (int *)data;
2077 if (! TYPE_P (*tp))
2079 *walk_subtrees = 0;
2081 else if (OVERLOAD_TYPE_P (*tp)
2082 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2084 *vis_p = VISIBILITY_ANON;
2085 return *tp;
2087 else if (CLASS_TYPE_P (*tp)
2088 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2089 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2090 return NULL;
2093 /* Returns the visibility of TYPE, which is the minimum visibility of its
2094 component types. */
2096 static int
2097 type_visibility (tree type)
2099 int vis = VISIBILITY_DEFAULT;
2100 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2101 return vis;
2104 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2105 specified (or if VISIBILITY is static). If TMPL is true, this
2106 constraint is for a template argument, and takes precedence
2107 over explicitly-specified visibility on the template. */
2109 static void
2110 constrain_visibility (tree decl, int visibility, bool tmpl)
2112 if (visibility == VISIBILITY_ANON)
2114 /* extern "C" declarations aren't affected by the anonymous
2115 namespace. */
2116 if (!DECL_EXTERN_C_P (decl))
2118 TREE_PUBLIC (decl) = 0;
2119 DECL_WEAK (decl) = 0;
2120 DECL_COMMON (decl) = 0;
2121 DECL_COMDAT (decl) = false;
2122 if (VAR_OR_FUNCTION_DECL_P (decl))
2124 struct symtab_node *snode = symtab_node::get (decl);
2126 if (snode)
2127 snode->set_comdat_group (NULL);
2129 DECL_INTERFACE_KNOWN (decl) = 1;
2130 if (DECL_LANG_SPECIFIC (decl))
2131 DECL_NOT_REALLY_EXTERN (decl) = 1;
2134 else if (visibility > DECL_VISIBILITY (decl)
2135 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2137 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2138 /* This visibility was not specified. */
2139 DECL_VISIBILITY_SPECIFIED (decl) = false;
2143 /* Constrain the visibility of DECL based on the visibility of its template
2144 arguments. */
2146 static void
2147 constrain_visibility_for_template (tree decl, tree targs)
2149 /* If this is a template instantiation, check the innermost
2150 template args for visibility constraints. The outer template
2151 args are covered by the class check. */
2152 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2153 int i;
2154 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2156 int vis = 0;
2158 tree arg = TREE_VEC_ELT (args, i-1);
2159 if (TYPE_P (arg))
2160 vis = type_visibility (arg);
2161 else
2163 if (REFERENCE_REF_P (arg))
2164 arg = TREE_OPERAND (arg, 0);
2165 if (TREE_TYPE (arg))
2166 STRIP_NOPS (arg);
2167 if (TREE_CODE (arg) == ADDR_EXPR)
2168 arg = TREE_OPERAND (arg, 0);
2169 if (VAR_OR_FUNCTION_DECL_P (arg))
2171 if (! TREE_PUBLIC (arg))
2172 vis = VISIBILITY_ANON;
2173 else
2174 vis = DECL_VISIBILITY (arg);
2177 if (vis)
2178 constrain_visibility (decl, vis, true);
2182 /* Like c_determine_visibility, but with additional C++-specific
2183 behavior.
2185 Function-scope entities can rely on the function's visibility because
2186 it is set in start_preparsed_function.
2188 Class-scope entities cannot rely on the class's visibility until the end
2189 of the enclosing class definition.
2191 Note that because namespaces have multiple independent definitions,
2192 namespace visibility is handled elsewhere using the #pragma visibility
2193 machinery rather than by decorating the namespace declaration.
2195 The goal is for constraints from the type to give a diagnostic, and
2196 other constraints to be applied silently. */
2198 void
2199 determine_visibility (tree decl)
2201 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2203 /* Only relevant for names with external linkage. */
2204 if (!TREE_PUBLIC (decl))
2205 return;
2207 /* Cloned constructors and destructors get the same visibility as
2208 the underlying function. That should be set up in
2209 maybe_clone_body. */
2210 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2212 bool orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2213 enum symbol_visibility orig_visibility = DECL_VISIBILITY (decl);
2215 /* The decl may be a template instantiation, which could influence
2216 visibilty. */
2217 tree template_decl = NULL_TREE;
2218 if (TREE_CODE (decl) == TYPE_DECL)
2220 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2222 if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
2223 template_decl = decl;
2225 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2226 template_decl = decl;
2228 else if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
2229 template_decl = decl;
2231 /* If DECL is a member of a class, visibility specifiers on the
2232 class can influence the visibility of the DECL. */
2233 tree class_type = NULL_TREE;
2234 if (DECL_CLASS_SCOPE_P (decl))
2235 class_type = DECL_CONTEXT (decl);
2236 else
2238 /* Not a class member. */
2240 /* Virtual tables have DECL_CONTEXT set to their associated class,
2241 so they are automatically handled above. */
2242 gcc_assert (!VAR_P (decl)
2243 || !DECL_VTABLE_OR_VTT_P (decl));
2245 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2247 /* Local statics and classes get the visibility of their
2248 containing function by default, except that
2249 -fvisibility-inlines-hidden doesn't affect them. */
2250 tree fn = DECL_CONTEXT (decl);
2251 if (DECL_VISIBILITY_SPECIFIED (fn))
2253 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2254 DECL_VISIBILITY_SPECIFIED (decl) =
2255 DECL_VISIBILITY_SPECIFIED (fn);
2257 else
2259 if (DECL_CLASS_SCOPE_P (fn))
2260 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2261 else if (determine_hidden_inline (fn))
2263 DECL_VISIBILITY (decl) = default_visibility;
2264 DECL_VISIBILITY_SPECIFIED (decl) =
2265 visibility_options.inpragma;
2267 else
2269 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2270 DECL_VISIBILITY_SPECIFIED (decl) =
2271 DECL_VISIBILITY_SPECIFIED (fn);
2275 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2276 but have no TEMPLATE_INFO. Their containing template
2277 function does, and the local class could be constrained
2278 by that. */
2279 if (template_decl)
2280 template_decl = fn;
2282 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2283 && flag_visibility_ms_compat)
2285 /* Under -fvisibility-ms-compat, types are visible by default,
2286 even though their contents aren't. */
2287 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2288 int underlying_vis = type_visibility (underlying_type);
2289 if (underlying_vis == VISIBILITY_ANON
2290 || (CLASS_TYPE_P (underlying_type)
2291 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2292 constrain_visibility (decl, underlying_vis, false);
2293 else
2294 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2296 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2298 /* tinfo visibility is based on the type it's for. */
2299 constrain_visibility
2300 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2302 /* Give the target a chance to override the visibility associated
2303 with DECL. */
2304 if (TREE_PUBLIC (decl)
2305 && !DECL_REALLY_EXTERN (decl)
2306 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2307 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2308 targetm.cxx.determine_class_data_visibility (decl);
2310 else if (template_decl)
2311 /* Template instantiations and specializations get visibility based
2312 on their template unless they override it with an attribute. */;
2313 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2315 if (determine_hidden_inline (decl))
2316 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2317 else
2319 /* Set default visibility to whatever the user supplied with
2320 #pragma GCC visibility or a namespace visibility attribute. */
2321 DECL_VISIBILITY (decl) = default_visibility;
2322 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2327 if (template_decl)
2329 /* If the specialization doesn't specify visibility, use the
2330 visibility from the template. */
2331 tree tinfo = get_template_info (template_decl);
2332 tree args = TI_ARGS (tinfo);
2333 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2334 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2335 : DECL_ATTRIBUTES (decl));
2337 if (args != error_mark_node)
2339 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2341 if (!DECL_VISIBILITY_SPECIFIED (decl))
2343 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2344 && determine_hidden_inline (decl))
2345 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2346 else
2348 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2349 DECL_VISIBILITY_SPECIFIED (decl)
2350 = DECL_VISIBILITY_SPECIFIED (pattern);
2354 if (args
2355 /* Template argument visibility outweighs #pragma or namespace
2356 visibility, but not an explicit attribute. */
2357 && !lookup_attribute ("visibility", attribs))
2359 int depth = TMPL_ARGS_DEPTH (args);
2360 if (DECL_VISIBILITY_SPECIFIED (decl))
2362 /* A class template member with explicit visibility
2363 overrides the class visibility, so we need to apply
2364 all the levels of template args directly. */
2365 int i;
2366 for (i = 1; i <= depth; ++i)
2368 tree lev = TMPL_ARGS_LEVEL (args, i);
2369 constrain_visibility_for_template (decl, lev);
2372 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2373 /* Limit visibility based on its template arguments. */
2374 constrain_visibility_for_template (decl, args);
2379 if (class_type)
2380 determine_visibility_from_class (decl, class_type);
2382 if (decl_anon_ns_mem_p (decl))
2383 /* Names in an anonymous namespace get internal linkage.
2384 This might change once we implement export. */
2385 constrain_visibility (decl, VISIBILITY_ANON, false);
2386 else if (TREE_CODE (decl) != TYPE_DECL)
2388 /* Propagate anonymity from type to decl. */
2389 int tvis = type_visibility (TREE_TYPE (decl));
2390 if (tvis == VISIBILITY_ANON
2391 || ! DECL_VISIBILITY_SPECIFIED (decl))
2392 constrain_visibility (decl, tvis, false);
2394 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2395 /* DR 757: A type without linkage shall not be used as the type of a
2396 variable or function with linkage, unless
2397 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2398 o the variable or function is not used (3.2 [basic.def.odr]) or is
2399 defined in the same translation unit.
2401 Since non-extern "C" decls need to be defined in the same
2402 translation unit, we can make the type internal. */
2403 constrain_visibility (decl, VISIBILITY_ANON, false);
2405 /* If visibility changed and DECL already has DECL_RTL, ensure
2406 symbol flags are updated. */
2407 if ((DECL_VISIBILITY (decl) != orig_visibility
2408 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2409 && ((VAR_P (decl) && TREE_STATIC (decl))
2410 || TREE_CODE (decl) == FUNCTION_DECL)
2411 && DECL_RTL_SET_P (decl))
2412 make_decl_rtl (decl);
2415 /* By default, static data members and function members receive
2416 the visibility of their containing class. */
2418 static void
2419 determine_visibility_from_class (tree decl, tree class_type)
2421 if (DECL_VISIBILITY_SPECIFIED (decl))
2422 return;
2424 if (determine_hidden_inline (decl))
2425 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2426 else
2428 /* Default to the class visibility. */
2429 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2430 DECL_VISIBILITY_SPECIFIED (decl)
2431 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2434 /* Give the target a chance to override the visibility associated
2435 with DECL. */
2436 if (VAR_P (decl)
2437 && (DECL_TINFO_P (decl)
2438 || (DECL_VTABLE_OR_VTT_P (decl)
2439 /* Construction virtual tables are not exported because
2440 they cannot be referred to from other object files;
2441 their name is not standardized by the ABI. */
2442 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2443 && TREE_PUBLIC (decl)
2444 && !DECL_REALLY_EXTERN (decl)
2445 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2446 targetm.cxx.determine_class_data_visibility (decl);
2449 /* Returns true iff DECL is an inline that should get hidden visibility
2450 because of -fvisibility-inlines-hidden. */
2452 static bool
2453 determine_hidden_inline (tree decl)
2455 return (visibility_options.inlines_hidden
2456 /* Don't do this for inline templates; specializations might not be
2457 inline, and we don't want them to inherit the hidden
2458 visibility. We'll set it here for all inline instantiations. */
2459 && !processing_template_decl
2460 && TREE_CODE (decl) == FUNCTION_DECL
2461 && DECL_DECLARED_INLINE_P (decl)
2462 && (! DECL_LANG_SPECIFIC (decl)
2463 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2466 /* Constrain the visibility of a class TYPE based on the visibility of its
2467 field types. Warn if any fields require lesser visibility. */
2469 void
2470 constrain_class_visibility (tree type)
2472 tree binfo;
2473 tree t;
2474 int i;
2476 int vis = type_visibility (type);
2478 if (vis == VISIBILITY_ANON
2479 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2480 return;
2482 /* Don't warn about visibility if the class has explicit visibility. */
2483 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2484 vis = VISIBILITY_INTERNAL;
2486 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2487 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2489 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2490 int subvis = type_visibility (ftype);
2492 if (subvis == VISIBILITY_ANON)
2494 if (!in_main_input_context())
2496 tree nlt = no_linkage_check (ftype, /*relaxed_p=*/false);
2497 if (nlt)
2499 if (same_type_p (TREE_TYPE (t), nlt))
2500 warning (OPT_Wsubobject_linkage, "\
2501 %qT has a field %qD whose type has no linkage",
2502 type, t);
2503 else
2504 warning (OPT_Wsubobject_linkage, "\
2505 %qT has a field %qD whose type depends on the type %qT which has no linkage",
2506 type, t, nlt);
2508 else
2509 warning (OPT_Wsubobject_linkage, "\
2510 %qT has a field %qD whose type uses the anonymous namespace",
2511 type, t);
2514 else if (MAYBE_CLASS_TYPE_P (ftype)
2515 && vis < VISIBILITY_HIDDEN
2516 && subvis >= VISIBILITY_HIDDEN)
2517 warning (OPT_Wattributes, "\
2518 %qT declared with greater visibility than the type of its field %qD",
2519 type, t);
2522 binfo = TYPE_BINFO (type);
2523 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2525 int subvis = type_visibility (TREE_TYPE (t));
2527 if (subvis == VISIBILITY_ANON)
2529 if (!in_main_input_context())
2531 tree nlt = no_linkage_check (TREE_TYPE (t), /*relaxed_p=*/false);
2532 if (nlt)
2534 if (same_type_p (TREE_TYPE (t), nlt))
2535 warning (OPT_Wsubobject_linkage, "\
2536 %qT has a base %qT whose type has no linkage",
2537 type, TREE_TYPE (t));
2538 else
2539 warning (OPT_Wsubobject_linkage, "\
2540 %qT has a base %qT whose type depends on the type %qT which has no linkage",
2541 type, TREE_TYPE (t), nlt);
2543 else
2544 warning (OPT_Wsubobject_linkage, "\
2545 %qT has a base %qT whose type uses the anonymous namespace",
2546 type, TREE_TYPE (t));
2549 else if (vis < VISIBILITY_HIDDEN
2550 && subvis >= VISIBILITY_HIDDEN)
2551 warning (OPT_Wattributes, "\
2552 %qT declared with greater visibility than its base %qT",
2553 type, TREE_TYPE (t));
2557 /* Functions for adjusting the visibility of a tagged type and its nested
2558 types and declarations when it gets a name for linkage purposes from a
2559 typedef. */
2561 static void bt_reset_linkage_1 (binding_entry, void *);
2562 static void bt_reset_linkage_2 (binding_entry, void *);
2564 /* First reset the visibility of all the types. */
2566 static void
2567 reset_type_linkage_1 (tree type)
2569 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2570 if (CLASS_TYPE_P (type))
2571 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2572 bt_reset_linkage_1, NULL);
2574 static void
2575 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2577 reset_type_linkage_1 (b->type);
2580 /* Then reset the visibility of any static data members or member
2581 functions that use those types. */
2583 static void
2584 reset_decl_linkage (tree decl)
2586 if (TREE_PUBLIC (decl))
2587 return;
2588 if (DECL_CLONED_FUNCTION_P (decl))
2589 return;
2590 TREE_PUBLIC (decl) = true;
2591 DECL_INTERFACE_KNOWN (decl) = false;
2592 determine_visibility (decl);
2593 tentative_decl_linkage (decl);
2595 static void
2596 reset_type_linkage_2 (tree type)
2598 if (CLASS_TYPE_P (type))
2600 if (tree vt = CLASSTYPE_VTABLES (type))
2602 tree name = mangle_vtbl_for_type (type);
2603 DECL_NAME (vt) = name;
2604 SET_DECL_ASSEMBLER_NAME (vt, name);
2605 reset_decl_linkage (vt);
2607 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2609 tree name = mangle_typeinfo_for_type (type);
2610 DECL_NAME (ti) = name;
2611 SET_DECL_ASSEMBLER_NAME (ti, name);
2612 TREE_TYPE (name) = type;
2613 reset_decl_linkage (ti);
2615 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2617 tree mem = STRIP_TEMPLATE (m);
2618 if (VAR_P (mem))
2619 reset_decl_linkage (mem);
2621 for (tree m = TYPE_METHODS (type); m; m = DECL_CHAIN (m))
2623 tree mem = STRIP_TEMPLATE (m);
2624 reset_decl_linkage (mem);
2626 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2627 bt_reset_linkage_2, NULL);
2630 static void
2631 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2633 reset_type_linkage_2 (b->type);
2635 void
2636 reset_type_linkage (tree type)
2638 reset_type_linkage_1 (type);
2639 reset_type_linkage_2 (type);
2642 /* Set up our initial idea of what the linkage of DECL should be. */
2644 void
2645 tentative_decl_linkage (tree decl)
2647 if (DECL_INTERFACE_KNOWN (decl))
2648 /* We've already made a decision as to how this function will
2649 be handled. */;
2650 else if (vague_linkage_p (decl))
2652 if (TREE_CODE (decl) == FUNCTION_DECL
2653 && decl_defined_p (decl))
2655 DECL_EXTERNAL (decl) = 1;
2656 DECL_NOT_REALLY_EXTERN (decl) = 1;
2657 note_vague_linkage_fn (decl);
2658 /* A non-template inline function with external linkage will
2659 always be COMDAT. As we must eventually determine the
2660 linkage of all functions, and as that causes writes to
2661 the data mapped in from the PCH file, it's advantageous
2662 to mark the functions at this point. */
2663 if (DECL_DECLARED_INLINE_P (decl)
2664 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2665 || DECL_DEFAULTED_FN (decl)))
2667 /* This function must have external linkage, as
2668 otherwise DECL_INTERFACE_KNOWN would have been
2669 set. */
2670 gcc_assert (TREE_PUBLIC (decl));
2671 comdat_linkage (decl);
2672 DECL_INTERFACE_KNOWN (decl) = 1;
2675 else if (VAR_P (decl))
2676 maybe_commonize_var (decl);
2680 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2681 for DECL has not already been determined, do so now by setting
2682 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2683 function is called entities with vague linkage whose definitions
2684 are available must have TREE_PUBLIC set.
2686 If this function decides to place DECL in COMDAT, it will set
2687 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2688 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2689 callers defer that decision until it is clear that DECL is actually
2690 required. */
2692 void
2693 import_export_decl (tree decl)
2695 int emit_p;
2696 bool comdat_p;
2697 bool import_p;
2698 tree class_type = NULL_TREE;
2700 if (DECL_INTERFACE_KNOWN (decl))
2701 return;
2703 /* We cannot determine what linkage to give to an entity with vague
2704 linkage until the end of the file. For example, a virtual table
2705 for a class will be defined if and only if the key method is
2706 defined in this translation unit. As a further example, consider
2707 that when compiling a translation unit that uses PCH file with
2708 "-frepo" it would be incorrect to make decisions about what
2709 entities to emit when building the PCH; those decisions must be
2710 delayed until the repository information has been processed. */
2711 gcc_assert (at_eof);
2712 /* Object file linkage for explicit instantiations is handled in
2713 mark_decl_instantiated. For static variables in functions with
2714 vague linkage, maybe_commonize_var is used.
2716 Therefore, the only declarations that should be provided to this
2717 function are those with external linkage that are:
2719 * implicit instantiations of function templates
2721 * inline function
2723 * implicit instantiations of static data members of class
2724 templates
2726 * virtual tables
2728 * typeinfo objects
2730 Furthermore, all entities that reach this point must have a
2731 definition available in this translation unit.
2733 The following assertions check these conditions. */
2734 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2735 /* Any code that creates entities with TREE_PUBLIC cleared should
2736 also set DECL_INTERFACE_KNOWN. */
2737 gcc_assert (TREE_PUBLIC (decl));
2738 if (TREE_CODE (decl) == FUNCTION_DECL)
2739 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2740 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2741 || DECL_DECLARED_INLINE_P (decl));
2742 else
2743 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2744 || DECL_VTABLE_OR_VTT_P (decl)
2745 || DECL_TINFO_P (decl));
2746 /* Check that a definition of DECL is available in this translation
2747 unit. */
2748 gcc_assert (!DECL_REALLY_EXTERN (decl));
2750 /* Assume that DECL will not have COMDAT linkage. */
2751 comdat_p = false;
2752 /* Assume that DECL will not be imported into this translation
2753 unit. */
2754 import_p = false;
2756 /* See if the repository tells us whether or not to emit DECL in
2757 this translation unit. */
2758 emit_p = repo_emit_p (decl);
2759 if (emit_p == 0)
2760 import_p = true;
2761 else if (emit_p == 1)
2763 /* The repository indicates that this entity should be defined
2764 here. Make sure the back end honors that request. */
2765 mark_needed (decl);
2766 /* Output the definition as an ordinary strong definition. */
2767 DECL_EXTERNAL (decl) = 0;
2768 DECL_INTERFACE_KNOWN (decl) = 1;
2769 return;
2772 if (import_p)
2773 /* We have already decided what to do with this DECL; there is no
2774 need to check anything further. */
2776 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2778 class_type = DECL_CONTEXT (decl);
2779 import_export_class (class_type);
2780 if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2781 && CLASSTYPE_INTERFACE_ONLY (class_type))
2782 import_p = true;
2783 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2784 && !CLASSTYPE_USE_TEMPLATE (class_type)
2785 && CLASSTYPE_KEY_METHOD (class_type)
2786 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2787 /* The ABI requires that all virtual tables be emitted with
2788 COMDAT linkage. However, on systems where COMDAT symbols
2789 don't show up in the table of contents for a static
2790 archive, or on systems without weak symbols (where we
2791 approximate COMDAT linkage by using internal linkage), the
2792 linker will report errors about undefined symbols because
2793 it will not see the virtual table definition. Therefore,
2794 in the case that we know that the virtual table will be
2795 emitted in only one translation unit, we make the virtual
2796 table an ordinary definition with external linkage. */
2797 DECL_EXTERNAL (decl) = 0;
2798 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2800 /* CLASS_TYPE is being exported from this translation unit,
2801 so DECL should be defined here. */
2802 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2803 /* If a class is declared in a header with the "extern
2804 template" extension, then it will not be instantiated,
2805 even in translation units that would normally require
2806 it. Often such classes are explicitly instantiated in
2807 one translation unit. Therefore, the explicit
2808 instantiation must be made visible to other translation
2809 units. */
2810 DECL_EXTERNAL (decl) = 0;
2811 else
2813 /* The generic C++ ABI says that class data is always
2814 COMDAT, even if there is a key function. Some
2815 variants (e.g., the ARM EABI) says that class data
2816 only has COMDAT linkage if the class data might be
2817 emitted in more than one translation unit. When the
2818 key method can be inline and is inline, we still have
2819 to arrange for comdat even though
2820 class_data_always_comdat is false. */
2821 if (!CLASSTYPE_KEY_METHOD (class_type)
2822 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2823 || targetm.cxx.class_data_always_comdat ())
2825 /* The ABI requires COMDAT linkage. Normally, we
2826 only emit COMDAT things when they are needed;
2827 make sure that we realize that this entity is
2828 indeed needed. */
2829 comdat_p = true;
2830 mark_needed (decl);
2834 else if (!flag_implicit_templates
2835 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2836 import_p = true;
2837 else
2838 comdat_p = true;
2840 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2842 tree type = TREE_TYPE (DECL_NAME (decl));
2843 if (CLASS_TYPE_P (type))
2845 class_type = type;
2846 import_export_class (type);
2847 if (CLASSTYPE_INTERFACE_KNOWN (type)
2848 && TYPE_POLYMORPHIC_P (type)
2849 && CLASSTYPE_INTERFACE_ONLY (type)
2850 /* If -fno-rtti was specified, then we cannot be sure
2851 that RTTI information will be emitted with the
2852 virtual table of the class, so we must emit it
2853 wherever it is used. */
2854 && flag_rtti)
2855 import_p = true;
2856 else
2858 if (CLASSTYPE_INTERFACE_KNOWN (type)
2859 && !CLASSTYPE_INTERFACE_ONLY (type))
2861 comdat_p = (targetm.cxx.class_data_always_comdat ()
2862 || (CLASSTYPE_KEY_METHOD (type)
2863 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2864 mark_needed (decl);
2865 if (!flag_weak)
2867 comdat_p = false;
2868 DECL_EXTERNAL (decl) = 0;
2871 else
2872 comdat_p = true;
2875 else
2876 comdat_p = true;
2878 else if (DECL_TEMPLOID_INSTANTIATION (decl))
2880 /* DECL is an implicit instantiation of a function or static
2881 data member. */
2882 if ((flag_implicit_templates
2883 && !flag_use_repository)
2884 || (flag_implicit_inline_templates
2885 && TREE_CODE (decl) == FUNCTION_DECL
2886 && DECL_DECLARED_INLINE_P (decl)))
2887 comdat_p = true;
2888 else
2889 /* If we are not implicitly generating templates, then mark
2890 this entity as undefined in this translation unit. */
2891 import_p = true;
2893 else if (DECL_FUNCTION_MEMBER_P (decl))
2895 if (!DECL_DECLARED_INLINE_P (decl))
2897 tree ctype = DECL_CONTEXT (decl);
2898 import_export_class (ctype);
2899 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2901 DECL_NOT_REALLY_EXTERN (decl)
2902 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2903 || (DECL_DECLARED_INLINE_P (decl)
2904 && ! flag_implement_inlines
2905 && !DECL_VINDEX (decl)));
2907 if (!DECL_NOT_REALLY_EXTERN (decl))
2908 DECL_EXTERNAL (decl) = 1;
2910 /* Always make artificials weak. */
2911 if (DECL_ARTIFICIAL (decl) && flag_weak)
2912 comdat_p = true;
2913 else
2914 maybe_make_one_only (decl);
2917 else
2918 comdat_p = true;
2920 else
2921 comdat_p = true;
2923 if (import_p)
2925 /* If we are importing DECL into this translation unit, mark is
2926 an undefined here. */
2927 DECL_EXTERNAL (decl) = 1;
2928 DECL_NOT_REALLY_EXTERN (decl) = 0;
2930 else if (comdat_p)
2932 /* If we decided to put DECL in COMDAT, mark it accordingly at
2933 this point. */
2934 comdat_linkage (decl);
2937 DECL_INTERFACE_KNOWN (decl) = 1;
2940 /* Return an expression that performs the destruction of DECL, which
2941 must be a VAR_DECL whose type has a non-trivial destructor, or is
2942 an array whose (innermost) elements have a non-trivial destructor. */
2944 tree
2945 build_cleanup (tree decl)
2947 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
2948 gcc_assert (clean != NULL_TREE);
2949 return clean;
2952 /* Returns the initialization guard variable for the variable DECL,
2953 which has static storage duration. */
2955 tree
2956 get_guard (tree decl)
2958 tree sname;
2959 tree guard;
2961 sname = mangle_guard_variable (decl);
2962 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2963 if (! guard)
2965 tree guard_type;
2967 /* We use a type that is big enough to contain a mutex as well
2968 as an integer counter. */
2969 guard_type = targetm.cxx.guard_type ();
2970 guard = build_decl (DECL_SOURCE_LOCATION (decl),
2971 VAR_DECL, sname, guard_type);
2973 /* The guard should have the same linkage as what it guards. */
2974 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
2975 TREE_STATIC (guard) = TREE_STATIC (decl);
2976 DECL_COMMON (guard) = DECL_COMMON (decl);
2977 DECL_COMDAT (guard) = DECL_COMDAT (decl);
2978 CP_DECL_THREAD_LOCAL_P (guard) = CP_DECL_THREAD_LOCAL_P (decl);
2979 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
2980 if (DECL_ONE_ONLY (decl))
2981 make_decl_one_only (guard, cxx_comdat_group (guard));
2982 if (TREE_PUBLIC (decl))
2983 DECL_WEAK (guard) = DECL_WEAK (decl);
2984 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
2985 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
2987 DECL_ARTIFICIAL (guard) = 1;
2988 DECL_IGNORED_P (guard) = 1;
2989 TREE_USED (guard) = 1;
2990 pushdecl_top_level_and_finish (guard, NULL_TREE);
2992 return guard;
2995 /* Return an atomic load of src with the appropriate memory model. */
2997 static tree
2998 build_atomic_load_byte (tree src, HOST_WIDE_INT model)
3000 tree ptr_type = build_pointer_type (char_type_node);
3001 tree mem_model = build_int_cst (integer_type_node, model);
3002 tree t, addr, val;
3003 unsigned int size;
3004 int fncode;
3006 size = tree_to_uhwi (TYPE_SIZE_UNIT (char_type_node));
3008 fncode = BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1;
3009 t = builtin_decl_implicit ((enum built_in_function) fncode);
3011 addr = build1 (ADDR_EXPR, ptr_type, src);
3012 val = build_call_expr (t, 2, addr, mem_model);
3013 return val;
3016 /* Return those bits of the GUARD variable that should be set when the
3017 guarded entity is actually initialized. */
3019 static tree
3020 get_guard_bits (tree guard)
3022 if (!targetm.cxx.guard_mask_bit ())
3024 /* We only set the first byte of the guard, in order to leave room
3025 for a mutex in the high-order bits. */
3026 guard = build1 (ADDR_EXPR,
3027 build_pointer_type (TREE_TYPE (guard)),
3028 guard);
3029 guard = build1 (NOP_EXPR,
3030 build_pointer_type (char_type_node),
3031 guard);
3032 guard = build1 (INDIRECT_REF, char_type_node, guard);
3035 return guard;
3038 /* Return an expression which determines whether or not the GUARD
3039 variable has already been initialized. */
3041 tree
3042 get_guard_cond (tree guard, bool thread_safe)
3044 tree guard_value;
3046 if (!thread_safe)
3047 guard = get_guard_bits (guard);
3048 else
3049 guard = build_atomic_load_byte (guard, MEMMODEL_ACQUIRE);
3051 /* Mask off all but the low bit. */
3052 if (targetm.cxx.guard_mask_bit ())
3054 guard_value = integer_one_node;
3055 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3056 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3057 guard = cp_build_binary_op (input_location,
3058 BIT_AND_EXPR, guard, guard_value,
3059 tf_warning_or_error);
3062 guard_value = integer_zero_node;
3063 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3064 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3065 return cp_build_binary_op (input_location,
3066 EQ_EXPR, guard, guard_value,
3067 tf_warning_or_error);
3070 /* Return an expression which sets the GUARD variable, indicating that
3071 the variable being guarded has been initialized. */
3073 tree
3074 set_guard (tree guard)
3076 tree guard_init;
3078 /* Set the GUARD to one. */
3079 guard = get_guard_bits (guard);
3080 guard_init = integer_one_node;
3081 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3082 guard_init = fold_convert (TREE_TYPE (guard), guard_init);
3083 return cp_build_modify_expr (input_location, guard, NOP_EXPR, guard_init,
3084 tf_warning_or_error);
3087 /* Returns true iff we can tell that VAR does not have a dynamic
3088 initializer. */
3090 static bool
3091 var_defined_without_dynamic_init (tree var)
3093 /* If it's defined in another TU, we can't tell. */
3094 if (DECL_EXTERNAL (var))
3095 return false;
3096 /* If it has a non-trivial destructor, registering the destructor
3097 counts as dynamic initialization. */
3098 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3099 return false;
3100 /* If it's in this TU, its initializer has been processed, unless
3101 it's a case of self-initialization, then DECL_INITIALIZED_P is
3102 false while the initializer is handled by finish_id_expression. */
3103 if (!DECL_INITIALIZED_P (var))
3104 return false;
3105 /* If it has no initializer or a constant one, it's not dynamic. */
3106 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3107 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3110 /* Returns true iff VAR is a variable that needs uses to be
3111 wrapped for possible dynamic initialization. */
3113 static bool
3114 var_needs_tls_wrapper (tree var)
3116 return (!error_operand_p (var)
3117 && CP_DECL_THREAD_LOCAL_P (var)
3118 && !DECL_GNU_TLS_P (var)
3119 && !DECL_FUNCTION_SCOPE_P (var)
3120 && !var_defined_without_dynamic_init (var));
3123 /* Get the FUNCTION_DECL for the shared TLS init function for this
3124 translation unit. */
3126 static tree
3127 get_local_tls_init_fn (void)
3129 tree sname = get_identifier ("__tls_init");
3130 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3131 if (!fn)
3133 fn = build_lang_decl (FUNCTION_DECL, sname,
3134 build_function_type (void_type_node,
3135 void_list_node));
3136 SET_DECL_LANGUAGE (fn, lang_c);
3137 TREE_PUBLIC (fn) = false;
3138 DECL_ARTIFICIAL (fn) = true;
3139 mark_used (fn);
3140 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3142 return fn;
3145 /* Get a FUNCTION_DECL for the init function for the thread_local
3146 variable VAR. The init function will be an alias to the function
3147 that initializes all the non-local TLS variables in the translation
3148 unit. The init function is only used by the wrapper function. */
3150 static tree
3151 get_tls_init_fn (tree var)
3153 /* Only C++11 TLS vars need this init fn. */
3154 if (!var_needs_tls_wrapper (var))
3155 return NULL_TREE;
3157 /* If -fno-extern-tls-init, assume that we don't need to call
3158 a tls init function for a variable defined in another TU. */
3159 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3160 return NULL_TREE;
3162 #ifdef ASM_OUTPUT_DEF
3163 /* If the variable is internal, or if we can't generate aliases,
3164 call the local init function directly. */
3165 if (!TREE_PUBLIC (var))
3166 #endif
3167 return get_local_tls_init_fn ();
3169 tree sname = mangle_tls_init_fn (var);
3170 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3171 if (!fn)
3173 fn = build_lang_decl (FUNCTION_DECL, sname,
3174 build_function_type (void_type_node,
3175 void_list_node));
3176 SET_DECL_LANGUAGE (fn, lang_c);
3177 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3178 DECL_ARTIFICIAL (fn) = true;
3179 DECL_COMDAT (fn) = DECL_COMDAT (var);
3180 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3181 if (DECL_ONE_ONLY (var))
3182 make_decl_one_only (fn, cxx_comdat_group (fn));
3183 if (TREE_PUBLIC (var))
3185 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3186 /* If the variable is defined somewhere else and might have static
3187 initialization, make the init function a weak reference. */
3188 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3189 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3190 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3191 && DECL_EXTERNAL (var))
3192 declare_weak (fn);
3193 else
3194 DECL_WEAK (fn) = DECL_WEAK (var);
3196 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3197 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3198 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3199 DECL_IGNORED_P (fn) = 1;
3200 mark_used (fn);
3202 DECL_BEFRIENDING_CLASSES (fn) = var;
3204 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3206 return fn;
3209 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3210 variable VAR. The wrapper function calls the init function (if any) for
3211 VAR and then returns a reference to VAR. The wrapper function is used
3212 in place of VAR everywhere VAR is mentioned. */
3214 tree
3215 get_tls_wrapper_fn (tree var)
3217 /* Only C++11 TLS vars need this wrapper fn. */
3218 if (!var_needs_tls_wrapper (var))
3219 return NULL_TREE;
3221 tree sname = mangle_tls_wrapper_fn (var);
3222 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3223 if (!fn)
3225 /* A named rvalue reference is an lvalue, so the wrapper should
3226 always return an lvalue reference. */
3227 tree type = non_reference (TREE_TYPE (var));
3228 type = build_reference_type (type);
3229 tree fntype = build_function_type (type, void_list_node);
3230 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3231 SET_DECL_LANGUAGE (fn, lang_c);
3232 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3233 DECL_ARTIFICIAL (fn) = true;
3234 DECL_IGNORED_P (fn) = 1;
3235 /* The wrapper is inline and emitted everywhere var is used. */
3236 DECL_DECLARED_INLINE_P (fn) = true;
3237 if (TREE_PUBLIC (var))
3239 comdat_linkage (fn);
3240 #ifdef HAVE_GAS_HIDDEN
3241 /* Make the wrapper bind locally; there's no reason to share
3242 the wrapper between multiple shared objects. */
3243 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3244 DECL_VISIBILITY_SPECIFIED (fn) = true;
3245 #endif
3247 if (!TREE_PUBLIC (fn))
3248 DECL_INTERFACE_KNOWN (fn) = true;
3249 mark_used (fn);
3250 note_vague_linkage_fn (fn);
3252 #if 0
3253 /* We want CSE to commonize calls to the wrapper, but marking it as
3254 pure is unsafe since it has side-effects. I guess we need a new
3255 ECF flag even weaker than ECF_PURE. FIXME! */
3256 DECL_PURE_P (fn) = true;
3257 #endif
3259 DECL_BEFRIENDING_CLASSES (fn) = var;
3261 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3263 return fn;
3266 /* At EOF, generate the definition for the TLS wrapper function FN:
3268 T& var_wrapper() {
3269 if (init_fn) init_fn();
3270 return var;
3271 } */
3273 static void
3274 generate_tls_wrapper (tree fn)
3276 tree var = DECL_BEFRIENDING_CLASSES (fn);
3278 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3279 tree body = begin_function_body ();
3280 /* Only call the init fn if there might be one. */
3281 if (tree init_fn = get_tls_init_fn (var))
3283 tree if_stmt = NULL_TREE;
3284 /* If init_fn is a weakref, make sure it exists before calling. */
3285 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3287 if_stmt = begin_if_stmt ();
3288 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3289 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3290 NE_EXPR, addr, nullptr_node,
3291 tf_warning_or_error);
3292 finish_if_stmt_cond (cond, if_stmt);
3294 finish_expr_stmt (build_cxx_call
3295 (init_fn, 0, NULL, tf_warning_or_error));
3296 if (if_stmt)
3298 finish_then_clause (if_stmt);
3299 finish_if_stmt (if_stmt);
3302 else
3303 /* If there's no initialization, the wrapper is a constant function. */
3304 TREE_READONLY (fn) = true;
3305 finish_return_stmt (convert_from_reference (var));
3306 finish_function_body (body);
3307 expand_or_defer_fn (finish_function (0));
3310 /* Start the process of running a particular set of global constructors
3311 or destructors. Subroutine of do_[cd]tors. Also called from
3312 vtv_start_verification_constructor_init_function. */
3314 static tree
3315 start_objects (int method_type, int initp)
3317 tree body;
3318 tree fndecl;
3319 char type[14];
3321 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3323 if (initp != DEFAULT_INIT_PRIORITY)
3325 char joiner;
3327 #ifdef JOINER
3328 joiner = JOINER;
3329 #else
3330 joiner = '_';
3331 #endif
3333 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3335 else
3336 sprintf (type, "sub_%c", method_type);
3338 fndecl = build_lang_decl (FUNCTION_DECL,
3339 get_file_function_name (type),
3340 build_function_type_list (void_type_node,
3341 NULL_TREE));
3342 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3344 TREE_PUBLIC (current_function_decl) = 0;
3346 /* Mark as artificial because it's not explicitly in the user's
3347 source code. */
3348 DECL_ARTIFICIAL (current_function_decl) = 1;
3350 /* Mark this declaration as used to avoid spurious warnings. */
3351 TREE_USED (current_function_decl) = 1;
3353 /* Mark this function as a global constructor or destructor. */
3354 if (method_type == 'I')
3355 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3356 else
3357 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3359 body = begin_compound_stmt (BCS_FN_BODY);
3361 return body;
3364 /* Finish the process of running a particular set of global constructors
3365 or destructors. Subroutine of do_[cd]tors. */
3367 static void
3368 finish_objects (int method_type, int initp, tree body)
3370 tree fn;
3372 /* Finish up. */
3373 finish_compound_stmt (body);
3374 fn = finish_function (0);
3376 if (method_type == 'I')
3378 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3379 decl_init_priority_insert (fn, initp);
3381 else
3383 DECL_STATIC_DESTRUCTOR (fn) = 1;
3384 decl_fini_priority_insert (fn, initp);
3387 expand_or_defer_fn (fn);
3390 /* The names of the parameters to the function created to handle
3391 initializations and destructions for objects with static storage
3392 duration. */
3393 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3394 #define PRIORITY_IDENTIFIER "__priority"
3396 /* The name of the function we create to handle initializations and
3397 destructions for objects with static storage duration. */
3398 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3400 /* The declaration for the __INITIALIZE_P argument. */
3401 static GTY(()) tree initialize_p_decl;
3403 /* The declaration for the __PRIORITY argument. */
3404 static GTY(()) tree priority_decl;
3406 /* The declaration for the static storage duration function. */
3407 static GTY(()) tree ssdf_decl;
3409 /* All the static storage duration functions created in this
3410 translation unit. */
3411 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3413 /* A map from priority levels to information about that priority
3414 level. There may be many such levels, so efficient lookup is
3415 important. */
3416 static splay_tree priority_info_map;
3418 /* Begins the generation of the function that will handle all
3419 initialization and destruction of objects with static storage
3420 duration. The function generated takes two parameters of type
3421 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3422 nonzero, it performs initializations. Otherwise, it performs
3423 destructions. It only performs those initializations or
3424 destructions with the indicated __PRIORITY. The generated function
3425 returns no value.
3427 It is assumed that this function will only be called once per
3428 translation unit. */
3430 static tree
3431 start_static_storage_duration_function (unsigned count)
3433 tree type;
3434 tree body;
3435 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3437 /* Create the identifier for this function. It will be of the form
3438 SSDF_IDENTIFIER_<number>. */
3439 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3441 type = build_function_type_list (void_type_node,
3442 integer_type_node, integer_type_node,
3443 NULL_TREE);
3445 /* Create the FUNCTION_DECL itself. */
3446 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3447 get_identifier (id),
3448 type);
3449 TREE_PUBLIC (ssdf_decl) = 0;
3450 DECL_ARTIFICIAL (ssdf_decl) = 1;
3452 /* Put this function in the list of functions to be called from the
3453 static constructors and destructors. */
3454 if (!ssdf_decls)
3456 vec_alloc (ssdf_decls, 32);
3458 /* Take this opportunity to initialize the map from priority
3459 numbers to information about that priority level. */
3460 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3461 /*delete_key_fn=*/0,
3462 /*delete_value_fn=*/
3463 (splay_tree_delete_value_fn) &free);
3465 /* We always need to generate functions for the
3466 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3467 priorities later, we'll be sure to find the
3468 DEFAULT_INIT_PRIORITY. */
3469 get_priority_info (DEFAULT_INIT_PRIORITY);
3472 vec_safe_push (ssdf_decls, ssdf_decl);
3474 /* Create the argument list. */
3475 initialize_p_decl = cp_build_parm_decl
3476 (ssdf_decl, get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3477 TREE_USED (initialize_p_decl) = 1;
3478 priority_decl = cp_build_parm_decl
3479 (ssdf_decl, get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3480 TREE_USED (priority_decl) = 1;
3482 DECL_CHAIN (initialize_p_decl) = priority_decl;
3483 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3485 /* Put the function in the global scope. */
3486 pushdecl (ssdf_decl);
3488 /* Start the function itself. This is equivalent to declaring the
3489 function as:
3491 static void __ssdf (int __initialize_p, init __priority_p);
3493 It is static because we only need to call this function from the
3494 various constructor and destructor functions for this module. */
3495 start_preparsed_function (ssdf_decl,
3496 /*attrs=*/NULL_TREE,
3497 SF_PRE_PARSED);
3499 /* Set up the scope of the outermost block in the function. */
3500 body = begin_compound_stmt (BCS_FN_BODY);
3502 return body;
3505 /* Finish the generation of the function which performs initialization
3506 and destruction of objects with static storage duration. After
3507 this point, no more such objects can be created. */
3509 static void
3510 finish_static_storage_duration_function (tree body)
3512 /* Close out the function. */
3513 finish_compound_stmt (body);
3514 expand_or_defer_fn (finish_function (0));
3517 /* Return the information about the indicated PRIORITY level. If no
3518 code to handle this level has yet been generated, generate the
3519 appropriate prologue. */
3521 static priority_info
3522 get_priority_info (int priority)
3524 priority_info pi;
3525 splay_tree_node n;
3527 n = splay_tree_lookup (priority_info_map,
3528 (splay_tree_key) priority);
3529 if (!n)
3531 /* Create a new priority information structure, and insert it
3532 into the map. */
3533 pi = XNEW (struct priority_info_s);
3534 pi->initializations_p = 0;
3535 pi->destructions_p = 0;
3536 splay_tree_insert (priority_info_map,
3537 (splay_tree_key) priority,
3538 (splay_tree_value) pi);
3540 else
3541 pi = (priority_info) n->value;
3543 return pi;
3546 /* The effective initialization priority of a DECL. */
3548 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3549 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3550 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3552 /* Whether a DECL needs a guard to protect it against multiple
3553 initialization. */
3555 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3556 || DECL_ONE_ONLY (decl) \
3557 || DECL_WEAK (decl)))
3559 /* Called from one_static_initialization_or_destruction(),
3560 via walk_tree.
3561 Walks the initializer list of a global variable and looks for
3562 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3563 and that have their DECL_CONTEXT() == NULL.
3564 For each such temporary variable, set their DECL_CONTEXT() to
3565 the current function. This is necessary because otherwise
3566 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3567 when trying to refer to a temporary variable that does not have
3568 it's DECL_CONTECT() properly set. */
3569 static tree
3570 fix_temporary_vars_context_r (tree *node,
3571 int * /*unused*/,
3572 void * /*unused1*/)
3574 gcc_assert (current_function_decl);
3576 if (TREE_CODE (*node) == BIND_EXPR)
3578 tree var;
3580 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3581 if (VAR_P (var)
3582 && !DECL_NAME (var)
3583 && DECL_ARTIFICIAL (var)
3584 && !DECL_CONTEXT (var))
3585 DECL_CONTEXT (var) = current_function_decl;
3588 return NULL_TREE;
3591 /* Set up to handle the initialization or destruction of DECL. If
3592 INITP is nonzero, we are initializing the variable. Otherwise, we
3593 are destroying it. */
3595 static void
3596 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3598 tree guard_if_stmt = NULL_TREE;
3599 tree guard;
3601 /* If we are supposed to destruct and there's a trivial destructor,
3602 nothing has to be done. */
3603 if (!initp
3604 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3605 return;
3607 /* Trick the compiler into thinking we are at the file and line
3608 where DECL was declared so that error-messages make sense, and so
3609 that the debugger will show somewhat sensible file and line
3610 information. */
3611 input_location = DECL_SOURCE_LOCATION (decl);
3613 /* Make sure temporary variables in the initialiser all have
3614 their DECL_CONTEXT() set to a value different from NULL_TREE.
3615 This can happen when global variables initializers are built.
3616 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3617 the temporary variables that might have been generated in the
3618 accompanying initializers is NULL_TREE, meaning the variables have been
3619 declared in the global namespace.
3620 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3621 of the temporaries are set to the current function decl. */
3622 cp_walk_tree_without_duplicates (&init,
3623 fix_temporary_vars_context_r,
3624 NULL);
3626 /* Because of:
3628 [class.access.spec]
3630 Access control for implicit calls to the constructors,
3631 the conversion functions, or the destructor called to
3632 create and destroy a static data member is performed as
3633 if these calls appeared in the scope of the member's
3634 class.
3636 we pretend we are in a static member function of the class of
3637 which the DECL is a member. */
3638 if (member_p (decl))
3640 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3641 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3644 /* Assume we don't need a guard. */
3645 guard = NULL_TREE;
3646 /* We need a guard if this is an object with external linkage that
3647 might be initialized in more than one place. (For example, a
3648 static data member of a template, when the data member requires
3649 construction.) */
3650 if (NEEDS_GUARD_P (decl))
3652 tree guard_cond;
3654 guard = get_guard (decl);
3656 /* When using __cxa_atexit, we just check the GUARD as we would
3657 for a local static. */
3658 if (flag_use_cxa_atexit)
3660 /* When using __cxa_atexit, we never try to destroy
3661 anything from a static destructor. */
3662 gcc_assert (initp);
3663 guard_cond = get_guard_cond (guard, false);
3665 /* If we don't have __cxa_atexit, then we will be running
3666 destructors from .fini sections, or their equivalents. So,
3667 we need to know how many times we've tried to initialize this
3668 object. We do initializations only if the GUARD is zero,
3669 i.e., if we are the first to initialize the variable. We do
3670 destructions only if the GUARD is one, i.e., if we are the
3671 last to destroy the variable. */
3672 else if (initp)
3673 guard_cond
3674 = cp_build_binary_op (input_location,
3675 EQ_EXPR,
3676 cp_build_unary_op (PREINCREMENT_EXPR,
3677 guard,
3678 /*noconvert=*/true,
3679 tf_warning_or_error),
3680 integer_one_node,
3681 tf_warning_or_error);
3682 else
3683 guard_cond
3684 = cp_build_binary_op (input_location,
3685 EQ_EXPR,
3686 cp_build_unary_op (PREDECREMENT_EXPR,
3687 guard,
3688 /*noconvert=*/true,
3689 tf_warning_or_error),
3690 integer_zero_node,
3691 tf_warning_or_error);
3693 guard_if_stmt = begin_if_stmt ();
3694 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3698 /* If we're using __cxa_atexit, we have not already set the GUARD,
3699 so we must do so now. */
3700 if (guard && initp && flag_use_cxa_atexit)
3701 finish_expr_stmt (set_guard (guard));
3703 /* Perform the initialization or destruction. */
3704 if (initp)
3706 if (init)
3708 finish_expr_stmt (init);
3709 if (sanitize_flags_p (SANITIZE_ADDRESS, decl))
3711 varpool_node *vnode = varpool_node::get (decl);
3712 if (vnode)
3713 vnode->dynamically_initialized = 1;
3717 /* If we're using __cxa_atexit, register a function that calls the
3718 destructor for the object. */
3719 if (flag_use_cxa_atexit)
3720 finish_expr_stmt (register_dtor_fn (decl));
3722 else
3723 finish_expr_stmt (build_cleanup (decl));
3725 /* Finish the guard if-stmt, if necessary. */
3726 if (guard)
3728 finish_then_clause (guard_if_stmt);
3729 finish_if_stmt (guard_if_stmt);
3732 /* Now that we're done with DECL we don't need to pretend to be a
3733 member of its class any longer. */
3734 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3735 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3738 /* Generate code to do the initialization or destruction of the decls in VARS,
3739 a TREE_LIST of VAR_DECL with static storage duration.
3740 Whether initialization or destruction is performed is specified by INITP. */
3742 static void
3743 do_static_initialization_or_destruction (tree vars, bool initp)
3745 tree node, init_if_stmt, cond;
3747 /* Build the outer if-stmt to check for initialization or destruction. */
3748 init_if_stmt = begin_if_stmt ();
3749 cond = initp ? integer_one_node : integer_zero_node;
3750 cond = cp_build_binary_op (input_location,
3751 EQ_EXPR,
3752 initialize_p_decl,
3753 cond,
3754 tf_warning_or_error);
3755 finish_if_stmt_cond (cond, init_if_stmt);
3757 /* To make sure dynamic construction doesn't access globals from other
3758 compilation units where they might not be yet constructed, for
3759 -fsanitize=address insert __asan_before_dynamic_init call that
3760 prevents access to either all global variables that need construction
3761 in other compilation units, or at least those that haven't been
3762 initialized yet. Variables that need dynamic construction in
3763 the current compilation unit are kept accessible. */
3764 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
3765 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3767 node = vars;
3768 do {
3769 tree decl = TREE_VALUE (node);
3770 tree priority_if_stmt;
3771 int priority;
3772 priority_info pi;
3774 /* If we don't need a destructor, there's nothing to do. Avoid
3775 creating a possibly empty if-stmt. */
3776 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3778 node = TREE_CHAIN (node);
3779 continue;
3782 /* Remember that we had an initialization or finalization at this
3783 priority. */
3784 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3785 pi = get_priority_info (priority);
3786 if (initp)
3787 pi->initializations_p = 1;
3788 else
3789 pi->destructions_p = 1;
3791 /* Conditionalize this initialization on being in the right priority
3792 and being initializing/finalizing appropriately. */
3793 priority_if_stmt = begin_if_stmt ();
3794 cond = cp_build_binary_op (input_location,
3795 EQ_EXPR,
3796 priority_decl,
3797 build_int_cst (NULL_TREE, priority),
3798 tf_warning_or_error);
3799 finish_if_stmt_cond (cond, priority_if_stmt);
3801 /* Process initializers with same priority. */
3802 for (; node
3803 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3804 node = TREE_CHAIN (node))
3805 /* Do one initialization or destruction. */
3806 one_static_initialization_or_destruction (TREE_VALUE (node),
3807 TREE_PURPOSE (node), initp);
3809 /* Finish up the priority if-stmt body. */
3810 finish_then_clause (priority_if_stmt);
3811 finish_if_stmt (priority_if_stmt);
3813 } while (node);
3815 /* Revert what __asan_before_dynamic_init did by calling
3816 __asan_after_dynamic_init. */
3817 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
3818 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3820 /* Finish up the init/destruct if-stmt body. */
3821 finish_then_clause (init_if_stmt);
3822 finish_if_stmt (init_if_stmt);
3825 /* VARS is a list of variables with static storage duration which may
3826 need initialization and/or finalization. Remove those variables
3827 that don't really need to be initialized or finalized, and return
3828 the resulting list. The order in which the variables appear in
3829 VARS is in reverse order of the order in which they should actually
3830 be initialized. The list we return is in the unreversed order;
3831 i.e., the first variable should be initialized first. */
3833 static tree
3834 prune_vars_needing_no_initialization (tree *vars)
3836 tree *var = vars;
3837 tree result = NULL_TREE;
3839 while (*var)
3841 tree t = *var;
3842 tree decl = TREE_VALUE (t);
3843 tree init = TREE_PURPOSE (t);
3845 /* Deal gracefully with error. */
3846 if (error_operand_p (decl))
3848 var = &TREE_CHAIN (t);
3849 continue;
3852 /* The only things that can be initialized are variables. */
3853 gcc_assert (VAR_P (decl));
3855 /* If this object is not defined, we don't need to do anything
3856 here. */
3857 if (DECL_EXTERNAL (decl))
3859 var = &TREE_CHAIN (t);
3860 continue;
3863 /* Also, if the initializer already contains errors, we can bail
3864 out now. */
3865 if (init && TREE_CODE (init) == TREE_LIST
3866 && value_member (error_mark_node, init))
3868 var = &TREE_CHAIN (t);
3869 continue;
3872 /* This variable is going to need initialization and/or
3873 finalization, so we add it to the list. */
3874 *var = TREE_CHAIN (t);
3875 TREE_CHAIN (t) = result;
3876 result = t;
3879 return result;
3882 /* Make sure we have told the back end about all the variables in
3883 VARS. */
3885 static void
3886 write_out_vars (tree vars)
3888 tree v;
3890 for (v = vars; v; v = TREE_CHAIN (v))
3892 tree var = TREE_VALUE (v);
3893 if (!var_finalized_p (var))
3895 import_export_decl (var);
3896 rest_of_decl_compilation (var, 1, 1);
3901 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3902 (otherwise) that will initialize all global objects with static
3903 storage duration having the indicated PRIORITY. */
3905 static void
3906 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3907 location_t *locus)
3909 char function_key;
3910 tree fndecl;
3911 tree body;
3912 size_t i;
3914 input_location = *locus;
3915 /* ??? */
3916 /* Was: locus->line++; */
3918 /* We use `I' to indicate initialization and `D' to indicate
3919 destruction. */
3920 function_key = constructor_p ? 'I' : 'D';
3922 /* We emit the function lazily, to avoid generating empty
3923 global constructors and destructors. */
3924 body = NULL_TREE;
3926 /* For Objective-C++, we may need to initialize metadata found in this module.
3927 This must be done _before_ any other static initializations. */
3928 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3929 && constructor_p && objc_static_init_needed_p ())
3931 body = start_objects (function_key, priority);
3932 objc_generate_static_init_call (NULL_TREE);
3935 /* Call the static storage duration function with appropriate
3936 arguments. */
3937 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
3939 /* Calls to pure or const functions will expand to nothing. */
3940 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3942 tree call;
3944 if (! body)
3945 body = start_objects (function_key, priority);
3947 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
3948 build_int_cst (NULL_TREE,
3949 constructor_p),
3950 build_int_cst (NULL_TREE,
3951 priority),
3952 NULL_TREE);
3953 finish_expr_stmt (call);
3957 /* Close out the function. */
3958 if (body)
3959 finish_objects (function_key, priority, body);
3962 /* Generate constructor and destructor functions for the priority
3963 indicated by N. */
3965 static int
3966 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
3968 location_t *locus = (location_t *) data;
3969 int priority = (int) n->key;
3970 priority_info pi = (priority_info) n->value;
3972 /* Generate the functions themselves, but only if they are really
3973 needed. */
3974 if (pi->initializations_p)
3975 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
3976 if (pi->destructions_p)
3977 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
3979 /* Keep iterating. */
3980 return 0;
3983 /* Return C++ property of T, based on given operation OP. */
3985 static int
3986 cpp_check (tree t, cpp_operation op)
3988 switch (op)
3990 case HAS_DEPENDENT_TEMPLATE_ARGS:
3992 tree ti = CLASSTYPE_TEMPLATE_INFO (t);
3993 if (!ti)
3994 return 0;
3995 ++processing_template_decl;
3996 const bool dep = any_dependent_template_arguments_p (TI_ARGS (ti));
3997 --processing_template_decl;
3998 return dep;
4000 case IS_ABSTRACT:
4001 return DECL_PURE_VIRTUAL_P (t);
4002 case IS_CONSTRUCTOR:
4003 return DECL_CONSTRUCTOR_P (t);
4004 case IS_DESTRUCTOR:
4005 return DECL_DESTRUCTOR_P (t);
4006 case IS_COPY_CONSTRUCTOR:
4007 return DECL_COPY_CONSTRUCTOR_P (t);
4008 case IS_MOVE_CONSTRUCTOR:
4009 return DECL_MOVE_CONSTRUCTOR_P (t);
4010 case IS_TEMPLATE:
4011 return TREE_CODE (t) == TEMPLATE_DECL;
4012 case IS_TRIVIAL:
4013 return trivial_type_p (t);
4014 default:
4015 return 0;
4019 /* Collect source file references recursively, starting from NAMESPC. */
4021 static void
4022 collect_source_refs (tree namespc)
4024 /* Iterate over names in this name space. */
4025 for (tree t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4026 if (DECL_IS_BUILTIN (t))
4028 else if (TREE_CODE (t) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (t))
4029 collect_source_refs (t);
4030 else
4031 collect_source_ref (DECL_SOURCE_FILE (t));
4034 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4035 starting from NAMESPC. */
4037 static void
4038 collect_ada_namespace (tree namespc, const char *source_file)
4040 tree decl = NAMESPACE_LEVEL (namespc)->names;
4042 /* Collect decls from this namespace. This will skip
4043 NAMESPACE_DECLs (both aliases and regular, it cannot tell). */
4044 collect_ada_nodes (decl, source_file);
4046 /* Now scan for namespace children, and dump them. */
4047 for (; decl; decl = TREE_CHAIN (decl))
4048 if (TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl))
4049 collect_ada_namespace (decl, source_file);
4052 /* Returns true iff there is a definition available for variable or
4053 function DECL. */
4055 bool
4056 decl_defined_p (tree decl)
4058 if (TREE_CODE (decl) == FUNCTION_DECL)
4059 return (DECL_INITIAL (decl) != NULL_TREE
4060 /* A pending instantiation of a friend temploid is defined. */
4061 || (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
4062 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4063 (DECL_TI_TEMPLATE (decl)))));
4064 else
4066 gcc_assert (VAR_P (decl));
4067 return !DECL_EXTERNAL (decl);
4071 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4073 [expr.const]
4075 An integral constant-expression can only involve ... const
4076 variables of integral or enumeration types initialized with
4077 constant expressions ...
4079 C++0x also allows constexpr variables and temporaries initialized
4080 with constant expressions. We handle the former here, but the latter
4081 are just folded away in cxx_eval_constant_expression.
4083 The standard does not require that the expression be non-volatile.
4084 G++ implements the proposed correction in DR 457. */
4086 bool
4087 decl_constant_var_p (tree decl)
4089 if (!decl_maybe_constant_var_p (decl))
4090 return false;
4092 /* We don't know if a template static data member is initialized with
4093 a constant expression until we instantiate its initializer. Even
4094 in the case of a constexpr variable, we can't treat it as a
4095 constant until its initializer is complete in case it's used in
4096 its own initializer. */
4097 maybe_instantiate_decl (decl);
4098 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4101 /* Returns true if DECL could be a symbolic constant variable, depending on
4102 its initializer. */
4104 bool
4105 decl_maybe_constant_var_p (tree decl)
4107 tree type = TREE_TYPE (decl);
4108 if (!VAR_P (decl))
4109 return false;
4110 if (DECL_DECLARED_CONSTEXPR_P (decl))
4111 return true;
4112 if (DECL_HAS_VALUE_EXPR_P (decl))
4113 /* A proxy isn't constant. */
4114 return false;
4115 if (TREE_CODE (type) == REFERENCE_TYPE)
4116 /* References can be constant. */;
4117 else if (CP_TYPE_CONST_NON_VOLATILE_P (type)
4118 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4119 /* And const integers. */;
4120 else
4121 return false;
4123 if (DECL_INITIAL (decl)
4124 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
4125 /* We know the initializer, and it isn't constant. */
4126 return false;
4127 else
4128 return true;
4131 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4132 called from grokfndecl and grokvardecl; in all modes it is called from
4133 cp_write_global_declarations. */
4135 void
4136 no_linkage_error (tree decl)
4138 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4139 /* In C++11 it's ok if the decl is defined. */
4140 return;
4141 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4142 if (t == NULL_TREE)
4143 /* The type that got us on no_linkage_decls must have gotten a name for
4144 linkage purposes. */;
4145 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4146 /* The type might end up having a typedef name for linkage purposes. */
4147 vec_safe_push (no_linkage_decls, decl);
4148 else if (TYPE_UNNAMED_P (t))
4150 bool d = false;
4151 if (cxx_dialect >= cxx11)
4152 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4153 "unnamed type, is used but never defined", decl);
4154 else if (DECL_EXTERN_C_P (decl))
4155 /* Allow this; it's pretty common in C. */;
4156 else if (VAR_P (decl))
4157 /* DRs 132, 319 and 389 seem to indicate types with
4158 no linkage can only be used to declare extern "C"
4159 entities. Since it's not always an error in the
4160 ISO C++ 90 Standard, we only issue a warning. */
4161 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "unnamed type "
4162 "with no linkage used to declare variable %q#D with "
4163 "linkage", decl);
4164 else
4165 d = permerror (DECL_SOURCE_LOCATION (decl), "unnamed type with no "
4166 "linkage used to declare function %q#D with linkage",
4167 decl);
4168 if (d && is_typedef_decl (TYPE_NAME (t)))
4169 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4170 "to the unqualified type, so it is not used for linkage",
4171 TYPE_NAME (t));
4173 else if (cxx_dialect >= cxx11)
4175 if (VAR_P (decl) || !DECL_PURE_VIRTUAL_P (decl))
4176 permerror (DECL_SOURCE_LOCATION (decl),
4177 "%q#D, declared using local type "
4178 "%qT, is used but never defined", decl, t);
4180 else if (VAR_P (decl))
4181 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4182 "used to declare variable %q#D with linkage", t, decl);
4183 else
4184 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4185 "to declare function %q#D with linkage", t, decl);
4188 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4190 static void
4191 collect_all_refs (const char *source_file)
4193 collect_ada_namespace (global_namespace, source_file);
4196 /* Clear DECL_EXTERNAL for NODE. */
4198 static bool
4199 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4201 DECL_EXTERNAL (node->decl) = 0;
4202 return false;
4205 /* Build up the function to run dynamic initializers for thread_local
4206 variables in this translation unit and alias the init functions for the
4207 individual variables to it. */
4209 static void
4210 handle_tls_init (void)
4212 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4213 if (vars == NULL_TREE)
4214 return;
4216 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4218 write_out_vars (vars);
4220 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4221 boolean_type_node);
4222 TREE_PUBLIC (guard) = false;
4223 TREE_STATIC (guard) = true;
4224 DECL_ARTIFICIAL (guard) = true;
4225 DECL_IGNORED_P (guard) = true;
4226 TREE_USED (guard) = true;
4227 CP_DECL_THREAD_LOCAL_P (guard) = true;
4228 set_decl_tls_model (guard, decl_default_tls_model (guard));
4229 pushdecl_top_level_and_finish (guard, NULL_TREE);
4231 tree fn = get_local_tls_init_fn ();
4232 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4233 tree body = begin_function_body ();
4234 tree if_stmt = begin_if_stmt ();
4235 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4236 tf_warning_or_error);
4237 finish_if_stmt_cond (cond, if_stmt);
4238 finish_expr_stmt (cp_build_modify_expr (loc, guard, NOP_EXPR,
4239 boolean_true_node,
4240 tf_warning_or_error));
4241 for (; vars; vars = TREE_CHAIN (vars))
4243 tree var = TREE_VALUE (vars);
4244 tree init = TREE_PURPOSE (vars);
4245 one_static_initialization_or_destruction (var, init, true);
4247 #ifdef ASM_OUTPUT_DEF
4248 /* Output init aliases even with -fno-extern-tls-init. */
4249 if (TREE_PUBLIC (var))
4251 tree single_init_fn = get_tls_init_fn (var);
4252 if (single_init_fn == NULL_TREE)
4253 continue;
4254 cgraph_node *alias
4255 = cgraph_node::get_create (fn)->create_same_body_alias
4256 (single_init_fn, fn);
4257 gcc_assert (alias != NULL);
4259 #endif
4262 finish_then_clause (if_stmt);
4263 finish_if_stmt (if_stmt);
4264 finish_function_body (body);
4265 expand_or_defer_fn (finish_function (0));
4268 /* We're at the end of compilation, so generate any mangling aliases that
4269 we've been saving up, if DECL is going to be output and ID2 isn't
4270 already taken by another declaration. */
4272 static void
4273 generate_mangling_alias (tree decl, tree id2)
4275 /* If there's a declaration already using this mangled name,
4276 don't create a compatibility alias that conflicts. */
4277 if (IDENTIFIER_GLOBAL_VALUE (id2))
4278 return;
4280 struct cgraph_node *n = NULL;
4281 if (TREE_CODE (decl) == FUNCTION_DECL
4282 && !(n = cgraph_node::get (decl)))
4283 /* Don't create an alias to an unreferenced function. */
4284 return;
4286 tree alias = make_alias_for (decl, id2);
4287 SET_IDENTIFIER_GLOBAL_VALUE (id2, alias);
4288 DECL_IGNORED_P (alias) = 1;
4289 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
4290 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
4291 if (vague_linkage_p (decl))
4292 DECL_WEAK (alias) = 1;
4293 if (TREE_CODE (decl) == FUNCTION_DECL)
4294 n->create_same_body_alias (alias, decl);
4295 else
4296 varpool_node::create_extra_name_alias (alias, decl);
4299 /* Note that we might want to emit an alias with the symbol ID2 for DECL at
4300 the end of translation, for compatibility across bugs in the mangling
4301 implementation. */
4303 void
4304 note_mangling_alias (tree decl ATTRIBUTE_UNUSED, tree id2 ATTRIBUTE_UNUSED)
4306 #ifdef ASM_OUTPUT_DEF
4307 if (!defer_mangling_aliases)
4308 generate_mangling_alias (decl, id2);
4309 else
4311 vec_safe_push (mangling_aliases, decl);
4312 vec_safe_push (mangling_aliases, id2);
4314 #endif
4317 /* Emit all mangling aliases that were deferred up to this point. */
4319 void
4320 generate_mangling_aliases ()
4322 while (!vec_safe_is_empty (mangling_aliases))
4324 tree id2 = mangling_aliases->pop();
4325 tree decl = mangling_aliases->pop();
4326 generate_mangling_alias (decl, id2);
4328 defer_mangling_aliases = false;
4331 /* The entire file is now complete. If requested, dump everything
4332 to a file. */
4334 static void
4335 dump_tu (void)
4337 dump_flags_t flags;
4338 if (FILE *stream = dump_begin (raw_dump_id, &flags))
4340 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4341 dump_end (raw_dump_id, stream);
4345 static location_t locus_at_end_of_parsing;
4347 /* Check the deallocation functions for CODE to see if we want to warn that
4348 only one was defined. */
4350 static void
4351 maybe_warn_sized_delete (enum tree_code code)
4353 tree sized = NULL_TREE;
4354 tree unsized = NULL_TREE;
4356 for (ovl_iterator iter (IDENTIFIER_GLOBAL_VALUE (cp_operator_id (code)));
4357 iter; ++iter)
4359 tree fn = *iter;
4360 /* We're only interested in usual deallocation functions. */
4361 if (!usual_deallocation_fn_p (fn))
4362 continue;
4363 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
4364 unsized = fn;
4365 else
4366 sized = fn;
4368 if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
4369 warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
4370 "the program should also define %qD", sized);
4371 else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
4372 warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
4373 "the program should also define %qD", unsized);
4376 /* Check the global deallocation functions to see if we want to warn about
4377 defining unsized without sized (or vice versa). */
4379 static void
4380 maybe_warn_sized_delete ()
4382 if (!flag_sized_deallocation || !warn_sized_deallocation)
4383 return;
4384 maybe_warn_sized_delete (DELETE_EXPR);
4385 maybe_warn_sized_delete (VEC_DELETE_EXPR);
4388 /* Earlier we left PTRMEM_CST in variable initializers alone so that we could
4389 look them up when evaluating non-type template parameters. Now we need to
4390 lower them to something the back end can understand. */
4392 static void
4393 lower_var_init ()
4395 varpool_node *node;
4396 FOR_EACH_VARIABLE (node)
4398 tree d = node->decl;
4399 if (tree init = DECL_INITIAL (d))
4400 DECL_INITIAL (d) = cplus_expand_constant (init);
4404 /* This routine is called at the end of compilation.
4405 Its job is to create all the code needed to initialize and
4406 destroy the global aggregates. We do the destruction
4407 first, since that way we only need to reverse the decls once. */
4409 void
4410 c_parse_final_cleanups (void)
4412 tree vars;
4413 bool reconsider;
4414 size_t i;
4415 unsigned ssdf_count = 0;
4416 int retries = 0;
4417 tree decl;
4419 locus_at_end_of_parsing = input_location;
4420 at_eof = 1;
4422 /* Bad parse errors. Just forget about it. */
4423 if (! global_bindings_p () || current_class_type
4424 || !vec_safe_is_empty (decl_namespace_list))
4425 return;
4427 /* This is the point to write out a PCH if we're doing that.
4428 In that case we do not want to do anything else. */
4429 if (pch_file)
4431 /* Mangle all symbols at PCH creation time. */
4432 symtab_node *node;
4433 FOR_EACH_SYMBOL (node)
4434 if (! is_a <varpool_node *> (node)
4435 || ! DECL_HARD_REGISTER (node->decl))
4436 DECL_ASSEMBLER_NAME (node->decl);
4437 c_common_write_pch ();
4438 dump_tu ();
4439 /* Ensure even the callers don't try to finalize the CU. */
4440 flag_syntax_only = 1;
4441 return;
4444 timevar_stop (TV_PHASE_PARSING);
4445 timevar_start (TV_PHASE_DEFERRED);
4447 symtab->process_same_body_aliases ();
4449 /* Handle -fdump-ada-spec[-slim] */
4450 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4452 if (flag_dump_ada_spec_slim)
4453 collect_source_ref (main_input_filename);
4454 else
4455 collect_source_refs (global_namespace);
4457 dump_ada_specs (collect_all_refs, cpp_check);
4460 /* FIXME - huh? was input_line -= 1;*/
4462 /* We now have to write out all the stuff we put off writing out.
4463 These include:
4465 o Template specializations that we have not yet instantiated,
4466 but which are needed.
4467 o Initialization and destruction for non-local objects with
4468 static storage duration. (Local objects with static storage
4469 duration are initialized when their scope is first entered,
4470 and are cleaned up via atexit.)
4471 o Virtual function tables.
4473 All of these may cause others to be needed. For example,
4474 instantiating one function may cause another to be needed, and
4475 generating the initializer for an object may cause templates to be
4476 instantiated, etc., etc. */
4478 emit_support_tinfos ();
4482 tree t;
4483 tree decl;
4485 reconsider = false;
4487 /* If there are templates that we've put off instantiating, do
4488 them now. */
4489 instantiate_pending_templates (retries);
4490 ggc_collect ();
4492 /* Write out virtual tables as required. Writing out the
4493 virtual table for a template class may cause the
4494 instantiation of members of that class. If we write out
4495 vtables then we remove the class from our list so we don't
4496 have to look at it again. */
4497 for (i = keyed_classes->length ();
4498 keyed_classes->iterate (--i, &t);)
4499 if (maybe_emit_vtables (t))
4501 reconsider = true;
4502 keyed_classes->unordered_remove (i);
4505 /* Write out needed type info variables. We have to be careful
4506 looping through unemitted decls, because emit_tinfo_decl may
4507 cause other variables to be needed. New elements will be
4508 appended, and we remove from the vector those that actually
4509 get emitted. */
4510 for (i = unemitted_tinfo_decls->length ();
4511 unemitted_tinfo_decls->iterate (--i, &t);)
4512 if (emit_tinfo_decl (t))
4514 reconsider = true;
4515 unemitted_tinfo_decls->unordered_remove (i);
4518 /* The list of objects with static storage duration is built up
4519 in reverse order. We clear STATIC_AGGREGATES so that any new
4520 aggregates added during the initialization of these will be
4521 initialized in the correct order when we next come around the
4522 loop. */
4523 vars = prune_vars_needing_no_initialization (&static_aggregates);
4525 if (vars)
4527 /* We need to start a new initialization function each time
4528 through the loop. That's because we need to know which
4529 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4530 isn't computed until a function is finished, and written
4531 out. That's a deficiency in the back end. When this is
4532 fixed, these initialization functions could all become
4533 inline, with resulting performance improvements. */
4534 tree ssdf_body;
4536 /* Set the line and file, so that it is obviously not from
4537 the source file. */
4538 input_location = locus_at_end_of_parsing;
4539 ssdf_body = start_static_storage_duration_function (ssdf_count);
4541 /* Make sure the back end knows about all the variables. */
4542 write_out_vars (vars);
4544 /* First generate code to do all the initializations. */
4545 if (vars)
4546 do_static_initialization_or_destruction (vars, /*initp=*/true);
4548 /* Then, generate code to do all the destructions. Do these
4549 in reverse order so that the most recently constructed
4550 variable is the first destroyed. If we're using
4551 __cxa_atexit, then we don't need to do this; functions
4552 were registered at initialization time to destroy the
4553 local statics. */
4554 if (!flag_use_cxa_atexit && vars)
4556 vars = nreverse (vars);
4557 do_static_initialization_or_destruction (vars, /*initp=*/false);
4559 else
4560 vars = NULL_TREE;
4562 /* Finish up the static storage duration function for this
4563 round. */
4564 input_location = locus_at_end_of_parsing;
4565 finish_static_storage_duration_function (ssdf_body);
4567 /* All those initializations and finalizations might cause
4568 us to need more inline functions, more template
4569 instantiations, etc. */
4570 reconsider = true;
4571 ssdf_count++;
4572 /* ??? was: locus_at_end_of_parsing.line++; */
4575 /* Now do the same for thread_local variables. */
4576 handle_tls_init ();
4578 /* Go through the set of inline functions whose bodies have not
4579 been emitted yet. If out-of-line copies of these functions
4580 are required, emit them. */
4581 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4583 /* Does it need synthesizing? */
4584 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4585 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4587 /* Even though we're already at the top-level, we push
4588 there again. That way, when we pop back a few lines
4589 hence, all of our state is restored. Otherwise,
4590 finish_function doesn't clean things up, and we end
4591 up with CURRENT_FUNCTION_DECL set. */
4592 push_to_top_level ();
4593 /* The decl's location will mark where it was first
4594 needed. Save that so synthesize method can indicate
4595 where it was needed from, in case of error */
4596 input_location = DECL_SOURCE_LOCATION (decl);
4597 synthesize_method (decl);
4598 pop_from_top_level ();
4599 reconsider = true;
4602 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4603 generate_tls_wrapper (decl);
4605 if (!DECL_SAVED_TREE (decl))
4606 continue;
4608 cgraph_node *node = cgraph_node::get_create (decl);
4610 /* We lie to the back end, pretending that some functions
4611 are not defined when they really are. This keeps these
4612 functions from being put out unnecessarily. But, we must
4613 stop lying when the functions are referenced, or if they
4614 are not comdat since they need to be put out now. If
4615 DECL_INTERFACE_KNOWN, then we have already set
4616 DECL_EXTERNAL appropriately, so there's no need to check
4617 again, and we do not want to clear DECL_EXTERNAL if a
4618 previous call to import_export_decl set it.
4620 This is done in a separate for cycle, because if some
4621 deferred function is contained in another deferred
4622 function later in deferred_fns varray,
4623 rest_of_compilation would skip this function and we
4624 really cannot expand the same function twice. */
4625 import_export_decl (decl);
4626 if (DECL_NOT_REALLY_EXTERN (decl)
4627 && DECL_INITIAL (decl)
4628 && decl_needed_p (decl))
4630 if (node->cpp_implicit_alias)
4631 node = node->get_alias_target ();
4633 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
4634 NULL, true);
4635 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4636 group, we need to mark all symbols in the same comdat group
4637 that way. */
4638 if (node->same_comdat_group)
4639 for (cgraph_node *next
4640 = dyn_cast<cgraph_node *> (node->same_comdat_group);
4641 next != node;
4642 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
4643 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
4644 NULL, true);
4647 /* If we're going to need to write this function out, and
4648 there's already a body for it, create RTL for it now.
4649 (There might be no body if this is a method we haven't
4650 gotten around to synthesizing yet.) */
4651 if (!DECL_EXTERNAL (decl)
4652 && decl_needed_p (decl)
4653 && !TREE_ASM_WRITTEN (decl)
4654 && !node->definition)
4656 /* We will output the function; no longer consider it in this
4657 loop. */
4658 DECL_DEFER_OUTPUT (decl) = 0;
4659 /* Generate RTL for this function now that we know we
4660 need it. */
4661 expand_or_defer_fn (decl);
4662 /* If we're compiling -fsyntax-only pretend that this
4663 function has been written out so that we don't try to
4664 expand it again. */
4665 if (flag_syntax_only)
4666 TREE_ASM_WRITTEN (decl) = 1;
4667 reconsider = true;
4671 if (wrapup_namespace_globals ())
4672 reconsider = true;
4674 /* Static data members are just like namespace-scope globals. */
4675 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4677 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4678 /* Don't write it out if we haven't seen a definition. */
4679 || (DECL_IN_AGGR_P (decl) && !DECL_INLINE_VAR_P (decl)))
4680 continue;
4681 import_export_decl (decl);
4682 /* If this static data member is needed, provide it to the
4683 back end. */
4684 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4685 DECL_EXTERNAL (decl) = 0;
4687 if (vec_safe_length (pending_statics) != 0
4688 && wrapup_global_declarations (pending_statics->address (),
4689 pending_statics->length ()))
4690 reconsider = true;
4692 retries++;
4694 while (reconsider);
4696 lower_var_init ();
4698 generate_mangling_aliases ();
4700 /* All used inline functions must have a definition at this point. */
4701 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4703 if (/* Check online inline functions that were actually used. */
4704 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4705 /* If the definition actually was available here, then the
4706 fact that the function was not defined merely represents
4707 that for some reason (use of a template repository,
4708 #pragma interface, etc.) we decided not to emit the
4709 definition here. */
4710 && !DECL_INITIAL (decl)
4711 /* Don't complain if the template was defined. */
4712 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4713 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4714 (template_for_substitution (decl)))))
4716 warning_at (DECL_SOURCE_LOCATION (decl), 0,
4717 "inline function %qD used but never defined", decl);
4718 /* Avoid a duplicate warning from check_global_declaration. */
4719 TREE_NO_WARNING (decl) = 1;
4723 /* So must decls that use a type with no linkage. */
4724 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4725 no_linkage_error (decl);
4727 maybe_warn_sized_delete ();
4729 /* Then, do the Objective-C stuff. This is where all the
4730 Objective-C module stuff gets generated (symtab,
4731 class/protocol/selector lists etc). This must be done after C++
4732 templates, destructors etc. so that selectors used in C++
4733 templates are properly allocated. */
4734 if (c_dialect_objc ())
4735 objc_write_global_declarations ();
4737 /* We give C linkage to static constructors and destructors. */
4738 push_lang_context (lang_name_c);
4740 /* Generate initialization and destruction functions for all
4741 priorities for which they are required. */
4742 if (priority_info_map)
4743 splay_tree_foreach (priority_info_map,
4744 generate_ctor_and_dtor_functions_for_priority,
4745 /*data=*/&locus_at_end_of_parsing);
4746 else if (c_dialect_objc () && objc_static_init_needed_p ())
4747 /* If this is obj-c++ and we need a static init, call
4748 generate_ctor_or_dtor_function. */
4749 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4750 DEFAULT_INIT_PRIORITY,
4751 &locus_at_end_of_parsing);
4753 /* We're done with the splay-tree now. */
4754 if (priority_info_map)
4755 splay_tree_delete (priority_info_map);
4757 /* Generate any missing aliases. */
4758 maybe_apply_pending_pragma_weaks ();
4760 /* We're done with static constructors, so we can go back to "C++"
4761 linkage now. */
4762 pop_lang_context ();
4764 if (flag_vtable_verify)
4766 vtv_recover_class_info ();
4767 vtv_compute_class_hierarchy_transitive_closure ();
4768 vtv_build_vtable_verify_fndecl ();
4771 perform_deferred_noexcept_checks ();
4773 finish_repo ();
4774 fini_constexpr ();
4776 /* The entire file is now complete. If requested, dump everything
4777 to a file. */
4778 dump_tu ();
4780 if (flag_detailed_statistics)
4782 dump_tree_statistics ();
4783 dump_time_statistics ();
4786 timevar_stop (TV_PHASE_DEFERRED);
4787 timevar_start (TV_PHASE_PARSING);
4789 /* Indicate that we're done with front end processing. */
4790 at_eof = 2;
4793 /* Perform any post compilation-proper cleanups for the C++ front-end.
4794 This should really go away. No front-end should need to do
4795 anything past the compilation process. */
4797 void
4798 cxx_post_compilation_parsing_cleanups (void)
4800 timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS);
4802 if (flag_vtable_verify)
4804 /* Generate the special constructor initialization function that
4805 calls __VLTRegisterPairs, and give it a very high
4806 initialization priority. This must be done after
4807 finalize_compilation_unit so that we have accurate
4808 information about which vtable will actually be emitted. */
4809 vtv_generate_init_routine ();
4812 input_location = locus_at_end_of_parsing;
4814 if (flag_checking)
4815 validate_conversion_obstack ();
4817 timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS);
4820 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4821 function to call in parse-tree form; it has not yet been
4822 semantically analyzed. ARGS are the arguments to the function.
4823 They have already been semantically analyzed. This may change
4824 ARGS. */
4826 tree
4827 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4828 tsubst_flags_t complain)
4830 tree orig_fn;
4831 vec<tree, va_gc> *orig_args = NULL;
4832 tree expr;
4833 tree object;
4835 orig_fn = fn;
4836 object = TREE_OPERAND (fn, 0);
4838 if (processing_template_decl)
4840 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4841 || TREE_CODE (fn) == MEMBER_REF);
4842 if (type_dependent_expression_p (fn)
4843 || any_type_dependent_arguments_p (*args))
4844 return build_min_nt_call_vec (fn, *args);
4846 orig_args = make_tree_vector_copy (*args);
4848 /* Transform the arguments and add the implicit "this"
4849 parameter. That must be done before the FN is transformed
4850 because we depend on the form of FN. */
4851 make_args_non_dependent (*args);
4852 object = build_non_dependent_expr (object);
4853 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4855 if (TREE_CODE (fn) == DOTSTAR_EXPR)
4856 object = cp_build_addr_expr (object, complain);
4857 vec_safe_insert (*args, 0, object);
4859 /* Now that the arguments are done, transform FN. */
4860 fn = build_non_dependent_expr (fn);
4863 /* A qualified name corresponding to a bound pointer-to-member is
4864 represented as an OFFSET_REF:
4866 struct B { void g(); };
4867 void (B::*p)();
4868 void B::g() { (this->*p)(); } */
4869 if (TREE_CODE (fn) == OFFSET_REF)
4871 tree object_addr = cp_build_addr_expr (object, complain);
4872 fn = TREE_OPERAND (fn, 1);
4873 fn = get_member_function_from_ptrfunc (&object_addr, fn,
4874 complain);
4875 vec_safe_insert (*args, 0, object_addr);
4878 if (CLASS_TYPE_P (TREE_TYPE (fn)))
4879 expr = build_op_call (fn, args, complain);
4880 else
4881 expr = cp_build_function_call_vec (fn, args, complain);
4882 if (processing_template_decl && expr != error_mark_node)
4883 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
4885 if (orig_args != NULL)
4886 release_tree_vector (orig_args);
4888 return expr;
4892 void
4893 check_default_args (tree x)
4895 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4896 bool saw_def = false;
4897 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4898 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4900 if (TREE_PURPOSE (arg))
4901 saw_def = true;
4902 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
4904 error ("default argument missing for parameter %P of %q+#D", i, x);
4905 TREE_PURPOSE (arg) = error_mark_node;
4910 /* Return true if function DECL can be inlined. This is used to force
4911 instantiation of methods that might be interesting for inlining. */
4912 bool
4913 possibly_inlined_p (tree decl)
4915 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4916 if (DECL_UNINLINABLE (decl))
4917 return false;
4918 if (!optimize)
4919 return DECL_DECLARED_INLINE_P (decl);
4920 /* When optimizing, we might inline everything when flatten
4921 attribute or heuristics inlining for size or autoinlining
4922 is used. */
4923 return true;
4926 /* Normally, we can wait until instantiation-time to synthesize DECL.
4927 However, if DECL is a static data member initialized with a constant
4928 or a constexpr function, we need it right now because a reference to
4929 such a data member or a call to such function is not value-dependent.
4930 For a function that uses auto in the return type, we need to instantiate
4931 it to find out its type. For OpenMP user defined reductions, we need
4932 them instantiated for reduction clauses which inline them by hand
4933 directly. */
4935 static void
4936 maybe_instantiate_decl (tree decl)
4938 if (DECL_LANG_SPECIFIC (decl)
4939 && DECL_TEMPLATE_INFO (decl)
4940 && (decl_maybe_constant_var_p (decl)
4941 || (TREE_CODE (decl) == FUNCTION_DECL
4942 && DECL_OMP_DECLARE_REDUCTION_P (decl))
4943 || undeduced_auto_decl (decl))
4944 && !DECL_DECLARED_CONCEPT_P (decl)
4945 && !uses_template_parms (DECL_TI_ARGS (decl)))
4947 /* Instantiating a function will result in garbage collection. We
4948 must treat this situation as if we were within the body of a
4949 function so as to avoid collecting live data only referenced from
4950 the stack (such as overload resolution candidates). */
4951 ++function_depth;
4952 instantiate_decl (decl, /*defer_ok=*/false,
4953 /*expl_inst_class_mem_p=*/false);
4954 --function_depth;
4958 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
4959 If DECL is a specialization or implicitly declared class member,
4960 generate the actual definition. Return false if something goes
4961 wrong, true otherwise. */
4963 bool
4964 mark_used (tree decl, tsubst_flags_t complain)
4966 /* If DECL is a BASELINK for a single function, then treat it just
4967 like the DECL for the function. Otherwise, if the BASELINK is
4968 for an overloaded function, we don't know which function was
4969 actually used until after overload resolution. */
4970 if (BASELINK_P (decl))
4972 decl = BASELINK_FUNCTIONS (decl);
4973 if (really_overloaded_fn (decl))
4974 return true;
4975 decl = OVL_FIRST (decl);
4978 /* Set TREE_USED for the benefit of -Wunused. */
4979 TREE_USED (decl) = 1;
4980 /* And for structured bindings also the underlying decl. */
4981 if (DECL_DECOMPOSITION_P (decl) && DECL_DECOMP_BASE (decl))
4982 TREE_USED (DECL_DECOMP_BASE (decl)) = 1;
4984 if (TREE_CODE (decl) == TEMPLATE_DECL)
4985 return true;
4987 if (DECL_CLONED_FUNCTION_P (decl))
4988 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4990 /* Mark enumeration types as used. */
4991 if (TREE_CODE (decl) == CONST_DECL)
4992 used_types_insert (DECL_CONTEXT (decl));
4994 if (TREE_CODE (decl) == FUNCTION_DECL)
4995 maybe_instantiate_noexcept (decl);
4997 if (TREE_CODE (decl) == FUNCTION_DECL
4998 && DECL_DELETED_FN (decl))
5000 if (DECL_ARTIFICIAL (decl))
5002 if (DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
5003 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
5005 /* We mark a lambda conversion op as deleted if we can't
5006 generate it properly; see maybe_add_lambda_conv_op. */
5007 sorry ("converting lambda which uses %<...%> to "
5008 "function pointer");
5009 return false;
5012 if (complain & tf_error)
5014 error ("use of deleted function %qD", decl);
5015 if (!maybe_explain_implicit_delete (decl))
5016 inform (DECL_SOURCE_LOCATION (decl), "declared here");
5018 return false;
5021 if (TREE_DEPRECATED (decl) && (complain & tf_warning)
5022 && deprecated_state != DEPRECATED_SUPPRESS)
5023 warn_deprecated_use (decl, NULL_TREE);
5025 /* We can only check DECL_ODR_USED on variables or functions with
5026 DECL_LANG_SPECIFIC set, and these are also the only decls that we
5027 might need special handling for. */
5028 if (!VAR_OR_FUNCTION_DECL_P (decl)
5029 || DECL_LANG_SPECIFIC (decl) == NULL
5030 || DECL_THUNK_P (decl))
5032 if (!processing_template_decl
5033 && !require_deduced_type (decl, complain))
5034 return false;
5035 return true;
5038 /* We only want to do this processing once. We don't need to keep trying
5039 to instantiate inline templates, because unit-at-a-time will make sure
5040 we get them compiled before functions that want to inline them. */
5041 if (DECL_ODR_USED (decl))
5042 return true;
5044 /* Normally, we can wait until instantiation-time to synthesize DECL.
5045 However, if DECL is a static data member initialized with a constant
5046 or a constexpr function, we need it right now because a reference to
5047 such a data member or a call to such function is not value-dependent.
5048 For a function that uses auto in the return type, we need to instantiate
5049 it to find out its type. For OpenMP user defined reductions, we need
5050 them instantiated for reduction clauses which inline them by hand
5051 directly. */
5052 maybe_instantiate_decl (decl);
5054 if (processing_template_decl || in_template_function ())
5055 return true;
5057 /* Check this too in case we're within instantiate_non_dependent_expr. */
5058 if (DECL_TEMPLATE_INFO (decl)
5059 && uses_template_parms (DECL_TI_ARGS (decl)))
5060 return true;
5062 if (!require_deduced_type (decl, complain))
5063 return false;
5065 if (builtin_pack_fn_p (decl))
5067 error ("use of built-in parameter pack %qD outside of a template",
5068 DECL_NAME (decl));
5069 return false;
5072 /* If we don't need a value, then we don't need to synthesize DECL. */
5073 if (cp_unevaluated_operand || in_discarded_stmt)
5074 return true;
5076 DECL_ODR_USED (decl) = 1;
5077 if (DECL_CLONED_FUNCTION_P (decl))
5078 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5080 /* DR 757: A type without linkage shall not be used as the type of a
5081 variable or function with linkage, unless
5082 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5083 o the variable or function is not used (3.2 [basic.def.odr]) or is
5084 defined in the same translation unit. */
5085 if (cxx_dialect > cxx98
5086 && decl_linkage (decl) != lk_none
5087 && !DECL_EXTERN_C_P (decl)
5088 && !DECL_ARTIFICIAL (decl)
5089 && !decl_defined_p (decl)
5090 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
5092 if (is_local_extern (decl))
5093 /* There's no way to define a local extern, and adding it to
5094 the vector interferes with GC, so give an error now. */
5095 no_linkage_error (decl);
5096 else
5097 vec_safe_push (no_linkage_decls, decl);
5100 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
5101 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
5102 /* Remember it, so we can check it was defined. */
5103 note_vague_linkage_fn (decl);
5105 /* Is it a synthesized method that needs to be synthesized? */
5106 if (TREE_CODE (decl) == FUNCTION_DECL
5107 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5108 && DECL_DEFAULTED_FN (decl)
5109 /* A function defaulted outside the class is synthesized either by
5110 cp_finish_decl or instantiate_decl. */
5111 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
5112 && ! DECL_INITIAL (decl))
5114 /* Defer virtual destructors so that thunks get the right
5115 linkage. */
5116 if (DECL_VIRTUAL_P (decl) && !at_eof)
5118 note_vague_linkage_fn (decl);
5119 return true;
5122 /* Remember the current location for a function we will end up
5123 synthesizing. Then we can inform the user where it was
5124 required in the case of error. */
5125 DECL_SOURCE_LOCATION (decl) = input_location;
5127 /* Synthesizing an implicitly defined member function will result in
5128 garbage collection. We must treat this situation as if we were
5129 within the body of a function so as to avoid collecting live data
5130 on the stack (such as overload resolution candidates).
5132 We could just let cp_write_global_declarations handle synthesizing
5133 this function by adding it to deferred_fns, but doing
5134 it at the use site produces better error messages. */
5135 ++function_depth;
5136 synthesize_method (decl);
5137 --function_depth;
5138 /* If this is a synthesized method we don't need to
5139 do the instantiation test below. */
5141 else if (VAR_OR_FUNCTION_DECL_P (decl)
5142 && DECL_TEMPLATE_INFO (decl)
5143 && !DECL_DECLARED_CONCEPT_P (decl)
5144 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5145 || always_instantiate_p (decl)))
5146 /* If this is a function or variable that is an instance of some
5147 template, we now know that we will need to actually do the
5148 instantiation. We check that DECL is not an explicit
5149 instantiation because that is not checked in instantiate_decl.
5151 We put off instantiating functions in order to improve compile
5152 times. Maintaining a stack of active functions is expensive,
5153 and the inliner knows to instantiate any functions it might
5154 need. Therefore, we always try to defer instantiation. */
5156 ++function_depth;
5157 instantiate_decl (decl, /*defer_ok=*/true,
5158 /*expl_inst_class_mem_p=*/false);
5159 --function_depth;
5162 return true;
5165 bool
5166 mark_used (tree decl)
5168 return mark_used (decl, tf_warning_or_error);
5171 tree
5172 vtv_start_verification_constructor_init_function (void)
5174 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5177 tree
5178 vtv_finish_verification_constructor_init_function (tree function_body)
5180 tree fn;
5182 finish_compound_stmt (function_body);
5183 fn = finish_function (0);
5184 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5185 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5187 return fn;
5190 #include "gt-cp-decl2.h"