2017-03-06 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / cp / decl2.c
blobb50fadf050db0e34616c420f6a715f8c0fc1e20f
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 else
1064 DECL_INITIAL (value) = width;
1065 SET_DECL_C_BIT_FIELD (value);
1069 DECL_IN_AGGR_P (value) = 1;
1071 if (attrlist)
1072 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1074 return value;
1078 /* Returns true iff ATTR is an attribute which needs to be applied at
1079 instantiation time rather than template definition time. */
1081 static bool
1082 is_late_template_attribute (tree attr, tree decl)
1084 tree name = get_attribute_name (attr);
1085 tree args = TREE_VALUE (attr);
1086 const struct attribute_spec *spec = lookup_attribute_spec (name);
1087 tree arg;
1089 if (!spec)
1090 /* Unknown attribute. */
1091 return false;
1093 /* Attribute weak handling wants to write out assembly right away. */
1094 if (is_attribute_p ("weak", name))
1095 return true;
1097 /* Attribute unused is applied directly, as it appertains to
1098 decls. */
1099 if (is_attribute_p ("unused", name))
1100 return false;
1102 /* Attribute tls_model wants to modify the symtab. */
1103 if (is_attribute_p ("tls_model", name))
1104 return true;
1106 /* #pragma omp declare simd attribute needs to be always deferred. */
1107 if (flag_openmp
1108 && is_attribute_p ("omp declare simd", name))
1109 return true;
1111 /* An attribute pack is clearly dependent. */
1112 if (args && PACK_EXPANSION_P (args))
1113 return true;
1115 /* If any of the arguments are dependent expressions, we can't evaluate
1116 the attribute until instantiation time. */
1117 for (arg = args; arg; arg = TREE_CHAIN (arg))
1119 tree t = TREE_VALUE (arg);
1121 /* If the first attribute argument is an identifier, only consider
1122 second and following arguments. Attributes like mode, format,
1123 cleanup and several target specific attributes aren't late
1124 just because they have an IDENTIFIER_NODE as first argument. */
1125 if (arg == args && attribute_takes_identifier_p (name)
1126 && identifier_p (t))
1127 continue;
1129 if (value_dependent_expression_p (t)
1130 || type_dependent_expression_p (t))
1131 return true;
1134 if (TREE_CODE (decl) == TYPE_DECL
1135 || TYPE_P (decl)
1136 || spec->type_required)
1138 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1140 /* We can't apply any attributes to a completely unknown type until
1141 instantiation time. */
1142 enum tree_code code = TREE_CODE (type);
1143 if (code == TEMPLATE_TYPE_PARM
1144 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1145 || code == TYPENAME_TYPE)
1146 return true;
1147 /* Also defer most attributes on dependent types. This is not
1148 necessary in all cases, but is the better default. */
1149 else if (dependent_type_p (type)
1150 /* But some attributes specifically apply to templates. */
1151 && !is_attribute_p ("abi_tag", name)
1152 && !is_attribute_p ("deprecated", name)
1153 && !is_attribute_p ("visibility", name))
1154 return true;
1155 else
1156 return false;
1158 else
1159 return false;
1162 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1163 applied at instantiation time and return them. If IS_DEPENDENT is true,
1164 the declaration itself is dependent, so all attributes should be applied
1165 at instantiation time. */
1167 static tree
1168 splice_template_attributes (tree *attr_p, tree decl)
1170 tree *p = attr_p;
1171 tree late_attrs = NULL_TREE;
1172 tree *q = &late_attrs;
1174 if (!p)
1175 return NULL_TREE;
1177 for (; *p; )
1179 if (is_late_template_attribute (*p, decl))
1181 ATTR_IS_DEPENDENT (*p) = 1;
1182 *q = *p;
1183 *p = TREE_CHAIN (*p);
1184 q = &TREE_CHAIN (*q);
1185 *q = NULL_TREE;
1187 else
1188 p = &TREE_CHAIN (*p);
1191 return late_attrs;
1194 /* Remove any late attributes from the list in ATTR_P and attach them to
1195 DECL_P. */
1197 static void
1198 save_template_attributes (tree *attr_p, tree *decl_p)
1200 tree *q;
1202 if (attr_p && *attr_p == error_mark_node)
1203 return;
1205 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1206 if (!late_attrs)
1207 return;
1209 if (DECL_P (*decl_p))
1210 q = &DECL_ATTRIBUTES (*decl_p);
1211 else
1212 q = &TYPE_ATTRIBUTES (*decl_p);
1214 tree old_attrs = *q;
1216 /* Merge the late attributes at the beginning with the attribute
1217 list. */
1218 late_attrs = merge_attributes (late_attrs, *q);
1219 *q = late_attrs;
1221 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1223 /* We've added new attributes directly to the main variant, so
1224 now we need to update all of the other variants to include
1225 these new attributes. */
1226 tree variant;
1227 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1228 variant = TYPE_NEXT_VARIANT (variant))
1230 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1231 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1236 /* True if ATTRS contains any dependent attributes that affect type
1237 identity. */
1239 bool
1240 any_dependent_type_attributes_p (tree attrs)
1242 for (tree a = attrs; a; a = TREE_CHAIN (a))
1243 if (ATTR_IS_DEPENDENT (a))
1245 const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (a));
1246 if (as && as->affects_type_identity)
1247 return true;
1249 return false;
1252 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1253 to a typedef which gives a previously unnamed class or enum a name for
1254 linkage purposes. */
1256 bool
1257 attributes_naming_typedef_ok (tree attrs)
1259 for (; attrs; attrs = TREE_CHAIN (attrs))
1261 tree name = get_attribute_name (attrs);
1262 if (is_attribute_p ("vector_size", name))
1263 return false;
1265 return true;
1268 /* Like reconstruct_complex_type, but handle also template trees. */
1270 tree
1271 cp_reconstruct_complex_type (tree type, tree bottom)
1273 tree inner, outer;
1274 bool late_return_type_p = false;
1276 if (TYPE_PTR_P (type))
1278 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1279 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1280 TYPE_REF_CAN_ALIAS_ALL (type));
1282 else if (TREE_CODE (type) == REFERENCE_TYPE)
1284 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1285 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1286 TYPE_REF_CAN_ALIAS_ALL (type));
1288 else if (TREE_CODE (type) == ARRAY_TYPE)
1290 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1291 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1292 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1293 element type qualification will be handled by the recursive
1294 cp_reconstruct_complex_type call and cp_build_qualified_type
1295 for ARRAY_TYPEs changes the element type. */
1296 return outer;
1298 else if (TREE_CODE (type) == FUNCTION_TYPE)
1300 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1301 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1302 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1303 outer = apply_memfn_quals (outer,
1304 type_memfn_quals (type),
1305 type_memfn_rqual (type));
1307 else if (TREE_CODE (type) == METHOD_TYPE)
1309 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1310 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1311 /* The build_method_type_directly() routine prepends 'this' to argument list,
1312 so we must compensate by getting rid of it. */
1313 outer
1314 = build_method_type_directly
1315 (class_of_this_parm (type), inner,
1316 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1318 else if (TREE_CODE (type) == OFFSET_TYPE)
1320 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1321 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1323 else
1324 return bottom;
1326 if (TYPE_ATTRIBUTES (type))
1327 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1328 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1330 if (late_return_type_p)
1331 TYPE_HAS_LATE_RETURN_TYPE (outer) = 1;
1333 return outer;
1336 /* Replaces any constexpr expression that may be into the attributes
1337 arguments with their reduced value. */
1339 static void
1340 cp_check_const_attributes (tree attributes)
1342 if (attributes == error_mark_node)
1343 return;
1345 tree attr;
1346 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1348 tree arg;
1349 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1351 tree expr = TREE_VALUE (arg);
1352 if (EXPR_P (expr))
1353 TREE_VALUE (arg) = maybe_constant_value (expr);
1358 /* Return true if TYPE is an OpenMP mappable type. */
1359 bool
1360 cp_omp_mappable_type (tree type)
1362 /* Mappable type has to be complete. */
1363 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1364 return false;
1365 /* Arrays have mappable type if the elements have mappable type. */
1366 while (TREE_CODE (type) == ARRAY_TYPE)
1367 type = TREE_TYPE (type);
1368 /* A mappable type cannot contain virtual members. */
1369 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1370 return false;
1371 /* All data members must be non-static. */
1372 if (CLASS_TYPE_P (type))
1374 tree field;
1375 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1376 if (VAR_P (field))
1377 return false;
1378 /* All fields must have mappable types. */
1379 else if (TREE_CODE (field) == FIELD_DECL
1380 && !cp_omp_mappable_type (TREE_TYPE (field)))
1381 return false;
1383 return true;
1386 /* Like decl_attributes, but handle C++ complexity. */
1388 void
1389 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1391 if (*decl == NULL_TREE || *decl == void_type_node
1392 || *decl == error_mark_node)
1393 return;
1395 /* Add implicit "omp declare target" attribute if requested. */
1396 if (scope_chain->omp_declare_target_attribute
1397 && ((VAR_P (*decl)
1398 && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
1399 || TREE_CODE (*decl) == FUNCTION_DECL))
1401 if (VAR_P (*decl)
1402 && DECL_CLASS_SCOPE_P (*decl))
1403 error ("%q+D static data member inside of declare target directive",
1404 *decl);
1405 else if (!processing_template_decl
1406 && VAR_P (*decl)
1407 && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1408 error ("%q+D in declare target directive does not have mappable type",
1409 *decl);
1410 else
1411 attributes = tree_cons (get_identifier ("omp declare target"),
1412 NULL_TREE, attributes);
1415 if (processing_template_decl)
1417 if (check_for_bare_parameter_packs (attributes))
1418 return;
1420 save_template_attributes (&attributes, decl);
1423 cp_check_const_attributes (attributes);
1425 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1426 decl = &DECL_TEMPLATE_RESULT (*decl);
1428 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1430 attributes
1431 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1432 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1433 attributes, flags);
1435 else
1436 decl_attributes (decl, attributes, flags);
1438 if (TREE_CODE (*decl) == TYPE_DECL)
1439 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1441 /* Propagate deprecation out to the template. */
1442 if (TREE_DEPRECATED (*decl))
1443 if (tree ti = get_template_info (*decl))
1445 tree tmpl = TI_TEMPLATE (ti);
1446 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1447 : DECL_TEMPLATE_RESULT (tmpl));
1448 if (*decl == pattern)
1449 TREE_DEPRECATED (tmpl) = true;
1453 /* Walks through the namespace- or function-scope anonymous union
1454 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1455 Returns one of the fields for use in the mangled name. */
1457 static tree
1458 build_anon_union_vars (tree type, tree object)
1460 tree main_decl = NULL_TREE;
1461 tree field;
1463 /* Rather than write the code to handle the non-union case,
1464 just give an error. */
1465 if (TREE_CODE (type) != UNION_TYPE)
1467 error ("anonymous struct not inside named type");
1468 return error_mark_node;
1471 for (field = TYPE_FIELDS (type);
1472 field != NULL_TREE;
1473 field = DECL_CHAIN (field))
1475 tree decl;
1476 tree ref;
1478 if (DECL_ARTIFICIAL (field))
1479 continue;
1480 if (TREE_CODE (field) != FIELD_DECL)
1482 permerror (DECL_SOURCE_LOCATION (field),
1483 "%q#D invalid; an anonymous union can only "
1484 "have non-static data members", field);
1485 continue;
1488 if (TREE_PRIVATE (field))
1489 permerror (DECL_SOURCE_LOCATION (field),
1490 "private member %q#D in anonymous union", field);
1491 else if (TREE_PROTECTED (field))
1492 permerror (DECL_SOURCE_LOCATION (field),
1493 "protected member %q#D in anonymous union", field);
1495 if (processing_template_decl)
1496 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1497 DECL_NAME (field), NULL_TREE);
1498 else
1499 ref = build_class_member_access_expr (object, field, NULL_TREE,
1500 false, tf_warning_or_error);
1502 if (DECL_NAME (field))
1504 tree base;
1506 decl = build_decl (input_location,
1507 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1508 DECL_ANON_UNION_VAR_P (decl) = 1;
1509 DECL_ARTIFICIAL (decl) = 1;
1511 base = get_base_address (object);
1512 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1513 TREE_STATIC (decl) = TREE_STATIC (base);
1514 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1516 SET_DECL_VALUE_EXPR (decl, ref);
1517 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1519 decl = pushdecl (decl);
1521 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1522 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1523 else
1524 decl = 0;
1526 if (main_decl == NULL_TREE)
1527 main_decl = decl;
1530 return main_decl;
1533 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1534 anonymous union, then all members must be laid out together. PUBLIC_P
1535 is nonzero if this union is not declared static. */
1537 void
1538 finish_anon_union (tree anon_union_decl)
1540 tree type;
1541 tree main_decl;
1542 bool public_p;
1544 if (anon_union_decl == error_mark_node)
1545 return;
1547 type = TREE_TYPE (anon_union_decl);
1548 public_p = TREE_PUBLIC (anon_union_decl);
1550 /* The VAR_DECL's context is the same as the TYPE's context. */
1551 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1553 if (TYPE_FIELDS (type) == NULL_TREE)
1554 return;
1556 if (public_p)
1558 error ("namespace-scope anonymous aggregates must be static");
1559 return;
1562 main_decl = build_anon_union_vars (type, anon_union_decl);
1563 if (main_decl == error_mark_node)
1564 return;
1565 if (main_decl == NULL_TREE)
1567 warning (0, "anonymous union with no members");
1568 return;
1571 if (!processing_template_decl)
1573 /* Use main_decl to set the mangled name. */
1574 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1575 maybe_commonize_var (anon_union_decl);
1576 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1577 mangle_decl (anon_union_decl);
1578 DECL_NAME (anon_union_decl) = NULL_TREE;
1581 pushdecl (anon_union_decl);
1582 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1585 /* Auxiliary functions to make type signatures for
1586 `operator new' and `operator delete' correspond to
1587 what compiler will be expecting. */
1589 tree
1590 coerce_new_type (tree type)
1592 int e = 0;
1593 tree args = TYPE_ARG_TYPES (type);
1595 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1597 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1599 e = 1;
1600 error ("%<operator new%> must return type %qT", ptr_type_node);
1603 if (args && args != void_list_node)
1605 if (TREE_PURPOSE (args))
1607 /* [basic.stc.dynamic.allocation]
1609 The first parameter shall not have an associated default
1610 argument. */
1611 error ("the first parameter of %<operator new%> cannot "
1612 "have a default argument");
1613 /* Throw away the default argument. */
1614 TREE_PURPOSE (args) = NULL_TREE;
1617 if (!same_type_p (TREE_VALUE (args), size_type_node))
1619 e = 2;
1620 args = TREE_CHAIN (args);
1623 else
1624 e = 2;
1626 if (e == 2)
1627 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1628 "as first parameter", size_type_node);
1630 switch (e)
1632 case 2:
1633 args = tree_cons (NULL_TREE, size_type_node, args);
1634 /* Fall through. */
1635 case 1:
1636 type = build_exception_variant
1637 (build_function_type (ptr_type_node, args),
1638 TYPE_RAISES_EXCEPTIONS (type));
1639 /* Fall through. */
1640 default:;
1642 return type;
1645 tree
1646 coerce_delete_type (tree type)
1648 int e = 0;
1649 tree args = TYPE_ARG_TYPES (type);
1651 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1653 if (!same_type_p (TREE_TYPE (type), void_type_node))
1655 e = 1;
1656 error ("%<operator delete%> must return type %qT", void_type_node);
1659 if (!args || args == void_list_node
1660 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1662 e = 2;
1663 if (args && args != void_list_node)
1664 args = TREE_CHAIN (args);
1665 error ("%<operator delete%> takes type %qT as first parameter",
1666 ptr_type_node);
1668 switch (e)
1670 case 2:
1671 args = tree_cons (NULL_TREE, ptr_type_node, args);
1672 /* Fall through. */
1673 case 1:
1674 type = build_exception_variant
1675 (build_function_type (void_type_node, args),
1676 TYPE_RAISES_EXCEPTIONS (type));
1677 /* Fall through. */
1678 default:;
1681 return type;
1684 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1685 and mark them as needed. */
1687 static void
1688 mark_vtable_entries (tree decl)
1690 tree fnaddr;
1691 unsigned HOST_WIDE_INT idx;
1693 /* It's OK for the vtable to refer to deprecated virtual functions. */
1694 warning_sentinel w(warn_deprecated_decl);
1696 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1697 idx, fnaddr)
1699 tree fn;
1701 STRIP_NOPS (fnaddr);
1703 if (TREE_CODE (fnaddr) != ADDR_EXPR
1704 && TREE_CODE (fnaddr) != FDESC_EXPR)
1705 /* This entry is an offset: a virtual base class offset, a
1706 virtual call offset, an RTTI offset, etc. */
1707 continue;
1709 fn = TREE_OPERAND (fnaddr, 0);
1710 TREE_ADDRESSABLE (fn) = 1;
1711 /* When we don't have vcall offsets, we output thunks whenever
1712 we output the vtables that contain them. With vcall offsets,
1713 we know all the thunks we'll need when we emit a virtual
1714 function, so we emit the thunks there instead. */
1715 if (DECL_THUNK_P (fn))
1716 use_thunk (fn, /*emit_p=*/0);
1717 mark_used (fn);
1721 /* Set DECL up to have the closest approximation of "initialized common"
1722 linkage available. */
1724 void
1725 comdat_linkage (tree decl)
1727 if (flag_weak)
1728 make_decl_one_only (decl, cxx_comdat_group (decl));
1729 else if (TREE_CODE (decl) == FUNCTION_DECL
1730 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1731 /* We can just emit function and compiler-generated variables
1732 statically; having multiple copies is (for the most part) only
1733 a waste of space.
1735 There are two correctness issues, however: the address of a
1736 template instantiation with external linkage should be the
1737 same, independent of what translation unit asks for the
1738 address, and this will not hold when we emit multiple copies of
1739 the function. However, there's little else we can do.
1741 Also, by default, the typeinfo implementation assumes that
1742 there will be only one copy of the string used as the name for
1743 each type. Therefore, if weak symbols are unavailable, the
1744 run-time library should perform a more conservative check; it
1745 should perform a string comparison, rather than an address
1746 comparison. */
1747 TREE_PUBLIC (decl) = 0;
1748 else
1750 /* Static data member template instantiations, however, cannot
1751 have multiple copies. */
1752 if (DECL_INITIAL (decl) == 0
1753 || DECL_INITIAL (decl) == error_mark_node)
1754 DECL_COMMON (decl) = 1;
1755 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1757 DECL_COMMON (decl) = 1;
1758 DECL_INITIAL (decl) = error_mark_node;
1760 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1762 /* We can't do anything useful; leave vars for explicit
1763 instantiation. */
1764 DECL_EXTERNAL (decl) = 1;
1765 DECL_NOT_REALLY_EXTERN (decl) = 0;
1769 if (TREE_PUBLIC (decl))
1770 DECL_COMDAT (decl) = 1;
1773 /* For win32 we also want to put explicit instantiations in
1774 linkonce sections, so that they will be merged with implicit
1775 instantiations; otherwise we get duplicate symbol errors.
1776 For Darwin we do not want explicit instantiations to be
1777 linkonce. */
1779 void
1780 maybe_make_one_only (tree decl)
1782 /* We used to say that this was not necessary on targets that support weak
1783 symbols, because the implicit instantiations will defer to the explicit
1784 one. However, that's not actually the case in SVR4; a strong definition
1785 after a weak one is an error. Also, not making explicit
1786 instantiations one_only means that we can end up with two copies of
1787 some template instantiations. */
1788 if (! flag_weak)
1789 return;
1791 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1792 we can get away with not emitting them if they aren't used. We need
1793 to for variables so that cp_finish_decl will update their linkage,
1794 because their DECL_INITIAL may not have been set properly yet. */
1796 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1797 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1798 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1800 make_decl_one_only (decl, cxx_comdat_group (decl));
1802 if (VAR_P (decl))
1804 varpool_node *node = varpool_node::get_create (decl);
1805 DECL_COMDAT (decl) = 1;
1806 /* Mark it needed so we don't forget to emit it. */
1807 node->forced_by_abi = true;
1808 TREE_USED (decl) = 1;
1813 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1814 This predicate will give the right answer during parsing of the
1815 function, which other tests may not. */
1817 bool
1818 vague_linkage_p (tree decl)
1820 if (!TREE_PUBLIC (decl))
1822 /* maybe_thunk_body clears TREE_PUBLIC on the maybe-in-charge 'tor
1823 variants, check one of the "clones" for the real linkage. */
1824 if ((DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
1825 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl))
1826 && DECL_CHAIN (decl)
1827 && DECL_CLONED_FUNCTION (DECL_CHAIN (decl)))
1828 return vague_linkage_p (DECL_CHAIN (decl));
1830 gcc_checking_assert (!DECL_COMDAT (decl));
1831 return false;
1833 /* Unfortunately, import_export_decl has not always been called
1834 before the function is processed, so we cannot simply check
1835 DECL_COMDAT. */
1836 if (DECL_COMDAT (decl)
1837 || (TREE_CODE (decl) == FUNCTION_DECL
1838 && DECL_DECLARED_INLINE_P (decl))
1839 || (DECL_LANG_SPECIFIC (decl)
1840 && DECL_TEMPLATE_INSTANTIATION (decl))
1841 || (VAR_P (decl) && DECL_INLINE_VAR_P (decl)))
1842 return true;
1843 else if (DECL_FUNCTION_SCOPE_P (decl))
1844 /* A local static in an inline effectively has vague linkage. */
1845 return (TREE_STATIC (decl)
1846 && vague_linkage_p (DECL_CONTEXT (decl)));
1847 else
1848 return false;
1851 /* Determine whether or not we want to specifically import or export CTYPE,
1852 using various heuristics. */
1854 static void
1855 import_export_class (tree ctype)
1857 /* -1 for imported, 1 for exported. */
1858 int import_export = 0;
1860 /* It only makes sense to call this function at EOF. The reason is
1861 that this function looks at whether or not the first non-inline
1862 non-abstract virtual member function has been defined in this
1863 translation unit. But, we can't possibly know that until we've
1864 seen the entire translation unit. */
1865 gcc_assert (at_eof);
1867 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1868 return;
1870 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1871 we will have CLASSTYPE_INTERFACE_ONLY set but not
1872 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1873 heuristic because someone will supply a #pragma implementation
1874 elsewhere, and deducing it here would produce a conflict. */
1875 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1876 return;
1878 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1879 import_export = -1;
1880 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1881 import_export = 1;
1882 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1883 && !flag_implicit_templates)
1884 /* For a template class, without -fimplicit-templates, check the
1885 repository. If the virtual table is assigned to this
1886 translation unit, then export the class; otherwise, import
1887 it. */
1888 import_export = repo_export_class_p (ctype) ? 1 : -1;
1889 else if (TYPE_POLYMORPHIC_P (ctype))
1891 /* The ABI specifies that the virtual table and associated
1892 information are emitted with the key method, if any. */
1893 tree method = CLASSTYPE_KEY_METHOD (ctype);
1894 /* If weak symbol support is not available, then we must be
1895 careful not to emit the vtable when the key function is
1896 inline. An inline function can be defined in multiple
1897 translation units. If we were to emit the vtable in each
1898 translation unit containing a definition, we would get
1899 multiple definition errors at link-time. */
1900 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1901 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1904 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1905 a definition anywhere else. */
1906 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1907 import_export = 0;
1909 /* Allow back ends the chance to overrule the decision. */
1910 if (targetm.cxx.import_export_class)
1911 import_export = targetm.cxx.import_export_class (ctype, import_export);
1913 if (import_export)
1915 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1916 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1920 /* Return true if VAR has already been provided to the back end; in that
1921 case VAR should not be modified further by the front end. */
1922 static bool
1923 var_finalized_p (tree var)
1925 return varpool_node::get_create (var)->definition;
1928 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1929 must be emitted in this translation unit. Mark it as such. */
1931 void
1932 mark_needed (tree decl)
1934 TREE_USED (decl) = 1;
1935 if (TREE_CODE (decl) == FUNCTION_DECL)
1937 /* Extern inline functions don't become needed when referenced.
1938 If we know a method will be emitted in other TU and no new
1939 functions can be marked reachable, just use the external
1940 definition. */
1941 struct cgraph_node *node = cgraph_node::get_create (decl);
1942 node->forced_by_abi = true;
1944 /* #pragma interface and -frepo code can call mark_needed for
1945 maybe-in-charge 'tors; mark the clones as well. */
1946 tree clone;
1947 FOR_EACH_CLONE (clone, decl)
1948 mark_needed (clone);
1950 else if (VAR_P (decl))
1952 varpool_node *node = varpool_node::get_create (decl);
1953 /* C++ frontend use mark_decl_references to force COMDAT variables
1954 to be output that might appear dead otherwise. */
1955 node->forced_by_abi = true;
1959 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1960 returns true if a definition of this entity should be provided in
1961 this object file. Callers use this function to determine whether
1962 or not to let the back end know that a definition of DECL is
1963 available in this translation unit. */
1965 bool
1966 decl_needed_p (tree decl)
1968 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
1969 /* This function should only be called at the end of the translation
1970 unit. We cannot be sure of whether or not something will be
1971 COMDAT until that point. */
1972 gcc_assert (at_eof);
1974 /* All entities with external linkage that are not COMDAT/EXTERN should be
1975 emitted; they may be referred to from other object files. */
1976 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
1977 return true;
1978 /* Functions marked "dllexport" must be emitted so that they are
1979 visible to other DLLs. */
1980 if (flag_keep_inline_dllexport
1981 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
1982 return true;
1984 /* When not optimizing, do not bother to produce definitions for extern
1985 symbols. */
1986 if (DECL_REALLY_EXTERN (decl)
1987 && ((TREE_CODE (decl) != FUNCTION_DECL
1988 && !optimize)
1989 || (TREE_CODE (decl) == FUNCTION_DECL
1990 && !opt_for_fn (decl, optimize)))
1991 && !lookup_attribute ("always_inline", decl))
1992 return false;
1994 /* If this entity was used, let the back end see it; it will decide
1995 whether or not to emit it into the object file. */
1996 if (TREE_USED (decl))
1997 return true;
1998 /* Virtual functions might be needed for devirtualization. */
1999 if (flag_devirtualize
2000 && TREE_CODE (decl) == FUNCTION_DECL
2001 && DECL_VIRTUAL_P (decl))
2002 return true;
2003 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2004 reference to DECL might cause it to be emitted later. */
2005 return false;
2008 /* If necessary, write out the vtables for the dynamic class CTYPE.
2009 Returns true if any vtables were emitted. */
2011 static bool
2012 maybe_emit_vtables (tree ctype)
2014 tree vtbl;
2015 tree primary_vtbl;
2016 int needed = 0;
2017 varpool_node *current = NULL, *last = NULL;
2019 /* If the vtables for this class have already been emitted there is
2020 nothing more to do. */
2021 primary_vtbl = CLASSTYPE_VTABLES (ctype);
2022 if (var_finalized_p (primary_vtbl))
2023 return false;
2024 /* Ignore dummy vtables made by get_vtable_decl. */
2025 if (TREE_TYPE (primary_vtbl) == void_type_node)
2026 return false;
2028 /* On some targets, we cannot determine the key method until the end
2029 of the translation unit -- which is when this function is
2030 called. */
2031 if (!targetm.cxx.key_method_may_be_inline ())
2032 determine_key_method (ctype);
2034 /* See if any of the vtables are needed. */
2035 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2037 import_export_decl (vtbl);
2038 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2039 needed = 1;
2041 if (!needed)
2043 /* If the references to this class' vtables are optimized away,
2044 still emit the appropriate debugging information. See
2045 dfs_debug_mark. */
2046 if (DECL_COMDAT (primary_vtbl)
2047 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2048 note_debug_info_needed (ctype);
2049 return false;
2052 /* The ABI requires that we emit all of the vtables if we emit any
2053 of them. */
2054 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2056 /* Mark entities references from the virtual table as used. */
2057 mark_vtable_entries (vtbl);
2059 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2061 vec<tree, va_gc> *cleanups = NULL;
2062 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2063 LOOKUP_NORMAL);
2065 /* It had better be all done at compile-time. */
2066 gcc_assert (!expr && !cleanups);
2069 /* Write it out. */
2070 DECL_EXTERNAL (vtbl) = 0;
2071 rest_of_decl_compilation (vtbl, 1, 1);
2073 /* Because we're only doing syntax-checking, we'll never end up
2074 actually marking the variable as written. */
2075 if (flag_syntax_only)
2076 TREE_ASM_WRITTEN (vtbl) = 1;
2077 else if (DECL_ONE_ONLY (vtbl))
2079 current = varpool_node::get_create (vtbl);
2080 if (last)
2081 current->add_to_same_comdat_group (last);
2082 last = current;
2086 /* Since we're writing out the vtable here, also write the debug
2087 info. */
2088 note_debug_info_needed (ctype);
2090 return true;
2093 /* A special return value from type_visibility meaning internal
2094 linkage. */
2096 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2098 /* walk_tree helper function for type_visibility. */
2100 static tree
2101 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2103 int *vis_p = (int *)data;
2104 if (! TYPE_P (*tp))
2106 *walk_subtrees = 0;
2108 else if (OVERLOAD_TYPE_P (*tp)
2109 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2111 *vis_p = VISIBILITY_ANON;
2112 return *tp;
2114 else if (CLASS_TYPE_P (*tp)
2115 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2116 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2117 return NULL;
2120 /* Returns the visibility of TYPE, which is the minimum visibility of its
2121 component types. */
2123 static int
2124 type_visibility (tree type)
2126 int vis = VISIBILITY_DEFAULT;
2127 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2128 return vis;
2131 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2132 specified (or if VISIBILITY is static). If TMPL is true, this
2133 constraint is for a template argument, and takes precedence
2134 over explicitly-specified visibility on the template. */
2136 static void
2137 constrain_visibility (tree decl, int visibility, bool tmpl)
2139 if (visibility == VISIBILITY_ANON)
2141 /* extern "C" declarations aren't affected by the anonymous
2142 namespace. */
2143 if (!DECL_EXTERN_C_P (decl))
2145 TREE_PUBLIC (decl) = 0;
2146 DECL_WEAK (decl) = 0;
2147 DECL_COMMON (decl) = 0;
2148 DECL_COMDAT (decl) = false;
2149 if (VAR_OR_FUNCTION_DECL_P (decl))
2151 struct symtab_node *snode = symtab_node::get (decl);
2153 if (snode)
2154 snode->set_comdat_group (NULL);
2156 DECL_INTERFACE_KNOWN (decl) = 1;
2157 if (DECL_LANG_SPECIFIC (decl))
2158 DECL_NOT_REALLY_EXTERN (decl) = 1;
2161 else if (visibility > DECL_VISIBILITY (decl)
2162 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2164 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2165 /* This visibility was not specified. */
2166 DECL_VISIBILITY_SPECIFIED (decl) = false;
2170 /* Constrain the visibility of DECL based on the visibility of its template
2171 arguments. */
2173 static void
2174 constrain_visibility_for_template (tree decl, tree targs)
2176 /* If this is a template instantiation, check the innermost
2177 template args for visibility constraints. The outer template
2178 args are covered by the class check. */
2179 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2180 int i;
2181 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2183 int vis = 0;
2185 tree arg = TREE_VEC_ELT (args, i-1);
2186 if (TYPE_P (arg))
2187 vis = type_visibility (arg);
2188 else
2190 if (REFERENCE_REF_P (arg))
2191 arg = TREE_OPERAND (arg, 0);
2192 if (TREE_TYPE (arg))
2193 STRIP_NOPS (arg);
2194 if (TREE_CODE (arg) == ADDR_EXPR)
2195 arg = TREE_OPERAND (arg, 0);
2196 if (VAR_OR_FUNCTION_DECL_P (arg))
2198 if (! TREE_PUBLIC (arg))
2199 vis = VISIBILITY_ANON;
2200 else
2201 vis = DECL_VISIBILITY (arg);
2204 if (vis)
2205 constrain_visibility (decl, vis, true);
2209 /* Like c_determine_visibility, but with additional C++-specific
2210 behavior.
2212 Function-scope entities can rely on the function's visibility because
2213 it is set in start_preparsed_function.
2215 Class-scope entities cannot rely on the class's visibility until the end
2216 of the enclosing class definition.
2218 Note that because namespaces have multiple independent definitions,
2219 namespace visibility is handled elsewhere using the #pragma visibility
2220 machinery rather than by decorating the namespace declaration.
2222 The goal is for constraints from the type to give a diagnostic, and
2223 other constraints to be applied silently. */
2225 void
2226 determine_visibility (tree decl)
2228 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2230 /* Only relevant for names with external linkage. */
2231 if (!TREE_PUBLIC (decl))
2232 return;
2234 /* Cloned constructors and destructors get the same visibility as
2235 the underlying function. That should be set up in
2236 maybe_clone_body. */
2237 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2239 bool orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2240 enum symbol_visibility orig_visibility = DECL_VISIBILITY (decl);
2242 /* The decl may be a template instantiation, which could influence
2243 visibilty. */
2244 tree template_decl = NULL_TREE;
2245 if (TREE_CODE (decl) == TYPE_DECL)
2247 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2249 if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
2250 template_decl = decl;
2252 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2253 template_decl = decl;
2255 else if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
2256 template_decl = decl;
2258 /* If DECL is a member of a class, visibility specifiers on the
2259 class can influence the visibility of the DECL. */
2260 tree class_type = NULL_TREE;
2261 if (DECL_CLASS_SCOPE_P (decl))
2262 class_type = DECL_CONTEXT (decl);
2263 else
2265 /* Not a class member. */
2267 /* Virtual tables have DECL_CONTEXT set to their associated class,
2268 so they are automatically handled above. */
2269 gcc_assert (!VAR_P (decl)
2270 || !DECL_VTABLE_OR_VTT_P (decl));
2272 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2274 /* Local statics and classes get the visibility of their
2275 containing function by default, except that
2276 -fvisibility-inlines-hidden doesn't affect them. */
2277 tree fn = DECL_CONTEXT (decl);
2278 if (DECL_VISIBILITY_SPECIFIED (fn))
2280 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2281 DECL_VISIBILITY_SPECIFIED (decl) =
2282 DECL_VISIBILITY_SPECIFIED (fn);
2284 else
2286 if (DECL_CLASS_SCOPE_P (fn))
2287 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2288 else if (determine_hidden_inline (fn))
2290 DECL_VISIBILITY (decl) = default_visibility;
2291 DECL_VISIBILITY_SPECIFIED (decl) =
2292 visibility_options.inpragma;
2294 else
2296 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2297 DECL_VISIBILITY_SPECIFIED (decl) =
2298 DECL_VISIBILITY_SPECIFIED (fn);
2302 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2303 but have no TEMPLATE_INFO. Their containing template
2304 function does, and the local class could be constrained
2305 by that. */
2306 if (template_decl)
2307 template_decl = fn;
2309 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2310 && flag_visibility_ms_compat)
2312 /* Under -fvisibility-ms-compat, types are visible by default,
2313 even though their contents aren't. */
2314 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2315 int underlying_vis = type_visibility (underlying_type);
2316 if (underlying_vis == VISIBILITY_ANON
2317 || (CLASS_TYPE_P (underlying_type)
2318 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2319 constrain_visibility (decl, underlying_vis, false);
2320 else
2321 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2323 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2325 /* tinfo visibility is based on the type it's for. */
2326 constrain_visibility
2327 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2329 /* Give the target a chance to override the visibility associated
2330 with DECL. */
2331 if (TREE_PUBLIC (decl)
2332 && !DECL_REALLY_EXTERN (decl)
2333 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2334 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2335 targetm.cxx.determine_class_data_visibility (decl);
2337 else if (template_decl)
2338 /* Template instantiations and specializations get visibility based
2339 on their template unless they override it with an attribute. */;
2340 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2342 if (determine_hidden_inline (decl))
2343 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2344 else
2346 /* Set default visibility to whatever the user supplied with
2347 #pragma GCC visibility or a namespace visibility attribute. */
2348 DECL_VISIBILITY (decl) = default_visibility;
2349 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2354 if (template_decl)
2356 /* If the specialization doesn't specify visibility, use the
2357 visibility from the template. */
2358 tree tinfo = get_template_info (template_decl);
2359 tree args = TI_ARGS (tinfo);
2360 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2361 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2362 : DECL_ATTRIBUTES (decl));
2364 if (args != error_mark_node)
2366 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2368 if (!DECL_VISIBILITY_SPECIFIED (decl))
2370 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2371 && determine_hidden_inline (decl))
2372 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2373 else
2375 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2376 DECL_VISIBILITY_SPECIFIED (decl)
2377 = DECL_VISIBILITY_SPECIFIED (pattern);
2381 if (args
2382 /* Template argument visibility outweighs #pragma or namespace
2383 visibility, but not an explicit attribute. */
2384 && !lookup_attribute ("visibility", attribs))
2386 int depth = TMPL_ARGS_DEPTH (args);
2387 if (DECL_VISIBILITY_SPECIFIED (decl))
2389 /* A class template member with explicit visibility
2390 overrides the class visibility, so we need to apply
2391 all the levels of template args directly. */
2392 int i;
2393 for (i = 1; i <= depth; ++i)
2395 tree lev = TMPL_ARGS_LEVEL (args, i);
2396 constrain_visibility_for_template (decl, lev);
2399 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2400 /* Limit visibility based on its template arguments. */
2401 constrain_visibility_for_template (decl, args);
2406 if (class_type)
2407 determine_visibility_from_class (decl, class_type);
2409 if (decl_anon_ns_mem_p (decl))
2410 /* Names in an anonymous namespace get internal linkage.
2411 This might change once we implement export. */
2412 constrain_visibility (decl, VISIBILITY_ANON, false);
2413 else if (TREE_CODE (decl) != TYPE_DECL)
2415 /* Propagate anonymity from type to decl. */
2416 int tvis = type_visibility (TREE_TYPE (decl));
2417 if (tvis == VISIBILITY_ANON
2418 || ! DECL_VISIBILITY_SPECIFIED (decl))
2419 constrain_visibility (decl, tvis, false);
2421 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2422 /* DR 757: A type without linkage shall not be used as the type of a
2423 variable or function with linkage, unless
2424 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2425 o the variable or function is not used (3.2 [basic.def.odr]) or is
2426 defined in the same translation unit.
2428 Since non-extern "C" decls need to be defined in the same
2429 translation unit, we can make the type internal. */
2430 constrain_visibility (decl, VISIBILITY_ANON, false);
2432 /* If visibility changed and DECL already has DECL_RTL, ensure
2433 symbol flags are updated. */
2434 if ((DECL_VISIBILITY (decl) != orig_visibility
2435 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2436 && ((VAR_P (decl) && TREE_STATIC (decl))
2437 || TREE_CODE (decl) == FUNCTION_DECL)
2438 && DECL_RTL_SET_P (decl))
2439 make_decl_rtl (decl);
2442 /* By default, static data members and function members receive
2443 the visibility of their containing class. */
2445 static void
2446 determine_visibility_from_class (tree decl, tree class_type)
2448 if (DECL_VISIBILITY_SPECIFIED (decl))
2449 return;
2451 if (determine_hidden_inline (decl))
2452 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2453 else
2455 /* Default to the class visibility. */
2456 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2457 DECL_VISIBILITY_SPECIFIED (decl)
2458 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2461 /* Give the target a chance to override the visibility associated
2462 with DECL. */
2463 if (VAR_P (decl)
2464 && (DECL_TINFO_P (decl)
2465 || (DECL_VTABLE_OR_VTT_P (decl)
2466 /* Construction virtual tables are not exported because
2467 they cannot be referred to from other object files;
2468 their name is not standardized by the ABI. */
2469 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2470 && TREE_PUBLIC (decl)
2471 && !DECL_REALLY_EXTERN (decl)
2472 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2473 targetm.cxx.determine_class_data_visibility (decl);
2476 /* Returns true iff DECL is an inline that should get hidden visibility
2477 because of -fvisibility-inlines-hidden. */
2479 static bool
2480 determine_hidden_inline (tree decl)
2482 return (visibility_options.inlines_hidden
2483 /* Don't do this for inline templates; specializations might not be
2484 inline, and we don't want them to inherit the hidden
2485 visibility. We'll set it here for all inline instantiations. */
2486 && !processing_template_decl
2487 && TREE_CODE (decl) == FUNCTION_DECL
2488 && DECL_DECLARED_INLINE_P (decl)
2489 && (! DECL_LANG_SPECIFIC (decl)
2490 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2493 /* Constrain the visibility of a class TYPE based on the visibility of its
2494 field types. Warn if any fields require lesser visibility. */
2496 void
2497 constrain_class_visibility (tree type)
2499 tree binfo;
2500 tree t;
2501 int i;
2503 int vis = type_visibility (type);
2505 if (vis == VISIBILITY_ANON
2506 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2507 return;
2509 /* Don't warn about visibility if the class has explicit visibility. */
2510 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2511 vis = VISIBILITY_INTERNAL;
2513 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2514 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2516 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2517 int subvis = type_visibility (ftype);
2519 if (subvis == VISIBILITY_ANON)
2521 if (!in_main_input_context())
2523 tree nlt = no_linkage_check (ftype, /*relaxed_p=*/false);
2524 if (nlt)
2526 if (same_type_p (TREE_TYPE (t), nlt))
2527 warning (OPT_Wsubobject_linkage, "\
2528 %qT has a field %qD whose type has no linkage",
2529 type, t);
2530 else
2531 warning (OPT_Wsubobject_linkage, "\
2532 %qT has a field %qD whose type depends on the type %qT which has no linkage",
2533 type, t, nlt);
2535 else
2536 warning (OPT_Wsubobject_linkage, "\
2537 %qT has a field %qD whose type uses the anonymous namespace",
2538 type, t);
2541 else if (MAYBE_CLASS_TYPE_P (ftype)
2542 && vis < VISIBILITY_HIDDEN
2543 && subvis >= VISIBILITY_HIDDEN)
2544 warning (OPT_Wattributes, "\
2545 %qT declared with greater visibility than the type of its field %qD",
2546 type, t);
2549 binfo = TYPE_BINFO (type);
2550 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2552 int subvis = type_visibility (TREE_TYPE (t));
2554 if (subvis == VISIBILITY_ANON)
2556 if (!in_main_input_context())
2558 tree nlt = no_linkage_check (TREE_TYPE (t), /*relaxed_p=*/false);
2559 if (nlt)
2561 if (same_type_p (TREE_TYPE (t), nlt))
2562 warning (OPT_Wsubobject_linkage, "\
2563 %qT has a base %qT whose type has no linkage",
2564 type, TREE_TYPE (t));
2565 else
2566 warning (OPT_Wsubobject_linkage, "\
2567 %qT has a base %qT whose type depends on the type %qT which has no linkage",
2568 type, TREE_TYPE (t), nlt);
2570 else
2571 warning (OPT_Wsubobject_linkage, "\
2572 %qT has a base %qT whose type uses the anonymous namespace",
2573 type, TREE_TYPE (t));
2576 else if (vis < VISIBILITY_HIDDEN
2577 && subvis >= VISIBILITY_HIDDEN)
2578 warning (OPT_Wattributes, "\
2579 %qT declared with greater visibility than its base %qT",
2580 type, TREE_TYPE (t));
2584 /* Functions for adjusting the visibility of a tagged type and its nested
2585 types and declarations when it gets a name for linkage purposes from a
2586 typedef. */
2588 static void bt_reset_linkage_1 (binding_entry, void *);
2589 static void bt_reset_linkage_2 (binding_entry, void *);
2591 /* First reset the visibility of all the types. */
2593 static void
2594 reset_type_linkage_1 (tree type)
2596 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2597 if (CLASS_TYPE_P (type))
2598 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2599 bt_reset_linkage_1, NULL);
2601 static void
2602 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2604 reset_type_linkage_1 (b->type);
2607 /* Then reset the visibility of any static data members or member
2608 functions that use those types. */
2610 static void
2611 reset_decl_linkage (tree decl)
2613 if (TREE_PUBLIC (decl))
2614 return;
2615 if (DECL_CLONED_FUNCTION_P (decl))
2616 return;
2617 TREE_PUBLIC (decl) = true;
2618 DECL_INTERFACE_KNOWN (decl) = false;
2619 determine_visibility (decl);
2620 tentative_decl_linkage (decl);
2622 static void
2623 reset_type_linkage_2 (tree type)
2625 if (CLASS_TYPE_P (type))
2627 if (tree vt = CLASSTYPE_VTABLES (type))
2629 tree name = mangle_vtbl_for_type (type);
2630 DECL_NAME (vt) = name;
2631 SET_DECL_ASSEMBLER_NAME (vt, name);
2632 reset_decl_linkage (vt);
2634 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2636 tree name = mangle_typeinfo_for_type (type);
2637 DECL_NAME (ti) = name;
2638 SET_DECL_ASSEMBLER_NAME (ti, name);
2639 TREE_TYPE (name) = type;
2640 reset_decl_linkage (ti);
2642 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2644 tree mem = STRIP_TEMPLATE (m);
2645 if (VAR_P (mem))
2646 reset_decl_linkage (mem);
2648 for (tree m = TYPE_METHODS (type); m; m = DECL_CHAIN (m))
2650 tree mem = STRIP_TEMPLATE (m);
2651 reset_decl_linkage (mem);
2652 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (mem))
2654 /* Also update its name, for cxx_dwarf_name. */
2655 DECL_NAME (mem) = TYPE_IDENTIFIER (type);
2656 if (m != mem)
2657 DECL_NAME (m) = TYPE_IDENTIFIER (type);
2660 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2661 bt_reset_linkage_2, NULL);
2664 static void
2665 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2667 reset_type_linkage_2 (b->type);
2669 void
2670 reset_type_linkage (tree type)
2672 reset_type_linkage_1 (type);
2673 reset_type_linkage_2 (type);
2676 /* Set up our initial idea of what the linkage of DECL should be. */
2678 void
2679 tentative_decl_linkage (tree decl)
2681 if (DECL_INTERFACE_KNOWN (decl))
2682 /* We've already made a decision as to how this function will
2683 be handled. */;
2684 else if (vague_linkage_p (decl))
2686 if (TREE_CODE (decl) == FUNCTION_DECL
2687 && decl_defined_p (decl))
2689 DECL_EXTERNAL (decl) = 1;
2690 DECL_NOT_REALLY_EXTERN (decl) = 1;
2691 note_vague_linkage_fn (decl);
2692 /* A non-template inline function with external linkage will
2693 always be COMDAT. As we must eventually determine the
2694 linkage of all functions, and as that causes writes to
2695 the data mapped in from the PCH file, it's advantageous
2696 to mark the functions at this point. */
2697 if (DECL_DECLARED_INLINE_P (decl)
2698 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2699 || DECL_DEFAULTED_FN (decl)))
2701 /* This function must have external linkage, as
2702 otherwise DECL_INTERFACE_KNOWN would have been
2703 set. */
2704 gcc_assert (TREE_PUBLIC (decl));
2705 comdat_linkage (decl);
2706 DECL_INTERFACE_KNOWN (decl) = 1;
2709 else if (VAR_P (decl))
2710 maybe_commonize_var (decl);
2714 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2715 for DECL has not already been determined, do so now by setting
2716 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2717 function is called entities with vague linkage whose definitions
2718 are available must have TREE_PUBLIC set.
2720 If this function decides to place DECL in COMDAT, it will set
2721 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2722 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2723 callers defer that decision until it is clear that DECL is actually
2724 required. */
2726 void
2727 import_export_decl (tree decl)
2729 int emit_p;
2730 bool comdat_p;
2731 bool import_p;
2732 tree class_type = NULL_TREE;
2734 if (DECL_INTERFACE_KNOWN (decl))
2735 return;
2737 /* We cannot determine what linkage to give to an entity with vague
2738 linkage until the end of the file. For example, a virtual table
2739 for a class will be defined if and only if the key method is
2740 defined in this translation unit. As a further example, consider
2741 that when compiling a translation unit that uses PCH file with
2742 "-frepo" it would be incorrect to make decisions about what
2743 entities to emit when building the PCH; those decisions must be
2744 delayed until the repository information has been processed. */
2745 gcc_assert (at_eof);
2746 /* Object file linkage for explicit instantiations is handled in
2747 mark_decl_instantiated. For static variables in functions with
2748 vague linkage, maybe_commonize_var is used.
2750 Therefore, the only declarations that should be provided to this
2751 function are those with external linkage that are:
2753 * implicit instantiations of function templates
2755 * inline function
2757 * implicit instantiations of static data members of class
2758 templates
2760 * virtual tables
2762 * typeinfo objects
2764 Furthermore, all entities that reach this point must have a
2765 definition available in this translation unit.
2767 The following assertions check these conditions. */
2768 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2769 /* Any code that creates entities with TREE_PUBLIC cleared should
2770 also set DECL_INTERFACE_KNOWN. */
2771 gcc_assert (TREE_PUBLIC (decl));
2772 if (TREE_CODE (decl) == FUNCTION_DECL)
2773 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2774 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2775 || DECL_DECLARED_INLINE_P (decl));
2776 else
2777 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2778 || DECL_VTABLE_OR_VTT_P (decl)
2779 || DECL_TINFO_P (decl));
2780 /* Check that a definition of DECL is available in this translation
2781 unit. */
2782 gcc_assert (!DECL_REALLY_EXTERN (decl));
2784 /* Assume that DECL will not have COMDAT linkage. */
2785 comdat_p = false;
2786 /* Assume that DECL will not be imported into this translation
2787 unit. */
2788 import_p = false;
2790 /* See if the repository tells us whether or not to emit DECL in
2791 this translation unit. */
2792 emit_p = repo_emit_p (decl);
2793 if (emit_p == 0)
2794 import_p = true;
2795 else if (emit_p == 1)
2797 /* The repository indicates that this entity should be defined
2798 here. Make sure the back end honors that request. */
2799 mark_needed (decl);
2800 /* Output the definition as an ordinary strong definition. */
2801 DECL_EXTERNAL (decl) = 0;
2802 DECL_INTERFACE_KNOWN (decl) = 1;
2803 return;
2806 if (import_p)
2807 /* We have already decided what to do with this DECL; there is no
2808 need to check anything further. */
2810 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2812 class_type = DECL_CONTEXT (decl);
2813 import_export_class (class_type);
2814 if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2815 && CLASSTYPE_INTERFACE_ONLY (class_type))
2816 import_p = true;
2817 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2818 && !CLASSTYPE_USE_TEMPLATE (class_type)
2819 && CLASSTYPE_KEY_METHOD (class_type)
2820 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2821 /* The ABI requires that all virtual tables be emitted with
2822 COMDAT linkage. However, on systems where COMDAT symbols
2823 don't show up in the table of contents for a static
2824 archive, or on systems without weak symbols (where we
2825 approximate COMDAT linkage by using internal linkage), the
2826 linker will report errors about undefined symbols because
2827 it will not see the virtual table definition. Therefore,
2828 in the case that we know that the virtual table will be
2829 emitted in only one translation unit, we make the virtual
2830 table an ordinary definition with external linkage. */
2831 DECL_EXTERNAL (decl) = 0;
2832 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2834 /* CLASS_TYPE is being exported from this translation unit,
2835 so DECL should be defined here. */
2836 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2837 /* If a class is declared in a header with the "extern
2838 template" extension, then it will not be instantiated,
2839 even in translation units that would normally require
2840 it. Often such classes are explicitly instantiated in
2841 one translation unit. Therefore, the explicit
2842 instantiation must be made visible to other translation
2843 units. */
2844 DECL_EXTERNAL (decl) = 0;
2845 else
2847 /* The generic C++ ABI says that class data is always
2848 COMDAT, even if there is a key function. Some
2849 variants (e.g., the ARM EABI) says that class data
2850 only has COMDAT linkage if the class data might be
2851 emitted in more than one translation unit. When the
2852 key method can be inline and is inline, we still have
2853 to arrange for comdat even though
2854 class_data_always_comdat is false. */
2855 if (!CLASSTYPE_KEY_METHOD (class_type)
2856 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2857 || targetm.cxx.class_data_always_comdat ())
2859 /* The ABI requires COMDAT linkage. Normally, we
2860 only emit COMDAT things when they are needed;
2861 make sure that we realize that this entity is
2862 indeed needed. */
2863 comdat_p = true;
2864 mark_needed (decl);
2868 else if (!flag_implicit_templates
2869 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2870 import_p = true;
2871 else
2872 comdat_p = true;
2874 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2876 tree type = TREE_TYPE (DECL_NAME (decl));
2877 if (CLASS_TYPE_P (type))
2879 class_type = type;
2880 import_export_class (type);
2881 if (CLASSTYPE_INTERFACE_KNOWN (type)
2882 && TYPE_POLYMORPHIC_P (type)
2883 && CLASSTYPE_INTERFACE_ONLY (type)
2884 /* If -fno-rtti was specified, then we cannot be sure
2885 that RTTI information will be emitted with the
2886 virtual table of the class, so we must emit it
2887 wherever it is used. */
2888 && flag_rtti)
2889 import_p = true;
2890 else
2892 if (CLASSTYPE_INTERFACE_KNOWN (type)
2893 && !CLASSTYPE_INTERFACE_ONLY (type))
2895 comdat_p = (targetm.cxx.class_data_always_comdat ()
2896 || (CLASSTYPE_KEY_METHOD (type)
2897 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2898 mark_needed (decl);
2899 if (!flag_weak)
2901 comdat_p = false;
2902 DECL_EXTERNAL (decl) = 0;
2905 else
2906 comdat_p = true;
2909 else
2910 comdat_p = true;
2912 else if (DECL_TEMPLOID_INSTANTIATION (decl))
2914 /* DECL is an implicit instantiation of a function or static
2915 data member. */
2916 if ((flag_implicit_templates
2917 && !flag_use_repository)
2918 || (flag_implicit_inline_templates
2919 && TREE_CODE (decl) == FUNCTION_DECL
2920 && DECL_DECLARED_INLINE_P (decl)))
2921 comdat_p = true;
2922 else
2923 /* If we are not implicitly generating templates, then mark
2924 this entity as undefined in this translation unit. */
2925 import_p = true;
2927 else if (DECL_FUNCTION_MEMBER_P (decl))
2929 if (!DECL_DECLARED_INLINE_P (decl))
2931 tree ctype = DECL_CONTEXT (decl);
2932 import_export_class (ctype);
2933 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2935 DECL_NOT_REALLY_EXTERN (decl)
2936 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2937 || (DECL_DECLARED_INLINE_P (decl)
2938 && ! flag_implement_inlines
2939 && !DECL_VINDEX (decl)));
2941 if (!DECL_NOT_REALLY_EXTERN (decl))
2942 DECL_EXTERNAL (decl) = 1;
2944 /* Always make artificials weak. */
2945 if (DECL_ARTIFICIAL (decl) && flag_weak)
2946 comdat_p = true;
2947 else
2948 maybe_make_one_only (decl);
2951 else
2952 comdat_p = true;
2954 else
2955 comdat_p = true;
2957 if (import_p)
2959 /* If we are importing DECL into this translation unit, mark is
2960 an undefined here. */
2961 DECL_EXTERNAL (decl) = 1;
2962 DECL_NOT_REALLY_EXTERN (decl) = 0;
2964 else if (comdat_p)
2966 /* If we decided to put DECL in COMDAT, mark it accordingly at
2967 this point. */
2968 comdat_linkage (decl);
2971 DECL_INTERFACE_KNOWN (decl) = 1;
2974 /* Return an expression that performs the destruction of DECL, which
2975 must be a VAR_DECL whose type has a non-trivial destructor, or is
2976 an array whose (innermost) elements have a non-trivial destructor. */
2978 tree
2979 build_cleanup (tree decl)
2981 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
2982 gcc_assert (clean != NULL_TREE);
2983 return clean;
2986 /* Returns the initialization guard variable for the variable DECL,
2987 which has static storage duration. */
2989 tree
2990 get_guard (tree decl)
2992 tree sname;
2993 tree guard;
2995 sname = mangle_guard_variable (decl);
2996 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2997 if (! guard)
2999 tree guard_type;
3001 /* We use a type that is big enough to contain a mutex as well
3002 as an integer counter. */
3003 guard_type = targetm.cxx.guard_type ();
3004 guard = build_decl (DECL_SOURCE_LOCATION (decl),
3005 VAR_DECL, sname, guard_type);
3007 /* The guard should have the same linkage as what it guards. */
3008 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
3009 TREE_STATIC (guard) = TREE_STATIC (decl);
3010 DECL_COMMON (guard) = DECL_COMMON (decl);
3011 DECL_COMDAT (guard) = DECL_COMDAT (decl);
3012 CP_DECL_THREAD_LOCAL_P (guard) = CP_DECL_THREAD_LOCAL_P (decl);
3013 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
3014 if (DECL_ONE_ONLY (decl))
3015 make_decl_one_only (guard, cxx_comdat_group (guard));
3016 if (TREE_PUBLIC (decl))
3017 DECL_WEAK (guard) = DECL_WEAK (decl);
3018 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
3019 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
3021 DECL_ARTIFICIAL (guard) = 1;
3022 DECL_IGNORED_P (guard) = 1;
3023 TREE_USED (guard) = 1;
3024 pushdecl_top_level_and_finish (guard, NULL_TREE);
3026 return guard;
3029 /* Return an atomic load of src with the appropriate memory model. */
3031 static tree
3032 build_atomic_load_byte (tree src, HOST_WIDE_INT model)
3034 tree ptr_type = build_pointer_type (char_type_node);
3035 tree mem_model = build_int_cst (integer_type_node, model);
3036 tree t, addr, val;
3037 unsigned int size;
3038 int fncode;
3040 size = tree_to_uhwi (TYPE_SIZE_UNIT (char_type_node));
3042 fncode = BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1;
3043 t = builtin_decl_implicit ((enum built_in_function) fncode);
3045 addr = build1 (ADDR_EXPR, ptr_type, src);
3046 val = build_call_expr (t, 2, addr, mem_model);
3047 return val;
3050 /* Return those bits of the GUARD variable that should be set when the
3051 guarded entity is actually initialized. */
3053 static tree
3054 get_guard_bits (tree guard)
3056 if (!targetm.cxx.guard_mask_bit ())
3058 /* We only set the first byte of the guard, in order to leave room
3059 for a mutex in the high-order bits. */
3060 guard = build1 (ADDR_EXPR,
3061 build_pointer_type (TREE_TYPE (guard)),
3062 guard);
3063 guard = build1 (NOP_EXPR,
3064 build_pointer_type (char_type_node),
3065 guard);
3066 guard = build1 (INDIRECT_REF, char_type_node, guard);
3069 return guard;
3072 /* Return an expression which determines whether or not the GUARD
3073 variable has already been initialized. */
3075 tree
3076 get_guard_cond (tree guard, bool thread_safe)
3078 tree guard_value;
3080 if (!thread_safe)
3081 guard = get_guard_bits (guard);
3082 else
3083 guard = build_atomic_load_byte (guard, MEMMODEL_ACQUIRE);
3085 /* Mask off all but the low bit. */
3086 if (targetm.cxx.guard_mask_bit ())
3088 guard_value = integer_one_node;
3089 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3090 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3091 guard = cp_build_binary_op (input_location,
3092 BIT_AND_EXPR, guard, guard_value,
3093 tf_warning_or_error);
3096 guard_value = integer_zero_node;
3097 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3098 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3099 return cp_build_binary_op (input_location,
3100 EQ_EXPR, guard, guard_value,
3101 tf_warning_or_error);
3104 /* Return an expression which sets the GUARD variable, indicating that
3105 the variable being guarded has been initialized. */
3107 tree
3108 set_guard (tree guard)
3110 tree guard_init;
3112 /* Set the GUARD to one. */
3113 guard = get_guard_bits (guard);
3114 guard_init = integer_one_node;
3115 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3116 guard_init = fold_convert (TREE_TYPE (guard), guard_init);
3117 return cp_build_modify_expr (input_location, guard, NOP_EXPR, guard_init,
3118 tf_warning_or_error);
3121 /* Returns true iff we can tell that VAR does not have a dynamic
3122 initializer. */
3124 static bool
3125 var_defined_without_dynamic_init (tree var)
3127 /* If it's defined in another TU, we can't tell. */
3128 if (DECL_EXTERNAL (var))
3129 return false;
3130 /* If it has a non-trivial destructor, registering the destructor
3131 counts as dynamic initialization. */
3132 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3133 return false;
3134 /* If it's in this TU, its initializer has been processed, unless
3135 it's a case of self-initialization, then DECL_INITIALIZED_P is
3136 false while the initializer is handled by finish_id_expression. */
3137 if (!DECL_INITIALIZED_P (var))
3138 return false;
3139 /* If it has no initializer or a constant one, it's not dynamic. */
3140 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3141 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3144 /* Returns true iff VAR is a variable that needs uses to be
3145 wrapped for possible dynamic initialization. */
3147 static bool
3148 var_needs_tls_wrapper (tree var)
3150 return (!error_operand_p (var)
3151 && CP_DECL_THREAD_LOCAL_P (var)
3152 && !DECL_GNU_TLS_P (var)
3153 && !DECL_FUNCTION_SCOPE_P (var)
3154 && !var_defined_without_dynamic_init (var));
3157 /* Get the FUNCTION_DECL for the shared TLS init function for this
3158 translation unit. */
3160 static tree
3161 get_local_tls_init_fn (void)
3163 tree sname = get_identifier ("__tls_init");
3164 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3165 if (!fn)
3167 fn = build_lang_decl (FUNCTION_DECL, sname,
3168 build_function_type (void_type_node,
3169 void_list_node));
3170 SET_DECL_LANGUAGE (fn, lang_c);
3171 TREE_PUBLIC (fn) = false;
3172 DECL_ARTIFICIAL (fn) = true;
3173 mark_used (fn);
3174 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3176 return fn;
3179 /* Get a FUNCTION_DECL for the init function for the thread_local
3180 variable VAR. The init function will be an alias to the function
3181 that initializes all the non-local TLS variables in the translation
3182 unit. The init function is only used by the wrapper function. */
3184 static tree
3185 get_tls_init_fn (tree var)
3187 /* Only C++11 TLS vars need this init fn. */
3188 if (!var_needs_tls_wrapper (var))
3189 return NULL_TREE;
3191 /* If -fno-extern-tls-init, assume that we don't need to call
3192 a tls init function for a variable defined in another TU. */
3193 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3194 return NULL_TREE;
3196 #ifdef ASM_OUTPUT_DEF
3197 /* If the variable is internal, or if we can't generate aliases,
3198 call the local init function directly. */
3199 if (!TREE_PUBLIC (var))
3200 #endif
3201 return get_local_tls_init_fn ();
3203 tree sname = mangle_tls_init_fn (var);
3204 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3205 if (!fn)
3207 fn = build_lang_decl (FUNCTION_DECL, sname,
3208 build_function_type (void_type_node,
3209 void_list_node));
3210 SET_DECL_LANGUAGE (fn, lang_c);
3211 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3212 DECL_ARTIFICIAL (fn) = true;
3213 DECL_COMDAT (fn) = DECL_COMDAT (var);
3214 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3215 if (DECL_ONE_ONLY (var))
3216 make_decl_one_only (fn, cxx_comdat_group (fn));
3217 if (TREE_PUBLIC (var))
3219 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3220 /* If the variable is defined somewhere else and might have static
3221 initialization, make the init function a weak reference. */
3222 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3223 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3224 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3225 && DECL_EXTERNAL (var))
3226 declare_weak (fn);
3227 else
3228 DECL_WEAK (fn) = DECL_WEAK (var);
3230 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3231 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3232 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3233 DECL_IGNORED_P (fn) = 1;
3234 mark_used (fn);
3236 DECL_BEFRIENDING_CLASSES (fn) = var;
3238 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3240 return fn;
3243 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3244 variable VAR. The wrapper function calls the init function (if any) for
3245 VAR and then returns a reference to VAR. The wrapper function is used
3246 in place of VAR everywhere VAR is mentioned. */
3248 tree
3249 get_tls_wrapper_fn (tree var)
3251 /* Only C++11 TLS vars need this wrapper fn. */
3252 if (!var_needs_tls_wrapper (var))
3253 return NULL_TREE;
3255 tree sname = mangle_tls_wrapper_fn (var);
3256 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3257 if (!fn)
3259 /* A named rvalue reference is an lvalue, so the wrapper should
3260 always return an lvalue reference. */
3261 tree type = non_reference (TREE_TYPE (var));
3262 type = build_reference_type (type);
3263 tree fntype = build_function_type (type, void_list_node);
3264 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3265 SET_DECL_LANGUAGE (fn, lang_c);
3266 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3267 DECL_ARTIFICIAL (fn) = true;
3268 DECL_IGNORED_P (fn) = 1;
3269 /* The wrapper is inline and emitted everywhere var is used. */
3270 DECL_DECLARED_INLINE_P (fn) = true;
3271 if (TREE_PUBLIC (var))
3273 comdat_linkage (fn);
3274 #ifdef HAVE_GAS_HIDDEN
3275 /* Make the wrapper bind locally; there's no reason to share
3276 the wrapper between multiple shared objects. */
3277 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3278 DECL_VISIBILITY_SPECIFIED (fn) = true;
3279 #endif
3281 if (!TREE_PUBLIC (fn))
3282 DECL_INTERFACE_KNOWN (fn) = true;
3283 mark_used (fn);
3284 note_vague_linkage_fn (fn);
3286 #if 0
3287 /* We want CSE to commonize calls to the wrapper, but marking it as
3288 pure is unsafe since it has side-effects. I guess we need a new
3289 ECF flag even weaker than ECF_PURE. FIXME! */
3290 DECL_PURE_P (fn) = true;
3291 #endif
3293 DECL_BEFRIENDING_CLASSES (fn) = var;
3295 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3297 return fn;
3300 /* At EOF, generate the definition for the TLS wrapper function FN:
3302 T& var_wrapper() {
3303 if (init_fn) init_fn();
3304 return var;
3305 } */
3307 static void
3308 generate_tls_wrapper (tree fn)
3310 tree var = DECL_BEFRIENDING_CLASSES (fn);
3312 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3313 tree body = begin_function_body ();
3314 /* Only call the init fn if there might be one. */
3315 if (tree init_fn = get_tls_init_fn (var))
3317 tree if_stmt = NULL_TREE;
3318 /* If init_fn is a weakref, make sure it exists before calling. */
3319 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3321 if_stmt = begin_if_stmt ();
3322 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3323 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3324 NE_EXPR, addr, nullptr_node,
3325 tf_warning_or_error);
3326 finish_if_stmt_cond (cond, if_stmt);
3328 finish_expr_stmt (build_cxx_call
3329 (init_fn, 0, NULL, tf_warning_or_error));
3330 if (if_stmt)
3332 finish_then_clause (if_stmt);
3333 finish_if_stmt (if_stmt);
3336 else
3337 /* If there's no initialization, the wrapper is a constant function. */
3338 TREE_READONLY (fn) = true;
3339 finish_return_stmt (convert_from_reference (var));
3340 finish_function_body (body);
3341 expand_or_defer_fn (finish_function (0));
3344 /* Start the process of running a particular set of global constructors
3345 or destructors. Subroutine of do_[cd]tors. Also called from
3346 vtv_start_verification_constructor_init_function. */
3348 static tree
3349 start_objects (int method_type, int initp)
3351 tree body;
3352 tree fndecl;
3353 char type[14];
3355 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3357 if (initp != DEFAULT_INIT_PRIORITY)
3359 char joiner;
3361 #ifdef JOINER
3362 joiner = JOINER;
3363 #else
3364 joiner = '_';
3365 #endif
3367 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3369 else
3370 sprintf (type, "sub_%c", method_type);
3372 fndecl = build_lang_decl (FUNCTION_DECL,
3373 get_file_function_name (type),
3374 build_function_type_list (void_type_node,
3375 NULL_TREE));
3376 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3378 TREE_PUBLIC (current_function_decl) = 0;
3380 /* Mark as artificial because it's not explicitly in the user's
3381 source code. */
3382 DECL_ARTIFICIAL (current_function_decl) = 1;
3384 /* Mark this declaration as used to avoid spurious warnings. */
3385 TREE_USED (current_function_decl) = 1;
3387 /* Mark this function as a global constructor or destructor. */
3388 if (method_type == 'I')
3389 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3390 else
3391 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3393 body = begin_compound_stmt (BCS_FN_BODY);
3395 return body;
3398 /* Finish the process of running a particular set of global constructors
3399 or destructors. Subroutine of do_[cd]tors. */
3401 static void
3402 finish_objects (int method_type, int initp, tree body)
3404 tree fn;
3406 /* Finish up. */
3407 finish_compound_stmt (body);
3408 fn = finish_function (0);
3410 if (method_type == 'I')
3412 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3413 decl_init_priority_insert (fn, initp);
3415 else
3417 DECL_STATIC_DESTRUCTOR (fn) = 1;
3418 decl_fini_priority_insert (fn, initp);
3421 expand_or_defer_fn (fn);
3424 /* The names of the parameters to the function created to handle
3425 initializations and destructions for objects with static storage
3426 duration. */
3427 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3428 #define PRIORITY_IDENTIFIER "__priority"
3430 /* The name of the function we create to handle initializations and
3431 destructions for objects with static storage duration. */
3432 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3434 /* The declaration for the __INITIALIZE_P argument. */
3435 static GTY(()) tree initialize_p_decl;
3437 /* The declaration for the __PRIORITY argument. */
3438 static GTY(()) tree priority_decl;
3440 /* The declaration for the static storage duration function. */
3441 static GTY(()) tree ssdf_decl;
3443 /* All the static storage duration functions created in this
3444 translation unit. */
3445 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3447 /* A map from priority levels to information about that priority
3448 level. There may be many such levels, so efficient lookup is
3449 important. */
3450 static splay_tree priority_info_map;
3452 /* Begins the generation of the function that will handle all
3453 initialization and destruction of objects with static storage
3454 duration. The function generated takes two parameters of type
3455 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3456 nonzero, it performs initializations. Otherwise, it performs
3457 destructions. It only performs those initializations or
3458 destructions with the indicated __PRIORITY. The generated function
3459 returns no value.
3461 It is assumed that this function will only be called once per
3462 translation unit. */
3464 static tree
3465 start_static_storage_duration_function (unsigned count)
3467 tree type;
3468 tree body;
3469 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3471 /* Create the identifier for this function. It will be of the form
3472 SSDF_IDENTIFIER_<number>. */
3473 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3475 type = build_function_type_list (void_type_node,
3476 integer_type_node, integer_type_node,
3477 NULL_TREE);
3479 /* Create the FUNCTION_DECL itself. */
3480 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3481 get_identifier (id),
3482 type);
3483 TREE_PUBLIC (ssdf_decl) = 0;
3484 DECL_ARTIFICIAL (ssdf_decl) = 1;
3486 /* Put this function in the list of functions to be called from the
3487 static constructors and destructors. */
3488 if (!ssdf_decls)
3490 vec_alloc (ssdf_decls, 32);
3492 /* Take this opportunity to initialize the map from priority
3493 numbers to information about that priority level. */
3494 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3495 /*delete_key_fn=*/0,
3496 /*delete_value_fn=*/
3497 (splay_tree_delete_value_fn) &free);
3499 /* We always need to generate functions for the
3500 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3501 priorities later, we'll be sure to find the
3502 DEFAULT_INIT_PRIORITY. */
3503 get_priority_info (DEFAULT_INIT_PRIORITY);
3506 vec_safe_push (ssdf_decls, ssdf_decl);
3508 /* Create the argument list. */
3509 initialize_p_decl = cp_build_parm_decl
3510 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3511 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
3512 TREE_USED (initialize_p_decl) = 1;
3513 priority_decl = cp_build_parm_decl
3514 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3515 DECL_CONTEXT (priority_decl) = ssdf_decl;
3516 TREE_USED (priority_decl) = 1;
3518 DECL_CHAIN (initialize_p_decl) = priority_decl;
3519 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3521 /* Put the function in the global scope. */
3522 pushdecl (ssdf_decl);
3524 /* Start the function itself. This is equivalent to declaring the
3525 function as:
3527 static void __ssdf (int __initialize_p, init __priority_p);
3529 It is static because we only need to call this function from the
3530 various constructor and destructor functions for this module. */
3531 start_preparsed_function (ssdf_decl,
3532 /*attrs=*/NULL_TREE,
3533 SF_PRE_PARSED);
3535 /* Set up the scope of the outermost block in the function. */
3536 body = begin_compound_stmt (BCS_FN_BODY);
3538 return body;
3541 /* Finish the generation of the function which performs initialization
3542 and destruction of objects with static storage duration. After
3543 this point, no more such objects can be created. */
3545 static void
3546 finish_static_storage_duration_function (tree body)
3548 /* Close out the function. */
3549 finish_compound_stmt (body);
3550 expand_or_defer_fn (finish_function (0));
3553 /* Return the information about the indicated PRIORITY level. If no
3554 code to handle this level has yet been generated, generate the
3555 appropriate prologue. */
3557 static priority_info
3558 get_priority_info (int priority)
3560 priority_info pi;
3561 splay_tree_node n;
3563 n = splay_tree_lookup (priority_info_map,
3564 (splay_tree_key) priority);
3565 if (!n)
3567 /* Create a new priority information structure, and insert it
3568 into the map. */
3569 pi = XNEW (struct priority_info_s);
3570 pi->initializations_p = 0;
3571 pi->destructions_p = 0;
3572 splay_tree_insert (priority_info_map,
3573 (splay_tree_key) priority,
3574 (splay_tree_value) pi);
3576 else
3577 pi = (priority_info) n->value;
3579 return pi;
3582 /* The effective initialization priority of a DECL. */
3584 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3585 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3586 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3588 /* Whether a DECL needs a guard to protect it against multiple
3589 initialization. */
3591 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3592 || DECL_ONE_ONLY (decl) \
3593 || DECL_WEAK (decl)))
3595 /* Called from one_static_initialization_or_destruction(),
3596 via walk_tree.
3597 Walks the initializer list of a global variable and looks for
3598 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3599 and that have their DECL_CONTEXT() == NULL.
3600 For each such temporary variable, set their DECL_CONTEXT() to
3601 the current function. This is necessary because otherwise
3602 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3603 when trying to refer to a temporary variable that does not have
3604 it's DECL_CONTECT() properly set. */
3605 static tree
3606 fix_temporary_vars_context_r (tree *node,
3607 int * /*unused*/,
3608 void * /*unused1*/)
3610 gcc_assert (current_function_decl);
3612 if (TREE_CODE (*node) == BIND_EXPR)
3614 tree var;
3616 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3617 if (VAR_P (var)
3618 && !DECL_NAME (var)
3619 && DECL_ARTIFICIAL (var)
3620 && !DECL_CONTEXT (var))
3621 DECL_CONTEXT (var) = current_function_decl;
3624 return NULL_TREE;
3627 /* Set up to handle the initialization or destruction of DECL. If
3628 INITP is nonzero, we are initializing the variable. Otherwise, we
3629 are destroying it. */
3631 static void
3632 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3634 tree guard_if_stmt = NULL_TREE;
3635 tree guard;
3637 /* If we are supposed to destruct and there's a trivial destructor,
3638 nothing has to be done. */
3639 if (!initp
3640 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3641 return;
3643 /* Trick the compiler into thinking we are at the file and line
3644 where DECL was declared so that error-messages make sense, and so
3645 that the debugger will show somewhat sensible file and line
3646 information. */
3647 input_location = DECL_SOURCE_LOCATION (decl);
3649 /* Make sure temporary variables in the initialiser all have
3650 their DECL_CONTEXT() set to a value different from NULL_TREE.
3651 This can happen when global variables initialisers are built.
3652 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3653 the temporary variables that might have been generated in the
3654 accompagning initialisers is NULL_TREE, meaning the variables have been
3655 declared in the global namespace.
3656 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3657 of the temporaries are set to the current function decl. */
3658 cp_walk_tree_without_duplicates (&init,
3659 fix_temporary_vars_context_r,
3660 NULL);
3662 /* Because of:
3664 [class.access.spec]
3666 Access control for implicit calls to the constructors,
3667 the conversion functions, or the destructor called to
3668 create and destroy a static data member is performed as
3669 if these calls appeared in the scope of the member's
3670 class.
3672 we pretend we are in a static member function of the class of
3673 which the DECL is a member. */
3674 if (member_p (decl))
3676 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3677 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3680 /* Assume we don't need a guard. */
3681 guard = NULL_TREE;
3682 /* We need a guard if this is an object with external linkage that
3683 might be initialized in more than one place. (For example, a
3684 static data member of a template, when the data member requires
3685 construction.) */
3686 if (NEEDS_GUARD_P (decl))
3688 tree guard_cond;
3690 guard = get_guard (decl);
3692 /* When using __cxa_atexit, we just check the GUARD as we would
3693 for a local static. */
3694 if (flag_use_cxa_atexit)
3696 /* When using __cxa_atexit, we never try to destroy
3697 anything from a static destructor. */
3698 gcc_assert (initp);
3699 guard_cond = get_guard_cond (guard, false);
3701 /* If we don't have __cxa_atexit, then we will be running
3702 destructors from .fini sections, or their equivalents. So,
3703 we need to know how many times we've tried to initialize this
3704 object. We do initializations only if the GUARD is zero,
3705 i.e., if we are the first to initialize the variable. We do
3706 destructions only if the GUARD is one, i.e., if we are the
3707 last to destroy the variable. */
3708 else if (initp)
3709 guard_cond
3710 = cp_build_binary_op (input_location,
3711 EQ_EXPR,
3712 cp_build_unary_op (PREINCREMENT_EXPR,
3713 guard,
3714 /*noconvert=*/true,
3715 tf_warning_or_error),
3716 integer_one_node,
3717 tf_warning_or_error);
3718 else
3719 guard_cond
3720 = cp_build_binary_op (input_location,
3721 EQ_EXPR,
3722 cp_build_unary_op (PREDECREMENT_EXPR,
3723 guard,
3724 /*noconvert=*/true,
3725 tf_warning_or_error),
3726 integer_zero_node,
3727 tf_warning_or_error);
3729 guard_if_stmt = begin_if_stmt ();
3730 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3734 /* If we're using __cxa_atexit, we have not already set the GUARD,
3735 so we must do so now. */
3736 if (guard && initp && flag_use_cxa_atexit)
3737 finish_expr_stmt (set_guard (guard));
3739 /* Perform the initialization or destruction. */
3740 if (initp)
3742 if (init)
3744 finish_expr_stmt (init);
3745 if (flag_sanitize & SANITIZE_ADDRESS)
3747 varpool_node *vnode = varpool_node::get (decl);
3748 if (vnode)
3749 vnode->dynamically_initialized = 1;
3753 /* If we're using __cxa_atexit, register a function that calls the
3754 destructor for the object. */
3755 if (flag_use_cxa_atexit)
3756 finish_expr_stmt (register_dtor_fn (decl));
3758 else
3759 finish_expr_stmt (build_cleanup (decl));
3761 /* Finish the guard if-stmt, if necessary. */
3762 if (guard)
3764 finish_then_clause (guard_if_stmt);
3765 finish_if_stmt (guard_if_stmt);
3768 /* Now that we're done with DECL we don't need to pretend to be a
3769 member of its class any longer. */
3770 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3771 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3774 /* Generate code to do the initialization or destruction of the decls in VARS,
3775 a TREE_LIST of VAR_DECL with static storage duration.
3776 Whether initialization or destruction is performed is specified by INITP. */
3778 static void
3779 do_static_initialization_or_destruction (tree vars, bool initp)
3781 tree node, init_if_stmt, cond;
3783 /* Build the outer if-stmt to check for initialization or destruction. */
3784 init_if_stmt = begin_if_stmt ();
3785 cond = initp ? integer_one_node : integer_zero_node;
3786 cond = cp_build_binary_op (input_location,
3787 EQ_EXPR,
3788 initialize_p_decl,
3789 cond,
3790 tf_warning_or_error);
3791 finish_if_stmt_cond (cond, init_if_stmt);
3793 /* To make sure dynamic construction doesn't access globals from other
3794 compilation units where they might not be yet constructed, for
3795 -fsanitize=address insert __asan_before_dynamic_init call that
3796 prevents access to either all global variables that need construction
3797 in other compilation units, or at least those that haven't been
3798 initialized yet. Variables that need dynamic construction in
3799 the current compilation unit are kept accessible. */
3800 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
3801 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3803 node = vars;
3804 do {
3805 tree decl = TREE_VALUE (node);
3806 tree priority_if_stmt;
3807 int priority;
3808 priority_info pi;
3810 /* If we don't need a destructor, there's nothing to do. Avoid
3811 creating a possibly empty if-stmt. */
3812 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3814 node = TREE_CHAIN (node);
3815 continue;
3818 /* Remember that we had an initialization or finalization at this
3819 priority. */
3820 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3821 pi = get_priority_info (priority);
3822 if (initp)
3823 pi->initializations_p = 1;
3824 else
3825 pi->destructions_p = 1;
3827 /* Conditionalize this initialization on being in the right priority
3828 and being initializing/finalizing appropriately. */
3829 priority_if_stmt = begin_if_stmt ();
3830 cond = cp_build_binary_op (input_location,
3831 EQ_EXPR,
3832 priority_decl,
3833 build_int_cst (NULL_TREE, priority),
3834 tf_warning_or_error);
3835 finish_if_stmt_cond (cond, priority_if_stmt);
3837 /* Process initializers with same priority. */
3838 for (; node
3839 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3840 node = TREE_CHAIN (node))
3841 /* Do one initialization or destruction. */
3842 one_static_initialization_or_destruction (TREE_VALUE (node),
3843 TREE_PURPOSE (node), initp);
3845 /* Finish up the priority if-stmt body. */
3846 finish_then_clause (priority_if_stmt);
3847 finish_if_stmt (priority_if_stmt);
3849 } while (node);
3851 /* Revert what __asan_before_dynamic_init did by calling
3852 __asan_after_dynamic_init. */
3853 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
3854 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3856 /* Finish up the init/destruct if-stmt body. */
3857 finish_then_clause (init_if_stmt);
3858 finish_if_stmt (init_if_stmt);
3861 /* VARS is a list of variables with static storage duration which may
3862 need initialization and/or finalization. Remove those variables
3863 that don't really need to be initialized or finalized, and return
3864 the resulting list. The order in which the variables appear in
3865 VARS is in reverse order of the order in which they should actually
3866 be initialized. The list we return is in the unreversed order;
3867 i.e., the first variable should be initialized first. */
3869 static tree
3870 prune_vars_needing_no_initialization (tree *vars)
3872 tree *var = vars;
3873 tree result = NULL_TREE;
3875 while (*var)
3877 tree t = *var;
3878 tree decl = TREE_VALUE (t);
3879 tree init = TREE_PURPOSE (t);
3881 /* Deal gracefully with error. */
3882 if (error_operand_p (decl))
3884 var = &TREE_CHAIN (t);
3885 continue;
3888 /* The only things that can be initialized are variables. */
3889 gcc_assert (VAR_P (decl));
3891 /* If this object is not defined, we don't need to do anything
3892 here. */
3893 if (DECL_EXTERNAL (decl))
3895 var = &TREE_CHAIN (t);
3896 continue;
3899 /* Also, if the initializer already contains errors, we can bail
3900 out now. */
3901 if (init && TREE_CODE (init) == TREE_LIST
3902 && value_member (error_mark_node, init))
3904 var = &TREE_CHAIN (t);
3905 continue;
3908 /* This variable is going to need initialization and/or
3909 finalization, so we add it to the list. */
3910 *var = TREE_CHAIN (t);
3911 TREE_CHAIN (t) = result;
3912 result = t;
3915 return result;
3918 /* Make sure we have told the back end about all the variables in
3919 VARS. */
3921 static void
3922 write_out_vars (tree vars)
3924 tree v;
3926 for (v = vars; v; v = TREE_CHAIN (v))
3928 tree var = TREE_VALUE (v);
3929 if (!var_finalized_p (var))
3931 import_export_decl (var);
3932 rest_of_decl_compilation (var, 1, 1);
3937 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3938 (otherwise) that will initialize all global objects with static
3939 storage duration having the indicated PRIORITY. */
3941 static void
3942 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3943 location_t *locus)
3945 char function_key;
3946 tree fndecl;
3947 tree body;
3948 size_t i;
3950 input_location = *locus;
3951 /* ??? */
3952 /* Was: locus->line++; */
3954 /* We use `I' to indicate initialization and `D' to indicate
3955 destruction. */
3956 function_key = constructor_p ? 'I' : 'D';
3958 /* We emit the function lazily, to avoid generating empty
3959 global constructors and destructors. */
3960 body = NULL_TREE;
3962 /* For Objective-C++, we may need to initialize metadata found in this module.
3963 This must be done _before_ any other static initializations. */
3964 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3965 && constructor_p && objc_static_init_needed_p ())
3967 body = start_objects (function_key, priority);
3968 objc_generate_static_init_call (NULL_TREE);
3971 /* Call the static storage duration function with appropriate
3972 arguments. */
3973 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
3975 /* Calls to pure or const functions will expand to nothing. */
3976 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3978 tree call;
3980 if (! body)
3981 body = start_objects (function_key, priority);
3983 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
3984 build_int_cst (NULL_TREE,
3985 constructor_p),
3986 build_int_cst (NULL_TREE,
3987 priority),
3988 NULL_TREE);
3989 finish_expr_stmt (call);
3993 /* Close out the function. */
3994 if (body)
3995 finish_objects (function_key, priority, body);
3998 /* Generate constructor and destructor functions for the priority
3999 indicated by N. */
4001 static int
4002 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
4004 location_t *locus = (location_t *) data;
4005 int priority = (int) n->key;
4006 priority_info pi = (priority_info) n->value;
4008 /* Generate the functions themselves, but only if they are really
4009 needed. */
4010 if (pi->initializations_p)
4011 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
4012 if (pi->destructions_p)
4013 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
4015 /* Keep iterating. */
4016 return 0;
4019 /* Return C++ property of T, based on given operation OP. */
4021 static int
4022 cpp_check (tree t, cpp_operation op)
4024 switch (op)
4026 case HAS_DEPENDENT_TEMPLATE_ARGS:
4028 tree ti = CLASSTYPE_TEMPLATE_INFO (t);
4029 if (!ti)
4030 return 0;
4031 ++processing_template_decl;
4032 const bool dep = any_dependent_template_arguments_p (TI_ARGS (ti));
4033 --processing_template_decl;
4034 return dep;
4036 case IS_ABSTRACT:
4037 return DECL_PURE_VIRTUAL_P (t);
4038 case IS_CONSTRUCTOR:
4039 return DECL_CONSTRUCTOR_P (t);
4040 case IS_DESTRUCTOR:
4041 return DECL_DESTRUCTOR_P (t);
4042 case IS_COPY_CONSTRUCTOR:
4043 return DECL_COPY_CONSTRUCTOR_P (t);
4044 case IS_MOVE_CONSTRUCTOR:
4045 return DECL_MOVE_CONSTRUCTOR_P (t);
4046 case IS_TEMPLATE:
4047 return TREE_CODE (t) == TEMPLATE_DECL;
4048 case IS_TRIVIAL:
4049 return trivial_type_p (t);
4050 default:
4051 return 0;
4055 /* Collect source file references recursively, starting from NAMESPC. */
4057 static void
4058 collect_source_refs (tree namespc)
4060 tree t;
4062 if (!namespc)
4063 return;
4065 /* Iterate over names in this name space. */
4066 for (t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4067 if (!DECL_IS_BUILTIN (t) )
4068 collect_source_ref (DECL_SOURCE_FILE (t));
4070 /* Dump siblings, if any */
4071 collect_source_refs (TREE_CHAIN (namespc));
4073 /* Dump children, if any */
4074 collect_source_refs (NAMESPACE_LEVEL (namespc)->namespaces);
4077 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4078 starting from NAMESPC. */
4080 static void
4081 collect_ada_namespace (tree namespc, const char *source_file)
4083 if (!namespc)
4084 return;
4086 /* Collect decls from this namespace */
4087 collect_ada_nodes (NAMESPACE_LEVEL (namespc)->names, source_file);
4089 /* Collect siblings, if any */
4090 collect_ada_namespace (TREE_CHAIN (namespc), source_file);
4092 /* Collect children, if any */
4093 collect_ada_namespace (NAMESPACE_LEVEL (namespc)->namespaces, source_file);
4096 /* Returns true iff there is a definition available for variable or
4097 function DECL. */
4099 bool
4100 decl_defined_p (tree decl)
4102 if (TREE_CODE (decl) == FUNCTION_DECL)
4103 return (DECL_INITIAL (decl) != NULL_TREE
4104 /* A pending instantiation of a friend temploid is defined. */
4105 || (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
4106 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4107 (DECL_TI_TEMPLATE (decl)))));
4108 else
4110 gcc_assert (VAR_P (decl));
4111 return !DECL_EXTERNAL (decl);
4115 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4117 [expr.const]
4119 An integral constant-expression can only involve ... const
4120 variables of integral or enumeration types initialized with
4121 constant expressions ...
4123 C++0x also allows constexpr variables and temporaries initialized
4124 with constant expressions. We handle the former here, but the latter
4125 are just folded away in cxx_eval_constant_expression.
4127 The standard does not require that the expression be non-volatile.
4128 G++ implements the proposed correction in DR 457. */
4130 bool
4131 decl_constant_var_p (tree decl)
4133 if (!decl_maybe_constant_var_p (decl))
4134 return false;
4136 /* We don't know if a template static data member is initialized with
4137 a constant expression until we instantiate its initializer. Even
4138 in the case of a constexpr variable, we can't treat it as a
4139 constant until its initializer is complete in case it's used in
4140 its own initializer. */
4141 maybe_instantiate_decl (decl);
4142 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4145 /* Returns true if DECL could be a symbolic constant variable, depending on
4146 its initializer. */
4148 bool
4149 decl_maybe_constant_var_p (tree decl)
4151 tree type = TREE_TYPE (decl);
4152 if (!VAR_P (decl))
4153 return false;
4154 if (DECL_DECLARED_CONSTEXPR_P (decl))
4155 return true;
4156 if (DECL_HAS_VALUE_EXPR_P (decl))
4157 /* A proxy isn't constant. */
4158 return false;
4159 if (TREE_CODE (type) == REFERENCE_TYPE)
4160 /* References can be constant. */
4161 return true;
4162 return (CP_TYPE_CONST_NON_VOLATILE_P (type)
4163 && INTEGRAL_OR_ENUMERATION_TYPE_P (type));
4166 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4167 called from grokfndecl and grokvardecl; in all modes it is called from
4168 cp_write_global_declarations. */
4170 void
4171 no_linkage_error (tree decl)
4173 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4174 /* In C++11 it's ok if the decl is defined. */
4175 return;
4176 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4177 if (t == NULL_TREE)
4178 /* The type that got us on no_linkage_decls must have gotten a name for
4179 linkage purposes. */;
4180 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4181 /* The type might end up having a typedef name for linkage purposes. */
4182 vec_safe_push (no_linkage_decls, decl);
4183 else if (TYPE_UNNAMED_P (t))
4185 bool d = false;
4186 if (cxx_dialect >= cxx11)
4187 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4188 "unnamed type, is used but never defined", decl);
4189 else if (DECL_EXTERN_C_P (decl))
4190 /* Allow this; it's pretty common in C. */;
4191 else if (VAR_P (decl))
4192 /* DRs 132, 319 and 389 seem to indicate types with
4193 no linkage can only be used to declare extern "C"
4194 entities. Since it's not always an error in the
4195 ISO C++ 90 Standard, we only issue a warning. */
4196 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "unnamed type "
4197 "with no linkage used to declare variable %q#D with "
4198 "linkage", decl);
4199 else
4200 d = permerror (DECL_SOURCE_LOCATION (decl), "unnamed type with no "
4201 "linkage used to declare function %q#D with linkage",
4202 decl);
4203 if (d && is_typedef_decl (TYPE_NAME (t)))
4204 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4205 "to the unqualified type, so it is not used for linkage",
4206 TYPE_NAME (t));
4208 else if (cxx_dialect >= cxx11)
4210 if (VAR_P (decl) || !DECL_PURE_VIRTUAL_P (decl))
4211 permerror (DECL_SOURCE_LOCATION (decl),
4212 "%q#D, declared using local type "
4213 "%qT, is used but never defined", decl, t);
4215 else if (VAR_P (decl))
4216 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4217 "used to declare variable %q#D with linkage", t, decl);
4218 else
4219 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4220 "to declare function %q#D with linkage", t, decl);
4223 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4225 static void
4226 collect_all_refs (const char *source_file)
4228 collect_ada_namespace (global_namespace, source_file);
4231 /* Clear DECL_EXTERNAL for NODE. */
4233 static bool
4234 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4236 DECL_EXTERNAL (node->decl) = 0;
4237 return false;
4240 /* Build up the function to run dynamic initializers for thread_local
4241 variables in this translation unit and alias the init functions for the
4242 individual variables to it. */
4244 static void
4245 handle_tls_init (void)
4247 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4248 if (vars == NULL_TREE)
4249 return;
4251 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4253 write_out_vars (vars);
4255 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4256 boolean_type_node);
4257 TREE_PUBLIC (guard) = false;
4258 TREE_STATIC (guard) = true;
4259 DECL_ARTIFICIAL (guard) = true;
4260 DECL_IGNORED_P (guard) = true;
4261 TREE_USED (guard) = true;
4262 CP_DECL_THREAD_LOCAL_P (guard) = true;
4263 set_decl_tls_model (guard, decl_default_tls_model (guard));
4264 pushdecl_top_level_and_finish (guard, NULL_TREE);
4266 tree fn = get_local_tls_init_fn ();
4267 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4268 tree body = begin_function_body ();
4269 tree if_stmt = begin_if_stmt ();
4270 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4271 tf_warning_or_error);
4272 finish_if_stmt_cond (cond, if_stmt);
4273 finish_expr_stmt (cp_build_modify_expr (loc, guard, NOP_EXPR,
4274 boolean_true_node,
4275 tf_warning_or_error));
4276 for (; vars; vars = TREE_CHAIN (vars))
4278 tree var = TREE_VALUE (vars);
4279 tree init = TREE_PURPOSE (vars);
4280 one_static_initialization_or_destruction (var, init, true);
4282 #ifdef ASM_OUTPUT_DEF
4283 /* Output init aliases even with -fno-extern-tls-init. */
4284 if (TREE_PUBLIC (var))
4286 tree single_init_fn = get_tls_init_fn (var);
4287 if (single_init_fn == NULL_TREE)
4288 continue;
4289 cgraph_node *alias
4290 = cgraph_node::get_create (fn)->create_same_body_alias
4291 (single_init_fn, fn);
4292 gcc_assert (alias != NULL);
4294 #endif
4297 finish_then_clause (if_stmt);
4298 finish_if_stmt (if_stmt);
4299 finish_function_body (body);
4300 expand_or_defer_fn (finish_function (0));
4303 /* We're at the end of compilation, so generate any mangling aliases that
4304 we've been saving up, if DECL is going to be output and ID2 isn't
4305 already taken by another declaration. */
4307 static void
4308 generate_mangling_alias (tree decl, tree id2)
4310 /* If there's a declaration already using this mangled name,
4311 don't create a compatibility alias that conflicts. */
4312 if (IDENTIFIER_GLOBAL_VALUE (id2))
4313 return;
4315 struct cgraph_node *n = NULL;
4316 if (TREE_CODE (decl) == FUNCTION_DECL
4317 && !(n = cgraph_node::get (decl)))
4318 /* Don't create an alias to an unreferenced function. */
4319 return;
4321 tree alias = make_alias_for (decl, id2);
4322 SET_IDENTIFIER_GLOBAL_VALUE (id2, alias);
4323 DECL_IGNORED_P (alias) = 1;
4324 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
4325 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
4326 if (vague_linkage_p (decl))
4327 DECL_WEAK (alias) = 1;
4328 if (TREE_CODE (decl) == FUNCTION_DECL)
4329 n->create_same_body_alias (alias, decl);
4330 else
4331 varpool_node::create_extra_name_alias (alias, decl);
4334 /* Note that we might want to emit an alias with the symbol ID2 for DECL at
4335 the end of translation, for compatibility across bugs in the mangling
4336 implementation. */
4338 void
4339 note_mangling_alias (tree decl ATTRIBUTE_UNUSED, tree id2 ATTRIBUTE_UNUSED)
4341 #ifdef ASM_OUTPUT_DEF
4342 if (!defer_mangling_aliases)
4343 generate_mangling_alias (decl, id2);
4344 else
4346 vec_safe_push (mangling_aliases, decl);
4347 vec_safe_push (mangling_aliases, id2);
4349 #endif
4352 /* Emit all mangling aliases that were deferred up to this point. */
4354 void
4355 generate_mangling_aliases ()
4357 while (!vec_safe_is_empty (mangling_aliases))
4359 tree id2 = mangling_aliases->pop();
4360 tree decl = mangling_aliases->pop();
4361 generate_mangling_alias (decl, id2);
4363 defer_mangling_aliases = false;
4366 /* The entire file is now complete. If requested, dump everything
4367 to a file. */
4369 static void
4370 dump_tu (void)
4372 int flags;
4373 FILE *stream = dump_begin (TDI_tu, &flags);
4375 if (stream)
4377 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4378 dump_end (TDI_tu, stream);
4382 static location_t locus_at_end_of_parsing;
4384 /* Check the deallocation functions for CODE to see if we want to warn that
4385 only one was defined. */
4387 static void
4388 maybe_warn_sized_delete (enum tree_code code)
4390 tree sized = NULL_TREE;
4391 tree unsized = NULL_TREE;
4393 for (tree ovl = IDENTIFIER_GLOBAL_VALUE (cp_operator_id (code));
4394 ovl; ovl = OVL_NEXT (ovl))
4396 tree fn = OVL_CURRENT (ovl);
4397 /* We're only interested in usual deallocation functions. */
4398 if (!usual_deallocation_fn_p (fn))
4399 continue;
4400 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
4401 unsized = fn;
4402 else
4403 sized = fn;
4405 if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
4406 warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
4407 "the program should also define %qD", sized);
4408 else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
4409 warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
4410 "the program should also define %qD", unsized);
4413 /* Check the global deallocation functions to see if we want to warn about
4414 defining unsized without sized (or vice versa). */
4416 static void
4417 maybe_warn_sized_delete ()
4419 if (!flag_sized_deallocation || !warn_sized_deallocation)
4420 return;
4421 maybe_warn_sized_delete (DELETE_EXPR);
4422 maybe_warn_sized_delete (VEC_DELETE_EXPR);
4425 /* Earlier we left PTRMEM_CST in variable initializers alone so that we could
4426 look them up when evaluating non-type template parameters. Now we need to
4427 lower them to something the back end can understand. */
4429 static void
4430 lower_var_init ()
4432 varpool_node *node;
4433 FOR_EACH_VARIABLE (node)
4435 tree d = node->decl;
4436 if (tree init = DECL_INITIAL (d))
4437 DECL_INITIAL (d) = cplus_expand_constant (init);
4441 /* This routine is called at the end of compilation.
4442 Its job is to create all the code needed to initialize and
4443 destroy the global aggregates. We do the destruction
4444 first, since that way we only need to reverse the decls once. */
4446 void
4447 c_parse_final_cleanups (void)
4449 tree vars;
4450 bool reconsider;
4451 size_t i;
4452 unsigned ssdf_count = 0;
4453 int retries = 0;
4454 tree decl;
4456 locus_at_end_of_parsing = input_location;
4457 at_eof = 1;
4459 /* Bad parse errors. Just forget about it. */
4460 if (! global_bindings_p () || current_class_type
4461 || !vec_safe_is_empty (decl_namespace_list))
4462 return;
4464 /* This is the point to write out a PCH if we're doing that.
4465 In that case we do not want to do anything else. */
4466 if (pch_file)
4468 /* Mangle all symbols at PCH creation time. */
4469 symtab_node *node;
4470 FOR_EACH_SYMBOL (node)
4471 if (! is_a <varpool_node *> (node)
4472 || ! DECL_HARD_REGISTER (node->decl))
4473 DECL_ASSEMBLER_NAME (node->decl);
4474 c_common_write_pch ();
4475 dump_tu ();
4476 /* Ensure even the callers don't try to finalize the CU. */
4477 flag_syntax_only = 1;
4478 return;
4481 timevar_stop (TV_PHASE_PARSING);
4482 timevar_start (TV_PHASE_DEFERRED);
4484 symtab->process_same_body_aliases ();
4486 /* Handle -fdump-ada-spec[-slim] */
4487 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4489 if (flag_dump_ada_spec_slim)
4490 collect_source_ref (main_input_filename);
4491 else
4492 collect_source_refs (global_namespace);
4494 dump_ada_specs (collect_all_refs, cpp_check);
4497 /* FIXME - huh? was input_line -= 1;*/
4499 /* We now have to write out all the stuff we put off writing out.
4500 These include:
4502 o Template specializations that we have not yet instantiated,
4503 but which are needed.
4504 o Initialization and destruction for non-local objects with
4505 static storage duration. (Local objects with static storage
4506 duration are initialized when their scope is first entered,
4507 and are cleaned up via atexit.)
4508 o Virtual function tables.
4510 All of these may cause others to be needed. For example,
4511 instantiating one function may cause another to be needed, and
4512 generating the initializer for an object may cause templates to be
4513 instantiated, etc., etc. */
4515 emit_support_tinfos ();
4519 tree t;
4520 tree decl;
4522 reconsider = false;
4524 /* If there are templates that we've put off instantiating, do
4525 them now. */
4526 instantiate_pending_templates (retries);
4527 ggc_collect ();
4529 /* Write out virtual tables as required. Note that writing out
4530 the virtual table for a template class may cause the
4531 instantiation of members of that class. If we write out
4532 vtables then we remove the class from our list so we don't
4533 have to look at it again. */
4535 while (keyed_classes != NULL_TREE
4536 && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
4538 reconsider = true;
4539 keyed_classes = TREE_CHAIN (keyed_classes);
4542 t = keyed_classes;
4543 if (t != NULL_TREE)
4545 tree next = TREE_CHAIN (t);
4547 while (next)
4549 if (maybe_emit_vtables (TREE_VALUE (next)))
4551 reconsider = true;
4552 TREE_CHAIN (t) = TREE_CHAIN (next);
4554 else
4555 t = next;
4557 next = TREE_CHAIN (t);
4561 /* Write out needed type info variables. We have to be careful
4562 looping through unemitted decls, because emit_tinfo_decl may
4563 cause other variables to be needed. New elements will be
4564 appended, and we remove from the vector those that actually
4565 get emitted. */
4566 for (i = unemitted_tinfo_decls->length ();
4567 unemitted_tinfo_decls->iterate (--i, &t);)
4568 if (emit_tinfo_decl (t))
4570 reconsider = true;
4571 unemitted_tinfo_decls->unordered_remove (i);
4574 /* The list of objects with static storage duration is built up
4575 in reverse order. We clear STATIC_AGGREGATES so that any new
4576 aggregates added during the initialization of these will be
4577 initialized in the correct order when we next come around the
4578 loop. */
4579 vars = prune_vars_needing_no_initialization (&static_aggregates);
4581 if (vars)
4583 /* We need to start a new initialization function each time
4584 through the loop. That's because we need to know which
4585 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4586 isn't computed until a function is finished, and written
4587 out. That's a deficiency in the back end. When this is
4588 fixed, these initialization functions could all become
4589 inline, with resulting performance improvements. */
4590 tree ssdf_body;
4592 /* Set the line and file, so that it is obviously not from
4593 the source file. */
4594 input_location = locus_at_end_of_parsing;
4595 ssdf_body = start_static_storage_duration_function (ssdf_count);
4597 /* Make sure the back end knows about all the variables. */
4598 write_out_vars (vars);
4600 /* First generate code to do all the initializations. */
4601 if (vars)
4602 do_static_initialization_or_destruction (vars, /*initp=*/true);
4604 /* Then, generate code to do all the destructions. Do these
4605 in reverse order so that the most recently constructed
4606 variable is the first destroyed. If we're using
4607 __cxa_atexit, then we don't need to do this; functions
4608 were registered at initialization time to destroy the
4609 local statics. */
4610 if (!flag_use_cxa_atexit && vars)
4612 vars = nreverse (vars);
4613 do_static_initialization_or_destruction (vars, /*initp=*/false);
4615 else
4616 vars = NULL_TREE;
4618 /* Finish up the static storage duration function for this
4619 round. */
4620 input_location = locus_at_end_of_parsing;
4621 finish_static_storage_duration_function (ssdf_body);
4623 /* All those initializations and finalizations might cause
4624 us to need more inline functions, more template
4625 instantiations, etc. */
4626 reconsider = true;
4627 ssdf_count++;
4628 /* ??? was: locus_at_end_of_parsing.line++; */
4631 /* Now do the same for thread_local variables. */
4632 handle_tls_init ();
4634 /* Go through the set of inline functions whose bodies have not
4635 been emitted yet. If out-of-line copies of these functions
4636 are required, emit them. */
4637 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4639 /* Does it need synthesizing? */
4640 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4641 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4643 /* Even though we're already at the top-level, we push
4644 there again. That way, when we pop back a few lines
4645 hence, all of our state is restored. Otherwise,
4646 finish_function doesn't clean things up, and we end
4647 up with CURRENT_FUNCTION_DECL set. */
4648 push_to_top_level ();
4649 /* The decl's location will mark where it was first
4650 needed. Save that so synthesize method can indicate
4651 where it was needed from, in case of error */
4652 input_location = DECL_SOURCE_LOCATION (decl);
4653 synthesize_method (decl);
4654 pop_from_top_level ();
4655 reconsider = true;
4658 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4659 generate_tls_wrapper (decl);
4661 if (!DECL_SAVED_TREE (decl))
4662 continue;
4664 /* We lie to the back end, pretending that some functions
4665 are not defined when they really are. This keeps these
4666 functions from being put out unnecessarily. But, we must
4667 stop lying when the functions are referenced, or if they
4668 are not comdat since they need to be put out now. If
4669 DECL_INTERFACE_KNOWN, then we have already set
4670 DECL_EXTERNAL appropriately, so there's no need to check
4671 again, and we do not want to clear DECL_EXTERNAL if a
4672 previous call to import_export_decl set it.
4674 This is done in a separate for cycle, because if some
4675 deferred function is contained in another deferred
4676 function later in deferred_fns varray,
4677 rest_of_compilation would skip this function and we
4678 really cannot expand the same function twice. */
4679 import_export_decl (decl);
4680 if (DECL_NOT_REALLY_EXTERN (decl)
4681 && DECL_INITIAL (decl)
4682 && decl_needed_p (decl))
4684 struct cgraph_node *node, *next;
4686 node = cgraph_node::get (decl);
4687 if (node->cpp_implicit_alias)
4688 node = node->get_alias_target ();
4690 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
4691 NULL, true);
4692 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4693 group, we need to mark all symbols in the same comdat group
4694 that way. */
4695 if (node->same_comdat_group)
4696 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
4697 next != node;
4698 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
4699 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
4700 NULL, true);
4703 /* If we're going to need to write this function out, and
4704 there's already a body for it, create RTL for it now.
4705 (There might be no body if this is a method we haven't
4706 gotten around to synthesizing yet.) */
4707 if (!DECL_EXTERNAL (decl)
4708 && decl_needed_p (decl)
4709 && !TREE_ASM_WRITTEN (decl)
4710 && !cgraph_node::get (decl)->definition)
4712 /* We will output the function; no longer consider it in this
4713 loop. */
4714 DECL_DEFER_OUTPUT (decl) = 0;
4715 /* Generate RTL for this function now that we know we
4716 need it. */
4717 expand_or_defer_fn (decl);
4718 /* If we're compiling -fsyntax-only pretend that this
4719 function has been written out so that we don't try to
4720 expand it again. */
4721 if (flag_syntax_only)
4722 TREE_ASM_WRITTEN (decl) = 1;
4723 reconsider = true;
4727 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
4728 reconsider = true;
4730 /* Static data members are just like namespace-scope globals. */
4731 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4733 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4734 /* Don't write it out if we haven't seen a definition. */
4735 || (DECL_IN_AGGR_P (decl) && !DECL_INLINE_VAR_P (decl)))
4736 continue;
4737 import_export_decl (decl);
4738 /* If this static data member is needed, provide it to the
4739 back end. */
4740 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4741 DECL_EXTERNAL (decl) = 0;
4743 if (vec_safe_length (pending_statics) != 0
4744 && wrapup_global_declarations (pending_statics->address (),
4745 pending_statics->length ()))
4746 reconsider = true;
4748 retries++;
4750 while (reconsider);
4752 walk_namespaces (diagnose_inline_vars_for_namespace, /*data=*/0);
4754 lower_var_init ();
4756 generate_mangling_aliases ();
4758 /* All used inline functions must have a definition at this point. */
4759 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4761 if (/* Check online inline functions that were actually used. */
4762 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4763 /* If the definition actually was available here, then the
4764 fact that the function was not defined merely represents
4765 that for some reason (use of a template repository,
4766 #pragma interface, etc.) we decided not to emit the
4767 definition here. */
4768 && !DECL_INITIAL (decl)
4769 /* Don't complain if the template was defined. */
4770 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4771 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4772 (template_for_substitution (decl)))))
4774 warning_at (DECL_SOURCE_LOCATION (decl), 0,
4775 "inline function %qD used but never defined", decl);
4776 /* Avoid a duplicate warning from check_global_declaration. */
4777 TREE_NO_WARNING (decl) = 1;
4781 /* So must decls that use a type with no linkage. */
4782 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4783 no_linkage_error (decl);
4785 maybe_warn_sized_delete ();
4787 /* Then, do the Objective-C stuff. This is where all the
4788 Objective-C module stuff gets generated (symtab,
4789 class/protocol/selector lists etc). This must be done after C++
4790 templates, destructors etc. so that selectors used in C++
4791 templates are properly allocated. */
4792 if (c_dialect_objc ())
4793 objc_write_global_declarations ();
4795 /* We give C linkage to static constructors and destructors. */
4796 push_lang_context (lang_name_c);
4798 /* Generate initialization and destruction functions for all
4799 priorities for which they are required. */
4800 if (priority_info_map)
4801 splay_tree_foreach (priority_info_map,
4802 generate_ctor_and_dtor_functions_for_priority,
4803 /*data=*/&locus_at_end_of_parsing);
4804 else if (c_dialect_objc () && objc_static_init_needed_p ())
4805 /* If this is obj-c++ and we need a static init, call
4806 generate_ctor_or_dtor_function. */
4807 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4808 DEFAULT_INIT_PRIORITY,
4809 &locus_at_end_of_parsing);
4811 /* We're done with the splay-tree now. */
4812 if (priority_info_map)
4813 splay_tree_delete (priority_info_map);
4815 /* Generate any missing aliases. */
4816 maybe_apply_pending_pragma_weaks ();
4818 /* We're done with static constructors, so we can go back to "C++"
4819 linkage now. */
4820 pop_lang_context ();
4822 if (flag_vtable_verify)
4824 vtv_recover_class_info ();
4825 vtv_compute_class_hierarchy_transitive_closure ();
4826 vtv_build_vtable_verify_fndecl ();
4829 perform_deferred_noexcept_checks ();
4831 finish_repo ();
4833 fini_constexpr ();
4835 /* The entire file is now complete. If requested, dump everything
4836 to a file. */
4837 dump_tu ();
4839 if (flag_detailed_statistics)
4841 dump_tree_statistics ();
4842 dump_time_statistics ();
4845 timevar_stop (TV_PHASE_DEFERRED);
4846 timevar_start (TV_PHASE_PARSING);
4848 /* Indicate that we're done with front end processing. */
4849 at_eof = 2;
4852 /* Perform any post compilation-proper cleanups for the C++ front-end.
4853 This should really go away. No front-end should need to do
4854 anything past the compilation process. */
4856 void
4857 cxx_post_compilation_parsing_cleanups (void)
4859 timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS);
4861 if (flag_vtable_verify)
4863 /* Generate the special constructor initialization function that
4864 calls __VLTRegisterPairs, and give it a very high
4865 initialization priority. This must be done after
4866 finalize_compilation_unit so that we have accurate
4867 information about which vtable will actually be emitted. */
4868 vtv_generate_init_routine ();
4871 input_location = locus_at_end_of_parsing;
4873 if (flag_checking)
4874 validate_conversion_obstack ();
4876 timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS);
4879 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4880 function to call in parse-tree form; it has not yet been
4881 semantically analyzed. ARGS are the arguments to the function.
4882 They have already been semantically analyzed. This may change
4883 ARGS. */
4885 tree
4886 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4887 tsubst_flags_t complain)
4889 tree orig_fn;
4890 vec<tree, va_gc> *orig_args = NULL;
4891 tree expr;
4892 tree object;
4894 orig_fn = fn;
4895 object = TREE_OPERAND (fn, 0);
4897 if (processing_template_decl)
4899 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4900 || TREE_CODE (fn) == MEMBER_REF);
4901 if (type_dependent_expression_p (fn)
4902 || any_type_dependent_arguments_p (*args))
4903 return build_nt_call_vec (fn, *args);
4905 orig_args = make_tree_vector_copy (*args);
4907 /* Transform the arguments and add the implicit "this"
4908 parameter. That must be done before the FN is transformed
4909 because we depend on the form of FN. */
4910 make_args_non_dependent (*args);
4911 object = build_non_dependent_expr (object);
4912 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4914 if (TREE_CODE (fn) == DOTSTAR_EXPR)
4915 object = cp_build_addr_expr (object, complain);
4916 vec_safe_insert (*args, 0, object);
4918 /* Now that the arguments are done, transform FN. */
4919 fn = build_non_dependent_expr (fn);
4922 /* A qualified name corresponding to a bound pointer-to-member is
4923 represented as an OFFSET_REF:
4925 struct B { void g(); };
4926 void (B::*p)();
4927 void B::g() { (this->*p)(); } */
4928 if (TREE_CODE (fn) == OFFSET_REF)
4930 tree object_addr = cp_build_addr_expr (object, complain);
4931 fn = TREE_OPERAND (fn, 1);
4932 fn = get_member_function_from_ptrfunc (&object_addr, fn,
4933 complain);
4934 vec_safe_insert (*args, 0, object_addr);
4937 if (CLASS_TYPE_P (TREE_TYPE (fn)))
4938 expr = build_op_call (fn, args, complain);
4939 else
4940 expr = cp_build_function_call_vec (fn, args, complain);
4941 if (processing_template_decl && expr != error_mark_node)
4942 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
4944 if (orig_args != NULL)
4945 release_tree_vector (orig_args);
4947 return expr;
4951 void
4952 check_default_args (tree x)
4954 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4955 bool saw_def = false;
4956 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4957 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4959 if (TREE_PURPOSE (arg))
4960 saw_def = true;
4961 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
4963 error ("default argument missing for parameter %P of %q+#D", i, x);
4964 TREE_PURPOSE (arg) = error_mark_node;
4969 /* Return true if function DECL can be inlined. This is used to force
4970 instantiation of methods that might be interesting for inlining. */
4971 bool
4972 possibly_inlined_p (tree decl)
4974 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4975 if (DECL_UNINLINABLE (decl))
4976 return false;
4977 if (!optimize)
4978 return DECL_DECLARED_INLINE_P (decl);
4979 /* When optimizing, we might inline everything when flatten
4980 attribute or heuristics inlining for size or autoinlining
4981 is used. */
4982 return true;
4985 /* Normally, we can wait until instantiation-time to synthesize DECL.
4986 However, if DECL is a static data member initialized with a constant
4987 or a constexpr function, we need it right now because a reference to
4988 such a data member or a call to such function is not value-dependent.
4989 For a function that uses auto in the return type, we need to instantiate
4990 it to find out its type. For OpenMP user defined reductions, we need
4991 them instantiated for reduction clauses which inline them by hand
4992 directly. */
4994 static void
4995 maybe_instantiate_decl (tree decl)
4997 if (DECL_LANG_SPECIFIC (decl)
4998 && DECL_TEMPLATE_INFO (decl)
4999 && (decl_maybe_constant_var_p (decl)
5000 || (TREE_CODE (decl) == FUNCTION_DECL
5001 && DECL_OMP_DECLARE_REDUCTION_P (decl))
5002 || undeduced_auto_decl (decl))
5003 && !DECL_DECLARED_CONCEPT_P (decl)
5004 && !uses_template_parms (DECL_TI_ARGS (decl)))
5006 /* Instantiating a function will result in garbage collection. We
5007 must treat this situation as if we were within the body of a
5008 function so as to avoid collecting live data only referenced from
5009 the stack (such as overload resolution candidates). */
5010 ++function_depth;
5011 instantiate_decl (decl, /*defer_ok=*/false,
5012 /*expl_inst_class_mem_p=*/false);
5013 --function_depth;
5017 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
5018 If DECL is a specialization or implicitly declared class member,
5019 generate the actual definition. Return false if something goes
5020 wrong, true otherwise. */
5022 bool
5023 mark_used (tree decl, tsubst_flags_t complain)
5025 /* If DECL is a BASELINK for a single function, then treat it just
5026 like the DECL for the function. Otherwise, if the BASELINK is
5027 for an overloaded function, we don't know which function was
5028 actually used until after overload resolution. */
5029 if (BASELINK_P (decl))
5031 decl = BASELINK_FUNCTIONS (decl);
5032 if (really_overloaded_fn (decl))
5033 return true;
5034 decl = OVL_CURRENT (decl);
5037 /* Set TREE_USED for the benefit of -Wunused. */
5038 TREE_USED (decl) = 1;
5040 if (TREE_CODE (decl) == TEMPLATE_DECL)
5041 return true;
5043 if (DECL_CLONED_FUNCTION_P (decl))
5044 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5046 /* Mark enumeration types as used. */
5047 if (TREE_CODE (decl) == CONST_DECL)
5048 used_types_insert (DECL_CONTEXT (decl));
5050 if (TREE_CODE (decl) == FUNCTION_DECL)
5051 maybe_instantiate_noexcept (decl);
5053 if (TREE_CODE (decl) == FUNCTION_DECL
5054 && DECL_DELETED_FN (decl))
5056 if (DECL_ARTIFICIAL (decl))
5058 if (DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
5059 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
5061 /* We mark a lambda conversion op as deleted if we can't
5062 generate it properly; see maybe_add_lambda_conv_op. */
5063 sorry ("converting lambda which uses %<...%> to "
5064 "function pointer");
5065 return false;
5068 if (complain & tf_error)
5070 error ("use of deleted function %qD", decl);
5071 if (!maybe_explain_implicit_delete (decl))
5072 inform (DECL_SOURCE_LOCATION (decl), "declared here");
5074 return false;
5077 if (TREE_DEPRECATED (decl) && (complain & tf_warning)
5078 && deprecated_state != DEPRECATED_SUPPRESS)
5079 warn_deprecated_use (decl, NULL_TREE);
5081 /* We can only check DECL_ODR_USED on variables or functions with
5082 DECL_LANG_SPECIFIC set, and these are also the only decls that we
5083 might need special handling for. */
5084 if (!VAR_OR_FUNCTION_DECL_P (decl)
5085 || DECL_LANG_SPECIFIC (decl) == NULL
5086 || DECL_THUNK_P (decl))
5088 if (!processing_template_decl
5089 && !require_deduced_type (decl, complain))
5090 return false;
5091 return true;
5094 /* We only want to do this processing once. We don't need to keep trying
5095 to instantiate inline templates, because unit-at-a-time will make sure
5096 we get them compiled before functions that want to inline them. */
5097 if (DECL_ODR_USED (decl))
5098 return true;
5100 /* Normally, we can wait until instantiation-time to synthesize DECL.
5101 However, if DECL is a static data member initialized with a constant
5102 or a constexpr function, we need it right now because a reference to
5103 such a data member or a call to such function is not value-dependent.
5104 For a function that uses auto in the return type, we need to instantiate
5105 it to find out its type. For OpenMP user defined reductions, we need
5106 them instantiated for reduction clauses which inline them by hand
5107 directly. */
5108 maybe_instantiate_decl (decl);
5110 if (processing_template_decl || in_template_function ())
5111 return true;
5113 /* Check this too in case we're within instantiate_non_dependent_expr. */
5114 if (DECL_TEMPLATE_INFO (decl)
5115 && uses_template_parms (DECL_TI_ARGS (decl)))
5116 return true;
5118 if (!require_deduced_type (decl, complain))
5119 return false;
5121 /* If we don't need a value, then we don't need to synthesize DECL. */
5122 if (cp_unevaluated_operand || in_discarded_stmt)
5123 return true;
5125 DECL_ODR_USED (decl) = 1;
5126 if (DECL_CLONED_FUNCTION_P (decl))
5127 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5129 /* DR 757: A type without linkage shall not be used as the type of a
5130 variable or function with linkage, unless
5131 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5132 o the variable or function is not used (3.2 [basic.def.odr]) or is
5133 defined in the same translation unit. */
5134 if (cxx_dialect > cxx98
5135 && decl_linkage (decl) != lk_none
5136 && !DECL_EXTERN_C_P (decl)
5137 && !DECL_ARTIFICIAL (decl)
5138 && !decl_defined_p (decl)
5139 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
5141 if (is_local_extern (decl))
5142 /* There's no way to define a local extern, and adding it to
5143 the vector interferes with GC, so give an error now. */
5144 no_linkage_error (decl);
5145 else
5146 vec_safe_push (no_linkage_decls, decl);
5149 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
5150 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
5151 /* Remember it, so we can check it was defined. */
5152 note_vague_linkage_fn (decl);
5154 /* Is it a synthesized method that needs to be synthesized? */
5155 if (TREE_CODE (decl) == FUNCTION_DECL
5156 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5157 && DECL_DEFAULTED_FN (decl)
5158 /* A function defaulted outside the class is synthesized either by
5159 cp_finish_decl or instantiate_decl. */
5160 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
5161 && ! DECL_INITIAL (decl))
5163 /* Defer virtual destructors so that thunks get the right
5164 linkage. */
5165 if (DECL_VIRTUAL_P (decl) && !at_eof)
5167 note_vague_linkage_fn (decl);
5168 return true;
5171 /* Remember the current location for a function we will end up
5172 synthesizing. Then we can inform the user where it was
5173 required in the case of error. */
5174 DECL_SOURCE_LOCATION (decl) = input_location;
5176 /* Synthesizing an implicitly defined member function will result in
5177 garbage collection. We must treat this situation as if we were
5178 within the body of a function so as to avoid collecting live data
5179 on the stack (such as overload resolution candidates).
5181 We could just let cp_write_global_declarations handle synthesizing
5182 this function by adding it to deferred_fns, but doing
5183 it at the use site produces better error messages. */
5184 ++function_depth;
5185 synthesize_method (decl);
5186 --function_depth;
5187 /* If this is a synthesized method we don't need to
5188 do the instantiation test below. */
5190 else if (VAR_OR_FUNCTION_DECL_P (decl)
5191 && DECL_TEMPLATE_INFO (decl)
5192 && !DECL_DECLARED_CONCEPT_P (decl)
5193 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5194 || always_instantiate_p (decl)))
5195 /* If this is a function or variable that is an instance of some
5196 template, we now know that we will need to actually do the
5197 instantiation. We check that DECL is not an explicit
5198 instantiation because that is not checked in instantiate_decl.
5200 We put off instantiating functions in order to improve compile
5201 times. Maintaining a stack of active functions is expensive,
5202 and the inliner knows to instantiate any functions it might
5203 need. Therefore, we always try to defer instantiation. */
5205 ++function_depth;
5206 instantiate_decl (decl, /*defer_ok=*/true,
5207 /*expl_inst_class_mem_p=*/false);
5208 --function_depth;
5211 return true;
5214 bool
5215 mark_used (tree decl)
5217 return mark_used (decl, tf_warning_or_error);
5220 tree
5221 vtv_start_verification_constructor_init_function (void)
5223 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5226 tree
5227 vtv_finish_verification_constructor_init_function (tree function_body)
5229 tree fn;
5231 finish_compound_stmt (function_body);
5232 fn = finish_function (0);
5233 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5234 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5236 return fn;
5239 #include "gt-cp-decl2.h"