[C++ PATCH] overloaded operator fns [2/N]
[official-gcc.git] / gcc / cp / decl2.c
bloba23b96c53e734cd4661a83a7f5626ff51bca0e34
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 /* Nothing is deletable. Everything is insertable. */
127 static bool is_deleted (value_type) { return false; }
128 static void mark_deleted (value_type) { gcc_unreachable (); }
131 /* A hash table of decls keyed by mangled name. Used to figure out if
132 we need compatibility aliases. */
133 static GTY(()) hash_table<mangled_decl_hash> *mangled_decls;
135 /* Nonzero if we're done parsing and into end-of-file activities. */
137 int at_eof;
139 /* True if note_mangling_alias should enqueue mangling aliases for
140 later generation, rather than emitting them right away. */
142 bool defer_mangling_aliases = true;
145 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
146 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
147 that apply to the function). */
149 tree
150 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
151 cp_ref_qualifier rqual)
153 tree raises;
154 tree attrs;
155 int type_quals;
156 bool late_return_type_p;
158 if (fntype == error_mark_node || ctype == error_mark_node)
159 return error_mark_node;
161 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
162 || TREE_CODE (fntype) == METHOD_TYPE);
164 type_quals = quals & ~TYPE_QUAL_RESTRICT;
165 ctype = cp_build_qualified_type (ctype, type_quals);
166 raises = TYPE_RAISES_EXCEPTIONS (fntype);
167 attrs = TYPE_ATTRIBUTES (fntype);
168 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
169 fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
170 (TREE_CODE (fntype) == METHOD_TYPE
171 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
172 : TYPE_ARG_TYPES (fntype)));
173 if (attrs)
174 fntype = cp_build_type_attribute_variant (fntype, attrs);
175 if (rqual)
176 fntype = build_ref_qualified_type (fntype, rqual);
177 if (raises)
178 fntype = build_exception_variant (fntype, raises);
179 if (late_return_type_p)
180 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
182 return fntype;
185 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
186 return type changed to NEW_RET. */
188 tree
189 change_return_type (tree new_ret, tree fntype)
191 tree newtype;
192 tree args = TYPE_ARG_TYPES (fntype);
193 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
194 tree attrs = TYPE_ATTRIBUTES (fntype);
195 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
197 if (new_ret == error_mark_node)
198 return fntype;
200 if (same_type_p (new_ret, TREE_TYPE (fntype)))
201 return fntype;
203 if (TREE_CODE (fntype) == FUNCTION_TYPE)
205 newtype = build_function_type (new_ret, args);
206 newtype = apply_memfn_quals (newtype,
207 type_memfn_quals (fntype),
208 type_memfn_rqual (fntype));
210 else
211 newtype = build_method_type_directly
212 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
213 if (FUNCTION_REF_QUALIFIED (fntype))
214 newtype = build_ref_qualified_type (newtype, type_memfn_rqual (fntype));
215 if (raises)
216 newtype = build_exception_variant (newtype, raises);
217 if (attrs)
218 newtype = cp_build_type_attribute_variant (newtype, attrs);
219 if (late_return_type_p)
220 TYPE_HAS_LATE_RETURN_TYPE (newtype) = 1;
222 return newtype;
225 /* Build a PARM_DECL of FN with NAME and TYPE, and set DECL_ARG_TYPE
226 appropriately. */
228 tree
229 cp_build_parm_decl (tree fn, tree name, tree type)
231 tree parm = build_decl (input_location,
232 PARM_DECL, name, type);
233 DECL_CONTEXT (parm) = fn;
235 /* DECL_ARG_TYPE is only used by the back end and the back end never
236 sees templates. */
237 if (!processing_template_decl)
238 DECL_ARG_TYPE (parm) = type_passed_as (type);
240 return parm;
243 /* Returns a PARM_DECL of FN for a parameter of the indicated TYPE, with the
244 indicated NAME. */
246 tree
247 build_artificial_parm (tree fn, tree name, tree type)
249 tree parm = cp_build_parm_decl (fn, name, type);
250 DECL_ARTIFICIAL (parm) = 1;
251 /* All our artificial parms are implicitly `const'; they cannot be
252 assigned to. */
253 TREE_READONLY (parm) = 1;
254 return parm;
257 /* Constructors for types with virtual baseclasses need an "in-charge" flag
258 saying whether this constructor is responsible for initialization of
259 virtual baseclasses or not. All destructors also need this "in-charge"
260 flag, which additionally determines whether or not the destructor should
261 free the memory for the object.
263 This function adds the "in-charge" flag to member function FN if
264 appropriate. It is called from grokclassfn and tsubst.
265 FN must be either a constructor or destructor.
267 The in-charge flag follows the 'this' parameter, and is followed by the
268 VTT parm (if any), then the user-written parms. */
270 void
271 maybe_retrofit_in_chrg (tree fn)
273 tree basetype, arg_types, parms, parm, fntype;
275 /* If we've already add the in-charge parameter don't do it again. */
276 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
277 return;
279 /* When processing templates we can't know, in general, whether or
280 not we're going to have virtual baseclasses. */
281 if (processing_template_decl)
282 return;
284 /* We don't need an in-charge parameter for constructors that don't
285 have virtual bases. */
286 if (DECL_CONSTRUCTOR_P (fn)
287 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
288 return;
290 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
291 basetype = TREE_TYPE (TREE_VALUE (arg_types));
292 arg_types = TREE_CHAIN (arg_types);
294 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
296 /* If this is a subobject constructor or destructor, our caller will
297 pass us a pointer to our VTT. */
298 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
300 parm = build_artificial_parm (fn, vtt_parm_identifier, vtt_parm_type);
302 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
303 DECL_CHAIN (parm) = parms;
304 parms = parm;
306 /* ...and then to TYPE_ARG_TYPES. */
307 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
309 DECL_HAS_VTT_PARM_P (fn) = 1;
312 /* Then add the in-charge parm (before the VTT parm). */
313 parm = build_artificial_parm (fn, in_charge_identifier, integer_type_node);
314 DECL_CHAIN (parm) = parms;
315 parms = parm;
316 arg_types = hash_tree_chain (integer_type_node, arg_types);
318 /* Insert our new parameter(s) into the list. */
319 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
321 /* And rebuild the function type. */
322 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
323 arg_types);
324 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
325 fntype = build_exception_variant (fntype,
326 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
327 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
328 fntype = (cp_build_type_attribute_variant
329 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
330 TREE_TYPE (fn) = fntype;
332 /* Now we've got the in-charge parameter. */
333 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
336 /* Classes overload their constituent function names automatically.
337 When a function name is declared in a record structure,
338 its name is changed to it overloaded name. Since names for
339 constructors and destructors can conflict, we place a leading
340 '$' for destructors.
342 CNAME is the name of the class we are grokking for.
344 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
346 FLAGS contains bits saying what's special about today's
347 arguments. DTOR_FLAG == DESTRUCTOR.
349 If FUNCTION is a destructor, then we must add the `auto-delete' field
350 as a second parameter. There is some hair associated with the fact
351 that we must "declare" this variable in the manner consistent with the
352 way the rest of the arguments were declared.
354 QUALS are the qualifiers for the this pointer. */
356 void
357 grokclassfn (tree ctype, tree function, enum overload_flags flags)
359 tree fn_name = DECL_NAME (function);
361 /* Even within an `extern "C"' block, members get C++ linkage. See
362 [dcl.link] for details. */
363 SET_DECL_LANGUAGE (function, lang_cplusplus);
365 if (fn_name == NULL_TREE)
367 error ("name missing for member function");
368 fn_name = get_identifier ("<anonymous>");
369 DECL_NAME (function) = fn_name;
372 DECL_CONTEXT (function) = ctype;
374 if (flags == DTOR_FLAG)
375 DECL_CXX_DESTRUCTOR_P (function) = 1;
377 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
378 maybe_retrofit_in_chrg (function);
381 /* Create an ARRAY_REF, checking for the user doing things backwards
382 along the way. DECLTYPE_P is for N3276, as in the parser. */
384 tree
385 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
386 bool decltype_p)
388 tree type;
389 tree expr;
390 tree orig_array_expr = array_expr;
391 tree orig_index_exp = index_exp;
392 tree overload = NULL_TREE;
394 if (error_operand_p (array_expr) || error_operand_p (index_exp))
395 return error_mark_node;
397 if (processing_template_decl)
399 if (type_dependent_expression_p (array_expr)
400 || type_dependent_expression_p (index_exp))
401 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
402 NULL_TREE, NULL_TREE);
403 array_expr = build_non_dependent_expr (array_expr);
404 index_exp = build_non_dependent_expr (index_exp);
407 type = TREE_TYPE (array_expr);
408 gcc_assert (type);
409 type = non_reference (type);
411 /* If they have an `operator[]', use that. */
412 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
414 tsubst_flags_t complain = tf_warning_or_error;
415 if (decltype_p)
416 complain |= tf_decltype;
417 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
418 index_exp, NULL_TREE, &overload, complain);
420 else
422 tree p1, p2, i1, i2;
424 /* Otherwise, create an ARRAY_REF for a pointer or array type.
425 It is a little-known fact that, if `a' is an array and `i' is
426 an int, you can write `i[a]', which means the same thing as
427 `a[i]'. */
428 if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
429 p1 = array_expr;
430 else
431 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
433 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
434 p2 = index_exp;
435 else
436 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
438 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
439 false);
440 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
441 false);
443 if ((p1 && i2) && (i1 && p2))
444 error ("ambiguous conversion for array subscript");
446 if (p1 && i2)
447 array_expr = p1, index_exp = i2;
448 else if (i1 && p2)
449 array_expr = p2, index_exp = i1;
450 else
452 error ("invalid types %<%T[%T]%> for array subscript",
453 type, TREE_TYPE (index_exp));
454 return error_mark_node;
457 if (array_expr == error_mark_node || index_exp == error_mark_node)
458 error ("ambiguous conversion for array subscript");
460 if (TREE_CODE (TREE_TYPE (array_expr)) == POINTER_TYPE)
461 array_expr = mark_rvalue_use (array_expr);
462 else
463 array_expr = mark_lvalue_use_nonread (array_expr);
464 index_exp = mark_rvalue_use (index_exp);
465 expr = build_array_ref (input_location, array_expr, index_exp);
467 if (processing_template_decl && expr != error_mark_node)
469 if (overload != NULL_TREE)
470 return (build_min_non_dep_op_overload
471 (ARRAY_REF, expr, overload, orig_array_expr, orig_index_exp));
473 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
474 NULL_TREE, NULL_TREE);
476 return expr;
479 /* Given the cast expression EXP, checking out its validity. Either return
480 an error_mark_node if there was an unavoidable error, return a cast to
481 void for trying to delete a pointer w/ the value 0, or return the
482 call to delete. If DOING_VEC is true, we handle things differently
483 for doing an array delete.
484 Implements ARM $5.3.4. This is called from the parser. */
486 tree
487 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
488 tsubst_flags_t complain)
490 tree t, type;
492 if (exp == error_mark_node)
493 return exp;
495 if (processing_template_decl)
497 t = build_min (DELETE_EXPR, void_type_node, exp, size);
498 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
499 DELETE_EXPR_USE_VEC (t) = doing_vec;
500 TREE_SIDE_EFFECTS (t) = 1;
501 return t;
504 /* An array can't have been allocated by new, so complain. */
505 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
506 warning (0, "deleting array %q#E", exp);
508 t = build_expr_type_conversion (WANT_POINTER, exp, true);
510 if (t == NULL_TREE || t == error_mark_node)
512 error ("type %q#T argument given to %<delete%>, expected pointer",
513 TREE_TYPE (exp));
514 return error_mark_node;
517 type = TREE_TYPE (t);
519 /* As of Valley Forge, you can delete a pointer to const. */
521 /* You can't delete functions. */
522 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
524 error ("cannot delete a function. Only pointer-to-objects are "
525 "valid arguments to %<delete%>");
526 return error_mark_node;
529 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
530 if (VOID_TYPE_P (TREE_TYPE (type)))
532 warning (OPT_Wdelete_incomplete, "deleting %qT is undefined", type);
533 doing_vec = 0;
536 /* Deleting a pointer with the value zero is valid and has no effect. */
537 if (integer_zerop (t))
538 return build1 (NOP_EXPR, void_type_node, t);
540 if (doing_vec)
541 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
542 sfk_deleting_destructor,
543 use_global_delete, complain);
544 else
545 return build_delete (type, t, sfk_deleting_destructor,
546 LOOKUP_NORMAL, use_global_delete,
547 complain);
550 /* Report an error if the indicated template declaration is not the
551 sort of thing that should be a member template. */
553 void
554 check_member_template (tree tmpl)
556 tree decl;
558 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
559 decl = DECL_TEMPLATE_RESULT (tmpl);
561 if (TREE_CODE (decl) == FUNCTION_DECL
562 || DECL_ALIAS_TEMPLATE_P (tmpl)
563 || (TREE_CODE (decl) == TYPE_DECL
564 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
566 /* The parser rejects template declarations in local classes
567 (with the exception of generic lambdas). */
568 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
569 /* The parser rejects any use of virtual in a function template. */
570 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
571 && DECL_VIRTUAL_P (decl)));
573 /* The debug-information generating code doesn't know what to do
574 with member templates. */
575 DECL_IGNORED_P (tmpl) = 1;
577 else if (variable_template_p (tmpl))
578 /* OK */;
579 else
580 error ("template declaration of %q#D", decl);
583 /* Sanity check: report error if this function FUNCTION is not
584 really a member of the class (CTYPE) it is supposed to belong to.
585 TEMPLATE_PARMS is used to specify the template parameters of a member
586 template passed as FUNCTION_DECL. If the member template is passed as a
587 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
588 from the declaration. If the function is not a function template, it
589 must be NULL.
590 It returns the original declaration for the function, NULL_TREE if
591 no declaration was found, error_mark_node if an error was emitted. */
593 tree
594 check_classfn (tree ctype, tree function, tree template_parms)
596 if (DECL_USE_TEMPLATE (function)
597 && !(TREE_CODE (function) == TEMPLATE_DECL
598 && DECL_TEMPLATE_SPECIALIZATION (function))
599 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
600 /* Since this is a specialization of a member template,
601 we're not going to find the declaration in the class.
602 For example, in:
604 struct S { template <typename T> void f(T); };
605 template <> void S::f(int);
607 we're not going to find `S::f(int)', but there's no
608 reason we should, either. We let our callers know we didn't
609 find the method, but we don't complain. */
610 return NULL_TREE;
612 /* Basic sanity check: for a template function, the template parameters
613 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
614 if (TREE_CODE (function) == TEMPLATE_DECL)
616 if (template_parms
617 && !comp_template_parms (template_parms,
618 DECL_TEMPLATE_PARMS (function)))
620 error ("template parameter lists provided don%'t match the "
621 "template parameters of %qD", function);
622 return error_mark_node;
624 template_parms = DECL_TEMPLATE_PARMS (function);
627 /* OK, is this a definition of a member template? */
628 bool is_template = (template_parms != NULL_TREE);
630 /* [temp.mem]
632 A destructor shall not be a member template. */
633 if (DECL_DESTRUCTOR_P (function) && is_template)
635 error ("destructor %qD declared as member template", function);
636 return error_mark_node;
639 /* We must enter the scope here, because conversion operators are
640 named by target type, and type equivalence relies on typenames
641 resolving within the scope of CTYPE. */
642 tree pushed_scope = push_scope (ctype);
643 tree matched = NULL_TREE;
644 tree fns = get_class_binding (ctype, DECL_NAME (function));
646 for (ovl_iterator iter (fns); !matched && iter; ++iter)
648 tree fndecl = *iter;
650 /* A member template definition only matches a member template
651 declaration. */
652 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
653 continue;
655 if (!DECL_DECLARES_FUNCTION_P (fndecl))
656 continue;
658 tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
659 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
661 /* We cannot simply call decls_match because this doesn't work
662 for static member functions that are pretending to be
663 methods, and because the name may have been changed by
664 asm("new_name"). */
666 /* Get rid of the this parameter on functions that become
667 static. */
668 if (DECL_STATIC_FUNCTION_P (fndecl)
669 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
670 p1 = TREE_CHAIN (p1);
672 /* ref-qualifier or absence of same must match. */
673 if (type_memfn_rqual (TREE_TYPE (function))
674 != type_memfn_rqual (TREE_TYPE (fndecl)))
675 continue;
677 // Include constraints in the match.
678 tree c1 = get_constraints (function);
679 tree c2 = get_constraints (fndecl);
681 /* While finding a match, same types and params are not enough
682 if the function is versioned. Also check version ("target")
683 attributes. */
684 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
685 TREE_TYPE (TREE_TYPE (fndecl)))
686 && compparms (p1, p2)
687 && !targetm.target_option.function_versions (function, fndecl)
688 && (!is_template
689 || comp_template_parms (template_parms,
690 DECL_TEMPLATE_PARMS (fndecl)))
691 && equivalent_constraints (c1, c2)
692 && (DECL_TEMPLATE_SPECIALIZATION (function)
693 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
694 && (!DECL_TEMPLATE_SPECIALIZATION (function)
695 || (DECL_TI_TEMPLATE (function) == DECL_TI_TEMPLATE (fndecl))))
696 matched = fndecl;
699 if (!matched)
701 if (!COMPLETE_TYPE_P (ctype))
702 cxx_incomplete_type_error (function, ctype);
703 else
705 if (DECL_CONV_FN_P (function))
706 fns = get_class_binding (ctype, conv_op_identifier);
708 error_at (DECL_SOURCE_LOCATION (function),
709 "no declaration matches %q#D", function);
710 if (fns)
711 print_candidates (fns);
712 else if (DECL_CONV_FN_P (function))
713 inform (DECL_SOURCE_LOCATION (function),
714 "no conversion operators declared");
715 else
716 inform (DECL_SOURCE_LOCATION (function),
717 "no functions named %qD", function);
718 inform (DECL_SOURCE_LOCATION (TYPE_NAME (ctype)),
719 "%#qT defined here", ctype);
721 matched = error_mark_node;
724 if (pushed_scope)
725 pop_scope (pushed_scope);
727 return matched;
730 /* DECL is a function with vague linkage. Remember it so that at the
731 end of the translation unit we can decide whether or not to emit
732 it. */
734 void
735 note_vague_linkage_fn (tree decl)
737 DECL_DEFER_OUTPUT (decl) = 1;
738 vec_safe_push (deferred_fns, decl);
741 /* As above, but for variable template instantiations. */
743 void
744 note_variable_template_instantiation (tree decl)
746 vec_safe_push (pending_statics, decl);
749 /* We have just processed the DECL, which is a static data member.
750 The other parameters are as for cp_finish_decl. */
752 void
753 finish_static_data_member_decl (tree decl,
754 tree init, bool init_const_expr_p,
755 tree asmspec_tree,
756 int flags)
758 DECL_CONTEXT (decl) = current_class_type;
760 /* We cannot call pushdecl here, because that would fill in the
761 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
762 the right thing, namely, to put this decl out straight away. */
764 if (! processing_template_decl)
765 vec_safe_push (pending_statics, decl);
767 if (LOCAL_CLASS_P (current_class_type)
768 /* We already complained about the template definition. */
769 && !DECL_TEMPLATE_INSTANTIATION (decl))
770 permerror (input_location, "local class %q#T shall not have static data member %q#D",
771 current_class_type, decl);
772 else
773 for (tree t = current_class_type; TYPE_P (t);
774 t = CP_TYPE_CONTEXT (t))
775 if (TYPE_UNNAMED_P (t))
777 if (permerror (DECL_SOURCE_LOCATION (decl),
778 "static data member %qD in unnamed class", decl))
779 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
780 "unnamed class defined here");
781 break;
784 DECL_IN_AGGR_P (decl) = 1;
786 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
787 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
788 SET_VAR_HAD_UNKNOWN_BOUND (decl);
790 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
793 /* DECLARATOR and DECLSPECS correspond to a class member. The other
794 parameters are as for cp_finish_decl. Return the DECL for the
795 class member declared. */
797 tree
798 grokfield (const cp_declarator *declarator,
799 cp_decl_specifier_seq *declspecs,
800 tree init, bool init_const_expr_p,
801 tree asmspec_tree,
802 tree attrlist)
804 tree value;
805 const char *asmspec = 0;
806 int flags;
807 tree name;
809 if (init
810 && TREE_CODE (init) == TREE_LIST
811 && TREE_VALUE (init) == error_mark_node
812 && TREE_CHAIN (init) == NULL_TREE)
813 init = NULL_TREE;
815 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
816 if (! value || value == error_mark_node)
817 /* friend or constructor went bad. */
818 return error_mark_node;
819 if (TREE_TYPE (value) == error_mark_node)
820 return value;
822 if (TREE_CODE (value) == TYPE_DECL && init)
824 error ("typedef %qD is initialized (use decltype instead)", value);
825 init = NULL_TREE;
828 /* Pass friendly classes back. */
829 if (value == void_type_node)
830 return value;
833 name = DECL_NAME (value);
835 if (name != NULL_TREE)
837 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
839 error ("explicit template argument list not allowed");
840 return error_mark_node;
843 if (IDENTIFIER_POINTER (name)[0] == '_'
844 && id_equal (name, "_vptr"))
845 error ("member %qD conflicts with virtual function table field name",
846 value);
849 /* Stash away type declarations. */
850 if (TREE_CODE (value) == TYPE_DECL)
852 DECL_NONLOCAL (value) = 1;
853 DECL_CONTEXT (value) = current_class_type;
855 if (attrlist)
857 int attrflags = 0;
859 /* If this is a typedef that names the class for linkage purposes
860 (7.1.3p8), apply any attributes directly to the type. */
861 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
862 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
863 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
865 cplus_decl_attributes (&value, attrlist, attrflags);
868 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
869 && TREE_TYPE (value) != error_mark_node
870 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
871 set_underlying_type (value);
873 /* It's important that push_template_decl below follows
874 set_underlying_type above so that the created template
875 carries the properly set type of VALUE. */
876 if (processing_template_decl)
877 value = push_template_decl (value);
879 record_locally_defined_typedef (value);
880 return value;
883 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
885 if (!friendp && DECL_IN_AGGR_P (value))
887 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
888 return void_type_node;
891 if (asmspec_tree && asmspec_tree != error_mark_node)
892 asmspec = TREE_STRING_POINTER (asmspec_tree);
894 if (init)
896 if (TREE_CODE (value) == FUNCTION_DECL)
898 if (init == ridpointers[(int)RID_DELETE])
900 DECL_DELETED_FN (value) = 1;
901 DECL_DECLARED_INLINE_P (value) = 1;
902 DECL_INITIAL (value) = error_mark_node;
904 else if (init == ridpointers[(int)RID_DEFAULT])
906 if (defaultable_fn_check (value))
908 DECL_DEFAULTED_FN (value) = 1;
909 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
910 DECL_DECLARED_INLINE_P (value) = 1;
913 else if (TREE_CODE (init) == DEFAULT_ARG)
914 error ("invalid initializer for member function %qD", value);
915 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
917 if (integer_zerop (init))
918 DECL_PURE_VIRTUAL_P (value) = 1;
919 else if (error_operand_p (init))
920 ; /* An error has already been reported. */
921 else
922 error ("invalid initializer for member function %qD",
923 value);
925 else
927 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
928 if (friendp)
929 error ("initializer specified for friend function %qD",
930 value);
931 else
932 error ("initializer specified for static member function %qD",
933 value);
936 else if (TREE_CODE (value) == FIELD_DECL)
937 /* C++11 NSDMI, keep going. */;
938 else if (!VAR_P (value))
939 gcc_unreachable ();
942 /* Pass friend decls back. */
943 if ((TREE_CODE (value) == FUNCTION_DECL
944 || TREE_CODE (value) == TEMPLATE_DECL)
945 && DECL_CONTEXT (value) != current_class_type)
946 return value;
948 /* Need to set this before push_template_decl. */
949 if (VAR_P (value))
950 DECL_CONTEXT (value) = current_class_type;
952 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
954 value = push_template_decl (value);
955 if (error_operand_p (value))
956 return error_mark_node;
959 if (attrlist)
960 cplus_decl_attributes (&value, attrlist, 0);
962 if (init && DIRECT_LIST_INIT_P (init))
963 flags = LOOKUP_NORMAL;
964 else
965 flags = LOOKUP_IMPLICIT;
967 switch (TREE_CODE (value))
969 case VAR_DECL:
970 finish_static_data_member_decl (value, init, init_const_expr_p,
971 asmspec_tree, flags);
972 return value;
974 case FIELD_DECL:
975 if (asmspec)
976 error ("%<asm%> specifiers are not permitted on non-static data members");
977 if (DECL_INITIAL (value) == error_mark_node)
978 init = error_mark_node;
979 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
980 NULL_TREE, flags);
981 DECL_IN_AGGR_P (value) = 1;
982 return value;
984 case FUNCTION_DECL:
985 if (asmspec)
986 set_user_assembler_name (value, asmspec);
988 cp_finish_decl (value,
989 /*init=*/NULL_TREE,
990 /*init_const_expr_p=*/false,
991 asmspec_tree, flags);
993 /* Pass friends back this way. */
994 if (DECL_FRIEND_P (value))
995 return void_type_node;
997 DECL_IN_AGGR_P (value) = 1;
998 return value;
1000 default:
1001 gcc_unreachable ();
1003 return NULL_TREE;
1006 /* Like `grokfield', but for bitfields.
1007 WIDTH is the width of the bitfield, a constant expression.
1008 The other parameters are as for grokfield. */
1010 tree
1011 grokbitfield (const cp_declarator *declarator,
1012 cp_decl_specifier_seq *declspecs, tree width, tree init,
1013 tree attrlist)
1015 tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1016 init != NULL_TREE, &attrlist);
1018 if (value == error_mark_node)
1019 return NULL_TREE; /* friends went bad. */
1020 if (TREE_TYPE (value) == error_mark_node)
1021 return value;
1023 /* Pass friendly classes back. */
1024 if (VOID_TYPE_P (value))
1025 return void_type_node;
1027 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
1028 && (POINTER_TYPE_P (value)
1029 || !dependent_type_p (TREE_TYPE (value))))
1031 error ("bit-field %qD with non-integral type", value);
1032 return error_mark_node;
1035 if (TREE_CODE (value) == TYPE_DECL)
1037 error ("cannot declare %qD to be a bit-field type", value);
1038 return NULL_TREE;
1041 /* Usually, finish_struct_1 catches bitfields with invalid types.
1042 But, in the case of bitfields with function type, we confuse
1043 ourselves into thinking they are member functions, so we must
1044 check here. */
1045 if (TREE_CODE (value) == FUNCTION_DECL)
1047 error ("cannot declare bit-field %qD with function type",
1048 DECL_NAME (value));
1049 return NULL_TREE;
1052 if (width && TYPE_WARN_IF_NOT_ALIGN (TREE_TYPE (value)))
1054 error ("cannot declare bit-field %qD with %<warn_if_not_aligned%> type",
1055 DECL_NAME (value));
1056 return NULL_TREE;
1059 if (DECL_IN_AGGR_P (value))
1061 error ("%qD is already defined in the class %qT", value,
1062 DECL_CONTEXT (value));
1063 return void_type_node;
1066 if (TREE_STATIC (value))
1068 error ("static member %qD cannot be a bit-field", value);
1069 return NULL_TREE;
1072 int flags = LOOKUP_IMPLICIT;
1073 if (init && DIRECT_LIST_INIT_P (init))
1074 flags = LOOKUP_NORMAL;
1075 cp_finish_decl (value, init, false, NULL_TREE, flags);
1077 if (width != error_mark_node)
1079 /* The width must be an integer type. */
1080 if (!type_dependent_expression_p (width)
1081 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1082 error ("width of bit-field %qD has non-integral type %qT", value,
1083 TREE_TYPE (width));
1084 else
1086 /* Temporarily stash the width in DECL_BIT_FIELD_REPRESENTATIVE.
1087 check_bitfield_decl picks it from there later and sets DECL_SIZE
1088 accordingly. */
1089 DECL_BIT_FIELD_REPRESENTATIVE (value) = width;
1090 SET_DECL_C_BIT_FIELD (value);
1094 DECL_IN_AGGR_P (value) = 1;
1096 if (attrlist)
1097 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1099 return value;
1103 /* Returns true iff ATTR is an attribute which needs to be applied at
1104 instantiation time rather than template definition time. */
1106 static bool
1107 is_late_template_attribute (tree attr, tree decl)
1109 tree name = get_attribute_name (attr);
1110 tree args = TREE_VALUE (attr);
1111 const struct attribute_spec *spec = lookup_attribute_spec (name);
1112 tree arg;
1114 if (!spec)
1115 /* Unknown attribute. */
1116 return false;
1118 /* Attribute weak handling wants to write out assembly right away. */
1119 if (is_attribute_p ("weak", name))
1120 return true;
1122 /* Attributes used and unused are applied directly, as they appertain to
1123 decls. */
1124 if (is_attribute_p ("unused", name)
1125 || is_attribute_p ("used", name))
1126 return false;
1128 /* Attribute tls_model wants to modify the symtab. */
1129 if (is_attribute_p ("tls_model", name))
1130 return true;
1132 /* #pragma omp declare simd attribute needs to be always deferred. */
1133 if (flag_openmp
1134 && is_attribute_p ("omp declare simd", name))
1135 return true;
1137 /* An attribute pack is clearly dependent. */
1138 if (args && PACK_EXPANSION_P (args))
1139 return true;
1141 /* If any of the arguments are dependent expressions, we can't evaluate
1142 the attribute until instantiation time. */
1143 for (arg = args; arg; arg = TREE_CHAIN (arg))
1145 tree t = TREE_VALUE (arg);
1147 /* If the first attribute argument is an identifier, only consider
1148 second and following arguments. Attributes like mode, format,
1149 cleanup and several target specific attributes aren't late
1150 just because they have an IDENTIFIER_NODE as first argument. */
1151 if (arg == args && attribute_takes_identifier_p (name)
1152 && identifier_p (t))
1153 continue;
1155 if (value_dependent_expression_p (t)
1156 || type_dependent_expression_p (t))
1157 return true;
1160 if (TREE_CODE (decl) == TYPE_DECL
1161 || TYPE_P (decl)
1162 || spec->type_required)
1164 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1166 /* We can't apply any attributes to a completely unknown type until
1167 instantiation time. */
1168 enum tree_code code = TREE_CODE (type);
1169 if (code == TEMPLATE_TYPE_PARM
1170 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1171 || code == TYPENAME_TYPE)
1172 return true;
1173 /* Also defer most attributes on dependent types. This is not
1174 necessary in all cases, but is the better default. */
1175 else if (dependent_type_p (type)
1176 /* But some attributes specifically apply to templates. */
1177 && !is_attribute_p ("abi_tag", name)
1178 && !is_attribute_p ("deprecated", name)
1179 && !is_attribute_p ("visibility", name))
1180 return true;
1181 else
1182 return false;
1184 else
1185 return false;
1188 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1189 applied at instantiation time and return them. If IS_DEPENDENT is true,
1190 the declaration itself is dependent, so all attributes should be applied
1191 at instantiation time. */
1193 static tree
1194 splice_template_attributes (tree *attr_p, tree decl)
1196 tree *p = attr_p;
1197 tree late_attrs = NULL_TREE;
1198 tree *q = &late_attrs;
1200 if (!p)
1201 return NULL_TREE;
1203 for (; *p; )
1205 if (is_late_template_attribute (*p, decl))
1207 ATTR_IS_DEPENDENT (*p) = 1;
1208 *q = *p;
1209 *p = TREE_CHAIN (*p);
1210 q = &TREE_CHAIN (*q);
1211 *q = NULL_TREE;
1213 else
1214 p = &TREE_CHAIN (*p);
1217 return late_attrs;
1220 /* Remove any late attributes from the list in ATTR_P and attach them to
1221 DECL_P. */
1223 static void
1224 save_template_attributes (tree *attr_p, tree *decl_p)
1226 tree *q;
1228 if (attr_p && *attr_p == error_mark_node)
1229 return;
1231 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1232 if (!late_attrs)
1233 return;
1235 if (DECL_P (*decl_p))
1236 q = &DECL_ATTRIBUTES (*decl_p);
1237 else
1238 q = &TYPE_ATTRIBUTES (*decl_p);
1240 tree old_attrs = *q;
1242 /* Merge the late attributes at the beginning with the attribute
1243 list. */
1244 late_attrs = merge_attributes (late_attrs, *q);
1245 *q = late_attrs;
1247 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1249 /* We've added new attributes directly to the main variant, so
1250 now we need to update all of the other variants to include
1251 these new attributes. */
1252 tree variant;
1253 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1254 variant = TYPE_NEXT_VARIANT (variant))
1256 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1257 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1262 /* True if ATTRS contains any dependent attributes that affect type
1263 identity. */
1265 bool
1266 any_dependent_type_attributes_p (tree attrs)
1268 for (tree a = attrs; a; a = TREE_CHAIN (a))
1269 if (ATTR_IS_DEPENDENT (a))
1271 const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (a));
1272 if (as && as->affects_type_identity)
1273 return true;
1275 return false;
1278 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1279 to a typedef which gives a previously unnamed class or enum a name for
1280 linkage purposes. */
1282 bool
1283 attributes_naming_typedef_ok (tree attrs)
1285 for (; attrs; attrs = TREE_CHAIN (attrs))
1287 tree name = get_attribute_name (attrs);
1288 if (is_attribute_p ("vector_size", name))
1289 return false;
1291 return true;
1294 /* Like reconstruct_complex_type, but handle also template trees. */
1296 tree
1297 cp_reconstruct_complex_type (tree type, tree bottom)
1299 tree inner, outer;
1300 bool late_return_type_p = false;
1302 if (TYPE_PTR_P (type))
1304 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1305 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1306 TYPE_REF_CAN_ALIAS_ALL (type));
1308 else if (TREE_CODE (type) == REFERENCE_TYPE)
1310 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1311 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1312 TYPE_REF_CAN_ALIAS_ALL (type));
1314 else if (TREE_CODE (type) == ARRAY_TYPE)
1316 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1317 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1318 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1319 element type qualification will be handled by the recursive
1320 cp_reconstruct_complex_type call and cp_build_qualified_type
1321 for ARRAY_TYPEs changes the element type. */
1322 return outer;
1324 else if (TREE_CODE (type) == FUNCTION_TYPE)
1326 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1327 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1328 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1329 outer = apply_memfn_quals (outer,
1330 type_memfn_quals (type),
1331 type_memfn_rqual (type));
1333 else if (TREE_CODE (type) == METHOD_TYPE)
1335 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1336 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1337 /* The build_method_type_directly() routine prepends 'this' to argument list,
1338 so we must compensate by getting rid of it. */
1339 outer
1340 = build_method_type_directly
1341 (class_of_this_parm (type), inner,
1342 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1344 else if (TREE_CODE (type) == OFFSET_TYPE)
1346 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1347 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1349 else
1350 return bottom;
1352 if (TYPE_ATTRIBUTES (type))
1353 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1354 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1356 if (late_return_type_p)
1357 TYPE_HAS_LATE_RETURN_TYPE (outer) = 1;
1359 return outer;
1362 /* Replaces any constexpr expression that may be into the attributes
1363 arguments with their reduced value. */
1365 static void
1366 cp_check_const_attributes (tree attributes)
1368 if (attributes == error_mark_node)
1369 return;
1371 tree attr;
1372 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1374 tree arg;
1375 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1377 tree expr = TREE_VALUE (arg);
1378 if (EXPR_P (expr))
1379 TREE_VALUE (arg) = maybe_constant_value (expr);
1384 /* Return true if TYPE is an OpenMP mappable type. */
1385 bool
1386 cp_omp_mappable_type (tree type)
1388 /* Mappable type has to be complete. */
1389 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1390 return false;
1391 /* Arrays have mappable type if the elements have mappable type. */
1392 while (TREE_CODE (type) == ARRAY_TYPE)
1393 type = TREE_TYPE (type);
1394 /* A mappable type cannot contain virtual members. */
1395 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1396 return false;
1397 /* All data members must be non-static. */
1398 if (CLASS_TYPE_P (type))
1400 tree field;
1401 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1402 if (VAR_P (field))
1403 return false;
1404 /* All fields must have mappable types. */
1405 else if (TREE_CODE (field) == FIELD_DECL
1406 && !cp_omp_mappable_type (TREE_TYPE (field)))
1407 return false;
1409 return true;
1412 /* Like decl_attributes, but handle C++ complexity. */
1414 void
1415 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1417 if (*decl == NULL_TREE || *decl == void_type_node
1418 || *decl == error_mark_node)
1419 return;
1421 /* Add implicit "omp declare target" attribute if requested. */
1422 if (scope_chain->omp_declare_target_attribute
1423 && ((VAR_P (*decl)
1424 && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
1425 || TREE_CODE (*decl) == FUNCTION_DECL))
1427 if (VAR_P (*decl)
1428 && DECL_CLASS_SCOPE_P (*decl))
1429 error ("%q+D static data member inside of declare target directive",
1430 *decl);
1431 else if (!processing_template_decl
1432 && VAR_P (*decl)
1433 && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1434 error ("%q+D in declare target directive does not have mappable type",
1435 *decl);
1436 else
1437 attributes = tree_cons (get_identifier ("omp declare target"),
1438 NULL_TREE, attributes);
1441 if (processing_template_decl)
1443 if (check_for_bare_parameter_packs (attributes))
1444 return;
1446 save_template_attributes (&attributes, decl);
1449 cp_check_const_attributes (attributes);
1451 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1452 decl = &DECL_TEMPLATE_RESULT (*decl);
1454 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1456 attributes
1457 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1458 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1459 attributes, flags);
1461 else
1462 decl_attributes (decl, attributes, flags);
1464 if (TREE_CODE (*decl) == TYPE_DECL)
1465 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1467 /* Propagate deprecation out to the template. */
1468 if (TREE_DEPRECATED (*decl))
1469 if (tree ti = get_template_info (*decl))
1471 tree tmpl = TI_TEMPLATE (ti);
1472 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1473 : DECL_TEMPLATE_RESULT (tmpl));
1474 if (*decl == pattern)
1475 TREE_DEPRECATED (tmpl) = true;
1479 /* Walks through the namespace- or function-scope anonymous union
1480 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1481 Returns one of the fields for use in the mangled name. */
1483 static tree
1484 build_anon_union_vars (tree type, tree object)
1486 tree main_decl = NULL_TREE;
1487 tree field;
1489 /* Rather than write the code to handle the non-union case,
1490 just give an error. */
1491 if (TREE_CODE (type) != UNION_TYPE)
1493 error ("anonymous struct not inside named type");
1494 return error_mark_node;
1497 for (field = TYPE_FIELDS (type);
1498 field != NULL_TREE;
1499 field = DECL_CHAIN (field))
1501 tree decl;
1502 tree ref;
1504 if (DECL_ARTIFICIAL (field))
1505 continue;
1506 if (TREE_CODE (field) != FIELD_DECL)
1508 permerror (DECL_SOURCE_LOCATION (field),
1509 "%q#D invalid; an anonymous union can only "
1510 "have non-static data members", field);
1511 continue;
1514 if (TREE_PRIVATE (field))
1515 permerror (DECL_SOURCE_LOCATION (field),
1516 "private member %q#D in anonymous union", field);
1517 else if (TREE_PROTECTED (field))
1518 permerror (DECL_SOURCE_LOCATION (field),
1519 "protected member %q#D in anonymous union", field);
1521 if (processing_template_decl)
1522 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1523 DECL_NAME (field), NULL_TREE);
1524 else
1525 ref = build_class_member_access_expr (object, field, NULL_TREE,
1526 false, tf_warning_or_error);
1528 if (DECL_NAME (field))
1530 tree base;
1532 decl = build_decl (input_location,
1533 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1534 DECL_ANON_UNION_VAR_P (decl) = 1;
1535 DECL_ARTIFICIAL (decl) = 1;
1537 base = get_base_address (object);
1538 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1539 TREE_STATIC (decl) = TREE_STATIC (base);
1540 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1542 SET_DECL_VALUE_EXPR (decl, ref);
1543 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1545 decl = pushdecl (decl);
1547 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1548 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1549 else
1550 decl = 0;
1552 if (main_decl == NULL_TREE)
1553 main_decl = decl;
1556 return main_decl;
1559 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1560 anonymous union, then all members must be laid out together. PUBLIC_P
1561 is nonzero if this union is not declared static. */
1563 void
1564 finish_anon_union (tree anon_union_decl)
1566 tree type;
1567 tree main_decl;
1568 bool public_p;
1570 if (anon_union_decl == error_mark_node)
1571 return;
1573 type = TREE_TYPE (anon_union_decl);
1574 public_p = TREE_PUBLIC (anon_union_decl);
1576 /* The VAR_DECL's context is the same as the TYPE's context. */
1577 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1579 if (TYPE_FIELDS (type) == NULL_TREE)
1580 return;
1582 if (public_p)
1584 error ("namespace-scope anonymous aggregates must be static");
1585 return;
1588 main_decl = build_anon_union_vars (type, anon_union_decl);
1589 if (main_decl == error_mark_node)
1590 return;
1591 if (main_decl == NULL_TREE)
1593 pedwarn (input_location, 0, "anonymous union with no members");
1594 return;
1597 if (!processing_template_decl)
1599 /* Use main_decl to set the mangled name. */
1600 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1601 maybe_commonize_var (anon_union_decl);
1602 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1603 mangle_decl (anon_union_decl);
1604 DECL_NAME (anon_union_decl) = NULL_TREE;
1607 pushdecl (anon_union_decl);
1608 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1611 /* Auxiliary functions to make type signatures for
1612 `operator new' and `operator delete' correspond to
1613 what compiler will be expecting. */
1615 tree
1616 coerce_new_type (tree type)
1618 int e = 0;
1619 tree args = TYPE_ARG_TYPES (type);
1621 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1623 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1625 e = 1;
1626 error ("%<operator new%> must return type %qT", ptr_type_node);
1629 if (args && args != void_list_node)
1631 if (TREE_PURPOSE (args))
1633 /* [basic.stc.dynamic.allocation]
1635 The first parameter shall not have an associated default
1636 argument. */
1637 error ("the first parameter of %<operator new%> cannot "
1638 "have a default argument");
1639 /* Throw away the default argument. */
1640 TREE_PURPOSE (args) = NULL_TREE;
1643 if (!same_type_p (TREE_VALUE (args), size_type_node))
1645 e = 2;
1646 args = TREE_CHAIN (args);
1649 else
1650 e = 2;
1652 if (e == 2)
1653 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1654 "as first parameter", size_type_node);
1656 switch (e)
1658 case 2:
1659 args = tree_cons (NULL_TREE, size_type_node, args);
1660 /* Fall through. */
1661 case 1:
1662 type = build_exception_variant
1663 (build_function_type (ptr_type_node, args),
1664 TYPE_RAISES_EXCEPTIONS (type));
1665 /* Fall through. */
1666 default:;
1668 return type;
1671 tree
1672 coerce_delete_type (tree type)
1674 int e = 0;
1675 tree args = TYPE_ARG_TYPES (type);
1677 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1679 if (!same_type_p (TREE_TYPE (type), void_type_node))
1681 e = 1;
1682 error ("%<operator delete%> must return type %qT", void_type_node);
1685 if (!args || args == void_list_node
1686 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1688 e = 2;
1689 if (args && args != void_list_node)
1690 args = TREE_CHAIN (args);
1691 error ("%<operator delete%> takes type %qT as first parameter",
1692 ptr_type_node);
1694 switch (e)
1696 case 2:
1697 args = tree_cons (NULL_TREE, ptr_type_node, args);
1698 /* Fall through. */
1699 case 1:
1700 type = build_exception_variant
1701 (build_function_type (void_type_node, args),
1702 TYPE_RAISES_EXCEPTIONS (type));
1703 /* Fall through. */
1704 default:;
1707 return type;
1710 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1711 and mark them as needed. */
1713 static void
1714 mark_vtable_entries (tree decl)
1716 tree fnaddr;
1717 unsigned HOST_WIDE_INT idx;
1719 /* It's OK for the vtable to refer to deprecated virtual functions. */
1720 warning_sentinel w(warn_deprecated_decl);
1722 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1723 idx, fnaddr)
1725 tree fn;
1727 STRIP_NOPS (fnaddr);
1729 if (TREE_CODE (fnaddr) != ADDR_EXPR
1730 && TREE_CODE (fnaddr) != FDESC_EXPR)
1731 /* This entry is an offset: a virtual base class offset, a
1732 virtual call offset, an RTTI offset, etc. */
1733 continue;
1735 fn = TREE_OPERAND (fnaddr, 0);
1736 TREE_ADDRESSABLE (fn) = 1;
1737 /* When we don't have vcall offsets, we output thunks whenever
1738 we output the vtables that contain them. With vcall offsets,
1739 we know all the thunks we'll need when we emit a virtual
1740 function, so we emit the thunks there instead. */
1741 if (DECL_THUNK_P (fn))
1742 use_thunk (fn, /*emit_p=*/0);
1743 mark_used (fn);
1747 /* Set DECL up to have the closest approximation of "initialized common"
1748 linkage available. */
1750 void
1751 comdat_linkage (tree decl)
1753 if (flag_weak)
1754 make_decl_one_only (decl, cxx_comdat_group (decl));
1755 else if (TREE_CODE (decl) == FUNCTION_DECL
1756 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1757 /* We can just emit function and compiler-generated variables
1758 statically; having multiple copies is (for the most part) only
1759 a waste of space.
1761 There are two correctness issues, however: the address of a
1762 template instantiation with external linkage should be the
1763 same, independent of what translation unit asks for the
1764 address, and this will not hold when we emit multiple copies of
1765 the function. However, there's little else we can do.
1767 Also, by default, the typeinfo implementation assumes that
1768 there will be only one copy of the string used as the name for
1769 each type. Therefore, if weak symbols are unavailable, the
1770 run-time library should perform a more conservative check; it
1771 should perform a string comparison, rather than an address
1772 comparison. */
1773 TREE_PUBLIC (decl) = 0;
1774 else
1776 /* Static data member template instantiations, however, cannot
1777 have multiple copies. */
1778 if (DECL_INITIAL (decl) == 0
1779 || DECL_INITIAL (decl) == error_mark_node)
1780 DECL_COMMON (decl) = 1;
1781 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1783 DECL_COMMON (decl) = 1;
1784 DECL_INITIAL (decl) = error_mark_node;
1786 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1788 /* We can't do anything useful; leave vars for explicit
1789 instantiation. */
1790 DECL_EXTERNAL (decl) = 1;
1791 DECL_NOT_REALLY_EXTERN (decl) = 0;
1795 if (TREE_PUBLIC (decl))
1796 DECL_COMDAT (decl) = 1;
1799 /* For win32 we also want to put explicit instantiations in
1800 linkonce sections, so that they will be merged with implicit
1801 instantiations; otherwise we get duplicate symbol errors.
1802 For Darwin we do not want explicit instantiations to be
1803 linkonce. */
1805 void
1806 maybe_make_one_only (tree decl)
1808 /* We used to say that this was not necessary on targets that support weak
1809 symbols, because the implicit instantiations will defer to the explicit
1810 one. However, that's not actually the case in SVR4; a strong definition
1811 after a weak one is an error. Also, not making explicit
1812 instantiations one_only means that we can end up with two copies of
1813 some template instantiations. */
1814 if (! flag_weak)
1815 return;
1817 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1818 we can get away with not emitting them if they aren't used. We need
1819 to for variables so that cp_finish_decl will update their linkage,
1820 because their DECL_INITIAL may not have been set properly yet. */
1822 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1823 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1824 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1826 make_decl_one_only (decl, cxx_comdat_group (decl));
1828 if (VAR_P (decl))
1830 varpool_node *node = varpool_node::get_create (decl);
1831 DECL_COMDAT (decl) = 1;
1832 /* Mark it needed so we don't forget to emit it. */
1833 node->forced_by_abi = true;
1834 TREE_USED (decl) = 1;
1839 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1840 This predicate will give the right answer during parsing of the
1841 function, which other tests may not. */
1843 bool
1844 vague_linkage_p (tree decl)
1846 if (!TREE_PUBLIC (decl))
1848 /* maybe_thunk_body clears TREE_PUBLIC on the maybe-in-charge 'tor
1849 variants, check one of the "clones" for the real linkage. */
1850 if ((DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
1851 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl))
1852 && DECL_CHAIN (decl)
1853 && DECL_CLONED_FUNCTION (DECL_CHAIN (decl)))
1854 return vague_linkage_p (DECL_CHAIN (decl));
1856 gcc_checking_assert (!DECL_COMDAT (decl));
1857 return false;
1859 /* Unfortunately, import_export_decl has not always been called
1860 before the function is processed, so we cannot simply check
1861 DECL_COMDAT. */
1862 if (DECL_COMDAT (decl)
1863 || (TREE_CODE (decl) == FUNCTION_DECL
1864 && DECL_DECLARED_INLINE_P (decl))
1865 || (DECL_LANG_SPECIFIC (decl)
1866 && DECL_TEMPLATE_INSTANTIATION (decl))
1867 || (VAR_P (decl) && DECL_INLINE_VAR_P (decl)))
1868 return true;
1869 else if (DECL_FUNCTION_SCOPE_P (decl))
1870 /* A local static in an inline effectively has vague linkage. */
1871 return (TREE_STATIC (decl)
1872 && vague_linkage_p (DECL_CONTEXT (decl)));
1873 else
1874 return false;
1877 /* Determine whether or not we want to specifically import or export CTYPE,
1878 using various heuristics. */
1880 static void
1881 import_export_class (tree ctype)
1883 /* -1 for imported, 1 for exported. */
1884 int import_export = 0;
1886 /* It only makes sense to call this function at EOF. The reason is
1887 that this function looks at whether or not the first non-inline
1888 non-abstract virtual member function has been defined in this
1889 translation unit. But, we can't possibly know that until we've
1890 seen the entire translation unit. */
1891 gcc_assert (at_eof);
1893 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1894 return;
1896 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1897 we will have CLASSTYPE_INTERFACE_ONLY set but not
1898 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1899 heuristic because someone will supply a #pragma implementation
1900 elsewhere, and deducing it here would produce a conflict. */
1901 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1902 return;
1904 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1905 import_export = -1;
1906 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1907 import_export = 1;
1908 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1909 && !flag_implicit_templates)
1910 /* For a template class, without -fimplicit-templates, check the
1911 repository. If the virtual table is assigned to this
1912 translation unit, then export the class; otherwise, import
1913 it. */
1914 import_export = repo_export_class_p (ctype) ? 1 : -1;
1915 else if (TYPE_POLYMORPHIC_P (ctype))
1917 /* The ABI specifies that the virtual table and associated
1918 information are emitted with the key method, if any. */
1919 tree method = CLASSTYPE_KEY_METHOD (ctype);
1920 /* If weak symbol support is not available, then we must be
1921 careful not to emit the vtable when the key function is
1922 inline. An inline function can be defined in multiple
1923 translation units. If we were to emit the vtable in each
1924 translation unit containing a definition, we would get
1925 multiple definition errors at link-time. */
1926 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1927 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1930 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1931 a definition anywhere else. */
1932 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1933 import_export = 0;
1935 /* Allow back ends the chance to overrule the decision. */
1936 if (targetm.cxx.import_export_class)
1937 import_export = targetm.cxx.import_export_class (ctype, import_export);
1939 if (import_export)
1941 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1942 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1946 /* Return true if VAR has already been provided to the back end; in that
1947 case VAR should not be modified further by the front end. */
1948 static bool
1949 var_finalized_p (tree var)
1951 return varpool_node::get_create (var)->definition;
1954 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1955 must be emitted in this translation unit. Mark it as such. */
1957 void
1958 mark_needed (tree decl)
1960 TREE_USED (decl) = 1;
1961 if (TREE_CODE (decl) == FUNCTION_DECL)
1963 /* Extern inline functions don't become needed when referenced.
1964 If we know a method will be emitted in other TU and no new
1965 functions can be marked reachable, just use the external
1966 definition. */
1967 struct cgraph_node *node = cgraph_node::get_create (decl);
1968 node->forced_by_abi = true;
1970 /* #pragma interface and -frepo code can call mark_needed for
1971 maybe-in-charge 'tors; mark the clones as well. */
1972 tree clone;
1973 FOR_EACH_CLONE (clone, decl)
1974 mark_needed (clone);
1976 else if (VAR_P (decl))
1978 varpool_node *node = varpool_node::get_create (decl);
1979 /* C++ frontend use mark_decl_references to force COMDAT variables
1980 to be output that might appear dead otherwise. */
1981 node->forced_by_abi = true;
1985 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1986 returns true if a definition of this entity should be provided in
1987 this object file. Callers use this function to determine whether
1988 or not to let the back end know that a definition of DECL is
1989 available in this translation unit. */
1991 bool
1992 decl_needed_p (tree decl)
1994 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
1995 /* This function should only be called at the end of the translation
1996 unit. We cannot be sure of whether or not something will be
1997 COMDAT until that point. */
1998 gcc_assert (at_eof);
2000 /* All entities with external linkage that are not COMDAT/EXTERN should be
2001 emitted; they may be referred to from other object files. */
2002 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
2003 return true;
2004 /* Functions marked "dllexport" must be emitted so that they are
2005 visible to other DLLs. */
2006 if (flag_keep_inline_dllexport
2007 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
2008 return true;
2010 /* When not optimizing, do not bother to produce definitions for extern
2011 symbols. */
2012 if (DECL_REALLY_EXTERN (decl)
2013 && ((TREE_CODE (decl) != FUNCTION_DECL
2014 && !optimize)
2015 || (TREE_CODE (decl) == FUNCTION_DECL
2016 && !opt_for_fn (decl, optimize)))
2017 && !lookup_attribute ("always_inline", decl))
2018 return false;
2020 /* If this entity was used, let the back end see it; it will decide
2021 whether or not to emit it into the object file. */
2022 if (TREE_USED (decl))
2023 return true;
2025 /* Virtual functions might be needed for devirtualization. */
2026 if (flag_devirtualize
2027 && TREE_CODE (decl) == FUNCTION_DECL
2028 && DECL_VIRTUAL_P (decl))
2029 return true;
2031 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2032 reference to DECL might cause it to be emitted later. */
2033 return false;
2036 /* If necessary, write out the vtables for the dynamic class CTYPE.
2037 Returns true if any vtables were emitted. */
2039 static bool
2040 maybe_emit_vtables (tree ctype)
2042 tree vtbl;
2043 tree primary_vtbl;
2044 int needed = 0;
2045 varpool_node *current = NULL, *last = NULL;
2047 /* If the vtables for this class have already been emitted there is
2048 nothing more to do. */
2049 primary_vtbl = CLASSTYPE_VTABLES (ctype);
2050 if (var_finalized_p (primary_vtbl))
2051 return false;
2052 /* Ignore dummy vtables made by get_vtable_decl. */
2053 if (TREE_TYPE (primary_vtbl) == void_type_node)
2054 return false;
2056 /* On some targets, we cannot determine the key method until the end
2057 of the translation unit -- which is when this function is
2058 called. */
2059 if (!targetm.cxx.key_method_may_be_inline ())
2060 determine_key_method (ctype);
2062 /* See if any of the vtables are needed. */
2063 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2065 import_export_decl (vtbl);
2066 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2067 needed = 1;
2069 if (!needed)
2071 /* If the references to this class' vtables are optimized away,
2072 still emit the appropriate debugging information. See
2073 dfs_debug_mark. */
2074 if (DECL_COMDAT (primary_vtbl)
2075 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2076 note_debug_info_needed (ctype);
2077 return false;
2080 /* The ABI requires that we emit all of the vtables if we emit any
2081 of them. */
2082 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2084 /* Mark entities references from the virtual table as used. */
2085 mark_vtable_entries (vtbl);
2087 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2089 vec<tree, va_gc> *cleanups = NULL;
2090 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2091 LOOKUP_NORMAL);
2093 /* It had better be all done at compile-time. */
2094 gcc_assert (!expr && !cleanups);
2097 /* Write it out. */
2098 DECL_EXTERNAL (vtbl) = 0;
2099 rest_of_decl_compilation (vtbl, 1, 1);
2101 /* Because we're only doing syntax-checking, we'll never end up
2102 actually marking the variable as written. */
2103 if (flag_syntax_only)
2104 TREE_ASM_WRITTEN (vtbl) = 1;
2105 else if (DECL_ONE_ONLY (vtbl))
2107 current = varpool_node::get_create (vtbl);
2108 if (last)
2109 current->add_to_same_comdat_group (last);
2110 last = current;
2114 /* Since we're writing out the vtable here, also write the debug
2115 info. */
2116 note_debug_info_needed (ctype);
2118 return true;
2121 /* A special return value from type_visibility meaning internal
2122 linkage. */
2124 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2126 /* walk_tree helper function for type_visibility. */
2128 static tree
2129 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2131 int *vis_p = (int *)data;
2132 if (! TYPE_P (*tp))
2134 *walk_subtrees = 0;
2136 else if (OVERLOAD_TYPE_P (*tp)
2137 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2139 *vis_p = VISIBILITY_ANON;
2140 return *tp;
2142 else if (CLASS_TYPE_P (*tp)
2143 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2144 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2145 return NULL;
2148 /* Returns the visibility of TYPE, which is the minimum visibility of its
2149 component types. */
2151 static int
2152 type_visibility (tree type)
2154 int vis = VISIBILITY_DEFAULT;
2155 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2156 return vis;
2159 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2160 specified (or if VISIBILITY is static). If TMPL is true, this
2161 constraint is for a template argument, and takes precedence
2162 over explicitly-specified visibility on the template. */
2164 static void
2165 constrain_visibility (tree decl, int visibility, bool tmpl)
2167 if (visibility == VISIBILITY_ANON)
2169 /* extern "C" declarations aren't affected by the anonymous
2170 namespace. */
2171 if (!DECL_EXTERN_C_P (decl))
2173 TREE_PUBLIC (decl) = 0;
2174 DECL_WEAK (decl) = 0;
2175 DECL_COMMON (decl) = 0;
2176 DECL_COMDAT (decl) = false;
2177 if (VAR_OR_FUNCTION_DECL_P (decl))
2179 struct symtab_node *snode = symtab_node::get (decl);
2181 if (snode)
2182 snode->set_comdat_group (NULL);
2184 DECL_INTERFACE_KNOWN (decl) = 1;
2185 if (DECL_LANG_SPECIFIC (decl))
2186 DECL_NOT_REALLY_EXTERN (decl) = 1;
2189 else if (visibility > DECL_VISIBILITY (decl)
2190 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2192 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2193 /* This visibility was not specified. */
2194 DECL_VISIBILITY_SPECIFIED (decl) = false;
2198 /* Constrain the visibility of DECL based on the visibility of its template
2199 arguments. */
2201 static void
2202 constrain_visibility_for_template (tree decl, tree targs)
2204 /* If this is a template instantiation, check the innermost
2205 template args for visibility constraints. The outer template
2206 args are covered by the class check. */
2207 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2208 int i;
2209 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2211 int vis = 0;
2213 tree arg = TREE_VEC_ELT (args, i-1);
2214 if (TYPE_P (arg))
2215 vis = type_visibility (arg);
2216 else
2218 if (REFERENCE_REF_P (arg))
2219 arg = TREE_OPERAND (arg, 0);
2220 if (TREE_TYPE (arg))
2221 STRIP_NOPS (arg);
2222 if (TREE_CODE (arg) == ADDR_EXPR)
2223 arg = TREE_OPERAND (arg, 0);
2224 if (VAR_OR_FUNCTION_DECL_P (arg))
2226 if (! TREE_PUBLIC (arg))
2227 vis = VISIBILITY_ANON;
2228 else
2229 vis = DECL_VISIBILITY (arg);
2232 if (vis)
2233 constrain_visibility (decl, vis, true);
2237 /* Like c_determine_visibility, but with additional C++-specific
2238 behavior.
2240 Function-scope entities can rely on the function's visibility because
2241 it is set in start_preparsed_function.
2243 Class-scope entities cannot rely on the class's visibility until the end
2244 of the enclosing class definition.
2246 Note that because namespaces have multiple independent definitions,
2247 namespace visibility is handled elsewhere using the #pragma visibility
2248 machinery rather than by decorating the namespace declaration.
2250 The goal is for constraints from the type to give a diagnostic, and
2251 other constraints to be applied silently. */
2253 void
2254 determine_visibility (tree decl)
2256 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2258 /* Only relevant for names with external linkage. */
2259 if (!TREE_PUBLIC (decl))
2260 return;
2262 /* Cloned constructors and destructors get the same visibility as
2263 the underlying function. That should be set up in
2264 maybe_clone_body. */
2265 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2267 bool orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2268 enum symbol_visibility orig_visibility = DECL_VISIBILITY (decl);
2270 /* The decl may be a template instantiation, which could influence
2271 visibilty. */
2272 tree template_decl = NULL_TREE;
2273 if (TREE_CODE (decl) == TYPE_DECL)
2275 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2277 if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
2278 template_decl = decl;
2280 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2281 template_decl = decl;
2283 else if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
2284 template_decl = decl;
2286 /* If DECL is a member of a class, visibility specifiers on the
2287 class can influence the visibility of the DECL. */
2288 tree class_type = NULL_TREE;
2289 if (DECL_CLASS_SCOPE_P (decl))
2290 class_type = DECL_CONTEXT (decl);
2291 else
2293 /* Not a class member. */
2295 /* Virtual tables have DECL_CONTEXT set to their associated class,
2296 so they are automatically handled above. */
2297 gcc_assert (!VAR_P (decl)
2298 || !DECL_VTABLE_OR_VTT_P (decl));
2300 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2302 /* Local statics and classes get the visibility of their
2303 containing function by default, except that
2304 -fvisibility-inlines-hidden doesn't affect them. */
2305 tree fn = DECL_CONTEXT (decl);
2306 if (DECL_VISIBILITY_SPECIFIED (fn))
2308 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2309 DECL_VISIBILITY_SPECIFIED (decl) =
2310 DECL_VISIBILITY_SPECIFIED (fn);
2312 else
2314 if (DECL_CLASS_SCOPE_P (fn))
2315 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2316 else if (determine_hidden_inline (fn))
2318 DECL_VISIBILITY (decl) = default_visibility;
2319 DECL_VISIBILITY_SPECIFIED (decl) =
2320 visibility_options.inpragma;
2322 else
2324 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2325 DECL_VISIBILITY_SPECIFIED (decl) =
2326 DECL_VISIBILITY_SPECIFIED (fn);
2330 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2331 but have no TEMPLATE_INFO. Their containing template
2332 function does, and the local class could be constrained
2333 by that. */
2334 if (template_decl)
2335 template_decl = fn;
2337 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2338 && flag_visibility_ms_compat)
2340 /* Under -fvisibility-ms-compat, types are visible by default,
2341 even though their contents aren't. */
2342 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2343 int underlying_vis = type_visibility (underlying_type);
2344 if (underlying_vis == VISIBILITY_ANON
2345 || (CLASS_TYPE_P (underlying_type)
2346 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2347 constrain_visibility (decl, underlying_vis, false);
2348 else
2349 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2351 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2353 /* tinfo visibility is based on the type it's for. */
2354 constrain_visibility
2355 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2357 /* Give the target a chance to override the visibility associated
2358 with DECL. */
2359 if (TREE_PUBLIC (decl)
2360 && !DECL_REALLY_EXTERN (decl)
2361 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2362 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2363 targetm.cxx.determine_class_data_visibility (decl);
2365 else if (template_decl)
2366 /* Template instantiations and specializations get visibility based
2367 on their template unless they override it with an attribute. */;
2368 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2370 if (determine_hidden_inline (decl))
2371 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2372 else
2374 /* Set default visibility to whatever the user supplied with
2375 #pragma GCC visibility or a namespace visibility attribute. */
2376 DECL_VISIBILITY (decl) = default_visibility;
2377 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2382 if (template_decl)
2384 /* If the specialization doesn't specify visibility, use the
2385 visibility from the template. */
2386 tree tinfo = get_template_info (template_decl);
2387 tree args = TI_ARGS (tinfo);
2388 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2389 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2390 : DECL_ATTRIBUTES (decl));
2392 if (args != error_mark_node)
2394 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2396 if (!DECL_VISIBILITY_SPECIFIED (decl))
2398 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2399 && determine_hidden_inline (decl))
2400 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2401 else
2403 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2404 DECL_VISIBILITY_SPECIFIED (decl)
2405 = DECL_VISIBILITY_SPECIFIED (pattern);
2409 if (args
2410 /* Template argument visibility outweighs #pragma or namespace
2411 visibility, but not an explicit attribute. */
2412 && !lookup_attribute ("visibility", attribs))
2414 int depth = TMPL_ARGS_DEPTH (args);
2415 if (DECL_VISIBILITY_SPECIFIED (decl))
2417 /* A class template member with explicit visibility
2418 overrides the class visibility, so we need to apply
2419 all the levels of template args directly. */
2420 int i;
2421 for (i = 1; i <= depth; ++i)
2423 tree lev = TMPL_ARGS_LEVEL (args, i);
2424 constrain_visibility_for_template (decl, lev);
2427 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2428 /* Limit visibility based on its template arguments. */
2429 constrain_visibility_for_template (decl, args);
2434 if (class_type)
2435 determine_visibility_from_class (decl, class_type);
2437 if (decl_anon_ns_mem_p (decl))
2438 /* Names in an anonymous namespace get internal linkage.
2439 This might change once we implement export. */
2440 constrain_visibility (decl, VISIBILITY_ANON, false);
2441 else if (TREE_CODE (decl) != TYPE_DECL)
2443 /* Propagate anonymity from type to decl. */
2444 int tvis = type_visibility (TREE_TYPE (decl));
2445 if (tvis == VISIBILITY_ANON
2446 || ! DECL_VISIBILITY_SPECIFIED (decl))
2447 constrain_visibility (decl, tvis, false);
2449 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2450 /* DR 757: A type without linkage shall not be used as the type of a
2451 variable or function with linkage, unless
2452 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2453 o the variable or function is not used (3.2 [basic.def.odr]) or is
2454 defined in the same translation unit.
2456 Since non-extern "C" decls need to be defined in the same
2457 translation unit, we can make the type internal. */
2458 constrain_visibility (decl, VISIBILITY_ANON, false);
2460 /* If visibility changed and DECL already has DECL_RTL, ensure
2461 symbol flags are updated. */
2462 if ((DECL_VISIBILITY (decl) != orig_visibility
2463 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2464 && ((VAR_P (decl) && TREE_STATIC (decl))
2465 || TREE_CODE (decl) == FUNCTION_DECL)
2466 && DECL_RTL_SET_P (decl))
2467 make_decl_rtl (decl);
2470 /* By default, static data members and function members receive
2471 the visibility of their containing class. */
2473 static void
2474 determine_visibility_from_class (tree decl, tree class_type)
2476 if (DECL_VISIBILITY_SPECIFIED (decl))
2477 return;
2479 if (determine_hidden_inline (decl))
2480 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2481 else
2483 /* Default to the class visibility. */
2484 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2485 DECL_VISIBILITY_SPECIFIED (decl)
2486 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2489 /* Give the target a chance to override the visibility associated
2490 with DECL. */
2491 if (VAR_P (decl)
2492 && (DECL_TINFO_P (decl)
2493 || (DECL_VTABLE_OR_VTT_P (decl)
2494 /* Construction virtual tables are not exported because
2495 they cannot be referred to from other object files;
2496 their name is not standardized by the ABI. */
2497 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2498 && TREE_PUBLIC (decl)
2499 && !DECL_REALLY_EXTERN (decl)
2500 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2501 targetm.cxx.determine_class_data_visibility (decl);
2504 /* Returns true iff DECL is an inline that should get hidden visibility
2505 because of -fvisibility-inlines-hidden. */
2507 static bool
2508 determine_hidden_inline (tree decl)
2510 return (visibility_options.inlines_hidden
2511 /* Don't do this for inline templates; specializations might not be
2512 inline, and we don't want them to inherit the hidden
2513 visibility. We'll set it here for all inline instantiations. */
2514 && !processing_template_decl
2515 && TREE_CODE (decl) == FUNCTION_DECL
2516 && DECL_DECLARED_INLINE_P (decl)
2517 && (! DECL_LANG_SPECIFIC (decl)
2518 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2521 /* Constrain the visibility of a class TYPE based on the visibility of its
2522 field types. Warn if any fields require lesser visibility. */
2524 void
2525 constrain_class_visibility (tree type)
2527 tree binfo;
2528 tree t;
2529 int i;
2531 int vis = type_visibility (type);
2533 if (vis == VISIBILITY_ANON
2534 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2535 return;
2537 /* Don't warn about visibility if the class has explicit visibility. */
2538 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2539 vis = VISIBILITY_INTERNAL;
2541 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2542 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2544 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2545 int subvis = type_visibility (ftype);
2547 if (subvis == VISIBILITY_ANON)
2549 if (!in_main_input_context())
2551 tree nlt = no_linkage_check (ftype, /*relaxed_p=*/false);
2552 if (nlt)
2554 if (same_type_p (TREE_TYPE (t), nlt))
2555 warning (OPT_Wsubobject_linkage, "\
2556 %qT has a field %qD whose type has no linkage",
2557 type, t);
2558 else
2559 warning (OPT_Wsubobject_linkage, "\
2560 %qT has a field %qD whose type depends on the type %qT which has no linkage",
2561 type, t, nlt);
2563 else
2564 warning (OPT_Wsubobject_linkage, "\
2565 %qT has a field %qD whose type uses the anonymous namespace",
2566 type, t);
2569 else if (MAYBE_CLASS_TYPE_P (ftype)
2570 && vis < VISIBILITY_HIDDEN
2571 && subvis >= VISIBILITY_HIDDEN)
2572 warning (OPT_Wattributes, "\
2573 %qT declared with greater visibility than the type of its field %qD",
2574 type, t);
2577 binfo = TYPE_BINFO (type);
2578 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2580 int subvis = type_visibility (TREE_TYPE (t));
2582 if (subvis == VISIBILITY_ANON)
2584 if (!in_main_input_context())
2586 tree nlt = no_linkage_check (TREE_TYPE (t), /*relaxed_p=*/false);
2587 if (nlt)
2589 if (same_type_p (TREE_TYPE (t), nlt))
2590 warning (OPT_Wsubobject_linkage, "\
2591 %qT has a base %qT whose type has no linkage",
2592 type, TREE_TYPE (t));
2593 else
2594 warning (OPT_Wsubobject_linkage, "\
2595 %qT has a base %qT whose type depends on the type %qT which has no linkage",
2596 type, TREE_TYPE (t), nlt);
2598 else
2599 warning (OPT_Wsubobject_linkage, "\
2600 %qT has a base %qT whose type uses the anonymous namespace",
2601 type, TREE_TYPE (t));
2604 else if (vis < VISIBILITY_HIDDEN
2605 && subvis >= VISIBILITY_HIDDEN)
2606 warning (OPT_Wattributes, "\
2607 %qT declared with greater visibility than its base %qT",
2608 type, TREE_TYPE (t));
2612 /* Functions for adjusting the visibility of a tagged type and its nested
2613 types and declarations when it gets a name for linkage purposes from a
2614 typedef. */
2616 static void bt_reset_linkage_1 (binding_entry, void *);
2617 static void bt_reset_linkage_2 (binding_entry, void *);
2619 /* First reset the visibility of all the types. */
2621 static void
2622 reset_type_linkage_1 (tree type)
2624 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2625 if (CLASS_TYPE_P (type))
2626 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2627 bt_reset_linkage_1, NULL);
2629 static void
2630 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2632 reset_type_linkage_1 (b->type);
2635 /* Then reset the visibility of any static data members or member
2636 functions that use those types. */
2638 static void
2639 reset_decl_linkage (tree decl)
2641 if (TREE_PUBLIC (decl))
2642 return;
2643 if (DECL_CLONED_FUNCTION_P (decl))
2644 return;
2645 TREE_PUBLIC (decl) = true;
2646 DECL_INTERFACE_KNOWN (decl) = false;
2647 determine_visibility (decl);
2648 tentative_decl_linkage (decl);
2651 static void
2652 reset_type_linkage_2 (tree type)
2654 if (CLASS_TYPE_P (type))
2656 if (tree vt = CLASSTYPE_VTABLES (type))
2658 tree name = mangle_vtbl_for_type (type);
2659 DECL_NAME (vt) = name;
2660 SET_DECL_ASSEMBLER_NAME (vt, name);
2661 reset_decl_linkage (vt);
2663 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2665 tree name = mangle_typeinfo_for_type (type);
2666 DECL_NAME (ti) = name;
2667 SET_DECL_ASSEMBLER_NAME (ti, name);
2668 TREE_TYPE (name) = type;
2669 reset_decl_linkage (ti);
2671 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2673 tree mem = STRIP_TEMPLATE (m);
2674 if (TREE_CODE (mem) == VAR_DECL || TREE_CODE (mem) == FUNCTION_DECL)
2675 reset_decl_linkage (mem);
2677 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2678 bt_reset_linkage_2, NULL);
2682 static void
2683 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2685 reset_type_linkage_2 (b->type);
2687 void
2688 reset_type_linkage (tree type)
2690 reset_type_linkage_1 (type);
2691 reset_type_linkage_2 (type);
2694 /* Set up our initial idea of what the linkage of DECL should be. */
2696 void
2697 tentative_decl_linkage (tree decl)
2699 if (DECL_INTERFACE_KNOWN (decl))
2700 /* We've already made a decision as to how this function will
2701 be handled. */;
2702 else if (vague_linkage_p (decl))
2704 if (TREE_CODE (decl) == FUNCTION_DECL
2705 && decl_defined_p (decl))
2707 DECL_EXTERNAL (decl) = 1;
2708 DECL_NOT_REALLY_EXTERN (decl) = 1;
2709 note_vague_linkage_fn (decl);
2710 /* A non-template inline function with external linkage will
2711 always be COMDAT. As we must eventually determine the
2712 linkage of all functions, and as that causes writes to
2713 the data mapped in from the PCH file, it's advantageous
2714 to mark the functions at this point. */
2715 if (DECL_DECLARED_INLINE_P (decl)
2716 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2717 || DECL_DEFAULTED_FN (decl)))
2719 /* This function must have external linkage, as
2720 otherwise DECL_INTERFACE_KNOWN would have been
2721 set. */
2722 gcc_assert (TREE_PUBLIC (decl));
2723 comdat_linkage (decl);
2724 DECL_INTERFACE_KNOWN (decl) = 1;
2727 else if (VAR_P (decl))
2728 maybe_commonize_var (decl);
2732 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2733 for DECL has not already been determined, do so now by setting
2734 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2735 function is called entities with vague linkage whose definitions
2736 are available must have TREE_PUBLIC set.
2738 If this function decides to place DECL in COMDAT, it will set
2739 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2740 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2741 callers defer that decision until it is clear that DECL is actually
2742 required. */
2744 void
2745 import_export_decl (tree decl)
2747 int emit_p;
2748 bool comdat_p;
2749 bool import_p;
2750 tree class_type = NULL_TREE;
2752 if (DECL_INTERFACE_KNOWN (decl))
2753 return;
2755 /* We cannot determine what linkage to give to an entity with vague
2756 linkage until the end of the file. For example, a virtual table
2757 for a class will be defined if and only if the key method is
2758 defined in this translation unit. As a further example, consider
2759 that when compiling a translation unit that uses PCH file with
2760 "-frepo" it would be incorrect to make decisions about what
2761 entities to emit when building the PCH; those decisions must be
2762 delayed until the repository information has been processed. */
2763 gcc_assert (at_eof);
2764 /* Object file linkage for explicit instantiations is handled in
2765 mark_decl_instantiated. For static variables in functions with
2766 vague linkage, maybe_commonize_var is used.
2768 Therefore, the only declarations that should be provided to this
2769 function are those with external linkage that are:
2771 * implicit instantiations of function templates
2773 * inline function
2775 * implicit instantiations of static data members of class
2776 templates
2778 * virtual tables
2780 * typeinfo objects
2782 Furthermore, all entities that reach this point must have a
2783 definition available in this translation unit.
2785 The following assertions check these conditions. */
2786 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2787 /* Any code that creates entities with TREE_PUBLIC cleared should
2788 also set DECL_INTERFACE_KNOWN. */
2789 gcc_assert (TREE_PUBLIC (decl));
2790 if (TREE_CODE (decl) == FUNCTION_DECL)
2791 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2792 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2793 || DECL_DECLARED_INLINE_P (decl));
2794 else
2795 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2796 || DECL_VTABLE_OR_VTT_P (decl)
2797 || DECL_TINFO_P (decl));
2798 /* Check that a definition of DECL is available in this translation
2799 unit. */
2800 gcc_assert (!DECL_REALLY_EXTERN (decl));
2802 /* Assume that DECL will not have COMDAT linkage. */
2803 comdat_p = false;
2804 /* Assume that DECL will not be imported into this translation
2805 unit. */
2806 import_p = false;
2808 /* See if the repository tells us whether or not to emit DECL in
2809 this translation unit. */
2810 emit_p = repo_emit_p (decl);
2811 if (emit_p == 0)
2812 import_p = true;
2813 else if (emit_p == 1)
2815 /* The repository indicates that this entity should be defined
2816 here. Make sure the back end honors that request. */
2817 mark_needed (decl);
2818 /* Output the definition as an ordinary strong definition. */
2819 DECL_EXTERNAL (decl) = 0;
2820 DECL_INTERFACE_KNOWN (decl) = 1;
2821 return;
2824 if (import_p)
2825 /* We have already decided what to do with this DECL; there is no
2826 need to check anything further. */
2828 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2830 class_type = DECL_CONTEXT (decl);
2831 import_export_class (class_type);
2832 if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2833 && CLASSTYPE_INTERFACE_ONLY (class_type))
2834 import_p = true;
2835 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2836 && !CLASSTYPE_USE_TEMPLATE (class_type)
2837 && CLASSTYPE_KEY_METHOD (class_type)
2838 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2839 /* The ABI requires that all virtual tables be emitted with
2840 COMDAT linkage. However, on systems where COMDAT symbols
2841 don't show up in the table of contents for a static
2842 archive, or on systems without weak symbols (where we
2843 approximate COMDAT linkage by using internal linkage), the
2844 linker will report errors about undefined symbols because
2845 it will not see the virtual table definition. Therefore,
2846 in the case that we know that the virtual table will be
2847 emitted in only one translation unit, we make the virtual
2848 table an ordinary definition with external linkage. */
2849 DECL_EXTERNAL (decl) = 0;
2850 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2852 /* CLASS_TYPE is being exported from this translation unit,
2853 so DECL should be defined here. */
2854 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2855 /* If a class is declared in a header with the "extern
2856 template" extension, then it will not be instantiated,
2857 even in translation units that would normally require
2858 it. Often such classes are explicitly instantiated in
2859 one translation unit. Therefore, the explicit
2860 instantiation must be made visible to other translation
2861 units. */
2862 DECL_EXTERNAL (decl) = 0;
2863 else
2865 /* The generic C++ ABI says that class data is always
2866 COMDAT, even if there is a key function. Some
2867 variants (e.g., the ARM EABI) says that class data
2868 only has COMDAT linkage if the class data might be
2869 emitted in more than one translation unit. When the
2870 key method can be inline and is inline, we still have
2871 to arrange for comdat even though
2872 class_data_always_comdat is false. */
2873 if (!CLASSTYPE_KEY_METHOD (class_type)
2874 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2875 || targetm.cxx.class_data_always_comdat ())
2877 /* The ABI requires COMDAT linkage. Normally, we
2878 only emit COMDAT things when they are needed;
2879 make sure that we realize that this entity is
2880 indeed needed. */
2881 comdat_p = true;
2882 mark_needed (decl);
2886 else if (!flag_implicit_templates
2887 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2888 import_p = true;
2889 else
2890 comdat_p = true;
2892 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2894 tree type = TREE_TYPE (DECL_NAME (decl));
2895 if (CLASS_TYPE_P (type))
2897 class_type = type;
2898 import_export_class (type);
2899 if (CLASSTYPE_INTERFACE_KNOWN (type)
2900 && TYPE_POLYMORPHIC_P (type)
2901 && CLASSTYPE_INTERFACE_ONLY (type)
2902 /* If -fno-rtti was specified, then we cannot be sure
2903 that RTTI information will be emitted with the
2904 virtual table of the class, so we must emit it
2905 wherever it is used. */
2906 && flag_rtti)
2907 import_p = true;
2908 else
2910 if (CLASSTYPE_INTERFACE_KNOWN (type)
2911 && !CLASSTYPE_INTERFACE_ONLY (type))
2913 comdat_p = (targetm.cxx.class_data_always_comdat ()
2914 || (CLASSTYPE_KEY_METHOD (type)
2915 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2916 mark_needed (decl);
2917 if (!flag_weak)
2919 comdat_p = false;
2920 DECL_EXTERNAL (decl) = 0;
2923 else
2924 comdat_p = true;
2927 else
2928 comdat_p = true;
2930 else if (DECL_TEMPLOID_INSTANTIATION (decl))
2932 /* DECL is an implicit instantiation of a function or static
2933 data member. */
2934 if ((flag_implicit_templates
2935 && !flag_use_repository)
2936 || (flag_implicit_inline_templates
2937 && TREE_CODE (decl) == FUNCTION_DECL
2938 && DECL_DECLARED_INLINE_P (decl)))
2939 comdat_p = true;
2940 else
2941 /* If we are not implicitly generating templates, then mark
2942 this entity as undefined in this translation unit. */
2943 import_p = true;
2945 else if (DECL_FUNCTION_MEMBER_P (decl))
2947 if (!DECL_DECLARED_INLINE_P (decl))
2949 tree ctype = DECL_CONTEXT (decl);
2950 import_export_class (ctype);
2951 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2953 DECL_NOT_REALLY_EXTERN (decl)
2954 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2955 || (DECL_DECLARED_INLINE_P (decl)
2956 && ! flag_implement_inlines
2957 && !DECL_VINDEX (decl)));
2959 if (!DECL_NOT_REALLY_EXTERN (decl))
2960 DECL_EXTERNAL (decl) = 1;
2962 /* Always make artificials weak. */
2963 if (DECL_ARTIFICIAL (decl) && flag_weak)
2964 comdat_p = true;
2965 else
2966 maybe_make_one_only (decl);
2969 else
2970 comdat_p = true;
2972 else
2973 comdat_p = true;
2975 if (import_p)
2977 /* If we are importing DECL into this translation unit, mark is
2978 an undefined here. */
2979 DECL_EXTERNAL (decl) = 1;
2980 DECL_NOT_REALLY_EXTERN (decl) = 0;
2982 else if (comdat_p)
2984 /* If we decided to put DECL in COMDAT, mark it accordingly at
2985 this point. */
2986 comdat_linkage (decl);
2989 DECL_INTERFACE_KNOWN (decl) = 1;
2992 /* Return an expression that performs the destruction of DECL, which
2993 must be a VAR_DECL whose type has a non-trivial destructor, or is
2994 an array whose (innermost) elements have a non-trivial destructor. */
2996 tree
2997 build_cleanup (tree decl)
2999 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
3000 gcc_assert (clean != NULL_TREE);
3001 return clean;
3004 /* Returns the initialization guard variable for the variable DECL,
3005 which has static storage duration. */
3007 tree
3008 get_guard (tree decl)
3010 tree sname;
3011 tree guard;
3013 sname = mangle_guard_variable (decl);
3014 guard = get_global_binding (sname);
3015 if (! guard)
3017 tree guard_type;
3019 /* We use a type that is big enough to contain a mutex as well
3020 as an integer counter. */
3021 guard_type = targetm.cxx.guard_type ();
3022 guard = build_decl (DECL_SOURCE_LOCATION (decl),
3023 VAR_DECL, sname, guard_type);
3025 /* The guard should have the same linkage as what it guards. */
3026 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
3027 TREE_STATIC (guard) = TREE_STATIC (decl);
3028 DECL_COMMON (guard) = DECL_COMMON (decl);
3029 DECL_COMDAT (guard) = DECL_COMDAT (decl);
3030 CP_DECL_THREAD_LOCAL_P (guard) = CP_DECL_THREAD_LOCAL_P (decl);
3031 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
3032 if (DECL_ONE_ONLY (decl))
3033 make_decl_one_only (guard, cxx_comdat_group (guard));
3034 if (TREE_PUBLIC (decl))
3035 DECL_WEAK (guard) = DECL_WEAK (decl);
3036 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
3037 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
3039 DECL_ARTIFICIAL (guard) = 1;
3040 DECL_IGNORED_P (guard) = 1;
3041 TREE_USED (guard) = 1;
3042 pushdecl_top_level_and_finish (guard, NULL_TREE);
3044 return guard;
3047 /* Return an atomic load of src with the appropriate memory model. */
3049 static tree
3050 build_atomic_load_byte (tree src, HOST_WIDE_INT model)
3052 tree ptr_type = build_pointer_type (char_type_node);
3053 tree mem_model = build_int_cst (integer_type_node, model);
3054 tree t, addr, val;
3055 unsigned int size;
3056 int fncode;
3058 size = tree_to_uhwi (TYPE_SIZE_UNIT (char_type_node));
3060 fncode = BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1;
3061 t = builtin_decl_implicit ((enum built_in_function) fncode);
3063 addr = build1 (ADDR_EXPR, ptr_type, src);
3064 val = build_call_expr (t, 2, addr, mem_model);
3065 return val;
3068 /* Return those bits of the GUARD variable that should be set when the
3069 guarded entity is actually initialized. */
3071 static tree
3072 get_guard_bits (tree guard)
3074 if (!targetm.cxx.guard_mask_bit ())
3076 /* We only set the first byte of the guard, in order to leave room
3077 for a mutex in the high-order bits. */
3078 guard = build1 (ADDR_EXPR,
3079 build_pointer_type (TREE_TYPE (guard)),
3080 guard);
3081 guard = build1 (NOP_EXPR,
3082 build_pointer_type (char_type_node),
3083 guard);
3084 guard = build1 (INDIRECT_REF, char_type_node, guard);
3087 return guard;
3090 /* Return an expression which determines whether or not the GUARD
3091 variable has already been initialized. */
3093 tree
3094 get_guard_cond (tree guard, bool thread_safe)
3096 tree guard_value;
3098 if (!thread_safe)
3099 guard = get_guard_bits (guard);
3100 else
3101 guard = build_atomic_load_byte (guard, MEMMODEL_ACQUIRE);
3103 /* Mask off all but the low bit. */
3104 if (targetm.cxx.guard_mask_bit ())
3106 guard_value = integer_one_node;
3107 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3108 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3109 guard = cp_build_binary_op (input_location,
3110 BIT_AND_EXPR, guard, guard_value,
3111 tf_warning_or_error);
3114 guard_value = integer_zero_node;
3115 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3116 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3117 return cp_build_binary_op (input_location,
3118 EQ_EXPR, guard, guard_value,
3119 tf_warning_or_error);
3122 /* Return an expression which sets the GUARD variable, indicating that
3123 the variable being guarded has been initialized. */
3125 tree
3126 set_guard (tree guard)
3128 tree guard_init;
3130 /* Set the GUARD to one. */
3131 guard = get_guard_bits (guard);
3132 guard_init = integer_one_node;
3133 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3134 guard_init = fold_convert (TREE_TYPE (guard), guard_init);
3135 return cp_build_modify_expr (input_location, guard, NOP_EXPR, guard_init,
3136 tf_warning_or_error);
3139 /* Returns true iff we can tell that VAR does not have a dynamic
3140 initializer. */
3142 static bool
3143 var_defined_without_dynamic_init (tree var)
3145 /* If it's defined in another TU, we can't tell. */
3146 if (DECL_EXTERNAL (var))
3147 return false;
3148 /* If it has a non-trivial destructor, registering the destructor
3149 counts as dynamic initialization. */
3150 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3151 return false;
3152 /* If it's in this TU, its initializer has been processed, unless
3153 it's a case of self-initialization, then DECL_INITIALIZED_P is
3154 false while the initializer is handled by finish_id_expression. */
3155 if (!DECL_INITIALIZED_P (var))
3156 return false;
3157 /* If it has no initializer or a constant one, it's not dynamic. */
3158 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3159 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3162 /* Returns true iff VAR is a variable that needs uses to be
3163 wrapped for possible dynamic initialization. */
3165 static bool
3166 var_needs_tls_wrapper (tree var)
3168 return (!error_operand_p (var)
3169 && CP_DECL_THREAD_LOCAL_P (var)
3170 && !DECL_GNU_TLS_P (var)
3171 && !DECL_FUNCTION_SCOPE_P (var)
3172 && !var_defined_without_dynamic_init (var));
3175 /* Get the FUNCTION_DECL for the shared TLS init function for this
3176 translation unit. */
3178 static tree
3179 get_local_tls_init_fn (void)
3181 tree sname = get_identifier ("__tls_init");
3182 tree fn = get_global_binding (sname);
3183 if (!fn)
3185 fn = build_lang_decl (FUNCTION_DECL, sname,
3186 build_function_type (void_type_node,
3187 void_list_node));
3188 SET_DECL_LANGUAGE (fn, lang_c);
3189 TREE_PUBLIC (fn) = false;
3190 DECL_ARTIFICIAL (fn) = true;
3191 mark_used (fn);
3192 set_global_binding (fn);
3194 return fn;
3197 /* Get a FUNCTION_DECL for the init function for the thread_local
3198 variable VAR. The init function will be an alias to the function
3199 that initializes all the non-local TLS variables in the translation
3200 unit. The init function is only used by the wrapper function. */
3202 static tree
3203 get_tls_init_fn (tree var)
3205 /* Only C++11 TLS vars need this init fn. */
3206 if (!var_needs_tls_wrapper (var))
3207 return NULL_TREE;
3209 /* If -fno-extern-tls-init, assume that we don't need to call
3210 a tls init function for a variable defined in another TU. */
3211 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3212 return NULL_TREE;
3214 /* If the variable is internal, or if we can't generate aliases,
3215 call the local init function directly. */
3216 if (!TREE_PUBLIC (var) || !TARGET_SUPPORTS_ALIASES)
3217 return get_local_tls_init_fn ();
3219 tree sname = mangle_tls_init_fn (var);
3220 tree fn = get_global_binding (sname);
3221 if (!fn)
3223 fn = build_lang_decl (FUNCTION_DECL, sname,
3224 build_function_type (void_type_node,
3225 void_list_node));
3226 SET_DECL_LANGUAGE (fn, lang_c);
3227 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3228 DECL_ARTIFICIAL (fn) = true;
3229 DECL_COMDAT (fn) = DECL_COMDAT (var);
3230 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3231 if (DECL_ONE_ONLY (var))
3232 make_decl_one_only (fn, cxx_comdat_group (fn));
3233 if (TREE_PUBLIC (var))
3235 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3236 /* If the variable is defined somewhere else and might have static
3237 initialization, make the init function a weak reference. */
3238 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3239 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3240 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3241 && DECL_EXTERNAL (var))
3242 declare_weak (fn);
3243 else
3244 DECL_WEAK (fn) = DECL_WEAK (var);
3246 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3247 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3248 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3249 DECL_IGNORED_P (fn) = 1;
3250 mark_used (fn);
3252 DECL_BEFRIENDING_CLASSES (fn) = var;
3254 set_global_binding (fn);
3256 return fn;
3259 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3260 variable VAR. The wrapper function calls the init function (if any) for
3261 VAR and then returns a reference to VAR. The wrapper function is used
3262 in place of VAR everywhere VAR is mentioned. */
3264 tree
3265 get_tls_wrapper_fn (tree var)
3267 /* Only C++11 TLS vars need this wrapper fn. */
3268 if (!var_needs_tls_wrapper (var))
3269 return NULL_TREE;
3271 tree sname = mangle_tls_wrapper_fn (var);
3272 tree fn = get_global_binding (sname);
3273 if (!fn)
3275 /* A named rvalue reference is an lvalue, so the wrapper should
3276 always return an lvalue reference. */
3277 tree type = non_reference (TREE_TYPE (var));
3278 type = build_reference_type (type);
3279 tree fntype = build_function_type (type, void_list_node);
3280 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3281 SET_DECL_LANGUAGE (fn, lang_c);
3282 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3283 DECL_ARTIFICIAL (fn) = true;
3284 DECL_IGNORED_P (fn) = 1;
3285 /* The wrapper is inline and emitted everywhere var is used. */
3286 DECL_DECLARED_INLINE_P (fn) = true;
3287 if (TREE_PUBLIC (var))
3289 comdat_linkage (fn);
3290 #ifdef HAVE_GAS_HIDDEN
3291 /* Make the wrapper bind locally; there's no reason to share
3292 the wrapper between multiple shared objects. */
3293 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3294 DECL_VISIBILITY_SPECIFIED (fn) = true;
3295 #endif
3297 if (!TREE_PUBLIC (fn))
3298 DECL_INTERFACE_KNOWN (fn) = true;
3299 mark_used (fn);
3300 note_vague_linkage_fn (fn);
3302 #if 0
3303 /* We want CSE to commonize calls to the wrapper, but marking it as
3304 pure is unsafe since it has side-effects. I guess we need a new
3305 ECF flag even weaker than ECF_PURE. FIXME! */
3306 DECL_PURE_P (fn) = true;
3307 #endif
3309 DECL_BEFRIENDING_CLASSES (fn) = var;
3311 set_global_binding (fn);
3313 return fn;
3316 /* At EOF, generate the definition for the TLS wrapper function FN:
3318 T& var_wrapper() {
3319 if (init_fn) init_fn();
3320 return var;
3321 } */
3323 static void
3324 generate_tls_wrapper (tree fn)
3326 tree var = DECL_BEFRIENDING_CLASSES (fn);
3328 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3329 tree body = begin_function_body ();
3330 /* Only call the init fn if there might be one. */
3331 if (tree init_fn = get_tls_init_fn (var))
3333 tree if_stmt = NULL_TREE;
3334 /* If init_fn is a weakref, make sure it exists before calling. */
3335 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3337 if_stmt = begin_if_stmt ();
3338 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3339 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3340 NE_EXPR, addr, nullptr_node,
3341 tf_warning_or_error);
3342 finish_if_stmt_cond (cond, if_stmt);
3344 finish_expr_stmt (build_cxx_call
3345 (init_fn, 0, NULL, tf_warning_or_error));
3346 if (if_stmt)
3348 finish_then_clause (if_stmt);
3349 finish_if_stmt (if_stmt);
3352 else
3353 /* If there's no initialization, the wrapper is a constant function. */
3354 TREE_READONLY (fn) = true;
3355 finish_return_stmt (convert_from_reference (var));
3356 finish_function_body (body);
3357 expand_or_defer_fn (finish_function (/*inline_p=*/false));
3360 /* Start the process of running a particular set of global constructors
3361 or destructors. Subroutine of do_[cd]tors. Also called from
3362 vtv_start_verification_constructor_init_function. */
3364 static tree
3365 start_objects (int method_type, int initp)
3367 tree body;
3368 tree fndecl;
3369 char type[14];
3371 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3373 if (initp != DEFAULT_INIT_PRIORITY)
3375 char joiner;
3377 #ifdef JOINER
3378 joiner = JOINER;
3379 #else
3380 joiner = '_';
3381 #endif
3383 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3385 else
3386 sprintf (type, "sub_%c", method_type);
3388 fndecl = build_lang_decl (FUNCTION_DECL,
3389 get_file_function_name (type),
3390 build_function_type_list (void_type_node,
3391 NULL_TREE));
3392 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3394 TREE_PUBLIC (current_function_decl) = 0;
3396 /* Mark as artificial because it's not explicitly in the user's
3397 source code. */
3398 DECL_ARTIFICIAL (current_function_decl) = 1;
3400 /* Mark this declaration as used to avoid spurious warnings. */
3401 TREE_USED (current_function_decl) = 1;
3403 /* Mark this function as a global constructor or destructor. */
3404 if (method_type == 'I')
3405 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3406 else
3407 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3409 body = begin_compound_stmt (BCS_FN_BODY);
3411 return body;
3414 /* Finish the process of running a particular set of global constructors
3415 or destructors. Subroutine of do_[cd]tors. */
3417 static void
3418 finish_objects (int method_type, int initp, tree body)
3420 tree fn;
3422 /* Finish up. */
3423 finish_compound_stmt (body);
3424 fn = finish_function (/*inline_p=*/false);
3426 if (method_type == 'I')
3428 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3429 decl_init_priority_insert (fn, initp);
3431 else
3433 DECL_STATIC_DESTRUCTOR (fn) = 1;
3434 decl_fini_priority_insert (fn, initp);
3437 expand_or_defer_fn (fn);
3440 /* The names of the parameters to the function created to handle
3441 initializations and destructions for objects with static storage
3442 duration. */
3443 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3444 #define PRIORITY_IDENTIFIER "__priority"
3446 /* The name of the function we create to handle initializations and
3447 destructions for objects with static storage duration. */
3448 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3450 /* The declaration for the __INITIALIZE_P argument. */
3451 static GTY(()) tree initialize_p_decl;
3453 /* The declaration for the __PRIORITY argument. */
3454 static GTY(()) tree priority_decl;
3456 /* The declaration for the static storage duration function. */
3457 static GTY(()) tree ssdf_decl;
3459 /* All the static storage duration functions created in this
3460 translation unit. */
3461 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3463 /* A map from priority levels to information about that priority
3464 level. There may be many such levels, so efficient lookup is
3465 important. */
3466 static splay_tree priority_info_map;
3468 /* Begins the generation of the function that will handle all
3469 initialization and destruction of objects with static storage
3470 duration. The function generated takes two parameters of type
3471 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3472 nonzero, it performs initializations. Otherwise, it performs
3473 destructions. It only performs those initializations or
3474 destructions with the indicated __PRIORITY. The generated function
3475 returns no value.
3477 It is assumed that this function will only be called once per
3478 translation unit. */
3480 static tree
3481 start_static_storage_duration_function (unsigned count)
3483 tree type;
3484 tree body;
3485 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3487 /* Create the identifier for this function. It will be of the form
3488 SSDF_IDENTIFIER_<number>. */
3489 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3491 type = build_function_type_list (void_type_node,
3492 integer_type_node, integer_type_node,
3493 NULL_TREE);
3495 /* Create the FUNCTION_DECL itself. */
3496 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3497 get_identifier (id),
3498 type);
3499 TREE_PUBLIC (ssdf_decl) = 0;
3500 DECL_ARTIFICIAL (ssdf_decl) = 1;
3502 /* Put this function in the list of functions to be called from the
3503 static constructors and destructors. */
3504 if (!ssdf_decls)
3506 vec_alloc (ssdf_decls, 32);
3508 /* Take this opportunity to initialize the map from priority
3509 numbers to information about that priority level. */
3510 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3511 /*delete_key_fn=*/0,
3512 /*delete_value_fn=*/
3513 (splay_tree_delete_value_fn) &free);
3515 /* We always need to generate functions for the
3516 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3517 priorities later, we'll be sure to find the
3518 DEFAULT_INIT_PRIORITY. */
3519 get_priority_info (DEFAULT_INIT_PRIORITY);
3522 vec_safe_push (ssdf_decls, ssdf_decl);
3524 /* Create the argument list. */
3525 initialize_p_decl = cp_build_parm_decl
3526 (ssdf_decl, get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3527 TREE_USED (initialize_p_decl) = 1;
3528 priority_decl = cp_build_parm_decl
3529 (ssdf_decl, get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3530 TREE_USED (priority_decl) = 1;
3532 DECL_CHAIN (initialize_p_decl) = priority_decl;
3533 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3535 /* Put the function in the global scope. */
3536 pushdecl (ssdf_decl);
3538 /* Start the function itself. This is equivalent to declaring the
3539 function as:
3541 static void __ssdf (int __initialize_p, init __priority_p);
3543 It is static because we only need to call this function from the
3544 various constructor and destructor functions for this module. */
3545 start_preparsed_function (ssdf_decl,
3546 /*attrs=*/NULL_TREE,
3547 SF_PRE_PARSED);
3549 /* Set up the scope of the outermost block in the function. */
3550 body = begin_compound_stmt (BCS_FN_BODY);
3552 return body;
3555 /* Finish the generation of the function which performs initialization
3556 and destruction of objects with static storage duration. After
3557 this point, no more such objects can be created. */
3559 static void
3560 finish_static_storage_duration_function (tree body)
3562 /* Close out the function. */
3563 finish_compound_stmt (body);
3564 expand_or_defer_fn (finish_function (/*inline_p=*/false));
3567 /* Return the information about the indicated PRIORITY level. If no
3568 code to handle this level has yet been generated, generate the
3569 appropriate prologue. */
3571 static priority_info
3572 get_priority_info (int priority)
3574 priority_info pi;
3575 splay_tree_node n;
3577 n = splay_tree_lookup (priority_info_map,
3578 (splay_tree_key) priority);
3579 if (!n)
3581 /* Create a new priority information structure, and insert it
3582 into the map. */
3583 pi = XNEW (struct priority_info_s);
3584 pi->initializations_p = 0;
3585 pi->destructions_p = 0;
3586 splay_tree_insert (priority_info_map,
3587 (splay_tree_key) priority,
3588 (splay_tree_value) pi);
3590 else
3591 pi = (priority_info) n->value;
3593 return pi;
3596 /* The effective initialization priority of a DECL. */
3598 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3599 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3600 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3602 /* Whether a DECL needs a guard to protect it against multiple
3603 initialization. */
3605 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3606 || DECL_ONE_ONLY (decl) \
3607 || DECL_WEAK (decl)))
3609 /* Called from one_static_initialization_or_destruction(),
3610 via walk_tree.
3611 Walks the initializer list of a global variable and looks for
3612 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3613 and that have their DECL_CONTEXT() == NULL.
3614 For each such temporary variable, set their DECL_CONTEXT() to
3615 the current function. This is necessary because otherwise
3616 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3617 when trying to refer to a temporary variable that does not have
3618 it's DECL_CONTECT() properly set. */
3619 static tree
3620 fix_temporary_vars_context_r (tree *node,
3621 int * /*unused*/,
3622 void * /*unused1*/)
3624 gcc_assert (current_function_decl);
3626 if (TREE_CODE (*node) == BIND_EXPR)
3628 tree var;
3630 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3631 if (VAR_P (var)
3632 && !DECL_NAME (var)
3633 && DECL_ARTIFICIAL (var)
3634 && !DECL_CONTEXT (var))
3635 DECL_CONTEXT (var) = current_function_decl;
3638 return NULL_TREE;
3641 /* Set up to handle the initialization or destruction of DECL. If
3642 INITP is nonzero, we are initializing the variable. Otherwise, we
3643 are destroying it. */
3645 static void
3646 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3648 tree guard_if_stmt = NULL_TREE;
3649 tree guard;
3651 /* If we are supposed to destruct and there's a trivial destructor,
3652 nothing has to be done. */
3653 if (!initp
3654 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3655 return;
3657 /* Trick the compiler into thinking we are at the file and line
3658 where DECL was declared so that error-messages make sense, and so
3659 that the debugger will show somewhat sensible file and line
3660 information. */
3661 input_location = DECL_SOURCE_LOCATION (decl);
3663 /* Make sure temporary variables in the initialiser all have
3664 their DECL_CONTEXT() set to a value different from NULL_TREE.
3665 This can happen when global variables initializers are built.
3666 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3667 the temporary variables that might have been generated in the
3668 accompanying initializers is NULL_TREE, meaning the variables have been
3669 declared in the global namespace.
3670 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3671 of the temporaries are set to the current function decl. */
3672 cp_walk_tree_without_duplicates (&init,
3673 fix_temporary_vars_context_r,
3674 NULL);
3676 /* Because of:
3678 [class.access.spec]
3680 Access control for implicit calls to the constructors,
3681 the conversion functions, or the destructor called to
3682 create and destroy a static data member is performed as
3683 if these calls appeared in the scope of the member's
3684 class.
3686 we pretend we are in a static member function of the class of
3687 which the DECL is a member. */
3688 if (member_p (decl))
3690 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3691 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3694 /* Assume we don't need a guard. */
3695 guard = NULL_TREE;
3696 /* We need a guard if this is an object with external linkage that
3697 might be initialized in more than one place. (For example, a
3698 static data member of a template, when the data member requires
3699 construction.) */
3700 if (NEEDS_GUARD_P (decl))
3702 tree guard_cond;
3704 guard = get_guard (decl);
3706 /* When using __cxa_atexit, we just check the GUARD as we would
3707 for a local static. */
3708 if (flag_use_cxa_atexit)
3710 /* When using __cxa_atexit, we never try to destroy
3711 anything from a static destructor. */
3712 gcc_assert (initp);
3713 guard_cond = get_guard_cond (guard, false);
3715 /* If we don't have __cxa_atexit, then we will be running
3716 destructors from .fini sections, or their equivalents. So,
3717 we need to know how many times we've tried to initialize this
3718 object. We do initializations only if the GUARD is zero,
3719 i.e., if we are the first to initialize the variable. We do
3720 destructions only if the GUARD is one, i.e., if we are the
3721 last to destroy the variable. */
3722 else if (initp)
3723 guard_cond
3724 = cp_build_binary_op (input_location,
3725 EQ_EXPR,
3726 cp_build_unary_op (PREINCREMENT_EXPR,
3727 guard,
3728 /*noconvert=*/true,
3729 tf_warning_or_error),
3730 integer_one_node,
3731 tf_warning_or_error);
3732 else
3733 guard_cond
3734 = cp_build_binary_op (input_location,
3735 EQ_EXPR,
3736 cp_build_unary_op (PREDECREMENT_EXPR,
3737 guard,
3738 /*noconvert=*/true,
3739 tf_warning_or_error),
3740 integer_zero_node,
3741 tf_warning_or_error);
3743 guard_if_stmt = begin_if_stmt ();
3744 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3748 /* If we're using __cxa_atexit, we have not already set the GUARD,
3749 so we must do so now. */
3750 if (guard && initp && flag_use_cxa_atexit)
3751 finish_expr_stmt (set_guard (guard));
3753 /* Perform the initialization or destruction. */
3754 if (initp)
3756 if (init)
3758 finish_expr_stmt (init);
3759 if (sanitize_flags_p (SANITIZE_ADDRESS, decl))
3761 varpool_node *vnode = varpool_node::get (decl);
3762 if (vnode)
3763 vnode->dynamically_initialized = 1;
3767 /* If we're using __cxa_atexit, register a function that calls the
3768 destructor for the object. */
3769 if (flag_use_cxa_atexit)
3770 finish_expr_stmt (register_dtor_fn (decl));
3772 else
3773 finish_expr_stmt (build_cleanup (decl));
3775 /* Finish the guard if-stmt, if necessary. */
3776 if (guard)
3778 finish_then_clause (guard_if_stmt);
3779 finish_if_stmt (guard_if_stmt);
3782 /* Now that we're done with DECL we don't need to pretend to be a
3783 member of its class any longer. */
3784 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3785 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3788 /* Generate code to do the initialization or destruction of the decls in VARS,
3789 a TREE_LIST of VAR_DECL with static storage duration.
3790 Whether initialization or destruction is performed is specified by INITP. */
3792 static void
3793 do_static_initialization_or_destruction (tree vars, bool initp)
3795 tree node, init_if_stmt, cond;
3797 /* Build the outer if-stmt to check for initialization or destruction. */
3798 init_if_stmt = begin_if_stmt ();
3799 cond = initp ? integer_one_node : integer_zero_node;
3800 cond = cp_build_binary_op (input_location,
3801 EQ_EXPR,
3802 initialize_p_decl,
3803 cond,
3804 tf_warning_or_error);
3805 finish_if_stmt_cond (cond, init_if_stmt);
3807 /* To make sure dynamic construction doesn't access globals from other
3808 compilation units where they might not be yet constructed, for
3809 -fsanitize=address insert __asan_before_dynamic_init call that
3810 prevents access to either all global variables that need construction
3811 in other compilation units, or at least those that haven't been
3812 initialized yet. Variables that need dynamic construction in
3813 the current compilation unit are kept accessible. */
3814 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
3815 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3817 node = vars;
3818 do {
3819 tree decl = TREE_VALUE (node);
3820 tree priority_if_stmt;
3821 int priority;
3822 priority_info pi;
3824 /* If we don't need a destructor, there's nothing to do. Avoid
3825 creating a possibly empty if-stmt. */
3826 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3828 node = TREE_CHAIN (node);
3829 continue;
3832 /* Remember that we had an initialization or finalization at this
3833 priority. */
3834 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3835 pi = get_priority_info (priority);
3836 if (initp)
3837 pi->initializations_p = 1;
3838 else
3839 pi->destructions_p = 1;
3841 /* Conditionalize this initialization on being in the right priority
3842 and being initializing/finalizing appropriately. */
3843 priority_if_stmt = begin_if_stmt ();
3844 cond = cp_build_binary_op (input_location,
3845 EQ_EXPR,
3846 priority_decl,
3847 build_int_cst (NULL_TREE, priority),
3848 tf_warning_or_error);
3849 finish_if_stmt_cond (cond, priority_if_stmt);
3851 /* Process initializers with same priority. */
3852 for (; node
3853 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3854 node = TREE_CHAIN (node))
3855 /* Do one initialization or destruction. */
3856 one_static_initialization_or_destruction (TREE_VALUE (node),
3857 TREE_PURPOSE (node), initp);
3859 /* Finish up the priority if-stmt body. */
3860 finish_then_clause (priority_if_stmt);
3861 finish_if_stmt (priority_if_stmt);
3863 } while (node);
3865 /* Revert what __asan_before_dynamic_init did by calling
3866 __asan_after_dynamic_init. */
3867 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
3868 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3870 /* Finish up the init/destruct if-stmt body. */
3871 finish_then_clause (init_if_stmt);
3872 finish_if_stmt (init_if_stmt);
3875 /* VARS is a list of variables with static storage duration which may
3876 need initialization and/or finalization. Remove those variables
3877 that don't really need to be initialized or finalized, and return
3878 the resulting list. The order in which the variables appear in
3879 VARS is in reverse order of the order in which they should actually
3880 be initialized. The list we return is in the unreversed order;
3881 i.e., the first variable should be initialized first. */
3883 static tree
3884 prune_vars_needing_no_initialization (tree *vars)
3886 tree *var = vars;
3887 tree result = NULL_TREE;
3889 while (*var)
3891 tree t = *var;
3892 tree decl = TREE_VALUE (t);
3893 tree init = TREE_PURPOSE (t);
3895 /* Deal gracefully with error. */
3896 if (error_operand_p (decl))
3898 var = &TREE_CHAIN (t);
3899 continue;
3902 /* The only things that can be initialized are variables. */
3903 gcc_assert (VAR_P (decl));
3905 /* If this object is not defined, we don't need to do anything
3906 here. */
3907 if (DECL_EXTERNAL (decl))
3909 var = &TREE_CHAIN (t);
3910 continue;
3913 /* Also, if the initializer already contains errors, we can bail
3914 out now. */
3915 if (init && TREE_CODE (init) == TREE_LIST
3916 && value_member (error_mark_node, init))
3918 var = &TREE_CHAIN (t);
3919 continue;
3922 /* This variable is going to need initialization and/or
3923 finalization, so we add it to the list. */
3924 *var = TREE_CHAIN (t);
3925 TREE_CHAIN (t) = result;
3926 result = t;
3929 return result;
3932 /* Make sure we have told the back end about all the variables in
3933 VARS. */
3935 static void
3936 write_out_vars (tree vars)
3938 tree v;
3940 for (v = vars; v; v = TREE_CHAIN (v))
3942 tree var = TREE_VALUE (v);
3943 if (!var_finalized_p (var))
3945 import_export_decl (var);
3946 rest_of_decl_compilation (var, 1, 1);
3951 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3952 (otherwise) that will initialize all global objects with static
3953 storage duration having the indicated PRIORITY. */
3955 static void
3956 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3957 location_t *locus)
3959 char function_key;
3960 tree fndecl;
3961 tree body;
3962 size_t i;
3964 input_location = *locus;
3965 /* ??? */
3966 /* Was: locus->line++; */
3968 /* We use `I' to indicate initialization and `D' to indicate
3969 destruction. */
3970 function_key = constructor_p ? 'I' : 'D';
3972 /* We emit the function lazily, to avoid generating empty
3973 global constructors and destructors. */
3974 body = NULL_TREE;
3976 /* For Objective-C++, we may need to initialize metadata found in this module.
3977 This must be done _before_ any other static initializations. */
3978 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3979 && constructor_p && objc_static_init_needed_p ())
3981 body = start_objects (function_key, priority);
3982 objc_generate_static_init_call (NULL_TREE);
3985 /* Call the static storage duration function with appropriate
3986 arguments. */
3987 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
3989 /* Calls to pure or const functions will expand to nothing. */
3990 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3992 tree call;
3994 if (! body)
3995 body = start_objects (function_key, priority);
3997 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
3998 build_int_cst (NULL_TREE,
3999 constructor_p),
4000 build_int_cst (NULL_TREE,
4001 priority),
4002 NULL_TREE);
4003 finish_expr_stmt (call);
4007 /* Close out the function. */
4008 if (body)
4009 finish_objects (function_key, priority, body);
4012 /* Generate constructor and destructor functions for the priority
4013 indicated by N. */
4015 static int
4016 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
4018 location_t *locus = (location_t *) data;
4019 int priority = (int) n->key;
4020 priority_info pi = (priority_info) n->value;
4022 /* Generate the functions themselves, but only if they are really
4023 needed. */
4024 if (pi->initializations_p)
4025 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
4026 if (pi->destructions_p)
4027 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
4029 /* Keep iterating. */
4030 return 0;
4033 /* Return C++ property of T, based on given operation OP. */
4035 static int
4036 cpp_check (tree t, cpp_operation op)
4038 switch (op)
4040 case HAS_DEPENDENT_TEMPLATE_ARGS:
4042 tree ti = CLASSTYPE_TEMPLATE_INFO (t);
4043 if (!ti)
4044 return 0;
4045 ++processing_template_decl;
4046 const bool dep = any_dependent_template_arguments_p (TI_ARGS (ti));
4047 --processing_template_decl;
4048 return dep;
4050 case IS_ABSTRACT:
4051 return DECL_PURE_VIRTUAL_P (t);
4052 case IS_CONSTRUCTOR:
4053 return DECL_CONSTRUCTOR_P (t);
4054 case IS_DESTRUCTOR:
4055 return DECL_DESTRUCTOR_P (t);
4056 case IS_COPY_CONSTRUCTOR:
4057 return DECL_COPY_CONSTRUCTOR_P (t);
4058 case IS_MOVE_CONSTRUCTOR:
4059 return DECL_MOVE_CONSTRUCTOR_P (t);
4060 case IS_TEMPLATE:
4061 return TREE_CODE (t) == TEMPLATE_DECL;
4062 case IS_TRIVIAL:
4063 return trivial_type_p (t);
4064 default:
4065 return 0;
4069 /* Collect source file references recursively, starting from NAMESPC. */
4071 static void
4072 collect_source_refs (tree namespc)
4074 /* Iterate over names in this name space. */
4075 for (tree t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4076 if (DECL_IS_BUILTIN (t))
4078 else if (TREE_CODE (t) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (t))
4079 collect_source_refs (t);
4080 else
4081 collect_source_ref (DECL_SOURCE_FILE (t));
4084 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4085 starting from NAMESPC. */
4087 static void
4088 collect_ada_namespace (tree namespc, const char *source_file)
4090 tree decl = NAMESPACE_LEVEL (namespc)->names;
4092 /* Collect decls from this namespace. This will skip
4093 NAMESPACE_DECLs (both aliases and regular, it cannot tell). */
4094 collect_ada_nodes (decl, source_file);
4096 /* Now scan for namespace children, and dump them. */
4097 for (; decl; decl = TREE_CHAIN (decl))
4098 if (TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl))
4099 collect_ada_namespace (decl, source_file);
4102 /* Returns true iff there is a definition available for variable or
4103 function DECL. */
4105 bool
4106 decl_defined_p (tree decl)
4108 if (TREE_CODE (decl) == FUNCTION_DECL)
4109 return (DECL_INITIAL (decl) != NULL_TREE
4110 /* A pending instantiation of a friend temploid is defined. */
4111 || (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
4112 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4113 (DECL_TI_TEMPLATE (decl)))));
4114 else
4116 gcc_assert (VAR_P (decl));
4117 return !DECL_EXTERNAL (decl);
4121 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4123 [expr.const]
4125 An integral constant-expression can only involve ... const
4126 variables of integral or enumeration types initialized with
4127 constant expressions ...
4129 C++0x also allows constexpr variables and temporaries initialized
4130 with constant expressions. We handle the former here, but the latter
4131 are just folded away in cxx_eval_constant_expression.
4133 The standard does not require that the expression be non-volatile.
4134 G++ implements the proposed correction in DR 457. */
4136 bool
4137 decl_constant_var_p (tree decl)
4139 if (!decl_maybe_constant_var_p (decl))
4140 return false;
4142 /* We don't know if a template static data member is initialized with
4143 a constant expression until we instantiate its initializer. Even
4144 in the case of a constexpr variable, we can't treat it as a
4145 constant until its initializer is complete in case it's used in
4146 its own initializer. */
4147 maybe_instantiate_decl (decl);
4148 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4151 /* Returns true if DECL could be a symbolic constant variable, depending on
4152 its initializer. */
4154 bool
4155 decl_maybe_constant_var_p (tree decl)
4157 tree type = TREE_TYPE (decl);
4158 if (!VAR_P (decl))
4159 return false;
4160 if (DECL_DECLARED_CONSTEXPR_P (decl))
4161 return true;
4162 if (DECL_HAS_VALUE_EXPR_P (decl))
4163 /* A proxy isn't constant. */
4164 return false;
4165 if (TREE_CODE (type) == REFERENCE_TYPE)
4166 /* References can be constant. */;
4167 else if (CP_TYPE_CONST_NON_VOLATILE_P (type)
4168 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4169 /* And const integers. */;
4170 else
4171 return false;
4173 if (DECL_INITIAL (decl)
4174 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
4175 /* We know the initializer, and it isn't constant. */
4176 return false;
4177 else
4178 return true;
4181 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4182 called from grokfndecl and grokvardecl; in all modes it is called from
4183 cp_write_global_declarations. */
4185 void
4186 no_linkage_error (tree decl)
4188 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4189 /* In C++11 it's ok if the decl is defined. */
4190 return;
4191 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4192 if (t == NULL_TREE)
4193 /* The type that got us on no_linkage_decls must have gotten a name for
4194 linkage purposes. */;
4195 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4196 /* The type might end up having a typedef name for linkage purposes. */
4197 vec_safe_push (no_linkage_decls, decl);
4198 else if (TYPE_UNNAMED_P (t))
4200 bool d = false;
4201 if (cxx_dialect >= cxx11)
4202 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4203 "unnamed type, is used but never defined", decl);
4204 else if (DECL_EXTERN_C_P (decl))
4205 /* Allow this; it's pretty common in C. */;
4206 else if (VAR_P (decl))
4207 /* DRs 132, 319 and 389 seem to indicate types with
4208 no linkage can only be used to declare extern "C"
4209 entities. Since it's not always an error in the
4210 ISO C++ 90 Standard, we only issue a warning. */
4211 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "unnamed type "
4212 "with no linkage used to declare variable %q#D with "
4213 "linkage", decl);
4214 else
4215 d = permerror (DECL_SOURCE_LOCATION (decl), "unnamed type with no "
4216 "linkage used to declare function %q#D with linkage",
4217 decl);
4218 if (d && is_typedef_decl (TYPE_NAME (t)))
4219 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4220 "to the unqualified type, so it is not used for linkage",
4221 TYPE_NAME (t));
4223 else if (cxx_dialect >= cxx11)
4225 if (VAR_P (decl) || !DECL_PURE_VIRTUAL_P (decl))
4226 permerror (DECL_SOURCE_LOCATION (decl),
4227 "%q#D, declared using local type "
4228 "%qT, is used but never defined", decl, t);
4230 else if (VAR_P (decl))
4231 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4232 "used to declare variable %q#D with linkage", t, decl);
4233 else
4234 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4235 "to declare function %q#D with linkage", t, decl);
4238 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4240 static void
4241 collect_all_refs (const char *source_file)
4243 collect_ada_namespace (global_namespace, source_file);
4246 /* Clear DECL_EXTERNAL for NODE. */
4248 static bool
4249 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4251 DECL_EXTERNAL (node->decl) = 0;
4252 return false;
4255 /* Build up the function to run dynamic initializers for thread_local
4256 variables in this translation unit and alias the init functions for the
4257 individual variables to it. */
4259 static void
4260 handle_tls_init (void)
4262 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4263 if (vars == NULL_TREE)
4264 return;
4266 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4268 write_out_vars (vars);
4270 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4271 boolean_type_node);
4272 TREE_PUBLIC (guard) = false;
4273 TREE_STATIC (guard) = true;
4274 DECL_ARTIFICIAL (guard) = true;
4275 DECL_IGNORED_P (guard) = true;
4276 TREE_USED (guard) = true;
4277 CP_DECL_THREAD_LOCAL_P (guard) = true;
4278 set_decl_tls_model (guard, decl_default_tls_model (guard));
4279 pushdecl_top_level_and_finish (guard, NULL_TREE);
4281 tree fn = get_local_tls_init_fn ();
4282 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4283 tree body = begin_function_body ();
4284 tree if_stmt = begin_if_stmt ();
4285 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4286 tf_warning_or_error);
4287 finish_if_stmt_cond (cond, if_stmt);
4288 finish_expr_stmt (cp_build_modify_expr (loc, guard, NOP_EXPR,
4289 boolean_true_node,
4290 tf_warning_or_error));
4291 for (; vars; vars = TREE_CHAIN (vars))
4293 tree var = TREE_VALUE (vars);
4294 tree init = TREE_PURPOSE (vars);
4295 one_static_initialization_or_destruction (var, init, true);
4297 /* Output init aliases even with -fno-extern-tls-init. */
4298 if (TARGET_SUPPORTS_ALIASES && TREE_PUBLIC (var))
4300 tree single_init_fn = get_tls_init_fn (var);
4301 if (single_init_fn == NULL_TREE)
4302 continue;
4303 cgraph_node *alias
4304 = cgraph_node::get_create (fn)->create_same_body_alias
4305 (single_init_fn, fn);
4306 gcc_assert (alias != NULL);
4310 finish_then_clause (if_stmt);
4311 finish_if_stmt (if_stmt);
4312 finish_function_body (body);
4313 expand_or_defer_fn (finish_function (/*inline_p=*/false));
4316 /* We're at the end of compilation, so generate any mangling aliases that
4317 we've been saving up, if DECL is going to be output and ID2 isn't
4318 already taken by another declaration. */
4320 static void
4321 generate_mangling_alias (tree decl, tree id2)
4323 struct cgraph_node *n = NULL;
4325 if (TREE_CODE (decl) == FUNCTION_DECL)
4327 n = cgraph_node::get (decl);
4328 if (!n)
4329 /* Don't create an alias to an unreferenced function. */
4330 return;
4333 tree *slot
4334 = mangled_decls->find_slot_with_hash (id2, IDENTIFIER_HASH_VALUE (id2),
4335 INSERT);
4337 /* If there's a declaration already using this mangled name,
4338 don't create a compatibility alias that conflicts. */
4339 if (*slot)
4340 return;
4342 tree alias = make_alias_for (decl, id2);
4343 *slot = alias;
4345 DECL_IGNORED_P (alias) = 1;
4346 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
4347 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
4348 if (vague_linkage_p (decl))
4349 DECL_WEAK (alias) = 1;
4351 if (n)
4352 n->create_same_body_alias (alias, decl);
4353 else
4354 varpool_node::create_extra_name_alias (alias, decl);
4357 /* Note that we might want to emit an alias with the symbol ID2 for DECL at
4358 the end of translation, for compatibility across bugs in the mangling
4359 implementation. */
4361 void
4362 note_mangling_alias (tree decl, tree id2)
4364 if (TARGET_SUPPORTS_ALIASES)
4366 if (!defer_mangling_aliases)
4367 generate_mangling_alias (decl, id2);
4368 else
4370 vec_safe_push (mangling_aliases, decl);
4371 vec_safe_push (mangling_aliases, id2);
4376 /* Emit all mangling aliases that were deferred up to this point. */
4378 void
4379 generate_mangling_aliases ()
4381 while (!vec_safe_is_empty (mangling_aliases))
4383 tree id2 = mangling_aliases->pop();
4384 tree decl = mangling_aliases->pop();
4385 generate_mangling_alias (decl, id2);
4387 defer_mangling_aliases = false;
4390 /* Record a mangling of DECL, whose DECL_ASSEMBLER_NAME has just been
4391 set. NEED_WARNING is true if we must warn about collisions. We do
4392 this to spot changes in mangling that may require compatibility
4393 aliases. */
4395 void
4396 record_mangling (tree decl, bool need_warning)
4398 if (!mangled_decls)
4399 mangled_decls = hash_table<mangled_decl_hash>::create_ggc (499);
4401 gcc_checking_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
4402 tree id = DECL_ASSEMBLER_NAME_RAW (decl);
4403 tree *slot
4404 = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id),
4405 INSERT);
4407 /* If this is already an alias, remove the alias, because the real
4408 decl takes precedence. */
4409 if (*slot && DECL_ARTIFICIAL (*slot) && DECL_IGNORED_P (*slot))
4410 if (symtab_node *n = symtab_node::get (*slot))
4411 if (n->cpp_implicit_alias)
4413 n->remove ();
4414 *slot = NULL_TREE;
4417 if (!*slot)
4418 *slot = decl;
4419 else if (need_warning)
4421 error_at (DECL_SOURCE_LOCATION (decl),
4422 "mangling of %q#D as %qE conflicts with a previous mangle",
4423 decl, id);
4424 inform (DECL_SOURCE_LOCATION (*slot),
4425 "previous mangling %q#D", *slot);
4426 inform (DECL_SOURCE_LOCATION (decl),
4427 "a later -fabi-version= (or =0)"
4428 " avoids this error with a change in mangling");
4429 *slot = decl;
4433 /* The entire file is now complete. If requested, dump everything
4434 to a file. */
4436 static void
4437 dump_tu (void)
4439 dump_flags_t flags;
4440 if (FILE *stream = dump_begin (raw_dump_id, &flags))
4442 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4443 dump_end (raw_dump_id, stream);
4447 static location_t locus_at_end_of_parsing;
4449 /* Check the deallocation functions for CODE to see if we want to warn that
4450 only one was defined. */
4452 static void
4453 maybe_warn_sized_delete (enum tree_code code)
4455 tree sized = NULL_TREE;
4456 tree unsized = NULL_TREE;
4458 for (ovl_iterator iter (get_global_binding (ovl_op_identifier (false, code)));
4459 iter; ++iter)
4461 tree fn = *iter;
4462 /* We're only interested in usual deallocation functions. */
4463 if (!usual_deallocation_fn_p (fn))
4464 continue;
4465 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
4466 unsized = fn;
4467 else
4468 sized = fn;
4470 if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
4471 warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
4472 "the program should also define %qD", sized);
4473 else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
4474 warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
4475 "the program should also define %qD", unsized);
4478 /* Check the global deallocation functions to see if we want to warn about
4479 defining unsized without sized (or vice versa). */
4481 static void
4482 maybe_warn_sized_delete ()
4484 if (!flag_sized_deallocation || !warn_sized_deallocation)
4485 return;
4486 maybe_warn_sized_delete (DELETE_EXPR);
4487 maybe_warn_sized_delete (VEC_DELETE_EXPR);
4490 /* Earlier we left PTRMEM_CST in variable initializers alone so that we could
4491 look them up when evaluating non-type template parameters. Now we need to
4492 lower them to something the back end can understand. */
4494 static void
4495 lower_var_init ()
4497 varpool_node *node;
4498 FOR_EACH_VARIABLE (node)
4500 tree d = node->decl;
4501 if (tree init = DECL_INITIAL (d))
4502 DECL_INITIAL (d) = cplus_expand_constant (init);
4506 /* This routine is called at the end of compilation.
4507 Its job is to create all the code needed to initialize and
4508 destroy the global aggregates. We do the destruction
4509 first, since that way we only need to reverse the decls once. */
4511 void
4512 c_parse_final_cleanups (void)
4514 tree vars;
4515 bool reconsider;
4516 size_t i;
4517 unsigned ssdf_count = 0;
4518 int retries = 0;
4519 tree decl;
4521 locus_at_end_of_parsing = input_location;
4522 at_eof = 1;
4524 /* Bad parse errors. Just forget about it. */
4525 if (! global_bindings_p () || current_class_type
4526 || !vec_safe_is_empty (decl_namespace_list))
4527 return;
4529 /* This is the point to write out a PCH if we're doing that.
4530 In that case we do not want to do anything else. */
4531 if (pch_file)
4533 /* Mangle all symbols at PCH creation time. */
4534 symtab_node *node;
4535 FOR_EACH_SYMBOL (node)
4536 if (! is_a <varpool_node *> (node)
4537 || ! DECL_HARD_REGISTER (node->decl))
4538 DECL_ASSEMBLER_NAME (node->decl);
4539 c_common_write_pch ();
4540 dump_tu ();
4541 /* Ensure even the callers don't try to finalize the CU. */
4542 flag_syntax_only = 1;
4543 return;
4546 timevar_stop (TV_PHASE_PARSING);
4547 timevar_start (TV_PHASE_DEFERRED);
4549 symtab->process_same_body_aliases ();
4551 /* Handle -fdump-ada-spec[-slim] */
4552 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4554 if (flag_dump_ada_spec_slim)
4555 collect_source_ref (main_input_filename);
4556 else
4557 collect_source_refs (global_namespace);
4559 dump_ada_specs (collect_all_refs, cpp_check);
4562 /* FIXME - huh? was input_line -= 1;*/
4564 /* We now have to write out all the stuff we put off writing out.
4565 These include:
4567 o Template specializations that we have not yet instantiated,
4568 but which are needed.
4569 o Initialization and destruction for non-local objects with
4570 static storage duration. (Local objects with static storage
4571 duration are initialized when their scope is first entered,
4572 and are cleaned up via atexit.)
4573 o Virtual function tables.
4575 All of these may cause others to be needed. For example,
4576 instantiating one function may cause another to be needed, and
4577 generating the initializer for an object may cause templates to be
4578 instantiated, etc., etc. */
4580 emit_support_tinfos ();
4584 tree t;
4585 tree decl;
4587 reconsider = false;
4589 /* If there are templates that we've put off instantiating, do
4590 them now. */
4591 instantiate_pending_templates (retries);
4592 ggc_collect ();
4594 /* Write out virtual tables as required. Writing out the
4595 virtual table for a template class may cause the
4596 instantiation of members of that class. If we write out
4597 vtables then we remove the class from our list so we don't
4598 have to look at it again. */
4599 for (i = keyed_classes->length ();
4600 keyed_classes->iterate (--i, &t);)
4601 if (maybe_emit_vtables (t))
4603 reconsider = true;
4604 keyed_classes->unordered_remove (i);
4607 /* Write out needed type info variables. We have to be careful
4608 looping through unemitted decls, because emit_tinfo_decl may
4609 cause other variables to be needed. New elements will be
4610 appended, and we remove from the vector those that actually
4611 get emitted. */
4612 for (i = unemitted_tinfo_decls->length ();
4613 unemitted_tinfo_decls->iterate (--i, &t);)
4614 if (emit_tinfo_decl (t))
4616 reconsider = true;
4617 unemitted_tinfo_decls->unordered_remove (i);
4620 /* The list of objects with static storage duration is built up
4621 in reverse order. We clear STATIC_AGGREGATES so that any new
4622 aggregates added during the initialization of these will be
4623 initialized in the correct order when we next come around the
4624 loop. */
4625 vars = prune_vars_needing_no_initialization (&static_aggregates);
4627 if (vars)
4629 /* We need to start a new initialization function each time
4630 through the loop. That's because we need to know which
4631 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4632 isn't computed until a function is finished, and written
4633 out. That's a deficiency in the back end. When this is
4634 fixed, these initialization functions could all become
4635 inline, with resulting performance improvements. */
4636 tree ssdf_body;
4638 /* Set the line and file, so that it is obviously not from
4639 the source file. */
4640 input_location = locus_at_end_of_parsing;
4641 ssdf_body = start_static_storage_duration_function (ssdf_count);
4643 /* Make sure the back end knows about all the variables. */
4644 write_out_vars (vars);
4646 /* First generate code to do all the initializations. */
4647 if (vars)
4648 do_static_initialization_or_destruction (vars, /*initp=*/true);
4650 /* Then, generate code to do all the destructions. Do these
4651 in reverse order so that the most recently constructed
4652 variable is the first destroyed. If we're using
4653 __cxa_atexit, then we don't need to do this; functions
4654 were registered at initialization time to destroy the
4655 local statics. */
4656 if (!flag_use_cxa_atexit && vars)
4658 vars = nreverse (vars);
4659 do_static_initialization_or_destruction (vars, /*initp=*/false);
4661 else
4662 vars = NULL_TREE;
4664 /* Finish up the static storage duration function for this
4665 round. */
4666 input_location = locus_at_end_of_parsing;
4667 finish_static_storage_duration_function (ssdf_body);
4669 /* All those initializations and finalizations might cause
4670 us to need more inline functions, more template
4671 instantiations, etc. */
4672 reconsider = true;
4673 ssdf_count++;
4674 /* ??? was: locus_at_end_of_parsing.line++; */
4677 /* Now do the same for thread_local variables. */
4678 handle_tls_init ();
4680 /* Go through the set of inline functions whose bodies have not
4681 been emitted yet. If out-of-line copies of these functions
4682 are required, emit them. */
4683 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4685 /* Does it need synthesizing? */
4686 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4687 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4689 /* Even though we're already at the top-level, we push
4690 there again. That way, when we pop back a few lines
4691 hence, all of our state is restored. Otherwise,
4692 finish_function doesn't clean things up, and we end
4693 up with CURRENT_FUNCTION_DECL set. */
4694 push_to_top_level ();
4695 /* The decl's location will mark where it was first
4696 needed. Save that so synthesize method can indicate
4697 where it was needed from, in case of error */
4698 input_location = DECL_SOURCE_LOCATION (decl);
4699 synthesize_method (decl);
4700 pop_from_top_level ();
4701 reconsider = true;
4704 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4705 generate_tls_wrapper (decl);
4707 if (!DECL_SAVED_TREE (decl))
4708 continue;
4710 cgraph_node *node = cgraph_node::get_create (decl);
4712 /* We lie to the back end, pretending that some functions
4713 are not defined when they really are. This keeps these
4714 functions from being put out unnecessarily. But, we must
4715 stop lying when the functions are referenced, or if they
4716 are not comdat since they need to be put out now. If
4717 DECL_INTERFACE_KNOWN, then we have already set
4718 DECL_EXTERNAL appropriately, so there's no need to check
4719 again, and we do not want to clear DECL_EXTERNAL if a
4720 previous call to import_export_decl set it.
4722 This is done in a separate for cycle, because if some
4723 deferred function is contained in another deferred
4724 function later in deferred_fns varray,
4725 rest_of_compilation would skip this function and we
4726 really cannot expand the same function twice. */
4727 import_export_decl (decl);
4728 if (DECL_NOT_REALLY_EXTERN (decl)
4729 && DECL_INITIAL (decl)
4730 && decl_needed_p (decl))
4732 if (node->cpp_implicit_alias)
4733 node = node->get_alias_target ();
4735 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
4736 NULL, true);
4737 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4738 group, we need to mark all symbols in the same comdat group
4739 that way. */
4740 if (node->same_comdat_group)
4741 for (cgraph_node *next
4742 = dyn_cast<cgraph_node *> (node->same_comdat_group);
4743 next != node;
4744 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
4745 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
4746 NULL, true);
4749 /* If we're going to need to write this function out, and
4750 there's already a body for it, create RTL for it now.
4751 (There might be no body if this is a method we haven't
4752 gotten around to synthesizing yet.) */
4753 if (!DECL_EXTERNAL (decl)
4754 && decl_needed_p (decl)
4755 && !TREE_ASM_WRITTEN (decl)
4756 && !node->definition)
4758 /* We will output the function; no longer consider it in this
4759 loop. */
4760 DECL_DEFER_OUTPUT (decl) = 0;
4761 /* Generate RTL for this function now that we know we
4762 need it. */
4763 expand_or_defer_fn (decl);
4764 /* If we're compiling -fsyntax-only pretend that this
4765 function has been written out so that we don't try to
4766 expand it again. */
4767 if (flag_syntax_only)
4768 TREE_ASM_WRITTEN (decl) = 1;
4769 reconsider = true;
4773 if (wrapup_namespace_globals ())
4774 reconsider = true;
4776 /* Static data members are just like namespace-scope globals. */
4777 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4779 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4780 /* Don't write it out if we haven't seen a definition. */
4781 || (DECL_IN_AGGR_P (decl) && !DECL_INLINE_VAR_P (decl)))
4782 continue;
4783 import_export_decl (decl);
4784 /* If this static data member is needed, provide it to the
4785 back end. */
4786 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4787 DECL_EXTERNAL (decl) = 0;
4789 if (vec_safe_length (pending_statics) != 0
4790 && wrapup_global_declarations (pending_statics->address (),
4791 pending_statics->length ()))
4792 reconsider = true;
4794 retries++;
4796 while (reconsider);
4798 lower_var_init ();
4800 generate_mangling_aliases ();
4802 /* All used inline functions must have a definition at this point. */
4803 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4805 if (/* Check online inline functions that were actually used. */
4806 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4807 /* If the definition actually was available here, then the
4808 fact that the function was not defined merely represents
4809 that for some reason (use of a template repository,
4810 #pragma interface, etc.) we decided not to emit the
4811 definition here. */
4812 && !DECL_INITIAL (decl)
4813 /* Don't complain if the template was defined. */
4814 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4815 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4816 (template_for_substitution (decl)))))
4818 warning_at (DECL_SOURCE_LOCATION (decl), 0,
4819 "inline function %qD used but never defined", decl);
4820 /* Avoid a duplicate warning from check_global_declaration. */
4821 TREE_NO_WARNING (decl) = 1;
4825 /* So must decls that use a type with no linkage. */
4826 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4827 no_linkage_error (decl);
4829 maybe_warn_sized_delete ();
4831 /* Then, do the Objective-C stuff. This is where all the
4832 Objective-C module stuff gets generated (symtab,
4833 class/protocol/selector lists etc). This must be done after C++
4834 templates, destructors etc. so that selectors used in C++
4835 templates are properly allocated. */
4836 if (c_dialect_objc ())
4837 objc_write_global_declarations ();
4839 /* We give C linkage to static constructors and destructors. */
4840 push_lang_context (lang_name_c);
4842 /* Generate initialization and destruction functions for all
4843 priorities for which they are required. */
4844 if (priority_info_map)
4845 splay_tree_foreach (priority_info_map,
4846 generate_ctor_and_dtor_functions_for_priority,
4847 /*data=*/&locus_at_end_of_parsing);
4848 else if (c_dialect_objc () && objc_static_init_needed_p ())
4849 /* If this is obj-c++ and we need a static init, call
4850 generate_ctor_or_dtor_function. */
4851 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4852 DEFAULT_INIT_PRIORITY,
4853 &locus_at_end_of_parsing);
4855 /* We're done with the splay-tree now. */
4856 if (priority_info_map)
4857 splay_tree_delete (priority_info_map);
4859 /* Generate any missing aliases. */
4860 maybe_apply_pending_pragma_weaks ();
4862 /* We're done with static constructors, so we can go back to "C++"
4863 linkage now. */
4864 pop_lang_context ();
4866 if (flag_vtable_verify)
4868 vtv_recover_class_info ();
4869 vtv_compute_class_hierarchy_transitive_closure ();
4870 vtv_build_vtable_verify_fndecl ();
4873 perform_deferred_noexcept_checks ();
4875 finish_repo ();
4876 fini_constexpr ();
4878 /* The entire file is now complete. If requested, dump everything
4879 to a file. */
4880 dump_tu ();
4882 if (flag_detailed_statistics)
4884 dump_tree_statistics ();
4885 dump_time_statistics ();
4888 timevar_stop (TV_PHASE_DEFERRED);
4889 timevar_start (TV_PHASE_PARSING);
4891 /* Indicate that we're done with front end processing. */
4892 at_eof = 2;
4895 /* Perform any post compilation-proper cleanups for the C++ front-end.
4896 This should really go away. No front-end should need to do
4897 anything past the compilation process. */
4899 void
4900 cxx_post_compilation_parsing_cleanups (void)
4902 timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS);
4904 if (flag_vtable_verify)
4906 /* Generate the special constructor initialization function that
4907 calls __VLTRegisterPairs, and give it a very high
4908 initialization priority. This must be done after
4909 finalize_compilation_unit so that we have accurate
4910 information about which vtable will actually be emitted. */
4911 vtv_generate_init_routine ();
4914 input_location = locus_at_end_of_parsing;
4916 if (flag_checking)
4917 validate_conversion_obstack ();
4919 timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS);
4922 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4923 function to call in parse-tree form; it has not yet been
4924 semantically analyzed. ARGS are the arguments to the function.
4925 They have already been semantically analyzed. This may change
4926 ARGS. */
4928 tree
4929 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4930 tsubst_flags_t complain)
4932 tree orig_fn;
4933 vec<tree, va_gc> *orig_args = NULL;
4934 tree expr;
4935 tree object;
4937 orig_fn = fn;
4938 object = TREE_OPERAND (fn, 0);
4940 if (processing_template_decl)
4942 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4943 || TREE_CODE (fn) == MEMBER_REF);
4944 if (type_dependent_expression_p (fn)
4945 || any_type_dependent_arguments_p (*args))
4946 return build_min_nt_call_vec (fn, *args);
4948 orig_args = make_tree_vector_copy (*args);
4950 /* Transform the arguments and add the implicit "this"
4951 parameter. That must be done before the FN is transformed
4952 because we depend on the form of FN. */
4953 make_args_non_dependent (*args);
4954 object = build_non_dependent_expr (object);
4955 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4957 if (TREE_CODE (fn) == DOTSTAR_EXPR)
4958 object = cp_build_addr_expr (object, complain);
4959 vec_safe_insert (*args, 0, object);
4961 /* Now that the arguments are done, transform FN. */
4962 fn = build_non_dependent_expr (fn);
4965 /* A qualified name corresponding to a bound pointer-to-member is
4966 represented as an OFFSET_REF:
4968 struct B { void g(); };
4969 void (B::*p)();
4970 void B::g() { (this->*p)(); } */
4971 if (TREE_CODE (fn) == OFFSET_REF)
4973 tree object_addr = cp_build_addr_expr (object, complain);
4974 fn = TREE_OPERAND (fn, 1);
4975 fn = get_member_function_from_ptrfunc (&object_addr, fn,
4976 complain);
4977 vec_safe_insert (*args, 0, object_addr);
4980 if (CLASS_TYPE_P (TREE_TYPE (fn)))
4981 expr = build_op_call (fn, args, complain);
4982 else
4983 expr = cp_build_function_call_vec (fn, args, complain);
4984 if (processing_template_decl && expr != error_mark_node)
4985 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
4987 if (orig_args != NULL)
4988 release_tree_vector (orig_args);
4990 return expr;
4994 void
4995 check_default_args (tree x)
4997 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4998 bool saw_def = false;
4999 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
5000 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
5002 if (TREE_PURPOSE (arg))
5003 saw_def = true;
5004 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
5006 error ("default argument missing for parameter %P of %q+#D", i, x);
5007 TREE_PURPOSE (arg) = error_mark_node;
5012 /* Return true if function DECL can be inlined. This is used to force
5013 instantiation of methods that might be interesting for inlining. */
5014 bool
5015 possibly_inlined_p (tree decl)
5017 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
5018 if (DECL_UNINLINABLE (decl))
5019 return false;
5020 if (!optimize)
5021 return DECL_DECLARED_INLINE_P (decl);
5022 /* When optimizing, we might inline everything when flatten
5023 attribute or heuristics inlining for size or autoinlining
5024 is used. */
5025 return true;
5028 /* Normally, we can wait until instantiation-time to synthesize DECL.
5029 However, if DECL is a static data member initialized with a constant
5030 or a constexpr function, we need it right now because a reference to
5031 such a data member or a call to such function is not value-dependent.
5032 For a function that uses auto in the return type, we need to instantiate
5033 it to find out its type. For OpenMP user defined reductions, we need
5034 them instantiated for reduction clauses which inline them by hand
5035 directly. */
5037 static void
5038 maybe_instantiate_decl (tree decl)
5040 if (DECL_LANG_SPECIFIC (decl)
5041 && DECL_TEMPLATE_INFO (decl)
5042 && (decl_maybe_constant_var_p (decl)
5043 || (TREE_CODE (decl) == FUNCTION_DECL
5044 && DECL_OMP_DECLARE_REDUCTION_P (decl))
5045 || undeduced_auto_decl (decl))
5046 && !DECL_DECLARED_CONCEPT_P (decl)
5047 && !uses_template_parms (DECL_TI_ARGS (decl)))
5049 /* Instantiating a function will result in garbage collection. We
5050 must treat this situation as if we were within the body of a
5051 function so as to avoid collecting live data only referenced from
5052 the stack (such as overload resolution candidates). */
5053 ++function_depth;
5054 instantiate_decl (decl, /*defer_ok=*/false,
5055 /*expl_inst_class_mem_p=*/false);
5056 --function_depth;
5060 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
5061 If DECL is a specialization or implicitly declared class member,
5062 generate the actual definition. Return false if something goes
5063 wrong, true otherwise. */
5065 bool
5066 mark_used (tree decl, tsubst_flags_t complain)
5068 /* If DECL is a BASELINK for a single function, then treat it just
5069 like the DECL for the function. Otherwise, if the BASELINK is
5070 for an overloaded function, we don't know which function was
5071 actually used until after overload resolution. */
5072 if (BASELINK_P (decl))
5074 decl = BASELINK_FUNCTIONS (decl);
5075 if (really_overloaded_fn (decl))
5076 return true;
5077 decl = OVL_FIRST (decl);
5080 /* Set TREE_USED for the benefit of -Wunused. */
5081 TREE_USED (decl) = 1;
5082 /* And for structured bindings also the underlying decl. */
5083 if (DECL_DECOMPOSITION_P (decl) && DECL_DECOMP_BASE (decl))
5084 TREE_USED (DECL_DECOMP_BASE (decl)) = 1;
5086 if (TREE_CODE (decl) == TEMPLATE_DECL)
5087 return true;
5089 if (DECL_CLONED_FUNCTION_P (decl))
5090 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5092 /* Mark enumeration types as used. */
5093 if (TREE_CODE (decl) == CONST_DECL)
5094 used_types_insert (DECL_CONTEXT (decl));
5096 if (TREE_CODE (decl) == FUNCTION_DECL
5097 && !maybe_instantiate_noexcept (decl, complain))
5098 return false;
5100 if (TREE_CODE (decl) == FUNCTION_DECL
5101 && DECL_DELETED_FN (decl))
5103 if (DECL_ARTIFICIAL (decl)
5104 && DECL_CONV_FN_P (decl)
5105 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
5106 /* We mark a lambda conversion op as deleted if we can't
5107 generate it properly; see maybe_add_lambda_conv_op. */
5108 sorry ("converting lambda that uses %<...%> to function pointer");
5109 else if (complain & tf_error)
5111 error ("use of deleted function %qD", decl);
5112 if (!maybe_explain_implicit_delete (decl))
5113 inform (DECL_SOURCE_LOCATION (decl), "declared here");
5115 return false;
5118 if (TREE_DEPRECATED (decl) && (complain & tf_warning)
5119 && deprecated_state != DEPRECATED_SUPPRESS)
5120 warn_deprecated_use (decl, NULL_TREE);
5122 /* We can only check DECL_ODR_USED on variables or functions with
5123 DECL_LANG_SPECIFIC set, and these are also the only decls that we
5124 might need special handling for. */
5125 if (!VAR_OR_FUNCTION_DECL_P (decl)
5126 || DECL_LANG_SPECIFIC (decl) == NULL
5127 || DECL_THUNK_P (decl))
5129 if (!processing_template_decl
5130 && !require_deduced_type (decl, complain))
5131 return false;
5132 return true;
5135 /* We only want to do this processing once. We don't need to keep trying
5136 to instantiate inline templates, because unit-at-a-time will make sure
5137 we get them compiled before functions that want to inline them. */
5138 if (DECL_ODR_USED (decl))
5139 return true;
5141 /* Normally, we can wait until instantiation-time to synthesize DECL.
5142 However, if DECL is a static data member initialized with a constant
5143 or a constexpr function, we need it right now because a reference to
5144 such a data member or a call to such function is not value-dependent.
5145 For a function that uses auto in the return type, we need to instantiate
5146 it to find out its type. For OpenMP user defined reductions, we need
5147 them instantiated for reduction clauses which inline them by hand
5148 directly. */
5149 maybe_instantiate_decl (decl);
5151 if (processing_template_decl || in_template_function ())
5152 return true;
5154 /* Check this too in case we're within instantiate_non_dependent_expr. */
5155 if (DECL_TEMPLATE_INFO (decl)
5156 && uses_template_parms (DECL_TI_ARGS (decl)))
5157 return true;
5159 if (!require_deduced_type (decl, complain))
5160 return false;
5162 if (builtin_pack_fn_p (decl))
5164 error ("use of built-in parameter pack %qD outside of a template",
5165 DECL_NAME (decl));
5166 return false;
5169 /* If we don't need a value, then we don't need to synthesize DECL. */
5170 if (cp_unevaluated_operand || in_discarded_stmt)
5171 return true;
5173 DECL_ODR_USED (decl) = 1;
5174 if (DECL_CLONED_FUNCTION_P (decl))
5175 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5177 /* DR 757: A type without linkage shall not be used as the type of a
5178 variable or function with linkage, unless
5179 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5180 o the variable or function is not used (3.2 [basic.def.odr]) or is
5181 defined in the same translation unit. */
5182 if (cxx_dialect > cxx98
5183 && decl_linkage (decl) != lk_none
5184 && !DECL_EXTERN_C_P (decl)
5185 && !DECL_ARTIFICIAL (decl)
5186 && !decl_defined_p (decl)
5187 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
5189 if (is_local_extern (decl))
5190 /* There's no way to define a local extern, and adding it to
5191 the vector interferes with GC, so give an error now. */
5192 no_linkage_error (decl);
5193 else
5194 vec_safe_push (no_linkage_decls, decl);
5197 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
5198 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
5199 /* Remember it, so we can check it was defined. */
5200 note_vague_linkage_fn (decl);
5202 /* Is it a synthesized method that needs to be synthesized? */
5203 if (TREE_CODE (decl) == FUNCTION_DECL
5204 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5205 && DECL_DEFAULTED_FN (decl)
5206 /* A function defaulted outside the class is synthesized either by
5207 cp_finish_decl or instantiate_decl. */
5208 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
5209 && ! DECL_INITIAL (decl))
5211 /* Defer virtual destructors so that thunks get the right
5212 linkage. */
5213 if (DECL_VIRTUAL_P (decl) && !at_eof)
5215 note_vague_linkage_fn (decl);
5216 return true;
5219 /* Remember the current location for a function we will end up
5220 synthesizing. Then we can inform the user where it was
5221 required in the case of error. */
5222 DECL_SOURCE_LOCATION (decl) = input_location;
5224 /* Synthesizing an implicitly defined member function will result in
5225 garbage collection. We must treat this situation as if we were
5226 within the body of a function so as to avoid collecting live data
5227 on the stack (such as overload resolution candidates).
5229 We could just let cp_write_global_declarations handle synthesizing
5230 this function by adding it to deferred_fns, but doing
5231 it at the use site produces better error messages. */
5232 ++function_depth;
5233 synthesize_method (decl);
5234 --function_depth;
5235 /* If this is a synthesized method we don't need to
5236 do the instantiation test below. */
5238 else if (VAR_OR_FUNCTION_DECL_P (decl)
5239 && DECL_TEMPLATE_INFO (decl)
5240 && !DECL_DECLARED_CONCEPT_P (decl)
5241 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5242 || always_instantiate_p (decl)))
5243 /* If this is a function or variable that is an instance of some
5244 template, we now know that we will need to actually do the
5245 instantiation. We check that DECL is not an explicit
5246 instantiation because that is not checked in instantiate_decl.
5248 We put off instantiating functions in order to improve compile
5249 times. Maintaining a stack of active functions is expensive,
5250 and the inliner knows to instantiate any functions it might
5251 need. Therefore, we always try to defer instantiation. */
5253 ++function_depth;
5254 instantiate_decl (decl, /*defer_ok=*/true,
5255 /*expl_inst_class_mem_p=*/false);
5256 --function_depth;
5259 return true;
5262 bool
5263 mark_used (tree decl)
5265 return mark_used (decl, tf_warning_or_error);
5268 tree
5269 vtv_start_verification_constructor_init_function (void)
5271 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5274 tree
5275 vtv_finish_verification_constructor_init_function (tree function_body)
5277 tree fn;
5279 finish_compound_stmt (function_body);
5280 fn = finish_function (/*inline_p=*/false);
5281 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5282 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5284 return fn;
5287 #include "gt-cp-decl2.h"