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