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