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