]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/decl2.c
Wrap option names in gcc internal messages with %< and %>.
[thirdparty/gcc.git] / gcc / cp / decl2.c
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2019 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
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. */
53 int raw_dump_id;
54
55 extern cpp_reader *parse_in;
56
57 /* This structure contains information about the initializations
58 and/or destructions required for a particular priority level. */
59 typedef struct priority_info_s {
60 /* Nonzero if there have been any initializations at this priority
61 throughout the translation unit. */
62 int initializations_p;
63 /* Nonzero if there have been any destructions at this priority
64 throughout the translation unit. */
65 int destructions_p;
66 } *priority_info;
67
68 static void mark_vtable_entries (tree);
69 static bool maybe_emit_vtables (tree);
70 static tree start_objects (int, int);
71 static void finish_objects (int, int, tree);
72 static tree start_static_storage_duration_function (unsigned);
73 static void finish_static_storage_duration_function (tree);
74 static priority_info get_priority_info (int);
75 static void do_static_initialization_or_destruction (tree, bool);
76 static void one_static_initialization_or_destruction (tree, tree, bool);
77 static void generate_ctor_or_dtor_function (bool, int, location_t *);
78 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
79 void *);
80 static tree prune_vars_needing_no_initialization (tree *);
81 static void write_out_vars (tree);
82 static void import_export_class (tree);
83 static tree get_guard_bits (tree);
84 static void determine_visibility_from_class (tree, tree);
85 static bool determine_hidden_inline (tree);
86 static void maybe_instantiate_decl (tree);
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. */
91 static 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. */
95 static 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. */
99 static 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. */
103 static GTY(()) vec<tree, va_gc> *mangling_aliases;
104
105 /* hash traits for declarations. Hashes single decls via
106 DECL_ASSEMBLER_NAME_RAW. */
107
108 struct 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. */
138 static GTY(()) hash_table<mangled_decl_hash> *mangled_decls;
139
140 /* Nonzero if we're done parsing and into end-of-file activities. */
141
142 int at_eof;
143
144 /* True if note_mangling_alias should enqueue mangling aliases for
145 later generation, rather than emitting them right away. */
146
147 bool defer_mangling_aliases = true;
148 \f
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
154 tree
155 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
156 cp_ref_qualifier rqual)
157 {
158 if (fntype == error_mark_node || ctype == error_mark_node)
159 return error_mark_node;
160
161 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
162 || TREE_CODE (fntype) == METHOD_TYPE);
163
164 cp_cv_quals type_quals = quals & ~TYPE_QUAL_RESTRICT;
165 ctype = cp_build_qualified_type (ctype, type_quals);
166
167 tree newtype
168 = build_method_type_directly (ctype, TREE_TYPE (fntype),
169 (TREE_CODE (fntype) == METHOD_TYPE
170 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
171 : TYPE_ARG_TYPES (fntype)));
172 if (tree attrs = TYPE_ATTRIBUTES (fntype))
173 newtype = cp_build_type_attribute_variant (newtype, attrs);
174 newtype = build_cp_fntype_variant (newtype, rqual,
175 TYPE_RAISES_EXCEPTIONS (fntype),
176 TYPE_HAS_LATE_RETURN_TYPE (fntype));
177
178 return newtype;
179 }
180
181 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
182 return type changed to NEW_RET. */
183
184 tree
185 change_return_type (tree new_ret, tree fntype)
186 {
187 if (new_ret == error_mark_node)
188 return fntype;
189
190 if (same_type_p (new_ret, TREE_TYPE (fntype)))
191 return fntype;
192
193 tree newtype;
194 tree args = TYPE_ARG_TYPES (fntype);
195
196 if (TREE_CODE (fntype) == FUNCTION_TYPE)
197 {
198 newtype = build_function_type (new_ret, args);
199 newtype = apply_memfn_quals (newtype,
200 type_memfn_quals (fntype));
201 }
202 else
203 newtype = build_method_type_directly
204 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
205
206 if (tree attrs = TYPE_ATTRIBUTES (fntype))
207 newtype = cp_build_type_attribute_variant (newtype, attrs);
208 newtype = cxx_copy_lang_qualifiers (newtype, fntype);
209
210 return newtype;
211 }
212
213 /* Build a PARM_DECL of FN with NAME and TYPE, and set DECL_ARG_TYPE
214 appropriately. */
215
216 tree
217 cp_build_parm_decl (tree fn, tree name, tree type)
218 {
219 tree parm = build_decl (input_location,
220 PARM_DECL, name, type);
221 DECL_CONTEXT (parm) = fn;
222
223 /* DECL_ARG_TYPE is only used by the back end and the back end never
224 sees templates. */
225 if (!processing_template_decl)
226 DECL_ARG_TYPE (parm) = type_passed_as (type);
227
228 return parm;
229 }
230
231 /* Returns a PARM_DECL of FN for a parameter of the indicated TYPE, with the
232 indicated NAME. */
233
234 tree
235 build_artificial_parm (tree fn, tree name, tree type)
236 {
237 tree parm = cp_build_parm_decl (fn, name, type);
238 DECL_ARTIFICIAL (parm) = 1;
239 /* All our artificial parms are implicitly `const'; they cannot be
240 assigned to. */
241 TREE_READONLY (parm) = 1;
242 return parm;
243 }
244
245 /* Constructors for types with virtual baseclasses need an "in-charge" flag
246 saying whether this constructor is responsible for initialization of
247 virtual baseclasses or not. All destructors also need this "in-charge"
248 flag, which additionally determines whether or not the destructor should
249 free the memory for the object.
250
251 This function adds the "in-charge" flag to member function FN if
252 appropriate. It is called from grokclassfn and tsubst.
253 FN must be either a constructor or destructor.
254
255 The in-charge flag follows the 'this' parameter, and is followed by the
256 VTT parm (if any), then the user-written parms. */
257
258 void
259 maybe_retrofit_in_chrg (tree fn)
260 {
261 tree basetype, arg_types, parms, parm, fntype;
262
263 /* If we've already add the in-charge parameter don't do it again. */
264 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
265 return;
266
267 /* When processing templates we can't know, in general, whether or
268 not we're going to have virtual baseclasses. */
269 if (processing_template_decl)
270 return;
271
272 /* We don't need an in-charge parameter for constructors that don't
273 have virtual bases. */
274 if (DECL_CONSTRUCTOR_P (fn)
275 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
276 return;
277
278 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
279 basetype = TREE_TYPE (TREE_VALUE (arg_types));
280 arg_types = TREE_CHAIN (arg_types);
281
282 parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
283
284 /* If this is a subobject constructor or destructor, our caller will
285 pass us a pointer to our VTT. */
286 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
287 {
288 parm = build_artificial_parm (fn, vtt_parm_identifier, vtt_parm_type);
289
290 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
291 DECL_CHAIN (parm) = parms;
292 parms = parm;
293
294 /* ...and then to TYPE_ARG_TYPES. */
295 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
296
297 DECL_HAS_VTT_PARM_P (fn) = 1;
298 }
299
300 /* Then add the in-charge parm (before the VTT parm). */
301 parm = build_artificial_parm (fn, in_charge_identifier, integer_type_node);
302 DECL_CHAIN (parm) = parms;
303 parms = parm;
304 arg_types = hash_tree_chain (integer_type_node, arg_types);
305
306 /* Insert our new parameter(s) into the list. */
307 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
308
309 /* And rebuild the function type. */
310 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
311 arg_types);
312 if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
313 fntype = (cp_build_type_attribute_variant
314 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
315 fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (fn));
316 TREE_TYPE (fn) = fntype;
317
318 /* Now we've got the in-charge parameter. */
319 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
320 }
321
322 /* Classes overload their constituent function names automatically.
323 When a function name is declared in a record structure,
324 its name is changed to it overloaded name. Since names for
325 constructors and destructors can conflict, we place a leading
326 '$' for destructors.
327
328 CNAME is the name of the class we are grokking for.
329
330 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
331
332 FLAGS contains bits saying what's special about today's
333 arguments. DTOR_FLAG == DESTRUCTOR.
334
335 If FUNCTION is a destructor, then we must add the `auto-delete' field
336 as a second parameter. There is some hair associated with the fact
337 that we must "declare" this variable in the manner consistent with the
338 way the rest of the arguments were declared.
339
340 QUALS are the qualifiers for the this pointer. */
341
342 void
343 grokclassfn (tree ctype, tree function, enum overload_flags flags)
344 {
345 tree fn_name = DECL_NAME (function);
346
347 /* Even within an `extern "C"' block, members get C++ linkage. See
348 [dcl.link] for details. */
349 SET_DECL_LANGUAGE (function, lang_cplusplus);
350
351 if (fn_name == NULL_TREE)
352 {
353 error ("name missing for member function");
354 fn_name = get_identifier ("<anonymous>");
355 DECL_NAME (function) = fn_name;
356 }
357
358 DECL_CONTEXT (function) = ctype;
359
360 if (flags == DTOR_FLAG)
361 DECL_CXX_DESTRUCTOR_P (function) = 1;
362
363 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
364 maybe_retrofit_in_chrg (function);
365 }
366
367 /* Create an ARRAY_REF, checking for the user doing things backwards
368 along the way. DECLTYPE_P is for N3276, as in the parser. */
369
370 tree
371 grok_array_decl (location_t loc, tree array_expr, tree index_exp,
372 bool decltype_p)
373 {
374 tree type;
375 tree expr;
376 tree orig_array_expr = array_expr;
377 tree orig_index_exp = index_exp;
378 tree overload = NULL_TREE;
379
380 if (error_operand_p (array_expr) || error_operand_p (index_exp))
381 return error_mark_node;
382
383 if (processing_template_decl)
384 {
385 if (type_dependent_expression_p (array_expr)
386 || type_dependent_expression_p (index_exp))
387 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
388 NULL_TREE, NULL_TREE);
389 array_expr = build_non_dependent_expr (array_expr);
390 index_exp = build_non_dependent_expr (index_exp);
391 }
392
393 type = TREE_TYPE (array_expr);
394 gcc_assert (type);
395 type = non_reference (type);
396
397 /* If they have an `operator[]', use that. */
398 if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
399 {
400 tsubst_flags_t complain = tf_warning_or_error;
401 if (decltype_p)
402 complain |= tf_decltype;
403 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
404 index_exp, NULL_TREE, &overload, complain);
405 }
406 else
407 {
408 tree p1, p2, i1, i2;
409
410 /* Otherwise, create an ARRAY_REF for a pointer or array type.
411 It is a little-known fact that, if `a' is an array and `i' is
412 an int, you can write `i[a]', which means the same thing as
413 `a[i]'. */
414 if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
415 p1 = array_expr;
416 else
417 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
418
419 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
420 p2 = index_exp;
421 else
422 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
423
424 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
425 false);
426 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
427 false);
428
429 if ((p1 && i2) && (i1 && p2))
430 error ("ambiguous conversion for array subscript");
431
432 if (p1 && i2)
433 array_expr = p1, index_exp = i2;
434 else if (i1 && p2)
435 array_expr = p2, index_exp = i1;
436 else
437 {
438 error ("invalid types %<%T[%T]%> for array subscript",
439 type, TREE_TYPE (index_exp));
440 return error_mark_node;
441 }
442
443 if (array_expr == error_mark_node || index_exp == error_mark_node)
444 error ("ambiguous conversion for array subscript");
445
446 if (TYPE_PTR_P (TREE_TYPE (array_expr)))
447 array_expr = mark_rvalue_use (array_expr);
448 else
449 array_expr = mark_lvalue_use_nonread (array_expr);
450 index_exp = mark_rvalue_use (index_exp);
451 expr = build_array_ref (input_location, array_expr, index_exp);
452 }
453 if (processing_template_decl && expr != error_mark_node)
454 {
455 if (overload != NULL_TREE)
456 return (build_min_non_dep_op_overload
457 (ARRAY_REF, expr, overload, orig_array_expr, orig_index_exp));
458
459 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
460 NULL_TREE, NULL_TREE);
461 }
462 return expr;
463 }
464
465 /* Given the cast expression EXP, checking out its validity. Either return
466 an error_mark_node if there was an unavoidable error, return a cast to
467 void for trying to delete a pointer w/ the value 0, or return the
468 call to delete. If DOING_VEC is true, we handle things differently
469 for doing an array delete.
470 Implements ARM $5.3.4. This is called from the parser. */
471
472 tree
473 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
474 tsubst_flags_t complain)
475 {
476 tree t, type;
477
478 if (exp == error_mark_node)
479 return exp;
480
481 if (processing_template_decl)
482 {
483 t = build_min (DELETE_EXPR, void_type_node, exp, size);
484 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
485 DELETE_EXPR_USE_VEC (t) = doing_vec;
486 TREE_SIDE_EFFECTS (t) = 1;
487 return t;
488 }
489
490 /* An array can't have been allocated by new, so complain. */
491 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
492 warning (0, "deleting array %q#E", exp);
493
494 t = build_expr_type_conversion (WANT_POINTER, exp, true);
495
496 if (t == NULL_TREE || t == error_mark_node)
497 {
498 error ("type %q#T argument given to %<delete%>, expected pointer",
499 TREE_TYPE (exp));
500 return error_mark_node;
501 }
502
503 type = TREE_TYPE (t);
504
505 /* As of Valley Forge, you can delete a pointer to const. */
506
507 /* You can't delete functions. */
508 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
509 {
510 error ("cannot delete a function. Only pointer-to-objects are "
511 "valid arguments to %<delete%>");
512 return error_mark_node;
513 }
514
515 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
516 if (VOID_TYPE_P (TREE_TYPE (type)))
517 {
518 warning (OPT_Wdelete_incomplete, "deleting %qT is undefined", type);
519 doing_vec = 0;
520 }
521
522 /* Deleting a pointer with the value zero is valid and has no effect. */
523 if (integer_zerop (t))
524 return build1 (NOP_EXPR, void_type_node, t);
525
526 if (doing_vec)
527 return build_vec_delete (t, /*maxindex=*/NULL_TREE,
528 sfk_deleting_destructor,
529 use_global_delete, complain);
530 else
531 return build_delete (type, t, sfk_deleting_destructor,
532 LOOKUP_NORMAL, use_global_delete,
533 complain);
534 }
535
536 /* Report an error if the indicated template declaration is not the
537 sort of thing that should be a member template. */
538
539 void
540 check_member_template (tree tmpl)
541 {
542 tree decl;
543
544 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
545 decl = DECL_TEMPLATE_RESULT (tmpl);
546
547 if (TREE_CODE (decl) == FUNCTION_DECL
548 || DECL_ALIAS_TEMPLATE_P (tmpl)
549 || (TREE_CODE (decl) == TYPE_DECL
550 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
551 {
552 /* The parser rejects template declarations in local classes
553 (with the exception of generic lambdas). */
554 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
555 /* The parser rejects any use of virtual in a function template. */
556 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
557 && DECL_VIRTUAL_P (decl)));
558
559 /* The debug-information generating code doesn't know what to do
560 with member templates. */
561 DECL_IGNORED_P (tmpl) = 1;
562 }
563 else if (variable_template_p (tmpl))
564 /* OK */;
565 else
566 error ("template declaration of %q#D", decl);
567 }
568
569 /* Sanity check: report error if this function FUNCTION is not
570 really a member of the class (CTYPE) it is supposed to belong to.
571 TEMPLATE_PARMS is used to specify the template parameters of a member
572 template passed as FUNCTION_DECL. If the member template is passed as a
573 TEMPLATE_DECL, it can be NULL since the parameters can be extracted
574 from the declaration. If the function is not a function template, it
575 must be NULL.
576 It returns the original declaration for the function, NULL_TREE if
577 no declaration was found, error_mark_node if an error was emitted. */
578
579 tree
580 check_classfn (tree ctype, tree function, tree template_parms)
581 {
582 if (DECL_USE_TEMPLATE (function)
583 && !(TREE_CODE (function) == TEMPLATE_DECL
584 && DECL_TEMPLATE_SPECIALIZATION (function))
585 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
586 /* Since this is a specialization of a member template,
587 we're not going to find the declaration in the class.
588 For example, in:
589
590 struct S { template <typename T> void f(T); };
591 template <> void S::f(int);
592
593 we're not going to find `S::f(int)', but there's no
594 reason we should, either. We let our callers know we didn't
595 find the method, but we don't complain. */
596 return NULL_TREE;
597
598 /* Basic sanity check: for a template function, the template parameters
599 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
600 if (TREE_CODE (function) == TEMPLATE_DECL)
601 {
602 if (template_parms
603 && !comp_template_parms (template_parms,
604 DECL_TEMPLATE_PARMS (function)))
605 {
606 error ("template parameter lists provided don%'t match the "
607 "template parameters of %qD", function);
608 return error_mark_node;
609 }
610 template_parms = DECL_TEMPLATE_PARMS (function);
611 }
612
613 /* OK, is this a definition of a member template? */
614 bool is_template = (template_parms != NULL_TREE);
615
616 /* [temp.mem]
617
618 A destructor shall not be a member template. */
619 if (DECL_DESTRUCTOR_P (function) && is_template)
620 {
621 error ("destructor %qD declared as member template", function);
622 return error_mark_node;
623 }
624
625 /* We must enter the scope here, because conversion operators are
626 named by target type, and type equivalence relies on typenames
627 resolving within the scope of CTYPE. */
628 tree pushed_scope = push_scope (ctype);
629 tree matched = NULL_TREE;
630 tree fns = get_class_binding (ctype, DECL_NAME (function));
631
632 for (ovl_iterator iter (fns); !matched && iter; ++iter)
633 {
634 tree fndecl = *iter;
635
636 /* A member template definition only matches a member template
637 declaration. */
638 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
639 continue;
640
641 if (!DECL_DECLARES_FUNCTION_P (fndecl))
642 continue;
643
644 tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
645 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
646
647 /* We cannot simply call decls_match because this doesn't work
648 for static member functions that are pretending to be
649 methods, and because the name may have been changed by
650 asm("new_name"). */
651
652 /* Get rid of the this parameter on functions that become
653 static. */
654 if (DECL_STATIC_FUNCTION_P (fndecl)
655 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
656 p1 = TREE_CHAIN (p1);
657
658 /* ref-qualifier or absence of same must match. */
659 if (type_memfn_rqual (TREE_TYPE (function))
660 != type_memfn_rqual (TREE_TYPE (fndecl)))
661 continue;
662
663 // Include constraints in the match.
664 tree c1 = get_constraints (function);
665 tree c2 = get_constraints (fndecl);
666
667 /* While finding a match, same types and params are not enough
668 if the function is versioned. Also check version ("target")
669 attributes. */
670 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
671 TREE_TYPE (TREE_TYPE (fndecl)))
672 && compparms (p1, p2)
673 && !targetm.target_option.function_versions (function, fndecl)
674 && (!is_template
675 || comp_template_parms (template_parms,
676 DECL_TEMPLATE_PARMS (fndecl)))
677 && equivalent_constraints (c1, c2)
678 && (DECL_TEMPLATE_SPECIALIZATION (function)
679 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
680 && (!DECL_TEMPLATE_SPECIALIZATION (function)
681 || (DECL_TI_TEMPLATE (function) == DECL_TI_TEMPLATE (fndecl))))
682 matched = fndecl;
683 }
684
685 if (!matched)
686 {
687 if (!COMPLETE_TYPE_P (ctype))
688 cxx_incomplete_type_error (function, ctype);
689 else
690 {
691 if (DECL_CONV_FN_P (function))
692 fns = get_class_binding (ctype, conv_op_identifier);
693
694 error_at (DECL_SOURCE_LOCATION (function),
695 "no declaration matches %q#D", function);
696 if (fns)
697 print_candidates (fns);
698 else if (DECL_CONV_FN_P (function))
699 inform (DECL_SOURCE_LOCATION (function),
700 "no conversion operators declared");
701 else
702 inform (DECL_SOURCE_LOCATION (function),
703 "no functions named %qD", function);
704 inform (DECL_SOURCE_LOCATION (TYPE_NAME (ctype)),
705 "%#qT defined here", ctype);
706 }
707 matched = error_mark_node;
708 }
709
710 if (pushed_scope)
711 pop_scope (pushed_scope);
712
713 return matched;
714 }
715
716 /* DECL is a function with vague linkage. Remember it so that at the
717 end of the translation unit we can decide whether or not to emit
718 it. */
719
720 void
721 note_vague_linkage_fn (tree decl)
722 {
723 if (processing_template_decl)
724 return;
725
726 DECL_DEFER_OUTPUT (decl) = 1;
727 vec_safe_push (deferred_fns, decl);
728 }
729
730 /* As above, but for variable template instantiations. */
731
732 void
733 note_variable_template_instantiation (tree decl)
734 {
735 vec_safe_push (pending_statics, decl);
736 }
737
738 /* We have just processed the DECL, which is a static data member.
739 The other parameters are as for cp_finish_decl. */
740
741 void
742 finish_static_data_member_decl (tree decl,
743 tree init, bool init_const_expr_p,
744 tree asmspec_tree,
745 int flags)
746 {
747 if (DECL_TEMPLATE_INSTANTIATED (decl))
748 /* We already needed to instantiate this, so the processing in this
749 function is unnecessary/wrong. */
750 return;
751
752 DECL_CONTEXT (decl) = current_class_type;
753
754 /* We cannot call pushdecl here, because that would fill in the
755 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
756 the right thing, namely, to put this decl out straight away. */
757
758 if (! processing_template_decl)
759 vec_safe_push (pending_statics, decl);
760
761 if (LOCAL_CLASS_P (current_class_type)
762 /* We already complained about the template definition. */
763 && !DECL_TEMPLATE_INSTANTIATION (decl))
764 permerror (DECL_SOURCE_LOCATION (decl),
765 "local class %q#T shall not have static data member %q#D",
766 current_class_type, decl);
767 else
768 for (tree t = current_class_type; TYPE_P (t);
769 t = CP_TYPE_CONTEXT (t))
770 if (TYPE_UNNAMED_P (t))
771 {
772 auto_diagnostic_group d;
773 if (permerror (DECL_SOURCE_LOCATION (decl),
774 "static data member %qD in unnamed class", decl))
775 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
776 "unnamed class defined here");
777 break;
778 }
779
780 if (DECL_INLINE_VAR_P (decl) && !DECL_TEMPLATE_INSTANTIATION (decl))
781 /* An inline variable is immediately defined, so don't set DECL_IN_AGGR_P.
782 Except that if decl is a template instantiation, it isn't defined until
783 instantiate_decl. */;
784 else
785 DECL_IN_AGGR_P (decl) = 1;
786
787 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
788 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
789 SET_VAR_HAD_UNKNOWN_BOUND (decl);
790
791 if (init)
792 {
793 /* Similarly to start_decl_1, we want to complete the type in order
794 to do the right thing in cp_apply_type_quals_to_decl, possibly
795 clear TYPE_QUAL_CONST (c++/65579). */
796 tree type = TREE_TYPE (decl) = complete_type (TREE_TYPE (decl));
797 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
798 }
799
800 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
801 }
802
803 /* DECLARATOR and DECLSPECS correspond to a class member. The other
804 parameters are as for cp_finish_decl. Return the DECL for the
805 class member declared. */
806
807 tree
808 grokfield (const cp_declarator *declarator,
809 cp_decl_specifier_seq *declspecs,
810 tree init, bool init_const_expr_p,
811 tree asmspec_tree,
812 tree attrlist)
813 {
814 tree value;
815 const char *asmspec = 0;
816 int flags;
817
818 if (init
819 && TREE_CODE (init) == TREE_LIST
820 && TREE_VALUE (init) == error_mark_node
821 && TREE_CHAIN (init) == NULL_TREE)
822 init = NULL_TREE;
823
824 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
825 if (! value || value == error_mark_node)
826 /* friend or constructor went bad. */
827 return error_mark_node;
828 if (TREE_TYPE (value) == error_mark_node)
829 return value;
830
831 if (TREE_CODE (value) == TYPE_DECL && init)
832 {
833 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (value)),
834 "typedef %qD is initialized (use decltype instead)", value);
835 init = NULL_TREE;
836 }
837
838 /* Pass friendly classes back. */
839 if (value == void_type_node)
840 return value;
841
842 if (DECL_NAME (value)
843 && TREE_CODE (DECL_NAME (value)) == TEMPLATE_ID_EXPR)
844 {
845 error_at (declarator->id_loc,
846 "explicit template argument list not allowed");
847 return error_mark_node;
848 }
849
850 /* Stash away type declarations. */
851 if (TREE_CODE (value) == TYPE_DECL)
852 {
853 DECL_NONLOCAL (value) = 1;
854 DECL_CONTEXT (value) = current_class_type;
855
856 if (attrlist)
857 {
858 int attrflags = 0;
859
860 /* If this is a typedef that names the class for linkage purposes
861 (7.1.3p8), apply any attributes directly to the type. */
862 if (OVERLOAD_TYPE_P (TREE_TYPE (value))
863 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
864 attrflags = ATTR_FLAG_TYPE_IN_PLACE;
865
866 cplus_decl_attributes (&value, attrlist, attrflags);
867 }
868
869 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
870 && TREE_TYPE (value) != error_mark_node
871 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
872 set_underlying_type (value);
873
874 /* It's important that push_template_decl below follows
875 set_underlying_type above so that the created template
876 carries the properly set type of VALUE. */
877 if (processing_template_decl)
878 value = push_template_decl (value);
879
880 record_locally_defined_typedef (value);
881 return value;
882 }
883
884 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
885
886 if (!friendp && DECL_IN_AGGR_P (value))
887 {
888 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
889 return void_type_node;
890 }
891
892 if (asmspec_tree && asmspec_tree != error_mark_node)
893 asmspec = TREE_STRING_POINTER (asmspec_tree);
894
895 if (init)
896 {
897 if (TREE_CODE (value) == FUNCTION_DECL)
898 {
899 if (init == ridpointers[(int)RID_DELETE])
900 {
901 if (friendp && decl_defined_p (value))
902 {
903 error ("redefinition of %q#D", value);
904 inform (DECL_SOURCE_LOCATION (value),
905 "%q#D previously defined here", value);
906 }
907 else
908 {
909 DECL_DELETED_FN (value) = 1;
910 DECL_DECLARED_INLINE_P (value) = 1;
911 DECL_INITIAL (value) = error_mark_node;
912 }
913 }
914 else if (init == ridpointers[(int)RID_DEFAULT])
915 {
916 if (defaultable_fn_check (value))
917 {
918 DECL_DEFAULTED_FN (value) = 1;
919 DECL_INITIALIZED_IN_CLASS_P (value) = 1;
920 DECL_DECLARED_INLINE_P (value) = 1;
921 }
922 }
923 else if (TREE_CODE (init) == DEFAULT_ARG)
924 error ("invalid initializer for member function %qD", value);
925 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
926 {
927 if (integer_zerop (init))
928 DECL_PURE_VIRTUAL_P (value) = 1;
929 else if (error_operand_p (init))
930 ; /* An error has already been reported. */
931 else
932 error ("invalid initializer for member function %qD",
933 value);
934 }
935 else
936 {
937 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
938 location_t iloc
939 = cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (value));
940 if (friendp)
941 error_at (iloc, "initializer specified for friend "
942 "function %qD", value);
943 else
944 error_at (iloc, "initializer specified for static "
945 "member function %qD", value);
946 }
947 }
948 else if (TREE_CODE (value) == FIELD_DECL)
949 /* C++11 NSDMI, keep going. */;
950 else if (!VAR_P (value))
951 gcc_unreachable ();
952 }
953
954 /* Pass friend decls back. */
955 if ((TREE_CODE (value) == FUNCTION_DECL
956 || TREE_CODE (value) == TEMPLATE_DECL)
957 && DECL_CONTEXT (value) != current_class_type)
958 return value;
959
960 /* Need to set this before push_template_decl. */
961 if (VAR_P (value))
962 DECL_CONTEXT (value) = current_class_type;
963
964 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
965 {
966 value = push_template_decl (value);
967 if (error_operand_p (value))
968 return error_mark_node;
969 }
970
971 if (attrlist)
972 cplus_decl_attributes (&value, attrlist, 0);
973
974 if (init && DIRECT_LIST_INIT_P (init))
975 flags = LOOKUP_NORMAL;
976 else
977 flags = LOOKUP_IMPLICIT;
978
979 switch (TREE_CODE (value))
980 {
981 case VAR_DECL:
982 finish_static_data_member_decl (value, init, init_const_expr_p,
983 asmspec_tree, flags);
984 return value;
985
986 case FIELD_DECL:
987 if (asmspec)
988 error ("%<asm%> specifiers are not permitted on non-static data members");
989 if (DECL_INITIAL (value) == error_mark_node)
990 init = error_mark_node;
991 cp_finish_decl (value, init, /*init_const_expr_p=*/false,
992 NULL_TREE, flags);
993 DECL_IN_AGGR_P (value) = 1;
994 return value;
995
996 case FUNCTION_DECL:
997 if (asmspec)
998 set_user_assembler_name (value, asmspec);
999
1000 cp_finish_decl (value,
1001 /*init=*/NULL_TREE,
1002 /*init_const_expr_p=*/false,
1003 asmspec_tree, flags);
1004
1005 /* Pass friends back this way. */
1006 if (DECL_FRIEND_P (value))
1007 return void_type_node;
1008
1009 DECL_IN_AGGR_P (value) = 1;
1010 return value;
1011
1012 default:
1013 gcc_unreachable ();
1014 }
1015 return NULL_TREE;
1016 }
1017
1018 /* Like `grokfield', but for bitfields.
1019 WIDTH is the width of the bitfield, a constant expression.
1020 The other parameters are as for grokfield. */
1021
1022 tree
1023 grokbitfield (const cp_declarator *declarator,
1024 cp_decl_specifier_seq *declspecs, tree width, tree init,
1025 tree attrlist)
1026 {
1027 tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1028 init != NULL_TREE, &attrlist);
1029
1030 if (value == error_mark_node)
1031 return NULL_TREE; /* friends went bad. */
1032
1033 tree type = TREE_TYPE (value);
1034 if (type == error_mark_node)
1035 return value;
1036
1037 /* Pass friendly classes back. */
1038 if (VOID_TYPE_P (value))
1039 return void_type_node;
1040
1041 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type)
1042 && (INDIRECT_TYPE_P (type) || !dependent_type_p (type)))
1043 {
1044 error_at (DECL_SOURCE_LOCATION (value),
1045 "bit-field %qD with non-integral type %qT",
1046 value, type);
1047 return error_mark_node;
1048 }
1049
1050 if (TREE_CODE (value) == TYPE_DECL)
1051 {
1052 error_at (DECL_SOURCE_LOCATION (value),
1053 "cannot declare %qD to be a bit-field type", value);
1054 return NULL_TREE;
1055 }
1056
1057 /* Usually, finish_struct_1 catches bitfields with invalid types.
1058 But, in the case of bitfields with function type, we confuse
1059 ourselves into thinking they are member functions, so we must
1060 check here. */
1061 if (TREE_CODE (value) == FUNCTION_DECL)
1062 {
1063 error_at (DECL_SOURCE_LOCATION (value),
1064 "cannot declare bit-field %qD with function type", value);
1065 return NULL_TREE;
1066 }
1067
1068 if (TYPE_WARN_IF_NOT_ALIGN (type))
1069 {
1070 error_at (DECL_SOURCE_LOCATION (value), "cannot declare bit-field "
1071 "%qD with %<warn_if_not_aligned%> type", value);
1072 return NULL_TREE;
1073 }
1074
1075 if (DECL_IN_AGGR_P (value))
1076 {
1077 error ("%qD is already defined in the class %qT", value,
1078 DECL_CONTEXT (value));
1079 return void_type_node;
1080 }
1081
1082 if (TREE_STATIC (value))
1083 {
1084 error_at (DECL_SOURCE_LOCATION (value),
1085 "static member %qD cannot be a bit-field", value);
1086 return NULL_TREE;
1087 }
1088
1089 int flags = LOOKUP_IMPLICIT;
1090 if (init && DIRECT_LIST_INIT_P (init))
1091 flags = LOOKUP_NORMAL;
1092 cp_finish_decl (value, init, false, NULL_TREE, flags);
1093
1094 if (width != error_mark_node)
1095 {
1096 /* The width must be an integer type. */
1097 if (!type_dependent_expression_p (width)
1098 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1099 error ("width of bit-field %qD has non-integral type %qT", value,
1100 TREE_TYPE (width));
1101 else
1102 {
1103 /* Temporarily stash the width in DECL_BIT_FIELD_REPRESENTATIVE.
1104 check_bitfield_decl picks it from there later and sets DECL_SIZE
1105 accordingly. */
1106 DECL_BIT_FIELD_REPRESENTATIVE (value) = width;
1107 SET_DECL_C_BIT_FIELD (value);
1108 }
1109 }
1110
1111 DECL_IN_AGGR_P (value) = 1;
1112
1113 if (attrlist)
1114 cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1115
1116 return value;
1117 }
1118
1119 \f
1120 /* Returns true iff ATTR is an attribute which needs to be applied at
1121 instantiation time rather than template definition time. */
1122
1123 static bool
1124 is_late_template_attribute (tree attr, tree decl)
1125 {
1126 tree name = get_attribute_name (attr);
1127 tree args = TREE_VALUE (attr);
1128 const struct attribute_spec *spec = lookup_attribute_spec (name);
1129 tree arg;
1130
1131 if (!spec)
1132 /* Unknown attribute. */
1133 return false;
1134
1135 /* Attribute weak handling wants to write out assembly right away. */
1136 if (is_attribute_p ("weak", name))
1137 return true;
1138
1139 /* Attributes used and unused are applied directly to typedefs for the
1140 benefit of maybe_warn_unused_local_typedefs. */
1141 if (TREE_CODE (decl) == TYPE_DECL
1142 && (is_attribute_p ("unused", name)
1143 || is_attribute_p ("used", name)))
1144 return false;
1145
1146 /* Attribute tls_model wants to modify the symtab. */
1147 if (is_attribute_p ("tls_model", name))
1148 return true;
1149
1150 /* #pragma omp declare simd attribute needs to be always deferred. */
1151 if (flag_openmp
1152 && is_attribute_p ("omp declare simd", name))
1153 return true;
1154
1155 /* An attribute pack is clearly dependent. */
1156 if (args && PACK_EXPANSION_P (args))
1157 return true;
1158
1159 /* If any of the arguments are dependent expressions, we can't evaluate
1160 the attribute until instantiation time. */
1161 for (arg = args; arg; arg = TREE_CHAIN (arg))
1162 {
1163 tree t = TREE_VALUE (arg);
1164
1165 /* If the first attribute argument is an identifier, only consider
1166 second and following arguments. Attributes like mode, format,
1167 cleanup and several target specific attributes aren't late
1168 just because they have an IDENTIFIER_NODE as first argument. */
1169 if (arg == args && attribute_takes_identifier_p (name)
1170 && identifier_p (t))
1171 continue;
1172
1173 if (value_dependent_expression_p (t)
1174 || type_dependent_expression_p (t))
1175 return true;
1176 }
1177
1178 if (TREE_CODE (decl) == TYPE_DECL
1179 || TYPE_P (decl)
1180 || spec->type_required)
1181 {
1182 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1183
1184 /* We can't apply any attributes to a completely unknown type until
1185 instantiation time. */
1186 enum tree_code code = TREE_CODE (type);
1187 if (code == TEMPLATE_TYPE_PARM
1188 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1189 || code == TYPENAME_TYPE)
1190 return true;
1191 /* Also defer most attributes on dependent types. This is not
1192 necessary in all cases, but is the better default. */
1193 else if (dependent_type_p (type)
1194 /* But some attributes specifically apply to templates. */
1195 && !is_attribute_p ("abi_tag", name)
1196 && !is_attribute_p ("deprecated", name)
1197 && !is_attribute_p ("visibility", name))
1198 return true;
1199 else
1200 return false;
1201 }
1202 else
1203 return false;
1204 }
1205
1206 /* ATTR_P is a list of attributes. Remove any attributes which need to be
1207 applied at instantiation time and return them. If IS_DEPENDENT is true,
1208 the declaration itself is dependent, so all attributes should be applied
1209 at instantiation time. */
1210
1211 static tree
1212 splice_template_attributes (tree *attr_p, tree decl)
1213 {
1214 tree *p = attr_p;
1215 tree late_attrs = NULL_TREE;
1216 tree *q = &late_attrs;
1217
1218 if (!p)
1219 return NULL_TREE;
1220
1221 for (; *p; )
1222 {
1223 if (is_late_template_attribute (*p, decl))
1224 {
1225 ATTR_IS_DEPENDENT (*p) = 1;
1226 *q = *p;
1227 *p = TREE_CHAIN (*p);
1228 q = &TREE_CHAIN (*q);
1229 *q = NULL_TREE;
1230 }
1231 else
1232 p = &TREE_CHAIN (*p);
1233 }
1234
1235 return late_attrs;
1236 }
1237
1238 /* Remove any late attributes from the list in ATTR_P and attach them to
1239 DECL_P. */
1240
1241 static void
1242 save_template_attributes (tree *attr_p, tree *decl_p, int flags)
1243 {
1244 tree *q;
1245
1246 if (attr_p && *attr_p == error_mark_node)
1247 return;
1248
1249 tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1250 if (!late_attrs)
1251 return;
1252
1253 if (DECL_P (*decl_p))
1254 q = &DECL_ATTRIBUTES (*decl_p);
1255 else
1256 q = &TYPE_ATTRIBUTES (*decl_p);
1257
1258 tree old_attrs = *q;
1259
1260 /* Merge the late attributes at the beginning with the attribute
1261 list. */
1262 late_attrs = merge_attributes (late_attrs, *q);
1263 if (*q != late_attrs
1264 && !DECL_P (*decl_p)
1265 && !(flags & ATTR_FLAG_TYPE_IN_PLACE))
1266 {
1267 if (!dependent_type_p (*decl_p))
1268 *decl_p = cp_build_type_attribute_variant (*decl_p, late_attrs);
1269 else
1270 {
1271 *decl_p = build_variant_type_copy (*decl_p);
1272 TYPE_ATTRIBUTES (*decl_p) = late_attrs;
1273 }
1274 }
1275 else
1276 *q = late_attrs;
1277
1278 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1279 {
1280 /* We've added new attributes directly to the main variant, so
1281 now we need to update all of the other variants to include
1282 these new attributes. */
1283 tree variant;
1284 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1285 variant = TYPE_NEXT_VARIANT (variant))
1286 {
1287 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1288 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1289 }
1290 }
1291 }
1292
1293 /* True if ATTRS contains any dependent attributes that affect type
1294 identity. */
1295
1296 bool
1297 any_dependent_type_attributes_p (tree attrs)
1298 {
1299 for (tree a = attrs; a; a = TREE_CHAIN (a))
1300 if (ATTR_IS_DEPENDENT (a))
1301 {
1302 const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (a));
1303 if (as && as->affects_type_identity)
1304 return true;
1305 }
1306 return false;
1307 }
1308
1309 /* Return true iff ATTRS are acceptable attributes to be applied in-place
1310 to a typedef which gives a previously unnamed class or enum a name for
1311 linkage purposes. */
1312
1313 bool
1314 attributes_naming_typedef_ok (tree attrs)
1315 {
1316 for (; attrs; attrs = TREE_CHAIN (attrs))
1317 {
1318 tree name = get_attribute_name (attrs);
1319 if (is_attribute_p ("vector_size", name))
1320 return false;
1321 }
1322 return true;
1323 }
1324
1325 /* Like reconstruct_complex_type, but handle also template trees. */
1326
1327 tree
1328 cp_reconstruct_complex_type (tree type, tree bottom)
1329 {
1330 tree inner, outer;
1331
1332 if (TYPE_PTR_P (type))
1333 {
1334 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1335 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1336 TYPE_REF_CAN_ALIAS_ALL (type));
1337 }
1338 else if (TYPE_REF_P (type))
1339 {
1340 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1341 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1342 TYPE_REF_CAN_ALIAS_ALL (type));
1343 }
1344 else if (TREE_CODE (type) == ARRAY_TYPE)
1345 {
1346 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1347 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1348 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1349 element type qualification will be handled by the recursive
1350 cp_reconstruct_complex_type call and cp_build_qualified_type
1351 for ARRAY_TYPEs changes the element type. */
1352 return outer;
1353 }
1354 else if (TREE_CODE (type) == FUNCTION_TYPE)
1355 {
1356 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1357 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1358 outer = apply_memfn_quals (outer, type_memfn_quals (type));
1359 }
1360 else if (TREE_CODE (type) == METHOD_TYPE)
1361 {
1362 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1363 /* The build_method_type_directly() routine prepends 'this' to argument list,
1364 so we must compensate by getting rid of it. */
1365 outer
1366 = build_method_type_directly
1367 (class_of_this_parm (type), inner,
1368 TREE_CHAIN (TYPE_ARG_TYPES (type)));
1369 }
1370 else if (TREE_CODE (type) == OFFSET_TYPE)
1371 {
1372 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1373 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1374 }
1375 else
1376 return bottom;
1377
1378 if (TYPE_ATTRIBUTES (type))
1379 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1380 outer = cp_build_qualified_type (outer, cp_type_quals (type));
1381 outer = cxx_copy_lang_qualifiers (outer, type);
1382
1383 return outer;
1384 }
1385
1386 /* Replaces any constexpr expression that may be into the attributes
1387 arguments with their reduced value. */
1388
1389 void
1390 cp_check_const_attributes (tree attributes)
1391 {
1392 if (attributes == error_mark_node)
1393 return;
1394
1395 tree attr;
1396 for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1397 {
1398 tree arg;
1399 for (arg = TREE_VALUE (attr); arg && TREE_CODE (arg) == TREE_LIST;
1400 arg = TREE_CHAIN (arg))
1401 {
1402 tree expr = TREE_VALUE (arg);
1403 if (EXPR_P (expr))
1404 TREE_VALUE (arg) = fold_non_dependent_expr (expr);
1405 }
1406 }
1407 }
1408
1409 /* Return true if TYPE is an OpenMP mappable type. */
1410 bool
1411 cp_omp_mappable_type (tree type)
1412 {
1413 /* Mappable type has to be complete. */
1414 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1415 return false;
1416 /* Arrays have mappable type if the elements have mappable type. */
1417 while (TREE_CODE (type) == ARRAY_TYPE)
1418 type = TREE_TYPE (type);
1419 /* A mappable type cannot contain virtual members. */
1420 if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
1421 return false;
1422 /* All data members must be non-static. */
1423 if (CLASS_TYPE_P (type))
1424 {
1425 tree field;
1426 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1427 if (VAR_P (field))
1428 return false;
1429 /* All fields must have mappable types. */
1430 else if (TREE_CODE (field) == FIELD_DECL
1431 && !cp_omp_mappable_type (TREE_TYPE (field)))
1432 return false;
1433 }
1434 return true;
1435 }
1436
1437 /* Return the last pushed declaration for the symbol DECL or NULL
1438 when no such declaration exists. */
1439
1440 static tree
1441 find_last_decl (tree decl)
1442 {
1443 tree last_decl = NULL_TREE;
1444
1445 if (tree name = DECL_P (decl) ? DECL_NAME (decl) : NULL_TREE)
1446 {
1447 /* Look up the declaration in its scope. */
1448 tree pushed_scope = NULL_TREE;
1449 if (tree ctype = DECL_CONTEXT (decl))
1450 pushed_scope = push_scope (ctype);
1451
1452 last_decl = lookup_name (name);
1453
1454 if (pushed_scope)
1455 pop_scope (pushed_scope);
1456
1457 /* The declaration may be a member conversion operator
1458 or a bunch of overfloads (handle the latter below). */
1459 if (last_decl && BASELINK_P (last_decl))
1460 last_decl = BASELINK_FUNCTIONS (last_decl);
1461 }
1462
1463 if (!last_decl)
1464 return NULL_TREE;
1465
1466 if (DECL_P (last_decl) || TREE_CODE (last_decl) == OVERLOAD)
1467 {
1468 /* A set of overloads of the same function. */
1469 for (lkp_iterator iter (last_decl); iter; ++iter)
1470 {
1471 if (TREE_CODE (*iter) == OVERLOAD)
1472 continue;
1473
1474 if (decls_match (decl, *iter, /*record_decls=*/false))
1475 return *iter;
1476 }
1477 return NULL_TREE;
1478 }
1479
1480 return NULL_TREE;
1481 }
1482
1483 /* Like decl_attributes, but handle C++ complexity. */
1484
1485 void
1486 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1487 {
1488 if (*decl == NULL_TREE || *decl == void_type_node
1489 || *decl == error_mark_node)
1490 return;
1491
1492 /* Add implicit "omp declare target" attribute if requested. */
1493 if (scope_chain->omp_declare_target_attribute
1494 && ((VAR_P (*decl)
1495 && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
1496 || TREE_CODE (*decl) == FUNCTION_DECL))
1497 {
1498 if (VAR_P (*decl)
1499 && DECL_CLASS_SCOPE_P (*decl))
1500 error ("%q+D static data member inside of declare target directive",
1501 *decl);
1502 else if (VAR_P (*decl)
1503 && (processing_template_decl
1504 || !cp_omp_mappable_type (TREE_TYPE (*decl))))
1505 attributes = tree_cons (get_identifier ("omp declare target implicit"),
1506 NULL_TREE, attributes);
1507 else
1508 attributes = tree_cons (get_identifier ("omp declare target"),
1509 NULL_TREE, attributes);
1510 }
1511
1512 if (processing_template_decl)
1513 {
1514 if (check_for_bare_parameter_packs (attributes))
1515 return;
1516
1517 save_template_attributes (&attributes, decl, flags);
1518 }
1519
1520 cp_check_const_attributes (attributes);
1521
1522 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1523 decl = &DECL_TEMPLATE_RESULT (*decl);
1524
1525 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
1526 {
1527 attributes
1528 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
1529 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
1530 attributes, flags);
1531 }
1532 else
1533 {
1534 tree last_decl = find_last_decl (*decl);
1535 decl_attributes (decl, attributes, flags, last_decl);
1536 }
1537
1538 if (TREE_CODE (*decl) == TYPE_DECL)
1539 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1540
1541 /* Propagate deprecation out to the template. */
1542 if (TREE_DEPRECATED (*decl))
1543 if (tree ti = get_template_info (*decl))
1544 {
1545 tree tmpl = TI_TEMPLATE (ti);
1546 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
1547 : DECL_TEMPLATE_RESULT (tmpl));
1548 if (*decl == pattern)
1549 TREE_DEPRECATED (tmpl) = true;
1550 }
1551 }
1552 \f
1553 /* Walks through the namespace- or function-scope anonymous union
1554 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1555 Returns one of the fields for use in the mangled name. */
1556
1557 static tree
1558 build_anon_union_vars (tree type, tree object)
1559 {
1560 tree main_decl = NULL_TREE;
1561 tree field;
1562
1563 /* Rather than write the code to handle the non-union case,
1564 just give an error. */
1565 if (TREE_CODE (type) != UNION_TYPE)
1566 {
1567 error ("anonymous struct not inside named type");
1568 return error_mark_node;
1569 }
1570
1571 for (field = TYPE_FIELDS (type);
1572 field != NULL_TREE;
1573 field = DECL_CHAIN (field))
1574 {
1575 tree decl;
1576 tree ref;
1577
1578 if (DECL_ARTIFICIAL (field))
1579 continue;
1580 if (TREE_CODE (field) != FIELD_DECL)
1581 {
1582 permerror (DECL_SOURCE_LOCATION (field),
1583 "%q#D invalid; an anonymous union can only "
1584 "have non-static data members", field);
1585 continue;
1586 }
1587
1588 if (TREE_PRIVATE (field))
1589 permerror (DECL_SOURCE_LOCATION (field),
1590 "private member %q#D in anonymous union", field);
1591 else if (TREE_PROTECTED (field))
1592 permerror (DECL_SOURCE_LOCATION (field),
1593 "protected member %q#D in anonymous union", field);
1594
1595 if (processing_template_decl)
1596 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
1597 DECL_NAME (field), NULL_TREE);
1598 else
1599 ref = build_class_member_access_expr (object, field, NULL_TREE,
1600 false, tf_warning_or_error);
1601
1602 if (DECL_NAME (field))
1603 {
1604 tree base;
1605
1606 decl = build_decl (input_location,
1607 VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1608 DECL_ANON_UNION_VAR_P (decl) = 1;
1609 DECL_ARTIFICIAL (decl) = 1;
1610
1611 base = get_base_address (object);
1612 TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1613 TREE_STATIC (decl) = TREE_STATIC (base);
1614 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1615
1616 SET_DECL_VALUE_EXPR (decl, ref);
1617 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1618
1619 decl = pushdecl (decl);
1620 }
1621 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1622 decl = build_anon_union_vars (TREE_TYPE (field), ref);
1623 else
1624 decl = 0;
1625
1626 if (main_decl == NULL_TREE)
1627 main_decl = decl;
1628 }
1629
1630 return main_decl;
1631 }
1632
1633 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1634 anonymous union, then all members must be laid out together. PUBLIC_P
1635 is nonzero if this union is not declared static. */
1636
1637 void
1638 finish_anon_union (tree anon_union_decl)
1639 {
1640 tree type;
1641 tree main_decl;
1642 bool public_p;
1643
1644 if (anon_union_decl == error_mark_node)
1645 return;
1646
1647 type = TREE_TYPE (anon_union_decl);
1648 public_p = TREE_PUBLIC (anon_union_decl);
1649
1650 /* The VAR_DECL's context is the same as the TYPE's context. */
1651 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1652
1653 if (TYPE_FIELDS (type) == NULL_TREE)
1654 return;
1655
1656 if (public_p)
1657 {
1658 error ("namespace-scope anonymous aggregates must be static");
1659 return;
1660 }
1661
1662 main_decl = build_anon_union_vars (type, anon_union_decl);
1663 if (main_decl == error_mark_node)
1664 return;
1665 if (main_decl == NULL_TREE)
1666 {
1667 pedwarn (input_location, 0, "anonymous union with no members");
1668 return;
1669 }
1670
1671 if (!processing_template_decl)
1672 {
1673 /* Use main_decl to set the mangled name. */
1674 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1675 maybe_commonize_var (anon_union_decl);
1676 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1677 {
1678 if (DECL_DISCRIMINATOR_P (anon_union_decl))
1679 determine_local_discriminator (anon_union_decl);
1680 mangle_decl (anon_union_decl);
1681 }
1682 DECL_NAME (anon_union_decl) = NULL_TREE;
1683 }
1684
1685 pushdecl (anon_union_decl);
1686 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
1687 }
1688 \f
1689 /* Auxiliary functions to make type signatures for
1690 `operator new' and `operator delete' correspond to
1691 what compiler will be expecting. */
1692
1693 tree
1694 coerce_new_type (tree type, location_t loc)
1695 {
1696 int e = 0;
1697 tree args = TYPE_ARG_TYPES (type);
1698
1699 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1700
1701 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1702 {
1703 e = 1;
1704 error_at (loc, "%<operator new%> must return type %qT",
1705 ptr_type_node);
1706 }
1707
1708 if (args && args != void_list_node)
1709 {
1710 if (TREE_PURPOSE (args))
1711 {
1712 /* [basic.stc.dynamic.allocation]
1713
1714 The first parameter shall not have an associated default
1715 argument. */
1716 error_at (loc, "the first parameter of %<operator new%> cannot "
1717 "have a default argument");
1718 /* Throw away the default argument. */
1719 TREE_PURPOSE (args) = NULL_TREE;
1720 }
1721
1722 if (!same_type_p (TREE_VALUE (args), size_type_node))
1723 {
1724 e = 2;
1725 args = TREE_CHAIN (args);
1726 }
1727 }
1728 else
1729 e = 2;
1730
1731 if (e == 2)
1732 permerror (loc, "%<operator new%> takes type %<size_t%> (%qT) "
1733 "as first parameter", size_type_node);
1734
1735 switch (e)
1736 {
1737 case 2:
1738 args = tree_cons (NULL_TREE, size_type_node, args);
1739 /* Fall through. */
1740 case 1:
1741 type = (cxx_copy_lang_qualifiers
1742 (build_function_type (ptr_type_node, args),
1743 type));
1744 /* Fall through. */
1745 default:;
1746 }
1747 return type;
1748 }
1749
1750 void
1751 coerce_delete_type (tree decl, location_t loc)
1752 {
1753 int e = 0;
1754 tree type = TREE_TYPE (decl);
1755 tree args = TYPE_ARG_TYPES (type);
1756
1757 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1758
1759 if (!same_type_p (TREE_TYPE (type), void_type_node))
1760 {
1761 e = 1;
1762 error_at (loc, "%<operator delete%> must return type %qT",
1763 void_type_node);
1764 }
1765
1766 tree ptrtype = ptr_type_node;
1767 if (destroying_delete_p (decl))
1768 {
1769 if (DECL_CLASS_SCOPE_P (decl))
1770 /* If the function is a destroying operator delete declared in class
1771 type C, the type of its first parameter shall be C*. */
1772 ptrtype = build_pointer_type (DECL_CONTEXT (decl));
1773 else
1774 /* A destroying operator delete shall be a class member function named
1775 operator delete. */
1776 error_at (loc, "destroying operator delete must be a member function");
1777 const ovl_op_info_t *op = IDENTIFIER_OVL_OP_INFO (DECL_NAME (decl));
1778 if (op->flags & OVL_OP_FLAG_VEC)
1779 error_at (loc, "operator delete[] cannot be a destroying delete");
1780 if (!usual_deallocation_fn_p (decl))
1781 error_at (loc, "destroying operator delete must be a usual "
1782 "deallocation function");
1783 }
1784
1785 if (!args || args == void_list_node
1786 || !same_type_p (TREE_VALUE (args), ptrtype))
1787 {
1788 e = 2;
1789 if (args && args != void_list_node)
1790 args = TREE_CHAIN (args);
1791 error_at (loc, "%<operator delete%> takes type %qT as first parameter",
1792 ptrtype);
1793 }
1794 switch (e)
1795 {
1796 case 2:
1797 args = tree_cons (NULL_TREE, ptrtype, args);
1798 /* Fall through. */
1799 case 1:
1800 type = (cxx_copy_lang_qualifiers
1801 (build_function_type (void_type_node, args),
1802 type));
1803 /* Fall through. */
1804 default:;
1805 }
1806
1807 TREE_TYPE (decl) = type;
1808 }
1809 \f
1810 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1811 and mark them as needed. */
1812
1813 static void
1814 mark_vtable_entries (tree decl)
1815 {
1816 tree fnaddr;
1817 unsigned HOST_WIDE_INT idx;
1818
1819 /* It's OK for the vtable to refer to deprecated virtual functions. */
1820 warning_sentinel w(warn_deprecated_decl);
1821
1822 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1823 idx, fnaddr)
1824 {
1825 tree fn;
1826
1827 STRIP_NOPS (fnaddr);
1828
1829 if (TREE_CODE (fnaddr) != ADDR_EXPR
1830 && TREE_CODE (fnaddr) != FDESC_EXPR)
1831 /* This entry is an offset: a virtual base class offset, a
1832 virtual call offset, an RTTI offset, etc. */
1833 continue;
1834
1835 fn = TREE_OPERAND (fnaddr, 0);
1836 TREE_ADDRESSABLE (fn) = 1;
1837 /* When we don't have vcall offsets, we output thunks whenever
1838 we output the vtables that contain them. With vcall offsets,
1839 we know all the thunks we'll need when we emit a virtual
1840 function, so we emit the thunks there instead. */
1841 if (DECL_THUNK_P (fn))
1842 use_thunk (fn, /*emit_p=*/0);
1843 /* Set the location, as marking the function could cause
1844 instantiation. We do not need to preserve the incoming
1845 location, as we're called from c_parse_final_cleanups, which
1846 takes care of that. */
1847 input_location = DECL_SOURCE_LOCATION (fn);
1848 mark_used (fn);
1849 }
1850 }
1851
1852 /* Adjust the TLS model on variable DECL if need be, typically after
1853 the linkage of DECL has been modified. */
1854
1855 static void
1856 adjust_var_decl_tls_model (tree decl)
1857 {
1858 if (CP_DECL_THREAD_LOCAL_P (decl)
1859 && !lookup_attribute ("tls_model", DECL_ATTRIBUTES (decl)))
1860 set_decl_tls_model (decl, decl_default_tls_model (decl));
1861 }
1862
1863 /* Set DECL up to have the closest approximation of "initialized common"
1864 linkage available. */
1865
1866 void
1867 comdat_linkage (tree decl)
1868 {
1869 if (flag_weak)
1870 make_decl_one_only (decl, cxx_comdat_group (decl));
1871 else if (TREE_CODE (decl) == FUNCTION_DECL
1872 || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
1873 /* We can just emit function and compiler-generated variables
1874 statically; having multiple copies is (for the most part) only
1875 a waste of space.
1876
1877 There are two correctness issues, however: the address of a
1878 template instantiation with external linkage should be the
1879 same, independent of what translation unit asks for the
1880 address, and this will not hold when we emit multiple copies of
1881 the function. However, there's little else we can do.
1882
1883 Also, by default, the typeinfo implementation assumes that
1884 there will be only one copy of the string used as the name for
1885 each type. Therefore, if weak symbols are unavailable, the
1886 run-time library should perform a more conservative check; it
1887 should perform a string comparison, rather than an address
1888 comparison. */
1889 TREE_PUBLIC (decl) = 0;
1890 else
1891 {
1892 /* Static data member template instantiations, however, cannot
1893 have multiple copies. */
1894 if (DECL_INITIAL (decl) == 0
1895 || DECL_INITIAL (decl) == error_mark_node)
1896 DECL_COMMON (decl) = 1;
1897 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1898 {
1899 DECL_COMMON (decl) = 1;
1900 DECL_INITIAL (decl) = error_mark_node;
1901 }
1902 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1903 {
1904 /* We can't do anything useful; leave vars for explicit
1905 instantiation. */
1906 DECL_EXTERNAL (decl) = 1;
1907 DECL_NOT_REALLY_EXTERN (decl) = 0;
1908 }
1909 }
1910
1911 if (TREE_PUBLIC (decl))
1912 DECL_COMDAT (decl) = 1;
1913
1914 if (VAR_P (decl))
1915 adjust_var_decl_tls_model (decl);
1916 }
1917
1918 /* For win32 we also want to put explicit instantiations in
1919 linkonce sections, so that they will be merged with implicit
1920 instantiations; otherwise we get duplicate symbol errors.
1921 For Darwin we do not want explicit instantiations to be
1922 linkonce. */
1923
1924 void
1925 maybe_make_one_only (tree decl)
1926 {
1927 /* We used to say that this was not necessary on targets that support weak
1928 symbols, because the implicit instantiations will defer to the explicit
1929 one. However, that's not actually the case in SVR4; a strong definition
1930 after a weak one is an error. Also, not making explicit
1931 instantiations one_only means that we can end up with two copies of
1932 some template instantiations. */
1933 if (! flag_weak)
1934 return;
1935
1936 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1937 we can get away with not emitting them if they aren't used. We need
1938 to for variables so that cp_finish_decl will update their linkage,
1939 because their DECL_INITIAL may not have been set properly yet. */
1940
1941 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1942 || (! DECL_EXPLICIT_INSTANTIATION (decl)
1943 && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1944 {
1945 make_decl_one_only (decl, cxx_comdat_group (decl));
1946
1947 if (VAR_P (decl))
1948 {
1949 varpool_node *node = varpool_node::get_create (decl);
1950 DECL_COMDAT (decl) = 1;
1951 /* Mark it needed so we don't forget to emit it. */
1952 node->forced_by_abi = true;
1953 TREE_USED (decl) = 1;
1954
1955 adjust_var_decl_tls_model (decl);
1956 }
1957 }
1958 }
1959
1960 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1961 This predicate will give the right answer during parsing of the
1962 function, which other tests may not. */
1963
1964 bool
1965 vague_linkage_p (tree decl)
1966 {
1967 if (!TREE_PUBLIC (decl))
1968 {
1969 /* maybe_thunk_body clears TREE_PUBLIC and DECL_ABSTRACT_P on the
1970 maybe-in-charge 'tor variants; in that case we need to check one of
1971 the "clones" for the real linkage. But only in that case; before
1972 maybe_clone_body we haven't yet copied the linkage to the clones. */
1973 if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl)
1974 && !DECL_ABSTRACT_P (decl)
1975 && DECL_CHAIN (decl)
1976 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)))
1977 return vague_linkage_p (DECL_CHAIN (decl));
1978
1979 gcc_checking_assert (!DECL_COMDAT (decl));
1980 return false;
1981 }
1982 /* Unfortunately, import_export_decl has not always been called
1983 before the function is processed, so we cannot simply check
1984 DECL_COMDAT. */
1985 if (DECL_COMDAT (decl)
1986 || (TREE_CODE (decl) == FUNCTION_DECL
1987 && DECL_DECLARED_INLINE_P (decl))
1988 || (DECL_LANG_SPECIFIC (decl)
1989 && DECL_TEMPLATE_INSTANTIATION (decl))
1990 || (VAR_P (decl) && DECL_INLINE_VAR_P (decl)))
1991 return true;
1992 else if (DECL_FUNCTION_SCOPE_P (decl))
1993 /* A local static in an inline effectively has vague linkage. */
1994 return (TREE_STATIC (decl)
1995 && vague_linkage_p (DECL_CONTEXT (decl)));
1996 else
1997 return false;
1998 }
1999
2000 /* Determine whether or not we want to specifically import or export CTYPE,
2001 using various heuristics. */
2002
2003 static void
2004 import_export_class (tree ctype)
2005 {
2006 /* -1 for imported, 1 for exported. */
2007 int import_export = 0;
2008
2009 /* It only makes sense to call this function at EOF. The reason is
2010 that this function looks at whether or not the first non-inline
2011 non-abstract virtual member function has been defined in this
2012 translation unit. But, we can't possibly know that until we've
2013 seen the entire translation unit. */
2014 gcc_assert (at_eof);
2015
2016 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2017 return;
2018
2019 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
2020 we will have CLASSTYPE_INTERFACE_ONLY set but not
2021 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
2022 heuristic because someone will supply a #pragma implementation
2023 elsewhere, and deducing it here would produce a conflict. */
2024 if (CLASSTYPE_INTERFACE_ONLY (ctype))
2025 return;
2026
2027 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
2028 import_export = -1;
2029 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
2030 import_export = 1;
2031 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
2032 && !flag_implicit_templates)
2033 /* For a template class, without -fimplicit-templates, check the
2034 repository. If the virtual table is assigned to this
2035 translation unit, then export the class; otherwise, import
2036 it. */
2037 import_export = repo_export_class_p (ctype) ? 1 : -1;
2038 else if (TYPE_POLYMORPHIC_P (ctype))
2039 {
2040 /* The ABI specifies that the virtual table and associated
2041 information are emitted with the key method, if any. */
2042 tree method = CLASSTYPE_KEY_METHOD (ctype);
2043 /* If weak symbol support is not available, then we must be
2044 careful not to emit the vtable when the key function is
2045 inline. An inline function can be defined in multiple
2046 translation units. If we were to emit the vtable in each
2047 translation unit containing a definition, we would get
2048 multiple definition errors at link-time. */
2049 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
2050 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
2051 }
2052
2053 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
2054 a definition anywhere else. */
2055 if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
2056 import_export = 0;
2057
2058 /* Allow back ends the chance to overrule the decision. */
2059 if (targetm.cxx.import_export_class)
2060 import_export = targetm.cxx.import_export_class (ctype, import_export);
2061
2062 if (import_export)
2063 {
2064 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
2065 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
2066 }
2067 }
2068
2069 /* Return true if VAR has already been provided to the back end; in that
2070 case VAR should not be modified further by the front end. */
2071 static bool
2072 var_finalized_p (tree var)
2073 {
2074 return varpool_node::get_create (var)->definition;
2075 }
2076
2077 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
2078 must be emitted in this translation unit. Mark it as such. */
2079
2080 void
2081 mark_needed (tree decl)
2082 {
2083 TREE_USED (decl) = 1;
2084 if (TREE_CODE (decl) == FUNCTION_DECL)
2085 {
2086 /* Extern inline functions don't become needed when referenced.
2087 If we know a method will be emitted in other TU and no new
2088 functions can be marked reachable, just use the external
2089 definition. */
2090 struct cgraph_node *node = cgraph_node::get_create (decl);
2091 node->forced_by_abi = true;
2092
2093 /* #pragma interface and -frepo code can call mark_needed for
2094 maybe-in-charge 'tors; mark the clones as well. */
2095 tree clone;
2096 FOR_EACH_CLONE (clone, decl)
2097 mark_needed (clone);
2098 }
2099 else if (VAR_P (decl))
2100 {
2101 varpool_node *node = varpool_node::get_create (decl);
2102 /* C++ frontend use mark_decl_references to force COMDAT variables
2103 to be output that might appear dead otherwise. */
2104 node->forced_by_abi = true;
2105 }
2106 }
2107
2108 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
2109 returns true if a definition of this entity should be provided in
2110 this object file. Callers use this function to determine whether
2111 or not to let the back end know that a definition of DECL is
2112 available in this translation unit. */
2113
2114 bool
2115 decl_needed_p (tree decl)
2116 {
2117 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2118 /* This function should only be called at the end of the translation
2119 unit. We cannot be sure of whether or not something will be
2120 COMDAT until that point. */
2121 gcc_assert (at_eof);
2122
2123 /* All entities with external linkage that are not COMDAT/EXTERN should be
2124 emitted; they may be referred to from other object files. */
2125 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
2126 return true;
2127 /* Functions marked "dllexport" must be emitted so that they are
2128 visible to other DLLs. */
2129 if (flag_keep_inline_dllexport
2130 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
2131 return true;
2132
2133 /* When not optimizing, do not bother to produce definitions for extern
2134 symbols. */
2135 if (DECL_REALLY_EXTERN (decl)
2136 && ((TREE_CODE (decl) != FUNCTION_DECL
2137 && !optimize)
2138 || (TREE_CODE (decl) == FUNCTION_DECL
2139 && !opt_for_fn (decl, optimize)))
2140 && !lookup_attribute ("always_inline", decl))
2141 return false;
2142
2143 /* If this entity was used, let the back end see it; it will decide
2144 whether or not to emit it into the object file. */
2145 if (TREE_USED (decl))
2146 return true;
2147
2148 /* Virtual functions might be needed for devirtualization. */
2149 if (flag_devirtualize
2150 && TREE_CODE (decl) == FUNCTION_DECL
2151 && DECL_VIRTUAL_P (decl))
2152 return true;
2153
2154 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
2155 reference to DECL might cause it to be emitted later. */
2156 return false;
2157 }
2158
2159 /* If necessary, write out the vtables for the dynamic class CTYPE.
2160 Returns true if any vtables were emitted. */
2161
2162 static bool
2163 maybe_emit_vtables (tree ctype)
2164 {
2165 tree vtbl;
2166 tree primary_vtbl;
2167 int needed = 0;
2168 varpool_node *current = NULL, *last = NULL;
2169
2170 /* If the vtables for this class have already been emitted there is
2171 nothing more to do. */
2172 primary_vtbl = CLASSTYPE_VTABLES (ctype);
2173 if (var_finalized_p (primary_vtbl))
2174 return false;
2175 /* Ignore dummy vtables made by get_vtable_decl. */
2176 if (TREE_TYPE (primary_vtbl) == void_type_node)
2177 return false;
2178
2179 /* On some targets, we cannot determine the key method until the end
2180 of the translation unit -- which is when this function is
2181 called. */
2182 if (!targetm.cxx.key_method_may_be_inline ())
2183 determine_key_method (ctype);
2184
2185 /* See if any of the vtables are needed. */
2186 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2187 {
2188 import_export_decl (vtbl);
2189 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
2190 needed = 1;
2191 }
2192 if (!needed)
2193 {
2194 /* If the references to this class' vtables are optimized away,
2195 still emit the appropriate debugging information. See
2196 dfs_debug_mark. */
2197 if (DECL_COMDAT (primary_vtbl)
2198 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2199 note_debug_info_needed (ctype);
2200 return false;
2201 }
2202
2203 /* The ABI requires that we emit all of the vtables if we emit any
2204 of them. */
2205 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
2206 {
2207 /* Mark entities references from the virtual table as used. */
2208 mark_vtable_entries (vtbl);
2209
2210 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
2211 {
2212 vec<tree, va_gc> *cleanups = NULL;
2213 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
2214 LOOKUP_NORMAL);
2215
2216 /* It had better be all done at compile-time. */
2217 gcc_assert (!expr && !cleanups);
2218 }
2219
2220 /* Write it out. */
2221 DECL_EXTERNAL (vtbl) = 0;
2222 rest_of_decl_compilation (vtbl, 1, 1);
2223
2224 /* Because we're only doing syntax-checking, we'll never end up
2225 actually marking the variable as written. */
2226 if (flag_syntax_only)
2227 TREE_ASM_WRITTEN (vtbl) = 1;
2228 else if (DECL_ONE_ONLY (vtbl))
2229 {
2230 current = varpool_node::get_create (vtbl);
2231 if (last)
2232 current->add_to_same_comdat_group (last);
2233 last = current;
2234 }
2235 }
2236
2237 /* For abstract classes, the destructor has been removed from the
2238 vtable (in class.c's build_vtbl_initializer). For a compiler-
2239 generated destructor, it hence might not have been generated in
2240 this translation unit - and with '#pragma interface' it might
2241 never get generated. */
2242 if (CLASSTYPE_PURE_VIRTUALS (ctype)
2243 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype)
2244 && !CLASSTYPE_LAZY_DESTRUCTOR (ctype)
2245 && DECL_DEFAULTED_IN_CLASS_P (CLASSTYPE_DESTRUCTOR (ctype)))
2246 note_vague_linkage_fn (CLASSTYPE_DESTRUCTOR (ctype));
2247
2248 /* Since we're writing out the vtable here, also write the debug
2249 info. */
2250 note_debug_info_needed (ctype);
2251
2252 return true;
2253 }
2254
2255 /* A special return value from type_visibility meaning internal
2256 linkage. */
2257
2258 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
2259
2260 static int expr_visibility (tree);
2261 static int type_visibility (tree);
2262
2263 /* walk_tree helper function for type_visibility. */
2264
2265 static tree
2266 min_vis_r (tree *tp, int *walk_subtrees, void *data)
2267 {
2268 int *vis_p = (int *)data;
2269 if (! TYPE_P (*tp))
2270 {
2271 *walk_subtrees = 0;
2272 }
2273 else if (OVERLOAD_TYPE_P (*tp)
2274 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
2275 {
2276 *vis_p = VISIBILITY_ANON;
2277 return *tp;
2278 }
2279 else if (CLASS_TYPE_P (*tp)
2280 && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
2281 *vis_p = CLASSTYPE_VISIBILITY (*tp);
2282 else if (TREE_CODE (*tp) == ARRAY_TYPE
2283 && uses_template_parms (TYPE_DOMAIN (*tp)))
2284 {
2285 int evis = expr_visibility (TYPE_MAX_VALUE (TYPE_DOMAIN (*tp)));
2286 if (evis > *vis_p)
2287 *vis_p = evis;
2288 }
2289 return NULL;
2290 }
2291
2292 /* walk_tree helper function for expr_visibility. */
2293
2294 static tree
2295 min_vis_expr_r (tree *tp, int */*walk_subtrees*/, void *data)
2296 {
2297 int *vis_p = (int *)data;
2298 int tpvis = VISIBILITY_DEFAULT;
2299
2300 switch (TREE_CODE (*tp))
2301 {
2302 case CAST_EXPR:
2303 case IMPLICIT_CONV_EXPR:
2304 case STATIC_CAST_EXPR:
2305 case REINTERPRET_CAST_EXPR:
2306 case CONST_CAST_EXPR:
2307 case DYNAMIC_CAST_EXPR:
2308 case NEW_EXPR:
2309 case CONSTRUCTOR:
2310 case LAMBDA_EXPR:
2311 tpvis = type_visibility (TREE_TYPE (*tp));
2312 break;
2313
2314 case VAR_DECL:
2315 case FUNCTION_DECL:
2316 if (! TREE_PUBLIC (*tp))
2317 tpvis = VISIBILITY_ANON;
2318 else
2319 tpvis = DECL_VISIBILITY (*tp);
2320 break;
2321
2322 default:
2323 break;
2324 }
2325
2326 if (tpvis > *vis_p)
2327 *vis_p = tpvis;
2328
2329 return NULL_TREE;
2330 }
2331
2332 /* Returns the visibility of TYPE, which is the minimum visibility of its
2333 component types. */
2334
2335 static int
2336 type_visibility (tree type)
2337 {
2338 int vis = VISIBILITY_DEFAULT;
2339 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
2340 return vis;
2341 }
2342
2343 /* Returns the visibility of an expression EXPR that appears in the signature
2344 of a function template, which is the minimum visibility of names that appear
2345 in its mangling. */
2346
2347 static int
2348 expr_visibility (tree expr)
2349 {
2350 int vis = VISIBILITY_DEFAULT;
2351 cp_walk_tree_without_duplicates (&expr, min_vis_expr_r, &vis);
2352 return vis;
2353 }
2354
2355 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
2356 specified (or if VISIBILITY is static). If TMPL is true, this
2357 constraint is for a template argument, and takes precedence
2358 over explicitly-specified visibility on the template. */
2359
2360 static void
2361 constrain_visibility (tree decl, int visibility, bool tmpl)
2362 {
2363 if (visibility == VISIBILITY_ANON)
2364 {
2365 /* extern "C" declarations aren't affected by the anonymous
2366 namespace. */
2367 if (!DECL_EXTERN_C_P (decl))
2368 {
2369 TREE_PUBLIC (decl) = 0;
2370 DECL_WEAK (decl) = 0;
2371 DECL_COMMON (decl) = 0;
2372 DECL_COMDAT (decl) = false;
2373 if (VAR_OR_FUNCTION_DECL_P (decl))
2374 {
2375 struct symtab_node *snode = symtab_node::get (decl);
2376
2377 if (snode)
2378 snode->set_comdat_group (NULL);
2379 }
2380 DECL_INTERFACE_KNOWN (decl) = 1;
2381 if (DECL_LANG_SPECIFIC (decl))
2382 DECL_NOT_REALLY_EXTERN (decl) = 1;
2383 }
2384 }
2385 else if (visibility > DECL_VISIBILITY (decl)
2386 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
2387 {
2388 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
2389 /* This visibility was not specified. */
2390 DECL_VISIBILITY_SPECIFIED (decl) = false;
2391 }
2392 }
2393
2394 /* Constrain the visibility of DECL based on the visibility of its template
2395 arguments. */
2396
2397 static void
2398 constrain_visibility_for_template (tree decl, tree targs)
2399 {
2400 /* If this is a template instantiation, check the innermost
2401 template args for visibility constraints. The outer template
2402 args are covered by the class check. */
2403 tree args = INNERMOST_TEMPLATE_ARGS (targs);
2404 int i;
2405 for (i = TREE_VEC_LENGTH (args); i > 0; --i)
2406 {
2407 int vis = 0;
2408
2409 tree arg = TREE_VEC_ELT (args, i-1);
2410 if (TYPE_P (arg))
2411 vis = type_visibility (arg);
2412 else
2413 vis = expr_visibility (arg);
2414 if (vis)
2415 constrain_visibility (decl, vis, true);
2416 }
2417 }
2418
2419 /* Like c_determine_visibility, but with additional C++-specific
2420 behavior.
2421
2422 Function-scope entities can rely on the function's visibility because
2423 it is set in start_preparsed_function.
2424
2425 Class-scope entities cannot rely on the class's visibility until the end
2426 of the enclosing class definition.
2427
2428 Note that because namespaces have multiple independent definitions,
2429 namespace visibility is handled elsewhere using the #pragma visibility
2430 machinery rather than by decorating the namespace declaration.
2431
2432 The goal is for constraints from the type to give a diagnostic, and
2433 other constraints to be applied silently. */
2434
2435 void
2436 determine_visibility (tree decl)
2437 {
2438 /* Remember that all decls get VISIBILITY_DEFAULT when built. */
2439
2440 /* Only relevant for names with external linkage. */
2441 if (!TREE_PUBLIC (decl))
2442 return;
2443
2444 /* Cloned constructors and destructors get the same visibility as
2445 the underlying function. That should be set up in
2446 maybe_clone_body. */
2447 gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2448
2449 bool orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2450 enum symbol_visibility orig_visibility = DECL_VISIBILITY (decl);
2451
2452 /* The decl may be a template instantiation, which could influence
2453 visibilty. */
2454 tree template_decl = NULL_TREE;
2455 if (TREE_CODE (decl) == TYPE_DECL)
2456 {
2457 if (CLASS_TYPE_P (TREE_TYPE (decl)))
2458 {
2459 if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
2460 template_decl = decl;
2461 }
2462 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2463 template_decl = decl;
2464 }
2465 else if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
2466 template_decl = decl;
2467
2468 /* If DECL is a member of a class, visibility specifiers on the
2469 class can influence the visibility of the DECL. */
2470 tree class_type = NULL_TREE;
2471 if (DECL_CLASS_SCOPE_P (decl))
2472 class_type = DECL_CONTEXT (decl);
2473 else
2474 {
2475 /* Not a class member. */
2476
2477 /* Virtual tables have DECL_CONTEXT set to their associated class,
2478 so they are automatically handled above. */
2479 gcc_assert (!VAR_P (decl)
2480 || !DECL_VTABLE_OR_VTT_P (decl));
2481
2482 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2483 {
2484 /* Local statics and classes get the visibility of their
2485 containing function by default, except that
2486 -fvisibility-inlines-hidden doesn't affect them. */
2487 tree fn = DECL_CONTEXT (decl);
2488 if (DECL_VISIBILITY_SPECIFIED (fn))
2489 {
2490 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2491 DECL_VISIBILITY_SPECIFIED (decl) =
2492 DECL_VISIBILITY_SPECIFIED (fn);
2493 }
2494 else
2495 {
2496 if (DECL_CLASS_SCOPE_P (fn))
2497 determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2498 else if (determine_hidden_inline (fn))
2499 {
2500 DECL_VISIBILITY (decl) = default_visibility;
2501 DECL_VISIBILITY_SPECIFIED (decl) =
2502 visibility_options.inpragma;
2503 }
2504 else
2505 {
2506 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2507 DECL_VISIBILITY_SPECIFIED (decl) =
2508 DECL_VISIBILITY_SPECIFIED (fn);
2509 }
2510 }
2511
2512 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2513 but have no TEMPLATE_INFO, so don't try to check it. */
2514 template_decl = NULL_TREE;
2515 }
2516 else if (VAR_P (decl) && DECL_TINFO_P (decl)
2517 && flag_visibility_ms_compat)
2518 {
2519 /* Under -fvisibility-ms-compat, types are visible by default,
2520 even though their contents aren't. */
2521 tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2522 int underlying_vis = type_visibility (underlying_type);
2523 if (underlying_vis == VISIBILITY_ANON
2524 || (CLASS_TYPE_P (underlying_type)
2525 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2526 constrain_visibility (decl, underlying_vis, false);
2527 else
2528 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2529 }
2530 else if (VAR_P (decl) && DECL_TINFO_P (decl))
2531 {
2532 /* tinfo visibility is based on the type it's for. */
2533 constrain_visibility
2534 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2535
2536 /* Give the target a chance to override the visibility associated
2537 with DECL. */
2538 if (TREE_PUBLIC (decl)
2539 && !DECL_REALLY_EXTERN (decl)
2540 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2541 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2542 targetm.cxx.determine_class_data_visibility (decl);
2543 }
2544 else if (template_decl)
2545 /* Template instantiations and specializations get visibility based
2546 on their template unless they override it with an attribute. */;
2547 else if (! DECL_VISIBILITY_SPECIFIED (decl))
2548 {
2549 if (determine_hidden_inline (decl))
2550 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2551 else
2552 {
2553 /* Set default visibility to whatever the user supplied with
2554 #pragma GCC visibility or a namespace visibility attribute. */
2555 DECL_VISIBILITY (decl) = default_visibility;
2556 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2557 }
2558 }
2559 }
2560
2561 if (template_decl)
2562 {
2563 /* If the specialization doesn't specify visibility, use the
2564 visibility from the template. */
2565 tree tinfo = get_template_info (template_decl);
2566 tree args = TI_ARGS (tinfo);
2567 tree attribs = (TREE_CODE (decl) == TYPE_DECL
2568 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2569 : DECL_ATTRIBUTES (decl));
2570
2571 if (args != error_mark_node)
2572 {
2573 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2574
2575 if (!DECL_VISIBILITY_SPECIFIED (decl))
2576 {
2577 if (!DECL_VISIBILITY_SPECIFIED (pattern)
2578 && determine_hidden_inline (decl))
2579 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2580 else
2581 {
2582 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2583 DECL_VISIBILITY_SPECIFIED (decl)
2584 = DECL_VISIBILITY_SPECIFIED (pattern);
2585 }
2586 }
2587
2588 if (args
2589 /* Template argument visibility outweighs #pragma or namespace
2590 visibility, but not an explicit attribute. */
2591 && !lookup_attribute ("visibility", attribs))
2592 {
2593 int depth = TMPL_ARGS_DEPTH (args);
2594 if (DECL_VISIBILITY_SPECIFIED (decl))
2595 {
2596 /* A class template member with explicit visibility
2597 overrides the class visibility, so we need to apply
2598 all the levels of template args directly. */
2599 int i;
2600 for (i = 1; i <= depth; ++i)
2601 {
2602 tree lev = TMPL_ARGS_LEVEL (args, i);
2603 constrain_visibility_for_template (decl, lev);
2604 }
2605 }
2606 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
2607 /* Limit visibility based on its template arguments. */
2608 constrain_visibility_for_template (decl, args);
2609 }
2610 }
2611 }
2612
2613 if (class_type)
2614 determine_visibility_from_class (decl, class_type);
2615
2616 if (decl_anon_ns_mem_p (decl))
2617 /* Names in an anonymous namespace get internal linkage.
2618 This might change once we implement export. */
2619 constrain_visibility (decl, VISIBILITY_ANON, false);
2620 else if (TREE_CODE (decl) != TYPE_DECL)
2621 {
2622 /* Propagate anonymity from type to decl. */
2623 int tvis = type_visibility (TREE_TYPE (decl));
2624 if (tvis == VISIBILITY_ANON
2625 || ! DECL_VISIBILITY_SPECIFIED (decl))
2626 constrain_visibility (decl, tvis, false);
2627 }
2628 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2629 /* DR 757: A type without linkage shall not be used as the type of a
2630 variable or function with linkage, unless
2631 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2632 o the variable or function is not used (3.2 [basic.def.odr]) or is
2633 defined in the same translation unit.
2634
2635 Since non-extern "C" decls need to be defined in the same
2636 translation unit, we can make the type internal. */
2637 constrain_visibility (decl, VISIBILITY_ANON, false);
2638
2639 /* If visibility changed and DECL already has DECL_RTL, ensure
2640 symbol flags are updated. */
2641 if ((DECL_VISIBILITY (decl) != orig_visibility
2642 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2643 && ((VAR_P (decl) && TREE_STATIC (decl))
2644 || TREE_CODE (decl) == FUNCTION_DECL)
2645 && DECL_RTL_SET_P (decl))
2646 make_decl_rtl (decl);
2647 }
2648
2649 /* By default, static data members and function members receive
2650 the visibility of their containing class. */
2651
2652 static void
2653 determine_visibility_from_class (tree decl, tree class_type)
2654 {
2655 if (DECL_VISIBILITY_SPECIFIED (decl))
2656 return;
2657
2658 if (determine_hidden_inline (decl))
2659 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2660 else
2661 {
2662 /* Default to the class visibility. */
2663 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2664 DECL_VISIBILITY_SPECIFIED (decl)
2665 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2666 }
2667
2668 /* Give the target a chance to override the visibility associated
2669 with DECL. */
2670 if (VAR_P (decl)
2671 && (DECL_TINFO_P (decl)
2672 || (DECL_VTABLE_OR_VTT_P (decl)
2673 /* Construction virtual tables are not exported because
2674 they cannot be referred to from other object files;
2675 their name is not standardized by the ABI. */
2676 && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2677 && TREE_PUBLIC (decl)
2678 && !DECL_REALLY_EXTERN (decl)
2679 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2680 targetm.cxx.determine_class_data_visibility (decl);
2681 }
2682
2683 /* Returns true iff DECL is an inline that should get hidden visibility
2684 because of -fvisibility-inlines-hidden. */
2685
2686 static bool
2687 determine_hidden_inline (tree decl)
2688 {
2689 return (visibility_options.inlines_hidden
2690 /* Don't do this for inline templates; specializations might not be
2691 inline, and we don't want them to inherit the hidden
2692 visibility. We'll set it here for all inline instantiations. */
2693 && !processing_template_decl
2694 && TREE_CODE (decl) == FUNCTION_DECL
2695 && DECL_DECLARED_INLINE_P (decl)
2696 && (! DECL_LANG_SPECIFIC (decl)
2697 || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2698 }
2699
2700 /* Constrain the visibility of a class TYPE based on the visibility of its
2701 field types. Warn if any fields require lesser visibility. */
2702
2703 void
2704 constrain_class_visibility (tree type)
2705 {
2706 tree binfo;
2707 tree t;
2708 int i;
2709
2710 int vis = type_visibility (type);
2711
2712 if (vis == VISIBILITY_ANON
2713 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2714 return;
2715
2716 /* Don't warn about visibility if the class has explicit visibility. */
2717 if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2718 vis = VISIBILITY_INTERNAL;
2719
2720 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2721 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node
2722 && !DECL_ARTIFICIAL (t))
2723 {
2724 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2725 int subvis = type_visibility (ftype);
2726
2727 if (subvis == VISIBILITY_ANON)
2728 {
2729 if (!in_main_input_context())
2730 {
2731 tree nlt = no_linkage_check (ftype, /*relaxed_p=*/false);
2732 if (nlt)
2733 {
2734 if (same_type_p (TREE_TYPE (t), nlt))
2735 warning (OPT_Wsubobject_linkage, "\
2736 %qT has a field %qD whose type has no linkage",
2737 type, t);
2738 else
2739 warning (OPT_Wsubobject_linkage, "\
2740 %qT has a field %qD whose type depends on the type %qT which has no linkage",
2741 type, t, nlt);
2742 }
2743 else
2744 warning (OPT_Wsubobject_linkage, "\
2745 %qT has a field %qD whose type uses the anonymous namespace",
2746 type, t);
2747 }
2748 }
2749 else if (MAYBE_CLASS_TYPE_P (ftype)
2750 && vis < VISIBILITY_HIDDEN
2751 && subvis >= VISIBILITY_HIDDEN)
2752 warning (OPT_Wattributes, "\
2753 %qT declared with greater visibility than the type of its field %qD",
2754 type, t);
2755 }
2756
2757 binfo = TYPE_BINFO (type);
2758 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2759 {
2760 int subvis = type_visibility (TREE_TYPE (t));
2761
2762 if (subvis == VISIBILITY_ANON)
2763 {
2764 if (!in_main_input_context())
2765 {
2766 tree nlt = no_linkage_check (TREE_TYPE (t), /*relaxed_p=*/false);
2767 if (nlt)
2768 {
2769 if (same_type_p (TREE_TYPE (t), nlt))
2770 warning (OPT_Wsubobject_linkage, "\
2771 %qT has a base %qT whose type has no linkage",
2772 type, TREE_TYPE (t));
2773 else
2774 warning (OPT_Wsubobject_linkage, "\
2775 %qT has a base %qT whose type depends on the type %qT which has no linkage",
2776 type, TREE_TYPE (t), nlt);
2777 }
2778 else
2779 warning (OPT_Wsubobject_linkage, "\
2780 %qT has a base %qT whose type uses the anonymous namespace",
2781 type, TREE_TYPE (t));
2782 }
2783 }
2784 else if (vis < VISIBILITY_HIDDEN
2785 && subvis >= VISIBILITY_HIDDEN)
2786 warning (OPT_Wattributes, "\
2787 %qT declared with greater visibility than its base %qT",
2788 type, TREE_TYPE (t));
2789 }
2790 }
2791
2792 /* Functions for adjusting the visibility of a tagged type and its nested
2793 types and declarations when it gets a name for linkage purposes from a
2794 typedef. */
2795
2796 static void bt_reset_linkage_1 (binding_entry, void *);
2797 static void bt_reset_linkage_2 (binding_entry, void *);
2798
2799 /* First reset the visibility of all the types. */
2800
2801 static void
2802 reset_type_linkage_1 (tree type)
2803 {
2804 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
2805 if (CLASS_TYPE_P (type))
2806 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2807 bt_reset_linkage_1, NULL);
2808 }
2809 static void
2810 bt_reset_linkage_1 (binding_entry b, void */*data*/)
2811 {
2812 reset_type_linkage_1 (b->type);
2813 }
2814
2815 /* Then reset the visibility of any static data members or member
2816 functions that use those types. */
2817
2818 static void
2819 reset_decl_linkage (tree decl)
2820 {
2821 if (TREE_PUBLIC (decl))
2822 return;
2823 if (DECL_CLONED_FUNCTION_P (decl))
2824 return;
2825 TREE_PUBLIC (decl) = true;
2826 DECL_INTERFACE_KNOWN (decl) = false;
2827 determine_visibility (decl);
2828 tentative_decl_linkage (decl);
2829 }
2830
2831 static void
2832 reset_type_linkage_2 (tree type)
2833 {
2834 if (CLASS_TYPE_P (type))
2835 {
2836 if (tree vt = CLASSTYPE_VTABLES (type))
2837 {
2838 tree name = mangle_vtbl_for_type (type);
2839 DECL_NAME (vt) = name;
2840 SET_DECL_ASSEMBLER_NAME (vt, name);
2841 reset_decl_linkage (vt);
2842 }
2843 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
2844 {
2845 tree name = mangle_typeinfo_for_type (type);
2846 DECL_NAME (ti) = name;
2847 SET_DECL_ASSEMBLER_NAME (ti, name);
2848 TREE_TYPE (name) = type;
2849 reset_decl_linkage (ti);
2850 }
2851 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
2852 {
2853 tree mem = STRIP_TEMPLATE (m);
2854 if (TREE_CODE (mem) == VAR_DECL || TREE_CODE (mem) == FUNCTION_DECL)
2855 reset_decl_linkage (mem);
2856 }
2857 binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
2858 bt_reset_linkage_2, NULL);
2859 }
2860 }
2861
2862 static void
2863 bt_reset_linkage_2 (binding_entry b, void */*data*/)
2864 {
2865 reset_type_linkage_2 (b->type);
2866 }
2867 void
2868 reset_type_linkage (tree type)
2869 {
2870 reset_type_linkage_1 (type);
2871 reset_type_linkage_2 (type);
2872 }
2873
2874 /* Set up our initial idea of what the linkage of DECL should be. */
2875
2876 void
2877 tentative_decl_linkage (tree decl)
2878 {
2879 if (DECL_INTERFACE_KNOWN (decl))
2880 /* We've already made a decision as to how this function will
2881 be handled. */;
2882 else if (vague_linkage_p (decl))
2883 {
2884 if (TREE_CODE (decl) == FUNCTION_DECL
2885 && decl_defined_p (decl))
2886 {
2887 DECL_EXTERNAL (decl) = 1;
2888 DECL_NOT_REALLY_EXTERN (decl) = 1;
2889 note_vague_linkage_fn (decl);
2890 /* A non-template inline function with external linkage will
2891 always be COMDAT. As we must eventually determine the
2892 linkage of all functions, and as that causes writes to
2893 the data mapped in from the PCH file, it's advantageous
2894 to mark the functions at this point. */
2895 if (DECL_DECLARED_INLINE_P (decl)
2896 && (!DECL_IMPLICIT_INSTANTIATION (decl)
2897 || DECL_DEFAULTED_FN (decl)))
2898 {
2899 /* This function must have external linkage, as
2900 otherwise DECL_INTERFACE_KNOWN would have been
2901 set. */
2902 gcc_assert (TREE_PUBLIC (decl));
2903 comdat_linkage (decl);
2904 DECL_INTERFACE_KNOWN (decl) = 1;
2905 }
2906 }
2907 else if (VAR_P (decl))
2908 maybe_commonize_var (decl);
2909 }
2910 }
2911
2912 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
2913 for DECL has not already been determined, do so now by setting
2914 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
2915 function is called entities with vague linkage whose definitions
2916 are available must have TREE_PUBLIC set.
2917
2918 If this function decides to place DECL in COMDAT, it will set
2919 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
2920 the caller to decide whether or not to clear DECL_EXTERNAL. Some
2921 callers defer that decision until it is clear that DECL is actually
2922 required. */
2923
2924 void
2925 import_export_decl (tree decl)
2926 {
2927 int emit_p;
2928 bool comdat_p;
2929 bool import_p;
2930 tree class_type = NULL_TREE;
2931
2932 if (DECL_INTERFACE_KNOWN (decl))
2933 return;
2934
2935 /* We cannot determine what linkage to give to an entity with vague
2936 linkage until the end of the file. For example, a virtual table
2937 for a class will be defined if and only if the key method is
2938 defined in this translation unit. As a further example, consider
2939 that when compiling a translation unit that uses PCH file with
2940 "-frepo" it would be incorrect to make decisions about what
2941 entities to emit when building the PCH; those decisions must be
2942 delayed until the repository information has been processed. */
2943 gcc_assert (at_eof);
2944 /* Object file linkage for explicit instantiations is handled in
2945 mark_decl_instantiated. For static variables in functions with
2946 vague linkage, maybe_commonize_var is used.
2947
2948 Therefore, the only declarations that should be provided to this
2949 function are those with external linkage that are:
2950
2951 * implicit instantiations of function templates
2952
2953 * inline function
2954
2955 * implicit instantiations of static data members of class
2956 templates
2957
2958 * virtual tables
2959
2960 * typeinfo objects
2961
2962 Furthermore, all entities that reach this point must have a
2963 definition available in this translation unit.
2964
2965 The following assertions check these conditions. */
2966 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
2967 /* Any code that creates entities with TREE_PUBLIC cleared should
2968 also set DECL_INTERFACE_KNOWN. */
2969 gcc_assert (TREE_PUBLIC (decl));
2970 if (TREE_CODE (decl) == FUNCTION_DECL)
2971 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2972 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2973 || DECL_DECLARED_INLINE_P (decl));
2974 else
2975 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2976 || DECL_VTABLE_OR_VTT_P (decl)
2977 || DECL_TINFO_P (decl));
2978 /* Check that a definition of DECL is available in this translation
2979 unit. */
2980 gcc_assert (!DECL_REALLY_EXTERN (decl));
2981
2982 /* Assume that DECL will not have COMDAT linkage. */
2983 comdat_p = false;
2984 /* Assume that DECL will not be imported into this translation
2985 unit. */
2986 import_p = false;
2987
2988 /* See if the repository tells us whether or not to emit DECL in
2989 this translation unit. */
2990 emit_p = repo_emit_p (decl);
2991 if (emit_p == 0)
2992 import_p = true;
2993 else if (emit_p == 1)
2994 {
2995 /* The repository indicates that this entity should be defined
2996 here. Make sure the back end honors that request. */
2997 mark_needed (decl);
2998 /* Output the definition as an ordinary strong definition. */
2999 DECL_EXTERNAL (decl) = 0;
3000 DECL_INTERFACE_KNOWN (decl) = 1;
3001 return;
3002 }
3003
3004 if (import_p)
3005 /* We have already decided what to do with this DECL; there is no
3006 need to check anything further. */
3007 ;
3008 else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
3009 {
3010 class_type = DECL_CONTEXT (decl);
3011 import_export_class (class_type);
3012 if (CLASSTYPE_INTERFACE_KNOWN (class_type)
3013 && CLASSTYPE_INTERFACE_ONLY (class_type))
3014 import_p = true;
3015 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
3016 && !CLASSTYPE_USE_TEMPLATE (class_type)
3017 && CLASSTYPE_KEY_METHOD (class_type)
3018 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
3019 /* The ABI requires that all virtual tables be emitted with
3020 COMDAT linkage. However, on systems where COMDAT symbols
3021 don't show up in the table of contents for a static
3022 archive, or on systems without weak symbols (where we
3023 approximate COMDAT linkage by using internal linkage), the
3024 linker will report errors about undefined symbols because
3025 it will not see the virtual table definition. Therefore,
3026 in the case that we know that the virtual table will be
3027 emitted in only one translation unit, we make the virtual
3028 table an ordinary definition with external linkage. */
3029 DECL_EXTERNAL (decl) = 0;
3030 else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
3031 {
3032 /* CLASS_TYPE is being exported from this translation unit,
3033 so DECL should be defined here. */
3034 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
3035 /* If a class is declared in a header with the "extern
3036 template" extension, then it will not be instantiated,
3037 even in translation units that would normally require
3038 it. Often such classes are explicitly instantiated in
3039 one translation unit. Therefore, the explicit
3040 instantiation must be made visible to other translation
3041 units. */
3042 DECL_EXTERNAL (decl) = 0;
3043 else
3044 {
3045 /* The generic C++ ABI says that class data is always
3046 COMDAT, even if there is a key function. Some
3047 variants (e.g., the ARM EABI) says that class data
3048 only has COMDAT linkage if the class data might be
3049 emitted in more than one translation unit. When the
3050 key method can be inline and is inline, we still have
3051 to arrange for comdat even though
3052 class_data_always_comdat is false. */
3053 if (!CLASSTYPE_KEY_METHOD (class_type)
3054 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
3055 || targetm.cxx.class_data_always_comdat ())
3056 {
3057 /* The ABI requires COMDAT linkage. Normally, we
3058 only emit COMDAT things when they are needed;
3059 make sure that we realize that this entity is
3060 indeed needed. */
3061 comdat_p = true;
3062 mark_needed (decl);
3063 }
3064 }
3065 }
3066 else if (!flag_implicit_templates
3067 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
3068 import_p = true;
3069 else
3070 comdat_p = true;
3071 }
3072 else if (VAR_P (decl) && DECL_TINFO_P (decl))
3073 {
3074 tree type = TREE_TYPE (DECL_NAME (decl));
3075 if (CLASS_TYPE_P (type))
3076 {
3077 class_type = type;
3078 import_export_class (type);
3079 if (CLASSTYPE_INTERFACE_KNOWN (type)
3080 && TYPE_POLYMORPHIC_P (type)
3081 && CLASSTYPE_INTERFACE_ONLY (type)
3082 /* If -fno-rtti was specified, then we cannot be sure
3083 that RTTI information will be emitted with the
3084 virtual table of the class, so we must emit it
3085 wherever it is used. */
3086 && flag_rtti)
3087 import_p = true;
3088 else
3089 {
3090 if (CLASSTYPE_INTERFACE_KNOWN (type)
3091 && !CLASSTYPE_INTERFACE_ONLY (type))
3092 {
3093 comdat_p = (targetm.cxx.class_data_always_comdat ()
3094 || (CLASSTYPE_KEY_METHOD (type)
3095 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
3096 mark_needed (decl);
3097 if (!flag_weak)
3098 {
3099 comdat_p = false;
3100 DECL_EXTERNAL (decl) = 0;
3101 }
3102 }
3103 else
3104 comdat_p = true;
3105 }
3106 }
3107 else
3108 comdat_p = true;
3109 }
3110 else if (DECL_TEMPLOID_INSTANTIATION (decl))
3111 {
3112 /* DECL is an implicit instantiation of a function or static
3113 data member. */
3114 if ((flag_implicit_templates
3115 && !flag_use_repository)
3116 || (flag_implicit_inline_templates
3117 && TREE_CODE (decl) == FUNCTION_DECL
3118 && DECL_DECLARED_INLINE_P (decl)))
3119 comdat_p = true;
3120 else
3121 /* If we are not implicitly generating templates, then mark
3122 this entity as undefined in this translation unit. */
3123 import_p = true;
3124 }
3125 else if (DECL_FUNCTION_MEMBER_P (decl))
3126 {
3127 if (!DECL_DECLARED_INLINE_P (decl))
3128 {
3129 tree ctype = DECL_CONTEXT (decl);
3130 import_export_class (ctype);
3131 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
3132 {
3133 DECL_NOT_REALLY_EXTERN (decl)
3134 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
3135 || (DECL_DECLARED_INLINE_P (decl)
3136 && ! flag_implement_inlines
3137 && !DECL_VINDEX (decl)));
3138
3139 if (!DECL_NOT_REALLY_EXTERN (decl))
3140 DECL_EXTERNAL (decl) = 1;
3141
3142 /* Always make artificials weak. */
3143 if (DECL_ARTIFICIAL (decl) && flag_weak)
3144 comdat_p = true;
3145 else
3146 maybe_make_one_only (decl);
3147 }
3148 }
3149 else
3150 comdat_p = true;
3151 }
3152 else
3153 comdat_p = true;
3154
3155 if (import_p)
3156 {
3157 /* If we are importing DECL into this translation unit, mark is
3158 an undefined here. */
3159 DECL_EXTERNAL (decl) = 1;
3160 DECL_NOT_REALLY_EXTERN (decl) = 0;
3161 }
3162 else if (comdat_p)
3163 {
3164 /* If we decided to put DECL in COMDAT, mark it accordingly at
3165 this point. */
3166 comdat_linkage (decl);
3167 }
3168
3169 DECL_INTERFACE_KNOWN (decl) = 1;
3170 }
3171
3172 /* Return an expression that performs the destruction of DECL, which
3173 must be a VAR_DECL whose type has a non-trivial destructor, or is
3174 an array whose (innermost) elements have a non-trivial destructor. */
3175
3176 tree
3177 build_cleanup (tree decl)
3178 {
3179 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
3180 gcc_assert (clean != NULL_TREE);
3181 return clean;
3182 }
3183
3184 /* Returns the initialization guard variable for the variable DECL,
3185 which has static storage duration. */
3186
3187 tree
3188 get_guard (tree decl)
3189 {
3190 tree sname;
3191 tree guard;
3192
3193 sname = mangle_guard_variable (decl);
3194 guard = get_global_binding (sname);
3195 if (! guard)
3196 {
3197 tree guard_type;
3198
3199 /* We use a type that is big enough to contain a mutex as well
3200 as an integer counter. */
3201 guard_type = targetm.cxx.guard_type ();
3202 guard = build_decl (DECL_SOURCE_LOCATION (decl),
3203 VAR_DECL, sname, guard_type);
3204
3205 /* The guard should have the same linkage as what it guards. */
3206 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
3207 TREE_STATIC (guard) = TREE_STATIC (decl);
3208 DECL_COMMON (guard) = DECL_COMMON (decl);
3209 DECL_COMDAT (guard) = DECL_COMDAT (decl);
3210 CP_DECL_THREAD_LOCAL_P (guard) = CP_DECL_THREAD_LOCAL_P (decl);
3211 set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
3212 if (DECL_ONE_ONLY (decl))
3213 make_decl_one_only (guard, cxx_comdat_group (guard));
3214 if (TREE_PUBLIC (decl))
3215 DECL_WEAK (guard) = DECL_WEAK (decl);
3216 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
3217 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
3218
3219 DECL_ARTIFICIAL (guard) = 1;
3220 DECL_IGNORED_P (guard) = 1;
3221 TREE_USED (guard) = 1;
3222 pushdecl_top_level_and_finish (guard, NULL_TREE);
3223 }
3224 return guard;
3225 }
3226
3227 /* Return an atomic load of src with the appropriate memory model. */
3228
3229 static tree
3230 build_atomic_load_byte (tree src, HOST_WIDE_INT model)
3231 {
3232 tree ptr_type = build_pointer_type (char_type_node);
3233 tree mem_model = build_int_cst (integer_type_node, model);
3234 tree t, addr, val;
3235 unsigned int size;
3236 int fncode;
3237
3238 size = tree_to_uhwi (TYPE_SIZE_UNIT (char_type_node));
3239
3240 fncode = BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1;
3241 t = builtin_decl_implicit ((enum built_in_function) fncode);
3242
3243 addr = build1 (ADDR_EXPR, ptr_type, src);
3244 val = build_call_expr (t, 2, addr, mem_model);
3245 return val;
3246 }
3247
3248 /* Return those bits of the GUARD variable that should be set when the
3249 guarded entity is actually initialized. */
3250
3251 static tree
3252 get_guard_bits (tree guard)
3253 {
3254 if (!targetm.cxx.guard_mask_bit ())
3255 {
3256 /* We only set the first byte of the guard, in order to leave room
3257 for a mutex in the high-order bits. */
3258 guard = build1 (ADDR_EXPR,
3259 build_pointer_type (TREE_TYPE (guard)),
3260 guard);
3261 guard = build1 (NOP_EXPR,
3262 build_pointer_type (char_type_node),
3263 guard);
3264 guard = build1 (INDIRECT_REF, char_type_node, guard);
3265 }
3266
3267 return guard;
3268 }
3269
3270 /* Return an expression which determines whether or not the GUARD
3271 variable has already been initialized. */
3272
3273 tree
3274 get_guard_cond (tree guard, bool thread_safe)
3275 {
3276 tree guard_value;
3277
3278 if (!thread_safe)
3279 guard = get_guard_bits (guard);
3280 else
3281 guard = build_atomic_load_byte (guard, MEMMODEL_ACQUIRE);
3282
3283 /* Mask off all but the low bit. */
3284 if (targetm.cxx.guard_mask_bit ())
3285 {
3286 guard_value = integer_one_node;
3287 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3288 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3289 guard = cp_build_binary_op (input_location,
3290 BIT_AND_EXPR, guard, guard_value,
3291 tf_warning_or_error);
3292 }
3293
3294 guard_value = integer_zero_node;
3295 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
3296 guard_value = fold_convert (TREE_TYPE (guard), guard_value);
3297 return cp_build_binary_op (input_location,
3298 EQ_EXPR, guard, guard_value,
3299 tf_warning_or_error);
3300 }
3301
3302 /* Return an expression which sets the GUARD variable, indicating that
3303 the variable being guarded has been initialized. */
3304
3305 tree
3306 set_guard (tree guard)
3307 {
3308 tree guard_init;
3309
3310 /* Set the GUARD to one. */
3311 guard = get_guard_bits (guard);
3312 guard_init = integer_one_node;
3313 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
3314 guard_init = fold_convert (TREE_TYPE (guard), guard_init);
3315 return cp_build_modify_expr (input_location, guard, NOP_EXPR, guard_init,
3316 tf_warning_or_error);
3317 }
3318
3319 /* Returns true iff we can tell that VAR does not have a dynamic
3320 initializer. */
3321
3322 static bool
3323 var_defined_without_dynamic_init (tree var)
3324 {
3325 /* If it's defined in another TU, we can't tell. */
3326 if (DECL_EXTERNAL (var))
3327 return false;
3328 /* If it has a non-trivial destructor, registering the destructor
3329 counts as dynamic initialization. */
3330 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
3331 return false;
3332 /* If it's in this TU, its initializer has been processed, unless
3333 it's a case of self-initialization, then DECL_INITIALIZED_P is
3334 false while the initializer is handled by finish_id_expression. */
3335 if (!DECL_INITIALIZED_P (var))
3336 return false;
3337 /* If it has no initializer or a constant one, it's not dynamic. */
3338 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
3339 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
3340 }
3341
3342 /* Returns true iff VAR is a variable that needs uses to be
3343 wrapped for possible dynamic initialization. */
3344
3345 static bool
3346 var_needs_tls_wrapper (tree var)
3347 {
3348 return (!error_operand_p (var)
3349 && CP_DECL_THREAD_LOCAL_P (var)
3350 && !DECL_GNU_TLS_P (var)
3351 && !DECL_FUNCTION_SCOPE_P (var)
3352 && !var_defined_without_dynamic_init (var));
3353 }
3354
3355 /* Get the FUNCTION_DECL for the shared TLS init function for this
3356 translation unit. */
3357
3358 static tree
3359 get_local_tls_init_fn (location_t loc)
3360 {
3361 tree sname = get_identifier ("__tls_init");
3362 tree fn = get_global_binding (sname);
3363 if (!fn)
3364 {
3365 fn = build_lang_decl_loc (loc, FUNCTION_DECL, sname,
3366 build_function_type (void_type_node,
3367 void_list_node));
3368 SET_DECL_LANGUAGE (fn, lang_c);
3369 TREE_PUBLIC (fn) = false;
3370 DECL_ARTIFICIAL (fn) = true;
3371 mark_used (fn);
3372 set_global_binding (fn);
3373 }
3374 return fn;
3375 }
3376
3377 /* Get a FUNCTION_DECL for the init function for the thread_local
3378 variable VAR. The init function will be an alias to the function
3379 that initializes all the non-local TLS variables in the translation
3380 unit. The init function is only used by the wrapper function. */
3381
3382 static tree
3383 get_tls_init_fn (tree var)
3384 {
3385 /* Only C++11 TLS vars need this init fn. */
3386 if (!var_needs_tls_wrapper (var))
3387 return NULL_TREE;
3388
3389 /* If -fno-extern-tls-init, assume that we don't need to call
3390 a tls init function for a variable defined in another TU. */
3391 if (!flag_extern_tls_init && DECL_EXTERNAL (var))
3392 return NULL_TREE;
3393
3394 /* If the variable is internal, or if we can't generate aliases,
3395 call the local init function directly. */
3396 if (!TREE_PUBLIC (var) || !TARGET_SUPPORTS_ALIASES)
3397 return get_local_tls_init_fn (DECL_SOURCE_LOCATION (var));
3398
3399 tree sname = mangle_tls_init_fn (var);
3400 tree fn = get_global_binding (sname);
3401 if (!fn)
3402 {
3403 fn = build_lang_decl (FUNCTION_DECL, sname,
3404 build_function_type (void_type_node,
3405 void_list_node));
3406 SET_DECL_LANGUAGE (fn, lang_c);
3407 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3408 DECL_ARTIFICIAL (fn) = true;
3409 DECL_COMDAT (fn) = DECL_COMDAT (var);
3410 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
3411 if (DECL_ONE_ONLY (var))
3412 make_decl_one_only (fn, cxx_comdat_group (fn));
3413 if (TREE_PUBLIC (var))
3414 {
3415 tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
3416 /* If the variable is defined somewhere else and might have static
3417 initialization, make the init function a weak reference. */
3418 if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
3419 || TYPE_HAS_CONSTEXPR_CTOR (obtype)
3420 || TYPE_HAS_TRIVIAL_DFLT (obtype))
3421 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
3422 && DECL_EXTERNAL (var))
3423 declare_weak (fn);
3424 else
3425 DECL_WEAK (fn) = DECL_WEAK (var);
3426 }
3427 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
3428 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
3429 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
3430 DECL_IGNORED_P (fn) = 1;
3431 mark_used (fn);
3432
3433 DECL_BEFRIENDING_CLASSES (fn) = var;
3434
3435 set_global_binding (fn);
3436 }
3437 return fn;
3438 }
3439
3440 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
3441 variable VAR. The wrapper function calls the init function (if any) for
3442 VAR and then returns a reference to VAR. The wrapper function is used
3443 in place of VAR everywhere VAR is mentioned. */
3444
3445 tree
3446 get_tls_wrapper_fn (tree var)
3447 {
3448 /* Only C++11 TLS vars need this wrapper fn. */
3449 if (!var_needs_tls_wrapper (var))
3450 return NULL_TREE;
3451
3452 tree sname = mangle_tls_wrapper_fn (var);
3453 tree fn = get_global_binding (sname);
3454 if (!fn)
3455 {
3456 /* A named rvalue reference is an lvalue, so the wrapper should
3457 always return an lvalue reference. */
3458 tree type = non_reference (TREE_TYPE (var));
3459 type = build_reference_type (type);
3460 tree fntype = build_function_type (type, void_list_node);
3461
3462 fn = build_lang_decl_loc (DECL_SOURCE_LOCATION (var),
3463 FUNCTION_DECL, sname, fntype);
3464 SET_DECL_LANGUAGE (fn, lang_c);
3465 TREE_PUBLIC (fn) = TREE_PUBLIC (var);
3466 DECL_ARTIFICIAL (fn) = true;
3467 DECL_IGNORED_P (fn) = 1;
3468 /* The wrapper is inline and emitted everywhere var is used. */
3469 DECL_DECLARED_INLINE_P (fn) = true;
3470 if (TREE_PUBLIC (var))
3471 {
3472 comdat_linkage (fn);
3473 #ifdef HAVE_GAS_HIDDEN
3474 /* Make the wrapper bind locally; there's no reason to share
3475 the wrapper between multiple shared objects. */
3476 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
3477 DECL_VISIBILITY_SPECIFIED (fn) = true;
3478 #endif
3479 }
3480 if (!TREE_PUBLIC (fn))
3481 DECL_INTERFACE_KNOWN (fn) = true;
3482 mark_used (fn);
3483 note_vague_linkage_fn (fn);
3484
3485 #if 0
3486 /* We want CSE to commonize calls to the wrapper, but marking it as
3487 pure is unsafe since it has side-effects. I guess we need a new
3488 ECF flag even weaker than ECF_PURE. FIXME! */
3489 DECL_PURE_P (fn) = true;
3490 #endif
3491
3492 DECL_BEFRIENDING_CLASSES (fn) = var;
3493
3494 set_global_binding (fn);
3495 }
3496 return fn;
3497 }
3498
3499 /* At EOF, generate the definition for the TLS wrapper function FN:
3500
3501 T& var_wrapper() {
3502 if (init_fn) init_fn();
3503 return var;
3504 } */
3505
3506 static void
3507 generate_tls_wrapper (tree fn)
3508 {
3509 tree var = DECL_BEFRIENDING_CLASSES (fn);
3510
3511 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
3512 tree body = begin_function_body ();
3513 /* Only call the init fn if there might be one. */
3514 if (tree init_fn = get_tls_init_fn (var))
3515 {
3516 tree if_stmt = NULL_TREE;
3517 /* If init_fn is a weakref, make sure it exists before calling. */
3518 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
3519 {
3520 if_stmt = begin_if_stmt ();
3521 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
3522 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
3523 NE_EXPR, addr, nullptr_node,
3524 tf_warning_or_error);
3525 finish_if_stmt_cond (cond, if_stmt);
3526 }
3527 finish_expr_stmt (build_cxx_call
3528 (init_fn, 0, NULL, tf_warning_or_error));
3529 if (if_stmt)
3530 {
3531 finish_then_clause (if_stmt);
3532 finish_if_stmt (if_stmt);
3533 }
3534 }
3535 else
3536 /* If there's no initialization, the wrapper is a constant function. */
3537 TREE_READONLY (fn) = true;
3538 finish_return_stmt (convert_from_reference (var));
3539 finish_function_body (body);
3540 expand_or_defer_fn (finish_function (/*inline_p=*/false));
3541 }
3542
3543 /* Start the process of running a particular set of global constructors
3544 or destructors. Subroutine of do_[cd]tors. Also called from
3545 vtv_start_verification_constructor_init_function. */
3546
3547 static tree
3548 start_objects (int method_type, int initp)
3549 {
3550 tree body;
3551 tree fndecl;
3552 char type[14];
3553
3554 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3555
3556 if (initp != DEFAULT_INIT_PRIORITY)
3557 {
3558 char joiner;
3559
3560 #ifdef JOINER
3561 joiner = JOINER;
3562 #else
3563 joiner = '_';
3564 #endif
3565
3566 sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
3567 }
3568 else
3569 sprintf (type, "sub_%c", method_type);
3570
3571 fndecl = build_lang_decl (FUNCTION_DECL,
3572 get_file_function_name (type),
3573 build_function_type_list (void_type_node,
3574 NULL_TREE));
3575 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
3576
3577 TREE_PUBLIC (current_function_decl) = 0;
3578
3579 /* Mark as artificial because it's not explicitly in the user's
3580 source code. */
3581 DECL_ARTIFICIAL (current_function_decl) = 1;
3582
3583 /* Mark this declaration as used to avoid spurious warnings. */
3584 TREE_USED (current_function_decl) = 1;
3585
3586 /* Mark this function as a global constructor or destructor. */
3587 if (method_type == 'I')
3588 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
3589 else
3590 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
3591
3592 body = begin_compound_stmt (BCS_FN_BODY);
3593
3594 return body;
3595 }
3596
3597 /* Finish the process of running a particular set of global constructors
3598 or destructors. Subroutine of do_[cd]tors. */
3599
3600 static void
3601 finish_objects (int method_type, int initp, tree body)
3602 {
3603 tree fn;
3604
3605 /* Finish up. */
3606 finish_compound_stmt (body);
3607 fn = finish_function (/*inline_p=*/false);
3608
3609 if (method_type == 'I')
3610 {
3611 DECL_STATIC_CONSTRUCTOR (fn) = 1;
3612 decl_init_priority_insert (fn, initp);
3613 }
3614 else
3615 {
3616 DECL_STATIC_DESTRUCTOR (fn) = 1;
3617 decl_fini_priority_insert (fn, initp);
3618 }
3619
3620 expand_or_defer_fn (fn);
3621 }
3622
3623 /* The names of the parameters to the function created to handle
3624 initializations and destructions for objects with static storage
3625 duration. */
3626 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3627 #define PRIORITY_IDENTIFIER "__priority"
3628
3629 /* The name of the function we create to handle initializations and
3630 destructions for objects with static storage duration. */
3631 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3632
3633 /* The declaration for the __INITIALIZE_P argument. */
3634 static GTY(()) tree initialize_p_decl;
3635
3636 /* The declaration for the __PRIORITY argument. */
3637 static GTY(()) tree priority_decl;
3638
3639 /* The declaration for the static storage duration function. */
3640 static GTY(()) tree ssdf_decl;
3641
3642 /* All the static storage duration functions created in this
3643 translation unit. */
3644 static GTY(()) vec<tree, va_gc> *ssdf_decls;
3645
3646 /* A map from priority levels to information about that priority
3647 level. There may be many such levels, so efficient lookup is
3648 important. */
3649 static splay_tree priority_info_map;
3650
3651 /* Begins the generation of the function that will handle all
3652 initialization and destruction of objects with static storage
3653 duration. The function generated takes two parameters of type
3654 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3655 nonzero, it performs initializations. Otherwise, it performs
3656 destructions. It only performs those initializations or
3657 destructions with the indicated __PRIORITY. The generated function
3658 returns no value.
3659
3660 It is assumed that this function will only be called once per
3661 translation unit. */
3662
3663 static tree
3664 start_static_storage_duration_function (unsigned count)
3665 {
3666 tree type;
3667 tree body;
3668 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3669
3670 /* Create the identifier for this function. It will be of the form
3671 SSDF_IDENTIFIER_<number>. */
3672 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
3673
3674 type = build_function_type_list (void_type_node,
3675 integer_type_node, integer_type_node,
3676 NULL_TREE);
3677
3678 /* Create the FUNCTION_DECL itself. */
3679 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3680 get_identifier (id),
3681 type);
3682 TREE_PUBLIC (ssdf_decl) = 0;
3683 DECL_ARTIFICIAL (ssdf_decl) = 1;
3684
3685 /* Put this function in the list of functions to be called from the
3686 static constructors and destructors. */
3687 if (!ssdf_decls)
3688 {
3689 vec_alloc (ssdf_decls, 32);
3690
3691 /* Take this opportunity to initialize the map from priority
3692 numbers to information about that priority level. */
3693 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3694 /*delete_key_fn=*/0,
3695 /*delete_value_fn=*/
3696 splay_tree_delete_pointers);
3697
3698 /* We always need to generate functions for the
3699 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3700 priorities later, we'll be sure to find the
3701 DEFAULT_INIT_PRIORITY. */
3702 get_priority_info (DEFAULT_INIT_PRIORITY);
3703 }
3704
3705 vec_safe_push (ssdf_decls, ssdf_decl);
3706
3707 /* Create the argument list. */
3708 initialize_p_decl = cp_build_parm_decl
3709 (ssdf_decl, get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
3710 TREE_USED (initialize_p_decl) = 1;
3711 priority_decl = cp_build_parm_decl
3712 (ssdf_decl, get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
3713 TREE_USED (priority_decl) = 1;
3714
3715 DECL_CHAIN (initialize_p_decl) = priority_decl;
3716 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3717
3718 /* Put the function in the global scope. */
3719 pushdecl (ssdf_decl);
3720
3721 /* Start the function itself. This is equivalent to declaring the
3722 function as:
3723
3724 static void __ssdf (int __initialize_p, init __priority_p);
3725
3726 It is static because we only need to call this function from the
3727 various constructor and destructor functions for this module. */
3728 start_preparsed_function (ssdf_decl,
3729 /*attrs=*/NULL_TREE,
3730 SF_PRE_PARSED);
3731
3732 /* Set up the scope of the outermost block in the function. */
3733 body = begin_compound_stmt (BCS_FN_BODY);
3734
3735 return body;
3736 }
3737
3738 /* Finish the generation of the function which performs initialization
3739 and destruction of objects with static storage duration. After
3740 this point, no more such objects can be created. */
3741
3742 static void
3743 finish_static_storage_duration_function (tree body)
3744 {
3745 /* Close out the function. */
3746 finish_compound_stmt (body);
3747 expand_or_defer_fn (finish_function (/*inline_p=*/false));
3748 }
3749
3750 /* Return the information about the indicated PRIORITY level. If no
3751 code to handle this level has yet been generated, generate the
3752 appropriate prologue. */
3753
3754 static priority_info
3755 get_priority_info (int priority)
3756 {
3757 priority_info pi;
3758 splay_tree_node n;
3759
3760 n = splay_tree_lookup (priority_info_map,
3761 (splay_tree_key) priority);
3762 if (!n)
3763 {
3764 /* Create a new priority information structure, and insert it
3765 into the map. */
3766 pi = XNEW (struct priority_info_s);
3767 pi->initializations_p = 0;
3768 pi->destructions_p = 0;
3769 splay_tree_insert (priority_info_map,
3770 (splay_tree_key) priority,
3771 (splay_tree_value) pi);
3772 }
3773 else
3774 pi = (priority_info) n->value;
3775
3776 return pi;
3777 }
3778
3779 /* The effective initialization priority of a DECL. */
3780
3781 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
3782 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3783 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3784
3785 /* Whether a DECL needs a guard to protect it against multiple
3786 initialization. */
3787
3788 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
3789 || DECL_ONE_ONLY (decl) \
3790 || DECL_WEAK (decl)))
3791
3792 /* Called from one_static_initialization_or_destruction(),
3793 via walk_tree.
3794 Walks the initializer list of a global variable and looks for
3795 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3796 and that have their DECL_CONTEXT() == NULL.
3797 For each such temporary variable, set their DECL_CONTEXT() to
3798 the current function. This is necessary because otherwise
3799 some optimizers (enabled by -O2 -fprofile-arcs) might crash
3800 when trying to refer to a temporary variable that does not have
3801 it's DECL_CONTECT() properly set. */
3802 static tree
3803 fix_temporary_vars_context_r (tree *node,
3804 int * /*unused*/,
3805 void * /*unused1*/)
3806 {
3807 gcc_assert (current_function_decl);
3808
3809 if (TREE_CODE (*node) == BIND_EXPR)
3810 {
3811 tree var;
3812
3813 for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3814 if (VAR_P (var)
3815 && !DECL_NAME (var)
3816 && DECL_ARTIFICIAL (var)
3817 && !DECL_CONTEXT (var))
3818 DECL_CONTEXT (var) = current_function_decl;
3819 }
3820
3821 return NULL_TREE;
3822 }
3823
3824 /* Set up to handle the initialization or destruction of DECL. If
3825 INITP is nonzero, we are initializing the variable. Otherwise, we
3826 are destroying it. */
3827
3828 static void
3829 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3830 {
3831 tree guard_if_stmt = NULL_TREE;
3832 tree guard;
3833
3834 /* If we are supposed to destruct and there's a trivial destructor,
3835 nothing has to be done. */
3836 if (!initp
3837 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3838 return;
3839
3840 /* Trick the compiler into thinking we are at the file and line
3841 where DECL was declared so that error-messages make sense, and so
3842 that the debugger will show somewhat sensible file and line
3843 information. */
3844 input_location = DECL_SOURCE_LOCATION (decl);
3845
3846 /* Make sure temporary variables in the initialiser all have
3847 their DECL_CONTEXT() set to a value different from NULL_TREE.
3848 This can happen when global variables initializers are built.
3849 In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3850 the temporary variables that might have been generated in the
3851 accompanying initializers is NULL_TREE, meaning the variables have been
3852 declared in the global namespace.
3853 What we want to do here is to fix that and make sure the DECL_CONTEXT()
3854 of the temporaries are set to the current function decl. */
3855 cp_walk_tree_without_duplicates (&init,
3856 fix_temporary_vars_context_r,
3857 NULL);
3858
3859 /* Because of:
3860
3861 [class.access.spec]
3862
3863 Access control for implicit calls to the constructors,
3864 the conversion functions, or the destructor called to
3865 create and destroy a static data member is performed as
3866 if these calls appeared in the scope of the member's
3867 class.
3868
3869 we pretend we are in a static member function of the class of
3870 which the DECL is a member. */
3871 if (member_p (decl))
3872 {
3873 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3874 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3875 }
3876
3877 /* Assume we don't need a guard. */
3878 guard = NULL_TREE;
3879 /* We need a guard if this is an object with external linkage that
3880 might be initialized in more than one place. (For example, a
3881 static data member of a template, when the data member requires
3882 construction.) */
3883 if (NEEDS_GUARD_P (decl))
3884 {
3885 tree guard_cond;
3886
3887 guard = get_guard (decl);
3888
3889 /* When using __cxa_atexit, we just check the GUARD as we would
3890 for a local static. */
3891 if (flag_use_cxa_atexit)
3892 {
3893 /* When using __cxa_atexit, we never try to destroy
3894 anything from a static destructor. */
3895 gcc_assert (initp);
3896 guard_cond = get_guard_cond (guard, false);
3897 }
3898 /* If we don't have __cxa_atexit, then we will be running
3899 destructors from .fini sections, or their equivalents. So,
3900 we need to know how many times we've tried to initialize this
3901 object. We do initializations only if the GUARD is zero,
3902 i.e., if we are the first to initialize the variable. We do
3903 destructions only if the GUARD is one, i.e., if we are the
3904 last to destroy the variable. */
3905 else if (initp)
3906 guard_cond
3907 = cp_build_binary_op (input_location,
3908 EQ_EXPR,
3909 cp_build_unary_op (PREINCREMENT_EXPR,
3910 guard,
3911 /*noconvert=*/true,
3912 tf_warning_or_error),
3913 integer_one_node,
3914 tf_warning_or_error);
3915 else
3916 guard_cond
3917 = cp_build_binary_op (input_location,
3918 EQ_EXPR,
3919 cp_build_unary_op (PREDECREMENT_EXPR,
3920 guard,
3921 /*noconvert=*/true,
3922 tf_warning_or_error),
3923 integer_zero_node,
3924 tf_warning_or_error);
3925
3926 guard_if_stmt = begin_if_stmt ();
3927 finish_if_stmt_cond (guard_cond, guard_if_stmt);
3928 }
3929
3930
3931 /* If we're using __cxa_atexit, we have not already set the GUARD,
3932 so we must do so now. */
3933 if (guard && initp && flag_use_cxa_atexit)
3934 finish_expr_stmt (set_guard (guard));
3935
3936 /* Perform the initialization or destruction. */
3937 if (initp)
3938 {
3939 if (init)
3940 {
3941 finish_expr_stmt (init);
3942 if (sanitize_flags_p (SANITIZE_ADDRESS, decl))
3943 {
3944 varpool_node *vnode = varpool_node::get (decl);
3945 if (vnode)
3946 vnode->dynamically_initialized = 1;
3947 }
3948 }
3949
3950 /* If we're using __cxa_atexit, register a function that calls the
3951 destructor for the object. */
3952 if (flag_use_cxa_atexit)
3953 finish_expr_stmt (register_dtor_fn (decl));
3954 }
3955 else
3956 finish_expr_stmt (build_cleanup (decl));
3957
3958 /* Finish the guard if-stmt, if necessary. */
3959 if (guard)
3960 {
3961 finish_then_clause (guard_if_stmt);
3962 finish_if_stmt (guard_if_stmt);
3963 }
3964
3965 /* Now that we're done with DECL we don't need to pretend to be a
3966 member of its class any longer. */
3967 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3968 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3969 }
3970
3971 /* Generate code to do the initialization or destruction of the decls in VARS,
3972 a TREE_LIST of VAR_DECL with static storage duration.
3973 Whether initialization or destruction is performed is specified by INITP. */
3974
3975 static void
3976 do_static_initialization_or_destruction (tree vars, bool initp)
3977 {
3978 tree node, init_if_stmt, cond;
3979
3980 /* Build the outer if-stmt to check for initialization or destruction. */
3981 init_if_stmt = begin_if_stmt ();
3982 cond = initp ? integer_one_node : integer_zero_node;
3983 cond = cp_build_binary_op (input_location,
3984 EQ_EXPR,
3985 initialize_p_decl,
3986 cond,
3987 tf_warning_or_error);
3988 finish_if_stmt_cond (cond, init_if_stmt);
3989
3990 /* To make sure dynamic construction doesn't access globals from other
3991 compilation units where they might not be yet constructed, for
3992 -fsanitize=address insert __asan_before_dynamic_init call that
3993 prevents access to either all global variables that need construction
3994 in other compilation units, or at least those that haven't been
3995 initialized yet. Variables that need dynamic construction in
3996 the current compilation unit are kept accessible. */
3997 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
3998 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
3999
4000 node = vars;
4001 do {
4002 tree decl = TREE_VALUE (node);
4003 tree priority_if_stmt;
4004 int priority;
4005 priority_info pi;
4006
4007 /* If we don't need a destructor, there's nothing to do. Avoid
4008 creating a possibly empty if-stmt. */
4009 if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
4010 {
4011 node = TREE_CHAIN (node);
4012 continue;
4013 }
4014
4015 /* Remember that we had an initialization or finalization at this
4016 priority. */
4017 priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
4018 pi = get_priority_info (priority);
4019 if (initp)
4020 pi->initializations_p = 1;
4021 else
4022 pi->destructions_p = 1;
4023
4024 /* Conditionalize this initialization on being in the right priority
4025 and being initializing/finalizing appropriately. */
4026 priority_if_stmt = begin_if_stmt ();
4027 cond = cp_build_binary_op (input_location,
4028 EQ_EXPR,
4029 priority_decl,
4030 build_int_cst (NULL_TREE, priority),
4031 tf_warning_or_error);
4032 finish_if_stmt_cond (cond, priority_if_stmt);
4033
4034 /* Process initializers with same priority. */
4035 for (; node
4036 && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
4037 node = TREE_CHAIN (node))
4038 /* Do one initialization or destruction. */
4039 one_static_initialization_or_destruction (TREE_VALUE (node),
4040 TREE_PURPOSE (node), initp);
4041
4042 /* Finish up the priority if-stmt body. */
4043 finish_then_clause (priority_if_stmt);
4044 finish_if_stmt (priority_if_stmt);
4045
4046 } while (node);
4047
4048 /* Revert what __asan_before_dynamic_init did by calling
4049 __asan_after_dynamic_init. */
4050 if (initp && (flag_sanitize & SANITIZE_ADDRESS))
4051 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
4052
4053 /* Finish up the init/destruct if-stmt body. */
4054 finish_then_clause (init_if_stmt);
4055 finish_if_stmt (init_if_stmt);
4056 }
4057
4058 /* VARS is a list of variables with static storage duration which may
4059 need initialization and/or finalization. Remove those variables
4060 that don't really need to be initialized or finalized, and return
4061 the resulting list. The order in which the variables appear in
4062 VARS is in reverse order of the order in which they should actually
4063 be initialized. The list we return is in the unreversed order;
4064 i.e., the first variable should be initialized first. */
4065
4066 static tree
4067 prune_vars_needing_no_initialization (tree *vars)
4068 {
4069 tree *var = vars;
4070 tree result = NULL_TREE;
4071
4072 while (*var)
4073 {
4074 tree t = *var;
4075 tree decl = TREE_VALUE (t);
4076 tree init = TREE_PURPOSE (t);
4077
4078 /* Deal gracefully with error. */
4079 if (error_operand_p (decl))
4080 {
4081 var = &TREE_CHAIN (t);
4082 continue;
4083 }
4084
4085 /* The only things that can be initialized are variables. */
4086 gcc_assert (VAR_P (decl));
4087
4088 /* If this object is not defined, we don't need to do anything
4089 here. */
4090 if (DECL_EXTERNAL (decl))
4091 {
4092 var = &TREE_CHAIN (t);
4093 continue;
4094 }
4095
4096 /* Also, if the initializer already contains errors, we can bail
4097 out now. */
4098 if (init && TREE_CODE (init) == TREE_LIST
4099 && value_member (error_mark_node, init))
4100 {
4101 var = &TREE_CHAIN (t);
4102 continue;
4103 }
4104
4105 /* This variable is going to need initialization and/or
4106 finalization, so we add it to the list. */
4107 *var = TREE_CHAIN (t);
4108 TREE_CHAIN (t) = result;
4109 result = t;
4110 }
4111
4112 return result;
4113 }
4114
4115 /* Make sure we have told the back end about all the variables in
4116 VARS. */
4117
4118 static void
4119 write_out_vars (tree vars)
4120 {
4121 tree v;
4122
4123 for (v = vars; v; v = TREE_CHAIN (v))
4124 {
4125 tree var = TREE_VALUE (v);
4126 if (!var_finalized_p (var))
4127 {
4128 import_export_decl (var);
4129 rest_of_decl_compilation (var, 1, 1);
4130 }
4131 }
4132 }
4133
4134 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
4135 (otherwise) that will initialize all global objects with static
4136 storage duration having the indicated PRIORITY. */
4137
4138 static void
4139 generate_ctor_or_dtor_function (bool constructor_p, int priority,
4140 location_t *locus)
4141 {
4142 char function_key;
4143 tree fndecl;
4144 tree body;
4145 size_t i;
4146
4147 input_location = *locus;
4148 /* ??? */
4149 /* Was: locus->line++; */
4150
4151 /* We use `I' to indicate initialization and `D' to indicate
4152 destruction. */
4153 function_key = constructor_p ? 'I' : 'D';
4154
4155 /* We emit the function lazily, to avoid generating empty
4156 global constructors and destructors. */
4157 body = NULL_TREE;
4158
4159 /* For Objective-C++, we may need to initialize metadata found in this module.
4160 This must be done _before_ any other static initializations. */
4161 if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
4162 && constructor_p && objc_static_init_needed_p ())
4163 {
4164 body = start_objects (function_key, priority);
4165 objc_generate_static_init_call (NULL_TREE);
4166 }
4167
4168 /* Call the static storage duration function with appropriate
4169 arguments. */
4170 FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
4171 {
4172 /* Calls to pure or const functions will expand to nothing. */
4173 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
4174 {
4175 tree call;
4176
4177 if (! body)
4178 body = start_objects (function_key, priority);
4179
4180 call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
4181 build_int_cst (NULL_TREE,
4182 constructor_p),
4183 build_int_cst (NULL_TREE,
4184 priority),
4185 NULL_TREE);
4186 finish_expr_stmt (call);
4187 }
4188 }
4189
4190 /* Close out the function. */
4191 if (body)
4192 finish_objects (function_key, priority, body);
4193 }
4194
4195 /* Generate constructor and destructor functions for the priority
4196 indicated by N. */
4197
4198 static int
4199 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
4200 {
4201 location_t *locus = (location_t *) data;
4202 int priority = (int) n->key;
4203 priority_info pi = (priority_info) n->value;
4204
4205 /* Generate the functions themselves, but only if they are really
4206 needed. */
4207 if (pi->initializations_p)
4208 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
4209 if (pi->destructions_p)
4210 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
4211
4212 /* Keep iterating. */
4213 return 0;
4214 }
4215
4216 /* Return C++ property of T, based on given operation OP. */
4217
4218 static int
4219 cpp_check (tree t, cpp_operation op)
4220 {
4221 switch (op)
4222 {
4223 case HAS_DEPENDENT_TEMPLATE_ARGS:
4224 {
4225 tree ti = CLASSTYPE_TEMPLATE_INFO (t);
4226 if (!ti)
4227 return 0;
4228 ++processing_template_decl;
4229 const bool dep = any_dependent_template_arguments_p (TI_ARGS (ti));
4230 --processing_template_decl;
4231 return dep;
4232 }
4233 case IS_ABSTRACT:
4234 return DECL_PURE_VIRTUAL_P (t);
4235 case IS_CONSTRUCTOR:
4236 return DECL_CONSTRUCTOR_P (t);
4237 case IS_DESTRUCTOR:
4238 return DECL_DESTRUCTOR_P (t);
4239 case IS_COPY_CONSTRUCTOR:
4240 return DECL_COPY_CONSTRUCTOR_P (t);
4241 case IS_MOVE_CONSTRUCTOR:
4242 return DECL_MOVE_CONSTRUCTOR_P (t);
4243 case IS_TEMPLATE:
4244 return TREE_CODE (t) == TEMPLATE_DECL;
4245 case IS_TRIVIAL:
4246 return trivial_type_p (t);
4247 default:
4248 return 0;
4249 }
4250 }
4251
4252 /* Collect source file references recursively, starting from NAMESPC. */
4253
4254 static void
4255 collect_source_refs (tree namespc)
4256 {
4257 /* Iterate over names in this name space. */
4258 for (tree t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
4259 if (DECL_IS_BUILTIN (t))
4260 ;
4261 else if (TREE_CODE (t) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (t))
4262 collect_source_refs (t);
4263 else
4264 collect_source_ref (DECL_SOURCE_FILE (t));
4265 }
4266
4267 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
4268 starting from NAMESPC. */
4269
4270 static void
4271 collect_ada_namespace (tree namespc, const char *source_file)
4272 {
4273 tree decl = NAMESPACE_LEVEL (namespc)->names;
4274
4275 /* Collect decls from this namespace. This will skip
4276 NAMESPACE_DECLs (both aliases and regular, it cannot tell). */
4277 collect_ada_nodes (decl, source_file);
4278
4279 /* Now scan for namespace children, and dump them. */
4280 for (; decl; decl = TREE_CHAIN (decl))
4281 if (TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl))
4282 collect_ada_namespace (decl, source_file);
4283 }
4284
4285 /* Returns true iff there is a definition available for variable or
4286 function DECL. */
4287
4288 bool
4289 decl_defined_p (tree decl)
4290 {
4291 if (TREE_CODE (decl) == FUNCTION_DECL)
4292 return (DECL_INITIAL (decl) != NULL_TREE
4293 /* A pending instantiation of a friend temploid is defined. */
4294 || (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
4295 && DECL_INITIAL (DECL_TEMPLATE_RESULT
4296 (DECL_TI_TEMPLATE (decl)))));
4297 else
4298 {
4299 gcc_assert (VAR_P (decl));
4300 return !DECL_EXTERNAL (decl);
4301 }
4302 }
4303
4304 /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
4305
4306 [expr.const]
4307
4308 An integral constant-expression can only involve ... const
4309 variables of integral or enumeration types initialized with
4310 constant expressions ...
4311
4312 C++0x also allows constexpr variables and temporaries initialized
4313 with constant expressions. We handle the former here, but the latter
4314 are just folded away in cxx_eval_constant_expression.
4315
4316 The standard does not require that the expression be non-volatile.
4317 G++ implements the proposed correction in DR 457. */
4318
4319 bool
4320 decl_constant_var_p (tree decl)
4321 {
4322 if (!decl_maybe_constant_var_p (decl))
4323 return false;
4324
4325 /* We don't know if a template static data member is initialized with
4326 a constant expression until we instantiate its initializer. Even
4327 in the case of a constexpr variable, we can't treat it as a
4328 constant until its initializer is complete in case it's used in
4329 its own initializer. */
4330 maybe_instantiate_decl (decl);
4331 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
4332 }
4333
4334 /* Returns true if DECL could be a symbolic constant variable, depending on
4335 its initializer. */
4336
4337 bool
4338 decl_maybe_constant_var_p (tree decl)
4339 {
4340 tree type = TREE_TYPE (decl);
4341 if (!VAR_P (decl))
4342 return false;
4343 if (DECL_DECLARED_CONSTEXPR_P (decl) && !TREE_THIS_VOLATILE (decl))
4344 return true;
4345 if (DECL_HAS_VALUE_EXPR_P (decl))
4346 /* A proxy isn't constant. */
4347 return false;
4348 if (TYPE_REF_P (type))
4349 /* References can be constant. */;
4350 else if (CP_TYPE_CONST_NON_VOLATILE_P (type)
4351 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4352 /* And const integers. */;
4353 else
4354 return false;
4355
4356 if (DECL_INITIAL (decl)
4357 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
4358 /* We know the initializer, and it isn't constant. */
4359 return false;
4360 else
4361 return true;
4362 }
4363
4364 /* Complain that DECL uses a type with no linkage. In C++98 mode this is
4365 called from grokfndecl and grokvardecl; in all modes it is called from
4366 cp_write_global_declarations. */
4367
4368 void
4369 no_linkage_error (tree decl)
4370 {
4371 if (cxx_dialect >= cxx11 && decl_defined_p (decl))
4372 /* In C++11 it's ok if the decl is defined. */
4373 return;
4374 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
4375 if (t == NULL_TREE)
4376 /* The type that got us on no_linkage_decls must have gotten a name for
4377 linkage purposes. */;
4378 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
4379 /* The type might end up having a typedef name for linkage purposes. */
4380 vec_safe_push (no_linkage_decls, decl);
4381 else if (TYPE_UNNAMED_P (t))
4382 {
4383 bool d = false;
4384 auto_diagnostic_group grp;
4385 if (cxx_dialect >= cxx11)
4386 d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
4387 "unnamed type, is used but never defined", decl);
4388 else if (DECL_EXTERN_C_P (decl))
4389 /* Allow this; it's pretty common in C. */;
4390 else if (VAR_P (decl))
4391 /* DRs 132, 319 and 389 seem to indicate types with
4392 no linkage can only be used to declare extern "C"
4393 entities. Since it's not always an error in the
4394 ISO C++ 90 Standard, we only issue a warning. */
4395 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "unnamed type "
4396 "with no linkage used to declare variable %q#D with "
4397 "linkage", decl);
4398 else
4399 d = permerror (DECL_SOURCE_LOCATION (decl), "unnamed type with no "
4400 "linkage used to declare function %q#D with linkage",
4401 decl);
4402 if (d && is_typedef_decl (TYPE_NAME (t)))
4403 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
4404 "to the unqualified type, so it is not used for linkage",
4405 TYPE_NAME (t));
4406 }
4407 else if (cxx_dialect >= cxx11)
4408 {
4409 if (VAR_P (decl) || !DECL_PURE_VIRTUAL_P (decl))
4410 permerror (DECL_SOURCE_LOCATION (decl),
4411 "%q#D, declared using local type "
4412 "%qT, is used but never defined", decl, t);
4413 }
4414 else if (VAR_P (decl))
4415 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
4416 "used to declare variable %q#D with linkage", t, decl);
4417 else
4418 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
4419 "to declare function %q#D with linkage", t, decl);
4420 }
4421
4422 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
4423
4424 static void
4425 collect_all_refs (const char *source_file)
4426 {
4427 collect_ada_namespace (global_namespace, source_file);
4428 }
4429
4430 /* Clear DECL_EXTERNAL for NODE. */
4431
4432 static bool
4433 clear_decl_external (struct cgraph_node *node, void * /*data*/)
4434 {
4435 DECL_EXTERNAL (node->decl) = 0;
4436 return false;
4437 }
4438
4439 /* Build up the function to run dynamic initializers for thread_local
4440 variables in this translation unit and alias the init functions for the
4441 individual variables to it. */
4442
4443 static void
4444 handle_tls_init (void)
4445 {
4446 tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
4447 if (vars == NULL_TREE)
4448 return;
4449
4450 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
4451
4452 write_out_vars (vars);
4453
4454 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
4455 boolean_type_node);
4456 TREE_PUBLIC (guard) = false;
4457 TREE_STATIC (guard) = true;
4458 DECL_ARTIFICIAL (guard) = true;
4459 DECL_IGNORED_P (guard) = true;
4460 TREE_USED (guard) = true;
4461 CP_DECL_THREAD_LOCAL_P (guard) = true;
4462 set_decl_tls_model (guard, decl_default_tls_model (guard));
4463 pushdecl_top_level_and_finish (guard, NULL_TREE);
4464
4465 tree fn = get_local_tls_init_fn (loc);
4466 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
4467 tree body = begin_function_body ();
4468 tree if_stmt = begin_if_stmt ();
4469 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
4470 tf_warning_or_error);
4471 finish_if_stmt_cond (cond, if_stmt);
4472 finish_expr_stmt (cp_build_modify_expr (loc, guard, NOP_EXPR,
4473 boolean_true_node,
4474 tf_warning_or_error));
4475 for (; vars; vars = TREE_CHAIN (vars))
4476 {
4477 tree var = TREE_VALUE (vars);
4478 tree init = TREE_PURPOSE (vars);
4479 one_static_initialization_or_destruction (var, init, true);
4480
4481 /* Output init aliases even with -fno-extern-tls-init. */
4482 if (TARGET_SUPPORTS_ALIASES && TREE_PUBLIC (var))
4483 {
4484 tree single_init_fn = get_tls_init_fn (var);
4485 if (single_init_fn == NULL_TREE)
4486 continue;
4487 cgraph_node *alias
4488 = cgraph_node::get_create (fn)->create_same_body_alias
4489 (single_init_fn, fn);
4490 gcc_assert (alias != NULL);
4491 }
4492 }
4493
4494 finish_then_clause (if_stmt);
4495 finish_if_stmt (if_stmt);
4496 finish_function_body (body);
4497 expand_or_defer_fn (finish_function (/*inline_p=*/false));
4498 }
4499
4500 /* We're at the end of compilation, so generate any mangling aliases that
4501 we've been saving up, if DECL is going to be output and ID2 isn't
4502 already taken by another declaration. */
4503
4504 static void
4505 generate_mangling_alias (tree decl, tree id2)
4506 {
4507 struct cgraph_node *n = NULL;
4508
4509 if (TREE_CODE (decl) == FUNCTION_DECL)
4510 {
4511 n = cgraph_node::get (decl);
4512 if (!n)
4513 /* Don't create an alias to an unreferenced function. */
4514 return;
4515 }
4516
4517 tree *slot
4518 = mangled_decls->find_slot_with_hash (id2, IDENTIFIER_HASH_VALUE (id2),
4519 INSERT);
4520
4521 /* If there's a declaration already using this mangled name,
4522 don't create a compatibility alias that conflicts. */
4523 if (*slot)
4524 return;
4525
4526 tree alias = make_alias_for (decl, id2);
4527 *slot = alias;
4528
4529 DECL_IGNORED_P (alias) = 1;
4530 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
4531 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
4532 if (vague_linkage_p (decl))
4533 DECL_WEAK (alias) = 1;
4534
4535 if (n)
4536 n->create_same_body_alias (alias, decl);
4537 else
4538 varpool_node::create_extra_name_alias (alias, decl);
4539 }
4540
4541 /* Note that we might want to emit an alias with the symbol ID2 for DECL at
4542 the end of translation, for compatibility across bugs in the mangling
4543 implementation. */
4544
4545 void
4546 note_mangling_alias (tree decl, tree id2)
4547 {
4548 if (TARGET_SUPPORTS_ALIASES)
4549 {
4550 if (!defer_mangling_aliases)
4551 generate_mangling_alias (decl, id2);
4552 else
4553 {
4554 vec_safe_push (mangling_aliases, decl);
4555 vec_safe_push (mangling_aliases, id2);
4556 }
4557 }
4558 }
4559
4560 /* Emit all mangling aliases that were deferred up to this point. */
4561
4562 void
4563 generate_mangling_aliases ()
4564 {
4565 while (!vec_safe_is_empty (mangling_aliases))
4566 {
4567 tree id2 = mangling_aliases->pop();
4568 tree decl = mangling_aliases->pop();
4569 generate_mangling_alias (decl, id2);
4570 }
4571 defer_mangling_aliases = false;
4572 }
4573
4574 /* Record a mangling of DECL, whose DECL_ASSEMBLER_NAME has just been
4575 set. NEED_WARNING is true if we must warn about collisions. We do
4576 this to spot changes in mangling that may require compatibility
4577 aliases. */
4578
4579 void
4580 record_mangling (tree decl, bool need_warning)
4581 {
4582 if (!mangled_decls)
4583 mangled_decls = hash_table<mangled_decl_hash>::create_ggc (499);
4584
4585 gcc_checking_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
4586 tree id = DECL_ASSEMBLER_NAME_RAW (decl);
4587 tree *slot
4588 = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id),
4589 INSERT);
4590
4591 /* If this is already an alias, remove the alias, because the real
4592 decl takes precedence. */
4593 if (*slot && DECL_ARTIFICIAL (*slot) && DECL_IGNORED_P (*slot))
4594 if (symtab_node *n = symtab_node::get (*slot))
4595 if (n->cpp_implicit_alias)
4596 {
4597 n->remove ();
4598 *slot = NULL_TREE;
4599 }
4600
4601 if (!*slot)
4602 *slot = decl;
4603 else if (need_warning)
4604 {
4605 error_at (DECL_SOURCE_LOCATION (decl),
4606 "mangling of %q#D as %qE conflicts with a previous mangle",
4607 decl, id);
4608 inform (DECL_SOURCE_LOCATION (*slot),
4609 "previous mangling %q#D", *slot);
4610 inform (DECL_SOURCE_LOCATION (decl),
4611 "a later %<-fabi-version=%> (or =0)"
4612 " avoids this error with a change in mangling");
4613 *slot = decl;
4614 }
4615 }
4616
4617 /* The mangled name of DECL is being forcibly changed to NAME. Remove
4618 any existing knowledge of DECL's mangled name meaning DECL. */
4619
4620 void
4621 overwrite_mangling (tree decl, tree name)
4622 {
4623 if (tree id = DECL_ASSEMBLER_NAME_RAW (decl))
4624 if ((TREE_CODE (decl) == VAR_DECL
4625 || TREE_CODE (decl) == FUNCTION_DECL)
4626 && mangled_decls)
4627 if (tree *slot
4628 = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id),
4629 NO_INSERT))
4630 if (*slot == decl)
4631 {
4632 mangled_decls->clear_slot (slot);
4633
4634 /* If this is an alias, remove it from the symbol table. */
4635 if (DECL_ARTIFICIAL (decl) && DECL_IGNORED_P (decl))
4636 if (symtab_node *n = symtab_node::get (decl))
4637 if (n->cpp_implicit_alias)
4638 n->remove ();
4639 }
4640
4641 DECL_ASSEMBLER_NAME_RAW (decl) = name;
4642 }
4643
4644 /* The entire file is now complete. If requested, dump everything
4645 to a file. */
4646
4647 static void
4648 dump_tu (void)
4649 {
4650 dump_flags_t flags;
4651 if (FILE *stream = dump_begin (raw_dump_id, &flags))
4652 {
4653 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4654 dump_end (raw_dump_id, stream);
4655 }
4656 }
4657
4658 static location_t locus_at_end_of_parsing;
4659
4660 /* Check the deallocation functions for CODE to see if we want to warn that
4661 only one was defined. */
4662
4663 static void
4664 maybe_warn_sized_delete (enum tree_code code)
4665 {
4666 tree sized = NULL_TREE;
4667 tree unsized = NULL_TREE;
4668
4669 for (ovl_iterator iter (get_global_binding (ovl_op_identifier (false, code)));
4670 iter; ++iter)
4671 {
4672 tree fn = *iter;
4673 /* We're only interested in usual deallocation functions. */
4674 if (!usual_deallocation_fn_p (fn))
4675 continue;
4676 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
4677 unsized = fn;
4678 else
4679 sized = fn;
4680 }
4681 if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
4682 warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
4683 "the program should also define %qD", sized);
4684 else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
4685 warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
4686 "the program should also define %qD", unsized);
4687 }
4688
4689 /* Check the global deallocation functions to see if we want to warn about
4690 defining unsized without sized (or vice versa). */
4691
4692 static void
4693 maybe_warn_sized_delete ()
4694 {
4695 if (!flag_sized_deallocation || !warn_sized_deallocation)
4696 return;
4697 maybe_warn_sized_delete (DELETE_EXPR);
4698 maybe_warn_sized_delete (VEC_DELETE_EXPR);
4699 }
4700
4701 /* Earlier we left PTRMEM_CST in variable initializers alone so that we could
4702 look them up when evaluating non-type template parameters. Now we need to
4703 lower them to something the back end can understand. */
4704
4705 static void
4706 lower_var_init ()
4707 {
4708 varpool_node *node;
4709 FOR_EACH_VARIABLE (node)
4710 {
4711 tree d = node->decl;
4712 if (tree init = DECL_INITIAL (d))
4713 DECL_INITIAL (d) = cplus_expand_constant (init);
4714 }
4715 }
4716
4717 /* This routine is called at the end of compilation.
4718 Its job is to create all the code needed to initialize and
4719 destroy the global aggregates. We do the destruction
4720 first, since that way we only need to reverse the decls once. */
4721
4722 void
4723 c_parse_final_cleanups (void)
4724 {
4725 tree vars;
4726 bool reconsider;
4727 size_t i;
4728 unsigned ssdf_count = 0;
4729 int retries = 0;
4730 tree decl;
4731
4732 locus_at_end_of_parsing = input_location;
4733 at_eof = 1;
4734
4735 /* Bad parse errors. Just forget about it. */
4736 if (! global_bindings_p () || current_class_type
4737 || !vec_safe_is_empty (decl_namespace_list))
4738 return;
4739
4740 /* This is the point to write out a PCH if we're doing that.
4741 In that case we do not want to do anything else. */
4742 if (pch_file)
4743 {
4744 /* Mangle all symbols at PCH creation time. */
4745 symtab_node *node;
4746 FOR_EACH_SYMBOL (node)
4747 if (! is_a <varpool_node *> (node)
4748 || ! DECL_HARD_REGISTER (node->decl))
4749 DECL_ASSEMBLER_NAME (node->decl);
4750 c_common_write_pch ();
4751 dump_tu ();
4752 /* Ensure even the callers don't try to finalize the CU. */
4753 flag_syntax_only = 1;
4754 return;
4755 }
4756
4757 timevar_stop (TV_PHASE_PARSING);
4758 timevar_start (TV_PHASE_DEFERRED);
4759
4760 symtab->process_same_body_aliases ();
4761
4762 /* Handle -fdump-ada-spec[-slim] */
4763 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
4764 {
4765 if (flag_dump_ada_spec_slim)
4766 collect_source_ref (main_input_filename);
4767 else
4768 collect_source_refs (global_namespace);
4769
4770 dump_ada_specs (collect_all_refs, cpp_check);
4771 }
4772
4773 /* FIXME - huh? was input_line -= 1;*/
4774
4775 /* We now have to write out all the stuff we put off writing out.
4776 These include:
4777
4778 o Template specializations that we have not yet instantiated,
4779 but which are needed.
4780 o Initialization and destruction for non-local objects with
4781 static storage duration. (Local objects with static storage
4782 duration are initialized when their scope is first entered,
4783 and are cleaned up via atexit.)
4784 o Virtual function tables.
4785
4786 All of these may cause others to be needed. For example,
4787 instantiating one function may cause another to be needed, and
4788 generating the initializer for an object may cause templates to be
4789 instantiated, etc., etc. */
4790
4791 emit_support_tinfos ();
4792
4793 do
4794 {
4795 tree t;
4796 tree decl;
4797
4798 reconsider = false;
4799
4800 /* If there are templates that we've put off instantiating, do
4801 them now. */
4802 instantiate_pending_templates (retries);
4803 ggc_collect ();
4804
4805 /* Write out virtual tables as required. Writing out the
4806 virtual table for a template class may cause the
4807 instantiation of members of that class. If we write out
4808 vtables then we remove the class from our list so we don't
4809 have to look at it again. */
4810 for (i = keyed_classes->length ();
4811 keyed_classes->iterate (--i, &t);)
4812 if (maybe_emit_vtables (t))
4813 {
4814 reconsider = true;
4815 keyed_classes->unordered_remove (i);
4816 }
4817 /* The input_location may have been changed during marking of
4818 vtable entries. */
4819 input_location = locus_at_end_of_parsing;
4820
4821 /* Write out needed type info variables. We have to be careful
4822 looping through unemitted decls, because emit_tinfo_decl may
4823 cause other variables to be needed. New elements will be
4824 appended, and we remove from the vector those that actually
4825 get emitted. */
4826 for (i = unemitted_tinfo_decls->length ();
4827 unemitted_tinfo_decls->iterate (--i, &t);)
4828 if (emit_tinfo_decl (t))
4829 {
4830 reconsider = true;
4831 unemitted_tinfo_decls->unordered_remove (i);
4832 }
4833
4834 /* The list of objects with static storage duration is built up
4835 in reverse order. We clear STATIC_AGGREGATES so that any new
4836 aggregates added during the initialization of these will be
4837 initialized in the correct order when we next come around the
4838 loop. */
4839 vars = prune_vars_needing_no_initialization (&static_aggregates);
4840
4841 if (vars)
4842 {
4843 /* We need to start a new initialization function each time
4844 through the loop. That's because we need to know which
4845 vtables have been referenced, and TREE_SYMBOL_REFERENCED
4846 isn't computed until a function is finished, and written
4847 out. That's a deficiency in the back end. When this is
4848 fixed, these initialization functions could all become
4849 inline, with resulting performance improvements. */
4850 tree ssdf_body;
4851
4852 /* Make sure the back end knows about all the variables. */
4853 write_out_vars (vars);
4854
4855 /* Set the line and file, so that it is obviously not from
4856 the source file. */
4857 input_location = locus_at_end_of_parsing;
4858 ssdf_body = start_static_storage_duration_function (ssdf_count);
4859
4860 /* First generate code to do all the initializations. */
4861 if (vars)
4862 do_static_initialization_or_destruction (vars, /*initp=*/true);
4863
4864 /* Then, generate code to do all the destructions. Do these
4865 in reverse order so that the most recently constructed
4866 variable is the first destroyed. If we're using
4867 __cxa_atexit, then we don't need to do this; functions
4868 were registered at initialization time to destroy the
4869 local statics. */
4870 if (!flag_use_cxa_atexit && vars)
4871 {
4872 vars = nreverse (vars);
4873 do_static_initialization_or_destruction (vars, /*initp=*/false);
4874 }
4875 else
4876 vars = NULL_TREE;
4877
4878 /* Finish up the static storage duration function for this
4879 round. */
4880 input_location = locus_at_end_of_parsing;
4881 finish_static_storage_duration_function (ssdf_body);
4882
4883 /* All those initializations and finalizations might cause
4884 us to need more inline functions, more template
4885 instantiations, etc. */
4886 reconsider = true;
4887 ssdf_count++;
4888 /* ??? was: locus_at_end_of_parsing.line++; */
4889 }
4890
4891 /* Now do the same for thread_local variables. */
4892 handle_tls_init ();
4893
4894 /* Go through the set of inline functions whose bodies have not
4895 been emitted yet. If out-of-line copies of these functions
4896 are required, emit them. */
4897 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
4898 {
4899 /* Does it need synthesizing? */
4900 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
4901 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
4902 {
4903 /* Even though we're already at the top-level, we push
4904 there again. That way, when we pop back a few lines
4905 hence, all of our state is restored. Otherwise,
4906 finish_function doesn't clean things up, and we end
4907 up with CURRENT_FUNCTION_DECL set. */
4908 push_to_top_level ();
4909 /* The decl's location will mark where it was first
4910 needed. Save that so synthesize method can indicate
4911 where it was needed from, in case of error */
4912 input_location = DECL_SOURCE_LOCATION (decl);
4913 synthesize_method (decl);
4914 pop_from_top_level ();
4915 reconsider = true;
4916 }
4917
4918 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
4919 generate_tls_wrapper (decl);
4920
4921 if (!DECL_SAVED_TREE (decl))
4922 continue;
4923
4924 cgraph_node *node = cgraph_node::get_create (decl);
4925
4926 /* We lie to the back end, pretending that some functions
4927 are not defined when they really are. This keeps these
4928 functions from being put out unnecessarily. But, we must
4929 stop lying when the functions are referenced, or if they
4930 are not comdat since they need to be put out now. If
4931 DECL_INTERFACE_KNOWN, then we have already set
4932 DECL_EXTERNAL appropriately, so there's no need to check
4933 again, and we do not want to clear DECL_EXTERNAL if a
4934 previous call to import_export_decl set it.
4935
4936 This is done in a separate for cycle, because if some
4937 deferred function is contained in another deferred
4938 function later in deferred_fns varray,
4939 rest_of_compilation would skip this function and we
4940 really cannot expand the same function twice. */
4941 import_export_decl (decl);
4942 if (DECL_NOT_REALLY_EXTERN (decl)
4943 && DECL_INITIAL (decl)
4944 && decl_needed_p (decl))
4945 {
4946 if (node->cpp_implicit_alias)
4947 node = node->get_alias_target ();
4948
4949 node->call_for_symbol_thunks_and_aliases (clear_decl_external,
4950 NULL, true);
4951 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
4952 group, we need to mark all symbols in the same comdat group
4953 that way. */
4954 if (node->same_comdat_group)
4955 for (cgraph_node *next
4956 = dyn_cast<cgraph_node *> (node->same_comdat_group);
4957 next != node;
4958 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
4959 next->call_for_symbol_thunks_and_aliases (clear_decl_external,
4960 NULL, true);
4961 }
4962
4963 /* If we're going to need to write this function out, and
4964 there's already a body for it, create RTL for it now.
4965 (There might be no body if this is a method we haven't
4966 gotten around to synthesizing yet.) */
4967 if (!DECL_EXTERNAL (decl)
4968 && decl_needed_p (decl)
4969 && !TREE_ASM_WRITTEN (decl)
4970 && !node->definition)
4971 {
4972 /* We will output the function; no longer consider it in this
4973 loop. */
4974 DECL_DEFER_OUTPUT (decl) = 0;
4975 /* Generate RTL for this function now that we know we
4976 need it. */
4977 expand_or_defer_fn (decl);
4978 reconsider = true;
4979 }
4980 }
4981
4982 if (wrapup_namespace_globals ())
4983 reconsider = true;
4984
4985 /* Static data members are just like namespace-scope globals. */
4986 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
4987 {
4988 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
4989 /* Don't write it out if we haven't seen a definition. */
4990 || DECL_IN_AGGR_P (decl))
4991 continue;
4992 import_export_decl (decl);
4993 /* If this static data member is needed, provide it to the
4994 back end. */
4995 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
4996 DECL_EXTERNAL (decl) = 0;
4997 }
4998 if (vec_safe_length (pending_statics) != 0
4999 && wrapup_global_declarations (pending_statics->address (),
5000 pending_statics->length ()))
5001 reconsider = true;
5002
5003 retries++;
5004 }
5005 while (reconsider);
5006
5007 lower_var_init ();
5008
5009 generate_mangling_aliases ();
5010
5011 /* All used inline functions must have a definition at this point. */
5012 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
5013 {
5014 if (/* Check online inline functions that were actually used. */
5015 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
5016 /* If the definition actually was available here, then the
5017 fact that the function was not defined merely represents
5018 that for some reason (use of a template repository,
5019 #pragma interface, etc.) we decided not to emit the
5020 definition here. */
5021 && !DECL_INITIAL (decl)
5022 /* Don't complain if the template was defined. */
5023 && !(DECL_TEMPLATE_INSTANTIATION (decl)
5024 && DECL_INITIAL (DECL_TEMPLATE_RESULT
5025 (template_for_substitution (decl))))
5026 && warning_at (DECL_SOURCE_LOCATION (decl), 0,
5027 "inline function %qD used but never defined", decl))
5028 /* Avoid a duplicate warning from check_global_declaration. */
5029 TREE_NO_WARNING (decl) = 1;
5030 }
5031
5032 /* So must decls that use a type with no linkage. */
5033 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
5034 no_linkage_error (decl);
5035
5036 maybe_warn_sized_delete ();
5037
5038 /* Then, do the Objective-C stuff. This is where all the
5039 Objective-C module stuff gets generated (symtab,
5040 class/protocol/selector lists etc). This must be done after C++
5041 templates, destructors etc. so that selectors used in C++
5042 templates are properly allocated. */
5043 if (c_dialect_objc ())
5044 objc_write_global_declarations ();
5045
5046 /* We give C linkage to static constructors and destructors. */
5047 push_lang_context (lang_name_c);
5048
5049 /* Generate initialization and destruction functions for all
5050 priorities for which they are required. */
5051 if (priority_info_map)
5052 splay_tree_foreach (priority_info_map,
5053 generate_ctor_and_dtor_functions_for_priority,
5054 /*data=*/&locus_at_end_of_parsing);
5055 else if (c_dialect_objc () && objc_static_init_needed_p ())
5056 /* If this is obj-c++ and we need a static init, call
5057 generate_ctor_or_dtor_function. */
5058 generate_ctor_or_dtor_function (/*constructor_p=*/true,
5059 DEFAULT_INIT_PRIORITY,
5060 &locus_at_end_of_parsing);
5061
5062 /* We're done with the splay-tree now. */
5063 if (priority_info_map)
5064 splay_tree_delete (priority_info_map);
5065
5066 /* Generate any missing aliases. */
5067 maybe_apply_pending_pragma_weaks ();
5068
5069 /* We're done with static constructors, so we can go back to "C++"
5070 linkage now. */
5071 pop_lang_context ();
5072
5073 if (flag_vtable_verify)
5074 {
5075 vtv_recover_class_info ();
5076 vtv_compute_class_hierarchy_transitive_closure ();
5077 vtv_build_vtable_verify_fndecl ();
5078 }
5079
5080 perform_deferred_noexcept_checks ();
5081
5082 finish_repo ();
5083 fini_constexpr ();
5084
5085 /* The entire file is now complete. If requested, dump everything
5086 to a file. */
5087 dump_tu ();
5088
5089 if (flag_detailed_statistics)
5090 {
5091 dump_tree_statistics ();
5092 dump_time_statistics ();
5093 }
5094
5095 timevar_stop (TV_PHASE_DEFERRED);
5096 timevar_start (TV_PHASE_PARSING);
5097
5098 /* Indicate that we're done with front end processing. */
5099 at_eof = 2;
5100 }
5101
5102 /* Perform any post compilation-proper cleanups for the C++ front-end.
5103 This should really go away. No front-end should need to do
5104 anything past the compilation process. */
5105
5106 void
5107 cxx_post_compilation_parsing_cleanups (void)
5108 {
5109 timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS);
5110
5111 if (flag_vtable_verify)
5112 {
5113 /* Generate the special constructor initialization function that
5114 calls __VLTRegisterPairs, and give it a very high
5115 initialization priority. This must be done after
5116 finalize_compilation_unit so that we have accurate
5117 information about which vtable will actually be emitted. */
5118 vtv_generate_init_routine ();
5119 }
5120
5121 input_location = locus_at_end_of_parsing;
5122
5123 if (flag_checking)
5124 validate_conversion_obstack ();
5125
5126 timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS);
5127 }
5128
5129 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
5130 function to call in parse-tree form; it has not yet been
5131 semantically analyzed. ARGS are the arguments to the function.
5132 They have already been semantically analyzed. This may change
5133 ARGS. */
5134
5135 tree
5136 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
5137 tsubst_flags_t complain)
5138 {
5139 tree orig_fn;
5140 vec<tree, va_gc> *orig_args = NULL;
5141 tree expr;
5142 tree object;
5143
5144 orig_fn = fn;
5145 object = TREE_OPERAND (fn, 0);
5146
5147 if (processing_template_decl)
5148 {
5149 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
5150 || TREE_CODE (fn) == MEMBER_REF);
5151 if (type_dependent_expression_p (fn)
5152 || any_type_dependent_arguments_p (*args))
5153 return build_min_nt_call_vec (fn, *args);
5154
5155 orig_args = make_tree_vector_copy (*args);
5156
5157 /* Transform the arguments and add the implicit "this"
5158 parameter. That must be done before the FN is transformed
5159 because we depend on the form of FN. */
5160 make_args_non_dependent (*args);
5161 object = build_non_dependent_expr (object);
5162 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5163 {
5164 if (TREE_CODE (fn) == DOTSTAR_EXPR)
5165 object = cp_build_addr_expr (object, complain);
5166 vec_safe_insert (*args, 0, object);
5167 }
5168 /* Now that the arguments are done, transform FN. */
5169 fn = build_non_dependent_expr (fn);
5170 }
5171
5172 /* A qualified name corresponding to a bound pointer-to-member is
5173 represented as an OFFSET_REF:
5174
5175 struct B { void g(); };
5176 void (B::*p)();
5177 void B::g() { (this->*p)(); } */
5178 if (TREE_CODE (fn) == OFFSET_REF)
5179 {
5180 tree object_addr = cp_build_addr_expr (object, complain);
5181 fn = TREE_OPERAND (fn, 1);
5182 fn = get_member_function_from_ptrfunc (&object_addr, fn,
5183 complain);
5184 vec_safe_insert (*args, 0, object_addr);
5185 }
5186
5187 if (CLASS_TYPE_P (TREE_TYPE (fn)))
5188 expr = build_op_call (fn, args, complain);
5189 else
5190 expr = cp_build_function_call_vec (fn, args, complain);
5191 if (processing_template_decl && expr != error_mark_node)
5192 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
5193
5194 if (orig_args != NULL)
5195 release_tree_vector (orig_args);
5196
5197 return expr;
5198 }
5199
5200
5201 void
5202 check_default_args (tree x)
5203 {
5204 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
5205 bool saw_def = false;
5206 bool noted_first_def = false;
5207 int idx_of_first_default_arg = 0;
5208 location_t loc_of_first_default_arg = UNKNOWN_LOCATION;
5209 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
5210 tree fndecl = STRIP_TEMPLATE (x);
5211 auto_diagnostic_group d;
5212 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
5213 {
5214 if (TREE_PURPOSE (arg))
5215 {
5216 if (!saw_def)
5217 {
5218 saw_def = true;
5219 idx_of_first_default_arg = i;
5220 location_t loc = get_fndecl_argument_location (fndecl, i);
5221 if (loc != DECL_SOURCE_LOCATION (x))
5222 loc_of_first_default_arg = loc;
5223 }
5224 }
5225 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
5226 {
5227 error_at (get_fndecl_argument_location (fndecl, i),
5228 "default argument missing for parameter %P of %q#D", i, x);
5229 if (loc_of_first_default_arg != UNKNOWN_LOCATION
5230 && !noted_first_def)
5231 {
5232 inform (loc_of_first_default_arg,
5233 "...following parameter %P which has a default argument",
5234 idx_of_first_default_arg);
5235 noted_first_def = true;
5236 }
5237 TREE_PURPOSE (arg) = error_mark_node;
5238 }
5239 }
5240 }
5241
5242 /* Return true if function DECL can be inlined. This is used to force
5243 instantiation of methods that might be interesting for inlining. */
5244 bool
5245 possibly_inlined_p (tree decl)
5246 {
5247 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
5248 if (DECL_UNINLINABLE (decl))
5249 return false;
5250 if (!optimize)
5251 return DECL_DECLARED_INLINE_P (decl);
5252 /* When optimizing, we might inline everything when flatten
5253 attribute or heuristics inlining for size or autoinlining
5254 is used. */
5255 return true;
5256 }
5257
5258 /* Normally, we can wait until instantiation-time to synthesize DECL.
5259 However, if DECL is a static data member initialized with a constant
5260 or a constexpr function, we need it right now because a reference to
5261 such a data member or a call to such function is not value-dependent.
5262 For a function that uses auto in the return type, we need to instantiate
5263 it to find out its type. For OpenMP user defined reductions, we need
5264 them instantiated for reduction clauses which inline them by hand
5265 directly. */
5266
5267 static void
5268 maybe_instantiate_decl (tree decl)
5269 {
5270 if (DECL_LANG_SPECIFIC (decl)
5271 && DECL_TEMPLATE_INFO (decl)
5272 && (decl_maybe_constant_var_p (decl)
5273 || (TREE_CODE (decl) == FUNCTION_DECL
5274 && DECL_OMP_DECLARE_REDUCTION_P (decl))
5275 || undeduced_auto_decl (decl))
5276 && !DECL_DECLARED_CONCEPT_P (decl)
5277 && !uses_template_parms (DECL_TI_ARGS (decl)))
5278 {
5279 /* Instantiating a function will result in garbage collection. We
5280 must treat this situation as if we were within the body of a
5281 function so as to avoid collecting live data only referenced from
5282 the stack (such as overload resolution candidates). */
5283 ++function_depth;
5284 instantiate_decl (decl, /*defer_ok=*/false,
5285 /*expl_inst_class_mem_p=*/false);
5286 --function_depth;
5287 }
5288 }
5289
5290 /* Maybe warn if DECL is deprecated, subject to COMPLAIN. Returns whether or
5291 not a warning was emitted. */
5292
5293 bool
5294 cp_warn_deprecated_use (tree decl, tsubst_flags_t complain)
5295 {
5296 if (!(complain & tf_warning) || !decl
5297 || deprecated_state == DEPRECATED_SUPPRESS)
5298 return false;
5299
5300 if (!TREE_DEPRECATED (decl))
5301 {
5302 /* Perhaps this is a deprecated typedef. */
5303 if (TYPE_P (decl) && TYPE_NAME (decl))
5304 decl = TYPE_NAME (decl);
5305
5306 if (!TREE_DEPRECATED (decl))
5307 return false;
5308 }
5309
5310 /* Don't warn within members of a deprecated type. */
5311 if (TYPE_P (decl)
5312 && currently_open_class (decl))
5313 return false;
5314
5315 bool warned = false;
5316 if (cxx_dialect >= cxx11
5317 && DECL_P (decl)
5318 && DECL_ARTIFICIAL (decl)
5319 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5320 && copy_fn_p (decl))
5321 {
5322 if (warn_deprecated_copy
5323 /* Don't warn about system library classes (c++/86342). */
5324 && (!DECL_IN_SYSTEM_HEADER (decl)
5325 || global_dc->dc_warn_system_headers))
5326 {
5327 auto_diagnostic_group d;
5328 tree ctx = DECL_CONTEXT (decl);
5329 tree other = classtype_has_depr_implicit_copy (ctx);
5330 int opt = (DECL_DESTRUCTOR_P (other)
5331 ? OPT_Wdeprecated_copy_dtor
5332 : OPT_Wdeprecated_copy);
5333 warned = warning (opt, "implicitly-declared %qD is deprecated",
5334 decl);
5335 if (warned)
5336 inform (DECL_SOURCE_LOCATION (other),
5337 "because %qT has user-provided %qD",
5338 ctx, other);
5339 }
5340 }
5341 else
5342 warned = warn_deprecated_use (decl, NULL_TREE);
5343
5344 return warned;
5345 }
5346
5347 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
5348 If DECL is a specialization or implicitly declared class member,
5349 generate the actual definition. Return false if something goes
5350 wrong, true otherwise. */
5351
5352 bool
5353 mark_used (tree decl, tsubst_flags_t complain)
5354 {
5355 /* If we're just testing conversions or resolving overloads, we
5356 don't want any permanent effects like forcing functions to be
5357 output or instantiating templates. */
5358 if ((complain & tf_conv))
5359 return true;
5360
5361 /* If DECL is a BASELINK for a single function, then treat it just
5362 like the DECL for the function. Otherwise, if the BASELINK is
5363 for an overloaded function, we don't know which function was
5364 actually used until after overload resolution. */
5365 if (BASELINK_P (decl))
5366 {
5367 decl = BASELINK_FUNCTIONS (decl);
5368 if (really_overloaded_fn (decl))
5369 return true;
5370 decl = OVL_FIRST (decl);
5371 }
5372
5373 /* Set TREE_USED for the benefit of -Wunused. */
5374 TREE_USED (decl) = 1;
5375 /* And for structured bindings also the underlying decl. */
5376 if (DECL_DECOMPOSITION_P (decl) && DECL_DECOMP_BASE (decl))
5377 TREE_USED (DECL_DECOMP_BASE (decl)) = 1;
5378
5379 if (TREE_CODE (decl) == TEMPLATE_DECL)
5380 return true;
5381
5382 if (DECL_CLONED_FUNCTION_P (decl))
5383 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5384
5385 /* Mark enumeration types as used. */
5386 if (TREE_CODE (decl) == CONST_DECL)
5387 used_types_insert (DECL_CONTEXT (decl));
5388
5389 if (TREE_CODE (decl) == FUNCTION_DECL
5390 && !maybe_instantiate_noexcept (decl, complain))
5391 return false;
5392
5393 if (TREE_CODE (decl) == FUNCTION_DECL
5394 && DECL_DELETED_FN (decl))
5395 {
5396 if (DECL_ARTIFICIAL (decl)
5397 && DECL_CONV_FN_P (decl)
5398 && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
5399 /* We mark a lambda conversion op as deleted if we can't
5400 generate it properly; see maybe_add_lambda_conv_op. */
5401 sorry ("converting lambda that uses %<...%> to function pointer");
5402 else if (complain & tf_error)
5403 {
5404 error ("use of deleted function %qD", decl);
5405 if (!maybe_explain_implicit_delete (decl))
5406 inform (DECL_SOURCE_LOCATION (decl), "declared here");
5407 }
5408 return false;
5409 }
5410
5411 cp_warn_deprecated_use (decl, complain);
5412
5413 /* We can only check DECL_ODR_USED on variables or functions with
5414 DECL_LANG_SPECIFIC set, and these are also the only decls that we
5415 might need special handling for. */
5416 if (!VAR_OR_FUNCTION_DECL_P (decl)
5417 || DECL_LANG_SPECIFIC (decl) == NULL
5418 || DECL_THUNK_P (decl))
5419 {
5420 if (!processing_template_decl
5421 && !require_deduced_type (decl, complain))
5422 return false;
5423 return true;
5424 }
5425
5426 /* We only want to do this processing once. We don't need to keep trying
5427 to instantiate inline templates, because unit-at-a-time will make sure
5428 we get them compiled before functions that want to inline them. */
5429 if (DECL_ODR_USED (decl))
5430 return true;
5431
5432 /* Normally, we can wait until instantiation-time to synthesize DECL.
5433 However, if DECL is a static data member initialized with a constant
5434 or a constexpr function, we need it right now because a reference to
5435 such a data member or a call to such function is not value-dependent.
5436 For a function that uses auto in the return type, we need to instantiate
5437 it to find out its type. For OpenMP user defined reductions, we need
5438 them instantiated for reduction clauses which inline them by hand
5439 directly. */
5440 maybe_instantiate_decl (decl);
5441
5442 if (processing_template_decl || in_template_function ())
5443 return true;
5444
5445 /* Check this too in case we're within instantiate_non_dependent_expr. */
5446 if (DECL_TEMPLATE_INFO (decl)
5447 && uses_template_parms (DECL_TI_ARGS (decl)))
5448 return true;
5449
5450 if (!require_deduced_type (decl, complain))
5451 return false;
5452
5453 if (builtin_pack_fn_p (decl))
5454 {
5455 error ("use of built-in parameter pack %qD outside of a template",
5456 DECL_NAME (decl));
5457 return false;
5458 }
5459
5460 /* If we don't need a value, then we don't need to synthesize DECL. */
5461 if (cp_unevaluated_operand || in_discarded_stmt)
5462 return true;
5463
5464 DECL_ODR_USED (decl) = 1;
5465 if (DECL_CLONED_FUNCTION_P (decl))
5466 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
5467
5468 /* DR 757: A type without linkage shall not be used as the type of a
5469 variable or function with linkage, unless
5470 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
5471 o the variable or function is not used (3.2 [basic.def.odr]) or is
5472 defined in the same translation unit. */
5473 if (cxx_dialect > cxx98
5474 && decl_linkage (decl) != lk_none
5475 && !DECL_EXTERN_C_P (decl)
5476 && !DECL_ARTIFICIAL (decl)
5477 && !decl_defined_p (decl)
5478 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
5479 {
5480 if (is_local_extern (decl))
5481 /* There's no way to define a local extern, and adding it to
5482 the vector interferes with GC, so give an error now. */
5483 no_linkage_error (decl);
5484 else
5485 vec_safe_push (no_linkage_decls, decl);
5486 }
5487
5488 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
5489 && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
5490 /* Remember it, so we can check it was defined. */
5491 note_vague_linkage_fn (decl);
5492
5493 /* Is it a synthesized method that needs to be synthesized? */
5494 if (TREE_CODE (decl) == FUNCTION_DECL
5495 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5496 && DECL_DEFAULTED_FN (decl)
5497 /* A function defaulted outside the class is synthesized either by
5498 cp_finish_decl or instantiate_decl. */
5499 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
5500 && ! DECL_INITIAL (decl))
5501 {
5502 /* Defer virtual destructors so that thunks get the right
5503 linkage. */
5504 if (DECL_VIRTUAL_P (decl) && !at_eof)
5505 {
5506 note_vague_linkage_fn (decl);
5507 return true;
5508 }
5509
5510 /* Remember the current location for a function we will end up
5511 synthesizing. Then we can inform the user where it was
5512 required in the case of error. */
5513 DECL_SOURCE_LOCATION (decl) = input_location;
5514
5515 /* Synthesizing an implicitly defined member function will result in
5516 garbage collection. We must treat this situation as if we were
5517 within the body of a function so as to avoid collecting live data
5518 on the stack (such as overload resolution candidates).
5519
5520 We could just let c_parse_final_cleanups handle synthesizing
5521 this function by adding it to deferred_fns, but doing
5522 it at the use site produces better error messages. */
5523 ++function_depth;
5524 synthesize_method (decl);
5525 --function_depth;
5526 /* If this is a synthesized method we don't need to
5527 do the instantiation test below. */
5528 }
5529 else if (VAR_OR_FUNCTION_DECL_P (decl)
5530 && DECL_TEMPLATE_INFO (decl)
5531 && !DECL_DECLARED_CONCEPT_P (decl)
5532 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5533 || always_instantiate_p (decl)))
5534 /* If this is a function or variable that is an instance of some
5535 template, we now know that we will need to actually do the
5536 instantiation. We check that DECL is not an explicit
5537 instantiation because that is not checked in instantiate_decl.
5538
5539 We put off instantiating functions in order to improve compile
5540 times. Maintaining a stack of active functions is expensive,
5541 and the inliner knows to instantiate any functions it might
5542 need. Therefore, we always try to defer instantiation. */
5543 {
5544 ++function_depth;
5545 instantiate_decl (decl, /*defer_ok=*/true,
5546 /*expl_inst_class_mem_p=*/false);
5547 --function_depth;
5548 }
5549
5550 return true;
5551 }
5552
5553 bool
5554 mark_used (tree decl)
5555 {
5556 return mark_used (decl, tf_warning_or_error);
5557 }
5558
5559 tree
5560 vtv_start_verification_constructor_init_function (void)
5561 {
5562 return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
5563 }
5564
5565 tree
5566 vtv_finish_verification_constructor_init_function (tree function_body)
5567 {
5568 tree fn;
5569
5570 finish_compound_stmt (function_body);
5571 fn = finish_function (/*inline_p=*/false);
5572 DECL_STATIC_CONSTRUCTOR (fn) = 1;
5573 decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
5574
5575 return fn;
5576 }
5577
5578 #include "gt-cp-decl2.h"