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