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