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)
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 3, or (at your option)
10any later version.
11
12GCC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
21
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. */
25
26/* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
28
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"
51
52/* Id for dumping the raw trees. */
53int raw_dump_id;
54
55extern cpp_reader *parse_in;
56
57/* This structure contains information about the initializations
58 and/or destructions required for a particular priority level. */
59typedef 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;
67
68static void mark_vtable_entries (tree);
69static bool maybe_emit_vtables (tree);
70static tree start_objects (int, int);
71static void finish_objects (int, int, tree);
72static tree start_static_storage_duration_function (unsigned);
73static void finish_static_storage_duration_function (tree);
74static priority_info get_priority_info (int);
75static void do_static_initialization_or_destruction (tree, bool);
76static void one_static_initialization_or_destruction (tree, tree, bool);
77static void generate_ctor_or_dtor_function (bool, int, location_t *);
78static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
79 void *);
80static tree prune_vars_needing_no_initialization (tree *);
81static void write_out_vars (tree);
82static void import_export_class (tree);
83static tree get_guard_bits (tree);
84static void determine_visibility_from_class (tree, tree);
85static bool determine_hidden_inline (tree);
86static void maybe_instantiate_decl (tree);
87
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. */
91static GTY(()) vec<tree, va_gc> *pending_statics;
92
93/* A list of functions which were declared inline, but which we
94 may need to emit outline anyway. */
95static GTY(()) vec<tree, va_gc> *deferred_fns;
96
97/* A list of decls that use types with no linkage, which we need to make
98 sure are defined. */
99static GTY(()) vec<tree, va_gc> *no_linkage_decls;
100
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. */
103static GTY(()) vec<tree, va_gc> *mangling_aliases;
104
105/* hash traits for declarations. Hashes single decls via
106 DECL_ASSEMBLER_NAME_RAW. */
107
108struct mangled_decl_hash : ggc_remove <tree>
109{
110 typedef tree value_type; /* A DECL. */
111 typedef tree compare_type; /* An identifier. */
112
113 static hashval_t hash (const value_type decl)
114 {
115 return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME_RAW (decl));
116 }
117 static bool equal (const value_type existing, compare_type candidate)
118 {
119 tree name = DECL_ASSEMBLER_NAME_RAW (existing);
120 return candidate == name;
121 }
122
123 static inline void mark_empty (value_type &p) {p = NULL_TREE;}
124 static inline bool is_empty (value_type p) {return !p;}
125
126 static bool is_deleted (value_type e)
127 {
128 return e == reinterpret_cast <value_type> (1);
129 }
130 static void mark_deleted (value_type &e)
131 {
132 e = reinterpret_cast <value_type> (1);
133 }
134};
135
136/* A hash table of decls keyed by mangled name. Used to figure out if
137 we need compatibility aliases. */
138static GTY(()) hash_table<mangled_decl_hash> *mangled_decls;
139
140/* Nonzero if we're done parsing and into end-of-file activities. */
141
142int at_eof;
143
144/* True if note_mangling_alias should enqueue mangling aliases for
145 later generation, rather than emitting them right away. */
146
147bool defer_mangling_aliases = true;
148
149
150/* Return a member function type (a METHOD_TYPE), given FNTYPE (a
151 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
152 that apply to the function). */
153
154tree
155build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
156 cp_ref_qualifier rqual)
157{
158 tree raises;
159 tree attrs;
160 int type_quals;
161 bool late_return_type_p;
162
163 if (fntype == error_mark_node || ctype == error_mark_node)
164 return error_mark_node;
165
166 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
167 || TREE_CODE (fntype) == METHOD_TYPE);
168
169 type_quals = quals & ~TYPE_QUAL_RESTRICT;
170 ctype = cp_build_qualified_type (ctype, type_quals);
171 raises = TYPE_RAISES_EXCEPTIONS (fntype);
172 attrs = TYPE_ATTRIBUTES (fntype);
173 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
174 fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
175 (TREE_CODE (fntype) == METHOD_TYPE
176 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
177 : TYPE_ARG_TYPES (fntype)));
178 if (attrs)
179 fntype = cp_build_type_attribute_variant (fntype, attrs);
180 if (rqual)
181 fntype = build_ref_qualified_type (fntype, rqual);
182 if (raises)
183 fntype = build_exception_variant (fntype, raises);
184 if (late_return_type_p)
185 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
186
187 return fntype;
188}
189
190/* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
191 return type changed to NEW_RET. */
192
193tree
194change_return_type (tree new_ret, tree fntype)
195{
196 tree newtype;
197 tree args = TYPE_ARG_TYPES (fntype);
198 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
199 tree attrs = TYPE_ATTRIBUTES (fntype);
200 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
201
202 if (new_ret == error_mark_node)
203 return fntype;
204
205 if (same_type_p (new_ret, TREE_TYPE (fntype)))
206 return fntype;
207
208 if (TREE_CODE (fntype) == FUNCTION_TYPE)
209 {
210 newtype = build_function_type (new_ret, args);
211 newtype = apply_memfn_quals (newtype,
212 type_memfn_quals (fntype),
213 type_memfn_rqual (fntype));
214 }
215 else
216 newtype = build_method_type_directly
217 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
218 if (FUNCTION_REF_QUALIFIED (fntype))
219 newtype = build_ref_qualified_type (newtype, type_memfn_rqual (fntype));
220 if (raises)
221 newtype = build_exception_variant (newtype, raises);
222 if (attrs)
223 newtype = cp_build_type_attribute_variant (newtype, attrs);
224 if (late_return_type_p)
225 TYPE_HAS_LATE_RETURN_TYPE (newtype) = 1;
226
227 return newtype;
228}
229
230/* Build a PARM_DECL of FN with NAME and TYPE, and set DECL_ARG_TYPE
231 appropriately. */
232
233tree
234cp_build_parm_decl (tree fn, tree name, tree type)
235{
236 tree parm = build_decl (input_location,
237 PARM_DECL, name, type);
238 DECL_CONTEXT (parm) = fn;
239
240 /* DECL_ARG_TYPE is only used by the back end and the back end never
241 sees templates. */
242 if (!processing_template_decl)
243 DECL_ARG_TYPE (parm) = type_passed_as (type);
244
245 return parm;
246}
247
248/* Returns a PARM_DECL of FN for a parameter of the indicated TYPE, with the
249 indicated NAME. */
250
251tree
252build_artificial_parm (tree fn, tree name, tree type)
253{
254 tree parm = cp_build_parm_decl (fn, name, type);
255 DECL_ARTIFICIAL (parm) = 1;
256 /* All our artificial parms are implicitly `const'; they cannot be
257 assigned to. */
258 TREE_READONLY (parm) = 1;
259 return parm;
260}
261
262/* Constructors for types with virtual baseclasses need an "in-charge" flag
263 saying whether this constructor is responsible for initialization of
264 virtual baseclasses or not. All destructors also need this "in-charge"
265 flag, which additionally determines whether or not the destructor should
266 free the memory for the object.
267
268 This function adds the "in-charge" flag to member function FN if
269 appropriate. It is called from grokclassfn and tsubst.
270 FN must be either a constructor or destructor.
271
272 The in-charge flag follows the 'this' parameter, and is followed by the
273 VTT parm (if any), then the user-written parms. */
274
275void
276maybe_retrofit_in_chrg (tree fn)
277{
278 tree basetype, arg_types, parms, parm, fntype;
279
280 /* If we've already add the in-charge parameter don't do it again. */
281 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
282 return;
283
284 /* When processing templates we can't know, in general, whether or
285 not we're going to have virtual baseclasses. */
286 if (processing_template_decl)
287 return;
288
289 /* We don't need an in-charge parameter for constructors that don't
290 have virtual bases. */
291 if (DECL_CONSTRUCTOR_P (fn)
292 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
293 return;
294
295 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
296 basetype = TREE_TYPE (TREE_VALUE (arg_types));
297 arg_types = TREE_CHAIN (arg_types);
298
299 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
300
301 /* If this is a subobject constructor or destructor, our caller will
302 pass us a pointer to our VTT. */
303 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
304 {
305 parm = build_artificial_parm (fn, vtt_parm_identifier, vtt_parm_type);
306
307 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
308 DECL_CHAIN (parm) = parms;
309 parms = parm;
310
311 /* ...and then to TYPE_ARG_TYPES. */
312 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
313
314 DECL_HAS_VTT_PARM_P (fn) = 1;
315 }
316
317 /* Then add the in-charge parm (before the VTT parm). */
318 parm = build_artificial_parm (fn, in_charge_identifier, integer_type_node);
319 DECL_CHAIN (parm) = parms;
320 parms = parm;
321 arg_types = hash_tree_chain (integer_type_node, arg_types);
322
323 /* Insert our new parameter(s) into the list. */
324 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
325
326 /* And rebuild the function type. */
327 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
328 arg_types);
329 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
330 fntype = build_exception_variant (fntype,
331 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
332 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
333 fntype = (cp_build_type_attribute_variant
334 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
335 TREE_TYPE (fn) = fntype;
336
337 /* Now we've got the in-charge parameter. */
338 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
339}
340
341/* Classes overload their constituent function names automatically.
342 When a function name is declared in a record structure,
343 its name is changed to it overloaded name. Since names for
344 constructors and destructors can conflict, we place a leading
345 '$' for destructors.
346
347 CNAME is the name of the class we are grokking for.
348
349 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
350
351 FLAGS contains bits saying what's special about today's
352 arguments. DTOR_FLAG == DESTRUCTOR.
353
354 If FUNCTION is a destructor, then we must add the `auto-delete' field
355 as a second parameter. There is some hair associated with the fact
356 that we must "declare" this variable in the manner consistent with the
357 way the rest of the arguments were declared.
358
359 QUALS are the qualifiers for the this pointer. */
360
361void
362grokclassfn (tree ctype, tree function, enum overload_flags flags)
363{
364 tree fn_name = DECL_NAME (function);
365
366 /* Even within an `extern "C"' block, members get C++ linkage. See
367 [dcl.link] for details. */
368 SET_DECL_LANGUAGE (function, lang_cplusplus);
369
370 if (fn_name == NULL_TREE)
371 {
372 error ("name missing for member function");
373 fn_name = get_identifier ("<anonymous>");
374 DECL_NAME (function) = fn_name;
375 }
376
377 DECL_CONTEXT (function) = ctype;
378
379 if (flags == DTOR_FLAG)
380 DECL_CXX_DESTRUCTOR_P (function) = 1;
381
382 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
383 maybe_retrofit_in_chrg (function);
384}
385
386/* Create an ARRAY_REF, checking for the user doing things backwards
387 along the way. DECLTYPE_P is for N3276, as in the parser. */
388
389tree
390grok_array_decl (location_t loc, tree array_expr, tree index_exp,
391 bool decltype_p)
392{
393 tree type;
394 tree expr;
395 tree orig_array_expr = array_expr;
396 tree orig_index_exp = index_exp;
397 tree overload = NULL_TREE;
398
399 if (error_operand_p (array_expr) || error_operand_p (index_exp))
400 return error_mark_node;
401
402 if (processing_template_decl)
403 {
404 if (type_dependent_expression_p (array_expr)
405 || type_dependent_expression_p (index_exp))
406 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
407 NULL_TREE, NULL_TREE);
408 array_expr = build_non_dependent_expr (array_expr);
409 index_exp = build_non_dependent_expr (index_exp);
410 }
411
412 type = TREE_TYPE (array_expr);
413 gcc_assert (type);
414 type = non_reference (type);
415
416 /* If they have an `operator[]', use that. */
417 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
418 {
419 tsubst_flags_t complain = tf_warning_or_error;
420 if (decltype_p)
421 complain |= tf_decltype;
422 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
423 index_exp, NULL_TREE, &overload, complain);
424 }
425 else
426 {
427 tree p1, p2, i1, i2;
428
429 /* Otherwise, create an ARRAY_REF for a pointer or array type.
430 It is a little-known fact that, if `a' is an array and `i' is
431 an int, you can write `i[a]', which means the same thing as
432 `a[i]'. */
433 if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
434 p1 = array_expr;
435 else
436 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
437
438 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
439 p2 = index_exp;
440 else
441 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
442
443 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
444 false);
445 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
446 false);
447
448 if ((p1 && i2) && (i1 && p2))
449 error ("ambiguous conversion for array subscript");
450
451 if (p1 && i2)
452 array_expr = p1, index_exp = i2;
453 else if (i1 && p2)
454 array_expr = p2, index_exp = i1;
455 else
456 {
457 error ("invalid types %<%T[%T]%> for array subscript",
458 type, TREE_TYPE (index_exp));
459 return error_mark_node;
460 }
461
462 if (array_expr == error_mark_node || index_exp == error_mark_node)
463 error ("ambiguous conversion for array subscript");
464
465 if (TREE_CODE (TREE_TYPE (array_expr)) == POINTER_TYPE)
466 array_expr = mark_rvalue_use (array_expr);
467 else
468 array_expr = mark_lvalue_use_nonread (array_expr);
469 index_exp = mark_rvalue_use (index_exp);
470 expr = build_array_ref (input_location, array_expr, index_exp);
471 }
472 if (processing_template_decl && expr != error_mark_node)
473 {
474 if (overload != NULL_TREE)
475 return (build_min_non_dep_op_overload
476 (ARRAY_REF, expr, overload, orig_array_expr, orig_index_exp));
477
478 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
479 NULL_TREE, NULL_TREE);
480 }
481 return expr;
482}
483
484/* Given the cast expression EXP, checking out its validity. Either return
485 an error_mark_node if there was an unavoidable error, return a cast to
486 void for trying to delete a pointer w/ the value 0, or return the
487 call to delete. If DOING_VEC is true, we handle things differently
488 for doing an array delete.
489 Implements ARM $5.3.4. This is called from the parser. */
490
491tree
492delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
493 tsubst_flags_t complain)
494{
495 tree t, type;
496
497 if (exp == error_mark_node)
498 return exp;
499
500 if (processing_template_decl)
501 {
502 t = build_min (DELETE_EXPR, void_type_node, exp, size);
503 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
504 DELETE_EXPR_USE_VEC (t) = doing_vec;
505 TREE_SIDE_EFFECTS (t) = 1;
506 return t;
507 }
508
509 /* An array can't have been allocated by new, so complain. */
510 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
511 warning (0, "deleting array %q#E", exp);
512
513 t = build_expr_type_conversion (WANT_POINTER, exp, true);
514
515 if (t == NULL_TREE || t == error_mark_node)
516 {
517 error ("type %q#T argument given to %<delete%>, expected pointer",
518 TREE_TYPE (exp));
519 return error_mark_node;
520 }
521
522 type = TREE_TYPE (t);
523
524 /* As of Valley Forge, you can delete a pointer to const. */
525
526 /* You can't delete functions. */
527 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
528 {
529 error ("cannot delete a function. Only pointer-to-objects are "
530 "valid arguments to %<delete%>");
531 return error_mark_node;
532 }
533
534 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
535 if (VOID_TYPE_P (TREE_TYPE (type)))
536 {
537 warning (OPT_Wdelete_incomplete, "deleting %qT is undefined", type);
538 doing_vec = 0;
539 }
540
541 /* Deleting a pointer with the value zero is valid and has no effect. */
542 if (integer_zerop (t))
543 return build1 (NOP_EXPR, void_type_node, t);
544
545 if (doing_vec)
546 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
547 sfk_deleting_destructor,
548 use_global_delete, complain);
549 else
550 return build_delete (type, t, sfk_deleting_destructor,
551 LOOKUP_NORMAL, use_global_delete,
552 complain);
553}
554
555/* Report an error if the indicated template declaration is not the
556 sort of thing that should be a member template. */
557
558void
559check_member_template (tree tmpl)
560{
561 tree decl;
562
563 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
564 decl = DECL_TEMPLATE_RESULT (tmpl);
565
566 if (TREE_CODE (decl) == FUNCTION_DECL
567 || DECL_ALIAS_TEMPLATE_P (tmpl)
568 || (TREE_CODE (decl) == TYPE_DECL
569 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
570 {
571 /* The parser rejects template declarations in local classes
572 (with the exception of generic lambdas). */
573 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
574 /* The parser rejects any use of virtual in a function template. */
575 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
576 && DECL_VIRTUAL_P (decl)));
577
578 /* The debug-information generating code doesn't know what to do
579 with member templates. */
580 DECL_IGNORED_P (tmpl) = 1;
581 }
582 else if (variable_template_p (tmpl))
583 /* OK */;
584 else
585 error ("template declaration of %q#D", decl);
586}
587
588/* Sanity check: report error if this function FUNCTION is not
589 really a member of the class (CTYPE) it is supposed to belong to.
590 TEMPLATE_PARMS is used to specify the template parameters of a member
591 template passed as FUNCTION_DECL. If the member template is passed as a
592 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
593 from the declaration. If the function is not a function template, it
594 must be NULL.
595 It returns the original declaration for the function, NULL_TREE if
596 no declaration was found, error_mark_node if an error was emitted. */
597
598tree
599check_classfn (tree ctype, tree function, tree template_parms)
600{
601 if (DECL_USE_TEMPLATE (function)
602 && !(TREE_CODE (function) == TEMPLATE_DECL
603 && DECL_TEMPLATE_SPECIALIZATION (function))
604 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
605 /* Since this is a specialization of a member template,
606 we're not going to find the declaration in the class.
607 For example, in:
608
609 struct S { template <typename T> void f(T); };
610 template <> void S::f(int);
611
612 we're not going to find `S::f(int)', but there's no
613 reason we should, either. We let our callers know we didn't
614 find the method, but we don't complain. */
615 return NULL_TREE;
616
617 /* Basic sanity check: for a template function, the template parameters
618 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
619 if (TREE_CODE (function) == TEMPLATE_DECL)
620 {
621 if (template_parms
622 && !comp_template_parms (template_parms,
623 DECL_TEMPLATE_PARMS (function)))
624 {
625 error ("template parameter lists provided don%'t match the "
626 "template parameters of %qD", function);
627 return error_mark_node;
628 }
629 template_parms = DECL_TEMPLATE_PARMS (function);
630 }
631
632 /* OK, is this a definition of a member template? */
633 bool is_template = (template_parms != NULL_TREE);
634
635 /* [temp.mem]
636
637 A destructor shall not be a member template. */
638 if (DECL_DESTRUCTOR_P (function) && is_template)
639 {
640 error ("destructor %qD declared as member template", function);
641 return error_mark_node;
642 }
643
644 /* We must enter the scope here, because conversion operators are
645 named by target type, and type equivalence relies on typenames
646 resolving within the scope of CTYPE. */
647 tree pushed_scope = push_scope (ctype);
648 tree matched = NULL_TREE;
649 tree fns = get_class_binding (ctype, DECL_NAME (function));
650
651 for (ovl_iterator iter (fns); !matched && iter; ++iter)
652 {
653 tree fndecl = *iter;
654
655 /* A member template definition only matches a member template
656 declaration. */
657 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
658 continue;
659
660 if (!DECL_DECLARES_FUNCTION_P (fndecl))
661 continue;
662
663 tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
664 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
665
666 /* We cannot simply call decls_match because this doesn't work
667 for static member functions that are pretending to be
668 methods, and because the name may have been changed by
669 asm("new_name"). */
670
671 /* Get rid of the this parameter on functions that become
672 static. */
673 if (DECL_STATIC_FUNCTION_P (fndecl)
674 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
675 p1 = TREE_CHAIN (p1);
676
677 /* ref-qualifier or absence of same must match. */
678 if (type_memfn_rqual (TREE_TYPE (function))
679 != type_memfn_rqual (TREE_TYPE (fndecl)))
680 continue;
681
682 // Include constraints in the match.
683 tree c1 = get_constraints (function);
684 tree c2 = get_constraints (fndecl);
685
686 /* While finding a match, same types and params are not enough
687 if the function is versioned. Also check version ("target")
688 attributes. */
689 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
690 TREE_TYPE (TREE_TYPE (fndecl)))
691 && compparms (p1, p2)
692 && !targetm.target_option.function_versions (function, fndecl)
693 && (!is_template
694 || comp_template_parms (template_parms,
695 DECL_TEMPLATE_PARMS (fndecl)))
696 && equivalent_constraints (c1, c2)
697 && (DECL_TEMPLATE_SPECIALIZATION (function)
698 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
699 && (!DECL_TEMPLATE_SPECIALIZATION (function)
700 || (DECL_TI_TEMPLATE (function) == DECL_TI_TEMPLATE (fndecl))))
701 matched = fndecl;
702 }
703
704 if (!matched)
705 {
706 if (!COMPLETE_TYPE_P (ctype))
707 cxx_incomplete_type_error (function, ctype);
708 else
709 {
710 if (DECL_CONV_FN_P (function))
711 fns = get_class_binding (ctype, conv_op_identifier);
712
713 error_at (DECL_SOURCE_LOCATION (function),
714 "no declaration matches %q#D", function);
715 if (fns)
716 print_candidates (fns);
717 else if (DECL_CONV_FN_P (function))
718 inform (DECL_SOURCE_LOCATION (function),
719 "no conversion operators declared");
720 else
721 inform (DECL_SOURCE_LOCATION (function),
722 "no functions named %qD", function);
723 inform (DECL_SOURCE_LOCATION (TYPE_NAME (ctype)),
724 "%#qT defined here", ctype);
725 }
726 matched = error_mark_node;
727 }
728
729 if (pushed_scope)
730 pop_scope (pushed_scope);
731
732 return matched;
733}
734
735/* DECL is a function with vague linkage. Remember it so that at the
736 end of the translation unit we can decide whether or not to emit
737 it. */
738
739void
740note_vague_linkage_fn (tree decl)
741{
742 DECL_DEFER_OUTPUT (decl) = 1;
743 vec_safe_push (deferred_fns, decl);
744}
745
746/* As above, but for variable template instantiations. */
747
748void
749note_variable_template_instantiation (tree decl)
750{
751 vec_safe_push (pending_statics, decl);
752}
753
754/* We have just processed the DECL, which is a static data member.
755 The other parameters are as for cp_finish_decl. */
756
757void
758finish_static_data_member_decl (tree decl,
759 tree init, bool init_const_expr_p,
760 tree asmspec_tree,
761 int flags)
762{
763 DECL_CONTEXT (decl) = current_class_type;
764
765 /* We cannot call pushdecl here, because that would fill in the
766 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
767 the right thing, namely, to put this decl out straight away. */
768
769 if (! processing_template_decl)
770 vec_safe_push (pending_statics, decl);
771
772 if (LOCAL_CLASS_P (current_class_type)
773 /* We already complained about the template definition. */
774 && !DECL_TEMPLATE_INSTANTIATION (decl))
775 permerror (input_location, "local class %q#T shall not have static data member %q#D",
776 current_class_type, decl);
777 else
778 for (tree t = current_class_type; TYPE_P (t);
779 t = CP_TYPE_CONTEXT (t))
780 if (TYPE_UNNAMED_P (t))
781 {
782 if (permerror (DECL_SOURCE_LOCATION (decl),
783 "static data member %qD in unnamed class", decl))
784 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
785 "unnamed class defined here");
786 break;
787 }
788
789 DECL_IN_AGGR_P (decl) = 1;
790
791 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
792 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
793 SET_VAR_HAD_UNKNOWN_BOUND (decl);
794
795 if (init)
796 {
797 /* Similarly to start_decl_1, we want to complete the type in order
798 to do the right thing in cp_apply_type_quals_to_decl, possibly
799 clear TYPE_QUAL_CONST (c++/65579). */
800 tree type = TREE_TYPE (decl) = complete_type (TREE_TYPE (decl));
801 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
802 }
803
804 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
805}
806
807/* DECLARATOR and DECLSPECS correspond to a class member. The other
808 parameters are as for cp_finish_decl. Return the DECL for the
809 class member declared. */
810
811tree
812grokfield (const cp_declarator *declarator,
813 cp_decl_specifier_seq *declspecs,
814 tree init, bool init_const_expr_p,
815 tree asmspec_tree,
816 tree attrlist)
817{
818 tree value;
819 const char *asmspec = 0;
820 int flags;
821 tree name;
822
823 if (init
824 && TREE_CODE (init) == TREE_LIST
825 && TREE_VALUE (init) == error_mark_node
826 && TREE_CHAIN (init) == NULL_TREE)
827 init = NULL_TREE;
828
829 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
830 if (! value || value == error_mark_node)
831 /* friend or constructor went bad. */
832 return error_mark_node;
833 if (TREE_TYPE (value) == error_mark_node)
834 return value;
835
836 if (TREE_CODE (value) == TYPE_DECL && init)
837 {
838 error ("typedef %qD is initialized (use decltype instead)", value);
839 init = NULL_TREE;
840 }
841
842 /* Pass friendly classes back. */
843 if (value == void_type_node)
844 return value;
845
846
847 name = DECL_NAME (value);
848
849 if (name != NULL_TREE)
850 {
851 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
852 {
853 error ("explicit template argument list not allowed");
854 return error_mark_node;
855 }
856
857 if (IDENTIFIER_POINTER (name)[0] == '_'
858 && id_equal (name, "_vptr"))
859 error ("member %qD conflicts with virtual function table field name",
860 value);
861 }
862
863 /* Stash away type declarations. */
864 if (TREE_CODE (value) == TYPE_DECL)
865 {
866 DECL_NONLOCAL (value) = 1;
867 DECL_CONTEXT (value) = current_class_type;
868
869 if (attrlist)
870 {
871 int attrflags = 0;
872
873 /* If this is a typedef that names the class for linkage purposes
874 (7.1.3p8), apply any attributes directly to the type. */
875 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
876 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
877 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
878
879 cplus_decl_attributes (&value, attrlist, attrflags);
880 }
881
882 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
883 && TREE_TYPE (value) != error_mark_node
884 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
885 set_underlying_type (value);
886
887 /* It's important that push_template_decl below follows
888 set_underlying_type above so that the created template
889 carries the properly set type of VALUE. */
890 if (processing_template_decl)
891 value = push_template_decl (value);
892
893 record_locally_defined_typedef (value);
894 return value;
895 }
896
897 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
898
899 if (!friendp && DECL_IN_AGGR_P (value))
900 {
901 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
902 return void_type_node;
903 }
904
905 if (asmspec_tree && asmspec_tree != error_mark_node)
906 asmspec = TREE_STRING_POINTER (asmspec_tree);
907
908 if (init)
909 {
910 if (TREE_CODE (value) == FUNCTION_DECL)
911 {
912 if (init == ridpointers[(int)RID_DELETE])
913 {
914 if (friendp && decl_defined_p (value))
915 {
916 error ("redefinition of %q#D", value);
917 inform (DECL_SOURCE_LOCATION (value),
918 "%q#D previously defined here", value);
919 }
920 else
921 {
922 DECL_DELETED_FN (value) = 1;
923 DECL_DECLARED_INLINE_P (value) = 1;
924 DECL_INITIAL (value) = error_mark_node;
925 }
926 }
927 else if (init == ridpointers[(int)RID_DEFAULT])
928 {
929 if (defaultable_fn_check (value))
930 {
931 DECL_DEFAULTED_FN (value) = 1;
932 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
933 DECL_DECLARED_INLINE_P (value) = 1;
934 }
935 }
936 else if (TREE_CODE (init) == DEFAULT_ARG)
937 error ("invalid initializer for member function %qD", value);
938 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
939 {
940 if (integer_zerop (init))
941 DECL_PURE_VIRTUAL_P (value) = 1;
942 else if (error_operand_p (init))
943 ; /* An error has already been reported. */
944 else
945 error ("invalid initializer for member function %qD",
946 value);
947 }
948 else
949 {
950 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
951 if (friendp)
952 error ("initializer specified for friend function %qD",
953 value);
954 else
955 error ("initializer specified for static member function %qD",
956 value);
957 }
958 }
959 else if (TREE_CODE (value) == FIELD_DECL)
960 /* C++11 NSDMI, keep going. */;
961 else if (!VAR_P (value))
962 gcc_unreachable ();
963 }
964
965 /* Pass friend decls back. */
966 if ((TREE_CODE (value) == FUNCTION_DECL
967 || TREE_CODE (value) == TEMPLATE_DECL)
968 && DECL_CONTEXT (value) != current_class_type)
969 return value;
970
971 /* Need to set this before push_template_decl. */
972 if (VAR_P (value))
973 DECL_CONTEXT (value) = current_class_type;
974
975 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
976 {
977 value = push_template_decl (value);
978 if (error_operand_p (value))
979 return error_mark_node;
980 }
981
982 if (attrlist)
983 cplus_decl_attributes (&value, attrlist, 0);
984
985 if (init && DIRECT_LIST_INIT_P (init))
986 flags = LOOKUP_NORMAL;
987 else
988 flags = LOOKUP_IMPLICIT;
989
990 switch (TREE_CODE (value))
991 {
992 case VAR_DECL:
993 finish_static_data_member_decl (value, init, init_const_expr_p,
994 asmspec_tree, flags);
995 return value;
996
997 case FIELD_DECL:
998 if (asmspec)
999 error ("%<asm%> specifiers are not permitted on non-static data members");
1000 if (DECL_INITIAL (value) == error_mark_node)
1001 init = error_mark_node;
1002 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
1003 NULL_TREE, flags);
1004 DECL_IN_AGGR_P (value) = 1;
1005 return value;
1006
1007 case FUNCTION_DECL:
1008 if (asmspec)
1009 set_user_assembler_name (value, asmspec);
1010
1011 cp_finish_decl (value,
1012 /*init=*/NULL_TREE,
1013 /*init_const_expr_p=*/false,
1014 asmspec_tree, flags);
1015
1016 /* Pass friends back this way. */
1017 if (DECL_FRIEND_P (value))
1018 return void_type_node;
1019
1020 DECL_IN_AGGR_P (value) = 1;
1021 return value;
1022
1023 default:
1024 gcc_unreachable ();
1025 }
1026 return NULL_TREE;
1027}
1028
1029/* Like `grokfield', but for bitfields.
1030 WIDTH is the width of the bitfield, a constant expression.
1031 The other parameters are as for grokfield. */
1032
1033tree
1034grokbitfield (const cp_declarator *declarator,
1035 cp_decl_specifier_seq *declspecs, tree width, tree init,
1036 tree attrlist)
1037{
1038 tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1039 init != NULL_TREE, &attrlist);
1040
1041 if (value == error_mark_node)
1042 return NULL_TREE; /* friends went bad. */
1043 if (TREE_TYPE (value) == error_mark_node)
1044 return value;
1045
1046 /* Pass friendly classes back. */
1047 if (VOID_TYPE_P (value))
1048 return void_type_node;
1049
1050 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
1051 && (POINTER_TYPE_P (value)
1052 || !dependent_type_p (TREE_TYPE (value))))
1053 {
1054 error ("bit-field %qD with non-integral type", value);
1055 return error_mark_node;
1056 }
1057
1058 if (TREE_CODE (value) == TYPE_DECL)
1059 {
1060 error ("cannot declare %qD to be a bit-field type", value);
1061 return NULL_TREE;
1062 }
1063
1064 /* Usually, finish_struct_1 catches bitfields with invalid types.
1065 But, in the case of bitfields with function type, we confuse
1066 ourselves into thinking they are member functions, so we must
1067 check here. */
1068 if (TREE_CODE (value) == FUNCTION_DECL)
1069 {
1070 error ("cannot declare bit-field %qD with function type",
1071 DECL_NAME (value));
1072 return NULL_TREE;
1073 }
1074
1075 if (width && TYPE_WARN_IF_NOT_ALIGN (TREE_TYPE (value)))
1076 {
1077 error ("cannot declare bit-field %qD with %<warn_if_not_aligned%> type",
1078 DECL_NAME (value));
1079 return NULL_TREE;
1080 }
1081
1082 if (DECL_IN_AGGR_P (value))
1083 {
1084 error ("%qD is already defined in the class %qT", value,
1085 DECL_CONTEXT (value));
1086 return void_type_node;
1087 }
1088
1089 if (TREE_STATIC (value))
1090 {
1091 error ("static member %qD cannot be a bit-field", value);
1092 return NULL_TREE;
1093 }
1094
1095 int flags = LOOKUP_IMPLICIT;
1096 if (init && DIRECT_LIST_INIT_P (init))
1097 flags = LOOKUP_NORMAL;
1098 cp_finish_decl (value, init, false, NULL_TREE, flags);
1099
1100 if (width != error_mark_node)
1101 {
1102 /* The width must be an integer type. */
1103 if (!type_dependent_expression_p (width)
1104 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1105 error ("width of bit-field %qD has non-integral type %qT", value,
1106 TREE_TYPE (width));
1107 else
1108 {
1109 /* Temporarily stash the width in DECL_BIT_FIELD_REPRESENTATIVE.
1110 check_bitfield_decl picks it from there later and sets DECL_SIZE
1111 accordingly. */
1112 DECL_BIT_FIELD_REPRESENTATIVE (value) = width;
1113 SET_DECL_C_BIT_FIELD (value);
1114 }
1115 }
1116
1117 DECL_IN_AGGR_P (value) = 1;
1118
1119 if (attrlist)
1120 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1121
1122 return value;
1123}
1124
1125
1126/* Returns true iff ATTR is an attribute which needs to be applied at
1127 instantiation time rather than template definition time. */
1128
1129static bool
1130is_late_template_attribute (tree attr, tree decl)
1131{
1132 tree name = get_attribute_name (attr);
1133 tree args = TREE_VALUE (attr);
1134 const struct attribute_spec *spec = lookup_attribute_spec (name);
1135 tree arg;
1136
1137 if (!spec)
1138 /* Unknown attribute. */
1139 return false;
1140
1141 /* Attribute weak handling wants to write out assembly right away. */
1142 if (is_attribute_p ("weak", name))
1143 return true;
1144
1145 /* Attributes used and unused are applied directly, as they appertain to
1146 decls. */
1147 if (is_attribute_p ("unused", name)
1148 || is_attribute_p ("used", name))
1149 return false;
1150
1151 /* Attribute tls_model wants to modify the symtab. */
1152 if (is_attribute_p ("tls_model", name))
1153 return true;
1154
1155 /* #pragma omp declare simd attribute needs to be always deferred. */
1156 if (flag_openmp
1157 && is_attribute_p ("omp declare simd", name))
1158 return true;
1159
1160 /* An attribute pack is clearly dependent. */
1161 if (args && PACK_EXPANSION_P (args))
1162 return true;
1163
1164 /* If any of the arguments are dependent expressions, we can't evaluate
1165 the attribute until instantiation time. */
1166 for (arg = args; arg; arg = TREE_CHAIN (arg))
1167 {
1168 tree t = TREE_VALUE (arg);
1169
1170 /* If the first attribute argument is an identifier, only consider
1171 second and following arguments. Attributes like mode, format,
1172 cleanup and several target specific attributes aren't late
1173 just because they have an IDENTIFIER_NODE as first argument. */
1174 if (arg == args && attribute_takes_identifier_p (name)
1175 && identifier_p (t))
1176 continue;
1177
1178 if (value_dependent_expression_p (t)
1179 || type_dependent_expression_p (t))
1180 return true;
1181 }
1182
1183 if (TREE_CODE (decl) == TYPE_DECL
1184 || TYPE_P (decl)
1185 || spec->type_required)
1186 {
1187 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1188
1189 /* We can't apply any attributes to a completely unknown type until
1190 instantiation time. */
1191 enum tree_code code = TREE_CODE (type);
1192 if (code == TEMPLATE_TYPE_PARM
1193 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1194 || code == TYPENAME_TYPE)
1195 return true;
1196 /* Also defer most attributes on dependent types. This is not
1197 necessary in all cases, but is the better default. */
1198 else if (dependent_type_p (type)
1199 /* But some attributes specifically apply to templates. */
1200 && !is_attribute_p ("abi_tag", name)
1201 && !is_attribute_p ("deprecated", name)
1202 && !is_attribute_p ("visibility", name))
1203 return true;
1204 else
1205 return false;
1206 }
1207 else
1208 return false;
1209}
1210
1211/* ATTR_P is a list of attributes. Remove any attributes which need to be
1212 applied at instantiation time and return them. If IS_DEPENDENT is true,
1213 the declaration itself is dependent, so all attributes should be applied
1214 at instantiation time. */
1215
1216static tree
1217splice_template_attributes (tree *attr_p, tree decl)
1218{
1219 tree *p = attr_p;
1220 tree late_attrs = NULL_TREE;
1221 tree *q = &late_attrs;
1222
1223 if (!p)
1224 return NULL_TREE;
1225
1226 for (; *p; )
1227 {
1228 if (is_late_template_attribute (*p, decl))
1229 {
1230 ATTR_IS_DEPENDENT (*p) = 1;
1231 *q = *p;
1232 *p = TREE_CHAIN (*p);
1233 q = &TREE_CHAIN (*q);
1234 *q = NULL_TREE;
1235 }
1236 else
1237 p = &TREE_CHAIN (*p);
1238 }
1239
1240 return late_attrs;
1241}
1242
1243/* Remove any late attributes from the list in ATTR_P and attach them to
1244 DECL_P. */
1245
1246static void
1247save_template_attributes (tree *attr_p, tree *decl_p)
1248{
1249 tree *q;
1250
1251 if (attr_p && *attr_p == error_mark_node)
1252 return;
1253
1254 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1255 if (!late_attrs)
1256 return;
1257
1258 if (DECL_P (*decl_p))
1259 q = &DECL_ATTRIBUTES (*decl_p);
1260 else
1261 q = &TYPE_ATTRIBUTES (*decl_p);
1262
1263 tree old_attrs = *q;
1264
1265 /* Merge the late attributes at the beginning with the attribute
1266 list. */
1267 late_attrs = merge_attributes (late_attrs, *q);
1268 *q = late_attrs;
1269
1270 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1271 {
1272 /* We've added new attributes directly to the main variant, so
1273 now we need to update all of the other variants to include
1274 these new attributes. */
1275 tree variant;
1276 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1277 variant = TYPE_NEXT_VARIANT (variant))
1278 {
1279 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1280 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1281 }
1282 }
1283}
1284
1285/* True if ATTRS contains any dependent attributes that affect type
1286 identity. */
1287
1288bool
1289any_dependent_type_attributes_p (tree attrs)
1290{
1291 for (tree a = attrs; a; a = TREE_CHAIN (a))
1292 if (ATTR_IS_DEPENDENT (a))
1293 {
1294 const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (a));
1295 if (as && as->affects_type_identity)
1296 return true;
1297 }
1298 return false;
1299}
1300
1301/* Return true iff ATTRS are acceptable attributes to be applied in-place
1302 to a typedef which gives a previously unnamed class or enum a name for
1303 linkage purposes. */
1304
1305bool
1306attributes_naming_typedef_ok (tree attrs)
1307{
1308 for (; attrs; attrs = TREE_CHAIN (attrs))
1309 {
1310 tree name = get_attribute_name (attrs);
1311 if (is_attribute_p ("vector_size", name))
1312 return false;
1313 }
1314 return true;
1315}
1316
1317/* Like reconstruct_complex_type, but handle also template trees. */
1318
1319tree
1320cp_reconstruct_complex_type (tree type, tree bottom)
1321{
1322 tree inner, outer;
1323 bool late_return_type_p = false;
1324
1325 if (TYPE_PTR_P (type))
1326 {
1327 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1328 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1329 TYPE_REF_CAN_ALIAS_ALL (type));
1330 }
1331 else if (TREE_CODE (type) == REFERENCE_TYPE)
1332 {
1333 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1334 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1335 TYPE_REF_CAN_ALIAS_ALL (type));
1336 }
1337 else if (TREE_CODE (type) == ARRAY_TYPE)
1338 {
1339 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1340 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1341 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1342 element type qualification will be handled by the recursive
1343 cp_reconstruct_complex_type call and cp_build_qualified_type
1344 for ARRAY_TYPEs changes the element type. */
1345 return outer;
1346 }
1347 else if (TREE_CODE (type) == FUNCTION_TYPE)
1348 {
1349 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1350 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1351 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1352 outer = apply_memfn_quals (outer,
1353 type_memfn_quals (type),
1354 type_memfn_rqual (type));
1355 }
1356 else if (TREE_CODE (type) == METHOD_TYPE)
1357 {
1358 late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
1359 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1360 /* The build_method_type_directly() routine prepends 'this' to argument list,
1361 so we must compensate by getting rid of it. */
1362 outer
1363 = build_method_type_directly
1364 (class_of_this_parm (type), inner,
1365 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1366 }
1367 else if (TREE_CODE (type) == OFFSET_TYPE)
1368 {
1369 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1370 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1371 }
1372 else
1373 return bottom;
1374
1375 if (TYPE_ATTRIBUTES (type))
1376 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1377 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1378
1379 if (late_return_type_p)
1380 TYPE_HAS_LATE_RETURN_TYPE (outer) = 1;
1381
1382 return outer;
1383}
1384
1385/* Replaces any constexpr expression that may be into the attributes
1386 arguments with their reduced value. */
1387
1388static void
1389cp_check_const_attributes (tree attributes)
1390{
1391 if (attributes == error_mark_node)
1392 return;
1393
1394 tree attr;
1395 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1396 {
1397 tree arg;
1398 for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1399 {
1400 tree expr = TREE_VALUE (arg);
1401 if (EXPR_P (expr))
1402 TREE_VALUE (arg) = maybe_constant_value (expr);
1403 }
1404 }
1405}
1406
1407/* Return true if TYPE is an OpenMP mappable type. */
1408bool
1409cp_omp_mappable_type (tree type)
1410{
1411 /* Mappable type has to be complete. */
1412 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1413 return false;
1414 /* Arrays have mappable type if the elements have mappable type. */
1415 while (TREE_CODE (type) == ARRAY_TYPE)
1416 type = TREE_TYPE (type);
1417 /* A mappable type cannot contain virtual members. */
1418 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1419 return false;
1420 /* All data members must be non-static. */
1421 if (CLASS_TYPE_P (type))
1422 {
1423 tree field;
1424 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1425 if (VAR_P (field))
1426 return false;
1427 /* All fields must have mappable types. */
1428 else if (TREE_CODE (field) == FIELD_DECL
1429 && !cp_omp_mappable_type (TREE_TYPE (field)))
1430 return false;
1431 }
1432 return true;
1433}
1434
1435/* Like decl_attributes, but handle C++ complexity. */
1436
1437void
1438cplus_decl_attributes (tree *decl, tree attributes, int flags)
1439{
1440 if (*decl == NULL_TREE || *decl == void_type_node
1441 || *decl == error_mark_node)
1442 return;
1443
1444 /* Add implicit "omp declare target" attribute if requested. */
1445 if (scope_chain->omp_declare_target_attribute
1446 && ((VAR_P (*decl)
1447 && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
1448 || TREE_CODE (*decl) == FUNCTION_DECL))
1449 {
1450 if (VAR_P (*decl)
1451 && DECL_CLASS_SCOPE_P (*decl))
1452 error ("%q+D static data member inside of declare target directive",
1453 *decl);
1454 else if (!processing_template_decl
1455 && VAR_P (*decl)
1456 && !cp_omp_mappable_type (TREE_TYPE (*decl)))
1457 error ("%q+D in declare target directive does not have mappable type",
1458 *decl);
1459 else
1460 attributes = tree_cons (get_identifier ("omp declare target"),
1461 NULL_TREE, attributes);
1462 }
1463
1464 if (processing_template_decl)
1465 {
1466 if (check_for_bare_parameter_packs (attributes))
1467 return;
1468
1469 save_template_attributes (&attributes, decl);
1470 }
1471
1472 cp_check_const_attributes (attributes);
1473
1474 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1475 decl = &DECL_TEMPLATE_RESULT (*decl);
1476
1477 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1478 {
1479 attributes
1480 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1481 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1482 attributes, flags);
1483 }
1484 else
1485 {
1486 tree last_decl = (DECL_P (*decl) && DECL_NAME (*decl)
1487 ? lookup_name (DECL_NAME (*decl)) : NULL_TREE);
1488
1489 if (last_decl && TREE_CODE (last_decl) == OVERLOAD)
1490 for (ovl_iterator iter (last_decl, true); ; ++iter)
1491 {
1492 if (!iter)
1493 {
1494 last_decl = NULL_TREE;
1495 break;
1496 }
1497
1498 if (TREE_CODE (*iter) == OVERLOAD)
1499 continue;
1500
1501 if (decls_match (*decl, *iter, /*record_decls=*/false))
1502 {
1503 last_decl = *iter;
1504 break;
1505 }
1506 }
1507
1508 decl_attributes (decl, attributes, flags, last_decl);
1509 }
1510
1511 if (TREE_CODE (*decl) == TYPE_DECL)
1512 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1513
1514 /* Propagate deprecation out to the template. */
1515 if (TREE_DEPRECATED (*decl))
1516 if (tree ti = get_template_info (*decl))
1517 {
1518 tree tmpl = TI_TEMPLATE (ti);
1519 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1520 : DECL_TEMPLATE_RESULT (tmpl));
1521 if (*decl == pattern)
1522 TREE_DEPRECATED (tmpl) = true;
1523 }
1524}
1525
1526/* Walks through the namespace- or function-scope anonymous union
1527 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1528 Returns one of the fields for use in the mangled name. */
1529
1530static tree
1531build_anon_union_vars (tree type, tree object)
1532{
1533 tree main_decl = NULL_TREE;
1534 tree field;
1535
1536 /* Rather than write the code to handle the non-union case,
1537 just give an error. */
1538 if (TREE_CODE (type) != UNION_TYPE)
1539 {
1540 error ("anonymous struct not inside named type");
1541 return error_mark_node;
1542 }
1543
1544 for (field = TYPE_FIELDS (type);
1545 field != NULL_TREE;
1546 field = DECL_CHAIN (field))
1547 {
1548 tree decl;
1549 tree ref;
1550
1551 if (DECL_ARTIFICIAL (field))
1552 continue;
1553 if (TREE_CODE (field) != FIELD_DECL)
1554 {
1555 permerror (DECL_SOURCE_LOCATION (field),
1556 "%q#D invalid; an anonymous union can only "
1557 "have non-static data members", field);
1558 continue;
1559 }
1560
1561 if (TREE_PRIVATE (field))
1562 permerror (DECL_SOURCE_LOCATION (field),
1563 "private member %q#D in anonymous union", field);
1564 else if (TREE_PROTECTED (field))
1565 permerror (DECL_SOURCE_LOCATION (field),
1566 "protected member %q#D in anonymous union", field);
1567
1568 if (processing_template_decl)
1569 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1570 DECL_NAME (field), NULL_TREE);
1571 else
1572 ref = build_class_member_access_expr (object, field, NULL_TREE,
1573 false, tf_warning_or_error);
1574
1575 if (DECL_NAME (field))
1576 {
1577 tree base;
1578
1579 decl = build_decl (input_location,
1580 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1581 DECL_ANON_UNION_VAR_P (decl) = 1;
1582 DECL_ARTIFICIAL (decl) = 1;
1583
1584 base = get_base_address (object);
1585 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1586 TREE_STATIC (decl) = TREE_STATIC (base);
1587 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1588
1589 SET_DECL_VALUE_EXPR (decl, ref);
1590 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1591
1592 decl = pushdecl (decl);
1593 }
1594 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1595 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1596 else
1597 decl = 0;
1598
1599 if (main_decl == NULL_TREE)
1600 main_decl = decl;
1601 }
1602
1603 return main_decl;
1604}
1605
1606/* Finish off the processing of a UNION_TYPE structure. If the union is an
1607 anonymous union, then all members must be laid out together. PUBLIC_P
1608 is nonzero if this union is not declared static. */
1609
1610void
1611finish_anon_union (tree anon_union_decl)
1612{
1613 tree type;
1614 tree main_decl;
1615 bool public_p;
1616
1617 if (anon_union_decl == error_mark_node)
1618 return;
1619
1620 type = TREE_TYPE (anon_union_decl);
1621 public_p = TREE_PUBLIC (anon_union_decl);
1622
1623 /* The VAR_DECL's context is the same as the TYPE's context. */
1624 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1625
1626 if (TYPE_FIELDS (type) == NULL_TREE)
1627 return;
1628
1629 if (public_p)
1630 {
1631 error ("namespace-scope anonymous aggregates must be static");
1632 return;
1633 }
1634
1635 main_decl = build_anon_union_vars (type, anon_union_decl);
1636 if (main_decl == error_mark_node)
1637 return;
1638 if (main_decl == NULL_TREE)
1639 {
1640 pedwarn (input_location, 0, "anonymous union with no members");
1641 return;
1642 }
1643
1644 if (!processing_template_decl)
1645 {
1646 /* Use main_decl to set the mangled name. */
1647 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1648 maybe_commonize_var (anon_union_decl);
1649 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1650 mangle_decl (anon_union_decl);
1651 DECL_NAME (anon_union_decl) = NULL_TREE;
1652 }
1653
1654 pushdecl (anon_union_decl);
1655 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1656}
1657
1658/* Auxiliary functions to make type signatures for
1659 `operator new' and `operator delete' correspond to
1660 what compiler will be expecting. */
1661
1662tree
1663coerce_new_type (tree type)
1664{
1665 int e = 0;
1666 tree args = TYPE_ARG_TYPES (type);
1667
1668 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1669
1670 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1671 {
1672 e = 1;
1673 error ("%<operator new%> must return type %qT", ptr_type_node);
1674 }
1675
1676 if (args && args != void_list_node)
1677 {
1678 if (TREE_PURPOSE (args))
1679 {
1680 /* [basic.stc.dynamic.allocation]
1681
1682 The first parameter shall not have an associated default
1683 argument. */
1684 error ("the first parameter of %<operator new%> cannot "
1685 "have a default argument");
1686 /* Throw away the default argument. */
1687 TREE_PURPOSE (args) = NULL_TREE;
1688 }
1689
1690 if (!same_type_p (TREE_VALUE (args), size_type_node))
1691 {
1692 e = 2;
1693 args = TREE_CHAIN (args);
1694 }
1695 }
1696 else
1697 e = 2;
1698
1699 if (e == 2)
1700 permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1701 "as first parameter", size_type_node);
1702
1703 switch (e)
1704 {
1705 case 2:
1706 args = tree_cons (NULL_TREE, size_type_node, args);
1707 /* Fall through. */
1708 case 1:
1709 type = build_exception_variant
1710 (build_function_type (ptr_type_node, args),
1711 TYPE_RAISES_EXCEPTIONS (type));
1712 /* Fall through. */
1713 default:;
1714 }
1715 return type;
1716}
1717
1718tree
1719coerce_delete_type (tree type)
1720{
1721 int e = 0;
1722 tree args = TYPE_ARG_TYPES (type);
1723
1724 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1725
1726 if (!same_type_p (TREE_TYPE (type), void_type_node))
1727 {
1728 e = 1;
1729 error ("%<operator delete%> must return type %qT", void_type_node);
1730 }
1731
1732 if (!args || args == void_list_node
1733 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1734 {
1735 e = 2;
1736 if (args && args != void_list_node)
1737 args = TREE_CHAIN (args);
1738 error ("%<operator delete%> takes type %qT as first parameter",
1739 ptr_type_node);
1740 }
1741 switch (e)
1742 {
1743 case 2:
1744 args = tree_cons (NULL_TREE, ptr_type_node, args);
1745 /* Fall through. */
1746 case 1:
1747 type = build_exception_variant
1748 (build_function_type (void_type_node, args),
1749 TYPE_RAISES_EXCEPTIONS (type));
1750 /* Fall through. */
1751 default:;
1752 }
1753
1754 return type;
1755}
1756
1757/* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1758 and mark them as needed. */
1759
1760static void
1761mark_vtable_entries (tree decl)
1762{
1763 tree fnaddr;
1764 unsigned HOST_WIDE_INT idx;
1765
1766 /* It's OK for the vtable to refer to deprecated virtual functions. */
1767 warning_sentinel w(warn_deprecated_decl);
1768
1769 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1770 idx, fnaddr)
1771 {
1772 tree fn;
1773
1774 STRIP_NOPS (fnaddr);
1775
1776 if (TREE_CODE (fnaddr) != ADDR_EXPR
1777 && TREE_CODE (fnaddr) != FDESC_EXPR)
1778 /* This entry is an offset: a virtual base class offset, a
1779 virtual call offset, an RTTI offset, etc. */
1780 continue;
1781
1782 fn = TREE_OPERAND (fnaddr, 0);
1783 TREE_ADDRESSABLE (fn) = 1;
1784 /* When we don't have vcall offsets, we output thunks whenever
1785 we output the vtables that contain them. With vcall offsets,
1786 we know all the thunks we'll need when we emit a virtual
1787 function, so we emit the thunks there instead. */
1788 if (DECL_THUNK_P (fn))
1789 use_thunk (fn, /*emit_p=*/0);
1790 mark_used (fn);
1791 }
1792}
1793
1794/* Set DECL up to have the closest approximation of "initialized common"
1795 linkage available. */
1796
1797void
1798comdat_linkage (tree decl)
1799{
1800 if (flag_weak)
1801 make_decl_one_only (decl, cxx_comdat_group (decl));
1802 else if (TREE_CODE (decl) == FUNCTION_DECL
1803 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1804 /* We can just emit function and compiler-generated variables
1805 statically; having multiple copies is (for the most part) only
1806 a waste of space.
1807
1808 There are two correctness issues, however: the address of a
1809 template instantiation with external linkage should be the
1810 same, independent of what translation unit asks for the
1811 address, and this will not hold when we emit multiple copies of
1812 the function. However, there's little else we can do.
1813
1814 Also, by default, the typeinfo implementation assumes that
1815 there will be only one copy of the string used as the name for
1816 each type. Therefore, if weak symbols are unavailable, the
1817 run-time library should perform a more conservative check; it
1818 should perform a string comparison, rather than an address
1819 comparison. */
1820 TREE_PUBLIC (decl) = 0;
1821 else
1822 {
1823 /* Static data member template instantiations, however, cannot
1824 have multiple copies. */
1825 if (DECL_INITIAL (decl) == 0
1826 || DECL_INITIAL (decl) == error_mark_node)
1827 DECL_COMMON (decl) = 1;
1828 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1829 {
1830 DECL_COMMON (decl) = 1;
1831 DECL_INITIAL (decl) = error_mark_node;
1832 }
1833 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1834 {
1835 /* We can't do anything useful; leave vars for explicit
1836 instantiation. */
1837 DECL_EXTERNAL (decl) = 1;
1838 DECL_NOT_REALLY_EXTERN (decl) = 0;
1839 }
1840 }
1841
1842 if (TREE_PUBLIC (decl))
1843 DECL_COMDAT (decl) = 1;
1844}
1845
1846/* For win32 we also want to put explicit instantiations in
1847 linkonce sections, so that they will be merged with implicit
1848 instantiations; otherwise we get duplicate symbol errors.
1849 For Darwin we do not want explicit instantiations to be
1850 linkonce. */
1851
1852void
1853maybe_make_one_only (tree decl)
1854{
1855 /* We used to say that this was not necessary on targets that support weak
1856 symbols, because the implicit instantiations will defer to the explicit
1857 one. However, that's not actually the case in SVR4; a strong definition
1858 after a weak one is an error. Also, not making explicit
1859 instantiations one_only means that we can end up with two copies of
1860 some template instantiations. */
1861 if (! flag_weak)
1862 return;
1863
1864 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1865 we can get away with not emitting them if they aren't used. We need
1866 to for variables so that cp_finish_decl will update their linkage,
1867 because their DECL_INITIAL may not have been set properly yet. */
1868
1869 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1870 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1871 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1872 {
1873 make_decl_one_only (decl, cxx_comdat_group (decl));
1874
1875 if (VAR_P (decl))
1876 {
1877 varpool_node *node = varpool_node::get_create (decl);
1878 DECL_COMDAT (decl) = 1;
1879 /* Mark it needed so we don't forget to emit it. */
1880 node->forced_by_abi = true;
1881 TREE_USED (decl) = 1;
1882 }
1883 }
1884}
1885
1886/* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1887 This predicate will give the right answer during parsing of the
1888 function, which other tests may not. */
1889
1890bool
1891vague_linkage_p (tree decl)
1892{
1893 if (!TREE_PUBLIC (decl))
1894 {
1895 /* maybe_thunk_body clears TREE_PUBLIC on the maybe-in-charge 'tor
1896 variants, check one of the "clones" for the real linkage. */
1897 if ((DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
1898 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl))
1899 && DECL_CHAIN (decl)
1900 && DECL_CLONED_FUNCTION (DECL_CHAIN (decl)))
1901 return vague_linkage_p (DECL_CHAIN (decl));
1902
1903 gcc_checking_assert (!DECL_COMDAT (decl));
1904 return false;
1905 }
1906 /* Unfortunately, import_export_decl has not always been called
1907 before the function is processed, so we cannot simply check
1908 DECL_COMDAT. */
1909 if (DECL_COMDAT (decl)
1910 || (TREE_CODE (decl) == FUNCTION_DECL
1911 && DECL_DECLARED_INLINE_P (decl))
1912 || (DECL_LANG_SPECIFIC (decl)
1913 && DECL_TEMPLATE_INSTANTIATION (decl))
1914 || (VAR_P (decl) && DECL_INLINE_VAR_P (decl)))
1915 return true;
1916 else if (DECL_FUNCTION_SCOPE_P (decl))
1917 /* A local static in an inline effectively has vague linkage. */
1918 return (TREE_STATIC (decl)
1919 && vague_linkage_p (DECL_CONTEXT (decl)));
1920 else
1921 return false;
1922}
1923
1924/* Determine whether or not we want to specifically import or export CTYPE,
1925 using various heuristics. */
1926
1927static void
1928import_export_class (tree ctype)
1929{
1930 /* -1 for imported, 1 for exported. */
1931 int import_export = 0;
1932
1933 /* It only makes sense to call this function at EOF. The reason is
1934 that this function looks at whether or not the first non-inline
1935 non-abstract virtual member function has been defined in this
1936 translation unit. But, we can't possibly know that until we've
1937 seen the entire translation unit. */
1938 gcc_assert (at_eof);
1939
1940 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1941 return;
1942
1943 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1944 we will have CLASSTYPE_INTERFACE_ONLY set but not
1945 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1946 heuristic because someone will supply a #pragma implementation
1947 elsewhere, and deducing it here would produce a conflict. */
1948 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1949 return;
1950
1951 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1952 import_export = -1;
1953 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1954 import_export = 1;
1955 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1956 && !flag_implicit_templates)
1957 /* For a template class, without -fimplicit-templates, check the
1958 repository. If the virtual table is assigned to this
1959 translation unit, then export the class; otherwise, import
1960 it. */
1961 import_export = repo_export_class_p (ctype) ? 1 : -1;
1962 else if (TYPE_POLYMORPHIC_P (ctype))
1963 {
1964 /* The ABI specifies that the virtual table and associated
1965 information are emitted with the key method, if any. */
1966 tree method = CLASSTYPE_KEY_METHOD (ctype);
1967 /* If weak symbol support is not available, then we must be
1968 careful not to emit the vtable when the key function is
1969 inline. An inline function can be defined in multiple
1970 translation units. If we were to emit the vtable in each
1971 translation unit containing a definition, we would get
1972 multiple definition errors at link-time. */
1973 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1974 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1975 }
1976
1977 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1978 a definition anywhere else. */
1979 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1980 import_export = 0;
1981
1982 /* Allow back ends the chance to overrule the decision. */
1983 if (targetm.cxx.import_export_class)
1984 import_export = targetm.cxx.import_export_class (ctype, import_export);
1985
1986 if (import_export)
1987 {
1988 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1989 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1990 }
1991}
1992
1993/* Return true if VAR has already been provided to the back end; in that
1994 case VAR should not be modified further by the front end. */
1995static bool
1996var_finalized_p (tree var)
1997{
1998 return varpool_node::get_create (var)->definition;
1999}
2000
2001/* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
2002 must be emitted in this translation unit. Mark it as such. */
2003
2004void
2005mark_needed (tree decl)
2006{
2007 TREE_USED (decl) = 1;
2008 if (TREE_CODE (decl) == FUNCTION_DECL)
2009 {
2010 /* Extern inline functions don't become needed when referenced.
2011 If we know a method will be emitted in other TU and no new
2012 functions can be marked reachable, just use the external
2013 definition. */
2014 struct cgraph_node *node = cgraph_node::get_create (decl);
2015 node->forced_by_abi = true;
2016
2017 /* #pragma interface and -frepo code can call mark_needed for
2018 maybe-in-charge 'tors; mark the clones as well. */
2019 tree clone;
2020 FOR_EACH_CLONE (clone, decl)
2021 mark_needed (clone);
2022 }
2023 else if (VAR_P (decl))
2024 {
2025 varpool_node *node = varpool_node::get_create (decl);
2026 /* C++ frontend use mark_decl_references to force COMDAT variables
2027 to be output that might appear dead otherwise. */
2028 node->forced_by_abi = true;
2029 }
2030}
2031
2032/* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
2033 returns true if a definition of this entity should be provided in
2034 this object file. Callers use this function to determine whether
2035 or not to let the back end know that a definition of DECL is
2036 available in this translation unit. */
2037
2038bool
2039decl_needed_p (tree decl)
2040{
2041 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2042 /* This function should only be called at the end of the translation
2043 unit. We cannot be sure of whether or not something will be
2044 COMDAT until that point. */
2045 gcc_assert (at_eof);
2046
2047 /* All entities with external linkage that are not COMDAT/EXTERN should be
2048 emitted; they may be referred to from other object files. */
2049 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
2050 return true;
2051 /* Functions marked "dllexport" must be emitted so that they are
2052 visible to other DLLs. */
2053 if (flag_keep_inline_dllexport
2054 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
2055 return true;
2056
2057 /* When not optimizing, do not bother to produce definitions for extern
2058 symbols. */
2059 if (DECL_REALLY_EXTERN (decl)
2060 && ((TREE_CODE (decl) != FUNCTION_DECL
2061 && !optimize)
2062 || (TREE_CODE (decl) == FUNCTION_DECL
2063 && !opt_for_fn (decl, optimize)))
2064 && !lookup_attribute ("always_inline", decl))
2065 return false;
2066
2067 /* If this entity was used, let the back end see it; it will decide
2068 whether or not to emit it into the object file. */
2069 if (TREE_USED (decl))
2070 return true;
2071
2072 /* Virtual functions might be needed for devirtualization. */
2073 if (flag_devirtualize
2074 && TREE_CODE (decl) == FUNCTION_DECL
2075 && DECL_VIRTUAL_P (decl))
2076 return true;
2077
2078 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2079 reference to DECL might cause it to be emitted later. */
2080 return false;
2081}
2082
2083/* If necessary, write out the vtables for the dynamic class CTYPE.
2084 Returns true if any vtables were emitted. */
2085
2086static bool
2087maybe_emit_vtables (tree ctype)
2088{
2089 tree vtbl;
2090 tree primary_vtbl;
2091 int needed = 0;
2092 varpool_node *current = NULL, *last = NULL;
2093
2094 /* If the vtables for this class have already been emitted there is
2095 nothing more to do. */
2096 primary_vtbl = CLASSTYPE_VTABLES (ctype);
2097 if (var_finalized_p (primary_vtbl))
2098 return false;
2099 /* Ignore dummy vtables made by get_vtable_decl. */
2100 if (TREE_TYPE (primary_vtbl) == void_type_node)
2101 return false;
2102
2103 /* On some targets, we cannot determine the key method until the end
2104 of the translation unit -- which is when this function is
2105 called. */
2106 if (!targetm.cxx.key_method_may_be_inline ())
2107 determine_key_method (ctype);
2108
2109 /* See if any of the vtables are needed. */
2110 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2111 {
2112 import_export_decl (vtbl);
2113 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2114 needed = 1;
2115 }
2116 if (!needed)
2117 {
2118 /* If the references to this class' vtables are optimized away,
2119 still emit the appropriate debugging information. See
2120 dfs_debug_mark. */
2121 if (DECL_COMDAT (primary_vtbl)
2122 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2123 note_debug_info_needed (ctype);
2124 return false;
2125 }
2126
2127 /* The ABI requires that we emit all of the vtables if we emit any
2128 of them. */
2129 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2130 {
2131 /* Mark entities references from the virtual table as used. */
2132 mark_vtable_entries (vtbl);
2133
2134 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2135 {
2136 vec<tree, va_gc> *cleanups = NULL;
2137 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2138 LOOKUP_NORMAL);
2139
2140 /* It had better be all done at compile-time. */
2141 gcc_assert (!expr && !cleanups);
2142 }
2143
2144 /* Write it out. */
2145 DECL_EXTERNAL (vtbl) = 0;
2146 rest_of_decl_compilation (vtbl, 1, 1);
2147
2148 /* Because we're only doing syntax-checking, we'll never end up
2149 actually marking the variable as written. */
2150 if (flag_syntax_only)
2151 TREE_ASM_WRITTEN (vtbl) = 1;
2152 else if (DECL_ONE_ONLY (vtbl))
2153 {
2154 current = varpool_node::get_create (vtbl);
2155 if (last)
2156 current->add_to_same_comdat_group (last);
2157 last = current;
2158 }
2159 }
2160
2161 /* Since we're writing out the vtable here, also write the debug
2162 info. */
2163 note_debug_info_needed (ctype);
2164
2165 return true;
2166}
2167
2168/* A special return value from type_visibility meaning internal
2169 linkage. */
2170
2171enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2172
2173/* walk_tree helper function for type_visibility. */
2174
2175static tree
2176min_vis_r (tree *tp, int *walk_subtrees, void *data)
2177{
2178 int *vis_p = (int *)data;
2179 if (! TYPE_P (*tp))
2180 {
2181 *walk_subtrees = 0;
2182 }
2183 else if (OVERLOAD_TYPE_P (*tp)
2184 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2185 {
2186 *vis_p = VISIBILITY_ANON;
2187 return *tp;
2188 }
2189 else if (CLASS_TYPE_P (*tp)
2190 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2191 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2192 return NULL;
2193}
2194
2195/* Returns the visibility of TYPE, which is the minimum visibility of its
2196 component types. */
2197
2198static int
2199type_visibility (tree type)
2200{
2201 int vis = VISIBILITY_DEFAULT;
2202 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2203 return vis;
2204}
2205
2206/* Limit the visibility of DECL to VISIBILITY, if not explicitly
2207 specified (or if VISIBILITY is static). If TMPL is true, this
2208 constraint is for a template argument, and takes precedence
2209 over explicitly-specified visibility on the template. */
2210
2211static void
2212constrain_visibility (tree decl, int visibility, bool tmpl)
2213{
2214 if (visibility == VISIBILITY_ANON)
2215 {
2216 /* extern "C" declarations aren't affected by the anonymous
2217 namespace. */
2218 if (!DECL_EXTERN_C_P (decl))
2219 {
2220 TREE_PUBLIC (decl) = 0;
2221 DECL_WEAK (decl) = 0;
2222 DECL_COMMON (decl) = 0;
2223 DECL_COMDAT (decl) = false;
2224 if (VAR_OR_FUNCTION_DECL_P (decl))
2225 {
2226 struct symtab_node *snode = symtab_node::get (decl);
2227
2228 if (snode)
2229 snode->set_comdat_group (NULL);
2230 }
2231 DECL_INTERFACE_KNOWN (decl) = 1;
2232 if (DECL_LANG_SPECIFIC (decl))
2233 DECL_NOT_REALLY_EXTERN (decl) = 1;
2234 }
2235 }
2236 else if (visibility > DECL_VISIBILITY (decl)
2237 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2238 {
2239 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2240 /* This visibility was not specified. */
2241 DECL_VISIBILITY_SPECIFIED (decl) = false;
2242 }
2243}
2244
2245/* Constrain the visibility of DECL based on the visibility of its template
2246 arguments. */
2247
2248static void
2249constrain_visibility_for_template (tree decl, tree targs)
2250{
2251 /* If this is a template instantiation, check the innermost
2252 template args for visibility constraints. The outer template
2253 args are covered by the class check. */
2254 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2255 int i;
2256 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2257 {
2258 int vis = 0;
2259
2260 tree arg = TREE_VEC_ELT (args, i-1);
2261 if (TYPE_P (arg))
2262 vis = type_visibility (arg);
2263 else
2264 {
2265 if (REFERENCE_REF_P (arg))
2266 arg = TREE_OPERAND (arg, 0);
2267 if (TREE_TYPE (arg))
2268 STRIP_NOPS (arg);
2269 if (TREE_CODE (arg) == ADDR_EXPR)
2270 arg = TREE_OPERAND (arg, 0);
2271 if (VAR_OR_FUNCTION_DECL_P (arg))
2272 {
2273 if (! TREE_PUBLIC (arg))
2274 vis = VISIBILITY_ANON;
2275 else
2276 vis = DECL_VISIBILITY (arg);
2277 }
2278 }
2279 if (vis)
2280 constrain_visibility (decl, vis, true);
2281 }
2282}
2283
2284/* Like c_determine_visibility, but with additional C++-specific
2285 behavior.
2286
2287 Function-scope entities can rely on the function's visibility because
2288 it is set in start_preparsed_function.
2289
2290 Class-scope entities cannot rely on the class's visibility until the end
2291 of the enclosing class definition.
2292
2293 Note that because namespaces have multiple independent definitions,
2294 namespace visibility is handled elsewhere using the #pragma visibility
2295 machinery rather than by decorating the namespace declaration.
2296
2297 The goal is for constraints from the type to give a diagnostic, and
2298 other constraints to be applied silently. */
2299
2300void
2301determine_visibility (tree decl)
2302{
2303 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2304
2305 /* Only relevant for names with external linkage. */
2306 if (!TREE_PUBLIC (decl))
2307 return;
2308
2309 /* Cloned constructors and destructors get the same visibility as
2310 the underlying function. That should be set up in
2311 maybe_clone_body. */
2312 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2313
2314 bool orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2315 enum symbol_visibility orig_visibility = DECL_VISIBILITY (decl);
2316
2317 /* The decl may be a template instantiation, which could influence
2318 visibilty. */
2319 tree template_decl = NULL_TREE;
2320 if (TREE_CODE (decl) == TYPE_DECL)
2321 {
2322 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2323 {
2324 if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
2325 template_decl = decl;
2326 }
2327 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2328 template_decl = decl;
2329 }
2330 else if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
2331 template_decl = decl;
2332
2333 /* If DECL is a member of a class, visibility specifiers on the
2334 class can influence the visibility of the DECL. */
2335 tree class_type = NULL_TREE;
2336 if (DECL_CLASS_SCOPE_P (decl))
2337 class_type = DECL_CONTEXT (decl);
2338 else
2339 {
2340 /* Not a class member. */
2341
2342 /* Virtual tables have DECL_CONTEXT set to their associated class,
2343 so they are automatically handled above. */
2344 gcc_assert (!VAR_P (decl)
2345 || !DECL_VTABLE_OR_VTT_P (decl));
2346
2347 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2348 {
2349 /* Local statics and classes get the visibility of their
2350 containing function by default, except that
2351 -fvisibility-inlines-hidden doesn't affect them. */
2352 tree fn = DECL_CONTEXT (decl);
2353 if (DECL_VISIBILITY_SPECIFIED (fn))
2354 {
2355 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2356 DECL_VISIBILITY_SPECIFIED (decl) =
2357 DECL_VISIBILITY_SPECIFIED (fn);
2358 }
2359 else
2360 {
2361 if (DECL_CLASS_SCOPE_P (fn))
2362 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2363 else if (determine_hidden_inline (fn))
2364 {
2365 DECL_VISIBILITY (decl) = default_visibility;
2366 DECL_VISIBILITY_SPECIFIED (decl) =
2367 visibility_options.inpragma;
2368 }
2369 else
2370 {
2371 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2372 DECL_VISIBILITY_SPECIFIED (decl) =
2373 DECL_VISIBILITY_SPECIFIED (fn);
2374 }
2375 }
2376
2377 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2378 but have no TEMPLATE_INFO. Their containing template
2379 function does, and the local class could be constrained
2380 by that. */
2381 if (template_decl)
2382 template_decl = fn;
2383 }
2384 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2385 && flag_visibility_ms_compat)
2386 {
2387 /* Under -fvisibility-ms-compat, types are visible by default,
2388 even though their contents aren't. */
2389 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2390 int underlying_vis = type_visibility (underlying_type);
2391 if (underlying_vis == VISIBILITY_ANON
2392 || (CLASS_TYPE_P (underlying_type)
2393 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2394 constrain_visibility (decl, underlying_vis, false);
2395 else
2396 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2397 }
2398 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2399 {
2400 /* tinfo visibility is based on the type it's for. */
2401 constrain_visibility
2402 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2403
2404 /* Give the target a chance to override the visibility associated
2405 with DECL. */
2406 if (TREE_PUBLIC (decl)
2407 && !DECL_REALLY_EXTERN (decl)
2408 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2409 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2410 targetm.cxx.determine_class_data_visibility (decl);
2411 }
2412 else if (template_decl)
2413 /* Template instantiations and specializations get visibility based
2414 on their template unless they override it with an attribute. */;
2415 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2416 {
2417 if (determine_hidden_inline (decl))
2418 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2419 else
2420 {
2421 /* Set default visibility to whatever the user supplied with
2422 #pragma GCC visibility or a namespace visibility attribute. */
2423 DECL_VISIBILITY (decl) = default_visibility;
2424 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2425 }
2426 }
2427 }
2428
2429 if (template_decl)
2430 {
2431 /* If the specialization doesn't specify visibility, use the
2432 visibility from the template. */
2433 tree tinfo = get_template_info (template_decl);
2434 tree args = TI_ARGS (tinfo);
2435 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2436 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2437 : DECL_ATTRIBUTES (decl));
2438
2439 if (args != error_mark_node)
2440 {
2441 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2442
2443 if (!DECL_VISIBILITY_SPECIFIED (decl))
2444 {
2445 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2446 && determine_hidden_inline (decl))
2447 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2448 else
2449 {
2450 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2451 DECL_VISIBILITY_SPECIFIED (decl)
2452 = DECL_VISIBILITY_SPECIFIED (pattern);
2453 }
2454 }
2455
2456 if (args
2457 /* Template argument visibility outweighs #pragma or namespace
2458 visibility, but not an explicit attribute. */
2459 && !lookup_attribute ("visibility", attribs))
2460 {
2461 int depth = TMPL_ARGS_DEPTH (args);
2462 if (DECL_VISIBILITY_SPECIFIED (decl))
2463 {
2464 /* A class template member with explicit visibility
2465 overrides the class visibility, so we need to apply
2466 all the levels of template args directly. */
2467 int i;
2468 for (i = 1; i <= depth; ++i)
2469 {
2470 tree lev = TMPL_ARGS_LEVEL (args, i);
2471 constrain_visibility_for_template (decl, lev);
2472 }
2473 }
2474 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2475 /* Limit visibility based on its template arguments. */
2476 constrain_visibility_for_template (decl, args);
2477 }
2478 }
2479 }
2480
2481 if (class_type)
2482 determine_visibility_from_class (decl, class_type);
2483
2484 if (decl_anon_ns_mem_p (decl))
2485 /* Names in an anonymous namespace get internal linkage.
2486 This might change once we implement export. */
2487 constrain_visibility (decl, VISIBILITY_ANON, false);
2488 else if (TREE_CODE (decl) != TYPE_DECL)
2489 {
2490 /* Propagate anonymity from type to decl. */
2491 int tvis = type_visibility (TREE_TYPE (decl));
2492 if (tvis == VISIBILITY_ANON
2493 || ! DECL_VISIBILITY_SPECIFIED (decl))
2494 constrain_visibility (decl, tvis, false);
2495 }
2496 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2497 /* DR 757: A type without linkage shall not be used as the type of a
2498 variable or function with linkage, unless
2499 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2500 o the variable or function is not used (3.2 [basic.def.odr]) or is
2501 defined in the same translation unit.
2502
2503 Since non-extern "C" decls need to be defined in the same
2504 translation unit, we can make the type internal. */
2505 constrain_visibility (decl, VISIBILITY_ANON, false);
2506
2507 /* If visibility changed and DECL already has DECL_RTL, ensure
2508 symbol flags are updated. */
2509 if ((DECL_VISIBILITY (decl) != orig_visibility
2510 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2511 && ((VAR_P (decl) && TREE_STATIC (decl))
2512 || TREE_CODE (decl) == FUNCTION_DECL)
2513 && DECL_RTL_SET_P (decl))
2514 make_decl_rtl (decl);
2515}
2516
2517/* By default, static data members and function members receive
2518 the visibility of their containing class. */
2519
2520static void
2521determine_visibility_from_class (tree decl, tree class_type)
2522{
2523 if (DECL_VISIBILITY_SPECIFIED (decl))
2524 return;
2525
2526 if (determine_hidden_inline (decl))
2527 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2528 else
2529 {
2530 /* Default to the class visibility. */
2531 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2532 DECL_VISIBILITY_SPECIFIED (decl)
2533 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2534 }
2535
2536 /* Give the target a chance to override the visibility associated
2537 with DECL. */
2538 if (VAR_P (decl)
2539 && (DECL_TINFO_P (decl)
2540 || (DECL_VTABLE_OR_VTT_P (decl)
2541 /* Construction virtual tables are not exported because
2542 they cannot be referred to from other object files;
2543 their name is not standardized by the ABI. */
2544 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2545 && TREE_PUBLIC (decl)
2546 && !DECL_REALLY_EXTERN (decl)
2547 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2548 targetm.cxx.determine_class_data_visibility (decl);
2549}
2550
2551/* Returns true iff DECL is an inline that should get hidden visibility
2552 because of -fvisibility-inlines-hidden. */
2553
2554static bool
2555determine_hidden_inline (tree decl)
2556{
2557 return (visibility_options.inlines_hidden
2558 /* Don't do this for inline templates; specializations might not be
2559 inline, and we don't want them to inherit the hidden
2560 visibility. We'll set it here for all inline instantiations. */
2561 && !processing_template_decl
2562 && TREE_CODE (decl) == FUNCTION_DECL
2563 && DECL_DECLARED_INLINE_P (decl)
2564 && (! DECL_LANG_SPECIFIC (decl)
2565 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2566}
2567
2568/* Constrain the visibility of a class TYPE based on the visibility of its
2569 field types. Warn if any fields require lesser visibility. */
2570
2571void
2572constrain_class_visibility (tree type)
2573{
2574 tree binfo;
2575 tree t;
2576 int i;
2577
2578 int vis = type_visibility (type);
2579
2580 if (vis == VISIBILITY_ANON
2581 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2582 return;
2583
2584 /* Don't warn about visibility if the class has explicit visibility. */
2585 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2586 vis = VISIBILITY_INTERNAL;
2587
2588 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2589 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node
2590 && !DECL_ARTIFICIAL (t))
2591 {
2592 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2593 int subvis = type_visibility (ftype);
2594
2595 if (subvis == VISIBILITY_ANON)
2596 {
2597 if (!in_main_input_context())
2598 {
2599 tree nlt = no_linkage_check (ftype, /*relaxed_p=*/false);
2600 if (nlt)
2601 {
2602 if (same_type_p (TREE_TYPE (t), nlt))
2603 warning (OPT_Wsubobject_linkage, "\
2604%qT has a field %qD whose type has no linkage",
2605 type, t);
2606 else
2607 warning (OPT_Wsubobject_linkage, "\
2608%qT has a field %qD whose type depends on the type %qT which has no linkage",
2609 type, t, nlt);
2610 }
2611 else
2612 warning (OPT_Wsubobject_linkage, "\
2613%qT has a field %qD whose type uses the anonymous namespace",
2614 type, t);
2615 }
2616 }
2617 else if (MAYBE_CLASS_TYPE_P (ftype)
2618 && vis < VISIBILITY_HIDDEN
2619 && subvis >= VISIBILITY_HIDDEN)
2620 warning (OPT_Wattributes, "\
2621%qT declared with greater visibility than the type of its field %qD",
2622 type, t);
2623 }
2624
2625 binfo = TYPE_BINFO (type);
2626 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2627 {
2628 int subvis = type_visibility (TREE_TYPE (t));
2629
2630 if (subvis == VISIBILITY_ANON)
2631 {
2632 if (!in_main_input_context())
2633 {
2634 tree nlt = no_linkage_check (TREE_TYPE (t), /*relaxed_p=*/false);
2635 if (nlt)
2636 {
2637 if (same_type_p (TREE_TYPE (t), nlt))
2638 warning (OPT_Wsubobject_linkage, "\
2639%qT has a base %qT whose type has no linkage",
2640 type, TREE_TYPE (t));
2641 else
2642 warning (OPT_Wsubobject_linkage, "\
2643%qT has a base %qT whose type depends on the type %qT which has no linkage",
2644 type, TREE_TYPE (t), nlt);
2645 }
2646 else
2647 warning (OPT_Wsubobject_linkage, "\
2648%qT has a base %qT whose type uses the anonymous namespace",
2649 type, TREE_TYPE (t));
2650 }
2651 }
2652 else if (vis < VISIBILITY_HIDDEN
2653 && subvis >= VISIBILITY_HIDDEN)
2654 warning (OPT_Wattributes, "\
2655%qT declared with greater visibility than its base %qT",
2656 type, TREE_TYPE (t));
2657 }
2658}
2659
2660/* Functions for adjusting the visibility of a tagged type and its nested
2661 types and declarations when it gets a name for linkage purposes from a
2662 typedef. */
2663
2664static void bt_reset_linkage_1 (binding_entry, void *);
2665static void bt_reset_linkage_2 (binding_entry, void *);
2666
2667/* First reset the visibility of all the types. */
2668
2669static void
2670reset_type_linkage_1 (tree type)
2671{
2672 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2673 if (CLASS_TYPE_P (type))
2674 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2675 bt_reset_linkage_1, NULL);
2676}
2677static void
2678bt_reset_linkage_1 (binding_entry b, void */*data*/)
2679{
2680 reset_type_linkage_1 (b->type);
2681}
2682
2683/* Then reset the visibility of any static data members or member
2684 functions that use those types. */
2685
2686static void
2687reset_decl_linkage (tree decl)
2688{
2689 if (TREE_PUBLIC (decl))
2690 return;
2691 if (DECL_CLONED_FUNCTION_P (decl))
2692 return;
2693 TREE_PUBLIC (decl) = true;
2694 DECL_INTERFACE_KNOWN (decl) = false;
2695 determine_visibility (decl);
2696 tentative_decl_linkage (decl);
2697}
2698
2699static void
2700reset_type_linkage_2 (tree type)
2701{
2702 if (CLASS_TYPE_P (type))
2703 {
2704 if (tree vt = CLASSTYPE_VTABLES (type))
2705 {
2706 tree name = mangle_vtbl_for_type (type);
2707 DECL_NAME (vt) = name;
2708 SET_DECL_ASSEMBLER_NAME (vt, name);
2709 reset_decl_linkage (vt);
2710 }
2711 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2712 {
2713 tree name = mangle_typeinfo_for_type (type);
2714 DECL_NAME (ti) = name;
2715 SET_DECL_ASSEMBLER_NAME (ti, name);
2716 TREE_TYPE (name) = type;
2717 reset_decl_linkage (ti);
2718 }
2719 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2720 {
2721 tree mem = STRIP_TEMPLATE (m);
2722 if (TREE_CODE (mem) == VAR_DECL || TREE_CODE (mem) == FUNCTION_DECL)
2723 reset_decl_linkage (mem);
2724 }
2725 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2726 bt_reset_linkage_2, NULL);
2727 }
2728}
2729
2730static void
2731bt_reset_linkage_2 (binding_entry b, void */*data*/)
2732{
2733 reset_type_linkage_2 (b->type);
2734}
2735void
2736reset_type_linkage (tree type)
2737{
2738 reset_type_linkage_1 (type);
2739 reset_type_linkage_2 (type);
2740}
2741
2742/* Set up our initial idea of what the linkage of DECL should be. */
2743
2744void
2745tentative_decl_linkage (tree decl)
2746{
2747 if (DECL_INTERFACE_KNOWN (decl))
2748 /* We've already made a decision as to how this function will
2749 be handled. */;
2750 else if (vague_linkage_p (decl))
2751 {
2752 if (TREE_CODE (decl) == FUNCTION_DECL
2753 && decl_defined_p (decl))
2754 {
2755 DECL_EXTERNAL (decl) = 1;
2756 DECL_NOT_REALLY_EXTERN (decl) = 1;
2757 note_vague_linkage_fn (decl);
2758 /* A non-template inline function with external linkage will
2759 always be COMDAT. As we must eventually determine the
2760 linkage of all functions, and as that causes writes to
2761 the data mapped in from the PCH file, it's advantageous
2762 to mark the functions at this point. */
2763 if (DECL_DECLARED_INLINE_P (decl)
2764 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2765 || DECL_DEFAULTED_FN (decl)))
2766 {
2767 /* This function must have external linkage, as
2768 otherwise DECL_INTERFACE_KNOWN would have been
2769 set. */
2770 gcc_assert (TREE_PUBLIC (decl));
2771 comdat_linkage (decl);
2772 DECL_INTERFACE_KNOWN (decl) = 1;
2773 }
2774 }
2775 else if (VAR_P (decl))
2776 maybe_commonize_var (decl);
2777 }
2778}
2779
2780/* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2781 for DECL has not already been determined, do so now by setting
2782 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2783 function is called entities with vague linkage whose definitions
2784 are available must have TREE_PUBLIC set.
2785
2786 If this function decides to place DECL in COMDAT, it will set
2787 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2788 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2789 callers defer that decision until it is clear that DECL is actually
2790 required. */
2791
2792void
2793import_export_decl (tree decl)
2794{
2795 int emit_p;
2796 bool comdat_p;
2797 bool import_p;
2798 tree class_type = NULL_TREE;
2799
2800 if (DECL_INTERFACE_KNOWN (decl))
2801 return;
2802
2803 /* We cannot determine what linkage to give to an entity with vague
2804 linkage until the end of the file. For example, a virtual table
2805 for a class will be defined if and only if the key method is
2806 defined in this translation unit. As a further example, consider
2807 that when compiling a translation unit that uses PCH file with
2808 "-frepo" it would be incorrect to make decisions about what
2809 entities to emit when building the PCH; those decisions must be
2810 delayed until the repository information has been processed. */
2811 gcc_assert (at_eof);
2812 /* Object file linkage for explicit instantiations is handled in
2813 mark_decl_instantiated. For static variables in functions with
2814 vague linkage, maybe_commonize_var is used.
2815
2816 Therefore, the only declarations that should be provided to this
2817 function are those with external linkage that are:
2818
2819 * implicit instantiations of function templates
2820
2821 * inline function
2822
2823 * implicit instantiations of static data members of class
2824 templates
2825
2826 * virtual tables
2827
2828 * typeinfo objects
2829
2830 Furthermore, all entities that reach this point must have a
2831 definition available in this translation unit.
2832
2833 The following assertions check these conditions. */
2834 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2835 /* Any code that creates entities with TREE_PUBLIC cleared should
2836 also set DECL_INTERFACE_KNOWN. */
2837 gcc_assert (TREE_PUBLIC (decl));
2838 if (TREE_CODE (decl) == FUNCTION_DECL)
2839 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2840 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2841 || DECL_DECLARED_INLINE_P (decl));
2842 else
2843 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2844 || DECL_VTABLE_OR_VTT_P (decl)
2845 || DECL_TINFO_P (decl));
2846 /* Check that a definition of DECL is available in this translation
2847 unit. */
2848 gcc_assert (!DECL_REALLY_EXTERN (decl));
2849
2850 /* Assume that DECL will not have COMDAT linkage. */
2851 comdat_p = false;
2852 /* Assume that DECL will not be imported into this translation
2853 unit. */
2854 import_p = false;
2855
2856 /* See if the repository tells us whether or not to emit DECL in
2857 this translation unit. */
2858 emit_p = repo_emit_p (decl);
2859 if (emit_p == 0)
2860 import_p = true;
2861 else if (emit_p == 1)
2862 {
2863 /* The repository indicates that this entity should be defined
2864 here. Make sure the back end honors that request. */
2865 mark_needed (decl);
2866 /* Output the definition as an ordinary strong definition. */
2867 DECL_EXTERNAL (decl) = 0;
2868 DECL_INTERFACE_KNOWN (decl) = 1;
2869 return;
2870 }
2871
2872 if (import_p)
2873 /* We have already decided what to do with this DECL; there is no
2874 need to check anything further. */
2875 ;
2876 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
2877 {
2878 class_type = DECL_CONTEXT (decl);
2879 import_export_class (class_type);
2880 if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2881 && CLASSTYPE_INTERFACE_ONLY (class_type))
2882 import_p = true;
2883 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2884 && !CLASSTYPE_USE_TEMPLATE (class_type)
2885 && CLASSTYPE_KEY_METHOD (class_type)
2886 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2887 /* The ABI requires that all virtual tables be emitted with
2888 COMDAT linkage. However, on systems where COMDAT symbols
2889 don't show up in the table of contents for a static
2890 archive, or on systems without weak symbols (where we
2891 approximate COMDAT linkage by using internal linkage), the
2892 linker will report errors about undefined symbols because
2893 it will not see the virtual table definition. Therefore,
2894 in the case that we know that the virtual table will be
2895 emitted in only one translation unit, we make the virtual
2896 table an ordinary definition with external linkage. */
2897 DECL_EXTERNAL (decl) = 0;
2898 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2899 {
2900 /* CLASS_TYPE is being exported from this translation unit,
2901 so DECL should be defined here. */
2902 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2903 /* If a class is declared in a header with the "extern
2904 template" extension, then it will not be instantiated,
2905 even in translation units that would normally require
2906 it. Often such classes are explicitly instantiated in
2907 one translation unit. Therefore, the explicit
2908 instantiation must be made visible to other translation
2909 units. */
2910 DECL_EXTERNAL (decl) = 0;
2911 else
2912 {
2913 /* The generic C++ ABI says that class data is always
2914 COMDAT, even if there is a key function. Some
2915 variants (e.g., the ARM EABI) says that class data
2916 only has COMDAT linkage if the class data might be
2917 emitted in more than one translation unit. When the
2918 key method can be inline and is inline, we still have
2919 to arrange for comdat even though
2920 class_data_always_comdat is false. */
2921 if (!CLASSTYPE_KEY_METHOD (class_type)
2922 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2923 || targetm.cxx.class_data_always_comdat ())
2924 {
2925 /* The ABI requires COMDAT linkage. Normally, we
2926 only emit COMDAT things when they are needed;
2927 make sure that we realize that this entity is
2928 indeed needed. */
2929 comdat_p = true;
2930 mark_needed (decl);
2931 }
2932 }
2933 }
2934 else if (!flag_implicit_templates
2935 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2936 import_p = true;
2937 else
2938 comdat_p = true;
2939 }
2940 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2941 {
2942 tree type = TREE_TYPE (DECL_NAME (decl));
2943 if (CLASS_TYPE_P (type))
2944 {
2945 class_type = type;
2946 import_export_class (type);
2947 if (CLASSTYPE_INTERFACE_KNOWN (type)
2948 && TYPE_POLYMORPHIC_P (type)
2949 && CLASSTYPE_INTERFACE_ONLY (type)
2950 /* If -fno-rtti was specified, then we cannot be sure
2951 that RTTI information will be emitted with the
2952 virtual table of the class, so we must emit it
2953 wherever it is used. */
2954 && flag_rtti)
2955 import_p = true;
2956 else
2957 {
2958 if (CLASSTYPE_INTERFACE_KNOWN (type)
2959 && !CLASSTYPE_INTERFACE_ONLY (type))
2960 {
2961 comdat_p = (targetm.cxx.class_data_always_comdat ()
2962 || (CLASSTYPE_KEY_METHOD (type)
2963 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2964 mark_needed (decl);
2965 if (!flag_weak)
2966 {
2967 comdat_p = false;
2968 DECL_EXTERNAL (decl) = 0;
2969 }
2970 }
2971 else
2972 comdat_p = true;
2973 }
2974 }
2975 else
2976 comdat_p = true;
2977 }
2978 else if (DECL_TEMPLOID_INSTANTIATION (decl))
2979 {
2980 /* DECL is an implicit instantiation of a function or static
2981 data member. */
2982 if ((flag_implicit_templates
2983 && !flag_use_repository)
2984 || (flag_implicit_inline_templates
2985 && TREE_CODE (decl) == FUNCTION_DECL
2986 && DECL_DECLARED_INLINE_P (decl)))
2987 comdat_p = true;
2988 else
2989 /* If we are not implicitly generating templates, then mark
2990 this entity as undefined in this translation unit. */
2991 import_p = true;
2992 }
2993 else if (DECL_FUNCTION_MEMBER_P (decl))
2994 {
2995 if (!DECL_DECLARED_INLINE_P (decl))
2996 {
2997 tree ctype = DECL_CONTEXT (decl);
2998 import_export_class (ctype);
2999 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
3000 {
3001 DECL_NOT_REALLY_EXTERN (decl)
3002 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
3003 || (DECL_DECLARED_INLINE_P (decl)
3004 && ! flag_implement_inlines
3005 && !DECL_VINDEX (decl)));
3006
3007 if (!DECL_NOT_REALLY_EXTERN (decl))
3008 DECL_EXTERNAL (decl) = 1;
3009
3010 /* Always make artificials weak. */
3011 if (DECL_ARTIFICIAL (decl) && flag_weak)
3012 comdat_p = true;
3013 else
3014 maybe_make_one_only (decl);
3015 }
3016 }
3017 else
3018 comdat_p = true;
3019 }
3020 else
3021 comdat_p = true;
3022
3023 if (import_p)
3024 {
3025 /* If we are importing DECL into this translation unit, mark is
3026 an undefined here. */
3027 DECL_EXTERNAL (decl) = 1;
3028 DECL_NOT_REALLY_EXTERN (decl) = 0;
3029 }
3030 else if (comdat_p)
3031 {
3032 /* If we decided to put DECL in COMDAT, mark it accordingly at
3033 this point. */
3034 comdat_linkage (decl);
3035 }
3036
3037 DECL_INTERFACE_KNOWN (decl) = 1;
3038}
3039
3040/* Return an expression that performs the destruction of DECL, which
3041 must be a VAR_DECL whose type has a non-trivial destructor, or is
3042 an array whose (innermost) elements have a non-trivial destructor. */
3043
3044tree
3045build_cleanup (tree decl)
3046{
3047 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
3048 gcc_assert (clean != NULL_TREE);
3049 return clean;
3050}
3051
3052/* Returns the initialization guard variable for the variable DECL,
3053 which has static storage duration. */
3054
3055tree
3056get_guard (tree decl)
3057{
3058 tree sname;
3059 tree guard;
3060
3061 sname = mangle_guard_variable (decl);
3062 guard = get_global_binding (sname);
3063 if (! guard)
3064 {
3065 tree guard_type;
3066
3067 /* We use a type that is big enough to contain a mutex as well
3068 as an integer counter. */
3069 guard_type = targetm.cxx.guard_type ();
3070 guard = build_decl (DECL_SOURCE_LOCATION (decl),
3071 VAR_DECL, sname, guard_type);
3072
3073 /* The guard should have the same linkage as what it guards. */
3074 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
3075 TREE_STATIC (guard) = TREE_STATIC (decl);
3076 DECL_COMMON (guard) = DECL_COMMON (decl);
3077 DECL_COMDAT (guard) = DECL_COMDAT (decl);
3078 CP_DECL_THREAD_LOCAL_P (guard) = CP_DECL_THREAD_LOCAL_P (decl);
3079 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
3080 if (DECL_ONE_ONLY (decl))
3081 make_decl_one_only (guard, cxx_comdat_group (guard));
3082 if (TREE_PUBLIC (decl))
3083 DECL_WEAK (guard) = DECL_WEAK (decl);
3084 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
3085 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
3086
3087 DECL_ARTIFICIAL (guard) = 1;
3088 DECL_IGNORED_P (guard) = 1;
3089 TREE_USED (guard) = 1;
3090 pushdecl_top_level_and_finish (guard, NULL_TREE);
3091 }
3092 return guard;
3093}
3094
3095/* Return an atomic load of src with the appropriate memory model. */
3096
3097static tree
3098build_atomic_load_byte (tree src, HOST_WIDE_INT model)
3099{
3100 tree ptr_type = build_pointer_type (char_type_node);
3101 tree mem_model = build_int_cst (integer_type_node, model);
3102 tree t, addr, val;
3103 unsigned int size;
3104 int fncode;
3105
3106 size = tree_to_uhwi (TYPE_SIZE_UNIT (char_type_node));
3107
3108 fncode = BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1;
3109 t = builtin_decl_implicit ((enum built_in_function) fncode);
3110
3111 addr = build1 (ADDR_EXPR, ptr_type, src);
3112 val = build_call_expr (t, 2, addr, mem_model);
3113 return val;
3114}
3115
3116/* Return those bits of the GUARD variable that should be set when the
3117 guarded entity is actually initialized. */
3118
3119static tree
3120get_guard_bits (tree guard)
3121{
3122 if (!targetm.cxx.guard_mask_bit ())
3123 {
3124 /* We only set the first byte of the guard, in order to leave room
3125 for a mutex in the high-order bits. */
3126 guard = build1 (ADDR_EXPR,
3127 build_pointer_type (TREE_TYPE (guard)),
3128 guard);
3129 guard = build1 (NOP_EXPR,
3130 build_pointer_type (char_type_node),
3131 guard);
3132 guard = build1 (INDIRECT_REF, char_type_node, guard);
3133 }
3134
3135 return guard;
3136}
3137
3138/* Return an expression which determines whether or not the GUARD
3139 variable has already been initialized. */
3140
3141tree
3142get_guard_cond (tree guard, bool thread_safe)
3143{
3144 tree guard_value;
3145
3146 if (!thread_safe)
3147 guard = get_guard_bits (guard);
3148 else
3149 guard = build_atomic_load_byte (guard, MEMMODEL_ACQUIRE);
3150
3151 /* Mask off all but the low bit. */
3152 if (targetm.cxx.guard_mask_bit ())
3153 {
3154 guard_value = integer_one_node;
3155 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3156 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3157 guard = cp_build_binary_op (input_location,
3158 BIT_AND_EXPR, guard, guard_value,
3159 tf_warning_or_error);
3160 }
3161
3162 guard_value = integer_zero_node;
3163 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3164 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3165 return cp_build_binary_op (input_location,
3166 EQ_EXPR, guard, guard_value,
3167 tf_warning_or_error);
3168}
3169
3170/* Return an expression which sets the GUARD variable, indicating that
3171 the variable being guarded has been initialized. */
3172
3173tree
3174set_guard (tree guard)
3175{
3176 tree guard_init;
3177
3178 /* Set the GUARD to one. */
3179 guard = get_guard_bits (guard);
3180 guard_init = integer_one_node;
3181 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3182 guard_init = fold_convert (TREE_TYPE (guard), guard_init);
3183 return cp_build_modify_expr (input_location, guard, NOP_EXPR, guard_init,
3184 tf_warning_or_error);
3185}
3186
3187/* Returns true iff we can tell that VAR does not have a dynamic
3188 initializer. */
3189
3190static bool
3191var_defined_without_dynamic_init (tree var)
3192{
3193 /* If it's defined in another TU, we can't tell. */
3194 if (DECL_EXTERNAL (var))
3195 return false;
3196 /* If it has a non-trivial destructor, registering the destructor
3197 counts as dynamic initialization. */
3198 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3199 return false;
3200 /* If it's in this TU, its initializer has been processed, unless
3201 it's a case of self-initialization, then DECL_INITIALIZED_P is
3202 false while the initializer is handled by finish_id_expression. */
3203 if (!DECL_INITIALIZED_P (var))
3204 return false;
3205 /* If it has no initializer or a constant one, it's not dynamic. */
3206 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3207 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3208}
3209
3210/* Returns true iff VAR is a variable that needs uses to be
3211 wrapped for possible dynamic initialization. */
3212
3213static bool
3214var_needs_tls_wrapper (tree var)
3215{
3216 return (!error_operand_p (var)
3217 && CP_DECL_THREAD_LOCAL_P (var)
3218 && !DECL_GNU_TLS_P (var)
3219 && !DECL_FUNCTION_SCOPE_P (var)
3220 && !var_defined_without_dynamic_init (var));
3221}
3222
3223/* Get the FUNCTION_DECL for the shared TLS init function for this
3224 translation unit. */
3225
3226static tree
3227get_local_tls_init_fn (void)
3228{
3229 tree sname = get_identifier ("__tls_init");
3230 tree fn = get_global_binding (sname);
3231 if (!fn)
3232 {
3233 fn = build_lang_decl (FUNCTION_DECL, sname,
3234 build_function_type (void_type_node,
3235 void_list_node));
3236 SET_DECL_LANGUAGE (fn, lang_c);
3237 TREE_PUBLIC (fn) = false;
3238 DECL_ARTIFICIAL (fn) = true;
3239 mark_used (fn);
3240 set_global_binding (fn);
3241 }
3242 return fn;
3243}
3244
3245/* Get a FUNCTION_DECL for the init function for the thread_local
3246 variable VAR. The init function will be an alias to the function
3247 that initializes all the non-local TLS variables in the translation
3248 unit. The init function is only used by the wrapper function. */
3249
3250static tree
3251get_tls_init_fn (tree var)
3252{
3253 /* Only C++11 TLS vars need this init fn. */
3254 if (!var_needs_tls_wrapper (var))
3255 return NULL_TREE;
3256
3257 /* If -fno-extern-tls-init, assume that we don't need to call
3258 a tls init function for a variable defined in another TU. */
3259 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3260 return NULL_TREE;
3261
3262 /* If the variable is internal, or if we can't generate aliases,
3263 call the local init function directly. */
3264 if (!TREE_PUBLIC (var) || !TARGET_SUPPORTS_ALIASES)
3265 return get_local_tls_init_fn ();
3266
3267 tree sname = mangle_tls_init_fn (var);
3268 tree fn = get_global_binding (sname);
3269 if (!fn)
3270 {
3271 fn = build_lang_decl (FUNCTION_DECL, sname,
3272 build_function_type (void_type_node,
3273 void_list_node));
3274 SET_DECL_LANGUAGE (fn, lang_c);
3275 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3276 DECL_ARTIFICIAL (fn) = true;
3277 DECL_COMDAT (fn) = DECL_COMDAT (var);
3278 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3279 if (DECL_ONE_ONLY (var))
3280 make_decl_one_only (fn, cxx_comdat_group (fn));
3281 if (TREE_PUBLIC (var))
3282 {
3283 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3284 /* If the variable is defined somewhere else and might have static
3285 initialization, make the init function a weak reference. */
3286 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3287 || TYPE_HAS_CONSTEXPR_CTOR (obtype))
3288 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3289 && DECL_EXTERNAL (var))
3290 declare_weak (fn);
3291 else
3292 DECL_WEAK (fn) = DECL_WEAK (var);
3293 }
3294 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3295 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3296 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3297 DECL_IGNORED_P (fn) = 1;
3298 mark_used (fn);
3299
3300 DECL_BEFRIENDING_CLASSES (fn) = var;
3301
3302 set_global_binding (fn);
3303 }
3304 return fn;
3305}
3306
3307/* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3308 variable VAR. The wrapper function calls the init function (if any) for
3309 VAR and then returns a reference to VAR. The wrapper function is used
3310 in place of VAR everywhere VAR is mentioned. */
3311
3312tree
3313get_tls_wrapper_fn (tree var)
3314{
3315 /* Only C++11 TLS vars need this wrapper fn. */
3316 if (!var_needs_tls_wrapper (var))
3317 return NULL_TREE;
3318
3319 tree sname = mangle_tls_wrapper_fn (var);
3320 tree fn = get_global_binding (sname);
3321 if (!fn)
3322 {
3323 /* A named rvalue reference is an lvalue, so the wrapper should
3324 always return an lvalue reference. */
3325 tree type = non_reference (TREE_TYPE (var));
3326 type = build_reference_type (type);
3327 tree fntype = build_function_type (type, void_list_node);
3328 fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
3329 SET_DECL_LANGUAGE (fn, lang_c);
3330 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3331 DECL_ARTIFICIAL (fn) = true;
3332 DECL_IGNORED_P (fn) = 1;
3333 /* The wrapper is inline and emitted everywhere var is used. */
3334 DECL_DECLARED_INLINE_P (fn) = true;
3335 if (TREE_PUBLIC (var))
3336 {
3337 comdat_linkage (fn);
3338#ifdef HAVE_GAS_HIDDEN
3339 /* Make the wrapper bind locally; there's no reason to share
3340 the wrapper between multiple shared objects. */
3341 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3342 DECL_VISIBILITY_SPECIFIED (fn) = true;
3343#endif
3344 }
3345 if (!TREE_PUBLIC (fn))
3346 DECL_INTERFACE_KNOWN (fn) = true;
3347 mark_used (fn);
3348 note_vague_linkage_fn (fn);
3349
3350#if 0
3351 /* We want CSE to commonize calls to the wrapper, but marking it as
3352 pure is unsafe since it has side-effects. I guess we need a new
3353 ECF flag even weaker than ECF_PURE. FIXME! */
3354 DECL_PURE_P (fn) = true;
3355#endif
3356
3357 DECL_BEFRIENDING_CLASSES (fn) = var;
3358
3359 set_global_binding (fn);
3360 }
3361 return fn;
3362}
3363
3364/* At EOF, generate the definition for the TLS wrapper function FN:
3365
3366 T& var_wrapper() {
3367 if (init_fn) init_fn();
3368 return var;
3369 } */
3370
3371static void
3372generate_tls_wrapper (tree fn)
3373{
3374 tree var = DECL_BEFRIENDING_CLASSES (fn);
3375
3376 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3377 tree body = begin_function_body ();
3378 /* Only call the init fn if there might be one. */
3379 if (tree init_fn = get_tls_init_fn (var))
3380 {
3381 tree if_stmt = NULL_TREE;
3382 /* If init_fn is a weakref, make sure it exists before calling. */
3383 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3384 {
3385 if_stmt = begin_if_stmt ();
3386 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3387 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3388 NE_EXPR, addr, nullptr_node,
3389 tf_warning_or_error);
3390 finish_if_stmt_cond (cond, if_stmt);
3391 }
3392 finish_expr_stmt (build_cxx_call
3393 (init_fn, 0, NULL, tf_warning_or_error));
3394 if (if_stmt)
3395 {
3396 finish_then_clause (if_stmt);
3397 finish_if_stmt (if_stmt);
3398 }
3399 }
3400 else
3401 /* If there's no initialization, the wrapper is a constant function. */
3402 TREE_READONLY (fn) = true;
3403 finish_return_stmt (convert_from_reference (var));
3404 finish_function_body (body);
3405 expand_or_defer_fn (finish_function (/*inline_p=*/false));
3406}
3407
3408/* Start the process of running a particular set of global constructors
3409 or destructors. Subroutine of do_[cd]tors. Also called from
3410 vtv_start_verification_constructor_init_function. */
3411
3412static tree
3413start_objects (int method_type, int initp)
3414{
3415 tree body;
3416 tree fndecl;
3417 char type[14];
3418
3419 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3420
3421 if (initp != DEFAULT_INIT_PRIORITY)
3422 {
3423 char joiner;
3424
3425#ifdef JOINER
3426 joiner = JOINER;
3427#else
3428 joiner = '_';
3429#endif
3430
3431 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3432 }
3433 else
3434 sprintf (type, "sub_%c", method_type);
3435
3436 fndecl = build_lang_decl (FUNCTION_DECL,
3437 get_file_function_name (type),
3438 build_function_type_list (void_type_node,
3439 NULL_TREE));
3440 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3441
3442 TREE_PUBLIC (current_function_decl) = 0;
3443
3444 /* Mark as artificial because it's not explicitly in the user's
3445 source code. */
3446 DECL_ARTIFICIAL (current_function_decl) = 1;
3447
3448 /* Mark this declaration as used to avoid spurious warnings. */
3449 TREE_USED (current_function_decl) = 1;
3450
3451 /* Mark this function as a global constructor or destructor. */
3452 if (method_type == 'I')
3453 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3454 else
3455 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3456
3457 body = begin_compound_stmt (BCS_FN_BODY);
3458
3459 return body;
3460}
3461
3462/* Finish the process of running a particular set of global constructors
3463 or destructors. Subroutine of do_[cd]tors. */
3464
3465static void
3466finish_objects (int method_type, int initp, tree body)
3467{
3468 tree fn;
3469
3470 /* Finish up. */
3471 finish_compound_stmt (body);
3472 fn = finish_function (/*inline_p=*/false);
3473
3474 if (method_type == 'I')
3475 {
3476 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3477 decl_init_priority_insert (fn, initp);
3478 }
3479 else
3480 {
3481 DECL_STATIC_DESTRUCTOR (fn) = 1;
3482 decl_fini_priority_insert (fn, initp);
3483 }
3484
3485 expand_or_defer_fn (fn);
3486}
3487
3488/* The names of the parameters to the function created to handle
3489 initializations and destructions for objects with static storage
3490 duration. */
3491#define INITIALIZE_P_IDENTIFIER "__initialize_p"
3492#define PRIORITY_IDENTIFIER "__priority"
3493
3494/* The name of the function we create to handle initializations and
3495 destructions for objects with static storage duration. */
3496#define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3497
3498/* The declaration for the __INITIALIZE_P argument. */
3499static GTY(()) tree initialize_p_decl;
3500
3501/* The declaration for the __PRIORITY argument. */
3502static GTY(()) tree priority_decl;
3503
3504/* The declaration for the static storage duration function. */
3505static GTY(()) tree ssdf_decl;
3506
3507/* All the static storage duration functions created in this
3508 translation unit. */
3509static GTY(()) vec<tree, va_gc> *ssdf_decls;
3510
3511/* A map from priority levels to information about that priority
3512 level. There may be many such levels, so efficient lookup is
3513 important. */
3514static splay_tree priority_info_map;
3515
3516/* Begins the generation of the function that will handle all
3517 initialization and destruction of objects with static storage
3518 duration. The function generated takes two parameters of type
3519 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3520 nonzero, it performs initializations. Otherwise, it performs
3521 destructions. It only performs those initializations or
3522 destructions with the indicated __PRIORITY. The generated function
3523 returns no value.
3524
3525 It is assumed that this function will only be called once per
3526 translation unit. */
3527
3528static tree
3529start_static_storage_duration_function (unsigned count)
3530{
3531 tree type;
3532 tree body;
3533 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3534
3535 /* Create the identifier for this function. It will be of the form
3536 SSDF_IDENTIFIER_<number>. */
3537 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3538
3539 type = build_function_type_list (void_type_node,
3540 integer_type_node, integer_type_node,
3541 NULL_TREE);
3542
3543 /* Create the FUNCTION_DECL itself. */
3544 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3545 get_identifier (id),
3546 type);
3547 TREE_PUBLIC (ssdf_decl) = 0;
3548 DECL_ARTIFICIAL (ssdf_decl) = 1;
3549
3550 /* Put this function in the list of functions to be called from the
3551 static constructors and destructors. */
3552 if (!ssdf_decls)
3553 {
3554 vec_alloc (ssdf_decls, 32);
3555
3556 /* Take this opportunity to initialize the map from priority
3557 numbers to information about that priority level. */
3558 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3559 /*delete_key_fn=*/0,
3560 /*delete_value_fn=*/
3561 (splay_tree_delete_value_fn) &free);
3562
3563 /* We always need to generate functions for the
3564 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3565 priorities later, we'll be sure to find the
3566 DEFAULT_INIT_PRIORITY. */
3567 get_priority_info (DEFAULT_INIT_PRIORITY);
3568 }
3569
3570 vec_safe_push (ssdf_decls, ssdf_decl);
3571
3572 /* Create the argument list. */
3573 initialize_p_decl = cp_build_parm_decl
3574 (ssdf_decl, get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3575 TREE_USED (initialize_p_decl) = 1;
3576 priority_decl = cp_build_parm_decl
3577 (ssdf_decl, get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3578 TREE_USED (priority_decl) = 1;
3579
3580 DECL_CHAIN (initialize_p_decl) = priority_decl;
3581 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3582
3583 /* Put the function in the global scope. */
3584 pushdecl (ssdf_decl);
3585
3586 /* Start the function itself. This is equivalent to declaring the
3587 function as:
3588
3589 static void __ssdf (int __initialize_p, init __priority_p);
3590
3591 It is static because we only need to call this function from the
3592 various constructor and destructor functions for this module. */
3593 start_preparsed_function (ssdf_decl,
3594 /*attrs=*/NULL_TREE,
3595 SF_PRE_PARSED);
3596
3597 /* Set up the scope of the outermost block in the function. */
3598 body = begin_compound_stmt (BCS_FN_BODY);
3599
3600 return body;
3601}
3602
3603/* Finish the generation of the function which performs initialization
3604 and destruction of objects with static storage duration. After
3605 this point, no more such objects can be created. */
3606
3607static void
3608finish_static_storage_duration_function (tree body)
3609{
3610 /* Close out the function. */
3611 finish_compound_stmt (body);
3612 expand_or_defer_fn (finish_function (/*inline_p=*/false));
3613}
3614
3615/* Return the information about the indicated PRIORITY level. If no
3616 code to handle this level has yet been generated, generate the
3617 appropriate prologue. */
3618
3619static priority_info
3620get_priority_info (int priority)
3621{
3622 priority_info pi;
3623 splay_tree_node n;
3624
3625 n = splay_tree_lookup (priority_info_map,
3626 (splay_tree_key) priority);
3627 if (!n)
3628 {
3629 /* Create a new priority information structure, and insert it
3630 into the map. */
3631 pi = XNEW (struct priority_info_s);
3632 pi->initializations_p = 0;
3633 pi->destructions_p = 0;
3634 splay_tree_insert (priority_info_map,
3635 (splay_tree_key) priority,
3636 (splay_tree_value) pi);
3637 }
3638 else
3639 pi = (priority_info) n->value;
3640
3641 return pi;
3642}
3643
3644/* The effective initialization priority of a DECL. */
3645
3646#define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3647 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3648 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3649
3650/* Whether a DECL needs a guard to protect it against multiple
3651 initialization. */
3652
3653#define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3654 || DECL_ONE_ONLY (decl) \
3655 || DECL_WEAK (decl)))
3656
3657/* Called from one_static_initialization_or_destruction(),
3658 via walk_tree.
3659 Walks the initializer list of a global variable and looks for
3660 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3661 and that have their DECL_CONTEXT() == NULL.
3662 For each such temporary variable, set their DECL_CONTEXT() to
3663 the current function. This is necessary because otherwise
3664 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3665 when trying to refer to a temporary variable that does not have
3666 it's DECL_CONTECT() properly set. */
3667static tree
3668fix_temporary_vars_context_r (tree *node,
3669 int * /*unused*/,
3670 void * /*unused1*/)
3671{
3672 gcc_assert (current_function_decl);
3673
3674 if (TREE_CODE (*node) == BIND_EXPR)
3675 {
3676 tree var;
3677
3678 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3679 if (VAR_P (var)
3680 && !DECL_NAME (var)
3681 && DECL_ARTIFICIAL (var)
3682 && !DECL_CONTEXT (var))
3683 DECL_CONTEXT (var) = current_function_decl;
3684 }
3685
3686 return NULL_TREE;
3687}
3688
3689/* Set up to handle the initialization or destruction of DECL. If
3690 INITP is nonzero, we are initializing the variable. Otherwise, we
3691 are destroying it. */
3692
3693static void
3694one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3695{
3696 tree guard_if_stmt = NULL_TREE;
3697 tree guard;
3698
3699 /* If we are supposed to destruct and there's a trivial destructor,
3700 nothing has to be done. */
3701 if (!initp
3702 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3703 return;
3704
3705 /* Trick the compiler into thinking we are at the file and line
3706 where DECL was declared so that error-messages make sense, and so
3707 that the debugger will show somewhat sensible file and line
3708 information. */
3709 input_location = DECL_SOURCE_LOCATION (decl);
3710
3711 /* Make sure temporary variables in the initialiser all have
3712 their DECL_CONTEXT() set to a value different from NULL_TREE.
3713 This can happen when global variables initializers are built.
3714 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3715 the temporary variables that might have been generated in the
3716 accompanying initializers is NULL_TREE, meaning the variables have been
3717 declared in the global namespace.
3718 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3719 of the temporaries are set to the current function decl. */
3720 cp_walk_tree_without_duplicates (&init,
3721 fix_temporary_vars_context_r,
3722 NULL);
3723
3724 /* Because of:
3725
3726 [class.access.spec]
3727
3728 Access control for implicit calls to the constructors,
3729 the conversion functions, or the destructor called to
3730 create and destroy a static data member is performed as
3731 if these calls appeared in the scope of the member's
3732 class.
3733
3734 we pretend we are in a static member function of the class of
3735 which the DECL is a member. */
3736 if (member_p (decl))
3737 {
3738 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3739 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3740 }
3741
3742 /* Assume we don't need a guard. */
3743 guard = NULL_TREE;
3744 /* We need a guard if this is an object with external linkage that
3745 might be initialized in more than one place. (For example, a
3746 static data member of a template, when the data member requires
3747 construction.) */
3748 if (NEEDS_GUARD_P (decl))
3749 {
3750 tree guard_cond;
3751
3752 guard = get_guard (decl);
3753
3754 /* When using __cxa_atexit, we just check the GUARD as we would
3755 for a local static. */
3756 if (flag_use_cxa_atexit)
3757 {
3758 /* When using __cxa_atexit, we never try to destroy
3759 anything from a static destructor. */
3760 gcc_assert (initp);
3761 guard_cond = get_guard_cond (guard, false);
3762 }
3763 /* If we don't have __cxa_atexit, then we will be running
3764 destructors from .fini sections, or their equivalents. So,
3765 we need to know how many times we've tried to initialize this
3766 object. We do initializations only if the GUARD is zero,
3767 i.e., if we are the first to initialize the variable. We do
3768 destructions only if the GUARD is one, i.e., if we are the
3769 last to destroy the variable. */
3770 else if (initp)
3771 guard_cond
3772 = cp_build_binary_op (input_location,
3773 EQ_EXPR,
3774 cp_build_unary_op (PREINCREMENT_EXPR,
3775 guard,
3776 /*noconvert=*/true,
3777 tf_warning_or_error),
3778 integer_one_node,
3779 tf_warning_or_error);
3780 else
3781 guard_cond
3782 = cp_build_binary_op (input_location,
3783 EQ_EXPR,
3784 cp_build_unary_op (PREDECREMENT_EXPR,
3785 guard,
3786 /*noconvert=*/true,
3787 tf_warning_or_error),
3788 integer_zero_node,
3789 tf_warning_or_error);
3790
3791 guard_if_stmt = begin_if_stmt ();
3792 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3793 }
3794
3795
3796 /* If we're using __cxa_atexit, we have not already set the GUARD,
3797 so we must do so now. */
3798 if (guard && initp && flag_use_cxa_atexit)
3799 finish_expr_stmt (set_guard (guard));
3800
3801 /* Perform the initialization or destruction. */
3802 if (initp)
3803 {
3804 if (init)
3805 {
3806 finish_expr_stmt (init);
3807 if (sanitize_flags_p (SANITIZE_ADDRESS, decl))
3808 {
3809 varpool_node *vnode = varpool_node::get (decl);
3810 if (vnode)
3811 vnode->dynamically_initialized = 1;
3812 }
3813 }
3814
3815 /* If we're using __cxa_atexit, register a function that calls the
3816 destructor for the object. */
3817 if (flag_use_cxa_atexit)
3818 finish_expr_stmt (register_dtor_fn (decl));
3819 }
3820 else
3821 finish_expr_stmt (build_cleanup (decl));
3822
3823 /* Finish the guard if-stmt, if necessary. */
3824 if (guard)
3825 {
3826 finish_then_clause (guard_if_stmt);
3827 finish_if_stmt (guard_if_stmt);
3828 }
3829
3830 /* Now that we're done with DECL we don't need to pretend to be a
3831 member of its class any longer. */
3832 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3833 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3834}
3835
3836/* Generate code to do the initialization or destruction of the decls in VARS,
3837 a TREE_LIST of VAR_DECL with static storage duration.
3838 Whether initialization or destruction is performed is specified by INITP. */
3839
3840static void
3841do_static_initialization_or_destruction (tree vars, bool initp)
3842{
3843 tree node, init_if_stmt, cond;
3844
3845 /* Build the outer if-stmt to check for initialization or destruction. */
3846 init_if_stmt = begin_if_stmt ();
3847 cond = initp ? integer_one_node : integer_zero_node;
3848 cond = cp_build_binary_op (input_location,
3849 EQ_EXPR,
3850 initialize_p_decl,
3851 cond,
3852 tf_warning_or_error);
3853 finish_if_stmt_cond (cond, init_if_stmt);
3854
3855 /* To make sure dynamic construction doesn't access globals from other
3856 compilation units where they might not be yet constructed, for
3857 -fsanitize=address insert __asan_before_dynamic_init call that
3858 prevents access to either all global variables that need construction
3859 in other compilation units, or at least those that haven't been
3860 initialized yet. Variables that need dynamic construction in
3861 the current compilation unit are kept accessible. */
3862 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
3863 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3864
3865 node = vars;
3866 do {
3867 tree decl = TREE_VALUE (node);
3868 tree priority_if_stmt;
3869 int priority;
3870 priority_info pi;
3871
3872 /* If we don't need a destructor, there's nothing to do. Avoid
3873 creating a possibly empty if-stmt. */
3874 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3875 {
3876 node = TREE_CHAIN (node);
3877 continue;
3878 }
3879
3880 /* Remember that we had an initialization or finalization at this
3881 priority. */
3882 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3883 pi = get_priority_info (priority);
3884 if (initp)
3885 pi->initializations_p = 1;
3886 else
3887 pi->destructions_p = 1;
3888
3889 /* Conditionalize this initialization on being in the right priority
3890 and being initializing/finalizing appropriately. */
3891 priority_if_stmt = begin_if_stmt ();
3892 cond = cp_build_binary_op (input_location,
3893 EQ_EXPR,
3894 priority_decl,
3895 build_int_cst (NULL_TREE, priority),
3896 tf_warning_or_error);
3897 finish_if_stmt_cond (cond, priority_if_stmt);
3898
3899 /* Process initializers with same priority. */
3900 for (; node
3901 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3902 node = TREE_CHAIN (node))
3903 /* Do one initialization or destruction. */
3904 one_static_initialization_or_destruction (TREE_VALUE (node),
3905 TREE_PURPOSE (node), initp);
3906
3907 /* Finish up the priority if-stmt body. */
3908 finish_then_clause (priority_if_stmt);
3909 finish_if_stmt (priority_if_stmt);
3910
3911 } while (node);
3912
3913 /* Revert what __asan_before_dynamic_init did by calling
3914 __asan_after_dynamic_init. */
3915 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
3916 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
3917
3918 /* Finish up the init/destruct if-stmt body. */
3919 finish_then_clause (init_if_stmt);
3920 finish_if_stmt (init_if_stmt);
3921}
3922
3923/* VARS is a list of variables with static storage duration which may
3924 need initialization and/or finalization. Remove those variables
3925 that don't really need to be initialized or finalized, and return
3926 the resulting list. The order in which the variables appear in
3927 VARS is in reverse order of the order in which they should actually
3928 be initialized. The list we return is in the unreversed order;
3929 i.e., the first variable should be initialized first. */
3930
3931static tree
3932prune_vars_needing_no_initialization (tree *vars)
3933{
3934 tree *var = vars;
3935 tree result = NULL_TREE;
3936
3937 while (*var)
3938 {
3939 tree t = *var;
3940 tree decl = TREE_VALUE (t);
3941 tree init = TREE_PURPOSE (t);
3942
3943 /* Deal gracefully with error. */
3944 if (error_operand_p (decl))
3945 {
3946 var = &TREE_CHAIN (t);
3947 continue;
3948 }
3949
3950 /* The only things that can be initialized are variables. */
3951 gcc_assert (VAR_P (decl));
3952
3953 /* If this object is not defined, we don't need to do anything
3954 here. */
3955 if (DECL_EXTERNAL (decl))
3956 {
3957 var = &TREE_CHAIN (t);
3958 continue;
3959 }
3960
3961 /* Also, if the initializer already contains errors, we can bail
3962 out now. */
3963 if (init && TREE_CODE (init) == TREE_LIST
3964 && value_member (error_mark_node, init))
3965 {
3966 var = &TREE_CHAIN (t);
3967 continue;
3968 }
3969
3970 /* This variable is going to need initialization and/or
3971 finalization, so we add it to the list. */
3972 *var = TREE_CHAIN (t);
3973 TREE_CHAIN (t) = result;
3974 result = t;
3975 }
3976
3977 return result;
3978}
3979
3980/* Make sure we have told the back end about all the variables in
3981 VARS. */
3982
3983static void
3984write_out_vars (tree vars)
3985{
3986 tree v;
3987
3988 for (v = vars; v; v = TREE_CHAIN (v))
3989 {
3990 tree var = TREE_VALUE (v);
3991 if (!var_finalized_p (var))
3992 {
3993 import_export_decl (var);
3994 rest_of_decl_compilation (var, 1, 1);
3995 }
3996 }
3997}
3998
3999/* Generate a static constructor (if CONSTRUCTOR_P) or destructor
4000 (otherwise) that will initialize all global objects with static
4001 storage duration having the indicated PRIORITY. */
4002
4003static void
4004generate_ctor_or_dtor_function (bool constructor_p, int priority,
4005 location_t *locus)
4006{
4007 char function_key;
4008 tree fndecl;
4009 tree body;
4010 size_t i;
4011
4012 input_location = *locus;
4013 /* ??? */
4014 /* Was: locus->line++; */
4015
4016 /* We use `I' to indicate initialization and `D' to indicate
4017 destruction. */
4018 function_key = constructor_p ? 'I' : 'D';
4019
4020 /* We emit the function lazily, to avoid generating empty
4021 global constructors and destructors. */
4022 body = NULL_TREE;
4023
4024 /* For Objective-C++, we may need to initialize metadata found in this module.
4025 This must be done _before_ any other static initializations. */
4026 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
4027 && constructor_p && objc_static_init_needed_p ())
4028 {
4029 body = start_objects (function_key, priority);
4030 objc_generate_static_init_call (NULL_TREE);
4031 }
4032
4033 /* Call the static storage duration function with appropriate
4034 arguments. */
4035 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
4036 {
4037 /* Calls to pure or const functions will expand to nothing. */
4038 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
4039 {
4040 tree call;
4041
4042 if (! body)
4043 body = start_objects (function_key, priority);
4044
4045 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
4046 build_int_cst (NULL_TREE,
4047 constructor_p),
4048 build_int_cst (NULL_TREE,
4049 priority),
4050 NULL_TREE);
4051 finish_expr_stmt (call);
4052 }
4053 }
4054
4055 /* Close out the function. */
4056 if (body)
4057 finish_objects (function_key, priority, body);
4058}
4059
4060/* Generate constructor and destructor functions for the priority
4061 indicated by N. */
4062
4063static int
4064generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
4065{
4066 location_t *locus = (location_t *) data;
4067 int priority = (int) n->key;
4068 priority_info pi = (priority_info) n->value;
4069
4070 /* Generate the functions themselves, but only if they are really
4071 needed. */
4072 if (pi->initializations_p)
4073 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
4074 if (pi->destructions_p)
4075 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
4076
4077 /* Keep iterating. */
4078 return 0;
4079}
4080
4081/* Return C++ property of T, based on given operation OP. */
4082
4083static int
4084cpp_check (tree t, cpp_operation op)
4085{
4086 switch (op)
4087 {
4088 case HAS_DEPENDENT_TEMPLATE_ARGS:
4089 {
4090 tree ti = CLASSTYPE_TEMPLATE_INFO (t);
4091 if (!ti)
4092 return 0;
4093 ++processing_template_decl;
4094 const bool dep = any_dependent_template_arguments_p (TI_ARGS (ti));
4095 --processing_template_decl;
4096 return dep;
4097 }
4098 case IS_ABSTRACT:
4099 return DECL_PURE_VIRTUAL_P (t);
4100 case IS_CONSTRUCTOR:
4101 return DECL_CONSTRUCTOR_P (t);
4102 case IS_DESTRUCTOR:
4103 return DECL_DESTRUCTOR_P (t);
4104 case IS_COPY_CONSTRUCTOR:
4105 return DECL_COPY_CONSTRUCTOR_P (t);
4106 case IS_MOVE_CONSTRUCTOR:
4107 return DECL_MOVE_CONSTRUCTOR_P (t);
4108 case IS_TEMPLATE:
4109 return TREE_CODE (t) == TEMPLATE_DECL;
4110 case IS_TRIVIAL:
4111 return trivial_type_p (t);
4112 default:
4113 return 0;
4114 }
4115}
4116
4117/* Collect source file references recursively, starting from NAMESPC. */
4118
4119static void
4120collect_source_refs (tree namespc)
4121{
4122 /* Iterate over names in this name space. */
4123 for (tree t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4124 if (DECL_IS_BUILTIN (t))
4125 ;
4126 else if (TREE_CODE (t) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (t))
4127 collect_source_refs (t);
4128 else
4129 collect_source_ref (DECL_SOURCE_FILE (t));
4130}
4131
4132/* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4133 starting from NAMESPC. */
4134
4135static void
4136collect_ada_namespace (tree namespc, const char *source_file)
4137{
4138 tree decl = NAMESPACE_LEVEL (namespc)->names;
4139
4140 /* Collect decls from this namespace. This will skip
4141 NAMESPACE_DECLs (both aliases and regular, it cannot tell). */
4142 collect_ada_nodes (decl, source_file);
4143
4144 /* Now scan for namespace children, and dump them. */
4145 for (; decl; decl = TREE_CHAIN (decl))
4146 if (TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl))
4147 collect_ada_namespace (decl, source_file);
4148}
4149
4150/* Returns true iff there is a definition available for variable or
4151 function DECL. */
4152
4153bool
4154decl_defined_p (tree decl)
4155{
4156 if (TREE_CODE (decl) == FUNCTION_DECL)
4157 return (DECL_INITIAL (decl) != NULL_TREE
4158 /* A pending instantiation of a friend temploid is defined. */
4159 || (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
4160 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4161 (DECL_TI_TEMPLATE (decl)))));
4162 else
4163 {
4164 gcc_assert (VAR_P (decl));
4165 return !DECL_EXTERNAL (decl);
4166 }
4167}
4168
4169/* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4170
4171 [expr.const]
4172
4173 An integral constant-expression can only involve ... const
4174 variables of integral or enumeration types initialized with
4175 constant expressions ...
4176
4177 C++0x also allows constexpr variables and temporaries initialized
4178 with constant expressions. We handle the former here, but the latter
4179 are just folded away in cxx_eval_constant_expression.
4180
4181 The standard does not require that the expression be non-volatile.
4182 G++ implements the proposed correction in DR 457. */
4183
4184bool
4185decl_constant_var_p (tree decl)
4186{
4187 if (!decl_maybe_constant_var_p (decl))
4188 return false;
4189
4190 /* We don't know if a template static data member is initialized with
4191 a constant expression until we instantiate its initializer. Even
4192 in the case of a constexpr variable, we can't treat it as a
4193 constant until its initializer is complete in case it's used in
4194 its own initializer. */
4195 maybe_instantiate_decl (decl);
4196 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4197}
4198
4199/* Returns true if DECL could be a symbolic constant variable, depending on
4200 its initializer. */
4201
4202bool
4203decl_maybe_constant_var_p (tree decl)
4204{
4205 tree type = TREE_TYPE (decl);
4206 if (!VAR_P (decl))
4207 return false;
4208 if (DECL_DECLARED_CONSTEXPR_P (decl))
4209 return true;
4210 if (DECL_HAS_VALUE_EXPR_P (decl))
4211 /* A proxy isn't constant. */
4212 return false;
4213 if (TREE_CODE (type) == REFERENCE_TYPE)
4214 /* References can be constant. */;
4215 else if (CP_TYPE_CONST_NON_VOLATILE_P (type)
4216 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4217 /* And const integers. */;
4218 else
4219 return false;
4220
4221 if (DECL_INITIAL (decl)
4222 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
4223 /* We know the initializer, and it isn't constant. */
4224 return false;
4225 else
4226 return true;
4227}
4228
4229/* Complain that DECL uses a type with no linkage. In C++98 mode this is
4230 called from grokfndecl and grokvardecl; in all modes it is called from
4231 cp_write_global_declarations. */
4232
4233void
4234no_linkage_error (tree decl)
4235{
4236 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4237 /* In C++11 it's ok if the decl is defined. */
4238 return;
4239 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4240 if (t == NULL_TREE)
4241 /* The type that got us on no_linkage_decls must have gotten a name for
4242 linkage purposes. */;
4243 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4244 /* The type might end up having a typedef name for linkage purposes. */
4245 vec_safe_push (no_linkage_decls, decl);
4246 else if (TYPE_UNNAMED_P (t))
4247 {
4248 bool d = false;
4249 if (cxx_dialect >= cxx11)
4250 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4251 "unnamed type, is used but never defined", decl);
4252 else if (DECL_EXTERN_C_P (decl))
4253 /* Allow this; it's pretty common in C. */;
4254 else if (VAR_P (decl))
4255 /* DRs 132, 319 and 389 seem to indicate types with
4256 no linkage can only be used to declare extern "C"
4257 entities. Since it's not always an error in the
4258 ISO C++ 90 Standard, we only issue a warning. */
4259 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "unnamed type "
4260 "with no linkage used to declare variable %q#D with "
4261 "linkage", decl);
4262 else
4263 d = permerror (DECL_SOURCE_LOCATION (decl), "unnamed type with no "
4264 "linkage used to declare function %q#D with linkage",
4265 decl);
4266 if (d && is_typedef_decl (TYPE_NAME (t)))
4267 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4268 "to the unqualified type, so it is not used for linkage",
4269 TYPE_NAME (t));
4270 }
4271 else if (cxx_dialect >= cxx11)
4272 {
4273 if (VAR_P (decl) || !