* Makefile.am: Remove references to c-compiler-name.h and
[official-gcc.git] / gcc / cp / decl2.c
blobbc0db0000deb049186441aaa0d73d26a52f1849f
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2017 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* Process declarations and symbol lookup for C++ front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "memmodel.h"
33 #include "target.h"
34 #include "cp-tree.h"
35 #include "c-family/c-common.h"
36 #include "timevar.h"
37 #include "stringpool.h"
38 #include "cgraph.h"
39 #include "varasm.h"
40 #include "attribs.h"
41 #include "stor-layout.h"
42 #include "calls.h"
43 #include "decl.h"
44 #include "toplev.h"
45 #include "c-family/c-objc.h"
46 #include "c-family/c-pragma.h"
47 #include "dumpfile.h"
48 #include "intl.h"
49 #include "c-family/c-ada-spec.h"
50 #include "asan.h"
52 /* Id for dumping the raw trees. */
53 int raw_dump_id;
55 extern cpp_reader *parse_in;
57 /* This structure contains information about the initializations
58 and/or destructions required for a particular priority level. */
59 typedef struct priority_info_s {
60 /* Nonzero if there have been any initializations at this priority
61 throughout the translation unit. */
62 int initializations_p;
63 /* Nonzero if there have been any destructions at this priority
64 throughout the translation unit. */
65 int destructions_p;
66 } *priority_info;
68 static void mark_vtable_entries (tree);
69 static bool maybe_emit_vtables (tree);
70 static tree start_objects (int, int);
71 static void finish_objects (int, int, tree);
72 static tree start_static_storage_duration_function (unsigned);
73 static void finish_static_storage_duration_function (tree);
74 static priority_info get_priority_info (int);
75 static void do_static_initialization_or_destruction (tree, bool);
76 static void one_static_initialization_or_destruction (tree, tree, bool);
77 static void generate_ctor_or_dtor_function (bool, int, location_t *);
78 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
79 void *);
80 static tree prune_vars_needing_no_initialization (tree *);
81 static void write_out_vars (tree);
82 static void import_export_class (tree);
83 static tree get_guard_bits (tree);
84 static void determine_visibility_from_class (tree, tree);
85 static bool determine_hidden_inline (tree);
86 static void maybe_instantiate_decl (tree);
88 /* A list of static class variables. This is needed, because a
89 static class variable can be declared inside the class without
90 an initializer, and then initialized, statically, outside the class. */
91 static GTY(()) vec<tree, va_gc> *pending_statics;
93 /* A list of functions which were declared inline, but which we
94 may need to emit outline anyway. */
95 static GTY(()) vec<tree, va_gc> *deferred_fns;
97 /* A list of decls that use types with no linkage, which we need to make
98 sure are defined. */
99 static GTY(()) vec<tree, va_gc> *no_linkage_decls;
101 /* A vector of alternating decls and identifiers, where the latter
102 is to be an alias for the former if the former is defined. */
103 static GTY(()) vec<tree, va_gc> *mangling_aliases;
105 /* hash traits for declarations. Hashes single decls via
106 DECL_ASSEMBLER_NAME_RAW. */
108 struct mangled_decl_hash : ggc_remove <tree>
110 typedef tree value_type; /* A DECL. */
111 typedef tree compare_type; /* An identifier. */
113 static hashval_t hash (const value_type decl)
115 return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME_RAW (decl));
117 static bool equal (const value_type existing, compare_type candidate)
119 tree name = DECL_ASSEMBLER_NAME_RAW (existing);
120 return candidate == name;
123 static inline void mark_empty (value_type &p) {p = NULL_TREE;}
124 static inline bool is_empty (value_type p) {return !p;}
126 static bool is_deleted (value_type e)
128 return e == reinterpret_cast <value_type> (1);
130 static void mark_deleted (value_type &e)
132 e = reinterpret_cast <value_type> (1);
136 /* A hash table of decls keyed by mangled name. Used to figure out if
137 we need compatibility aliases. */
138 static GTY(()) hash_table<mangled_decl_hash> *mangled_decls;
140 /* Nonzero if we're done parsing and into end-of-file activities. */
142 int at_eof;
144 /* True if note_mangling_alias should enqueue mangling aliases for
145 later generation, rather than emitting them right away. */
147 bool defer_mangling_aliases = true;
150 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
151 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
152 that apply to the function). */
154 tree
155 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
156 cp_ref_qualifier rqual)
158 tree raises;
159 tree attrs;
160 int type_quals;
161 bool late_return_type_p;
163 if (fntype == error_mark_node || ctype == error_mark_node)
164 return error_mark_node;
166 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
167 || TREE_CODE (fntype) == METHOD_TYPE);
169 type_quals = quals & ~TYPE_QUAL_RESTRICT;
170 ctype = cp_build_qualified_type (ctype, type_quals);
171 raises = TYPE_RAISES_EXCEPTIONS (fntype);
172 attrs = TYPE_ATTRIBUTES (fntype);
173 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
174 fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
175 (TREE_CODE (fntype) == METHOD_TYPE
176 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
177 : TYPE_ARG_TYPES (fntype)));
178 if (attrs)
179 fntype = cp_build_type_attribute_variant (fntype, attrs);
180 if (rqual)
181 fntype = build_ref_qualified_type (fntype, rqual);
182 if (raises)
183 fntype = build_exception_variant (fntype, raises);
184 if (late_return_type_p)
185 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
187 return fntype;
190 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
191 return type changed to NEW_RET. */
193 tree
194 change_return_type (tree new_ret, tree fntype)
196 tree newtype;
197 tree args = TYPE_ARG_TYPES (fntype);
198 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
199 tree attrs = TYPE_ATTRIBUTES (fntype);
200 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
202 if (new_ret == error_mark_node)
203 return fntype;
205 if (same_type_p (new_ret, TREE_TYPE (fntype)))
206 return fntype;
208 if (TREE_CODE (fntype) == FUNCTION_TYPE)
210 newtype = build_function_type (new_ret, args);
211 newtype = apply_memfn_quals (newtype,
212 type_memfn_quals (fntype),
213 type_memfn_rqual (fntype));
215 else
216 newtype = build_method_type_directly
217 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
218 if (FUNCTION_REF_QUALIFIED (fntype))
219 newtype = build_ref_qualified_type (newtype, type_memfn_rqual (fntype));
220 if (raises)
221 newtype = build_exception_variant (newtype, raises);
222 if (attrs)
223 newtype = cp_build_type_attribute_variant (newtype, attrs);
224 if (late_return_type_p)
225 TYPE_HAS_LATE_RETURN_TYPE (newtype) = 1;
227 return newtype;
230 /* Build a PARM_DECL of FN with NAME and TYPE, and set DECL_ARG_TYPE
231 appropriately. */
233 tree
234 cp_build_parm_decl (tree fn, tree name, tree type)
236 tree parm = build_decl (input_location,
237 PARM_DECL, name, type);
238 DECL_CONTEXT (parm) = fn;
240 /* DECL_ARG_TYPE is only used by the back end and the back end never
241 sees templates. */
242 if (!processing_template_decl)
243 DECL_ARG_TYPE (parm) = type_passed_as (type);
245 return parm;
248 /* Returns a PARM_DECL of FN for a parameter of the indicated TYPE, with the
249 indicated NAME. */
251 tree
252 build_artificial_parm (tree fn, tree name, tree type)
254 tree parm = cp_build_parm_decl (fn, name, type);
255 DECL_ARTIFICIAL (parm) = 1;
256 /* All our artificial parms are implicitly `const'; they cannot be
257 assigned to. */
258 TREE_READONLY (parm) = 1;
259 return parm;
262 /* Constructors for types with virtual baseclasses need an "in-charge" flag
263 saying whether this constructor is responsible for initialization of
264 virtual baseclasses or not. All destructors also need this "in-charge"
265 flag, which additionally determines whether or not the destructor should
266 free the memory for the object.
268 This function adds the "in-charge" flag to member function FN if
269 appropriate. It is called from grokclassfn and tsubst.
270 FN must be either a constructor or destructor.
272 The in-charge flag follows the 'this' parameter, and is followed by the
273 VTT parm (if any), then the user-written parms. */
275 void
276 maybe_retrofit_in_chrg (tree fn)
278 tree basetype, arg_types, parms, parm, fntype;
280 /* If we've already add the in-charge parameter don't do it again. */
281 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
282 return;
284 /* When processing templates we can't know, in general, whether or
285 not we're going to have virtual baseclasses. */
286 if (processing_template_decl)
287 return;
289 /* We don't need an in-charge parameter for constructors that don't
290 have virtual bases. */
291 if (DECL_CONSTRUCTOR_P (fn)
292 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
293 return;
295 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
296 basetype = TREE_TYPE (TREE_VALUE (arg_types));
297 arg_types = TREE_CHAIN (arg_types);
299 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
301 /* If this is a subobject constructor or destructor, our caller will
302 pass us a pointer to our VTT. */
303 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
305 parm = build_artificial_parm (fn, vtt_parm_identifier, vtt_parm_type);
307 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
308 DECL_CHAIN (parm) = parms;
309 parms = parm;
311 /* ...and then to TYPE_ARG_TYPES. */
312 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
314 DECL_HAS_VTT_PARM_P (fn) = 1;
317 /* Then add the in-charge parm (before the VTT parm). */
318 parm = build_artificial_parm (fn, in_charge_identifier, integer_type_node);
319 DECL_CHAIN (parm) = parms;
320 parms = parm;
321 arg_types = hash_tree_chain (integer_type_node, arg_types);
323 /* Insert our new parameter(s) into the list. */
324 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
326 /* And rebuild the function type. */
327 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
328 arg_types);
329 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
330 fntype = build_exception_variant (fntype,
331 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
332 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
333 fntype = (cp_build_type_attribute_variant
334 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
335 TREE_TYPE (fn) = fntype;
337 /* Now we've got the in-charge parameter. */
338 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
341 /* Classes overload their constituent function names automatically.
342 When a function name is declared in a record structure,
343 its name is changed to it overloaded name. Since names for
344 constructors and destructors can conflict, we place a leading
345 '$' for destructors.
347 CNAME is the name of the class we are grokking for.
349 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
351 FLAGS contains bits saying what's special about today's
352 arguments. DTOR_FLAG == DESTRUCTOR.
354 If FUNCTION is a destructor, then we must add the `auto-delete' field
355 as a second parameter. There is some hair associated with the fact
356 that we must "declare" this variable in the manner consistent with the
357 way the rest of the arguments were declared.
359 QUALS are the qualifiers for the this pointer. */
361 void
362 grokclassfn (tree ctype, tree function, enum overload_flags flags)
364 tree fn_name = DECL_NAME (function);
366 /* Even within an `extern "C"' block, members get C++ linkage. See
367 [dcl.link] for details. */
368 SET_DECL_LANGUAGE (function, lang_cplusplus);
370 if (fn_name == NULL_TREE)
372 error ("name missing for member function");
373 fn_name = get_identifier ("<anonymous>");
374 DECL_NAME (function) = fn_name;
377 DECL_CONTEXT (function) = ctype;
379 if (flags == DTOR_FLAG)
380 DECL_CXX_DESTRUCTOR_P (function) = 1;
382 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
383 maybe_retrofit_in_chrg (function);
386 /* Create an ARRAY_REF, checking for the user doing things backwards
387 along the way. DECLTYPE_P is for N3276, as in the parser. */
389 tree
390 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
391 bool decltype_p)
393 tree type;
394 tree expr;
395 tree orig_array_expr = array_expr;
396 tree orig_index_exp = index_exp;
397 tree overload = NULL_TREE;
399 if (error_operand_p (array_expr) || error_operand_p (index_exp))
400 return error_mark_node;
402 if (processing_template_decl)
404 if (type_dependent_expression_p (array_expr)
405 || type_dependent_expression_p (index_exp))
406 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
407 NULL_TREE, NULL_TREE);
408 array_expr = build_non_dependent_expr (array_expr);
409 index_exp = build_non_dependent_expr (index_exp);
412 type = TREE_TYPE (array_expr);
413 gcc_assert (type);
414 type = non_reference (type);
416 /* If they have an `operator[]', use that. */
417 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
419 tsubst_flags_t complain = tf_warning_or_error;
420 if (decltype_p)
421 complain |= tf_decltype;
422 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
423 index_exp, NULL_TREE, &overload, complain);
425 else
427 tree p1, p2, i1, i2;
429 /* Otherwise, create an ARRAY_REF for a pointer or array type.
430 It is a little-known fact that, if `a' is an array and `i' is
431 an int, you can write `i[a]', which means the same thing as
432 `a[i]'. */
433 if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
434 p1 = array_expr;
435 else
436 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
438 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
439 p2 = index_exp;
440 else
441 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
443 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
444 false);
445 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
446 false);
448 if ((p1 && i2) && (i1 && p2))
449 error ("ambiguous conversion for array subscript");
451 if (p1 && i2)
452 array_expr = p1, index_exp = i2;
453 else if (i1 && p2)
454 array_expr = p2, index_exp = i1;
455 else
457 error ("invalid types %<%T[%T]%> for array subscript",
458 type, TREE_TYPE (index_exp));
459 return error_mark_node;
462 if (array_expr == error_mark_node || index_exp == error_mark_node)
463 error ("ambiguous conversion for array subscript");
465 if (TREE_CODE (TREE_TYPE (array_expr)) == POINTER_TYPE)
466 array_expr = mark_rvalue_use (array_expr);
467 else
468 array_expr = mark_lvalue_use_nonread (array_expr);
469 index_exp = mark_rvalue_use (index_exp);
470 expr = build_array_ref (input_location, array_expr, index_exp);
472 if (processing_template_decl && expr != error_mark_node)
474 if (overload != NULL_TREE)
475 return (build_min_non_dep_op_overload
476 (ARRAY_REF, expr, overload, orig_array_expr, orig_index_exp));
478 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
479 NULL_TREE, NULL_TREE);
481 return expr;
484 /* Given the cast expression EXP, checking out its validity. Either return
485 an error_mark_node if there was an unavoidable error, return a cast to
486 void for trying to delete a pointer w/ the value 0, or return the
487 call to delete. If DOING_VEC is true, we handle things differently
488 for doing an array delete.
489 Implements ARM $5.3.4. This is called from the parser. */
491 tree
492 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
493 tsubst_flags_t complain)
495 tree t, type;
497 if (exp == error_mark_node)
498 return exp;
500 if (processing_template_decl)
502 t = build_min (DELETE_EXPR, void_type_node, exp, size);
503 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
504 DELETE_EXPR_USE_VEC (t) = doing_vec;
505 TREE_SIDE_EFFECTS (t) = 1;
506 return t;
509 /* An array can't have been allocated by new, so complain. */
510 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
511 warning (0, "deleting array %q#E", exp);
513 t = build_expr_type_conversion (WANT_POINTER, exp, true);
515 if (t == NULL_TREE || t == error_mark_node)
517 error ("type %q#T argument given to %<delete%>, expected pointer",
518 TREE_TYPE (exp));
519 return error_mark_node;
522 type = TREE_TYPE (t);
524 /* As of Valley Forge, you can delete a pointer to const. */
526 /* You can't delete functions. */
527 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
529 error ("cannot delete a function. Only pointer-to-objects are "
530 "valid arguments to %<delete%>");
531 return error_mark_node;
534 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
535 if (VOID_TYPE_P (TREE_TYPE (type)))
537 warning (OPT_Wdelete_incomplete, "deleting %qT is undefined", type);
538 doing_vec = 0;
541 /* Deleting a pointer with the value zero is valid and has no effect. */
542 if (integer_zerop (t))
543 return build1 (NOP_EXPR, void_type_node, t);
545 if (doing_vec)
546 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
547 sfk_deleting_destructor,
548 use_global_delete, complain);
549 else
550 return build_delete (type, t, sfk_deleting_destructor,
551 LOOKUP_NORMAL, use_global_delete,
552 complain);
555 /* Report an error if the indicated template declaration is not the
556 sort of thing that should be a member template. */
558 void
559 check_member_template (tree tmpl)
561 tree decl;
563 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
564 decl = DECL_TEMPLATE_RESULT (tmpl);
566 if (TREE_CODE (decl) == FUNCTION_DECL
567 || DECL_ALIAS_TEMPLATE_P (tmpl)
568 || (TREE_CODE (decl) == TYPE_DECL
569 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
571 /* The parser rejects template declarations in local classes
572 (with the exception of generic lambdas). */
573 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
574 /* The parser rejects any use of virtual in a function template. */
575 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
576 && DECL_VIRTUAL_P (decl)));
578 /* The debug-information generating code doesn't know what to do
579 with member templates. */
580 DECL_IGNORED_P (tmpl) = 1;
582 else if (variable_template_p (tmpl))
583 /* OK */;
584 else
585 error ("template declaration of %q#D", decl);
588 /* Sanity check: report error if this function FUNCTION is not
589 really a member of the class (CTYPE) it is supposed to belong to.
590 TEMPLATE_PARMS is used to specify the template parameters of a member
591 template passed as FUNCTION_DECL. If the member template is passed as a
592 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
593 from the declaration. If the function is not a function template, it
594 must be NULL.
595 It returns the original declaration for the function, NULL_TREE if
596 no declaration was found, error_mark_node if an error was emitted. */
598 tree
599 check_classfn (tree ctype, tree function, tree template_parms)
601 if (DECL_USE_TEMPLATE (function)
602 && !(TREE_CODE (function) == TEMPLATE_DECL
603 && DECL_TEMPLATE_SPECIALIZATION (function))
604 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
605 /* Since this is a specialization of a member template,
606 we're not going to find the declaration in the class.
607 For example, in:
609 struct S { template <typename T> void f(T); };
610 template <> void S::f(int);
612 we're not going to find `S::f(int)', but there's no
613 reason we should, either. We let our callers know we didn't
614 find the method, but we don't complain. */
615 return NULL_TREE;
617 /* Basic sanity check: for a template function, the template parameters
618 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
619 if (TREE_CODE (function) == TEMPLATE_DECL)
621 if (template_parms
622 && !comp_template_parms (template_parms,
623 DECL_TEMPLATE_PARMS (function)))
625 error ("template parameter lists provided don%'t match the "
626 "template parameters of %qD", function);
627 return error_mark_node;
629 template_parms = DECL_TEMPLATE_PARMS (function);
632 /* OK, is this a definition of a member template? */
633 bool is_template = (template_parms != NULL_TREE);
635 /* [temp.mem]
637 A destructor shall not be a member template. */
638 if (DECL_DESTRUCTOR_P (function) && is_template)
640 error ("destructor %qD declared as member template", function);
641 return error_mark_node;
644 /* We must enter the scope here, because conversion operators are
645 named by target type, and type equivalence relies on typenames
646 resolving within the scope of CTYPE. */
647 tree pushed_scope = push_scope (ctype);
648 tree matched = NULL_TREE;
649 tree fns = get_class_binding (ctype, DECL_NAME (function));
651 for (ovl_iterator iter (fns); !matched && iter; ++iter)
653 tree fndecl = *iter;
655 /* A member template definition only matches a member template
656 declaration. */
657 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
658 continue;
660 if (!DECL_DECLARES_FUNCTION_P (fndecl))
661 continue;
663 tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
664 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
666 /* We cannot simply call decls_match because this doesn't work
667 for static member functions that are pretending to be
668 methods, and because the name may have been changed by
669 asm("new_name"). */
671 /* Get rid of the this parameter on functions that become
672 static. */
673 if (DECL_STATIC_FUNCTION_P (fndecl)
674 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
675 p1 = TREE_CHAIN (p1);
677 /* ref-qualifier or absence of same must match. */
678 if (type_memfn_rqual (TREE_TYPE (function))
679 != type_memfn_rqual (TREE_TYPE (fndecl)))
680 continue;
682 // Include constraints in the match.
683 tree c1 = get_constraints (function);
684 tree c2 = get_constraints (fndecl);
686 /* While finding a match, same types and params are not enough
687 if the function is versioned. Also check version ("target")
688 attributes. */
689 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
690 TREE_TYPE (TREE_TYPE (fndecl)))
691 && compparms (p1, p2)
692 && !targetm.target_option.function_versions (function, fndecl)
693 && (!is_template
694 || comp_template_parms (template_parms,
695 DECL_TEMPLATE_PARMS (fndecl)))
696 && equivalent_constraints (c1, c2)
697 && (DECL_TEMPLATE_SPECIALIZATION (function)
698 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
699 && (!DECL_TEMPLATE_SPECIALIZATION (function)
700 || (DECL_TI_TEMPLATE (function) == DECL_TI_TEMPLATE (fndecl))))
701 matched = fndecl;
704 if (!matched)
706 if (!COMPLETE_TYPE_P (ctype))
707 cxx_incomplete_type_error (function, ctype);
708 else
710 if (DECL_CONV_FN_P (function))
711 fns = get_class_binding (ctype, conv_op_identifier);
713 error_at (DECL_SOURCE_LOCATION (function),
714 "no declaration matches %q#D", function);
715 if (fns)
716 print_candidates (fns);
717 else if (DECL_CONV_FN_P (function))
718 inform (DECL_SOURCE_LOCATION (function),
719 "no conversion operators declared");
720 else
721 inform (DECL_SOURCE_LOCATION (function),
722 "no functions named %qD", function);
723 inform (DECL_SOURCE_LOCATION (TYPE_NAME (ctype)),
724 "%#qT defined here", ctype);
726 matched = error_mark_node;
729 if (pushed_scope)
730 pop_scope (pushed_scope);
732 return matched;
735 /* DECL is a function with vague linkage. Remember it so that at the
736 end of the translation unit we can decide whether or not to emit
737 it. */
739 void
740 note_vague_linkage_fn (tree decl)
742 DECL_DEFER_OUTPUT (decl) = 1;
743 vec_safe_push (deferred_fns, decl);
746 /* As above, but for variable template instantiations. */
748 void
749 note_variable_template_instantiation (tree decl)
751 vec_safe_push (pending_statics, decl);
754 /* We have just processed the DECL, which is a static data member.
755 The other parameters are as for cp_finish_decl. */
757 void
758 finish_static_data_member_decl (tree decl,
759 tree init, bool init_const_expr_p,
760 tree asmspec_tree,
761 int flags)
763 DECL_CONTEXT (decl) = current_class_type;
765 /* We cannot call pushdecl here, because that would fill in the
766 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
767 the right thing, namely, to put this decl out straight away. */
769 if (! processing_template_decl)
770 vec_safe_push (pending_statics, decl);
772 if (LOCAL_CLASS_P (current_class_type)
773 /* We already complained about the template definition. */
774 && !DECL_TEMPLATE_INSTANTIATION (decl))
775 permerror (input_location, "local class %q#T shall not have static data member %q#D",
776 current_class_type, decl);
777 else
778 for (tree t = current_class_type; TYPE_P (t);
779 t = CP_TYPE_CONTEXT (t))
780 if (TYPE_UNNAMED_P (t))
782 if (permerror (DECL_SOURCE_LOCATION (decl),
783 "static data member %qD in unnamed class", decl))
784 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
785 "unnamed class defined here");
786 break;
789 DECL_IN_AGGR_P (decl) = 1;
791 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
792 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
793 SET_VAR_HAD_UNKNOWN_BOUND (decl);
795 if (init)
797 /* Similarly to start_decl_1, we want to complete the type in order
798 to do the right thing in cp_apply_type_quals_to_decl, possibly
799 clear TYPE_QUAL_CONST (c++/65579). */
800 tree type = TREE_TYPE (decl) = complete_type (TREE_TYPE (decl));
801 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
804 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
807 /* DECLARATOR and DECLSPECS correspond to a class member. The other
808 parameters are as for cp_finish_decl. Return the DECL for the
809 class member declared. */
811 tree
812 grokfield (const cp_declarator *declarator,
813 cp_decl_specifier_seq *declspecs,
814 tree init, bool init_const_expr_p,
815 tree asmspec_tree,
816 tree attrlist)
818 tree value;
819 const char *asmspec = 0;
820 int flags;
821 tree name;
823 if (init
824 && TREE_CODE (init) == TREE_LIST
825 && TREE_VALUE (init) == error_mark_node
826 && TREE_CHAIN (init) == NULL_TREE)
827 init = NULL_TREE;
829 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
830 if (! value || value == error_mark_node)
831 /* friend or constructor went bad. */
832 return error_mark_node;
833 if (TREE_TYPE (value) == error_mark_node)
834 return value;
836 if (TREE_CODE (value) == TYPE_DECL && init)
838 error ("typedef %qD is initialized (use decltype instead)", value);
839 init = NULL_TREE;
842 /* Pass friendly classes back. */
843 if (value == void_type_node)
844 return value;
847 name = DECL_NAME (value);
849 if (name != NULL_TREE)
851 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
853 error ("explicit template argument list not allowed");
854 return error_mark_node;
857 if (IDENTIFIER_POINTER (name)[0] == '_'
858 && id_equal (name, "_vptr"))
859 error ("member %qD conflicts with virtual function table field name",
860 value);
863 /* Stash away type declarations. */
864 if (TREE_CODE (value) == TYPE_DECL)
866 DECL_NONLOCAL (value) = 1;
867 DECL_CONTEXT (value) = current_class_type;
869 if (attrlist)
871 int attrflags = 0;
873 /* If this is a typedef that names the class for linkage purposes
874 (7.1.3p8), apply any attributes directly to the type. */
875 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
876 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
877 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
879 cplus_decl_attributes (&value, attrlist, attrflags);
882 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
883 && TREE_TYPE (value) != error_mark_node
884 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
885 set_underlying_type (value);
887 /* It's important that push_template_decl below follows
888 set_underlying_type above so that the created template
889 carries the properly set type of VALUE. */
890 if (processing_template_decl)
891 value = push_template_decl (value);
893 record_locally_defined_typedef (value);
894 return value;
897 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
899 if (!friendp && DECL_IN_AGGR_P (value))
901 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
902 return void_type_node;
905 if (asmspec_tree && asmspec_tree != error_mark_node)
906 asmspec = TREE_STRING_POINTER (asmspec_tree);
908 if (init)
910 if (TREE_CODE (value) == FUNCTION_DECL)
912 if (init == ridpointers[(int)RID_DELETE])
914 DECL_DELETED_FN (value) = 1;
915 DECL_DECLARED_INLINE_P (value) = 1;
916 DECL_INITIAL (value) = error_mark_node;
918 else if (init == ridpointers[(int)RID_DEFAULT])
920 if (defaultable_fn_check (value))
922 DECL_DEFAULTED_FN (value) = 1;
923 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
924 DECL_DECLARED_INLINE_P (value) = 1;
927 else if (TREE_CODE (init) == DEFAULT_ARG)
928 error ("invalid initializer for member function %qD", value);
929 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
931 if (integer_zerop (init))
932 DECL_PURE_VIRTUAL_P (value) = 1;
933 else if (error_operand_p (init))
934 ; /* An error has already been reported. */
935 else
936 error ("invalid initializer for member function %qD",
937 value);
939 else
941 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
942 if (friendp)
943 error ("initializer specified for friend function %qD",
944 value);
945 else
946 error ("initializer specified for static member function %qD",
947 value);
950 else if (TREE_CODE (value) == FIELD_DECL)
951 /* C++11 NSDMI, keep going. */;
952 else if (!VAR_P (value))
953 gcc_unreachable ();
956 /* Pass friend decls back. */
957 if ((TREE_CODE (value) == FUNCTION_DECL
958 || TREE_CODE (value) == TEMPLATE_DECL)
959 && DECL_CONTEXT (value) != current_class_type)
960 return value;
962 /* Need to set this before push_template_decl. */
963 if (VAR_P (value))
964 DECL_CONTEXT (value) = current_class_type;
966 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
968 value = push_template_decl (value);
969 if (error_operand_p (value))
970 return error_mark_node;
973 if (attrlist)
974 cplus_decl_attributes (&value, attrlist, 0);
976 if (init && DIRECT_LIST_INIT_P (init))
977 flags = LOOKUP_NORMAL;
978 else
979 flags = LOOKUP_IMPLICIT;
981 switch (TREE_CODE (value))
983 case VAR_DECL:
984 finish_static_data_member_decl (value, init, init_const_expr_p,
985 asmspec_tree, flags);
986 return value;
988 case FIELD_DECL:
989 if (asmspec)
990 error ("%<asm%> specifiers are not permitted on non-static data members");
991 if (DECL_INITIAL (value) == error_mark_node)
992 init = error_mark_node;
993 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
994 NULL_TREE, flags);
995 DECL_IN_AGGR_P (value) = 1;
996 return value;
998 case FUNCTION_DECL:
999 if (asmspec)
1000 set_user_assembler_name (value, asmspec);
1002 cp_finish_decl (value,
1003 /*init=*/NULL_TREE,
1004 /*init_const_expr_p=*/false,
1005 asmspec_tree, flags);
1007 /* Pass friends back this way. */
1008 if (DECL_FRIEND_P (value))
1009 return void_type_node;
1011 DECL_IN_AGGR_P (value) = 1;
1012 return value;
1014 default:
1015 gcc_unreachable ();
1017 return NULL_TREE;
1020 /* Like `grokfield', but for bitfields.
1021 WIDTH is the width of the bitfield, a constant expression.
1022 The other parameters are as for grokfield. */
1024 tree
1025 grokbitfield (const cp_declarator *declarator,
1026 cp_decl_specifier_seq *declspecs, tree width, tree init,
1027 tree attrlist)
1029 tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1030 init != NULL_TREE, &attrlist);
1032 if (value == error_mark_node)
1033 return NULL_TREE; /* friends went bad. */
1034 if (TREE_TYPE (value) == error_mark_node)
1035 return value;
1037 /* Pass friendly classes back. */
1038 if (VOID_TYPE_P (value))
1039 return void_type_node;
1041 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
1042 && (POINTER_TYPE_P (value)
1043 || !dependent_type_p (TREE_TYPE (value))))
1045 error ("bit-field %qD with non-integral type", value);
1046 return error_mark_node;
1049 if (TREE_CODE (value) == TYPE_DECL)
1051 error ("cannot declare %qD to be a bit-field type", value);
1052 return NULL_TREE;
1055 /* Usually, finish_struct_1 catches bitfields with invalid types.
1056 But, in the case of bitfields with function type, we confuse
1057 ourselves into thinking they are member functions, so we must
1058 check here. */
1059 if (TREE_CODE (value) == FUNCTION_DECL)
1061 error ("cannot declare bit-field %qD with function type",
1062 DECL_NAME (value));
1063 return NULL_TREE;
1066 if (width && TYPE_WARN_IF_NOT_ALIGN (TREE_TYPE (value)))
1068 error ("cannot declare bit-field %qD with %<warn_if_not_aligned%> type",
1069 DECL_NAME (value));
1070 return NULL_TREE;
1073 if (DECL_IN_AGGR_P (value))
1075 error ("%qD is already defined in the class %qT", value,
1076 DECL_CONTEXT (value));
1077 return void_type_node;
1080 if (TREE_STATIC (value))
1082 error ("static member %qD cannot be a bit-field", value);
1083 return NULL_TREE;
1086 int flags = LOOKUP_IMPLICIT;
1087 if (init && DIRECT_LIST_INIT_P (init))
1088 flags = LOOKUP_NORMAL;
1089 cp_finish_decl (value, init, false, NULL_TREE, flags);
1091 if (width != error_mark_node)
1093 /* The width must be an integer type. */
1094 if (!type_dependent_expression_p (width)
1095 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1096 error ("width of bit-field %qD has non-integral type %qT", value,
1097 TREE_TYPE (width));
1098 else
1100 /* Temporarily stash the width in DECL_BIT_FIELD_REPRESENTATIVE.
1101 check_bitfield_decl picks it from there later and sets DECL_SIZE
1102 accordingly. */
1103 DECL_BIT_FIELD_REPRESENTATIVE (value) = width;
1104 SET_DECL_C_BIT_FIELD (value);
1108 DECL_IN_AGGR_P (value) = 1;
1110 if (attrlist)
1111 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1113 return value;
1117 /* Returns true iff ATTR is an attribute which needs to be applied at
1118 instantiation time rather than template definition time. */
1120 static bool
1121 is_late_template_attribute (tree attr, tree decl)
1123 tree name = get_attribute_name (attr);
1124 tree args = TREE_VALUE (attr);
1125 const struct attribute_spec *spec = lookup_attribute_spec (name);
1126 tree arg;
1128 if (!spec)
1129 /* Unknown attribute. */
1130 return false;
1132 /* Attribute weak handling wants to write out assembly right away. */
1133 if (is_attribute_p ("weak", name))
1134 return true;
1136 /* Attributes used and unused are applied directly, as they appertain to
1137 decls. */
1138 if (is_attribute_p ("unused", name)
1139 || is_attribute_p ("used", name))
1140 return false;
1142 /* Attribute tls_model wants to modify the symtab. */
1143 if (is_attribute_p ("tls_model", name))
1144 return true;
1146 /* #pragma omp declare simd attribute needs to be always deferred. */
1147 if (flag_openmp
1148 && is_attribute_p ("omp declare simd", name))
1149 return true;
1151 /* An attribute pack is clearly dependent. */
1152 if (args && PACK_EXPANSION_P (args))
1153 return true;
1155 /* If any of the arguments are dependent expressions, we can't evaluate
1156 the attribute until instantiation time. */
1157 for (arg = args; arg; arg = TREE_CHAIN (arg))
1159 tree t = TREE_VALUE (arg);
1161 /* If the first attribute argument is an identifier, only consider
1162 second and following arguments. Attributes like mode, format,
1163 cleanup and several target specific attributes aren't late
1164 just because they have an IDENTIFIER_NODE as first argument. */
1165 if (arg == args && attribute_takes_identifier_p (name)
1166 && identifier_p (t))
1167 continue;
1169 if (value_dependent_expression_p (t)
1170 || type_dependent_expression_p (t))
1171 return true;
1174 if (TREE_CODE (decl) == TYPE_DECL
1175 || TYPE_P (decl)
1176 || spec->type_required)
1178 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1180 /* We can't apply any attributes to a completely unknown type until
1181 instantiation time. */
1182 enum tree_code code = TREE_CODE (type);
1183 if (code == TEMPLATE_TYPE_PARM
1184 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1185 || code == TYPENAME_TYPE)
1186 return true;
1187 /* Also defer most attributes on dependent types. This is not
1188 necessary in all cases, but is the better default. */
1189 else if (dependent_type_p (type)
1190 /* But some attributes specifically apply to templates. */
1191 && !is_attribute_p ("abi_tag", name)
1192 && !is_attribute_p ("deprecated", name)
1193 && !is_attribute_p ("visibility", name))
1194 return true;
1195 else
1196 return false;
1198 else
1199 return false;
1202 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1203 applied at instantiation time and return them. If IS_DEPENDENT is true,
1204 the declaration itself is dependent, so all attributes should be applied
1205 at instantiation time. */
1207 static tree
1208 splice_template_attributes (tree *attr_p, tree decl)
1210 tree *p = attr_p;
1211 tree late_attrs = NULL_TREE;
1212 tree *q = &late_attrs;
1214 if (!p)
1215 return NULL_TREE;
1217 for (; *p; )
1219 if (is_late_template_attribute (*p, decl))
1221 ATTR_IS_DEPENDENT (*p) = 1;
1222 *q = *p;
1223 *p = TREE_CHAIN (*p);
1224 q = &TREE_CHAIN (*q);
1225 *q = NULL_TREE;
1227 else
1228 p = &TREE_CHAIN (*p);
1231 return late_attrs;
1234 /* Remove any late attributes from the list in ATTR_P and attach them to
1235 DECL_P. */
1237 static void
1238 save_template_attributes (tree *attr_p, tree *decl_p)
1240 tree *q;
1242 if (attr_p && *attr_p == error_mark_node)
1243 return;
1245 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1246 if (!late_attrs)
1247 return;
1249 if (DECL_P (*decl_p))
1250 q = &DECL_ATTRIBUTES (*decl_p);
1251 else
1252 q = &TYPE_ATTRIBUTES (*decl_p);
1254 tree old_attrs = *q;
1256 /* Merge the late attributes at the beginning with the attribute
1257 list. */
1258 late_attrs = merge_attributes (late_attrs, *q);
1259 *q = late_attrs;
1261 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1263 /* We've added new attributes directly to the main variant, so
1264 now we need to update all of the other variants to include
1265 these new attributes. */
1266 tree variant;
1267 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1268 variant = TYPE_NEXT_VARIANT (variant))
1270 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1271 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1276 /* True if ATTRS contains any dependent attributes that affect type
1277 identity. */
1279 bool
1280 any_dependent_type_attributes_p (tree attrs)
1282 for (tree a = attrs; a; a = TREE_CHAIN (a))
1283 if (ATTR_IS_DEPENDENT (a))
1285 const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (a));
1286 if (as && as->affects_type_identity)
1287 return true;
1289 return false;
1292 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1293 to a typedef which gives a previously unnamed class or enum a name for
1294 linkage purposes. */
1296 bool
1297 attributes_naming_typedef_ok (tree attrs)
1299 for (; attrs; attrs = TREE_CHAIN (attrs))
1301 tree name = get_attribute_name (attrs);
1302 if (is_attribute_p ("vector_size", name))
1303 return false;
1305 return true;
1308 /* Like reconstruct_complex_type, but handle also template trees. */
1310 tree
1311 cp_reconstruct_complex_type (tree type, tree bottom)
1313 tree inner, outer;
1314 bool late_return_type_p = false;
1316 if (TYPE_PTR_P (type))
1318 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1319 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1320 TYPE_REF_CAN_ALIAS_ALL (type));
1322 else if (TREE_CODE (type) == REFERENCE_TYPE)
1324 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1325 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1326 TYPE_REF_CAN_ALIAS_ALL (type));
1328 else if (TREE_CODE (type) == ARRAY_TYPE)
1330 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1331 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1332 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1333 element type qualification will be handled by the recursive
1334 cp_reconstruct_complex_type call and cp_build_qualified_type
1335 for ARRAY_TYPEs changes the element type. */
1336 return outer;
1338 else if (TREE_CODE (type) == FUNCTION_TYPE)
1340 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1341 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1342 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1343 outer = apply_memfn_quals (outer,
1344 type_memfn_quals (type),
1345 type_memfn_rqual (type));
1347 else if (TREE_CODE (type) == METHOD_TYPE)
1349 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1350 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1351 /* The build_method_type_directly() routine prepends 'this' to argument list,
1352 so we must compensate by getting rid of it. */
1353 outer
1354 = build_method_type_directly
1355 (class_of_this_parm (type), inner,
1356 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1358 else if (TREE_CODE (type) == OFFSET_TYPE)
1360 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1361 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1363 else
1364 return bottom;
1366 if (TYPE_ATTRIBUTES (type))
1367 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1368 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1370 if (late_return_type_p)
1371 TYPE_HAS_LATE_RETURN_TYPE (outer) = 1;
1373 return outer;
1376 /* Replaces any constexpr expression that may be into the attributes
1377 arguments with their reduced value. */
1379 static void
1380 cp_check_const_attributes (tree attributes)
1382 if (attributes == error_mark_node)
1383 return;
1385 tree attr;
1386 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1388 tree arg;
1389 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1391 tree expr = TREE_VALUE (arg);
1392 if (EXPR_P (expr))
1393 TREE_VALUE (arg) = maybe_constant_value (expr);
1398 /* Return true if TYPE is an OpenMP mappable type. */
1399 bool
1400 cp_omp_mappable_type (tree type)
1402 /* Mappable type has to be complete. */
1403 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1404 return false;
1405 /* Arrays have mappable type if the elements have mappable type. */
1406 while (TREE_CODE (type) == ARRAY_TYPE)
1407 type = TREE_TYPE (type);
1408 /* A mappable type cannot contain virtual members. */
1409 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1410 return false;
1411 /* All data members must be non-static. */
1412 if (CLASS_TYPE_P (type))
1414 tree field;
1415 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1416 if (VAR_P (field))
1417 return false;
1418 /* All fields must have mappable types. */
1419 else if (TREE_CODE (field) == FIELD_DECL
1420 && !cp_omp_mappable_type (TREE_TYPE (field)))
1421 return false;
1423 return true;
1426 /* Like decl_attributes, but handle C++ complexity. */
1428 void
1429 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1431 if (*decl == NULL_TREE || *decl == void_type_node
1432 || *decl == error_mark_node)
1433 return;
1435 /* Add implicit "omp declare target" attribute if requested. */
1436 if (scope_chain->omp_declare_target_attribute
1437 && ((VAR_P (*decl)
1438 && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
1439 || TREE_CODE (*decl) == FUNCTION_DECL))
1441 if (VAR_P (*decl)
1442 && DECL_CLASS_SCOPE_P (*decl))
1443 error ("%q+D static data member inside of declare target directive",
1444 *decl);
1445 else if (!processing_template_decl
1446 && VAR_P (*decl)
1447 && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1448 error ("%q+D in declare target directive does not have mappable type",
1449 *decl);
1450 else
1451 attributes = tree_cons (get_identifier ("omp declare target"),
1452 NULL_TREE, attributes);
1455 if (processing_template_decl)
1457 if (check_for_bare_parameter_packs (attributes))
1458 return;
1460 save_template_attributes (&attributes, decl);
1463 cp_check_const_attributes (attributes);
1465 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1466 decl = &DECL_TEMPLATE_RESULT (*decl);
1468 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1470 attributes
1471 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1472 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1473 attributes, flags);
1475 else
1476 decl_attributes (decl, attributes, flags);
1478 if (TREE_CODE (*decl) == TYPE_DECL)
1479 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1481 /* Propagate deprecation out to the template. */
1482 if (TREE_DEPRECATED (*decl))
1483 if (tree ti = get_template_info (*decl))
1485 tree tmpl = TI_TEMPLATE (ti);
1486 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1487 : DECL_TEMPLATE_RESULT (tmpl));
1488 if (*decl == pattern)
1489 TREE_DEPRECATED (tmpl) = true;
1493 /* Walks through the namespace- or function-scope anonymous union
1494 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1495 Returns one of the fields for use in the mangled name. */
1497 static tree
1498 build_anon_union_vars (tree type, tree object)
1500 tree main_decl = NULL_TREE;
1501 tree field;
1503 /* Rather than write the code to handle the non-union case,
1504 just give an error. */
1505 if (TREE_CODE (type) != UNION_TYPE)
1507 error ("anonymous struct not inside named type");
1508 return error_mark_node;
1511 for (field = TYPE_FIELDS (type);
1512 field != NULL_TREE;
1513 field = DECL_CHAIN (field))
1515 tree decl;
1516 tree ref;
1518 if (DECL_ARTIFICIAL (field))
1519 continue;
1520 if (TREE_CODE (field) != FIELD_DECL)
1522 permerror (DECL_SOURCE_LOCATION (field),
1523 "%q#D invalid; an anonymous union can only "
1524 "have non-static data members", field);
1525 continue;
1528 if (TREE_PRIVATE (field))
1529 permerror (DECL_SOURCE_LOCATION (field),
1530 "private member %q#D in anonymous union", field);
1531 else if (TREE_PROTECTED (field))
1532 permerror (DECL_SOURCE_LOCATION (field),
1533 "protected member %q#D in anonymous union", field);
1535 if (processing_template_decl)
1536 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1537 DECL_NAME (field), NULL_TREE);
1538 else
1539 ref = build_class_member_access_expr (object, field, NULL_TREE,
1540 false, tf_warning_or_error);
1542 if (DECL_NAME (field))
1544 tree base;
1546 decl = build_decl (input_location,
1547 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1548 DECL_ANON_UNION_VAR_P (decl) = 1;
1549 DECL_ARTIFICIAL (decl) = 1;
1551 base = get_base_address (object);
1552 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1553 TREE_STATIC (decl) = TREE_STATIC (base);
1554 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1556 SET_DECL_VALUE_EXPR (decl, ref);
1557 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1559 decl = pushdecl (decl);
1561 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1562 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1563 else
1564 decl = 0;
1566 if (main_decl == NULL_TREE)
1567 main_decl = decl;
1570 return main_decl;
1573 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1574 anonymous union, then all members must be laid out together. PUBLIC_P
1575 is nonzero if this union is not declared static. */
1577 void
1578 finish_anon_union (tree anon_union_decl)
1580 tree type;
1581 tree main_decl;
1582 bool public_p;
1584 if (anon_union_decl == error_mark_node)
1585 return;
1587 type = TREE_TYPE (anon_union_decl);
1588 public_p = TREE_PUBLIC (anon_union_decl);
1590 /* The VAR_DECL's context is the same as the TYPE's context. */
1591 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1593 if (TYPE_FIELDS (type) == NULL_TREE)
1594 return;
1596 if (public_p)
1598 error ("namespace-scope anonymous aggregates must be static");
1599 return;
1602 main_decl = build_anon_union_vars (type, anon_union_decl);
1603 if (main_decl == error_mark_node)
1604 return;
1605 if (main_decl == NULL_TREE)
1607 pedwarn (input_location, 0, "anonymous union with no members");
1608 return;
1611 if (!processing_template_decl)
1613 /* Use main_decl to set the mangled name. */
1614 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1615 maybe_commonize_var (anon_union_decl);
1616 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1617 mangle_decl (anon_union_decl);
1618 DECL_NAME (anon_union_decl) = NULL_TREE;
1621 pushdecl (anon_union_decl);
1622 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1625 /* Auxiliary functions to make type signatures for
1626 `operator new' and `operator delete' correspond to
1627 what compiler will be expecting. */
1629 tree
1630 coerce_new_type (tree type)
1632 int e = 0;
1633 tree args = TYPE_ARG_TYPES (type);
1635 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1637 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1639 e = 1;
1640 error ("%<operator new%> must return type %qT", ptr_type_node);
1643 if (args && args != void_list_node)
1645 if (TREE_PURPOSE (args))
1647 /* [basic.stc.dynamic.allocation]
1649 The first parameter shall not have an associated default
1650 argument. */
1651 error ("the first parameter of %<operator new%> cannot "
1652 "have a default argument");
1653 /* Throw away the default argument. */
1654 TREE_PURPOSE (args) = NULL_TREE;
1657 if (!same_type_p (TREE_VALUE (args), size_type_node))
1659 e = 2;
1660 args = TREE_CHAIN (args);
1663 else
1664 e = 2;
1666 if (e == 2)
1667 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1668 "as first parameter", size_type_node);
1670 switch (e)
1672 case 2:
1673 args = tree_cons (NULL_TREE, size_type_node, args);
1674 /* Fall through. */
1675 case 1:
1676 type = build_exception_variant
1677 (build_function_type (ptr_type_node, args),
1678 TYPE_RAISES_EXCEPTIONS (type));
1679 /* Fall through. */
1680 default:;
1682 return type;
1685 tree
1686 coerce_delete_type (tree type)
1688 int e = 0;
1689 tree args = TYPE_ARG_TYPES (type);
1691 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1693 if (!same_type_p (TREE_TYPE (type), void_type_node))
1695 e = 1;
1696 error ("%<operator delete%> must return type %qT", void_type_node);
1699 if (!args || args == void_list_node
1700 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1702 e = 2;
1703 if (args && args != void_list_node)
1704 args = TREE_CHAIN (args);
1705 error ("%<operator delete%> takes type %qT as first parameter",
1706 ptr_type_node);
1708 switch (e)
1710 case 2:
1711 args = tree_cons (NULL_TREE, ptr_type_node, args);
1712 /* Fall through. */
1713 case 1:
1714 type = build_exception_variant
1715 (build_function_type (void_type_node, args),
1716 TYPE_RAISES_EXCEPTIONS (type));
1717 /* Fall through. */
1718 default:;
1721 return type;
1724 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1725 and mark them as needed. */
1727 static void
1728 mark_vtable_entries (tree decl)
1730 tree fnaddr;
1731 unsigned HOST_WIDE_INT idx;
1733 /* It's OK for the vtable to refer to deprecated virtual functions. */
1734 warning_sentinel w(warn_deprecated_decl);
1736 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1737 idx, fnaddr)
1739 tree fn;
1741 STRIP_NOPS (fnaddr);
1743 if (TREE_CODE (fnaddr) != ADDR_EXPR
1744 && TREE_CODE (fnaddr) != FDESC_EXPR)
1745 /* This entry is an offset: a virtual base class offset, a
1746 virtual call offset, an RTTI offset, etc. */
1747 continue;
1749 fn = TREE_OPERAND (fnaddr, 0);
1750 TREE_ADDRESSABLE (fn) = 1;
1751 /* When we don't have vcall offsets, we output thunks whenever
1752 we output the vtables that contain them. With vcall offsets,
1753 we know all the thunks we'll need when we emit a virtual
1754 function, so we emit the thunks there instead. */
1755 if (DECL_THUNK_P (fn))
1756 use_thunk (fn, /*emit_p=*/0);
1757 mark_used (fn);
1761 /* Set DECL up to have the closest approximation of "initialized common"
1762 linkage available. */
1764 void
1765 comdat_linkage (tree decl)
1767 if (flag_weak)
1768 make_decl_one_only (decl, cxx_comdat_group (decl));
1769 else if (TREE_CODE (decl) == FUNCTION_DECL
1770 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1771 /* We can just emit function and compiler-generated variables
1772 statically; having multiple copies is (for the most part) only
1773 a waste of space.
1775 There are two correctness issues, however: the address of a
1776 template instantiation with external linkage should be the
1777 same, independent of what translation unit asks for the
1778 address, and this will not hold when we emit multiple copies of
1779 the function. However, there's little else we can do.
1781 Also, by default, the typeinfo implementation assumes that
1782 there will be only one copy of the string used as the name for
1783 each type. Therefore, if weak symbols are unavailable, the
1784 run-time library should perform a more conservative check; it
1785 should perform a string comparison, rather than an address
1786 comparison. */
1787 TREE_PUBLIC (decl) = 0;
1788 else
1790 /* Static data member template instantiations, however, cannot
1791 have multiple copies. */
1792 if (DECL_INITIAL (decl) == 0
1793 || DECL_INITIAL (decl) == error_mark_node)
1794 DECL_COMMON (decl) = 1;
1795 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1797 DECL_COMMON (decl) = 1;
1798 DECL_INITIAL (decl) = error_mark_node;
1800 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1802 /* We can't do anything useful; leave vars for explicit
1803 instantiation. */
1804 DECL_EXTERNAL (decl) = 1;
1805 DECL_NOT_REALLY_EXTERN (decl) = 0;
1809 if (TREE_PUBLIC (decl))
1810 DECL_COMDAT (decl) = 1;
1813 /* For win32 we also want to put explicit instantiations in
1814 linkonce sections, so that they will be merged with implicit
1815 instantiations; otherwise we get duplicate symbol errors.
1816 For Darwin we do not want explicit instantiations to be
1817 linkonce. */
1819 void
1820 maybe_make_one_only (tree decl)
1822 /* We used to say that this was not necessary on targets that support weak
1823 symbols, because the implicit instantiations will defer to the explicit
1824 one. However, that's not actually the case in SVR4; a strong definition
1825 after a weak one is an error. Also, not making explicit
1826 instantiations one_only means that we can end up with two copies of
1827 some template instantiations. */
1828 if (! flag_weak)
1829 return;
1831 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1832 we can get away with not emitting them if they aren't used. We need
1833 to for variables so that cp_finish_decl will update their linkage,
1834 because their DECL_INITIAL may not have been set properly yet. */
1836 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1837 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1838 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1840 make_decl_one_only (decl, cxx_comdat_group (decl));
1842 if (VAR_P (decl))
1844 varpool_node *node = varpool_node::get_create (decl);
1845 DECL_COMDAT (decl) = 1;
1846 /* Mark it needed so we don't forget to emit it. */
1847 node->forced_by_abi = true;
1848 TREE_USED (decl) = 1;
1853 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1854 This predicate will give the right answer during parsing of the
1855 function, which other tests may not. */
1857 bool
1858 vague_linkage_p (tree decl)
1860 if (!TREE_PUBLIC (decl))
1862 /* maybe_thunk_body clears TREE_PUBLIC on the maybe-in-charge 'tor
1863 variants, check one of the "clones" for the real linkage. */
1864 if ((DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
1865 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl))
1866 && DECL_CHAIN (decl)
1867 && DECL_CLONED_FUNCTION (DECL_CHAIN (decl)))
1868 return vague_linkage_p (DECL_CHAIN (decl));
1870 gcc_checking_assert (!DECL_COMDAT (decl));
1871 return false;
1873 /* Unfortunately, import_export_decl has not always been called
1874 before the function is processed, so we cannot simply check
1875 DECL_COMDAT. */
1876 if (DECL_COMDAT (decl)
1877 || (TREE_CODE (decl) == FUNCTION_DECL
1878 && DECL_DECLARED_INLINE_P (decl))
1879 || (DECL_LANG_SPECIFIC (decl)
1880 && DECL_TEMPLATE_INSTANTIATION (decl))
1881 || (VAR_P (decl) && DECL_INLINE_VAR_P (decl)))
1882 return true;
1883 else if (DECL_FUNCTION_SCOPE_P (decl))
1884 /* A local static in an inline effectively has vague linkage. */
1885 return (TREE_STATIC (decl)
1886 && vague_linkage_p (DECL_CONTEXT (decl)));
1887 else
1888 return false;
1891 /* Determine whether or not we want to specifically import or export CTYPE,
1892 using various heuristics. */
1894 static void
1895 import_export_class (tree ctype)
1897 /* -1 for imported, 1 for exported. */
1898 int import_export = 0;
1900 /* It only makes sense to call this function at EOF. The reason is
1901 that this function looks at whether or not the first non-inline
1902 non-abstract virtual member function has been defined in this
1903 translation unit. But, we can't possibly know that until we've
1904 seen the entire translation unit. */
1905 gcc_assert (at_eof);
1907 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1908 return;
1910 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1911 we will have CLASSTYPE_INTERFACE_ONLY set but not
1912 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1913 heuristic because someone will supply a #pragma implementation
1914 elsewhere, and deducing it here would produce a conflict. */
1915 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1916 return;
1918 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1919 import_export = -1;
1920 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1921 import_export = 1;
1922 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1923 && !flag_implicit_templates)
1924 /* For a template class, without -fimplicit-templates, check the
1925 repository. If the virtual table is assigned to this
1926 translation unit, then export the class; otherwise, import
1927 it. */
1928 import_export = repo_export_class_p (ctype) ? 1 : -1;
1929 else if (TYPE_POLYMORPHIC_P (ctype))
1931 /* The ABI specifies that the virtual table and associated
1932 information are emitted with the key method, if any. */
1933 tree method = CLASSTYPE_KEY_METHOD (ctype);
1934 /* If weak symbol support is not available, then we must be
1935 careful not to emit the vtable when the key function is
1936 inline. An inline function can be defined in multiple
1937 translation units. If we were to emit the vtable in each
1938 translation unit containing a definition, we would get
1939 multiple definition errors at link-time. */
1940 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1941 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1944 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1945 a definition anywhere else. */
1946 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1947 import_export = 0;
1949 /* Allow back ends the chance to overrule the decision. */
1950 if (targetm.cxx.import_export_class)
1951 import_export = targetm.cxx.import_export_class (ctype, import_export);
1953 if (import_export)
1955 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1956 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1960 /* Return true if VAR has already been provided to the back end; in that
1961 case VAR should not be modified further by the front end. */
1962 static bool
1963 var_finalized_p (tree var)
1965 return varpool_node::get_create (var)->definition;
1968 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1969 must be emitted in this translation unit. Mark it as such. */
1971 void
1972 mark_needed (tree decl)
1974 TREE_USED (decl) = 1;
1975 if (TREE_CODE (decl) == FUNCTION_DECL)
1977 /* Extern inline functions don't become needed when referenced.
1978 If we know a method will be emitted in other TU and no new
1979 functions can be marked reachable, just use the external
1980 definition. */
1981 struct cgraph_node *node = cgraph_node::get_create (decl);
1982 node->forced_by_abi = true;
1984 /* #pragma interface and -frepo code can call mark_needed for
1985 maybe-in-charge 'tors; mark the clones as well. */
1986 tree clone;
1987 FOR_EACH_CLONE (clone, decl)
1988 mark_needed (clone);
1990 else if (VAR_P (decl))
1992 varpool_node *node = varpool_node::get_create (decl);
1993 /* C++ frontend use mark_decl_references to force COMDAT variables
1994 to be output that might appear dead otherwise. */
1995 node->forced_by_abi = true;
1999 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
2000 returns true if a definition of this entity should be provided in
2001 this object file. Callers use this function to determine whether
2002 or not to let the back end know that a definition of DECL is
2003 available in this translation unit. */
2005 bool
2006 decl_needed_p (tree decl)
2008 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2009 /* This function should only be called at the end of the translation
2010 unit. We cannot be sure of whether or not something will be
2011 COMDAT until that point. */
2012 gcc_assert (at_eof);
2014 /* All entities with external linkage that are not COMDAT/EXTERN should be
2015 emitted; they may be referred to from other object files. */
2016 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
2017 return true;
2018 /* Functions marked "dllexport" must be emitted so that they are
2019 visible to other DLLs. */
2020 if (flag_keep_inline_dllexport
2021 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
2022 return true;
2024 /* When not optimizing, do not bother to produce definitions for extern
2025 symbols. */
2026 if (DECL_REALLY_EXTERN (decl)
2027 && ((TREE_CODE (decl) != FUNCTION_DECL
2028 && !optimize)
2029 || (TREE_CODE (decl) == FUNCTION_DECL
2030 && !opt_for_fn (decl, optimize)))
2031 && !lookup_attribute ("always_inline", decl))
2032 return false;
2034 /* If this entity was used, let the back end see it; it will decide
2035 whether or not to emit it into the object file. */
2036 if (TREE_USED (decl))
2037 return true;
2039 /* Virtual functions might be needed for devirtualization. */
2040 if (flag_devirtualize
2041 && TREE_CODE (decl) == FUNCTION_DECL
2042 && DECL_VIRTUAL_P (decl))
2043 return true;
2045 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2046 reference to DECL might cause it to be emitted later. */
2047 return false;
2050 /* If necessary, write out the vtables for the dynamic class CTYPE.
2051 Returns true if any vtables were emitted. */
2053 static bool
2054 maybe_emit_vtables (tree ctype)
2056 tree vtbl;
2057 tree primary_vtbl;
2058 int needed = 0;
2059 varpool_node *current = NULL, *last = NULL;
2061 /* If the vtables for this class have already been emitted there is
2062 nothing more to do. */
2063 primary_vtbl = CLASSTYPE_VTABLES (ctype);
2064 if (var_finalized_p (primary_vtbl))
2065 return false;
2066 /* Ignore dummy vtables made by get_vtable_decl. */
2067 if (TREE_TYPE (primary_vtbl) == void_type_node)
2068 return false;
2070 /* On some targets, we cannot determine the key method until the end
2071 of the translation unit -- which is when this function is
2072 called. */
2073 if (!targetm.cxx.key_method_may_be_inline ())
2074 determine_key_method (ctype);
2076 /* See if any of the vtables are needed. */
2077 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2079 import_export_decl (vtbl);
2080 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2081 needed = 1;
2083 if (!needed)
2085 /* If the references to this class' vtables are optimized away,
2086 still emit the appropriate debugging information. See
2087 dfs_debug_mark. */
2088 if (DECL_COMDAT (primary_vtbl)
2089 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2090 note_debug_info_needed (ctype);
2091 return false;
2094 /* The ABI requires that we emit all of the vtables if we emit any
2095 of them. */
2096 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2098 /* Mark entities references from the virtual table as used. */
2099 mark_vtable_entries (vtbl);
2101 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2103 vec<tree, va_gc> *cleanups = NULL;
2104 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2105 LOOKUP_NORMAL);
2107 /* It had better be all done at compile-time. */
2108 gcc_assert (!expr && !cleanups);
2111 /* Write it out. */
2112 DECL_EXTERNAL (vtbl) = 0;
2113 rest_of_decl_compilation (vtbl, 1, 1);
2115 /* Because we're only doing syntax-checking, we'll never end up
2116 actually marking the variable as written. */
2117 if (flag_syntax_only)
2118 TREE_ASM_WRITTEN (vtbl) = 1;
2119 else if (DECL_ONE_ONLY (vtbl))
2121 current = varpool_node::get_create (vtbl);
2122 if (last)
2123 current->add_to_same_comdat_group (last);
2124 last = current;
2128 /* Since we're writing out the vtable here, also write the debug
2129 info. */
2130 note_debug_info_needed (ctype);
2132 return true;
2135 /* A special return value from type_visibility meaning internal
2136 linkage. */
2138 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2140 /* walk_tree helper function for type_visibility. */
2142 static tree
2143 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2145 int *vis_p = (int *)data;
2146 if (! TYPE_P (*tp))
2148 *walk_subtrees = 0;
2150 else if (OVERLOAD_TYPE_P (*tp)
2151 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2153 *vis_p = VISIBILITY_ANON;
2154 return *tp;
2156 else if (CLASS_TYPE_P (*tp)
2157 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2158 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2159 return NULL;
2162 /* Returns the visibility of TYPE, which is the minimum visibility of its
2163 component types. */
2165 static int
2166 type_visibility (tree type)
2168 int vis = VISIBILITY_DEFAULT;
2169 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2170 return vis;
2173 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2174 specified (or if VISIBILITY is static). If TMPL is true, this
2175 constraint is for a template argument, and takes precedence
2176 over explicitly-specified visibility on the template. */
2178 static void
2179 constrain_visibility (tree decl, int visibility, bool tmpl)
2181 if (visibility == VISIBILITY_ANON)
2183 /* extern "C" declarations aren't affected by the anonymous
2184 namespace. */
2185 if (!DECL_EXTERN_C_P (decl))
2187 TREE_PUBLIC (decl) = 0;
2188 DECL_WEAK (decl) = 0;
2189 DECL_COMMON (decl) = 0;
2190 DECL_COMDAT (decl) = false;
2191 if (VAR_OR_FUNCTION_DECL_P (decl))
2193 struct symtab_node *snode = symtab_node::get (decl);
2195 if (snode)
2196 snode->set_comdat_group (NULL);
2198 DECL_INTERFACE_KNOWN (decl) = 1;
2199 if (DECL_LANG_SPECIFIC (decl))
2200 DECL_NOT_REALLY_EXTERN (decl) = 1;
2203 else if (visibility > DECL_VISIBILITY (decl)
2204 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2206 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2207 /* This visibility was not specified. */
2208 DECL_VISIBILITY_SPECIFIED (decl) = false;
2212 /* Constrain the visibility of DECL based on the visibility of its template
2213 arguments. */
2215 static void
2216 constrain_visibility_for_template (tree decl, tree targs)
2218 /* If this is a template instantiation, check the innermost
2219 template args for visibility constraints. The outer template
2220 args are covered by the class check. */
2221 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2222 int i;
2223 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2225 int vis = 0;
2227 tree arg = TREE_VEC_ELT (args, i-1);
2228 if (TYPE_P (arg))
2229 vis = type_visibility (arg);
2230 else
2232 if (REFERENCE_REF_P (arg))
2233 arg = TREE_OPERAND (arg, 0);
2234 if (TREE_TYPE (arg))
2235 STRIP_NOPS (arg);
2236 if (TREE_CODE (arg) == ADDR_EXPR)
2237 arg = TREE_OPERAND (arg, 0);
2238 if (VAR_OR_FUNCTION_DECL_P (arg))
2240 if (! TREE_PUBLIC (arg))
2241 vis = VISIBILITY_ANON;
2242 else
2243 vis = DECL_VISIBILITY (arg);
2246 if (vis)
2247 constrain_visibility (decl, vis, true);
2251 /* Like c_determine_visibility, but with additional C++-specific
2252 behavior.
2254 Function-scope entities can rely on the function's visibility because
2255 it is set in start_preparsed_function.
2257 Class-scope entities cannot rely on the class's visibility until the end
2258 of the enclosing class definition.
2260 Note that because namespaces have multiple independent definitions,
2261 namespace visibility is handled elsewhere using the #pragma visibility
2262 machinery rather than by decorating the namespace declaration.
2264 The goal is for constraints from the type to give a diagnostic, and
2265 other constraints to be applied silently. */
2267 void
2268 determine_visibility (tree decl)
2270 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2272 /* Only relevant for names with external linkage. */
2273 if (!TREE_PUBLIC (decl))
2274 return;
2276 /* Cloned constructors and destructors get the same visibility as
2277 the underlying function. That should be set up in
2278 maybe_clone_body. */
2279 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2281 bool orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2282 enum symbol_visibility orig_visibility = DECL_VISIBILITY (decl);
2284 /* The decl may be a template instantiation, which could influence
2285 visibilty. */
2286 tree template_decl = NULL_TREE;
2287 if (TREE_CODE (decl) == TYPE_DECL)
2289 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2291 if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
2292 template_decl = decl;
2294 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2295 template_decl = decl;
2297 else if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
2298 template_decl = decl;
2300 /* If DECL is a member of a class, visibility specifiers on the
2301 class can influence the visibility of the DECL. */
2302 tree class_type = NULL_TREE;
2303 if (DECL_CLASS_SCOPE_P (decl))
2304 class_type = DECL_CONTEXT (decl);
2305 else
2307 /* Not a class member. */
2309 /* Virtual tables have DECL_CONTEXT set to their associated class,
2310 so they are automatically handled above. */
2311 gcc_assert (!VAR_P (decl)
2312 || !DECL_VTABLE_OR_VTT_P (decl));
2314 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2316 /* Local statics and classes get the visibility of their
2317 containing function by default, except that
2318 -fvisibility-inlines-hidden doesn't affect them. */
2319 tree fn = DECL_CONTEXT (decl);
2320 if (DECL_VISIBILITY_SPECIFIED (fn))
2322 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2323 DECL_VISIBILITY_SPECIFIED (decl) =
2324 DECL_VISIBILITY_SPECIFIED (fn);
2326 else
2328 if (DECL_CLASS_SCOPE_P (fn))
2329 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2330 else if (determine_hidden_inline (fn))
2332 DECL_VISIBILITY (decl) = default_visibility;
2333 DECL_VISIBILITY_SPECIFIED (decl) =
2334 visibility_options.inpragma;
2336 else
2338 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2339 DECL_VISIBILITY_SPECIFIED (decl) =
2340 DECL_VISIBILITY_SPECIFIED (fn);
2344 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2345 but have no TEMPLATE_INFO. Their containing template
2346 function does, and the local class could be constrained
2347 by that. */
2348 if (template_decl)
2349 template_decl = fn;
2351 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2352 && flag_visibility_ms_compat)
2354 /* Under -fvisibility-ms-compat, types are visible by default,
2355 even though their contents aren't. */
2356 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2357 int underlying_vis = type_visibility (underlying_type);
2358 if (underlying_vis == VISIBILITY_ANON
2359 || (CLASS_TYPE_P (underlying_type)
2360 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2361 constrain_visibility (decl, underlying_vis, false);
2362 else
2363 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2365 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2367 /* tinfo visibility is based on the type it's for. */
2368 constrain_visibility
2369 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2371 /* Give the target a chance to override the visibility associated
2372 with DECL. */
2373 if (TREE_PUBLIC (decl)
2374 && !DECL_REALLY_EXTERN (decl)
2375 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2376 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2377 targetm.cxx.determine_class_data_visibility (decl);
2379 else if (template_decl)
2380 /* Template instantiations and specializations get visibility based
2381 on their template unless they override it with an attribute. */;
2382 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2384 if (determine_hidden_inline (decl))
2385 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2386 else
2388 /* Set default visibility to whatever the user supplied with
2389 #pragma GCC visibility or a namespace visibility attribute. */
2390 DECL_VISIBILITY (decl) = default_visibility;
2391 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2396 if (template_decl)
2398 /* If the specialization doesn't specify visibility, use the
2399 visibility from the template. */
2400 tree tinfo = get_template_info (template_decl);
2401 tree args = TI_ARGS (tinfo);
2402 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2403 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2404 : DECL_ATTRIBUTES (decl));
2406 if (args != error_mark_node)
2408 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2410 if (!DECL_VISIBILITY_SPECIFIED (decl))
2412 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2413 && determine_hidden_inline (decl))
2414 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2415 else
2417 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2418 DECL_VISIBILITY_SPECIFIED (decl)
2419 = DECL_VISIBILITY_SPECIFIED (pattern);
2423 if (args
2424 /* Template argument visibility outweighs #pragma or namespace
2425 visibility, but not an explicit attribute. */
2426 && !lookup_attribute ("visibility", attribs))
2428 int depth = TMPL_ARGS_DEPTH (args);
2429 if (DECL_VISIBILITY_SPECIFIED (decl))
2431 /* A class template member with explicit visibility
2432 overrides the class visibility, so we need to apply
2433 all the levels of template args directly. */
2434 int i;
2435 for (i = 1; i <= depth; ++i)
2437 tree lev = TMPL_ARGS_LEVEL (args, i);
2438 constrain_visibility_for_template (decl, lev);
2441 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2442 /* Limit visibility based on its template arguments. */
2443 constrain_visibility_for_template (decl, args);
2448 if (class_type)
2449 determine_visibility_from_class (decl, class_type);
2451 if (decl_anon_ns_mem_p (decl))
2452 /* Names in an anonymous namespace get internal linkage.
2453 This might change once we implement export. */
2454 constrain_visibility (decl, VISIBILITY_ANON, false);
2455 else if (TREE_CODE (decl) != TYPE_DECL)
2457 /* Propagate anonymity from type to decl. */
2458 int tvis = type_visibility (TREE_TYPE (decl));
2459 if (tvis == VISIBILITY_ANON
2460 || ! DECL_VISIBILITY_SPECIFIED (decl))
2461 constrain_visibility (decl, tvis, false);
2463 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2464 /* DR 757: A type without linkage shall not be used as the type of a
2465 variable or function with linkage, unless
2466 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2467 o the variable or function is not used (3.2 [basic.def.odr]) or is
2468 defined in the same translation unit.
2470 Since non-extern "C" decls need to be defined in the same
2471 translation unit, we can make the type internal. */
2472 constrain_visibility (decl, VISIBILITY_ANON, false);
2474 /* If visibility changed and DECL already has DECL_RTL, ensure
2475 symbol flags are updated. */
2476 if ((DECL_VISIBILITY (decl) != orig_visibility
2477 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2478 && ((VAR_P (decl) && TREE_STATIC (decl))
2479 || TREE_CODE (decl) == FUNCTION_DECL)
2480 && DECL_RTL_SET_P (decl))
2481 make_decl_rtl (decl);
2484 /* By default, static data members and function members receive
2485 the visibility of their containing class. */
2487 static void
2488 determine_visibility_from_class (tree decl, tree class_type)
2490 if (DECL_VISIBILITY_SPECIFIED (decl))
2491 return;
2493 if (determine_hidden_inline (decl))
2494 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2495 else
2497 /* Default to the class visibility. */
2498 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2499 DECL_VISIBILITY_SPECIFIED (decl)
2500 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2503 /* Give the target a chance to override the visibility associated
2504 with DECL. */
2505 if (VAR_P (decl)
2506 && (DECL_TINFO_P (decl)
2507 || (DECL_VTABLE_OR_VTT_P (decl)
2508 /* Construction virtual tables are not exported because
2509 they cannot be referred to from other object files;
2510 their name is not standardized by the ABI. */
2511 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2512 && TREE_PUBLIC (decl)
2513 && !DECL_REALLY_EXTERN (decl)
2514 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2515 targetm.cxx.determine_class_data_visibility (decl);
2518 /* Returns true iff DECL is an inline that should get hidden visibility
2519 because of -fvisibility-inlines-hidden. */
2521 static bool
2522 determine_hidden_inline (tree decl)
2524 return (visibility_options.inlines_hidden
2525 /* Don't do this for inline templates; specializations might not be
2526 inline, and we don't want them to inherit the hidden
2527 visibility. We'll set it here for all inline instantiations. */
2528 && !processing_template_decl
2529 && TREE_CODE (decl) == FUNCTION_DECL
2530 && DECL_DECLARED_INLINE_P (decl)
2531 && (! DECL_LANG_SPECIFIC (decl)
2532 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2535 /* Constrain the visibility of a class TYPE based on the visibility of its
2536 field types. Warn if any fields require lesser visibility. */
2538 void
2539 constrain_class_visibility (tree type)
2541 tree binfo;
2542 tree t;
2543 int i;
2545 int vis = type_visibility (type);
2547 if (vis == VISIBILITY_ANON
2548 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2549 return;
2551 /* Don't warn about visibility if the class has explicit visibility. */
2552 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2553 vis = VISIBILITY_INTERNAL;
2555 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2556 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2558 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2559 int subvis = type_visibility (ftype);
2561 if (subvis == VISIBILITY_ANON)
2563 if (!in_main_input_context())
2565 tree nlt = no_linkage_check (ftype, /*relaxed_p=*/false);
2566 if (nlt)
2568 if (same_type_p (TREE_TYPE (t), nlt))
2569 warning (OPT_Wsubobject_linkage, "\
2570 %qT has a field %qD whose type has no linkage",
2571 type, t);
2572 else
2573 warning (OPT_Wsubobject_linkage, "\
2574 %qT has a field %qD whose type depends on the type %qT which has no linkage",
2575 type, t, nlt);
2577 else
2578 warning (OPT_Wsubobject_linkage, "\
2579 %qT has a field %qD whose type uses the anonymous namespace",
2580 type, t);
2583 else if (MAYBE_CLASS_TYPE_P (ftype)
2584 && vis < VISIBILITY_HIDDEN
2585 && subvis >= VISIBILITY_HIDDEN)
2586 warning (OPT_Wattributes, "\
2587 %qT declared with greater visibility than the type of its field %qD",
2588 type, t);
2591 binfo = TYPE_BINFO (type);
2592 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2594 int subvis = type_visibility (TREE_TYPE (t));
2596 if (subvis == VISIBILITY_ANON)
2598 if (!in_main_input_context())
2600 tree nlt = no_linkage_check (TREE_TYPE (t), /*relaxed_p=*/false);
2601 if (nlt)
2603 if (same_type_p (TREE_TYPE (t), nlt))
2604 warning (OPT_Wsubobject_linkage, "\
2605 %qT has a base %qT whose type has no linkage",
2606 type, TREE_TYPE (t));
2607 else
2608 warning (OPT_Wsubobject_linkage, "\
2609 %qT has a base %qT whose type depends on the type %qT which has no linkage",
2610 type, TREE_TYPE (t), nlt);
2612 else
2613 warning (OPT_Wsubobject_linkage, "\
2614 %qT has a base %qT whose type uses the anonymous namespace",
2615 type, TREE_TYPE (t));
2618 else if (vis < VISIBILITY_HIDDEN
2619 && subvis >= VISIBILITY_HIDDEN)
2620 warning (OPT_Wattributes, "\
2621 %qT declared with greater visibility than its base %qT",
2622 type, TREE_TYPE (t));
2626 /* Functions for adjusting the visibility of a tagged type and its nested
2627 types and declarations when it gets a name for linkage purposes from a
2628 typedef. */
2630 static void bt_reset_linkage_1 (binding_entry, void *);
2631 static void bt_reset_linkage_2 (binding_entry, void *);
2633 /* First reset the visibility of all the types. */
2635 static void
2636 reset_type_linkage_1 (tree type)
2638 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2639 if (CLASS_TYPE_P (type))
2640 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2641 bt_reset_linkage_1, NULL);
2643 static void
2644 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2646 reset_type_linkage_1 (b->type);
2649 /* Then reset the visibility of any static data members or member
2650 functions that use those types. */
2652 static void
2653 reset_decl_linkage (tree decl)
2655 if (TREE_PUBLIC (decl))
2656 return;
2657 if (DECL_CLONED_FUNCTION_P (decl))
2658 return;
2659 TREE_PUBLIC (decl) = true;
2660 DECL_INTERFACE_KNOWN (decl) = false;
2661 determine_visibility (decl);
2662 tentative_decl_linkage (decl);
2665 static void
2666 reset_type_linkage_2 (tree type)
2668 if (CLASS_TYPE_P (type))
2670 if (tree vt = CLASSTYPE_VTABLES (type))
2672 tree name = mangle_vtbl_for_type (type);
2673 DECL_NAME (vt) = name;
2674 SET_DECL_ASSEMBLER_NAME (vt, name);
2675 reset_decl_linkage (vt);
2677 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2679 tree name = mangle_typeinfo_for_type (type);
2680 DECL_NAME (ti) = name;
2681 SET_DECL_ASSEMBLER_NAME (ti, name);
2682 TREE_TYPE (name) = type;
2683 reset_decl_linkage (ti);
2685 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2687 tree mem = STRIP_TEMPLATE (m);
2688 if (TREE_CODE (mem) == VAR_DECL || TREE_CODE (mem) == FUNCTION_DECL)
2689 reset_decl_linkage (mem);
2691 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2692 bt_reset_linkage_2, NULL);
2696 static void
2697 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2699 reset_type_linkage_2 (b->type);
2701 void
2702 reset_type_linkage (tree type)
2704 reset_type_linkage_1 (type);
2705 reset_type_linkage_2 (type);
2708 /* Set up our initial idea of what the linkage of DECL should be. */
2710 void
2711 tentative_decl_linkage (tree decl)
2713 if (DECL_INTERFACE_KNOWN (decl))
2714 /* We've already made a decision as to how this function will
2715 be handled. */;
2716 else if (vague_linkage_p (decl))
2718 if (TREE_CODE (decl) == FUNCTION_DECL
2719 && decl_defined_p (decl))
2721 DECL_EXTERNAL (decl) = 1;
2722 DECL_NOT_REALLY_EXTERN (decl) = 1;
2723 note_vague_linkage_fn (decl);
2724 /* A non-template inline function with external linkage will
2725 always be COMDAT. As we must eventually determine the
2726 linkage of all functions, and as that causes writes to
2727 the data mapped in from the PCH file, it's advantageous
2728 to mark the functions at this point. */
2729 if (DECL_DECLARED_INLINE_P (decl)
2730 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2731 || DECL_DEFAULTED_FN (decl)))
2733 /* This function must have external linkage, as
2734 otherwise DECL_INTERFACE_KNOWN would have been
2735 set. */
2736 gcc_assert (TREE_PUBLIC (decl));
2737 comdat_linkage (decl);
2738 DECL_INTERFACE_KNOWN (decl) = 1;
2741 else if (VAR_P (decl))
2742 maybe_commonize_var (decl);
2746 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2747 for DECL has not already been determined, do so now by setting
2748 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2749 function is called entities with vague linkage whose definitions
2750 are available must have TREE_PUBLIC set.
2752 If this function decides to place DECL in COMDAT, it will set
2753 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2754 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2755 callers defer that decision until it is clear that DECL is actually
2756 required. */
2758 void
2759 import_export_decl (tree decl)
2761 int emit_p;
2762 bool comdat_p;
2763 bool import_p;
2764 tree class_type = NULL_TREE;
2766 if (DECL_INTERFACE_KNOWN (decl))
2767 return;
2769 /* We cannot determine what linkage to give to an entity with vague
2770 linkage until the end of the file. For example, a virtual table
2771 for a class will be defined if and only if the key method is
2772 defined in this translation unit. As a further example, consider
2773 that when compiling a translation unit that uses PCH file with
2774 "-frepo" it would be incorrect to make decisions about what
2775 entities to emit when building the PCH; those decisions must be
2776 delayed until the repository information has been processed. */
2777 gcc_assert (at_eof);
2778 /* Object file linkage for explicit instantiations is handled in
2779 mark_decl_instantiated. For static variables in functions with
2780 vague linkage, maybe_commonize_var is used.
2782 Therefore, the only declarations that should be provided to this
2783 function are those with external linkage that are:
2785 * implicit instantiations of function templates
2787 * inline function
2789 * implicit instantiations of static data members of class
2790 templates
2792 * virtual tables
2794 * typeinfo objects
2796 Furthermore, all entities that reach this point must have a
2797 definition available in this translation unit.
2799 The following assertions check these conditions. */
2800 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2801 /* Any code that creates entities with TREE_PUBLIC cleared should
2802 also set DECL_INTERFACE_KNOWN. */
2803 gcc_assert (TREE_PUBLIC (decl));
2804 if (TREE_CODE (decl) == FUNCTION_DECL)
2805 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2806 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2807 || DECL_DECLARED_INLINE_P (decl));
2808 else
2809 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2810 || DECL_VTABLE_OR_VTT_P (decl)
2811 || DECL_TINFO_P (decl));
2812 /* Check that a definition of DECL is available in this translation
2813 unit. */
2814 gcc_assert (!DECL_REALLY_EXTERN (decl));
2816 /* Assume that DECL will not have COMDAT linkage. */
2817 comdat_p = false;
2818 /* Assume that DECL will not be imported into this translation
2819 unit. */
2820 import_p = false;
2822 /* See if the repository tells us whether or not to emit DECL in
2823 this translation unit. */
2824 emit_p = repo_emit_p (decl);
2825 if (emit_p == 0)
2826 import_p = true;
2827 else if (emit_p == 1)
2829 /* The repository indicates that this entity should be defined
2830 here. Make sure the back end honors that request. */
2831 mark_needed (decl);
2832 /* Output the definition as an ordinary strong definition. */
2833 DECL_EXTERNAL (decl) = 0;
2834 DECL_INTERFACE_KNOWN (decl) = 1;
2835 return;
2838 if (import_p)
2839 /* We have already decided what to do with this DECL; there is no
2840 need to check anything further. */
2842 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2844 class_type = DECL_CONTEXT (decl);
2845 import_export_class (class_type);
2846 if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2847 && CLASSTYPE_INTERFACE_ONLY (class_type))
2848 import_p = true;
2849 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2850 && !CLASSTYPE_USE_TEMPLATE (class_type)
2851 && CLASSTYPE_KEY_METHOD (class_type)
2852 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2853 /* The ABI requires that all virtual tables be emitted with
2854 COMDAT linkage. However, on systems where COMDAT symbols
2855 don't show up in the table of contents for a static
2856 archive, or on systems without weak symbols (where we
2857 approximate COMDAT linkage by using internal linkage), the
2858 linker will report errors about undefined symbols because
2859 it will not see the virtual table definition. Therefore,
2860 in the case that we know that the virtual table will be
2861 emitted in only one translation unit, we make the virtual
2862 table an ordinary definition with external linkage. */
2863 DECL_EXTERNAL (decl) = 0;
2864 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2866 /* CLASS_TYPE is being exported from this translation unit,
2867 so DECL should be defined here. */
2868 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2869 /* If a class is declared in a header with the "extern
2870 template" extension, then it will not be instantiated,
2871 even in translation units that would normally require
2872 it. Often such classes are explicitly instantiated in
2873 one translation unit. Therefore, the explicit
2874 instantiation must be made visible to other translation
2875 units. */
2876 DECL_EXTERNAL (decl) = 0;
2877 else
2879 /* The generic C++ ABI says that class data is always
2880 COMDAT, even if there is a key function. Some
2881 variants (e.g., the ARM EABI) says that class data
2882 only has COMDAT linkage if the class data might be
2883 emitted in more than one translation unit. When the
2884 key method can be inline and is inline, we still have
2885 to arrange for comdat even though
2886 class_data_always_comdat is false. */
2887 if (!CLASSTYPE_KEY_METHOD (class_type)
2888 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2889 || targetm.cxx.class_data_always_comdat ())
2891 /* The ABI requires COMDAT linkage. Normally, we
2892 only emit COMDAT things when they are needed;
2893 make sure that we realize that this entity is
2894 indeed needed. */
2895 comdat_p = true;
2896 mark_needed (decl);
2900 else if (!flag_implicit_templates
2901 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2902 import_p = true;
2903 else
2904 comdat_p = true;
2906 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2908 tree type = TREE_TYPE (DECL_NAME (decl));
2909 if (CLASS_TYPE_P (type))
2911 class_type = type;
2912 import_export_class (type);
2913 if (CLASSTYPE_INTERFACE_KNOWN (type)
2914 && TYPE_POLYMORPHIC_P (type)
2915 && CLASSTYPE_INTERFACE_ONLY (type)
2916 /* If -fno-rtti was specified, then we cannot be sure
2917 that RTTI information will be emitted with the
2918 virtual table of the class, so we must emit it
2919 wherever it is used. */
2920 && flag_rtti)
2921 import_p = true;
2922 else
2924 if (CLASSTYPE_INTERFACE_KNOWN (type)
2925 && !CLASSTYPE_INTERFACE_ONLY (type))
2927 comdat_p = (targetm.cxx.class_data_always_comdat ()
2928 || (CLASSTYPE_KEY_METHOD (type)
2929 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2930 mark_needed (decl);
2931 if (!flag_weak)
2933 comdat_p = false;
2934 DECL_EXTERNAL (decl) = 0;
2937 else
2938 comdat_p = true;
2941 else
2942 comdat_p = true;
2944 else if (DECL_TEMPLOID_INSTANTIATION (decl))
2946 /* DECL is an implicit instantiation of a function or static
2947 data member. */
2948 if ((flag_implicit_templates
2949 && !flag_use_repository)
2950 || (flag_implicit_inline_templates
2951 && TREE_CODE (decl) == FUNCTION_DECL
2952 && DECL_DECLARED_INLINE_P (decl)))
2953 comdat_p = true;
2954 else
2955 /* If we are not implicitly generating templates, then mark
2956 this entity as undefined in this translation unit. */
2957 import_p = true;
2959 else if (DECL_FUNCTION_MEMBER_P (decl))
2961 if (!DECL_DECLARED_INLINE_P (decl))
2963 tree ctype = DECL_CONTEXT (decl);
2964 import_export_class (ctype);
2965 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2967 DECL_NOT_REALLY_EXTERN (decl)
2968 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2969 || (DECL_DECLARED_INLINE_P (decl)
2970 && ! flag_implement_inlines
2971 && !DECL_VINDEX (decl)));
2973 if (!DECL_NOT_REALLY_EXTERN (decl))
2974 DECL_EXTERNAL (decl) = 1;
2976 /* Always make artificials weak. */
2977 if (DECL_ARTIFICIAL (decl) && flag_weak)
2978 comdat_p = true;
2979 else
2980 maybe_make_one_only (decl);
2983 else
2984 comdat_p = true;
2986 else
2987 comdat_p = true;
2989 if (import_p)
2991 /* If we are importing DECL into this translation unit, mark is
2992 an undefined here. */
2993 DECL_EXTERNAL (decl) = 1;
2994 DECL_NOT_REALLY_EXTERN (decl) = 0;
2996 else if (comdat_p)
2998 /* If we decided to put DECL in COMDAT, mark it accordingly at
2999 this point. */
3000 comdat_linkage (decl);
3003 DECL_INTERFACE_KNOWN (decl) = 1;
3006 /* Return an expression that performs the destruction of DECL, which
3007 must be a VAR_DECL whose type has a non-trivial destructor, or is
3008 an array whose (innermost) elements have a non-trivial destructor. */
3010 tree
3011 build_cleanup (tree decl)
3013 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
3014 gcc_assert (clean != NULL_TREE);
3015 return clean;
3018 /* Returns the initialization guard variable for the variable DECL,
3019 which has static storage duration. */
3021 tree
3022 get_guard (tree decl)
3024 tree sname;
3025 tree guard;
3027 sname = mangle_guard_variable (decl);
3028 guard = get_global_binding (sname);
3029 if (! guard)
3031 tree guard_type;
3033 /* We use a type that is big enough to contain a mutex as well
3034 as an integer counter. */
3035 guard_type = targetm.cxx.guard_type ();
3036 guard = build_decl (DECL_SOURCE_LOCATION (decl),
3037 VAR_DECL, sname, guard_type);
3039 /* The guard should have the same linkage as what it guards. */
3040 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
3041 TREE_STATIC (guard) = TREE_STATIC (decl);
3042 DECL_COMMON (guard) = DECL_COMMON (decl);
3043 DECL_COMDAT (guard) = DECL_COMDAT (decl);
3044 CP_DECL_THREAD_LOCAL_P (guard) = CP_DECL_THREAD_LOCAL_P (decl);
3045 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
3046 if (DECL_ONE_ONLY (decl))
3047 make_decl_one_only (guard, cxx_comdat_group (guard));
3048 if (TREE_PUBLIC (decl))
3049 DECL_WEAK (guard) = DECL_WEAK (decl);
3050 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
3051 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
3053 DECL_ARTIFICIAL (guard) = 1;
3054 DECL_IGNORED_P (guard) = 1;
3055 TREE_USED (guard) = 1;
3056 pushdecl_top_level_and_finish (guard, NULL_TREE);
3058 return guard;
3061 /* Return an atomic load of src with the appropriate memory model. */
3063 static tree
3064 build_atomic_load_byte (tree src, HOST_WIDE_INT model)
3066 tree ptr_type = build_pointer_type (char_type_node);
3067 tree mem_model = build_int_cst (integer_type_node, model);
3068 tree t, addr, val;
3069 unsigned int size;
3070 int fncode;
3072 size = tree_to_uhwi (TYPE_SIZE_UNIT (char_type_node));
3074 fncode = BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1;
3075 t = builtin_decl_implicit ((enum built_in_function) fncode);
3077 addr = build1 (ADDR_EXPR, ptr_type, src);
3078 val = build_call_expr (t, 2, addr, mem_model);
3079 return val;
3082 /* Return those bits of the GUARD variable that should be set when the
3083 guarded entity is actually initialized. */
3085 static tree
3086 get_guard_bits (tree guard)
3088 if (!targetm.cxx.guard_mask_bit ())
3090 /* We only set the first byte of the guard, in order to leave room
3091 for a mutex in the high-order bits. */
3092 guard = build1 (ADDR_EXPR,
3093 build_pointer_type (TREE_TYPE (guard)),
3094 guard);
3095 guard = build1 (NOP_EXPR,
3096 build_pointer_type (char_type_node),
3097 guard);
3098 guard = build1 (INDIRECT_REF, char_type_node, guard);
3101 return guard;
3104 /* Return an expression which determines whether or not the GUARD
3105 variable has already been initialized. */
3107 tree
3108 get_guard_cond (tree guard, bool thread_safe)
3110 tree guard_value;
3112 if (!thread_safe)
3113 guard = get_guard_bits (guard);
3114 else
3115 guard = build_atomic_load_byte (guard, MEMMODEL_ACQUIRE);
3117 /* Mask off all but the low bit. */
3118 if (targetm.cxx.guard_mask_bit ())
3120 guard_value = integer_one_node;
3121 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3122 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3123 guard = cp_build_binary_op (input_location,
3124 BIT_AND_EXPR, guard, guard_value,
3125 tf_warning_or_error);
3128 guard_value = integer_zero_node;
3129 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3130 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3131 return cp_build_binary_op (input_location,
3132 EQ_EXPR, guard, guard_value,
3133 tf_warning_or_error);
3136 /* Return an expression which sets the GUARD variable, indicating that
3137 the variable being guarded has been initialized. */
3139 tree
3140 set_guard (tree guard)
3142 tree guard_init;
3144 /* Set the GUARD to one. */
3145 guard = get_guard_bits (guard);
3146 guard_init = integer_one_node;
3147 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3148 guard_init = fold_convert (TREE_TYPE (guard), guard_init);
3149 return cp_build_modify_expr (input_location, guard, NOP_EXPR, guard_init,
3150 tf_warning_or_error);
3153 /* Returns true iff we can tell that VAR does not have a dynamic
3154 initializer. */
3156 static bool
3157 var_defined_without_dynamic_init (tree var)
3159 /* If it's defined in another TU, we can't tell. */
3160 if (DECL_EXTERNAL (var))
3161 return false;
3162 /* If it has a non-trivial destructor, registering the destructor
3163 counts as dynamic initialization. */
3164 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3165 return false;
3166 /* If it's in this TU, its initializer has been processed, unless
3167 it's a case of self-initialization, then DECL_INITIALIZED_P is
3168 false while the initializer is handled by finish_id_expression. */
3169 if (!DECL_INITIALIZED_P (var))
3170 return false;
3171 /* If it has no initializer or a constant one, it's not dynamic. */
3172 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3173 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3176 /* Returns true iff VAR is a variable that needs uses to be
3177 wrapped for possible dynamic initialization. */
3179 static bool
3180 var_needs_tls_wrapper (tree var)
3182 return (!error_operand_p (var)
3183 && CP_DECL_THREAD_LOCAL_P (var)
3184 && !DECL_GNU_TLS_P (var)
3185 && !DECL_FUNCTION_SCOPE_P (var)
3186 && !var_defined_without_dynamic_init (var));
3189 /* Get the FUNCTION_DECL for the shared TLS init function for this
3190 translation unit. */
3192 static tree
3193 get_local_tls_init_fn (void)
3195 tree sname = get_identifier ("__tls_init");
3196 tree fn = get_global_binding (sname);
3197 if (!fn)
3199 fn = build_lang_decl (FUNCTION_DECL, sname,
3200 build_function_type (void_type_node,
3201 void_list_node));
3202 SET_DECL_LANGUAGE (fn, lang_c);
3203 TREE_PUBLIC (fn) = false;
3204 DECL_ARTIFICIAL (fn) = true;
3205 mark_used (fn);
3206 set_global_binding (fn);
3208 return fn;
3211 /* Get a FUNCTION_DECL for the init function for the thread_local
3212 variable VAR. The init function will be an alias to the function
3213 that initializes all the non-local TLS variables in the translation
3214 unit. The init function is only used by the wrapper function. */
3216 static tree
3217 get_tls_init_fn (tree var)
3219 /* Only C++11 TLS vars need this init fn. */
3220 if (!var_needs_tls_wrapper (var))
3221 return NULL_TREE;
3223 /* If -fno-extern-tls-init, assume that we don't need to call
3224 a tls init function for a variable defined in another TU. */
3225 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3226 return NULL_TREE;
3228 /* If the variable is internal, or if we can't generate aliases,
3229 call the local init function directly. */
3230 if (!TREE_PUBLIC (var) || !TARGET_SUPPORTS_ALIASES)
3231 return get_local_tls_init_fn ();
3233 tree sname = mangle_tls_init_fn (var);
3234 tree fn = get_global_binding (sname);
3235 if (!fn)
3237 fn = build_lang_decl (FUNCTION_DECL, sname,
3238 build_function_type (void_type_node,
3239 void_list_node));
3240 SET_DECL_LANGUAGE (fn, lang_c);
3241 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3242 DECL_ARTIFICIAL (fn) = true;
3243 DECL_COMDAT (fn) = DECL_COMDAT (var);
3244 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3245 if (DECL_ONE_ONLY (var))
3246 make_decl_one_only (fn, cxx_comdat_group (fn));
3247 if (TREE_PUBLIC (var))
3249 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3250 /* If the variable is defined somewhere else and might have static
3251 initialization, make the init function a weak reference. */
3252 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3253 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3254 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3255 && DECL_EXTERNAL (var))
3256 declare_weak (fn);
3257 else
3258 DECL_WEAK (fn) = DECL_WEAK (var);
3260 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3261 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3262 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3263 DECL_IGNORED_P (fn) = 1;
3264 mark_used (fn);
3266 DECL_BEFRIENDING_CLASSES (fn) = var;
3268 set_global_binding (fn);
3270 return fn;
3273 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3274 variable VAR. The wrapper function calls the init function (if any) for
3275 VAR and then returns a reference to VAR. The wrapper function is used
3276 in place of VAR everywhere VAR is mentioned. */
3278 tree
3279 get_tls_wrapper_fn (tree var)
3281 /* Only C++11 TLS vars need this wrapper fn. */
3282 if (!var_needs_tls_wrapper (var))
3283 return NULL_TREE;
3285 tree sname = mangle_tls_wrapper_fn (var);
3286 tree fn = get_global_binding (sname);
3287 if (!fn)
3289 /* A named rvalue reference is an lvalue, so the wrapper should
3290 always return an lvalue reference. */
3291 tree type = non_reference (TREE_TYPE (var));
3292 type = build_reference_type (type);
3293 tree fntype = build_function_type (type, void_list_node);
3294 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3295 SET_DECL_LANGUAGE (fn, lang_c);
3296 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3297 DECL_ARTIFICIAL (fn) = true;
3298 DECL_IGNORED_P (fn) = 1;
3299 /* The wrapper is inline and emitted everywhere var is used. */
3300 DECL_DECLARED_INLINE_P (fn) = true;
3301 if (TREE_PUBLIC (var))
3303 comdat_linkage (fn);
3304 #ifdef HAVE_GAS_HIDDEN
3305 /* Make the wrapper bind locally; there's no reason to share
3306 the wrapper between multiple shared objects. */
3307 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3308 DECL_VISIBILITY_SPECIFIED (fn) = true;
3309 #endif
3311 if (!TREE_PUBLIC (fn))
3312 DECL_INTERFACE_KNOWN (fn) = true;
3313 mark_used (fn);
3314 note_vague_linkage_fn (fn);
3316 #if 0
3317 /* We want CSE to commonize calls to the wrapper, but marking it as
3318 pure is unsafe since it has side-effects. I guess we need a new
3319 ECF flag even weaker than ECF_PURE. FIXME! */
3320 DECL_PURE_P (fn) = true;
3321 #endif
3323 DECL_BEFRIENDING_CLASSES (fn) = var;
3325 set_global_binding (fn);
3327 return fn;
3330 /* At EOF, generate the definition for the TLS wrapper function FN:
3332 T& var_wrapper() {
3333 if (init_fn) init_fn();
3334 return var;
3335 } */
3337 static void
3338 generate_tls_wrapper (tree fn)
3340 tree var = DECL_BEFRIENDING_CLASSES (fn);
3342 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3343 tree body = begin_function_body ();
3344 /* Only call the init fn if there might be one. */
3345 if (tree init_fn = get_tls_init_fn (var))
3347 tree if_stmt = NULL_TREE;
3348 /* If init_fn is a weakref, make sure it exists before calling. */
3349 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3351 if_stmt = begin_if_stmt ();
3352 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3353 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3354 NE_EXPR, addr, nullptr_node,
3355 tf_warning_or_error);
3356 finish_if_stmt_cond (cond, if_stmt);
3358 finish_expr_stmt (build_cxx_call
3359 (init_fn, 0, NULL, tf_warning_or_error));
3360 if (if_stmt)
3362 finish_then_clause (if_stmt);
3363 finish_if_stmt (if_stmt);
3366 else
3367 /* If there's no initialization, the wrapper is a constant function. */
3368 TREE_READONLY (fn) = true;
3369 finish_return_stmt (convert_from_reference (var));
3370 finish_function_body (body);
3371 expand_or_defer_fn (finish_function (/*inline_p=*/false));
3374 /* Start the process of running a particular set of global constructors
3375 or destructors. Subroutine of do_[cd]tors. Also called from
3376 vtv_start_verification_constructor_init_function. */
3378 static tree
3379 start_objects (int method_type, int initp)
3381 tree body;
3382 tree fndecl;
3383 char type[14];
3385 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3387 if (initp != DEFAULT_INIT_PRIORITY)
3389 char joiner;
3391 #ifdef JOINER
3392 joiner = JOINER;
3393 #else
3394 joiner = '_';
3395 #endif
3397 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3399 else
3400 sprintf (type, "sub_%c", method_type);
3402 fndecl = build_lang_decl (FUNCTION_DECL,
3403 get_file_function_name (type),
3404 build_function_type_list (void_type_node,
3405 NULL_TREE));
3406 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3408 TREE_PUBLIC (current_function_decl) = 0;
3410 /* Mark as artificial because it's not explicitly in the user's
3411 source code. */
3412 DECL_ARTIFICIAL (current_function_decl) = 1;
3414 /* Mark this declaration as used to avoid spurious warnings. */
3415 TREE_USED (current_function_decl) = 1;
3417 /* Mark this function as a global constructor or destructor. */
3418 if (method_type == 'I')
3419 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3420 else
3421 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3423 body = begin_compound_stmt (BCS_FN_BODY);
3425 return body;
3428 /* Finish the process of running a particular set of global constructors
3429 or destructors. Subroutine of do_[cd]tors. */
3431 static void
3432 finish_objects (int method_type, int initp, tree body)
3434 tree fn;
3436 /* Finish up. */
3437 finish_compound_stmt (body);
3438 fn = finish_function (/*inline_p=*/false);
3440 if (method_type == 'I')
3442 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3443 decl_init_priority_insert (fn, initp);
3445 else
3447 DECL_STATIC_DESTRUCTOR (fn) = 1;
3448 decl_fini_priority_insert (fn, initp);
3451 expand_or_defer_fn (fn);
3454 /* The names of the parameters to the function created to handle
3455 initializations and destructions for objects with static storage
3456 duration. */
3457 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3458 #define PRIORITY_IDENTIFIER "__priority"
3460 /* The name of the function we create to handle initializations and
3461 destructions for objects with static storage duration. */
3462 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3464 /* The declaration for the __INITIALIZE_P argument. */
3465 static GTY(()) tree initialize_p_decl;
3467 /* The declaration for the __PRIORITY argument. */
3468 static GTY(()) tree priority_decl;
3470 /* The declaration for the static storage duration function. */
3471 static GTY(()) tree ssdf_decl;
3473 /* All the static storage duration functions created in this
3474 translation unit. */
3475 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3477 /* A map from priority levels to information about that priority
3478 level. There may be many such levels, so efficient lookup is
3479 important. */
3480 static splay_tree priority_info_map;
3482 /* Begins the generation of the function that will handle all
3483 initialization and destruction of objects with static storage
3484 duration. The function generated takes two parameters of type
3485 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3486 nonzero, it performs initializations. Otherwise, it performs
3487 destructions. It only performs those initializations or
3488 destructions with the indicated __PRIORITY. The generated function
3489 returns no value.
3491 It is assumed that this function will only be called once per
3492 translation unit. */
3494 static tree
3495 start_static_storage_duration_function (unsigned count)
3497 tree type;
3498 tree body;
3499 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3501 /* Create the identifier for this function. It will be of the form
3502 SSDF_IDENTIFIER_<number>. */
3503 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3505 type = build_function_type_list (void_type_node,
3506 integer_type_node, integer_type_node,
3507 NULL_TREE);
3509 /* Create the FUNCTION_DECL itself. */
3510 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3511 get_identifier (id),
3512 type);
3513 TREE_PUBLIC (ssdf_decl) = 0;
3514 DECL_ARTIFICIAL (ssdf_decl) = 1;
3516 /* Put this function in the list of functions to be called from the
3517 static constructors and destructors. */
3518 if (!ssdf_decls)
3520 vec_alloc (ssdf_decls, 32);
3522 /* Take this opportunity to initialize the map from priority
3523 numbers to information about that priority level. */
3524 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3525 /*delete_key_fn=*/0,
3526 /*delete_value_fn=*/
3527 (splay_tree_delete_value_fn) &free);
3529 /* We always need to generate functions for the
3530 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3531 priorities later, we'll be sure to find the
3532 DEFAULT_INIT_PRIORITY. */
3533 get_priority_info (DEFAULT_INIT_PRIORITY);
3536 vec_safe_push (ssdf_decls, ssdf_decl);
3538 /* Create the argument list. */
3539 initialize_p_decl = cp_build_parm_decl
3540 (ssdf_decl, get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3541 TREE_USED (initialize_p_decl) = 1;
3542 priority_decl = cp_build_parm_decl
3543 (ssdf_decl, get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3544 TREE_USED (priority_decl) = 1;
3546 DECL_CHAIN (initialize_p_decl) = priority_decl;
3547 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3549 /* Put the function in the global scope. */
3550 pushdecl (ssdf_decl);
3552 /* Start the function itself. This is equivalent to declaring the
3553 function as:
3555 static void __ssdf (int __initialize_p, init __priority_p);
3557 It is static because we only need to call this function from the
3558 various constructor and destructor functions for this module. */
3559 start_preparsed_function (ssdf_decl,
3560 /*attrs=*/NULL_TREE,
3561 SF_PRE_PARSED);
3563 /* Set up the scope of the outermost block in the function. */
3564 body = begin_compound_stmt (BCS_FN_BODY);
3566 return body;
3569 /* Finish the generation of the function which performs initialization
3570 and destruction of objects with static storage duration. After
3571 this point, no more such objects can be created. */
3573 static void
3574 finish_static_storage_duration_function (tree body)
3576 /* Close out the function. */
3577 finish_compound_stmt (body);
3578 expand_or_defer_fn (finish_function (/*inline_p=*/false));
3581 /* Return the information about the indicated PRIORITY level. If no
3582 code to handle this level has yet been generated, generate the
3583 appropriate prologue. */
3585 static priority_info
3586 get_priority_info (int priority)
3588 priority_info pi;
3589 splay_tree_node n;
3591 n = splay_tree_lookup (priority_info_map,
3592 (splay_tree_key) priority);
3593 if (!n)
3595 /* Create a new priority information structure, and insert it
3596 into the map. */
3597 pi = XNEW (struct priority_info_s);
3598 pi->initializations_p = 0;
3599 pi->destructions_p = 0;
3600 splay_tree_insert (priority_info_map,
3601 (splay_tree_key) priority,
3602 (splay_tree_value) pi);
3604 else
3605 pi = (priority_info) n->value;
3607 return pi;
3610 /* The effective initialization priority of a DECL. */
3612 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3613 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3614 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3616 /* Whether a DECL needs a guard to protect it against multiple
3617 initialization. */
3619 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3620 || DECL_ONE_ONLY (decl) \
3621 || DECL_WEAK (decl)))
3623 /* Called from one_static_initialization_or_destruction(),
3624 via walk_tree.
3625 Walks the initializer list of a global variable and looks for
3626 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3627 and that have their DECL_CONTEXT() == NULL.
3628 For each such temporary variable, set their DECL_CONTEXT() to
3629 the current function. This is necessary because otherwise
3630 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3631 when trying to refer to a temporary variable that does not have
3632 it's DECL_CONTECT() properly set. */
3633 static tree
3634 fix_temporary_vars_context_r (tree *node,
3635 int * /*unused*/,
3636 void * /*unused1*/)
3638 gcc_assert (current_function_decl);
3640 if (TREE_CODE (*node) == BIND_EXPR)
3642 tree var;
3644 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3645 if (VAR_P (var)
3646 && !DECL_NAME (var)
3647 && DECL_ARTIFICIAL (var)
3648 && !DECL_CONTEXT (var))
3649 DECL_CONTEXT (var) = current_function_decl;
3652 return NULL_TREE;
3655 /* Set up to handle the initialization or destruction of DECL. If
3656 INITP is nonzero, we are initializing the variable. Otherwise, we
3657 are destroying it. */
3659 static void
3660 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3662 tree guard_if_stmt = NULL_TREE;
3663 tree guard;
3665 /* If we are supposed to destruct and there's a trivial destructor,
3666 nothing has to be done. */
3667 if (!initp
3668 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3669 return;
3671 /* Trick the compiler into thinking we are at the file and line
3672 where DECL was declared so that error-messages make sense, and so
3673 that the debugger will show somewhat sensible file and line
3674 information. */
3675 input_location = DECL_SOURCE_LOCATION (decl);
3677 /* Make sure temporary variables in the initialiser all have
3678 their DECL_CONTEXT() set to a value different from NULL_TREE.
3679 This can happen when global variables initializers are built.
3680 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3681 the temporary variables that might have been generated in the
3682 accompanying initializers is NULL_TREE, meaning the variables have been
3683 declared in the global namespace.
3684 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3685 of the temporaries are set to the current function decl. */
3686 cp_walk_tree_without_duplicates (&init,
3687 fix_temporary_vars_context_r,
3688 NULL);
3690 /* Because of:
3692 [class.access.spec]
3694 Access control for implicit calls to the constructors,
3695 the conversion functions, or the destructor called to
3696 create and destroy a static data member is performed as
3697 if these calls appeared in the scope of the member's
3698 class.
3700 we pretend we are in a static member function of the class of
3701 which the DECL is a member. */
3702 if (member_p (decl))
3704 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3705 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3708 /* Assume we don't need a guard. */
3709 guard = NULL_TREE;
3710 /* We need a guard if this is an object with external linkage that
3711 might be initialized in more than one place. (For example, a
3712 static data member of a template, when the data member requires
3713 construction.) */
3714 if (NEEDS_GUARD_P (decl))
3716 tree guard_cond;
3718 guard = get_guard (decl);
3720 /* When using __cxa_atexit, we just check the GUARD as we would
3721 for a local static. */
3722 if (flag_use_cxa_atexit)
3724 /* When using __cxa_atexit, we never try to destroy
3725 anything from a static destructor. */
3726 gcc_assert (initp);
3727 guard_cond = get_guard_cond (guard, false);
3729 /* If we don't have __cxa_atexit, then we will be running
3730 destructors from .fini sections, or their equivalents. So,
3731 we need to know how many times we've tried to initialize this
3732 object. We do initializations only if the GUARD is zero,
3733 i.e., if we are the first to initialize the variable. We do
3734 destructions only if the GUARD is one, i.e., if we are the
3735 last to destroy the variable. */
3736 else if (initp)
3737 guard_cond
3738 = cp_build_binary_op (input_location,
3739 EQ_EXPR,
3740 cp_build_unary_op (PREINCREMENT_EXPR,
3741 guard,
3742 /*noconvert=*/true,
3743 tf_warning_or_error),
3744 integer_one_node,
3745 tf_warning_or_error);
3746 else
3747 guard_cond
3748 = cp_build_binary_op (input_location,
3749 EQ_EXPR,
3750 cp_build_unary_op (PREDECREMENT_EXPR,
3751 guard,
3752 /*noconvert=*/true,
3753 tf_warning_or_error),
3754 integer_zero_node,
3755 tf_warning_or_error);
3757 guard_if_stmt = begin_if_stmt ();
3758 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3762 /* If we're using __cxa_atexit, we have not already set the GUARD,
3763 so we must do so now. */
3764 if (guard && initp && flag_use_cxa_atexit)
3765 finish_expr_stmt (set_guard (guard));
3767 /* Perform the initialization or destruction. */
3768 if (initp)
3770 if (init)
3772 finish_expr_stmt (init);
3773 if (sanitize_flags_p (SANITIZE_ADDRESS, decl))
3775 varpool_node *vnode = varpool_node::get (decl);
3776 if (vnode)
3777 vnode->dynamically_initialized = 1;
3781 /* If we're using __cxa_atexit, register a function that calls the
3782 destructor for the object. */
3783 if (flag_use_cxa_atexit)
3784 finish_expr_stmt (register_dtor_fn (decl));
3786 else
3787 finish_expr_stmt (build_cleanup (decl));
3789 /* Finish the guard if-stmt, if necessary. */
3790 if (guard)
3792 finish_then_clause (guard_if_stmt);
3793 finish_if_stmt (guard_if_stmt);
3796 /* Now that we're done with DECL we don't need to pretend to be a
3797 member of its class any longer. */
3798 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3799 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3802 /* Generate code to do the initialization or destruction of the decls in VARS,
3803 a TREE_LIST of VAR_DECL with static storage duration.
3804 Whether initialization or destruction is performed is specified by INITP. */
3806 static void
3807 do_static_initialization_or_destruction (tree vars, bool initp)
3809 tree node, init_if_stmt, cond;
3811 /* Build the outer if-stmt to check for initialization or destruction. */
3812 init_if_stmt = begin_if_stmt ();
3813 cond = initp ? integer_one_node : integer_zero_node;
3814 cond = cp_build_binary_op (input_location,
3815 EQ_EXPR,
3816 initialize_p_decl,
3817 cond,
3818 tf_warning_or_error);
3819 finish_if_stmt_cond (cond, init_if_stmt);
3821 /* To make sure dynamic construction doesn't access globals from other
3822 compilation units where they might not be yet constructed, for
3823 -fsanitize=address insert __asan_before_dynamic_init call that
3824 prevents access to either all global variables that need construction
3825 in other compilation units, or at least those that haven't been
3826 initialized yet. Variables that need dynamic construction in
3827 the current compilation unit are kept accessible. */
3828 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
3829 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3831 node = vars;
3832 do {
3833 tree decl = TREE_VALUE (node);
3834 tree priority_if_stmt;
3835 int priority;
3836 priority_info pi;
3838 /* If we don't need a destructor, there's nothing to do. Avoid
3839 creating a possibly empty if-stmt. */
3840 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3842 node = TREE_CHAIN (node);
3843 continue;
3846 /* Remember that we had an initialization or finalization at this
3847 priority. */
3848 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3849 pi = get_priority_info (priority);
3850 if (initp)
3851 pi->initializations_p = 1;
3852 else
3853 pi->destructions_p = 1;
3855 /* Conditionalize this initialization on being in the right priority
3856 and being initializing/finalizing appropriately. */
3857 priority_if_stmt = begin_if_stmt ();
3858 cond = cp_build_binary_op (input_location,
3859 EQ_EXPR,
3860 priority_decl,
3861 build_int_cst (NULL_TREE, priority),
3862 tf_warning_or_error);
3863 finish_if_stmt_cond (cond, priority_if_stmt);
3865 /* Process initializers with same priority. */
3866 for (; node
3867 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3868 node = TREE_CHAIN (node))
3869 /* Do one initialization or destruction. */
3870 one_static_initialization_or_destruction (TREE_VALUE (node),
3871 TREE_PURPOSE (node), initp);
3873 /* Finish up the priority if-stmt body. */
3874 finish_then_clause (priority_if_stmt);
3875 finish_if_stmt (priority_if_stmt);
3877 } while (node);
3879 /* Revert what __asan_before_dynamic_init did by calling
3880 __asan_after_dynamic_init. */
3881 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
3882 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3884 /* Finish up the init/destruct if-stmt body. */
3885 finish_then_clause (init_if_stmt);
3886 finish_if_stmt (init_if_stmt);
3889 /* VARS is a list of variables with static storage duration which may
3890 need initialization and/or finalization. Remove those variables
3891 that don't really need to be initialized or finalized, and return
3892 the resulting list. The order in which the variables appear in
3893 VARS is in reverse order of the order in which they should actually
3894 be initialized. The list we return is in the unreversed order;
3895 i.e., the first variable should be initialized first. */
3897 static tree
3898 prune_vars_needing_no_initialization (tree *vars)
3900 tree *var = vars;
3901 tree result = NULL_TREE;
3903 while (*var)
3905 tree t = *var;
3906 tree decl = TREE_VALUE (t);
3907 tree init = TREE_PURPOSE (t);
3909 /* Deal gracefully with error. */
3910 if (error_operand_p (decl))
3912 var = &TREE_CHAIN (t);
3913 continue;
3916 /* The only things that can be initialized are variables. */
3917 gcc_assert (VAR_P (decl));
3919 /* If this object is not defined, we don't need to do anything
3920 here. */
3921 if (DECL_EXTERNAL (decl))
3923 var = &TREE_CHAIN (t);
3924 continue;
3927 /* Also, if the initializer already contains errors, we can bail
3928 out now. */
3929 if (init && TREE_CODE (init) == TREE_LIST
3930 && value_member (error_mark_node, init))
3932 var = &TREE_CHAIN (t);
3933 continue;
3936 /* This variable is going to need initialization and/or
3937 finalization, so we add it to the list. */
3938 *var = TREE_CHAIN (t);
3939 TREE_CHAIN (t) = result;
3940 result = t;
3943 return result;
3946 /* Make sure we have told the back end about all the variables in
3947 VARS. */
3949 static void
3950 write_out_vars (tree vars)
3952 tree v;
3954 for (v = vars; v; v = TREE_CHAIN (v))
3956 tree var = TREE_VALUE (v);
3957 if (!var_finalized_p (var))
3959 import_export_decl (var);
3960 rest_of_decl_compilation (var, 1, 1);
3965 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3966 (otherwise) that will initialize all global objects with static
3967 storage duration having the indicated PRIORITY. */
3969 static void
3970 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3971 location_t *locus)
3973 char function_key;
3974 tree fndecl;
3975 tree body;
3976 size_t i;
3978 input_location = *locus;
3979 /* ??? */
3980 /* Was: locus->line++; */
3982 /* We use `I' to indicate initialization and `D' to indicate
3983 destruction. */
3984 function_key = constructor_p ? 'I' : 'D';
3986 /* We emit the function lazily, to avoid generating empty
3987 global constructors and destructors. */
3988 body = NULL_TREE;
3990 /* For Objective-C++, we may need to initialize metadata found in this module.
3991 This must be done _before_ any other static initializations. */
3992 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3993 && constructor_p && objc_static_init_needed_p ())
3995 body = start_objects (function_key, priority);
3996 objc_generate_static_init_call (NULL_TREE);
3999 /* Call the static storage duration function with appropriate
4000 arguments. */
4001 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
4003 /* Calls to pure or const functions will expand to nothing. */
4004 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
4006 tree call;
4008 if (! body)
4009 body = start_objects (function_key, priority);
4011 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
4012 build_int_cst (NULL_TREE,
4013 constructor_p),
4014 build_int_cst (NULL_TREE,
4015 priority),
4016 NULL_TREE);
4017 finish_expr_stmt (call);
4021 /* Close out the function. */
4022 if (body)
4023 finish_objects (function_key, priority, body);
4026 /* Generate constructor and destructor functions for the priority
4027 indicated by N. */
4029 static int
4030 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
4032 location_t *locus = (location_t *) data;
4033 int priority = (int) n->key;
4034 priority_info pi = (priority_info) n->value;
4036 /* Generate the functions themselves, but only if they are really
4037 needed. */
4038 if (pi->initializations_p)
4039 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
4040 if (pi->destructions_p)
4041 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
4043 /* Keep iterating. */
4044 return 0;
4047 /* Return C++ property of T, based on given operation OP. */
4049 static int
4050 cpp_check (tree t, cpp_operation op)
4052 switch (op)
4054 case HAS_DEPENDENT_TEMPLATE_ARGS:
4056 tree ti = CLASSTYPE_TEMPLATE_INFO (t);
4057 if (!ti)
4058 return 0;
4059 ++processing_template_decl;
4060 const bool dep = any_dependent_template_arguments_p (TI_ARGS (ti));
4061 --processing_template_decl;
4062 return dep;
4064 case IS_ABSTRACT:
4065 return DECL_PURE_VIRTUAL_P (t);
4066 case IS_CONSTRUCTOR:
4067 return DECL_CONSTRUCTOR_P (t);
4068 case IS_DESTRUCTOR:
4069 return DECL_DESTRUCTOR_P (t);
4070 case IS_COPY_CONSTRUCTOR:
4071 return DECL_COPY_CONSTRUCTOR_P (t);
4072 case IS_MOVE_CONSTRUCTOR:
4073 return DECL_MOVE_CONSTRUCTOR_P (t);
4074 case IS_TEMPLATE:
4075 return TREE_CODE (t) == TEMPLATE_DECL;
4076 case IS_TRIVIAL:
4077 return trivial_type_p (t);
4078 default:
4079 return 0;
4083 /* Collect source file references recursively, starting from NAMESPC. */
4085 static void
4086 collect_source_refs (tree namespc)
4088 /* Iterate over names in this name space. */
4089 for (tree t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4090 if (DECL_IS_BUILTIN (t))
4092 else if (TREE_CODE (t) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (t))
4093 collect_source_refs (t);
4094 else
4095 collect_source_ref (DECL_SOURCE_FILE (t));
4098 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4099 starting from NAMESPC. */
4101 static void
4102 collect_ada_namespace (tree namespc, const char *source_file)
4104 tree decl = NAMESPACE_LEVEL (namespc)->names;
4106 /* Collect decls from this namespace. This will skip
4107 NAMESPACE_DECLs (both aliases and regular, it cannot tell). */
4108 collect_ada_nodes (decl, source_file);
4110 /* Now scan for namespace children, and dump them. */
4111 for (; decl; decl = TREE_CHAIN (decl))
4112 if (TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl))
4113 collect_ada_namespace (decl, source_file);
4116 /* Returns true iff there is a definition available for variable or
4117 function DECL. */
4119 bool
4120 decl_defined_p (tree decl)
4122 if (TREE_CODE (decl) == FUNCTION_DECL)
4123 return (DECL_INITIAL (decl) != NULL_TREE
4124 /* A pending instantiation of a friend temploid is defined. */
4125 || (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
4126 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4127 (DECL_TI_TEMPLATE (decl)))));
4128 else
4130 gcc_assert (VAR_P (decl));
4131 return !DECL_EXTERNAL (decl);
4135 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4137 [expr.const]
4139 An integral constant-expression can only involve ... const
4140 variables of integral or enumeration types initialized with
4141 constant expressions ...
4143 C++0x also allows constexpr variables and temporaries initialized
4144 with constant expressions. We handle the former here, but the latter
4145 are just folded away in cxx_eval_constant_expression.
4147 The standard does not require that the expression be non-volatile.
4148 G++ implements the proposed correction in DR 457. */
4150 bool
4151 decl_constant_var_p (tree decl)
4153 if (!decl_maybe_constant_var_p (decl))
4154 return false;
4156 /* We don't know if a template static data member is initialized with
4157 a constant expression until we instantiate its initializer. Even
4158 in the case of a constexpr variable, we can't treat it as a
4159 constant until its initializer is complete in case it's used in
4160 its own initializer. */
4161 maybe_instantiate_decl (decl);
4162 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4165 /* Returns true if DECL could be a symbolic constant variable, depending on
4166 its initializer. */
4168 bool
4169 decl_maybe_constant_var_p (tree decl)
4171 tree type = TREE_TYPE (decl);
4172 if (!VAR_P (decl))
4173 return false;
4174 if (DECL_DECLARED_CONSTEXPR_P (decl))
4175 return true;
4176 if (DECL_HAS_VALUE_EXPR_P (decl))
4177 /* A proxy isn't constant. */
4178 return false;
4179 if (TREE_CODE (type) == REFERENCE_TYPE)
4180 /* References can be constant. */;
4181 else if (CP_TYPE_CONST_NON_VOLATILE_P (type)
4182 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4183 /* And const integers. */;
4184 else
4185 return false;
4187 if (DECL_INITIAL (decl)
4188 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
4189 /* We know the initializer, and it isn't constant. */
4190 return false;
4191 else
4192 return true;
4195 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4196 called from grokfndecl and grokvardecl; in all modes it is called from
4197 cp_write_global_declarations. */
4199 void
4200 no_linkage_error (tree decl)
4202 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4203 /* In C++11 it's ok if the decl is defined. */
4204 return;
4205 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4206 if (t == NULL_TREE)
4207 /* The type that got us on no_linkage_decls must have gotten a name for
4208 linkage purposes. */;
4209 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4210 /* The type might end up having a typedef name for linkage purposes. */
4211 vec_safe_push (no_linkage_decls, decl);
4212 else if (TYPE_UNNAMED_P (t))
4214 bool d = false;
4215 if (cxx_dialect >= cxx11)
4216 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4217 "unnamed type, is used but never defined", decl);
4218 else if (DECL_EXTERN_C_P (decl))
4219 /* Allow this; it's pretty common in C. */;
4220 else if (VAR_P (decl))
4221 /* DRs 132, 319 and 389 seem to indicate types with
4222 no linkage can only be used to declare extern "C"
4223 entities. Since it's not always an error in the
4224 ISO C++ 90 Standard, we only issue a warning. */
4225 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "unnamed type "
4226 "with no linkage used to declare variable %q#D with "
4227 "linkage", decl);
4228 else
4229 d = permerror (DECL_SOURCE_LOCATION (decl), "unnamed type with no "
4230 "linkage used to declare function %q#D with linkage",
4231 decl);
4232 if (d && is_typedef_decl (TYPE_NAME (t)))
4233 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4234 "to the unqualified type, so it is not used for linkage",
4235 TYPE_NAME (t));
4237 else if (cxx_dialect >= cxx11)
4239 if (VAR_P (decl) || !DECL_PURE_VIRTUAL_P (decl))
4240 permerror (DECL_SOURCE_LOCATION (decl),
4241 "%q#D, declared using local type "
4242 "%qT, is used but never defined", decl, t);
4244 else if (VAR_P (decl))
4245 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4246 "used to declare variable %q#D with linkage", t, decl);
4247 else
4248 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4249 "to declare function %q#D with linkage", t, decl);
4252 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4254 static void
4255 collect_all_refs (const char *source_file)
4257 collect_ada_namespace (global_namespace, source_file);
4260 /* Clear DECL_EXTERNAL for NODE. */
4262 static bool
4263 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4265 DECL_EXTERNAL (node->decl) = 0;
4266 return false;
4269 /* Build up the function to run dynamic initializers for thread_local
4270 variables in this translation unit and alias the init functions for the
4271 individual variables to it. */
4273 static void
4274 handle_tls_init (void)
4276 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4277 if (vars == NULL_TREE)
4278 return;
4280 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4282 write_out_vars (vars);
4284 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4285 boolean_type_node);
4286 TREE_PUBLIC (guard) = false;
4287 TREE_STATIC (guard) = true;
4288 DECL_ARTIFICIAL (guard) = true;
4289 DECL_IGNORED_P (guard) = true;
4290 TREE_USED (guard) = true;
4291 CP_DECL_THREAD_LOCAL_P (guard) = true;
4292 set_decl_tls_model (guard, decl_default_tls_model (guard));
4293 pushdecl_top_level_and_finish (guard, NULL_TREE);
4295 tree fn = get_local_tls_init_fn ();
4296 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4297 tree body = begin_function_body ();
4298 tree if_stmt = begin_if_stmt ();
4299 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4300 tf_warning_or_error);
4301 finish_if_stmt_cond (cond, if_stmt);
4302 finish_expr_stmt (cp_build_modify_expr (loc, guard, NOP_EXPR,
4303 boolean_true_node,
4304 tf_warning_or_error));
4305 for (; vars; vars = TREE_CHAIN (vars))
4307 tree var = TREE_VALUE (vars);
4308 tree init = TREE_PURPOSE (vars);
4309 one_static_initialization_or_destruction (var, init, true);
4311 /* Output init aliases even with -fno-extern-tls-init. */
4312 if (TARGET_SUPPORTS_ALIASES && TREE_PUBLIC (var))
4314 tree single_init_fn = get_tls_init_fn (var);
4315 if (single_init_fn == NULL_TREE)
4316 continue;
4317 cgraph_node *alias
4318 = cgraph_node::get_create (fn)->create_same_body_alias
4319 (single_init_fn, fn);
4320 gcc_assert (alias != NULL);
4324 finish_then_clause (if_stmt);
4325 finish_if_stmt (if_stmt);
4326 finish_function_body (body);
4327 expand_or_defer_fn (finish_function (/*inline_p=*/false));
4330 /* We're at the end of compilation, so generate any mangling aliases that
4331 we've been saving up, if DECL is going to be output and ID2 isn't
4332 already taken by another declaration. */
4334 static void
4335 generate_mangling_alias (tree decl, tree id2)
4337 struct cgraph_node *n = NULL;
4339 if (TREE_CODE (decl) == FUNCTION_DECL)
4341 n = cgraph_node::get (decl);
4342 if (!n)
4343 /* Don't create an alias to an unreferenced function. */
4344 return;
4347 tree *slot
4348 = mangled_decls->find_slot_with_hash (id2, IDENTIFIER_HASH_VALUE (id2),
4349 INSERT);
4351 /* If there's a declaration already using this mangled name,
4352 don't create a compatibility alias that conflicts. */
4353 if (*slot)
4354 return;
4356 tree alias = make_alias_for (decl, id2);
4357 *slot = alias;
4359 DECL_IGNORED_P (alias) = 1;
4360 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
4361 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
4362 if (vague_linkage_p (decl))
4363 DECL_WEAK (alias) = 1;
4365 if (n)
4366 n->create_same_body_alias (alias, decl);
4367 else
4368 varpool_node::create_extra_name_alias (alias, decl);
4371 /* Note that we might want to emit an alias with the symbol ID2 for DECL at
4372 the end of translation, for compatibility across bugs in the mangling
4373 implementation. */
4375 void
4376 note_mangling_alias (tree decl, tree id2)
4378 if (TARGET_SUPPORTS_ALIASES)
4380 if (!defer_mangling_aliases)
4381 generate_mangling_alias (decl, id2);
4382 else
4384 vec_safe_push (mangling_aliases, decl);
4385 vec_safe_push (mangling_aliases, id2);
4390 /* Emit all mangling aliases that were deferred up to this point. */
4392 void
4393 generate_mangling_aliases ()
4395 while (!vec_safe_is_empty (mangling_aliases))
4397 tree id2 = mangling_aliases->pop();
4398 tree decl = mangling_aliases->pop();
4399 generate_mangling_alias (decl, id2);
4401 defer_mangling_aliases = false;
4404 /* Record a mangling of DECL, whose DECL_ASSEMBLER_NAME has just been
4405 set. NEED_WARNING is true if we must warn about collisions. We do
4406 this to spot changes in mangling that may require compatibility
4407 aliases. */
4409 void
4410 record_mangling (tree decl, bool need_warning)
4412 if (!mangled_decls)
4413 mangled_decls = hash_table<mangled_decl_hash>::create_ggc (499);
4415 gcc_checking_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
4416 tree id = DECL_ASSEMBLER_NAME_RAW (decl);
4417 tree *slot
4418 = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id),
4419 INSERT);
4421 /* If this is already an alias, remove the alias, because the real
4422 decl takes precedence. */
4423 if (*slot && DECL_ARTIFICIAL (*slot) && DECL_IGNORED_P (*slot))
4424 if (symtab_node *n = symtab_node::get (*slot))
4425 if (n->cpp_implicit_alias)
4427 n->remove ();
4428 *slot = NULL_TREE;
4431 if (!*slot)
4432 *slot = decl;
4433 else if (need_warning)
4435 error_at (DECL_SOURCE_LOCATION (decl),
4436 "mangling of %q#D as %qE conflicts with a previous mangle",
4437 decl, id);
4438 inform (DECL_SOURCE_LOCATION (*slot),
4439 "previous mangling %q#D", *slot);
4440 inform (DECL_SOURCE_LOCATION (decl),
4441 "a later -fabi-version= (or =0)"
4442 " avoids this error with a change in mangling");
4443 *slot = decl;
4447 /* The mangled name of DECL is being forcibly changed to NAME. Remove
4448 any existing knowledge of DECL's mangled name meaning DECL. */
4450 void
4451 overwrite_mangling (tree decl, tree name)
4453 if (tree id = DECL_ASSEMBLER_NAME_RAW (decl))
4454 if ((TREE_CODE (decl) == VAR_DECL
4455 || TREE_CODE (decl) == FUNCTION_DECL)
4456 && mangled_decls)
4457 if (tree *slot
4458 = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id),
4459 NO_INSERT))
4460 if (*slot == decl)
4462 mangled_decls->clear_slot (slot);
4464 /* If this is an alias, remove it from the symbol table. */
4465 if (DECL_ARTIFICIAL (decl) && DECL_IGNORED_P (decl))
4466 if (symtab_node *n = symtab_node::get (decl))
4467 if (n->cpp_implicit_alias)
4468 n->remove ();
4471 DECL_ASSEMBLER_NAME_RAW (decl) = name;
4474 /* The entire file is now complete. If requested, dump everything
4475 to a file. */
4477 static void
4478 dump_tu (void)
4480 dump_flags_t flags;
4481 if (FILE *stream = dump_begin (raw_dump_id, &flags))
4483 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4484 dump_end (raw_dump_id, stream);
4488 static location_t locus_at_end_of_parsing;
4490 /* Check the deallocation functions for CODE to see if we want to warn that
4491 only one was defined. */
4493 static void
4494 maybe_warn_sized_delete (enum tree_code code)
4496 tree sized = NULL_TREE;
4497 tree unsized = NULL_TREE;
4499 for (ovl_iterator iter (get_global_binding (ovl_op_identifier (false, code)));
4500 iter; ++iter)
4502 tree fn = *iter;
4503 /* We're only interested in usual deallocation functions. */
4504 if (!usual_deallocation_fn_p (fn))
4505 continue;
4506 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
4507 unsized = fn;
4508 else
4509 sized = fn;
4511 if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
4512 warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
4513 "the program should also define %qD", sized);
4514 else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
4515 warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
4516 "the program should also define %qD", unsized);
4519 /* Check the global deallocation functions to see if we want to warn about
4520 defining unsized without sized (or vice versa). */
4522 static void
4523 maybe_warn_sized_delete ()
4525 if (!flag_sized_deallocation || !warn_sized_deallocation)
4526 return;
4527 maybe_warn_sized_delete (DELETE_EXPR);
4528 maybe_warn_sized_delete (VEC_DELETE_EXPR);
4531 /* Earlier we left PTRMEM_CST in variable initializers alone so that we could
4532 look them up when evaluating non-type template parameters. Now we need to
4533 lower them to something the back end can understand. */
4535 static void
4536 lower_var_init ()
4538 varpool_node *node;
4539 FOR_EACH_VARIABLE (node)
4541 tree d = node->decl;
4542 if (tree init = DECL_INITIAL (d))
4543 DECL_INITIAL (d) = cplus_expand_constant (init);
4547 /* This routine is called at the end of compilation.
4548 Its job is to create all the code needed to initialize and
4549 destroy the global aggregates. We do the destruction
4550 first, since that way we only need to reverse the decls once. */
4552 void
4553 c_parse_final_cleanups (void)
4555 tree vars;
4556 bool reconsider;
4557 size_t i;
4558 unsigned ssdf_count = 0;
4559 int retries = 0;
4560 tree decl;
4562 locus_at_end_of_parsing = input_location;
4563 at_eof = 1;
4565 /* Bad parse errors. Just forget about it. */
4566 if (! global_bindings_p () || current_class_type
4567 || !vec_safe_is_empty (decl_namespace_list))
4568 return;
4570 /* This is the point to write out a PCH if we're doing that.
4571 In that case we do not want to do anything else. */
4572 if (pch_file)
4574 /* Mangle all symbols at PCH creation time. */
4575 symtab_node *node;
4576 FOR_EACH_SYMBOL (node)
4577 if (! is_a <varpool_node *> (node)
4578 || ! DECL_HARD_REGISTER (node->decl))
4579 DECL_ASSEMBLER_NAME (node->decl);
4580 c_common_write_pch ();
4581 dump_tu ();
4582 /* Ensure even the callers don't try to finalize the CU. */
4583 flag_syntax_only = 1;
4584 return;
4587 timevar_stop (TV_PHASE_PARSING);
4588 timevar_start (TV_PHASE_DEFERRED);
4590 symtab->process_same_body_aliases ();
4592 /* Handle -fdump-ada-spec[-slim] */
4593 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4595 if (flag_dump_ada_spec_slim)
4596 collect_source_ref (main_input_filename);
4597 else
4598 collect_source_refs (global_namespace);
4600 dump_ada_specs (collect_all_refs, cpp_check);
4603 /* FIXME - huh? was input_line -= 1;*/
4605 /* We now have to write out all the stuff we put off writing out.
4606 These include:
4608 o Template specializations that we have not yet instantiated,
4609 but which are needed.
4610 o Initialization and destruction for non-local objects with
4611 static storage duration. (Local objects with static storage
4612 duration are initialized when their scope is first entered,
4613 and are cleaned up via atexit.)
4614 o Virtual function tables.
4616 All of these may cause others to be needed. For example,
4617 instantiating one function may cause another to be needed, and
4618 generating the initializer for an object may cause templates to be
4619 instantiated, etc., etc. */
4621 emit_support_tinfos ();
4625 tree t;
4626 tree decl;
4628 reconsider = false;
4630 /* If there are templates that we've put off instantiating, do
4631 them now. */
4632 instantiate_pending_templates (retries);
4633 ggc_collect ();
4635 /* Write out virtual tables as required. Writing out the
4636 virtual table for a template class may cause the
4637 instantiation of members of that class. If we write out
4638 vtables then we remove the class from our list so we don't
4639 have to look at it again. */
4640 for (i = keyed_classes->length ();
4641 keyed_classes->iterate (--i, &t);)
4642 if (maybe_emit_vtables (t))
4644 reconsider = true;
4645 keyed_classes->unordered_remove (i);
4648 /* Write out needed type info variables. We have to be careful
4649 looping through unemitted decls, because emit_tinfo_decl may
4650 cause other variables to be needed. New elements will be
4651 appended, and we remove from the vector those that actually
4652 get emitted. */
4653 for (i = unemitted_tinfo_decls->length ();
4654 unemitted_tinfo_decls->iterate (--i, &t);)
4655 if (emit_tinfo_decl (t))
4657 reconsider = true;
4658 unemitted_tinfo_decls->unordered_remove (i);
4661 /* The list of objects with static storage duration is built up
4662 in reverse order. We clear STATIC_AGGREGATES so that any new
4663 aggregates added during the initialization of these will be
4664 initialized in the correct order when we next come around the
4665 loop. */
4666 vars = prune_vars_needing_no_initialization (&static_aggregates);
4668 if (vars)
4670 /* We need to start a new initialization function each time
4671 through the loop. That's because we need to know which
4672 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4673 isn't computed until a function is finished, and written
4674 out. That's a deficiency in the back end. When this is
4675 fixed, these initialization functions could all become
4676 inline, with resulting performance improvements. */
4677 tree ssdf_body;
4679 /* Set the line and file, so that it is obviously not from
4680 the source file. */
4681 input_location = locus_at_end_of_parsing;
4682 ssdf_body = start_static_storage_duration_function (ssdf_count);
4684 /* Make sure the back end knows about all the variables. */
4685 write_out_vars (vars);
4687 /* First generate code to do all the initializations. */
4688 if (vars)
4689 do_static_initialization_or_destruction (vars, /*initp=*/true);
4691 /* Then, generate code to do all the destructions. Do these
4692 in reverse order so that the most recently constructed
4693 variable is the first destroyed. If we're using
4694 __cxa_atexit, then we don't need to do this; functions
4695 were registered at initialization time to destroy the
4696 local statics. */
4697 if (!flag_use_cxa_atexit && vars)
4699 vars = nreverse (vars);
4700 do_static_initialization_or_destruction (vars, /*initp=*/false);
4702 else
4703 vars = NULL_TREE;
4705 /* Finish up the static storage duration function for this
4706 round. */
4707 input_location = locus_at_end_of_parsing;
4708 finish_static_storage_duration_function (ssdf_body);
4710 /* All those initializations and finalizations might cause
4711 us to need more inline functions, more template
4712 instantiations, etc. */
4713 reconsider = true;
4714 ssdf_count++;
4715 /* ??? was: locus_at_end_of_parsing.line++; */
4718 /* Now do the same for thread_local variables. */
4719 handle_tls_init ();
4721 /* Go through the set of inline functions whose bodies have not
4722 been emitted yet. If out-of-line copies of these functions
4723 are required, emit them. */
4724 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4726 /* Does it need synthesizing? */
4727 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4728 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4730 /* Even though we're already at the top-level, we push
4731 there again. That way, when we pop back a few lines
4732 hence, all of our state is restored. Otherwise,
4733 finish_function doesn't clean things up, and we end
4734 up with CURRENT_FUNCTION_DECL set. */
4735 push_to_top_level ();
4736 /* The decl's location will mark where it was first
4737 needed. Save that so synthesize method can indicate
4738 where it was needed from, in case of error */
4739 input_location = DECL_SOURCE_LOCATION (decl);
4740 synthesize_method (decl);
4741 pop_from_top_level ();
4742 reconsider = true;
4745 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4746 generate_tls_wrapper (decl);
4748 if (!DECL_SAVED_TREE (decl))
4749 continue;
4751 cgraph_node *node = cgraph_node::get_create (decl);
4753 /* We lie to the back end, pretending that some functions
4754 are not defined when they really are. This keeps these
4755 functions from being put out unnecessarily. But, we must
4756 stop lying when the functions are referenced, or if they
4757 are not comdat since they need to be put out now. If
4758 DECL_INTERFACE_KNOWN, then we have already set
4759 DECL_EXTERNAL appropriately, so there's no need to check
4760 again, and we do not want to clear DECL_EXTERNAL if a
4761 previous call to import_export_decl set it.
4763 This is done in a separate for cycle, because if some
4764 deferred function is contained in another deferred
4765 function later in deferred_fns varray,
4766 rest_of_compilation would skip this function and we
4767 really cannot expand the same function twice. */
4768 import_export_decl (decl);
4769 if (DECL_NOT_REALLY_EXTERN (decl)
4770 && DECL_INITIAL (decl)
4771 && decl_needed_p (decl))
4773 if (node->cpp_implicit_alias)
4774 node = node->get_alias_target ();
4776 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
4777 NULL, true);
4778 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4779 group, we need to mark all symbols in the same comdat group
4780 that way. */
4781 if (node->same_comdat_group)
4782 for (cgraph_node *next
4783 = dyn_cast<cgraph_node *> (node->same_comdat_group);
4784 next != node;
4785 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
4786 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
4787 NULL, true);
4790 /* If we're going to need to write this function out, and
4791 there's already a body for it, create RTL for it now.
4792 (There might be no body if this is a method we haven't
4793 gotten around to synthesizing yet.) */
4794 if (!DECL_EXTERNAL (decl)
4795 && decl_needed_p (decl)
4796 && !TREE_ASM_WRITTEN (decl)
4797 && !node->definition)
4799 /* We will output the function; no longer consider it in this
4800 loop. */
4801 DECL_DEFER_OUTPUT (decl) = 0;
4802 /* Generate RTL for this function now that we know we
4803 need it. */
4804 expand_or_defer_fn (decl);
4805 /* If we're compiling -fsyntax-only pretend that this
4806 function has been written out so that we don't try to
4807 expand it again. */
4808 if (flag_syntax_only)
4809 TREE_ASM_WRITTEN (decl) = 1;
4810 reconsider = true;
4814 if (wrapup_namespace_globals ())
4815 reconsider = true;
4817 /* Static data members are just like namespace-scope globals. */
4818 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4820 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4821 /* Don't write it out if we haven't seen a definition. */
4822 || (DECL_IN_AGGR_P (decl) && !DECL_INLINE_VAR_P (decl)))
4823 continue;
4824 import_export_decl (decl);
4825 /* If this static data member is needed, provide it to the
4826 back end. */
4827 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4828 DECL_EXTERNAL (decl) = 0;
4830 if (vec_safe_length (pending_statics) != 0
4831 && wrapup_global_declarations (pending_statics->address (),
4832 pending_statics->length ()))
4833 reconsider = true;
4835 retries++;
4837 while (reconsider);
4839 lower_var_init ();
4841 generate_mangling_aliases ();
4843 /* All used inline functions must have a definition at this point. */
4844 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4846 if (/* Check online inline functions that were actually used. */
4847 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4848 /* If the definition actually was available here, then the
4849 fact that the function was not defined merely represents
4850 that for some reason (use of a template repository,
4851 #pragma interface, etc.) we decided not to emit the
4852 definition here. */
4853 && !DECL_INITIAL (decl)
4854 /* Don't complain if the template was defined. */
4855 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4856 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4857 (template_for_substitution (decl)))))
4859 warning_at (DECL_SOURCE_LOCATION (decl), 0,
4860 "inline function %qD used but never defined", decl);
4861 /* Avoid a duplicate warning from check_global_declaration. */
4862 TREE_NO_WARNING (decl) = 1;
4866 /* So must decls that use a type with no linkage. */
4867 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4868 no_linkage_error (decl);
4870 maybe_warn_sized_delete ();
4872 /* Then, do the Objective-C stuff. This is where all the
4873 Objective-C module stuff gets generated (symtab,
4874 class/protocol/selector lists etc). This must be done after C++
4875 templates, destructors etc. so that selectors used in C++
4876 templates are properly allocated. */
4877 if (c_dialect_objc ())
4878 objc_write_global_declarations ();
4880 /* We give C linkage to static constructors and destructors. */
4881 push_lang_context (lang_name_c);
4883 /* Generate initialization and destruction functions for all
4884 priorities for which they are required. */
4885 if (priority_info_map)
4886 splay_tree_foreach (priority_info_map,
4887 generate_ctor_and_dtor_functions_for_priority,
4888 /*data=*/&locus_at_end_of_parsing);
4889 else if (c_dialect_objc () && objc_static_init_needed_p ())
4890 /* If this is obj-c++ and we need a static init, call
4891 generate_ctor_or_dtor_function. */
4892 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4893 DEFAULT_INIT_PRIORITY,
4894 &locus_at_end_of_parsing);
4896 /* We're done with the splay-tree now. */
4897 if (priority_info_map)
4898 splay_tree_delete (priority_info_map);
4900 /* Generate any missing aliases. */
4901 maybe_apply_pending_pragma_weaks ();
4903 /* We're done with static constructors, so we can go back to "C++"
4904 linkage now. */
4905 pop_lang_context ();
4907 if (flag_vtable_verify)
4909 vtv_recover_class_info ();
4910 vtv_compute_class_hierarchy_transitive_closure ();
4911 vtv_build_vtable_verify_fndecl ();
4914 perform_deferred_noexcept_checks ();
4916 finish_repo ();
4917 fini_constexpr ();
4919 /* The entire file is now complete. If requested, dump everything
4920 to a file. */
4921 dump_tu ();
4923 if (flag_detailed_statistics)
4925 dump_tree_statistics ();
4926 dump_time_statistics ();
4929 timevar_stop (TV_PHASE_DEFERRED);
4930 timevar_start (TV_PHASE_PARSING);
4932 /* Indicate that we're done with front end processing. */
4933 at_eof = 2;
4936 /* Perform any post compilation-proper cleanups for the C++ front-end.
4937 This should really go away. No front-end should need to do
4938 anything past the compilation process. */
4940 void
4941 cxx_post_compilation_parsing_cleanups (void)
4943 timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS);
4945 if (flag_vtable_verify)
4947 /* Generate the special constructor initialization function that
4948 calls __VLTRegisterPairs, and give it a very high
4949 initialization priority. This must be done after
4950 finalize_compilation_unit so that we have accurate
4951 information about which vtable will actually be emitted. */
4952 vtv_generate_init_routine ();
4955 input_location = locus_at_end_of_parsing;
4957 if (flag_checking)
4958 validate_conversion_obstack ();
4960 timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS);
4963 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4964 function to call in parse-tree form; it has not yet been
4965 semantically analyzed. ARGS are the arguments to the function.
4966 They have already been semantically analyzed. This may change
4967 ARGS. */
4969 tree
4970 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4971 tsubst_flags_t complain)
4973 tree orig_fn;
4974 vec<tree, va_gc> *orig_args = NULL;
4975 tree expr;
4976 tree object;
4978 orig_fn = fn;
4979 object = TREE_OPERAND (fn, 0);
4981 if (processing_template_decl)
4983 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4984 || TREE_CODE (fn) == MEMBER_REF);
4985 if (type_dependent_expression_p (fn)
4986 || any_type_dependent_arguments_p (*args))
4987 return build_min_nt_call_vec (fn, *args);
4989 orig_args = make_tree_vector_copy (*args);
4991 /* Transform the arguments and add the implicit "this"
4992 parameter. That must be done before the FN is transformed
4993 because we depend on the form of FN. */
4994 make_args_non_dependent (*args);
4995 object = build_non_dependent_expr (object);
4996 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4998 if (TREE_CODE (fn) == DOTSTAR_EXPR)
4999 object = cp_build_addr_expr (object, complain);
5000 vec_safe_insert (*args, 0, object);
5002 /* Now that the arguments are done, transform FN. */
5003 fn = build_non_dependent_expr (fn);
5006 /* A qualified name corresponding to a bound pointer-to-member is
5007 represented as an OFFSET_REF:
5009 struct B { void g(); };
5010 void (B::*p)();
5011 void B::g() { (this->*p)(); } */
5012 if (TREE_CODE (fn) == OFFSET_REF)
5014 tree object_addr = cp_build_addr_expr (object, complain);
5015 fn = TREE_OPERAND (fn, 1);
5016 fn = get_member_function_from_ptrfunc (&object_addr, fn,
5017 complain);
5018 vec_safe_insert (*args, 0, object_addr);
5021 if (CLASS_TYPE_P (TREE_TYPE (fn)))
5022 expr = build_op_call (fn, args, complain);
5023 else
5024 expr = cp_build_function_call_vec (fn, args, complain);
5025 if (processing_template_decl && expr != error_mark_node)
5026 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
5028 if (orig_args != NULL)
5029 release_tree_vector (orig_args);
5031 return expr;
5035 void
5036 check_default_args (tree x)
5038 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
5039 bool saw_def = false;
5040 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
5041 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
5043 if (TREE_PURPOSE (arg))
5044 saw_def = true;
5045 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
5047 error ("default argument missing for parameter %P of %q+#D", i, x);
5048 TREE_PURPOSE (arg) = error_mark_node;
5053 /* Return true if function DECL can be inlined. This is used to force
5054 instantiation of methods that might be interesting for inlining. */
5055 bool
5056 possibly_inlined_p (tree decl)
5058 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
5059 if (DECL_UNINLINABLE (decl))
5060 return false;
5061 if (!optimize)
5062 return DECL_DECLARED_INLINE_P (decl);
5063 /* When optimizing, we might inline everything when flatten
5064 attribute or heuristics inlining for size or autoinlining
5065 is used. */
5066 return true;
5069 /* Normally, we can wait until instantiation-time to synthesize DECL.
5070 However, if DECL is a static data member initialized with a constant
5071 or a constexpr function, we need it right now because a reference to
5072 such a data member or a call to such function is not value-dependent.
5073 For a function that uses auto in the return type, we need to instantiate
5074 it to find out its type. For OpenMP user defined reductions, we need
5075 them instantiated for reduction clauses which inline them by hand
5076 directly. */
5078 static void
5079 maybe_instantiate_decl (tree decl)
5081 if (DECL_LANG_SPECIFIC (decl)
5082 && DECL_TEMPLATE_INFO (decl)
5083 && (decl_maybe_constant_var_p (decl)
5084 || (TREE_CODE (decl) == FUNCTION_DECL
5085 && DECL_OMP_DECLARE_REDUCTION_P (decl))
5086 || undeduced_auto_decl (decl))
5087 && !DECL_DECLARED_CONCEPT_P (decl)
5088 && !uses_template_parms (DECL_TI_ARGS (decl)))
5090 /* Instantiating a function will result in garbage collection. We
5091 must treat this situation as if we were within the body of a
5092 function so as to avoid collecting live data only referenced from
5093 the stack (such as overload resolution candidates). */
5094 ++function_depth;
5095 instantiate_decl (decl, /*defer_ok=*/false,
5096 /*expl_inst_class_mem_p=*/false);
5097 --function_depth;
5101 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
5102 If DECL is a specialization or implicitly declared class member,
5103 generate the actual definition. Return false if something goes
5104 wrong, true otherwise. */
5106 bool
5107 mark_used (tree decl, tsubst_flags_t complain)
5109 /* If DECL is a BASELINK for a single function, then treat it just
5110 like the DECL for the function. Otherwise, if the BASELINK is
5111 for an overloaded function, we don't know which function was
5112 actually used until after overload resolution. */
5113 if (BASELINK_P (decl))
5115 decl = BASELINK_FUNCTIONS (decl);
5116 if (really_overloaded_fn (decl))
5117 return true;
5118 decl = OVL_FIRST (decl);
5121 /* Set TREE_USED for the benefit of -Wunused. */
5122 TREE_USED (decl) = 1;
5123 /* And for structured bindings also the underlying decl. */
5124 if (DECL_DECOMPOSITION_P (decl) && DECL_DECOMP_BASE (decl))
5125 TREE_USED (DECL_DECOMP_BASE (decl)) = 1;
5127 if (TREE_CODE (decl) == TEMPLATE_DECL)
5128 return true;
5130 if (DECL_CLONED_FUNCTION_P (decl))
5131 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5133 /* Mark enumeration types as used. */
5134 if (TREE_CODE (decl) == CONST_DECL)
5135 used_types_insert (DECL_CONTEXT (decl));
5137 if (TREE_CODE (decl) == FUNCTION_DECL
5138 && !maybe_instantiate_noexcept (decl, complain))
5139 return false;
5141 if (TREE_CODE (decl) == FUNCTION_DECL
5142 && DECL_DELETED_FN (decl))
5144 if (DECL_ARTIFICIAL (decl)
5145 && DECL_CONV_FN_P (decl)
5146 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
5147 /* We mark a lambda conversion op as deleted if we can't
5148 generate it properly; see maybe_add_lambda_conv_op. */
5149 sorry ("converting lambda that uses %<...%> to function pointer");
5150 else if (complain & tf_error)
5152 error ("use of deleted function %qD", decl);
5153 if (!maybe_explain_implicit_delete (decl))
5154 inform (DECL_SOURCE_LOCATION (decl), "declared here");
5156 return false;
5159 if (TREE_DEPRECATED (decl) && (complain & tf_warning)
5160 && deprecated_state != DEPRECATED_SUPPRESS)
5161 warn_deprecated_use (decl, NULL_TREE);
5163 /* We can only check DECL_ODR_USED on variables or functions with
5164 DECL_LANG_SPECIFIC set, and these are also the only decls that we
5165 might need special handling for. */
5166 if (!VAR_OR_FUNCTION_DECL_P (decl)
5167 || DECL_LANG_SPECIFIC (decl) == NULL
5168 || DECL_THUNK_P (decl))
5170 if (!processing_template_decl
5171 && !require_deduced_type (decl, complain))
5172 return false;
5173 return true;
5176 /* We only want to do this processing once. We don't need to keep trying
5177 to instantiate inline templates, because unit-at-a-time will make sure
5178 we get them compiled before functions that want to inline them. */
5179 if (DECL_ODR_USED (decl))
5180 return true;
5182 /* Normally, we can wait until instantiation-time to synthesize DECL.
5183 However, if DECL is a static data member initialized with a constant
5184 or a constexpr function, we need it right now because a reference to
5185 such a data member or a call to such function is not value-dependent.
5186 For a function that uses auto in the return type, we need to instantiate
5187 it to find out its type. For OpenMP user defined reductions, we need
5188 them instantiated for reduction clauses which inline them by hand
5189 directly. */
5190 maybe_instantiate_decl (decl);
5192 if (processing_template_decl || in_template_function ())
5193 return true;
5195 /* Check this too in case we're within instantiate_non_dependent_expr. */
5196 if (DECL_TEMPLATE_INFO (decl)
5197 && uses_template_parms (DECL_TI_ARGS (decl)))
5198 return true;
5200 if (!require_deduced_type (decl, complain))
5201 return false;
5203 if (builtin_pack_fn_p (decl))
5205 error ("use of built-in parameter pack %qD outside of a template",
5206 DECL_NAME (decl));
5207 return false;
5210 /* If we don't need a value, then we don't need to synthesize DECL. */
5211 if (cp_unevaluated_operand || in_discarded_stmt)
5212 return true;
5214 DECL_ODR_USED (decl) = 1;
5215 if (DECL_CLONED_FUNCTION_P (decl))
5216 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5218 /* DR 757: A type without linkage shall not be used as the type of a
5219 variable or function with linkage, unless
5220 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5221 o the variable or function is not used (3.2 [basic.def.odr]) or is
5222 defined in the same translation unit. */
5223 if (cxx_dialect > cxx98
5224 && decl_linkage (decl) != lk_none
5225 && !DECL_EXTERN_C_P (decl)
5226 && !DECL_ARTIFICIAL (decl)
5227 && !decl_defined_p (decl)
5228 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
5230 if (is_local_extern (decl))
5231 /* There's no way to define a local extern, and adding it to
5232 the vector interferes with GC, so give an error now. */
5233 no_linkage_error (decl);
5234 else
5235 vec_safe_push (no_linkage_decls, decl);
5238 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
5239 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
5240 /* Remember it, so we can check it was defined. */
5241 note_vague_linkage_fn (decl);
5243 /* Is it a synthesized method that needs to be synthesized? */
5244 if (TREE_CODE (decl) == FUNCTION_DECL
5245 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5246 && DECL_DEFAULTED_FN (decl)
5247 /* A function defaulted outside the class is synthesized either by
5248 cp_finish_decl or instantiate_decl. */
5249 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
5250 && ! DECL_INITIAL (decl))
5252 /* Defer virtual destructors so that thunks get the right
5253 linkage. */
5254 if (DECL_VIRTUAL_P (decl) && !at_eof)
5256 note_vague_linkage_fn (decl);
5257 return true;
5260 /* Remember the current location for a function we will end up
5261 synthesizing. Then we can inform the user where it was
5262 required in the case of error. */
5263 DECL_SOURCE_LOCATION (decl) = input_location;
5265 /* Synthesizing an implicitly defined member function will result in
5266 garbage collection. We must treat this situation as if we were
5267 within the body of a function so as to avoid collecting live data
5268 on the stack (such as overload resolution candidates).
5270 We could just let cp_write_global_declarations handle synthesizing
5271 this function by adding it to deferred_fns, but doing
5272 it at the use site produces better error messages. */
5273 ++function_depth;
5274 synthesize_method (decl);
5275 --function_depth;
5276 /* If this is a synthesized method we don't need to
5277 do the instantiation test below. */
5279 else if (VAR_OR_FUNCTION_DECL_P (decl)
5280 && DECL_TEMPLATE_INFO (decl)
5281 && !DECL_DECLARED_CONCEPT_P (decl)
5282 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5283 || always_instantiate_p (decl)))
5284 /* If this is a function or variable that is an instance of some
5285 template, we now know that we will need to actually do the
5286 instantiation. We check that DECL is not an explicit
5287 instantiation because that is not checked in instantiate_decl.
5289 We put off instantiating functions in order to improve compile
5290 times. Maintaining a stack of active functions is expensive,
5291 and the inliner knows to instantiate any functions it might
5292 need. Therefore, we always try to defer instantiation. */
5294 ++function_depth;
5295 instantiate_decl (decl, /*defer_ok=*/true,
5296 /*expl_inst_class_mem_p=*/false);
5297 --function_depth;
5300 return true;
5303 bool
5304 mark_used (tree decl)
5306 return mark_used (decl, tf_warning_or_error);
5309 tree
5310 vtv_start_verification_constructor_init_function (void)
5312 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5315 tree
5316 vtv_finish_verification_constructor_init_function (tree function_body)
5318 tree fn;
5320 finish_compound_stmt (function_body);
5321 fn = finish_function (/*inline_p=*/false);
5322 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5323 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5325 return fn;
5328 #include "gt-cp-decl2.h"