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