[C++ PATCH] Move mangling alias out of ::
[official-gcc.git] / gcc / cp / decl2.c
blob354d503da6d5dc1b14beab0aab04b7381bc5ff60
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 /* A hash table of mangled names to decls. Used to figure out if we
106 need compatibility aliases. */
107 static GTY(()) hash_map<lang_identifier *, tree> *mangled_decls;
109 /* Nonzero if we're done parsing and into end-of-file activities. */
111 int at_eof;
113 /* True if note_mangling_alias should enqueue mangling aliases for
114 later generation, rather than emitting them right away. */
116 bool defer_mangling_aliases = true;
119 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
120 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
121 that apply to the function). */
123 tree
124 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
125 cp_ref_qualifier rqual)
127 tree raises;
128 tree attrs;
129 int type_quals;
130 bool late_return_type_p;
132 if (fntype == error_mark_node || ctype == error_mark_node)
133 return error_mark_node;
135 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
136 || TREE_CODE (fntype) == METHOD_TYPE);
138 type_quals = quals & ~TYPE_QUAL_RESTRICT;
139 ctype = cp_build_qualified_type (ctype, type_quals);
140 raises = TYPE_RAISES_EXCEPTIONS (fntype);
141 attrs = TYPE_ATTRIBUTES (fntype);
142 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
143 fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
144 (TREE_CODE (fntype) == METHOD_TYPE
145 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
146 : TYPE_ARG_TYPES (fntype)));
147 if (attrs)
148 fntype = cp_build_type_attribute_variant (fntype, attrs);
149 if (rqual)
150 fntype = build_ref_qualified_type (fntype, rqual);
151 if (raises)
152 fntype = build_exception_variant (fntype, raises);
153 if (late_return_type_p)
154 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
156 return fntype;
159 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
160 return type changed to NEW_RET. */
162 tree
163 change_return_type (tree new_ret, tree fntype)
165 tree newtype;
166 tree args = TYPE_ARG_TYPES (fntype);
167 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
168 tree attrs = TYPE_ATTRIBUTES (fntype);
169 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
171 if (new_ret == error_mark_node)
172 return fntype;
174 if (same_type_p (new_ret, TREE_TYPE (fntype)))
175 return fntype;
177 if (TREE_CODE (fntype) == FUNCTION_TYPE)
179 newtype = build_function_type (new_ret, args);
180 newtype = apply_memfn_quals (newtype,
181 type_memfn_quals (fntype),
182 type_memfn_rqual (fntype));
184 else
185 newtype = build_method_type_directly
186 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
187 if (FUNCTION_REF_QUALIFIED (fntype))
188 newtype = build_ref_qualified_type (newtype, type_memfn_rqual (fntype));
189 if (raises)
190 newtype = build_exception_variant (newtype, raises);
191 if (attrs)
192 newtype = cp_build_type_attribute_variant (newtype, attrs);
193 if (late_return_type_p)
194 TYPE_HAS_LATE_RETURN_TYPE (newtype) = 1;
196 return newtype;
199 /* Build a PARM_DECL of FN with NAME and TYPE, and set DECL_ARG_TYPE
200 appropriately. */
202 tree
203 cp_build_parm_decl (tree fn, tree name, tree type)
205 tree parm = build_decl (input_location,
206 PARM_DECL, name, type);
207 DECL_CONTEXT (parm) = fn;
209 /* DECL_ARG_TYPE is only used by the back end and the back end never
210 sees templates. */
211 if (!processing_template_decl)
212 DECL_ARG_TYPE (parm) = type_passed_as (type);
214 return parm;
217 /* Returns a PARM_DECL of FN for a parameter of the indicated TYPE, with the
218 indicated NAME. */
220 tree
221 build_artificial_parm (tree fn, tree name, tree type)
223 tree parm = cp_build_parm_decl (fn, name, type);
224 DECL_ARTIFICIAL (parm) = 1;
225 /* All our artificial parms are implicitly `const'; they cannot be
226 assigned to. */
227 TREE_READONLY (parm) = 1;
228 return parm;
231 /* Constructors for types with virtual baseclasses need an "in-charge" flag
232 saying whether this constructor is responsible for initialization of
233 virtual baseclasses or not. All destructors also need this "in-charge"
234 flag, which additionally determines whether or not the destructor should
235 free the memory for the object.
237 This function adds the "in-charge" flag to member function FN if
238 appropriate. It is called from grokclassfn and tsubst.
239 FN must be either a constructor or destructor.
241 The in-charge flag follows the 'this' parameter, and is followed by the
242 VTT parm (if any), then the user-written parms. */
244 void
245 maybe_retrofit_in_chrg (tree fn)
247 tree basetype, arg_types, parms, parm, fntype;
249 /* If we've already add the in-charge parameter don't do it again. */
250 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
251 return;
253 /* When processing templates we can't know, in general, whether or
254 not we're going to have virtual baseclasses. */
255 if (processing_template_decl)
256 return;
258 /* We don't need an in-charge parameter for constructors that don't
259 have virtual bases. */
260 if (DECL_CONSTRUCTOR_P (fn)
261 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
262 return;
264 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
265 basetype = TREE_TYPE (TREE_VALUE (arg_types));
266 arg_types = TREE_CHAIN (arg_types);
268 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
270 /* If this is a subobject constructor or destructor, our caller will
271 pass us a pointer to our VTT. */
272 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
274 parm = build_artificial_parm (fn, vtt_parm_identifier, vtt_parm_type);
276 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
277 DECL_CHAIN (parm) = parms;
278 parms = parm;
280 /* ...and then to TYPE_ARG_TYPES. */
281 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
283 DECL_HAS_VTT_PARM_P (fn) = 1;
286 /* Then add the in-charge parm (before the VTT parm). */
287 parm = build_artificial_parm (fn, in_charge_identifier, integer_type_node);
288 DECL_CHAIN (parm) = parms;
289 parms = parm;
290 arg_types = hash_tree_chain (integer_type_node, arg_types);
292 /* Insert our new parameter(s) into the list. */
293 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
295 /* And rebuild the function type. */
296 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
297 arg_types);
298 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
299 fntype = build_exception_variant (fntype,
300 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
301 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
302 fntype = (cp_build_type_attribute_variant
303 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
304 TREE_TYPE (fn) = fntype;
306 /* Now we've got the in-charge parameter. */
307 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
310 /* Classes overload their constituent function names automatically.
311 When a function name is declared in a record structure,
312 its name is changed to it overloaded name. Since names for
313 constructors and destructors can conflict, we place a leading
314 '$' for destructors.
316 CNAME is the name of the class we are grokking for.
318 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
320 FLAGS contains bits saying what's special about today's
321 arguments. DTOR_FLAG == DESTRUCTOR.
323 If FUNCTION is a destructor, then we must add the `auto-delete' field
324 as a second parameter. There is some hair associated with the fact
325 that we must "declare" this variable in the manner consistent with the
326 way the rest of the arguments were declared.
328 QUALS are the qualifiers for the this pointer. */
330 void
331 grokclassfn (tree ctype, tree function, enum overload_flags flags)
333 tree fn_name = DECL_NAME (function);
335 /* Even within an `extern "C"' block, members get C++ linkage. See
336 [dcl.link] for details. */
337 SET_DECL_LANGUAGE (function, lang_cplusplus);
339 if (fn_name == NULL_TREE)
341 error ("name missing for member function");
342 fn_name = get_identifier ("<anonymous>");
343 DECL_NAME (function) = fn_name;
346 DECL_CONTEXT (function) = ctype;
348 if (flags == DTOR_FLAG)
349 DECL_CXX_DESTRUCTOR_P (function) = 1;
351 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
352 maybe_retrofit_in_chrg (function);
355 /* Create an ARRAY_REF, checking for the user doing things backwards
356 along the way. DECLTYPE_P is for N3276, as in the parser. */
358 tree
359 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
360 bool decltype_p)
362 tree type;
363 tree expr;
364 tree orig_array_expr = array_expr;
365 tree orig_index_exp = index_exp;
366 tree overload = NULL_TREE;
368 if (error_operand_p (array_expr) || error_operand_p (index_exp))
369 return error_mark_node;
371 if (processing_template_decl)
373 if (type_dependent_expression_p (array_expr)
374 || type_dependent_expression_p (index_exp))
375 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
376 NULL_TREE, NULL_TREE);
377 array_expr = build_non_dependent_expr (array_expr);
378 index_exp = build_non_dependent_expr (index_exp);
381 type = TREE_TYPE (array_expr);
382 gcc_assert (type);
383 type = non_reference (type);
385 /* If they have an `operator[]', use that. */
386 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
388 tsubst_flags_t complain = tf_warning_or_error;
389 if (decltype_p)
390 complain |= tf_decltype;
391 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
392 index_exp, NULL_TREE, &overload, complain);
394 else
396 tree p1, p2, i1, i2;
398 /* Otherwise, create an ARRAY_REF for a pointer or array type.
399 It is a little-known fact that, if `a' is an array and `i' is
400 an int, you can write `i[a]', which means the same thing as
401 `a[i]'. */
402 if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
403 p1 = array_expr;
404 else
405 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
407 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
408 p2 = index_exp;
409 else
410 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
412 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
413 false);
414 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
415 false);
417 if ((p1 && i2) && (i1 && p2))
418 error ("ambiguous conversion for array subscript");
420 if (p1 && i2)
421 array_expr = p1, index_exp = i2;
422 else if (i1 && p2)
423 array_expr = p2, index_exp = i1;
424 else
426 error ("invalid types %<%T[%T]%> for array subscript",
427 type, TREE_TYPE (index_exp));
428 return error_mark_node;
431 if (array_expr == error_mark_node || index_exp == error_mark_node)
432 error ("ambiguous conversion for array subscript");
434 if (TREE_CODE (TREE_TYPE (array_expr)) == POINTER_TYPE)
435 array_expr = mark_rvalue_use (array_expr);
436 else
437 array_expr = mark_lvalue_use_nonread (array_expr);
438 index_exp = mark_rvalue_use (index_exp);
439 expr = build_array_ref (input_location, array_expr, index_exp);
441 if (processing_template_decl && expr != error_mark_node)
443 if (overload != NULL_TREE)
444 return (build_min_non_dep_op_overload
445 (ARRAY_REF, expr, overload, orig_array_expr, orig_index_exp));
447 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
448 NULL_TREE, NULL_TREE);
450 return expr;
453 /* Given the cast expression EXP, checking out its validity. Either return
454 an error_mark_node if there was an unavoidable error, return a cast to
455 void for trying to delete a pointer w/ the value 0, or return the
456 call to delete. If DOING_VEC is true, we handle things differently
457 for doing an array delete.
458 Implements ARM $5.3.4. This is called from the parser. */
460 tree
461 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
462 tsubst_flags_t complain)
464 tree t, type;
466 if (exp == error_mark_node)
467 return exp;
469 if (processing_template_decl)
471 t = build_min (DELETE_EXPR, void_type_node, exp, size);
472 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
473 DELETE_EXPR_USE_VEC (t) = doing_vec;
474 TREE_SIDE_EFFECTS (t) = 1;
475 return t;
478 /* An array can't have been allocated by new, so complain. */
479 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
480 warning (0, "deleting array %q#E", exp);
482 t = build_expr_type_conversion (WANT_POINTER, exp, true);
484 if (t == NULL_TREE || t == error_mark_node)
486 error ("type %q#T argument given to %<delete%>, expected pointer",
487 TREE_TYPE (exp));
488 return error_mark_node;
491 type = TREE_TYPE (t);
493 /* As of Valley Forge, you can delete a pointer to const. */
495 /* You can't delete functions. */
496 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
498 error ("cannot delete a function. Only pointer-to-objects are "
499 "valid arguments to %<delete%>");
500 return error_mark_node;
503 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
504 if (VOID_TYPE_P (TREE_TYPE (type)))
506 warning (OPT_Wdelete_incomplete, "deleting %qT is undefined", type);
507 doing_vec = 0;
510 /* Deleting a pointer with the value zero is valid and has no effect. */
511 if (integer_zerop (t))
512 return build1 (NOP_EXPR, void_type_node, t);
514 if (doing_vec)
515 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
516 sfk_deleting_destructor,
517 use_global_delete, complain);
518 else
519 return build_delete (type, t, sfk_deleting_destructor,
520 LOOKUP_NORMAL, use_global_delete,
521 complain);
524 /* Report an error if the indicated template declaration is not the
525 sort of thing that should be a member template. */
527 void
528 check_member_template (tree tmpl)
530 tree decl;
532 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
533 decl = DECL_TEMPLATE_RESULT (tmpl);
535 if (TREE_CODE (decl) == FUNCTION_DECL
536 || DECL_ALIAS_TEMPLATE_P (tmpl)
537 || (TREE_CODE (decl) == TYPE_DECL
538 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
540 /* The parser rejects template declarations in local classes
541 (with the exception of generic lambdas). */
542 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
543 /* The parser rejects any use of virtual in a function template. */
544 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
545 && DECL_VIRTUAL_P (decl)));
547 /* The debug-information generating code doesn't know what to do
548 with member templates. */
549 DECL_IGNORED_P (tmpl) = 1;
551 else if (variable_template_p (tmpl))
552 /* OK */;
553 else
554 error ("template declaration of %q#D", decl);
557 /* Sanity check: report error if this function FUNCTION is not
558 really a member of the class (CTYPE) it is supposed to belong to.
559 TEMPLATE_PARMS is used to specify the template parameters of a member
560 template passed as FUNCTION_DECL. If the member template is passed as a
561 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
562 from the declaration. If the function is not a function template, it
563 must be NULL.
564 It returns the original declaration for the function, NULL_TREE if
565 no declaration was found, error_mark_node if an error was emitted. */
567 tree
568 check_classfn (tree ctype, tree function, tree template_parms)
570 if (DECL_USE_TEMPLATE (function)
571 && !(TREE_CODE (function) == TEMPLATE_DECL
572 && DECL_TEMPLATE_SPECIALIZATION (function))
573 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
574 /* Since this is a specialization of a member template,
575 we're not going to find the declaration in the class.
576 For example, in:
578 struct S { template <typename T> void f(T); };
579 template <> void S::f(int);
581 we're not going to find `S::f(int)', but there's no
582 reason we should, either. We let our callers know we didn't
583 find the method, but we don't complain. */
584 return NULL_TREE;
586 /* Basic sanity check: for a template function, the template parameters
587 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
588 if (TREE_CODE (function) == TEMPLATE_DECL)
590 if (template_parms
591 && !comp_template_parms (template_parms,
592 DECL_TEMPLATE_PARMS (function)))
594 error ("template parameter lists provided don%'t match the "
595 "template parameters of %qD", function);
596 return error_mark_node;
598 template_parms = DECL_TEMPLATE_PARMS (function);
601 /* OK, is this a definition of a member template? */
602 bool is_template = (template_parms != NULL_TREE);
604 /* [temp.mem]
606 A destructor shall not be a member template. */
607 if (DECL_DESTRUCTOR_P (function) && is_template)
609 error ("destructor %qD declared as member template", function);
610 return error_mark_node;
613 /* We must enter the scope here, because conversion operators are
614 named by target type, and type equivalence relies on typenames
615 resolving within the scope of CTYPE. */
616 tree pushed_scope = push_scope (ctype);
617 tree matched = NULL_TREE;
618 tree fns = get_class_binding (ctype, DECL_NAME (function));
620 for (ovl_iterator iter (fns); !matched && iter; ++iter)
622 tree fndecl = *iter;
624 /* A member template definition only matches a member template
625 declaration. */
626 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
627 continue;
629 if (!DECL_DECLARES_FUNCTION_P (fndecl))
630 continue;
632 tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
633 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
635 /* We cannot simply call decls_match because this doesn't work
636 for static member functions that are pretending to be
637 methods, and because the name may have been changed by
638 asm("new_name"). */
640 /* Get rid of the this parameter on functions that become
641 static. */
642 if (DECL_STATIC_FUNCTION_P (fndecl)
643 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
644 p1 = TREE_CHAIN (p1);
646 /* ref-qualifier or absence of same must match. */
647 if (type_memfn_rqual (TREE_TYPE (function))
648 != type_memfn_rqual (TREE_TYPE (fndecl)))
649 continue;
651 // Include constraints in the match.
652 tree c1 = get_constraints (function);
653 tree c2 = get_constraints (fndecl);
655 /* While finding a match, same types and params are not enough
656 if the function is versioned. Also check version ("target")
657 attributes. */
658 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
659 TREE_TYPE (TREE_TYPE (fndecl)))
660 && compparms (p1, p2)
661 && !targetm.target_option.function_versions (function, fndecl)
662 && (!is_template
663 || comp_template_parms (template_parms,
664 DECL_TEMPLATE_PARMS (fndecl)))
665 && equivalent_constraints (c1, c2)
666 && (DECL_TEMPLATE_SPECIALIZATION (function)
667 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
668 && (!DECL_TEMPLATE_SPECIALIZATION (function)
669 || (DECL_TI_TEMPLATE (function) == DECL_TI_TEMPLATE (fndecl))))
670 matched = fndecl;
673 if (!matched)
675 if (!COMPLETE_TYPE_P (ctype))
676 cxx_incomplete_type_error (function, ctype);
677 else
679 if (DECL_CONV_FN_P (function))
680 fns = get_class_binding (ctype, conv_op_identifier);
682 error_at (DECL_SOURCE_LOCATION (function),
683 "no declaration matches %q#D", function);
684 if (fns)
685 print_candidates (fns);
686 else if (DECL_CONV_FN_P (function))
687 inform (DECL_SOURCE_LOCATION (function),
688 "no conversion operators declared");
689 else
690 inform (DECL_SOURCE_LOCATION (function),
691 "no functions named %qD", function);
692 inform (DECL_SOURCE_LOCATION (TYPE_NAME (ctype)),
693 "%#qT defined here", ctype);
695 matched = error_mark_node;
698 if (pushed_scope)
699 pop_scope (pushed_scope);
701 return matched;
704 /* DECL is a function with vague linkage. Remember it so that at the
705 end of the translation unit we can decide whether or not to emit
706 it. */
708 void
709 note_vague_linkage_fn (tree decl)
711 DECL_DEFER_OUTPUT (decl) = 1;
712 vec_safe_push (deferred_fns, decl);
715 /* As above, but for variable template instantiations. */
717 void
718 note_variable_template_instantiation (tree decl)
720 vec_safe_push (pending_statics, decl);
723 /* We have just processed the DECL, which is a static data member.
724 The other parameters are as for cp_finish_decl. */
726 void
727 finish_static_data_member_decl (tree decl,
728 tree init, bool init_const_expr_p,
729 tree asmspec_tree,
730 int flags)
732 DECL_CONTEXT (decl) = current_class_type;
734 /* We cannot call pushdecl here, because that would fill in the
735 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
736 the right thing, namely, to put this decl out straight away. */
738 if (! processing_template_decl)
739 vec_safe_push (pending_statics, decl);
741 if (LOCAL_CLASS_P (current_class_type)
742 /* We already complained about the template definition. */
743 && !DECL_TEMPLATE_INSTANTIATION (decl))
744 permerror (input_location, "local class %q#T shall not have static data member %q#D",
745 current_class_type, decl);
746 else
747 for (tree t = current_class_type; TYPE_P (t);
748 t = CP_TYPE_CONTEXT (t))
749 if (TYPE_UNNAMED_P (t))
751 if (permerror (DECL_SOURCE_LOCATION (decl),
752 "static data member %qD in unnamed class", decl))
753 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
754 "unnamed class defined here");
755 break;
758 DECL_IN_AGGR_P (decl) = 1;
760 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
761 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
762 SET_VAR_HAD_UNKNOWN_BOUND (decl);
764 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
767 /* DECLARATOR and DECLSPECS correspond to a class member. The other
768 parameters are as for cp_finish_decl. Return the DECL for the
769 class member declared. */
771 tree
772 grokfield (const cp_declarator *declarator,
773 cp_decl_specifier_seq *declspecs,
774 tree init, bool init_const_expr_p,
775 tree asmspec_tree,
776 tree attrlist)
778 tree value;
779 const char *asmspec = 0;
780 int flags;
781 tree name;
783 if (init
784 && TREE_CODE (init) == TREE_LIST
785 && TREE_VALUE (init) == error_mark_node
786 && TREE_CHAIN (init) == NULL_TREE)
787 init = NULL_TREE;
789 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
790 if (! value || value == error_mark_node)
791 /* friend or constructor went bad. */
792 return error_mark_node;
793 if (TREE_TYPE (value) == error_mark_node)
794 return value;
796 if (TREE_CODE (value) == TYPE_DECL && init)
798 error ("typedef %qD is initialized (use decltype instead)", value);
799 init = NULL_TREE;
802 /* Pass friendly classes back. */
803 if (value == void_type_node)
804 return value;
807 name = DECL_NAME (value);
809 if (name != NULL_TREE)
811 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
813 error ("explicit template argument list not allowed");
814 return error_mark_node;
817 if (IDENTIFIER_POINTER (name)[0] == '_'
818 && id_equal (name, "_vptr"))
819 error ("member %qD conflicts with virtual function table field name",
820 value);
823 /* Stash away type declarations. */
824 if (TREE_CODE (value) == TYPE_DECL)
826 DECL_NONLOCAL (value) = 1;
827 DECL_CONTEXT (value) = current_class_type;
829 if (attrlist)
831 int attrflags = 0;
833 /* If this is a typedef that names the class for linkage purposes
834 (7.1.3p8), apply any attributes directly to the type. */
835 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
836 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
837 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
839 cplus_decl_attributes (&value, attrlist, attrflags);
842 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
843 && TREE_TYPE (value) != error_mark_node
844 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
845 set_underlying_type (value);
847 /* It's important that push_template_decl below follows
848 set_underlying_type above so that the created template
849 carries the properly set type of VALUE. */
850 if (processing_template_decl)
851 value = push_template_decl (value);
853 record_locally_defined_typedef (value);
854 return value;
857 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
859 if (!friendp && DECL_IN_AGGR_P (value))
861 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
862 return void_type_node;
865 if (asmspec_tree && asmspec_tree != error_mark_node)
866 asmspec = TREE_STRING_POINTER (asmspec_tree);
868 if (init)
870 if (TREE_CODE (value) == FUNCTION_DECL)
872 if (init == ridpointers[(int)RID_DELETE])
874 DECL_DELETED_FN (value) = 1;
875 DECL_DECLARED_INLINE_P (value) = 1;
876 DECL_INITIAL (value) = error_mark_node;
878 else if (init == ridpointers[(int)RID_DEFAULT])
880 if (defaultable_fn_check (value))
882 DECL_DEFAULTED_FN (value) = 1;
883 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
884 DECL_DECLARED_INLINE_P (value) = 1;
887 else if (TREE_CODE (init) == DEFAULT_ARG)
888 error ("invalid initializer for member function %qD", value);
889 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
891 if (integer_zerop (init))
892 DECL_PURE_VIRTUAL_P (value) = 1;
893 else if (error_operand_p (init))
894 ; /* An error has already been reported. */
895 else
896 error ("invalid initializer for member function %qD",
897 value);
899 else
901 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
902 if (friendp)
903 error ("initializer specified for friend function %qD",
904 value);
905 else
906 error ("initializer specified for static member function %qD",
907 value);
910 else if (TREE_CODE (value) == FIELD_DECL)
911 /* C++11 NSDMI, keep going. */;
912 else if (!VAR_P (value))
913 gcc_unreachable ();
916 /* Pass friend decls back. */
917 if ((TREE_CODE (value) == FUNCTION_DECL
918 || TREE_CODE (value) == TEMPLATE_DECL)
919 && DECL_CONTEXT (value) != current_class_type)
920 return value;
922 /* Need to set this before push_template_decl. */
923 if (VAR_P (value))
924 DECL_CONTEXT (value) = current_class_type;
926 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
928 value = push_template_decl (value);
929 if (error_operand_p (value))
930 return error_mark_node;
933 if (attrlist)
934 cplus_decl_attributes (&value, attrlist, 0);
936 if (init && DIRECT_LIST_INIT_P (init))
937 flags = LOOKUP_NORMAL;
938 else
939 flags = LOOKUP_IMPLICIT;
941 switch (TREE_CODE (value))
943 case VAR_DECL:
944 finish_static_data_member_decl (value, init, init_const_expr_p,
945 asmspec_tree, flags);
946 return value;
948 case FIELD_DECL:
949 if (asmspec)
950 error ("%<asm%> specifiers are not permitted on non-static data members");
951 if (DECL_INITIAL (value) == error_mark_node)
952 init = error_mark_node;
953 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
954 NULL_TREE, flags);
955 DECL_IN_AGGR_P (value) = 1;
956 return value;
958 case FUNCTION_DECL:
959 if (asmspec)
960 set_user_assembler_name (value, asmspec);
962 cp_finish_decl (value,
963 /*init=*/NULL_TREE,
964 /*init_const_expr_p=*/false,
965 asmspec_tree, flags);
967 /* Pass friends back this way. */
968 if (DECL_FRIEND_P (value))
969 return void_type_node;
971 DECL_IN_AGGR_P (value) = 1;
972 return value;
974 default:
975 gcc_unreachable ();
977 return NULL_TREE;
980 /* Like `grokfield', but for bitfields.
981 WIDTH is the width of the bitfield, a constant expression.
982 The other parameters are as for grokfield. */
984 tree
985 grokbitfield (const cp_declarator *declarator,
986 cp_decl_specifier_seq *declspecs, tree width, tree init,
987 tree attrlist)
989 tree value = grokdeclarator (declarator, declspecs, BITFIELD,
990 init != NULL_TREE, &attrlist);
992 if (value == error_mark_node)
993 return NULL_TREE; /* friends went bad. */
994 if (TREE_TYPE (value) == error_mark_node)
995 return value;
997 /* Pass friendly classes back. */
998 if (VOID_TYPE_P (value))
999 return void_type_node;
1001 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
1002 && (POINTER_TYPE_P (value)
1003 || !dependent_type_p (TREE_TYPE (value))))
1005 error ("bit-field %qD with non-integral type", value);
1006 return error_mark_node;
1009 if (TREE_CODE (value) == TYPE_DECL)
1011 error ("cannot declare %qD to be a bit-field type", value);
1012 return NULL_TREE;
1015 /* Usually, finish_struct_1 catches bitfields with invalid types.
1016 But, in the case of bitfields with function type, we confuse
1017 ourselves into thinking they are member functions, so we must
1018 check here. */
1019 if (TREE_CODE (value) == FUNCTION_DECL)
1021 error ("cannot declare bit-field %qD with function type",
1022 DECL_NAME (value));
1023 return NULL_TREE;
1026 if (width && TYPE_WARN_IF_NOT_ALIGN (TREE_TYPE (value)))
1028 error ("cannot declare bit-field %qD with %<warn_if_not_aligned%> type",
1029 DECL_NAME (value));
1030 return NULL_TREE;
1033 if (DECL_IN_AGGR_P (value))
1035 error ("%qD is already defined in the class %qT", value,
1036 DECL_CONTEXT (value));
1037 return void_type_node;
1040 if (TREE_STATIC (value))
1042 error ("static member %qD cannot be a bit-field", value);
1043 return NULL_TREE;
1046 int flags = LOOKUP_IMPLICIT;
1047 if (init && DIRECT_LIST_INIT_P (init))
1048 flags = LOOKUP_NORMAL;
1049 cp_finish_decl (value, init, false, NULL_TREE, flags);
1051 if (width != error_mark_node)
1053 /* The width must be an integer type. */
1054 if (!type_dependent_expression_p (width)
1055 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1056 error ("width of bit-field %qD has non-integral type %qT", value,
1057 TREE_TYPE (width));
1058 else
1060 /* Temporarily stash the width in DECL_BIT_FIELD_REPRESENTATIVE.
1061 check_bitfield_decl picks it from there later and sets DECL_SIZE
1062 accordingly. */
1063 DECL_BIT_FIELD_REPRESENTATIVE (value) = width;
1064 SET_DECL_C_BIT_FIELD (value);
1068 DECL_IN_AGGR_P (value) = 1;
1070 if (attrlist)
1071 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1073 return value;
1077 /* Returns true iff ATTR is an attribute which needs to be applied at
1078 instantiation time rather than template definition time. */
1080 static bool
1081 is_late_template_attribute (tree attr, tree decl)
1083 tree name = get_attribute_name (attr);
1084 tree args = TREE_VALUE (attr);
1085 const struct attribute_spec *spec = lookup_attribute_spec (name);
1086 tree arg;
1088 if (!spec)
1089 /* Unknown attribute. */
1090 return false;
1092 /* Attribute weak handling wants to write out assembly right away. */
1093 if (is_attribute_p ("weak", name))
1094 return true;
1096 /* Attributes used and unused are applied directly, as they appertain to
1097 decls. */
1098 if (is_attribute_p ("unused", name)
1099 || is_attribute_p ("used", name))
1100 return false;
1102 /* Attribute tls_model wants to modify the symtab. */
1103 if (is_attribute_p ("tls_model", name))
1104 return true;
1106 /* #pragma omp declare simd attribute needs to be always deferred. */
1107 if (flag_openmp
1108 && is_attribute_p ("omp declare simd", name))
1109 return true;
1111 /* An attribute pack is clearly dependent. */
1112 if (args && PACK_EXPANSION_P (args))
1113 return true;
1115 /* If any of the arguments are dependent expressions, we can't evaluate
1116 the attribute until instantiation time. */
1117 for (arg = args; arg; arg = TREE_CHAIN (arg))
1119 tree t = TREE_VALUE (arg);
1121 /* If the first attribute argument is an identifier, only consider
1122 second and following arguments. Attributes like mode, format,
1123 cleanup and several target specific attributes aren't late
1124 just because they have an IDENTIFIER_NODE as first argument. */
1125 if (arg == args && attribute_takes_identifier_p (name)
1126 && identifier_p (t))
1127 continue;
1129 if (value_dependent_expression_p (t)
1130 || type_dependent_expression_p (t))
1131 return true;
1134 if (TREE_CODE (decl) == TYPE_DECL
1135 || TYPE_P (decl)
1136 || spec->type_required)
1138 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1140 /* We can't apply any attributes to a completely unknown type until
1141 instantiation time. */
1142 enum tree_code code = TREE_CODE (type);
1143 if (code == TEMPLATE_TYPE_PARM
1144 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1145 || code == TYPENAME_TYPE)
1146 return true;
1147 /* Also defer most attributes on dependent types. This is not
1148 necessary in all cases, but is the better default. */
1149 else if (dependent_type_p (type)
1150 /* But some attributes specifically apply to templates. */
1151 && !is_attribute_p ("abi_tag", name)
1152 && !is_attribute_p ("deprecated", name)
1153 && !is_attribute_p ("visibility", name))
1154 return true;
1155 else
1156 return false;
1158 else
1159 return false;
1162 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1163 applied at instantiation time and return them. If IS_DEPENDENT is true,
1164 the declaration itself is dependent, so all attributes should be applied
1165 at instantiation time. */
1167 static tree
1168 splice_template_attributes (tree *attr_p, tree decl)
1170 tree *p = attr_p;
1171 tree late_attrs = NULL_TREE;
1172 tree *q = &late_attrs;
1174 if (!p)
1175 return NULL_TREE;
1177 for (; *p; )
1179 if (is_late_template_attribute (*p, decl))
1181 ATTR_IS_DEPENDENT (*p) = 1;
1182 *q = *p;
1183 *p = TREE_CHAIN (*p);
1184 q = &TREE_CHAIN (*q);
1185 *q = NULL_TREE;
1187 else
1188 p = &TREE_CHAIN (*p);
1191 return late_attrs;
1194 /* Remove any late attributes from the list in ATTR_P and attach them to
1195 DECL_P. */
1197 static void
1198 save_template_attributes (tree *attr_p, tree *decl_p)
1200 tree *q;
1202 if (attr_p && *attr_p == error_mark_node)
1203 return;
1205 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1206 if (!late_attrs)
1207 return;
1209 if (DECL_P (*decl_p))
1210 q = &DECL_ATTRIBUTES (*decl_p);
1211 else
1212 q = &TYPE_ATTRIBUTES (*decl_p);
1214 tree old_attrs = *q;
1216 /* Merge the late attributes at the beginning with the attribute
1217 list. */
1218 late_attrs = merge_attributes (late_attrs, *q);
1219 *q = late_attrs;
1221 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1223 /* We've added new attributes directly to the main variant, so
1224 now we need to update all of the other variants to include
1225 these new attributes. */
1226 tree variant;
1227 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1228 variant = TYPE_NEXT_VARIANT (variant))
1230 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1231 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1236 /* True if ATTRS contains any dependent attributes that affect type
1237 identity. */
1239 bool
1240 any_dependent_type_attributes_p (tree attrs)
1242 for (tree a = attrs; a; a = TREE_CHAIN (a))
1243 if (ATTR_IS_DEPENDENT (a))
1245 const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (a));
1246 if (as && as->affects_type_identity)
1247 return true;
1249 return false;
1252 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1253 to a typedef which gives a previously unnamed class or enum a name for
1254 linkage purposes. */
1256 bool
1257 attributes_naming_typedef_ok (tree attrs)
1259 for (; attrs; attrs = TREE_CHAIN (attrs))
1261 tree name = get_attribute_name (attrs);
1262 if (is_attribute_p ("vector_size", name))
1263 return false;
1265 return true;
1268 /* Like reconstruct_complex_type, but handle also template trees. */
1270 tree
1271 cp_reconstruct_complex_type (tree type, tree bottom)
1273 tree inner, outer;
1274 bool late_return_type_p = false;
1276 if (TYPE_PTR_P (type))
1278 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1279 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1280 TYPE_REF_CAN_ALIAS_ALL (type));
1282 else if (TREE_CODE (type) == REFERENCE_TYPE)
1284 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1285 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1286 TYPE_REF_CAN_ALIAS_ALL (type));
1288 else if (TREE_CODE (type) == ARRAY_TYPE)
1290 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1291 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1292 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1293 element type qualification will be handled by the recursive
1294 cp_reconstruct_complex_type call and cp_build_qualified_type
1295 for ARRAY_TYPEs changes the element type. */
1296 return outer;
1298 else if (TREE_CODE (type) == FUNCTION_TYPE)
1300 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1301 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1302 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1303 outer = apply_memfn_quals (outer,
1304 type_memfn_quals (type),
1305 type_memfn_rqual (type));
1307 else if (TREE_CODE (type) == METHOD_TYPE)
1309 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1310 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1311 /* The build_method_type_directly() routine prepends 'this' to argument list,
1312 so we must compensate by getting rid of it. */
1313 outer
1314 = build_method_type_directly
1315 (class_of_this_parm (type), inner,
1316 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1318 else if (TREE_CODE (type) == OFFSET_TYPE)
1320 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1321 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1323 else
1324 return bottom;
1326 if (TYPE_ATTRIBUTES (type))
1327 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1328 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1330 if (late_return_type_p)
1331 TYPE_HAS_LATE_RETURN_TYPE (outer) = 1;
1333 return outer;
1336 /* Replaces any constexpr expression that may be into the attributes
1337 arguments with their reduced value. */
1339 static void
1340 cp_check_const_attributes (tree attributes)
1342 if (attributes == error_mark_node)
1343 return;
1345 tree attr;
1346 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1348 tree arg;
1349 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1351 tree expr = TREE_VALUE (arg);
1352 if (EXPR_P (expr))
1353 TREE_VALUE (arg) = maybe_constant_value (expr);
1358 /* Return true if TYPE is an OpenMP mappable type. */
1359 bool
1360 cp_omp_mappable_type (tree type)
1362 /* Mappable type has to be complete. */
1363 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1364 return false;
1365 /* Arrays have mappable type if the elements have mappable type. */
1366 while (TREE_CODE (type) == ARRAY_TYPE)
1367 type = TREE_TYPE (type);
1368 /* A mappable type cannot contain virtual members. */
1369 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1370 return false;
1371 /* All data members must be non-static. */
1372 if (CLASS_TYPE_P (type))
1374 tree field;
1375 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1376 if (VAR_P (field))
1377 return false;
1378 /* All fields must have mappable types. */
1379 else if (TREE_CODE (field) == FIELD_DECL
1380 && !cp_omp_mappable_type (TREE_TYPE (field)))
1381 return false;
1383 return true;
1386 /* Like decl_attributes, but handle C++ complexity. */
1388 void
1389 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1391 if (*decl == NULL_TREE || *decl == void_type_node
1392 || *decl == error_mark_node)
1393 return;
1395 /* Add implicit "omp declare target" attribute if requested. */
1396 if (scope_chain->omp_declare_target_attribute
1397 && ((VAR_P (*decl)
1398 && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
1399 || TREE_CODE (*decl) == FUNCTION_DECL))
1401 if (VAR_P (*decl)
1402 && DECL_CLASS_SCOPE_P (*decl))
1403 error ("%q+D static data member inside of declare target directive",
1404 *decl);
1405 else if (!processing_template_decl
1406 && VAR_P (*decl)
1407 && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1408 error ("%q+D in declare target directive does not have mappable type",
1409 *decl);
1410 else
1411 attributes = tree_cons (get_identifier ("omp declare target"),
1412 NULL_TREE, attributes);
1415 if (processing_template_decl)
1417 if (check_for_bare_parameter_packs (attributes))
1418 return;
1420 save_template_attributes (&attributes, decl);
1423 cp_check_const_attributes (attributes);
1425 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1426 decl = &DECL_TEMPLATE_RESULT (*decl);
1428 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1430 attributes
1431 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1432 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1433 attributes, flags);
1435 else
1436 decl_attributes (decl, attributes, flags);
1438 if (TREE_CODE (*decl) == TYPE_DECL)
1439 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1441 /* Propagate deprecation out to the template. */
1442 if (TREE_DEPRECATED (*decl))
1443 if (tree ti = get_template_info (*decl))
1445 tree tmpl = TI_TEMPLATE (ti);
1446 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1447 : DECL_TEMPLATE_RESULT (tmpl));
1448 if (*decl == pattern)
1449 TREE_DEPRECATED (tmpl) = true;
1453 /* Walks through the namespace- or function-scope anonymous union
1454 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1455 Returns one of the fields for use in the mangled name. */
1457 static tree
1458 build_anon_union_vars (tree type, tree object)
1460 tree main_decl = NULL_TREE;
1461 tree field;
1463 /* Rather than write the code to handle the non-union case,
1464 just give an error. */
1465 if (TREE_CODE (type) != UNION_TYPE)
1467 error ("anonymous struct not inside named type");
1468 return error_mark_node;
1471 for (field = TYPE_FIELDS (type);
1472 field != NULL_TREE;
1473 field = DECL_CHAIN (field))
1475 tree decl;
1476 tree ref;
1478 if (DECL_ARTIFICIAL (field))
1479 continue;
1480 if (TREE_CODE (field) != FIELD_DECL)
1482 permerror (DECL_SOURCE_LOCATION (field),
1483 "%q#D invalid; an anonymous union can only "
1484 "have non-static data members", field);
1485 continue;
1488 if (TREE_PRIVATE (field))
1489 permerror (DECL_SOURCE_LOCATION (field),
1490 "private member %q#D in anonymous union", field);
1491 else if (TREE_PROTECTED (field))
1492 permerror (DECL_SOURCE_LOCATION (field),
1493 "protected member %q#D in anonymous union", field);
1495 if (processing_template_decl)
1496 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1497 DECL_NAME (field), NULL_TREE);
1498 else
1499 ref = build_class_member_access_expr (object, field, NULL_TREE,
1500 false, tf_warning_or_error);
1502 if (DECL_NAME (field))
1504 tree base;
1506 decl = build_decl (input_location,
1507 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1508 DECL_ANON_UNION_VAR_P (decl) = 1;
1509 DECL_ARTIFICIAL (decl) = 1;
1511 base = get_base_address (object);
1512 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1513 TREE_STATIC (decl) = TREE_STATIC (base);
1514 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1516 SET_DECL_VALUE_EXPR (decl, ref);
1517 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1519 decl = pushdecl (decl);
1521 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1522 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1523 else
1524 decl = 0;
1526 if (main_decl == NULL_TREE)
1527 main_decl = decl;
1530 return main_decl;
1533 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1534 anonymous union, then all members must be laid out together. PUBLIC_P
1535 is nonzero if this union is not declared static. */
1537 void
1538 finish_anon_union (tree anon_union_decl)
1540 tree type;
1541 tree main_decl;
1542 bool public_p;
1544 if (anon_union_decl == error_mark_node)
1545 return;
1547 type = TREE_TYPE (anon_union_decl);
1548 public_p = TREE_PUBLIC (anon_union_decl);
1550 /* The VAR_DECL's context is the same as the TYPE's context. */
1551 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1553 if (TYPE_FIELDS (type) == NULL_TREE)
1554 return;
1556 if (public_p)
1558 error ("namespace-scope anonymous aggregates must be static");
1559 return;
1562 main_decl = build_anon_union_vars (type, anon_union_decl);
1563 if (main_decl == error_mark_node)
1564 return;
1565 if (main_decl == NULL_TREE)
1567 pedwarn (input_location, 0, "anonymous union with no members");
1568 return;
1571 if (!processing_template_decl)
1573 /* Use main_decl to set the mangled name. */
1574 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1575 maybe_commonize_var (anon_union_decl);
1576 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1577 mangle_decl (anon_union_decl);
1578 DECL_NAME (anon_union_decl) = NULL_TREE;
1581 pushdecl (anon_union_decl);
1582 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1585 /* Auxiliary functions to make type signatures for
1586 `operator new' and `operator delete' correspond to
1587 what compiler will be expecting. */
1589 tree
1590 coerce_new_type (tree type)
1592 int e = 0;
1593 tree args = TYPE_ARG_TYPES (type);
1595 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1597 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1599 e = 1;
1600 error ("%<operator new%> must return type %qT", ptr_type_node);
1603 if (args && args != void_list_node)
1605 if (TREE_PURPOSE (args))
1607 /* [basic.stc.dynamic.allocation]
1609 The first parameter shall not have an associated default
1610 argument. */
1611 error ("the first parameter of %<operator new%> cannot "
1612 "have a default argument");
1613 /* Throw away the default argument. */
1614 TREE_PURPOSE (args) = NULL_TREE;
1617 if (!same_type_p (TREE_VALUE (args), size_type_node))
1619 e = 2;
1620 args = TREE_CHAIN (args);
1623 else
1624 e = 2;
1626 if (e == 2)
1627 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1628 "as first parameter", size_type_node);
1630 switch (e)
1632 case 2:
1633 args = tree_cons (NULL_TREE, size_type_node, args);
1634 /* Fall through. */
1635 case 1:
1636 type = build_exception_variant
1637 (build_function_type (ptr_type_node, args),
1638 TYPE_RAISES_EXCEPTIONS (type));
1639 /* Fall through. */
1640 default:;
1642 return type;
1645 tree
1646 coerce_delete_type (tree type)
1648 int e = 0;
1649 tree args = TYPE_ARG_TYPES (type);
1651 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1653 if (!same_type_p (TREE_TYPE (type), void_type_node))
1655 e = 1;
1656 error ("%<operator delete%> must return type %qT", void_type_node);
1659 if (!args || args == void_list_node
1660 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1662 e = 2;
1663 if (args && args != void_list_node)
1664 args = TREE_CHAIN (args);
1665 error ("%<operator delete%> takes type %qT as first parameter",
1666 ptr_type_node);
1668 switch (e)
1670 case 2:
1671 args = tree_cons (NULL_TREE, ptr_type_node, args);
1672 /* Fall through. */
1673 case 1:
1674 type = build_exception_variant
1675 (build_function_type (void_type_node, args),
1676 TYPE_RAISES_EXCEPTIONS (type));
1677 /* Fall through. */
1678 default:;
1681 return type;
1684 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1685 and mark them as needed. */
1687 static void
1688 mark_vtable_entries (tree decl)
1690 tree fnaddr;
1691 unsigned HOST_WIDE_INT idx;
1693 /* It's OK for the vtable to refer to deprecated virtual functions. */
1694 warning_sentinel w(warn_deprecated_decl);
1696 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1697 idx, fnaddr)
1699 tree fn;
1701 STRIP_NOPS (fnaddr);
1703 if (TREE_CODE (fnaddr) != ADDR_EXPR
1704 && TREE_CODE (fnaddr) != FDESC_EXPR)
1705 /* This entry is an offset: a virtual base class offset, a
1706 virtual call offset, an RTTI offset, etc. */
1707 continue;
1709 fn = TREE_OPERAND (fnaddr, 0);
1710 TREE_ADDRESSABLE (fn) = 1;
1711 /* When we don't have vcall offsets, we output thunks whenever
1712 we output the vtables that contain them. With vcall offsets,
1713 we know all the thunks we'll need when we emit a virtual
1714 function, so we emit the thunks there instead. */
1715 if (DECL_THUNK_P (fn))
1716 use_thunk (fn, /*emit_p=*/0);
1717 mark_used (fn);
1721 /* Set DECL up to have the closest approximation of "initialized common"
1722 linkage available. */
1724 void
1725 comdat_linkage (tree decl)
1727 if (flag_weak)
1728 make_decl_one_only (decl, cxx_comdat_group (decl));
1729 else if (TREE_CODE (decl) == FUNCTION_DECL
1730 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1731 /* We can just emit function and compiler-generated variables
1732 statically; having multiple copies is (for the most part) only
1733 a waste of space.
1735 There are two correctness issues, however: the address of a
1736 template instantiation with external linkage should be the
1737 same, independent of what translation unit asks for the
1738 address, and this will not hold when we emit multiple copies of
1739 the function. However, there's little else we can do.
1741 Also, by default, the typeinfo implementation assumes that
1742 there will be only one copy of the string used as the name for
1743 each type. Therefore, if weak symbols are unavailable, the
1744 run-time library should perform a more conservative check; it
1745 should perform a string comparison, rather than an address
1746 comparison. */
1747 TREE_PUBLIC (decl) = 0;
1748 else
1750 /* Static data member template instantiations, however, cannot
1751 have multiple copies. */
1752 if (DECL_INITIAL (decl) == 0
1753 || DECL_INITIAL (decl) == error_mark_node)
1754 DECL_COMMON (decl) = 1;
1755 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1757 DECL_COMMON (decl) = 1;
1758 DECL_INITIAL (decl) = error_mark_node;
1760 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1762 /* We can't do anything useful; leave vars for explicit
1763 instantiation. */
1764 DECL_EXTERNAL (decl) = 1;
1765 DECL_NOT_REALLY_EXTERN (decl) = 0;
1769 if (TREE_PUBLIC (decl))
1770 DECL_COMDAT (decl) = 1;
1773 /* For win32 we also want to put explicit instantiations in
1774 linkonce sections, so that they will be merged with implicit
1775 instantiations; otherwise we get duplicate symbol errors.
1776 For Darwin we do not want explicit instantiations to be
1777 linkonce. */
1779 void
1780 maybe_make_one_only (tree decl)
1782 /* We used to say that this was not necessary on targets that support weak
1783 symbols, because the implicit instantiations will defer to the explicit
1784 one. However, that's not actually the case in SVR4; a strong definition
1785 after a weak one is an error. Also, not making explicit
1786 instantiations one_only means that we can end up with two copies of
1787 some template instantiations. */
1788 if (! flag_weak)
1789 return;
1791 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1792 we can get away with not emitting them if they aren't used. We need
1793 to for variables so that cp_finish_decl will update their linkage,
1794 because their DECL_INITIAL may not have been set properly yet. */
1796 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1797 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1798 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1800 make_decl_one_only (decl, cxx_comdat_group (decl));
1802 if (VAR_P (decl))
1804 varpool_node *node = varpool_node::get_create (decl);
1805 DECL_COMDAT (decl) = 1;
1806 /* Mark it needed so we don't forget to emit it. */
1807 node->forced_by_abi = true;
1808 TREE_USED (decl) = 1;
1813 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1814 This predicate will give the right answer during parsing of the
1815 function, which other tests may not. */
1817 bool
1818 vague_linkage_p (tree decl)
1820 if (!TREE_PUBLIC (decl))
1822 /* maybe_thunk_body clears TREE_PUBLIC on the maybe-in-charge 'tor
1823 variants, check one of the "clones" for the real linkage. */
1824 if ((DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
1825 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl))
1826 && DECL_CHAIN (decl)
1827 && DECL_CLONED_FUNCTION (DECL_CHAIN (decl)))
1828 return vague_linkage_p (DECL_CHAIN (decl));
1830 gcc_checking_assert (!DECL_COMDAT (decl));
1831 return false;
1833 /* Unfortunately, import_export_decl has not always been called
1834 before the function is processed, so we cannot simply check
1835 DECL_COMDAT. */
1836 if (DECL_COMDAT (decl)
1837 || (TREE_CODE (decl) == FUNCTION_DECL
1838 && DECL_DECLARED_INLINE_P (decl))
1839 || (DECL_LANG_SPECIFIC (decl)
1840 && DECL_TEMPLATE_INSTANTIATION (decl))
1841 || (VAR_P (decl) && DECL_INLINE_VAR_P (decl)))
1842 return true;
1843 else if (DECL_FUNCTION_SCOPE_P (decl))
1844 /* A local static in an inline effectively has vague linkage. */
1845 return (TREE_STATIC (decl)
1846 && vague_linkage_p (DECL_CONTEXT (decl)));
1847 else
1848 return false;
1851 /* Determine whether or not we want to specifically import or export CTYPE,
1852 using various heuristics. */
1854 static void
1855 import_export_class (tree ctype)
1857 /* -1 for imported, 1 for exported. */
1858 int import_export = 0;
1860 /* It only makes sense to call this function at EOF. The reason is
1861 that this function looks at whether or not the first non-inline
1862 non-abstract virtual member function has been defined in this
1863 translation unit. But, we can't possibly know that until we've
1864 seen the entire translation unit. */
1865 gcc_assert (at_eof);
1867 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1868 return;
1870 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1871 we will have CLASSTYPE_INTERFACE_ONLY set but not
1872 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1873 heuristic because someone will supply a #pragma implementation
1874 elsewhere, and deducing it here would produce a conflict. */
1875 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1876 return;
1878 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1879 import_export = -1;
1880 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1881 import_export = 1;
1882 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1883 && !flag_implicit_templates)
1884 /* For a template class, without -fimplicit-templates, check the
1885 repository. If the virtual table is assigned to this
1886 translation unit, then export the class; otherwise, import
1887 it. */
1888 import_export = repo_export_class_p (ctype) ? 1 : -1;
1889 else if (TYPE_POLYMORPHIC_P (ctype))
1891 /* The ABI specifies that the virtual table and associated
1892 information are emitted with the key method, if any. */
1893 tree method = CLASSTYPE_KEY_METHOD (ctype);
1894 /* If weak symbol support is not available, then we must be
1895 careful not to emit the vtable when the key function is
1896 inline. An inline function can be defined in multiple
1897 translation units. If we were to emit the vtable in each
1898 translation unit containing a definition, we would get
1899 multiple definition errors at link-time. */
1900 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1901 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1904 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1905 a definition anywhere else. */
1906 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1907 import_export = 0;
1909 /* Allow back ends the chance to overrule the decision. */
1910 if (targetm.cxx.import_export_class)
1911 import_export = targetm.cxx.import_export_class (ctype, import_export);
1913 if (import_export)
1915 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1916 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1920 /* Return true if VAR has already been provided to the back end; in that
1921 case VAR should not be modified further by the front end. */
1922 static bool
1923 var_finalized_p (tree var)
1925 return varpool_node::get_create (var)->definition;
1928 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1929 must be emitted in this translation unit. Mark it as such. */
1931 void
1932 mark_needed (tree decl)
1934 TREE_USED (decl) = 1;
1935 if (TREE_CODE (decl) == FUNCTION_DECL)
1937 /* Extern inline functions don't become needed when referenced.
1938 If we know a method will be emitted in other TU and no new
1939 functions can be marked reachable, just use the external
1940 definition. */
1941 struct cgraph_node *node = cgraph_node::get_create (decl);
1942 node->forced_by_abi = true;
1944 /* #pragma interface and -frepo code can call mark_needed for
1945 maybe-in-charge 'tors; mark the clones as well. */
1946 tree clone;
1947 FOR_EACH_CLONE (clone, decl)
1948 mark_needed (clone);
1950 else if (VAR_P (decl))
1952 varpool_node *node = varpool_node::get_create (decl);
1953 /* C++ frontend use mark_decl_references to force COMDAT variables
1954 to be output that might appear dead otherwise. */
1955 node->forced_by_abi = true;
1959 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
1960 returns true if a definition of this entity should be provided in
1961 this object file. Callers use this function to determine whether
1962 or not to let the back end know that a definition of DECL is
1963 available in this translation unit. */
1965 bool
1966 decl_needed_p (tree decl)
1968 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
1969 /* This function should only be called at the end of the translation
1970 unit. We cannot be sure of whether or not something will be
1971 COMDAT until that point. */
1972 gcc_assert (at_eof);
1974 /* All entities with external linkage that are not COMDAT/EXTERN should be
1975 emitted; they may be referred to from other object files. */
1976 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
1977 return true;
1978 /* Functions marked "dllexport" must be emitted so that they are
1979 visible to other DLLs. */
1980 if (flag_keep_inline_dllexport
1981 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
1982 return true;
1984 /* When not optimizing, do not bother to produce definitions for extern
1985 symbols. */
1986 if (DECL_REALLY_EXTERN (decl)
1987 && ((TREE_CODE (decl) != FUNCTION_DECL
1988 && !optimize)
1989 || (TREE_CODE (decl) == FUNCTION_DECL
1990 && !opt_for_fn (decl, optimize)))
1991 && !lookup_attribute ("always_inline", decl))
1992 return false;
1994 /* If this entity was used, let the back end see it; it will decide
1995 whether or not to emit it into the object file. */
1996 if (TREE_USED (decl))
1997 return true;
1999 /* Virtual functions might be needed for devirtualization. */
2000 if (flag_devirtualize
2001 && TREE_CODE (decl) == FUNCTION_DECL
2002 && DECL_VIRTUAL_P (decl))
2003 return true;
2005 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2006 reference to DECL might cause it to be emitted later. */
2007 return false;
2010 /* If necessary, write out the vtables for the dynamic class CTYPE.
2011 Returns true if any vtables were emitted. */
2013 static bool
2014 maybe_emit_vtables (tree ctype)
2016 tree vtbl;
2017 tree primary_vtbl;
2018 int needed = 0;
2019 varpool_node *current = NULL, *last = NULL;
2021 /* If the vtables for this class have already been emitted there is
2022 nothing more to do. */
2023 primary_vtbl = CLASSTYPE_VTABLES (ctype);
2024 if (var_finalized_p (primary_vtbl))
2025 return false;
2026 /* Ignore dummy vtables made by get_vtable_decl. */
2027 if (TREE_TYPE (primary_vtbl) == void_type_node)
2028 return false;
2030 /* On some targets, we cannot determine the key method until the end
2031 of the translation unit -- which is when this function is
2032 called. */
2033 if (!targetm.cxx.key_method_may_be_inline ())
2034 determine_key_method (ctype);
2036 /* See if any of the vtables are needed. */
2037 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2039 import_export_decl (vtbl);
2040 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2041 needed = 1;
2043 if (!needed)
2045 /* If the references to this class' vtables are optimized away,
2046 still emit the appropriate debugging information. See
2047 dfs_debug_mark. */
2048 if (DECL_COMDAT (primary_vtbl)
2049 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2050 note_debug_info_needed (ctype);
2051 return false;
2054 /* The ABI requires that we emit all of the vtables if we emit any
2055 of them. */
2056 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2058 /* Mark entities references from the virtual table as used. */
2059 mark_vtable_entries (vtbl);
2061 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2063 vec<tree, va_gc> *cleanups = NULL;
2064 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2065 LOOKUP_NORMAL);
2067 /* It had better be all done at compile-time. */
2068 gcc_assert (!expr && !cleanups);
2071 /* Write it out. */
2072 DECL_EXTERNAL (vtbl) = 0;
2073 rest_of_decl_compilation (vtbl, 1, 1);
2075 /* Because we're only doing syntax-checking, we'll never end up
2076 actually marking the variable as written. */
2077 if (flag_syntax_only)
2078 TREE_ASM_WRITTEN (vtbl) = 1;
2079 else if (DECL_ONE_ONLY (vtbl))
2081 current = varpool_node::get_create (vtbl);
2082 if (last)
2083 current->add_to_same_comdat_group (last);
2084 last = current;
2088 /* Since we're writing out the vtable here, also write the debug
2089 info. */
2090 note_debug_info_needed (ctype);
2092 return true;
2095 /* A special return value from type_visibility meaning internal
2096 linkage. */
2098 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2100 /* walk_tree helper function for type_visibility. */
2102 static tree
2103 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2105 int *vis_p = (int *)data;
2106 if (! TYPE_P (*tp))
2108 *walk_subtrees = 0;
2110 else if (OVERLOAD_TYPE_P (*tp)
2111 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2113 *vis_p = VISIBILITY_ANON;
2114 return *tp;
2116 else if (CLASS_TYPE_P (*tp)
2117 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2118 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2119 return NULL;
2122 /* Returns the visibility of TYPE, which is the minimum visibility of its
2123 component types. */
2125 static int
2126 type_visibility (tree type)
2128 int vis = VISIBILITY_DEFAULT;
2129 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2130 return vis;
2133 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2134 specified (or if VISIBILITY is static). If TMPL is true, this
2135 constraint is for a template argument, and takes precedence
2136 over explicitly-specified visibility on the template. */
2138 static void
2139 constrain_visibility (tree decl, int visibility, bool tmpl)
2141 if (visibility == VISIBILITY_ANON)
2143 /* extern "C" declarations aren't affected by the anonymous
2144 namespace. */
2145 if (!DECL_EXTERN_C_P (decl))
2147 TREE_PUBLIC (decl) = 0;
2148 DECL_WEAK (decl) = 0;
2149 DECL_COMMON (decl) = 0;
2150 DECL_COMDAT (decl) = false;
2151 if (VAR_OR_FUNCTION_DECL_P (decl))
2153 struct symtab_node *snode = symtab_node::get (decl);
2155 if (snode)
2156 snode->set_comdat_group (NULL);
2158 DECL_INTERFACE_KNOWN (decl) = 1;
2159 if (DECL_LANG_SPECIFIC (decl))
2160 DECL_NOT_REALLY_EXTERN (decl) = 1;
2163 else if (visibility > DECL_VISIBILITY (decl)
2164 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2166 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2167 /* This visibility was not specified. */
2168 DECL_VISIBILITY_SPECIFIED (decl) = false;
2172 /* Constrain the visibility of DECL based on the visibility of its template
2173 arguments. */
2175 static void
2176 constrain_visibility_for_template (tree decl, tree targs)
2178 /* If this is a template instantiation, check the innermost
2179 template args for visibility constraints. The outer template
2180 args are covered by the class check. */
2181 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2182 int i;
2183 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2185 int vis = 0;
2187 tree arg = TREE_VEC_ELT (args, i-1);
2188 if (TYPE_P (arg))
2189 vis = type_visibility (arg);
2190 else
2192 if (REFERENCE_REF_P (arg))
2193 arg = TREE_OPERAND (arg, 0);
2194 if (TREE_TYPE (arg))
2195 STRIP_NOPS (arg);
2196 if (TREE_CODE (arg) == ADDR_EXPR)
2197 arg = TREE_OPERAND (arg, 0);
2198 if (VAR_OR_FUNCTION_DECL_P (arg))
2200 if (! TREE_PUBLIC (arg))
2201 vis = VISIBILITY_ANON;
2202 else
2203 vis = DECL_VISIBILITY (arg);
2206 if (vis)
2207 constrain_visibility (decl, vis, true);
2211 /* Like c_determine_visibility, but with additional C++-specific
2212 behavior.
2214 Function-scope entities can rely on the function's visibility because
2215 it is set in start_preparsed_function.
2217 Class-scope entities cannot rely on the class's visibility until the end
2218 of the enclosing class definition.
2220 Note that because namespaces have multiple independent definitions,
2221 namespace visibility is handled elsewhere using the #pragma visibility
2222 machinery rather than by decorating the namespace declaration.
2224 The goal is for constraints from the type to give a diagnostic, and
2225 other constraints to be applied silently. */
2227 void
2228 determine_visibility (tree decl)
2230 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2232 /* Only relevant for names with external linkage. */
2233 if (!TREE_PUBLIC (decl))
2234 return;
2236 /* Cloned constructors and destructors get the same visibility as
2237 the underlying function. That should be set up in
2238 maybe_clone_body. */
2239 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2241 bool orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2242 enum symbol_visibility orig_visibility = DECL_VISIBILITY (decl);
2244 /* The decl may be a template instantiation, which could influence
2245 visibilty. */
2246 tree template_decl = NULL_TREE;
2247 if (TREE_CODE (decl) == TYPE_DECL)
2249 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2251 if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
2252 template_decl = decl;
2254 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2255 template_decl = decl;
2257 else if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
2258 template_decl = decl;
2260 /* If DECL is a member of a class, visibility specifiers on the
2261 class can influence the visibility of the DECL. */
2262 tree class_type = NULL_TREE;
2263 if (DECL_CLASS_SCOPE_P (decl))
2264 class_type = DECL_CONTEXT (decl);
2265 else
2267 /* Not a class member. */
2269 /* Virtual tables have DECL_CONTEXT set to their associated class,
2270 so they are automatically handled above. */
2271 gcc_assert (!VAR_P (decl)
2272 || !DECL_VTABLE_OR_VTT_P (decl));
2274 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2276 /* Local statics and classes get the visibility of their
2277 containing function by default, except that
2278 -fvisibility-inlines-hidden doesn't affect them. */
2279 tree fn = DECL_CONTEXT (decl);
2280 if (DECL_VISIBILITY_SPECIFIED (fn))
2282 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2283 DECL_VISIBILITY_SPECIFIED (decl) =
2284 DECL_VISIBILITY_SPECIFIED (fn);
2286 else
2288 if (DECL_CLASS_SCOPE_P (fn))
2289 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2290 else if (determine_hidden_inline (fn))
2292 DECL_VISIBILITY (decl) = default_visibility;
2293 DECL_VISIBILITY_SPECIFIED (decl) =
2294 visibility_options.inpragma;
2296 else
2298 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2299 DECL_VISIBILITY_SPECIFIED (decl) =
2300 DECL_VISIBILITY_SPECIFIED (fn);
2304 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2305 but have no TEMPLATE_INFO. Their containing template
2306 function does, and the local class could be constrained
2307 by that. */
2308 if (template_decl)
2309 template_decl = fn;
2311 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2312 && flag_visibility_ms_compat)
2314 /* Under -fvisibility-ms-compat, types are visible by default,
2315 even though their contents aren't. */
2316 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2317 int underlying_vis = type_visibility (underlying_type);
2318 if (underlying_vis == VISIBILITY_ANON
2319 || (CLASS_TYPE_P (underlying_type)
2320 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2321 constrain_visibility (decl, underlying_vis, false);
2322 else
2323 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2325 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2327 /* tinfo visibility is based on the type it's for. */
2328 constrain_visibility
2329 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2331 /* Give the target a chance to override the visibility associated
2332 with DECL. */
2333 if (TREE_PUBLIC (decl)
2334 && !DECL_REALLY_EXTERN (decl)
2335 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2336 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2337 targetm.cxx.determine_class_data_visibility (decl);
2339 else if (template_decl)
2340 /* Template instantiations and specializations get visibility based
2341 on their template unless they override it with an attribute. */;
2342 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2344 if (determine_hidden_inline (decl))
2345 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2346 else
2348 /* Set default visibility to whatever the user supplied with
2349 #pragma GCC visibility or a namespace visibility attribute. */
2350 DECL_VISIBILITY (decl) = default_visibility;
2351 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2356 if (template_decl)
2358 /* If the specialization doesn't specify visibility, use the
2359 visibility from the template. */
2360 tree tinfo = get_template_info (template_decl);
2361 tree args = TI_ARGS (tinfo);
2362 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2363 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2364 : DECL_ATTRIBUTES (decl));
2366 if (args != error_mark_node)
2368 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2370 if (!DECL_VISIBILITY_SPECIFIED (decl))
2372 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2373 && determine_hidden_inline (decl))
2374 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2375 else
2377 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2378 DECL_VISIBILITY_SPECIFIED (decl)
2379 = DECL_VISIBILITY_SPECIFIED (pattern);
2383 if (args
2384 /* Template argument visibility outweighs #pragma or namespace
2385 visibility, but not an explicit attribute. */
2386 && !lookup_attribute ("visibility", attribs))
2388 int depth = TMPL_ARGS_DEPTH (args);
2389 if (DECL_VISIBILITY_SPECIFIED (decl))
2391 /* A class template member with explicit visibility
2392 overrides the class visibility, so we need to apply
2393 all the levels of template args directly. */
2394 int i;
2395 for (i = 1; i <= depth; ++i)
2397 tree lev = TMPL_ARGS_LEVEL (args, i);
2398 constrain_visibility_for_template (decl, lev);
2401 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2402 /* Limit visibility based on its template arguments. */
2403 constrain_visibility_for_template (decl, args);
2408 if (class_type)
2409 determine_visibility_from_class (decl, class_type);
2411 if (decl_anon_ns_mem_p (decl))
2412 /* Names in an anonymous namespace get internal linkage.
2413 This might change once we implement export. */
2414 constrain_visibility (decl, VISIBILITY_ANON, false);
2415 else if (TREE_CODE (decl) != TYPE_DECL)
2417 /* Propagate anonymity from type to decl. */
2418 int tvis = type_visibility (TREE_TYPE (decl));
2419 if (tvis == VISIBILITY_ANON
2420 || ! DECL_VISIBILITY_SPECIFIED (decl))
2421 constrain_visibility (decl, tvis, false);
2423 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2424 /* DR 757: A type without linkage shall not be used as the type of a
2425 variable or function with linkage, unless
2426 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2427 o the variable or function is not used (3.2 [basic.def.odr]) or is
2428 defined in the same translation unit.
2430 Since non-extern "C" decls need to be defined in the same
2431 translation unit, we can make the type internal. */
2432 constrain_visibility (decl, VISIBILITY_ANON, false);
2434 /* If visibility changed and DECL already has DECL_RTL, ensure
2435 symbol flags are updated. */
2436 if ((DECL_VISIBILITY (decl) != orig_visibility
2437 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2438 && ((VAR_P (decl) && TREE_STATIC (decl))
2439 || TREE_CODE (decl) == FUNCTION_DECL)
2440 && DECL_RTL_SET_P (decl))
2441 make_decl_rtl (decl);
2444 /* By default, static data members and function members receive
2445 the visibility of their containing class. */
2447 static void
2448 determine_visibility_from_class (tree decl, tree class_type)
2450 if (DECL_VISIBILITY_SPECIFIED (decl))
2451 return;
2453 if (determine_hidden_inline (decl))
2454 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2455 else
2457 /* Default to the class visibility. */
2458 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2459 DECL_VISIBILITY_SPECIFIED (decl)
2460 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2463 /* Give the target a chance to override the visibility associated
2464 with DECL. */
2465 if (VAR_P (decl)
2466 && (DECL_TINFO_P (decl)
2467 || (DECL_VTABLE_OR_VTT_P (decl)
2468 /* Construction virtual tables are not exported because
2469 they cannot be referred to from other object files;
2470 their name is not standardized by the ABI. */
2471 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2472 && TREE_PUBLIC (decl)
2473 && !DECL_REALLY_EXTERN (decl)
2474 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2475 targetm.cxx.determine_class_data_visibility (decl);
2478 /* Returns true iff DECL is an inline that should get hidden visibility
2479 because of -fvisibility-inlines-hidden. */
2481 static bool
2482 determine_hidden_inline (tree decl)
2484 return (visibility_options.inlines_hidden
2485 /* Don't do this for inline templates; specializations might not be
2486 inline, and we don't want them to inherit the hidden
2487 visibility. We'll set it here for all inline instantiations. */
2488 && !processing_template_decl
2489 && TREE_CODE (decl) == FUNCTION_DECL
2490 && DECL_DECLARED_INLINE_P (decl)
2491 && (! DECL_LANG_SPECIFIC (decl)
2492 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2495 /* Constrain the visibility of a class TYPE based on the visibility of its
2496 field types. Warn if any fields require lesser visibility. */
2498 void
2499 constrain_class_visibility (tree type)
2501 tree binfo;
2502 tree t;
2503 int i;
2505 int vis = type_visibility (type);
2507 if (vis == VISIBILITY_ANON
2508 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2509 return;
2511 /* Don't warn about visibility if the class has explicit visibility. */
2512 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2513 vis = VISIBILITY_INTERNAL;
2515 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2516 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2518 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2519 int subvis = type_visibility (ftype);
2521 if (subvis == VISIBILITY_ANON)
2523 if (!in_main_input_context())
2525 tree nlt = no_linkage_check (ftype, /*relaxed_p=*/false);
2526 if (nlt)
2528 if (same_type_p (TREE_TYPE (t), nlt))
2529 warning (OPT_Wsubobject_linkage, "\
2530 %qT has a field %qD whose type has no linkage",
2531 type, t);
2532 else
2533 warning (OPT_Wsubobject_linkage, "\
2534 %qT has a field %qD whose type depends on the type %qT which has no linkage",
2535 type, t, nlt);
2537 else
2538 warning (OPT_Wsubobject_linkage, "\
2539 %qT has a field %qD whose type uses the anonymous namespace",
2540 type, t);
2543 else if (MAYBE_CLASS_TYPE_P (ftype)
2544 && vis < VISIBILITY_HIDDEN
2545 && subvis >= VISIBILITY_HIDDEN)
2546 warning (OPT_Wattributes, "\
2547 %qT declared with greater visibility than the type of its field %qD",
2548 type, t);
2551 binfo = TYPE_BINFO (type);
2552 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2554 int subvis = type_visibility (TREE_TYPE (t));
2556 if (subvis == VISIBILITY_ANON)
2558 if (!in_main_input_context())
2560 tree nlt = no_linkage_check (TREE_TYPE (t), /*relaxed_p=*/false);
2561 if (nlt)
2563 if (same_type_p (TREE_TYPE (t), nlt))
2564 warning (OPT_Wsubobject_linkage, "\
2565 %qT has a base %qT whose type has no linkage",
2566 type, TREE_TYPE (t));
2567 else
2568 warning (OPT_Wsubobject_linkage, "\
2569 %qT has a base %qT whose type depends on the type %qT which has no linkage",
2570 type, TREE_TYPE (t), nlt);
2572 else
2573 warning (OPT_Wsubobject_linkage, "\
2574 %qT has a base %qT whose type uses the anonymous namespace",
2575 type, TREE_TYPE (t));
2578 else if (vis < VISIBILITY_HIDDEN
2579 && subvis >= VISIBILITY_HIDDEN)
2580 warning (OPT_Wattributes, "\
2581 %qT declared with greater visibility than its base %qT",
2582 type, TREE_TYPE (t));
2586 /* Functions for adjusting the visibility of a tagged type and its nested
2587 types and declarations when it gets a name for linkage purposes from a
2588 typedef. */
2590 static void bt_reset_linkage_1 (binding_entry, void *);
2591 static void bt_reset_linkage_2 (binding_entry, void *);
2593 /* First reset the visibility of all the types. */
2595 static void
2596 reset_type_linkage_1 (tree type)
2598 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2599 if (CLASS_TYPE_P (type))
2600 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2601 bt_reset_linkage_1, NULL);
2603 static void
2604 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2606 reset_type_linkage_1 (b->type);
2609 /* Then reset the visibility of any static data members or member
2610 functions that use those types. */
2612 static void
2613 reset_decl_linkage (tree decl)
2615 if (TREE_PUBLIC (decl))
2616 return;
2617 if (DECL_CLONED_FUNCTION_P (decl))
2618 return;
2619 TREE_PUBLIC (decl) = true;
2620 DECL_INTERFACE_KNOWN (decl) = false;
2621 determine_visibility (decl);
2622 tentative_decl_linkage (decl);
2625 static void
2626 reset_type_linkage_2 (tree type)
2628 if (CLASS_TYPE_P (type))
2630 if (tree vt = CLASSTYPE_VTABLES (type))
2632 tree name = mangle_vtbl_for_type (type);
2633 DECL_NAME (vt) = name;
2634 SET_DECL_ASSEMBLER_NAME (vt, name);
2635 reset_decl_linkage (vt);
2637 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2639 tree name = mangle_typeinfo_for_type (type);
2640 DECL_NAME (ti) = name;
2641 SET_DECL_ASSEMBLER_NAME (ti, name);
2642 TREE_TYPE (name) = type;
2643 reset_decl_linkage (ti);
2645 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2647 tree mem = STRIP_TEMPLATE (m);
2648 if (TREE_CODE (mem) == VAR_DECL || TREE_CODE (mem) == FUNCTION_DECL)
2649 reset_decl_linkage (mem);
2651 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2652 bt_reset_linkage_2, NULL);
2656 static void
2657 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2659 reset_type_linkage_2 (b->type);
2661 void
2662 reset_type_linkage (tree type)
2664 reset_type_linkage_1 (type);
2665 reset_type_linkage_2 (type);
2668 /* Set up our initial idea of what the linkage of DECL should be. */
2670 void
2671 tentative_decl_linkage (tree decl)
2673 if (DECL_INTERFACE_KNOWN (decl))
2674 /* We've already made a decision as to how this function will
2675 be handled. */;
2676 else if (vague_linkage_p (decl))
2678 if (TREE_CODE (decl) == FUNCTION_DECL
2679 && decl_defined_p (decl))
2681 DECL_EXTERNAL (decl) = 1;
2682 DECL_NOT_REALLY_EXTERN (decl) = 1;
2683 note_vague_linkage_fn (decl);
2684 /* A non-template inline function with external linkage will
2685 always be COMDAT. As we must eventually determine the
2686 linkage of all functions, and as that causes writes to
2687 the data mapped in from the PCH file, it's advantageous
2688 to mark the functions at this point. */
2689 if (DECL_DECLARED_INLINE_P (decl)
2690 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2691 || DECL_DEFAULTED_FN (decl)))
2693 /* This function must have external linkage, as
2694 otherwise DECL_INTERFACE_KNOWN would have been
2695 set. */
2696 gcc_assert (TREE_PUBLIC (decl));
2697 comdat_linkage (decl);
2698 DECL_INTERFACE_KNOWN (decl) = 1;
2701 else if (VAR_P (decl))
2702 maybe_commonize_var (decl);
2706 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2707 for DECL has not already been determined, do so now by setting
2708 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2709 function is called entities with vague linkage whose definitions
2710 are available must have TREE_PUBLIC set.
2712 If this function decides to place DECL in COMDAT, it will set
2713 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2714 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2715 callers defer that decision until it is clear that DECL is actually
2716 required. */
2718 void
2719 import_export_decl (tree decl)
2721 int emit_p;
2722 bool comdat_p;
2723 bool import_p;
2724 tree class_type = NULL_TREE;
2726 if (DECL_INTERFACE_KNOWN (decl))
2727 return;
2729 /* We cannot determine what linkage to give to an entity with vague
2730 linkage until the end of the file. For example, a virtual table
2731 for a class will be defined if and only if the key method is
2732 defined in this translation unit. As a further example, consider
2733 that when compiling a translation unit that uses PCH file with
2734 "-frepo" it would be incorrect to make decisions about what
2735 entities to emit when building the PCH; those decisions must be
2736 delayed until the repository information has been processed. */
2737 gcc_assert (at_eof);
2738 /* Object file linkage for explicit instantiations is handled in
2739 mark_decl_instantiated. For static variables in functions with
2740 vague linkage, maybe_commonize_var is used.
2742 Therefore, the only declarations that should be provided to this
2743 function are those with external linkage that are:
2745 * implicit instantiations of function templates
2747 * inline function
2749 * implicit instantiations of static data members of class
2750 templates
2752 * virtual tables
2754 * typeinfo objects
2756 Furthermore, all entities that reach this point must have a
2757 definition available in this translation unit.
2759 The following assertions check these conditions. */
2760 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2761 /* Any code that creates entities with TREE_PUBLIC cleared should
2762 also set DECL_INTERFACE_KNOWN. */
2763 gcc_assert (TREE_PUBLIC (decl));
2764 if (TREE_CODE (decl) == FUNCTION_DECL)
2765 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2766 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2767 || DECL_DECLARED_INLINE_P (decl));
2768 else
2769 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2770 || DECL_VTABLE_OR_VTT_P (decl)
2771 || DECL_TINFO_P (decl));
2772 /* Check that a definition of DECL is available in this translation
2773 unit. */
2774 gcc_assert (!DECL_REALLY_EXTERN (decl));
2776 /* Assume that DECL will not have COMDAT linkage. */
2777 comdat_p = false;
2778 /* Assume that DECL will not be imported into this translation
2779 unit. */
2780 import_p = false;
2782 /* See if the repository tells us whether or not to emit DECL in
2783 this translation unit. */
2784 emit_p = repo_emit_p (decl);
2785 if (emit_p == 0)
2786 import_p = true;
2787 else if (emit_p == 1)
2789 /* The repository indicates that this entity should be defined
2790 here. Make sure the back end honors that request. */
2791 mark_needed (decl);
2792 /* Output the definition as an ordinary strong definition. */
2793 DECL_EXTERNAL (decl) = 0;
2794 DECL_INTERFACE_KNOWN (decl) = 1;
2795 return;
2798 if (import_p)
2799 /* We have already decided what to do with this DECL; there is no
2800 need to check anything further. */
2802 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2804 class_type = DECL_CONTEXT (decl);
2805 import_export_class (class_type);
2806 if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2807 && CLASSTYPE_INTERFACE_ONLY (class_type))
2808 import_p = true;
2809 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2810 && !CLASSTYPE_USE_TEMPLATE (class_type)
2811 && CLASSTYPE_KEY_METHOD (class_type)
2812 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2813 /* The ABI requires that all virtual tables be emitted with
2814 COMDAT linkage. However, on systems where COMDAT symbols
2815 don't show up in the table of contents for a static
2816 archive, or on systems without weak symbols (where we
2817 approximate COMDAT linkage by using internal linkage), the
2818 linker will report errors about undefined symbols because
2819 it will not see the virtual table definition. Therefore,
2820 in the case that we know that the virtual table will be
2821 emitted in only one translation unit, we make the virtual
2822 table an ordinary definition with external linkage. */
2823 DECL_EXTERNAL (decl) = 0;
2824 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2826 /* CLASS_TYPE is being exported from this translation unit,
2827 so DECL should be defined here. */
2828 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2829 /* If a class is declared in a header with the "extern
2830 template" extension, then it will not be instantiated,
2831 even in translation units that would normally require
2832 it. Often such classes are explicitly instantiated in
2833 one translation unit. Therefore, the explicit
2834 instantiation must be made visible to other translation
2835 units. */
2836 DECL_EXTERNAL (decl) = 0;
2837 else
2839 /* The generic C++ ABI says that class data is always
2840 COMDAT, even if there is a key function. Some
2841 variants (e.g., the ARM EABI) says that class data
2842 only has COMDAT linkage if the class data might be
2843 emitted in more than one translation unit. When the
2844 key method can be inline and is inline, we still have
2845 to arrange for comdat even though
2846 class_data_always_comdat is false. */
2847 if (!CLASSTYPE_KEY_METHOD (class_type)
2848 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2849 || targetm.cxx.class_data_always_comdat ())
2851 /* The ABI requires COMDAT linkage. Normally, we
2852 only emit COMDAT things when they are needed;
2853 make sure that we realize that this entity is
2854 indeed needed. */
2855 comdat_p = true;
2856 mark_needed (decl);
2860 else if (!flag_implicit_templates
2861 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2862 import_p = true;
2863 else
2864 comdat_p = true;
2866 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2868 tree type = TREE_TYPE (DECL_NAME (decl));
2869 if (CLASS_TYPE_P (type))
2871 class_type = type;
2872 import_export_class (type);
2873 if (CLASSTYPE_INTERFACE_KNOWN (type)
2874 && TYPE_POLYMORPHIC_P (type)
2875 && CLASSTYPE_INTERFACE_ONLY (type)
2876 /* If -fno-rtti was specified, then we cannot be sure
2877 that RTTI information will be emitted with the
2878 virtual table of the class, so we must emit it
2879 wherever it is used. */
2880 && flag_rtti)
2881 import_p = true;
2882 else
2884 if (CLASSTYPE_INTERFACE_KNOWN (type)
2885 && !CLASSTYPE_INTERFACE_ONLY (type))
2887 comdat_p = (targetm.cxx.class_data_always_comdat ()
2888 || (CLASSTYPE_KEY_METHOD (type)
2889 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2890 mark_needed (decl);
2891 if (!flag_weak)
2893 comdat_p = false;
2894 DECL_EXTERNAL (decl) = 0;
2897 else
2898 comdat_p = true;
2901 else
2902 comdat_p = true;
2904 else if (DECL_TEMPLOID_INSTANTIATION (decl))
2906 /* DECL is an implicit instantiation of a function or static
2907 data member. */
2908 if ((flag_implicit_templates
2909 && !flag_use_repository)
2910 || (flag_implicit_inline_templates
2911 && TREE_CODE (decl) == FUNCTION_DECL
2912 && DECL_DECLARED_INLINE_P (decl)))
2913 comdat_p = true;
2914 else
2915 /* If we are not implicitly generating templates, then mark
2916 this entity as undefined in this translation unit. */
2917 import_p = true;
2919 else if (DECL_FUNCTION_MEMBER_P (decl))
2921 if (!DECL_DECLARED_INLINE_P (decl))
2923 tree ctype = DECL_CONTEXT (decl);
2924 import_export_class (ctype);
2925 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2927 DECL_NOT_REALLY_EXTERN (decl)
2928 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2929 || (DECL_DECLARED_INLINE_P (decl)
2930 && ! flag_implement_inlines
2931 && !DECL_VINDEX (decl)));
2933 if (!DECL_NOT_REALLY_EXTERN (decl))
2934 DECL_EXTERNAL (decl) = 1;
2936 /* Always make artificials weak. */
2937 if (DECL_ARTIFICIAL (decl) && flag_weak)
2938 comdat_p = true;
2939 else
2940 maybe_make_one_only (decl);
2943 else
2944 comdat_p = true;
2946 else
2947 comdat_p = true;
2949 if (import_p)
2951 /* If we are importing DECL into this translation unit, mark is
2952 an undefined here. */
2953 DECL_EXTERNAL (decl) = 1;
2954 DECL_NOT_REALLY_EXTERN (decl) = 0;
2956 else if (comdat_p)
2958 /* If we decided to put DECL in COMDAT, mark it accordingly at
2959 this point. */
2960 comdat_linkage (decl);
2963 DECL_INTERFACE_KNOWN (decl) = 1;
2966 /* Return an expression that performs the destruction of DECL, which
2967 must be a VAR_DECL whose type has a non-trivial destructor, or is
2968 an array whose (innermost) elements have a non-trivial destructor. */
2970 tree
2971 build_cleanup (tree decl)
2973 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
2974 gcc_assert (clean != NULL_TREE);
2975 return clean;
2978 /* Returns the initialization guard variable for the variable DECL,
2979 which has static storage duration. */
2981 tree
2982 get_guard (tree decl)
2984 tree sname;
2985 tree guard;
2987 sname = mangle_guard_variable (decl);
2988 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2989 if (! guard)
2991 tree guard_type;
2993 /* We use a type that is big enough to contain a mutex as well
2994 as an integer counter. */
2995 guard_type = targetm.cxx.guard_type ();
2996 guard = build_decl (DECL_SOURCE_LOCATION (decl),
2997 VAR_DECL, sname, guard_type);
2999 /* The guard should have the same linkage as what it guards. */
3000 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
3001 TREE_STATIC (guard) = TREE_STATIC (decl);
3002 DECL_COMMON (guard) = DECL_COMMON (decl);
3003 DECL_COMDAT (guard) = DECL_COMDAT (decl);
3004 CP_DECL_THREAD_LOCAL_P (guard) = CP_DECL_THREAD_LOCAL_P (decl);
3005 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
3006 if (DECL_ONE_ONLY (decl))
3007 make_decl_one_only (guard, cxx_comdat_group (guard));
3008 if (TREE_PUBLIC (decl))
3009 DECL_WEAK (guard) = DECL_WEAK (decl);
3010 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
3011 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
3013 DECL_ARTIFICIAL (guard) = 1;
3014 DECL_IGNORED_P (guard) = 1;
3015 TREE_USED (guard) = 1;
3016 pushdecl_top_level_and_finish (guard, NULL_TREE);
3018 return guard;
3021 /* Return an atomic load of src with the appropriate memory model. */
3023 static tree
3024 build_atomic_load_byte (tree src, HOST_WIDE_INT model)
3026 tree ptr_type = build_pointer_type (char_type_node);
3027 tree mem_model = build_int_cst (integer_type_node, model);
3028 tree t, addr, val;
3029 unsigned int size;
3030 int fncode;
3032 size = tree_to_uhwi (TYPE_SIZE_UNIT (char_type_node));
3034 fncode = BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1;
3035 t = builtin_decl_implicit ((enum built_in_function) fncode);
3037 addr = build1 (ADDR_EXPR, ptr_type, src);
3038 val = build_call_expr (t, 2, addr, mem_model);
3039 return val;
3042 /* Return those bits of the GUARD variable that should be set when the
3043 guarded entity is actually initialized. */
3045 static tree
3046 get_guard_bits (tree guard)
3048 if (!targetm.cxx.guard_mask_bit ())
3050 /* We only set the first byte of the guard, in order to leave room
3051 for a mutex in the high-order bits. */
3052 guard = build1 (ADDR_EXPR,
3053 build_pointer_type (TREE_TYPE (guard)),
3054 guard);
3055 guard = build1 (NOP_EXPR,
3056 build_pointer_type (char_type_node),
3057 guard);
3058 guard = build1 (INDIRECT_REF, char_type_node, guard);
3061 return guard;
3064 /* Return an expression which determines whether or not the GUARD
3065 variable has already been initialized. */
3067 tree
3068 get_guard_cond (tree guard, bool thread_safe)
3070 tree guard_value;
3072 if (!thread_safe)
3073 guard = get_guard_bits (guard);
3074 else
3075 guard = build_atomic_load_byte (guard, MEMMODEL_ACQUIRE);
3077 /* Mask off all but the low bit. */
3078 if (targetm.cxx.guard_mask_bit ())
3080 guard_value = integer_one_node;
3081 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3082 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3083 guard = cp_build_binary_op (input_location,
3084 BIT_AND_EXPR, guard, guard_value,
3085 tf_warning_or_error);
3088 guard_value = integer_zero_node;
3089 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3090 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3091 return cp_build_binary_op (input_location,
3092 EQ_EXPR, guard, guard_value,
3093 tf_warning_or_error);
3096 /* Return an expression which sets the GUARD variable, indicating that
3097 the variable being guarded has been initialized. */
3099 tree
3100 set_guard (tree guard)
3102 tree guard_init;
3104 /* Set the GUARD to one. */
3105 guard = get_guard_bits (guard);
3106 guard_init = integer_one_node;
3107 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3108 guard_init = fold_convert (TREE_TYPE (guard), guard_init);
3109 return cp_build_modify_expr (input_location, guard, NOP_EXPR, guard_init,
3110 tf_warning_or_error);
3113 /* Returns true iff we can tell that VAR does not have a dynamic
3114 initializer. */
3116 static bool
3117 var_defined_without_dynamic_init (tree var)
3119 /* If it's defined in another TU, we can't tell. */
3120 if (DECL_EXTERNAL (var))
3121 return false;
3122 /* If it has a non-trivial destructor, registering the destructor
3123 counts as dynamic initialization. */
3124 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3125 return false;
3126 /* If it's in this TU, its initializer has been processed, unless
3127 it's a case of self-initialization, then DECL_INITIALIZED_P is
3128 false while the initializer is handled by finish_id_expression. */
3129 if (!DECL_INITIALIZED_P (var))
3130 return false;
3131 /* If it has no initializer or a constant one, it's not dynamic. */
3132 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3133 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3136 /* Returns true iff VAR is a variable that needs uses to be
3137 wrapped for possible dynamic initialization. */
3139 static bool
3140 var_needs_tls_wrapper (tree var)
3142 return (!error_operand_p (var)
3143 && CP_DECL_THREAD_LOCAL_P (var)
3144 && !DECL_GNU_TLS_P (var)
3145 && !DECL_FUNCTION_SCOPE_P (var)
3146 && !var_defined_without_dynamic_init (var));
3149 /* Get the FUNCTION_DECL for the shared TLS init function for this
3150 translation unit. */
3152 static tree
3153 get_local_tls_init_fn (void)
3155 tree sname = get_identifier ("__tls_init");
3156 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3157 if (!fn)
3159 fn = build_lang_decl (FUNCTION_DECL, sname,
3160 build_function_type (void_type_node,
3161 void_list_node));
3162 SET_DECL_LANGUAGE (fn, lang_c);
3163 TREE_PUBLIC (fn) = false;
3164 DECL_ARTIFICIAL (fn) = true;
3165 mark_used (fn);
3166 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3168 return fn;
3171 /* Get a FUNCTION_DECL for the init function for the thread_local
3172 variable VAR. The init function will be an alias to the function
3173 that initializes all the non-local TLS variables in the translation
3174 unit. The init function is only used by the wrapper function. */
3176 static tree
3177 get_tls_init_fn (tree var)
3179 /* Only C++11 TLS vars need this init fn. */
3180 if (!var_needs_tls_wrapper (var))
3181 return NULL_TREE;
3183 /* If -fno-extern-tls-init, assume that we don't need to call
3184 a tls init function for a variable defined in another TU. */
3185 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3186 return NULL_TREE;
3188 /* If the variable is internal, or if we can't generate aliases,
3189 call the local init function directly. */
3190 if (!TREE_PUBLIC (var) || !TARGET_SUPPORTS_ALIASES)
3191 return get_local_tls_init_fn ();
3193 tree sname = mangle_tls_init_fn (var);
3194 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3195 if (!fn)
3197 fn = build_lang_decl (FUNCTION_DECL, sname,
3198 build_function_type (void_type_node,
3199 void_list_node));
3200 SET_DECL_LANGUAGE (fn, lang_c);
3201 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3202 DECL_ARTIFICIAL (fn) = true;
3203 DECL_COMDAT (fn) = DECL_COMDAT (var);
3204 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3205 if (DECL_ONE_ONLY (var))
3206 make_decl_one_only (fn, cxx_comdat_group (fn));
3207 if (TREE_PUBLIC (var))
3209 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3210 /* If the variable is defined somewhere else and might have static
3211 initialization, make the init function a weak reference. */
3212 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3213 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3214 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3215 && DECL_EXTERNAL (var))
3216 declare_weak (fn);
3217 else
3218 DECL_WEAK (fn) = DECL_WEAK (var);
3220 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3221 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3222 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3223 DECL_IGNORED_P (fn) = 1;
3224 mark_used (fn);
3226 DECL_BEFRIENDING_CLASSES (fn) = var;
3228 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3230 return fn;
3233 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3234 variable VAR. The wrapper function calls the init function (if any) for
3235 VAR and then returns a reference to VAR. The wrapper function is used
3236 in place of VAR everywhere VAR is mentioned. */
3238 tree
3239 get_tls_wrapper_fn (tree var)
3241 /* Only C++11 TLS vars need this wrapper fn. */
3242 if (!var_needs_tls_wrapper (var))
3243 return NULL_TREE;
3245 tree sname = mangle_tls_wrapper_fn (var);
3246 tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
3247 if (!fn)
3249 /* A named rvalue reference is an lvalue, so the wrapper should
3250 always return an lvalue reference. */
3251 tree type = non_reference (TREE_TYPE (var));
3252 type = build_reference_type (type);
3253 tree fntype = build_function_type (type, void_list_node);
3254 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3255 SET_DECL_LANGUAGE (fn, lang_c);
3256 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3257 DECL_ARTIFICIAL (fn) = true;
3258 DECL_IGNORED_P (fn) = 1;
3259 /* The wrapper is inline and emitted everywhere var is used. */
3260 DECL_DECLARED_INLINE_P (fn) = true;
3261 if (TREE_PUBLIC (var))
3263 comdat_linkage (fn);
3264 #ifdef HAVE_GAS_HIDDEN
3265 /* Make the wrapper bind locally; there's no reason to share
3266 the wrapper between multiple shared objects. */
3267 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3268 DECL_VISIBILITY_SPECIFIED (fn) = true;
3269 #endif
3271 if (!TREE_PUBLIC (fn))
3272 DECL_INTERFACE_KNOWN (fn) = true;
3273 mark_used (fn);
3274 note_vague_linkage_fn (fn);
3276 #if 0
3277 /* We want CSE to commonize calls to the wrapper, but marking it as
3278 pure is unsafe since it has side-effects. I guess we need a new
3279 ECF flag even weaker than ECF_PURE. FIXME! */
3280 DECL_PURE_P (fn) = true;
3281 #endif
3283 DECL_BEFRIENDING_CLASSES (fn) = var;
3285 SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
3287 return fn;
3290 /* At EOF, generate the definition for the TLS wrapper function FN:
3292 T& var_wrapper() {
3293 if (init_fn) init_fn();
3294 return var;
3295 } */
3297 static void
3298 generate_tls_wrapper (tree fn)
3300 tree var = DECL_BEFRIENDING_CLASSES (fn);
3302 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3303 tree body = begin_function_body ();
3304 /* Only call the init fn if there might be one. */
3305 if (tree init_fn = get_tls_init_fn (var))
3307 tree if_stmt = NULL_TREE;
3308 /* If init_fn is a weakref, make sure it exists before calling. */
3309 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3311 if_stmt = begin_if_stmt ();
3312 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3313 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3314 NE_EXPR, addr, nullptr_node,
3315 tf_warning_or_error);
3316 finish_if_stmt_cond (cond, if_stmt);
3318 finish_expr_stmt (build_cxx_call
3319 (init_fn, 0, NULL, tf_warning_or_error));
3320 if (if_stmt)
3322 finish_then_clause (if_stmt);
3323 finish_if_stmt (if_stmt);
3326 else
3327 /* If there's no initialization, the wrapper is a constant function. */
3328 TREE_READONLY (fn) = true;
3329 finish_return_stmt (convert_from_reference (var));
3330 finish_function_body (body);
3331 expand_or_defer_fn (finish_function (0));
3334 /* Start the process of running a particular set of global constructors
3335 or destructors. Subroutine of do_[cd]tors. Also called from
3336 vtv_start_verification_constructor_init_function. */
3338 static tree
3339 start_objects (int method_type, int initp)
3341 tree body;
3342 tree fndecl;
3343 char type[14];
3345 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3347 if (initp != DEFAULT_INIT_PRIORITY)
3349 char joiner;
3351 #ifdef JOINER
3352 joiner = JOINER;
3353 #else
3354 joiner = '_';
3355 #endif
3357 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3359 else
3360 sprintf (type, "sub_%c", method_type);
3362 fndecl = build_lang_decl (FUNCTION_DECL,
3363 get_file_function_name (type),
3364 build_function_type_list (void_type_node,
3365 NULL_TREE));
3366 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3368 TREE_PUBLIC (current_function_decl) = 0;
3370 /* Mark as artificial because it's not explicitly in the user's
3371 source code. */
3372 DECL_ARTIFICIAL (current_function_decl) = 1;
3374 /* Mark this declaration as used to avoid spurious warnings. */
3375 TREE_USED (current_function_decl) = 1;
3377 /* Mark this function as a global constructor or destructor. */
3378 if (method_type == 'I')
3379 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3380 else
3381 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3383 body = begin_compound_stmt (BCS_FN_BODY);
3385 return body;
3388 /* Finish the process of running a particular set of global constructors
3389 or destructors. Subroutine of do_[cd]tors. */
3391 static void
3392 finish_objects (int method_type, int initp, tree body)
3394 tree fn;
3396 /* Finish up. */
3397 finish_compound_stmt (body);
3398 fn = finish_function (0);
3400 if (method_type == 'I')
3402 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3403 decl_init_priority_insert (fn, initp);
3405 else
3407 DECL_STATIC_DESTRUCTOR (fn) = 1;
3408 decl_fini_priority_insert (fn, initp);
3411 expand_or_defer_fn (fn);
3414 /* The names of the parameters to the function created to handle
3415 initializations and destructions for objects with static storage
3416 duration. */
3417 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3418 #define PRIORITY_IDENTIFIER "__priority"
3420 /* The name of the function we create to handle initializations and
3421 destructions for objects with static storage duration. */
3422 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3424 /* The declaration for the __INITIALIZE_P argument. */
3425 static GTY(()) tree initialize_p_decl;
3427 /* The declaration for the __PRIORITY argument. */
3428 static GTY(()) tree priority_decl;
3430 /* The declaration for the static storage duration function. */
3431 static GTY(()) tree ssdf_decl;
3433 /* All the static storage duration functions created in this
3434 translation unit. */
3435 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3437 /* A map from priority levels to information about that priority
3438 level. There may be many such levels, so efficient lookup is
3439 important. */
3440 static splay_tree priority_info_map;
3442 /* Begins the generation of the function that will handle all
3443 initialization and destruction of objects with static storage
3444 duration. The function generated takes two parameters of type
3445 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3446 nonzero, it performs initializations. Otherwise, it performs
3447 destructions. It only performs those initializations or
3448 destructions with the indicated __PRIORITY. The generated function
3449 returns no value.
3451 It is assumed that this function will only be called once per
3452 translation unit. */
3454 static tree
3455 start_static_storage_duration_function (unsigned count)
3457 tree type;
3458 tree body;
3459 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3461 /* Create the identifier for this function. It will be of the form
3462 SSDF_IDENTIFIER_<number>. */
3463 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3465 type = build_function_type_list (void_type_node,
3466 integer_type_node, integer_type_node,
3467 NULL_TREE);
3469 /* Create the FUNCTION_DECL itself. */
3470 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3471 get_identifier (id),
3472 type);
3473 TREE_PUBLIC (ssdf_decl) = 0;
3474 DECL_ARTIFICIAL (ssdf_decl) = 1;
3476 /* Put this function in the list of functions to be called from the
3477 static constructors and destructors. */
3478 if (!ssdf_decls)
3480 vec_alloc (ssdf_decls, 32);
3482 /* Take this opportunity to initialize the map from priority
3483 numbers to information about that priority level. */
3484 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3485 /*delete_key_fn=*/0,
3486 /*delete_value_fn=*/
3487 (splay_tree_delete_value_fn) &free);
3489 /* We always need to generate functions for the
3490 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3491 priorities later, we'll be sure to find the
3492 DEFAULT_INIT_PRIORITY. */
3493 get_priority_info (DEFAULT_INIT_PRIORITY);
3496 vec_safe_push (ssdf_decls, ssdf_decl);
3498 /* Create the argument list. */
3499 initialize_p_decl = cp_build_parm_decl
3500 (ssdf_decl, get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3501 TREE_USED (initialize_p_decl) = 1;
3502 priority_decl = cp_build_parm_decl
3503 (ssdf_decl, get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3504 TREE_USED (priority_decl) = 1;
3506 DECL_CHAIN (initialize_p_decl) = priority_decl;
3507 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3509 /* Put the function in the global scope. */
3510 pushdecl (ssdf_decl);
3512 /* Start the function itself. This is equivalent to declaring the
3513 function as:
3515 static void __ssdf (int __initialize_p, init __priority_p);
3517 It is static because we only need to call this function from the
3518 various constructor and destructor functions for this module. */
3519 start_preparsed_function (ssdf_decl,
3520 /*attrs=*/NULL_TREE,
3521 SF_PRE_PARSED);
3523 /* Set up the scope of the outermost block in the function. */
3524 body = begin_compound_stmt (BCS_FN_BODY);
3526 return body;
3529 /* Finish the generation of the function which performs initialization
3530 and destruction of objects with static storage duration. After
3531 this point, no more such objects can be created. */
3533 static void
3534 finish_static_storage_duration_function (tree body)
3536 /* Close out the function. */
3537 finish_compound_stmt (body);
3538 expand_or_defer_fn (finish_function (0));
3541 /* Return the information about the indicated PRIORITY level. If no
3542 code to handle this level has yet been generated, generate the
3543 appropriate prologue. */
3545 static priority_info
3546 get_priority_info (int priority)
3548 priority_info pi;
3549 splay_tree_node n;
3551 n = splay_tree_lookup (priority_info_map,
3552 (splay_tree_key) priority);
3553 if (!n)
3555 /* Create a new priority information structure, and insert it
3556 into the map. */
3557 pi = XNEW (struct priority_info_s);
3558 pi->initializations_p = 0;
3559 pi->destructions_p = 0;
3560 splay_tree_insert (priority_info_map,
3561 (splay_tree_key) priority,
3562 (splay_tree_value) pi);
3564 else
3565 pi = (priority_info) n->value;
3567 return pi;
3570 /* The effective initialization priority of a DECL. */
3572 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3573 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3574 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3576 /* Whether a DECL needs a guard to protect it against multiple
3577 initialization. */
3579 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3580 || DECL_ONE_ONLY (decl) \
3581 || DECL_WEAK (decl)))
3583 /* Called from one_static_initialization_or_destruction(),
3584 via walk_tree.
3585 Walks the initializer list of a global variable and looks for
3586 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3587 and that have their DECL_CONTEXT() == NULL.
3588 For each such temporary variable, set their DECL_CONTEXT() to
3589 the current function. This is necessary because otherwise
3590 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3591 when trying to refer to a temporary variable that does not have
3592 it's DECL_CONTECT() properly set. */
3593 static tree
3594 fix_temporary_vars_context_r (tree *node,
3595 int * /*unused*/,
3596 void * /*unused1*/)
3598 gcc_assert (current_function_decl);
3600 if (TREE_CODE (*node) == BIND_EXPR)
3602 tree var;
3604 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3605 if (VAR_P (var)
3606 && !DECL_NAME (var)
3607 && DECL_ARTIFICIAL (var)
3608 && !DECL_CONTEXT (var))
3609 DECL_CONTEXT (var) = current_function_decl;
3612 return NULL_TREE;
3615 /* Set up to handle the initialization or destruction of DECL. If
3616 INITP is nonzero, we are initializing the variable. Otherwise, we
3617 are destroying it. */
3619 static void
3620 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3622 tree guard_if_stmt = NULL_TREE;
3623 tree guard;
3625 /* If we are supposed to destruct and there's a trivial destructor,
3626 nothing has to be done. */
3627 if (!initp
3628 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3629 return;
3631 /* Trick the compiler into thinking we are at the file and line
3632 where DECL was declared so that error-messages make sense, and so
3633 that the debugger will show somewhat sensible file and line
3634 information. */
3635 input_location = DECL_SOURCE_LOCATION (decl);
3637 /* Make sure temporary variables in the initialiser all have
3638 their DECL_CONTEXT() set to a value different from NULL_TREE.
3639 This can happen when global variables initializers are built.
3640 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3641 the temporary variables that might have been generated in the
3642 accompanying initializers is NULL_TREE, meaning the variables have been
3643 declared in the global namespace.
3644 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3645 of the temporaries are set to the current function decl. */
3646 cp_walk_tree_without_duplicates (&init,
3647 fix_temporary_vars_context_r,
3648 NULL);
3650 /* Because of:
3652 [class.access.spec]
3654 Access control for implicit calls to the constructors,
3655 the conversion functions, or the destructor called to
3656 create and destroy a static data member is performed as
3657 if these calls appeared in the scope of the member's
3658 class.
3660 we pretend we are in a static member function of the class of
3661 which the DECL is a member. */
3662 if (member_p (decl))
3664 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3665 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3668 /* Assume we don't need a guard. */
3669 guard = NULL_TREE;
3670 /* We need a guard if this is an object with external linkage that
3671 might be initialized in more than one place. (For example, a
3672 static data member of a template, when the data member requires
3673 construction.) */
3674 if (NEEDS_GUARD_P (decl))
3676 tree guard_cond;
3678 guard = get_guard (decl);
3680 /* When using __cxa_atexit, we just check the GUARD as we would
3681 for a local static. */
3682 if (flag_use_cxa_atexit)
3684 /* When using __cxa_atexit, we never try to destroy
3685 anything from a static destructor. */
3686 gcc_assert (initp);
3687 guard_cond = get_guard_cond (guard, false);
3689 /* If we don't have __cxa_atexit, then we will be running
3690 destructors from .fini sections, or their equivalents. So,
3691 we need to know how many times we've tried to initialize this
3692 object. We do initializations only if the GUARD is zero,
3693 i.e., if we are the first to initialize the variable. We do
3694 destructions only if the GUARD is one, i.e., if we are the
3695 last to destroy the variable. */
3696 else if (initp)
3697 guard_cond
3698 = cp_build_binary_op (input_location,
3699 EQ_EXPR,
3700 cp_build_unary_op (PREINCREMENT_EXPR,
3701 guard,
3702 /*noconvert=*/true,
3703 tf_warning_or_error),
3704 integer_one_node,
3705 tf_warning_or_error);
3706 else
3707 guard_cond
3708 = cp_build_binary_op (input_location,
3709 EQ_EXPR,
3710 cp_build_unary_op (PREDECREMENT_EXPR,
3711 guard,
3712 /*noconvert=*/true,
3713 tf_warning_or_error),
3714 integer_zero_node,
3715 tf_warning_or_error);
3717 guard_if_stmt = begin_if_stmt ();
3718 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3722 /* If we're using __cxa_atexit, we have not already set the GUARD,
3723 so we must do so now. */
3724 if (guard && initp && flag_use_cxa_atexit)
3725 finish_expr_stmt (set_guard (guard));
3727 /* Perform the initialization or destruction. */
3728 if (initp)
3730 if (init)
3732 finish_expr_stmt (init);
3733 if (sanitize_flags_p (SANITIZE_ADDRESS, decl))
3735 varpool_node *vnode = varpool_node::get (decl);
3736 if (vnode)
3737 vnode->dynamically_initialized = 1;
3741 /* If we're using __cxa_atexit, register a function that calls the
3742 destructor for the object. */
3743 if (flag_use_cxa_atexit)
3744 finish_expr_stmt (register_dtor_fn (decl));
3746 else
3747 finish_expr_stmt (build_cleanup (decl));
3749 /* Finish the guard if-stmt, if necessary. */
3750 if (guard)
3752 finish_then_clause (guard_if_stmt);
3753 finish_if_stmt (guard_if_stmt);
3756 /* Now that we're done with DECL we don't need to pretend to be a
3757 member of its class any longer. */
3758 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3759 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3762 /* Generate code to do the initialization or destruction of the decls in VARS,
3763 a TREE_LIST of VAR_DECL with static storage duration.
3764 Whether initialization or destruction is performed is specified by INITP. */
3766 static void
3767 do_static_initialization_or_destruction (tree vars, bool initp)
3769 tree node, init_if_stmt, cond;
3771 /* Build the outer if-stmt to check for initialization or destruction. */
3772 init_if_stmt = begin_if_stmt ();
3773 cond = initp ? integer_one_node : integer_zero_node;
3774 cond = cp_build_binary_op (input_location,
3775 EQ_EXPR,
3776 initialize_p_decl,
3777 cond,
3778 tf_warning_or_error);
3779 finish_if_stmt_cond (cond, init_if_stmt);
3781 /* To make sure dynamic construction doesn't access globals from other
3782 compilation units where they might not be yet constructed, for
3783 -fsanitize=address insert __asan_before_dynamic_init call that
3784 prevents access to either all global variables that need construction
3785 in other compilation units, or at least those that haven't been
3786 initialized yet. Variables that need dynamic construction in
3787 the current compilation unit are kept accessible. */
3788 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
3789 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3791 node = vars;
3792 do {
3793 tree decl = TREE_VALUE (node);
3794 tree priority_if_stmt;
3795 int priority;
3796 priority_info pi;
3798 /* If we don't need a destructor, there's nothing to do. Avoid
3799 creating a possibly empty if-stmt. */
3800 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3802 node = TREE_CHAIN (node);
3803 continue;
3806 /* Remember that we had an initialization or finalization at this
3807 priority. */
3808 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3809 pi = get_priority_info (priority);
3810 if (initp)
3811 pi->initializations_p = 1;
3812 else
3813 pi->destructions_p = 1;
3815 /* Conditionalize this initialization on being in the right priority
3816 and being initializing/finalizing appropriately. */
3817 priority_if_stmt = begin_if_stmt ();
3818 cond = cp_build_binary_op (input_location,
3819 EQ_EXPR,
3820 priority_decl,
3821 build_int_cst (NULL_TREE, priority),
3822 tf_warning_or_error);
3823 finish_if_stmt_cond (cond, priority_if_stmt);
3825 /* Process initializers with same priority. */
3826 for (; node
3827 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3828 node = TREE_CHAIN (node))
3829 /* Do one initialization or destruction. */
3830 one_static_initialization_or_destruction (TREE_VALUE (node),
3831 TREE_PURPOSE (node), initp);
3833 /* Finish up the priority if-stmt body. */
3834 finish_then_clause (priority_if_stmt);
3835 finish_if_stmt (priority_if_stmt);
3837 } while (node);
3839 /* Revert what __asan_before_dynamic_init did by calling
3840 __asan_after_dynamic_init. */
3841 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
3842 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3844 /* Finish up the init/destruct if-stmt body. */
3845 finish_then_clause (init_if_stmt);
3846 finish_if_stmt (init_if_stmt);
3849 /* VARS is a list of variables with static storage duration which may
3850 need initialization and/or finalization. Remove those variables
3851 that don't really need to be initialized or finalized, and return
3852 the resulting list. The order in which the variables appear in
3853 VARS is in reverse order of the order in which they should actually
3854 be initialized. The list we return is in the unreversed order;
3855 i.e., the first variable should be initialized first. */
3857 static tree
3858 prune_vars_needing_no_initialization (tree *vars)
3860 tree *var = vars;
3861 tree result = NULL_TREE;
3863 while (*var)
3865 tree t = *var;
3866 tree decl = TREE_VALUE (t);
3867 tree init = TREE_PURPOSE (t);
3869 /* Deal gracefully with error. */
3870 if (error_operand_p (decl))
3872 var = &TREE_CHAIN (t);
3873 continue;
3876 /* The only things that can be initialized are variables. */
3877 gcc_assert (VAR_P (decl));
3879 /* If this object is not defined, we don't need to do anything
3880 here. */
3881 if (DECL_EXTERNAL (decl))
3883 var = &TREE_CHAIN (t);
3884 continue;
3887 /* Also, if the initializer already contains errors, we can bail
3888 out now. */
3889 if (init && TREE_CODE (init) == TREE_LIST
3890 && value_member (error_mark_node, init))
3892 var = &TREE_CHAIN (t);
3893 continue;
3896 /* This variable is going to need initialization and/or
3897 finalization, so we add it to the list. */
3898 *var = TREE_CHAIN (t);
3899 TREE_CHAIN (t) = result;
3900 result = t;
3903 return result;
3906 /* Make sure we have told the back end about all the variables in
3907 VARS. */
3909 static void
3910 write_out_vars (tree vars)
3912 tree v;
3914 for (v = vars; v; v = TREE_CHAIN (v))
3916 tree var = TREE_VALUE (v);
3917 if (!var_finalized_p (var))
3919 import_export_decl (var);
3920 rest_of_decl_compilation (var, 1, 1);
3925 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3926 (otherwise) that will initialize all global objects with static
3927 storage duration having the indicated PRIORITY. */
3929 static void
3930 generate_ctor_or_dtor_function (bool constructor_p, int priority,
3931 location_t *locus)
3933 char function_key;
3934 tree fndecl;
3935 tree body;
3936 size_t i;
3938 input_location = *locus;
3939 /* ??? */
3940 /* Was: locus->line++; */
3942 /* We use `I' to indicate initialization and `D' to indicate
3943 destruction. */
3944 function_key = constructor_p ? 'I' : 'D';
3946 /* We emit the function lazily, to avoid generating empty
3947 global constructors and destructors. */
3948 body = NULL_TREE;
3950 /* For Objective-C++, we may need to initialize metadata found in this module.
3951 This must be done _before_ any other static initializations. */
3952 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3953 && constructor_p && objc_static_init_needed_p ())
3955 body = start_objects (function_key, priority);
3956 objc_generate_static_init_call (NULL_TREE);
3959 /* Call the static storage duration function with appropriate
3960 arguments. */
3961 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
3963 /* Calls to pure or const functions will expand to nothing. */
3964 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3966 tree call;
3968 if (! body)
3969 body = start_objects (function_key, priority);
3971 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
3972 build_int_cst (NULL_TREE,
3973 constructor_p),
3974 build_int_cst (NULL_TREE,
3975 priority),
3976 NULL_TREE);
3977 finish_expr_stmt (call);
3981 /* Close out the function. */
3982 if (body)
3983 finish_objects (function_key, priority, body);
3986 /* Generate constructor and destructor functions for the priority
3987 indicated by N. */
3989 static int
3990 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
3992 location_t *locus = (location_t *) data;
3993 int priority = (int) n->key;
3994 priority_info pi = (priority_info) n->value;
3996 /* Generate the functions themselves, but only if they are really
3997 needed. */
3998 if (pi->initializations_p)
3999 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
4000 if (pi->destructions_p)
4001 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
4003 /* Keep iterating. */
4004 return 0;
4007 /* Return C++ property of T, based on given operation OP. */
4009 static int
4010 cpp_check (tree t, cpp_operation op)
4012 switch (op)
4014 case HAS_DEPENDENT_TEMPLATE_ARGS:
4016 tree ti = CLASSTYPE_TEMPLATE_INFO (t);
4017 if (!ti)
4018 return 0;
4019 ++processing_template_decl;
4020 const bool dep = any_dependent_template_arguments_p (TI_ARGS (ti));
4021 --processing_template_decl;
4022 return dep;
4024 case IS_ABSTRACT:
4025 return DECL_PURE_VIRTUAL_P (t);
4026 case IS_CONSTRUCTOR:
4027 return DECL_CONSTRUCTOR_P (t);
4028 case IS_DESTRUCTOR:
4029 return DECL_DESTRUCTOR_P (t);
4030 case IS_COPY_CONSTRUCTOR:
4031 return DECL_COPY_CONSTRUCTOR_P (t);
4032 case IS_MOVE_CONSTRUCTOR:
4033 return DECL_MOVE_CONSTRUCTOR_P (t);
4034 case IS_TEMPLATE:
4035 return TREE_CODE (t) == TEMPLATE_DECL;
4036 case IS_TRIVIAL:
4037 return trivial_type_p (t);
4038 default:
4039 return 0;
4043 /* Collect source file references recursively, starting from NAMESPC. */
4045 static void
4046 collect_source_refs (tree namespc)
4048 /* Iterate over names in this name space. */
4049 for (tree t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4050 if (DECL_IS_BUILTIN (t))
4052 else if (TREE_CODE (t) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (t))
4053 collect_source_refs (t);
4054 else
4055 collect_source_ref (DECL_SOURCE_FILE (t));
4058 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4059 starting from NAMESPC. */
4061 static void
4062 collect_ada_namespace (tree namespc, const char *source_file)
4064 tree decl = NAMESPACE_LEVEL (namespc)->names;
4066 /* Collect decls from this namespace. This will skip
4067 NAMESPACE_DECLs (both aliases and regular, it cannot tell). */
4068 collect_ada_nodes (decl, source_file);
4070 /* Now scan for namespace children, and dump them. */
4071 for (; decl; decl = TREE_CHAIN (decl))
4072 if (TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl))
4073 collect_ada_namespace (decl, source_file);
4076 /* Returns true iff there is a definition available for variable or
4077 function DECL. */
4079 bool
4080 decl_defined_p (tree decl)
4082 if (TREE_CODE (decl) == FUNCTION_DECL)
4083 return (DECL_INITIAL (decl) != NULL_TREE
4084 /* A pending instantiation of a friend temploid is defined. */
4085 || (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
4086 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4087 (DECL_TI_TEMPLATE (decl)))));
4088 else
4090 gcc_assert (VAR_P (decl));
4091 return !DECL_EXTERNAL (decl);
4095 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4097 [expr.const]
4099 An integral constant-expression can only involve ... const
4100 variables of integral or enumeration types initialized with
4101 constant expressions ...
4103 C++0x also allows constexpr variables and temporaries initialized
4104 with constant expressions. We handle the former here, but the latter
4105 are just folded away in cxx_eval_constant_expression.
4107 The standard does not require that the expression be non-volatile.
4108 G++ implements the proposed correction in DR 457. */
4110 bool
4111 decl_constant_var_p (tree decl)
4113 if (!decl_maybe_constant_var_p (decl))
4114 return false;
4116 /* We don't know if a template static data member is initialized with
4117 a constant expression until we instantiate its initializer. Even
4118 in the case of a constexpr variable, we can't treat it as a
4119 constant until its initializer is complete in case it's used in
4120 its own initializer. */
4121 maybe_instantiate_decl (decl);
4122 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4125 /* Returns true if DECL could be a symbolic constant variable, depending on
4126 its initializer. */
4128 bool
4129 decl_maybe_constant_var_p (tree decl)
4131 tree type = TREE_TYPE (decl);
4132 if (!VAR_P (decl))
4133 return false;
4134 if (DECL_DECLARED_CONSTEXPR_P (decl))
4135 return true;
4136 if (DECL_HAS_VALUE_EXPR_P (decl))
4137 /* A proxy isn't constant. */
4138 return false;
4139 if (TREE_CODE (type) == REFERENCE_TYPE)
4140 /* References can be constant. */;
4141 else if (CP_TYPE_CONST_NON_VOLATILE_P (type)
4142 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4143 /* And const integers. */;
4144 else
4145 return false;
4147 if (DECL_INITIAL (decl)
4148 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
4149 /* We know the initializer, and it isn't constant. */
4150 return false;
4151 else
4152 return true;
4155 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4156 called from grokfndecl and grokvardecl; in all modes it is called from
4157 cp_write_global_declarations. */
4159 void
4160 no_linkage_error (tree decl)
4162 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4163 /* In C++11 it's ok if the decl is defined. */
4164 return;
4165 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4166 if (t == NULL_TREE)
4167 /* The type that got us on no_linkage_decls must have gotten a name for
4168 linkage purposes. */;
4169 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4170 /* The type might end up having a typedef name for linkage purposes. */
4171 vec_safe_push (no_linkage_decls, decl);
4172 else if (TYPE_UNNAMED_P (t))
4174 bool d = false;
4175 if (cxx_dialect >= cxx11)
4176 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4177 "unnamed type, is used but never defined", decl);
4178 else if (DECL_EXTERN_C_P (decl))
4179 /* Allow this; it's pretty common in C. */;
4180 else if (VAR_P (decl))
4181 /* DRs 132, 319 and 389 seem to indicate types with
4182 no linkage can only be used to declare extern "C"
4183 entities. Since it's not always an error in the
4184 ISO C++ 90 Standard, we only issue a warning. */
4185 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "unnamed type "
4186 "with no linkage used to declare variable %q#D with "
4187 "linkage", decl);
4188 else
4189 d = permerror (DECL_SOURCE_LOCATION (decl), "unnamed type with no "
4190 "linkage used to declare function %q#D with linkage",
4191 decl);
4192 if (d && is_typedef_decl (TYPE_NAME (t)))
4193 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4194 "to the unqualified type, so it is not used for linkage",
4195 TYPE_NAME (t));
4197 else if (cxx_dialect >= cxx11)
4199 if (VAR_P (decl) || !DECL_PURE_VIRTUAL_P (decl))
4200 permerror (DECL_SOURCE_LOCATION (decl),
4201 "%q#D, declared using local type "
4202 "%qT, is used but never defined", decl, t);
4204 else if (VAR_P (decl))
4205 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4206 "used to declare variable %q#D with linkage", t, decl);
4207 else
4208 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4209 "to declare function %q#D with linkage", t, decl);
4212 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4214 static void
4215 collect_all_refs (const char *source_file)
4217 collect_ada_namespace (global_namespace, source_file);
4220 /* Clear DECL_EXTERNAL for NODE. */
4222 static bool
4223 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4225 DECL_EXTERNAL (node->decl) = 0;
4226 return false;
4229 /* Build up the function to run dynamic initializers for thread_local
4230 variables in this translation unit and alias the init functions for the
4231 individual variables to it. */
4233 static void
4234 handle_tls_init (void)
4236 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4237 if (vars == NULL_TREE)
4238 return;
4240 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4242 write_out_vars (vars);
4244 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4245 boolean_type_node);
4246 TREE_PUBLIC (guard) = false;
4247 TREE_STATIC (guard) = true;
4248 DECL_ARTIFICIAL (guard) = true;
4249 DECL_IGNORED_P (guard) = true;
4250 TREE_USED (guard) = true;
4251 CP_DECL_THREAD_LOCAL_P (guard) = true;
4252 set_decl_tls_model (guard, decl_default_tls_model (guard));
4253 pushdecl_top_level_and_finish (guard, NULL_TREE);
4255 tree fn = get_local_tls_init_fn ();
4256 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4257 tree body = begin_function_body ();
4258 tree if_stmt = begin_if_stmt ();
4259 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4260 tf_warning_or_error);
4261 finish_if_stmt_cond (cond, if_stmt);
4262 finish_expr_stmt (cp_build_modify_expr (loc, guard, NOP_EXPR,
4263 boolean_true_node,
4264 tf_warning_or_error));
4265 for (; vars; vars = TREE_CHAIN (vars))
4267 tree var = TREE_VALUE (vars);
4268 tree init = TREE_PURPOSE (vars);
4269 one_static_initialization_or_destruction (var, init, true);
4271 /* Output init aliases even with -fno-extern-tls-init. */
4272 if (TARGET_SUPPORTS_ALIASES && TREE_PUBLIC (var))
4274 tree single_init_fn = get_tls_init_fn (var);
4275 if (single_init_fn == NULL_TREE)
4276 continue;
4277 cgraph_node *alias
4278 = cgraph_node::get_create (fn)->create_same_body_alias
4279 (single_init_fn, fn);
4280 gcc_assert (alias != NULL);
4284 finish_then_clause (if_stmt);
4285 finish_if_stmt (if_stmt);
4286 finish_function_body (body);
4287 expand_or_defer_fn (finish_function (0));
4290 /* We're at the end of compilation, so generate any mangling aliases that
4291 we've been saving up, if DECL is going to be output and ID2 isn't
4292 already taken by another declaration. */
4294 static void
4295 generate_mangling_alias (tree decl, tree id2)
4297 struct cgraph_node *n = NULL;
4299 if (TREE_CODE (decl) == FUNCTION_DECL)
4301 n = cgraph_node::get (decl);
4302 if (!n)
4303 /* Don't create an alias to an unreferenced function. */
4304 return;
4307 bool existed;
4308 tree *slot = &mangled_decls->get_or_insert (id2, &existed);
4310 /* If there's a declaration already using this mangled name,
4311 don't create a compatibility alias that conflicts. */
4312 if (existed)
4313 return;
4315 tree alias = make_alias_for (decl, id2);
4316 *slot = alias;
4318 DECL_IGNORED_P (alias) = 1;
4319 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
4320 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
4321 if (vague_linkage_p (decl))
4322 DECL_WEAK (alias) = 1;
4324 if (n)
4325 n->create_same_body_alias (alias, decl);
4326 else
4327 varpool_node::create_extra_name_alias (alias, decl);
4330 /* Note that we might want to emit an alias with the symbol ID2 for DECL at
4331 the end of translation, for compatibility across bugs in the mangling
4332 implementation. */
4334 void
4335 note_mangling_alias (tree decl, tree id2)
4337 if (TARGET_SUPPORTS_ALIASES)
4339 if (!defer_mangling_aliases)
4340 generate_mangling_alias (decl, id2);
4341 else
4343 vec_safe_push (mangling_aliases, decl);
4344 vec_safe_push (mangling_aliases, id2);
4349 /* Emit all mangling aliases that were deferred up to this point. */
4351 void
4352 generate_mangling_aliases ()
4354 while (!vec_safe_is_empty (mangling_aliases))
4356 tree id2 = mangling_aliases->pop();
4357 tree decl = mangling_aliases->pop();
4358 generate_mangling_alias (decl, id2);
4360 defer_mangling_aliases = false;
4363 /* Record a mangling of DECL, whose DECL_ASSEMBLER_NAME has just been
4364 set. NEED_WARNING is true if we must warn about collisions. We do
4365 this to spot changes in mangling that may require compatibility
4366 aliases. */
4368 void
4369 record_mangling (tree decl, bool need_warning)
4371 if (!mangled_decls)
4372 mangled_decls = hash_map<lang_identifier *, tree>::create_ggc (499);
4374 gcc_checking_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
4375 tree id = DECL_ASSEMBLER_NAME (decl);
4376 bool existed;
4377 tree *slot = &mangled_decls->get_or_insert (id, &existed);
4379 /* If this is already an alias, remove the alias, because the real
4380 decl takes presidence. */
4381 if (!existed)
4383 else if (DECL_ARTIFICIAL (*slot) && DECL_IGNORED_P (*slot))
4384 if (symtab_node *n = symtab_node::get (*slot))
4385 if (n->cpp_implicit_alias)
4387 n->remove ();
4388 existed = false;
4391 if (!existed)
4392 *slot = decl;
4393 else if (need_warning)
4395 error_at (DECL_SOURCE_LOCATION (decl),
4396 "mangling of %q#D as %qE conflicts with a previous mangle",
4397 decl, id);
4398 inform (DECL_SOURCE_LOCATION (*slot),
4399 "previous mangling %q#D", *slot);
4400 inform (DECL_SOURCE_LOCATION (decl),
4401 "a later -fabi-version= (or =0)"
4402 " avoids this error with a change in mangling");
4403 *slot = decl;
4407 /* The entire file is now complete. If requested, dump everything
4408 to a file. */
4410 static void
4411 dump_tu (void)
4413 dump_flags_t flags;
4414 if (FILE *stream = dump_begin (raw_dump_id, &flags))
4416 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4417 dump_end (raw_dump_id, stream);
4421 static location_t locus_at_end_of_parsing;
4423 /* Check the deallocation functions for CODE to see if we want to warn that
4424 only one was defined. */
4426 static void
4427 maybe_warn_sized_delete (enum tree_code code)
4429 tree sized = NULL_TREE;
4430 tree unsized = NULL_TREE;
4432 for (ovl_iterator iter (IDENTIFIER_GLOBAL_VALUE (cp_operator_id (code)));
4433 iter; ++iter)
4435 tree fn = *iter;
4436 /* We're only interested in usual deallocation functions. */
4437 if (!usual_deallocation_fn_p (fn))
4438 continue;
4439 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
4440 unsized = fn;
4441 else
4442 sized = fn;
4444 if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
4445 warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
4446 "the program should also define %qD", sized);
4447 else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
4448 warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
4449 "the program should also define %qD", unsized);
4452 /* Check the global deallocation functions to see if we want to warn about
4453 defining unsized without sized (or vice versa). */
4455 static void
4456 maybe_warn_sized_delete ()
4458 if (!flag_sized_deallocation || !warn_sized_deallocation)
4459 return;
4460 maybe_warn_sized_delete (DELETE_EXPR);
4461 maybe_warn_sized_delete (VEC_DELETE_EXPR);
4464 /* Earlier we left PTRMEM_CST in variable initializers alone so that we could
4465 look them up when evaluating non-type template parameters. Now we need to
4466 lower them to something the back end can understand. */
4468 static void
4469 lower_var_init ()
4471 varpool_node *node;
4472 FOR_EACH_VARIABLE (node)
4474 tree d = node->decl;
4475 if (tree init = DECL_INITIAL (d))
4476 DECL_INITIAL (d) = cplus_expand_constant (init);
4480 /* This routine is called at the end of compilation.
4481 Its job is to create all the code needed to initialize and
4482 destroy the global aggregates. We do the destruction
4483 first, since that way we only need to reverse the decls once. */
4485 void
4486 c_parse_final_cleanups (void)
4488 tree vars;
4489 bool reconsider;
4490 size_t i;
4491 unsigned ssdf_count = 0;
4492 int retries = 0;
4493 tree decl;
4495 locus_at_end_of_parsing = input_location;
4496 at_eof = 1;
4498 /* Bad parse errors. Just forget about it. */
4499 if (! global_bindings_p () || current_class_type
4500 || !vec_safe_is_empty (decl_namespace_list))
4501 return;
4503 /* This is the point to write out a PCH if we're doing that.
4504 In that case we do not want to do anything else. */
4505 if (pch_file)
4507 /* Mangle all symbols at PCH creation time. */
4508 symtab_node *node;
4509 FOR_EACH_SYMBOL (node)
4510 if (! is_a <varpool_node *> (node)
4511 || ! DECL_HARD_REGISTER (node->decl))
4512 DECL_ASSEMBLER_NAME (node->decl);
4513 c_common_write_pch ();
4514 dump_tu ();
4515 /* Ensure even the callers don't try to finalize the CU. */
4516 flag_syntax_only = 1;
4517 return;
4520 timevar_stop (TV_PHASE_PARSING);
4521 timevar_start (TV_PHASE_DEFERRED);
4523 symtab->process_same_body_aliases ();
4525 /* Handle -fdump-ada-spec[-slim] */
4526 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4528 if (flag_dump_ada_spec_slim)
4529 collect_source_ref (main_input_filename);
4530 else
4531 collect_source_refs (global_namespace);
4533 dump_ada_specs (collect_all_refs, cpp_check);
4536 /* FIXME - huh? was input_line -= 1;*/
4538 /* We now have to write out all the stuff we put off writing out.
4539 These include:
4541 o Template specializations that we have not yet instantiated,
4542 but which are needed.
4543 o Initialization and destruction for non-local objects with
4544 static storage duration. (Local objects with static storage
4545 duration are initialized when their scope is first entered,
4546 and are cleaned up via atexit.)
4547 o Virtual function tables.
4549 All of these may cause others to be needed. For example,
4550 instantiating one function may cause another to be needed, and
4551 generating the initializer for an object may cause templates to be
4552 instantiated, etc., etc. */
4554 emit_support_tinfos ();
4558 tree t;
4559 tree decl;
4561 reconsider = false;
4563 /* If there are templates that we've put off instantiating, do
4564 them now. */
4565 instantiate_pending_templates (retries);
4566 ggc_collect ();
4568 /* Write out virtual tables as required. Writing out the
4569 virtual table for a template class may cause the
4570 instantiation of members of that class. If we write out
4571 vtables then we remove the class from our list so we don't
4572 have to look at it again. */
4573 for (i = keyed_classes->length ();
4574 keyed_classes->iterate (--i, &t);)
4575 if (maybe_emit_vtables (t))
4577 reconsider = true;
4578 keyed_classes->unordered_remove (i);
4581 /* Write out needed type info variables. We have to be careful
4582 looping through unemitted decls, because emit_tinfo_decl may
4583 cause other variables to be needed. New elements will be
4584 appended, and we remove from the vector those that actually
4585 get emitted. */
4586 for (i = unemitted_tinfo_decls->length ();
4587 unemitted_tinfo_decls->iterate (--i, &t);)
4588 if (emit_tinfo_decl (t))
4590 reconsider = true;
4591 unemitted_tinfo_decls->unordered_remove (i);
4594 /* The list of objects with static storage duration is built up
4595 in reverse order. We clear STATIC_AGGREGATES so that any new
4596 aggregates added during the initialization of these will be
4597 initialized in the correct order when we next come around the
4598 loop. */
4599 vars = prune_vars_needing_no_initialization (&static_aggregates);
4601 if (vars)
4603 /* We need to start a new initialization function each time
4604 through the loop. That's because we need to know which
4605 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4606 isn't computed until a function is finished, and written
4607 out. That's a deficiency in the back end. When this is
4608 fixed, these initialization functions could all become
4609 inline, with resulting performance improvements. */
4610 tree ssdf_body;
4612 /* Set the line and file, so that it is obviously not from
4613 the source file. */
4614 input_location = locus_at_end_of_parsing;
4615 ssdf_body = start_static_storage_duration_function (ssdf_count);
4617 /* Make sure the back end knows about all the variables. */
4618 write_out_vars (vars);
4620 /* First generate code to do all the initializations. */
4621 if (vars)
4622 do_static_initialization_or_destruction (vars, /*initp=*/true);
4624 /* Then, generate code to do all the destructions. Do these
4625 in reverse order so that the most recently constructed
4626 variable is the first destroyed. If we're using
4627 __cxa_atexit, then we don't need to do this; functions
4628 were registered at initialization time to destroy the
4629 local statics. */
4630 if (!flag_use_cxa_atexit && vars)
4632 vars = nreverse (vars);
4633 do_static_initialization_or_destruction (vars, /*initp=*/false);
4635 else
4636 vars = NULL_TREE;
4638 /* Finish up the static storage duration function for this
4639 round. */
4640 input_location = locus_at_end_of_parsing;
4641 finish_static_storage_duration_function (ssdf_body);
4643 /* All those initializations and finalizations might cause
4644 us to need more inline functions, more template
4645 instantiations, etc. */
4646 reconsider = true;
4647 ssdf_count++;
4648 /* ??? was: locus_at_end_of_parsing.line++; */
4651 /* Now do the same for thread_local variables. */
4652 handle_tls_init ();
4654 /* Go through the set of inline functions whose bodies have not
4655 been emitted yet. If out-of-line copies of these functions
4656 are required, emit them. */
4657 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4659 /* Does it need synthesizing? */
4660 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4661 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4663 /* Even though we're already at the top-level, we push
4664 there again. That way, when we pop back a few lines
4665 hence, all of our state is restored. Otherwise,
4666 finish_function doesn't clean things up, and we end
4667 up with CURRENT_FUNCTION_DECL set. */
4668 push_to_top_level ();
4669 /* The decl's location will mark where it was first
4670 needed. Save that so synthesize method can indicate
4671 where it was needed from, in case of error */
4672 input_location = DECL_SOURCE_LOCATION (decl);
4673 synthesize_method (decl);
4674 pop_from_top_level ();
4675 reconsider = true;
4678 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4679 generate_tls_wrapper (decl);
4681 if (!DECL_SAVED_TREE (decl))
4682 continue;
4684 cgraph_node *node = cgraph_node::get_create (decl);
4686 /* We lie to the back end, pretending that some functions
4687 are not defined when they really are. This keeps these
4688 functions from being put out unnecessarily. But, we must
4689 stop lying when the functions are referenced, or if they
4690 are not comdat since they need to be put out now. If
4691 DECL_INTERFACE_KNOWN, then we have already set
4692 DECL_EXTERNAL appropriately, so there's no need to check
4693 again, and we do not want to clear DECL_EXTERNAL if a
4694 previous call to import_export_decl set it.
4696 This is done in a separate for cycle, because if some
4697 deferred function is contained in another deferred
4698 function later in deferred_fns varray,
4699 rest_of_compilation would skip this function and we
4700 really cannot expand the same function twice. */
4701 import_export_decl (decl);
4702 if (DECL_NOT_REALLY_EXTERN (decl)
4703 && DECL_INITIAL (decl)
4704 && decl_needed_p (decl))
4706 if (node->cpp_implicit_alias)
4707 node = node->get_alias_target ();
4709 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
4710 NULL, true);
4711 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4712 group, we need to mark all symbols in the same comdat group
4713 that way. */
4714 if (node->same_comdat_group)
4715 for (cgraph_node *next
4716 = dyn_cast<cgraph_node *> (node->same_comdat_group);
4717 next != node;
4718 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
4719 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
4720 NULL, true);
4723 /* If we're going to need to write this function out, and
4724 there's already a body for it, create RTL for it now.
4725 (There might be no body if this is a method we haven't
4726 gotten around to synthesizing yet.) */
4727 if (!DECL_EXTERNAL (decl)
4728 && decl_needed_p (decl)
4729 && !TREE_ASM_WRITTEN (decl)
4730 && !node->definition)
4732 /* We will output the function; no longer consider it in this
4733 loop. */
4734 DECL_DEFER_OUTPUT (decl) = 0;
4735 /* Generate RTL for this function now that we know we
4736 need it. */
4737 expand_or_defer_fn (decl);
4738 /* If we're compiling -fsyntax-only pretend that this
4739 function has been written out so that we don't try to
4740 expand it again. */
4741 if (flag_syntax_only)
4742 TREE_ASM_WRITTEN (decl) = 1;
4743 reconsider = true;
4747 if (wrapup_namespace_globals ())
4748 reconsider = true;
4750 /* Static data members are just like namespace-scope globals. */
4751 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4753 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4754 /* Don't write it out if we haven't seen a definition. */
4755 || (DECL_IN_AGGR_P (decl) && !DECL_INLINE_VAR_P (decl)))
4756 continue;
4757 import_export_decl (decl);
4758 /* If this static data member is needed, provide it to the
4759 back end. */
4760 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4761 DECL_EXTERNAL (decl) = 0;
4763 if (vec_safe_length (pending_statics) != 0
4764 && wrapup_global_declarations (pending_statics->address (),
4765 pending_statics->length ()))
4766 reconsider = true;
4768 retries++;
4770 while (reconsider);
4772 lower_var_init ();
4774 generate_mangling_aliases ();
4776 /* All used inline functions must have a definition at this point. */
4777 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4779 if (/* Check online inline functions that were actually used. */
4780 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
4781 /* If the definition actually was available here, then the
4782 fact that the function was not defined merely represents
4783 that for some reason (use of a template repository,
4784 #pragma interface, etc.) we decided not to emit the
4785 definition here. */
4786 && !DECL_INITIAL (decl)
4787 /* Don't complain if the template was defined. */
4788 && !(DECL_TEMPLATE_INSTANTIATION (decl)
4789 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4790 (template_for_substitution (decl)))))
4792 warning_at (DECL_SOURCE_LOCATION (decl), 0,
4793 "inline function %qD used but never defined", decl);
4794 /* Avoid a duplicate warning from check_global_declaration. */
4795 TREE_NO_WARNING (decl) = 1;
4799 /* So must decls that use a type with no linkage. */
4800 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
4801 no_linkage_error (decl);
4803 maybe_warn_sized_delete ();
4805 /* Then, do the Objective-C stuff. This is where all the
4806 Objective-C module stuff gets generated (symtab,
4807 class/protocol/selector lists etc). This must be done after C++
4808 templates, destructors etc. so that selectors used in C++
4809 templates are properly allocated. */
4810 if (c_dialect_objc ())
4811 objc_write_global_declarations ();
4813 /* We give C linkage to static constructors and destructors. */
4814 push_lang_context (lang_name_c);
4816 /* Generate initialization and destruction functions for all
4817 priorities for which they are required. */
4818 if (priority_info_map)
4819 splay_tree_foreach (priority_info_map,
4820 generate_ctor_and_dtor_functions_for_priority,
4821 /*data=*/&locus_at_end_of_parsing);
4822 else if (c_dialect_objc () && objc_static_init_needed_p ())
4823 /* If this is obj-c++ and we need a static init, call
4824 generate_ctor_or_dtor_function. */
4825 generate_ctor_or_dtor_function (/*constructor_p=*/true,
4826 DEFAULT_INIT_PRIORITY,
4827 &locus_at_end_of_parsing);
4829 /* We're done with the splay-tree now. */
4830 if (priority_info_map)
4831 splay_tree_delete (priority_info_map);
4833 /* Generate any missing aliases. */
4834 maybe_apply_pending_pragma_weaks ();
4836 /* We're done with static constructors, so we can go back to "C++"
4837 linkage now. */
4838 pop_lang_context ();
4840 if (flag_vtable_verify)
4842 vtv_recover_class_info ();
4843 vtv_compute_class_hierarchy_transitive_closure ();
4844 vtv_build_vtable_verify_fndecl ();
4847 perform_deferred_noexcept_checks ();
4849 finish_repo ();
4850 fini_constexpr ();
4852 /* The entire file is now complete. If requested, dump everything
4853 to a file. */
4854 dump_tu ();
4856 if (flag_detailed_statistics)
4858 dump_tree_statistics ();
4859 dump_time_statistics ();
4862 timevar_stop (TV_PHASE_DEFERRED);
4863 timevar_start (TV_PHASE_PARSING);
4865 /* Indicate that we're done with front end processing. */
4866 at_eof = 2;
4869 /* Perform any post compilation-proper cleanups for the C++ front-end.
4870 This should really go away. No front-end should need to do
4871 anything past the compilation process. */
4873 void
4874 cxx_post_compilation_parsing_cleanups (void)
4876 timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS);
4878 if (flag_vtable_verify)
4880 /* Generate the special constructor initialization function that
4881 calls __VLTRegisterPairs, and give it a very high
4882 initialization priority. This must be done after
4883 finalize_compilation_unit so that we have accurate
4884 information about which vtable will actually be emitted. */
4885 vtv_generate_init_routine ();
4888 input_location = locus_at_end_of_parsing;
4890 if (flag_checking)
4891 validate_conversion_obstack ();
4893 timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS);
4896 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4897 function to call in parse-tree form; it has not yet been
4898 semantically analyzed. ARGS are the arguments to the function.
4899 They have already been semantically analyzed. This may change
4900 ARGS. */
4902 tree
4903 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
4904 tsubst_flags_t complain)
4906 tree orig_fn;
4907 vec<tree, va_gc> *orig_args = NULL;
4908 tree expr;
4909 tree object;
4911 orig_fn = fn;
4912 object = TREE_OPERAND (fn, 0);
4914 if (processing_template_decl)
4916 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4917 || TREE_CODE (fn) == MEMBER_REF);
4918 if (type_dependent_expression_p (fn)
4919 || any_type_dependent_arguments_p (*args))
4920 return build_min_nt_call_vec (fn, *args);
4922 orig_args = make_tree_vector_copy (*args);
4924 /* Transform the arguments and add the implicit "this"
4925 parameter. That must be done before the FN is transformed
4926 because we depend on the form of FN. */
4927 make_args_non_dependent (*args);
4928 object = build_non_dependent_expr (object);
4929 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4931 if (TREE_CODE (fn) == DOTSTAR_EXPR)
4932 object = cp_build_addr_expr (object, complain);
4933 vec_safe_insert (*args, 0, object);
4935 /* Now that the arguments are done, transform FN. */
4936 fn = build_non_dependent_expr (fn);
4939 /* A qualified name corresponding to a bound pointer-to-member is
4940 represented as an OFFSET_REF:
4942 struct B { void g(); };
4943 void (B::*p)();
4944 void B::g() { (this->*p)(); } */
4945 if (TREE_CODE (fn) == OFFSET_REF)
4947 tree object_addr = cp_build_addr_expr (object, complain);
4948 fn = TREE_OPERAND (fn, 1);
4949 fn = get_member_function_from_ptrfunc (&object_addr, fn,
4950 complain);
4951 vec_safe_insert (*args, 0, object_addr);
4954 if (CLASS_TYPE_P (TREE_TYPE (fn)))
4955 expr = build_op_call (fn, args, complain);
4956 else
4957 expr = cp_build_function_call_vec (fn, args, complain);
4958 if (processing_template_decl && expr != error_mark_node)
4959 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
4961 if (orig_args != NULL)
4962 release_tree_vector (orig_args);
4964 return expr;
4968 void
4969 check_default_args (tree x)
4971 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4972 bool saw_def = false;
4973 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4974 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4976 if (TREE_PURPOSE (arg))
4977 saw_def = true;
4978 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
4980 error ("default argument missing for parameter %P of %q+#D", i, x);
4981 TREE_PURPOSE (arg) = error_mark_node;
4986 /* Return true if function DECL can be inlined. This is used to force
4987 instantiation of methods that might be interesting for inlining. */
4988 bool
4989 possibly_inlined_p (tree decl)
4991 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4992 if (DECL_UNINLINABLE (decl))
4993 return false;
4994 if (!optimize)
4995 return DECL_DECLARED_INLINE_P (decl);
4996 /* When optimizing, we might inline everything when flatten
4997 attribute or heuristics inlining for size or autoinlining
4998 is used. */
4999 return true;
5002 /* Normally, we can wait until instantiation-time to synthesize DECL.
5003 However, if DECL is a static data member initialized with a constant
5004 or a constexpr function, we need it right now because a reference to
5005 such a data member or a call to such function is not value-dependent.
5006 For a function that uses auto in the return type, we need to instantiate
5007 it to find out its type. For OpenMP user defined reductions, we need
5008 them instantiated for reduction clauses which inline them by hand
5009 directly. */
5011 static void
5012 maybe_instantiate_decl (tree decl)
5014 if (DECL_LANG_SPECIFIC (decl)
5015 && DECL_TEMPLATE_INFO (decl)
5016 && (decl_maybe_constant_var_p (decl)
5017 || (TREE_CODE (decl) == FUNCTION_DECL
5018 && DECL_OMP_DECLARE_REDUCTION_P (decl))
5019 || undeduced_auto_decl (decl))
5020 && !DECL_DECLARED_CONCEPT_P (decl)
5021 && !uses_template_parms (DECL_TI_ARGS (decl)))
5023 /* Instantiating a function will result in garbage collection. We
5024 must treat this situation as if we were within the body of a
5025 function so as to avoid collecting live data only referenced from
5026 the stack (such as overload resolution candidates). */
5027 ++function_depth;
5028 instantiate_decl (decl, /*defer_ok=*/false,
5029 /*expl_inst_class_mem_p=*/false);
5030 --function_depth;
5034 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
5035 If DECL is a specialization or implicitly declared class member,
5036 generate the actual definition. Return false if something goes
5037 wrong, true otherwise. */
5039 bool
5040 mark_used (tree decl, tsubst_flags_t complain)
5042 /* If DECL is a BASELINK for a single function, then treat it just
5043 like the DECL for the function. Otherwise, if the BASELINK is
5044 for an overloaded function, we don't know which function was
5045 actually used until after overload resolution. */
5046 if (BASELINK_P (decl))
5048 decl = BASELINK_FUNCTIONS (decl);
5049 if (really_overloaded_fn (decl))
5050 return true;
5051 decl = OVL_FIRST (decl);
5054 /* Set TREE_USED for the benefit of -Wunused. */
5055 TREE_USED (decl) = 1;
5056 /* And for structured bindings also the underlying decl. */
5057 if (DECL_DECOMPOSITION_P (decl) && DECL_DECOMP_BASE (decl))
5058 TREE_USED (DECL_DECOMP_BASE (decl)) = 1;
5060 if (TREE_CODE (decl) == TEMPLATE_DECL)
5061 return true;
5063 if (DECL_CLONED_FUNCTION_P (decl))
5064 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5066 /* Mark enumeration types as used. */
5067 if (TREE_CODE (decl) == CONST_DECL)
5068 used_types_insert (DECL_CONTEXT (decl));
5070 if (TREE_CODE (decl) == FUNCTION_DECL
5071 && !maybe_instantiate_noexcept (decl, complain))
5072 return false;
5074 if (TREE_CODE (decl) == FUNCTION_DECL
5075 && DECL_DELETED_FN (decl))
5077 if (DECL_ARTIFICIAL (decl)
5078 && DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
5079 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
5080 /* We mark a lambda conversion op as deleted if we can't
5081 generate it properly; see maybe_add_lambda_conv_op. */
5082 sorry ("converting lambda which uses %<...%> to function pointer");
5083 else if (complain & tf_error)
5085 error ("use of deleted function %qD", decl);
5086 if (!maybe_explain_implicit_delete (decl))
5087 inform (DECL_SOURCE_LOCATION (decl), "declared here");
5089 return false;
5092 if (TREE_DEPRECATED (decl) && (complain & tf_warning)
5093 && deprecated_state != DEPRECATED_SUPPRESS)
5094 warn_deprecated_use (decl, NULL_TREE);
5096 /* We can only check DECL_ODR_USED on variables or functions with
5097 DECL_LANG_SPECIFIC set, and these are also the only decls that we
5098 might need special handling for. */
5099 if (!VAR_OR_FUNCTION_DECL_P (decl)
5100 || DECL_LANG_SPECIFIC (decl) == NULL
5101 || DECL_THUNK_P (decl))
5103 if (!processing_template_decl
5104 && !require_deduced_type (decl, complain))
5105 return false;
5106 return true;
5109 /* We only want to do this processing once. We don't need to keep trying
5110 to instantiate inline templates, because unit-at-a-time will make sure
5111 we get them compiled before functions that want to inline them. */
5112 if (DECL_ODR_USED (decl))
5113 return true;
5115 /* Normally, we can wait until instantiation-time to synthesize DECL.
5116 However, if DECL is a static data member initialized with a constant
5117 or a constexpr function, we need it right now because a reference to
5118 such a data member or a call to such function is not value-dependent.
5119 For a function that uses auto in the return type, we need to instantiate
5120 it to find out its type. For OpenMP user defined reductions, we need
5121 them instantiated for reduction clauses which inline them by hand
5122 directly. */
5123 maybe_instantiate_decl (decl);
5125 if (processing_template_decl || in_template_function ())
5126 return true;
5128 /* Check this too in case we're within instantiate_non_dependent_expr. */
5129 if (DECL_TEMPLATE_INFO (decl)
5130 && uses_template_parms (DECL_TI_ARGS (decl)))
5131 return true;
5133 if (!require_deduced_type (decl, complain))
5134 return false;
5136 if (builtin_pack_fn_p (decl))
5138 error ("use of built-in parameter pack %qD outside of a template",
5139 DECL_NAME (decl));
5140 return false;
5143 /* If we don't need a value, then we don't need to synthesize DECL. */
5144 if (cp_unevaluated_operand || in_discarded_stmt)
5145 return true;
5147 DECL_ODR_USED (decl) = 1;
5148 if (DECL_CLONED_FUNCTION_P (decl))
5149 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5151 /* DR 757: A type without linkage shall not be used as the type of a
5152 variable or function with linkage, unless
5153 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5154 o the variable or function is not used (3.2 [basic.def.odr]) or is
5155 defined in the same translation unit. */
5156 if (cxx_dialect > cxx98
5157 && decl_linkage (decl) != lk_none
5158 && !DECL_EXTERN_C_P (decl)
5159 && !DECL_ARTIFICIAL (decl)
5160 && !decl_defined_p (decl)
5161 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
5163 if (is_local_extern (decl))
5164 /* There's no way to define a local extern, and adding it to
5165 the vector interferes with GC, so give an error now. */
5166 no_linkage_error (decl);
5167 else
5168 vec_safe_push (no_linkage_decls, decl);
5171 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
5172 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
5173 /* Remember it, so we can check it was defined. */
5174 note_vague_linkage_fn (decl);
5176 /* Is it a synthesized method that needs to be synthesized? */
5177 if (TREE_CODE (decl) == FUNCTION_DECL
5178 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5179 && DECL_DEFAULTED_FN (decl)
5180 /* A function defaulted outside the class is synthesized either by
5181 cp_finish_decl or instantiate_decl. */
5182 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
5183 && ! DECL_INITIAL (decl))
5185 /* Defer virtual destructors so that thunks get the right
5186 linkage. */
5187 if (DECL_VIRTUAL_P (decl) && !at_eof)
5189 note_vague_linkage_fn (decl);
5190 return true;
5193 /* Remember the current location for a function we will end up
5194 synthesizing. Then we can inform the user where it was
5195 required in the case of error. */
5196 DECL_SOURCE_LOCATION (decl) = input_location;
5198 /* Synthesizing an implicitly defined member function will result in
5199 garbage collection. We must treat this situation as if we were
5200 within the body of a function so as to avoid collecting live data
5201 on the stack (such as overload resolution candidates).
5203 We could just let cp_write_global_declarations handle synthesizing
5204 this function by adding it to deferred_fns, but doing
5205 it at the use site produces better error messages. */
5206 ++function_depth;
5207 synthesize_method (decl);
5208 --function_depth;
5209 /* If this is a synthesized method we don't need to
5210 do the instantiation test below. */
5212 else if (VAR_OR_FUNCTION_DECL_P (decl)
5213 && DECL_TEMPLATE_INFO (decl)
5214 && !DECL_DECLARED_CONCEPT_P (decl)
5215 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5216 || always_instantiate_p (decl)))
5217 /* If this is a function or variable that is an instance of some
5218 template, we now know that we will need to actually do the
5219 instantiation. We check that DECL is not an explicit
5220 instantiation because that is not checked in instantiate_decl.
5222 We put off instantiating functions in order to improve compile
5223 times. Maintaining a stack of active functions is expensive,
5224 and the inliner knows to instantiate any functions it might
5225 need. Therefore, we always try to defer instantiation. */
5227 ++function_depth;
5228 instantiate_decl (decl, /*defer_ok=*/true,
5229 /*expl_inst_class_mem_p=*/false);
5230 --function_depth;
5233 return true;
5236 bool
5237 mark_used (tree decl)
5239 return mark_used (decl, tf_warning_or_error);
5242 tree
5243 vtv_start_verification_constructor_init_function (void)
5245 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5248 tree
5249 vtv_finish_verification_constructor_init_function (tree function_body)
5251 tree fn;
5253 finish_compound_stmt (function_body);
5254 fn = finish_function (0);
5255 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5256 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5258 return fn;
5261 #include "gt-cp-decl2.h"