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