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