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 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
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. */
28 /* ??? not all decl nodes are given the most useful possible
29 line numbers. For example, the CONST_DECLs for enum values. */
33 #include "coretypes.h"
50 #include "tree-inline.h"
51 extern cpp_reader
*parse_in
;
53 /* This structure contains information about the initializations
54 and/or destructions required for a particular priority level. */
55 typedef struct priority_info_s
{
56 /* Nonzero if there have been any initializations at this priority
57 throughout the translation unit. */
58 int initializations_p
;
59 /* Nonzero if there have been any destructions at this priority
60 throughout the translation unit. */
64 static void mark_vtable_entries (tree
);
65 static void grok_function_init (tree
, tree
);
66 static bool maybe_emit_vtables (tree
);
67 static void add_using_namespace (tree
, tree
, bool);
68 static cxx_binding
*ambiguous_decl (tree
, cxx_binding
*, cxx_binding
*, int);
69 static tree
build_anon_union_vars (tree
);
70 static bool acceptable_java_type (tree
);
71 static tree
start_objects (int, int);
72 static void finish_objects (int, int, tree
);
73 static tree
merge_functions (tree
, tree
);
74 static tree
decl_namespace (tree
);
75 static tree
validate_nonmember_using_decl (tree
, tree
*, tree
*);
76 static void do_nonmember_using_decl (tree
, tree
, tree
, tree
, tree
*, tree
*);
77 static tree
start_static_storage_duration_function (unsigned);
78 static void finish_static_storage_duration_function (tree
);
79 static priority_info
get_priority_info (int);
80 static void do_static_initialization (tree
, tree
);
81 static void do_static_destruction (tree
);
82 static tree
start_static_initialization_or_destruction (tree
, int);
83 static void finish_static_initialization_or_destruction (tree
);
84 static void generate_ctor_or_dtor_function (bool, int, location_t
*);
85 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node
,
87 static tree
prune_vars_needing_no_initialization (tree
*);
88 static void write_out_vars (tree
);
89 static void import_export_class (tree
);
90 static tree
get_guard_bits (tree
);
92 /* A list of static class variables. This is needed, because a
93 static class variable can be declared inside the class without
94 an initializer, and then initialized, statically, outside the class. */
95 static GTY(()) varray_type pending_statics
;
96 #define pending_statics_used \
97 (pending_statics ? pending_statics->elements_used : 0)
99 /* A list of functions which were declared inline, but which we
100 may need to emit outline anyway. */
101 static GTY(()) varray_type deferred_fns
;
102 #define deferred_fns_used \
103 (deferred_fns ? deferred_fns->elements_used : 0)
105 /* Flag used when debugging spew.c */
107 extern int spew_debug
;
109 /* Nonzero if we're done parsing and into end-of-file activities. */
113 /* Functions called along with real static constructors and destructors. */
118 /* The :: namespace. */
120 tree global_namespace
;
122 /* Incorporate `const' and `volatile' qualifiers for member functions.
123 FUNCTION is a TYPE_DECL or a FUNCTION_DECL.
124 QUALS is a list of qualifiers. Returns any explicit
125 top-level qualifiers of the method's this pointer, anything other than
126 TYPE_UNQUALIFIED will be an extension. */
129 grok_method_quals (tree ctype
, tree function
, tree quals
)
131 tree fntype
= TREE_TYPE (function
);
132 tree raises
= TYPE_RAISES_EXCEPTIONS (fntype
);
133 int type_quals
= TYPE_UNQUALIFIED
;
134 int dup_quals
= TYPE_UNQUALIFIED
;
135 int this_quals
= TYPE_UNQUALIFIED
;
139 int tq
= cp_type_qual_from_rid (TREE_VALUE (quals
));
141 if ((type_quals
| this_quals
) & tq
)
143 else if (tq
& TYPE_QUAL_RESTRICT
)
147 quals
= TREE_CHAIN (quals
);
151 if (dup_quals
!= TYPE_UNQUALIFIED
)
152 error ("duplicate type qualifiers in %s declaration",
153 TREE_CODE (function
) == FUNCTION_DECL
154 ? "member function" : "type");
156 ctype
= cp_build_qualified_type (ctype
, type_quals
);
157 fntype
= build_method_type_directly (ctype
, TREE_TYPE (fntype
),
158 (TREE_CODE (fntype
) == METHOD_TYPE
159 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype
))
160 : TYPE_ARG_TYPES (fntype
)));
162 fntype
= build_exception_variant (fntype
, raises
);
164 TREE_TYPE (function
) = fntype
;
168 /* Warn when -fexternal-templates is used and #pragma
169 interface/implementation is not used all the times it should be,
173 warn_if_unknown_interface (tree decl
)
175 static int already_warned
= 0;
176 if (already_warned
++)
179 if (flag_alt_external_templates
)
181 tree til
= tinst_for_decl ();
182 location_t saved_loc
= input_location
;
186 input_line
= TINST_LINE (til
);
187 input_filename
= TINST_FILE (til
);
189 warning ("template `%#D' instantiated in file without #pragma interface",
191 input_location
= saved_loc
;
194 cp_warning_at ("template `%#D' defined in file without #pragma interface",
198 /* A subroutine of the parser, to handle a component list. */
201 grok_x_components (tree specs
)
205 specs
= strip_attrs (specs
);
207 check_tag_decl (specs
);
208 t
= groktypename (build_tree_list (specs
, NULL_TREE
));
210 /* The only case where we need to do anything additional here is an
211 anonymous union field, e.g.: `struct S { union { int i; }; };'. */
212 if (t
== NULL_TREE
|| !ANON_AGGR_TYPE_P (t
))
215 fixup_anonymous_aggr (t
);
216 finish_member_declaration (build_decl (FIELD_DECL
, NULL_TREE
, t
));
219 /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
223 cp_build_parm_decl (tree name
, tree type
)
225 tree parm
= build_decl (PARM_DECL
, name
, type
);
226 /* DECL_ARG_TYPE is only used by the back end and the back end never
228 if (!processing_template_decl
)
229 DECL_ARG_TYPE (parm
) = type_passed_as (type
);
233 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
237 build_artificial_parm (tree name
, tree type
)
239 tree parm
= cp_build_parm_decl (name
, type
);
240 DECL_ARTIFICIAL (parm
) = 1;
241 /* All our artificial parms are implicitly `const'; they cannot be
243 TREE_READONLY (parm
) = 1;
247 /* Constructors for types with virtual baseclasses need an "in-charge" flag
248 saying whether this constructor is responsible for initialization of
249 virtual baseclasses or not. All destructors also need this "in-charge"
250 flag, which additionally determines whether or not the destructor should
251 free the memory for the object.
253 This function adds the "in-charge" flag to member function FN if
254 appropriate. It is called from grokclassfn and tsubst.
255 FN must be either a constructor or destructor.
257 The in-charge flag follows the 'this' parameter, and is followed by the
258 VTT parm (if any), then the user-written parms. */
261 maybe_retrofit_in_chrg (tree fn
)
263 tree basetype
, arg_types
, parms
, parm
, fntype
;
265 /* If we've already add the in-charge parameter don't do it again. */
266 if (DECL_HAS_IN_CHARGE_PARM_P (fn
))
269 /* When processing templates we can't know, in general, whether or
270 not we're going to have virtual baseclasses. */
271 if (uses_template_parms (fn
))
274 /* We don't need an in-charge parameter for constructors that don't
275 have virtual bases. */
276 if (DECL_CONSTRUCTOR_P (fn
)
277 && !TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn
)))
280 arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
281 basetype
= TREE_TYPE (TREE_VALUE (arg_types
));
282 arg_types
= TREE_CHAIN (arg_types
);
284 parms
= TREE_CHAIN (DECL_ARGUMENTS (fn
));
286 /* If this is a subobject constructor or destructor, our caller will
287 pass us a pointer to our VTT. */
288 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn
)))
290 parm
= build_artificial_parm (vtt_parm_identifier
, vtt_parm_type
);
292 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
293 TREE_CHAIN (parm
) = parms
;
296 /* ...and then to TYPE_ARG_TYPES. */
297 arg_types
= hash_tree_chain (vtt_parm_type
, arg_types
);
299 DECL_HAS_VTT_PARM_P (fn
) = 1;
302 /* Then add the in-charge parm (before the VTT parm). */
303 parm
= build_artificial_parm (in_charge_identifier
, integer_type_node
);
304 TREE_CHAIN (parm
) = parms
;
306 arg_types
= hash_tree_chain (integer_type_node
, arg_types
);
308 /* Insert our new parameter(s) into the list. */
309 TREE_CHAIN (DECL_ARGUMENTS (fn
)) = parms
;
311 /* And rebuild the function type. */
312 fntype
= build_method_type_directly (basetype
, TREE_TYPE (TREE_TYPE (fn
)),
314 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
)))
315 fntype
= build_exception_variant (fntype
,
316 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
)));
317 TREE_TYPE (fn
) = fntype
;
319 /* Now we've got the in-charge parameter. */
320 DECL_HAS_IN_CHARGE_PARM_P (fn
) = 1;
323 /* Classes overload their constituent function names automatically.
324 When a function name is declared in a record structure,
325 its name is changed to it overloaded name. Since names for
326 constructors and destructors can conflict, we place a leading
329 CNAME is the name of the class we are grokking for.
331 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
333 FLAGS contains bits saying what's special about today's
334 arguments. 1 == DESTRUCTOR. 2 == OPERATOR.
336 If FUNCTION is a destructor, then we must add the `auto-delete' field
337 as a second parameter. There is some hair associated with the fact
338 that we must "declare" this variable in the manner consistent with the
339 way the rest of the arguments were declared.
341 QUALS are the qualifiers for the this pointer. */
344 grokclassfn (tree ctype
, tree function
, enum overload_flags flags
, tree quals
)
346 tree fn_name
= DECL_NAME (function
);
347 int this_quals
= TYPE_UNQUALIFIED
;
349 /* Even within an `extern "C"' block, members get C++ linkage. See
350 [dcl.link] for details. */
351 SET_DECL_LANGUAGE (function
, lang_cplusplus
);
353 if (fn_name
== NULL_TREE
)
355 error ("name missing for member function");
356 fn_name
= get_identifier ("<anonymous>");
357 DECL_NAME (function
) = fn_name
;
361 this_quals
= grok_method_quals (ctype
, function
, quals
);
363 if (TREE_CODE (TREE_TYPE (function
)) == METHOD_TYPE
)
365 /* Must add the class instance variable up front. */
366 /* Right now we just make this a pointer. But later
367 we may wish to make it special. */
368 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (function
)));
372 /* The `this' parameter is implicitly `const'; it cannot be
374 this_quals
|= TYPE_QUAL_CONST
;
375 qual_type
= cp_build_qualified_type (type
, this_quals
);
376 parm
= build_artificial_parm (this_identifier
, qual_type
);
377 c_apply_type_quals_to_decl (this_quals
, parm
);
378 TREE_CHAIN (parm
) = last_function_parms
;
379 last_function_parms
= parm
;
382 DECL_ARGUMENTS (function
) = last_function_parms
;
383 DECL_CONTEXT (function
) = ctype
;
385 if (flags
== DTOR_FLAG
)
386 DECL_DESTRUCTOR_P (function
) = 1;
388 if (flags
== DTOR_FLAG
|| DECL_CONSTRUCTOR_P (function
))
389 maybe_retrofit_in_chrg (function
);
392 /* Create an ARRAY_REF, checking for the user doing things backwards
396 grok_array_decl (tree array_expr
, tree index_exp
)
400 tree orig_array_expr
= array_expr
;
401 tree orig_index_exp
= index_exp
;
403 if (error_operand_p (array_expr
) || error_operand_p (index_exp
))
404 return error_mark_node
;
406 if (processing_template_decl
)
408 if (type_dependent_expression_p (array_expr
)
409 || type_dependent_expression_p (index_exp
))
410 return build_min_nt (ARRAY_REF
, array_expr
, index_exp
);
411 array_expr
= build_non_dependent_expr (array_expr
);
412 index_exp
= build_non_dependent_expr (index_exp
);
415 type
= TREE_TYPE (array_expr
);
416 my_friendly_assert (type
, 20030626);
417 type
= non_reference (type
);
419 /* If they have an `operator[]', use that. */
420 if (IS_AGGR_TYPE (type
) || IS_AGGR_TYPE (TREE_TYPE (index_exp
)))
421 expr
= build_new_op (ARRAY_REF
, LOOKUP_NORMAL
,
422 array_expr
, index_exp
, NULL_TREE
);
427 /* Otherwise, create an ARRAY_REF for a pointer or array type.
428 It is a little-known fact that, if `a' is an array and `i' is
429 an int, you can write `i[a]', which means the same thing as
431 if (TREE_CODE (type
) == ARRAY_TYPE
)
434 p1
= build_expr_type_conversion (WANT_POINTER
, array_expr
, false);
436 if (TREE_CODE (TREE_TYPE (index_exp
)) == ARRAY_TYPE
)
439 p2
= build_expr_type_conversion (WANT_POINTER
, index_exp
, false);
441 i1
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
, array_expr
,
443 i2
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
, index_exp
,
446 if ((p1
&& i2
) && (i1
&& p2
))
447 error ("ambiguous conversion for array subscript");
450 array_expr
= p1
, index_exp
= i2
;
452 array_expr
= p2
, index_exp
= i1
;
455 error ("invalid types `%T[%T]' for array subscript",
456 type
, TREE_TYPE (index_exp
));
457 return error_mark_node
;
460 if (array_expr
== error_mark_node
|| index_exp
== error_mark_node
)
461 error ("ambiguous conversion for array subscript");
463 expr
= build_array_ref (array_expr
, index_exp
);
465 if (processing_template_decl
&& expr
!= error_mark_node
)
466 return build_min (ARRAY_REF
, TREE_TYPE (expr
), orig_array_expr
,
471 /* Given the cast expression EXP, checking out its validity. Either return
472 an error_mark_node if there was an unavoidable error, return a cast to
473 void for trying to delete a pointer w/ the value 0, or return the
474 call to delete. If DOING_VEC is 1, we handle things differently
475 for doing an array delete. If DOING_VEC is 2, they gave us the
476 array size as an argument to delete.
477 Implements ARM $5.3.4. This is called from the parser. */
480 delete_sanity (tree exp
, tree size
, int doing_vec
, int use_global_delete
)
483 /* For a regular vector delete (aka, no size argument) we will pass
484 this down as a NULL_TREE into build_vec_delete. */
485 tree maxindex
= NULL_TREE
;
487 if (exp
== error_mark_node
)
490 if (processing_template_decl
)
492 t
= build_min (DELETE_EXPR
, void_type_node
, exp
, size
);
493 DELETE_EXPR_USE_GLOBAL (t
) = use_global_delete
;
494 DELETE_EXPR_USE_VEC (t
) = doing_vec
;
498 exp
= convert_from_reference (exp
);
499 t
= build_expr_type_conversion (WANT_POINTER
, exp
, true);
501 if (t
== NULL_TREE
|| t
== error_mark_node
)
503 error ("type `%#T' argument given to `delete', expected pointer",
505 return error_mark_node
;
510 maxindex
= cp_build_binary_op (MINUS_EXPR
, size
, integer_one_node
);
511 pedwarn ("anachronistic use of array size in vector delete");
514 type
= TREE_TYPE (t
);
516 /* As of Valley Forge, you can delete a pointer to const. */
518 /* You can't delete functions. */
519 if (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
521 error ("cannot delete a function. Only pointer-to-objects are valid arguments to `delete'");
522 return error_mark_node
;
525 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
526 if (TREE_CODE (TREE_TYPE (type
)) == VOID_TYPE
)
528 warning ("deleting `%T' is undefined", type
);
532 /* An array can't have been allocated by new, so complain. */
533 if (TREE_CODE (t
) == ADDR_EXPR
534 && TREE_CODE (TREE_OPERAND (t
, 0)) == VAR_DECL
535 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t
, 0))) == ARRAY_TYPE
)
536 warning ("deleting array `%#D'", TREE_OPERAND (t
, 0));
538 /* Deleting a pointer with the value zero is valid and has no effect. */
539 if (integer_zerop (t
))
540 return build1 (NOP_EXPR
, void_type_node
, t
);
543 return build_vec_delete (t
, maxindex
, sfk_deleting_destructor
,
546 return build_delete (type
, t
, sfk_deleting_destructor
,
547 LOOKUP_NORMAL
, use_global_delete
);
550 /* Report an error if the indicated template declaration is not the
551 sort of thing that should be a member template. */
554 check_member_template (tree tmpl
)
558 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 0);
559 decl
= DECL_TEMPLATE_RESULT (tmpl
);
561 if (TREE_CODE (decl
) == FUNCTION_DECL
562 || (TREE_CODE (decl
) == TYPE_DECL
563 && IS_AGGR_TYPE (TREE_TYPE (decl
))))
565 if (current_function_decl
)
566 /* 14.5.2.2 [temp.mem]
568 A local class shall not have member templates. */
569 error ("invalid declaration of member template `%#D' in local class",
572 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_VIRTUAL_P (decl
))
574 /* 14.5.2.3 [temp.mem]
576 A member function template shall not be virtual. */
578 ("invalid use of `virtual' in template declaration of `%#D'",
580 DECL_VIRTUAL_P (decl
) = 0;
583 /* The debug-information generating code doesn't know what to do
584 with member templates. */
585 DECL_IGNORED_P (tmpl
) = 1;
588 error ("template declaration of `%#D'", decl
);
591 /* Return true iff TYPE is a valid Java parameter or return type. */
594 acceptable_java_type (tree type
)
596 if (TREE_CODE (type
) == VOID_TYPE
|| TYPE_FOR_JAVA (type
))
598 if (TREE_CODE (type
) == POINTER_TYPE
|| TREE_CODE (type
) == REFERENCE_TYPE
)
600 type
= TREE_TYPE (type
);
601 if (TREE_CODE (type
) == RECORD_TYPE
)
604 if (! TYPE_FOR_JAVA (type
))
606 if (! CLASSTYPE_TEMPLATE_INFO (type
))
608 args
= CLASSTYPE_TI_ARGS (type
);
609 i
= TREE_VEC_LENGTH (args
);
612 type
= TREE_VEC_ELT (args
, i
);
613 if (TREE_CODE (type
) == POINTER_TYPE
)
614 type
= TREE_TYPE (type
);
615 if (! TYPE_FOR_JAVA (type
))
624 /* For a METHOD in a Java class CTYPE, return true if
625 the parameter and return types are valid Java types.
626 Otherwise, print appropriate error messages, and return false. */
629 check_java_method (tree method
)
632 tree arg_types
= TYPE_ARG_TYPES (TREE_TYPE (method
));
633 tree ret_type
= TREE_TYPE (TREE_TYPE (method
));
634 if (!acceptable_java_type (ret_type
))
636 error ("Java method '%D' has non-Java return type `%T'",
640 for (; arg_types
!= NULL_TREE
; arg_types
= TREE_CHAIN (arg_types
))
642 tree type
= TREE_VALUE (arg_types
);
643 if (!acceptable_java_type (type
))
645 error ("Java method '%D' has non-Java parameter type `%T'",
653 /* Sanity check: report error if this function FUNCTION is not
654 really a member of the class (CTYPE) it is supposed to belong to.
655 CNAME is the same here as it is for grokclassfn above. */
658 check_classfn (tree ctype
, tree function
)
663 if (DECL_USE_TEMPLATE (function
)
664 && !(TREE_CODE (function
) == TEMPLATE_DECL
665 && DECL_TEMPLATE_SPECIALIZATION (function
))
666 && is_member_template (DECL_TI_TEMPLATE (function
)))
667 /* Since this is a specialization of a member template,
668 we're not going to find the declaration in the class.
671 struct S { template <typename T> void f(T); };
672 template <> void S::f(int);
674 we're not going to find `S::f(int)', but there's no
675 reason we should, either. We let our callers know we didn't
676 find the method, but we don't complain. */
679 /* OK, is this a definition of a member template? */
680 is_template
= (TREE_CODE (function
) == TEMPLATE_DECL
681 || (processing_template_decl
- template_class_depth (ctype
)));
683 ix
= lookup_fnfields_1 (complete_type (ctype
),
684 DECL_CONSTRUCTOR_P (function
) ? ctor_identifier
:
685 DECL_DESTRUCTOR_P (function
) ? dtor_identifier
:
686 DECL_NAME (function
));
690 tree methods
= CLASSTYPE_METHOD_VEC (ctype
);
691 tree fndecls
, fndecl
= 0;
693 const char *format
= NULL
;
696 for (fndecls
= TREE_VEC_ELT (methods
, ix
);
697 fndecls
; fndecls
= OVL_NEXT (fndecls
))
701 fndecl
= OVL_CURRENT (fndecls
);
702 p1
= TYPE_ARG_TYPES (TREE_TYPE (function
));
703 p2
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
705 /* We cannot simply call decls_match because this doesn't
706 work for static member functions that are pretending to
707 be methods, and because the name may have been changed by
710 /* Get rid of the this parameter on functions that become
712 if (DECL_STATIC_FUNCTION_P (fndecl
)
713 && TREE_CODE (TREE_TYPE (function
)) == METHOD_TYPE
)
714 p1
= TREE_CHAIN (p1
);
716 /* A member template definition only matches a member template
718 if (is_template
!= (TREE_CODE (fndecl
) == TEMPLATE_DECL
))
721 if (same_type_p (TREE_TYPE (TREE_TYPE (function
)),
722 TREE_TYPE (TREE_TYPE (fndecl
)))
723 && compparms (p1
, p2
)
724 && (DECL_TEMPLATE_SPECIALIZATION (function
)
725 == DECL_TEMPLATE_SPECIALIZATION (fndecl
))
726 && (!DECL_TEMPLATE_SPECIALIZATION (function
)
727 || (DECL_TI_TEMPLATE (function
)
728 == DECL_TI_TEMPLATE (fndecl
))))
733 return OVL_CURRENT (fndecls
);
734 error ("prototype for `%#D' does not match any in class `%T'",
736 is_conv_op
= DECL_CONV_FN_P (fndecl
);
739 ix
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
740 fndecls
= TREE_VEC_ELT (methods
, ix
);
743 fndecl
= OVL_CURRENT (fndecls
);
744 fndecls
= OVL_NEXT (fndecls
);
746 if (!fndecls
&& is_conv_op
)
748 if (TREE_VEC_LENGTH (methods
) > ix
)
751 fndecls
= TREE_VEC_ELT (methods
, ix
);
752 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls
)))
764 format
= "candidates are: %#D";
766 format
= "candidate is: %#D";
767 cp_error_at (format
, fndecl
);
770 else if (!COMPLETE_TYPE_P (ctype
))
771 cxx_incomplete_type_error (function
, ctype
);
773 error ("no `%#D' member function declared in class `%T'",
776 /* If we did not find the method in the class, add it to avoid
777 spurious errors (unless the CTYPE is not yet defined, in which
778 case we'll only confuse ourselves when the function is declared
779 properly within the class. */
780 if (COMPLETE_TYPE_P (ctype
))
781 add_method (ctype
, function
, /*error_p=*/1);
785 /* We have just processed the DECL, which is a static data member.
786 Its initializer, if present, is INIT. The ASMSPEC_TREE, if
787 present, is the assembly-language name for the data member.
788 FLAGS is as for cp_finish_decl. */
791 finish_static_data_member_decl (tree decl
, tree init
, tree asmspec_tree
,
794 my_friendly_assert (TREE_PUBLIC (decl
), 0);
796 DECL_CONTEXT (decl
) = current_class_type
;
798 /* We cannot call pushdecl here, because that would fill in the
799 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
800 the right thing, namely, to put this decl out straight away. */
801 /* current_class_type can be NULL_TREE in case of error. */
802 if (!asmspec_tree
&& current_class_type
)
803 DECL_INITIAL (decl
) = error_mark_node
;
805 if (! processing_template_decl
)
807 if (!pending_statics
)
808 VARRAY_TREE_INIT (pending_statics
, 32, "pending_statics");
809 VARRAY_PUSH_TREE (pending_statics
, decl
);
812 if (LOCAL_CLASS_P (current_class_type
))
813 pedwarn ("local class `%#T' shall not have static data member `%#D'",
814 current_class_type
, decl
);
816 /* Static consts need not be initialized in the class definition. */
817 if (init
!= NULL_TREE
&& TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl
)))
819 static int explained
= 0;
821 error ("initializer invalid for static member with constructor");
824 error ("(an out of class initialization is required)");
829 /* Force the compiler to know when an uninitialized static const
830 member is being used. */
831 if (CP_TYPE_CONST_P (TREE_TYPE (decl
)) && init
== 0)
832 TREE_USED (decl
) = 1;
833 DECL_INITIAL (decl
) = init
;
834 DECL_IN_AGGR_P (decl
) = 1;
836 cp_finish_decl (decl
, init
, asmspec_tree
, flags
);
839 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
840 of a structure component, returning a _DECL node.
841 QUALS is a list of type qualifiers for this decl (such as for declaring
842 const member functions).
844 This is done during the parsing of the struct declaration.
845 The _DECL nodes are chained together and the lot of them
846 are ultimately passed to `build_struct' to make the RECORD_TYPE node.
848 If class A defines that certain functions in class B are friends, then
849 the way I have set things up, it is B who is interested in permission
850 granted by A. However, it is in A's context that these declarations
851 are parsed. By returning a void_type_node, class A does not attempt
852 to incorporate the declarations of the friends within its structure.
854 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
855 CHANGES TO CODE IN `start_method'. */
858 grokfield (tree declarator
, tree declspecs
, tree init
, tree asmspec_tree
,
862 const char *asmspec
= 0;
863 int flags
= LOOKUP_ONLYCONVERTING
;
865 if (declspecs
== NULL_TREE
866 && TREE_CODE (declarator
) == SCOPE_REF
867 && TREE_CODE (TREE_OPERAND (declarator
, 1)) == IDENTIFIER_NODE
)
869 /* Access declaration */
870 if (! IS_AGGR_TYPE_CODE (TREE_CODE (TREE_OPERAND (declarator
, 0))))
872 else if (TREE_COMPLEXITY (declarator
) == current_class_depth
)
874 return do_class_using_decl (declarator
);
878 && TREE_CODE (init
) == TREE_LIST
879 && TREE_VALUE (init
) == error_mark_node
880 && TREE_CHAIN (init
) == NULL_TREE
)
883 value
= grokdeclarator (declarator
, declspecs
, FIELD
, init
!= 0, &attrlist
);
884 if (! value
|| value
== error_mark_node
)
885 /* friend or constructor went bad. */
887 if (TREE_TYPE (value
) == error_mark_node
)
888 return error_mark_node
;
890 if (TREE_CODE (value
) == TYPE_DECL
&& init
)
892 error ("typedef `%D' is initialized (use __typeof__ instead)", value
);
896 /* Pass friendly classes back. */
897 if (value
== void_type_node
)
900 /* Pass friend decls back. */
901 if ((TREE_CODE (value
) == FUNCTION_DECL
902 || TREE_CODE (value
) == TEMPLATE_DECL
)
903 && DECL_CONTEXT (value
) != current_class_type
)
906 if (DECL_NAME (value
) != NULL_TREE
907 && IDENTIFIER_POINTER (DECL_NAME (value
))[0] == '_'
908 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (value
)), "_vptr"))
909 error ("member `%D' conflicts with virtual function table field name",
912 /* Stash away type declarations. */
913 if (TREE_CODE (value
) == TYPE_DECL
)
915 DECL_NONLOCAL (value
) = 1;
916 DECL_CONTEXT (value
) = current_class_type
;
918 if (processing_template_decl
)
919 value
= push_template_decl (value
);
924 if (DECL_IN_AGGR_P (value
))
926 error ("`%D' is already defined in `%T'", value
,
927 DECL_CONTEXT (value
));
928 return void_type_node
;
932 asmspec
= TREE_STRING_POINTER (asmspec_tree
);
936 if (TREE_CODE (value
) == FUNCTION_DECL
)
938 grok_function_init (value
, init
);
941 else if (pedantic
&& TREE_CODE (value
) != VAR_DECL
)
942 /* Already complained in grokdeclarator. */
946 /* We allow initializers to become parameters to base
948 if (TREE_CODE (init
) == TREE_LIST
)
950 if (TREE_CHAIN (init
) == NULL_TREE
)
951 init
= TREE_VALUE (init
);
953 init
= digest_init (TREE_TYPE (value
), init
, (tree
*)0);
956 if (!processing_template_decl
)
958 if (TREE_CODE (init
) == CONST_DECL
)
959 init
= DECL_INITIAL (init
);
960 else if (TREE_READONLY_DECL_P (init
))
961 init
= decl_constant_value (init
);
962 else if (TREE_CODE (init
) == CONSTRUCTOR
)
963 init
= digest_init (TREE_TYPE (value
), init
, (tree
*)0);
964 if (init
!= error_mark_node
&& ! TREE_CONSTANT (init
))
966 /* We can allow references to things that are effectively
967 static, since references are initialized with the
969 if (TREE_CODE (TREE_TYPE (value
)) != REFERENCE_TYPE
970 || (TREE_STATIC (init
) == 0
971 && (!DECL_P (init
) || DECL_EXTERNAL (init
) == 0)))
973 error ("field initializer is not constant");
974 init
= error_mark_node
;
981 if (processing_template_decl
982 && (TREE_CODE (value
) == VAR_DECL
|| TREE_CODE (value
) == FUNCTION_DECL
))
983 value
= push_template_decl (value
);
986 cplus_decl_attributes (&value
, attrlist
, 0);
988 if (TREE_CODE (value
) == VAR_DECL
)
990 finish_static_data_member_decl (value
, init
, asmspec_tree
,
994 if (TREE_CODE (value
) == FIELD_DECL
)
997 error ("`asm' specifiers are not permitted on non-static data members");
998 if (DECL_INITIAL (value
) == error_mark_node
)
999 init
= error_mark_node
;
1000 cp_finish_decl (value
, init
, NULL_TREE
, flags
);
1001 DECL_INITIAL (value
) = init
;
1002 DECL_IN_AGGR_P (value
) = 1;
1005 if (TREE_CODE (value
) == FUNCTION_DECL
)
1009 /* This must override the asm specifier which was placed
1010 by grokclassfn. Lay this out fresh. */
1011 SET_DECL_RTL (value
, NULL_RTX
);
1012 SET_DECL_ASSEMBLER_NAME (value
, get_identifier (asmspec
));
1014 if (!DECL_FRIEND_P (value
))
1015 grok_special_member_properties (value
);
1017 cp_finish_decl (value
, init
, asmspec_tree
, flags
);
1019 /* Pass friends back this way. */
1020 if (DECL_FRIEND_P (value
))
1021 return void_type_node
;
1023 DECL_IN_AGGR_P (value
) = 1;
1031 /* Like `grokfield', but for bitfields.
1032 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1035 grokbitfield (tree declarator
, tree declspecs
, tree width
)
1037 register tree value
= grokdeclarator (declarator
, declspecs
, BITFIELD
,
1040 if (! value
) return NULL_TREE
; /* friends went bad. */
1042 /* Pass friendly classes back. */
1043 if (TREE_CODE (value
) == VOID_TYPE
)
1044 return void_type_node
;
1046 if (TREE_CODE (value
) == TYPE_DECL
)
1048 error ("cannot declare `%D' to be a bit-field type", value
);
1052 /* Usually, finish_struct_1 catches bitfields with invalid types.
1053 But, in the case of bitfields with function type, we confuse
1054 ourselves into thinking they are member functions, so we must
1056 if (TREE_CODE (value
) == FUNCTION_DECL
)
1058 error ("cannot declare bit-field `%D' with function type",
1063 if (DECL_IN_AGGR_P (value
))
1065 error ("`%D' is already defined in the class %T", value
,
1066 DECL_CONTEXT (value
));
1067 return void_type_node
;
1070 if (TREE_STATIC (value
))
1072 error ("static member `%D' cannot be a bit-field", value
);
1075 cp_finish_decl (value
, NULL_TREE
, NULL_TREE
, 0);
1077 if (width
!= error_mark_node
)
1079 constant_expression_warning (width
);
1080 DECL_INITIAL (value
) = width
;
1081 SET_DECL_C_BIT_FIELD (value
);
1084 DECL_IN_AGGR_P (value
) = 1;
1088 /* When a function is declared with an initializer,
1089 do the right thing. Currently, there are two possibilities:
1094 // initialization possibility #1.
1095 virtual void f () = 0;
1117 // initialization possibility #2
1124 grok_function_init (tree decl
, tree init
)
1126 /* An initializer for a function tells how this function should
1128 tree type
= TREE_TYPE (decl
);
1130 if (TREE_CODE (type
) == FUNCTION_TYPE
)
1131 error ("initializer specified for non-member function `%D'", decl
);
1132 else if (integer_zerop (init
))
1133 DECL_PURE_VIRTUAL_P (decl
) = 1;
1135 error ("invalid initializer for virtual method `%D'", decl
);
1139 cplus_decl_attributes (tree
*decl
, tree attributes
, int flags
)
1141 if (*decl
== NULL_TREE
|| *decl
== void_type_node
)
1144 if (TREE_CODE (*decl
) == TEMPLATE_DECL
)
1145 decl
= &DECL_TEMPLATE_RESULT (*decl
);
1147 decl_attributes (decl
, attributes
, flags
);
1149 if (TREE_CODE (*decl
) == TYPE_DECL
)
1150 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl
), TREE_TYPE (*decl
));
1153 /* Return the name for the constructor (or destructor) for the
1154 specified class TYPE. When given a template, this routine doesn't
1155 lose the specialization. */
1158 constructor_name_full (tree type
)
1160 type
= TYPE_MAIN_VARIANT (type
);
1161 if (CLASS_TYPE_P (type
) && TYPE_WAS_ANONYMOUS (type
)
1162 && TYPE_HAS_CONSTRUCTOR (type
))
1163 return DECL_NAME (OVL_CURRENT (CLASSTYPE_CONSTRUCTORS (type
)));
1165 return TYPE_IDENTIFIER (type
);
1168 /* Return the name for the constructor (or destructor) for the
1169 specified class. When given a template, return the plain
1170 unspecialized name. */
1173 constructor_name (tree type
)
1176 name
= constructor_name_full (type
);
1177 if (IDENTIFIER_TEMPLATE (name
))
1178 name
= IDENTIFIER_TEMPLATE (name
);
1182 /* Returns TRUE if NAME is the name for the constructor for TYPE. */
1185 constructor_name_p (tree name
, tree type
)
1192 if (TREE_CODE (name
) != IDENTIFIER_NODE
)
1195 ctor_name
= constructor_name_full (type
);
1196 if (name
== ctor_name
)
1198 if (IDENTIFIER_TEMPLATE (ctor_name
)
1199 && name
== IDENTIFIER_TEMPLATE (ctor_name
))
1205 /* Defer the compilation of the FN until the end of compilation. */
1210 if (DECL_DEFERRED_FN (fn
))
1212 DECL_DEFERRED_FN (fn
) = 1;
1213 DECL_DEFER_OUTPUT (fn
) = 1;
1215 VARRAY_TREE_INIT (deferred_fns
, 32, "deferred_fns");
1217 VARRAY_PUSH_TREE (deferred_fns
, fn
);
1220 /* Walks through the namespace- or function-scope anonymous union OBJECT,
1221 building appropriate ALIAS_DECLs. Returns one of the fields for use in
1222 the mangled name. */
1225 build_anon_union_vars (tree object
)
1227 tree type
= TREE_TYPE (object
);
1228 tree main_decl
= NULL_TREE
;
1231 /* Rather than write the code to handle the non-union case,
1232 just give an error. */
1233 if (TREE_CODE (type
) != UNION_TYPE
)
1234 error ("anonymous struct not inside named type");
1236 for (field
= TYPE_FIELDS (type
);
1238 field
= TREE_CHAIN (field
))
1243 if (DECL_ARTIFICIAL (field
))
1245 if (TREE_CODE (field
) != FIELD_DECL
)
1248 `%#D' invalid; an anonymous union can only have non-static data members",
1253 if (TREE_PRIVATE (field
))
1254 cp_pedwarn_at ("private member `%#D' in anonymous union", field
);
1255 else if (TREE_PROTECTED (field
))
1256 cp_pedwarn_at ("protected member `%#D' in anonymous union", field
);
1258 if (processing_template_decl
)
1259 ref
= build_min_nt (COMPONENT_REF
, object
, DECL_NAME (field
));
1261 ref
= build_class_member_access_expr (object
, field
, NULL_TREE
,
1264 if (DECL_NAME (field
))
1266 decl
= build_decl (ALIAS_DECL
, DECL_NAME (field
), TREE_TYPE (field
));
1267 DECL_INITIAL (decl
) = ref
;
1268 TREE_PUBLIC (decl
) = 0;
1269 TREE_STATIC (decl
) = 0;
1270 DECL_EXTERNAL (decl
) = 1;
1271 decl
= pushdecl (decl
);
1273 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
1274 decl
= build_anon_union_vars (ref
);
1278 if (main_decl
== NULL_TREE
)
1285 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1286 anonymous union, then all members must be laid out together. PUBLIC_P
1287 is nonzero if this union is not declared static. */
1290 finish_anon_union (tree anon_union_decl
)
1292 tree type
= TREE_TYPE (anon_union_decl
);
1294 bool public_p
= TREE_PUBLIC (anon_union_decl
);
1296 /* The VAR_DECL's context is the same as the TYPE's context. */
1297 DECL_CONTEXT (anon_union_decl
) = DECL_CONTEXT (TYPE_NAME (type
));
1299 if (TYPE_FIELDS (type
) == NULL_TREE
)
1304 error ("namespace-scope anonymous aggregates must be static");
1308 main_decl
= build_anon_union_vars (anon_union_decl
);
1309 if (main_decl
== NULL_TREE
)
1311 warning ("anonymous union with no members");
1315 if (!processing_template_decl
)
1317 /* Use main_decl to set the mangled name. */
1318 DECL_NAME (anon_union_decl
) = DECL_NAME (main_decl
);
1319 mangle_decl (anon_union_decl
);
1320 DECL_NAME (anon_union_decl
) = NULL_TREE
;
1323 pushdecl (anon_union_decl
);
1324 if (building_stmt_tree ()
1325 && at_function_scope_p ())
1326 add_decl_stmt (anon_union_decl
);
1327 else if (!processing_template_decl
)
1328 rest_of_decl_compilation (anon_union_decl
, NULL
,
1329 toplevel_bindings_p (), at_eof
);
1332 /* Auxiliary functions to make type signatures for
1333 `operator new' and `operator delete' correspond to
1334 what compiler will be expecting. */
1337 coerce_new_type (tree type
)
1340 tree args
= TYPE_ARG_TYPES (type
);
1342 my_friendly_assert (TREE_CODE (type
) == FUNCTION_TYPE
, 20001107);
1344 if (!same_type_p (TREE_TYPE (type
), ptr_type_node
))
1345 e
= 1, error ("`operator new' must return type `%T'", ptr_type_node
);
1347 if (!args
|| args
== void_list_node
1348 || !same_type_p (TREE_VALUE (args
), size_type_node
))
1351 if (args
&& args
!= void_list_node
)
1352 args
= TREE_CHAIN (args
);
1353 pedwarn ("`operator new' takes type `size_t' (`%T') as first parameter", size_type_node
);
1358 args
= tree_cons (NULL_TREE
, size_type_node
, args
);
1361 type
= build_exception_variant
1362 (build_function_type (ptr_type_node
, args
),
1363 TYPE_RAISES_EXCEPTIONS (type
));
1371 coerce_delete_type (tree type
)
1374 tree args
= TYPE_ARG_TYPES (type
);
1376 my_friendly_assert (TREE_CODE (type
) == FUNCTION_TYPE
, 20001107);
1378 if (!same_type_p (TREE_TYPE (type
), void_type_node
))
1379 e
= 1, error ("`operator delete' must return type `%T'", void_type_node
);
1381 if (!args
|| args
== void_list_node
1382 || !same_type_p (TREE_VALUE (args
), ptr_type_node
))
1385 if (args
&& args
!= void_list_node
)
1386 args
= TREE_CHAIN (args
);
1387 error ("`operator delete' takes type `%T' as first parameter", ptr_type_node
);
1392 args
= tree_cons (NULL_TREE
, ptr_type_node
, args
);
1395 type
= build_exception_variant
1396 (build_function_type (void_type_node
, args
),
1397 TYPE_RAISES_EXCEPTIONS (type
));
1406 mark_vtable_entries (tree decl
)
1408 tree entries
= CONSTRUCTOR_ELTS (DECL_INITIAL (decl
));
1410 for (; entries
; entries
= TREE_CHAIN (entries
))
1412 tree fnaddr
= TREE_VALUE (entries
);
1415 STRIP_NOPS (fnaddr
);
1417 if (TREE_CODE (fnaddr
) != ADDR_EXPR
1418 && TREE_CODE (fnaddr
) != FDESC_EXPR
)
1419 /* This entry is an offset: a virtual base class offset, a
1420 virtual call offset, an RTTI offset, etc. */
1423 fn
= TREE_OPERAND (fnaddr
, 0);
1424 TREE_ADDRESSABLE (fn
) = 1;
1425 /* When we don't have vcall offsets, we output thunks whenever
1426 we output the vtables that contain them. With vcall offsets,
1427 we know all the thunks we'll need when we emit a virtual
1428 function, so we emit the thunks there instead. */
1429 if (DECL_THUNK_P (fn
))
1430 use_thunk (fn
, /*emit_p=*/0);
1435 /* Set DECL up to have the closest approximation of "initialized common"
1436 linkage available. */
1439 comdat_linkage (tree decl
)
1442 make_decl_one_only (decl
);
1443 else if (TREE_CODE (decl
) == FUNCTION_DECL
1444 || (TREE_CODE (decl
) == VAR_DECL
&& DECL_ARTIFICIAL (decl
)))
1445 /* We can just emit function and compiler-generated variables
1446 statically; having multiple copies is (for the most part) only
1449 There are two correctness issues, however: the address of a
1450 template instantiation with external linkage should be the
1451 same, independent of what translation unit asks for the
1452 address, and this will not hold when we emit multiple copies of
1453 the function. However, there's little else we can do.
1455 Also, by default, the typeinfo implementation assumes that
1456 there will be only one copy of the string used as the name for
1457 each type. Therefore, if weak symbols are unavailable, the
1458 run-time library should perform a more conservative check; it
1459 should perform a string comparison, rather than an address
1461 TREE_PUBLIC (decl
) = 0;
1464 /* Static data member template instantiations, however, cannot
1465 have multiple copies. */
1466 if (DECL_INITIAL (decl
) == 0
1467 || DECL_INITIAL (decl
) == error_mark_node
)
1468 DECL_COMMON (decl
) = 1;
1469 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl
)))
1471 DECL_COMMON (decl
) = 1;
1472 DECL_INITIAL (decl
) = error_mark_node
;
1474 else if (!DECL_EXPLICIT_INSTANTIATION (decl
))
1476 /* We can't do anything useful; leave vars for explicit
1478 DECL_EXTERNAL (decl
) = 1;
1479 DECL_NOT_REALLY_EXTERN (decl
) = 0;
1483 if (DECL_LANG_SPECIFIC (decl
))
1484 DECL_COMDAT (decl
) = 1;
1487 /* For win32 we also want to put explicit instantiations in
1488 linkonce sections, so that they will be merged with implicit
1489 instantiations; otherwise we get duplicate symbol errors. */
1492 maybe_make_one_only (tree decl
)
1494 /* We used to say that this was not necessary on targets that support weak
1495 symbols, because the implicit instantiations will defer to the explicit
1496 one. However, that's not actually the case in SVR4; a strong definition
1497 after a weak one is an error. Also, not making explicit
1498 instantiations one_only means that we can end up with two copies of
1499 some template instantiations. */
1503 /* We can't set DECL_COMDAT on functions, or finish_file will think
1504 we can get away with not emitting them if they aren't used. We need
1505 to for variables so that cp_finish_decl will update their linkage,
1506 because their DECL_INITIAL may not have been set properly yet. */
1508 make_decl_one_only (decl
);
1510 if (TREE_CODE (decl
) == VAR_DECL
)
1512 DECL_COMDAT (decl
) = 1;
1513 /* Mark it needed so we don't forget to emit it. */
1514 mark_referenced (DECL_ASSEMBLER_NAME (decl
));
1518 /* Set TREE_PUBLIC and/or DECL_EXTERN on the vtable DECL,
1519 based on TYPE and other static flags.
1521 Note that anything public is tagged TREE_PUBLIC, whether
1522 it's public in this file or in another one. */
1525 import_export_vtable (tree decl
, tree type
, int final
)
1527 if (DECL_INTERFACE_KNOWN (decl
))
1530 if (TYPE_FOR_JAVA (type
))
1532 TREE_PUBLIC (decl
) = 1;
1533 DECL_EXTERNAL (decl
) = 1;
1534 DECL_INTERFACE_KNOWN (decl
) = 1;
1536 else if (CLASSTYPE_INTERFACE_KNOWN (type
))
1538 TREE_PUBLIC (decl
) = 1;
1539 DECL_EXTERNAL (decl
) = CLASSTYPE_INTERFACE_ONLY (type
);
1540 DECL_INTERFACE_KNOWN (decl
) = 1;
1544 /* We can only wait to decide if we have real non-inline virtual
1545 functions in our class, or if we come from a template. */
1547 int found
= (CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
1548 || CLASSTYPE_KEY_METHOD (type
) != NULL_TREE
);
1550 if (final
|| ! found
)
1552 comdat_linkage (decl
);
1553 DECL_EXTERNAL (decl
) = 0;
1557 TREE_PUBLIC (decl
) = 1;
1558 DECL_EXTERNAL (decl
) = 1;
1563 /* Determine whether or not we want to specifically import or export CTYPE,
1564 using various heuristics. */
1567 import_export_class (tree ctype
)
1569 /* -1 for imported, 1 for exported. */
1570 int import_export
= 0;
1572 /* It only makes sense to call this function at EOF. The reason is
1573 that this function looks at whether or not the first non-inline
1574 non-abstract virtual member function has been defined in this
1575 translation unit. But, we can't possibly know that until we've
1576 seen the entire translation unit. */
1577 my_friendly_assert (at_eof
, 20000226);
1579 if (CLASSTYPE_INTERFACE_KNOWN (ctype
))
1582 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma interface,
1583 we will have CLASSTYPE_INTERFACE_ONLY set but not
1584 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1585 heuristic because someone will supply a #pragma implementation
1586 elsewhere, and deducing it here would produce a conflict. */
1587 if (CLASSTYPE_INTERFACE_ONLY (ctype
))
1590 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype
)))
1592 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype
)))
1595 /* If we got -fno-implicit-templates, we import template classes that
1596 weren't explicitly instantiated. */
1597 if (import_export
== 0
1598 && CLASSTYPE_IMPLICIT_INSTANTIATION (ctype
)
1599 && ! flag_implicit_templates
)
1602 /* Base our import/export status on that of the first non-inline,
1603 non-pure virtual function, if any. */
1604 if (import_export
== 0
1605 && TYPE_POLYMORPHIC_P (ctype
))
1607 tree method
= CLASSTYPE_KEY_METHOD (ctype
);
1609 import_export
= (DECL_REALLY_EXTERN (method
) ? -1 : 1);
1612 #ifdef MULTIPLE_SYMBOL_SPACES
1613 if (import_export
== -1)
1619 SET_CLASSTYPE_INTERFACE_KNOWN (ctype
);
1620 CLASSTYPE_INTERFACE_ONLY (ctype
) = (import_export
< 0);
1624 /* If necessary, write out the vtables for the dynamic class CTYPE.
1625 Returns true if any vtables were emitted. */
1628 maybe_emit_vtables (tree ctype
)
1632 bool needed
= false;
1634 /* If the vtables for this class have already been emitted there is
1635 nothing more to do. */
1636 primary_vtbl
= CLASSTYPE_VTABLES (ctype
);
1637 if (TREE_ASM_WRITTEN (primary_vtbl
))
1639 /* Ignore dummy vtables made by get_vtable_decl. */
1640 if (TREE_TYPE (primary_vtbl
) == void_type_node
)
1643 import_export_class (ctype
);
1644 import_export_vtable (primary_vtbl
, ctype
, 1);
1646 /* See if any of the vtables are needed. */
1647 for (vtbl
= CLASSTYPE_VTABLES (ctype
); vtbl
; vtbl
= TREE_CHAIN (vtbl
))
1648 if (!DECL_EXTERNAL (vtbl
) && DECL_NEEDED_P (vtbl
))
1652 /* If the references to this class' vtables are optimized away,
1653 still emit the appropriate debugging information. See
1655 if (DECL_COMDAT (primary_vtbl
)
1656 && CLASSTYPE_DEBUG_REQUESTED (ctype
))
1657 note_debug_info_needed (ctype
);
1660 else if (TREE_PUBLIC (vtbl
) && !DECL_COMDAT (vtbl
))
1664 /* The ABI requires that we emit all of the vtables if we emit any
1666 for (vtbl
= CLASSTYPE_VTABLES (ctype
); vtbl
; vtbl
= TREE_CHAIN (vtbl
))
1669 import_export_vtable (vtbl
, ctype
, 1);
1670 mark_vtable_entries (vtbl
);
1672 /* If we know that DECL is needed, mark it as such for the varpool. */
1674 cgraph_varpool_mark_needed_node (cgraph_varpool_node (vtbl
));
1676 if (TREE_TYPE (DECL_INITIAL (vtbl
)) == 0)
1677 store_init_value (vtbl
, DECL_INITIAL (vtbl
));
1679 if (write_symbols
== DWARF_DEBUG
|| write_symbols
== DWARF2_DEBUG
)
1681 /* Mark the VAR_DECL node representing the vtable itself as a
1682 "gratuitous" one, thereby forcing dwarfout.c to ignore it.
1683 It is rather important that such things be ignored because
1684 any effort to actually generate DWARF for them will run
1685 into trouble when/if we encounter code like:
1688 struct S { virtual void member (); };
1690 because the artificial declaration of the vtable itself (as
1691 manufactured by the g++ front end) will say that the vtable
1692 is a static member of `S' but only *after* the debug output
1693 for the definition of `S' has already been output. This causes
1694 grief because the DWARF entry for the definition of the vtable
1695 will try to refer back to an earlier *declaration* of the
1696 vtable as a static member of `S' and there won't be one.
1697 We might be able to arrange to have the "vtable static member"
1698 attached to the member list for `S' before the debug info for
1699 `S' get written (which would solve the problem) but that would
1700 require more intrusive changes to the g++ front end. */
1702 DECL_IGNORED_P (vtbl
) = 1;
1705 /* Always make vtables weak. */
1707 comdat_linkage (vtbl
);
1709 rest_of_decl_compilation (vtbl
, NULL
, 1, 1);
1711 /* Because we're only doing syntax-checking, we'll never end up
1712 actually marking the variable as written. */
1713 if (flag_syntax_only
)
1714 TREE_ASM_WRITTEN (vtbl
) = 1;
1717 /* Since we're writing out the vtable here, also write the debug
1719 note_debug_info_needed (ctype
);
1724 /* Determines the proper settings of TREE_PUBLIC and DECL_EXTERNAL for an
1725 inline function or template instantiation at end-of-file. */
1728 import_export_decl (tree decl
)
1730 if (DECL_INTERFACE_KNOWN (decl
))
1733 if (DECL_TEMPLATE_INSTANTIATION (decl
)
1734 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl
))
1736 DECL_NOT_REALLY_EXTERN (decl
) = 1;
1737 if ((DECL_IMPLICIT_INSTANTIATION (decl
)
1738 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl
))
1739 && (flag_implicit_templates
1740 || (flag_implicit_inline_templates
1741 && TREE_CODE (decl
) == FUNCTION_DECL
1742 && DECL_DECLARED_INLINE_P (decl
))))
1744 if (!TREE_PUBLIC (decl
))
1745 /* Templates are allowed to have internal linkage. See
1749 comdat_linkage (decl
);
1753 DECL_EXTERNAL (decl
) = 1;
1754 DECL_NOT_REALLY_EXTERN (decl
) = 0;
1757 else if (DECL_FUNCTION_MEMBER_P (decl
))
1759 if (!DECL_DECLARED_INLINE_P (decl
))
1761 tree ctype
= DECL_CONTEXT (decl
);
1762 import_export_class (ctype
);
1763 if (CLASSTYPE_INTERFACE_KNOWN (ctype
))
1765 DECL_NOT_REALLY_EXTERN (decl
)
1766 = ! (CLASSTYPE_INTERFACE_ONLY (ctype
)
1767 || (DECL_DECLARED_INLINE_P (decl
)
1768 && ! flag_implement_inlines
1769 && !DECL_VINDEX (decl
)));
1771 if (!DECL_NOT_REALLY_EXTERN (decl
))
1772 DECL_EXTERNAL (decl
) = 1;
1774 /* Always make artificials weak. */
1775 if (DECL_ARTIFICIAL (decl
) && flag_weak
)
1776 comdat_linkage (decl
);
1778 maybe_make_one_only (decl
);
1782 comdat_linkage (decl
);
1785 comdat_linkage (decl
);
1787 DECL_INTERFACE_KNOWN (decl
) = 1;
1790 /* Here, we only decide whether or not the tinfo node should be
1791 emitted with the vtable. IS_IN_LIBRARY is nonzero iff the
1792 typeinfo for TYPE should be in the runtime library. */
1795 import_export_tinfo (tree decl
, tree type
, bool is_in_library
)
1797 if (DECL_INTERFACE_KNOWN (decl
))
1800 if (IS_AGGR_TYPE (type
))
1801 import_export_class (type
);
1803 if (IS_AGGR_TYPE (type
) && CLASSTYPE_INTERFACE_KNOWN (type
)
1804 && TYPE_POLYMORPHIC_P (type
)
1805 /* If -fno-rtti, we're not necessarily emitting this stuff with
1806 the class, so go ahead and emit it now. This can happen when
1807 a class is used in exception handling. */
1810 DECL_NOT_REALLY_EXTERN (decl
) = !CLASSTYPE_INTERFACE_ONLY (type
);
1811 DECL_COMDAT (decl
) = 0;
1815 DECL_NOT_REALLY_EXTERN (decl
) = 1;
1816 DECL_COMDAT (decl
) = 1;
1819 /* Now override some cases. */
1821 DECL_COMDAT (decl
) = 1;
1822 else if (is_in_library
)
1823 DECL_COMDAT (decl
) = 0;
1825 DECL_INTERFACE_KNOWN (decl
) = 1;
1828 /* Return an expression that performs the destruction of DECL, which
1829 must be a VAR_DECL whose type has a non-trivial destructor, or is
1830 an array whose (innermost) elements have a non-trivial destructor. */
1833 build_cleanup (tree decl
)
1836 tree type
= TREE_TYPE (decl
);
1838 /* This function should only be called for declarations that really
1839 require cleanups. */
1840 my_friendly_assert (!TYPE_HAS_TRIVIAL_DESTRUCTOR (type
), 20030106);
1842 /* Treat all objects with destructors as used; the destructor may do
1843 something substantive. */
1846 if (TREE_CODE (type
) == ARRAY_TYPE
)
1850 cxx_mark_addressable (decl
);
1851 temp
= build1 (ADDR_EXPR
, build_pointer_type (type
), decl
);
1853 temp
= build_delete (TREE_TYPE (temp
), temp
,
1854 sfk_complete_destructor
,
1855 LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
|LOOKUP_DESTRUCTOR
, 0);
1859 /* Returns the initialization guard variable for the variable DECL,
1860 which has static storage duration. */
1863 get_guard (tree decl
)
1868 sname
= mangle_guard_variable (decl
);
1869 guard
= IDENTIFIER_GLOBAL_VALUE (sname
);
1874 /* We use a type that is big enough to contain a mutex as well
1875 as an integer counter. */
1876 guard_type
= long_long_integer_type_node
;
1877 guard
= build_decl (VAR_DECL
, sname
, guard_type
);
1879 /* The guard should have the same linkage as what it guards. */
1880 TREE_PUBLIC (guard
) = TREE_PUBLIC (decl
);
1881 TREE_STATIC (guard
) = TREE_STATIC (decl
);
1882 DECL_COMMON (guard
) = DECL_COMMON (decl
);
1883 DECL_ONE_ONLY (guard
) = DECL_ONE_ONLY (decl
);
1884 if (TREE_PUBLIC (decl
))
1885 DECL_WEAK (guard
) = DECL_WEAK (decl
);
1887 DECL_ARTIFICIAL (guard
) = 1;
1888 TREE_USED (guard
) = 1;
1889 pushdecl_top_level_and_finish (guard
, NULL_TREE
);
1894 /* Return those bits of the GUARD variable that should be set when the
1895 guarded entity is actually initialized. */
1898 get_guard_bits (tree guard
)
1900 /* We only set the first byte of the guard, in order to leave room
1901 for a mutex in the high-order bits. */
1902 guard
= build1 (ADDR_EXPR
,
1903 build_pointer_type (TREE_TYPE (guard
)),
1905 guard
= build1 (NOP_EXPR
,
1906 build_pointer_type (char_type_node
),
1908 guard
= build1 (INDIRECT_REF
, char_type_node
, guard
);
1913 /* Return an expression which determines whether or not the GUARD
1914 variable has already been initialized. */
1917 get_guard_cond (tree guard
)
1921 /* Check to see if the GUARD is zero. */
1922 guard
= get_guard_bits (guard
);
1923 guard_value
= integer_zero_node
;
1924 if (!same_type_p (TREE_TYPE (guard_value
), TREE_TYPE (guard
)))
1925 guard_value
= convert (TREE_TYPE (guard
), guard_value
);
1926 return cp_build_binary_op (EQ_EXPR
, guard
, guard_value
);
1929 /* Return an expression which sets the GUARD variable, indicating that
1930 the variable being guarded has been initialized. */
1933 set_guard (tree guard
)
1937 /* Set the GUARD to one. */
1938 guard
= get_guard_bits (guard
);
1939 guard_init
= integer_one_node
;
1940 if (!same_type_p (TREE_TYPE (guard_init
), TREE_TYPE (guard
)))
1941 guard_init
= convert (TREE_TYPE (guard
), guard_init
);
1942 return build_modify_expr (guard
, NOP_EXPR
, guard_init
);
1945 /* Start the process of running a particular set of global constructors
1946 or destructors. Subroutine of do_[cd]tors. */
1949 start_objects (int method_type
, int initp
)
1955 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
1957 if (initp
!= DEFAULT_INIT_PRIORITY
)
1967 sprintf (type
, "%c%c%.5u", method_type
, joiner
, initp
);
1970 sprintf (type
, "%c", method_type
);
1972 fnname
= get_file_function_name_long (type
);
1974 start_function (void_list_node
,
1975 make_call_declarator (fnname
, void_list_node
, NULL_TREE
,
1977 NULL_TREE
, SF_DEFAULT
);
1979 /* It can be a static function as long as collect2 does not have
1980 to scan the object file to find its ctor/dtor routine. */
1981 TREE_PUBLIC (current_function_decl
) = ! targetm
.have_ctors_dtors
;
1983 /* Mark this declaration as used to avoid spurious warnings. */
1984 TREE_USED (current_function_decl
) = 1;
1986 /* Mark this function as a global constructor or destructor. */
1987 if (method_type
== 'I')
1988 DECL_GLOBAL_CTOR_P (current_function_decl
) = 1;
1990 DECL_GLOBAL_DTOR_P (current_function_decl
) = 1;
1991 DECL_LANG_SPECIFIC (current_function_decl
)->decl_flags
.u2sel
= 1;
1993 body
= begin_compound_stmt (/*has_no_scope=*/false);
1995 /* We cannot allow these functions to be elided, even if they do not
1996 have external linkage. And, there's no point in deferring
1997 compilation of thes functions; they're all going to have to be
1999 current_function_cannot_inline
2000 = "static constructors and destructors cannot be inlined";
2005 /* Finish the process of running a particular set of global constructors
2006 or destructors. Subroutine of do_[cd]tors. */
2009 finish_objects (int method_type
, int initp
, tree body
)
2014 finish_compound_stmt (body
);
2015 fn
= finish_function (0);
2016 expand_or_defer_fn (fn
);
2018 /* When only doing semantic analysis, and no RTL generation, we
2019 can't call functions that directly emit assembly code; there is
2020 no assembly file in which to put the code. */
2021 if (flag_syntax_only
)
2024 if (targetm
.have_ctors_dtors
)
2026 rtx fnsym
= XEXP (DECL_RTL (fn
), 0);
2027 if (method_type
== 'I')
2028 (* targetm
.asm_out
.constructor
) (fnsym
, initp
);
2030 (* targetm
.asm_out
.destructor
) (fnsym
, initp
);
2034 /* The names of the parameters to the function created to handle
2035 initializations and destructions for objects with static storage
2037 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
2038 #define PRIORITY_IDENTIFIER "__priority"
2040 /* The name of the function we create to handle initializations and
2041 destructions for objects with static storage duration. */
2042 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
2044 /* The declaration for the __INITIALIZE_P argument. */
2045 static GTY(()) tree initialize_p_decl
;
2047 /* The declaration for the __PRIORITY argument. */
2048 static GTY(()) tree priority_decl
;
2050 /* The declaration for the static storage duration function. */
2051 static GTY(()) tree ssdf_decl
;
2053 /* All the static storage duration functions created in this
2054 translation unit. */
2055 static GTY(()) varray_type ssdf_decls
;
2057 /* A map from priority levels to information about that priority
2058 level. There may be many such levels, so efficient lookup is
2060 static splay_tree priority_info_map
;
2062 /* Begins the generation of the function that will handle all
2063 initialization and destruction of objects with static storage
2064 duration. The function generated takes two parameters of type
2065 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
2066 nonzero, it performs initializations. Otherwise, it performs
2067 destructions. It only performs those initializations or
2068 destructions with the indicated __PRIORITY. The generated function
2071 It is assumed that this function will only be called once per
2072 translation unit. */
2075 start_static_storage_duration_function (unsigned count
)
2080 char id
[sizeof (SSDF_IDENTIFIER
) + 1 /* '\0' */ + 32];
2082 /* Create the identifier for this function. It will be of the form
2083 SSDF_IDENTIFIER_<number>. */
2084 sprintf (id
, "%s_%u", SSDF_IDENTIFIER
, count
);
2086 /* Create the parameters. */
2087 parm_types
= void_list_node
;
2088 parm_types
= tree_cons (NULL_TREE
, integer_type_node
, parm_types
);
2089 parm_types
= tree_cons (NULL_TREE
, integer_type_node
, parm_types
);
2090 type
= build_function_type (void_type_node
, parm_types
);
2092 /* Create the FUNCTION_DECL itself. */
2093 ssdf_decl
= build_lang_decl (FUNCTION_DECL
,
2094 get_identifier (id
),
2096 TREE_PUBLIC (ssdf_decl
) = 0;
2097 DECL_ARTIFICIAL (ssdf_decl
) = 1;
2099 /* Put this function in the list of functions to be called from the
2100 static constructors and destructors. */
2103 VARRAY_TREE_INIT (ssdf_decls
, 32, "ssdf_decls");
2105 /* Take this opportunity to initialize the map from priority
2106 numbers to information about that priority level. */
2107 priority_info_map
= splay_tree_new (splay_tree_compare_ints
,
2108 /*delete_key_fn=*/0,
2109 /*delete_value_fn=*/
2110 (splay_tree_delete_value_fn
) &free
);
2112 /* We always need to generate functions for the
2113 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
2114 priorities later, we'll be sure to find the
2115 DEFAULT_INIT_PRIORITY. */
2116 get_priority_info (DEFAULT_INIT_PRIORITY
);
2119 VARRAY_PUSH_TREE (ssdf_decls
, ssdf_decl
);
2121 /* Create the argument list. */
2122 initialize_p_decl
= cp_build_parm_decl
2123 (get_identifier (INITIALIZE_P_IDENTIFIER
), integer_type_node
);
2124 DECL_CONTEXT (initialize_p_decl
) = ssdf_decl
;
2125 TREE_USED (initialize_p_decl
) = 1;
2126 priority_decl
= cp_build_parm_decl
2127 (get_identifier (PRIORITY_IDENTIFIER
), integer_type_node
);
2128 DECL_CONTEXT (priority_decl
) = ssdf_decl
;
2129 TREE_USED (priority_decl
) = 1;
2131 TREE_CHAIN (initialize_p_decl
) = priority_decl
;
2132 DECL_ARGUMENTS (ssdf_decl
) = initialize_p_decl
;
2134 /* Put the function in the global scope. */
2135 pushdecl (ssdf_decl
);
2137 /* Start the function itself. This is equivalent to declarating the
2140 static void __ssdf (int __initialize_p, init __priority_p);
2142 It is static because we only need to call this function from the
2143 various constructor and destructor functions for this module. */
2144 start_function (/*specs=*/NULL_TREE
,
2146 /*attrs=*/NULL_TREE
,
2149 /* Set up the scope of the outermost block in the function. */
2150 body
= begin_compound_stmt (/*has_no_scope=*/false);
2152 /* This function must not be deferred because we are depending on
2153 its compilation to tell us what is TREE_SYMBOL_REFERENCED. */
2154 current_function_cannot_inline
2155 = "static storage duration functions cannot be inlined";
2160 /* Finish the generation of the function which performs initialization
2161 and destruction of objects with static storage duration. After
2162 this point, no more such objects can be created. */
2165 finish_static_storage_duration_function (tree body
)
2167 /* Close out the function. */
2168 finish_compound_stmt (body
);
2169 expand_or_defer_fn (finish_function (0));
2172 /* Return the information about the indicated PRIORITY level. If no
2173 code to handle this level has yet been generated, generate the
2174 appropriate prologue. */
2176 static priority_info
2177 get_priority_info (int priority
)
2182 n
= splay_tree_lookup (priority_info_map
,
2183 (splay_tree_key
) priority
);
2186 /* Create a new priority information structure, and insert it
2188 pi
= xmalloc (sizeof (struct priority_info_s
));
2189 pi
->initializations_p
= 0;
2190 pi
->destructions_p
= 0;
2191 splay_tree_insert (priority_info_map
,
2192 (splay_tree_key
) priority
,
2193 (splay_tree_value
) pi
);
2196 pi
= (priority_info
) n
->value
;
2201 /* Set up to handle the initialization or destruction of DECL. If
2202 INITP is nonzero, we are initializing the variable. Otherwise, we
2203 are destroying it. */
2206 start_static_initialization_or_destruction (tree decl
, int initp
)
2208 tree guard_if_stmt
= NULL_TREE
;
2215 /* Figure out the priority for this declaration. */
2216 priority
= DECL_INIT_PRIORITY (decl
);
2218 priority
= DEFAULT_INIT_PRIORITY
;
2220 /* Remember that we had an initialization or finalization at this
2222 pi
= get_priority_info (priority
);
2224 pi
->initializations_p
= 1;
2226 pi
->destructions_p
= 1;
2228 /* Trick the compiler into thinking we are at the file and line
2229 where DECL was declared so that error-messages make sense, and so
2230 that the debugger will show somewhat sensible file and line
2232 input_location
= DECL_SOURCE_LOCATION (decl
);
2238 Access control for implicit calls to the constructors,
2239 the conversion functions, or the destructor called to
2240 create and destroy a static data member is performed as
2241 if these calls appeared in the scope of the member's
2244 we pretend we are in a static member function of the class of
2245 which the DECL is a member. */
2246 if (member_p (decl
))
2248 DECL_CONTEXT (current_function_decl
) = DECL_CONTEXT (decl
);
2249 DECL_STATIC_FUNCTION_P (current_function_decl
) = 1;
2252 /* Conditionalize this initialization on being in the right priority
2253 and being initializing/finalizing appropriately. */
2254 guard_if_stmt
= begin_if_stmt ();
2255 cond
= cp_build_binary_op (EQ_EXPR
,
2257 build_int_2 (priority
, 0));
2258 init_cond
= initp
? integer_one_node
: integer_zero_node
;
2259 init_cond
= cp_build_binary_op (EQ_EXPR
,
2262 cond
= cp_build_binary_op (TRUTH_ANDIF_EXPR
, cond
, init_cond
);
2264 /* Assume we don't need a guard. */
2266 /* We need a guard if this is an object with external linkage that
2267 might be initialized in more than one place. (For example, a
2268 static data member of a template, when the data member requires
2270 if (TREE_PUBLIC (decl
) && (DECL_COMMON (decl
)
2271 || DECL_ONE_ONLY (decl
)
2272 || DECL_WEAK (decl
)))
2276 guard
= get_guard (decl
);
2278 /* When using __cxa_atexit, we just check the GUARD as we would
2279 for a local static. */
2280 if (flag_use_cxa_atexit
)
2282 /* When using __cxa_atexit, we never try to destroy
2283 anything from a static destructor. */
2284 my_friendly_assert (initp
, 20000629);
2285 guard_cond
= get_guard_cond (guard
);
2287 /* If we don't have __cxa_atexit, then we will be running
2288 destructors from .fini sections, or their equivalents. So,
2289 we need to know how many times we've tried to initialize this
2290 object. We do initializations only if the GUARD is zero,
2291 i.e., if we are the first to initialize the variable. We do
2292 destructions only if the GUARD is one, i.e., if we are the
2293 last to destroy the variable. */
2296 = cp_build_binary_op (EQ_EXPR
,
2297 build_unary_op (PREINCREMENT_EXPR
,
2303 = cp_build_binary_op (EQ_EXPR
,
2304 build_unary_op (PREDECREMENT_EXPR
,
2309 cond
= cp_build_binary_op (TRUTH_ANDIF_EXPR
, cond
, guard_cond
);
2312 finish_if_stmt_cond (cond
, guard_if_stmt
);
2314 /* If we're using __cxa_atexit, we have not already set the GUARD,
2315 so we must do so now. */
2316 if (guard
&& initp
&& flag_use_cxa_atexit
)
2317 finish_expr_stmt (set_guard (guard
));
2319 return guard_if_stmt
;
2322 /* We've just finished generating code to do an initialization or
2323 finalization. GUARD_IF_STMT is the if-statement we used to guard
2324 the initialization. */
2327 finish_static_initialization_or_destruction (tree guard_if_stmt
)
2329 finish_then_clause (guard_if_stmt
);
2332 /* Now that we're done with DECL we don't need to pretend to be a
2333 member of its class any longer. */
2334 DECL_CONTEXT (current_function_decl
) = NULL_TREE
;
2335 DECL_STATIC_FUNCTION_P (current_function_decl
) = 0;
2338 /* Generate code to do the initialization of DECL, a VAR_DECL with
2339 static storage duration. The initialization is INIT. */
2342 do_static_initialization (tree decl
, tree init
)
2346 /* Set up for the initialization. */
2348 = start_static_initialization_or_destruction (decl
,
2351 /* Perform the initialization. */
2353 finish_expr_stmt (init
);
2355 /* If we're using __cxa_atexit, register a a function that calls the
2356 destructor for the object. */
2357 if (flag_use_cxa_atexit
)
2358 register_dtor_fn (decl
);
2361 finish_static_initialization_or_destruction (guard_if_stmt
);
2364 /* Generate code to do the static destruction of DECL. If DECL may be
2365 initialized more than once in different object files, GUARD is the
2366 guard variable to check. PRIORITY is the priority for the
2370 do_static_destruction (tree decl
)
2374 /* If we're using __cxa_atexit, then destructors are registered
2375 immediately after objects are initialized. */
2376 my_friendly_assert (!flag_use_cxa_atexit
, 20000121);
2378 /* If we don't need a destructor, there's nothing to do. */
2379 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl
)))
2382 /* Actually do the destruction. */
2383 guard_if_stmt
= start_static_initialization_or_destruction (decl
,
2385 finish_expr_stmt (build_cleanup (decl
));
2386 finish_static_initialization_or_destruction (guard_if_stmt
);
2389 /* VARS is a list of variables with static storage duration which may
2390 need initialization and/or finalization. Remove those variables
2391 that don't really need to be initialized or finalized, and return
2392 the resulting list. The order in which the variables appear in
2393 VARS is in reverse order of the order in which they should actually
2394 be initialized. The list we return is in the unreversed order;
2395 i.e., the first variable should be initialized first. */
2398 prune_vars_needing_no_initialization (tree
*vars
)
2401 tree result
= NULL_TREE
;
2406 tree decl
= TREE_VALUE (t
);
2407 tree init
= TREE_PURPOSE (t
);
2409 /* Deal gracefully with error. */
2410 if (decl
== error_mark_node
)
2412 var
= &TREE_CHAIN (t
);
2416 /* The only things that can be initialized are variables. */
2417 my_friendly_assert (TREE_CODE (decl
) == VAR_DECL
, 19990420);
2419 /* If this object is not defined, we don't need to do anything
2421 if (DECL_EXTERNAL (decl
))
2423 var
= &TREE_CHAIN (t
);
2427 /* Also, if the initializer already contains errors, we can bail
2429 if (init
&& TREE_CODE (init
) == TREE_LIST
2430 && value_member (error_mark_node
, init
))
2432 var
= &TREE_CHAIN (t
);
2436 /* This variable is going to need initialization and/or
2437 finalization, so we add it to the list. */
2438 *var
= TREE_CHAIN (t
);
2439 TREE_CHAIN (t
) = result
;
2446 /* Make sure we have told the back end about all the variables in
2450 write_out_vars (tree vars
)
2454 for (v
= vars
; v
; v
= TREE_CHAIN (v
))
2455 if (! TREE_ASM_WRITTEN (TREE_VALUE (v
)))
2456 rest_of_decl_compilation (TREE_VALUE (v
), 0, 1, 1);
2459 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
2460 (otherwise) that will initialize all gobal objects with static
2461 storage duration having the indicated PRIORITY. */
2464 generate_ctor_or_dtor_function (bool constructor_p
, int priority
,
2473 input_location
= *locus
;
2476 /* We use `I' to indicate initialization and `D' to indicate
2478 function_key
= constructor_p
? 'I' : 'D';
2480 /* We emit the function lazily, to avoid generating empty
2481 global constructors and destructors. */
2484 /* Call the static storage duration function with appropriate
2487 for (i
= 0; i
< ssdf_decls
->elements_used
; ++i
)
2489 fndecl
= VARRAY_TREE (ssdf_decls
, i
);
2491 /* Calls to pure or const functions will expand to nothing. */
2492 if (! (flags_from_decl_or_type (fndecl
) & (ECF_CONST
| ECF_PURE
)))
2495 body
= start_objects (function_key
, priority
);
2497 arguments
= tree_cons (NULL_TREE
, build_int_2 (priority
, 0),
2499 arguments
= tree_cons (NULL_TREE
, build_int_2 (constructor_p
, 0),
2501 finish_expr_stmt (build_function_call (fndecl
, arguments
));
2505 /* If we're generating code for the DEFAULT_INIT_PRIORITY, throw in
2506 calls to any functions marked with attributes indicating that
2507 they should be called at initialization- or destruction-time. */
2508 if (priority
== DEFAULT_INIT_PRIORITY
)
2512 for (fns
= constructor_p
? static_ctors
: static_dtors
;
2514 fns
= TREE_CHAIN (fns
))
2516 fndecl
= TREE_VALUE (fns
);
2518 /* Calls to pure/const functions will expand to nothing. */
2519 if (! (flags_from_decl_or_type (fndecl
) & (ECF_CONST
| ECF_PURE
)))
2522 body
= start_objects (function_key
, priority
);
2523 finish_expr_stmt (build_function_call (fndecl
, NULL_TREE
));
2528 /* Close out the function. */
2530 finish_objects (function_key
, priority
, body
);
2533 /* Generate constructor and destructor functions for the priority
2537 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n
, void * data
)
2539 location_t
*locus
= data
;
2540 int priority
= (int) n
->key
;
2541 priority_info pi
= (priority_info
) n
->value
;
2543 /* Generate the functions themselves, but only if they are really
2545 if (pi
->initializations_p
2546 || (priority
== DEFAULT_INIT_PRIORITY
&& static_ctors
))
2547 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority
, locus
);
2548 if (pi
->destructions_p
2549 || (priority
== DEFAULT_INIT_PRIORITY
&& static_dtors
))
2550 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority
, locus
);
2552 /* Keep iterating. */
2556 /* Callgraph code does not understand the member pointers. Mark the methods
2557 referenced as used. */
2559 mark_member_pointers_and_eh_handlers (tree
*tp
,
2561 void *data ATTRIBUTE_UNUSED
)
2563 /* Avoid useless walking of complex type and declaration nodes. */
2564 if (TYPE_P (*tp
) || DECL_P (*tp
))
2569 switch (TREE_CODE (*tp
))
2572 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (*tp
)))
2573 cgraph_mark_needed_node (cgraph_node (PTRMEM_CST_MEMBER (*tp
)), 1);
2576 /* EH handlers will emit EH tables referencing typeinfo. */
2578 if (HANDLER_TYPE (*tp
))
2580 tree tinfo
= eh_type_info (HANDLER_TYPE (*tp
));
2582 cgraph_varpool_mark_needed_node (cgraph_varpool_node (tinfo
));
2590 for (type
= EH_SPEC_RAISES ((*tp
)); type
;
2591 type
= TREE_CHAIN (type
))
2593 tree tinfo
= eh_type_info (TREE_VALUE (type
));
2595 cgraph_varpool_mark_needed_node (cgraph_varpool_node (tinfo
));
2605 /* Called via LANGHOOK_CALLGRAPH_LOWER_FUNCTION. It is supposed to lower
2606 frontend specific constructs that would otherwise confuse the middle end. */
2608 lower_function (tree fn
)
2610 walk_tree_without_duplicates (&DECL_SAVED_TREE (fn
),
2611 mark_member_pointers_and_eh_handlers
,
2615 /* This routine is called from the last rule in yyparse ().
2616 Its job is to create all the code needed to initialize and
2617 destroy the global aggregates. We do the destruction
2618 first, since that way we only need to reverse the decls once. */
2627 unsigned ssdf_count
= 0;
2629 locus
= input_location
;
2632 /* Bad parse errors. Just forget about it. */
2633 if (! global_bindings_p () || current_class_type
|| decl_namespace_list
)
2637 c_common_write_pch ();
2639 /* Otherwise, GDB can get confused, because in only knows
2640 about source for LINENO-1 lines. */
2643 interface_unknown
= 1;
2646 /* We now have to write out all the stuff we put off writing out.
2649 o Template specializations that we have not yet instantiated,
2650 but which are needed.
2651 o Initialization and destruction for non-local objects with
2652 static storage duration. (Local objects with static storage
2653 duration are initialized when their scope is first entered,
2654 and are cleaned up via atexit.)
2655 o Virtual function tables.
2657 All of these may cause others to be needed. For example,
2658 instantiating one function may cause another to be needed, and
2659 generating the initializer for an object may cause templates to be
2660 instantiated, etc., etc. */
2662 timevar_push (TV_VARCONST
);
2664 emit_support_tinfos ();
2669 size_t n_old
, n_new
;
2673 /* If there are templates that we've put off instantiating, do
2675 instantiate_pending_templates ();
2677 /* Write out virtual tables as required. Note that writing out
2678 the virtual table for a template class may cause the
2679 instantiation of members of that class. If we write out
2680 vtables then we remove the class from our list so we don't
2681 have to look at it again. */
2683 while (keyed_classes
!= NULL_TREE
2684 && maybe_emit_vtables (TREE_VALUE (keyed_classes
)))
2687 keyed_classes
= TREE_CHAIN (keyed_classes
);
2693 tree next
= TREE_CHAIN (t
);
2697 if (maybe_emit_vtables (TREE_VALUE (next
)))
2700 TREE_CHAIN (t
) = TREE_CHAIN (next
);
2705 next
= TREE_CHAIN (t
);
2709 /* Write out needed type info variables. We have to be careful
2710 looping through unemitted decls, because emit_tinfo_decl may
2711 cause other variables to be needed. We stick new elements
2712 (and old elements that we may need to reconsider) at the end
2713 of the array, then shift them back to the beginning once we're
2716 n_old
= VARRAY_ACTIVE_SIZE (unemitted_tinfo_decls
);
2717 for (i
= 0; i
< n_old
; ++i
)
2719 tree tinfo_decl
= VARRAY_TREE (unemitted_tinfo_decls
, i
);
2720 if (emit_tinfo_decl (tinfo_decl
))
2723 VARRAY_PUSH_TREE (unemitted_tinfo_decls
, tinfo_decl
);
2726 /* The only elements we want to keep are the new ones. Copy
2727 them to the beginning of the array, then get rid of the
2729 n_new
= VARRAY_ACTIVE_SIZE (unemitted_tinfo_decls
) - n_old
;
2730 memmove (&VARRAY_TREE (unemitted_tinfo_decls
, 0),
2731 &VARRAY_TREE (unemitted_tinfo_decls
, n_old
),
2732 n_new
* sizeof (tree
));
2733 memset (&VARRAY_TREE (unemitted_tinfo_decls
, n_new
),
2735 n_old
* sizeof (tree
));
2736 VARRAY_ACTIVE_SIZE (unemitted_tinfo_decls
) = n_new
;
2738 /* The list of objects with static storage duration is built up
2739 in reverse order. We clear STATIC_AGGREGATES so that any new
2740 aggregates added during the initialization of these will be
2741 initialized in the correct order when we next come around the
2743 vars
= prune_vars_needing_no_initialization (&static_aggregates
);
2749 /* We need to start a new initialization function each time
2750 through the loop. That's because we need to know which
2751 vtables have been referenced, and TREE_SYMBOL_REFERENCED
2752 isn't computed until a function is finished, and written
2753 out. That's a deficiency in the back-end. When this is
2754 fixed, these initialization functions could all become
2755 inline, with resulting performance improvements. */
2758 /* Set the line and file, so that it is obviously not from
2760 input_location
= locus
;
2761 ssdf_body
= start_static_storage_duration_function (ssdf_count
);
2763 /* Make sure the back end knows about all the variables. */
2764 write_out_vars (vars
);
2766 /* First generate code to do all the initializations. */
2767 for (v
= vars
; v
; v
= TREE_CHAIN (v
))
2768 do_static_initialization (TREE_VALUE (v
),
2771 /* Then, generate code to do all the destructions. Do these
2772 in reverse order so that the most recently constructed
2773 variable is the first destroyed. If we're using
2774 __cxa_atexit, then we don't need to do this; functions
2775 were registered at initialization time to destroy the
2777 if (!flag_use_cxa_atexit
)
2779 vars
= nreverse (vars
);
2780 for (v
= vars
; v
; v
= TREE_CHAIN (v
))
2781 do_static_destruction (TREE_VALUE (v
));
2786 /* Finish up the static storage duration function for this
2788 input_location
= locus
;
2789 finish_static_storage_duration_function (ssdf_body
);
2791 /* All those initializations and finalizations might cause
2792 us to need more inline functions, more template
2793 instantiations, etc. */
2799 for (i
= 0; i
< deferred_fns_used
; ++i
)
2801 tree decl
= VARRAY_TREE (deferred_fns
, i
);
2803 /* Does it need synthesizing? */
2804 if (DECL_ARTIFICIAL (decl
) && ! DECL_INITIAL (decl
)
2806 && (! DECL_REALLY_EXTERN (decl
) || DECL_INLINE (decl
)))
2808 /* Even though we're already at the top-level, we push
2809 there again. That way, when we pop back a few lines
2810 hence, all of our state is restored. Otherwise,
2811 finish_function doesn't clean things up, and we end
2812 up with CURRENT_FUNCTION_DECL set. */
2813 push_to_top_level ();
2814 synthesize_method (decl
);
2815 pop_from_top_level ();
2819 /* If the function has no body, avoid calling
2820 import_export_decl. On a system without weak symbols,
2821 calling import_export_decl will make an inline template
2822 instantiation "static", which will result in errors about
2823 the use of undefined functions if there is no body for
2825 if (!DECL_SAVED_TREE (decl
))
2828 import_export_decl (decl
);
2830 /* We lie to the back-end, pretending that some functions
2831 are not defined when they really are. This keeps these
2832 functions from being put out unnecessarily. But, we must
2833 stop lying when the functions are referenced, or if they
2834 are not comdat since they need to be put out now. This
2835 is done in a separate for cycle, because if some deferred
2836 function is contained in another deferred function later
2837 in deferred_fns varray, rest_of_compilation would skip
2838 this function and we really cannot expand the same
2840 if (DECL_NOT_REALLY_EXTERN (decl
)
2841 && DECL_INITIAL (decl
)
2842 && DECL_NEEDED_P (decl
))
2843 DECL_EXTERNAL (decl
) = 0;
2845 /* If we're going to need to write this function out, and
2846 there's already a body for it, create RTL for it now.
2847 (There might be no body if this is a method we haven't
2848 gotten around to synthesizing yet.) */
2849 if (!DECL_EXTERNAL (decl
)
2850 && DECL_NEEDED_P (decl
)
2851 && DECL_SAVED_TREE (decl
)
2852 && !TREE_ASM_WRITTEN (decl
)
2853 && (!flag_unit_at_a_time
2854 || !cgraph_node (decl
)->local
.finalized
))
2856 /* We will output the function; no longer consider it in this
2858 DECL_DEFER_OUTPUT (decl
) = 0;
2859 /* Generate RTL for this function now that we know we
2861 expand_or_defer_fn (decl
);
2862 /* If we're compiling -fsyntax-only pretend that this
2863 function has been written out so that we don't try to
2865 if (flag_syntax_only
)
2866 TREE_ASM_WRITTEN (decl
) = 1;
2871 if (walk_namespaces (wrapup_globals_for_namespace
, /*data=*/0))
2874 /* Static data members are just like namespace-scope globals. */
2875 for (i
= 0; i
< pending_statics_used
; ++i
)
2877 tree decl
= VARRAY_TREE (pending_statics
, i
);
2878 if (TREE_ASM_WRITTEN (decl
))
2880 import_export_decl (decl
);
2881 if (DECL_NOT_REALLY_EXTERN (decl
) && ! DECL_IN_AGGR_P (decl
))
2882 DECL_EXTERNAL (decl
) = 0;
2885 && wrapup_global_declarations (&VARRAY_TREE (pending_statics
, 0),
2886 pending_statics_used
))
2891 /* All used inline functions must have a definition at this point. */
2892 for (i
= 0; i
< deferred_fns_used
; ++i
)
2894 tree decl
= VARRAY_TREE (deferred_fns
, i
);
2896 if (TREE_USED (decl
) && DECL_DECLARED_INLINE_P (decl
)
2897 && !(TREE_ASM_WRITTEN (decl
) || DECL_SAVED_TREE (decl
)
2898 /* An explicit instantiation can be used to specify
2899 that the body is in another unit. It will have
2900 already verified there was a definition. */
2901 || DECL_EXPLICIT_INSTANTIATION (decl
)))
2903 cp_warning_at ("inline function `%D' used but never defined", decl
);
2904 /* This symbol is effectively an "extern" declaration now.
2905 This is not strictly necessary, but removes a duplicate
2907 TREE_PUBLIC (decl
) = 1;
2912 /* We give C linkage to static constructors and destructors. */
2913 push_lang_context (lang_name_c
);
2915 /* Generate initialization and destruction functions for all
2916 priorities for which they are required. */
2917 if (priority_info_map
)
2918 splay_tree_foreach (priority_info_map
,
2919 generate_ctor_and_dtor_functions_for_priority
,
2925 generate_ctor_or_dtor_function (/*constructor_p=*/true,
2926 DEFAULT_INIT_PRIORITY
, &locus
);
2928 generate_ctor_or_dtor_function (/*constructor_p=*/false,
2929 DEFAULT_INIT_PRIORITY
, &locus
);
2932 /* We're done with the splay-tree now. */
2933 if (priority_info_map
)
2934 splay_tree_delete (priority_info_map
);
2936 /* We're done with static constructors, so we can go back to "C++"
2938 pop_lang_context ();
2940 if (flag_unit_at_a_time
)
2942 cgraph_finalize_compilation_unit ();
2946 /* Now, issue warnings about static, but not defined, functions,
2947 etc., and emit debugging information. */
2948 walk_namespaces (wrapup_globals_for_namespace
, /*data=*/&reconsider
);
2949 if (pending_statics
)
2950 check_global_declarations (&VARRAY_TREE (pending_statics
, 0),
2951 pending_statics_used
);
2955 /* The entire file is now complete. If requested, dump everything
2959 FILE *stream
= dump_begin (TDI_all
, &flags
);
2963 dump_node (global_namespace
, flags
& ~TDF_SLIM
, stream
);
2964 dump_end (TDI_all
, stream
);
2968 timevar_pop (TV_VARCONST
);
2970 if (flag_detailed_statistics
)
2972 dump_tree_statistics ();
2973 dump_time_statistics ();
2975 input_location
= locus
;
2978 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
2979 function to call in parse-tree form; it has not yet been
2980 semantically analyzed. ARGS are the arguments to the function.
2981 They have already been semantically analyzed. */
2984 build_offset_ref_call_from_tree (tree fn
, tree args
)
2993 object
= TREE_OPERAND (fn
, 0);
2995 if (processing_template_decl
)
2997 my_friendly_assert (TREE_CODE (fn
) == DOTSTAR_EXPR
2998 || TREE_CODE (fn
) == MEMBER_REF
,
3000 if (type_dependent_expression_p (fn
)
3001 || any_type_dependent_arguments_p (args
))
3002 return build_min_nt (CALL_EXPR
, fn
, args
);
3004 /* Transform the arguments and add the implicit "this"
3005 parameter. That must be done before the FN is transformed
3006 because we depend on the form of FN. */
3007 args
= build_non_dependent_args (args
);
3008 if (TREE_CODE (fn
) == DOTSTAR_EXPR
)
3009 object
= build_unary_op (ADDR_EXPR
, object
, 0);
3010 object
= build_non_dependent_expr (object
);
3011 args
= tree_cons (NULL_TREE
, object
, args
);
3012 /* Now that the arguments are done, transform FN. */
3013 fn
= build_non_dependent_expr (fn
);
3016 /* A qualified name corresponding to a bound pointer-to-member is
3017 represented as an OFFSET_REF:
3019 struct B { void g(); };
3021 void B::g() { (this->*p)(); } */
3022 if (TREE_CODE (fn
) == OFFSET_REF
)
3024 tree object_addr
= build_unary_op (ADDR_EXPR
, object
, 0);
3025 fn
= TREE_OPERAND (fn
, 1);
3026 fn
= get_member_function_from_ptrfunc (&object_addr
, fn
);
3027 args
= tree_cons (NULL_TREE
, object_addr
, args
);
3030 expr
= build_function_call (fn
, args
);
3031 if (processing_template_decl
&& expr
!= error_mark_node
)
3032 return build_min (CALL_EXPR
, TREE_TYPE (expr
), orig_fn
, orig_args
);
3036 /* Returns true if ROOT (a namespace, class, or function) encloses
3037 CHILD. CHILD may be either a class type or a namespace. */
3040 is_ancestor (tree root
, tree child
)
3042 my_friendly_assert ((TREE_CODE (root
) == NAMESPACE_DECL
3043 || TREE_CODE (root
) == FUNCTION_DECL
3044 || CLASS_TYPE_P (root
)), 20030307);
3045 my_friendly_assert ((TREE_CODE (child
) == NAMESPACE_DECL
3046 || CLASS_TYPE_P (child
)),
3049 /* The global namespace encloses everything. */
3050 if (root
== global_namespace
)
3055 /* If we've run out of scopes, stop. */
3058 /* If we've reached the ROOT, it encloses CHILD. */
3061 /* Go out one level. */
3063 child
= TYPE_NAME (child
);
3064 child
= DECL_CONTEXT (child
);
3069 /* Return the namespace that is the common ancestor
3070 of two given namespaces. */
3073 namespace_ancestor (tree ns1
, tree ns2
)
3075 timevar_push (TV_NAME_LOOKUP
);
3076 if (is_ancestor (ns1
, ns2
))
3077 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, ns1
);
3078 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
,
3079 namespace_ancestor (CP_DECL_CONTEXT (ns1
), ns2
));
3082 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
3083 directive is not directly from the source. Also find the common
3084 ancestor and let our users know about the new namespace */
3086 add_using_namespace (tree user
, tree used
, bool indirect
)
3089 timevar_push (TV_NAME_LOOKUP
);
3090 /* Using oneself is a no-op. */
3093 timevar_pop (TV_NAME_LOOKUP
);
3096 my_friendly_assert (TREE_CODE (user
) == NAMESPACE_DECL
, 380);
3097 my_friendly_assert (TREE_CODE (used
) == NAMESPACE_DECL
, 380);
3098 /* Check if we already have this. */
3099 t
= purpose_member (used
, DECL_NAMESPACE_USING (user
));
3103 /* Promote to direct usage. */
3104 TREE_INDIRECT_USING (t
) = 0;
3105 timevar_pop (TV_NAME_LOOKUP
);
3109 /* Add used to the user's using list. */
3110 DECL_NAMESPACE_USING (user
)
3111 = tree_cons (used
, namespace_ancestor (user
, used
),
3112 DECL_NAMESPACE_USING (user
));
3114 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user
)) = indirect
;
3116 /* Add user to the used's users list. */
3117 DECL_NAMESPACE_USERS (used
)
3118 = tree_cons (user
, 0, DECL_NAMESPACE_USERS (used
));
3120 /* Recursively add all namespaces used. */
3121 for (t
= DECL_NAMESPACE_USING (used
); t
; t
= TREE_CHAIN (t
))
3122 /* indirect usage */
3123 add_using_namespace (user
, TREE_PURPOSE (t
), 1);
3125 /* Tell everyone using us about the new used namespaces. */
3126 for (t
= DECL_NAMESPACE_USERS (user
); t
; t
= TREE_CHAIN (t
))
3127 add_using_namespace (TREE_PURPOSE (t
), used
, 1);
3128 timevar_pop (TV_NAME_LOOKUP
);
3131 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
3132 duplicates. The first list becomes the tail of the result.
3134 The algorithm is O(n^2). We could get this down to O(n log n) by
3135 doing a sort on the addresses of the functions, if that becomes
3139 merge_functions (tree s1
, tree s2
)
3141 for (; s2
; s2
= OVL_NEXT (s2
))
3143 tree fn2
= OVL_CURRENT (s2
);
3146 for (fns1
= s1
; fns1
; fns1
= OVL_NEXT (fns1
))
3148 tree fn1
= OVL_CURRENT (fns1
);
3150 /* If the function from S2 is already in S1, there is no
3151 need to add it again. For `extern "C"' functions, we
3152 might have two FUNCTION_DECLs for the same function, in
3153 different namespaces; again, we only need one of them. */
3155 || (DECL_EXTERN_C_P (fn1
) && DECL_EXTERN_C_P (fn2
)
3156 && DECL_NAME (fn1
) == DECL_NAME (fn2
)))
3160 /* If we exhausted all of the functions in S1, FN2 is new. */
3162 s1
= build_overload (fn2
, s1
);
3167 /* This should return an error not all definitions define functions.
3168 It is not an error if we find two functions with exactly the
3169 same signature, only if these are selected in overload resolution.
3170 old is the current set of bindings, new the freshly-found binding.
3171 XXX Do we want to give *all* candidates in case of ambiguity?
3172 XXX In what way should I treat extern declarations?
3173 XXX I don't want to repeat the entire duplicate_decls here */
3175 static cxx_binding
*
3176 ambiguous_decl (tree name
, cxx_binding
*old
, cxx_binding
*new, int flags
)
3179 my_friendly_assert (old
!= NULL
, 393);
3180 /* Copy the value. */
3181 val
= BINDING_VALUE (new);
3183 switch (TREE_CODE (val
))
3186 /* If we expect types or namespaces, and not templates,
3187 or this is not a template class. */
3188 if (LOOKUP_QUALIFIERS_ONLY (flags
)
3189 && !DECL_CLASS_TEMPLATE_P (val
))
3193 if (LOOKUP_NAMESPACES_ONLY (flags
))
3196 case NAMESPACE_DECL
:
3197 if (LOOKUP_TYPES_ONLY (flags
))
3201 /* Ignore built-in functions that are still anticipated. */
3202 if (LOOKUP_QUALIFIERS_ONLY (flags
) || DECL_ANTICIPATED (val
))
3206 if (LOOKUP_QUALIFIERS_ONLY (flags
))
3210 if (!BINDING_VALUE (old
))
3211 BINDING_VALUE (old
) = val
;
3212 else if (val
&& val
!= BINDING_VALUE (old
))
3214 if (is_overloaded_fn (BINDING_VALUE (old
)) && is_overloaded_fn (val
))
3215 BINDING_VALUE (old
) = merge_functions (BINDING_VALUE (old
), val
);
3218 /* Some declarations are functions, some are not. */
3219 if (flags
& LOOKUP_COMPLAIN
)
3221 /* If we've already given this error for this lookup,
3222 BINDING_VALUE (old) is error_mark_node, so let's not
3223 repeat ourselves. */
3224 if (BINDING_VALUE (old
) != error_mark_node
)
3226 error ("use of `%D' is ambiguous", name
);
3227 cp_error_at (" first declared as `%#D' here",
3228 BINDING_VALUE (old
));
3230 cp_error_at (" also declared as `%#D' here", val
);
3232 BINDING_VALUE (old
) = error_mark_node
;
3235 /* ... and copy the type. */
3236 type
= BINDING_TYPE (new);
3237 if (LOOKUP_NAMESPACES_ONLY (flags
))
3239 if (!BINDING_TYPE (old
))
3240 BINDING_TYPE (old
) = type
;
3241 else if (type
&& BINDING_TYPE (old
) != type
)
3243 if (flags
& LOOKUP_COMPLAIN
)
3245 error ("`%D' denotes an ambiguous type",name
);
3246 error ("%H first type here",
3247 &DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (BINDING_TYPE (old
))));
3248 error ("%H other type here",
3249 &DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)));
3255 /* Subroutine of unualified_namespace_lookup:
3256 Add the bindings of NAME in used namespaces to VAL.
3257 We are currently looking for names in namespace SCOPE, so we
3258 look through USINGS for using-directives of namespaces
3259 which have SCOPE as a common ancestor with the current scope.
3260 Returns false on errors. */
3263 lookup_using_namespace (tree name
, cxx_binding
*val
, tree usings
, tree scope
,
3264 int flags
, tree
*spacesp
)
3267 timevar_push (TV_NAME_LOOKUP
);
3268 /* Iterate over all used namespaces in current, searching for using
3269 directives of scope. */
3270 for (iter
= usings
; iter
; iter
= TREE_CHAIN (iter
))
3271 if (TREE_VALUE (iter
) == scope
)
3273 tree used
= ORIGINAL_NAMESPACE (TREE_PURPOSE (iter
));
3275 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (used
), name
);
3277 *spacesp
= tree_cons (used
, NULL_TREE
, *spacesp
);
3278 /* Resolve ambiguities. */
3280 val
= ambiguous_decl (name
, val
, val1
, flags
);
3282 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
,
3283 BINDING_VALUE (val
) != error_mark_node
);
3287 Accepts the NAME to lookup and its qualifying SCOPE.
3288 Returns the name/type pair found into the cxx_binding *RESULT,
3289 or false on error. */
3292 qualified_lookup_using_namespace (tree name
, tree scope
, cxx_binding
*result
,
3295 /* Maintain a list of namespaces visited... */
3296 tree seen
= NULL_TREE
;
3297 /* ... and a list of namespace yet to see. */
3298 tree todo
= NULL_TREE
;
3300 timevar_push (TV_NAME_LOOKUP
);
3301 /* Look through namespace aliases. */
3302 scope
= ORIGINAL_NAMESPACE (scope
);
3303 while (scope
&& result
->value
!= error_mark_node
)
3305 cxx_binding
*binding
=
3306 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope
), name
);
3307 seen
= tree_cons (scope
, NULL_TREE
, seen
);
3309 result
= ambiguous_decl (name
, result
, binding
, flags
);
3310 if (!BINDING_VALUE (result
) && !BINDING_TYPE (result
))
3311 /* Consider using directives. */
3312 for (usings
= DECL_NAMESPACE_USING (scope
); usings
;
3313 usings
= TREE_CHAIN (usings
))
3314 /* If this was a real directive, and we have not seen it. */
3315 if (!TREE_INDIRECT_USING (usings
)
3316 && !purpose_member (TREE_PURPOSE (usings
), seen
))
3317 todo
= tree_cons (TREE_PURPOSE (usings
), NULL_TREE
, todo
);
3320 scope
= TREE_PURPOSE (todo
);
3321 todo
= TREE_CHAIN (todo
);
3324 scope
= NULL_TREE
; /* If there never was a todo list. */
3326 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, result
->value
!= error_mark_node
);
3329 /* [namespace.memdef]/2 */
3331 /* Set the context of a declaration to scope. Complain if we are not
3335 set_decl_namespace (tree decl
, tree scope
, bool friendp
)
3339 /* Get rid of namespace aliases. */
3340 scope
= ORIGINAL_NAMESPACE (scope
);
3342 /* It is ok for friends to be qualified in parallel space. */
3343 if (!friendp
&& !is_ancestor (current_namespace
, scope
))
3344 error ("declaration of `%D' not in a namespace surrounding `%D'",
3346 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
3347 if (scope
!= current_namespace
)
3349 /* See whether this has been declared in the namespace. */
3350 old
= namespace_binding (DECL_NAME (decl
), scope
);
3352 /* No old declaration at all. */
3354 /* A template can be explicitly specialized in any namespace. */
3355 if (processing_explicit_instantiation
)
3357 if (!is_overloaded_fn (decl
))
3358 /* Don't compare non-function decls with decls_match here,
3359 since it can't check for the correct constness at this
3360 point. pushdecl will find those errors later. */
3362 /* Since decl is a function, old should contain a function decl. */
3363 if (!is_overloaded_fn (old
))
3365 if (processing_template_decl
|| processing_specialization
)
3366 /* We have not yet called push_template_decl to turn a
3367 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations
3368 won't match. But, we'll check later, when we construct the
3371 if (is_overloaded_fn (old
))
3373 for (; old
; old
= OVL_NEXT (old
))
3374 if (decls_match (decl
, OVL_CURRENT (old
)))
3378 if (decls_match (decl
, old
))
3384 error ("`%D' should have been declared inside `%D'",
3388 /* Compute the namespace where a declaration is defined. */
3391 decl_namespace (tree decl
)
3393 timevar_push (TV_NAME_LOOKUP
);
3395 decl
= TYPE_STUB_DECL (decl
);
3396 while (DECL_CONTEXT (decl
))
3398 decl
= DECL_CONTEXT (decl
);
3399 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
3400 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, decl
);
3402 decl
= TYPE_STUB_DECL (decl
);
3403 my_friendly_assert (DECL_P (decl
), 390);
3406 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, global_namespace
);
3409 /* Return the namespace where the current declaration is declared. */
3412 current_decl_namespace (void)
3415 /* If we have been pushed into a different namespace, use it. */
3416 if (decl_namespace_list
)
3417 return TREE_PURPOSE (decl_namespace_list
);
3419 if (current_class_type
)
3420 result
= decl_namespace (TYPE_STUB_DECL (current_class_type
));
3421 else if (current_function_decl
)
3422 result
= decl_namespace (current_function_decl
);
3424 result
= current_namespace
;
3428 /* Temporarily set the namespace for the current declaration. */
3431 push_decl_namespace (tree decl
)
3433 if (TREE_CODE (decl
) != NAMESPACE_DECL
)
3434 decl
= decl_namespace (decl
);
3435 decl_namespace_list
= tree_cons (ORIGINAL_NAMESPACE (decl
),
3436 NULL_TREE
, decl_namespace_list
);
3440 pop_decl_namespace (void)
3442 decl_namespace_list
= TREE_CHAIN (decl_namespace_list
);
3445 /* Enter a class or namespace scope. */
3450 if (TREE_CODE (t
) == NAMESPACE_DECL
)
3451 push_decl_namespace (t
);
3452 else if CLASS_TYPE_P (t
)
3453 push_nested_class (t
);
3456 /* Leave scope pushed by push_scope. */
3461 if (TREE_CODE (t
) == NAMESPACE_DECL
)
3462 pop_decl_namespace ();
3463 else if CLASS_TYPE_P (t
)
3464 pop_nested_class ();
3467 /* [basic.lookup.koenig] */
3468 /* A nonzero return value in the functions below indicates an error. */
3478 static bool arg_assoc (struct arg_lookup
*, tree
);
3479 static bool arg_assoc_args (struct arg_lookup
*, tree
);
3480 static bool arg_assoc_type (struct arg_lookup
*, tree
);
3481 static bool add_function (struct arg_lookup
*, tree
);
3482 static bool arg_assoc_namespace (struct arg_lookup
*, tree
);
3483 static bool arg_assoc_class (struct arg_lookup
*, tree
);
3484 static bool arg_assoc_template_arg (struct arg_lookup
*, tree
);
3486 /* Add a function to the lookup structure.
3487 Returns true on error. */
3490 add_function (struct arg_lookup
*k
, tree fn
)
3492 /* We used to check here to see if the function was already in the list,
3493 but that's O(n^2), which is just too expensive for function lookup.
3494 Now we deal with the occasional duplicate in joust. In doing this, we
3495 assume that the number of duplicates will be small compared to the
3496 total number of functions being compared, which should usually be the
3499 /* We must find only functions, or exactly one non-function. */
3502 else if (is_overloaded_fn (k
->functions
) && is_overloaded_fn (fn
))
3503 k
->functions
= build_overload (fn
, k
->functions
);
3506 tree f1
= OVL_CURRENT (k
->functions
);
3508 if (is_overloaded_fn (f1
))
3510 fn
= f1
; f1
= f2
; f2
= fn
;
3512 cp_error_at ("`%D' is not a function,", f1
);
3513 cp_error_at (" conflict with `%D'", f2
);
3514 error (" in call to `%D'", k
->name
);
3521 /* Add functions of a namespace to the lookup structure.
3522 Returns true on error. */
3525 arg_assoc_namespace (struct arg_lookup
*k
, tree scope
)
3529 if (purpose_member (scope
, k
->namespaces
))
3531 k
->namespaces
= tree_cons (scope
, NULL_TREE
, k
->namespaces
);
3533 value
= namespace_binding (k
->name
, scope
);
3537 for (; value
; value
= OVL_NEXT (value
))
3538 if (add_function (k
, OVL_CURRENT (value
)))
3544 /* Adds everything associated with a template argument to the lookup
3545 structure. Returns true on error. */
3548 arg_assoc_template_arg (struct arg_lookup
*k
, tree arg
)
3550 /* [basic.lookup.koenig]
3552 If T is a template-id, its associated namespaces and classes are
3553 ... the namespaces and classes associated with the types of the
3554 template arguments provided for template type parameters
3555 (excluding template template parameters); the namespaces in which
3556 any template template arguments are defined; and the classes in
3557 which any member templates used as template template arguments
3558 are defined. [Note: non-type template arguments do not
3559 contribute to the set of associated namespaces. ] */
3561 /* Consider first template template arguments. */
3562 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
3563 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
3565 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
3567 tree ctx
= CP_DECL_CONTEXT (arg
);
3569 /* It's not a member template. */
3570 if (TREE_CODE (ctx
) == NAMESPACE_DECL
)
3571 return arg_assoc_namespace (k
, ctx
);
3572 /* Otherwise, it must be member template. */
3574 return arg_assoc_class (k
, ctx
);
3576 /* It's not a template template argument, but it is a type template
3578 else if (TYPE_P (arg
))
3579 return arg_assoc_type (k
, arg
);
3580 /* It's a non-type template argument. */
3585 /* Adds everything associated with class to the lookup structure.
3586 Returns true on error. */
3589 arg_assoc_class (struct arg_lookup
*k
, tree type
)
3591 tree list
, friends
, context
;
3594 /* Backend build structures, such as __builtin_va_list, aren't
3595 affected by all this. */
3596 if (!CLASS_TYPE_P (type
))
3599 if (purpose_member (type
, k
->classes
))
3601 k
->classes
= tree_cons (type
, NULL_TREE
, k
->classes
);
3603 context
= decl_namespace (TYPE_MAIN_DECL (type
));
3604 if (arg_assoc_namespace (k
, context
))
3607 /* Process baseclasses. */
3608 for (i
= 0; i
< CLASSTYPE_N_BASECLASSES (type
); i
++)
3609 if (arg_assoc_class (k
, TYPE_BINFO_BASETYPE (type
, i
)))
3612 /* Process friends. */
3613 for (list
= DECL_FRIENDLIST (TYPE_MAIN_DECL (type
)); list
;
3614 list
= TREE_CHAIN (list
))
3615 if (k
->name
== FRIEND_NAME (list
))
3616 for (friends
= FRIEND_DECLS (list
); friends
;
3617 friends
= TREE_CHAIN (friends
))
3618 /* Only interested in global functions with potentially hidden
3619 (i.e. unqualified) declarations. */
3620 if (CP_DECL_CONTEXT (TREE_VALUE (friends
)) == context
)
3621 if (add_function (k
, TREE_VALUE (friends
)))
3624 /* Process template arguments. */
3625 if (CLASSTYPE_TEMPLATE_INFO (type
))
3627 list
= INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
3628 for (i
= 0; i
< TREE_VEC_LENGTH (list
); ++i
)
3629 arg_assoc_template_arg (k
, TREE_VEC_ELT (list
, i
));
3635 /* Adds everything associated with a given type.
3636 Returns 1 on error. */
3639 arg_assoc_type (struct arg_lookup
*k
, tree type
)
3641 /* As we do not get the type of non-type dependent expressions
3642 right, we can end up with such things without a type. */
3646 if (TYPE_PTRMEM_P (type
))
3648 /* Pointer to member: associate class type and value type. */
3649 if (arg_assoc_type (k
, TYPE_PTRMEM_CLASS_TYPE (type
)))
3651 return arg_assoc_type (k
, TYPE_PTRMEM_POINTED_TO_TYPE (type
));
3653 else switch (TREE_CODE (type
))
3666 if (TYPE_PTRMEMFUNC_P (type
))
3667 return arg_assoc_type (k
, TYPE_PTRMEMFUNC_FN_TYPE (type
));
3668 return arg_assoc_class (k
, type
);
3670 case REFERENCE_TYPE
:
3672 return arg_assoc_type (k
, TREE_TYPE (type
));
3675 return arg_assoc_namespace (k
, decl_namespace (TYPE_MAIN_DECL (type
)));
3677 /* The basetype is referenced in the first arg type, so just
3680 /* Associate the parameter types. */
3681 if (arg_assoc_args (k
, TYPE_ARG_TYPES (type
)))
3683 /* Associate the return type. */
3684 return arg_assoc_type (k
, TREE_TYPE (type
));
3685 case TEMPLATE_TYPE_PARM
:
3686 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3691 if (type
== unknown_type_node
)
3693 /* else fall through */
3700 /* Adds everything associated with arguments. Returns true on error. */
3703 arg_assoc_args (struct arg_lookup
*k
, tree args
)
3705 for (; args
; args
= TREE_CHAIN (args
))
3706 if (arg_assoc (k
, TREE_VALUE (args
)))
3711 /* Adds everything associated with a given tree_node. Returns 1 on error. */
3714 arg_assoc (struct arg_lookup
*k
, tree n
)
3716 if (n
== error_mark_node
)
3720 return arg_assoc_type (k
, n
);
3722 if (! type_unknown_p (n
))
3723 return arg_assoc_type (k
, TREE_TYPE (n
));
3725 if (TREE_CODE (n
) == ADDR_EXPR
)
3726 n
= TREE_OPERAND (n
, 0);
3727 if (TREE_CODE (n
) == COMPONENT_REF
)
3728 n
= TREE_OPERAND (n
, 1);
3729 if (TREE_CODE (n
) == OFFSET_REF
)
3730 n
= TREE_OPERAND (n
, 1);
3731 while (TREE_CODE (n
) == TREE_LIST
)
3733 if (TREE_CODE (n
) == BASELINK
)
3734 n
= BASELINK_FUNCTIONS (n
);
3736 if (TREE_CODE (n
) == FUNCTION_DECL
)
3737 return arg_assoc_type (k
, TREE_TYPE (n
));
3738 if (TREE_CODE (n
) == TEMPLATE_ID_EXPR
)
3740 /* [basic.lookup.koenig]
3742 If T is a template-id, its associated namespaces and classes
3743 are the namespace in which the template is defined; for
3744 member templates, the member template's class... */
3745 tree
template = TREE_OPERAND (n
, 0);
3746 tree args
= TREE_OPERAND (n
, 1);
3750 if (TREE_CODE (template) == COMPONENT_REF
)
3751 template = TREE_OPERAND (template, 1);
3753 /* First, the template. There may actually be more than one if
3754 this is an overloaded function template. But, in that case,
3755 we only need the first; all the functions will be in the same
3757 template = OVL_CURRENT (template);
3759 ctx
= CP_DECL_CONTEXT (template);
3761 if (TREE_CODE (ctx
) == NAMESPACE_DECL
)
3763 if (arg_assoc_namespace (k
, ctx
) == 1)
3766 /* It must be a member template. */
3767 else if (arg_assoc_class (k
, ctx
) == 1)
3770 /* Now the arguments. */
3771 for (ix
= TREE_VEC_LENGTH (args
); ix
--;)
3772 if (arg_assoc_template_arg (k
, TREE_VEC_ELT (args
, ix
)) == 1)
3777 my_friendly_assert (TREE_CODE (n
) == OVERLOAD
, 980715);
3779 for (; n
; n
= OVL_CHAIN (n
))
3780 if (arg_assoc_type (k
, TREE_TYPE (OVL_FUNCTION (n
))))
3787 /* Performs Koenig lookup depending on arguments, where fns
3788 are the functions found in normal lookup. */
3791 lookup_arg_dependent (tree name
, tree fns
, tree args
)
3793 struct arg_lookup k
;
3794 tree fn
= NULL_TREE
;
3796 timevar_push (TV_NAME_LOOKUP
);
3799 k
.classes
= NULL_TREE
;
3801 /* Note that we've already looked at some namespaces during normal
3802 unqualified lookup, unless we found a decl in function scope. */
3804 fn
= OVL_CURRENT (fns
);
3805 if (fn
&& TREE_CODE (fn
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (fn
))
3806 k
.namespaces
= NULL_TREE
;
3808 unqualified_namespace_lookup (name
, 0, &k
.namespaces
);
3810 arg_assoc_args (&k
, args
);
3811 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, k
.functions
);
3814 /* Process a namespace-alias declaration. */
3817 do_namespace_alias (tree alias
, tree
namespace)
3819 if (TREE_CODE (namespace) != NAMESPACE_DECL
)
3821 /* The parser did not find it, so it's not there. */
3822 error ("unknown namespace `%D'", namespace);
3826 namespace = ORIGINAL_NAMESPACE (namespace);
3828 /* Build the alias. */
3829 alias
= build_lang_decl (NAMESPACE_DECL
, alias
, void_type_node
);
3830 DECL_NAMESPACE_ALIAS (alias
) = namespace;
3831 DECL_EXTERNAL (alias
) = 1;
3835 /* Check a non-member using-declaration. Return the name and scope
3836 being used, and the USING_DECL, or NULL_TREE on failure. */
3839 validate_nonmember_using_decl (tree decl
, tree
*scope
, tree
*name
)
3841 *scope
= global_namespace
;
3844 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
)
3846 *name
= TREE_OPERAND (decl
, 0);
3848 A using-declaration shall not name a template-id. */
3849 error ("a using-declaration cannot specify a template-id. Try `using %D'", *name
);
3853 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
3855 error ("namespace `%D' not allowed in using-declaration", decl
);
3859 if (TREE_CODE (decl
) == SCOPE_REF
)
3861 /* It's a nested name with template parameter dependent scope.
3862 This can only be using-declaration for class member. */
3863 error ("`%T' is not a namespace", TREE_OPERAND (decl
, 0));
3867 if (is_overloaded_fn (decl
))
3868 decl
= get_first_fn (decl
);
3870 my_friendly_assert (DECL_P (decl
), 20020908);
3872 if (TREE_CODE (decl
) == CONST_DECL
)
3873 /* Enumeration constants to not have DECL_CONTEXT set. */
3874 *scope
= TYPE_CONTEXT (TREE_TYPE (decl
));
3876 *scope
= DECL_CONTEXT (decl
);
3878 *scope
= global_namespace
;
3880 /* [namespace.udecl]
3881 A using-declaration for a class member shall be a
3882 member-declaration. */
3883 if (TYPE_P (*scope
))
3885 error ("`%T' is not a namespace", *scope
);
3888 *name
= DECL_NAME (decl
);
3889 /* Make a USING_DECL. */
3890 return push_using_decl (*scope
, *name
);
3893 /* Process local and global using-declarations. */
3896 do_nonmember_using_decl (tree scope
, tree name
, tree oldval
, tree oldtype
,
3897 tree
*newval
, tree
*newtype
)
3901 *newval
= *newtype
= NULL_TREE
;
3902 cxx_binding_clear (&decls
);
3903 if (!qualified_lookup_using_namespace (name
, scope
, &decls
, 0))
3907 if (!decls
.value
&& !decls
.type
)
3909 error ("`%D' not declared", name
);
3913 /* Check for using functions. */
3914 if (decls
.value
&& is_overloaded_fn (decls
.value
))
3918 if (oldval
&& !is_overloaded_fn (oldval
))
3920 if (!DECL_IMPLICIT_TYPEDEF_P (oldval
))
3921 error ("`%D' is already declared in this scope", name
);
3926 for (tmp
= decls
.value
; tmp
; tmp
= OVL_NEXT (tmp
))
3928 tree new_fn
= OVL_CURRENT (tmp
);
3930 /* [namespace.udecl]
3932 If a function declaration in namespace scope or block
3933 scope has the same name and the same parameter types as a
3934 function introduced by a using declaration the program is
3936 for (tmp1
= oldval
; tmp1
; tmp1
= OVL_NEXT (tmp1
))
3938 tree old_fn
= OVL_CURRENT (tmp1
);
3940 if (new_fn
== old_fn
)
3941 /* The function already exists in the current namespace. */
3943 else if (OVL_USED (tmp1
))
3944 continue; /* this is a using decl */
3945 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn
)),
3946 TYPE_ARG_TYPES (TREE_TYPE (old_fn
))))
3948 /* There was already a non-using declaration in
3949 this scope with the same parameter types. If both
3950 are the same extern "C" functions, that's ok. */
3951 if (decls_match (new_fn
, old_fn
))
3953 /* If the OLD_FN was a builtin, there is now a
3954 real declaration. */
3955 if (DECL_ANTICIPATED (old_fn
))
3956 DECL_ANTICIPATED (old_fn
) = 0;
3959 else if (!DECL_ANTICIPATED (old_fn
))
3961 /* If the OLD_FN was really declared, the
3962 declarations don't match. */
3963 error ("`%D' is already declared in this scope", name
);
3967 /* If the OLD_FN was not really there, just ignore
3968 it and keep going. */
3972 /* If we broke out of the loop, there's no reason to add
3973 this function to the using declarations for this
3978 *newval
= build_overload (OVL_CURRENT (tmp
), *newval
);
3979 if (TREE_CODE (*newval
) != OVERLOAD
)
3980 *newval
= ovl_cons (*newval
, NULL_TREE
);
3981 OVL_USED (*newval
) = 1;
3986 *newval
= decls
.value
;
3987 if (oldval
&& !decls_match (*newval
, oldval
))
3988 error ("`%D' is already declared in this scope", name
);
3991 *newtype
= decls
.type
;
3992 if (oldtype
&& *newtype
&& !same_type_p (oldtype
, *newtype
))
3994 error ("using declaration `%D' introduced ambiguous type `%T'",
4000 /* Process a using-declaration not appearing in class or local scope. */
4003 do_toplevel_using_decl (tree decl
)
4006 tree oldval
, oldtype
, newval
, newtype
;
4007 cxx_binding
*binding
;
4009 decl
= validate_nonmember_using_decl (decl
, &scope
, &name
);
4010 if (decl
== NULL_TREE
)
4013 binding
= binding_for_name (NAMESPACE_LEVEL (current_namespace
), name
);
4015 oldval
= BINDING_VALUE (binding
);
4016 oldtype
= BINDING_TYPE (binding
);
4018 do_nonmember_using_decl (scope
, name
, oldval
, oldtype
, &newval
, &newtype
);
4020 /* Copy declarations found. */
4022 BINDING_VALUE (binding
) = newval
;
4024 BINDING_TYPE (binding
) = newtype
;
4028 /* Process a using-declaration at function scope. */
4031 do_local_using_decl (tree decl
)
4034 tree oldval
, oldtype
, newval
, newtype
;
4036 decl
= validate_nonmember_using_decl (decl
, &scope
, &name
);
4037 if (decl
== NULL_TREE
)
4040 if (building_stmt_tree ()
4041 && at_function_scope_p ())
4042 add_decl_stmt (decl
);
4044 oldval
= lookup_name_current_level (name
);
4045 oldtype
= lookup_type_current_level (name
);
4047 do_nonmember_using_decl (scope
, name
, oldval
, oldtype
, &newval
, &newtype
);
4051 if (is_overloaded_fn (newval
))
4055 /* We only need to push declarations for those functions
4056 that were not already bound in the current level.
4057 The old value might be NULL_TREE, it might be a single
4058 function, or an OVERLOAD. */
4059 if (oldval
&& TREE_CODE (oldval
) == OVERLOAD
)
4060 term
= OVL_FUNCTION (oldval
);
4063 for (fn
= newval
; fn
&& OVL_CURRENT (fn
) != term
;
4065 push_overloaded_decl (OVL_CURRENT (fn
),
4066 PUSH_LOCAL
| PUSH_USING
);
4069 push_local_binding (name
, newval
, PUSH_USING
);
4072 set_identifier_type_value (name
, newtype
);
4076 do_class_using_decl (tree decl
)
4078 tree name
, value
, scope
, type
;
4080 if (TREE_CODE (decl
) != SCOPE_REF
4081 || !TREE_OPERAND (decl
, 0)
4082 || !TYPE_P (TREE_OPERAND (decl
, 0)))
4084 error ("using-declaration for non-member at class scope");
4087 scope
= TREE_OPERAND (decl
, 0);
4088 name
= TREE_OPERAND (decl
, 1);
4089 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
4091 error ("using-declaration cannot name destructor");
4094 if (TREE_CODE (name
) == TYPE_DECL
)
4095 name
= DECL_NAME (name
);
4096 else if (TREE_CODE (name
) == TEMPLATE_DECL
)
4097 name
= DECL_NAME (name
);
4098 else if (BASELINK_P (name
))
4100 tree fns
= BASELINK_FUNCTIONS (name
);
4101 name
= DECL_NAME (get_first_fn (fns
));
4104 my_friendly_assert (TREE_CODE (name
) == IDENTIFIER_NODE
, 980716);
4106 /* Dependent using decls have a NULL type, non-dependent ones have a
4108 type
= dependent_type_p (scope
) ? NULL_TREE
: void_type_node
;
4109 value
= build_lang_decl (USING_DECL
, name
, type
);
4110 DECL_INITIAL (value
) = scope
;
4114 /* Process a using-directive. */
4117 do_using_directive (tree
namespace)
4119 if (building_stmt_tree ())
4120 add_stmt (build_stmt (USING_STMT
, namespace));
4122 /* using namespace A::B::C; */
4123 if (TREE_CODE (namespace) == SCOPE_REF
)
4124 namespace = TREE_OPERAND (namespace, 1);
4125 if (TREE_CODE (namespace) == IDENTIFIER_NODE
)
4127 /* Lookup in lexer did not find a namespace. */
4128 if (!processing_template_decl
)
4129 error ("namespace `%T' undeclared", namespace);
4132 if (TREE_CODE (namespace) != NAMESPACE_DECL
)
4134 if (!processing_template_decl
)
4135 error ("`%T' is not a namespace", namespace);
4138 namespace = ORIGINAL_NAMESPACE (namespace);
4139 if (!toplevel_bindings_p ())
4140 push_using_directive (namespace);
4143 add_using_namespace (current_namespace
, namespace, 0);
4147 check_default_args (tree x
)
4149 tree arg
= TYPE_ARG_TYPES (TREE_TYPE (x
));
4150 bool saw_def
= false;
4151 int i
= 0 - (TREE_CODE (TREE_TYPE (x
)) == METHOD_TYPE
);
4152 for (; arg
&& arg
!= void_list_node
; arg
= TREE_CHAIN (arg
), ++i
)
4154 if (TREE_PURPOSE (arg
))
4158 cp_error_at ("default argument missing for parameter %P of `%+#D'",
4166 mark_used (tree decl
)
4168 TREE_USED (decl
) = 1;
4169 if (processing_template_decl
|| skip_evaluation
)
4172 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_DECLARED_INLINE_P (decl
)
4173 && !TREE_ASM_WRITTEN (decl
))
4174 /* Remember it, so we can check it was defined. */
4177 assemble_external (decl
);
4179 /* Is it a synthesized method that needs to be synthesized? */
4180 if (TREE_CODE (decl
) == FUNCTION_DECL
4181 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
)
4182 && DECL_ARTIFICIAL (decl
)
4183 && !DECL_THUNK_P (decl
)
4184 && ! DECL_INITIAL (decl
)
4185 /* Kludge: don't synthesize for default args. */
4186 && current_function_decl
)
4188 synthesize_method (decl
);
4189 /* If we've already synthesized the method we don't need to
4190 instantiate it, so we can return right away. */
4194 /* If this is a function or variable that is an instance of some
4195 template, we now know that we will need to actually do the
4196 instantiation. We check that DECL is not an explicit
4197 instantiation because that is not checked in instantiate_decl. */
4198 if ((DECL_NON_THUNK_FUNCTION_P (decl
) || TREE_CODE (decl
) == VAR_DECL
)
4199 && DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
4200 && (!DECL_EXPLICIT_INSTANTIATION (decl
)
4201 || (TREE_CODE (decl
) == FUNCTION_DECL
4202 && DECL_INLINE (DECL_TEMPLATE_RESULT
4203 (template_for_substitution (decl
))))))
4207 /* Normally, we put off instantiating functions in order to
4208 improve compile times. Maintaining a stack of active
4209 functions is expensive, and the inliner knows to
4210 instantiate any functions it might need.
4212 However, if instantiating this function might help us mark
4213 the current function TREE_NOTHROW, we go ahead and
4214 instantiate it now. */
4215 defer
= (!flag_exceptions
4216 || TREE_CODE (decl
) != FUNCTION_DECL
4217 /* If the called function can't throw, we don't need to
4218 generate its body to find that out. */
4219 || TREE_NOTHROW (decl
)
4221 /* If we already know the current function can't throw,
4222 then we don't need to work hard to prove it. */
4223 || TREE_NOTHROW (current_function_decl
)
4224 /* If we already know that the current function *can*
4225 throw, there's no point in gathering more
4227 || cp_function_chain
->can_throw
);
4229 /* Our caller is likely to have lots of data on the stack. */
4230 ggc_push_context ();
4231 instantiate_decl (decl
, defer
);
4236 #include "gt-cp-decl2.h"