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