1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002 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"
49 extern cpp_reader *parse_in;
51 /* This structure contains information about the initializations
52 and/or destructions required for a particular priority level. */
53 typedef struct priority_info_s {
54 /* Nonzero if there have been any initializations at this priority
55 throughout the translation unit. */
56 int initializations_p;
57 /* Nonzero if there have been any destructions at this priority
58 throughout the translation unit. */
62 static void mark_vtable_entries (tree);
63 static void grok_function_init (tree, tree);
64 static bool maybe_emit_vtables (tree);
65 static bool is_namespace_ancestor (tree, tree);
66 static void add_using_namespace (tree, tree, bool);
67 static tree ambiguous_decl (tree, tree, tree,int);
68 static tree build_anon_union_vars (tree);
69 static bool acceptable_java_type (tree);
70 static void output_vtable_inherit (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 (void);
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);
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_cplus_method_type (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 ();
183 const char *sf = input_filename;
187 lineno = TINST_LINE (til);
188 input_filename = TINST_FILE (til);
190 warning ("template `%#D' instantiated in file without #pragma interface",
196 cp_warning_at ("template `%#D' defined in file without #pragma interface",
200 /* A subroutine of the parser, to handle a component list. */
203 grok_x_components (tree specs)
207 specs = strip_attrs (specs);
209 check_tag_decl (specs);
210 t = groktypename (build_tree_list (specs, NULL_TREE));
212 /* The only case where we need to do anything additional here is an
213 anonymous union field, e.g.: `struct S { union { int i; }; };'. */
214 if (t == NULL_TREE || !ANON_AGGR_TYPE_P (t))
217 fixup_anonymous_aggr (t);
218 finish_member_declaration (build_decl (FIELD_DECL, NULL_TREE, t));
221 /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
225 cp_build_parm_decl (tree name, tree type)
227 tree parm = build_decl (PARM_DECL, name, type);
228 DECL_ARG_TYPE (parm) = type_passed_as (type);
232 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
236 build_artificial_parm (tree name, tree type)
238 tree parm = cp_build_parm_decl (name, type);
239 DECL_ARTIFICIAL (parm) = 1;
240 /* All our artificial parms are implicitly `const'; they cannot be
242 TREE_READONLY (parm) = 1;
246 /* Constructors for types with virtual baseclasses need an "in-charge" flag
247 saying whether this constructor is responsible for initialization of
248 virtual baseclasses or not. All destructors also need this "in-charge"
249 flag, which additionally determines whether or not the destructor should
250 free the memory for the object.
252 This function adds the "in-charge" flag to member function FN if
253 appropriate. It is called from grokclassfn and tsubst.
254 FN must be either a constructor or destructor.
256 The in-charge flag follows the 'this' parameter, and is followed by the
257 VTT parm (if any), then the user-written parms. */
260 maybe_retrofit_in_chrg (tree fn)
262 tree basetype, arg_types, parms, parm, fntype;
264 /* If we've already add the in-charge parameter don't do it again. */
265 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
268 /* When processing templates we can't know, in general, whether or
269 not we're going to have virtual baseclasses. */
270 if (uses_template_parms (fn))
273 /* We don't need an in-charge parameter for constructors that don't
274 have virtual bases. */
275 if (DECL_CONSTRUCTOR_P (fn)
276 && !TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
279 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
280 basetype = TREE_TYPE (TREE_VALUE (arg_types));
281 arg_types = TREE_CHAIN (arg_types);
283 parms = TREE_CHAIN (DECL_ARGUMENTS (fn));
285 /* If this is a subobject constructor or destructor, our caller will
286 pass us a pointer to our VTT. */
287 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
289 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
291 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
292 TREE_CHAIN (parm) = parms;
295 /* ...and then to TYPE_ARG_TYPES. */
296 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
298 DECL_HAS_VTT_PARM_P (fn) = 1;
301 /* Then add the in-charge parm (before the VTT parm). */
302 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
303 TREE_CHAIN (parm) = parms;
305 arg_types = hash_tree_chain (integer_type_node, arg_types);
307 /* Insert our new parameter(s) into the list. */
308 TREE_CHAIN (DECL_ARGUMENTS (fn)) = parms;
310 /* And rebuild the function type. */
311 fntype = build_cplus_method_type (basetype, TREE_TYPE (TREE_TYPE (fn)),
313 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
314 fntype = build_exception_variant (fntype,
315 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
316 TREE_TYPE (fn) = fntype;
318 /* Now we've got the in-charge parameter. */
319 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
322 /* Classes overload their constituent function names automatically.
323 When a function name is declared in a record structure,
324 its name is changed to it overloaded name. Since names for
325 constructors and destructors can conflict, we place a leading
328 CNAME is the name of the class we are grokking for.
330 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
332 FLAGS contains bits saying what's special about today's
333 arguments. 1 == DESTRUCTOR. 2 == OPERATOR.
335 If FUNCTION is a destructor, then we must add the `auto-delete' field
336 as a second parameter. There is some hair associated with the fact
337 that we must "declare" this variable in the manner consistent with the
338 way the rest of the arguments were declared.
340 QUALS are the qualifiers for the this pointer. */
343 grokclassfn (tree ctype, tree function, enum overload_flags flags, tree quals)
345 tree fn_name = DECL_NAME (function);
346 int this_quals = TYPE_UNQUALIFIED;
348 /* Even within an `extern "C"' block, members get C++ linkage. See
349 [dcl.link] for details. */
350 SET_DECL_LANGUAGE (function, lang_cplusplus);
352 if (fn_name == NULL_TREE)
354 error ("name missing for member function");
355 fn_name = get_identifier ("<anonymous>");
356 DECL_NAME (function) = fn_name;
360 this_quals = grok_method_quals (ctype, function, quals);
362 if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
364 /* Must add the class instance variable up front. */
365 /* Right now we just make this a pointer. But later
366 we may wish to make it special. */
367 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (function)));
371 /* The `this' parameter is implicitly `const'; it cannot be
373 this_quals |= TYPE_QUAL_CONST;
374 qual_type = cp_build_qualified_type (type, this_quals);
375 parm = build_artificial_parm (this_identifier, qual_type);
376 c_apply_type_quals_to_decl (this_quals, parm);
377 TREE_CHAIN (parm) = last_function_parms;
378 last_function_parms = parm;
381 DECL_ARGUMENTS (function) = last_function_parms;
382 DECL_CONTEXT (function) = ctype;
384 if (flags == DTOR_FLAG)
385 DECL_DESTRUCTOR_P (function) = 1;
387 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
388 maybe_retrofit_in_chrg (function);
390 if (flags == DTOR_FLAG)
392 DECL_DESTRUCTOR_P (function) = 1;
393 TYPE_HAS_DESTRUCTOR (ctype) = 1;
397 /* Create an ARRAY_REF, checking for the user doing things backwards
401 grok_array_decl (tree array_expr, tree index_exp)
403 tree type = TREE_TYPE (array_expr);
406 if (type == error_mark_node || index_exp == error_mark_node)
407 return error_mark_node;
408 if (processing_template_decl)
409 return build_min (ARRAY_REF, type ? TREE_TYPE (type) : NULL_TREE,
410 array_expr, index_exp);
412 if (type == NULL_TREE)
414 /* Something has gone very wrong. Assume we are mistakenly reducing
415 an expression instead of a declaration. */
416 error ("parser may be lost: is there a '{' missing somewhere?");
420 if (TREE_CODE (type) == OFFSET_TYPE
421 || TREE_CODE (type) == REFERENCE_TYPE)
422 type = TREE_TYPE (type);
424 /* If they have an `operator[]', use that. */
425 if (IS_AGGR_TYPE (type) || IS_AGGR_TYPE (TREE_TYPE (index_exp)))
426 return build_opfncall (ARRAY_REF, LOOKUP_NORMAL,
427 array_expr, index_exp, NULL_TREE);
429 /* Otherwise, create an ARRAY_REF for a pointer or array type. It
430 is a little-known fact that, if `a' is an array and `i' is an
431 int, you can write `i[a]', which means the same thing as `a[i]'. */
433 if (TREE_CODE (type) == ARRAY_TYPE)
436 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
438 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
441 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
443 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr, false);
444 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp, false);
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 return build_array_ref (array_expr, index_exp);
466 /* Given the cast expression EXP, checking out its validity. Either return
467 an error_mark_node if there was an unavoidable error, return a cast to
468 void for trying to delete a pointer w/ the value 0, or return the
469 call to delete. If DOING_VEC is 1, we handle things differently
470 for doing an array delete. If DOING_VEC is 2, they gave us the
471 array size as an argument to delete.
472 Implements ARM $5.3.4. This is called from the parser. */
475 delete_sanity (tree exp, tree size, int doing_vec, int use_global_delete)
478 /* For a regular vector delete (aka, no size argument) we will pass
479 this down as a NULL_TREE into build_vec_delete. */
480 tree maxindex = NULL_TREE;
482 if (exp == error_mark_node)
485 if (processing_template_decl)
487 t = build_min (DELETE_EXPR, void_type_node, exp, size);
488 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
489 DELETE_EXPR_USE_VEC (t) = doing_vec;
493 if (TREE_CODE (exp) == OFFSET_REF)
494 exp = resolve_offset_ref (exp);
495 exp = convert_from_reference (exp);
496 t = stabilize_reference (exp);
497 t = build_expr_type_conversion (WANT_POINTER, t, true);
499 if (t == NULL_TREE || t == error_mark_node)
501 error ("type `%#T' argument given to `delete', expected pointer",
503 return error_mark_node;
508 maxindex = cp_build_binary_op (MINUS_EXPR, size, integer_one_node);
509 pedwarn ("anachronistic use of array size in vector delete");
512 type = TREE_TYPE (t);
514 /* As of Valley Forge, you can delete a pointer to const. */
516 /* You can't delete functions. */
517 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
519 error ("cannot delete a function. Only pointer-to-objects are valid arguments to `delete'");
520 return error_mark_node;
523 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
524 if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
526 warning ("deleting `%T' is undefined", type);
530 /* An array can't have been allocated by new, so complain. */
531 if (TREE_CODE (t) == ADDR_EXPR
532 && TREE_CODE (TREE_OPERAND (t, 0)) == VAR_DECL
533 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == ARRAY_TYPE)
534 warning ("deleting array `%#D'", TREE_OPERAND (t, 0));
536 /* Deleting a pointer with the value zero is valid and has no effect. */
537 if (integer_zerop (t))
538 return build1 (NOP_EXPR, void_type_node, t);
541 return build_vec_delete (t, maxindex, sfk_deleting_destructor,
544 return build_delete (type, t, sfk_deleting_destructor,
545 LOOKUP_NORMAL, use_global_delete);
548 /* Report an error if the indicated template declaration is not the
549 sort of thing that should be a member template. */
552 check_member_template (tree tmpl)
556 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
557 decl = DECL_TEMPLATE_RESULT (tmpl);
559 if (TREE_CODE (decl) == FUNCTION_DECL
560 || (TREE_CODE (decl) == TYPE_DECL
561 && IS_AGGR_TYPE (TREE_TYPE (decl))))
563 if (current_function_decl)
564 /* 14.5.2.2 [temp.mem]
566 A local class shall not have member templates. */
567 error ("invalid declaration of member template `%#D' in local class",
570 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
572 /* 14.5.2.3 [temp.mem]
574 A member function template shall not be virtual. */
576 ("invalid use of `virtual' in template declaration of `%#D'",
578 DECL_VIRTUAL_P (decl) = 0;
581 /* The debug-information generating code doesn't know what to do
582 with member templates. */
583 DECL_IGNORED_P (tmpl) = 1;
586 error ("template declaration of `%#D'", decl);
589 /* Return true iff TYPE is a valid Java parameter or return type. */
592 acceptable_java_type (tree type)
594 if (TREE_CODE (type) == VOID_TYPE || TYPE_FOR_JAVA (type))
596 if (TREE_CODE (type) == POINTER_TYPE || TREE_CODE (type) == REFERENCE_TYPE)
598 type = TREE_TYPE (type);
599 if (TREE_CODE (type) == RECORD_TYPE)
602 if (! TYPE_FOR_JAVA (type))
604 if (! CLASSTYPE_TEMPLATE_INFO (type))
606 args = CLASSTYPE_TI_ARGS (type);
607 i = TREE_VEC_LENGTH (args);
610 type = TREE_VEC_ELT (args, i);
611 if (TREE_CODE (type) == POINTER_TYPE)
612 type = TREE_TYPE (type);
613 if (! TYPE_FOR_JAVA (type))
622 /* For a METHOD in a Java class CTYPE, return true if
623 the parameter and return types are valid Java types.
624 Otherwise, print appropriate error messages, and return false. */
627 check_java_method (tree method)
630 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
631 tree ret_type = TREE_TYPE (TREE_TYPE (method));
632 if (!acceptable_java_type (ret_type))
634 error ("Java method '%D' has non-Java return type `%T'",
638 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
640 tree type = TREE_VALUE (arg_types);
641 if (!acceptable_java_type (type))
643 error ("Java method '%D' has non-Java parameter type `%T'",
651 /* Sanity check: report error if this function FUNCTION is not
652 really a member of the class (CTYPE) it is supposed to belong to.
653 CNAME is the same here as it is for grokclassfn above. */
656 check_classfn (tree ctype, tree function)
660 if (DECL_USE_TEMPLATE (function)
661 && !(TREE_CODE (function) == TEMPLATE_DECL
662 && DECL_TEMPLATE_SPECIALIZATION (function))
663 && is_member_template (DECL_TI_TEMPLATE (function)))
664 /* Since this is a specialization of a member template,
665 we're not going to find the declaration in the class.
668 struct S { template <typename T> void f(T); };
669 template <> void S::f(int);
671 we're not going to find `S::f(int)', but there's no
672 reason we should, either. We let our callers know we didn't
673 find the method, but we don't complain. */
676 ix = lookup_fnfields_1 (complete_type (ctype),
677 DECL_CONSTRUCTOR_P (function) ? ctor_identifier :
678 DECL_DESTRUCTOR_P (function) ? dtor_identifier :
679 DECL_NAME (function));
683 tree methods = CLASSTYPE_METHOD_VEC (ctype);
684 tree fndecls, fndecl;
686 const char *format = NULL;
688 for (fndecls = TREE_VEC_ELT (methods, ix);
689 fndecls; fndecls = OVL_NEXT (fndecls))
693 fndecl = OVL_CURRENT (fndecls);
694 p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
695 p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
697 /* We cannot simply call decls_match because this doesn't
698 work for static member functions that are pretending to
699 be methods, and because the name may have been changed by
702 /* Get rid of the this parameter on functions that become
704 if (DECL_STATIC_FUNCTION_P (fndecl)
705 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
706 p1 = TREE_CHAIN (p1);
708 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
709 TREE_TYPE (TREE_TYPE (fndecl)))
710 && compparms (p1, p2)
711 && (DECL_TEMPLATE_SPECIALIZATION (function)
712 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
713 && (!DECL_TEMPLATE_SPECIALIZATION (function)
714 || (DECL_TI_TEMPLATE (function)
715 == DECL_TI_TEMPLATE (fndecl))))
718 error ("prototype for `%#D' does not match any in class `%T'",
720 is_conv_op = DECL_CONV_FN_P (fndecl);
723 ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
724 fndecls = TREE_VEC_ELT (methods, ix);
727 fndecl = OVL_CURRENT (fndecls);
728 fndecls = OVL_NEXT (fndecls);
730 if (!fndecls && is_conv_op)
732 if (TREE_VEC_LENGTH (methods) > ix)
735 fndecls = TREE_VEC_ELT (methods, ix);
736 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
748 format = "candidates are: %#D";
750 format = "candidate is: %#D";
751 cp_error_at (format, fndecl);
754 else if (!COMPLETE_TYPE_P (ctype))
755 cxx_incomplete_type_error (function, ctype);
757 error ("no `%#D' member function declared in class `%T'",
760 /* If we did not find the method in the class, add it to avoid
761 spurious errors (unless the CTYPE is not yet defined, in which
762 case we'll only confuse ourselves when the function is declared
763 properly within the class. */
764 if (COMPLETE_TYPE_P (ctype))
765 add_method (ctype, function, /*error_p=*/1);
769 /* We have just processed the DECL, which is a static data member.
770 Its initializer, if present, is INIT. The ASMSPEC_TREE, if
771 present, is the assembly-language name for the data member.
772 FLAGS is as for cp_finish_decl. */
775 finish_static_data_member_decl (tree decl, tree init, tree asmspec_tree,
778 my_friendly_assert (TREE_PUBLIC (decl), 0);
780 DECL_CONTEXT (decl) = current_class_type;
782 /* We cannot call pushdecl here, because that would fill in the
783 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
784 the right thing, namely, to put this decl out straight away. */
785 /* current_class_type can be NULL_TREE in case of error. */
786 if (!asmspec_tree && current_class_type)
787 DECL_INITIAL (decl) = error_mark_node;
789 if (! processing_template_decl)
791 if (!pending_statics)
792 VARRAY_TREE_INIT (pending_statics, 32, "pending_statics");
793 VARRAY_PUSH_TREE (pending_statics, decl);
796 if (LOCAL_CLASS_P (current_class_type))
797 pedwarn ("local class `%#T' shall not have static data member `%#D'",
798 current_class_type, decl);
800 /* Static consts need not be initialized in the class definition. */
801 if (init != NULL_TREE && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
803 static int explained = 0;
805 error ("initializer invalid for static member with constructor");
808 error ("(an out of class initialization is required)");
813 /* Force the compiler to know when an uninitialized static const
814 member is being used. */
815 if (CP_TYPE_CONST_P (TREE_TYPE (decl)) && init == 0)
816 TREE_USED (decl) = 1;
817 DECL_INITIAL (decl) = init;
818 DECL_IN_AGGR_P (decl) = 1;
820 cp_finish_decl (decl, init, asmspec_tree, flags);
823 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
824 of a structure component, returning a _DECL node.
825 QUALS is a list of type qualifiers for this decl (such as for declaring
826 const member functions).
828 This is done during the parsing of the struct declaration.
829 The _DECL nodes are chained together and the lot of them
830 are ultimately passed to `build_struct' to make the RECORD_TYPE node.
832 If class A defines that certain functions in class B are friends, then
833 the way I have set things up, it is B who is interested in permission
834 granted by A. However, it is in A's context that these declarations
835 are parsed. By returning a void_type_node, class A does not attempt
836 to incorporate the declarations of the friends within its structure.
838 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
839 CHANGES TO CODE IN `start_method'. */
842 grokfield (tree declarator, tree declspecs, tree init, tree asmspec_tree,
846 const char *asmspec = 0;
847 int flags = LOOKUP_ONLYCONVERTING;
849 /* Convert () initializers to = initializers. */
850 if (init == NULL_TREE && declarator != NULL_TREE
851 && TREE_CODE (declarator) == CALL_EXPR
852 && TREE_OPERAND (declarator, 0)
853 && (TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE
854 || TREE_CODE (TREE_OPERAND (declarator, 0)) == SCOPE_REF)
855 && parmlist_is_exprlist (CALL_DECLARATOR_PARMS (declarator)))
857 /* It's invalid to try to initialize a data member using a
858 functional notation, e.g.:
864 Explain that to the user. */
865 static int explained;
867 error ("invalid data member initialization");
870 error ("(use `=' to initialize static data members)");
874 declarator = TREE_OPERAND (declarator, 0);
878 if (declspecs == NULL_TREE
879 && TREE_CODE (declarator) == SCOPE_REF
880 && TREE_CODE (TREE_OPERAND (declarator, 1)) == IDENTIFIER_NODE)
882 /* Access declaration */
883 if (! IS_AGGR_TYPE_CODE (TREE_CODE (TREE_OPERAND (declarator, 0))))
885 else if (TREE_COMPLEXITY (declarator) == current_class_depth)
887 return do_class_using_decl (declarator);
891 && TREE_CODE (init) == TREE_LIST
892 && TREE_VALUE (init) == error_mark_node
893 && TREE_CHAIN (init) == NULL_TREE)
896 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
897 if (! value || value == error_mark_node)
898 /* friend or constructor went bad. */
900 if (TREE_TYPE (value) == error_mark_node)
901 return error_mark_node;
903 if (TREE_CODE (value) == TYPE_DECL && init)
905 error ("typedef `%D' is initialized (use __typeof__ instead)", value);
909 /* Pass friendly classes back. */
910 if (TREE_CODE (value) == VOID_TYPE)
911 return void_type_node;
913 if (DECL_NAME (value) != NULL_TREE
914 && IDENTIFIER_POINTER (DECL_NAME (value))[0] == '_'
915 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (value)), "_vptr"))
916 error ("member `%D' conflicts with virtual function table field name",
919 /* Stash away type declarations. */
920 if (TREE_CODE (value) == TYPE_DECL)
922 DECL_NONLOCAL (value) = 1;
923 DECL_CONTEXT (value) = current_class_type;
925 if (CLASS_TYPE_P (TREE_TYPE (value)))
926 CLASSTYPE_GOT_SEMICOLON (TREE_TYPE (value)) = 1;
928 if (processing_template_decl)
929 value = push_template_decl (value);
934 if (DECL_IN_AGGR_P (value))
936 error ("`%D' is already defined in `%T'", value,
937 DECL_CONTEXT (value));
938 return void_type_node;
942 asmspec = TREE_STRING_POINTER (asmspec_tree);
946 if (TREE_CODE (value) == FUNCTION_DECL)
948 grok_function_init (value, init);
951 else if (pedantic && TREE_CODE (value) != VAR_DECL)
952 /* Already complained in grokdeclarator. */
956 /* We allow initializers to become parameters to base
958 if (TREE_CODE (init) == TREE_LIST)
960 if (TREE_CHAIN (init) == NULL_TREE)
961 init = TREE_VALUE (init);
963 init = digest_init (TREE_TYPE (value), init, (tree *)0);
966 if (!processing_template_decl)
968 if (TREE_CODE (init) == CONST_DECL)
969 init = DECL_INITIAL (init);
970 else if (TREE_READONLY_DECL_P (init))
971 init = decl_constant_value (init);
972 else if (TREE_CODE (init) == CONSTRUCTOR)
973 init = digest_init (TREE_TYPE (value), init, (tree *)0);
974 if (init == error_mark_node)
975 /* We must make this look different than `error_mark_node'
976 because `decl_const_value' would mis-interpret it
977 as only meaning that this VAR_DECL is defined. */
978 init = build1 (NOP_EXPR, TREE_TYPE (value), init);
979 else if (! TREE_CONSTANT (init))
981 /* We can allow references to things that are effectively
982 static, since references are initialized with the
984 if (TREE_CODE (TREE_TYPE (value)) != REFERENCE_TYPE
985 || (TREE_STATIC (init) == 0
986 && (!DECL_P (init) || DECL_EXTERNAL (init) == 0)))
988 error ("field initializer is not constant");
989 init = error_mark_node;
996 if (processing_template_decl && ! current_function_decl
997 && (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == FUNCTION_DECL))
998 value = push_template_decl (value);
1001 cplus_decl_attributes (&value, attrlist, 0);
1003 if (TREE_CODE (value) == VAR_DECL)
1005 finish_static_data_member_decl (value, init, asmspec_tree,
1009 if (TREE_CODE (value) == FIELD_DECL)
1012 error ("`asm' specifiers are not permitted on non-static data members");
1013 if (DECL_INITIAL (value) == error_mark_node)
1014 init = error_mark_node;
1015 cp_finish_decl (value, init, NULL_TREE, flags);
1016 DECL_INITIAL (value) = init;
1017 DECL_IN_AGGR_P (value) = 1;
1020 if (TREE_CODE (value) == FUNCTION_DECL)
1024 /* This must override the asm specifier which was placed
1025 by grokclassfn. Lay this out fresh. */
1026 SET_DECL_RTL (value, NULL_RTX);
1027 SET_DECL_ASSEMBLER_NAME (value, get_identifier (asmspec));
1029 if (!DECL_FRIEND_P (value))
1030 grok_special_member_properties (value);
1032 cp_finish_decl (value, init, asmspec_tree, flags);
1034 /* Pass friends back this way. */
1035 if (DECL_FRIEND_P (value))
1036 return void_type_node;
1038 DECL_IN_AGGR_P (value) = 1;
1046 /* Like `grokfield', but for bitfields.
1047 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1050 grokbitfield (tree declarator, tree declspecs, tree width)
1052 register tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1055 if (! value) return NULL_TREE; /* friends went bad. */
1057 /* Pass friendly classes back. */
1058 if (TREE_CODE (value) == VOID_TYPE)
1059 return void_type_node;
1061 if (TREE_CODE (value) == TYPE_DECL)
1063 error ("cannot declare `%D' to be a bit-field type", value);
1067 /* Usually, finish_struct_1 catches bitfields with invalid types.
1068 But, in the case of bitfields with function type, we confuse
1069 ourselves into thinking they are member functions, so we must
1071 if (TREE_CODE (value) == FUNCTION_DECL)
1073 error ("cannot declare bit-field `%D' with function type",
1078 if (DECL_IN_AGGR_P (value))
1080 error ("`%D' is already defined in the class %T", value,
1081 DECL_CONTEXT (value));
1082 return void_type_node;
1085 if (TREE_STATIC (value))
1087 error ("static member `%D' cannot be a bit-field", value);
1090 cp_finish_decl (value, NULL_TREE, NULL_TREE, 0);
1092 if (width != error_mark_node)
1094 constant_expression_warning (width);
1095 DECL_INITIAL (value) = width;
1096 SET_DECL_C_BIT_FIELD (value);
1099 DECL_IN_AGGR_P (value) = 1;
1103 /* Convert a conversion operator name to an identifier. SCOPE is the
1104 scope of the conversion operator, if explicit. */
1107 grokoptypename (tree declspecs, tree declarator, tree scope)
1109 tree t = grokdeclarator (declarator, declspecs, TYPENAME, 0, NULL);
1111 /* Resolve any TYPENAME_TYPEs that refer to SCOPE, before mangling
1112 the name, so that we mangle the right thing. */
1113 if (scope && current_template_parms
1114 && uses_template_parms (t)
1115 && uses_template_parms (scope))
1117 tree args = current_template_args ();
1120 t = tsubst (t, args, tf_error | tf_warning, NULL_TREE);
1124 return mangle_conv_op_name_for_type (t);
1127 /* When a function is declared with an initializer,
1128 do the right thing. Currently, there are two possibilities:
1133 // initialization possibility #1.
1134 virtual void f () = 0;
1156 // initialization possibility #2
1163 grok_function_init (tree decl, tree init)
1165 /* An initializer for a function tells how this function should
1167 tree type = TREE_TYPE (decl);
1169 if (TREE_CODE (type) == FUNCTION_TYPE)
1170 error ("initializer specified for non-member function `%D'", decl);
1171 else if (integer_zerop (init))
1172 DECL_PURE_VIRTUAL_P (decl) = 1;
1174 error ("invalid initializer for virtual method `%D'", decl);
1178 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1180 if (*decl == NULL_TREE || *decl == void_type_node)
1183 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1184 decl = &DECL_TEMPLATE_RESULT (*decl);
1186 decl_attributes (decl, attributes, flags);
1188 if (TREE_CODE (*decl) == TYPE_DECL)
1189 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1192 /* Return the name for the constructor (or destructor) for the
1193 specified class TYPE. When given a template, this routine doesn't
1194 lose the specialization. */
1197 constructor_name_full (tree type)
1199 type = TYPE_MAIN_VARIANT (type);
1200 if (CLASS_TYPE_P (type) && TYPE_WAS_ANONYMOUS (type)
1201 && TYPE_HAS_CONSTRUCTOR (type))
1202 return DECL_NAME (OVL_CURRENT (CLASSTYPE_CONSTRUCTORS (type)));
1204 return TYPE_IDENTIFIER (type);
1207 /* Return the name for the constructor (or destructor) for the
1208 specified class. When given a template, return the plain
1209 unspecialized name. */
1212 constructor_name (tree type)
1215 name = constructor_name_full (type);
1216 if (IDENTIFIER_TEMPLATE (name))
1217 name = IDENTIFIER_TEMPLATE (name);
1221 /* Returns TRUE if NAME is the name for the constructor for TYPE. */
1224 constructor_name_p (tree name, tree type)
1226 return (name == constructor_name (type)
1227 || name == constructor_name_full (type));
1231 /* Defer the compilation of the FN until the end of compilation. */
1236 if (DECL_DEFERRED_FN (fn))
1238 DECL_DEFERRED_FN (fn) = 1;
1240 VARRAY_TREE_INIT (deferred_fns, 32, "deferred_fns");
1242 VARRAY_PUSH_TREE (deferred_fns, fn);
1245 /* Walks through the namespace- or function-scope anonymous union OBJECT,
1246 building appropriate ALIAS_DECLs. Returns one of the fields for use in
1247 the mangled name. */
1250 build_anon_union_vars (tree object)
1252 tree type = TREE_TYPE (object);
1253 tree main_decl = NULL_TREE;
1256 /* Rather than write the code to handle the non-union case,
1257 just give an error. */
1258 if (TREE_CODE (type) != UNION_TYPE)
1259 error ("anonymous struct not inside named type");
1261 for (field = TYPE_FIELDS (type);
1263 field = TREE_CHAIN (field))
1268 if (DECL_ARTIFICIAL (field))
1270 if (TREE_CODE (field) != FIELD_DECL)
1273 `%#D' invalid; an anonymous union can only have non-static data members",
1278 if (TREE_PRIVATE (field))
1279 cp_pedwarn_at ("private member `%#D' in anonymous union", field);
1280 else if (TREE_PROTECTED (field))
1281 cp_pedwarn_at ("protected member `%#D' in anonymous union", field);
1283 ref = build_class_member_access_expr (object, field, NULL_TREE,
1286 if (DECL_NAME (field))
1288 decl = build_decl (ALIAS_DECL, DECL_NAME (field), TREE_TYPE (field));
1289 DECL_INITIAL (decl) = ref;
1290 TREE_PUBLIC (decl) = 0;
1291 TREE_STATIC (decl) = 0;
1292 DECL_EXTERNAL (decl) = 1;
1293 decl = pushdecl (decl);
1295 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1296 decl = build_anon_union_vars (ref);
1298 if (main_decl == NULL_TREE)
1305 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1306 anonymous union, then all members must be laid out together. PUBLIC_P
1307 is nonzero if this union is not declared static. */
1310 finish_anon_union (tree anon_union_decl)
1312 tree type = TREE_TYPE (anon_union_decl);
1314 bool public_p = TREE_PUBLIC (anon_union_decl);
1316 /* The VAR_DECL's context is the same as the TYPE's context. */
1317 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1319 if (TYPE_FIELDS (type) == NULL_TREE)
1324 error ("namespace-scope anonymous aggregates must be static");
1328 if (!processing_template_decl)
1330 main_decl = build_anon_union_vars (anon_union_decl);
1332 if (main_decl == NULL_TREE)
1334 warning ("anonymous union with no members");
1338 /* Use main_decl to set the mangled name. */
1339 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1340 mangle_decl (anon_union_decl);
1341 DECL_NAME (anon_union_decl) = NULL_TREE;
1344 pushdecl (anon_union_decl);
1345 if (building_stmt_tree ()
1346 && at_function_scope_p ())
1347 add_decl_stmt (anon_union_decl);
1348 else if (!processing_template_decl)
1349 rest_of_decl_compilation (anon_union_decl, NULL,
1350 toplevel_bindings_p (), at_eof);
1353 /* Auxiliary functions to make type signatures for
1354 `operator new' and `operator delete' correspond to
1355 what compiler will be expecting. */
1358 coerce_new_type (tree type)
1361 tree args = TYPE_ARG_TYPES (type);
1363 my_friendly_assert (TREE_CODE (type) == FUNCTION_TYPE, 20001107);
1365 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1366 e = 1, error ("`operator new' must return type `%T'", ptr_type_node);
1368 if (!args || args == void_list_node
1369 || !same_type_p (TREE_VALUE (args), size_type_node))
1372 if (args && args != void_list_node)
1373 args = TREE_CHAIN (args);
1374 pedwarn ("`operator new' takes type `size_t' (`%T') as first parameter", size_type_node);
1379 args = tree_cons (NULL_TREE, size_type_node, args);
1382 type = build_exception_variant
1383 (build_function_type (ptr_type_node, args),
1384 TYPE_RAISES_EXCEPTIONS (type));
1392 coerce_delete_type (tree type)
1395 tree args = TYPE_ARG_TYPES (type);
1397 my_friendly_assert (TREE_CODE (type) == FUNCTION_TYPE, 20001107);
1399 if (!same_type_p (TREE_TYPE (type), void_type_node))
1400 e = 1, error ("`operator delete' must return type `%T'", void_type_node);
1402 if (!args || args == void_list_node
1403 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1406 if (args && args != void_list_node)
1407 args = TREE_CHAIN (args);
1408 error ("`operator delete' takes type `%T' as first parameter", ptr_type_node);
1413 args = tree_cons (NULL_TREE, ptr_type_node, args);
1416 type = build_exception_variant
1417 (build_function_type (void_type_node, args),
1418 TYPE_RAISES_EXCEPTIONS (type));
1427 mark_vtable_entries (tree decl)
1429 tree entries = CONSTRUCTOR_ELTS (DECL_INITIAL (decl));
1431 for (; entries; entries = TREE_CHAIN (entries))
1433 tree fnaddr = TREE_VALUE (entries);
1436 if (TREE_CODE (fnaddr) != ADDR_EXPR
1437 && TREE_CODE (fnaddr) != FDESC_EXPR)
1438 /* This entry is an offset: a virtual base class offset, a
1439 virtual call offset, an RTTI offset, etc. */
1442 fn = TREE_OPERAND (fnaddr, 0);
1443 TREE_ADDRESSABLE (fn) = 1;
1444 /* When we don't have vcall offsets, we output thunks whenever
1445 we output the vtables that contain them. With vcall offsets,
1446 we know all the thunks we'll need when we emit a virtual
1447 function, so we emit the thunks there instead. */
1448 if (DECL_THUNK_P (fn))
1449 use_thunk (fn, /*emit_p=*/0);
1454 /* Set DECL up to have the closest approximation of "initialized common"
1455 linkage available. */
1458 comdat_linkage (tree decl)
1461 make_decl_one_only (decl);
1462 else if (TREE_CODE (decl) == FUNCTION_DECL
1463 || (TREE_CODE (decl) == VAR_DECL && DECL_ARTIFICIAL (decl)))
1464 /* We can just emit function and compiler-generated variables
1465 statically; having multiple copies is (for the most part) only
1468 There are two correctness issues, however: the address of a
1469 template instantiation with external linkage should be the
1470 same, independent of what translation unit asks for the
1471 address, and this will not hold when we emit multiple copies of
1472 the function. However, there's little else we can do.
1474 Also, by default, the typeinfo implementation assumes that
1475 there will be only one copy of the string used as the name for
1476 each type. Therefore, if weak symbols are unavailable, the
1477 run-time library should perform a more conservative check; it
1478 should perform a string comparison, rather than an address
1480 TREE_PUBLIC (decl) = 0;
1483 /* Static data member template instantiations, however, cannot
1484 have multiple copies. */
1485 if (DECL_INITIAL (decl) == 0
1486 || DECL_INITIAL (decl) == error_mark_node)
1487 DECL_COMMON (decl) = 1;
1488 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1490 DECL_COMMON (decl) = 1;
1491 DECL_INITIAL (decl) = error_mark_node;
1495 /* We can't do anything useful; leave vars for explicit
1497 DECL_EXTERNAL (decl) = 1;
1498 DECL_NOT_REALLY_EXTERN (decl) = 0;
1502 if (DECL_LANG_SPECIFIC (decl))
1503 DECL_COMDAT (decl) = 1;
1506 /* For win32 we also want to put explicit instantiations in
1507 linkonce sections, so that they will be merged with implicit
1508 instantiations; otherwise we get duplicate symbol errors. */
1511 maybe_make_one_only (tree decl)
1513 /* We used to say that this was not necessary on targets that support weak
1514 symbols, because the implicit instantiations will defer to the explicit
1515 one. However, that's not actually the case in SVR4; a strong definition
1516 after a weak one is an error. Also, not making explicit
1517 instantiations one_only means that we can end up with two copies of
1518 some template instantiations. */
1522 /* We can't set DECL_COMDAT on functions, or finish_file will think
1523 we can get away with not emitting them if they aren't used. We need
1524 to for variables so that cp_finish_decl will update their linkage,
1525 because their DECL_INITIAL may not have been set properly yet. */
1527 make_decl_one_only (decl);
1529 if (TREE_CODE (decl) == VAR_DECL)
1531 DECL_COMDAT (decl) = 1;
1532 /* Mark it needed so we don't forget to emit it. */
1533 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)) = 1;
1537 /* Set TREE_PUBLIC and/or DECL_EXTERN on the vtable DECL,
1538 based on TYPE and other static flags.
1540 Note that anything public is tagged TREE_PUBLIC, whether
1541 it's public in this file or in another one. */
1544 import_export_vtable (tree decl, tree type, int final)
1546 if (DECL_INTERFACE_KNOWN (decl))
1549 if (TYPE_FOR_JAVA (type))
1551 TREE_PUBLIC (decl) = 1;
1552 DECL_EXTERNAL (decl) = 1;
1553 DECL_INTERFACE_KNOWN (decl) = 1;
1555 else if (CLASSTYPE_INTERFACE_KNOWN (type))
1557 TREE_PUBLIC (decl) = 1;
1558 DECL_EXTERNAL (decl) = CLASSTYPE_INTERFACE_ONLY (type);
1559 DECL_INTERFACE_KNOWN (decl) = 1;
1563 /* We can only wait to decide if we have real non-inline virtual
1564 functions in our class, or if we come from a template. */
1566 int found = (CLASSTYPE_TEMPLATE_INSTANTIATION (type)
1567 || CLASSTYPE_KEY_METHOD (type) != NULL_TREE);
1569 if (final || ! found)
1571 comdat_linkage (decl);
1572 DECL_EXTERNAL (decl) = 0;
1576 TREE_PUBLIC (decl) = 1;
1577 DECL_EXTERNAL (decl) = 1;
1582 /* Determine whether or not we want to specifically import or export CTYPE,
1583 using various heuristics. */
1586 import_export_class (tree ctype)
1588 /* -1 for imported, 1 for exported. */
1589 int import_export = 0;
1591 /* It only makes sense to call this function at EOF. The reason is
1592 that this function looks at whether or not the first non-inline
1593 non-abstract virtual member function has been defined in this
1594 translation unit. But, we can't possibly know that until we've
1595 seen the entire translation unit. */
1596 my_friendly_assert (at_eof, 20000226);
1598 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1601 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma interface,
1602 we will have CLASSTYPE_INTERFACE_ONLY set but not
1603 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1604 heuristic because someone will supply a #pragma implementation
1605 elsewhere, and deducing it here would produce a conflict. */
1606 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1609 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1611 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1614 /* If we got -fno-implicit-templates, we import template classes that
1615 weren't explicitly instantiated. */
1616 if (import_export == 0
1617 && CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1618 && ! flag_implicit_templates)
1621 /* Base our import/export status on that of the first non-inline,
1622 non-pure virtual function, if any. */
1623 if (import_export == 0
1624 && TYPE_POLYMORPHIC_P (ctype))
1626 tree method = CLASSTYPE_KEY_METHOD (ctype);
1628 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1631 #ifdef MULTIPLE_SYMBOL_SPACES
1632 if (import_export == -1)
1638 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1639 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1643 /* We need to describe to the assembler the relationship between
1644 a vtable and the vtable of the parent class. */
1647 output_vtable_inherit (tree vars)
1650 rtx child_rtx, parent_rtx;
1652 child_rtx = XEXP (DECL_RTL (vars), 0); /* strip the mem ref */
1654 parent = binfo_for_vtable (vars);
1656 if (parent == TYPE_BINFO (DECL_CONTEXT (vars)))
1657 parent_rtx = const0_rtx;
1660 parent = get_vtbl_decl_for_binfo (TYPE_BINFO (BINFO_TYPE (parent)));
1661 parent_rtx = XEXP (DECL_RTL (parent), 0); /* strip the mem ref */
1666 assemble_vtable_inherit (child_rtx, parent_rtx);
1669 /* If necessary, write out the vtables for the dynamic class CTYPE.
1670 Returns true if any vtables were emitted. */
1673 maybe_emit_vtables (tree ctype)
1678 /* If the vtables for this class have already been emitted there is
1679 nothing more to do. */
1680 primary_vtbl = CLASSTYPE_VTABLES (ctype);
1681 if (TREE_ASM_WRITTEN (primary_vtbl))
1683 /* Ignore dummy vtables made by get_vtable_decl. */
1684 if (TREE_TYPE (primary_vtbl) == void_type_node)
1687 import_export_class (ctype);
1688 import_export_vtable (primary_vtbl, ctype, 1);
1690 /* See if any of the vtables are needed. */
1691 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = TREE_CHAIN (vtbl))
1692 if (!DECL_EXTERNAL (vtbl) && DECL_NEEDED_P (vtbl))
1697 /* If the references to this class' vtables are optimized away,
1698 still emit the appropriate debugging information. See
1700 if (DECL_COMDAT (primary_vtbl)
1701 && CLASSTYPE_DEBUG_REQUESTED (ctype))
1702 note_debug_info_needed (ctype);
1706 /* The ABI requires that we emit all of the vtables if we emit any
1708 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = TREE_CHAIN (vtbl))
1711 import_export_vtable (vtbl, ctype, 1);
1712 mark_vtable_entries (vtbl);
1713 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
1714 store_init_value (vtbl, DECL_INITIAL (vtbl));
1716 if (write_symbols == DWARF_DEBUG || write_symbols == DWARF2_DEBUG)
1718 /* Mark the VAR_DECL node representing the vtable itself as a
1719 "gratuitous" one, thereby forcing dwarfout.c to ignore it.
1720 It is rather important that such things be ignored because
1721 any effort to actually generate DWARF for them will run
1722 into trouble when/if we encounter code like:
1725 struct S { virtual void member (); };
1727 because the artificial declaration of the vtable itself (as
1728 manufactured by the g++ front end) will say that the vtable
1729 is a static member of `S' but only *after* the debug output
1730 for the definition of `S' has already been output. This causes
1731 grief because the DWARF entry for the definition of the vtable
1732 will try to refer back to an earlier *declaration* of the
1733 vtable as a static member of `S' and there won't be one.
1734 We might be able to arrange to have the "vtable static member"
1735 attached to the member list for `S' before the debug info for
1736 `S' get written (which would solve the problem) but that would
1737 require more intrusive changes to the g++ front end. */
1739 DECL_IGNORED_P (vtbl) = 1;
1742 /* Always make vtables weak. */
1744 comdat_linkage (vtbl);
1746 rest_of_decl_compilation (vtbl, NULL, 1, 1);
1749 output_vtable_inherit (vtbl);
1751 /* Because we're only doing syntax-checking, we'll never end up
1752 actually marking the variable as written. */
1753 if (flag_syntax_only)
1754 TREE_ASM_WRITTEN (vtbl) = 1;
1757 /* Since we're writing out the vtable here, also write the debug
1759 note_debug_info_needed (ctype);
1764 /* Determines the proper settings of TREE_PUBLIC and DECL_EXTERNAL for an
1765 inline function or template instantiation at end-of-file. */
1768 import_export_decl (tree decl)
1770 if (DECL_INTERFACE_KNOWN (decl))
1773 if (DECL_TEMPLATE_INSTANTIATION (decl)
1774 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
1776 DECL_NOT_REALLY_EXTERN (decl) = 1;
1777 if ((DECL_IMPLICIT_INSTANTIATION (decl)
1778 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
1779 && (flag_implicit_templates
1780 || (flag_implicit_inline_templates
1781 && DECL_DECLARED_INLINE_P (decl))))
1783 if (!TREE_PUBLIC (decl))
1784 /* Templates are allowed to have internal linkage. See
1788 comdat_linkage (decl);
1792 DECL_EXTERNAL (decl) = 1;
1793 DECL_NOT_REALLY_EXTERN (decl) = 0;
1796 else if (DECL_FUNCTION_MEMBER_P (decl))
1798 if (!DECL_DECLARED_INLINE_P (decl))
1800 tree ctype = DECL_CONTEXT (decl);
1801 import_export_class (ctype);
1802 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1804 DECL_NOT_REALLY_EXTERN (decl)
1805 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
1806 || (DECL_DECLARED_INLINE_P (decl)
1807 && ! flag_implement_inlines
1808 && !DECL_VINDEX (decl)));
1810 if (!DECL_NOT_REALLY_EXTERN (decl))
1811 DECL_EXTERNAL (decl) = 1;
1813 /* Always make artificials weak. */
1814 if (DECL_ARTIFICIAL (decl) && flag_weak)
1815 comdat_linkage (decl);
1817 maybe_make_one_only (decl);
1821 comdat_linkage (decl);
1824 comdat_linkage (decl);
1826 DECL_INTERFACE_KNOWN (decl) = 1;
1829 /* Here, we only decide whether or not the tinfo node should be
1830 emitted with the vtable. IS_IN_LIBRARY is nonzero iff the
1831 typeinfo for TYPE should be in the runtime library. */
1834 import_export_tinfo (tree decl, tree type, bool is_in_library)
1836 if (DECL_INTERFACE_KNOWN (decl))
1839 if (IS_AGGR_TYPE (type))
1840 import_export_class (type);
1842 if (IS_AGGR_TYPE (type) && CLASSTYPE_INTERFACE_KNOWN (type)
1843 && TYPE_POLYMORPHIC_P (type)
1844 /* If -fno-rtti, we're not necessarily emitting this stuff with
1845 the class, so go ahead and emit it now. This can happen when
1846 a class is used in exception handling. */
1849 DECL_NOT_REALLY_EXTERN (decl) = !CLASSTYPE_INTERFACE_ONLY (type);
1850 DECL_COMDAT (decl) = 0;
1854 DECL_NOT_REALLY_EXTERN (decl) = 1;
1855 DECL_COMDAT (decl) = 1;
1858 /* Now override some cases. */
1860 DECL_COMDAT (decl) = 1;
1861 else if (is_in_library)
1862 DECL_COMDAT (decl) = 0;
1864 DECL_INTERFACE_KNOWN (decl) = 1;
1867 /* Return an expression that performs the destruction of DECL, which
1868 must be a VAR_DECL whose type has a non-trivial destructor, or is
1869 an array whose (innermost) elements have a non-trivial destructor. */
1872 build_cleanup (tree decl)
1875 tree type = TREE_TYPE (decl);
1877 /* This function should only be called for declarations that really
1878 require cleanups. */
1879 my_friendly_assert (!TYPE_HAS_TRIVIAL_DESTRUCTOR (type), 20030106);
1881 /* Treat all objects with destructors as used; the destructor may do
1882 something substantive. */
1885 if (TREE_CODE (type) == ARRAY_TYPE)
1889 cxx_mark_addressable (decl);
1890 temp = build1 (ADDR_EXPR, build_pointer_type (type), decl);
1892 temp = build_delete (TREE_TYPE (temp), temp,
1893 sfk_complete_destructor,
1894 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
1898 /* Returns the initialization guard variable for the variable DECL,
1899 which has static storage duration. */
1902 get_guard (tree decl)
1907 sname = mangle_guard_variable (decl);
1908 guard = IDENTIFIER_GLOBAL_VALUE (sname);
1913 /* We use a type that is big enough to contain a mutex as well
1914 as an integer counter. */
1915 guard_type = long_long_integer_type_node;
1916 guard = build_decl (VAR_DECL, sname, guard_type);
1918 /* The guard should have the same linkage as what it guards. */
1919 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
1920 TREE_STATIC (guard) = TREE_STATIC (decl);
1921 DECL_COMMON (guard) = DECL_COMMON (decl);
1922 DECL_ONE_ONLY (guard) = DECL_ONE_ONLY (decl);
1923 if (TREE_PUBLIC (decl))
1924 DECL_WEAK (guard) = DECL_WEAK (decl);
1926 DECL_ARTIFICIAL (guard) = 1;
1927 TREE_USED (guard) = 1;
1928 pushdecl_top_level (guard);
1929 cp_finish_decl (guard, NULL_TREE, NULL_TREE, 0);
1934 /* Return those bits of the GUARD variable that should be set when the
1935 guarded entity is actually initialized. */
1938 get_guard_bits (tree guard)
1940 /* We only set the first byte of the guard, in order to leave room
1941 for a mutex in the high-order bits. */
1942 guard = build1 (ADDR_EXPR,
1943 build_pointer_type (TREE_TYPE (guard)),
1945 guard = build1 (NOP_EXPR,
1946 build_pointer_type (char_type_node),
1948 guard = build1 (INDIRECT_REF, char_type_node, guard);
1953 /* Return an expression which determines whether or not the GUARD
1954 variable has already been initialized. */
1957 get_guard_cond (tree guard)
1961 /* Check to see if the GUARD is zero. */
1962 guard = get_guard_bits (guard);
1963 guard_value = integer_zero_node;
1964 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
1965 guard_value = convert (TREE_TYPE (guard), guard_value);
1966 return cp_build_binary_op (EQ_EXPR, guard, guard_value);
1969 /* Return an expression which sets the GUARD variable, indicating that
1970 the variable being guarded has been initialized. */
1973 set_guard (tree guard)
1977 /* Set the GUARD to one. */
1978 guard = get_guard_bits (guard);
1979 guard_init = integer_one_node;
1980 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
1981 guard_init = convert (TREE_TYPE (guard), guard_init);
1982 return build_modify_expr (guard, NOP_EXPR, guard_init);
1985 /* Start the process of running a particular set of global constructors
1986 or destructors. Subroutine of do_[cd]tors. */
1989 start_objects (int method_type, int initp)
1995 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
1997 if (initp != DEFAULT_INIT_PRIORITY)
2007 sprintf (type, "%c%c%.5u", method_type, joiner, initp);
2010 sprintf (type, "%c", method_type);
2012 fnname = get_file_function_name_long (type);
2014 start_function (void_list_node,
2015 make_call_declarator (fnname, void_list_node, NULL_TREE,
2017 NULL_TREE, SF_DEFAULT);
2019 /* It can be a static function as long as collect2 does not have
2020 to scan the object file to find its ctor/dtor routine. */
2021 TREE_PUBLIC (current_function_decl) = ! targetm.have_ctors_dtors;
2023 /* Mark this declaration as used to avoid spurious warnings. */
2024 TREE_USED (current_function_decl) = 1;
2026 /* Mark this function as a global constructor or destructor. */
2027 if (method_type == 'I')
2028 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
2030 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
2031 DECL_LANG_SPECIFIC (current_function_decl)->decl_flags.u2sel = 1;
2033 body = begin_compound_stmt (/*has_no_scope=*/0);
2035 /* We cannot allow these functions to be elided, even if they do not
2036 have external linkage. And, there's no point in deferring
2037 copmilation of thes functions; they're all going to have to be
2039 current_function_cannot_inline
2040 = "static constructors and destructors cannot be inlined";
2045 /* Finish the process of running a particular set of global constructors
2046 or destructors. Subroutine of do_[cd]tors. */
2049 finish_objects (int method_type, int initp, tree body)
2054 finish_compound_stmt (/*has_no_scope=*/0, body);
2055 fn = finish_function (0);
2058 /* When only doing semantic analysis, and no RTL generation, we
2059 can't call functions that directly emit assembly code; there is
2060 no assembly file in which to put the code. */
2061 if (flag_syntax_only)
2064 if (targetm.have_ctors_dtors)
2066 rtx fnsym = XEXP (DECL_RTL (fn), 0);
2067 if (method_type == 'I')
2068 (* targetm.asm_out.constructor) (fnsym, initp);
2070 (* targetm.asm_out.destructor) (fnsym, initp);
2074 /* The names of the parameters to the function created to handle
2075 initializations and destructions for objects with static storage
2077 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
2078 #define PRIORITY_IDENTIFIER "__priority"
2080 /* The name of the function we create to handle initializations and
2081 destructions for objects with static storage duration. */
2082 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
2084 /* The declaration for the __INITIALIZE_P argument. */
2085 static GTY(()) tree initialize_p_decl;
2087 /* The declaration for the __PRIORITY argument. */
2088 static GTY(()) tree priority_decl;
2090 /* The declaration for the static storage duration function. */
2091 static GTY(()) tree ssdf_decl;
2093 /* All the static storage duration functions created in this
2094 translation unit. */
2095 static GTY(()) varray_type ssdf_decls;
2097 /* A map from priority levels to information about that priority
2098 level. There may be many such levels, so efficient lookup is
2100 static splay_tree priority_info_map;
2102 /* Begins the generation of the function that will handle all
2103 initialization and destruction of objects with static storage
2104 duration. The function generated takes two parameters of type
2105 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
2106 nonzero, it performs initializations. Otherwise, it performs
2107 destructions. It only performs those initializations or
2108 destructions with the indicated __PRIORITY. The generated function
2111 It is assumed that this function will only be called once per
2112 translation unit. */
2115 start_static_storage_duration_function (void)
2117 static unsigned ssdf_number;
2122 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
2124 /* Create the identifier for this function. It will be of the form
2125 SSDF_IDENTIFIER_<number>. */
2126 sprintf (id, "%s_%u", SSDF_IDENTIFIER, ssdf_number++);
2127 if (ssdf_number == 0)
2129 /* Overflow occurred. That means there are at least 4 billion
2130 initialization functions. */
2131 sorry ("too many initialization functions required");
2135 /* Create the parameters. */
2136 parm_types = void_list_node;
2137 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
2138 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
2139 type = build_function_type (void_type_node, parm_types);
2141 /* Create the FUNCTION_DECL itself. */
2142 ssdf_decl = build_lang_decl (FUNCTION_DECL,
2143 get_identifier (id),
2145 TREE_PUBLIC (ssdf_decl) = 0;
2146 DECL_ARTIFICIAL (ssdf_decl) = 1;
2148 /* Put this function in the list of functions to be called from the
2149 static constructors and destructors. */
2152 VARRAY_TREE_INIT (ssdf_decls, 32, "ssdf_decls");
2154 /* Take this opportunity to initialize the map from priority
2155 numbers to information about that priority level. */
2156 priority_info_map = splay_tree_new (splay_tree_compare_ints,
2157 /*delete_key_fn=*/0,
2158 /*delete_value_fn=*/
2159 (splay_tree_delete_value_fn) &free);
2161 /* We always need to generate functions for the
2162 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
2163 priorities later, we'll be sure to find the
2164 DEFAULT_INIT_PRIORITY. */
2165 get_priority_info (DEFAULT_INIT_PRIORITY);
2168 VARRAY_PUSH_TREE (ssdf_decls, ssdf_decl);
2170 /* Create the argument list. */
2171 initialize_p_decl = cp_build_parm_decl
2172 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
2173 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
2174 TREE_USED (initialize_p_decl) = 1;
2175 priority_decl = cp_build_parm_decl
2176 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
2177 DECL_CONTEXT (priority_decl) = ssdf_decl;
2178 TREE_USED (priority_decl) = 1;
2180 TREE_CHAIN (initialize_p_decl) = priority_decl;
2181 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
2183 /* Put the function in the global scope. */
2184 pushdecl (ssdf_decl);
2186 /* Start the function itself. This is equivalent to declarating the
2189 static void __ssdf (int __initialize_p, init __priority_p);
2191 It is static because we only need to call this function from the
2192 various constructor and destructor functions for this module. */
2193 start_function (/*specs=*/NULL_TREE,
2195 /*attrs=*/NULL_TREE,
2198 /* Set up the scope of the outermost block in the function. */
2199 body = begin_compound_stmt (/*has_no_scope=*/0);
2201 /* This function must not be deferred because we are depending on
2202 its compilation to tell us what is TREE_SYMBOL_REFERENCED. */
2203 current_function_cannot_inline
2204 = "static storage duration functions cannot be inlined";
2209 /* Finish the generation of the function which performs initialization
2210 and destruction of objects with static storage duration. After
2211 this point, no more such objects can be created. */
2214 finish_static_storage_duration_function (tree body)
2216 /* Close out the function. */
2217 finish_compound_stmt (/*has_no_scope=*/0, body);
2218 expand_body (finish_function (0));
2221 /* Return the information about the indicated PRIORITY level. If no
2222 code to handle this level has yet been generated, generate the
2223 appropriate prologue. */
2225 static priority_info
2226 get_priority_info (int priority)
2231 n = splay_tree_lookup (priority_info_map,
2232 (splay_tree_key) priority);
2235 /* Create a new priority information structure, and insert it
2237 pi = (priority_info) xmalloc (sizeof (struct priority_info_s));
2238 pi->initializations_p = 0;
2239 pi->destructions_p = 0;
2240 splay_tree_insert (priority_info_map,
2241 (splay_tree_key) priority,
2242 (splay_tree_value) pi);
2245 pi = (priority_info) n->value;
2250 /* Set up to handle the initialization or destruction of DECL. If
2251 INITP is nonzero, we are initializing the variable. Otherwise, we
2252 are destroying it. */
2255 start_static_initialization_or_destruction (tree decl, int initp)
2257 tree guard_if_stmt = NULL_TREE;
2264 /* Figure out the priority for this declaration. */
2265 priority = DECL_INIT_PRIORITY (decl);
2267 priority = DEFAULT_INIT_PRIORITY;
2269 /* Remember that we had an initialization or finalization at this
2271 pi = get_priority_info (priority);
2273 pi->initializations_p = 1;
2275 pi->destructions_p = 1;
2277 /* Trick the compiler into thinking we are at the file and line
2278 where DECL was declared so that error-messages make sense, and so
2279 that the debugger will show somewhat sensible file and line
2281 input_filename = DECL_SOURCE_FILE (decl);
2282 lineno = DECL_SOURCE_LINE (decl);
2288 Access control for implicit calls to the constructors,
2289 the conversion functions, or the destructor called to
2290 create and destroy a static data member is performed as
2291 if these calls appeared in the scope of the member's
2294 we pretend we are in a static member function of the class of
2295 which the DECL is a member. */
2296 if (member_p (decl))
2298 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
2299 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
2302 /* Conditionalize this initialization on being in the right priority
2303 and being initializing/finalizing appropriately. */
2304 guard_if_stmt = begin_if_stmt ();
2305 cond = cp_build_binary_op (EQ_EXPR,
2307 build_int_2 (priority, 0));
2308 init_cond = initp ? integer_one_node : integer_zero_node;
2309 init_cond = cp_build_binary_op (EQ_EXPR,
2312 cond = cp_build_binary_op (TRUTH_ANDIF_EXPR, cond, init_cond);
2314 /* Assume we don't need a guard. */
2316 /* We need a guard if this is an object with external linkage that
2317 might be initialized in more than one place. (For example, a
2318 static data member of a template, when the data member requires
2320 if (TREE_PUBLIC (decl) && (DECL_COMMON (decl)
2321 || DECL_ONE_ONLY (decl)
2322 || DECL_WEAK (decl)))
2326 guard = get_guard (decl);
2328 /* When using __cxa_atexit, we just check the GUARD as we would
2329 for a local static. */
2330 if (flag_use_cxa_atexit)
2332 /* When using __cxa_atexit, we never try to destroy
2333 anything from a static destructor. */
2334 my_friendly_assert (initp, 20000629);
2335 guard_cond = get_guard_cond (guard);
2337 /* If we don't have __cxa_atexit, then we will be running
2338 destructors from .fini sections, or their equivalents. So,
2339 we need to know how many times we've tried to initialize this
2340 object. We do initializations only if the GUARD is zero,
2341 i.e., if we are the first to initialize the variable. We do
2342 destructions only if the GUARD is one, i.e., if we are the
2343 last to destroy the variable. */
2346 = cp_build_binary_op (EQ_EXPR,
2347 build_unary_op (PREINCREMENT_EXPR,
2353 = cp_build_binary_op (EQ_EXPR,
2354 build_unary_op (PREDECREMENT_EXPR,
2359 cond = cp_build_binary_op (TRUTH_ANDIF_EXPR, cond, guard_cond);
2362 finish_if_stmt_cond (cond, guard_if_stmt);
2364 /* If we're using __cxa_atexit, we have not already set the GUARD,
2365 so we must do so now. */
2366 if (guard && initp && flag_use_cxa_atexit)
2367 finish_expr_stmt (set_guard (guard));
2369 return guard_if_stmt;
2372 /* We've just finished generating code to do an initialization or
2373 finalization. GUARD_IF_STMT is the if-statement we used to guard
2374 the initialization. */
2377 finish_static_initialization_or_destruction (tree guard_if_stmt)
2379 finish_then_clause (guard_if_stmt);
2382 /* Now that we're done with DECL we don't need to pretend to be a
2383 member of its class any longer. */
2384 DECL_CONTEXT (current_function_decl) = NULL_TREE;
2385 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
2388 /* Generate code to do the initialization of DECL, a VAR_DECL with
2389 static storage duration. The initialization is INIT. */
2392 do_static_initialization (tree decl, tree init)
2396 /* Set up for the initialization. */
2398 = start_static_initialization_or_destruction (decl,
2401 /* Perform the initialization. */
2403 finish_expr_stmt (init);
2405 /* If we're using __cxa_atexit, register a a function that calls the
2406 destructor for the object. */
2407 if (flag_use_cxa_atexit)
2408 register_dtor_fn (decl);
2411 finish_static_initialization_or_destruction (guard_if_stmt);
2414 /* Generate code to do the static destruction of DECL. If DECL may be
2415 initialized more than once in different object files, GUARD is the
2416 guard variable to check. PRIORITY is the priority for the
2420 do_static_destruction (tree decl)
2424 /* If we're using __cxa_atexit, then destructors are registered
2425 immediately after objects are initialized. */
2426 my_friendly_assert (!flag_use_cxa_atexit, 20000121);
2428 /* If we don't need a destructor, there's nothing to do. */
2429 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
2432 /* Actually do the destruction. */
2433 guard_if_stmt = start_static_initialization_or_destruction (decl,
2435 finish_expr_stmt (build_cleanup (decl));
2436 finish_static_initialization_or_destruction (guard_if_stmt);
2439 /* VARS is a list of variables with static storage duration which may
2440 need initialization and/or finalization. Remove those variables
2441 that don't really need to be initialized or finalized, and return
2442 the resulting list. The order in which the variables appear in
2443 VARS is in reverse order of the order in which they should actually
2444 be initialized. The list we return is in the unreversed order;
2445 i.e., the first variable should be initialized first. */
2448 prune_vars_needing_no_initialization (tree vars)
2453 for (var = vars, result = NULL_TREE;
2455 var = TREE_CHAIN (var))
2457 tree decl = TREE_VALUE (var);
2458 tree init = TREE_PURPOSE (var);
2460 /* Deal gracefully with error. */
2461 if (decl == error_mark_node)
2464 /* The only things that can be initialized are variables. */
2465 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 19990420);
2467 /* If this object is not defined, we don't need to do anything
2469 if (DECL_EXTERNAL (decl))
2472 /* Also, if the initializer already contains errors, we can bail
2474 if (init && TREE_CODE (init) == TREE_LIST
2475 && value_member (error_mark_node, init))
2478 /* This variable is going to need initialization and/or
2479 finalization, so we add it to the list. */
2480 result = tree_cons (init, decl, result);
2486 /* Make sure we have told the back end about all the variables in
2490 write_out_vars (tree vars)
2494 for (v = vars; v; v = TREE_CHAIN (v))
2495 if (! TREE_ASM_WRITTEN (TREE_VALUE (v)))
2496 rest_of_decl_compilation (TREE_VALUE (v), 0, 1, 1);
2499 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
2500 (otherwise) that will initialize all gobal objects with static
2501 storage duration having the indicated PRIORITY. */
2504 generate_ctor_or_dtor_function (bool constructor_p, int priority)
2511 /* We use `I' to indicate initialization and `D' to indicate
2518 /* Begin the function. */
2519 body = start_objects (function_key, priority);
2521 /* Call the static storage duration function with appropriate
2523 for (i = 0; i < ssdf_decls->elements_used; ++i)
2525 arguments = tree_cons (NULL_TREE, build_int_2 (priority, 0),
2527 arguments = tree_cons (NULL_TREE, build_int_2 (constructor_p, 0),
2529 finish_expr_stmt (build_function_call (VARRAY_TREE (ssdf_decls, i),
2533 /* If we're generating code for the DEFAULT_INIT_PRIORITY, throw in
2534 calls to any functions marked with attributes indicating that
2535 they should be called at initialization- or destruction-time. */
2536 if (priority == DEFAULT_INIT_PRIORITY)
2540 for (fns = constructor_p ? static_ctors : static_dtors;
2542 fns = TREE_CHAIN (fns))
2543 finish_expr_stmt (build_function_call (TREE_VALUE (fns), NULL_TREE));
2546 /* Close out the function. */
2547 finish_objects (function_key, priority, body);
2550 /* Generate constructor and destructor functions for the priority
2554 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n,
2555 void * data ATTRIBUTE_UNUSED)
2557 int priority = (int) n->key;
2558 priority_info pi = (priority_info) n->value;
2560 /* Generate the functions themselves, but only if they are really
2562 if (pi->initializations_p
2563 || (priority == DEFAULT_INIT_PRIORITY && static_ctors))
2564 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority);
2565 if (pi->destructions_p
2566 || (priority == DEFAULT_INIT_PRIORITY && static_dtors))
2567 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority);
2569 /* Keep iterating. */
2573 /* This routine is called from the last rule in yyparse ().
2574 Its job is to create all the code needed to initialize and
2575 destroy the global aggregates. We do the destruction
2576 first, since that way we only need to reverse the decls once. */
2587 /* Bad parse errors. Just forget about it. */
2588 if (! global_bindings_p () || current_class_type || decl_namespace_list)
2592 c_common_write_pch ();
2594 /* Otherwise, GDB can get confused, because in only knows
2595 about source for LINENO-1 lines. */
2598 interface_unknown = 1;
2601 /* We now have to write out all the stuff we put off writing out.
2604 o Template specializations that we have not yet instantiated,
2605 but which are needed.
2606 o Initialization and destruction for non-local objects with
2607 static storage duration. (Local objects with static storage
2608 duration are initialized when their scope is first entered,
2609 and are cleaned up via atexit.)
2610 o Virtual function tables.
2612 All of these may cause others to be needed. For example,
2613 instantiating one function may cause another to be needed, and
2614 generating the initializer for an object may cause templates to be
2615 instantiated, etc., etc. */
2617 timevar_push (TV_VARCONST);
2619 emit_support_tinfos ();
2627 /* If there are templates that we've put off instantiating, do
2629 instantiate_pending_templates ();
2631 /* Write out virtual tables as required. Note that writing out
2632 the virtual table for a template class may cause the
2633 instantiation of members of that class. If we write out
2634 vtables then we remove the class from our list so we don't
2635 have to look at it again. */
2637 while (keyed_classes != NULL_TREE
2638 && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
2641 keyed_classes = TREE_CHAIN (keyed_classes);
2647 tree next = TREE_CHAIN (t);
2651 if (maybe_emit_vtables (TREE_VALUE (next)))
2654 TREE_CHAIN (t) = TREE_CHAIN (next);
2659 next = TREE_CHAIN (t);
2663 /* Write out needed type info variables. Writing out one variable
2664 might cause others to be needed. */
2665 if (walk_globals (unemitted_tinfo_decl_p, emit_tinfo_decl, /*data=*/0))
2668 /* The list of objects with static storage duration is built up
2669 in reverse order. We clear STATIC_AGGREGATES so that any new
2670 aggregates added during the initialization of these will be
2671 initialized in the correct order when we next come around the
2673 vars = prune_vars_needing_no_initialization (static_aggregates);
2674 static_aggregates = NULL_TREE;
2680 /* We need to start a new initialization function each time
2681 through the loop. That's because we need to know which
2682 vtables have been referenced, and TREE_SYMBOL_REFERENCED
2683 isn't computed until a function is finished, and written
2684 out. That's a deficiency in the back-end. When this is
2685 fixed, these initialization functions could all become
2686 inline, with resulting performance improvements. */
2687 tree ssdf_body = start_static_storage_duration_function ();
2689 /* Make sure the back end knows about all the variables. */
2690 write_out_vars (vars);
2692 /* First generate code to do all the initializations. */
2693 for (v = vars; v; v = TREE_CHAIN (v))
2694 do_static_initialization (TREE_VALUE (v),
2697 /* Then, generate code to do all the destructions. Do these
2698 in reverse order so that the most recently constructed
2699 variable is the first destroyed. If we're using
2700 __cxa_atexit, then we don't need to do this; functions
2701 were registered at initialization time to destroy the
2703 if (!flag_use_cxa_atexit)
2705 vars = nreverse (vars);
2706 for (v = vars; v; v = TREE_CHAIN (v))
2707 do_static_destruction (TREE_VALUE (v));
2712 /* Finish up the static storage duration function for this
2714 finish_static_storage_duration_function (ssdf_body);
2716 /* All those initializations and finalizations might cause
2717 us to need more inline functions, more template
2718 instantiations, etc. */
2722 for (i = 0; i < deferred_fns_used; ++i)
2724 tree decl = VARRAY_TREE (deferred_fns, i);
2726 import_export_decl (decl);
2728 /* Does it need synthesizing? */
2729 if (DECL_ARTIFICIAL (decl) && ! DECL_INITIAL (decl)
2731 && (! DECL_REALLY_EXTERN (decl) || DECL_INLINE (decl)))
2733 /* Even though we're already at the top-level, we push
2734 there again. That way, when we pop back a few lines
2735 hence, all of our state is restored. Otherwise,
2736 finish_function doesn't clean things up, and we end
2737 up with CURRENT_FUNCTION_DECL set. */
2738 push_to_top_level ();
2739 synthesize_method (decl);
2740 pop_from_top_level ();
2744 /* We lie to the back-end, pretending that some functions
2745 are not defined when they really are. This keeps these
2746 functions from being put out unnecessarily. But, we must
2747 stop lying when the functions are referenced, or if they
2748 are not comdat since they need to be put out now. This
2749 is done in a separate for cycle, because if some deferred
2750 function is contained in another deferred function later
2751 in deferred_fns varray, rest_of_compilation would skip
2752 this function and we really cannot expand the same
2754 if (DECL_NOT_REALLY_EXTERN (decl)
2755 && DECL_INITIAL (decl)
2756 && DECL_NEEDED_P (decl))
2757 DECL_EXTERNAL (decl) = 0;
2759 /* If we're going to need to write this function out, and
2760 there's already a body for it, create RTL for it now.
2761 (There might be no body if this is a method we haven't
2762 gotten around to synthesizing yet.) */
2763 if (!DECL_EXTERNAL (decl)
2764 && DECL_NEEDED_P (decl)
2765 && DECL_SAVED_TREE (decl)
2766 && !TREE_ASM_WRITTEN (decl))
2768 int saved_not_really_extern;
2770 /* When we call finish_function in expand_body, it will
2771 try to reset DECL_NOT_REALLY_EXTERN so we save and
2773 saved_not_really_extern = DECL_NOT_REALLY_EXTERN (decl);
2774 /* Generate RTL for this function now that we know we
2777 /* Undo the damage done by finish_function. */
2778 DECL_EXTERNAL (decl) = 0;
2779 DECL_NOT_REALLY_EXTERN (decl) = saved_not_really_extern;
2780 /* If we're compiling -fsyntax-only pretend that this
2781 function has been written out so that we don't try to
2783 if (flag_syntax_only)
2784 TREE_ASM_WRITTEN (decl) = 1;
2789 if (deferred_fns_used
2790 && wrapup_global_declarations (&VARRAY_TREE (deferred_fns, 0),
2793 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
2796 /* Static data members are just like namespace-scope globals. */
2797 for (i = 0; i < pending_statics_used; ++i)
2799 tree decl = VARRAY_TREE (pending_statics, i);
2800 if (TREE_ASM_WRITTEN (decl))
2802 import_export_decl (decl);
2803 if (DECL_NOT_REALLY_EXTERN (decl) && ! DECL_IN_AGGR_P (decl))
2804 DECL_EXTERNAL (decl) = 0;
2807 && wrapup_global_declarations (&VARRAY_TREE (pending_statics, 0),
2808 pending_statics_used))
2813 /* All used inline functions must have a definition at this point. */
2814 for (i = 0; i < deferred_fns_used; ++i)
2816 tree decl = VARRAY_TREE (deferred_fns, i);
2818 if (TREE_USED (decl) && DECL_DECLARED_INLINE_P (decl)
2819 && !(TREE_ASM_WRITTEN (decl) || DECL_SAVED_TREE (decl)))
2821 cp_warning_at ("inline function `%D' used but never defined", decl);
2822 /* This symbol is effectively an "extern" declaration now.
2823 This is not strictly necessary, but removes a duplicate
2825 TREE_PUBLIC (decl) = 1;
2830 /* We give C linkage to static constructors and destructors. */
2831 push_lang_context (lang_name_c);
2833 /* Generate initialization and destruction functions for all
2834 priorities for which they are required. */
2835 if (priority_info_map)
2836 splay_tree_foreach (priority_info_map,
2837 generate_ctor_and_dtor_functions_for_priority,
2840 /* We're done with the splay-tree now. */
2841 if (priority_info_map)
2842 splay_tree_delete (priority_info_map);
2844 /* We're done with static constructors, so we can go back to "C++"
2846 pop_lang_context ();
2848 /* Now, issue warnings about static, but not defined, functions,
2849 etc., and emit debugging information. */
2850 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
2851 if (pending_statics)
2852 check_global_declarations (&VARRAY_TREE (pending_statics, 0),
2853 pending_statics_used);
2857 /* The entire file is now complete. If requested, dump everything
2861 FILE *stream = dump_begin (TDI_all, &flags);
2865 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
2866 dump_end (TDI_all, stream);
2870 timevar_pop (TV_VARCONST);
2872 if (flag_detailed_statistics)
2874 dump_tree_statistics ();
2875 dump_time_statistics ();
2879 /* This is something of the form 'A()()()()()+1' that has turned out to be an
2880 expr. Since it was parsed like a type, we need to wade through and fix
2881 that. Unfortunately, since operator() is left-associative, we can't use
2882 tail recursion. In the above example, TYPE is `A', and DECL is
2885 Maybe this shouldn't be recursive, but how often will it actually be
2889 reparse_absdcl_as_expr (type, decl)
2892 /* do build_functional_cast (type, NULL_TREE) at bottom */
2893 if (TREE_OPERAND (decl, 0) == NULL_TREE)
2894 return build_functional_cast (type, NULL_TREE);
2897 decl = reparse_absdcl_as_expr (type, TREE_OPERAND (decl, 0));
2899 return finish_call_expr (decl, NULL_TREE, /*disallow_virtual=*/false);
2902 /* This is something of the form `int ((int)(int)(int)1)' that has turned
2903 out to be an expr. Since it was parsed like a type, we need to wade
2904 through and fix that. Since casts are right-associative, we are
2905 reversing the order, so we don't have to recurse.
2907 In the above example, DECL is the `(int)(int)(int)', and EXPR is the
2911 reparse_absdcl_as_casts (decl, expr)
2917 if (TREE_CODE (expr) == CONSTRUCTOR
2918 && TREE_TYPE (expr) == 0)
2920 type = groktypename (TREE_VALUE (CALL_DECLARATOR_PARMS (decl)));
2921 decl = TREE_OPERAND (decl, 0);
2923 if (processing_template_decl)
2924 TREE_TYPE (expr) = type;
2927 expr = digest_init (type, expr, (tree *) 0);
2928 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
2930 int failure = complete_array_type (type, expr, 1);
2931 my_friendly_assert (!failure, 78);
2938 type = groktypename (TREE_VALUE (CALL_DECLARATOR_PARMS (decl)));
2939 decl = TREE_OPERAND (decl, 0);
2940 if (!VOID_TYPE_P (type))
2942 expr = build_c_cast (type, expr);
2945 if (warn_old_style_cast && ! in_system_header
2946 && non_void_p && current_lang_name != lang_name_c)
2947 warning ("use of old-style cast");
2952 /* T is the parse tree for an expression. Return the expression after
2953 performing semantic analysis. */
2956 build_expr_from_tree (t)
2959 if (t == NULL_TREE || t == error_mark_node)
2962 switch (TREE_CODE (t))
2964 case IDENTIFIER_NODE:
2965 return do_identifier (t, NULL_TREE);
2968 if (LOOKUP_EXPR_GLOBAL (t))
2970 tree token = TREE_OPERAND (t, 0);
2971 return do_scoped_id (token, IDENTIFIER_GLOBAL_VALUE (token));
2975 t = do_identifier (TREE_OPERAND (t, 0), NULL_TREE);
2976 if (TREE_CODE (t) == ALIAS_DECL)
2977 t = DECL_INITIAL (t);
2981 case TEMPLATE_ID_EXPR:
2987 template = build_expr_from_tree (TREE_OPERAND (t, 0));
2988 args = build_expr_from_tree (TREE_OPERAND (t, 1));
2990 if (TREE_CODE (template) == COMPONENT_REF)
2992 object = TREE_OPERAND (template, 0);
2993 template = TREE_OPERAND (template, 1);
2998 template = lookup_template_function (template, args);
3000 return build (COMPONENT_REF, TREE_TYPE (template),
3007 return build_x_indirect_ref
3008 (build_expr_from_tree (TREE_OPERAND (t, 0)), "unary *");
3011 return build_functional_cast
3012 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3014 case REINTERPRET_CAST_EXPR:
3015 return build_reinterpret_cast
3016 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3018 case CONST_CAST_EXPR:
3019 return build_const_cast
3020 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3022 case DYNAMIC_CAST_EXPR:
3023 return build_dynamic_cast
3024 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3026 case STATIC_CAST_EXPR:
3027 return build_static_cast
3028 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3030 case PREDECREMENT_EXPR:
3031 case PREINCREMENT_EXPR:
3032 case POSTDECREMENT_EXPR:
3033 case POSTINCREMENT_EXPR:
3037 case TRUTH_NOT_EXPR:
3039 case CONVERT_EXPR: /* Unary + */
3044 return build_x_unary_op (TREE_CODE (t),
3045 build_expr_from_tree (TREE_OPERAND (t, 0)));
3050 case TRUNC_DIV_EXPR:
3052 case FLOOR_DIV_EXPR:
3053 case ROUND_DIV_EXPR:
3054 case EXACT_DIV_EXPR:
3056 case BIT_ANDTC_EXPR:
3059 case TRUNC_MOD_EXPR:
3060 case FLOOR_MOD_EXPR:
3061 case TRUTH_ANDIF_EXPR:
3062 case TRUTH_ORIF_EXPR:
3063 case TRUTH_AND_EXPR:
3078 return build_x_binary_op
3080 build_expr_from_tree (TREE_OPERAND (t, 0)),
3081 build_expr_from_tree (TREE_OPERAND (t, 1)));
3084 return build_m_component_ref
3085 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3086 build_expr_from_tree (TREE_OPERAND (t, 1)));
3089 return build_offset_ref (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1));
3092 if (TREE_OPERAND (t, 0) == NULL_TREE)
3094 return build_nt (ARRAY_REF, NULL_TREE,
3095 build_expr_from_tree (TREE_OPERAND (t, 1)));
3096 return grok_array_decl (build_expr_from_tree (TREE_OPERAND (t, 0)),
3097 build_expr_from_tree (TREE_OPERAND (t, 1)));
3102 tree r = build_expr_from_tree (TREE_OPERAND (t, 0));
3104 return TREE_CODE (t) == SIZEOF_EXPR ? expr_sizeof (r) : c_alignof_expr (r);
3106 return cxx_sizeof_or_alignof_type (r, TREE_CODE (t), true);
3110 return build_x_modify_expr
3111 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3112 TREE_CODE (TREE_OPERAND (t, 1)),
3113 build_expr_from_tree (TREE_OPERAND (t, 2)));
3116 return build_x_arrow
3117 (build_expr_from_tree (TREE_OPERAND (t, 0)));
3121 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3122 build_expr_from_tree (TREE_OPERAND (t, 1)),
3123 build_expr_from_tree (TREE_OPERAND (t, 2)),
3124 NEW_EXPR_USE_GLOBAL (t));
3127 return delete_sanity
3128 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3129 build_expr_from_tree (TREE_OPERAND (t, 1)),
3130 DELETE_EXPR_USE_VEC (t), DELETE_EXPR_USE_GLOBAL (t));
3133 if (TREE_OPERAND (t, 1) == NULL_TREE)
3134 return build_x_compound_expr
3135 (build_expr_from_tree (TREE_OPERAND (t, 0)));
3139 case METHOD_CALL_EXPR:
3140 if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
3142 tree ref = TREE_OPERAND (t, 0);
3143 tree name = TREE_OPERAND (ref, 1);
3145 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3146 name = build_nt (TEMPLATE_ID_EXPR,
3147 TREE_OPERAND (name, 0),
3148 build_expr_from_tree (TREE_OPERAND (name, 1)));
3150 return build_scoped_method_call
3151 (build_expr_from_tree (TREE_OPERAND (t, 1)),
3152 build_expr_from_tree (TREE_OPERAND (ref, 0)),
3154 build_expr_from_tree (TREE_OPERAND (t, 2)));
3158 tree fn = TREE_OPERAND (t, 0);
3160 /* We can get a TEMPLATE_ID_EXPR here on code like:
3164 so we must resolve that. However, we can also get things
3165 like a BIT_NOT_EXPR here, when referring to a destructor,
3166 and things like that are not correctly resolved by
3167 build_expr_from_tree. So, just use build_expr_from_tree
3168 when we really need it. */
3169 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3170 fn = lookup_template_function
3171 (TREE_OPERAND (fn, 0),
3172 build_expr_from_tree (TREE_OPERAND (fn, 1)));
3174 return build_method_call
3175 (build_expr_from_tree (TREE_OPERAND (t, 1)),
3177 build_expr_from_tree (TREE_OPERAND (t, 2)),
3178 NULL_TREE, LOOKUP_NORMAL);
3182 if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
3184 tree ref = TREE_OPERAND (t, 0);
3185 tree name = TREE_OPERAND (ref, 1);
3186 tree fn, scope, args;
3188 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3189 name = build_nt (TEMPLATE_ID_EXPR,
3190 TREE_OPERAND (name, 0),
3191 build_expr_from_tree (TREE_OPERAND (name, 1)));
3193 scope = build_expr_from_tree (TREE_OPERAND (ref, 0));
3194 args = build_expr_from_tree (TREE_OPERAND (t, 1));
3195 fn = resolve_scoped_fn_name (scope, name);
3197 return build_call_from_tree (fn, args, 1);
3201 tree name = TREE_OPERAND (t, 0);
3203 tree args = build_expr_from_tree (TREE_OPERAND (t, 1));
3204 if (args != NULL_TREE && TREE_CODE (name) == LOOKUP_EXPR
3205 && !LOOKUP_EXPR_GLOBAL (name)
3206 && TREE_CODE ((id = TREE_OPERAND (name, 0))) == IDENTIFIER_NODE
3207 && (!current_class_type
3208 || !lookup_member (current_class_type, id, 0, 0)))
3210 /* Do Koenig lookup if there are no class members. */
3211 name = do_identifier (id, args);
3213 else if (TREE_CODE (name) == TEMPLATE_ID_EXPR
3214 || ! really_overloaded_fn (name))
3215 name = build_expr_from_tree (name);
3217 if (TREE_CODE (name) == OFFSET_REF)
3218 return build_offset_ref_call_from_tree (name, args);
3219 if (TREE_CODE (name) == COMPONENT_REF)
3220 return finish_object_call_expr (TREE_OPERAND (name, 1),
3221 TREE_OPERAND (name, 0),
3223 name = convert_from_reference (name);
3224 return build_call_from_tree (name, args,
3225 /*disallow_virtual=*/false);
3229 return build_x_conditional_expr
3230 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3231 build_expr_from_tree (TREE_OPERAND (t, 1)),
3232 build_expr_from_tree (TREE_OPERAND (t, 2)));
3234 case PSEUDO_DTOR_EXPR:
3235 return (finish_pseudo_destructor_expr
3236 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3237 build_expr_from_tree (TREE_OPERAND (t, 1)),
3238 build_expr_from_tree (TREE_OPERAND (t, 2))));
3242 tree purpose, value, chain;
3244 if (t == void_list_node)
3247 purpose = TREE_PURPOSE (t);
3249 purpose = build_expr_from_tree (purpose);
3250 value = TREE_VALUE (t);
3252 value = build_expr_from_tree (value);
3253 chain = TREE_CHAIN (t);
3254 if (chain && chain != void_type_node)
3255 chain = build_expr_from_tree (chain);
3256 return tree_cons (purpose, value, chain);
3261 tree object = build_expr_from_tree (TREE_OPERAND (t, 0));
3262 tree member = TREE_OPERAND (t, 1);
3264 if (!CLASS_TYPE_P (TREE_TYPE (object)))
3266 if (TREE_CODE (member) == BIT_NOT_EXPR)
3267 return finish_pseudo_destructor_expr (object,
3269 TREE_TYPE (object));
3270 else if (TREE_CODE (member) == SCOPE_REF
3271 && (TREE_CODE (TREE_OPERAND (member, 1)) == BIT_NOT_EXPR))
3272 return finish_pseudo_destructor_expr (object,
3273 TREE_OPERAND (t, 0),
3274 TREE_TYPE (object));
3276 else if (TREE_CODE (member) == SCOPE_REF
3277 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
3282 /* Lookup the template functions now that we know what the
3284 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
3285 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
3286 member = lookup_qualified_name (TREE_OPERAND (member, 0),
3290 if (BASELINK_P (member))
3291 BASELINK_FUNCTIONS (member)
3292 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
3296 error ("`%D' is not a member of `%T'",
3297 tmpl, TREE_TYPE (object));
3298 return error_mark_node;
3303 return finish_class_member_access_expr (object, member);
3307 return build_throw (build_expr_from_tree (TREE_OPERAND (t, 0)));
3313 tree type = TREE_TYPE (t);
3316 /* digest_init will do the wrong thing if we let it. */
3317 if (type && TYPE_PTRMEMFUNC_P (type))
3321 /* We do not want to process the purpose of aggregate
3322 initializers as they are identifier nodes which will be
3323 looked up by digest_init. */
3324 purpose_p = !(type && IS_AGGR_TYPE (type));
3325 for (elts = CONSTRUCTOR_ELTS (t); elts; elts = TREE_CHAIN (elts))
3327 tree purpose = TREE_PURPOSE (elts);
3328 tree value = TREE_VALUE (elts);
3330 if (purpose && purpose_p)
3331 purpose = build_expr_from_tree (purpose);
3332 value = build_expr_from_tree (value);
3333 r = tree_cons (purpose, value, r);
3336 r = build_nt (CONSTRUCTOR, NULL_TREE, nreverse (r));
3337 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
3340 return digest_init (type, r, 0);
3345 if (TYPE_P (TREE_OPERAND (t, 0)))
3346 return get_typeid (TREE_OPERAND (t, 0));
3347 return build_typeid (build_expr_from_tree (TREE_OPERAND (t, 0)));
3351 return convert_from_reference (t);
3354 return build_va_arg (build_expr_from_tree (TREE_OPERAND (t, 0)),
3362 /* FN is an OFFSET_REF indicating the function to call in parse-tree
3363 form; it has not yet been semantically analyzed. ARGS are the
3364 arguments to the function. They have already been semantically
3368 build_offset_ref_call_from_tree (tree fn, tree args)
3372 my_friendly_assert (TREE_CODE (fn) == OFFSET_REF, 20020725);
3374 /* A qualified name corresponding to a non-static member
3375 function or a pointer-to-member is represented as an
3378 For both of these function calls, FN will be an OFFSET_REF.
3380 struct A { void f(); };
3381 void A::f() { (A::f) (); }
3383 struct B { void g(); };
3385 void B::g() { (this->*p)(); } */
3387 /* This code is not really correct (for example, it does not
3388 handle the case that `A::f' is overloaded), but it is
3389 historically how we have handled this situation. */
3390 object_addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (fn, 0), 0);
3391 if (TREE_CODE (TREE_OPERAND (fn, 1)) == FIELD_DECL)
3392 fn = resolve_offset_ref (fn);
3395 fn = TREE_OPERAND (fn, 1);
3396 fn = get_member_function_from_ptrfunc (&object_addr, fn);
3398 args = tree_cons (NULL_TREE, object_addr, args);
3399 return build_function_call (fn, args);
3402 /* FN indicates the function to call. Name resolution has been
3403 performed on FN. ARGS are the arguments to the function. They
3404 have already been semantically analyzed. DISALLOW_VIRTUAL is true
3405 if the function call should be determined at compile time, even if
3409 build_call_from_tree (tree fn, tree args, bool disallow_virtual)
3415 /* Check to see that name lookup has already been performed. */
3416 my_friendly_assert (TREE_CODE (fn) != OFFSET_REF, 20020725);
3417 my_friendly_assert (TREE_CODE (fn) != SCOPE_REF, 20020725);
3419 /* In the future all of this should be eliminated. Instead,
3420 name-lookup for a member function should simply return a
3421 baselink, instead of a FUNCTION_DECL, TEMPLATE_DECL, or
3422 TEMPLATE_ID_EXPR. */
3424 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3427 template_args = TREE_OPERAND (fn, 1);
3428 fn = TREE_OPERAND (fn, 0);
3432 template_id = NULL_TREE;
3433 template_args = NULL_TREE;
3436 f = (TREE_CODE (fn) == OVERLOAD) ? get_first_fn (fn) : fn;
3437 /* Make sure we have a baselink (rather than simply a
3438 FUNCTION_DECL) for a member function. */
3439 if (current_class_type
3440 && ((TREE_CODE (f) == FUNCTION_DECL
3441 && DECL_FUNCTION_MEMBER_P (f))
3442 || (DECL_FUNCTION_TEMPLATE_P (f)
3443 && DECL_FUNCTION_MEMBER_P (f))))
3445 f = lookup_member (current_class_type, DECL_NAME (f),
3446 /*protect=*/1, /*want_type=*/0);
3453 if (BASELINK_P (fn))
3454 BASELINK_FUNCTIONS (fn) = build_nt (TEMPLATE_ID_EXPR,
3455 BASELINK_FUNCTIONS (fn),
3461 return finish_call_expr (fn, args, disallow_virtual);
3464 /* This is something of the form `int (*a)++' that has turned out to be an
3465 expr. It was only converted into parse nodes, so we need to go through
3466 and build up the semantics. Most of the work is done by
3467 build_expr_from_tree, above.
3469 In the above example, TYPE is `int' and DECL is `*a'. */
3472 reparse_decl_as_expr (tree type, tree decl)
3474 decl = build_expr_from_tree (decl);
3476 return build_functional_cast (type, build_tree_list (NULL_TREE, decl));
3481 /* This is something of the form `int (*a)' that has turned out to be a
3482 decl. It was only converted into parse nodes, so we need to do the
3483 checking that make_{pointer,reference}_declarator do. */
3486 finish_decl_parsing (tree decl)
3488 switch (TREE_CODE (decl))
3490 case IDENTIFIER_NODE:
3493 return make_pointer_declarator
3494 (NULL_TREE, finish_decl_parsing (TREE_OPERAND (decl, 0)));
3496 return make_reference_declarator
3497 (NULL_TREE, finish_decl_parsing (TREE_OPERAND (decl, 0)));
3499 TREE_OPERAND (decl, 0) = finish_decl_parsing (TREE_OPERAND (decl, 0));
3502 push_nested_class (TREE_TYPE (TREE_OPERAND (decl, 0)), 3);
3503 TREE_COMPLEXITY (decl) = current_class_depth;
3506 TREE_OPERAND (decl, 0) = finish_decl_parsing (TREE_OPERAND (decl, 0));
3509 /* For attribute handling. */
3510 TREE_VALUE (decl) = finish_decl_parsing (TREE_VALUE (decl));
3512 case TEMPLATE_ID_EXPR:
3520 /* Return 1 if root encloses child. */
3523 is_namespace_ancestor (tree root, tree child)
3527 if (root == global_namespace)
3529 if (child == global_namespace)
3531 return is_namespace_ancestor (root, CP_DECL_CONTEXT (child));
3535 /* Return the namespace that is the common ancestor
3536 of two given namespaces. */
3539 namespace_ancestor (tree ns1, tree ns2)
3541 if (is_namespace_ancestor (ns1, ns2))
3543 return namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2);
3546 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
3547 directive is not directly from the source. Also find the common
3548 ancestor and let our users know about the new namespace */
3550 add_using_namespace (tree user, tree used, bool indirect)
3553 /* Using oneself is a no-op. */
3556 my_friendly_assert (TREE_CODE (user) == NAMESPACE_DECL, 380);
3557 my_friendly_assert (TREE_CODE (used) == NAMESPACE_DECL, 380);
3558 /* Check if we already have this. */
3559 t = purpose_member (used, DECL_NAMESPACE_USING (user));
3563 /* Promote to direct usage. */
3564 TREE_INDIRECT_USING (t) = 0;
3568 /* Add used to the user's using list. */
3569 DECL_NAMESPACE_USING (user)
3570 = tree_cons (used, namespace_ancestor (user, used),
3571 DECL_NAMESPACE_USING (user));
3573 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
3575 /* Add user to the used's users list. */
3576 DECL_NAMESPACE_USERS (used)
3577 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
3579 /* Recursively add all namespaces used. */
3580 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
3581 /* indirect usage */
3582 add_using_namespace (user, TREE_PURPOSE (t), 1);
3584 /* Tell everyone using us about the new used namespaces. */
3585 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
3586 add_using_namespace (TREE_PURPOSE (t), used, 1);
3589 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
3590 duplicates. The first list becomes the tail of the result.
3592 The algorithm is O(n^2). We could get this down to O(n log n) by
3593 doing a sort on the addresses of the functions, if that becomes
3597 merge_functions (tree s1, tree s2)
3599 for (; s2; s2 = OVL_NEXT (s2))
3601 tree fn2 = OVL_CURRENT (s2);
3604 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
3606 tree fn1 = OVL_CURRENT (fns1);
3608 /* If the function from S2 is already in S1, there is no
3609 need to add it again. For `extern "C"' functions, we
3610 might have two FUNCTION_DECLs for the same function, in
3611 different namespaces; again, we only need one of them. */
3613 || (DECL_EXTERN_C_P (fn1) && DECL_EXTERN_C_P (fn2)
3614 && DECL_NAME (fn1) == DECL_NAME (fn2)))
3618 /* If we exhausted all of the functions in S1, FN2 is new. */
3620 s1 = build_overload (fn2, s1);
3625 /* This should return an error not all definitions define functions.
3626 It is not an error if we find two functions with exactly the
3627 same signature, only if these are selected in overload resolution.
3628 old is the current set of bindings, new the freshly-found binding.
3629 XXX Do we want to give *all* candidates in case of ambiguity?
3630 XXX In what way should I treat extern declarations?
3631 XXX I don't want to repeat the entire duplicate_decls here */
3634 ambiguous_decl (tree name, tree old, tree new, int flags)
3637 my_friendly_assert (old != NULL_TREE, 393);
3638 /* Copy the value. */
3639 val = BINDING_VALUE (new);
3641 switch (TREE_CODE (val))
3644 /* If we expect types or namespaces, and not templates,
3645 or this is not a template class. */
3646 if (LOOKUP_QUALIFIERS_ONLY (flags)
3647 && !DECL_CLASS_TEMPLATE_P (val))
3651 if (LOOKUP_NAMESPACES_ONLY (flags))
3654 case NAMESPACE_DECL:
3655 if (LOOKUP_TYPES_ONLY (flags))
3659 /* Ignore built-in functions that are still anticipated. */
3660 if (LOOKUP_QUALIFIERS_ONLY (flags) || DECL_ANTICIPATED (val))
3664 if (LOOKUP_QUALIFIERS_ONLY (flags))
3668 if (!BINDING_VALUE (old))
3669 BINDING_VALUE (old) = val;
3670 else if (val && val != BINDING_VALUE (old))
3672 if (is_overloaded_fn (BINDING_VALUE (old))
3673 && is_overloaded_fn (val))
3675 BINDING_VALUE (old) = merge_functions (BINDING_VALUE (old),
3680 /* Some declarations are functions, some are not. */
3681 if (flags & LOOKUP_COMPLAIN)
3683 /* If we've already given this error for this lookup,
3684 BINDING_VALUE (old) is error_mark_node, so let's not
3685 repeat ourselves. */
3686 if (BINDING_VALUE (old) != error_mark_node)
3688 error ("use of `%D' is ambiguous", name);
3689 cp_error_at (" first declared as `%#D' here",
3690 BINDING_VALUE (old));
3692 cp_error_at (" also declared as `%#D' here", val);
3694 BINDING_VALUE (old) = error_mark_node;
3697 /* ... and copy the type. */
3698 type = BINDING_TYPE (new);
3699 if (LOOKUP_NAMESPACES_ONLY (flags))
3701 if (!BINDING_TYPE (old))
3702 BINDING_TYPE (old) = type;
3703 else if (type && BINDING_TYPE (old) != type)
3705 if (flags & LOOKUP_COMPLAIN)
3707 error ("`%D' denotes an ambiguous type",name);
3708 cp_error_at (" first type here", BINDING_TYPE (old));
3709 cp_error_at (" other type here", type);
3715 /* Subroutine of unualified_namespace_lookup:
3716 Add the bindings of NAME in used namespaces to VAL.
3717 We are currently looking for names in namespace SCOPE, so we
3718 look through USINGS for using-directives of namespaces
3719 which have SCOPE as a common ancestor with the current scope.
3720 Returns false on errors. */
3723 lookup_using_namespace (tree name, tree val, tree usings, tree scope,
3724 int flags, tree *spacesp)
3728 /* Iterate over all used namespaces in current, searching for using
3729 directives of scope. */
3730 for (iter = usings; iter; iter = TREE_CHAIN (iter))
3731 if (TREE_VALUE (iter) == scope)
3734 *spacesp = tree_cons (TREE_PURPOSE (iter), NULL_TREE,
3736 val1 = binding_for_name (name, TREE_PURPOSE (iter));
3737 /* Resolve ambiguities. */
3738 val = ambiguous_decl (name, val, val1, flags);
3740 return BINDING_VALUE (val) != error_mark_node;
3744 Accepts the NAME to lookup and its qualifying SCOPE.
3745 Returns the name/type pair found into the CPLUS_BINDING RESULT,
3746 or false on error. */
3749 qualified_lookup_using_namespace (tree name, tree scope, tree result,
3752 /* Maintain a list of namespaces visited... */
3753 tree seen = NULL_TREE;
3754 /* ... and a list of namespace yet to see. */
3755 tree todo = NULL_TREE;
3757 /* Look through namespace aliases. */
3758 scope = ORIGINAL_NAMESPACE (scope);
3759 while (scope && (result != error_mark_node))
3761 seen = tree_cons (scope, NULL_TREE, seen);
3762 result = ambiguous_decl (name, result,
3763 binding_for_name (name, scope), flags);
3764 if (!BINDING_VALUE (result) && !BINDING_TYPE (result))
3765 /* Consider using directives. */
3766 for (usings = DECL_NAMESPACE_USING (scope); usings;
3767 usings = TREE_CHAIN (usings))
3768 /* If this was a real directive, and we have not seen it. */
3769 if (!TREE_INDIRECT_USING (usings)
3770 && !purpose_member (TREE_PURPOSE (usings), seen))
3771 todo = tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo);
3774 scope = TREE_PURPOSE (todo);
3775 todo = TREE_CHAIN (todo);
3778 scope = NULL_TREE; /* If there never was a todo list. */
3780 return result != error_mark_node;
3783 /* [namespace.memdef]/2 */
3785 /* Set the context of a declaration to scope. Complain if we are not
3789 set_decl_namespace (tree decl, tree scope, bool friendp)
3793 /* Get rid of namespace aliases. */
3794 scope = ORIGINAL_NAMESPACE (scope);
3796 /* It is ok for friends to be qualified in parallel space. */
3797 if (!friendp && !is_namespace_ancestor (current_namespace, scope))
3798 error ("declaration of `%D' not in a namespace surrounding `%D'",
3800 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3801 if (scope != current_namespace)
3803 /* See whether this has been declared in the namespace. */
3804 old = namespace_binding (DECL_NAME (decl), scope);
3806 /* No old declaration at all. */
3808 /* A template can be explicitly specialized in any namespace. */
3809 if (processing_explicit_instantiation)
3811 if (!is_overloaded_fn (decl))
3812 /* Don't compare non-function decls with decls_match here,
3813 since it can't check for the correct constness at this
3814 point. pushdecl will find those errors later. */
3816 /* Since decl is a function, old should contain a function decl. */
3817 if (!is_overloaded_fn (old))
3819 if (processing_template_decl || processing_specialization)
3820 /* We have not yet called push_template_decl to turn a
3821 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations
3822 won't match. But, we'll check later, when we construct the
3825 if (is_overloaded_fn (old))
3827 for (; old; old = OVL_NEXT (old))
3828 if (decls_match (decl, OVL_CURRENT (old)))
3832 if (decls_match (decl, old))
3838 error ("`%D' should have been declared inside `%D'",
3842 /* Compute the namespace where a declaration is defined. */
3845 decl_namespace (tree decl)
3848 decl = TYPE_STUB_DECL (decl);
3849 while (DECL_CONTEXT (decl))
3851 decl = DECL_CONTEXT (decl);
3852 if (TREE_CODE (decl) == NAMESPACE_DECL)
3855 decl = TYPE_STUB_DECL (decl);
3856 my_friendly_assert (DECL_P (decl), 390);
3859 return global_namespace;
3862 /* Return the namespace where the current declaration is declared. */
3865 current_decl_namespace (void)
3868 /* If we have been pushed into a different namespace, use it. */
3869 if (decl_namespace_list)
3870 return TREE_PURPOSE (decl_namespace_list);
3872 if (current_class_type)
3873 result = decl_namespace (TYPE_STUB_DECL (current_class_type));
3874 else if (current_function_decl)
3875 result = decl_namespace (current_function_decl);
3877 result = current_namespace;
3881 /* Temporarily set the namespace for the current declaration. */
3884 push_decl_namespace (tree decl)
3886 if (TREE_CODE (decl) != NAMESPACE_DECL)
3887 decl = decl_namespace (decl);
3888 decl_namespace_list = tree_cons (ORIGINAL_NAMESPACE (decl),
3889 NULL_TREE, decl_namespace_list);
3893 pop_decl_namespace (void)
3895 decl_namespace_list = TREE_CHAIN (decl_namespace_list);
3898 /* Enter a class or namespace scope. */
3903 if (TREE_CODE (t) == NAMESPACE_DECL)
3904 push_decl_namespace (t);
3905 else if CLASS_TYPE_P (t)
3906 push_nested_class (t, 2);
3909 /* Leave scope pushed by push_scope. */
3914 if (TREE_CODE (t) == NAMESPACE_DECL)
3915 pop_decl_namespace ();
3916 else if CLASS_TYPE_P (t)
3917 pop_nested_class ();
3920 /* [basic.lookup.koenig] */
3921 /* A nonzero return value in the functions below indicates an error. */
3931 static bool arg_assoc (struct arg_lookup*, tree);
3932 static bool arg_assoc_args (struct arg_lookup*, tree);
3933 static bool arg_assoc_type (struct arg_lookup*, tree);
3934 static bool add_function (struct arg_lookup *, tree);
3935 static bool arg_assoc_namespace (struct arg_lookup *, tree);
3936 static bool arg_assoc_class (struct arg_lookup *, tree);
3937 static bool arg_assoc_template_arg (struct arg_lookup*, tree);
3939 /* Add a function to the lookup structure.
3940 Returns true on error. */
3943 add_function (struct arg_lookup *k, tree fn)
3945 /* We used to check here to see if the function was already in the list,
3946 but that's O(n^2), which is just too expensive for function lookup.
3947 Now we deal with the occasional duplicate in joust. In doing this, we
3948 assume that the number of duplicates will be small compared to the
3949 total number of functions being compared, which should usually be the
3952 /* We must find only functions, or exactly one non-function. */
3955 else if (is_overloaded_fn (k->functions) && is_overloaded_fn (fn))
3956 k->functions = build_overload (fn, k->functions);
3959 tree f1 = OVL_CURRENT (k->functions);
3961 if (is_overloaded_fn (f1))
3963 fn = f1; f1 = f2; f2 = fn;
3965 cp_error_at ("`%D' is not a function,", f1);
3966 cp_error_at (" conflict with `%D'", f2);
3967 error (" in call to `%D'", k->name);
3974 /* Add functions of a namespace to the lookup structure.
3975 Returns true on error. */
3978 arg_assoc_namespace (struct arg_lookup *k, tree scope)
3982 if (purpose_member (scope, k->namespaces))
3984 k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces);
3986 value = namespace_binding (k->name, scope);
3990 for (; value; value = OVL_NEXT (value))
3991 if (add_function (k, OVL_CURRENT (value)))
3997 /* Adds everything associated with a template argument to the lookup
3998 structure. Returns true on error. */
4001 arg_assoc_template_arg (struct arg_lookup *k, tree arg)
4003 /* [basic.lookup.koenig]
4005 If T is a template-id, its associated namespaces and classes are
4006 ... the namespaces and classes associated with the types of the
4007 template arguments provided for template type parameters
4008 (excluding template template parameters); the namespaces in which
4009 any template template arguments are defined; and the classes in
4010 which any member templates used as template template arguments
4011 are defined. [Note: non-type template arguments do not
4012 contribute to the set of associated namespaces. ] */
4014 /* Consider first template template arguments. */
4015 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
4016 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
4018 else if (TREE_CODE (arg) == TEMPLATE_DECL)
4020 tree ctx = CP_DECL_CONTEXT (arg);
4022 /* It's not a member template. */
4023 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4024 return arg_assoc_namespace (k, ctx);
4025 /* Otherwise, it must be member template. */
4027 return arg_assoc_class (k, ctx);
4029 /* It's not a template template argument, but it is a type template
4031 else if (TYPE_P (arg))
4032 return arg_assoc_type (k, arg);
4033 /* It's a non-type template argument. */
4038 /* Adds everything associated with class to the lookup structure.
4039 Returns true on error. */
4042 arg_assoc_class (struct arg_lookup *k, tree type)
4044 tree list, friends, context;
4047 /* Backend build structures, such as __builtin_va_list, aren't
4048 affected by all this. */
4049 if (!CLASS_TYPE_P (type))
4052 if (purpose_member (type, k->classes))
4054 k->classes = tree_cons (type, NULL_TREE, k->classes);
4056 context = decl_namespace (TYPE_MAIN_DECL (type));
4057 if (arg_assoc_namespace (k, context))
4060 /* Process baseclasses. */
4061 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); i++)
4062 if (arg_assoc_class (k, TYPE_BINFO_BASETYPE (type, i)))
4065 /* Process friends. */
4066 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
4067 list = TREE_CHAIN (list))
4068 if (k->name == TREE_PURPOSE (list))
4069 for (friends = TREE_VALUE (list); friends;
4070 friends = TREE_CHAIN (friends))
4071 /* Only interested in global functions with potentially hidden
4072 (i.e. unqualified) declarations. */
4073 if (TREE_PURPOSE (friends) == error_mark_node && TREE_VALUE (friends)
4074 && decl_namespace (TREE_VALUE (friends)) == context)
4075 if (add_function (k, TREE_VALUE (friends)))
4078 /* Process template arguments. */
4079 if (CLASSTYPE_TEMPLATE_INFO (type))
4081 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
4082 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
4083 arg_assoc_template_arg (k, TREE_VEC_ELT (list, i));
4089 /* Adds everything associated with a given type.
4090 Returns 1 on error. */
4093 arg_assoc_type (struct arg_lookup *k, tree type)
4095 switch (TREE_CODE (type))
4106 if (TYPE_PTRMEMFUNC_P (type))
4107 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
4108 return arg_assoc_class (k, type);
4110 case REFERENCE_TYPE:
4112 return arg_assoc_type (k, TREE_TYPE (type));
4115 return arg_assoc_namespace (k, decl_namespace (TYPE_MAIN_DECL (type)));
4117 /* Pointer to member: associate class type and value type. */
4118 if (arg_assoc_type (k, TYPE_OFFSET_BASETYPE (type)))
4120 return arg_assoc_type (k, TREE_TYPE (type));
4122 /* The basetype is referenced in the first arg type, so just
4125 /* Associate the parameter types. */
4126 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
4128 /* Associate the return type. */
4129 return arg_assoc_type (k, TREE_TYPE (type));
4130 case TEMPLATE_TYPE_PARM:
4131 case BOUND_TEMPLATE_TEMPLATE_PARM:
4136 if (type == unknown_type_node)
4138 /* else fall through */
4145 /* Adds everything associated with arguments. Returns true on error. */
4148 arg_assoc_args (struct arg_lookup *k, tree args)
4150 for (; args; args = TREE_CHAIN (args))
4151 if (arg_assoc (k, TREE_VALUE (args)))
4156 /* Adds everything associated with a given tree_node. Returns 1 on error. */
4159 arg_assoc (struct arg_lookup *k, tree n)
4161 if (n == error_mark_node)
4165 return arg_assoc_type (k, n);
4167 if (! type_unknown_p (n))
4168 return arg_assoc_type (k, TREE_TYPE (n));
4170 if (TREE_CODE (n) == ADDR_EXPR)
4171 n = TREE_OPERAND (n, 0);
4172 if (TREE_CODE (n) == COMPONENT_REF)
4173 n = TREE_OPERAND (n, 1);
4174 if (TREE_CODE (n) == OFFSET_REF)
4175 n = TREE_OPERAND (n, 1);
4176 while (TREE_CODE (n) == TREE_LIST)
4178 if (TREE_CODE (n) == BASELINK)
4179 n = BASELINK_FUNCTIONS (n);
4181 if (TREE_CODE (n) == FUNCTION_DECL)
4182 return arg_assoc_type (k, TREE_TYPE (n));
4183 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
4185 /* [basic.lookup.koenig]
4187 If T is a template-id, its associated namespaces and classes
4188 are the namespace in which the template is defined; for
4189 member templates, the member template's class... */
4190 tree template = TREE_OPERAND (n, 0);
4191 tree args = TREE_OPERAND (n, 1);
4195 if (TREE_CODE (template) == COMPONENT_REF)
4196 template = TREE_OPERAND (template, 1);
4198 /* First, the template. There may actually be more than one if
4199 this is an overloaded function template. But, in that case,
4200 we only need the first; all the functions will be in the same
4202 template = OVL_CURRENT (template);
4204 ctx = CP_DECL_CONTEXT (template);
4206 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4208 if (arg_assoc_namespace (k, ctx) == 1)
4211 /* It must be a member template. */
4212 else if (arg_assoc_class (k, ctx) == 1)
4215 /* Now the arguments. */
4216 for (arg = args; arg != NULL_TREE; arg = TREE_CHAIN (arg))
4217 if (arg_assoc_template_arg (k, TREE_VALUE (arg)) == 1)
4222 my_friendly_assert (TREE_CODE (n) == OVERLOAD, 980715);
4224 for (; n; n = OVL_CHAIN (n))
4225 if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
4232 /* Performs Koenig lookup depending on arguments, where fns
4233 are the functions found in normal lookup. */
4236 lookup_arg_dependent (tree name, tree fns, tree args)
4238 struct arg_lookup k;
4239 tree fn = NULL_TREE;
4243 k.classes = NULL_TREE;
4245 /* Note that we've already looked at some namespaces during normal
4246 unqualified lookup, unless we found a decl in function scope. */
4248 fn = OVL_CURRENT (fns);
4249 if (fn && TREE_CODE (fn) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (fn))
4250 k.namespaces = NULL_TREE;
4252 unqualified_namespace_lookup (name, 0, &k.namespaces);
4254 arg_assoc_args (&k, args);
4258 /* Process a namespace-alias declaration. */
4261 do_namespace_alias (tree alias, tree namespace)
4263 if (TREE_CODE (namespace) != NAMESPACE_DECL)
4265 /* The parser did not find it, so it's not there. */
4266 error ("unknown namespace `%D'", namespace);
4270 namespace = ORIGINAL_NAMESPACE (namespace);
4272 /* Build the alias. */
4273 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
4274 DECL_NAMESPACE_ALIAS (alias) = namespace;
4275 DECL_EXTERNAL (alias) = 1;
4279 /* Check a non-member using-declaration. Return the name and scope
4280 being used, and the USING_DECL, or NULL_TREE on failure. */
4283 validate_nonmember_using_decl (tree decl, tree *scope, tree *name)
4285 *scope = global_namespace;
4288 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
4290 *name = TREE_OPERAND (decl, 0);
4292 A using-declaration shall not name a template-id. */
4293 error ("a using-declaration cannot specify a template-id. Try `using %D'", *name);
4297 if (TREE_CODE (decl) == NAMESPACE_DECL)
4299 error ("namespace `%D' not allowed in using-declaration", decl);
4303 if (is_overloaded_fn (decl))
4304 decl = get_first_fn (decl);
4306 my_friendly_assert (DECL_P (decl), 20020908);
4308 if (TREE_CODE (decl) == CONST_DECL)
4309 /* Enumeration constants to not have DECL_CONTEXT set. */
4310 *scope = TYPE_CONTEXT (TREE_TYPE (decl));
4312 *scope = DECL_CONTEXT (decl);
4314 *scope = global_namespace;
4316 /* [namespace.udecl]
4317 A using-declaration for a class member shall be a
4318 member-declaration. */
4319 if (TYPE_P (*scope))
4321 error ("`%T' is not a namespace", *scope);
4324 *name = DECL_NAME (decl);
4325 /* Make a USING_DECL. */
4326 return push_using_decl (*scope, *name);
4329 /* Process local and global using-declarations. */
4332 do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
4333 tree *newval, tree *newtype)
4337 *newval = *newtype = NULL_TREE;
4338 decls = make_node (CPLUS_BINDING);
4339 if (!qualified_lookup_using_namespace (name, scope, decls, 0))
4343 if (!BINDING_VALUE (decls) && !BINDING_TYPE (decls))
4345 error ("`%D' not declared", name);
4349 /* Check for using functions. */
4350 if (BINDING_VALUE (decls) && is_overloaded_fn (BINDING_VALUE (decls)))
4354 if (oldval && !is_overloaded_fn (oldval))
4356 duplicate_decls (OVL_CURRENT (BINDING_VALUE (decls)), oldval);
4361 for (tmp = BINDING_VALUE (decls); tmp; tmp = OVL_NEXT (tmp))
4363 tree new_fn = OVL_CURRENT (tmp);
4365 /* [namespace.udecl]
4367 If a function declaration in namespace scope or block
4368 scope has the same name and the same parameter types as a
4369 function introduced by a using declaration the program is
4371 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
4373 tree old_fn = OVL_CURRENT (tmp1);
4375 if (new_fn == old_fn)
4376 /* The function already exists in the current namespace. */
4378 else if (OVL_USED (tmp1))
4379 continue; /* this is a using decl */
4380 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
4381 TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
4383 /* If this using declaration introduces a function
4384 recognized as a built-in, no longer mark it as
4385 anticipated in this scope. */
4386 if (DECL_ANTICIPATED (old_fn))
4388 DECL_ANTICIPATED (old_fn) = 0;
4392 /* There was already a non-using declaration in
4393 this scope with the same parameter types. If both
4394 are the same extern "C" functions, that's ok. */
4395 if (!decls_match (new_fn, old_fn))
4396 error ("`%D' is already declared in this scope", name);
4401 /* If we broke out of the loop, there's no reason to add
4402 this function to the using declarations for this
4407 *newval = build_overload (OVL_CURRENT (tmp), *newval);
4408 if (TREE_CODE (*newval) != OVERLOAD)
4409 *newval = ovl_cons (*newval, NULL_TREE);
4410 OVL_USED (*newval) = 1;
4415 *newval = BINDING_VALUE (decls);
4417 duplicate_decls (*newval, oldval);
4420 *newtype = BINDING_TYPE (decls);
4421 if (oldtype && *newtype && oldtype != *newtype)
4423 error ("using declaration `%D' introduced ambiguous type `%T'",
4429 /* Process a using-declaration not appearing in class or local scope. */
4432 do_toplevel_using_decl (tree decl)
4434 tree scope, name, binding;
4435 tree oldval, oldtype, newval, newtype;
4437 decl = validate_nonmember_using_decl (decl, &scope, &name);
4438 if (decl == NULL_TREE)
4441 binding = binding_for_name (name, current_namespace);
4443 oldval = BINDING_VALUE (binding);
4444 oldtype = BINDING_TYPE (binding);
4446 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
4448 /* Copy declarations found. */
4450 BINDING_VALUE (binding) = newval;
4452 BINDING_TYPE (binding) = newtype;
4456 /* Process a using-declaration at function scope. */
4459 do_local_using_decl (tree decl)
4462 tree oldval, oldtype, newval, newtype;
4464 decl = validate_nonmember_using_decl (decl, &scope, &name);
4465 if (decl == NULL_TREE)
4468 if (building_stmt_tree ()
4469 && at_function_scope_p ())
4470 add_decl_stmt (decl);
4472 oldval = lookup_name_current_level (name);
4473 oldtype = lookup_type_current_level (name);
4475 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
4479 if (is_overloaded_fn (newval))
4483 /* We only need to push declarations for those functions
4484 that were not already bound in the current level.
4485 The old value might be NULL_TREE, it might be a single
4486 function, or an OVERLOAD. */
4487 if (oldval && TREE_CODE (oldval) == OVERLOAD)
4488 term = OVL_FUNCTION (oldval);
4491 for (fn = newval; fn && OVL_CURRENT (fn) != term;
4493 push_overloaded_decl (OVL_CURRENT (fn),
4494 PUSH_LOCAL | PUSH_USING);
4497 push_local_binding (name, newval, PUSH_USING);
4500 set_identifier_type_value (name, newtype);
4504 do_class_using_decl (tree decl)
4508 if (TREE_CODE (decl) != SCOPE_REF
4509 || !TYPE_P (TREE_OPERAND (decl, 0)))
4511 error ("using-declaration for non-member at class scope");
4514 name = TREE_OPERAND (decl, 1);
4515 if (TREE_CODE (name) == BIT_NOT_EXPR)
4517 error ("using-declaration for destructor");
4520 else if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4522 name = TREE_OPERAND (name, 0);
4523 error ("a using-declaration cannot specify a template-id. Try `using %T::%D'", TREE_OPERAND (decl, 0), name);
4526 if (TREE_CODE (name) == TYPE_DECL)
4528 tree type = TREE_TYPE (name);
4529 if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (name)))
4531 name = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type));
4532 error ("a using-declaration cannot specify a template-id.");
4535 name = DECL_NAME (name);
4537 else if (TREE_CODE (name) == TEMPLATE_DECL)
4538 name = DECL_NAME (name);
4539 else if (BASELINK_P (name))
4543 fns = BASELINK_FUNCTIONS (name);
4544 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
4546 fns = TREE_OPERAND (fns, 0);
4547 error ("a using-declaration cannot specify a template-id. Try `using %T::%D'",
4548 BASELINK_ACCESS_BINFO (name),
4549 DECL_NAME (get_first_fn (fns)));
4551 name = DECL_NAME (get_first_fn (fns));
4554 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 980716);
4556 value = build_lang_decl (USING_DECL, name, void_type_node);
4557 DECL_INITIAL (value) = TREE_OPERAND (decl, 0);
4561 /* Process a using-directive. */
4564 do_using_directive (tree namespace)
4566 if (building_stmt_tree ())
4567 add_stmt (build_stmt (USING_STMT, namespace));
4569 /* using namespace A::B::C; */
4570 if (TREE_CODE (namespace) == SCOPE_REF)
4571 namespace = TREE_OPERAND (namespace, 1);
4572 if (TREE_CODE (namespace) == IDENTIFIER_NODE)
4574 /* Lookup in lexer did not find a namespace. */
4575 if (!processing_template_decl)
4576 error ("namespace `%T' undeclared", namespace);
4579 if (TREE_CODE (namespace) != NAMESPACE_DECL)
4581 if (!processing_template_decl)
4582 error ("`%T' is not a namespace", namespace);
4585 namespace = ORIGINAL_NAMESPACE (namespace);
4586 if (!toplevel_bindings_p ())
4587 push_using_directive (namespace);
4590 add_using_namespace (current_namespace, namespace, 0);
4594 check_default_args (tree x)
4596 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4597 bool saw_def = false;
4598 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4599 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4601 if (TREE_PURPOSE (arg))
4605 cp_error_at ("default argument missing for parameter %P of `%+#D'",
4613 mark_used (tree decl)
4615 TREE_USED (decl) = 1;
4616 if (processing_template_decl)
4619 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
4620 && !TREE_ASM_WRITTEN (decl))
4621 /* Remember it, so we can check it was defined. */
4624 if (!skip_evaluation)
4625 assemble_external (decl);
4627 /* Is it a synthesized method that needs to be synthesized? */
4628 if (TREE_CODE (decl) == FUNCTION_DECL
4629 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
4630 && DECL_ARTIFICIAL (decl)
4631 && !DECL_THUNK_P (decl)
4632 && ! DECL_INITIAL (decl)
4633 /* Kludge: don't synthesize for default args. */
4634 && current_function_decl)
4636 synthesize_method (decl);
4637 /* If we've already synthesized the method we don't need to
4638 instantiate it, so we can return right away. */
4642 /* If this is a function or variable that is an instance of some
4643 template, we now know that we will need to actually do the
4644 instantiation. We check that DECL is not an explicit
4645 instantiation because that is not checked in instantiate_decl. */
4646 if ((DECL_NON_THUNK_FUNCTION_P (decl) || TREE_CODE (decl) == VAR_DECL)
4647 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
4648 && (!DECL_EXPLICIT_INSTANTIATION (decl)
4649 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_INLINE (decl))))
4650 instantiate_decl (decl, /*defer_ok=*/1);
4653 /* Helper function for class_head_decl and class_head_defn
4654 nonterminals. AGGR is the class, union or struct tag. SCOPE is the
4655 explicit scope used (NULL for no scope resolution). ID is the
4656 name. DEFN_P is true, if this is a definition of the class and
4657 NEW_TYPE_P is set to nonzero, if we push into the scope containing
4658 the to be defined aggregate.
4660 Return a TYPE_DECL for the type declared by ID in SCOPE. */
4663 handle_class_head (enum tag_types tag_kind, tree scope, tree id,
4664 tree attributes, bool defn_p, bool *new_type_p)
4666 tree decl = NULL_TREE;
4667 tree current = current_scope ();
4668 bool xrefd_p = false;
4670 if (current == NULL_TREE)
4671 current = current_namespace;
4673 *new_type_p = false;
4677 if (TREE_CODE (id) == TYPE_DECL)
4678 /* We must bash typedefs back to the main decl of the
4679 type. Otherwise we become confused about scopes. */
4680 decl = TYPE_MAIN_DECL (TREE_TYPE (id));
4681 else if (DECL_CLASS_TEMPLATE_P (id))
4682 decl = DECL_TEMPLATE_RESULT (id);
4687 /* According to the suggested resolution of core issue
4688 180, 'typename' is assumed after a class-key. */
4689 decl = make_typename_type (scope, id, tf_error);
4690 if (decl != error_mark_node)
4691 decl = TYPE_MAIN_DECL (decl);
4695 else if (scope == current)
4697 /* We've been given AGGR SCOPE::ID, when we're already
4698 inside SCOPE. Be nice about it. */
4700 pedwarn ("extra qualification `%T::' on member `%D' ignored",
4704 error ("`%T' does not have a class or union named `%D'",
4711 decl = TYPE_MAIN_DECL (xref_tag (tag_kind, id, attributes, !defn_p));
4715 if (!TYPE_BINFO (TREE_TYPE (decl)))
4717 error ("`%T' is not a class or union type", decl);
4718 return error_mark_node;
4723 /* For a definition, we want to enter the containing scope
4724 before looking up any base classes etc. Only do so, if this
4725 is different to the current scope. */
4726 tree context = CP_DECL_CONTEXT (decl);
4728 *new_type_p = (current != context
4729 && TREE_CODE (context) != TEMPLATE_TYPE_PARM
4730 && TREE_CODE (context) != BOUND_TEMPLATE_TEMPLATE_PARM);
4732 push_scope (context);
4735 && PROCESSING_REAL_TEMPLATE_DECL_P ()
4736 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
4737 decl = push_template_decl (decl);
4743 #include "gt-cp-decl2.h"