Store VECTOR_CST_NELTS directly in tree_node
[official-gcc.git] / gcc / cp / decl2.c
blob2bde588847e9ed1a750e9165e181159bd049fc4c
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2017 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* Process declarations and symbol lookup for C++ front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "memmodel.h"
33 #include "target.h"
34 #include "cp-tree.h"
35 #include "c-family/c-common.h"
36 #include "timevar.h"
37 #include "stringpool.h"
38 #include "cgraph.h"
39 #include "varasm.h"
40 #include "attribs.h"
41 #include "stor-layout.h"
42 #include "calls.h"
43 #include "decl.h"
44 #include "toplev.h"
45 #include "c-family/c-objc.h"
46 #include "c-family/c-pragma.h"
47 #include "dumpfile.h"
48 #include "intl.h"
49 #include "c-family/c-ada-spec.h"
50 #include "asan.h"
52 /* Id for dumping the raw trees. */
53 int raw_dump_id;
55 extern cpp_reader *parse_in;
57 /* This structure contains information about the initializations
58 and/or destructions required for a particular priority level. */
59 typedef struct priority_info_s {
60 /* Nonzero if there have been any initializations at this priority
61 throughout the translation unit. */
62 int initializations_p;
63 /* Nonzero if there have been any destructions at this priority
64 throughout the translation unit. */
65 int destructions_p;
66 } *priority_info;
68 static void mark_vtable_entries (tree);
69 static bool maybe_emit_vtables (tree);
70 static tree start_objects (int, int);
71 static void finish_objects (int, int, tree);
72 static tree start_static_storage_duration_function (unsigned);
73 static void finish_static_storage_duration_function (tree);
74 static priority_info get_priority_info (int);
75 static void do_static_initialization_or_destruction (tree, bool);
76 static void one_static_initialization_or_destruction (tree, tree, bool);
77 static void generate_ctor_or_dtor_function (bool, int, location_t *);
78 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
79 void *);
80 static tree prune_vars_needing_no_initialization (tree *);
81 static void write_out_vars (tree);
82 static void import_export_class (tree);
83 static tree get_guard_bits (tree);
84 static void determine_visibility_from_class (tree, tree);
85 static bool determine_hidden_inline (tree);
86 static void maybe_instantiate_decl (tree);
88 /* A list of static class variables. This is needed, because a
89 static class variable can be declared inside the class without
90 an initializer, and then initialized, statically, outside the class. */
91 static GTY(()) vec<tree, va_gc> *pending_statics;
93 /* A list of functions which were declared inline, but which we
94 may need to emit outline anyway. */
95 static GTY(()) vec<tree, va_gc> *deferred_fns;
97 /* A list of decls that use types with no linkage, which we need to make
98 sure are defined. */
99 static GTY(()) vec<tree, va_gc> *no_linkage_decls;
101 /* A vector of alternating decls and identifiers, where the latter
102 is to be an alias for the former if the former is defined. */
103 static GTY(()) vec<tree, va_gc> *mangling_aliases;
105 /* Nonzero if we're done parsing and into end-of-file activities. */
107 int at_eof;
109 /* True if note_mangling_alias should enqueue mangling aliases for
110 later generation, rather than emitting them right away. */
112 bool defer_mangling_aliases = true;
115 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
116 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
117 that apply to the function). */
119 tree
120 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
121 cp_ref_qualifier rqual)
123 tree raises;
124 tree attrs;
125 int type_quals;
126 bool late_return_type_p;
128 if (fntype == error_mark_node || ctype == error_mark_node)
129 return error_mark_node;
131 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
132 || TREE_CODE (fntype) == METHOD_TYPE);
134 type_quals = quals & ~TYPE_QUAL_RESTRICT;
135 ctype = cp_build_qualified_type (ctype, type_quals);
136 raises = TYPE_RAISES_EXCEPTIONS (fntype);
137 attrs = TYPE_ATTRIBUTES (fntype);
138 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
139 fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
140 (TREE_CODE (fntype) == METHOD_TYPE
141 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
142 : TYPE_ARG_TYPES (fntype)));
143 if (attrs)
144 fntype = cp_build_type_attribute_variant (fntype, attrs);
145 if (rqual)
146 fntype = build_ref_qualified_type (fntype, rqual);
147 if (raises)
148 fntype = build_exception_variant (fntype, raises);
149 if (late_return_type_p)
150 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
152 return fntype;
155 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
156 return type changed to NEW_RET. */
158 tree
159 change_return_type (tree new_ret, tree fntype)
161 tree newtype;
162 tree args = TYPE_ARG_TYPES (fntype);
163 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
164 tree attrs = TYPE_ATTRIBUTES (fntype);
165 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
167 if (new_ret == error_mark_node)
168 return fntype;
170 if (same_type_p (new_ret, TREE_TYPE (fntype)))
171 return fntype;
173 if (TREE_CODE (fntype) == FUNCTION_TYPE)
175 newtype = build_function_type (new_ret, args);
176 newtype = apply_memfn_quals (newtype,
177 type_memfn_quals (fntype),
178 type_memfn_rqual (fntype));
180 else
181 newtype = build_method_type_directly
182 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
183 if (FUNCTION_REF_QUALIFIED (fntype))
184 newtype = build_ref_qualified_type (newtype, type_memfn_rqual (fntype));
185 if (raises)
186 newtype = build_exception_variant (newtype, raises);
187 if (attrs)
188 newtype = cp_build_type_attribute_variant (newtype, attrs);
189 if (late_return_type_p)
190 TYPE_HAS_LATE_RETURN_TYPE (newtype) = 1;
192 return newtype;
195 /* Build a PARM_DECL of FN with NAME and TYPE, and set DECL_ARG_TYPE
196 appropriately. */
198 tree
199 cp_build_parm_decl (tree fn, tree name, tree type)
201 tree parm = build_decl (input_location,
202 PARM_DECL, name, type);
203 DECL_CONTEXT (parm) = fn;
205 /* DECL_ARG_TYPE is only used by the back end and the back end never
206 sees templates. */
207 if (!processing_template_decl)
208 DECL_ARG_TYPE (parm) = type_passed_as (type);
210 return parm;
213 /* Returns a PARM_DECL of FN for a parameter of the indicated TYPE, with the
214 indicated NAME. */
216 tree
217 build_artificial_parm (tree fn, tree name, tree type)
219 tree parm = cp_build_parm_decl (fn, name, type);
220 DECL_ARTIFICIAL (parm) = 1;
221 /* All our artificial parms are implicitly `const'; they cannot be
222 assigned to. */
223 TREE_READONLY (parm) = 1;
224 return parm;
227 /* Constructors for types with virtual baseclasses need an "in-charge" flag
228 saying whether this constructor is responsible for initialization of
229 virtual baseclasses or not. All destructors also need this "in-charge"
230 flag, which additionally determines whether or not the destructor should
231 free the memory for the object.
233 This function adds the "in-charge" flag to member function FN if
234 appropriate. It is called from grokclassfn and tsubst.
235 FN must be either a constructor or destructor.
237 The in-charge flag follows the 'this' parameter, and is followed by the
238 VTT parm (if any), then the user-written parms. */
240 void
241 maybe_retrofit_in_chrg (tree fn)
243 tree basetype, arg_types, parms, parm, fntype;
245 /* If we've already add the in-charge parameter don't do it again. */
246 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
247 return;
249 /* When processing templates we can't know, in general, whether or
250 not we're going to have virtual baseclasses. */
251 if (processing_template_decl)
252 return;
254 /* We don't need an in-charge parameter for constructors that don't
255 have virtual bases. */
256 if (DECL_CONSTRUCTOR_P (fn)
257 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
258 return;
260 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
261 basetype = TREE_TYPE (TREE_VALUE (arg_types));
262 arg_types = TREE_CHAIN (arg_types);
264 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
266 /* If this is a subobject constructor or destructor, our caller will
267 pass us a pointer to our VTT. */
268 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
270 parm = build_artificial_parm (fn, vtt_parm_identifier, vtt_parm_type);
272 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
273 DECL_CHAIN (parm) = parms;
274 parms = parm;
276 /* ...and then to TYPE_ARG_TYPES. */
277 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
279 DECL_HAS_VTT_PARM_P (fn) = 1;
282 /* Then add the in-charge parm (before the VTT parm). */
283 parm = build_artificial_parm (fn, in_charge_identifier, integer_type_node);
284 DECL_CHAIN (parm) = parms;
285 parms = parm;
286 arg_types = hash_tree_chain (integer_type_node, arg_types);
288 /* Insert our new parameter(s) into the list. */
289 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
291 /* And rebuild the function type. */
292 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
293 arg_types);
294 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
295 fntype = build_exception_variant (fntype,
296 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
297 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
298 fntype = (cp_build_type_attribute_variant
299 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
300 TREE_TYPE (fn) = fntype;
302 /* Now we've got the in-charge parameter. */
303 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
306 /* Classes overload their constituent function names automatically.
307 When a function name is declared in a record structure,
308 its name is changed to it overloaded name. Since names for
309 constructors and destructors can conflict, we place a leading
310 '$' for destructors.
312 CNAME is the name of the class we are grokking for.
314 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
316 FLAGS contains bits saying what's special about today's
317 arguments. DTOR_FLAG == DESTRUCTOR.
319 If FUNCTION is a destructor, then we must add the `auto-delete' field
320 as a second parameter. There is some hair associated with the fact
321 that we must "declare" this variable in the manner consistent with the
322 way the rest of the arguments were declared.
324 QUALS are the qualifiers for the this pointer. */
326 void
327 grokclassfn (tree ctype, tree function, enum overload_flags flags)
329 tree fn_name = DECL_NAME (function);
331 /* Even within an `extern "C"' block, members get C++ linkage. See
332 [dcl.link] for details. */
333 SET_DECL_LANGUAGE (function, lang_cplusplus);
335 if (fn_name == NULL_TREE)
337 error ("name missing for member function");
338 fn_name = get_identifier ("<anonymous>");
339 DECL_NAME (function) = fn_name;
342 DECL_CONTEXT (function) = ctype;
344 if (flags == DTOR_FLAG)
345 DECL_CXX_DESTRUCTOR_P (function) = 1;
347 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
348 maybe_retrofit_in_chrg (function);
351 /* Create an ARRAY_REF, checking for the user doing things backwards
352 along the way. DECLTYPE_P is for N3276, as in the parser. */
354 tree
355 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
356 bool decltype_p)
358 tree type;
359 tree expr;
360 tree orig_array_expr = array_expr;
361 tree orig_index_exp = index_exp;
362 tree overload = NULL_TREE;
364 if (error_operand_p (array_expr) || error_operand_p (index_exp))
365 return error_mark_node;
367 if (processing_template_decl)
369 if (type_dependent_expression_p (array_expr)
370 || type_dependent_expression_p (index_exp))
371 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
372 NULL_TREE, NULL_TREE);
373 array_expr = build_non_dependent_expr (array_expr);
374 index_exp = build_non_dependent_expr (index_exp);
377 type = TREE_TYPE (array_expr);
378 gcc_assert (type);
379 type = non_reference (type);
381 /* If they have an `operator[]', use that. */
382 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
384 tsubst_flags_t complain = tf_warning_or_error;
385 if (decltype_p)
386 complain |= tf_decltype;
387 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
388 index_exp, NULL_TREE, &overload, complain);
390 else
392 tree p1, p2, i1, i2;
394 /* Otherwise, create an ARRAY_REF for a pointer or array type.
395 It is a little-known fact that, if `a' is an array and `i' is
396 an int, you can write `i[a]', which means the same thing as
397 `a[i]'. */
398 if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
399 p1 = array_expr;
400 else
401 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
403 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
404 p2 = index_exp;
405 else
406 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
408 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
409 false);
410 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
411 false);
413 if ((p1 && i2) && (i1 && p2))
414 error ("ambiguous conversion for array subscript");
416 if (p1 && i2)
417 array_expr = p1, index_exp = i2;
418 else if (i1 && p2)
419 array_expr = p2, index_exp = i1;
420 else
422 error ("invalid types %<%T[%T]%> for array subscript",
423 type, TREE_TYPE (index_exp));
424 return error_mark_node;
427 if (array_expr == error_mark_node || index_exp == error_mark_node)
428 error ("ambiguous conversion for array subscript");
430 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 = get_class_binding (ctype, DECL_NAME (function));
611 for (ovl_iterator iter (fns); !matched && iter; ++iter)
613 tree fndecl = *iter;
615 /* A member template definition only matches a member template
616 declaration. */
617 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
618 continue;
620 if (!DECL_DECLARES_FUNCTION_P (fndecl))
621 continue;
623 tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
624 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
626 /* We cannot simply call decls_match because this doesn't work
627 for static member functions that are pretending to be
628 methods, and because the name may have been changed by
629 asm("new_name"). */
631 /* Get rid of the this parameter on functions that become
632 static. */
633 if (DECL_STATIC_FUNCTION_P (fndecl)
634 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
635 p1 = TREE_CHAIN (p1);
637 /* ref-qualifier or absence of same must match. */
638 if (type_memfn_rqual (TREE_TYPE (function))
639 != type_memfn_rqual (TREE_TYPE (fndecl)))
640 continue;
642 // Include constraints in the match.
643 tree c1 = get_constraints (function);
644 tree c2 = get_constraints (fndecl);
646 /* While finding a match, same types and params are not enough
647 if the function is versioned. Also check version ("target")
648 attributes. */
649 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
650 TREE_TYPE (TREE_TYPE (fndecl)))
651 && compparms (p1, p2)
652 && !targetm.target_option.function_versions (function, fndecl)
653 && (!is_template
654 || comp_template_parms (template_parms,
655 DECL_TEMPLATE_PARMS (fndecl)))
656 && equivalent_constraints (c1, c2)
657 && (DECL_TEMPLATE_SPECIALIZATION (function)
658 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
659 && (!DECL_TEMPLATE_SPECIALIZATION (function)
660 || (DECL_TI_TEMPLATE (function) == DECL_TI_TEMPLATE (fndecl))))
661 matched = fndecl;
664 if (!matched)
666 if (!COMPLETE_TYPE_P (ctype))
667 cxx_incomplete_type_error (function, ctype);
668 else
670 if (DECL_CONV_FN_P (function))
671 fns = get_class_binding (ctype, conv_op_identifier);
673 error_at (DECL_SOURCE_LOCATION (function),
674 "no declaration matches %q#D", function);
675 if (fns)
676 print_candidates (fns);
677 else if (DECL_CONV_FN_P (function))
678 inform (DECL_SOURCE_LOCATION (function),
679 "no conversion operators declared");
680 else
681 inform (DECL_SOURCE_LOCATION (function),
682 "no functions named %qD", function);
683 inform (DECL_SOURCE_LOCATION (TYPE_NAME (ctype)),
684 "%#qT defined here", ctype);
686 matched = error_mark_node;
689 if (pushed_scope)
690 pop_scope (pushed_scope);
692 return matched;
695 /* DECL is a function with vague linkage. Remember it so that at the
696 end of the translation unit we can decide whether or not to emit
697 it. */
699 void
700 note_vague_linkage_fn (tree decl)
702 DECL_DEFER_OUTPUT (decl) = 1;
703 vec_safe_push (deferred_fns, decl);
706 /* As above, but for variable template instantiations. */
708 void
709 note_variable_template_instantiation (tree decl)
711 vec_safe_push (pending_statics, decl);
714 /* We have just processed the DECL, which is a static data member.
715 The other parameters are as for cp_finish_decl. */
717 void
718 finish_static_data_member_decl (tree decl,
719 tree init, bool init_const_expr_p,
720 tree asmspec_tree,
721 int flags)
723 DECL_CONTEXT (decl) = current_class_type;
725 /* We cannot call pushdecl here, because that would fill in the
726 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
727 the right thing, namely, to put this decl out straight away. */
729 if (! processing_template_decl)
730 vec_safe_push (pending_statics, decl);
732 if (LOCAL_CLASS_P (current_class_type)
733 /* We already complained about the template definition. */
734 && !DECL_TEMPLATE_INSTANTIATION (decl))
735 permerror (input_location, "local class %q#T shall not have static data member %q#D",
736 current_class_type, decl);
737 else
738 for (tree t = current_class_type; TYPE_P (t);
739 t = CP_TYPE_CONTEXT (t))
740 if (TYPE_UNNAMED_P (t))
742 if (permerror (DECL_SOURCE_LOCATION (decl),
743 "static data member %qD in unnamed class", decl))
744 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
745 "unnamed class defined here");
746 break;
749 DECL_IN_AGGR_P (decl) = 1;
751 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
752 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
753 SET_VAR_HAD_UNKNOWN_BOUND (decl);
755 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
758 /* DECLARATOR and DECLSPECS correspond to a class member. The other
759 parameters are as for cp_finish_decl. Return the DECL for the
760 class member declared. */
762 tree
763 grokfield (const cp_declarator *declarator,
764 cp_decl_specifier_seq *declspecs,
765 tree init, bool init_const_expr_p,
766 tree asmspec_tree,
767 tree attrlist)
769 tree value;
770 const char *asmspec = 0;
771 int flags;
772 tree name;
774 if (init
775 && TREE_CODE (init) == TREE_LIST
776 && TREE_VALUE (init) == error_mark_node
777 && TREE_CHAIN (init) == NULL_TREE)
778 init = NULL_TREE;
780 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
781 if (! value || value == error_mark_node)
782 /* friend or constructor went bad. */
783 return error_mark_node;
784 if (TREE_TYPE (value) == error_mark_node)
785 return value;
787 if (TREE_CODE (value) == TYPE_DECL && init)
789 error ("typedef %qD is initialized (use decltype instead)", value);
790 init = NULL_TREE;
793 /* Pass friendly classes back. */
794 if (value == void_type_node)
795 return value;
798 name = DECL_NAME (value);
800 if (name != NULL_TREE)
802 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
804 error ("explicit template argument list not allowed");
805 return error_mark_node;
808 if (IDENTIFIER_POINTER (name)[0] == '_'
809 && id_equal (name, "_vptr"))
810 error ("member %qD conflicts with virtual function table field name",
811 value);
814 /* Stash away type declarations. */
815 if (TREE_CODE (value) == TYPE_DECL)
817 DECL_NONLOCAL (value) = 1;
818 DECL_CONTEXT (value) = current_class_type;
820 if (attrlist)
822 int attrflags = 0;
824 /* If this is a typedef that names the class for linkage purposes
825 (7.1.3p8), apply any attributes directly to the type. */
826 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
827 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
828 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
830 cplus_decl_attributes (&value, attrlist, attrflags);
833 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
834 && TREE_TYPE (value) != error_mark_node
835 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
836 set_underlying_type (value);
838 /* It's important that push_template_decl below follows
839 set_underlying_type above so that the created template
840 carries the properly set type of VALUE. */
841 if (processing_template_decl)
842 value = push_template_decl (value);
844 record_locally_defined_typedef (value);
845 return value;
848 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
850 if (!friendp && DECL_IN_AGGR_P (value))
852 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
853 return void_type_node;
856 if (asmspec_tree && asmspec_tree != error_mark_node)
857 asmspec = TREE_STRING_POINTER (asmspec_tree);
859 if (init)
861 if (TREE_CODE (value) == FUNCTION_DECL)
863 if (init == ridpointers[(int)RID_DELETE])
865 DECL_DELETED_FN (value) = 1;
866 DECL_DECLARED_INLINE_P (value) = 1;
867 DECL_INITIAL (value) = error_mark_node;
869 else if (init == ridpointers[(int)RID_DEFAULT])
871 if (defaultable_fn_check (value))
873 DECL_DEFAULTED_FN (value) = 1;
874 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
875 DECL_DECLARED_INLINE_P (value) = 1;
878 else if (TREE_CODE (init) == DEFAULT_ARG)
879 error ("invalid initializer for member function %qD", value);
880 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
882 if (integer_zerop (init))
883 DECL_PURE_VIRTUAL_P (value) = 1;
884 else if (error_operand_p (init))
885 ; /* An error has already been reported. */
886 else
887 error ("invalid initializer for member function %qD",
888 value);
890 else
892 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
893 if (friendp)
894 error ("initializer specified for friend function %qD",
895 value);
896 else
897 error ("initializer specified for static member function %qD",
898 value);
901 else if (TREE_CODE (value) == FIELD_DECL)
902 /* C++11 NSDMI, keep going. */;
903 else if (!VAR_P (value))
904 gcc_unreachable ();
907 /* Pass friend decls back. */
908 if ((TREE_CODE (value) == FUNCTION_DECL
909 || TREE_CODE (value) == TEMPLATE_DECL)
910 && DECL_CONTEXT (value) != current_class_type)
911 return value;
913 /* Need to set this before push_template_decl. */
914 if (VAR_P (value))
915 DECL_CONTEXT (value) = current_class_type;
917 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
919 value = push_template_decl (value);
920 if (error_operand_p (value))
921 return error_mark_node;
924 if (attrlist)
925 cplus_decl_attributes (&value, attrlist, 0);
927 if (init && DIRECT_LIST_INIT_P (init))
928 flags = LOOKUP_NORMAL;
929 else
930 flags = LOOKUP_IMPLICIT;
932 switch (TREE_CODE (value))
934 case VAR_DECL:
935 finish_static_data_member_decl (value, init, init_const_expr_p,
936 asmspec_tree, flags);
937 return value;
939 case FIELD_DECL:
940 if (asmspec)
941 error ("%<asm%> specifiers are not permitted on non-static data members");
942 if (DECL_INITIAL (value) == error_mark_node)
943 init = error_mark_node;
944 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
945 NULL_TREE, flags);
946 DECL_IN_AGGR_P (value) = 1;
947 return value;
949 case FUNCTION_DECL:
950 if (asmspec)
951 set_user_assembler_name (value, asmspec);
953 cp_finish_decl (value,
954 /*init=*/NULL_TREE,
955 /*init_const_expr_p=*/false,
956 asmspec_tree, flags);
958 /* Pass friends back this way. */
959 if (DECL_FRIEND_P (value))
960 return void_type_node;
962 DECL_IN_AGGR_P (value) = 1;
963 return value;
965 default:
966 gcc_unreachable ();
968 return NULL_TREE;
971 /* Like `grokfield', but for bitfields.
972 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
974 tree
975 grokbitfield (const cp_declarator *declarator,
976 cp_decl_specifier_seq *declspecs, tree width,
977 tree attrlist)
979 tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, &attrlist);
981 if (value == error_mark_node)
982 return NULL_TREE; /* friends went bad. */
983 if (TREE_TYPE (value) == error_mark_node)
984 return value;
986 /* Pass friendly classes back. */
987 if (VOID_TYPE_P (value))
988 return void_type_node;
990 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
991 && (POINTER_TYPE_P (value)
992 || !dependent_type_p (TREE_TYPE (value))))
994 error ("bit-field %qD with non-integral type", value);
995 return error_mark_node;
998 if (TREE_CODE (value) == TYPE_DECL)
1000 error ("cannot declare %qD to be a bit-field type", value);
1001 return NULL_TREE;
1004 /* Usually, finish_struct_1 catches bitfields with invalid types.
1005 But, in the case of bitfields with function type, we confuse
1006 ourselves into thinking they are member functions, so we must
1007 check here. */
1008 if (TREE_CODE (value) == FUNCTION_DECL)
1010 error ("cannot declare bit-field %qD with function type",
1011 DECL_NAME (value));
1012 return NULL_TREE;
1015 if (width && TYPE_WARN_IF_NOT_ALIGN (TREE_TYPE (value)))
1017 error ("cannot declare bit-field %qD with %<warn_if_not_aligned%> type",
1018 DECL_NAME (value));
1019 return NULL_TREE;
1022 if (DECL_IN_AGGR_P (value))
1024 error ("%qD is already defined in the class %qT", value,
1025 DECL_CONTEXT (value));
1026 return void_type_node;
1029 if (TREE_STATIC (value))
1031 error ("static member %qD cannot be a bit-field", value);
1032 return NULL_TREE;
1034 cp_finish_decl (value, NULL_TREE, false, NULL_TREE, 0);
1036 if (width != error_mark_node)
1038 /* The width must be an integer type. */
1039 if (!type_dependent_expression_p (width)
1040 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1041 error ("width of bit-field %qD has non-integral type %qT", value,
1042 TREE_TYPE (width));
1043 else
1045 DECL_INITIAL (value) = width;
1046 SET_DECL_C_BIT_FIELD (value);
1050 DECL_IN_AGGR_P (value) = 1;
1052 if (attrlist)
1053 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1055 return value;
1059 /* Returns true iff ATTR is an attribute which needs to be applied at
1060 instantiation time rather than template definition time. */
1062 static bool
1063 is_late_template_attribute (tree attr, tree decl)
1065 tree name = get_attribute_name (attr);
1066 tree args = TREE_VALUE (attr);
1067 const struct attribute_spec *spec = lookup_attribute_spec (name);
1068 tree arg;
1070 if (!spec)
1071 /* Unknown attribute. */
1072 return false;
1074 /* Attribute weak handling wants to write out assembly right away. */
1075 if (is_attribute_p ("weak", name))
1076 return true;
1078 /* Attributes used and unused are applied directly, as they appertain to
1079 decls. */
1080 if (is_attribute_p ("unused", name)
1081 || is_attribute_p ("used", name))
1082 return false;
1084 /* Attribute tls_model wants to modify the symtab. */
1085 if (is_attribute_p ("tls_model", name))
1086 return true;
1088 /* #pragma omp declare simd attribute needs to be always deferred. */
1089 if (flag_openmp
1090 && is_attribute_p ("omp declare simd", name))
1091 return true;
1093 /* An attribute pack is clearly dependent. */
1094 if (args && PACK_EXPANSION_P (args))
1095 return true;
1097 /* If any of the arguments are dependent expressions, we can't evaluate
1098 the attribute until instantiation time. */
1099 for (arg = args; arg; arg = TREE_CHAIN (arg))
1101 tree t = TREE_VALUE (arg);
1103 /* If the first attribute argument is an identifier, only consider
1104 second and following arguments. Attributes like mode, format,
1105 cleanup and several target specific attributes aren't late
1106 just because they have an IDENTIFIER_NODE as first argument. */
1107 if (arg == args && attribute_takes_identifier_p (name)
1108 && identifier_p (t))
1109 continue;
1111 if (value_dependent_expression_p (t)
1112 || type_dependent_expression_p (t))
1113 return true;
1116 if (TREE_CODE (decl) == TYPE_DECL
1117 || TYPE_P (decl)
1118 || spec->type_required)
1120 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1122 /* We can't apply any attributes to a completely unknown type until
1123 instantiation time. */
1124 enum tree_code code = TREE_CODE (type);
1125 if (code == TEMPLATE_TYPE_PARM
1126 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1127 || code == TYPENAME_TYPE)
1128 return true;
1129 /* Also defer most attributes on dependent types. This is not
1130 necessary in all cases, but is the better default. */
1131 else if (dependent_type_p (type)
1132 /* But some attributes specifically apply to templates. */
1133 && !is_attribute_p ("abi_tag", name)
1134 && !is_attribute_p ("deprecated", name)
1135 && !is_attribute_p ("visibility", name))
1136 return true;
1137 else
1138 return false;
1140 else
1141 return false;
1144 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1145 applied at instantiation time and return them. If IS_DEPENDENT is true,
1146 the declaration itself is dependent, so all attributes should be applied
1147 at instantiation time. */
1149 static tree
1150 splice_template_attributes (tree *attr_p, tree decl)
1152 tree *p = attr_p;
1153 tree late_attrs = NULL_TREE;
1154 tree *q = &late_attrs;
1156 if (!p)
1157 return NULL_TREE;
1159 for (; *p; )
1161 if (is_late_template_attribute (*p, decl))
1163 ATTR_IS_DEPENDENT (*p) = 1;
1164 *q = *p;
1165 *p = TREE_CHAIN (*p);
1166 q = &TREE_CHAIN (*q);
1167 *q = NULL_TREE;
1169 else
1170 p = &TREE_CHAIN (*p);
1173 return late_attrs;
1176 /* Remove any late attributes from the list in ATTR_P and attach them to
1177 DECL_P. */
1179 static void
1180 save_template_attributes (tree *attr_p, tree *decl_p)
1182 tree *q;
1184 if (attr_p && *attr_p == error_mark_node)
1185 return;
1187 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1188 if (!late_attrs)
1189 return;
1191 if (DECL_P (*decl_p))
1192 q = &DECL_ATTRIBUTES (*decl_p);
1193 else
1194 q = &TYPE_ATTRIBUTES (*decl_p);
1196 tree old_attrs = *q;
1198 /* Merge the late attributes at the beginning with the attribute
1199 list. */
1200 late_attrs = merge_attributes (late_attrs, *q);
1201 *q = late_attrs;
1203 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1205 /* We've added new attributes directly to the main variant, so
1206 now we need to update all of the other variants to include
1207 these new attributes. */
1208 tree variant;
1209 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1210 variant = TYPE_NEXT_VARIANT (variant))
1212 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1213 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1218 /* True if ATTRS contains any dependent attributes that affect type
1219 identity. */
1221 bool
1222 any_dependent_type_attributes_p (tree attrs)
1224 for (tree a = attrs; a; a = TREE_CHAIN (a))
1225 if (ATTR_IS_DEPENDENT (a))
1227 const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (a));
1228 if (as && as->affects_type_identity)
1229 return true;
1231 return false;
1234 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1235 to a typedef which gives a previously unnamed class or enum a name for
1236 linkage purposes. */
1238 bool
1239 attributes_naming_typedef_ok (tree attrs)
1241 for (; attrs; attrs = TREE_CHAIN (attrs))
1243 tree name = get_attribute_name (attrs);
1244 if (is_attribute_p ("vector_size", name))
1245 return false;
1247 return true;
1250 /* Like reconstruct_complex_type, but handle also template trees. */
1252 tree
1253 cp_reconstruct_complex_type (tree type, tree bottom)
1255 tree inner, outer;
1256 bool late_return_type_p = false;
1258 if (TYPE_PTR_P (type))
1260 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1261 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1262 TYPE_REF_CAN_ALIAS_ALL (type));
1264 else if (TREE_CODE (type) == REFERENCE_TYPE)
1266 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1267 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1268 TYPE_REF_CAN_ALIAS_ALL (type));
1270 else if (TREE_CODE (type) == ARRAY_TYPE)
1272 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1273 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1274 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1275 element type qualification will be handled by the recursive
1276 cp_reconstruct_complex_type call and cp_build_qualified_type
1277 for ARRAY_TYPEs changes the element type. */
1278 return outer;
1280 else if (TREE_CODE (type) == FUNCTION_TYPE)
1282 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1283 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1284 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1285 outer = apply_memfn_quals (outer,
1286 type_memfn_quals (type),
1287 type_memfn_rqual (type));
1289 else if (TREE_CODE (type) == METHOD_TYPE)
1291 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1292 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1293 /* The build_method_type_directly() routine prepends 'this' to argument list,
1294 so we must compensate by getting rid of it. */
1295 outer
1296 = build_method_type_directly
1297 (class_of_this_parm (type), inner,
1298 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1300 else if (TREE_CODE (type) == OFFSET_TYPE)
1302 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1303 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1305 else
1306 return bottom;
1308 if (TYPE_ATTRIBUTES (type))
1309 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1310 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1312 if (late_return_type_p)
1313 TYPE_HAS_LATE_RETURN_TYPE (outer) = 1;
1315 return outer;
1318 /* Replaces any constexpr expression that may be into the attributes
1319 arguments with their reduced value. */
1321 static void
1322 cp_check_const_attributes (tree attributes)
1324 if (attributes == error_mark_node)
1325 return;
1327 tree attr;
1328 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1330 tree arg;
1331 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1333 tree expr = TREE_VALUE (arg);
1334 if (EXPR_P (expr))
1335 TREE_VALUE (arg) = maybe_constant_value (expr);
1340 /* Return true if TYPE is an OpenMP mappable type. */
1341 bool
1342 cp_omp_mappable_type (tree type)
1344 /* Mappable type has to be complete. */
1345 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1346 return false;
1347 /* Arrays have mappable type if the elements have mappable type. */
1348 while (TREE_CODE (type) == ARRAY_TYPE)
1349 type = TREE_TYPE (type);
1350 /* A mappable type cannot contain virtual members. */
1351 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1352 return false;
1353 /* All data members must be non-static. */
1354 if (CLASS_TYPE_P (type))
1356 tree field;
1357 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1358 if (VAR_P (field))
1359 return false;
1360 /* All fields must have mappable types. */
1361 else if (TREE_CODE (field) == FIELD_DECL
1362 && !cp_omp_mappable_type (TREE_TYPE (field)))
1363 return false;
1365 return true;
1368 /* Like decl_attributes, but handle C++ complexity. */
1370 void
1371 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1373 if (*decl == NULL_TREE || *decl == void_type_node
1374 || *decl == error_mark_node)
1375 return;
1377 /* Add implicit "omp declare target" attribute if requested. */
1378 if (scope_chain->omp_declare_target_attribute
1379 && ((VAR_P (*decl)
1380 && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
1381 || TREE_CODE (*decl) == FUNCTION_DECL))
1383 if (VAR_P (*decl)
1384 && DECL_CLASS_SCOPE_P (*decl))
1385 error ("%q+D static data member inside of declare target directive",
1386 *decl);
1387 else if (!processing_template_decl
1388 && VAR_P (*decl)
1389 && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1390 error ("%q+D in declare target directive does not have mappable type",
1391 *decl);
1392 else
1393 attributes = tree_cons (get_identifier ("omp declare target"),
1394 NULL_TREE, attributes);
1397 if (processing_template_decl)
1399 if (check_for_bare_parameter_packs (attributes))
1400 return;
1402 save_template_attributes (&attributes, decl);
1405 cp_check_const_attributes (attributes);
1407 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1408 decl = &DECL_TEMPLATE_RESULT (*decl);
1410 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1412 attributes
1413 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1414 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1415 attributes, flags);
1417 else
1418 decl_attributes (decl, attributes, flags);
1420 if (TREE_CODE (*decl) == TYPE_DECL)
1421 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1423 /* Propagate deprecation out to the template. */
1424 if (TREE_DEPRECATED (*decl))
1425 if (tree ti = get_template_info (*decl))
1427 tree tmpl = TI_TEMPLATE (ti);
1428 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1429 : DECL_TEMPLATE_RESULT (tmpl));
1430 if (*decl == pattern)
1431 TREE_DEPRECATED (tmpl) = true;
1435 /* Walks through the namespace- or function-scope anonymous union
1436 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1437 Returns one of the fields for use in the mangled name. */
1439 static tree
1440 build_anon_union_vars (tree type, tree object)
1442 tree main_decl = NULL_TREE;
1443 tree field;
1445 /* Rather than write the code to handle the non-union case,
1446 just give an error. */
1447 if (TREE_CODE (type) != UNION_TYPE)
1449 error ("anonymous struct not inside named type");
1450 return error_mark_node;
1453 for (field = TYPE_FIELDS (type);
1454 field != NULL_TREE;
1455 field = DECL_CHAIN (field))
1457 tree decl;
1458 tree ref;
1460 if (DECL_ARTIFICIAL (field))
1461 continue;
1462 if (TREE_CODE (field) != FIELD_DECL)
1464 permerror (DECL_SOURCE_LOCATION (field),
1465 "%q#D invalid; an anonymous union can only "
1466 "have non-static data members", field);
1467 continue;
1470 if (TREE_PRIVATE (field))
1471 permerror (DECL_SOURCE_LOCATION (field),
1472 "private member %q#D in anonymous union", field);
1473 else if (TREE_PROTECTED (field))
1474 permerror (DECL_SOURCE_LOCATION (field),
1475 "protected member %q#D in anonymous union", field);
1477 if (processing_template_decl)
1478 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1479 DECL_NAME (field), NULL_TREE);
1480 else
1481 ref = build_class_member_access_expr (object, field, NULL_TREE,
1482 false, tf_warning_or_error);
1484 if (DECL_NAME (field))
1486 tree base;
1488 decl = build_decl (input_location,
1489 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1490 DECL_ANON_UNION_VAR_P (decl) = 1;
1491 DECL_ARTIFICIAL (decl) = 1;
1493 base = get_base_address (object);
1494 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1495 TREE_STATIC (decl) = TREE_STATIC (base);
1496 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1498 SET_DECL_VALUE_EXPR (decl, ref);
1499 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1501 decl = pushdecl (decl);
1503 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1504 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1505 else
1506 decl = 0;
1508 if (main_decl == NULL_TREE)
1509 main_decl = decl;
1512 return main_decl;
1515 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1516 anonymous union, then all members must be laid out together. PUBLIC_P
1517 is nonzero if this union is not declared static. */
1519 void
1520 finish_anon_union (tree anon_union_decl)
1522 tree type;
1523 tree main_decl;
1524 bool public_p;
1526 if (anon_union_decl == error_mark_node)
1527 return;
1529 type = TREE_TYPE (anon_union_decl);
1530 public_p = TREE_PUBLIC (anon_union_decl);
1532 /* The VAR_DECL's context is the same as the TYPE's context. */
1533 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1535 if (TYPE_FIELDS (type) == NULL_TREE)
1536 return;
1538 if (public_p)
1540 error ("namespace-scope anonymous aggregates must be static");
1541 return;
1544 main_decl = build_anon_union_vars (type, anon_union_decl);
1545 if (main_decl == error_mark_node)
1546 return;
1547 if (main_decl == NULL_TREE)
1549 warning (0, "anonymous union with no members");
1550 return;
1553 if (!processing_template_decl)
1555 /* Use main_decl to set the mangled name. */
1556 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1557 maybe_commonize_var (anon_union_decl);
1558 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1559 mangle_decl (anon_union_decl);
1560 DECL_NAME (anon_union_decl) = NULL_TREE;
1563 pushdecl (anon_union_decl);
1564 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1567 /* Auxiliary functions to make type signatures for
1568 `operator new' and `operator delete' correspond to
1569 what compiler will be expecting. */
1571 tree
1572 coerce_new_type (tree type)
1574 int e = 0;
1575 tree args = TYPE_ARG_TYPES (type);
1577 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1579 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1581 e = 1;
1582 error ("%<operator new%> must return type %qT", ptr_type_node);
1585 if (args && args != void_list_node)
1587 if (TREE_PURPOSE (args))
1589 /* [basic.stc.dynamic.allocation]
1591 The first parameter shall not have an associated default
1592 argument. */
1593 error ("the first parameter of %<operator new%> cannot "
1594 "have a default argument");
1595 /* Throw away the default argument. */
1596 TREE_PURPOSE (args) = NULL_TREE;
1599 if (!same_type_p (TREE_VALUE (args), size_type_node))
1601 e = 2;
1602 args = TREE_CHAIN (args);
1605 else
1606 e = 2;
1608 if (e == 2)
1609 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1610 "as first parameter", size_type_node);
1612 switch (e)
1614 case 2:
1615 args = tree_cons (NULL_TREE, size_type_node, args);
1616 /* Fall through. */
1617 case 1:
1618 type = build_exception_variant
1619 (build_function_type (ptr_type_node, args),
1620 TYPE_RAISES_EXCEPTIONS (type));
1621 /* Fall through. */
1622 default:;
1624 return type;
1627 tree
1628 coerce_delete_type (tree type)
1630 int e = 0;
1631 tree args = TYPE_ARG_TYPES (type);
1633 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1635 if (!same_type_p (TREE_TYPE (type), void_type_node))
1637 e = 1;
1638 error ("%<operator delete%> must return type %qT", void_type_node);
1641 if (!args || args == void_list_node
1642 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1644 e = 2;
1645 if (args && args != void_list_node)
1646 args = TREE_CHAIN (args);
1647 error ("%<operator delete%> takes type %qT as first parameter",
1648 ptr_type_node);
1650 switch (e)
1652 case 2:
1653 args = tree_cons (NULL_TREE, ptr_type_node, args);
1654 /* Fall through. */
1655 case 1:
1656 type = build_exception_variant
1657 (build_function_type (void_type_node, args),
1658 TYPE_RAISES_EXCEPTIONS (type));
1659 /* Fall through. */
1660 default:;
1663 return type;
1666 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1667 and mark them as needed. */
1669 static void
1670 mark_vtable_entries (tree decl)
1672 tree fnaddr;
1673 unsigned HOST_WIDE_INT idx;
1675 /* It's OK for the vtable to refer to deprecated virtual functions. */
1676 warning_sentinel w(warn_deprecated_decl);
1678 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1679 idx, fnaddr)
1681 tree fn;
1683 STRIP_NOPS (fnaddr);
1685 if (TREE_CODE (fnaddr) != ADDR_EXPR
1686 && TREE_CODE (fnaddr) != FDESC_EXPR)
1687 /* This entry is an offset: a virtual base class offset, a
1688 virtual call offset, an RTTI offset, etc. */
1689 continue;
1691 fn = TREE_OPERAND (fnaddr, 0);
1692 TREE_ADDRESSABLE (fn) = 1;
1693 /* When we don't have vcall offsets, we output thunks whenever
1694 we output the vtables that contain them. With vcall offsets,
1695 we know all the thunks we'll need when we emit a virtual
1696 function, so we emit the thunks there instead. */
1697 if (DECL_THUNK_P (fn))
1698 use_thunk (fn, /*emit_p=*/0);
1699 mark_used (fn);
1703 /* Set DECL up to have the closest approximation of "initialized common"
1704 linkage available. */
1706 void
1707 comdat_linkage (tree decl)
1709 if (flag_weak)
1710 make_decl_one_only (decl, cxx_comdat_group (decl));
1711 else if (TREE_CODE (decl) == FUNCTION_DECL
1712 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1713 /* We can just emit function and compiler-generated variables
1714 statically; having multiple copies is (for the most part) only
1715 a waste of space.
1717 There are two correctness issues, however: the address of a
1718 template instantiation with external linkage should be the
1719 same, independent of what translation unit asks for the
1720 address, and this will not hold when we emit multiple copies of
1721 the function. However, there's little else we can do.
1723 Also, by default, the typeinfo implementation assumes that
1724 there will be only one copy of the string used as the name for
1725 each type. Therefore, if weak symbols are unavailable, the
1726 run-time library should perform a more conservative check; it
1727 should perform a string comparison, rather than an address
1728 comparison. */
1729 TREE_PUBLIC (decl) = 0;
1730 else
1732 /* Static data member template instantiations, however, cannot
1733 have multiple copies. */
1734 if (DECL_INITIAL (decl) == 0
1735 || DECL_INITIAL (decl) == error_mark_node)
1736 DECL_COMMON (decl) = 1;
1737 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1739 DECL_COMMON (decl) = 1;
1740 DECL_INITIAL (decl) = error_mark_node;
1742 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1744 /* We can't do anything useful; leave vars for explicit
1745 instantiation. */
1746 DECL_EXTERNAL (decl) = 1;
1747 DECL_NOT_REALLY_EXTERN (decl) = 0;
1751 if (TREE_PUBLIC (decl))
1752 DECL_COMDAT (decl) = 1;
1755 /* For win32 we also want to put explicit instantiations in
1756 linkonce sections, so that they will be merged with implicit
1757 instantiations; otherwise we get duplicate symbol errors.
1758 For Darwin we do not want explicit instantiations to be
1759 linkonce. */
1761 void
1762 maybe_make_one_only (tree decl)
1764 /* We used to say that this was not necessary on targets that support weak
1765 symbols, because the implicit instantiations will defer to the explicit
1766 one. However, that's not actually the case in SVR4; a strong definition
1767 after a weak one is an error. Also, not making explicit
1768 instantiations one_only means that we can end up with two copies of
1769 some template instantiations. */
1770 if (! flag_weak)
1771 return;
1773 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1774 we can get away with not emitting them if they aren't used. We need
1775 to for variables so that cp_finish_decl will update their linkage,
1776 because their DECL_INITIAL may not have been set properly yet. */
1778 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1779 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1780 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1782 make_decl_one_only (decl, cxx_comdat_group (decl));
1784 if (VAR_P (decl))
1786 varpool_node *node = varpool_node::get_create (decl);
1787 DECL_COMDAT (decl) = 1;
1788 /* Mark it needed so we don't forget to emit it. */
1789 node->forced_by_abi = true;
1790 TREE_USED (decl) = 1;
1795 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1796 This predicate will give the right answer during parsing of the
1797 function, which other tests may not. */
1799 bool
1800 vague_linkage_p (tree decl)
1802 if (!TREE_PUBLIC (decl))
1804 /* maybe_thunk_body clears TREE_PUBLIC on the maybe-in-charge 'tor
1805 variants, check one of the "clones" for the real linkage. */
1806 if ((DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
1807 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl))
1808 && DECL_CHAIN (decl)
1809 && DECL_CLONED_FUNCTION (DECL_CHAIN (decl)))
1810 return vague_linkage_p (DECL_CHAIN (decl));
1812 gcc_checking_assert (!DECL_COMDAT (decl));
1813 return false;
1815 /* Unfortunately, import_export_decl has not always been called
1816 before the function is processed, so we cannot simply check
1817 DECL_COMDAT. */
1818 if (DECL_COMDAT (decl)
1819 || (TREE_CODE (decl) == FUNCTION_DECL
1820 && DECL_DECLARED_INLINE_P (decl))
1821 || (DECL_LANG_SPECIFIC (decl)
1822 && DECL_TEMPLATE_INSTANTIATION (decl))
1823 || (VAR_P (decl) && DECL_INLINE_VAR_P (decl)))
1824 return true;
1825 else if (DECL_FUNCTION_SCOPE_P (decl))
1826 /* A local static in an inline effectively has vague linkage. */
1827 return (TREE_STATIC (decl)
1828 && vague_linkage_p (DECL_CONTEXT (decl)));
1829 else
1830 return false;
1833 /* Determine whether or not we want to specifically import or export CTYPE,
1834 using various heuristics. */
1836 static void
1837 import_export_class (tree ctype)
1839 /* -1 for imported, 1 for exported. */
1840 int import_export = 0;
1842 /* It only makes sense to call this function at EOF. The reason is
1843 that this function looks at whether or not the first non-inline
1844 non-abstract virtual member function has been defined in this
1845 translation unit. But, we can't possibly know that until we've
1846 seen the entire translation unit. */
1847 gcc_assert (at_eof);
1849 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1850 return;
1852 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1853 we will have CLASSTYPE_INTERFACE_ONLY set but not
1854 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1855 heuristic because someone will supply a #pragma implementation
1856 elsewhere, and deducing it here would produce a conflict. */
1857 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1858 return;
1860 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1861 import_export = -1;
1862 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1863 import_export = 1;
1864 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1865 && !flag_implicit_templates)
1866 /* For a template class, without -fimplicit-templates, check the
1867 repository. If the virtual table is assigned to this
1868 translation unit, then export the class; otherwise, import
1869 it. */
1870 import_export = repo_export_class_p (ctype) ? 1 : -1;
1871 else if (TYPE_POLYMORPHIC_P (ctype))
1873 /* The ABI specifies that the virtual table and associated
1874 information are emitted with the key method, if any. */
1875 tree method = CLASSTYPE_KEY_METHOD (ctype);
1876 /* If weak symbol support is not available, then we must be
1877 careful not to emit the vtable when the key function is
1878 inline. An inline function can be defined in multiple
1879 translation units. If we were to emit the vtable in each
1880 translation unit containing a definition, we would get
1881 multiple definition errors at link-time. */
1882 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1883 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1886 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1887 a definition anywhere else. */
1888 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1889 import_export = 0;
1891 /* Allow back ends the chance to overrule the decision. */
1892 if (targetm.cxx.import_export_class)
1893 import_export = targetm.cxx.import_export_class (ctype, import_export);
1895 if (import_export)
1897 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1898 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1902 /* Return true if VAR has already been provided to the back end; in that
1903 case VAR should not be modified further by the front end. */
1904 static bool
1905 var_finalized_p (tree var)
1907 return varpool_node::get_create (var)->definition;
1910 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1911 must be emitted in this translation unit. Mark it as such. */
1913 void
1914 mark_needed (tree decl)
1916 TREE_USED (decl) = 1;
1917 if (TREE_CODE (decl) == FUNCTION_DECL)
1919 /* Extern inline functions don't become needed when referenced.
1920 If we know a method will be emitted in other TU and no new
1921 functions can be marked reachable, just use the external
1922 definition. */
1923 struct cgraph_node *node = cgraph_node::get_create (decl);
1924 node->forced_by_abi = true;
1926 /* #pragma interface and -frepo code can call mark_needed for
1927 maybe-in-charge 'tors; mark the clones as well. */
1928 tree clone;
1929 FOR_EACH_CLONE (clone, decl)
1930 mark_needed (clone);
1932 else if (VAR_P (decl))
1934 varpool_node *node = varpool_node::get_create (decl);
1935 /* C++ frontend use mark_decl_references to force COMDAT variables
1936 to be output that might appear dead otherwise. */
1937 node->forced_by_abi = true;
1941 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1942 returns true if a definition of this entity should be provided in
1943 this object file. Callers use this function to determine whether
1944 or not to let the back end know that a definition of DECL is
1945 available in this translation unit. */
1947 bool
1948 decl_needed_p (tree decl)
1950 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
1951 /* This function should only be called at the end of the translation
1952 unit. We cannot be sure of whether or not something will be
1953 COMDAT until that point. */
1954 gcc_assert (at_eof);
1956 /* All entities with external linkage that are not COMDAT/EXTERN should be
1957 emitted; they may be referred to from other object files. */
1958 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
1959 return true;
1960 /* Functions marked "dllexport" must be emitted so that they are
1961 visible to other DLLs. */
1962 if (flag_keep_inline_dllexport
1963 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
1964 return true;
1966 /* When not optimizing, do not bother to produce definitions for extern
1967 symbols. */
1968 if (DECL_REALLY_EXTERN (decl)
1969 && ((TREE_CODE (decl) != FUNCTION_DECL
1970 && !optimize)
1971 || (TREE_CODE (decl) == FUNCTION_DECL
1972 && !opt_for_fn (decl, optimize)))
1973 && !lookup_attribute ("always_inline", decl))
1974 return false;
1976 /* If this entity was used, let the back end see it; it will decide
1977 whether or not to emit it into the object file. */
1978 if (TREE_USED (decl))
1979 return true;
1981 /* Virtual functions might be needed for devirtualization. */
1982 if (flag_devirtualize
1983 && TREE_CODE (decl) == FUNCTION_DECL
1984 && DECL_VIRTUAL_P (decl))
1985 return true;
1987 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
1988 reference to DECL might cause it to be emitted later. */
1989 return false;
1992 /* If necessary, write out the vtables for the dynamic class CTYPE.
1993 Returns true if any vtables were emitted. */
1995 static bool
1996 maybe_emit_vtables (tree ctype)
1998 tree vtbl;
1999 tree primary_vtbl;
2000 int needed = 0;
2001 varpool_node *current = NULL, *last = NULL;
2003 /* If the vtables for this class have already been emitted there is
2004 nothing more to do. */
2005 primary_vtbl = CLASSTYPE_VTABLES (ctype);
2006 if (var_finalized_p (primary_vtbl))
2007 return false;
2008 /* Ignore dummy vtables made by get_vtable_decl. */
2009 if (TREE_TYPE (primary_vtbl) == void_type_node)
2010 return false;
2012 /* On some targets, we cannot determine the key method until the end
2013 of the translation unit -- which is when this function is
2014 called. */
2015 if (!targetm.cxx.key_method_may_be_inline ())
2016 determine_key_method (ctype);
2018 /* See if any of the vtables are needed. */
2019 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2021 import_export_decl (vtbl);
2022 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2023 needed = 1;
2025 if (!needed)
2027 /* If the references to this class' vtables are optimized away,
2028 still emit the appropriate debugging information. See
2029 dfs_debug_mark. */
2030 if (DECL_COMDAT (primary_vtbl)
2031 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2032 note_debug_info_needed (ctype);
2033 return false;
2036 /* The ABI requires that we emit all of the vtables if we emit any
2037 of them. */
2038 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2040 /* Mark entities references from the virtual table as used. */
2041 mark_vtable_entries (vtbl);
2043 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2045 vec<tree, va_gc> *cleanups = NULL;
2046 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2047 LOOKUP_NORMAL);
2049 /* It had better be all done at compile-time. */
2050 gcc_assert (!expr && !cleanups);
2053 /* Write it out. */
2054 DECL_EXTERNAL (vtbl) = 0;
2055 rest_of_decl_compilation (vtbl, 1, 1);
2057 /* Because we're only doing syntax-checking, we'll never end up
2058 actually marking the variable as written. */
2059 if (flag_syntax_only)
2060 TREE_ASM_WRITTEN (vtbl) = 1;
2061 else if (DECL_ONE_ONLY (vtbl))
2063 current = varpool_node::get_create (vtbl);
2064 if (last)
2065 current->add_to_same_comdat_group (last);
2066 last = current;
2070 /* Since we're writing out the vtable here, also write the debug
2071 info. */
2072 note_debug_info_needed (ctype);
2074 return true;
2077 /* A special return value from type_visibility meaning internal
2078 linkage. */
2080 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2082 /* walk_tree helper function for type_visibility. */
2084 static tree
2085 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2087 int *vis_p = (int *)data;
2088 if (! TYPE_P (*tp))
2090 *walk_subtrees = 0;
2092 else if (OVERLOAD_TYPE_P (*tp)
2093 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2095 *vis_p = VISIBILITY_ANON;
2096 return *tp;
2098 else if (CLASS_TYPE_P (*tp)
2099 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2100 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2101 return NULL;
2104 /* Returns the visibility of TYPE, which is the minimum visibility of its
2105 component types. */
2107 static int
2108 type_visibility (tree type)
2110 int vis = VISIBILITY_DEFAULT;
2111 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2112 return vis;
2115 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2116 specified (or if VISIBILITY is static). If TMPL is true, this
2117 constraint is for a template argument, and takes precedence
2118 over explicitly-specified visibility on the template. */
2120 static void
2121 constrain_visibility (tree decl, int visibility, bool tmpl)
2123 if (visibility == VISIBILITY_ANON)
2125 /* extern "C" declarations aren't affected by the anonymous
2126 namespace. */
2127 if (!DECL_EXTERN_C_P (decl))
2129 TREE_PUBLIC (decl) = 0;
2130 DECL_WEAK (decl) = 0;
2131 DECL_COMMON (decl) = 0;
2132 DECL_COMDAT (decl) = false;
2133 if (VAR_OR_FUNCTION_DECL_P (decl))
2135 struct symtab_node *snode = symtab_node::get (decl);
2137 if (snode)
2138 snode->set_comdat_group (NULL);
2140 DECL_INTERFACE_KNOWN (decl) = 1;
2141 if (DECL_LANG_SPECIFIC (decl))
2142 DECL_NOT_REALLY_EXTERN (decl) = 1;
2145 else if (visibility > DECL_VISIBILITY (decl)
2146 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2148 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2149 /* This visibility was not specified. */
2150 DECL_VISIBILITY_SPECIFIED (decl) = false;
2154 /* Constrain the visibility of DECL based on the visibility of its template
2155 arguments. */
2157 static void
2158 constrain_visibility_for_template (tree decl, tree targs)
2160 /* If this is a template instantiation, check the innermost
2161 template args for visibility constraints. The outer template
2162 args are covered by the class check. */
2163 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2164 int i;
2165 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2167 int vis = 0;
2169 tree arg = TREE_VEC_ELT (args, i-1);
2170 if (TYPE_P (arg))
2171 vis = type_visibility (arg);
2172 else
2174 if (REFERENCE_REF_P (arg))
2175 arg = TREE_OPERAND (arg, 0);
2176 if (TREE_TYPE (arg))
2177 STRIP_NOPS (arg);
2178 if (TREE_CODE (arg) == ADDR_EXPR)
2179 arg = TREE_OPERAND (arg, 0);
2180 if (VAR_OR_FUNCTION_DECL_P (arg))
2182 if (! TREE_PUBLIC (arg))
2183 vis = VISIBILITY_ANON;
2184 else
2185 vis = DECL_VISIBILITY (arg);
2188 if (vis)
2189 constrain_visibility (decl, vis, true);
2193 /* Like c_determine_visibility, but with additional C++-specific
2194 behavior.
2196 Function-scope entities can rely on the function's visibility because
2197 it is set in start_preparsed_function.
2199 Class-scope entities cannot rely on the class's visibility until the end
2200 of the enclosing class definition.
2202 Note that because namespaces have multiple independent definitions,
2203 namespace visibility is handled elsewhere using the #pragma visibility
2204 machinery rather than by decorating the namespace declaration.
2206 The goal is for constraints from the type to give a diagnostic, and
2207 other constraints to be applied silently. */
2209 void
2210 determine_visibility (tree decl)
2212 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2214 /* Only relevant for names with external linkage. */
2215 if (!TREE_PUBLIC (decl))
2216 return;
2218 /* Cloned constructors and destructors get the same visibility as
2219 the underlying function. That should be set up in
2220 maybe_clone_body. */
2221 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2223 bool orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2224 enum symbol_visibility orig_visibility = DECL_VISIBILITY (decl);
2226 /* The decl may be a template instantiation, which could influence
2227 visibilty. */
2228 tree template_decl = NULL_TREE;
2229 if (TREE_CODE (decl) == TYPE_DECL)
2231 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2233 if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
2234 template_decl = decl;
2236 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2237 template_decl = decl;
2239 else if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
2240 template_decl = decl;
2242 /* If DECL is a member of a class, visibility specifiers on the
2243 class can influence the visibility of the DECL. */
2244 tree class_type = NULL_TREE;
2245 if (DECL_CLASS_SCOPE_P (decl))
2246 class_type = DECL_CONTEXT (decl);
2247 else
2249 /* Not a class member. */
2251 /* Virtual tables have DECL_CONTEXT set to their associated class,
2252 so they are automatically handled above. */
2253 gcc_assert (!VAR_P (decl)
2254 || !DECL_VTABLE_OR_VTT_P (decl));
2256 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2258 /* Local statics and classes get the visibility of their
2259 containing function by default, except that
2260 -fvisibility-inlines-hidden doesn't affect them. */
2261 tree fn = DECL_CONTEXT (decl);
2262 if (DECL_VISIBILITY_SPECIFIED (fn))
2264 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2265 DECL_VISIBILITY_SPECIFIED (decl) =
2266 DECL_VISIBILITY_SPECIFIED (fn);
2268 else
2270 if (DECL_CLASS_SCOPE_P (fn))
2271 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2272 else if (determine_hidden_inline (fn))
2274 DECL_VISIBILITY (decl) = default_visibility;
2275 DECL_VISIBILITY_SPECIFIED (decl) =
2276 visibility_options.inpragma;
2278 else
2280 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2281 DECL_VISIBILITY_SPECIFIED (decl) =
2282 DECL_VISIBILITY_SPECIFIED (fn);
2286 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2287 but have no TEMPLATE_INFO. Their containing template
2288 function does, and the local class could be constrained
2289 by that. */
2290 if (template_decl)
2291 template_decl = fn;
2293 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2294 && flag_visibility_ms_compat)
2296 /* Under -fvisibility-ms-compat, types are visible by default,
2297 even though their contents aren't. */
2298 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2299 int underlying_vis = type_visibility (underlying_type);
2300 if (underlying_vis == VISIBILITY_ANON
2301 || (CLASS_TYPE_P (underlying_type)
2302 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2303 constrain_visibility (decl, underlying_vis, false);
2304 else
2305 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2307 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2309 /* tinfo visibility is based on the type it's for. */
2310 constrain_visibility
2311 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2313 /* Give the target a chance to override the visibility associated
2314 with DECL. */
2315 if (TREE_PUBLIC (decl)
2316 && !DECL_REALLY_EXTERN (decl)
2317 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2318 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2319 targetm.cxx.determine_class_data_visibility (decl);
2321 else if (template_decl)
2322 /* Template instantiations and specializations get visibility based
2323 on their template unless they override it with an attribute. */;
2324 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2326 if (determine_hidden_inline (decl))
2327 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2328 else
2330 /* Set default visibility to whatever the user supplied with
2331 #pragma GCC visibility or a namespace visibility attribute. */
2332 DECL_VISIBILITY (decl) = default_visibility;
2333 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2338 if (template_decl)
2340 /* If the specialization doesn't specify visibility, use the
2341 visibility from the template. */
2342 tree tinfo = get_template_info (template_decl);
2343 tree args = TI_ARGS (tinfo);
2344 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2345 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2346 : DECL_ATTRIBUTES (decl));
2348 if (args != error_mark_node)
2350 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2352 if (!DECL_VISIBILITY_SPECIFIED (decl))
2354 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2355 && determine_hidden_inline (decl))
2356 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2357 else
2359 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2360 DECL_VISIBILITY_SPECIFIED (decl)
2361 = DECL_VISIBILITY_SPECIFIED (pattern);
2365 if (args
2366 /* Template argument visibility outweighs #pragma or namespace
2367 visibility, but not an explicit attribute. */
2368 && !lookup_attribute ("visibility", attribs))
2370 int depth = TMPL_ARGS_DEPTH (args);
2371 if (DECL_VISIBILITY_SPECIFIED (decl))
2373 /* A class template member with explicit visibility
2374 overrides the class visibility, so we need to apply
2375 all the levels of template args directly. */
2376 int i;
2377 for (i = 1; i <= depth; ++i)
2379 tree lev = TMPL_ARGS_LEVEL (args, i);
2380 constrain_visibility_for_template (decl, lev);
2383 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2384 /* Limit visibility based on its template arguments. */
2385 constrain_visibility_for_template (decl, args);
2390 if (class_type)
2391 determine_visibility_from_class (decl, class_type);
2393 if (decl_anon_ns_mem_p (decl))
2394 /* Names in an anonymous namespace get internal linkage.
2395 This might change once we implement export. */
2396 constrain_visibility (decl, VISIBILITY_ANON, false);
2397 else if (TREE_CODE (decl) != TYPE_DECL)
2399 /* Propagate anonymity from type to decl. */
2400 int tvis = type_visibility (TREE_TYPE (decl));
2401 if (tvis == VISIBILITY_ANON
2402 || ! DECL_VISIBILITY_SPECIFIED (decl))
2403 constrain_visibility (decl, tvis, false);
2405 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2406 /* DR 757: A type without linkage shall not be used as the type of a
2407 variable or function with linkage, unless
2408 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2409 o the variable or function is not used (3.2 [basic.def.odr]) or is
2410 defined in the same translation unit.
2412 Since non-extern "C" decls need to be defined in the same
2413 translation unit, we can make the type internal. */
2414 constrain_visibility (decl, VISIBILITY_ANON, false);
2416 /* If visibility changed and DECL already has DECL_RTL, ensure
2417 symbol flags are updated. */
2418 if ((DECL_VISIBILITY (decl) != orig_visibility
2419 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2420 && ((VAR_P (decl) && TREE_STATIC (decl))
2421 || TREE_CODE (decl) == FUNCTION_DECL)
2422 && DECL_RTL_SET_P (decl))
2423 make_decl_rtl (decl);
2426 /* By default, static data members and function members receive
2427 the visibility of their containing class. */
2429 static void
2430 determine_visibility_from_class (tree decl, tree class_type)
2432 if (DECL_VISIBILITY_SPECIFIED (decl))
2433 return;
2435 if (determine_hidden_inline (decl))
2436 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2437 else
2439 /* Default to the class visibility. */
2440 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2441 DECL_VISIBILITY_SPECIFIED (decl)
2442 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2445 /* Give the target a chance to override the visibility associated
2446 with DECL. */
2447 if (VAR_P (decl)
2448 && (DECL_TINFO_P (decl)
2449 || (DECL_VTABLE_OR_VTT_P (decl)
2450 /* Construction virtual tables are not exported because
2451 they cannot be referred to from other object files;
2452 their name is not standardized by the ABI. */
2453 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2454 && TREE_PUBLIC (decl)
2455 && !DECL_REALLY_EXTERN (decl)
2456 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2457 targetm.cxx.determine_class_data_visibility (decl);
2460 /* Returns true iff DECL is an inline that should get hidden visibility
2461 because of -fvisibility-inlines-hidden. */
2463 static bool
2464 determine_hidden_inline (tree decl)
2466 return (visibility_options.inlines_hidden
2467 /* Don't do this for inline templates; specializations might not be
2468 inline, and we don't want them to inherit the hidden
2469 visibility. We'll set it here for all inline instantiations. */
2470 && !processing_template_decl
2471 && TREE_CODE (decl) == FUNCTION_DECL
2472 && DECL_DECLARED_INLINE_P (decl)
2473 && (! DECL_LANG_SPECIFIC (decl)
2474 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2477 /* Constrain the visibility of a class TYPE based on the visibility of its
2478 field types. Warn if any fields require lesser visibility. */
2480 void
2481 constrain_class_visibility (tree type)
2483 tree binfo;
2484 tree t;
2485 int i;
2487 int vis = type_visibility (type);
2489 if (vis == VISIBILITY_ANON
2490 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2491 return;
2493 /* Don't warn about visibility if the class has explicit visibility. */
2494 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2495 vis = VISIBILITY_INTERNAL;
2497 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2498 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2500 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2501 int subvis = type_visibility (ftype);
2503 if (subvis == VISIBILITY_ANON)
2505 if (!in_main_input_context())
2507 tree nlt = no_linkage_check (ftype, /*relaxed_p=*/false);
2508 if (nlt)
2510 if (same_type_p (TREE_TYPE (t), nlt))
2511 warning (OPT_Wsubobject_linkage, "\
2512 %qT has a field %qD whose type has no linkage",
2513 type, t);
2514 else
2515 warning (OPT_Wsubobject_linkage, "\
2516 %qT has a field %qD whose type depends on the type %qT which has no linkage",
2517 type, t, nlt);
2519 else
2520 warning (OPT_Wsubobject_linkage, "\
2521 %qT has a field %qD whose type uses the anonymous namespace",
2522 type, t);
2525 else if (MAYBE_CLASS_TYPE_P (ftype)
2526 && vis < VISIBILITY_HIDDEN
2527 && subvis >= VISIBILITY_HIDDEN)
2528 warning (OPT_Wattributes, "\
2529 %qT declared with greater visibility than the type of its field %qD",
2530 type, t);
2533 binfo = TYPE_BINFO (type);
2534 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2536 int subvis = type_visibility (TREE_TYPE (t));
2538 if (subvis == VISIBILITY_ANON)
2540 if (!in_main_input_context())
2542 tree nlt = no_linkage_check (TREE_TYPE (t), /*relaxed_p=*/false);
2543 if (nlt)
2545 if (same_type_p (TREE_TYPE (t), nlt))
2546 warning (OPT_Wsubobject_linkage, "\
2547 %qT has a base %qT whose type has no linkage",
2548 type, TREE_TYPE (t));
2549 else
2550 warning (OPT_Wsubobject_linkage, "\
2551 %qT has a base %qT whose type depends on the type %qT which has no linkage",
2552 type, TREE_TYPE (t), nlt);
2554 else
2555 warning (OPT_Wsubobject_linkage, "\
2556 %qT has a base %qT whose type uses the anonymous namespace",
2557 type, TREE_TYPE (t));
2560 else if (vis < VISIBILITY_HIDDEN
2561 && subvis >= VISIBILITY_HIDDEN)
2562 warning (OPT_Wattributes, "\
2563 %qT declared with greater visibility than its base %qT",
2564 type, TREE_TYPE (t));
2568 /* Functions for adjusting the visibility of a tagged type and its nested
2569 types and declarations when it gets a name for linkage purposes from a
2570 typedef. */
2572 static void bt_reset_linkage_1 (binding_entry, void *);
2573 static void bt_reset_linkage_2 (binding_entry, void *);
2575 /* First reset the visibility of all the types. */
2577 static void
2578 reset_type_linkage_1 (tree type)
2580 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2581 if (CLASS_TYPE_P (type))
2582 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2583 bt_reset_linkage_1, NULL);
2585 static void
2586 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2588 reset_type_linkage_1 (b->type);
2591 /* Then reset the visibility of any static data members or member
2592 functions that use those types. */
2594 static void
2595 reset_decl_linkage (tree decl)
2597 if (TREE_PUBLIC (decl))
2598 return;
2599 if (DECL_CLONED_FUNCTION_P (decl))
2600 return;
2601 TREE_PUBLIC (decl) = true;
2602 DECL_INTERFACE_KNOWN (decl) = false;
2603 determine_visibility (decl);
2604 tentative_decl_linkage (decl);
2607 static void
2608 reset_type_linkage_2 (tree type)
2610 if (CLASS_TYPE_P (type))
2612 if (tree vt = CLASSTYPE_VTABLES (type))
2614 tree name = mangle_vtbl_for_type (type);
2615 DECL_NAME (vt) = name;
2616 SET_DECL_ASSEMBLER_NAME (vt, name);
2617 reset_decl_linkage (vt);
2619 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2621 tree name = mangle_typeinfo_for_type (type);
2622 DECL_NAME (ti) = name;
2623 SET_DECL_ASSEMBLER_NAME (ti, name);
2624 TREE_TYPE (name) = type;
2625 reset_decl_linkage (ti);
2627 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2629 tree mem = STRIP_TEMPLATE (m);
2630 if (TREE_CODE (mem) == VAR_DECL || TREE_CODE (mem) == FUNCTION_DECL)
2631 reset_decl_linkage (mem);
2633 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2634 bt_reset_linkage_2, NULL);
2638 static void
2639 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2641 reset_type_linkage_2 (b->type);
2643 void
2644 reset_type_linkage (tree type)
2646 reset_type_linkage_1 (type);
2647 reset_type_linkage_2 (type);
2650 /* Set up our initial idea of what the linkage of DECL should be. */
2652 void
2653 tentative_decl_linkage (tree decl)
2655 if (DECL_INTERFACE_KNOWN (decl))
2656 /* We've already made a decision as to how this function will
2657 be handled. */;
2658 else if (vague_linkage_p (decl))
2660 if (TREE_CODE (decl) == FUNCTION_DECL
2661 && decl_defined_p (decl))
2663 DECL_EXTERNAL (decl) = 1;
2664 DECL_NOT_REALLY_EXTERN (decl) = 1;
2665 note_vague_linkage_fn (decl);
2666 /* A non-template inline function with external linkage will
2667 always be COMDAT. As we must eventually determine the
2668 linkage of all functions, and as that causes writes to
2669 the data mapped in from the PCH file, it's advantageous
2670 to mark the functions at this point. */
2671 if (DECL_DECLARED_INLINE_P (decl)
2672 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2673 || DECL_DEFAULTED_FN (decl)))
2675 /* This function must have external linkage, as
2676 otherwise DECL_INTERFACE_KNOWN would have been
2677 set. */
2678 gcc_assert (TREE_PUBLIC (decl));
2679 comdat_linkage (decl);
2680 DECL_INTERFACE_KNOWN (decl) = 1;
2683 else if (VAR_P (decl))
2684 maybe_commonize_var (decl);
2688 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2689 for DECL has not already been determined, do so now by setting
2690 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2691 function is called entities with vague linkage whose definitions
2692 are available must have TREE_PUBLIC set.
2694 If this function decides to place DECL in COMDAT, it will set
2695 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2696 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2697 callers defer that decision until it is clear that DECL is actually
2698 required. */
2700 void
2701 import_export_decl (tree decl)
2703 int emit_p;
2704 bool comdat_p;
2705 bool import_p;
2706 tree class_type = NULL_TREE;
2708 if (DECL_INTERFACE_KNOWN (decl))
2709 return;
2711 /* We cannot determine what linkage to give to an entity with vague
2712 linkage until the end of the file. For example, a virtual table
2713 for a class will be defined if and only if the key method is
2714 defined in this translation unit. As a further example, consider
2715 that when compiling a translation unit that uses PCH file with
2716 "-frepo" it would be incorrect to make decisions about what
2717 entities to emit when building the PCH; those decisions must be
2718 delayed until the repository information has been processed. */
2719 gcc_assert (at_eof);
2720 /* Object file linkage for explicit instantiations is handled in
2721 mark_decl_instantiated. For static variables in functions with
2722 vague linkage, maybe_commonize_var is used.
2724 Therefore, the only declarations that should be provided to this
2725 function are those with external linkage that are:
2727 * implicit instantiations of function templates
2729 * inline function
2731 * implicit instantiations of static data members of class
2732 templates
2734 * virtual tables
2736 * typeinfo objects
2738 Furthermore, all entities that reach this point must have a
2739 definition available in this translation unit.
2741 The following assertions check these conditions. */
2742 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2743 /* Any code that creates entities with TREE_PUBLIC cleared should
2744 also set DECL_INTERFACE_KNOWN. */
2745 gcc_assert (TREE_PUBLIC (decl));
2746 if (TREE_CODE (decl) == FUNCTION_DECL)
2747 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2748 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2749 || DECL_DECLARED_INLINE_P (decl));
2750 else
2751 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2752 || DECL_VTABLE_OR_VTT_P (decl)
2753 || DECL_TINFO_P (decl));
2754 /* Check that a definition of DECL is available in this translation
2755 unit. */
2756 gcc_assert (!DECL_REALLY_EXTERN (decl));
2758 /* Assume that DECL will not have COMDAT linkage. */
2759 comdat_p = false;
2760 /* Assume that DECL will not be imported into this translation
2761 unit. */
2762 import_p = false;
2764 /* See if the repository tells us whether or not to emit DECL in
2765 this translation unit. */
2766 emit_p = repo_emit_p (decl);
2767 if (emit_p == 0)
2768 import_p = true;
2769 else if (emit_p == 1)
2771 /* The repository indicates that this entity should be defined
2772 here. Make sure the back end honors that request. */
2773 mark_needed (decl);
2774 /* Output the definition as an ordinary strong definition. */
2775 DECL_EXTERNAL (decl) = 0;
2776 DECL_INTERFACE_KNOWN (decl) = 1;
2777 return;
2780 if (import_p)
2781 /* We have already decided what to do with this DECL; there is no
2782 need to check anything further. */
2784 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2786 class_type = DECL_CONTEXT (decl);
2787 import_export_class (class_type);
2788 if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2789 && CLASSTYPE_INTERFACE_ONLY (class_type))
2790 import_p = true;
2791 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2792 && !CLASSTYPE_USE_TEMPLATE (class_type)
2793 && CLASSTYPE_KEY_METHOD (class_type)
2794 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2795 /* The ABI requires that all virtual tables be emitted with
2796 COMDAT linkage. However, on systems where COMDAT symbols
2797 don't show up in the table of contents for a static
2798 archive, or on systems without weak symbols (where we
2799 approximate COMDAT linkage by using internal linkage), the
2800 linker will report errors about undefined symbols because
2801 it will not see the virtual table definition. Therefore,
2802 in the case that we know that the virtual table will be
2803 emitted in only one translation unit, we make the virtual
2804 table an ordinary definition with external linkage. */
2805 DECL_EXTERNAL (decl) = 0;
2806 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2808 /* CLASS_TYPE is being exported from this translation unit,
2809 so DECL should be defined here. */
2810 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2811 /* If a class is declared in a header with the "extern
2812 template" extension, then it will not be instantiated,
2813 even in translation units that would normally require
2814 it. Often such classes are explicitly instantiated in
2815 one translation unit. Therefore, the explicit
2816 instantiation must be made visible to other translation
2817 units. */
2818 DECL_EXTERNAL (decl) = 0;
2819 else
2821 /* The generic C++ ABI says that class data is always
2822 COMDAT, even if there is a key function. Some
2823 variants (e.g., the ARM EABI) says that class data
2824 only has COMDAT linkage if the class data might be
2825 emitted in more than one translation unit. When the
2826 key method can be inline and is inline, we still have
2827 to arrange for comdat even though
2828 class_data_always_comdat is false. */
2829 if (!CLASSTYPE_KEY_METHOD (class_type)
2830 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2831 || targetm.cxx.class_data_always_comdat ())
2833 /* The ABI requires COMDAT linkage. Normally, we
2834 only emit COMDAT things when they are needed;
2835 make sure that we realize that this entity is
2836 indeed needed. */
2837 comdat_p = true;
2838 mark_needed (decl);
2842 else if (!flag_implicit_templates
2843 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2844 import_p = true;
2845 else
2846 comdat_p = true;
2848 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2850 tree type = TREE_TYPE (DECL_NAME (decl));
2851 if (CLASS_TYPE_P (type))
2853 class_type = type;
2854 import_export_class (type);
2855 if (CLASSTYPE_INTERFACE_KNOWN (type)
2856 && TYPE_POLYMORPHIC_P (type)
2857 && CLASSTYPE_INTERFACE_ONLY (type)
2858 /* If -fno-rtti was specified, then we cannot be sure
2859 that RTTI information will be emitted with the
2860 virtual table of the class, so we must emit it
2861 wherever it is used. */
2862 && flag_rtti)
2863 import_p = true;
2864 else
2866 if (CLASSTYPE_INTERFACE_KNOWN (type)
2867 && !CLASSTYPE_INTERFACE_ONLY (type))
2869 comdat_p = (targetm.cxx.class_data_always_comdat ()
2870 || (CLASSTYPE_KEY_METHOD (type)
2871 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2872 mark_needed (decl);
2873 if (!flag_weak)
2875 comdat_p = false;
2876 DECL_EXTERNAL (decl) = 0;
2879 else
2880 comdat_p = true;
2883 else
2884 comdat_p = true;
2886 else if (DECL_TEMPLOID_INSTANTIATION (decl))
2888 /* DECL is an implicit instantiation of a function or static
2889 data member. */
2890 if ((flag_implicit_templates
2891 && !flag_use_repository)
2892 || (flag_implicit_inline_templates
2893 && TREE_CODE (decl) == FUNCTION_DECL
2894 && DECL_DECLARED_INLINE_P (decl)))
2895 comdat_p = true;
2896 else
2897 /* If we are not implicitly generating templates, then mark
2898 this entity as undefined in this translation unit. */
2899 import_p = true;
2901 else if (DECL_FUNCTION_MEMBER_P (decl))
2903 if (!DECL_DECLARED_INLINE_P (decl))
2905 tree ctype = DECL_CONTEXT (decl);
2906 import_export_class (ctype);
2907 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2909 DECL_NOT_REALLY_EXTERN (decl)
2910 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2911 || (DECL_DECLARED_INLINE_P (decl)
2912 && ! flag_implement_inlines
2913 && !DECL_VINDEX (decl)));
2915 if (!DECL_NOT_REALLY_EXTERN (decl))
2916 DECL_EXTERNAL (decl) = 1;
2918 /* Always make artificials weak. */
2919 if (DECL_ARTIFICIAL (decl) && flag_weak)
2920 comdat_p = true;
2921 else
2922 maybe_make_one_only (decl);
2925 else
2926 comdat_p = true;
2928 else
2929 comdat_p = true;
2931 if (import_p)
2933 /* If we are importing DECL into this translation unit, mark is
2934 an undefined here. */
2935 DECL_EXTERNAL (decl) = 1;
2936 DECL_NOT_REALLY_EXTERN (decl) = 0;
2938 else if (comdat_p)
2940 /* If we decided to put DECL in COMDAT, mark it accordingly at
2941 this point. */
2942 comdat_linkage (decl);
2945 DECL_INTERFACE_KNOWN (decl) = 1;
2948 /* Return an expression that performs the destruction of DECL, which
2949 must be a VAR_DECL whose type has a non-trivial destructor, or is
2950 an array whose (innermost) elements have a non-trivial destructor. */
2952 tree
2953 build_cleanup (tree decl)
2955 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
2956 gcc_assert (clean != NULL_TREE);
2957 return clean;
2960 /* Returns the initialization guard variable for the variable DECL,
2961 which has static storage duration. */
2963 tree
2964 get_guard (tree decl)
2966 tree sname;
2967 tree guard;
2969 sname = mangle_guard_variable (decl);
2970 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2971 if (! guard)
2973 tree guard_type;
2975 /* We use a type that is big enough to contain a mutex as well
2976 as an integer counter. */
2977 guard_type = targetm.cxx.guard_type ();
2978 guard = build_decl (DECL_SOURCE_LOCATION (decl),
2979 VAR_DECL, sname, guard_type);
2981 /* The guard should have the same linkage as what it guards. */
2982 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
2983 TREE_STATIC (guard) = TREE_STATIC (decl);
2984 DECL_COMMON (guard) = DECL_COMMON (decl);
2985 DECL_COMDAT (guard) = DECL_COMDAT (decl);
2986 CP_DECL_THREAD_LOCAL_P (guard) = CP_DECL_THREAD_LOCAL_P (decl);
2987 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
2988 if (DECL_ONE_ONLY (decl))
2989 make_decl_one_only (guard, cxx_comdat_group (guard));
2990 if (TREE_PUBLIC (decl))
2991 DECL_WEAK (guard) = DECL_WEAK (decl);
2992 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
2993 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
2995 DECL_ARTIFICIAL (guard) = 1;
2996 DECL_IGNORED_P (guard) = 1;
2997 TREE_USED (guard) = 1;
2998 pushdecl_top_level_and_finish (guard, NULL_TREE);
3000 return guard;
3003 /* Return an atomic load of src with the appropriate memory model. */
3005 static tree
3006 build_atomic_load_byte (tree src, HOST_WIDE_INT model)
3008 tree ptr_type = build_pointer_type (char_type_node);
3009 tree mem_model = build_int_cst (integer_type_node, model);
3010 tree t, addr, val;
3011 unsigned int size;
3012 int fncode;
3014 size = tree_to_uhwi (TYPE_SIZE_UNIT (char_type_node));
3016 fncode = BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1;
3017 t = builtin_decl_implicit ((enum built_in_function) fncode);
3019 addr = build1 (ADDR_EXPR, ptr_type, src);
3020 val = build_call_expr (t, 2, addr, mem_model);
3021 return val;
3024 /* Return those bits of the GUARD variable that should be set when the
3025 guarded entity is actually initialized. */
3027 static tree
3028 get_guard_bits (tree guard)
3030 if (!targetm.cxx.guard_mask_bit ())
3032 /* We only set the first byte of the guard, in order to leave room
3033 for a mutex in the high-order bits. */
3034 guard = build1 (ADDR_EXPR,
3035 build_pointer_type (TREE_TYPE (guard)),
3036 guard);
3037 guard = build1 (NOP_EXPR,
3038 build_pointer_type (char_type_node),
3039 guard);
3040 guard = build1 (INDIRECT_REF, char_type_node, guard);
3043 return guard;
3046 /* Return an expression which determines whether or not the GUARD
3047 variable has already been initialized. */
3049 tree
3050 get_guard_cond (tree guard, bool thread_safe)
3052 tree guard_value;
3054 if (!thread_safe)
3055 guard = get_guard_bits (guard);
3056 else
3057 guard = build_atomic_load_byte (guard, MEMMODEL_ACQUIRE);
3059 /* Mask off all but the low bit. */
3060 if (targetm.cxx.guard_mask_bit ())
3062 guard_value = integer_one_node;
3063 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3064 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3065 guard = cp_build_binary_op (input_location,
3066 BIT_AND_EXPR, guard, guard_value,
3067 tf_warning_or_error);
3070 guard_value = integer_zero_node;
3071 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3072 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3073 return cp_build_binary_op (input_location,
3074 EQ_EXPR, guard, guard_value,
3075 tf_warning_or_error);
3078 /* Return an expression which sets the GUARD variable, indicating that
3079 the variable being guarded has been initialized. */
3081 tree
3082 set_guard (tree guard)
3084 tree guard_init;
3086 /* Set the GUARD to one. */
3087 guard = get_guard_bits (guard);
3088 guard_init = integer_one_node;
3089 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3090 guard_init = fold_convert (TREE_TYPE (guard), guard_init);
3091 return cp_build_modify_expr (input_location, guard, NOP_EXPR, guard_init,
3092 tf_warning_or_error);
3095 /* Returns true iff we can tell that VAR does not have a dynamic
3096 initializer. */
3098 static bool
3099 var_defined_without_dynamic_init (tree var)
3101 /* If it's defined in another TU, we can't tell. */
3102 if (DECL_EXTERNAL (var))
3103 return false;
3104 /* If it has a non-trivial destructor, registering the destructor
3105 counts as dynamic initialization. */
3106 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3107 return false;
3108 /* If it's in this TU, its initializer has been processed, unless
3109 it's a case of self-initialization, then DECL_INITIALIZED_P is
3110 false while the initializer is handled by finish_id_expression. */
3111 if (!DECL_INITIALIZED_P (var))
3112 return false;
3113 /* If it has no initializer or a constant one, it's not dynamic. */
3114 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3115 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3118 /* Returns true iff VAR is a variable that needs uses to be
3119 wrapped for possible dynamic initialization. */
3121 static bool
3122 var_needs_tls_wrapper (tree var)
3124 return (!error_operand_p (var)
3125 && CP_DECL_THREAD_LOCAL_P (var)
3126 && !DECL_GNU_TLS_P (var)
3127 && !DECL_FUNCTION_SCOPE_P (var)
3128 && !var_defined_without_dynamic_init (var));
3131 /* Get the FUNCTION_DECL for the shared TLS init function for this
3132 translation unit. */
3134 static tree
3135 get_local_tls_init_fn (void)
3137 tree sname = get_identifier ("__tls_init");
3138 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3139 if (!fn)
3141 fn = build_lang_decl (FUNCTION_DECL, sname,
3142 build_function_type (void_type_node,
3143 void_list_node));
3144 SET_DECL_LANGUAGE (fn, lang_c);
3145 TREE_PUBLIC (fn) = false;
3146 DECL_ARTIFICIAL (fn) = true;
3147 mark_used (fn);
3148 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3150 return fn;
3153 /* Get a FUNCTION_DECL for the init function for the thread_local
3154 variable VAR. The init function will be an alias to the function
3155 that initializes all the non-local TLS variables in the translation
3156 unit. The init function is only used by the wrapper function. */
3158 static tree
3159 get_tls_init_fn (tree var)
3161 /* Only C++11 TLS vars need this init fn. */
3162 if (!var_needs_tls_wrapper (var))
3163 return NULL_TREE;
3165 /* If -fno-extern-tls-init, assume that we don't need to call
3166 a tls init function for a variable defined in another TU. */
3167 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3168 return NULL_TREE;
3170 /* If the variable is internal, or if we can't generate aliases,
3171 call the local init function directly. */
3172 if (!TREE_PUBLIC (var) || !TARGET_SUPPORTS_ALIASES)
3173 return get_local_tls_init_fn ();
3175 tree sname = mangle_tls_init_fn (var);
3176 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3177 if (!fn)
3179 fn = build_lang_decl (FUNCTION_DECL, sname,
3180 build_function_type (void_type_node,
3181 void_list_node));
3182 SET_DECL_LANGUAGE (fn, lang_c);
3183 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3184 DECL_ARTIFICIAL (fn) = true;
3185 DECL_COMDAT (fn) = DECL_COMDAT (var);
3186 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3187 if (DECL_ONE_ONLY (var))
3188 make_decl_one_only (fn, cxx_comdat_group (fn));
3189 if (TREE_PUBLIC (var))
3191 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3192 /* If the variable is defined somewhere else and might have static
3193 initialization, make the init function a weak reference. */
3194 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3195 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3196 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3197 && DECL_EXTERNAL (var))
3198 declare_weak (fn);
3199 else
3200 DECL_WEAK (fn) = DECL_WEAK (var);
3202 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3203 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3204 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3205 DECL_IGNORED_P (fn) = 1;
3206 mark_used (fn);
3208 DECL_BEFRIENDING_CLASSES (fn) = var;
3210 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3212 return fn;
3215 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3216 variable VAR. The wrapper function calls the init function (if any) for
3217 VAR and then returns a reference to VAR. The wrapper function is used
3218 in place of VAR everywhere VAR is mentioned. */
3220 tree
3221 get_tls_wrapper_fn (tree var)
3223 /* Only C++11 TLS vars need this wrapper fn. */
3224 if (!var_needs_tls_wrapper (var))
3225 return NULL_TREE;
3227 tree sname = mangle_tls_wrapper_fn (var);
3228 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3229 if (!fn)
3231 /* A named rvalue reference is an lvalue, so the wrapper should
3232 always return an lvalue reference. */
3233 tree type = non_reference (TREE_TYPE (var));
3234 type = build_reference_type (type);
3235 tree fntype = build_function_type (type, void_list_node);
3236 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3237 SET_DECL_LANGUAGE (fn, lang_c);
3238 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3239 DECL_ARTIFICIAL (fn) = true;
3240 DECL_IGNORED_P (fn) = 1;
3241 /* The wrapper is inline and emitted everywhere var is used. */
3242 DECL_DECLARED_INLINE_P (fn) = true;
3243 if (TREE_PUBLIC (var))
3245 comdat_linkage (fn);
3246 #ifdef HAVE_GAS_HIDDEN
3247 /* Make the wrapper bind locally; there's no reason to share
3248 the wrapper between multiple shared objects. */
3249 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3250 DECL_VISIBILITY_SPECIFIED (fn) = true;
3251 #endif
3253 if (!TREE_PUBLIC (fn))
3254 DECL_INTERFACE_KNOWN (fn) = true;
3255 mark_used (fn);
3256 note_vague_linkage_fn (fn);
3258 #if 0
3259 /* We want CSE to commonize calls to the wrapper, but marking it as
3260 pure is unsafe since it has side-effects. I guess we need a new
3261 ECF flag even weaker than ECF_PURE. FIXME! */
3262 DECL_PURE_P (fn) = true;
3263 #endif
3265 DECL_BEFRIENDING_CLASSES (fn) = var;
3267 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3269 return fn;
3272 /* At EOF, generate the definition for the TLS wrapper function FN:
3274 T& var_wrapper() {
3275 if (init_fn) init_fn();
3276 return var;
3277 } */
3279 static void
3280 generate_tls_wrapper (tree fn)
3282 tree var = DECL_BEFRIENDING_CLASSES (fn);
3284 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3285 tree body = begin_function_body ();
3286 /* Only call the init fn if there might be one. */
3287 if (tree init_fn = get_tls_init_fn (var))
3289 tree if_stmt = NULL_TREE;
3290 /* If init_fn is a weakref, make sure it exists before calling. */
3291 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3293 if_stmt = begin_if_stmt ();
3294 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3295 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3296 NE_EXPR, addr, nullptr_node,
3297 tf_warning_or_error);
3298 finish_if_stmt_cond (cond, if_stmt);
3300 finish_expr_stmt (build_cxx_call
3301 (init_fn, 0, NULL, tf_warning_or_error));
3302 if (if_stmt)
3304 finish_then_clause (if_stmt);
3305 finish_if_stmt (if_stmt);
3308 else
3309 /* If there's no initialization, the wrapper is a constant function. */
3310 TREE_READONLY (fn) = true;
3311 finish_return_stmt (convert_from_reference (var));
3312 finish_function_body (body);
3313 expand_or_defer_fn (finish_function (0));
3316 /* Start the process of running a particular set of global constructors
3317 or destructors. Subroutine of do_[cd]tors. Also called from
3318 vtv_start_verification_constructor_init_function. */
3320 static tree
3321 start_objects (int method_type, int initp)
3323 tree body;
3324 tree fndecl;
3325 char type[14];
3327 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3329 if (initp != DEFAULT_INIT_PRIORITY)
3331 char joiner;
3333 #ifdef JOINER
3334 joiner = JOINER;
3335 #else
3336 joiner = '_';
3337 #endif
3339 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3341 else
3342 sprintf (type, "sub_%c", method_type);
3344 fndecl = build_lang_decl (FUNCTION_DECL,
3345 get_file_function_name (type),
3346 build_function_type_list (void_type_node,
3347 NULL_TREE));
3348 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3350 TREE_PUBLIC (current_function_decl) = 0;
3352 /* Mark as artificial because it's not explicitly in the user's
3353 source code. */
3354 DECL_ARTIFICIAL (current_function_decl) = 1;
3356 /* Mark this declaration as used to avoid spurious warnings. */
3357 TREE_USED (current_function_decl) = 1;
3359 /* Mark this function as a global constructor or destructor. */
3360 if (method_type == 'I')
3361 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3362 else
3363 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3365 body = begin_compound_stmt (BCS_FN_BODY);
3367 return body;
3370 /* Finish the process of running a particular set of global constructors
3371 or destructors. Subroutine of do_[cd]tors. */
3373 static void
3374 finish_objects (int method_type, int initp, tree body)
3376 tree fn;
3378 /* Finish up. */
3379 finish_compound_stmt (body);
3380 fn = finish_function (0);
3382 if (method_type == 'I')
3384 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3385 decl_init_priority_insert (fn, initp);
3387 else
3389 DECL_STATIC_DESTRUCTOR (fn) = 1;
3390 decl_fini_priority_insert (fn, initp);
3393 expand_or_defer_fn (fn);
3396 /* The names of the parameters to the function created to handle
3397 initializations and destructions for objects with static storage
3398 duration. */
3399 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3400 #define PRIORITY_IDENTIFIER "__priority"
3402 /* The name of the function we create to handle initializations and
3403 destructions for objects with static storage duration. */
3404 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3406 /* The declaration for the __INITIALIZE_P argument. */
3407 static GTY(()) tree initialize_p_decl;
3409 /* The declaration for the __PRIORITY argument. */
3410 static GTY(()) tree priority_decl;
3412 /* The declaration for the static storage duration function. */
3413 static GTY(()) tree ssdf_decl;
3415 /* All the static storage duration functions created in this
3416 translation unit. */
3417 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3419 /* A map from priority levels to information about that priority
3420 level. There may be many such levels, so efficient lookup is
3421 important. */
3422 static splay_tree priority_info_map;
3424 /* Begins the generation of the function that will handle all
3425 initialization and destruction of objects with static storage
3426 duration. The function generated takes two parameters of type
3427 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3428 nonzero, it performs initializations. Otherwise, it performs
3429 destructions. It only performs those initializations or
3430 destructions with the indicated __PRIORITY. The generated function
3431 returns no value.
3433 It is assumed that this function will only be called once per
3434 translation unit. */
3436 static tree
3437 start_static_storage_duration_function (unsigned count)
3439 tree type;
3440 tree body;
3441 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3443 /* Create the identifier for this function. It will be of the form
3444 SSDF_IDENTIFIER_<number>. */
3445 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3447 type = build_function_type_list (void_type_node,
3448 integer_type_node, integer_type_node,
3449 NULL_TREE);
3451 /* Create the FUNCTION_DECL itself. */
3452 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3453 get_identifier (id),
3454 type);
3455 TREE_PUBLIC (ssdf_decl) = 0;
3456 DECL_ARTIFICIAL (ssdf_decl) = 1;
3458 /* Put this function in the list of functions to be called from the
3459 static constructors and destructors. */
3460 if (!ssdf_decls)
3462 vec_alloc (ssdf_decls, 32);
3464 /* Take this opportunity to initialize the map from priority
3465 numbers to information about that priority level. */
3466 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3467 /*delete_key_fn=*/0,
3468 /*delete_value_fn=*/
3469 (splay_tree_delete_value_fn) &free);
3471 /* We always need to generate functions for the
3472 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3473 priorities later, we'll be sure to find the
3474 DEFAULT_INIT_PRIORITY. */
3475 get_priority_info (DEFAULT_INIT_PRIORITY);
3478 vec_safe_push (ssdf_decls, ssdf_decl);
3480 /* Create the argument list. */
3481 initialize_p_decl = cp_build_parm_decl
3482 (ssdf_decl, get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3483 TREE_USED (initialize_p_decl) = 1;
3484 priority_decl = cp_build_parm_decl
3485 (ssdf_decl, get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3486 TREE_USED (priority_decl) = 1;
3488 DECL_CHAIN (initialize_p_decl) = priority_decl;
3489 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3491 /* Put the function in the global scope. */
3492 pushdecl (ssdf_decl);
3494 /* Start the function itself. This is equivalent to declaring the
3495 function as:
3497 static void __ssdf (int __initialize_p, init __priority_p);
3499 It is static because we only need to call this function from the
3500 various constructor and destructor functions for this module. */
3501 start_preparsed_function (ssdf_decl,
3502 /*attrs=*/NULL_TREE,
3503 SF_PRE_PARSED);
3505 /* Set up the scope of the outermost block in the function. */
3506 body = begin_compound_stmt (BCS_FN_BODY);
3508 return body;
3511 /* Finish the generation of the function which performs initialization
3512 and destruction of objects with static storage duration. After
3513 this point, no more such objects can be created. */
3515 static void
3516 finish_static_storage_duration_function (tree body)
3518 /* Close out the function. */
3519 finish_compound_stmt (body);
3520 expand_or_defer_fn (finish_function (0));
3523 /* Return the information about the indicated PRIORITY level. If no
3524 code to handle this level has yet been generated, generate the
3525 appropriate prologue. */
3527 static priority_info
3528 get_priority_info (int priority)
3530 priority_info pi;
3531 splay_tree_node n;
3533 n = splay_tree_lookup (priority_info_map,
3534 (splay_tree_key) priority);
3535 if (!n)
3537 /* Create a new priority information structure, and insert it
3538 into the map. */
3539 pi = XNEW (struct priority_info_s);
3540 pi->initializations_p = 0;
3541 pi->destructions_p = 0;
3542 splay_tree_insert (priority_info_map,
3543 (splay_tree_key) priority,
3544 (splay_tree_value) pi);
3546 else
3547 pi = (priority_info) n->value;
3549 return pi;
3552 /* The effective initialization priority of a DECL. */
3554 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3555 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3556 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3558 /* Whether a DECL needs a guard to protect it against multiple
3559 initialization. */
3561 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3562 || DECL_ONE_ONLY (decl) \
3563 || DECL_WEAK (decl)))
3565 /* Called from one_static_initialization_or_destruction(),
3566 via walk_tree.
3567 Walks the initializer list of a global variable and looks for
3568 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3569 and that have their DECL_CONTEXT() == NULL.
3570 For each such temporary variable, set their DECL_CONTEXT() to
3571 the current function. This is necessary because otherwise
3572 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3573 when trying to refer to a temporary variable that does not have
3574 it's DECL_CONTECT() properly set. */
3575 static tree
3576 fix_temporary_vars_context_r (tree *node,
3577 int * /*unused*/,
3578 void * /*unused1*/)
3580 gcc_assert (current_function_decl);
3582 if (TREE_CODE (*node) == BIND_EXPR)
3584 tree var;
3586 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3587 if (VAR_P (var)
3588 && !DECL_NAME (var)
3589 && DECL_ARTIFICIAL (var)
3590 && !DECL_CONTEXT (var))
3591 DECL_CONTEXT (var) = current_function_decl;
3594 return NULL_TREE;
3597 /* Set up to handle the initialization or destruction of DECL. If
3598 INITP is nonzero, we are initializing the variable. Otherwise, we
3599 are destroying it. */
3601 static void
3602 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3604 tree guard_if_stmt = NULL_TREE;
3605 tree guard;
3607 /* If we are supposed to destruct and there's a trivial destructor,
3608 nothing has to be done. */
3609 if (!initp
3610 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3611 return;
3613 /* Trick the compiler into thinking we are at the file and line
3614 where DECL was declared so that error-messages make sense, and so
3615 that the debugger will show somewhat sensible file and line
3616 information. */
3617 input_location = DECL_SOURCE_LOCATION (decl);
3619 /* Make sure temporary variables in the initialiser all have
3620 their DECL_CONTEXT() set to a value different from NULL_TREE.
3621 This can happen when global variables initializers are built.
3622 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3623 the temporary variables that might have been generated in the
3624 accompanying initializers is NULL_TREE, meaning the variables have been
3625 declared in the global namespace.
3626 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3627 of the temporaries are set to the current function decl. */
3628 cp_walk_tree_without_duplicates (&init,
3629 fix_temporary_vars_context_r,
3630 NULL);
3632 /* Because of:
3634 [class.access.spec]
3636 Access control for implicit calls to the constructors,
3637 the conversion functions, or the destructor called to
3638 create and destroy a static data member is performed as
3639 if these calls appeared in the scope of the member's
3640 class.
3642 we pretend we are in a static member function of the class of
3643 which the DECL is a member. */
3644 if (member_p (decl))
3646 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3647 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3650 /* Assume we don't need a guard. */
3651 guard = NULL_TREE;
3652 /* We need a guard if this is an object with external linkage that
3653 might be initialized in more than one place. (For example, a
3654 static data member of a template, when the data member requires
3655 construction.) */
3656 if (NEEDS_GUARD_P (decl))
3658 tree guard_cond;
3660 guard = get_guard (decl);
3662 /* When using __cxa_atexit, we just check the GUARD as we would
3663 for a local static. */
3664 if (flag_use_cxa_atexit)
3666 /* When using __cxa_atexit, we never try to destroy
3667 anything from a static destructor. */
3668 gcc_assert (initp);
3669 guard_cond = get_guard_cond (guard, false);
3671 /* If we don't have __cxa_atexit, then we will be running
3672 destructors from .fini sections, or their equivalents. So,
3673 we need to know how many times we've tried to initialize this
3674 object. We do initializations only if the GUARD is zero,
3675 i.e., if we are the first to initialize the variable. We do
3676 destructions only if the GUARD is one, i.e., if we are the
3677 last to destroy the variable. */
3678 else if (initp)
3679 guard_cond
3680 = cp_build_binary_op (input_location,
3681 EQ_EXPR,
3682 cp_build_unary_op (PREINCREMENT_EXPR,
3683 guard,
3684 /*noconvert=*/true,
3685 tf_warning_or_error),
3686 integer_one_node,
3687 tf_warning_or_error);
3688 else
3689 guard_cond
3690 = cp_build_binary_op (input_location,
3691 EQ_EXPR,
3692 cp_build_unary_op (PREDECREMENT_EXPR,
3693 guard,
3694 /*noconvert=*/true,
3695 tf_warning_or_error),
3696 integer_zero_node,
3697 tf_warning_or_error);
3699 guard_if_stmt = begin_if_stmt ();
3700 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3704 /* If we're using __cxa_atexit, we have not already set the GUARD,
3705 so we must do so now. */
3706 if (guard && initp && flag_use_cxa_atexit)
3707 finish_expr_stmt (set_guard (guard));
3709 /* Perform the initialization or destruction. */
3710 if (initp)
3712 if (init)
3714 finish_expr_stmt (init);
3715 if (sanitize_flags_p (SANITIZE_ADDRESS, decl))
3717 varpool_node *vnode = varpool_node::get (decl);
3718 if (vnode)
3719 vnode->dynamically_initialized = 1;
3723 /* If we're using __cxa_atexit, register a function that calls the
3724 destructor for the object. */
3725 if (flag_use_cxa_atexit)
3726 finish_expr_stmt (register_dtor_fn (decl));
3728 else
3729 finish_expr_stmt (build_cleanup (decl));
3731 /* Finish the guard if-stmt, if necessary. */
3732 if (guard)
3734 finish_then_clause (guard_if_stmt);
3735 finish_if_stmt (guard_if_stmt);
3738 /* Now that we're done with DECL we don't need to pretend to be a
3739 member of its class any longer. */
3740 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3741 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3744 /* Generate code to do the initialization or destruction of the decls in VARS,
3745 a TREE_LIST of VAR_DECL with static storage duration.
3746 Whether initialization or destruction is performed is specified by INITP. */
3748 static void
3749 do_static_initialization_or_destruction (tree vars, bool initp)
3751 tree node, init_if_stmt, cond;
3753 /* Build the outer if-stmt to check for initialization or destruction. */
3754 init_if_stmt = begin_if_stmt ();
3755 cond = initp ? integer_one_node : integer_zero_node;
3756 cond = cp_build_binary_op (input_location,
3757 EQ_EXPR,
3758 initialize_p_decl,
3759 cond,
3760 tf_warning_or_error);
3761 finish_if_stmt_cond (cond, init_if_stmt);
3763 /* To make sure dynamic construction doesn't access globals from other
3764 compilation units where they might not be yet constructed, for
3765 -fsanitize=address insert __asan_before_dynamic_init call that
3766 prevents access to either all global variables that need construction
3767 in other compilation units, or at least those that haven't been
3768 initialized yet. Variables that need dynamic construction in
3769 the current compilation unit are kept accessible. */
3770 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
3771 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3773 node = vars;
3774 do {
3775 tree decl = TREE_VALUE (node);
3776 tree priority_if_stmt;
3777 int priority;
3778 priority_info pi;
3780 /* If we don't need a destructor, there's nothing to do. Avoid
3781 creating a possibly empty if-stmt. */
3782 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3784 node = TREE_CHAIN (node);
3785 continue;
3788 /* Remember that we had an initialization or finalization at this
3789 priority. */
3790 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3791 pi = get_priority_info (priority);
3792 if (initp)
3793 pi->initializations_p = 1;
3794 else
3795 pi->destructions_p = 1;
3797 /* Conditionalize this initialization on being in the right priority
3798 and being initializing/finalizing appropriately. */
3799 priority_if_stmt = begin_if_stmt ();
3800 cond = cp_build_binary_op (input_location,
3801 EQ_EXPR,
3802 priority_decl,
3803 build_int_cst (NULL_TREE, priority),
3804 tf_warning_or_error);
3805 finish_if_stmt_cond (cond, priority_if_stmt);
3807 /* Process initializers with same priority. */
3808 for (; node
3809 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3810 node = TREE_CHAIN (node))
3811 /* Do one initialization or destruction. */
3812 one_static_initialization_or_destruction (TREE_VALUE (node),
3813 TREE_PURPOSE (node), initp);
3815 /* Finish up the priority if-stmt body. */
3816 finish_then_clause (priority_if_stmt);
3817 finish_if_stmt (priority_if_stmt);
3819 } while (node);
3821 /* Revert what __asan_before_dynamic_init did by calling
3822 __asan_after_dynamic_init. */
3823 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
3824 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3826 /* Finish up the init/destruct if-stmt body. */
3827 finish_then_clause (init_if_stmt);
3828 finish_if_stmt (init_if_stmt);
3831 /* VARS is a list of variables with static storage duration which may
3832 need initialization and/or finalization. Remove those variables
3833 that don't really need to be initialized or finalized, and return
3834 the resulting list. The order in which the variables appear in
3835 VARS is in reverse order of the order in which they should actually
3836 be initialized. The list we return is in the unreversed order;
3837 i.e., the first variable should be initialized first. */
3839 static tree
3840 prune_vars_needing_no_initialization (tree *vars)
3842 tree *var = vars;
3843 tree result = NULL_TREE;
3845 while (*var)
3847 tree t = *var;
3848 tree decl = TREE_VALUE (t);
3849 tree init = TREE_PURPOSE (t);
3851 /* Deal gracefully with error. */
3852 if (error_operand_p (decl))
3854 var = &TREE_CHAIN (t);
3855 continue;
3858 /* The only things that can be initialized are variables. */
3859 gcc_assert (VAR_P (decl));
3861 /* If this object is not defined, we don't need to do anything
3862 here. */
3863 if (DECL_EXTERNAL (decl))
3865 var = &TREE_CHAIN (t);
3866 continue;
3869 /* Also, if the initializer already contains errors, we can bail
3870 out now. */
3871 if (init && TREE_CODE (init) == TREE_LIST
3872 && value_member (error_mark_node, init))
3874 var = &TREE_CHAIN (t);
3875 continue;
3878 /* This variable is going to need initialization and/or
3879 finalization, so we add it to the list. */
3880 *var = TREE_CHAIN (t);
3881 TREE_CHAIN (t) = result;
3882 result = t;
3885 return result;
3888 /* Make sure we have told the back end about all the variables in
3889 VARS. */
3891 static void
3892 write_out_vars (tree vars)
3894 tree v;
3896 for (v = vars; v; v = TREE_CHAIN (v))
3898 tree var = TREE_VALUE (v);
3899 if (!var_finalized_p (var))
3901 import_export_decl (var);
3902 rest_of_decl_compilation (var, 1, 1);
3907 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3908 (otherwise) that will initialize all global objects with static
3909 storage duration having the indicated PRIORITY. */
3911 static void
3912 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3913 location_t *locus)
3915 char function_key;
3916 tree fndecl;
3917 tree body;
3918 size_t i;
3920 input_location = *locus;
3921 /* ??? */
3922 /* Was: locus->line++; */
3924 /* We use `I' to indicate initialization and `D' to indicate
3925 destruction. */
3926 function_key = constructor_p ? 'I' : 'D';
3928 /* We emit the function lazily, to avoid generating empty
3929 global constructors and destructors. */
3930 body = NULL_TREE;
3932 /* For Objective-C++, we may need to initialize metadata found in this module.
3933 This must be done _before_ any other static initializations. */
3934 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3935 && constructor_p && objc_static_init_needed_p ())
3937 body = start_objects (function_key, priority);
3938 objc_generate_static_init_call (NULL_TREE);
3941 /* Call the static storage duration function with appropriate
3942 arguments. */
3943 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
3945 /* Calls to pure or const functions will expand to nothing. */
3946 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3948 tree call;
3950 if (! body)
3951 body = start_objects (function_key, priority);
3953 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
3954 build_int_cst (NULL_TREE,
3955 constructor_p),
3956 build_int_cst (NULL_TREE,
3957 priority),
3958 NULL_TREE);
3959 finish_expr_stmt (call);
3963 /* Close out the function. */
3964 if (body)
3965 finish_objects (function_key, priority, body);
3968 /* Generate constructor and destructor functions for the priority
3969 indicated by N. */
3971 static int
3972 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
3974 location_t *locus = (location_t *) data;
3975 int priority = (int) n->key;
3976 priority_info pi = (priority_info) n->value;
3978 /* Generate the functions themselves, but only if they are really
3979 needed. */
3980 if (pi->initializations_p)
3981 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
3982 if (pi->destructions_p)
3983 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
3985 /* Keep iterating. */
3986 return 0;
3989 /* Return C++ property of T, based on given operation OP. */
3991 static int
3992 cpp_check (tree t, cpp_operation op)
3994 switch (op)
3996 case HAS_DEPENDENT_TEMPLATE_ARGS:
3998 tree ti = CLASSTYPE_TEMPLATE_INFO (t);
3999 if (!ti)
4000 return 0;
4001 ++processing_template_decl;
4002 const bool dep = any_dependent_template_arguments_p (TI_ARGS (ti));
4003 --processing_template_decl;
4004 return dep;
4006 case IS_ABSTRACT:
4007 return DECL_PURE_VIRTUAL_P (t);
4008 case IS_CONSTRUCTOR:
4009 return DECL_CONSTRUCTOR_P (t);
4010 case IS_DESTRUCTOR:
4011 return DECL_DESTRUCTOR_P (t);
4012 case IS_COPY_CONSTRUCTOR:
4013 return DECL_COPY_CONSTRUCTOR_P (t);
4014 case IS_MOVE_CONSTRUCTOR:
4015 return DECL_MOVE_CONSTRUCTOR_P (t);
4016 case IS_TEMPLATE:
4017 return TREE_CODE (t) == TEMPLATE_DECL;
4018 case IS_TRIVIAL:
4019 return trivial_type_p (t);
4020 default:
4021 return 0;
4025 /* Collect source file references recursively, starting from NAMESPC. */
4027 static void
4028 collect_source_refs (tree namespc)
4030 /* Iterate over names in this name space. */
4031 for (tree t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4032 if (DECL_IS_BUILTIN (t))
4034 else if (TREE_CODE (t) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (t))
4035 collect_source_refs (t);
4036 else
4037 collect_source_ref (DECL_SOURCE_FILE (t));
4040 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4041 starting from NAMESPC. */
4043 static void
4044 collect_ada_namespace (tree namespc, const char *source_file)
4046 tree decl = NAMESPACE_LEVEL (namespc)->names;
4048 /* Collect decls from this namespace. This will skip
4049 NAMESPACE_DECLs (both aliases and regular, it cannot tell). */
4050 collect_ada_nodes (decl, source_file);
4052 /* Now scan for namespace children, and dump them. */
4053 for (; decl; decl = TREE_CHAIN (decl))
4054 if (TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl))
4055 collect_ada_namespace (decl, source_file);
4058 /* Returns true iff there is a definition available for variable or
4059 function DECL. */
4061 bool
4062 decl_defined_p (tree decl)
4064 if (TREE_CODE (decl) == FUNCTION_DECL)
4065 return (DECL_INITIAL (decl) != NULL_TREE
4066 /* A pending instantiation of a friend temploid is defined. */
4067 || (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
4068 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4069 (DECL_TI_TEMPLATE (decl)))));
4070 else
4072 gcc_assert (VAR_P (decl));
4073 return !DECL_EXTERNAL (decl);
4077 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4079 [expr.const]
4081 An integral constant-expression can only involve ... const
4082 variables of integral or enumeration types initialized with
4083 constant expressions ...
4085 C++0x also allows constexpr variables and temporaries initialized
4086 with constant expressions. We handle the former here, but the latter
4087 are just folded away in cxx_eval_constant_expression.
4089 The standard does not require that the expression be non-volatile.
4090 G++ implements the proposed correction in DR 457. */
4092 bool
4093 decl_constant_var_p (tree decl)
4095 if (!decl_maybe_constant_var_p (decl))
4096 return false;
4098 /* We don't know if a template static data member is initialized with
4099 a constant expression until we instantiate its initializer. Even
4100 in the case of a constexpr variable, we can't treat it as a
4101 constant until its initializer is complete in case it's used in
4102 its own initializer. */
4103 maybe_instantiate_decl (decl);
4104 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4107 /* Returns true if DECL could be a symbolic constant variable, depending on
4108 its initializer. */
4110 bool
4111 decl_maybe_constant_var_p (tree decl)
4113 tree type = TREE_TYPE (decl);
4114 if (!VAR_P (decl))
4115 return false;
4116 if (DECL_DECLARED_CONSTEXPR_P (decl))
4117 return true;
4118 if (DECL_HAS_VALUE_EXPR_P (decl))
4119 /* A proxy isn't constant. */
4120 return false;
4121 if (TREE_CODE (type) == REFERENCE_TYPE)
4122 /* References can be constant. */;
4123 else if (CP_TYPE_CONST_NON_VOLATILE_P (type)
4124 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4125 /* And const integers. */;
4126 else
4127 return false;
4129 if (DECL_INITIAL (decl)
4130 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
4131 /* We know the initializer, and it isn't constant. */
4132 return false;
4133 else
4134 return true;
4137 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4138 called from grokfndecl and grokvardecl; in all modes it is called from
4139 cp_write_global_declarations. */
4141 void
4142 no_linkage_error (tree decl)
4144 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4145 /* In C++11 it's ok if the decl is defined. */
4146 return;
4147 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4148 if (t == NULL_TREE)
4149 /* The type that got us on no_linkage_decls must have gotten a name for
4150 linkage purposes. */;
4151 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4152 /* The type might end up having a typedef name for linkage purposes. */
4153 vec_safe_push (no_linkage_decls, decl);
4154 else if (TYPE_UNNAMED_P (t))
4156 bool d = false;
4157 if (cxx_dialect >= cxx11)
4158 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4159 "unnamed type, is used but never defined", decl);
4160 else if (DECL_EXTERN_C_P (decl))
4161 /* Allow this; it's pretty common in C. */;
4162 else if (VAR_P (decl))
4163 /* DRs 132, 319 and 389 seem to indicate types with
4164 no linkage can only be used to declare extern "C"
4165 entities. Since it's not always an error in the
4166 ISO C++ 90 Standard, we only issue a warning. */
4167 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "unnamed type "
4168 "with no linkage used to declare variable %q#D with "
4169 "linkage", decl);
4170 else
4171 d = permerror (DECL_SOURCE_LOCATION (decl), "unnamed type with no "
4172 "linkage used to declare function %q#D with linkage",
4173 decl);
4174 if (d && is_typedef_decl (TYPE_NAME (t)))
4175 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4176 "to the unqualified type, so it is not used for linkage",
4177 TYPE_NAME (t));
4179 else if (cxx_dialect >= cxx11)
4181 if (VAR_P (decl) || !DECL_PURE_VIRTUAL_P (decl))
4182 permerror (DECL_SOURCE_LOCATION (decl),
4183 "%q#D, declared using local type "
4184 "%qT, is used but never defined", decl, t);
4186 else if (VAR_P (decl))
4187 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4188 "used to declare variable %q#D with linkage", t, decl);
4189 else
4190 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4191 "to declare function %q#D with linkage", t, decl);
4194 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4196 static void
4197 collect_all_refs (const char *source_file)
4199 collect_ada_namespace (global_namespace, source_file);
4202 /* Clear DECL_EXTERNAL for NODE. */
4204 static bool
4205 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4207 DECL_EXTERNAL (node->decl) = 0;
4208 return false;
4211 /* Build up the function to run dynamic initializers for thread_local
4212 variables in this translation unit and alias the init functions for the
4213 individual variables to it. */
4215 static void
4216 handle_tls_init (void)
4218 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4219 if (vars == NULL_TREE)
4220 return;
4222 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4224 write_out_vars (vars);
4226 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4227 boolean_type_node);
4228 TREE_PUBLIC (guard) = false;
4229 TREE_STATIC (guard) = true;
4230 DECL_ARTIFICIAL (guard) = true;
4231 DECL_IGNORED_P (guard) = true;
4232 TREE_USED (guard) = true;
4233 CP_DECL_THREAD_LOCAL_P (guard) = true;
4234 set_decl_tls_model (guard, decl_default_tls_model (guard));
4235 pushdecl_top_level_and_finish (guard, NULL_TREE);
4237 tree fn = get_local_tls_init_fn ();
4238 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4239 tree body = begin_function_body ();
4240 tree if_stmt = begin_if_stmt ();
4241 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4242 tf_warning_or_error);
4243 finish_if_stmt_cond (cond, if_stmt);
4244 finish_expr_stmt (cp_build_modify_expr (loc, guard, NOP_EXPR,
4245 boolean_true_node,
4246 tf_warning_or_error));
4247 for (; vars; vars = TREE_CHAIN (vars))
4249 tree var = TREE_VALUE (vars);
4250 tree init = TREE_PURPOSE (vars);
4251 one_static_initialization_or_destruction (var, init, true);
4253 /* Output init aliases even with -fno-extern-tls-init. */
4254 if (TARGET_SUPPORTS_ALIASES && TREE_PUBLIC (var))
4256 tree single_init_fn = get_tls_init_fn (var);
4257 if (single_init_fn == NULL_TREE)
4258 continue;
4259 cgraph_node *alias
4260 = cgraph_node::get_create (fn)->create_same_body_alias
4261 (single_init_fn, fn);
4262 gcc_assert (alias != NULL);
4266 finish_then_clause (if_stmt);
4267 finish_if_stmt (if_stmt);
4268 finish_function_body (body);
4269 expand_or_defer_fn (finish_function (0));
4272 /* We're at the end of compilation, so generate any mangling aliases that
4273 we've been saving up, if DECL is going to be output and ID2 isn't
4274 already taken by another declaration. */
4276 static void
4277 generate_mangling_alias (tree decl, tree id2)
4279 /* If there's a declaration already using this mangled name,
4280 don't create a compatibility alias that conflicts. */
4281 if (IDENTIFIER_GLOBAL_VALUE (id2))
4282 return;
4284 struct cgraph_node *n = NULL;
4285 if (TREE_CODE (decl) == FUNCTION_DECL
4286 && !(n = cgraph_node::get (decl)))
4287 /* Don't create an alias to an unreferenced function. */
4288 return;
4290 tree alias = make_alias_for (decl, id2);
4291 SET_IDENTIFIER_GLOBAL_VALUE (id2, alias);
4292 DECL_IGNORED_P (alias) = 1;
4293 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
4294 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
4295 if (vague_linkage_p (decl))
4296 DECL_WEAK (alias) = 1;
4297 if (TREE_CODE (decl) == FUNCTION_DECL)
4298 n->create_same_body_alias (alias, decl);
4299 else
4300 varpool_node::create_extra_name_alias (alias, decl);
4303 /* Note that we might want to emit an alias with the symbol ID2 for DECL at
4304 the end of translation, for compatibility across bugs in the mangling
4305 implementation. */
4307 void
4308 note_mangling_alias (tree decl, tree id2)
4310 if (TARGET_SUPPORTS_ALIASES)
4312 if (!defer_mangling_aliases)
4313 generate_mangling_alias (decl, id2);
4314 else
4316 vec_safe_push (mangling_aliases, decl);
4317 vec_safe_push (mangling_aliases, id2);
4322 /* Emit all mangling aliases that were deferred up to this point. */
4324 void
4325 generate_mangling_aliases ()
4327 while (!vec_safe_is_empty (mangling_aliases))
4329 tree id2 = mangling_aliases->pop();
4330 tree decl = mangling_aliases->pop();
4331 generate_mangling_alias (decl, id2);
4333 defer_mangling_aliases = false;
4336 /* The entire file is now complete. If requested, dump everything
4337 to a file. */
4339 static void
4340 dump_tu (void)
4342 dump_flags_t flags;
4343 if (FILE *stream = dump_begin (raw_dump_id, &flags))
4345 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4346 dump_end (raw_dump_id, stream);
4350 static location_t locus_at_end_of_parsing;
4352 /* Check the deallocation functions for CODE to see if we want to warn that
4353 only one was defined. */
4355 static void
4356 maybe_warn_sized_delete (enum tree_code code)
4358 tree sized = NULL_TREE;
4359 tree unsized = NULL_TREE;
4361 for (ovl_iterator iter (IDENTIFIER_GLOBAL_VALUE (cp_operator_id (code)));
4362 iter; ++iter)
4364 tree fn = *iter;
4365 /* We're only interested in usual deallocation functions. */
4366 if (!usual_deallocation_fn_p (fn))
4367 continue;
4368 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
4369 unsized = fn;
4370 else
4371 sized = fn;
4373 if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
4374 warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
4375 "the program should also define %qD", sized);
4376 else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
4377 warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
4378 "the program should also define %qD", unsized);
4381 /* Check the global deallocation functions to see if we want to warn about
4382 defining unsized without sized (or vice versa). */
4384 static void
4385 maybe_warn_sized_delete ()
4387 if (!flag_sized_deallocation || !warn_sized_deallocation)
4388 return;
4389 maybe_warn_sized_delete (DELETE_EXPR);
4390 maybe_warn_sized_delete (VEC_DELETE_EXPR);
4393 /* Earlier we left PTRMEM_CST in variable initializers alone so that we could
4394 look them up when evaluating non-type template parameters. Now we need to
4395 lower them to something the back end can understand. */
4397 static void
4398 lower_var_init ()
4400 varpool_node *node;
4401 FOR_EACH_VARIABLE (node)
4403 tree d = node->decl;
4404 if (tree init = DECL_INITIAL (d))
4405 DECL_INITIAL (d) = cplus_expand_constant (init);
4409 /* This routine is called at the end of compilation.
4410 Its job is to create all the code needed to initialize and
4411 destroy the global aggregates. We do the destruction
4412 first, since that way we only need to reverse the decls once. */
4414 void
4415 c_parse_final_cleanups (void)
4417 tree vars;
4418 bool reconsider;
4419 size_t i;
4420 unsigned ssdf_count = 0;
4421 int retries = 0;
4422 tree decl;
4424 locus_at_end_of_parsing = input_location;
4425 at_eof = 1;
4427 /* Bad parse errors. Just forget about it. */
4428 if (! global_bindings_p () || current_class_type
4429 || !vec_safe_is_empty (decl_namespace_list))
4430 return;
4432 /* This is the point to write out a PCH if we're doing that.
4433 In that case we do not want to do anything else. */
4434 if (pch_file)
4436 /* Mangle all symbols at PCH creation time. */
4437 symtab_node *node;
4438 FOR_EACH_SYMBOL (node)
4439 if (! is_a <varpool_node *> (node)
4440 || ! DECL_HARD_REGISTER (node->decl))
4441 DECL_ASSEMBLER_NAME (node->decl);
4442 c_common_write_pch ();
4443 dump_tu ();
4444 /* Ensure even the callers don't try to finalize the CU. */
4445 flag_syntax_only = 1;
4446 return;
4449 timevar_stop (TV_PHASE_PARSING);
4450 timevar_start (TV_PHASE_DEFERRED);
4452 symtab->process_same_body_aliases ();
4454 /* Handle -fdump-ada-spec[-slim] */
4455 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4457 if (flag_dump_ada_spec_slim)
4458 collect_source_ref (main_input_filename);
4459 else
4460 collect_source_refs (global_namespace);
4462 dump_ada_specs (collect_all_refs, cpp_check);
4465 /* FIXME - huh? was input_line -= 1;*/
4467 /* We now have to write out all the stuff we put off writing out.
4468 These include:
4470 o Template specializations that we have not yet instantiated,
4471 but which are needed.
4472 o Initialization and destruction for non-local objects with
4473 static storage duration. (Local objects with static storage
4474 duration are initialized when their scope is first entered,
4475 and are cleaned up via atexit.)
4476 o Virtual function tables.
4478 All of these may cause others to be needed. For example,
4479 instantiating one function may cause another to be needed, and
4480 generating the initializer for an object may cause templates to be
4481 instantiated, etc., etc. */
4483 emit_support_tinfos ();
4487 tree t;
4488 tree decl;
4490 reconsider = false;
4492 /* If there are templates that we've put off instantiating, do
4493 them now. */
4494 instantiate_pending_templates (retries);
4495 ggc_collect ();
4497 /* Write out virtual tables as required. Writing out the
4498 virtual table for a template class may cause the
4499 instantiation of members of that class. If we write out
4500 vtables then we remove the class from our list so we don't
4501 have to look at it again. */
4502 for (i = keyed_classes->length ();
4503 keyed_classes->iterate (--i, &t);)
4504 if (maybe_emit_vtables (t))
4506 reconsider = true;
4507 keyed_classes->unordered_remove (i);
4510 /* Write out needed type info variables. We have to be careful
4511 looping through unemitted decls, because emit_tinfo_decl may
4512 cause other variables to be needed. New elements will be
4513 appended, and we remove from the vector those that actually
4514 get emitted. */
4515 for (i = unemitted_tinfo_decls->length ();
4516 unemitted_tinfo_decls->iterate (--i, &t);)
4517 if (emit_tinfo_decl (t))
4519 reconsider = true;
4520 unemitted_tinfo_decls->unordered_remove (i);
4523 /* The list of objects with static storage duration is built up
4524 in reverse order. We clear STATIC_AGGREGATES so that any new
4525 aggregates added during the initialization of these will be
4526 initialized in the correct order when we next come around the
4527 loop. */
4528 vars = prune_vars_needing_no_initialization (&static_aggregates);
4530 if (vars)
4532 /* We need to start a new initialization function each time
4533 through the loop. That's because we need to know which
4534 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4535 isn't computed until a function is finished, and written
4536 out. That's a deficiency in the back end. When this is
4537 fixed, these initialization functions could all become
4538 inline, with resulting performance improvements. */
4539 tree ssdf_body;
4541 /* Set the line and file, so that it is obviously not from
4542 the source file. */
4543 input_location = locus_at_end_of_parsing;
4544 ssdf_body = start_static_storage_duration_function (ssdf_count);
4546 /* Make sure the back end knows about all the variables. */
4547 write_out_vars (vars);
4549 /* First generate code to do all the initializations. */
4550 if (vars)
4551 do_static_initialization_or_destruction (vars, /*initp=*/true);
4553 /* Then, generate code to do all the destructions. Do these
4554 in reverse order so that the most recently constructed
4555 variable is the first destroyed. If we're using
4556 __cxa_atexit, then we don't need to do this; functions
4557 were registered at initialization time to destroy the
4558 local statics. */
4559 if (!flag_use_cxa_atexit && vars)
4561 vars = nreverse (vars);
4562 do_static_initialization_or_destruction (vars, /*initp=*/false);
4564 else
4565 vars = NULL_TREE;
4567 /* Finish up the static storage duration function for this
4568 round. */
4569 input_location = locus_at_end_of_parsing;
4570 finish_static_storage_duration_function (ssdf_body);
4572 /* All those initializations and finalizations might cause
4573 us to need more inline functions, more template
4574 instantiations, etc. */
4575 reconsider = true;
4576 ssdf_count++;
4577 /* ??? was: locus_at_end_of_parsing.line++; */
4580 /* Now do the same for thread_local variables. */
4581 handle_tls_init ();
4583 /* Go through the set of inline functions whose bodies have not
4584 been emitted yet. If out-of-line copies of these functions
4585 are required, emit them. */
4586 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4588 /* Does it need synthesizing? */
4589 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4590 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4592 /* Even though we're already at the top-level, we push
4593 there again. That way, when we pop back a few lines
4594 hence, all of our state is restored. Otherwise,
4595 finish_function doesn't clean things up, and we end
4596 up with CURRENT_FUNCTION_DECL set. */
4597 push_to_top_level ();
4598 /* The decl's location will mark where it was first
4599 needed. Save that so synthesize method can indicate
4600 where it was needed from, in case of error */
4601 input_location = DECL_SOURCE_LOCATION (decl);
4602 synthesize_method (decl);
4603 pop_from_top_level ();
4604 reconsider = true;
4607 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4608 generate_tls_wrapper (decl);
4610 if (!DECL_SAVED_TREE (decl))
4611 continue;
4613 cgraph_node *node = cgraph_node::get_create (decl);
4615 /* We lie to the back end, pretending that some functions
4616 are not defined when they really are. This keeps these
4617 functions from being put out unnecessarily. But, we must
4618 stop lying when the functions are referenced, or if they
4619 are not comdat since they need to be put out now. If
4620 DECL_INTERFACE_KNOWN, then we have already set
4621 DECL_EXTERNAL appropriately, so there's no need to check
4622 again, and we do not want to clear DECL_EXTERNAL if a
4623 previous call to import_export_decl set it.
4625 This is done in a separate for cycle, because if some
4626 deferred function is contained in another deferred
4627 function later in deferred_fns varray,
4628 rest_of_compilation would skip this function and we
4629 really cannot expand the same function twice. */
4630 import_export_decl (decl);
4631 if (DECL_NOT_REALLY_EXTERN (decl)
4632 && DECL_INITIAL (decl)
4633 && decl_needed_p (decl))
4635 if (node->cpp_implicit_alias)
4636 node = node->get_alias_target ();
4638 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
4639 NULL, true);
4640 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4641 group, we need to mark all symbols in the same comdat group
4642 that way. */
4643 if (node->same_comdat_group)
4644 for (cgraph_node *next
4645 = dyn_cast<cgraph_node *> (node->same_comdat_group);
4646 next != node;
4647 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
4648 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
4649 NULL, true);
4652 /* If we're going to need to write this function out, and
4653 there's already a body for it, create RTL for it now.
4654 (There might be no body if this is a method we haven't
4655 gotten around to synthesizing yet.) */
4656 if (!DECL_EXTERNAL (decl)
4657 && decl_needed_p (decl)
4658 && !TREE_ASM_WRITTEN (decl)
4659 && !node->definition)
4661 /* We will output the function; no longer consider it in this
4662 loop. */
4663 DECL_DEFER_OUTPUT (decl) = 0;
4664 /* Generate RTL for this function now that we know we
4665 need it. */
4666 expand_or_defer_fn (decl);
4667 /* If we're compiling -fsyntax-only pretend that this
4668 function has been written out so that we don't try to
4669 expand it again. */
4670 if (flag_syntax_only)
4671 TREE_ASM_WRITTEN (decl) = 1;
4672 reconsider = true;
4676 if (wrapup_namespace_globals ())
4677 reconsider = true;
4679 /* Static data members are just like namespace-scope globals. */
4680 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4682 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4683 /* Don't write it out if we haven't seen a definition. */
4684 || (DECL_IN_AGGR_P (decl) && !DECL_INLINE_VAR_P (decl)))
4685 continue;
4686 import_export_decl (decl);
4687 /* If this static data member is needed, provide it to the
4688 back end. */
4689 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4690 DECL_EXTERNAL (decl) = 0;
4692 if (vec_safe_length (pending_statics) != 0
4693 && wrapup_global_declarations (pending_statics->address (),
4694 pending_statics->length ()))
4695 reconsider = true;
4697 retries++;
4699 while (reconsider);
4701 lower_var_init ();
4703 generate_mangling_aliases ();
4705 /* All used inline functions must have a definition at this point. */
4706 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4708 if (/* Check online inline functions that were actually used. */
4709 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4710 /* If the definition actually was available here, then the
4711 fact that the function was not defined merely represents
4712 that for some reason (use of a template repository,
4713 #pragma interface, etc.) we decided not to emit the
4714 definition here. */
4715 && !DECL_INITIAL (decl)
4716 /* Don't complain if the template was defined. */
4717 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4718 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4719 (template_for_substitution (decl)))))
4721 warning_at (DECL_SOURCE_LOCATION (decl), 0,
4722 "inline function %qD used but never defined", decl);
4723 /* Avoid a duplicate warning from check_global_declaration. */
4724 TREE_NO_WARNING (decl) = 1;
4728 /* So must decls that use a type with no linkage. */
4729 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4730 no_linkage_error (decl);
4732 maybe_warn_sized_delete ();
4734 /* Then, do the Objective-C stuff. This is where all the
4735 Objective-C module stuff gets generated (symtab,
4736 class/protocol/selector lists etc). This must be done after C++
4737 templates, destructors etc. so that selectors used in C++
4738 templates are properly allocated. */
4739 if (c_dialect_objc ())
4740 objc_write_global_declarations ();
4742 /* We give C linkage to static constructors and destructors. */
4743 push_lang_context (lang_name_c);
4745 /* Generate initialization and destruction functions for all
4746 priorities for which they are required. */
4747 if (priority_info_map)
4748 splay_tree_foreach (priority_info_map,
4749 generate_ctor_and_dtor_functions_for_priority,
4750 /*data=*/&locus_at_end_of_parsing);
4751 else if (c_dialect_objc () && objc_static_init_needed_p ())
4752 /* If this is obj-c++ and we need a static init, call
4753 generate_ctor_or_dtor_function. */
4754 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4755 DEFAULT_INIT_PRIORITY,
4756 &locus_at_end_of_parsing);
4758 /* We're done with the splay-tree now. */
4759 if (priority_info_map)
4760 splay_tree_delete (priority_info_map);
4762 /* Generate any missing aliases. */
4763 maybe_apply_pending_pragma_weaks ();
4765 /* We're done with static constructors, so we can go back to "C++"
4766 linkage now. */
4767 pop_lang_context ();
4769 if (flag_vtable_verify)
4771 vtv_recover_class_info ();
4772 vtv_compute_class_hierarchy_transitive_closure ();
4773 vtv_build_vtable_verify_fndecl ();
4776 perform_deferred_noexcept_checks ();
4778 finish_repo ();
4779 fini_constexpr ();
4781 /* The entire file is now complete. If requested, dump everything
4782 to a file. */
4783 dump_tu ();
4785 if (flag_detailed_statistics)
4787 dump_tree_statistics ();
4788 dump_time_statistics ();
4791 timevar_stop (TV_PHASE_DEFERRED);
4792 timevar_start (TV_PHASE_PARSING);
4794 /* Indicate that we're done with front end processing. */
4795 at_eof = 2;
4798 /* Perform any post compilation-proper cleanups for the C++ front-end.
4799 This should really go away. No front-end should need to do
4800 anything past the compilation process. */
4802 void
4803 cxx_post_compilation_parsing_cleanups (void)
4805 timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS);
4807 if (flag_vtable_verify)
4809 /* Generate the special constructor initialization function that
4810 calls __VLTRegisterPairs, and give it a very high
4811 initialization priority. This must be done after
4812 finalize_compilation_unit so that we have accurate
4813 information about which vtable will actually be emitted. */
4814 vtv_generate_init_routine ();
4817 input_location = locus_at_end_of_parsing;
4819 if (flag_checking)
4820 validate_conversion_obstack ();
4822 timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS);
4825 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4826 function to call in parse-tree form; it has not yet been
4827 semantically analyzed. ARGS are the arguments to the function.
4828 They have already been semantically analyzed. This may change
4829 ARGS. */
4831 tree
4832 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4833 tsubst_flags_t complain)
4835 tree orig_fn;
4836 vec<tree, va_gc> *orig_args = NULL;
4837 tree expr;
4838 tree object;
4840 orig_fn = fn;
4841 object = TREE_OPERAND (fn, 0);
4843 if (processing_template_decl)
4845 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4846 || TREE_CODE (fn) == MEMBER_REF);
4847 if (type_dependent_expression_p (fn)
4848 || any_type_dependent_arguments_p (*args))
4849 return build_min_nt_call_vec (fn, *args);
4851 orig_args = make_tree_vector_copy (*args);
4853 /* Transform the arguments and add the implicit "this"
4854 parameter. That must be done before the FN is transformed
4855 because we depend on the form of FN. */
4856 make_args_non_dependent (*args);
4857 object = build_non_dependent_expr (object);
4858 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4860 if (TREE_CODE (fn) == DOTSTAR_EXPR)
4861 object = cp_build_addr_expr (object, complain);
4862 vec_safe_insert (*args, 0, object);
4864 /* Now that the arguments are done, transform FN. */
4865 fn = build_non_dependent_expr (fn);
4868 /* A qualified name corresponding to a bound pointer-to-member is
4869 represented as an OFFSET_REF:
4871 struct B { void g(); };
4872 void (B::*p)();
4873 void B::g() { (this->*p)(); } */
4874 if (TREE_CODE (fn) == OFFSET_REF)
4876 tree object_addr = cp_build_addr_expr (object, complain);
4877 fn = TREE_OPERAND (fn, 1);
4878 fn = get_member_function_from_ptrfunc (&object_addr, fn,
4879 complain);
4880 vec_safe_insert (*args, 0, object_addr);
4883 if (CLASS_TYPE_P (TREE_TYPE (fn)))
4884 expr = build_op_call (fn, args, complain);
4885 else
4886 expr = cp_build_function_call_vec (fn, args, complain);
4887 if (processing_template_decl && expr != error_mark_node)
4888 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
4890 if (orig_args != NULL)
4891 release_tree_vector (orig_args);
4893 return expr;
4897 void
4898 check_default_args (tree x)
4900 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4901 bool saw_def = false;
4902 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4903 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4905 if (TREE_PURPOSE (arg))
4906 saw_def = true;
4907 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
4909 error ("default argument missing for parameter %P of %q+#D", i, x);
4910 TREE_PURPOSE (arg) = error_mark_node;
4915 /* Return true if function DECL can be inlined. This is used to force
4916 instantiation of methods that might be interesting for inlining. */
4917 bool
4918 possibly_inlined_p (tree decl)
4920 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4921 if (DECL_UNINLINABLE (decl))
4922 return false;
4923 if (!optimize)
4924 return DECL_DECLARED_INLINE_P (decl);
4925 /* When optimizing, we might inline everything when flatten
4926 attribute or heuristics inlining for size or autoinlining
4927 is used. */
4928 return true;
4931 /* Normally, we can wait until instantiation-time to synthesize DECL.
4932 However, if DECL is a static data member initialized with a constant
4933 or a constexpr function, we need it right now because a reference to
4934 such a data member or a call to such function is not value-dependent.
4935 For a function that uses auto in the return type, we need to instantiate
4936 it to find out its type. For OpenMP user defined reductions, we need
4937 them instantiated for reduction clauses which inline them by hand
4938 directly. */
4940 static void
4941 maybe_instantiate_decl (tree decl)
4943 if (DECL_LANG_SPECIFIC (decl)
4944 && DECL_TEMPLATE_INFO (decl)
4945 && (decl_maybe_constant_var_p (decl)
4946 || (TREE_CODE (decl) == FUNCTION_DECL
4947 && DECL_OMP_DECLARE_REDUCTION_P (decl))
4948 || undeduced_auto_decl (decl))
4949 && !DECL_DECLARED_CONCEPT_P (decl)
4950 && !uses_template_parms (DECL_TI_ARGS (decl)))
4952 /* Instantiating a function will result in garbage collection. We
4953 must treat this situation as if we were within the body of a
4954 function so as to avoid collecting live data only referenced from
4955 the stack (such as overload resolution candidates). */
4956 ++function_depth;
4957 instantiate_decl (decl, /*defer_ok=*/false,
4958 /*expl_inst_class_mem_p=*/false);
4959 --function_depth;
4963 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
4964 If DECL is a specialization or implicitly declared class member,
4965 generate the actual definition. Return false if something goes
4966 wrong, true otherwise. */
4968 bool
4969 mark_used (tree decl, tsubst_flags_t complain)
4971 /* If DECL is a BASELINK for a single function, then treat it just
4972 like the DECL for the function. Otherwise, if the BASELINK is
4973 for an overloaded function, we don't know which function was
4974 actually used until after overload resolution. */
4975 if (BASELINK_P (decl))
4977 decl = BASELINK_FUNCTIONS (decl);
4978 if (really_overloaded_fn (decl))
4979 return true;
4980 decl = OVL_FIRST (decl);
4983 /* Set TREE_USED for the benefit of -Wunused. */
4984 TREE_USED (decl) = 1;
4985 /* And for structured bindings also the underlying decl. */
4986 if (DECL_DECOMPOSITION_P (decl) && DECL_DECOMP_BASE (decl))
4987 TREE_USED (DECL_DECOMP_BASE (decl)) = 1;
4989 if (TREE_CODE (decl) == TEMPLATE_DECL)
4990 return true;
4992 if (DECL_CLONED_FUNCTION_P (decl))
4993 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4995 /* Mark enumeration types as used. */
4996 if (TREE_CODE (decl) == CONST_DECL)
4997 used_types_insert (DECL_CONTEXT (decl));
4999 if (TREE_CODE (decl) == FUNCTION_DECL
5000 && !maybe_instantiate_noexcept (decl, complain))
5001 return false;
5003 if (TREE_CODE (decl) == FUNCTION_DECL
5004 && DECL_DELETED_FN (decl))
5006 if (DECL_ARTIFICIAL (decl)
5007 && DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
5008 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
5009 /* We mark a lambda conversion op as deleted if we can't
5010 generate it properly; see maybe_add_lambda_conv_op. */
5011 sorry ("converting lambda which uses %<...%> to function pointer");
5012 else 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"