Introduce C++ support in libcc1
[official-gcc.git] / gcc / cp / decl2.c
blob86d98202f0d733f4f595940adf089263cdd75942
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 extern cpp_reader *parse_in;
54 /* This structure contains information about the initializations
55 and/or destructions required for a particular priority level. */
56 typedef struct priority_info_s {
57 /* Nonzero if there have been any initializations at this priority
58 throughout the translation unit. */
59 int initializations_p;
60 /* Nonzero if there have been any destructions at this priority
61 throughout the translation unit. */
62 int destructions_p;
63 } *priority_info;
65 static void mark_vtable_entries (tree);
66 static bool maybe_emit_vtables (tree);
67 static tree start_objects (int, int);
68 static void finish_objects (int, int, tree);
69 static tree start_static_storage_duration_function (unsigned);
70 static void finish_static_storage_duration_function (tree);
71 static priority_info get_priority_info (int);
72 static void do_static_initialization_or_destruction (tree, bool);
73 static void one_static_initialization_or_destruction (tree, tree, bool);
74 static void generate_ctor_or_dtor_function (bool, int, location_t *);
75 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
76 void *);
77 static tree prune_vars_needing_no_initialization (tree *);
78 static void write_out_vars (tree);
79 static void import_export_class (tree);
80 static tree get_guard_bits (tree);
81 static void determine_visibility_from_class (tree, tree);
82 static bool determine_hidden_inline (tree);
83 static void maybe_instantiate_decl (tree);
85 /* A list of static class variables. This is needed, because a
86 static class variable can be declared inside the class without
87 an initializer, and then initialized, statically, outside the class. */
88 static GTY(()) vec<tree, va_gc> *pending_statics;
90 /* A list of functions which were declared inline, but which we
91 may need to emit outline anyway. */
92 static GTY(()) vec<tree, va_gc> *deferred_fns;
94 /* A list of decls that use types with no linkage, which we need to make
95 sure are defined. */
96 static GTY(()) vec<tree, va_gc> *no_linkage_decls;
98 /* A vector of alternating decls and identifiers, where the latter
99 is to be an alias for the former if the former is defined. */
100 static GTY(()) vec<tree, va_gc> *mangling_aliases;
102 /* Nonzero if we're done parsing and into end-of-file activities. */
104 int at_eof;
106 /* True if note_mangling_alias should enqueue mangling aliases for
107 later generation, rather than emitting them right away. */
109 bool defer_mangling_aliases = true;
112 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
113 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
114 that apply to the function). */
116 tree
117 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
118 cp_ref_qualifier rqual)
120 tree raises;
121 tree attrs;
122 int type_quals;
123 bool late_return_type_p;
125 if (fntype == error_mark_node || ctype == error_mark_node)
126 return error_mark_node;
128 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
129 || TREE_CODE (fntype) == METHOD_TYPE);
131 type_quals = quals & ~TYPE_QUAL_RESTRICT;
132 ctype = cp_build_qualified_type (ctype, type_quals);
133 raises = TYPE_RAISES_EXCEPTIONS (fntype);
134 attrs = TYPE_ATTRIBUTES (fntype);
135 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
136 fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
137 (TREE_CODE (fntype) == METHOD_TYPE
138 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
139 : TYPE_ARG_TYPES (fntype)));
140 if (attrs)
141 fntype = cp_build_type_attribute_variant (fntype, attrs);
142 if (rqual)
143 fntype = build_ref_qualified_type (fntype, rqual);
144 if (raises)
145 fntype = build_exception_variant (fntype, raises);
146 if (late_return_type_p)
147 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
149 return fntype;
152 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
153 return type changed to NEW_RET. */
155 tree
156 change_return_type (tree new_ret, tree fntype)
158 tree newtype;
159 tree args = TYPE_ARG_TYPES (fntype);
160 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
161 tree attrs = TYPE_ATTRIBUTES (fntype);
162 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
164 if (new_ret == error_mark_node)
165 return fntype;
167 if (same_type_p (new_ret, TREE_TYPE (fntype)))
168 return fntype;
170 if (TREE_CODE (fntype) == FUNCTION_TYPE)
172 newtype = build_function_type (new_ret, args);
173 newtype = apply_memfn_quals (newtype,
174 type_memfn_quals (fntype),
175 type_memfn_rqual (fntype));
177 else
178 newtype = build_method_type_directly
179 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
180 if (FUNCTION_REF_QUALIFIED (fntype))
181 newtype = build_ref_qualified_type (newtype, type_memfn_rqual (fntype));
182 if (raises)
183 newtype = build_exception_variant (newtype, raises);
184 if (attrs)
185 newtype = cp_build_type_attribute_variant (newtype, attrs);
186 if (late_return_type_p)
187 TYPE_HAS_LATE_RETURN_TYPE (newtype) = 1;
189 return newtype;
192 /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
193 appropriately. */
195 tree
196 cp_build_parm_decl (tree name, tree type)
198 tree parm = build_decl (input_location,
199 PARM_DECL, name, type);
200 /* DECL_ARG_TYPE is only used by the back end and the back end never
201 sees templates. */
202 if (!processing_template_decl)
203 DECL_ARG_TYPE (parm) = type_passed_as (type);
205 return parm;
208 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
209 indicated NAME. */
211 tree
212 build_artificial_parm (tree name, tree type)
214 tree parm = cp_build_parm_decl (name, type);
215 DECL_ARTIFICIAL (parm) = 1;
216 /* All our artificial parms are implicitly `const'; they cannot be
217 assigned to. */
218 TREE_READONLY (parm) = 1;
219 return parm;
222 /* Constructors for types with virtual baseclasses need an "in-charge" flag
223 saying whether this constructor is responsible for initialization of
224 virtual baseclasses or not. All destructors also need this "in-charge"
225 flag, which additionally determines whether or not the destructor should
226 free the memory for the object.
228 This function adds the "in-charge" flag to member function FN if
229 appropriate. It is called from grokclassfn and tsubst.
230 FN must be either a constructor or destructor.
232 The in-charge flag follows the 'this' parameter, and is followed by the
233 VTT parm (if any), then the user-written parms. */
235 void
236 maybe_retrofit_in_chrg (tree fn)
238 tree basetype, arg_types, parms, parm, fntype;
240 /* If we've already add the in-charge parameter don't do it again. */
241 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
242 return;
244 /* When processing templates we can't know, in general, whether or
245 not we're going to have virtual baseclasses. */
246 if (processing_template_decl)
247 return;
249 /* We don't need an in-charge parameter for constructors that don't
250 have virtual bases. */
251 if (DECL_CONSTRUCTOR_P (fn)
252 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
253 return;
255 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
256 basetype = TREE_TYPE (TREE_VALUE (arg_types));
257 arg_types = TREE_CHAIN (arg_types);
259 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
261 /* If this is a subobject constructor or destructor, our caller will
262 pass us a pointer to our VTT. */
263 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
265 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
267 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
268 DECL_CHAIN (parm) = parms;
269 parms = parm;
271 /* ...and then to TYPE_ARG_TYPES. */
272 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
274 DECL_HAS_VTT_PARM_P (fn) = 1;
277 /* Then add the in-charge parm (before the VTT parm). */
278 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
279 DECL_CHAIN (parm) = parms;
280 parms = parm;
281 arg_types = hash_tree_chain (integer_type_node, arg_types);
283 /* Insert our new parameter(s) into the list. */
284 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
286 /* And rebuild the function type. */
287 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
288 arg_types);
289 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
290 fntype = build_exception_variant (fntype,
291 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
292 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
293 fntype = (cp_build_type_attribute_variant
294 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
295 TREE_TYPE (fn) = fntype;
297 /* Now we've got the in-charge parameter. */
298 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
301 /* Classes overload their constituent function names automatically.
302 When a function name is declared in a record structure,
303 its name is changed to it overloaded name. Since names for
304 constructors and destructors can conflict, we place a leading
305 '$' for destructors.
307 CNAME is the name of the class we are grokking for.
309 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
311 FLAGS contains bits saying what's special about today's
312 arguments. DTOR_FLAG == DESTRUCTOR.
314 If FUNCTION is a destructor, then we must add the `auto-delete' field
315 as a second parameter. There is some hair associated with the fact
316 that we must "declare" this variable in the manner consistent with the
317 way the rest of the arguments were declared.
319 QUALS are the qualifiers for the this pointer. */
321 void
322 grokclassfn (tree ctype, tree function, enum overload_flags flags)
324 tree fn_name = DECL_NAME (function);
326 /* Even within an `extern "C"' block, members get C++ linkage. See
327 [dcl.link] for details. */
328 SET_DECL_LANGUAGE (function, lang_cplusplus);
330 if (fn_name == NULL_TREE)
332 error ("name missing for member function");
333 fn_name = get_identifier ("<anonymous>");
334 DECL_NAME (function) = fn_name;
337 DECL_CONTEXT (function) = ctype;
339 if (flags == DTOR_FLAG)
340 DECL_DESTRUCTOR_P (function) = 1;
342 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
343 maybe_retrofit_in_chrg (function);
346 /* Create an ARRAY_REF, checking for the user doing things backwards
347 along the way. DECLTYPE_P is for N3276, as in the parser. */
349 tree
350 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
351 bool decltype_p)
353 tree type;
354 tree expr;
355 tree orig_array_expr = array_expr;
356 tree orig_index_exp = index_exp;
357 tree overload = NULL_TREE;
359 if (error_operand_p (array_expr) || error_operand_p (index_exp))
360 return error_mark_node;
362 if (processing_template_decl)
364 if (type_dependent_expression_p (array_expr)
365 || type_dependent_expression_p (index_exp))
366 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
367 NULL_TREE, NULL_TREE);
368 array_expr = build_non_dependent_expr (array_expr);
369 index_exp = build_non_dependent_expr (index_exp);
372 type = TREE_TYPE (array_expr);
373 gcc_assert (type);
374 type = non_reference (type);
376 /* If they have an `operator[]', use that. */
377 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
379 tsubst_flags_t complain = tf_warning_or_error;
380 if (decltype_p)
381 complain |= tf_decltype;
382 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
383 index_exp, NULL_TREE, &overload, complain);
385 else
387 tree p1, p2, i1, i2;
389 /* Otherwise, create an ARRAY_REF for a pointer or array type.
390 It is a little-known fact that, if `a' is an array and `i' is
391 an int, you can write `i[a]', which means the same thing as
392 `a[i]'. */
393 if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
394 p1 = array_expr;
395 else
396 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
398 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
399 p2 = index_exp;
400 else
401 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
403 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
404 false);
405 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
406 false);
408 if ((p1 && i2) && (i1 && p2))
409 error ("ambiguous conversion for array subscript");
411 if (p1 && i2)
412 array_expr = p1, index_exp = i2;
413 else if (i1 && p2)
414 array_expr = p2, index_exp = i1;
415 else
417 error ("invalid types %<%T[%T]%> for array subscript",
418 type, TREE_TYPE (index_exp));
419 return error_mark_node;
422 if (array_expr == error_mark_node || index_exp == error_mark_node)
423 error ("ambiguous conversion for array subscript");
425 expr = build_array_ref (input_location, array_expr, index_exp);
427 if (processing_template_decl && expr != error_mark_node)
429 if (overload != NULL_TREE)
430 return (build_min_non_dep_op_overload
431 (ARRAY_REF, expr, overload, orig_array_expr, orig_index_exp));
433 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
434 NULL_TREE, NULL_TREE);
436 return expr;
439 /* Given the cast expression EXP, checking out its validity. Either return
440 an error_mark_node if there was an unavoidable error, return a cast to
441 void for trying to delete a pointer w/ the value 0, or return the
442 call to delete. If DOING_VEC is true, we handle things differently
443 for doing an array delete.
444 Implements ARM $5.3.4. This is called from the parser. */
446 tree
447 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
448 tsubst_flags_t complain)
450 tree t, type;
452 if (exp == error_mark_node)
453 return exp;
455 if (processing_template_decl)
457 t = build_min (DELETE_EXPR, void_type_node, exp, size);
458 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
459 DELETE_EXPR_USE_VEC (t) = doing_vec;
460 TREE_SIDE_EFFECTS (t) = 1;
461 return t;
464 /* An array can't have been allocated by new, so complain. */
465 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
466 warning (0, "deleting array %q#E", exp);
468 t = build_expr_type_conversion (WANT_POINTER, exp, true);
470 if (t == NULL_TREE || t == error_mark_node)
472 error ("type %q#T argument given to %<delete%>, expected pointer",
473 TREE_TYPE (exp));
474 return error_mark_node;
477 type = TREE_TYPE (t);
479 /* As of Valley Forge, you can delete a pointer to const. */
481 /* You can't delete functions. */
482 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
484 error ("cannot delete a function. Only pointer-to-objects are "
485 "valid arguments to %<delete%>");
486 return error_mark_node;
489 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
490 if (VOID_TYPE_P (TREE_TYPE (type)))
492 warning (OPT_Wdelete_incomplete, "deleting %qT is undefined", type);
493 doing_vec = 0;
496 /* Deleting a pointer with the value zero is valid and has no effect. */
497 if (integer_zerop (t))
498 return build1 (NOP_EXPR, void_type_node, t);
500 if (doing_vec)
501 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
502 sfk_deleting_destructor,
503 use_global_delete, complain);
504 else
505 return build_delete (type, t, sfk_deleting_destructor,
506 LOOKUP_NORMAL, use_global_delete,
507 complain);
510 /* Report an error if the indicated template declaration is not the
511 sort of thing that should be a member template. */
513 void
514 check_member_template (tree tmpl)
516 tree decl;
518 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
519 decl = DECL_TEMPLATE_RESULT (tmpl);
521 if (TREE_CODE (decl) == FUNCTION_DECL
522 || DECL_ALIAS_TEMPLATE_P (tmpl)
523 || (TREE_CODE (decl) == TYPE_DECL
524 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
526 /* The parser rejects template declarations in local classes
527 (with the exception of generic lambdas). */
528 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
529 /* The parser rejects any use of virtual in a function template. */
530 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
531 && DECL_VIRTUAL_P (decl)));
533 /* The debug-information generating code doesn't know what to do
534 with member templates. */
535 DECL_IGNORED_P (tmpl) = 1;
537 else if (variable_template_p (tmpl))
538 /* OK */;
539 else
540 error ("template declaration of %q#D", decl);
543 /* Sanity check: report error if this function FUNCTION is not
544 really a member of the class (CTYPE) it is supposed to belong to.
545 TEMPLATE_PARMS is used to specify the template parameters of a member
546 template passed as FUNCTION_DECL. If the member template is passed as a
547 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
548 from the declaration. If the function is not a function template, it
549 must be NULL.
550 It returns the original declaration for the function, NULL_TREE if
551 no declaration was found, error_mark_node if an error was emitted. */
553 tree
554 check_classfn (tree ctype, tree function, tree template_parms)
556 int ix;
557 bool is_template;
558 tree pushed_scope;
560 if (DECL_USE_TEMPLATE (function)
561 && !(TREE_CODE (function) == TEMPLATE_DECL
562 && DECL_TEMPLATE_SPECIALIZATION (function))
563 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
564 /* Since this is a specialization of a member template,
565 we're not going to find the declaration in the class.
566 For example, in:
568 struct S { template <typename T> void f(T); };
569 template <> void S::f(int);
571 we're not going to find `S::f(int)', but there's no
572 reason we should, either. We let our callers know we didn't
573 find the method, but we don't complain. */
574 return NULL_TREE;
576 /* Basic sanity check: for a template function, the template parameters
577 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
578 if (TREE_CODE (function) == TEMPLATE_DECL)
580 if (template_parms
581 && !comp_template_parms (template_parms,
582 DECL_TEMPLATE_PARMS (function)))
584 error ("template parameter lists provided don%'t match the "
585 "template parameters of %qD", function);
586 return error_mark_node;
588 template_parms = DECL_TEMPLATE_PARMS (function);
591 /* OK, is this a definition of a member template? */
592 is_template = (template_parms != NULL_TREE);
594 /* [temp.mem]
596 A destructor shall not be a member template. */
597 if (DECL_DESTRUCTOR_P (function) && is_template)
599 error ("destructor %qD declared as member template", function);
600 return error_mark_node;
603 /* We must enter the scope here, because conversion operators are
604 named by target type, and type equivalence relies on typenames
605 resolving within the scope of CTYPE. */
606 pushed_scope = push_scope (ctype);
607 ix = class_method_index_for_fn (complete_type (ctype), function);
608 if (ix >= 0)
610 vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (ctype);
611 tree fndecls, fndecl = 0;
612 bool is_conv_op;
613 const char *format = NULL;
615 for (fndecls = (*methods)[ix];
616 fndecls; fndecls = OVL_NEXT (fndecls))
618 tree p1, p2;
620 fndecl = OVL_CURRENT (fndecls);
621 p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
622 p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
624 /* We cannot simply call decls_match because this doesn't
625 work for static member functions that are pretending to
626 be methods, and because the name may have been changed by
627 asm("new_name"). */
629 /* Get rid of the this parameter on functions that become
630 static. */
631 if (DECL_STATIC_FUNCTION_P (fndecl)
632 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
633 p1 = TREE_CHAIN (p1);
635 /* A member template definition only matches a member template
636 declaration. */
637 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
638 continue;
640 /* ref-qualifier or absence of same must match. */
641 if (type_memfn_rqual (TREE_TYPE (function))
642 != type_memfn_rqual (TREE_TYPE (fndecl)))
643 continue;
645 // Include constraints in the match.
646 tree c1 = get_constraints (function);
647 tree c2 = get_constraints (fndecl);
649 /* While finding a match, same types and params are not enough
650 if the function is versioned. Also check version ("target")
651 attributes. */
652 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
653 TREE_TYPE (TREE_TYPE (fndecl)))
654 && compparms (p1, p2)
655 && !targetm.target_option.function_versions (function, fndecl)
656 && (!is_template
657 || comp_template_parms (template_parms,
658 DECL_TEMPLATE_PARMS (fndecl)))
659 && equivalent_constraints (c1, c2)
660 && (DECL_TEMPLATE_SPECIALIZATION (function)
661 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
662 && (!DECL_TEMPLATE_SPECIALIZATION (function)
663 || (DECL_TI_TEMPLATE (function)
664 == DECL_TI_TEMPLATE (fndecl))))
665 break;
667 if (fndecls)
669 if (pushed_scope)
670 pop_scope (pushed_scope);
671 return OVL_CURRENT (fndecls);
674 error_at (DECL_SOURCE_LOCATION (function),
675 "prototype for %q#D does not match any in class %qT",
676 function, ctype);
677 is_conv_op = DECL_CONV_FN_P (fndecl);
679 if (is_conv_op)
680 ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
681 fndecls = (*methods)[ix];
682 while (fndecls)
684 fndecl = OVL_CURRENT (fndecls);
685 fndecls = OVL_NEXT (fndecls);
687 if (!fndecls && is_conv_op)
689 if (methods->length () > (size_t) ++ix)
691 fndecls = (*methods)[ix];
692 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
694 fndecls = NULL_TREE;
695 is_conv_op = false;
698 else
699 is_conv_op = false;
701 if (format)
702 format = " %+#D";
703 else if (fndecls)
704 format = N_("candidates are: %+#D");
705 else
706 format = N_("candidate is: %+#D");
707 error (format, fndecl);
710 else if (!COMPLETE_TYPE_P (ctype))
711 cxx_incomplete_type_error (function, ctype);
712 else
713 error ("no %q#D member function declared in class %qT",
714 function, ctype);
716 if (pushed_scope)
717 pop_scope (pushed_scope);
718 return error_mark_node;
721 /* DECL is a function with vague linkage. Remember it so that at the
722 end of the translation unit we can decide whether or not to emit
723 it. */
725 void
726 note_vague_linkage_fn (tree decl)
728 DECL_DEFER_OUTPUT (decl) = 1;
729 vec_safe_push (deferred_fns, decl);
732 /* As above, but for variable template instantiations. */
734 void
735 note_variable_template_instantiation (tree decl)
737 vec_safe_push (pending_statics, decl);
740 /* We have just processed the DECL, which is a static data member.
741 The other parameters are as for cp_finish_decl. */
743 void
744 finish_static_data_member_decl (tree decl,
745 tree init, bool init_const_expr_p,
746 tree asmspec_tree,
747 int flags)
749 DECL_CONTEXT (decl) = current_class_type;
751 /* We cannot call pushdecl here, because that would fill in the
752 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
753 the right thing, namely, to put this decl out straight away. */
755 if (! processing_template_decl)
756 vec_safe_push (pending_statics, decl);
758 if (LOCAL_CLASS_P (current_class_type)
759 /* We already complained about the template definition. */
760 && !DECL_TEMPLATE_INSTANTIATION (decl))
761 permerror (input_location, "local class %q#T shall not have static data member %q#D",
762 current_class_type, decl);
763 else
764 for (tree t = current_class_type; TYPE_P (t);
765 t = CP_TYPE_CONTEXT (t))
766 if (TYPE_UNNAMED_P (t))
768 if (permerror (DECL_SOURCE_LOCATION (decl),
769 "static data member %qD in unnamed class", decl))
770 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
771 "unnamed class defined here");
772 break;
775 DECL_IN_AGGR_P (decl) = 1;
777 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
778 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
779 SET_VAR_HAD_UNKNOWN_BOUND (decl);
781 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
784 /* DECLARATOR and DECLSPECS correspond to a class member. The other
785 parameters are as for cp_finish_decl. Return the DECL for the
786 class member declared. */
788 tree
789 grokfield (const cp_declarator *declarator,
790 cp_decl_specifier_seq *declspecs,
791 tree init, bool init_const_expr_p,
792 tree asmspec_tree,
793 tree attrlist)
795 tree value;
796 const char *asmspec = 0;
797 int flags;
798 tree name;
800 if (init
801 && TREE_CODE (init) == TREE_LIST
802 && TREE_VALUE (init) == error_mark_node
803 && TREE_CHAIN (init) == NULL_TREE)
804 init = NULL_TREE;
806 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
807 if (! value || value == error_mark_node)
808 /* friend or constructor went bad. */
809 return error_mark_node;
810 if (TREE_TYPE (value) == error_mark_node)
811 return value;
813 if (TREE_CODE (value) == TYPE_DECL && init)
815 error ("typedef %qD is initialized (use decltype instead)", value);
816 init = NULL_TREE;
819 /* Pass friendly classes back. */
820 if (value == void_type_node)
821 return value;
824 name = DECL_NAME (value);
826 if (name != NULL_TREE)
828 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
830 error ("explicit template argument list not allowed");
831 return error_mark_node;
834 if (IDENTIFIER_POINTER (name)[0] == '_'
835 && ! strcmp (IDENTIFIER_POINTER (name), "_vptr"))
836 error ("member %qD conflicts with virtual function table field name",
837 value);
840 /* Stash away type declarations. */
841 if (TREE_CODE (value) == TYPE_DECL)
843 DECL_NONLOCAL (value) = 1;
844 DECL_CONTEXT (value) = current_class_type;
846 if (attrlist)
848 int attrflags = 0;
850 /* If this is a typedef that names the class for linkage purposes
851 (7.1.3p8), apply any attributes directly to the type. */
852 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
853 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
854 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
856 cplus_decl_attributes (&value, attrlist, attrflags);
859 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
860 && TREE_TYPE (value) != error_mark_node
861 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
862 set_underlying_type (value);
864 /* It's important that push_template_decl below follows
865 set_underlying_type above so that the created template
866 carries the properly set type of VALUE. */
867 if (processing_template_decl)
868 value = push_template_decl (value);
870 record_locally_defined_typedef (value);
871 return value;
874 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
876 if (!friendp && DECL_IN_AGGR_P (value))
878 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
879 return void_type_node;
882 if (asmspec_tree && asmspec_tree != error_mark_node)
883 asmspec = TREE_STRING_POINTER (asmspec_tree);
885 if (init)
887 if (TREE_CODE (value) == FUNCTION_DECL)
889 if (init == ridpointers[(int)RID_DELETE])
891 DECL_DELETED_FN (value) = 1;
892 DECL_DECLARED_INLINE_P (value) = 1;
893 DECL_INITIAL (value) = error_mark_node;
895 else if (init == ridpointers[(int)RID_DEFAULT])
897 if (defaultable_fn_check (value))
899 DECL_DEFAULTED_FN (value) = 1;
900 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
901 DECL_DECLARED_INLINE_P (value) = 1;
904 else if (TREE_CODE (init) == DEFAULT_ARG)
905 error ("invalid initializer for member function %qD", value);
906 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
908 if (integer_zerop (init))
909 DECL_PURE_VIRTUAL_P (value) = 1;
910 else if (error_operand_p (init))
911 ; /* An error has already been reported. */
912 else
913 error ("invalid initializer for member function %qD",
914 value);
916 else
918 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
919 if (friendp)
920 error ("initializer specified for friend function %qD",
921 value);
922 else
923 error ("initializer specified for static member function %qD",
924 value);
927 else if (TREE_CODE (value) == FIELD_DECL)
928 /* C++11 NSDMI, keep going. */;
929 else if (!VAR_P (value))
930 gcc_unreachable ();
933 /* Pass friend decls back. */
934 if ((TREE_CODE (value) == FUNCTION_DECL
935 || TREE_CODE (value) == TEMPLATE_DECL)
936 && DECL_CONTEXT (value) != current_class_type)
937 return value;
939 /* Need to set this before push_template_decl. */
940 if (VAR_P (value))
941 DECL_CONTEXT (value) = current_class_type;
943 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
945 value = push_template_decl (value);
946 if (error_operand_p (value))
947 return error_mark_node;
950 if (attrlist)
951 cplus_decl_attributes (&value, attrlist, 0);
953 if (init && DIRECT_LIST_INIT_P (init))
954 flags = LOOKUP_NORMAL;
955 else
956 flags = LOOKUP_IMPLICIT;
958 switch (TREE_CODE (value))
960 case VAR_DECL:
961 finish_static_data_member_decl (value, init, init_const_expr_p,
962 asmspec_tree, flags);
963 return value;
965 case FIELD_DECL:
966 if (asmspec)
967 error ("%<asm%> specifiers are not permitted on non-static data members");
968 if (DECL_INITIAL (value) == error_mark_node)
969 init = error_mark_node;
970 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
971 NULL_TREE, flags);
972 DECL_IN_AGGR_P (value) = 1;
973 return value;
975 case FUNCTION_DECL:
976 if (asmspec)
977 set_user_assembler_name (value, asmspec);
979 cp_finish_decl (value,
980 /*init=*/NULL_TREE,
981 /*init_const_expr_p=*/false,
982 asmspec_tree, flags);
984 /* Pass friends back this way. */
985 if (DECL_FRIEND_P (value))
986 return void_type_node;
988 DECL_IN_AGGR_P (value) = 1;
989 return value;
991 default:
992 gcc_unreachable ();
994 return NULL_TREE;
997 /* Like `grokfield', but for bitfields.
998 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1000 tree
1001 grokbitfield (const cp_declarator *declarator,
1002 cp_decl_specifier_seq *declspecs, tree width,
1003 tree attrlist)
1005 tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, &attrlist);
1007 if (value == error_mark_node)
1008 return NULL_TREE; /* friends went bad. */
1009 if (TREE_TYPE (value) == error_mark_node)
1010 return value;
1012 /* Pass friendly classes back. */
1013 if (VOID_TYPE_P (value))
1014 return void_type_node;
1016 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
1017 && (POINTER_TYPE_P (value)
1018 || !dependent_type_p (TREE_TYPE (value))))
1020 error ("bit-field %qD with non-integral type", value);
1021 return error_mark_node;
1024 if (TREE_CODE (value) == TYPE_DECL)
1026 error ("cannot declare %qD to be a bit-field type", value);
1027 return NULL_TREE;
1030 /* Usually, finish_struct_1 catches bitfields with invalid types.
1031 But, in the case of bitfields with function type, we confuse
1032 ourselves into thinking they are member functions, so we must
1033 check here. */
1034 if (TREE_CODE (value) == FUNCTION_DECL)
1036 error ("cannot declare bit-field %qD with function type",
1037 DECL_NAME (value));
1038 return NULL_TREE;
1041 if (DECL_IN_AGGR_P (value))
1043 error ("%qD is already defined in the class %qT", value,
1044 DECL_CONTEXT (value));
1045 return void_type_node;
1048 if (TREE_STATIC (value))
1050 error ("static member %qD cannot be a bit-field", value);
1051 return NULL_TREE;
1053 cp_finish_decl (value, NULL_TREE, false, NULL_TREE, 0);
1055 if (width != error_mark_node)
1057 /* The width must be an integer type. */
1058 if (!type_dependent_expression_p (width)
1059 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1060 error ("width of bit-field %qD has non-integral type %qT", value,
1061 TREE_TYPE (width));
1062 DECL_INITIAL (value) = width;
1063 SET_DECL_C_BIT_FIELD (value);
1066 DECL_IN_AGGR_P (value) = 1;
1068 if (attrlist)
1069 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1071 return value;
1075 /* Returns true iff ATTR is an attribute which needs to be applied at
1076 instantiation time rather than template definition time. */
1078 static bool
1079 is_late_template_attribute (tree attr, tree decl)
1081 tree name = get_attribute_name (attr);
1082 tree args = TREE_VALUE (attr);
1083 const struct attribute_spec *spec = lookup_attribute_spec (name);
1084 tree arg;
1086 if (!spec)
1087 /* Unknown attribute. */
1088 return false;
1090 /* Attribute weak handling wants to write out assembly right away. */
1091 if (is_attribute_p ("weak", name))
1092 return true;
1094 /* Attribute unused is applied directly, as it appertains to
1095 decls. */
1096 if (is_attribute_p ("unused", name))
1097 return false;
1099 /* Attribute tls_model wants to modify the symtab. */
1100 if (is_attribute_p ("tls_model", name))
1101 return true;
1103 /* #pragma omp declare simd attribute needs to be always deferred. */
1104 if (flag_openmp
1105 && is_attribute_p ("omp declare simd", name))
1106 return true;
1108 /* An attribute pack is clearly dependent. */
1109 if (args && PACK_EXPANSION_P (args))
1110 return true;
1112 /* If any of the arguments are dependent expressions, we can't evaluate
1113 the attribute until instantiation time. */
1114 for (arg = args; arg; arg = TREE_CHAIN (arg))
1116 tree t = TREE_VALUE (arg);
1118 /* If the first attribute argument is an identifier, only consider
1119 second and following arguments. Attributes like mode, format,
1120 cleanup and several target specific attributes aren't late
1121 just because they have an IDENTIFIER_NODE as first argument. */
1122 if (arg == args && attribute_takes_identifier_p (name)
1123 && identifier_p (t))
1124 continue;
1126 if (value_dependent_expression_p (t)
1127 || type_dependent_expression_p (t))
1128 return true;
1131 if (TREE_CODE (decl) == TYPE_DECL
1132 || TYPE_P (decl)
1133 || spec->type_required)
1135 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1137 /* We can't apply any attributes to a completely unknown type until
1138 instantiation time. */
1139 enum tree_code code = TREE_CODE (type);
1140 if (code == TEMPLATE_TYPE_PARM
1141 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1142 || code == TYPENAME_TYPE)
1143 return true;
1144 /* Also defer most attributes on dependent types. This is not
1145 necessary in all cases, but is the better default. */
1146 else if (dependent_type_p (type)
1147 /* But some attributes specifically apply to templates. */
1148 && !is_attribute_p ("abi_tag", name)
1149 && !is_attribute_p ("deprecated", name)
1150 && !is_attribute_p ("visibility", name))
1151 return true;
1152 else
1153 return false;
1155 else
1156 return false;
1159 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1160 applied at instantiation time and return them. If IS_DEPENDENT is true,
1161 the declaration itself is dependent, so all attributes should be applied
1162 at instantiation time. */
1164 static tree
1165 splice_template_attributes (tree *attr_p, tree decl)
1167 tree *p = attr_p;
1168 tree late_attrs = NULL_TREE;
1169 tree *q = &late_attrs;
1171 if (!p)
1172 return NULL_TREE;
1174 for (; *p; )
1176 if (is_late_template_attribute (*p, decl))
1178 ATTR_IS_DEPENDENT (*p) = 1;
1179 *q = *p;
1180 *p = TREE_CHAIN (*p);
1181 q = &TREE_CHAIN (*q);
1182 *q = NULL_TREE;
1184 else
1185 p = &TREE_CHAIN (*p);
1188 return late_attrs;
1191 /* Remove any late attributes from the list in ATTR_P and attach them to
1192 DECL_P. */
1194 static void
1195 save_template_attributes (tree *attr_p, tree *decl_p)
1197 tree *q;
1199 if (attr_p && *attr_p == error_mark_node)
1200 return;
1202 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1203 if (!late_attrs)
1204 return;
1206 if (DECL_P (*decl_p))
1207 q = &DECL_ATTRIBUTES (*decl_p);
1208 else
1209 q = &TYPE_ATTRIBUTES (*decl_p);
1211 tree old_attrs = *q;
1213 /* Merge the late attributes at the beginning with the attribute
1214 list. */
1215 late_attrs = merge_attributes (late_attrs, *q);
1216 *q = late_attrs;
1218 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1220 /* We've added new attributes directly to the main variant, so
1221 now we need to update all of the other variants to include
1222 these new attributes. */
1223 tree variant;
1224 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1225 variant = TYPE_NEXT_VARIANT (variant))
1227 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1228 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1233 /* True if ATTRS contains any dependent attributes that affect type
1234 identity. */
1236 bool
1237 any_dependent_type_attributes_p (tree attrs)
1239 for (tree a = attrs; a; a = TREE_CHAIN (a))
1240 if (ATTR_IS_DEPENDENT (a))
1242 const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (a));
1243 if (as && as->affects_type_identity)
1244 return true;
1246 return false;
1249 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1250 to a typedef which gives a previously unnamed class or enum a name for
1251 linkage purposes. */
1253 bool
1254 attributes_naming_typedef_ok (tree attrs)
1256 for (; attrs; attrs = TREE_CHAIN (attrs))
1258 tree name = get_attribute_name (attrs);
1259 if (is_attribute_p ("vector_size", name))
1260 return false;
1262 return true;
1265 /* Like reconstruct_complex_type, but handle also template trees. */
1267 tree
1268 cp_reconstruct_complex_type (tree type, tree bottom)
1270 tree inner, outer;
1271 bool late_return_type_p = false;
1273 if (TYPE_PTR_P (type))
1275 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1276 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1277 TYPE_REF_CAN_ALIAS_ALL (type));
1279 else if (TREE_CODE (type) == REFERENCE_TYPE)
1281 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1282 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1283 TYPE_REF_CAN_ALIAS_ALL (type));
1285 else if (TREE_CODE (type) == ARRAY_TYPE)
1287 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1288 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1289 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1290 element type qualification will be handled by the recursive
1291 cp_reconstruct_complex_type call and cp_build_qualified_type
1292 for ARRAY_TYPEs changes the element type. */
1293 return outer;
1295 else if (TREE_CODE (type) == FUNCTION_TYPE)
1297 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1298 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1299 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1300 outer = apply_memfn_quals (outer,
1301 type_memfn_quals (type),
1302 type_memfn_rqual (type));
1304 else if (TREE_CODE (type) == METHOD_TYPE)
1306 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1307 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1308 /* The build_method_type_directly() routine prepends 'this' to argument list,
1309 so we must compensate by getting rid of it. */
1310 outer
1311 = build_method_type_directly
1312 (class_of_this_parm (type), inner,
1313 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1315 else if (TREE_CODE (type) == OFFSET_TYPE)
1317 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1318 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1320 else
1321 return bottom;
1323 if (TYPE_ATTRIBUTES (type))
1324 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1325 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1327 if (late_return_type_p)
1328 TYPE_HAS_LATE_RETURN_TYPE (outer) = 1;
1330 return outer;
1333 /* Replaces any constexpr expression that may be into the attributes
1334 arguments with their reduced value. */
1336 static void
1337 cp_check_const_attributes (tree attributes)
1339 if (attributes == error_mark_node)
1340 return;
1342 tree attr;
1343 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1345 tree arg;
1346 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1348 tree expr = TREE_VALUE (arg);
1349 if (EXPR_P (expr))
1350 TREE_VALUE (arg) = maybe_constant_value (expr);
1355 /* Return true if TYPE is an OpenMP mappable type. */
1356 bool
1357 cp_omp_mappable_type (tree type)
1359 /* Mappable type has to be complete. */
1360 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1361 return false;
1362 /* Arrays have mappable type if the elements have mappable type. */
1363 while (TREE_CODE (type) == ARRAY_TYPE)
1364 type = TREE_TYPE (type);
1365 /* A mappable type cannot contain virtual members. */
1366 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1367 return false;
1368 /* All data members must be non-static. */
1369 if (CLASS_TYPE_P (type))
1371 tree field;
1372 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1373 if (VAR_P (field))
1374 return false;
1375 /* All fields must have mappable types. */
1376 else if (TREE_CODE (field) == FIELD_DECL
1377 && !cp_omp_mappable_type (TREE_TYPE (field)))
1378 return false;
1380 return true;
1383 /* Like decl_attributes, but handle C++ complexity. */
1385 void
1386 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1388 if (*decl == NULL_TREE || *decl == void_type_node
1389 || *decl == error_mark_node)
1390 return;
1392 /* Add implicit "omp declare target" attribute if requested. */
1393 if (scope_chain->omp_declare_target_attribute
1394 && ((VAR_P (*decl)
1395 && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
1396 || TREE_CODE (*decl) == FUNCTION_DECL))
1398 if (VAR_P (*decl)
1399 && DECL_CLASS_SCOPE_P (*decl))
1400 error ("%q+D static data member inside of declare target directive",
1401 *decl);
1402 else if (!processing_template_decl
1403 && VAR_P (*decl)
1404 && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1405 error ("%q+D in declare target directive does not have mappable type",
1406 *decl);
1407 else
1408 attributes = tree_cons (get_identifier ("omp declare target"),
1409 NULL_TREE, attributes);
1412 if (processing_template_decl)
1414 if (check_for_bare_parameter_packs (attributes))
1415 return;
1417 save_template_attributes (&attributes, decl);
1420 cp_check_const_attributes (attributes);
1422 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1423 decl = &DECL_TEMPLATE_RESULT (*decl);
1425 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1427 attributes
1428 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1429 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1430 attributes, flags);
1432 else
1433 decl_attributes (decl, attributes, flags);
1435 if (TREE_CODE (*decl) == TYPE_DECL)
1436 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1438 /* Propagate deprecation out to the template. */
1439 if (TREE_DEPRECATED (*decl))
1440 if (tree ti = get_template_info (*decl))
1442 tree tmpl = TI_TEMPLATE (ti);
1443 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1444 : DECL_TEMPLATE_RESULT (tmpl));
1445 if (*decl == pattern)
1446 TREE_DEPRECATED (tmpl) = true;
1450 /* Walks through the namespace- or function-scope anonymous union
1451 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1452 Returns one of the fields for use in the mangled name. */
1454 static tree
1455 build_anon_union_vars (tree type, tree object)
1457 tree main_decl = NULL_TREE;
1458 tree field;
1460 /* Rather than write the code to handle the non-union case,
1461 just give an error. */
1462 if (TREE_CODE (type) != UNION_TYPE)
1464 error ("anonymous struct not inside named type");
1465 return error_mark_node;
1468 for (field = TYPE_FIELDS (type);
1469 field != NULL_TREE;
1470 field = DECL_CHAIN (field))
1472 tree decl;
1473 tree ref;
1475 if (DECL_ARTIFICIAL (field))
1476 continue;
1477 if (TREE_CODE (field) != FIELD_DECL)
1479 permerror (DECL_SOURCE_LOCATION (field),
1480 "%q#D invalid; an anonymous union can only "
1481 "have non-static data members", field);
1482 continue;
1485 if (TREE_PRIVATE (field))
1486 permerror (DECL_SOURCE_LOCATION (field),
1487 "private member %q#D in anonymous union", field);
1488 else if (TREE_PROTECTED (field))
1489 permerror (DECL_SOURCE_LOCATION (field),
1490 "protected member %q#D in anonymous union", field);
1492 if (processing_template_decl)
1493 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1494 DECL_NAME (field), NULL_TREE);
1495 else
1496 ref = build_class_member_access_expr (object, field, NULL_TREE,
1497 false, tf_warning_or_error);
1499 if (DECL_NAME (field))
1501 tree base;
1503 decl = build_decl (input_location,
1504 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1505 DECL_ANON_UNION_VAR_P (decl) = 1;
1506 DECL_ARTIFICIAL (decl) = 1;
1508 base = get_base_address (object);
1509 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1510 TREE_STATIC (decl) = TREE_STATIC (base);
1511 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1513 SET_DECL_VALUE_EXPR (decl, ref);
1514 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1516 decl = pushdecl (decl);
1518 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1519 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1520 else
1521 decl = 0;
1523 if (main_decl == NULL_TREE)
1524 main_decl = decl;
1527 return main_decl;
1530 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1531 anonymous union, then all members must be laid out together. PUBLIC_P
1532 is nonzero if this union is not declared static. */
1534 void
1535 finish_anon_union (tree anon_union_decl)
1537 tree type;
1538 tree main_decl;
1539 bool public_p;
1541 if (anon_union_decl == error_mark_node)
1542 return;
1544 type = TREE_TYPE (anon_union_decl);
1545 public_p = TREE_PUBLIC (anon_union_decl);
1547 /* The VAR_DECL's context is the same as the TYPE's context. */
1548 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1550 if (TYPE_FIELDS (type) == NULL_TREE)
1551 return;
1553 if (public_p)
1555 error ("namespace-scope anonymous aggregates must be static");
1556 return;
1559 main_decl = build_anon_union_vars (type, anon_union_decl);
1560 if (main_decl == error_mark_node)
1561 return;
1562 if (main_decl == NULL_TREE)
1564 warning (0, "anonymous union with no members");
1565 return;
1568 if (!processing_template_decl)
1570 /* Use main_decl to set the mangled name. */
1571 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1572 maybe_commonize_var (anon_union_decl);
1573 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1574 mangle_decl (anon_union_decl);
1575 DECL_NAME (anon_union_decl) = NULL_TREE;
1578 pushdecl (anon_union_decl);
1579 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1582 /* Auxiliary functions to make type signatures for
1583 `operator new' and `operator delete' correspond to
1584 what compiler will be expecting. */
1586 tree
1587 coerce_new_type (tree type)
1589 int e = 0;
1590 tree args = TYPE_ARG_TYPES (type);
1592 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1594 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1596 e = 1;
1597 error ("%<operator new%> must return type %qT", ptr_type_node);
1600 if (args && args != void_list_node)
1602 if (TREE_PURPOSE (args))
1604 /* [basic.stc.dynamic.allocation]
1606 The first parameter shall not have an associated default
1607 argument. */
1608 error ("the first parameter of %<operator new%> cannot "
1609 "have a default argument");
1610 /* Throw away the default argument. */
1611 TREE_PURPOSE (args) = NULL_TREE;
1614 if (!same_type_p (TREE_VALUE (args), size_type_node))
1616 e = 2;
1617 args = TREE_CHAIN (args);
1620 else
1621 e = 2;
1623 if (e == 2)
1624 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1625 "as first parameter", size_type_node);
1627 switch (e)
1629 case 2:
1630 args = tree_cons (NULL_TREE, size_type_node, args);
1631 /* Fall through. */
1632 case 1:
1633 type = build_exception_variant
1634 (build_function_type (ptr_type_node, args),
1635 TYPE_RAISES_EXCEPTIONS (type));
1636 /* Fall through. */
1637 default:;
1639 return type;
1642 tree
1643 coerce_delete_type (tree type)
1645 int e = 0;
1646 tree args = TYPE_ARG_TYPES (type);
1648 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1650 if (!same_type_p (TREE_TYPE (type), void_type_node))
1652 e = 1;
1653 error ("%<operator delete%> must return type %qT", void_type_node);
1656 if (!args || args == void_list_node
1657 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1659 e = 2;
1660 if (args && args != void_list_node)
1661 args = TREE_CHAIN (args);
1662 error ("%<operator delete%> takes type %qT as first parameter",
1663 ptr_type_node);
1665 switch (e)
1667 case 2:
1668 args = tree_cons (NULL_TREE, ptr_type_node, args);
1669 /* Fall through. */
1670 case 1:
1671 type = build_exception_variant
1672 (build_function_type (void_type_node, args),
1673 TYPE_RAISES_EXCEPTIONS (type));
1674 /* Fall through. */
1675 default:;
1678 return type;
1681 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1682 and mark them as needed. */
1684 static void
1685 mark_vtable_entries (tree decl)
1687 tree fnaddr;
1688 unsigned HOST_WIDE_INT idx;
1690 /* It's OK for the vtable to refer to deprecated virtual functions. */
1691 warning_sentinel w(warn_deprecated_decl);
1693 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1694 idx, fnaddr)
1696 tree fn;
1698 STRIP_NOPS (fnaddr);
1700 if (TREE_CODE (fnaddr) != ADDR_EXPR
1701 && TREE_CODE (fnaddr) != FDESC_EXPR)
1702 /* This entry is an offset: a virtual base class offset, a
1703 virtual call offset, an RTTI offset, etc. */
1704 continue;
1706 fn = TREE_OPERAND (fnaddr, 0);
1707 TREE_ADDRESSABLE (fn) = 1;
1708 /* When we don't have vcall offsets, we output thunks whenever
1709 we output the vtables that contain them. With vcall offsets,
1710 we know all the thunks we'll need when we emit a virtual
1711 function, so we emit the thunks there instead. */
1712 if (DECL_THUNK_P (fn))
1713 use_thunk (fn, /*emit_p=*/0);
1714 mark_used (fn);
1718 /* Set DECL up to have the closest approximation of "initialized common"
1719 linkage available. */
1721 void
1722 comdat_linkage (tree decl)
1724 if (flag_weak)
1725 make_decl_one_only (decl, cxx_comdat_group (decl));
1726 else if (TREE_CODE (decl) == FUNCTION_DECL
1727 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1728 /* We can just emit function and compiler-generated variables
1729 statically; having multiple copies is (for the most part) only
1730 a waste of space.
1732 There are two correctness issues, however: the address of a
1733 template instantiation with external linkage should be the
1734 same, independent of what translation unit asks for the
1735 address, and this will not hold when we emit multiple copies of
1736 the function. However, there's little else we can do.
1738 Also, by default, the typeinfo implementation assumes that
1739 there will be only one copy of the string used as the name for
1740 each type. Therefore, if weak symbols are unavailable, the
1741 run-time library should perform a more conservative check; it
1742 should perform a string comparison, rather than an address
1743 comparison. */
1744 TREE_PUBLIC (decl) = 0;
1745 else
1747 /* Static data member template instantiations, however, cannot
1748 have multiple copies. */
1749 if (DECL_INITIAL (decl) == 0
1750 || DECL_INITIAL (decl) == error_mark_node)
1751 DECL_COMMON (decl) = 1;
1752 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1754 DECL_COMMON (decl) = 1;
1755 DECL_INITIAL (decl) = error_mark_node;
1757 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1759 /* We can't do anything useful; leave vars for explicit
1760 instantiation. */
1761 DECL_EXTERNAL (decl) = 1;
1762 DECL_NOT_REALLY_EXTERN (decl) = 0;
1766 if (TREE_PUBLIC (decl))
1767 DECL_COMDAT (decl) = 1;
1770 /* For win32 we also want to put explicit instantiations in
1771 linkonce sections, so that they will be merged with implicit
1772 instantiations; otherwise we get duplicate symbol errors.
1773 For Darwin we do not want explicit instantiations to be
1774 linkonce. */
1776 void
1777 maybe_make_one_only (tree decl)
1779 /* We used to say that this was not necessary on targets that support weak
1780 symbols, because the implicit instantiations will defer to the explicit
1781 one. However, that's not actually the case in SVR4; a strong definition
1782 after a weak one is an error. Also, not making explicit
1783 instantiations one_only means that we can end up with two copies of
1784 some template instantiations. */
1785 if (! flag_weak)
1786 return;
1788 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1789 we can get away with not emitting them if they aren't used. We need
1790 to for variables so that cp_finish_decl will update their linkage,
1791 because their DECL_INITIAL may not have been set properly yet. */
1793 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1794 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1795 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1797 make_decl_one_only (decl, cxx_comdat_group (decl));
1799 if (VAR_P (decl))
1801 varpool_node *node = varpool_node::get_create (decl);
1802 DECL_COMDAT (decl) = 1;
1803 /* Mark it needed so we don't forget to emit it. */
1804 node->forced_by_abi = true;
1805 TREE_USED (decl) = 1;
1810 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1811 This predicate will give the right answer during parsing of the
1812 function, which other tests may not. */
1814 bool
1815 vague_linkage_p (tree decl)
1817 if (!TREE_PUBLIC (decl))
1819 /* maybe_thunk_body clears TREE_PUBLIC on the maybe-in-charge 'tor
1820 variants, check one of the "clones" for the real linkage. */
1821 if ((DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
1822 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl))
1823 && DECL_CHAIN (decl)
1824 && DECL_CLONED_FUNCTION (DECL_CHAIN (decl)))
1825 return vague_linkage_p (DECL_CHAIN (decl));
1827 gcc_checking_assert (!DECL_COMDAT (decl));
1828 return false;
1830 /* Unfortunately, import_export_decl has not always been called
1831 before the function is processed, so we cannot simply check
1832 DECL_COMDAT. */
1833 if (DECL_COMDAT (decl)
1834 || (TREE_CODE (decl) == FUNCTION_DECL
1835 && DECL_DECLARED_INLINE_P (decl))
1836 || (DECL_LANG_SPECIFIC (decl)
1837 && DECL_TEMPLATE_INSTANTIATION (decl))
1838 || (VAR_P (decl) && DECL_INLINE_VAR_P (decl)))
1839 return true;
1840 else if (DECL_FUNCTION_SCOPE_P (decl))
1841 /* A local static in an inline effectively has vague linkage. */
1842 return (TREE_STATIC (decl)
1843 && vague_linkage_p (DECL_CONTEXT (decl)));
1844 else
1845 return false;
1848 /* Determine whether or not we want to specifically import or export CTYPE,
1849 using various heuristics. */
1851 static void
1852 import_export_class (tree ctype)
1854 /* -1 for imported, 1 for exported. */
1855 int import_export = 0;
1857 /* It only makes sense to call this function at EOF. The reason is
1858 that this function looks at whether or not the first non-inline
1859 non-abstract virtual member function has been defined in this
1860 translation unit. But, we can't possibly know that until we've
1861 seen the entire translation unit. */
1862 gcc_assert (at_eof);
1864 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1865 return;
1867 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1868 we will have CLASSTYPE_INTERFACE_ONLY set but not
1869 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1870 heuristic because someone will supply a #pragma implementation
1871 elsewhere, and deducing it here would produce a conflict. */
1872 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1873 return;
1875 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1876 import_export = -1;
1877 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1878 import_export = 1;
1879 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1880 && !flag_implicit_templates)
1881 /* For a template class, without -fimplicit-templates, check the
1882 repository. If the virtual table is assigned to this
1883 translation unit, then export the class; otherwise, import
1884 it. */
1885 import_export = repo_export_class_p (ctype) ? 1 : -1;
1886 else if (TYPE_POLYMORPHIC_P (ctype))
1888 /* The ABI specifies that the virtual table and associated
1889 information are emitted with the key method, if any. */
1890 tree method = CLASSTYPE_KEY_METHOD (ctype);
1891 /* If weak symbol support is not available, then we must be
1892 careful not to emit the vtable when the key function is
1893 inline. An inline function can be defined in multiple
1894 translation units. If we were to emit the vtable in each
1895 translation unit containing a definition, we would get
1896 multiple definition errors at link-time. */
1897 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1898 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1901 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1902 a definition anywhere else. */
1903 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1904 import_export = 0;
1906 /* Allow back ends the chance to overrule the decision. */
1907 if (targetm.cxx.import_export_class)
1908 import_export = targetm.cxx.import_export_class (ctype, import_export);
1910 if (import_export)
1912 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1913 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1917 /* Return true if VAR has already been provided to the back end; in that
1918 case VAR should not be modified further by the front end. */
1919 static bool
1920 var_finalized_p (tree var)
1922 return varpool_node::get_create (var)->definition;
1925 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1926 must be emitted in this translation unit. Mark it as such. */
1928 void
1929 mark_needed (tree decl)
1931 TREE_USED (decl) = 1;
1932 if (TREE_CODE (decl) == FUNCTION_DECL)
1934 /* Extern inline functions don't become needed when referenced.
1935 If we know a method will be emitted in other TU and no new
1936 functions can be marked reachable, just use the external
1937 definition. */
1938 struct cgraph_node *node = cgraph_node::get_create (decl);
1939 node->forced_by_abi = true;
1941 /* #pragma interface and -frepo code can call mark_needed for
1942 maybe-in-charge 'tors; mark the clones as well. */
1943 tree clone;
1944 FOR_EACH_CLONE (clone, decl)
1945 mark_needed (clone);
1947 else if (VAR_P (decl))
1949 varpool_node *node = varpool_node::get_create (decl);
1950 /* C++ frontend use mark_decl_references to force COMDAT variables
1951 to be output that might appear dead otherwise. */
1952 node->forced_by_abi = true;
1956 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1957 returns true if a definition of this entity should be provided in
1958 this object file. Callers use this function to determine whether
1959 or not to let the back end know that a definition of DECL is
1960 available in this translation unit. */
1962 bool
1963 decl_needed_p (tree decl)
1965 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
1966 /* This function should only be called at the end of the translation
1967 unit. We cannot be sure of whether or not something will be
1968 COMDAT until that point. */
1969 gcc_assert (at_eof);
1971 /* All entities with external linkage that are not COMDAT/EXTERN should be
1972 emitted; they may be referred to from other object files. */
1973 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
1974 return true;
1975 /* Functions marked "dllexport" must be emitted so that they are
1976 visible to other DLLs. */
1977 if (flag_keep_inline_dllexport
1978 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
1979 return true;
1981 /* When not optimizing, do not bother to produce definitions for extern
1982 symbols. */
1983 if (DECL_REALLY_EXTERN (decl)
1984 && ((TREE_CODE (decl) != FUNCTION_DECL
1985 && !optimize)
1986 || (TREE_CODE (decl) == FUNCTION_DECL
1987 && !opt_for_fn (decl, optimize)))
1988 && !lookup_attribute ("always_inline", decl))
1989 return false;
1991 /* If this entity was used, let the back end see it; it will decide
1992 whether or not to emit it into the object file. */
1993 if (TREE_USED (decl))
1994 return true;
1995 /* Virtual functions might be needed for devirtualization. */
1996 if (flag_devirtualize
1997 && TREE_CODE (decl) == FUNCTION_DECL
1998 && DECL_VIRTUAL_P (decl))
1999 return true;
2000 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2001 reference to DECL might cause it to be emitted later. */
2002 return false;
2005 /* If necessary, write out the vtables for the dynamic class CTYPE.
2006 Returns true if any vtables were emitted. */
2008 static bool
2009 maybe_emit_vtables (tree ctype)
2011 tree vtbl;
2012 tree primary_vtbl;
2013 int needed = 0;
2014 varpool_node *current = NULL, *last = NULL;
2016 /* If the vtables for this class have already been emitted there is
2017 nothing more to do. */
2018 primary_vtbl = CLASSTYPE_VTABLES (ctype);
2019 if (var_finalized_p (primary_vtbl))
2020 return false;
2021 /* Ignore dummy vtables made by get_vtable_decl. */
2022 if (TREE_TYPE (primary_vtbl) == void_type_node)
2023 return false;
2025 /* On some targets, we cannot determine the key method until the end
2026 of the translation unit -- which is when this function is
2027 called. */
2028 if (!targetm.cxx.key_method_may_be_inline ())
2029 determine_key_method (ctype);
2031 /* See if any of the vtables are needed. */
2032 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2034 import_export_decl (vtbl);
2035 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2036 needed = 1;
2038 if (!needed)
2040 /* If the references to this class' vtables are optimized away,
2041 still emit the appropriate debugging information. See
2042 dfs_debug_mark. */
2043 if (DECL_COMDAT (primary_vtbl)
2044 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2045 note_debug_info_needed (ctype);
2046 return false;
2049 /* The ABI requires that we emit all of the vtables if we emit any
2050 of them. */
2051 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2053 /* Mark entities references from the virtual table as used. */
2054 mark_vtable_entries (vtbl);
2056 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2058 vec<tree, va_gc> *cleanups = NULL;
2059 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2060 LOOKUP_NORMAL);
2062 /* It had better be all done at compile-time. */
2063 gcc_assert (!expr && !cleanups);
2066 /* Write it out. */
2067 DECL_EXTERNAL (vtbl) = 0;
2068 rest_of_decl_compilation (vtbl, 1, 1);
2070 /* Because we're only doing syntax-checking, we'll never end up
2071 actually marking the variable as written. */
2072 if (flag_syntax_only)
2073 TREE_ASM_WRITTEN (vtbl) = 1;
2074 else if (DECL_ONE_ONLY (vtbl))
2076 current = varpool_node::get_create (vtbl);
2077 if (last)
2078 current->add_to_same_comdat_group (last);
2079 last = current;
2083 /* Since we're writing out the vtable here, also write the debug
2084 info. */
2085 note_debug_info_needed (ctype);
2087 return true;
2090 /* A special return value from type_visibility meaning internal
2091 linkage. */
2093 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2095 /* walk_tree helper function for type_visibility. */
2097 static tree
2098 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2100 int *vis_p = (int *)data;
2101 if (! TYPE_P (*tp))
2103 *walk_subtrees = 0;
2105 else if (OVERLOAD_TYPE_P (*tp)
2106 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2108 *vis_p = VISIBILITY_ANON;
2109 return *tp;
2111 else if (CLASS_TYPE_P (*tp)
2112 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2113 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2114 return NULL;
2117 /* Returns the visibility of TYPE, which is the minimum visibility of its
2118 component types. */
2120 static int
2121 type_visibility (tree type)
2123 int vis = VISIBILITY_DEFAULT;
2124 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2125 return vis;
2128 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2129 specified (or if VISIBILITY is static). If TMPL is true, this
2130 constraint is for a template argument, and takes precedence
2131 over explicitly-specified visibility on the template. */
2133 static void
2134 constrain_visibility (tree decl, int visibility, bool tmpl)
2136 if (visibility == VISIBILITY_ANON)
2138 /* extern "C" declarations aren't affected by the anonymous
2139 namespace. */
2140 if (!DECL_EXTERN_C_P (decl))
2142 TREE_PUBLIC (decl) = 0;
2143 DECL_WEAK (decl) = 0;
2144 DECL_COMMON (decl) = 0;
2145 DECL_COMDAT (decl) = false;
2146 if (VAR_OR_FUNCTION_DECL_P (decl))
2148 struct symtab_node *snode = symtab_node::get (decl);
2150 if (snode)
2151 snode->set_comdat_group (NULL);
2153 DECL_INTERFACE_KNOWN (decl) = 1;
2154 if (DECL_LANG_SPECIFIC (decl))
2155 DECL_NOT_REALLY_EXTERN (decl) = 1;
2158 else if (visibility > DECL_VISIBILITY (decl)
2159 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2161 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2162 /* This visibility was not specified. */
2163 DECL_VISIBILITY_SPECIFIED (decl) = false;
2167 /* Constrain the visibility of DECL based on the visibility of its template
2168 arguments. */
2170 static void
2171 constrain_visibility_for_template (tree decl, tree targs)
2173 /* If this is a template instantiation, check the innermost
2174 template args for visibility constraints. The outer template
2175 args are covered by the class check. */
2176 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2177 int i;
2178 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2180 int vis = 0;
2182 tree arg = TREE_VEC_ELT (args, i-1);
2183 if (TYPE_P (arg))
2184 vis = type_visibility (arg);
2185 else
2187 if (REFERENCE_REF_P (arg))
2188 arg = TREE_OPERAND (arg, 0);
2189 if (TREE_TYPE (arg))
2190 STRIP_NOPS (arg);
2191 if (TREE_CODE (arg) == ADDR_EXPR)
2192 arg = TREE_OPERAND (arg, 0);
2193 if (VAR_OR_FUNCTION_DECL_P (arg))
2195 if (! TREE_PUBLIC (arg))
2196 vis = VISIBILITY_ANON;
2197 else
2198 vis = DECL_VISIBILITY (arg);
2201 if (vis)
2202 constrain_visibility (decl, vis, true);
2206 /* Like c_determine_visibility, but with additional C++-specific
2207 behavior.
2209 Function-scope entities can rely on the function's visibility because
2210 it is set in start_preparsed_function.
2212 Class-scope entities cannot rely on the class's visibility until the end
2213 of the enclosing class definition.
2215 Note that because namespaces have multiple independent definitions,
2216 namespace visibility is handled elsewhere using the #pragma visibility
2217 machinery rather than by decorating the namespace declaration.
2219 The goal is for constraints from the type to give a diagnostic, and
2220 other constraints to be applied silently. */
2222 void
2223 determine_visibility (tree decl)
2225 tree class_type = NULL_TREE;
2226 bool use_template;
2227 bool orig_visibility_specified;
2228 enum symbol_visibility orig_visibility;
2230 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2232 /* Only relevant for names with external linkage. */
2233 if (!TREE_PUBLIC (decl))
2234 return;
2236 /* Cloned constructors and destructors get the same visibility as
2237 the underlying function. That should be set up in
2238 maybe_clone_body. */
2239 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2241 orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2242 orig_visibility = DECL_VISIBILITY (decl);
2244 if (TREE_CODE (decl) == TYPE_DECL)
2246 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2247 use_template = CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl));
2248 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2249 use_template = 1;
2250 else
2251 use_template = 0;
2253 else if (DECL_LANG_SPECIFIC (decl))
2254 use_template = DECL_USE_TEMPLATE (decl);
2255 else
2256 use_template = 0;
2258 /* If DECL is a member of a class, visibility specifiers on the
2259 class can influence the visibility of the DECL. */
2260 if (DECL_CLASS_SCOPE_P (decl))
2261 class_type = DECL_CONTEXT (decl);
2262 else
2264 /* Not a class member. */
2266 /* Virtual tables have DECL_CONTEXT set to their associated class,
2267 so they are automatically handled above. */
2268 gcc_assert (!VAR_P (decl)
2269 || !DECL_VTABLE_OR_VTT_P (decl));
2271 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2273 /* Local statics and classes get the visibility of their
2274 containing function by default, except that
2275 -fvisibility-inlines-hidden doesn't affect them. */
2276 tree fn = DECL_CONTEXT (decl);
2277 if (DECL_VISIBILITY_SPECIFIED (fn))
2279 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2280 DECL_VISIBILITY_SPECIFIED (decl) =
2281 DECL_VISIBILITY_SPECIFIED (fn);
2283 else
2285 if (DECL_CLASS_SCOPE_P (fn))
2286 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2287 else if (determine_hidden_inline (fn))
2289 DECL_VISIBILITY (decl) = default_visibility;
2290 DECL_VISIBILITY_SPECIFIED (decl) =
2291 visibility_options.inpragma;
2293 else
2295 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2296 DECL_VISIBILITY_SPECIFIED (decl) =
2297 DECL_VISIBILITY_SPECIFIED (fn);
2301 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2302 but have no TEMPLATE_INFO, so don't try to check it. */
2303 use_template = 0;
2305 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2306 && flag_visibility_ms_compat)
2308 /* Under -fvisibility-ms-compat, types are visible by default,
2309 even though their contents aren't. */
2310 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2311 int underlying_vis = type_visibility (underlying_type);
2312 if (underlying_vis == VISIBILITY_ANON
2313 || (CLASS_TYPE_P (underlying_type)
2314 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2315 constrain_visibility (decl, underlying_vis, false);
2316 else
2317 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2319 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2321 /* tinfo visibility is based on the type it's for. */
2322 constrain_visibility
2323 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2325 /* Give the target a chance to override the visibility associated
2326 with DECL. */
2327 if (TREE_PUBLIC (decl)
2328 && !DECL_REALLY_EXTERN (decl)
2329 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2330 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2331 targetm.cxx.determine_class_data_visibility (decl);
2333 else if (use_template)
2334 /* Template instantiations and specializations get visibility based
2335 on their template unless they override it with an attribute. */;
2336 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2338 if (determine_hidden_inline (decl))
2339 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2340 else
2342 /* Set default visibility to whatever the user supplied with
2343 #pragma GCC visibility or a namespace visibility attribute. */
2344 DECL_VISIBILITY (decl) = default_visibility;
2345 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2350 if (use_template)
2352 /* If the specialization doesn't specify visibility, use the
2353 visibility from the template. */
2354 tree tinfo = get_template_info (decl);
2355 tree args = TI_ARGS (tinfo);
2356 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2357 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2358 : DECL_ATTRIBUTES (decl));
2360 if (args != error_mark_node)
2362 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2364 if (!DECL_VISIBILITY_SPECIFIED (decl))
2366 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2367 && determine_hidden_inline (decl))
2368 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2369 else
2371 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2372 DECL_VISIBILITY_SPECIFIED (decl)
2373 = DECL_VISIBILITY_SPECIFIED (pattern);
2377 if (args
2378 /* Template argument visibility outweighs #pragma or namespace
2379 visibility, but not an explicit attribute. */
2380 && !lookup_attribute ("visibility", attribs))
2382 int depth = TMPL_ARGS_DEPTH (args);
2383 if (DECL_VISIBILITY_SPECIFIED (decl))
2385 /* A class template member with explicit visibility
2386 overrides the class visibility, so we need to apply
2387 all the levels of template args directly. */
2388 int i;
2389 for (i = 1; i <= depth; ++i)
2391 tree lev = TMPL_ARGS_LEVEL (args, i);
2392 constrain_visibility_for_template (decl, lev);
2395 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2396 /* Limit visibility based on its template arguments. */
2397 constrain_visibility_for_template (decl, args);
2402 if (class_type)
2403 determine_visibility_from_class (decl, class_type);
2405 if (decl_anon_ns_mem_p (decl))
2406 /* Names in an anonymous namespace get internal linkage.
2407 This might change once we implement export. */
2408 constrain_visibility (decl, VISIBILITY_ANON, false);
2409 else if (TREE_CODE (decl) != TYPE_DECL)
2411 /* Propagate anonymity from type to decl. */
2412 int tvis = type_visibility (TREE_TYPE (decl));
2413 if (tvis == VISIBILITY_ANON
2414 || ! DECL_VISIBILITY_SPECIFIED (decl))
2415 constrain_visibility (decl, tvis, false);
2417 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2418 /* DR 757: A type without linkage shall not be used as the type of a
2419 variable or function with linkage, unless
2420 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2421 o the variable or function is not used (3.2 [basic.def.odr]) or is
2422 defined in the same translation unit.
2424 Since non-extern "C" decls need to be defined in the same
2425 translation unit, we can make the type internal. */
2426 constrain_visibility (decl, VISIBILITY_ANON, false);
2428 /* If visibility changed and DECL already has DECL_RTL, ensure
2429 symbol flags are updated. */
2430 if ((DECL_VISIBILITY (decl) != orig_visibility
2431 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2432 && ((VAR_P (decl) && TREE_STATIC (decl))
2433 || TREE_CODE (decl) == FUNCTION_DECL)
2434 && DECL_RTL_SET_P (decl))
2435 make_decl_rtl (decl);
2438 /* By default, static data members and function members receive
2439 the visibility of their containing class. */
2441 static void
2442 determine_visibility_from_class (tree decl, tree class_type)
2444 if (DECL_VISIBILITY_SPECIFIED (decl))
2445 return;
2447 if (determine_hidden_inline (decl))
2448 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2449 else
2451 /* Default to the class visibility. */
2452 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2453 DECL_VISIBILITY_SPECIFIED (decl)
2454 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2457 /* Give the target a chance to override the visibility associated
2458 with DECL. */
2459 if (VAR_P (decl)
2460 && (DECL_TINFO_P (decl)
2461 || (DECL_VTABLE_OR_VTT_P (decl)
2462 /* Construction virtual tables are not exported because
2463 they cannot be referred to from other object files;
2464 their name is not standardized by the ABI. */
2465 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2466 && TREE_PUBLIC (decl)
2467 && !DECL_REALLY_EXTERN (decl)
2468 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2469 targetm.cxx.determine_class_data_visibility (decl);
2472 /* Returns true iff DECL is an inline that should get hidden visibility
2473 because of -fvisibility-inlines-hidden. */
2475 static bool
2476 determine_hidden_inline (tree decl)
2478 return (visibility_options.inlines_hidden
2479 /* Don't do this for inline templates; specializations might not be
2480 inline, and we don't want them to inherit the hidden
2481 visibility. We'll set it here for all inline instantiations. */
2482 && !processing_template_decl
2483 && TREE_CODE (decl) == FUNCTION_DECL
2484 && DECL_DECLARED_INLINE_P (decl)
2485 && (! DECL_LANG_SPECIFIC (decl)
2486 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2489 /* Constrain the visibility of a class TYPE based on the visibility of its
2490 field types. Warn if any fields require lesser visibility. */
2492 void
2493 constrain_class_visibility (tree type)
2495 tree binfo;
2496 tree t;
2497 int i;
2499 int vis = type_visibility (type);
2501 if (vis == VISIBILITY_ANON
2502 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2503 return;
2505 /* Don't warn about visibility if the class has explicit visibility. */
2506 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2507 vis = VISIBILITY_INTERNAL;
2509 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2510 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2512 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2513 int subvis = type_visibility (ftype);
2515 if (subvis == VISIBILITY_ANON)
2517 if (!in_main_input_context())
2519 tree nlt = no_linkage_check (ftype, /*relaxed_p=*/false);
2520 if (nlt)
2522 if (same_type_p (TREE_TYPE (t), nlt))
2523 warning (OPT_Wsubobject_linkage, "\
2524 %qT has a field %qD whose type has no linkage",
2525 type, t);
2526 else
2527 warning (OPT_Wsubobject_linkage, "\
2528 %qT has a field %qD whose type depends on the type %qT which has no linkage",
2529 type, t, nlt);
2531 else
2532 warning (OPT_Wsubobject_linkage, "\
2533 %qT has a field %qD whose type uses the anonymous namespace",
2534 type, t);
2537 else if (MAYBE_CLASS_TYPE_P (ftype)
2538 && vis < VISIBILITY_HIDDEN
2539 && subvis >= VISIBILITY_HIDDEN)
2540 warning (OPT_Wattributes, "\
2541 %qT declared with greater visibility than the type of its field %qD",
2542 type, t);
2545 binfo = TYPE_BINFO (type);
2546 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2548 int subvis = type_visibility (TREE_TYPE (t));
2550 if (subvis == VISIBILITY_ANON)
2552 if (!in_main_input_context())
2554 tree nlt = no_linkage_check (TREE_TYPE (t), /*relaxed_p=*/false);
2555 if (nlt)
2557 if (same_type_p (TREE_TYPE (t), nlt))
2558 warning (OPT_Wsubobject_linkage, "\
2559 %qT has a base %qT whose type has no linkage",
2560 type, TREE_TYPE (t));
2561 else
2562 warning (OPT_Wsubobject_linkage, "\
2563 %qT has a base %qT whose type depends on the type %qT which has no linkage",
2564 type, TREE_TYPE (t), nlt);
2566 else
2567 warning (OPT_Wsubobject_linkage, "\
2568 %qT has a base %qT whose type uses the anonymous namespace",
2569 type, TREE_TYPE (t));
2572 else if (vis < VISIBILITY_HIDDEN
2573 && subvis >= VISIBILITY_HIDDEN)
2574 warning (OPT_Wattributes, "\
2575 %qT declared with greater visibility than its base %qT",
2576 type, TREE_TYPE (t));
2580 /* Functions for adjusting the visibility of a tagged type and its nested
2581 types and declarations when it gets a name for linkage purposes from a
2582 typedef. */
2584 static void bt_reset_linkage_1 (binding_entry, void *);
2585 static void bt_reset_linkage_2 (binding_entry, void *);
2587 /* First reset the visibility of all the types. */
2589 static void
2590 reset_type_linkage_1 (tree type)
2592 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2593 if (CLASS_TYPE_P (type))
2594 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2595 bt_reset_linkage_1, NULL);
2597 static void
2598 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2600 reset_type_linkage_1 (b->type);
2603 /* Then reset the visibility of any static data members or member
2604 functions that use those types. */
2606 static void
2607 reset_decl_linkage (tree decl)
2609 if (TREE_PUBLIC (decl))
2610 return;
2611 if (DECL_CLONED_FUNCTION_P (decl))
2612 return;
2613 TREE_PUBLIC (decl) = true;
2614 DECL_INTERFACE_KNOWN (decl) = false;
2615 determine_visibility (decl);
2616 tentative_decl_linkage (decl);
2618 static void
2619 reset_type_linkage_2 (tree type)
2621 if (CLASS_TYPE_P (type))
2623 if (tree vt = CLASSTYPE_VTABLES (type))
2625 tree name = mangle_vtbl_for_type (type);
2626 DECL_NAME (vt) = name;
2627 SET_DECL_ASSEMBLER_NAME (vt, name);
2628 reset_decl_linkage (vt);
2630 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2632 tree name = mangle_typeinfo_for_type (type);
2633 DECL_NAME (ti) = name;
2634 SET_DECL_ASSEMBLER_NAME (ti, name);
2635 TREE_TYPE (name) = type;
2636 reset_decl_linkage (ti);
2638 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2640 tree mem = STRIP_TEMPLATE (m);
2641 if (VAR_P (mem))
2642 reset_decl_linkage (mem);
2644 for (tree m = TYPE_METHODS (type); m; m = DECL_CHAIN (m))
2646 tree mem = STRIP_TEMPLATE (m);
2647 reset_decl_linkage (mem);
2648 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (mem))
2650 /* Also update its name, for cxx_dwarf_name. */
2651 DECL_NAME (mem) = TYPE_IDENTIFIER (type);
2652 if (m != mem)
2653 DECL_NAME (m) = TYPE_IDENTIFIER (type);
2656 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2657 bt_reset_linkage_2, NULL);
2660 static void
2661 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2663 reset_type_linkage_2 (b->type);
2665 void
2666 reset_type_linkage (tree type)
2668 reset_type_linkage_1 (type);
2669 reset_type_linkage_2 (type);
2672 /* Set up our initial idea of what the linkage of DECL should be. */
2674 void
2675 tentative_decl_linkage (tree decl)
2677 if (DECL_INTERFACE_KNOWN (decl))
2678 /* We've already made a decision as to how this function will
2679 be handled. */;
2680 else if (vague_linkage_p (decl))
2682 if (TREE_CODE (decl) == FUNCTION_DECL
2683 && decl_defined_p (decl))
2685 DECL_EXTERNAL (decl) = 1;
2686 DECL_NOT_REALLY_EXTERN (decl) = 1;
2687 note_vague_linkage_fn (decl);
2688 /* A non-template inline function with external linkage will
2689 always be COMDAT. As we must eventually determine the
2690 linkage of all functions, and as that causes writes to
2691 the data mapped in from the PCH file, it's advantageous
2692 to mark the functions at this point. */
2693 if (DECL_DECLARED_INLINE_P (decl)
2694 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2695 || DECL_DEFAULTED_FN (decl)))
2697 /* This function must have external linkage, as
2698 otherwise DECL_INTERFACE_KNOWN would have been
2699 set. */
2700 gcc_assert (TREE_PUBLIC (decl));
2701 comdat_linkage (decl);
2702 DECL_INTERFACE_KNOWN (decl) = 1;
2705 else if (VAR_P (decl))
2706 maybe_commonize_var (decl);
2710 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2711 for DECL has not already been determined, do so now by setting
2712 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2713 function is called entities with vague linkage whose definitions
2714 are available must have TREE_PUBLIC set.
2716 If this function decides to place DECL in COMDAT, it will set
2717 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2718 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2719 callers defer that decision until it is clear that DECL is actually
2720 required. */
2722 void
2723 import_export_decl (tree decl)
2725 int emit_p;
2726 bool comdat_p;
2727 bool import_p;
2728 tree class_type = NULL_TREE;
2730 if (DECL_INTERFACE_KNOWN (decl))
2731 return;
2733 /* We cannot determine what linkage to give to an entity with vague
2734 linkage until the end of the file. For example, a virtual table
2735 for a class will be defined if and only if the key method is
2736 defined in this translation unit. As a further example, consider
2737 that when compiling a translation unit that uses PCH file with
2738 "-frepo" it would be incorrect to make decisions about what
2739 entities to emit when building the PCH; those decisions must be
2740 delayed until the repository information has been processed. */
2741 gcc_assert (at_eof);
2742 /* Object file linkage for explicit instantiations is handled in
2743 mark_decl_instantiated. For static variables in functions with
2744 vague linkage, maybe_commonize_var is used.
2746 Therefore, the only declarations that should be provided to this
2747 function are those with external linkage that are:
2749 * implicit instantiations of function templates
2751 * inline function
2753 * implicit instantiations of static data members of class
2754 templates
2756 * virtual tables
2758 * typeinfo objects
2760 Furthermore, all entities that reach this point must have a
2761 definition available in this translation unit.
2763 The following assertions check these conditions. */
2764 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2765 /* Any code that creates entities with TREE_PUBLIC cleared should
2766 also set DECL_INTERFACE_KNOWN. */
2767 gcc_assert (TREE_PUBLIC (decl));
2768 if (TREE_CODE (decl) == FUNCTION_DECL)
2769 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2770 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2771 || DECL_DECLARED_INLINE_P (decl));
2772 else
2773 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2774 || DECL_VTABLE_OR_VTT_P (decl)
2775 || DECL_TINFO_P (decl));
2776 /* Check that a definition of DECL is available in this translation
2777 unit. */
2778 gcc_assert (!DECL_REALLY_EXTERN (decl));
2780 /* Assume that DECL will not have COMDAT linkage. */
2781 comdat_p = false;
2782 /* Assume that DECL will not be imported into this translation
2783 unit. */
2784 import_p = false;
2786 /* See if the repository tells us whether or not to emit DECL in
2787 this translation unit. */
2788 emit_p = repo_emit_p (decl);
2789 if (emit_p == 0)
2790 import_p = true;
2791 else if (emit_p == 1)
2793 /* The repository indicates that this entity should be defined
2794 here. Make sure the back end honors that request. */
2795 mark_needed (decl);
2796 /* Output the definition as an ordinary strong definition. */
2797 DECL_EXTERNAL (decl) = 0;
2798 DECL_INTERFACE_KNOWN (decl) = 1;
2799 return;
2802 if (import_p)
2803 /* We have already decided what to do with this DECL; there is no
2804 need to check anything further. */
2806 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2808 class_type = DECL_CONTEXT (decl);
2809 import_export_class (class_type);
2810 if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2811 && CLASSTYPE_INTERFACE_ONLY (class_type))
2812 import_p = true;
2813 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2814 && !CLASSTYPE_USE_TEMPLATE (class_type)
2815 && CLASSTYPE_KEY_METHOD (class_type)
2816 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2817 /* The ABI requires that all virtual tables be emitted with
2818 COMDAT linkage. However, on systems where COMDAT symbols
2819 don't show up in the table of contents for a static
2820 archive, or on systems without weak symbols (where we
2821 approximate COMDAT linkage by using internal linkage), the
2822 linker will report errors about undefined symbols because
2823 it will not see the virtual table definition. Therefore,
2824 in the case that we know that the virtual table will be
2825 emitted in only one translation unit, we make the virtual
2826 table an ordinary definition with external linkage. */
2827 DECL_EXTERNAL (decl) = 0;
2828 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2830 /* CLASS_TYPE is being exported from this translation unit,
2831 so DECL should be defined here. */
2832 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2833 /* If a class is declared in a header with the "extern
2834 template" extension, then it will not be instantiated,
2835 even in translation units that would normally require
2836 it. Often such classes are explicitly instantiated in
2837 one translation unit. Therefore, the explicit
2838 instantiation must be made visible to other translation
2839 units. */
2840 DECL_EXTERNAL (decl) = 0;
2841 else
2843 /* The generic C++ ABI says that class data is always
2844 COMDAT, even if there is a key function. Some
2845 variants (e.g., the ARM EABI) says that class data
2846 only has COMDAT linkage if the class data might be
2847 emitted in more than one translation unit. When the
2848 key method can be inline and is inline, we still have
2849 to arrange for comdat even though
2850 class_data_always_comdat is false. */
2851 if (!CLASSTYPE_KEY_METHOD (class_type)
2852 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2853 || targetm.cxx.class_data_always_comdat ())
2855 /* The ABI requires COMDAT linkage. Normally, we
2856 only emit COMDAT things when they are needed;
2857 make sure that we realize that this entity is
2858 indeed needed. */
2859 comdat_p = true;
2860 mark_needed (decl);
2864 else if (!flag_implicit_templates
2865 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2866 import_p = true;
2867 else
2868 comdat_p = true;
2870 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2872 tree type = TREE_TYPE (DECL_NAME (decl));
2873 if (CLASS_TYPE_P (type))
2875 class_type = type;
2876 import_export_class (type);
2877 if (CLASSTYPE_INTERFACE_KNOWN (type)
2878 && TYPE_POLYMORPHIC_P (type)
2879 && CLASSTYPE_INTERFACE_ONLY (type)
2880 /* If -fno-rtti was specified, then we cannot be sure
2881 that RTTI information will be emitted with the
2882 virtual table of the class, so we must emit it
2883 wherever it is used. */
2884 && flag_rtti)
2885 import_p = true;
2886 else
2888 if (CLASSTYPE_INTERFACE_KNOWN (type)
2889 && !CLASSTYPE_INTERFACE_ONLY (type))
2891 comdat_p = (targetm.cxx.class_data_always_comdat ()
2892 || (CLASSTYPE_KEY_METHOD (type)
2893 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2894 mark_needed (decl);
2895 if (!flag_weak)
2897 comdat_p = false;
2898 DECL_EXTERNAL (decl) = 0;
2901 else
2902 comdat_p = true;
2905 else
2906 comdat_p = true;
2908 else if (DECL_TEMPLOID_INSTANTIATION (decl))
2910 /* DECL is an implicit instantiation of a function or static
2911 data member. */
2912 if ((flag_implicit_templates
2913 && !flag_use_repository)
2914 || (flag_implicit_inline_templates
2915 && TREE_CODE (decl) == FUNCTION_DECL
2916 && DECL_DECLARED_INLINE_P (decl)))
2917 comdat_p = true;
2918 else
2919 /* If we are not implicitly generating templates, then mark
2920 this entity as undefined in this translation unit. */
2921 import_p = true;
2923 else if (DECL_FUNCTION_MEMBER_P (decl))
2925 if (!DECL_DECLARED_INLINE_P (decl))
2927 tree ctype = DECL_CONTEXT (decl);
2928 import_export_class (ctype);
2929 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2931 DECL_NOT_REALLY_EXTERN (decl)
2932 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2933 || (DECL_DECLARED_INLINE_P (decl)
2934 && ! flag_implement_inlines
2935 && !DECL_VINDEX (decl)));
2937 if (!DECL_NOT_REALLY_EXTERN (decl))
2938 DECL_EXTERNAL (decl) = 1;
2940 /* Always make artificials weak. */
2941 if (DECL_ARTIFICIAL (decl) && flag_weak)
2942 comdat_p = true;
2943 else
2944 maybe_make_one_only (decl);
2947 else
2948 comdat_p = true;
2950 else
2951 comdat_p = true;
2953 if (import_p)
2955 /* If we are importing DECL into this translation unit, mark is
2956 an undefined here. */
2957 DECL_EXTERNAL (decl) = 1;
2958 DECL_NOT_REALLY_EXTERN (decl) = 0;
2960 else if (comdat_p)
2962 /* If we decided to put DECL in COMDAT, mark it accordingly at
2963 this point. */
2964 comdat_linkage (decl);
2967 DECL_INTERFACE_KNOWN (decl) = 1;
2970 /* Return an expression that performs the destruction of DECL, which
2971 must be a VAR_DECL whose type has a non-trivial destructor, or is
2972 an array whose (innermost) elements have a non-trivial destructor. */
2974 tree
2975 build_cleanup (tree decl)
2977 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
2978 gcc_assert (clean != NULL_TREE);
2979 return clean;
2982 /* Returns the initialization guard variable for the variable DECL,
2983 which has static storage duration. */
2985 tree
2986 get_guard (tree decl)
2988 tree sname;
2989 tree guard;
2991 sname = mangle_guard_variable (decl);
2992 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2993 if (! guard)
2995 tree guard_type;
2997 /* We use a type that is big enough to contain a mutex as well
2998 as an integer counter. */
2999 guard_type = targetm.cxx.guard_type ();
3000 guard = build_decl (DECL_SOURCE_LOCATION (decl),
3001 VAR_DECL, sname, guard_type);
3003 /* The guard should have the same linkage as what it guards. */
3004 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
3005 TREE_STATIC (guard) = TREE_STATIC (decl);
3006 DECL_COMMON (guard) = DECL_COMMON (decl);
3007 DECL_COMDAT (guard) = DECL_COMDAT (decl);
3008 CP_DECL_THREAD_LOCAL_P (guard) = CP_DECL_THREAD_LOCAL_P (decl);
3009 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
3010 if (DECL_ONE_ONLY (decl))
3011 make_decl_one_only (guard, cxx_comdat_group (guard));
3012 if (TREE_PUBLIC (decl))
3013 DECL_WEAK (guard) = DECL_WEAK (decl);
3014 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
3015 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
3017 DECL_ARTIFICIAL (guard) = 1;
3018 DECL_IGNORED_P (guard) = 1;
3019 TREE_USED (guard) = 1;
3020 pushdecl_top_level_and_finish (guard, NULL_TREE);
3022 return guard;
3025 /* Return an atomic load of src with the appropriate memory model. */
3027 static tree
3028 build_atomic_load_byte (tree src, HOST_WIDE_INT model)
3030 tree ptr_type = build_pointer_type (char_type_node);
3031 tree mem_model = build_int_cst (integer_type_node, model);
3032 tree t, addr, val;
3033 unsigned int size;
3034 int fncode;
3036 size = tree_to_uhwi (TYPE_SIZE_UNIT (char_type_node));
3038 fncode = BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1;
3039 t = builtin_decl_implicit ((enum built_in_function) fncode);
3041 addr = build1 (ADDR_EXPR, ptr_type, src);
3042 val = build_call_expr (t, 2, addr, mem_model);
3043 return val;
3046 /* Return those bits of the GUARD variable that should be set when the
3047 guarded entity is actually initialized. */
3049 static tree
3050 get_guard_bits (tree guard)
3052 if (!targetm.cxx.guard_mask_bit ())
3054 /* We only set the first byte of the guard, in order to leave room
3055 for a mutex in the high-order bits. */
3056 guard = build1 (ADDR_EXPR,
3057 build_pointer_type (TREE_TYPE (guard)),
3058 guard);
3059 guard = build1 (NOP_EXPR,
3060 build_pointer_type (char_type_node),
3061 guard);
3062 guard = build1 (INDIRECT_REF, char_type_node, guard);
3065 return guard;
3068 /* Return an expression which determines whether or not the GUARD
3069 variable has already been initialized. */
3071 tree
3072 get_guard_cond (tree guard, bool thread_safe)
3074 tree guard_value;
3076 if (!thread_safe)
3077 guard = get_guard_bits (guard);
3078 else
3079 guard = build_atomic_load_byte (guard, MEMMODEL_ACQUIRE);
3081 /* Mask off all but the low bit. */
3082 if (targetm.cxx.guard_mask_bit ())
3084 guard_value = integer_one_node;
3085 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3086 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3087 guard = cp_build_binary_op (input_location,
3088 BIT_AND_EXPR, guard, guard_value,
3089 tf_warning_or_error);
3092 guard_value = integer_zero_node;
3093 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3094 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3095 return cp_build_binary_op (input_location,
3096 EQ_EXPR, guard, guard_value,
3097 tf_warning_or_error);
3100 /* Return an expression which sets the GUARD variable, indicating that
3101 the variable being guarded has been initialized. */
3103 tree
3104 set_guard (tree guard)
3106 tree guard_init;
3108 /* Set the GUARD to one. */
3109 guard = get_guard_bits (guard);
3110 guard_init = integer_one_node;
3111 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3112 guard_init = fold_convert (TREE_TYPE (guard), guard_init);
3113 return cp_build_modify_expr (input_location, guard, NOP_EXPR, guard_init,
3114 tf_warning_or_error);
3117 /* Returns true iff we can tell that VAR does not have a dynamic
3118 initializer. */
3120 static bool
3121 var_defined_without_dynamic_init (tree var)
3123 /* If it's defined in another TU, we can't tell. */
3124 if (DECL_EXTERNAL (var))
3125 return false;
3126 /* If it has a non-trivial destructor, registering the destructor
3127 counts as dynamic initialization. */
3128 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3129 return false;
3130 /* If it's in this TU, its initializer has been processed, unless
3131 it's a case of self-initialization, then DECL_INITIALIZED_P is
3132 false while the initializer is handled by finish_id_expression. */
3133 if (!DECL_INITIALIZED_P (var))
3134 return false;
3135 /* If it has no initializer or a constant one, it's not dynamic. */
3136 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3137 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3140 /* Returns true iff VAR is a variable that needs uses to be
3141 wrapped for possible dynamic initialization. */
3143 static bool
3144 var_needs_tls_wrapper (tree var)
3146 return (!error_operand_p (var)
3147 && CP_DECL_THREAD_LOCAL_P (var)
3148 && !DECL_GNU_TLS_P (var)
3149 && !DECL_FUNCTION_SCOPE_P (var)
3150 && !var_defined_without_dynamic_init (var));
3153 /* Get the FUNCTION_DECL for the shared TLS init function for this
3154 translation unit. */
3156 static tree
3157 get_local_tls_init_fn (void)
3159 tree sname = get_identifier ("__tls_init");
3160 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3161 if (!fn)
3163 fn = build_lang_decl (FUNCTION_DECL, sname,
3164 build_function_type (void_type_node,
3165 void_list_node));
3166 SET_DECL_LANGUAGE (fn, lang_c);
3167 TREE_PUBLIC (fn) = false;
3168 DECL_ARTIFICIAL (fn) = true;
3169 mark_used (fn);
3170 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3172 return fn;
3175 /* Get a FUNCTION_DECL for the init function for the thread_local
3176 variable VAR. The init function will be an alias to the function
3177 that initializes all the non-local TLS variables in the translation
3178 unit. The init function is only used by the wrapper function. */
3180 static tree
3181 get_tls_init_fn (tree var)
3183 /* Only C++11 TLS vars need this init fn. */
3184 if (!var_needs_tls_wrapper (var))
3185 return NULL_TREE;
3187 /* If -fno-extern-tls-init, assume that we don't need to call
3188 a tls init function for a variable defined in another TU. */
3189 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3190 return NULL_TREE;
3192 #ifdef ASM_OUTPUT_DEF
3193 /* If the variable is internal, or if we can't generate aliases,
3194 call the local init function directly. */
3195 if (!TREE_PUBLIC (var))
3196 #endif
3197 return get_local_tls_init_fn ();
3199 tree sname = mangle_tls_init_fn (var);
3200 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3201 if (!fn)
3203 fn = build_lang_decl (FUNCTION_DECL, sname,
3204 build_function_type (void_type_node,
3205 void_list_node));
3206 SET_DECL_LANGUAGE (fn, lang_c);
3207 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3208 DECL_ARTIFICIAL (fn) = true;
3209 DECL_COMDAT (fn) = DECL_COMDAT (var);
3210 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3211 if (DECL_ONE_ONLY (var))
3212 make_decl_one_only (fn, cxx_comdat_group (fn));
3213 if (TREE_PUBLIC (var))
3215 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3216 /* If the variable is defined somewhere else and might have static
3217 initialization, make the init function a weak reference. */
3218 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3219 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3220 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3221 && DECL_EXTERNAL (var))
3222 declare_weak (fn);
3223 else
3224 DECL_WEAK (fn) = DECL_WEAK (var);
3226 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3227 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3228 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3229 DECL_IGNORED_P (fn) = 1;
3230 mark_used (fn);
3232 DECL_BEFRIENDING_CLASSES (fn) = var;
3234 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3236 return fn;
3239 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3240 variable VAR. The wrapper function calls the init function (if any) for
3241 VAR and then returns a reference to VAR. The wrapper function is used
3242 in place of VAR everywhere VAR is mentioned. */
3244 tree
3245 get_tls_wrapper_fn (tree var)
3247 /* Only C++11 TLS vars need this wrapper fn. */
3248 if (!var_needs_tls_wrapper (var))
3249 return NULL_TREE;
3251 tree sname = mangle_tls_wrapper_fn (var);
3252 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3253 if (!fn)
3255 /* A named rvalue reference is an lvalue, so the wrapper should
3256 always return an lvalue reference. */
3257 tree type = non_reference (TREE_TYPE (var));
3258 type = build_reference_type (type);
3259 tree fntype = build_function_type (type, void_list_node);
3260 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3261 SET_DECL_LANGUAGE (fn, lang_c);
3262 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3263 DECL_ARTIFICIAL (fn) = true;
3264 DECL_IGNORED_P (fn) = 1;
3265 /* The wrapper is inline and emitted everywhere var is used. */
3266 DECL_DECLARED_INLINE_P (fn) = true;
3267 if (TREE_PUBLIC (var))
3269 comdat_linkage (fn);
3270 #ifdef HAVE_GAS_HIDDEN
3271 /* Make the wrapper bind locally; there's no reason to share
3272 the wrapper between multiple shared objects. */
3273 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3274 DECL_VISIBILITY_SPECIFIED (fn) = true;
3275 #endif
3277 if (!TREE_PUBLIC (fn))
3278 DECL_INTERFACE_KNOWN (fn) = true;
3279 mark_used (fn);
3280 note_vague_linkage_fn (fn);
3282 #if 0
3283 /* We want CSE to commonize calls to the wrapper, but marking it as
3284 pure is unsafe since it has side-effects. I guess we need a new
3285 ECF flag even weaker than ECF_PURE. FIXME! */
3286 DECL_PURE_P (fn) = true;
3287 #endif
3289 DECL_BEFRIENDING_CLASSES (fn) = var;
3291 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3293 return fn;
3296 /* At EOF, generate the definition for the TLS wrapper function FN:
3298 T& var_wrapper() {
3299 if (init_fn) init_fn();
3300 return var;
3301 } */
3303 static void
3304 generate_tls_wrapper (tree fn)
3306 tree var = DECL_BEFRIENDING_CLASSES (fn);
3308 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3309 tree body = begin_function_body ();
3310 /* Only call the init fn if there might be one. */
3311 if (tree init_fn = get_tls_init_fn (var))
3313 tree if_stmt = NULL_TREE;
3314 /* If init_fn is a weakref, make sure it exists before calling. */
3315 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3317 if_stmt = begin_if_stmt ();
3318 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3319 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3320 NE_EXPR, addr, nullptr_node,
3321 tf_warning_or_error);
3322 finish_if_stmt_cond (cond, if_stmt);
3324 finish_expr_stmt (build_cxx_call
3325 (init_fn, 0, NULL, tf_warning_or_error));
3326 if (if_stmt)
3328 finish_then_clause (if_stmt);
3329 finish_if_stmt (if_stmt);
3332 else
3333 /* If there's no initialization, the wrapper is a constant function. */
3334 TREE_READONLY (fn) = true;
3335 finish_return_stmt (convert_from_reference (var));
3336 finish_function_body (body);
3337 expand_or_defer_fn (finish_function (0));
3340 /* Start the process of running a particular set of global constructors
3341 or destructors. Subroutine of do_[cd]tors. Also called from
3342 vtv_start_verification_constructor_init_function. */
3344 static tree
3345 start_objects (int method_type, int initp)
3347 tree body;
3348 tree fndecl;
3349 char type[14];
3351 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3353 if (initp != DEFAULT_INIT_PRIORITY)
3355 char joiner;
3357 #ifdef JOINER
3358 joiner = JOINER;
3359 #else
3360 joiner = '_';
3361 #endif
3363 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3365 else
3366 sprintf (type, "sub_%c", method_type);
3368 fndecl = build_lang_decl (FUNCTION_DECL,
3369 get_file_function_name (type),
3370 build_function_type_list (void_type_node,
3371 NULL_TREE));
3372 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3374 TREE_PUBLIC (current_function_decl) = 0;
3376 /* Mark as artificial because it's not explicitly in the user's
3377 source code. */
3378 DECL_ARTIFICIAL (current_function_decl) = 1;
3380 /* Mark this declaration as used to avoid spurious warnings. */
3381 TREE_USED (current_function_decl) = 1;
3383 /* Mark this function as a global constructor or destructor. */
3384 if (method_type == 'I')
3385 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3386 else
3387 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3389 body = begin_compound_stmt (BCS_FN_BODY);
3391 return body;
3394 /* Finish the process of running a particular set of global constructors
3395 or destructors. Subroutine of do_[cd]tors. */
3397 static void
3398 finish_objects (int method_type, int initp, tree body)
3400 tree fn;
3402 /* Finish up. */
3403 finish_compound_stmt (body);
3404 fn = finish_function (0);
3406 if (method_type == 'I')
3408 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3409 decl_init_priority_insert (fn, initp);
3411 else
3413 DECL_STATIC_DESTRUCTOR (fn) = 1;
3414 decl_fini_priority_insert (fn, initp);
3417 expand_or_defer_fn (fn);
3420 /* The names of the parameters to the function created to handle
3421 initializations and destructions for objects with static storage
3422 duration. */
3423 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3424 #define PRIORITY_IDENTIFIER "__priority"
3426 /* The name of the function we create to handle initializations and
3427 destructions for objects with static storage duration. */
3428 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3430 /* The declaration for the __INITIALIZE_P argument. */
3431 static GTY(()) tree initialize_p_decl;
3433 /* The declaration for the __PRIORITY argument. */
3434 static GTY(()) tree priority_decl;
3436 /* The declaration for the static storage duration function. */
3437 static GTY(()) tree ssdf_decl;
3439 /* All the static storage duration functions created in this
3440 translation unit. */
3441 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3443 /* A map from priority levels to information about that priority
3444 level. There may be many such levels, so efficient lookup is
3445 important. */
3446 static splay_tree priority_info_map;
3448 /* Begins the generation of the function that will handle all
3449 initialization and destruction of objects with static storage
3450 duration. The function generated takes two parameters of type
3451 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3452 nonzero, it performs initializations. Otherwise, it performs
3453 destructions. It only performs those initializations or
3454 destructions with the indicated __PRIORITY. The generated function
3455 returns no value.
3457 It is assumed that this function will only be called once per
3458 translation unit. */
3460 static tree
3461 start_static_storage_duration_function (unsigned count)
3463 tree type;
3464 tree body;
3465 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3467 /* Create the identifier for this function. It will be of the form
3468 SSDF_IDENTIFIER_<number>. */
3469 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3471 type = build_function_type_list (void_type_node,
3472 integer_type_node, integer_type_node,
3473 NULL_TREE);
3475 /* Create the FUNCTION_DECL itself. */
3476 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3477 get_identifier (id),
3478 type);
3479 TREE_PUBLIC (ssdf_decl) = 0;
3480 DECL_ARTIFICIAL (ssdf_decl) = 1;
3482 /* Put this function in the list of functions to be called from the
3483 static constructors and destructors. */
3484 if (!ssdf_decls)
3486 vec_alloc (ssdf_decls, 32);
3488 /* Take this opportunity to initialize the map from priority
3489 numbers to information about that priority level. */
3490 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3491 /*delete_key_fn=*/0,
3492 /*delete_value_fn=*/
3493 (splay_tree_delete_value_fn) &free);
3495 /* We always need to generate functions for the
3496 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3497 priorities later, we'll be sure to find the
3498 DEFAULT_INIT_PRIORITY. */
3499 get_priority_info (DEFAULT_INIT_PRIORITY);
3502 vec_safe_push (ssdf_decls, ssdf_decl);
3504 /* Create the argument list. */
3505 initialize_p_decl = cp_build_parm_decl
3506 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3507 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
3508 TREE_USED (initialize_p_decl) = 1;
3509 priority_decl = cp_build_parm_decl
3510 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3511 DECL_CONTEXT (priority_decl) = ssdf_decl;
3512 TREE_USED (priority_decl) = 1;
3514 DECL_CHAIN (initialize_p_decl) = priority_decl;
3515 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3517 /* Put the function in the global scope. */
3518 pushdecl (ssdf_decl);
3520 /* Start the function itself. This is equivalent to declaring the
3521 function as:
3523 static void __ssdf (int __initialize_p, init __priority_p);
3525 It is static because we only need to call this function from the
3526 various constructor and destructor functions for this module. */
3527 start_preparsed_function (ssdf_decl,
3528 /*attrs=*/NULL_TREE,
3529 SF_PRE_PARSED);
3531 /* Set up the scope of the outermost block in the function. */
3532 body = begin_compound_stmt (BCS_FN_BODY);
3534 return body;
3537 /* Finish the generation of the function which performs initialization
3538 and destruction of objects with static storage duration. After
3539 this point, no more such objects can be created. */
3541 static void
3542 finish_static_storage_duration_function (tree body)
3544 /* Close out the function. */
3545 finish_compound_stmt (body);
3546 expand_or_defer_fn (finish_function (0));
3549 /* Return the information about the indicated PRIORITY level. If no
3550 code to handle this level has yet been generated, generate the
3551 appropriate prologue. */
3553 static priority_info
3554 get_priority_info (int priority)
3556 priority_info pi;
3557 splay_tree_node n;
3559 n = splay_tree_lookup (priority_info_map,
3560 (splay_tree_key) priority);
3561 if (!n)
3563 /* Create a new priority information structure, and insert it
3564 into the map. */
3565 pi = XNEW (struct priority_info_s);
3566 pi->initializations_p = 0;
3567 pi->destructions_p = 0;
3568 splay_tree_insert (priority_info_map,
3569 (splay_tree_key) priority,
3570 (splay_tree_value) pi);
3572 else
3573 pi = (priority_info) n->value;
3575 return pi;
3578 /* The effective initialization priority of a DECL. */
3580 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3581 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3582 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3584 /* Whether a DECL needs a guard to protect it against multiple
3585 initialization. */
3587 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3588 || DECL_ONE_ONLY (decl) \
3589 || DECL_WEAK (decl)))
3591 /* Called from one_static_initialization_or_destruction(),
3592 via walk_tree.
3593 Walks the initializer list of a global variable and looks for
3594 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3595 and that have their DECL_CONTEXT() == NULL.
3596 For each such temporary variable, set their DECL_CONTEXT() to
3597 the current function. This is necessary because otherwise
3598 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3599 when trying to refer to a temporary variable that does not have
3600 it's DECL_CONTECT() properly set. */
3601 static tree
3602 fix_temporary_vars_context_r (tree *node,
3603 int * /*unused*/,
3604 void * /*unused1*/)
3606 gcc_assert (current_function_decl);
3608 if (TREE_CODE (*node) == BIND_EXPR)
3610 tree var;
3612 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3613 if (VAR_P (var)
3614 && !DECL_NAME (var)
3615 && DECL_ARTIFICIAL (var)
3616 && !DECL_CONTEXT (var))
3617 DECL_CONTEXT (var) = current_function_decl;
3620 return NULL_TREE;
3623 /* Set up to handle the initialization or destruction of DECL. If
3624 INITP is nonzero, we are initializing the variable. Otherwise, we
3625 are destroying it. */
3627 static void
3628 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3630 tree guard_if_stmt = NULL_TREE;
3631 tree guard;
3633 /* If we are supposed to destruct and there's a trivial destructor,
3634 nothing has to be done. */
3635 if (!initp
3636 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3637 return;
3639 /* Trick the compiler into thinking we are at the file and line
3640 where DECL was declared so that error-messages make sense, and so
3641 that the debugger will show somewhat sensible file and line
3642 information. */
3643 input_location = DECL_SOURCE_LOCATION (decl);
3645 /* Make sure temporary variables in the initialiser all have
3646 their DECL_CONTEXT() set to a value different from NULL_TREE.
3647 This can happen when global variables initialisers are built.
3648 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3649 the temporary variables that might have been generated in the
3650 accompagning initialisers is NULL_TREE, meaning the variables have been
3651 declared in the global namespace.
3652 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3653 of the temporaries are set to the current function decl. */
3654 cp_walk_tree_without_duplicates (&init,
3655 fix_temporary_vars_context_r,
3656 NULL);
3658 /* Because of:
3660 [class.access.spec]
3662 Access control for implicit calls to the constructors,
3663 the conversion functions, or the destructor called to
3664 create and destroy a static data member is performed as
3665 if these calls appeared in the scope of the member's
3666 class.
3668 we pretend we are in a static member function of the class of
3669 which the DECL is a member. */
3670 if (member_p (decl))
3672 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3673 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3676 /* Assume we don't need a guard. */
3677 guard = NULL_TREE;
3678 /* We need a guard if this is an object with external linkage that
3679 might be initialized in more than one place. (For example, a
3680 static data member of a template, when the data member requires
3681 construction.) */
3682 if (NEEDS_GUARD_P (decl))
3684 tree guard_cond;
3686 guard = get_guard (decl);
3688 /* When using __cxa_atexit, we just check the GUARD as we would
3689 for a local static. */
3690 if (flag_use_cxa_atexit)
3692 /* When using __cxa_atexit, we never try to destroy
3693 anything from a static destructor. */
3694 gcc_assert (initp);
3695 guard_cond = get_guard_cond (guard, false);
3697 /* If we don't have __cxa_atexit, then we will be running
3698 destructors from .fini sections, or their equivalents. So,
3699 we need to know how many times we've tried to initialize this
3700 object. We do initializations only if the GUARD is zero,
3701 i.e., if we are the first to initialize the variable. We do
3702 destructions only if the GUARD is one, i.e., if we are the
3703 last to destroy the variable. */
3704 else if (initp)
3705 guard_cond
3706 = cp_build_binary_op (input_location,
3707 EQ_EXPR,
3708 cp_build_unary_op (PREINCREMENT_EXPR,
3709 guard,
3710 /*noconvert=*/true,
3711 tf_warning_or_error),
3712 integer_one_node,
3713 tf_warning_or_error);
3714 else
3715 guard_cond
3716 = cp_build_binary_op (input_location,
3717 EQ_EXPR,
3718 cp_build_unary_op (PREDECREMENT_EXPR,
3719 guard,
3720 /*noconvert=*/true,
3721 tf_warning_or_error),
3722 integer_zero_node,
3723 tf_warning_or_error);
3725 guard_if_stmt = begin_if_stmt ();
3726 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3730 /* If we're using __cxa_atexit, we have not already set the GUARD,
3731 so we must do so now. */
3732 if (guard && initp && flag_use_cxa_atexit)
3733 finish_expr_stmt (set_guard (guard));
3735 /* Perform the initialization or destruction. */
3736 if (initp)
3738 if (init)
3740 finish_expr_stmt (init);
3741 if (flag_sanitize & SANITIZE_ADDRESS)
3743 varpool_node *vnode = varpool_node::get (decl);
3744 if (vnode)
3745 vnode->dynamically_initialized = 1;
3749 /* If we're using __cxa_atexit, register a function that calls the
3750 destructor for the object. */
3751 if (flag_use_cxa_atexit)
3752 finish_expr_stmt (register_dtor_fn (decl));
3754 else
3755 finish_expr_stmt (build_cleanup (decl));
3757 /* Finish the guard if-stmt, if necessary. */
3758 if (guard)
3760 finish_then_clause (guard_if_stmt);
3761 finish_if_stmt (guard_if_stmt);
3764 /* Now that we're done with DECL we don't need to pretend to be a
3765 member of its class any longer. */
3766 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3767 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3770 /* Generate code to do the initialization or destruction of the decls in VARS,
3771 a TREE_LIST of VAR_DECL with static storage duration.
3772 Whether initialization or destruction is performed is specified by INITP. */
3774 static void
3775 do_static_initialization_or_destruction (tree vars, bool initp)
3777 tree node, init_if_stmt, cond;
3779 /* Build the outer if-stmt to check for initialization or destruction. */
3780 init_if_stmt = begin_if_stmt ();
3781 cond = initp ? integer_one_node : integer_zero_node;
3782 cond = cp_build_binary_op (input_location,
3783 EQ_EXPR,
3784 initialize_p_decl,
3785 cond,
3786 tf_warning_or_error);
3787 finish_if_stmt_cond (cond, init_if_stmt);
3789 /* To make sure dynamic construction doesn't access globals from other
3790 compilation units where they might not be yet constructed, for
3791 -fsanitize=address insert __asan_before_dynamic_init call that
3792 prevents access to either all global variables that need construction
3793 in other compilation units, or at least those that haven't been
3794 initialized yet. Variables that need dynamic construction in
3795 the current compilation unit are kept accessible. */
3796 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
3797 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3799 node = vars;
3800 do {
3801 tree decl = TREE_VALUE (node);
3802 tree priority_if_stmt;
3803 int priority;
3804 priority_info pi;
3806 /* If we don't need a destructor, there's nothing to do. Avoid
3807 creating a possibly empty if-stmt. */
3808 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3810 node = TREE_CHAIN (node);
3811 continue;
3814 /* Remember that we had an initialization or finalization at this
3815 priority. */
3816 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3817 pi = get_priority_info (priority);
3818 if (initp)
3819 pi->initializations_p = 1;
3820 else
3821 pi->destructions_p = 1;
3823 /* Conditionalize this initialization on being in the right priority
3824 and being initializing/finalizing appropriately. */
3825 priority_if_stmt = begin_if_stmt ();
3826 cond = cp_build_binary_op (input_location,
3827 EQ_EXPR,
3828 priority_decl,
3829 build_int_cst (NULL_TREE, priority),
3830 tf_warning_or_error);
3831 finish_if_stmt_cond (cond, priority_if_stmt);
3833 /* Process initializers with same priority. */
3834 for (; node
3835 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3836 node = TREE_CHAIN (node))
3837 /* Do one initialization or destruction. */
3838 one_static_initialization_or_destruction (TREE_VALUE (node),
3839 TREE_PURPOSE (node), initp);
3841 /* Finish up the priority if-stmt body. */
3842 finish_then_clause (priority_if_stmt);
3843 finish_if_stmt (priority_if_stmt);
3845 } while (node);
3847 /* Revert what __asan_before_dynamic_init did by calling
3848 __asan_after_dynamic_init. */
3849 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
3850 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3852 /* Finish up the init/destruct if-stmt body. */
3853 finish_then_clause (init_if_stmt);
3854 finish_if_stmt (init_if_stmt);
3857 /* VARS is a list of variables with static storage duration which may
3858 need initialization and/or finalization. Remove those variables
3859 that don't really need to be initialized or finalized, and return
3860 the resulting list. The order in which the variables appear in
3861 VARS is in reverse order of the order in which they should actually
3862 be initialized. The list we return is in the unreversed order;
3863 i.e., the first variable should be initialized first. */
3865 static tree
3866 prune_vars_needing_no_initialization (tree *vars)
3868 tree *var = vars;
3869 tree result = NULL_TREE;
3871 while (*var)
3873 tree t = *var;
3874 tree decl = TREE_VALUE (t);
3875 tree init = TREE_PURPOSE (t);
3877 /* Deal gracefully with error. */
3878 if (decl == error_mark_node)
3880 var = &TREE_CHAIN (t);
3881 continue;
3884 /* The only things that can be initialized are variables. */
3885 gcc_assert (VAR_P (decl));
3887 /* If this object is not defined, we don't need to do anything
3888 here. */
3889 if (DECL_EXTERNAL (decl))
3891 var = &TREE_CHAIN (t);
3892 continue;
3895 /* Also, if the initializer already contains errors, we can bail
3896 out now. */
3897 if (init && TREE_CODE (init) == TREE_LIST
3898 && value_member (error_mark_node, init))
3900 var = &TREE_CHAIN (t);
3901 continue;
3904 /* This variable is going to need initialization and/or
3905 finalization, so we add it to the list. */
3906 *var = TREE_CHAIN (t);
3907 TREE_CHAIN (t) = result;
3908 result = t;
3911 return result;
3914 /* Make sure we have told the back end about all the variables in
3915 VARS. */
3917 static void
3918 write_out_vars (tree vars)
3920 tree v;
3922 for (v = vars; v; v = TREE_CHAIN (v))
3924 tree var = TREE_VALUE (v);
3925 if (!var_finalized_p (var))
3927 import_export_decl (var);
3928 rest_of_decl_compilation (var, 1, 1);
3933 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3934 (otherwise) that will initialize all global objects with static
3935 storage duration having the indicated PRIORITY. */
3937 static void
3938 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3939 location_t *locus)
3941 char function_key;
3942 tree fndecl;
3943 tree body;
3944 size_t i;
3946 input_location = *locus;
3947 /* ??? */
3948 /* Was: locus->line++; */
3950 /* We use `I' to indicate initialization and `D' to indicate
3951 destruction. */
3952 function_key = constructor_p ? 'I' : 'D';
3954 /* We emit the function lazily, to avoid generating empty
3955 global constructors and destructors. */
3956 body = NULL_TREE;
3958 /* For Objective-C++, we may need to initialize metadata found in this module.
3959 This must be done _before_ any other static initializations. */
3960 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3961 && constructor_p && objc_static_init_needed_p ())
3963 body = start_objects (function_key, priority);
3964 objc_generate_static_init_call (NULL_TREE);
3967 /* Call the static storage duration function with appropriate
3968 arguments. */
3969 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
3971 /* Calls to pure or const functions will expand to nothing. */
3972 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3974 tree call;
3976 if (! body)
3977 body = start_objects (function_key, priority);
3979 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
3980 build_int_cst (NULL_TREE,
3981 constructor_p),
3982 build_int_cst (NULL_TREE,
3983 priority),
3984 NULL_TREE);
3985 finish_expr_stmt (call);
3989 /* Close out the function. */
3990 if (body)
3991 finish_objects (function_key, priority, body);
3994 /* Generate constructor and destructor functions for the priority
3995 indicated by N. */
3997 static int
3998 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
4000 location_t *locus = (location_t *) data;
4001 int priority = (int) n->key;
4002 priority_info pi = (priority_info) n->value;
4004 /* Generate the functions themselves, but only if they are really
4005 needed. */
4006 if (pi->initializations_p)
4007 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
4008 if (pi->destructions_p)
4009 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
4011 /* Keep iterating. */
4012 return 0;
4015 /* Return C++ property of T, based on given operation OP. */
4017 static int
4018 cpp_check (tree t, cpp_operation op)
4020 switch (op)
4022 case HAS_DEPENDENT_TEMPLATE_ARGS:
4024 tree ti = CLASSTYPE_TEMPLATE_INFO (t);
4025 if (!ti)
4026 return 0;
4027 ++processing_template_decl;
4028 const bool dep = any_dependent_template_arguments_p (TI_ARGS (ti));
4029 --processing_template_decl;
4030 return dep;
4032 case IS_ABSTRACT:
4033 return DECL_PURE_VIRTUAL_P (t);
4034 case IS_CONSTRUCTOR:
4035 return DECL_CONSTRUCTOR_P (t);
4036 case IS_DESTRUCTOR:
4037 return DECL_DESTRUCTOR_P (t);
4038 case IS_COPY_CONSTRUCTOR:
4039 return DECL_COPY_CONSTRUCTOR_P (t);
4040 case IS_MOVE_CONSTRUCTOR:
4041 return DECL_MOVE_CONSTRUCTOR_P (t);
4042 case IS_TEMPLATE:
4043 return TREE_CODE (t) == TEMPLATE_DECL;
4044 case IS_TRIVIAL:
4045 return trivial_type_p (t);
4046 default:
4047 return 0;
4051 /* Collect source file references recursively, starting from NAMESPC. */
4053 static void
4054 collect_source_refs (tree namespc)
4056 tree t;
4058 if (!namespc)
4059 return;
4061 /* Iterate over names in this name space. */
4062 for (t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4063 if (!DECL_IS_BUILTIN (t) )
4064 collect_source_ref (DECL_SOURCE_FILE (t));
4066 /* Dump siblings, if any */
4067 collect_source_refs (TREE_CHAIN (namespc));
4069 /* Dump children, if any */
4070 collect_source_refs (NAMESPACE_LEVEL (namespc)->namespaces);
4073 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4074 starting from NAMESPC. */
4076 static void
4077 collect_ada_namespace (tree namespc, const char *source_file)
4079 if (!namespc)
4080 return;
4082 /* Collect decls from this namespace */
4083 collect_ada_nodes (NAMESPACE_LEVEL (namespc)->names, source_file);
4085 /* Collect siblings, if any */
4086 collect_ada_namespace (TREE_CHAIN (namespc), source_file);
4088 /* Collect children, if any */
4089 collect_ada_namespace (NAMESPACE_LEVEL (namespc)->namespaces, source_file);
4092 /* Returns true iff there is a definition available for variable or
4093 function DECL. */
4095 bool
4096 decl_defined_p (tree decl)
4098 if (TREE_CODE (decl) == FUNCTION_DECL)
4099 return (DECL_INITIAL (decl) != NULL_TREE
4100 /* A pending instantiation of a friend temploid is defined. */
4101 || (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
4102 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4103 (DECL_TI_TEMPLATE (decl)))));
4104 else
4106 gcc_assert (VAR_P (decl));
4107 return !DECL_EXTERNAL (decl);
4111 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4113 [expr.const]
4115 An integral constant-expression can only involve ... const
4116 variables of integral or enumeration types initialized with
4117 constant expressions ...
4119 C++0x also allows constexpr variables and temporaries initialized
4120 with constant expressions. We handle the former here, but the latter
4121 are just folded away in cxx_eval_constant_expression.
4123 The standard does not require that the expression be non-volatile.
4124 G++ implements the proposed correction in DR 457. */
4126 bool
4127 decl_constant_var_p (tree decl)
4129 if (!decl_maybe_constant_var_p (decl))
4130 return false;
4132 /* We don't know if a template static data member is initialized with
4133 a constant expression until we instantiate its initializer. Even
4134 in the case of a constexpr variable, we can't treat it as a
4135 constant until its initializer is complete in case it's used in
4136 its own initializer. */
4137 maybe_instantiate_decl (decl);
4138 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4141 /* Returns true if DECL could be a symbolic constant variable, depending on
4142 its initializer. */
4144 bool
4145 decl_maybe_constant_var_p (tree decl)
4147 tree type = TREE_TYPE (decl);
4148 if (!VAR_P (decl))
4149 return false;
4150 if (DECL_DECLARED_CONSTEXPR_P (decl))
4151 return true;
4152 if (DECL_HAS_VALUE_EXPR_P (decl))
4153 /* A proxy isn't constant. */
4154 return false;
4155 if (TREE_CODE (type) == REFERENCE_TYPE)
4156 /* References can be constant. */
4157 return true;
4158 return (CP_TYPE_CONST_NON_VOLATILE_P (type)
4159 && INTEGRAL_OR_ENUMERATION_TYPE_P (type));
4162 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4163 called from grokfndecl and grokvardecl; in all modes it is called from
4164 cp_write_global_declarations. */
4166 void
4167 no_linkage_error (tree decl)
4169 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4170 /* In C++11 it's ok if the decl is defined. */
4171 return;
4172 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4173 if (t == NULL_TREE)
4174 /* The type that got us on no_linkage_decls must have gotten a name for
4175 linkage purposes. */;
4176 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4177 /* The type might end up having a typedef name for linkage purposes. */
4178 vec_safe_push (no_linkage_decls, decl);
4179 else if (TYPE_UNNAMED_P (t))
4181 bool d = false;
4182 if (cxx_dialect >= cxx11)
4183 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4184 "unnamed type, is used but never defined", decl);
4185 else if (DECL_EXTERN_C_P (decl))
4186 /* Allow this; it's pretty common in C. */;
4187 else if (VAR_P (decl))
4188 /* DRs 132, 319 and 389 seem to indicate types with
4189 no linkage can only be used to declare extern "C"
4190 entities. Since it's not always an error in the
4191 ISO C++ 90 Standard, we only issue a warning. */
4192 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "unnamed type "
4193 "with no linkage used to declare variable %q#D with "
4194 "linkage", decl);
4195 else
4196 d = permerror (DECL_SOURCE_LOCATION (decl), "unnamed type with no "
4197 "linkage used to declare function %q#D with linkage",
4198 decl);
4199 if (d && is_typedef_decl (TYPE_NAME (t)))
4200 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4201 "to the unqualified type, so it is not used for linkage",
4202 TYPE_NAME (t));
4204 else if (cxx_dialect >= cxx11)
4206 if (VAR_P (decl) || !DECL_PURE_VIRTUAL_P (decl))
4207 permerror (DECL_SOURCE_LOCATION (decl),
4208 "%q#D, declared using local type "
4209 "%qT, is used but never defined", decl, t);
4211 else if (VAR_P (decl))
4212 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4213 "used to declare variable %q#D with linkage", t, decl);
4214 else
4215 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4216 "to declare function %q#D with linkage", t, decl);
4219 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4221 static void
4222 collect_all_refs (const char *source_file)
4224 collect_ada_namespace (global_namespace, source_file);
4227 /* Clear DECL_EXTERNAL for NODE. */
4229 static bool
4230 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4232 DECL_EXTERNAL (node->decl) = 0;
4233 return false;
4236 /* Build up the function to run dynamic initializers for thread_local
4237 variables in this translation unit and alias the init functions for the
4238 individual variables to it. */
4240 static void
4241 handle_tls_init (void)
4243 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4244 if (vars == NULL_TREE)
4245 return;
4247 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4249 write_out_vars (vars);
4251 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4252 boolean_type_node);
4253 TREE_PUBLIC (guard) = false;
4254 TREE_STATIC (guard) = true;
4255 DECL_ARTIFICIAL (guard) = true;
4256 DECL_IGNORED_P (guard) = true;
4257 TREE_USED (guard) = true;
4258 CP_DECL_THREAD_LOCAL_P (guard) = true;
4259 set_decl_tls_model (guard, decl_default_tls_model (guard));
4260 pushdecl_top_level_and_finish (guard, NULL_TREE);
4262 tree fn = get_local_tls_init_fn ();
4263 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4264 tree body = begin_function_body ();
4265 tree if_stmt = begin_if_stmt ();
4266 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4267 tf_warning_or_error);
4268 finish_if_stmt_cond (cond, if_stmt);
4269 finish_expr_stmt (cp_build_modify_expr (loc, guard, NOP_EXPR,
4270 boolean_true_node,
4271 tf_warning_or_error));
4272 for (; vars; vars = TREE_CHAIN (vars))
4274 tree var = TREE_VALUE (vars);
4275 tree init = TREE_PURPOSE (vars);
4276 one_static_initialization_or_destruction (var, init, true);
4278 #ifdef ASM_OUTPUT_DEF
4279 /* Output init aliases even with -fno-extern-tls-init. */
4280 if (TREE_PUBLIC (var))
4282 tree single_init_fn = get_tls_init_fn (var);
4283 if (single_init_fn == NULL_TREE)
4284 continue;
4285 cgraph_node *alias
4286 = cgraph_node::get_create (fn)->create_same_body_alias
4287 (single_init_fn, fn);
4288 gcc_assert (alias != NULL);
4290 #endif
4293 finish_then_clause (if_stmt);
4294 finish_if_stmt (if_stmt);
4295 finish_function_body (body);
4296 expand_or_defer_fn (finish_function (0));
4299 /* We're at the end of compilation, so generate any mangling aliases that
4300 we've been saving up, if DECL is going to be output and ID2 isn't
4301 already taken by another declaration. */
4303 static void
4304 generate_mangling_alias (tree decl, tree id2)
4306 /* If there's a declaration already using this mangled name,
4307 don't create a compatibility alias that conflicts. */
4308 if (IDENTIFIER_GLOBAL_VALUE (id2))
4309 return;
4311 struct cgraph_node *n = NULL;
4312 if (TREE_CODE (decl) == FUNCTION_DECL
4313 && !(n = cgraph_node::get (decl)))
4314 /* Don't create an alias to an unreferenced function. */
4315 return;
4317 tree alias = make_alias_for (decl, id2);
4318 SET_IDENTIFIER_GLOBAL_VALUE (id2, alias);
4319 DECL_IGNORED_P (alias) = 1;
4320 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
4321 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
4322 if (vague_linkage_p (decl))
4323 DECL_WEAK (alias) = 1;
4324 if (TREE_CODE (decl) == FUNCTION_DECL)
4325 n->create_same_body_alias (alias, decl);
4326 else
4327 varpool_node::create_extra_name_alias (alias, decl);
4330 /* Note that we might want to emit an alias with the symbol ID2 for DECL at
4331 the end of translation, for compatibility across bugs in the mangling
4332 implementation. */
4334 void
4335 note_mangling_alias (tree decl ATTRIBUTE_UNUSED, tree id2 ATTRIBUTE_UNUSED)
4337 #ifdef ASM_OUTPUT_DEF
4338 if (!defer_mangling_aliases)
4339 generate_mangling_alias (decl, id2);
4340 else
4342 vec_safe_push (mangling_aliases, decl);
4343 vec_safe_push (mangling_aliases, id2);
4345 #endif
4348 /* Emit all mangling aliases that were deferred up to this point. */
4350 void
4351 generate_mangling_aliases ()
4353 while (!vec_safe_is_empty (mangling_aliases))
4355 tree id2 = mangling_aliases->pop();
4356 tree decl = mangling_aliases->pop();
4357 generate_mangling_alias (decl, id2);
4359 defer_mangling_aliases = false;
4362 /* The entire file is now complete. If requested, dump everything
4363 to a file. */
4365 static void
4366 dump_tu (void)
4368 int flags;
4369 FILE *stream = dump_begin (TDI_tu, &flags);
4371 if (stream)
4373 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4374 dump_end (TDI_tu, stream);
4378 static location_t locus_at_end_of_parsing;
4380 /* Check the deallocation functions for CODE to see if we want to warn that
4381 only one was defined. */
4383 static void
4384 maybe_warn_sized_delete (enum tree_code code)
4386 tree sized = NULL_TREE;
4387 tree unsized = NULL_TREE;
4389 for (tree ovl = IDENTIFIER_GLOBAL_VALUE (cp_operator_id (code));
4390 ovl; ovl = OVL_NEXT (ovl))
4392 tree fn = OVL_CURRENT (ovl);
4393 /* We're only interested in usual deallocation functions. */
4394 if (!usual_deallocation_fn_p (fn))
4395 continue;
4396 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
4397 unsized = fn;
4398 else
4399 sized = fn;
4401 if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
4402 warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
4403 "the program should also define %qD", sized);
4404 else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
4405 warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
4406 "the program should also define %qD", unsized);
4409 /* Check the global deallocation functions to see if we want to warn about
4410 defining unsized without sized (or vice versa). */
4412 static void
4413 maybe_warn_sized_delete ()
4415 if (!flag_sized_deallocation || !warn_sized_deallocation)
4416 return;
4417 maybe_warn_sized_delete (DELETE_EXPR);
4418 maybe_warn_sized_delete (VEC_DELETE_EXPR);
4421 /* Earlier we left PTRMEM_CST in variable initializers alone so that we could
4422 look them up when evaluating non-type template parameters. Now we need to
4423 lower them to something the back end can understand. */
4425 static void
4426 lower_var_init ()
4428 varpool_node *node;
4429 FOR_EACH_VARIABLE (node)
4431 tree d = node->decl;
4432 if (tree init = DECL_INITIAL (d))
4433 DECL_INITIAL (d) = cplus_expand_constant (init);
4437 /* This routine is called at the end of compilation.
4438 Its job is to create all the code needed to initialize and
4439 destroy the global aggregates. We do the destruction
4440 first, since that way we only need to reverse the decls once. */
4442 void
4443 c_parse_final_cleanups (void)
4445 tree vars;
4446 bool reconsider;
4447 size_t i;
4448 unsigned ssdf_count = 0;
4449 int retries = 0;
4450 tree decl;
4452 locus_at_end_of_parsing = input_location;
4453 at_eof = 1;
4455 /* Bad parse errors. Just forget about it. */
4456 if (! global_bindings_p () || current_class_type
4457 || !vec_safe_is_empty (decl_namespace_list))
4458 return;
4460 /* This is the point to write out a PCH if we're doing that.
4461 In that case we do not want to do anything else. */
4462 if (pch_file)
4464 /* Mangle all symbols at PCH creation time. */
4465 symtab_node *node;
4466 FOR_EACH_SYMBOL (node)
4467 if (! is_a <varpool_node *> (node)
4468 || ! DECL_HARD_REGISTER (node->decl))
4469 DECL_ASSEMBLER_NAME (node->decl);
4470 c_common_write_pch ();
4471 dump_tu ();
4472 /* Ensure even the callers don't try to finalize the CU. */
4473 flag_syntax_only = 1;
4474 return;
4477 timevar_stop (TV_PHASE_PARSING);
4478 timevar_start (TV_PHASE_DEFERRED);
4480 symtab->process_same_body_aliases ();
4482 /* Handle -fdump-ada-spec[-slim] */
4483 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4485 if (flag_dump_ada_spec_slim)
4486 collect_source_ref (main_input_filename);
4487 else
4488 collect_source_refs (global_namespace);
4490 dump_ada_specs (collect_all_refs, cpp_check);
4493 /* FIXME - huh? was input_line -= 1;*/
4495 /* We now have to write out all the stuff we put off writing out.
4496 These include:
4498 o Template specializations that we have not yet instantiated,
4499 but which are needed.
4500 o Initialization and destruction for non-local objects with
4501 static storage duration. (Local objects with static storage
4502 duration are initialized when their scope is first entered,
4503 and are cleaned up via atexit.)
4504 o Virtual function tables.
4506 All of these may cause others to be needed. For example,
4507 instantiating one function may cause another to be needed, and
4508 generating the initializer for an object may cause templates to be
4509 instantiated, etc., etc. */
4511 emit_support_tinfos ();
4515 tree t;
4516 tree decl;
4518 reconsider = false;
4520 /* If there are templates that we've put off instantiating, do
4521 them now. */
4522 instantiate_pending_templates (retries);
4523 ggc_collect ();
4525 /* Write out virtual tables as required. Note that writing out
4526 the virtual table for a template class may cause the
4527 instantiation of members of that class. If we write out
4528 vtables then we remove the class from our list so we don't
4529 have to look at it again. */
4531 while (keyed_classes != NULL_TREE
4532 && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
4534 reconsider = true;
4535 keyed_classes = TREE_CHAIN (keyed_classes);
4538 t = keyed_classes;
4539 if (t != NULL_TREE)
4541 tree next = TREE_CHAIN (t);
4543 while (next)
4545 if (maybe_emit_vtables (TREE_VALUE (next)))
4547 reconsider = true;
4548 TREE_CHAIN (t) = TREE_CHAIN (next);
4550 else
4551 t = next;
4553 next = TREE_CHAIN (t);
4557 /* Write out needed type info variables. We have to be careful
4558 looping through unemitted decls, because emit_tinfo_decl may
4559 cause other variables to be needed. New elements will be
4560 appended, and we remove from the vector those that actually
4561 get emitted. */
4562 for (i = unemitted_tinfo_decls->length ();
4563 unemitted_tinfo_decls->iterate (--i, &t);)
4564 if (emit_tinfo_decl (t))
4566 reconsider = true;
4567 unemitted_tinfo_decls->unordered_remove (i);
4570 /* The list of objects with static storage duration is built up
4571 in reverse order. We clear STATIC_AGGREGATES so that any new
4572 aggregates added during the initialization of these will be
4573 initialized in the correct order when we next come around the
4574 loop. */
4575 vars = prune_vars_needing_no_initialization (&static_aggregates);
4577 if (vars)
4579 /* We need to start a new initialization function each time
4580 through the loop. That's because we need to know which
4581 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4582 isn't computed until a function is finished, and written
4583 out. That's a deficiency in the back end. When this is
4584 fixed, these initialization functions could all become
4585 inline, with resulting performance improvements. */
4586 tree ssdf_body;
4588 /* Set the line and file, so that it is obviously not from
4589 the source file. */
4590 input_location = locus_at_end_of_parsing;
4591 ssdf_body = start_static_storage_duration_function (ssdf_count);
4593 /* Make sure the back end knows about all the variables. */
4594 write_out_vars (vars);
4596 /* First generate code to do all the initializations. */
4597 if (vars)
4598 do_static_initialization_or_destruction (vars, /*initp=*/true);
4600 /* Then, generate code to do all the destructions. Do these
4601 in reverse order so that the most recently constructed
4602 variable is the first destroyed. If we're using
4603 __cxa_atexit, then we don't need to do this; functions
4604 were registered at initialization time to destroy the
4605 local statics. */
4606 if (!flag_use_cxa_atexit && vars)
4608 vars = nreverse (vars);
4609 do_static_initialization_or_destruction (vars, /*initp=*/false);
4611 else
4612 vars = NULL_TREE;
4614 /* Finish up the static storage duration function for this
4615 round. */
4616 input_location = locus_at_end_of_parsing;
4617 finish_static_storage_duration_function (ssdf_body);
4619 /* All those initializations and finalizations might cause
4620 us to need more inline functions, more template
4621 instantiations, etc. */
4622 reconsider = true;
4623 ssdf_count++;
4624 /* ??? was: locus_at_end_of_parsing.line++; */
4627 /* Now do the same for thread_local variables. */
4628 handle_tls_init ();
4630 /* Go through the set of inline functions whose bodies have not
4631 been emitted yet. If out-of-line copies of these functions
4632 are required, emit them. */
4633 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4635 /* Does it need synthesizing? */
4636 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4637 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4639 /* Even though we're already at the top-level, we push
4640 there again. That way, when we pop back a few lines
4641 hence, all of our state is restored. Otherwise,
4642 finish_function doesn't clean things up, and we end
4643 up with CURRENT_FUNCTION_DECL set. */
4644 push_to_top_level ();
4645 /* The decl's location will mark where it was first
4646 needed. Save that so synthesize method can indicate
4647 where it was needed from, in case of error */
4648 input_location = DECL_SOURCE_LOCATION (decl);
4649 synthesize_method (decl);
4650 pop_from_top_level ();
4651 reconsider = true;
4654 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4655 generate_tls_wrapper (decl);
4657 if (!DECL_SAVED_TREE (decl))
4658 continue;
4660 /* We lie to the back end, pretending that some functions
4661 are not defined when they really are. This keeps these
4662 functions from being put out unnecessarily. But, we must
4663 stop lying when the functions are referenced, or if they
4664 are not comdat since they need to be put out now. If
4665 DECL_INTERFACE_KNOWN, then we have already set
4666 DECL_EXTERNAL appropriately, so there's no need to check
4667 again, and we do not want to clear DECL_EXTERNAL if a
4668 previous call to import_export_decl set it.
4670 This is done in a separate for cycle, because if some
4671 deferred function is contained in another deferred
4672 function later in deferred_fns varray,
4673 rest_of_compilation would skip this function and we
4674 really cannot expand the same function twice. */
4675 import_export_decl (decl);
4676 if (DECL_NOT_REALLY_EXTERN (decl)
4677 && DECL_INITIAL (decl)
4678 && decl_needed_p (decl))
4680 struct cgraph_node *node, *next;
4682 node = cgraph_node::get (decl);
4683 if (node->cpp_implicit_alias)
4684 node = node->get_alias_target ();
4686 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
4687 NULL, true);
4688 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4689 group, we need to mark all symbols in the same comdat group
4690 that way. */
4691 if (node->same_comdat_group)
4692 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
4693 next != node;
4694 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
4695 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
4696 NULL, true);
4699 /* If we're going to need to write this function out, and
4700 there's already a body for it, create RTL for it now.
4701 (There might be no body if this is a method we haven't
4702 gotten around to synthesizing yet.) */
4703 if (!DECL_EXTERNAL (decl)
4704 && decl_needed_p (decl)
4705 && !TREE_ASM_WRITTEN (decl)
4706 && !cgraph_node::get (decl)->definition)
4708 /* We will output the function; no longer consider it in this
4709 loop. */
4710 DECL_DEFER_OUTPUT (decl) = 0;
4711 /* Generate RTL for this function now that we know we
4712 need it. */
4713 expand_or_defer_fn (decl);
4714 /* If we're compiling -fsyntax-only pretend that this
4715 function has been written out so that we don't try to
4716 expand it again. */
4717 if (flag_syntax_only)
4718 TREE_ASM_WRITTEN (decl) = 1;
4719 reconsider = true;
4723 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
4724 reconsider = true;
4726 /* Static data members are just like namespace-scope globals. */
4727 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4729 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4730 /* Don't write it out if we haven't seen a definition. */
4731 || (DECL_IN_AGGR_P (decl) && !DECL_INLINE_VAR_P (decl)))
4732 continue;
4733 import_export_decl (decl);
4734 /* If this static data member is needed, provide it to the
4735 back end. */
4736 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4737 DECL_EXTERNAL (decl) = 0;
4739 if (vec_safe_length (pending_statics) != 0
4740 && wrapup_global_declarations (pending_statics->address (),
4741 pending_statics->length ()))
4742 reconsider = true;
4744 retries++;
4746 while (reconsider);
4748 walk_namespaces (diagnose_inline_vars_for_namespace, /*data=*/0);
4750 lower_var_init ();
4752 generate_mangling_aliases ();
4754 /* All used inline functions must have a definition at this point. */
4755 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4757 if (/* Check online inline functions that were actually used. */
4758 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4759 /* If the definition actually was available here, then the
4760 fact that the function was not defined merely represents
4761 that for some reason (use of a template repository,
4762 #pragma interface, etc.) we decided not to emit the
4763 definition here. */
4764 && !DECL_INITIAL (decl)
4765 /* Don't complain if the template was defined. */
4766 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4767 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4768 (template_for_substitution (decl)))))
4770 warning_at (DECL_SOURCE_LOCATION (decl), 0,
4771 "inline function %qD used but never defined", decl);
4772 /* Avoid a duplicate warning from check_global_declaration. */
4773 TREE_NO_WARNING (decl) = 1;
4777 /* So must decls that use a type with no linkage. */
4778 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4779 no_linkage_error (decl);
4781 maybe_warn_sized_delete ();
4783 /* Then, do the Objective-C stuff. This is where all the
4784 Objective-C module stuff gets generated (symtab,
4785 class/protocol/selector lists etc). This must be done after C++
4786 templates, destructors etc. so that selectors used in C++
4787 templates are properly allocated. */
4788 if (c_dialect_objc ())
4789 objc_write_global_declarations ();
4791 /* We give C linkage to static constructors and destructors. */
4792 push_lang_context (lang_name_c);
4794 /* Generate initialization and destruction functions for all
4795 priorities for which they are required. */
4796 if (priority_info_map)
4797 splay_tree_foreach (priority_info_map,
4798 generate_ctor_and_dtor_functions_for_priority,
4799 /*data=*/&locus_at_end_of_parsing);
4800 else if (c_dialect_objc () && objc_static_init_needed_p ())
4801 /* If this is obj-c++ and we need a static init, call
4802 generate_ctor_or_dtor_function. */
4803 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4804 DEFAULT_INIT_PRIORITY,
4805 &locus_at_end_of_parsing);
4807 /* We're done with the splay-tree now. */
4808 if (priority_info_map)
4809 splay_tree_delete (priority_info_map);
4811 /* Generate any missing aliases. */
4812 maybe_apply_pending_pragma_weaks ();
4814 /* We're done with static constructors, so we can go back to "C++"
4815 linkage now. */
4816 pop_lang_context ();
4818 if (flag_vtable_verify)
4820 vtv_recover_class_info ();
4821 vtv_compute_class_hierarchy_transitive_closure ();
4822 vtv_build_vtable_verify_fndecl ();
4825 perform_deferred_noexcept_checks ();
4827 finish_repo ();
4829 fini_constexpr ();
4831 /* The entire file is now complete. If requested, dump everything
4832 to a file. */
4833 dump_tu ();
4835 if (flag_detailed_statistics)
4837 dump_tree_statistics ();
4838 dump_time_statistics ();
4841 timevar_stop (TV_PHASE_DEFERRED);
4842 timevar_start (TV_PHASE_PARSING);
4844 /* Indicate that we're done with front end processing. */
4845 at_eof = 2;
4848 /* Perform any post compilation-proper cleanups for the C++ front-end.
4849 This should really go away. No front-end should need to do
4850 anything past the compilation process. */
4852 void
4853 cxx_post_compilation_parsing_cleanups (void)
4855 timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS);
4857 if (flag_vtable_verify)
4859 /* Generate the special constructor initialization function that
4860 calls __VLTRegisterPairs, and give it a very high
4861 initialization priority. This must be done after
4862 finalize_compilation_unit so that we have accurate
4863 information about which vtable will actually be emitted. */
4864 vtv_generate_init_routine ();
4867 input_location = locus_at_end_of_parsing;
4869 if (flag_checking)
4870 validate_conversion_obstack ();
4872 timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS);
4875 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4876 function to call in parse-tree form; it has not yet been
4877 semantically analyzed. ARGS are the arguments to the function.
4878 They have already been semantically analyzed. This may change
4879 ARGS. */
4881 tree
4882 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4883 tsubst_flags_t complain)
4885 tree orig_fn;
4886 vec<tree, va_gc> *orig_args = NULL;
4887 tree expr;
4888 tree object;
4890 orig_fn = fn;
4891 object = TREE_OPERAND (fn, 0);
4893 if (processing_template_decl)
4895 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4896 || TREE_CODE (fn) == MEMBER_REF);
4897 if (type_dependent_expression_p (fn)
4898 || any_type_dependent_arguments_p (*args))
4899 return build_nt_call_vec (fn, *args);
4901 orig_args = make_tree_vector_copy (*args);
4903 /* Transform the arguments and add the implicit "this"
4904 parameter. That must be done before the FN is transformed
4905 because we depend on the form of FN. */
4906 make_args_non_dependent (*args);
4907 object = build_non_dependent_expr (object);
4908 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4910 if (TREE_CODE (fn) == DOTSTAR_EXPR)
4911 object = cp_build_addr_expr (object, complain);
4912 vec_safe_insert (*args, 0, object);
4914 /* Now that the arguments are done, transform FN. */
4915 fn = build_non_dependent_expr (fn);
4918 /* A qualified name corresponding to a bound pointer-to-member is
4919 represented as an OFFSET_REF:
4921 struct B { void g(); };
4922 void (B::*p)();
4923 void B::g() { (this->*p)(); } */
4924 if (TREE_CODE (fn) == OFFSET_REF)
4926 tree object_addr = cp_build_addr_expr (object, complain);
4927 fn = TREE_OPERAND (fn, 1);
4928 fn = get_member_function_from_ptrfunc (&object_addr, fn,
4929 complain);
4930 vec_safe_insert (*args, 0, object_addr);
4933 if (CLASS_TYPE_P (TREE_TYPE (fn)))
4934 expr = build_op_call (fn, args, complain);
4935 else
4936 expr = cp_build_function_call_vec (fn, args, complain);
4937 if (processing_template_decl && expr != error_mark_node)
4938 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
4940 if (orig_args != NULL)
4941 release_tree_vector (orig_args);
4943 return expr;
4947 void
4948 check_default_args (tree x)
4950 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4951 bool saw_def = false;
4952 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4953 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4955 if (TREE_PURPOSE (arg))
4956 saw_def = true;
4957 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
4959 error ("default argument missing for parameter %P of %q+#D", i, x);
4960 TREE_PURPOSE (arg) = error_mark_node;
4965 /* Return true if function DECL can be inlined. This is used to force
4966 instantiation of methods that might be interesting for inlining. */
4967 bool
4968 possibly_inlined_p (tree decl)
4970 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4971 if (DECL_UNINLINABLE (decl))
4972 return false;
4973 if (!optimize)
4974 return DECL_DECLARED_INLINE_P (decl);
4975 /* When optimizing, we might inline everything when flatten
4976 attribute or heuristics inlining for size or autoinlining
4977 is used. */
4978 return true;
4981 /* Normally, we can wait until instantiation-time to synthesize DECL.
4982 However, if DECL is a static data member initialized with a constant
4983 or a constexpr function, we need it right now because a reference to
4984 such a data member or a call to such function is not value-dependent.
4985 For a function that uses auto in the return type, we need to instantiate
4986 it to find out its type. For OpenMP user defined reductions, we need
4987 them instantiated for reduction clauses which inline them by hand
4988 directly. */
4990 static void
4991 maybe_instantiate_decl (tree decl)
4993 if (DECL_LANG_SPECIFIC (decl)
4994 && DECL_TEMPLATE_INFO (decl)
4995 && (decl_maybe_constant_var_p (decl)
4996 || (TREE_CODE (decl) == FUNCTION_DECL
4997 && DECL_OMP_DECLARE_REDUCTION_P (decl))
4998 || undeduced_auto_decl (decl))
4999 && !DECL_DECLARED_CONCEPT_P (decl)
5000 && !uses_template_parms (DECL_TI_ARGS (decl)))
5002 /* Instantiating a function will result in garbage collection. We
5003 must treat this situation as if we were within the body of a
5004 function so as to avoid collecting live data only referenced from
5005 the stack (such as overload resolution candidates). */
5006 ++function_depth;
5007 instantiate_decl (decl, /*defer_ok=*/false,
5008 /*expl_inst_class_mem_p=*/false);
5009 --function_depth;
5013 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
5014 If DECL is a specialization or implicitly declared class member,
5015 generate the actual definition. Return false if something goes
5016 wrong, true otherwise. */
5018 bool
5019 mark_used (tree decl, tsubst_flags_t complain)
5021 /* If DECL is a BASELINK for a single function, then treat it just
5022 like the DECL for the function. Otherwise, if the BASELINK is
5023 for an overloaded function, we don't know which function was
5024 actually used until after overload resolution. */
5025 if (BASELINK_P (decl))
5027 decl = BASELINK_FUNCTIONS (decl);
5028 if (really_overloaded_fn (decl))
5029 return true;
5030 decl = OVL_CURRENT (decl);
5033 /* Set TREE_USED for the benefit of -Wunused. */
5034 TREE_USED (decl) = 1;
5036 if (TREE_CODE (decl) == TEMPLATE_DECL)
5037 return true;
5039 if (DECL_CLONED_FUNCTION_P (decl))
5040 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5042 /* Mark enumeration types as used. */
5043 if (TREE_CODE (decl) == CONST_DECL)
5044 used_types_insert (DECL_CONTEXT (decl));
5046 if (TREE_CODE (decl) == FUNCTION_DECL)
5047 maybe_instantiate_noexcept (decl);
5049 if (TREE_CODE (decl) == FUNCTION_DECL
5050 && DECL_DELETED_FN (decl))
5052 if (DECL_ARTIFICIAL (decl))
5054 if (DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
5055 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
5057 /* We mark a lambda conversion op as deleted if we can't
5058 generate it properly; see maybe_add_lambda_conv_op. */
5059 sorry ("converting lambda which uses %<...%> to "
5060 "function pointer");
5061 return false;
5064 if (complain & tf_error)
5066 error ("use of deleted function %qD", decl);
5067 if (!maybe_explain_implicit_delete (decl))
5068 inform (DECL_SOURCE_LOCATION (decl), "declared here");
5070 return false;
5073 if (TREE_DEPRECATED (decl) && (complain & tf_warning)
5074 && deprecated_state != DEPRECATED_SUPPRESS)
5075 warn_deprecated_use (decl, NULL_TREE);
5077 /* We can only check DECL_ODR_USED on variables or functions with
5078 DECL_LANG_SPECIFIC set, and these are also the only decls that we
5079 might need special handling for. */
5080 if (!VAR_OR_FUNCTION_DECL_P (decl)
5081 || DECL_LANG_SPECIFIC (decl) == NULL
5082 || DECL_THUNK_P (decl))
5084 if (!processing_template_decl && type_uses_auto (TREE_TYPE (decl)))
5086 if (complain & tf_error)
5087 error ("use of %qD before deduction of %<auto%>", decl);
5088 return false;
5090 return true;
5093 /* We only want to do this processing once. We don't need to keep trying
5094 to instantiate inline templates, because unit-at-a-time will make sure
5095 we get them compiled before functions that want to inline them. */
5096 if (DECL_ODR_USED (decl))
5097 return true;
5099 /* Normally, we can wait until instantiation-time to synthesize DECL.
5100 However, if DECL is a static data member initialized with a constant
5101 or a constexpr function, we need it right now because a reference to
5102 such a data member or a call to such function is not value-dependent.
5103 For a function that uses auto in the return type, we need to instantiate
5104 it to find out its type. For OpenMP user defined reductions, we need
5105 them instantiated for reduction clauses which inline them by hand
5106 directly. */
5107 maybe_instantiate_decl (decl);
5109 if (processing_template_decl || in_template_function ())
5110 return true;
5112 /* Check this too in case we're within instantiate_non_dependent_expr. */
5113 if (DECL_TEMPLATE_INFO (decl)
5114 && uses_template_parms (DECL_TI_ARGS (decl)))
5115 return true;
5117 if (undeduced_auto_decl (decl))
5119 if (complain & tf_error)
5120 error ("use of %qD before deduction of %<auto%>", decl);
5121 return false;
5124 /* If we don't need a value, then we don't need to synthesize DECL. */
5125 if (cp_unevaluated_operand || in_discarded_stmt)
5126 return true;
5128 DECL_ODR_USED (decl) = 1;
5129 if (DECL_CLONED_FUNCTION_P (decl))
5130 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5132 /* DR 757: A type without linkage shall not be used as the type of a
5133 variable or function with linkage, unless
5134 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5135 o the variable or function is not used (3.2 [basic.def.odr]) or is
5136 defined in the same translation unit. */
5137 if (cxx_dialect > cxx98
5138 && decl_linkage (decl) != lk_none
5139 && !DECL_EXTERN_C_P (decl)
5140 && !DECL_ARTIFICIAL (decl)
5141 && !decl_defined_p (decl)
5142 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
5144 if (is_local_extern (decl))
5145 /* There's no way to define a local extern, and adding it to
5146 the vector interferes with GC, so give an error now. */
5147 no_linkage_error (decl);
5148 else
5149 vec_safe_push (no_linkage_decls, decl);
5152 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
5153 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
5154 /* Remember it, so we can check it was defined. */
5155 note_vague_linkage_fn (decl);
5157 /* Is it a synthesized method that needs to be synthesized? */
5158 if (TREE_CODE (decl) == FUNCTION_DECL
5159 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5160 && DECL_DEFAULTED_FN (decl)
5161 /* A function defaulted outside the class is synthesized either by
5162 cp_finish_decl or instantiate_decl. */
5163 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
5164 && ! DECL_INITIAL (decl))
5166 /* Defer virtual destructors so that thunks get the right
5167 linkage. */
5168 if (DECL_VIRTUAL_P (decl) && !at_eof)
5170 note_vague_linkage_fn (decl);
5171 return true;
5174 /* Remember the current location for a function we will end up
5175 synthesizing. Then we can inform the user where it was
5176 required in the case of error. */
5177 DECL_SOURCE_LOCATION (decl) = input_location;
5179 /* Synthesizing an implicitly defined member function will result in
5180 garbage collection. We must treat this situation as if we were
5181 within the body of a function so as to avoid collecting live data
5182 on the stack (such as overload resolution candidates).
5184 We could just let cp_write_global_declarations handle synthesizing
5185 this function by adding it to deferred_fns, but doing
5186 it at the use site produces better error messages. */
5187 ++function_depth;
5188 synthesize_method (decl);
5189 --function_depth;
5190 /* If this is a synthesized method we don't need to
5191 do the instantiation test below. */
5193 else if (VAR_OR_FUNCTION_DECL_P (decl)
5194 && DECL_TEMPLATE_INFO (decl)
5195 && !DECL_DECLARED_CONCEPT_P (decl)
5196 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5197 || always_instantiate_p (decl)))
5198 /* If this is a function or variable that is an instance of some
5199 template, we now know that we will need to actually do the
5200 instantiation. We check that DECL is not an explicit
5201 instantiation because that is not checked in instantiate_decl.
5203 We put off instantiating functions in order to improve compile
5204 times. Maintaining a stack of active functions is expensive,
5205 and the inliner knows to instantiate any functions it might
5206 need. Therefore, we always try to defer instantiation. */
5208 ++function_depth;
5209 instantiate_decl (decl, /*defer_ok=*/true,
5210 /*expl_inst_class_mem_p=*/false);
5211 --function_depth;
5214 return true;
5217 bool
5218 mark_used (tree decl)
5220 return mark_used (decl, tf_warning_or_error);
5223 tree
5224 vtv_start_verification_constructor_init_function (void)
5226 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5229 tree
5230 vtv_finish_verification_constructor_init_function (tree function_body)
5232 tree fn;
5234 finish_compound_stmt (function_body);
5235 fn = finish_function (0);
5236 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5237 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5239 return fn;
5242 #include "gt-cp-decl2.h"