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