config/sparc/sol2-bi.h: Revert previous delta.
[official-gcc.git] / gcc / cp / decl2.c
blob0f8617c141f26454016786944f0b9ea552e03063
1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
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. */
31 #include "config.h"
32 #include "system.h"
33 #include "coretypes.h"
34 #include "tm.h"
35 #include "tree.h"
36 #include "rtl.h"
37 #include "expr.h"
38 #include "flags.h"
39 #include "cp-tree.h"
40 #include "decl.h"
41 #include "lex.h"
42 #include "output.h"
43 #include "except.h"
44 #include "toplev.h"
45 #include "timevar.h"
46 #include "cpplib.h"
47 #include "target.h"
48 #include "c-common.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. */
59 int destructions_p;
60 } *priority_info;
62 static void mark_vtable_entries (tree);
63 static void grok_function_init (tree, tree);
64 static bool maybe_emit_vtables (tree);
65 static void add_using_namespace (tree, tree, bool);
66 static cxx_binding *ambiguous_decl (tree, cxx_binding *, cxx_binding *, int);
67 static tree build_anon_union_vars (tree);
68 static bool acceptable_java_type (tree);
69 static void output_vtable_inherit (tree);
70 static tree start_objects (int, int);
71 static void finish_objects (int, int, tree);
72 static tree merge_functions (tree, tree);
73 static tree decl_namespace (tree);
74 static tree validate_nonmember_using_decl (tree, tree *, tree *);
75 static void do_nonmember_using_decl (tree, tree, tree, tree, tree *, tree *);
76 static tree start_static_storage_duration_function (void);
77 static void finish_static_storage_duration_function (tree);
78 static priority_info get_priority_info (int);
79 static void do_static_initialization (tree, tree);
80 static void do_static_destruction (tree);
81 static tree start_static_initialization_or_destruction (tree, int);
82 static void finish_static_initialization_or_destruction (tree);
83 static void generate_ctor_or_dtor_function (bool, int);
84 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
85 void *);
86 static tree prune_vars_needing_no_initialization (tree *);
87 static void write_out_vars (tree);
88 static void import_export_class (tree);
89 static tree get_guard_bits (tree);
91 /* A list of static class variables. This is needed, because a
92 static class variable can be declared inside the class without
93 an initializer, and then initialized, statically, outside the class. */
94 static GTY(()) varray_type pending_statics;
95 #define pending_statics_used \
96 (pending_statics ? pending_statics->elements_used : 0)
98 /* A list of functions which were declared inline, but which we
99 may need to emit outline anyway. */
100 static GTY(()) varray_type deferred_fns;
101 #define deferred_fns_used \
102 (deferred_fns ? deferred_fns->elements_used : 0)
104 /* Flag used when debugging spew.c */
106 extern int spew_debug;
108 /* Nonzero if we're done parsing and into end-of-file activities. */
110 int at_eof;
112 /* Functions called along with real static constructors and destructors. */
114 tree static_ctors;
115 tree static_dtors;
117 /* The :: namespace. */
119 tree global_namespace;
121 /* Incorporate `const' and `volatile' qualifiers for member functions.
122 FUNCTION is a TYPE_DECL or a FUNCTION_DECL.
123 QUALS is a list of qualifiers. Returns any explicit
124 top-level qualifiers of the method's this pointer, anything other than
125 TYPE_UNQUALIFIED will be an extension. */
128 grok_method_quals (tree ctype, tree function, tree quals)
130 tree fntype = TREE_TYPE (function);
131 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
132 int type_quals = TYPE_UNQUALIFIED;
133 int dup_quals = TYPE_UNQUALIFIED;
134 int this_quals = TYPE_UNQUALIFIED;
138 int tq = cp_type_qual_from_rid (TREE_VALUE (quals));
140 if ((type_quals | this_quals) & tq)
141 dup_quals |= tq;
142 else if (tq & TYPE_QUAL_RESTRICT)
143 this_quals |= tq;
144 else
145 type_quals |= tq;
146 quals = TREE_CHAIN (quals);
148 while (quals);
150 if (dup_quals != TYPE_UNQUALIFIED)
151 error ("duplicate type qualifiers in %s declaration",
152 TREE_CODE (function) == FUNCTION_DECL
153 ? "member function" : "type");
155 ctype = cp_build_qualified_type (ctype, type_quals);
156 fntype = build_cplus_method_type (ctype, TREE_TYPE (fntype),
157 (TREE_CODE (fntype) == METHOD_TYPE
158 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
159 : TYPE_ARG_TYPES (fntype)));
160 if (raises)
161 fntype = build_exception_variant (fntype, raises);
163 TREE_TYPE (function) = fntype;
164 return this_quals;
167 /* Warn when -fexternal-templates is used and #pragma
168 interface/implementation is not used all the times it should be,
169 inform the user. */
171 void
172 warn_if_unknown_interface (tree decl)
174 static int already_warned = 0;
175 if (already_warned++)
176 return;
178 if (flag_alt_external_templates)
180 tree til = tinst_for_decl ();
181 int sl = lineno;
182 const char *sf = input_filename;
184 if (til)
186 lineno = TINST_LINE (til);
187 input_filename = TINST_FILE (til);
189 warning ("template `%#D' instantiated in file without #pragma interface",
190 decl);
191 lineno = sl;
192 input_filename = sf;
194 else
195 cp_warning_at ("template `%#D' defined in file without #pragma interface",
196 decl);
199 /* A subroutine of the parser, to handle a component list. */
201 void
202 grok_x_components (tree specs)
204 tree t;
206 specs = strip_attrs (specs);
208 check_tag_decl (specs);
209 t = groktypename (build_tree_list (specs, NULL_TREE));
211 /* The only case where we need to do anything additional here is an
212 anonymous union field, e.g.: `struct S { union { int i; }; };'. */
213 if (t == NULL_TREE || !ANON_AGGR_TYPE_P (t))
214 return;
216 fixup_anonymous_aggr (t);
217 finish_member_declaration (build_decl (FIELD_DECL, NULL_TREE, t));
220 /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
221 appropriately. */
223 tree
224 cp_build_parm_decl (tree name, tree type)
226 tree parm = build_decl (PARM_DECL, name, type);
227 DECL_ARG_TYPE (parm) = type_passed_as (type);
228 return parm;
231 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
232 indicated NAME. */
234 tree
235 build_artificial_parm (tree name, tree type)
237 tree parm = cp_build_parm_decl (name, type);
238 DECL_ARTIFICIAL (parm) = 1;
239 /* All our artificial parms are implicitly `const'; they cannot be
240 assigned to. */
241 TREE_READONLY (parm) = 1;
242 return parm;
245 /* Constructors for types with virtual baseclasses need an "in-charge" flag
246 saying whether this constructor is responsible for initialization of
247 virtual baseclasses or not. All destructors also need this "in-charge"
248 flag, which additionally determines whether or not the destructor should
249 free the memory for the object.
251 This function adds the "in-charge" flag to member function FN if
252 appropriate. It is called from grokclassfn and tsubst.
253 FN must be either a constructor or destructor.
255 The in-charge flag follows the 'this' parameter, and is followed by the
256 VTT parm (if any), then the user-written parms. */
258 void
259 maybe_retrofit_in_chrg (tree fn)
261 tree basetype, arg_types, parms, parm, fntype;
263 /* If we've already add the in-charge parameter don't do it again. */
264 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
265 return;
267 /* When processing templates we can't know, in general, whether or
268 not we're going to have virtual baseclasses. */
269 if (uses_template_parms (fn))
270 return;
272 /* We don't need an in-charge parameter for constructors that don't
273 have virtual bases. */
274 if (DECL_CONSTRUCTOR_P (fn)
275 && !TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
276 return;
278 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
279 basetype = TREE_TYPE (TREE_VALUE (arg_types));
280 arg_types = TREE_CHAIN (arg_types);
282 parms = TREE_CHAIN (DECL_ARGUMENTS (fn));
284 /* If this is a subobject constructor or destructor, our caller will
285 pass us a pointer to our VTT. */
286 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
288 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
290 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
291 TREE_CHAIN (parm) = parms;
292 parms = parm;
294 /* ...and then to TYPE_ARG_TYPES. */
295 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
297 DECL_HAS_VTT_PARM_P (fn) = 1;
300 /* Then add the in-charge parm (before the VTT parm). */
301 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
302 TREE_CHAIN (parm) = parms;
303 parms = parm;
304 arg_types = hash_tree_chain (integer_type_node, arg_types);
306 /* Insert our new parameter(s) into the list. */
307 TREE_CHAIN (DECL_ARGUMENTS (fn)) = parms;
309 /* And rebuild the function type. */
310 fntype = build_cplus_method_type (basetype, TREE_TYPE (TREE_TYPE (fn)),
311 arg_types);
312 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
313 fntype = build_exception_variant (fntype,
314 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
315 TREE_TYPE (fn) = fntype;
317 /* Now we've got the in-charge parameter. */
318 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
321 /* Classes overload their constituent function names automatically.
322 When a function name is declared in a record structure,
323 its name is changed to it overloaded name. Since names for
324 constructors and destructors can conflict, we place a leading
325 '$' for destructors.
327 CNAME is the name of the class we are grokking for.
329 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
331 FLAGS contains bits saying what's special about today's
332 arguments. 1 == DESTRUCTOR. 2 == OPERATOR.
334 If FUNCTION is a destructor, then we must add the `auto-delete' field
335 as a second parameter. There is some hair associated with the fact
336 that we must "declare" this variable in the manner consistent with the
337 way the rest of the arguments were declared.
339 QUALS are the qualifiers for the this pointer. */
341 void
342 grokclassfn (tree ctype, tree function, enum overload_flags flags, tree quals)
344 tree fn_name = DECL_NAME (function);
345 int this_quals = TYPE_UNQUALIFIED;
347 /* Even within an `extern "C"' block, members get C++ linkage. See
348 [dcl.link] for details. */
349 SET_DECL_LANGUAGE (function, lang_cplusplus);
351 if (fn_name == NULL_TREE)
353 error ("name missing for member function");
354 fn_name = get_identifier ("<anonymous>");
355 DECL_NAME (function) = fn_name;
358 if (quals)
359 this_quals = grok_method_quals (ctype, function, quals);
361 if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
363 /* Must add the class instance variable up front. */
364 /* Right now we just make this a pointer. But later
365 we may wish to make it special. */
366 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (function)));
367 tree qual_type;
368 tree parm;
370 /* The `this' parameter is implicitly `const'; it cannot be
371 assigned to. */
372 this_quals |= TYPE_QUAL_CONST;
373 qual_type = cp_build_qualified_type (type, this_quals);
374 parm = build_artificial_parm (this_identifier, qual_type);
375 c_apply_type_quals_to_decl (this_quals, parm);
376 TREE_CHAIN (parm) = last_function_parms;
377 last_function_parms = parm;
380 DECL_ARGUMENTS (function) = last_function_parms;
381 DECL_CONTEXT (function) = ctype;
383 if (flags == DTOR_FLAG)
384 DECL_DESTRUCTOR_P (function) = 1;
386 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
387 maybe_retrofit_in_chrg (function);
389 if (flags == DTOR_FLAG)
391 DECL_DESTRUCTOR_P (function) = 1;
392 TYPE_HAS_DESTRUCTOR (ctype) = 1;
396 /* Create an ARRAY_REF, checking for the user doing things backwards
397 along the way. */
399 tree
400 grok_array_decl (tree array_expr, tree index_exp)
402 tree type = TREE_TYPE (array_expr);
403 tree p1, p2, i1, i2;
405 if (type == error_mark_node || index_exp == error_mark_node)
406 return error_mark_node;
407 if (processing_template_decl)
408 return build_min (ARRAY_REF, type ? TREE_TYPE (type) : NULL_TREE,
409 array_expr, index_exp);
411 if (type == NULL_TREE)
413 /* Something has gone very wrong. Assume we are mistakenly reducing
414 an expression instead of a declaration. */
415 error ("parser may be lost: is there a '{' missing somewhere?");
416 return NULL_TREE;
419 if (TREE_CODE (type) == OFFSET_TYPE
420 || TREE_CODE (type) == REFERENCE_TYPE)
421 type = TREE_TYPE (type);
423 /* If they have an `operator[]', use that. */
424 if (IS_AGGR_TYPE (type) || IS_AGGR_TYPE (TREE_TYPE (index_exp)))
425 return build_new_op (ARRAY_REF, LOOKUP_NORMAL,
426 array_expr, index_exp, NULL_TREE);
428 /* Otherwise, create an ARRAY_REF for a pointer or array type. It
429 is a little-known fact that, if `a' is an array and `i' is an
430 int, you can write `i[a]', which means the same thing as `a[i]'. */
432 if (TREE_CODE (type) == ARRAY_TYPE)
433 p1 = array_expr;
434 else
435 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
437 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
438 p2 = index_exp;
439 else
440 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
442 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr, false);
443 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp, false);
445 if ((p1 && i2) && (i1 && p2))
446 error ("ambiguous conversion for array subscript");
448 if (p1 && i2)
449 array_expr = p1, index_exp = i2;
450 else if (i1 && p2)
451 array_expr = p2, index_exp = i1;
452 else
454 error ("invalid types `%T[%T]' for array subscript",
455 type, TREE_TYPE (index_exp));
456 return error_mark_node;
459 if (array_expr == error_mark_node || index_exp == error_mark_node)
460 error ("ambiguous conversion for array subscript");
462 return build_array_ref (array_expr, index_exp);
465 /* Given the cast expression EXP, checking out its validity. Either return
466 an error_mark_node if there was an unavoidable error, return a cast to
467 void for trying to delete a pointer w/ the value 0, or return the
468 call to delete. If DOING_VEC is 1, we handle things differently
469 for doing an array delete. If DOING_VEC is 2, they gave us the
470 array size as an argument to delete.
471 Implements ARM $5.3.4. This is called from the parser. */
473 tree
474 delete_sanity (tree exp, tree size, int doing_vec, int use_global_delete)
476 tree t, type;
477 /* For a regular vector delete (aka, no size argument) we will pass
478 this down as a NULL_TREE into build_vec_delete. */
479 tree maxindex = NULL_TREE;
481 if (exp == error_mark_node)
482 return exp;
484 if (processing_template_decl)
486 t = build_min (DELETE_EXPR, void_type_node, exp, size);
487 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
488 DELETE_EXPR_USE_VEC (t) = doing_vec;
489 return t;
492 if (TREE_CODE (exp) == OFFSET_REF)
493 exp = resolve_offset_ref (exp);
494 exp = convert_from_reference (exp);
495 t = stabilize_reference (exp);
496 t = build_expr_type_conversion (WANT_POINTER, t, true);
498 if (t == NULL_TREE || t == error_mark_node)
500 error ("type `%#T' argument given to `delete', expected pointer",
501 TREE_TYPE (exp));
502 return error_mark_node;
505 if (doing_vec == 2)
507 maxindex = cp_build_binary_op (MINUS_EXPR, size, integer_one_node);
508 pedwarn ("anachronistic use of array size in vector delete");
511 type = TREE_TYPE (t);
513 /* As of Valley Forge, you can delete a pointer to const. */
515 /* You can't delete functions. */
516 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
518 error ("cannot delete a function. Only pointer-to-objects are valid arguments to `delete'");
519 return error_mark_node;
522 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
523 if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
525 warning ("deleting `%T' is undefined", type);
526 doing_vec = 0;
529 /* An array can't have been allocated by new, so complain. */
530 if (TREE_CODE (t) == ADDR_EXPR
531 && TREE_CODE (TREE_OPERAND (t, 0)) == VAR_DECL
532 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == ARRAY_TYPE)
533 warning ("deleting array `%#D'", TREE_OPERAND (t, 0));
535 /* Deleting a pointer with the value zero is valid and has no effect. */
536 if (integer_zerop (t))
537 return build1 (NOP_EXPR, void_type_node, t);
539 if (doing_vec)
540 return build_vec_delete (t, maxindex, sfk_deleting_destructor,
541 use_global_delete);
542 else
543 return build_delete (type, t, sfk_deleting_destructor,
544 LOOKUP_NORMAL, use_global_delete);
547 /* Report an error if the indicated template declaration is not the
548 sort of thing that should be a member template. */
550 void
551 check_member_template (tree tmpl)
553 tree decl;
555 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
556 decl = DECL_TEMPLATE_RESULT (tmpl);
558 if (TREE_CODE (decl) == FUNCTION_DECL
559 || (TREE_CODE (decl) == TYPE_DECL
560 && IS_AGGR_TYPE (TREE_TYPE (decl))))
562 if (current_function_decl)
563 /* 14.5.2.2 [temp.mem]
565 A local class shall not have member templates. */
566 error ("invalid declaration of member template `%#D' in local class",
567 decl);
569 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
571 /* 14.5.2.3 [temp.mem]
573 A member function template shall not be virtual. */
574 error
575 ("invalid use of `virtual' in template declaration of `%#D'",
576 decl);
577 DECL_VIRTUAL_P (decl) = 0;
580 /* The debug-information generating code doesn't know what to do
581 with member templates. */
582 DECL_IGNORED_P (tmpl) = 1;
584 else
585 error ("template declaration of `%#D'", decl);
588 /* Return true iff TYPE is a valid Java parameter or return type. */
590 static bool
591 acceptable_java_type (tree type)
593 if (TREE_CODE (type) == VOID_TYPE || TYPE_FOR_JAVA (type))
594 return 1;
595 if (TREE_CODE (type) == POINTER_TYPE || TREE_CODE (type) == REFERENCE_TYPE)
597 type = TREE_TYPE (type);
598 if (TREE_CODE (type) == RECORD_TYPE)
600 tree args; int i;
601 if (! TYPE_FOR_JAVA (type))
602 return false;
603 if (! CLASSTYPE_TEMPLATE_INFO (type))
604 return true;
605 args = CLASSTYPE_TI_ARGS (type);
606 i = TREE_VEC_LENGTH (args);
607 while (--i >= 0)
609 type = TREE_VEC_ELT (args, i);
610 if (TREE_CODE (type) == POINTER_TYPE)
611 type = TREE_TYPE (type);
612 if (! TYPE_FOR_JAVA (type))
613 return false;
615 return true;
618 return false;
621 /* For a METHOD in a Java class CTYPE, return true if
622 the parameter and return types are valid Java types.
623 Otherwise, print appropriate error messages, and return false. */
625 bool
626 check_java_method (tree method)
628 bool jerr = false;
629 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
630 tree ret_type = TREE_TYPE (TREE_TYPE (method));
631 if (!acceptable_java_type (ret_type))
633 error ("Java method '%D' has non-Java return type `%T'",
634 method, ret_type);
635 jerr = true;
637 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
639 tree type = TREE_VALUE (arg_types);
640 if (!acceptable_java_type (type))
642 error ("Java method '%D' has non-Java parameter type `%T'",
643 method, type);
644 jerr = true;
647 return !jerr;
650 /* Sanity check: report error if this function FUNCTION is not
651 really a member of the class (CTYPE) it is supposed to belong to.
652 CNAME is the same here as it is for grokclassfn above. */
654 tree
655 check_classfn (tree ctype, tree function)
657 int ix;
658 int is_template;
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.
666 For example, in:
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. */
674 return NULL_TREE;
676 /* OK, is this a definition of a member template? */
677 is_template = (TREE_CODE (function) == TEMPLATE_DECL
678 || (processing_template_decl - template_class_depth (ctype)));
680 ix = lookup_fnfields_1 (complete_type (ctype),
681 DECL_CONSTRUCTOR_P (function) ? ctor_identifier :
682 DECL_DESTRUCTOR_P (function) ? dtor_identifier :
683 DECL_NAME (function));
685 if (ix >= 0)
687 tree methods = CLASSTYPE_METHOD_VEC (ctype);
688 tree fndecls, fndecl = 0;
689 bool is_conv_op;
690 const char *format = NULL;
692 push_scope (ctype);
693 for (fndecls = TREE_VEC_ELT (methods, ix);
694 fndecls; fndecls = OVL_NEXT (fndecls))
696 tree p1, p2;
698 fndecl = OVL_CURRENT (fndecls);
699 p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
700 p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
702 /* We cannot simply call decls_match because this doesn't
703 work for static member functions that are pretending to
704 be methods, and because the name may have been changed by
705 asm("new_name"). */
707 /* Get rid of the this parameter on functions that become
708 static. */
709 if (DECL_STATIC_FUNCTION_P (fndecl)
710 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
711 p1 = TREE_CHAIN (p1);
713 /* A member template definition only matches a member template
714 declaration. */
715 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
716 continue;
718 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
719 TREE_TYPE (TREE_TYPE (fndecl)))
720 && compparms (p1, p2)
721 && (DECL_TEMPLATE_SPECIALIZATION (function)
722 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
723 && (!DECL_TEMPLATE_SPECIALIZATION (function)
724 || (DECL_TI_TEMPLATE (function)
725 == DECL_TI_TEMPLATE (fndecl))))
726 break;
728 pop_scope (ctype);
729 if (fndecls)
730 return OVL_CURRENT (fndecls);
731 error ("prototype for `%#D' does not match any in class `%T'",
732 function, ctype);
733 is_conv_op = DECL_CONV_FN_P (fndecl);
735 if (is_conv_op)
736 ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
737 fndecls = TREE_VEC_ELT (methods, ix);
738 while (fndecls)
740 fndecl = OVL_CURRENT (fndecls);
741 fndecls = OVL_NEXT (fndecls);
743 if (!fndecls && is_conv_op)
745 if (TREE_VEC_LENGTH (methods) > ix)
747 ix++;
748 fndecls = TREE_VEC_ELT (methods, ix);
749 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
751 fndecls = NULL_TREE;
752 is_conv_op = false;
755 else
756 is_conv_op = false;
758 if (format)
759 format = " %#D";
760 else if (fndecls)
761 format = "candidates are: %#D";
762 else
763 format = "candidate is: %#D";
764 cp_error_at (format, fndecl);
767 else if (!COMPLETE_TYPE_P (ctype))
768 cxx_incomplete_type_error (function, ctype);
769 else
770 error ("no `%#D' member function declared in class `%T'",
771 function, ctype);
773 /* If we did not find the method in the class, add it to avoid
774 spurious errors (unless the CTYPE is not yet defined, in which
775 case we'll only confuse ourselves when the function is declared
776 properly within the class. */
777 if (COMPLETE_TYPE_P (ctype))
778 add_method (ctype, function, /*error_p=*/1);
779 return NULL_TREE;
782 /* We have just processed the DECL, which is a static data member.
783 Its initializer, if present, is INIT. The ASMSPEC_TREE, if
784 present, is the assembly-language name for the data member.
785 FLAGS is as for cp_finish_decl. */
787 void
788 finish_static_data_member_decl (tree decl, tree init, tree asmspec_tree,
789 int flags)
791 my_friendly_assert (TREE_PUBLIC (decl), 0);
793 DECL_CONTEXT (decl) = current_class_type;
795 /* We cannot call pushdecl here, because that would fill in the
796 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
797 the right thing, namely, to put this decl out straight away. */
798 /* current_class_type can be NULL_TREE in case of error. */
799 if (!asmspec_tree && current_class_type)
800 DECL_INITIAL (decl) = error_mark_node;
802 if (! processing_template_decl)
804 if (!pending_statics)
805 VARRAY_TREE_INIT (pending_statics, 32, "pending_statics");
806 VARRAY_PUSH_TREE (pending_statics, decl);
809 if (LOCAL_CLASS_P (current_class_type))
810 pedwarn ("local class `%#T' shall not have static data member `%#D'",
811 current_class_type, decl);
813 /* Static consts need not be initialized in the class definition. */
814 if (init != NULL_TREE && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
816 static int explained = 0;
818 error ("initializer invalid for static member with constructor");
819 if (!explained)
821 error ("(an out of class initialization is required)");
822 explained = 1;
824 init = NULL_TREE;
826 /* Force the compiler to know when an uninitialized static const
827 member is being used. */
828 if (CP_TYPE_CONST_P (TREE_TYPE (decl)) && init == 0)
829 TREE_USED (decl) = 1;
830 DECL_INITIAL (decl) = init;
831 DECL_IN_AGGR_P (decl) = 1;
833 cp_finish_decl (decl, init, asmspec_tree, flags);
836 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
837 of a structure component, returning a _DECL node.
838 QUALS is a list of type qualifiers for this decl (such as for declaring
839 const member functions).
841 This is done during the parsing of the struct declaration.
842 The _DECL nodes are chained together and the lot of them
843 are ultimately passed to `build_struct' to make the RECORD_TYPE node.
845 If class A defines that certain functions in class B are friends, then
846 the way I have set things up, it is B who is interested in permission
847 granted by A. However, it is in A's context that these declarations
848 are parsed. By returning a void_type_node, class A does not attempt
849 to incorporate the declarations of the friends within its structure.
851 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
852 CHANGES TO CODE IN `start_method'. */
854 tree
855 grokfield (tree declarator, tree declspecs, tree init, tree asmspec_tree,
856 tree attrlist)
858 tree value;
859 const char *asmspec = 0;
860 int flags = LOOKUP_ONLYCONVERTING;
862 if (declspecs == NULL_TREE
863 && TREE_CODE (declarator) == SCOPE_REF
864 && TREE_CODE (TREE_OPERAND (declarator, 1)) == IDENTIFIER_NODE)
866 /* Access declaration */
867 if (! IS_AGGR_TYPE_CODE (TREE_CODE (TREE_OPERAND (declarator, 0))))
869 else if (TREE_COMPLEXITY (declarator) == current_class_depth)
870 pop_nested_class ();
871 return do_class_using_decl (declarator);
874 if (init
875 && TREE_CODE (init) == TREE_LIST
876 && TREE_VALUE (init) == error_mark_node
877 && TREE_CHAIN (init) == NULL_TREE)
878 init = NULL_TREE;
880 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
881 if (! value || value == error_mark_node)
882 /* friend or constructor went bad. */
883 return value;
884 if (TREE_TYPE (value) == error_mark_node)
885 return error_mark_node;
887 if (TREE_CODE (value) == TYPE_DECL && init)
889 error ("typedef `%D' is initialized (use __typeof__ instead)", value);
890 init = NULL_TREE;
893 /* Pass friendly classes back. */
894 if (TREE_CODE (value) == VOID_TYPE)
895 return void_type_node;
897 if (DECL_NAME (value) != NULL_TREE
898 && IDENTIFIER_POINTER (DECL_NAME (value))[0] == '_'
899 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (value)), "_vptr"))
900 error ("member `%D' conflicts with virtual function table field name",
901 value);
903 /* Stash away type declarations. */
904 if (TREE_CODE (value) == TYPE_DECL)
906 DECL_NONLOCAL (value) = 1;
907 DECL_CONTEXT (value) = current_class_type;
909 if (CLASS_TYPE_P (TREE_TYPE (value)))
910 CLASSTYPE_GOT_SEMICOLON (TREE_TYPE (value)) = 1;
912 if (processing_template_decl)
913 value = push_template_decl (value);
915 return value;
918 if (DECL_IN_AGGR_P (value))
920 error ("`%D' is already defined in `%T'", value,
921 DECL_CONTEXT (value));
922 return void_type_node;
925 if (asmspec_tree)
926 asmspec = TREE_STRING_POINTER (asmspec_tree);
928 if (init)
930 if (TREE_CODE (value) == FUNCTION_DECL)
932 grok_function_init (value, init);
933 init = NULL_TREE;
935 else if (pedantic && TREE_CODE (value) != VAR_DECL)
936 /* Already complained in grokdeclarator. */
937 init = NULL_TREE;
938 else
940 /* We allow initializers to become parameters to base
941 initializers. */
942 if (TREE_CODE (init) == TREE_LIST)
944 if (TREE_CHAIN (init) == NULL_TREE)
945 init = TREE_VALUE (init);
946 else
947 init = digest_init (TREE_TYPE (value), init, (tree *)0);
950 if (!processing_template_decl)
952 if (TREE_CODE (init) == CONST_DECL)
953 init = DECL_INITIAL (init);
954 else if (TREE_READONLY_DECL_P (init))
955 init = decl_constant_value (init);
956 else if (TREE_CODE (init) == CONSTRUCTOR)
957 init = digest_init (TREE_TYPE (value), init, (tree *)0);
958 if (init == error_mark_node)
959 /* We must make this look different than `error_mark_node'
960 because `decl_const_value' would mis-interpret it
961 as only meaning that this VAR_DECL is defined. */
962 init = build1 (NOP_EXPR, TREE_TYPE (value), init);
963 else if (! TREE_CONSTANT (init))
965 /* We can allow references to things that are effectively
966 static, since references are initialized with the
967 address. */
968 if (TREE_CODE (TREE_TYPE (value)) != REFERENCE_TYPE
969 || (TREE_STATIC (init) == 0
970 && (!DECL_P (init) || DECL_EXTERNAL (init) == 0)))
972 error ("field initializer is not constant");
973 init = error_mark_node;
980 if (processing_template_decl && ! current_function_decl
981 && (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == FUNCTION_DECL))
982 value = push_template_decl (value);
984 if (attrlist)
985 cplus_decl_attributes (&value, attrlist, 0);
987 if (TREE_CODE (value) == VAR_DECL)
989 finish_static_data_member_decl (value, init, asmspec_tree,
990 flags);
991 return value;
993 if (TREE_CODE (value) == FIELD_DECL)
995 if (asmspec)
996 error ("`asm' specifiers are not permitted on non-static data members");
997 if (DECL_INITIAL (value) == error_mark_node)
998 init = error_mark_node;
999 cp_finish_decl (value, init, NULL_TREE, flags);
1000 DECL_INITIAL (value) = init;
1001 DECL_IN_AGGR_P (value) = 1;
1002 return value;
1004 if (TREE_CODE (value) == FUNCTION_DECL)
1006 if (asmspec)
1008 /* This must override the asm specifier which was placed
1009 by grokclassfn. Lay this out fresh. */
1010 SET_DECL_RTL (value, NULL_RTX);
1011 SET_DECL_ASSEMBLER_NAME (value, get_identifier (asmspec));
1013 if (!DECL_FRIEND_P (value))
1014 grok_special_member_properties (value);
1016 cp_finish_decl (value, init, asmspec_tree, flags);
1018 /* Pass friends back this way. */
1019 if (DECL_FRIEND_P (value))
1020 return void_type_node;
1022 DECL_IN_AGGR_P (value) = 1;
1023 return value;
1025 abort ();
1026 /* NOTREACHED */
1027 return NULL_TREE;
1030 /* Like `grokfield', but for bitfields.
1031 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1033 tree
1034 grokbitfield (tree declarator, tree declspecs, tree width)
1036 register tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1037 0, NULL);
1039 if (! value) return NULL_TREE; /* friends went bad. */
1041 /* Pass friendly classes back. */
1042 if (TREE_CODE (value) == VOID_TYPE)
1043 return void_type_node;
1045 if (TREE_CODE (value) == TYPE_DECL)
1047 error ("cannot declare `%D' to be a bit-field type", value);
1048 return NULL_TREE;
1051 /* Usually, finish_struct_1 catches bitfields with invalid types.
1052 But, in the case of bitfields with function type, we confuse
1053 ourselves into thinking they are member functions, so we must
1054 check here. */
1055 if (TREE_CODE (value) == FUNCTION_DECL)
1057 error ("cannot declare bit-field `%D' with function type",
1058 DECL_NAME (value));
1059 return NULL_TREE;
1062 if (DECL_IN_AGGR_P (value))
1064 error ("`%D' is already defined in the class %T", value,
1065 DECL_CONTEXT (value));
1066 return void_type_node;
1069 if (TREE_STATIC (value))
1071 error ("static member `%D' cannot be a bit-field", value);
1072 return NULL_TREE;
1074 cp_finish_decl (value, NULL_TREE, NULL_TREE, 0);
1076 if (width != error_mark_node)
1078 constant_expression_warning (width);
1079 DECL_INITIAL (value) = width;
1080 SET_DECL_C_BIT_FIELD (value);
1083 DECL_IN_AGGR_P (value) = 1;
1084 return value;
1087 /* When a function is declared with an initializer,
1088 do the right thing. Currently, there are two possibilities:
1090 class B
1092 public:
1093 // initialization possibility #1.
1094 virtual void f () = 0;
1095 int g ();
1098 class D1 : B
1100 public:
1101 int d1;
1102 // error, no f ();
1105 class D2 : B
1107 public:
1108 int d2;
1109 void f ();
1112 class D3 : B
1114 public:
1115 int d3;
1116 // initialization possibility #2
1117 void f () = B::f;
1122 static void
1123 grok_function_init (tree decl, tree init)
1125 /* An initializer for a function tells how this function should
1126 be inherited. */
1127 tree type = TREE_TYPE (decl);
1129 if (TREE_CODE (type) == FUNCTION_TYPE)
1130 error ("initializer specified for non-member function `%D'", decl);
1131 else if (integer_zerop (init))
1132 DECL_PURE_VIRTUAL_P (decl) = 1;
1133 else
1134 error ("invalid initializer for virtual method `%D'", decl);
1137 void
1138 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1140 if (*decl == NULL_TREE || *decl == void_type_node)
1141 return;
1143 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1144 decl = &DECL_TEMPLATE_RESULT (*decl);
1146 decl_attributes (decl, attributes, flags);
1148 if (TREE_CODE (*decl) == TYPE_DECL)
1149 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1152 /* Return the name for the constructor (or destructor) for the
1153 specified class TYPE. When given a template, this routine doesn't
1154 lose the specialization. */
1156 tree
1157 constructor_name_full (tree type)
1159 type = TYPE_MAIN_VARIANT (type);
1160 if (CLASS_TYPE_P (type) && TYPE_WAS_ANONYMOUS (type)
1161 && TYPE_HAS_CONSTRUCTOR (type))
1162 return DECL_NAME (OVL_CURRENT (CLASSTYPE_CONSTRUCTORS (type)));
1163 else
1164 return TYPE_IDENTIFIER (type);
1167 /* Return the name for the constructor (or destructor) for the
1168 specified class. When given a template, return the plain
1169 unspecialized name. */
1171 tree
1172 constructor_name (tree type)
1174 tree name;
1175 name = constructor_name_full (type);
1176 if (IDENTIFIER_TEMPLATE (name))
1177 name = IDENTIFIER_TEMPLATE (name);
1178 return name;
1181 /* Returns TRUE if NAME is the name for the constructor for TYPE. */
1183 bool
1184 constructor_name_p (tree name, tree type)
1186 return (name == constructor_name (type)
1187 || name == constructor_name_full (type));
1191 /* Defer the compilation of the FN until the end of compilation. */
1193 void
1194 defer_fn (tree fn)
1196 if (DECL_DEFERRED_FN (fn))
1197 return;
1198 DECL_DEFERRED_FN (fn) = 1;
1199 if (!deferred_fns)
1200 VARRAY_TREE_INIT (deferred_fns, 32, "deferred_fns");
1202 VARRAY_PUSH_TREE (deferred_fns, fn);
1205 /* Walks through the namespace- or function-scope anonymous union OBJECT,
1206 building appropriate ALIAS_DECLs. Returns one of the fields for use in
1207 the mangled name. */
1209 static tree
1210 build_anon_union_vars (tree object)
1212 tree type = TREE_TYPE (object);
1213 tree main_decl = NULL_TREE;
1214 tree field;
1216 /* Rather than write the code to handle the non-union case,
1217 just give an error. */
1218 if (TREE_CODE (type) != UNION_TYPE)
1219 error ("anonymous struct not inside named type");
1221 for (field = TYPE_FIELDS (type);
1222 field != NULL_TREE;
1223 field = TREE_CHAIN (field))
1225 tree decl;
1226 tree ref;
1228 if (DECL_ARTIFICIAL (field))
1229 continue;
1230 if (TREE_CODE (field) != FIELD_DECL)
1232 cp_pedwarn_at ("\
1233 `%#D' invalid; an anonymous union can only have non-static data members",
1234 field);
1235 continue;
1238 if (TREE_PRIVATE (field))
1239 cp_pedwarn_at ("private member `%#D' in anonymous union", field);
1240 else if (TREE_PROTECTED (field))
1241 cp_pedwarn_at ("protected member `%#D' in anonymous union", field);
1243 if (processing_template_decl)
1244 ref = build_min_nt (COMPONENT_REF, object, DECL_NAME (field));
1245 else
1246 ref = build_class_member_access_expr (object, field, NULL_TREE,
1247 false);
1249 if (DECL_NAME (field))
1251 decl = build_decl (ALIAS_DECL, DECL_NAME (field), TREE_TYPE (field));
1252 DECL_INITIAL (decl) = ref;
1253 TREE_PUBLIC (decl) = 0;
1254 TREE_STATIC (decl) = 0;
1255 DECL_EXTERNAL (decl) = 1;
1256 decl = pushdecl (decl);
1258 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1259 decl = build_anon_union_vars (ref);
1260 else
1261 decl = 0;
1263 if (main_decl == NULL_TREE)
1264 main_decl = decl;
1267 return main_decl;
1270 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1271 anonymous union, then all members must be laid out together. PUBLIC_P
1272 is nonzero if this union is not declared static. */
1274 void
1275 finish_anon_union (tree anon_union_decl)
1277 tree type = TREE_TYPE (anon_union_decl);
1278 tree main_decl;
1279 bool public_p = TREE_PUBLIC (anon_union_decl);
1281 /* The VAR_DECL's context is the same as the TYPE's context. */
1282 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1284 if (TYPE_FIELDS (type) == NULL_TREE)
1285 return;
1287 if (public_p)
1289 error ("namespace-scope anonymous aggregates must be static");
1290 return;
1293 main_decl = build_anon_union_vars (anon_union_decl);
1294 if (main_decl == NULL_TREE)
1296 warning ("anonymous union with no members");
1297 return;
1300 if (!processing_template_decl)
1302 /* Use main_decl to set the mangled name. */
1303 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1304 mangle_decl (anon_union_decl);
1305 DECL_NAME (anon_union_decl) = NULL_TREE;
1308 pushdecl (anon_union_decl);
1309 if (building_stmt_tree ()
1310 && at_function_scope_p ())
1311 add_decl_stmt (anon_union_decl);
1312 else if (!processing_template_decl)
1313 rest_of_decl_compilation (anon_union_decl, NULL,
1314 toplevel_bindings_p (), at_eof);
1317 /* Auxiliary functions to make type signatures for
1318 `operator new' and `operator delete' correspond to
1319 what compiler will be expecting. */
1321 tree
1322 coerce_new_type (tree type)
1324 int e = 0;
1325 tree args = TYPE_ARG_TYPES (type);
1327 my_friendly_assert (TREE_CODE (type) == FUNCTION_TYPE, 20001107);
1329 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1330 e = 1, error ("`operator new' must return type `%T'", ptr_type_node);
1332 if (!args || args == void_list_node
1333 || !same_type_p (TREE_VALUE (args), size_type_node))
1335 e = 2;
1336 if (args && args != void_list_node)
1337 args = TREE_CHAIN (args);
1338 pedwarn ("`operator new' takes type `size_t' (`%T') as first parameter", size_type_node);
1340 switch (e)
1342 case 2:
1343 args = tree_cons (NULL_TREE, size_type_node, args);
1344 /* FALLTHROUGH */
1345 case 1:
1346 type = build_exception_variant
1347 (build_function_type (ptr_type_node, args),
1348 TYPE_RAISES_EXCEPTIONS (type));
1349 /* FALLTHROUGH */
1350 default:;
1352 return type;
1355 tree
1356 coerce_delete_type (tree type)
1358 int e = 0;
1359 tree args = TYPE_ARG_TYPES (type);
1361 my_friendly_assert (TREE_CODE (type) == FUNCTION_TYPE, 20001107);
1363 if (!same_type_p (TREE_TYPE (type), void_type_node))
1364 e = 1, error ("`operator delete' must return type `%T'", void_type_node);
1366 if (!args || args == void_list_node
1367 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1369 e = 2;
1370 if (args && args != void_list_node)
1371 args = TREE_CHAIN (args);
1372 error ("`operator delete' takes type `%T' as first parameter", ptr_type_node);
1374 switch (e)
1376 case 2:
1377 args = tree_cons (NULL_TREE, ptr_type_node, args);
1378 /* FALLTHROUGH */
1379 case 1:
1380 type = build_exception_variant
1381 (build_function_type (void_type_node, args),
1382 TYPE_RAISES_EXCEPTIONS (type));
1383 /* FALLTHROUGH */
1384 default:;
1387 return type;
1390 static void
1391 mark_vtable_entries (tree decl)
1393 tree entries = CONSTRUCTOR_ELTS (DECL_INITIAL (decl));
1395 for (; entries; entries = TREE_CHAIN (entries))
1397 tree fnaddr = TREE_VALUE (entries);
1398 tree fn;
1400 if (TREE_CODE (fnaddr) != ADDR_EXPR
1401 && TREE_CODE (fnaddr) != FDESC_EXPR)
1402 /* This entry is an offset: a virtual base class offset, a
1403 virtual call offset, an RTTI offset, etc. */
1404 continue;
1406 fn = TREE_OPERAND (fnaddr, 0);
1407 TREE_ADDRESSABLE (fn) = 1;
1408 /* When we don't have vcall offsets, we output thunks whenever
1409 we output the vtables that contain them. With vcall offsets,
1410 we know all the thunks we'll need when we emit a virtual
1411 function, so we emit the thunks there instead. */
1412 if (DECL_THUNK_P (fn))
1413 use_thunk (fn, /*emit_p=*/0);
1414 mark_used (fn);
1418 /* Set DECL up to have the closest approximation of "initialized common"
1419 linkage available. */
1421 void
1422 comdat_linkage (tree decl)
1424 if (flag_weak)
1425 make_decl_one_only (decl);
1426 else if (TREE_CODE (decl) == FUNCTION_DECL
1427 || (TREE_CODE (decl) == VAR_DECL && DECL_ARTIFICIAL (decl)))
1428 /* We can just emit function and compiler-generated variables
1429 statically; having multiple copies is (for the most part) only
1430 a waste of space.
1432 There are two correctness issues, however: the address of a
1433 template instantiation with external linkage should be the
1434 same, independent of what translation unit asks for the
1435 address, and this will not hold when we emit multiple copies of
1436 the function. However, there's little else we can do.
1438 Also, by default, the typeinfo implementation assumes that
1439 there will be only one copy of the string used as the name for
1440 each type. Therefore, if weak symbols are unavailable, the
1441 run-time library should perform a more conservative check; it
1442 should perform a string comparison, rather than an address
1443 comparison. */
1444 TREE_PUBLIC (decl) = 0;
1445 else
1447 /* Static data member template instantiations, however, cannot
1448 have multiple copies. */
1449 if (DECL_INITIAL (decl) == 0
1450 || DECL_INITIAL (decl) == error_mark_node)
1451 DECL_COMMON (decl) = 1;
1452 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1454 DECL_COMMON (decl) = 1;
1455 DECL_INITIAL (decl) = error_mark_node;
1457 else
1459 /* We can't do anything useful; leave vars for explicit
1460 instantiation. */
1461 DECL_EXTERNAL (decl) = 1;
1462 DECL_NOT_REALLY_EXTERN (decl) = 0;
1466 if (DECL_LANG_SPECIFIC (decl))
1467 DECL_COMDAT (decl) = 1;
1470 /* For win32 we also want to put explicit instantiations in
1471 linkonce sections, so that they will be merged with implicit
1472 instantiations; otherwise we get duplicate symbol errors. */
1474 void
1475 maybe_make_one_only (tree decl)
1477 /* We used to say that this was not necessary on targets that support weak
1478 symbols, because the implicit instantiations will defer to the explicit
1479 one. However, that's not actually the case in SVR4; a strong definition
1480 after a weak one is an error. Also, not making explicit
1481 instantiations one_only means that we can end up with two copies of
1482 some template instantiations. */
1483 if (! flag_weak)
1484 return;
1486 /* We can't set DECL_COMDAT on functions, or finish_file will think
1487 we can get away with not emitting them if they aren't used. We need
1488 to for variables so that cp_finish_decl will update their linkage,
1489 because their DECL_INITIAL may not have been set properly yet. */
1491 make_decl_one_only (decl);
1493 if (TREE_CODE (decl) == VAR_DECL)
1495 DECL_COMDAT (decl) = 1;
1496 /* Mark it needed so we don't forget to emit it. */
1497 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)) = 1;
1501 /* Set TREE_PUBLIC and/or DECL_EXTERN on the vtable DECL,
1502 based on TYPE and other static flags.
1504 Note that anything public is tagged TREE_PUBLIC, whether
1505 it's public in this file or in another one. */
1507 void
1508 import_export_vtable (tree decl, tree type, int final)
1510 if (DECL_INTERFACE_KNOWN (decl))
1511 return;
1513 if (TYPE_FOR_JAVA (type))
1515 TREE_PUBLIC (decl) = 1;
1516 DECL_EXTERNAL (decl) = 1;
1517 DECL_INTERFACE_KNOWN (decl) = 1;
1519 else if (CLASSTYPE_INTERFACE_KNOWN (type))
1521 TREE_PUBLIC (decl) = 1;
1522 DECL_EXTERNAL (decl) = CLASSTYPE_INTERFACE_ONLY (type);
1523 DECL_INTERFACE_KNOWN (decl) = 1;
1525 else
1527 /* We can only wait to decide if we have real non-inline virtual
1528 functions in our class, or if we come from a template. */
1530 int found = (CLASSTYPE_TEMPLATE_INSTANTIATION (type)
1531 || CLASSTYPE_KEY_METHOD (type) != NULL_TREE);
1533 if (final || ! found)
1535 comdat_linkage (decl);
1536 DECL_EXTERNAL (decl) = 0;
1538 else
1540 TREE_PUBLIC (decl) = 1;
1541 DECL_EXTERNAL (decl) = 1;
1546 /* Determine whether or not we want to specifically import or export CTYPE,
1547 using various heuristics. */
1549 static void
1550 import_export_class (tree ctype)
1552 /* -1 for imported, 1 for exported. */
1553 int import_export = 0;
1555 /* It only makes sense to call this function at EOF. The reason is
1556 that this function looks at whether or not the first non-inline
1557 non-abstract virtual member function has been defined in this
1558 translation unit. But, we can't possibly know that until we've
1559 seen the entire translation unit. */
1560 my_friendly_assert (at_eof, 20000226);
1562 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1563 return;
1565 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma interface,
1566 we will have CLASSTYPE_INTERFACE_ONLY set but not
1567 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1568 heuristic because someone will supply a #pragma implementation
1569 elsewhere, and deducing it here would produce a conflict. */
1570 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1571 return;
1573 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1574 import_export = -1;
1575 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1576 import_export = 1;
1578 /* If we got -fno-implicit-templates, we import template classes that
1579 weren't explicitly instantiated. */
1580 if (import_export == 0
1581 && CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1582 && ! flag_implicit_templates)
1583 import_export = -1;
1585 /* Base our import/export status on that of the first non-inline,
1586 non-pure virtual function, if any. */
1587 if (import_export == 0
1588 && TYPE_POLYMORPHIC_P (ctype))
1590 tree method = CLASSTYPE_KEY_METHOD (ctype);
1591 if (method)
1592 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1595 #ifdef MULTIPLE_SYMBOL_SPACES
1596 if (import_export == -1)
1597 import_export = 0;
1598 #endif
1600 if (import_export)
1602 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1603 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1607 /* We need to describe to the assembler the relationship between
1608 a vtable and the vtable of the parent class. */
1610 static void
1611 output_vtable_inherit (tree vars)
1613 tree parent;
1614 rtx child_rtx, parent_rtx;
1616 child_rtx = XEXP (DECL_RTL (vars), 0); /* strip the mem ref */
1618 parent = binfo_for_vtable (vars);
1620 if (parent == TYPE_BINFO (DECL_CONTEXT (vars)))
1621 parent_rtx = const0_rtx;
1622 else if (parent)
1624 parent = get_vtbl_decl_for_binfo (TYPE_BINFO (BINFO_TYPE (parent)));
1625 parent_rtx = XEXP (DECL_RTL (parent), 0); /* strip the mem ref */
1627 else
1628 abort ();
1630 assemble_vtable_inherit (child_rtx, parent_rtx);
1633 /* If necessary, write out the vtables for the dynamic class CTYPE.
1634 Returns true if any vtables were emitted. */
1636 static bool
1637 maybe_emit_vtables (tree ctype)
1639 tree vtbl;
1640 tree primary_vtbl;
1642 /* If the vtables for this class have already been emitted there is
1643 nothing more to do. */
1644 primary_vtbl = CLASSTYPE_VTABLES (ctype);
1645 if (TREE_ASM_WRITTEN (primary_vtbl))
1646 return false;
1647 /* Ignore dummy vtables made by get_vtable_decl. */
1648 if (TREE_TYPE (primary_vtbl) == void_type_node)
1649 return false;
1651 import_export_class (ctype);
1652 import_export_vtable (primary_vtbl, ctype, 1);
1654 /* See if any of the vtables are needed. */
1655 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = TREE_CHAIN (vtbl))
1656 if (!DECL_EXTERNAL (vtbl) && DECL_NEEDED_P (vtbl))
1657 break;
1659 if (!vtbl)
1661 /* If the references to this class' vtables are optimized away,
1662 still emit the appropriate debugging information. See
1663 dfs_debug_mark. */
1664 if (DECL_COMDAT (primary_vtbl)
1665 && CLASSTYPE_DEBUG_REQUESTED (ctype))
1666 note_debug_info_needed (ctype);
1667 return false;
1670 /* The ABI requires that we emit all of the vtables if we emit any
1671 of them. */
1672 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = TREE_CHAIN (vtbl))
1674 /* Write it out. */
1675 import_export_vtable (vtbl, ctype, 1);
1676 mark_vtable_entries (vtbl);
1677 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
1678 store_init_value (vtbl, DECL_INITIAL (vtbl));
1680 if (write_symbols == DWARF_DEBUG || write_symbols == DWARF2_DEBUG)
1682 /* Mark the VAR_DECL node representing the vtable itself as a
1683 "gratuitous" one, thereby forcing dwarfout.c to ignore it.
1684 It is rather important that such things be ignored because
1685 any effort to actually generate DWARF for them will run
1686 into trouble when/if we encounter code like:
1688 #pragma interface
1689 struct S { virtual void member (); };
1691 because the artificial declaration of the vtable itself (as
1692 manufactured by the g++ front end) will say that the vtable
1693 is a static member of `S' but only *after* the debug output
1694 for the definition of `S' has already been output. This causes
1695 grief because the DWARF entry for the definition of the vtable
1696 will try to refer back to an earlier *declaration* of the
1697 vtable as a static member of `S' and there won't be one.
1698 We might be able to arrange to have the "vtable static member"
1699 attached to the member list for `S' before the debug info for
1700 `S' get written (which would solve the problem) but that would
1701 require more intrusive changes to the g++ front end. */
1703 DECL_IGNORED_P (vtbl) = 1;
1706 /* Always make vtables weak. */
1707 if (flag_weak)
1708 comdat_linkage (vtbl);
1710 rest_of_decl_compilation (vtbl, NULL, 1, 1);
1712 if (flag_vtable_gc)
1713 output_vtable_inherit (vtbl);
1715 /* Because we're only doing syntax-checking, we'll never end up
1716 actually marking the variable as written. */
1717 if (flag_syntax_only)
1718 TREE_ASM_WRITTEN (vtbl) = 1;
1721 /* Since we're writing out the vtable here, also write the debug
1722 info. */
1723 note_debug_info_needed (ctype);
1725 return true;
1728 /* Determines the proper settings of TREE_PUBLIC and DECL_EXTERNAL for an
1729 inline function or template instantiation at end-of-file. */
1731 void
1732 import_export_decl (tree decl)
1734 if (DECL_INTERFACE_KNOWN (decl))
1735 return;
1737 if (DECL_TEMPLATE_INSTANTIATION (decl)
1738 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
1740 DECL_NOT_REALLY_EXTERN (decl) = 1;
1741 if ((DECL_IMPLICIT_INSTANTIATION (decl)
1742 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
1743 && (flag_implicit_templates
1744 || (flag_implicit_inline_templates
1745 && DECL_DECLARED_INLINE_P (decl))))
1747 if (!TREE_PUBLIC (decl))
1748 /* Templates are allowed to have internal linkage. See
1749 [basic.link]. */
1751 else
1752 comdat_linkage (decl);
1754 else
1756 DECL_EXTERNAL (decl) = 1;
1757 DECL_NOT_REALLY_EXTERN (decl) = 0;
1760 else if (DECL_FUNCTION_MEMBER_P (decl))
1762 if (!DECL_DECLARED_INLINE_P (decl))
1764 tree ctype = DECL_CONTEXT (decl);
1765 import_export_class (ctype);
1766 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1768 DECL_NOT_REALLY_EXTERN (decl)
1769 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
1770 || (DECL_DECLARED_INLINE_P (decl)
1771 && ! flag_implement_inlines
1772 && !DECL_VINDEX (decl)));
1774 if (!DECL_NOT_REALLY_EXTERN (decl))
1775 DECL_EXTERNAL (decl) = 1;
1777 /* Always make artificials weak. */
1778 if (DECL_ARTIFICIAL (decl) && flag_weak)
1779 comdat_linkage (decl);
1780 else
1781 maybe_make_one_only (decl);
1784 else
1785 comdat_linkage (decl);
1787 else
1788 comdat_linkage (decl);
1790 DECL_INTERFACE_KNOWN (decl) = 1;
1793 /* Here, we only decide whether or not the tinfo node should be
1794 emitted with the vtable. IS_IN_LIBRARY is nonzero iff the
1795 typeinfo for TYPE should be in the runtime library. */
1797 void
1798 import_export_tinfo (tree decl, tree type, bool is_in_library)
1800 if (DECL_INTERFACE_KNOWN (decl))
1801 return;
1803 if (IS_AGGR_TYPE (type))
1804 import_export_class (type);
1806 if (IS_AGGR_TYPE (type) && CLASSTYPE_INTERFACE_KNOWN (type)
1807 && TYPE_POLYMORPHIC_P (type)
1808 /* If -fno-rtti, we're not necessarily emitting this stuff with
1809 the class, so go ahead and emit it now. This can happen when
1810 a class is used in exception handling. */
1811 && flag_rtti)
1813 DECL_NOT_REALLY_EXTERN (decl) = !CLASSTYPE_INTERFACE_ONLY (type);
1814 DECL_COMDAT (decl) = 0;
1816 else
1818 DECL_NOT_REALLY_EXTERN (decl) = 1;
1819 DECL_COMDAT (decl) = 1;
1822 /* Now override some cases. */
1823 if (flag_weak)
1824 DECL_COMDAT (decl) = 1;
1825 else if (is_in_library)
1826 DECL_COMDAT (decl) = 0;
1828 DECL_INTERFACE_KNOWN (decl) = 1;
1831 /* Return an expression that performs the destruction of DECL, which
1832 must be a VAR_DECL whose type has a non-trivial destructor, or is
1833 an array whose (innermost) elements have a non-trivial destructor. */
1835 tree
1836 build_cleanup (tree decl)
1838 tree temp;
1839 tree type = TREE_TYPE (decl);
1841 /* This function should only be called for declarations that really
1842 require cleanups. */
1843 my_friendly_assert (!TYPE_HAS_TRIVIAL_DESTRUCTOR (type), 20030106);
1845 /* Treat all objects with destructors as used; the destructor may do
1846 something substantive. */
1847 mark_used (decl);
1849 if (TREE_CODE (type) == ARRAY_TYPE)
1850 temp = decl;
1851 else
1853 cxx_mark_addressable (decl);
1854 temp = build1 (ADDR_EXPR, build_pointer_type (type), decl);
1856 temp = build_delete (TREE_TYPE (temp), temp,
1857 sfk_complete_destructor,
1858 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
1859 return temp;
1862 /* Returns the initialization guard variable for the variable DECL,
1863 which has static storage duration. */
1865 tree
1866 get_guard (tree decl)
1868 tree sname;
1869 tree guard;
1871 sname = mangle_guard_variable (decl);
1872 guard = IDENTIFIER_GLOBAL_VALUE (sname);
1873 if (! guard)
1875 tree guard_type;
1877 /* We use a type that is big enough to contain a mutex as well
1878 as an integer counter. */
1879 guard_type = long_long_integer_type_node;
1880 guard = build_decl (VAR_DECL, sname, guard_type);
1882 /* The guard should have the same linkage as what it guards. */
1883 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
1884 TREE_STATIC (guard) = TREE_STATIC (decl);
1885 DECL_COMMON (guard) = DECL_COMMON (decl);
1886 DECL_ONE_ONLY (guard) = DECL_ONE_ONLY (decl);
1887 if (TREE_PUBLIC (decl))
1888 DECL_WEAK (guard) = DECL_WEAK (decl);
1890 DECL_ARTIFICIAL (guard) = 1;
1891 TREE_USED (guard) = 1;
1892 pushdecl_top_level (guard);
1893 cp_finish_decl (guard, NULL_TREE, NULL_TREE, 0);
1895 return guard;
1898 /* Return those bits of the GUARD variable that should be set when the
1899 guarded entity is actually initialized. */
1901 static tree
1902 get_guard_bits (tree guard)
1904 /* We only set the first byte of the guard, in order to leave room
1905 for a mutex in the high-order bits. */
1906 guard = build1 (ADDR_EXPR,
1907 build_pointer_type (TREE_TYPE (guard)),
1908 guard);
1909 guard = build1 (NOP_EXPR,
1910 build_pointer_type (char_type_node),
1911 guard);
1912 guard = build1 (INDIRECT_REF, char_type_node, guard);
1914 return guard;
1917 /* Return an expression which determines whether or not the GUARD
1918 variable has already been initialized. */
1920 tree
1921 get_guard_cond (tree guard)
1923 tree guard_value;
1925 /* Check to see if the GUARD is zero. */
1926 guard = get_guard_bits (guard);
1927 guard_value = integer_zero_node;
1928 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
1929 guard_value = convert (TREE_TYPE (guard), guard_value);
1930 return cp_build_binary_op (EQ_EXPR, guard, guard_value);
1933 /* Return an expression which sets the GUARD variable, indicating that
1934 the variable being guarded has been initialized. */
1936 tree
1937 set_guard (tree guard)
1939 tree guard_init;
1941 /* Set the GUARD to one. */
1942 guard = get_guard_bits (guard);
1943 guard_init = integer_one_node;
1944 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
1945 guard_init = convert (TREE_TYPE (guard), guard_init);
1946 return build_modify_expr (guard, NOP_EXPR, guard_init);
1949 /* Start the process of running a particular set of global constructors
1950 or destructors. Subroutine of do_[cd]tors. */
1952 static tree
1953 start_objects (int method_type, int initp)
1955 tree fnname;
1956 tree body;
1957 char type[10];
1959 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
1961 if (initp != DEFAULT_INIT_PRIORITY)
1963 char joiner;
1965 #ifdef JOINER
1966 joiner = JOINER;
1967 #else
1968 joiner = '_';
1969 #endif
1971 sprintf (type, "%c%c%.5u", method_type, joiner, initp);
1973 else
1974 sprintf (type, "%c", method_type);
1976 fnname = get_file_function_name_long (type);
1978 start_function (void_list_node,
1979 make_call_declarator (fnname, void_list_node, NULL_TREE,
1980 NULL_TREE),
1981 NULL_TREE, SF_DEFAULT);
1983 /* It can be a static function as long as collect2 does not have
1984 to scan the object file to find its ctor/dtor routine. */
1985 TREE_PUBLIC (current_function_decl) = ! targetm.have_ctors_dtors;
1987 /* Mark this declaration as used to avoid spurious warnings. */
1988 TREE_USED (current_function_decl) = 1;
1990 /* Mark this function as a global constructor or destructor. */
1991 if (method_type == 'I')
1992 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
1993 else
1994 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
1995 DECL_LANG_SPECIFIC (current_function_decl)->decl_flags.u2sel = 1;
1997 body = begin_compound_stmt (/*has_no_scope=*/0);
1999 /* We cannot allow these functions to be elided, even if they do not
2000 have external linkage. And, there's no point in deferring
2001 copmilation of thes functions; they're all going to have to be
2002 out anyhow. */
2003 current_function_cannot_inline
2004 = "static constructors and destructors cannot be inlined";
2006 return body;
2009 /* Finish the process of running a particular set of global constructors
2010 or destructors. Subroutine of do_[cd]tors. */
2012 static void
2013 finish_objects (int method_type, int initp, tree body)
2015 tree fn;
2017 /* Finish up. */
2018 finish_compound_stmt (/*has_no_scope=*/0, body);
2019 fn = finish_function (0);
2020 expand_body (fn);
2022 /* When only doing semantic analysis, and no RTL generation, we
2023 can't call functions that directly emit assembly code; there is
2024 no assembly file in which to put the code. */
2025 if (flag_syntax_only)
2026 return;
2028 if (targetm.have_ctors_dtors)
2030 rtx fnsym = XEXP (DECL_RTL (fn), 0);
2031 if (method_type == 'I')
2032 (* targetm.asm_out.constructor) (fnsym, initp);
2033 else
2034 (* targetm.asm_out.destructor) (fnsym, initp);
2038 /* The names of the parameters to the function created to handle
2039 initializations and destructions for objects with static storage
2040 duration. */
2041 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
2042 #define PRIORITY_IDENTIFIER "__priority"
2044 /* The name of the function we create to handle initializations and
2045 destructions for objects with static storage duration. */
2046 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
2048 /* The declaration for the __INITIALIZE_P argument. */
2049 static GTY(()) tree initialize_p_decl;
2051 /* The declaration for the __PRIORITY argument. */
2052 static GTY(()) tree priority_decl;
2054 /* The declaration for the static storage duration function. */
2055 static GTY(()) tree ssdf_decl;
2057 /* All the static storage duration functions created in this
2058 translation unit. */
2059 static GTY(()) varray_type ssdf_decls;
2061 /* A map from priority levels to information about that priority
2062 level. There may be many such levels, so efficient lookup is
2063 important. */
2064 static splay_tree priority_info_map;
2066 /* Begins the generation of the function that will handle all
2067 initialization and destruction of objects with static storage
2068 duration. The function generated takes two parameters of type
2069 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
2070 nonzero, it performs initializations. Otherwise, it performs
2071 destructions. It only performs those initializations or
2072 destructions with the indicated __PRIORITY. The generated function
2073 returns no value.
2075 It is assumed that this function will only be called once per
2076 translation unit. */
2078 static tree
2079 start_static_storage_duration_function (void)
2081 static unsigned ssdf_number;
2083 tree parm_types;
2084 tree type;
2085 tree body;
2086 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
2088 /* Create the identifier for this function. It will be of the form
2089 SSDF_IDENTIFIER_<number>. */
2090 sprintf (id, "%s_%u", SSDF_IDENTIFIER, ssdf_number++);
2091 if (ssdf_number == 0)
2093 /* Overflow occurred. That means there are at least 4 billion
2094 initialization functions. */
2095 sorry ("too many initialization functions required");
2096 abort ();
2099 /* Create the parameters. */
2100 parm_types = void_list_node;
2101 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
2102 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
2103 type = build_function_type (void_type_node, parm_types);
2105 /* Create the FUNCTION_DECL itself. */
2106 ssdf_decl = build_lang_decl (FUNCTION_DECL,
2107 get_identifier (id),
2108 type);
2109 TREE_PUBLIC (ssdf_decl) = 0;
2110 DECL_ARTIFICIAL (ssdf_decl) = 1;
2112 /* Put this function in the list of functions to be called from the
2113 static constructors and destructors. */
2114 if (!ssdf_decls)
2116 VARRAY_TREE_INIT (ssdf_decls, 32, "ssdf_decls");
2118 /* Take this opportunity to initialize the map from priority
2119 numbers to information about that priority level. */
2120 priority_info_map = splay_tree_new (splay_tree_compare_ints,
2121 /*delete_key_fn=*/0,
2122 /*delete_value_fn=*/
2123 (splay_tree_delete_value_fn) &free);
2125 /* We always need to generate functions for the
2126 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
2127 priorities later, we'll be sure to find the
2128 DEFAULT_INIT_PRIORITY. */
2129 get_priority_info (DEFAULT_INIT_PRIORITY);
2132 VARRAY_PUSH_TREE (ssdf_decls, ssdf_decl);
2134 /* Create the argument list. */
2135 initialize_p_decl = cp_build_parm_decl
2136 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
2137 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
2138 TREE_USED (initialize_p_decl) = 1;
2139 priority_decl = cp_build_parm_decl
2140 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
2141 DECL_CONTEXT (priority_decl) = ssdf_decl;
2142 TREE_USED (priority_decl) = 1;
2144 TREE_CHAIN (initialize_p_decl) = priority_decl;
2145 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
2147 /* Put the function in the global scope. */
2148 pushdecl (ssdf_decl);
2150 /* Start the function itself. This is equivalent to declarating the
2151 function as:
2153 static void __ssdf (int __initialize_p, init __priority_p);
2155 It is static because we only need to call this function from the
2156 various constructor and destructor functions for this module. */
2157 start_function (/*specs=*/NULL_TREE,
2158 ssdf_decl,
2159 /*attrs=*/NULL_TREE,
2160 SF_PRE_PARSED);
2162 /* Set up the scope of the outermost block in the function. */
2163 body = begin_compound_stmt (/*has_no_scope=*/0);
2165 /* This function must not be deferred because we are depending on
2166 its compilation to tell us what is TREE_SYMBOL_REFERENCED. */
2167 current_function_cannot_inline
2168 = "static storage duration functions cannot be inlined";
2170 return body;
2173 /* Finish the generation of the function which performs initialization
2174 and destruction of objects with static storage duration. After
2175 this point, no more such objects can be created. */
2177 static void
2178 finish_static_storage_duration_function (tree body)
2180 /* Close out the function. */
2181 finish_compound_stmt (/*has_no_scope=*/0, body);
2182 expand_body (finish_function (0));
2185 /* Return the information about the indicated PRIORITY level. If no
2186 code to handle this level has yet been generated, generate the
2187 appropriate prologue. */
2189 static priority_info
2190 get_priority_info (int priority)
2192 priority_info pi;
2193 splay_tree_node n;
2195 n = splay_tree_lookup (priority_info_map,
2196 (splay_tree_key) priority);
2197 if (!n)
2199 /* Create a new priority information structure, and insert it
2200 into the map. */
2201 pi = (priority_info) xmalloc (sizeof (struct priority_info_s));
2202 pi->initializations_p = 0;
2203 pi->destructions_p = 0;
2204 splay_tree_insert (priority_info_map,
2205 (splay_tree_key) priority,
2206 (splay_tree_value) pi);
2208 else
2209 pi = (priority_info) n->value;
2211 return pi;
2214 /* Set up to handle the initialization or destruction of DECL. If
2215 INITP is nonzero, we are initializing the variable. Otherwise, we
2216 are destroying it. */
2218 static tree
2219 start_static_initialization_or_destruction (tree decl, int initp)
2221 tree guard_if_stmt = NULL_TREE;
2222 int priority;
2223 tree cond;
2224 tree guard;
2225 tree init_cond;
2226 priority_info pi;
2228 /* Figure out the priority for this declaration. */
2229 priority = DECL_INIT_PRIORITY (decl);
2230 if (!priority)
2231 priority = DEFAULT_INIT_PRIORITY;
2233 /* Remember that we had an initialization or finalization at this
2234 priority. */
2235 pi = get_priority_info (priority);
2236 if (initp)
2237 pi->initializations_p = 1;
2238 else
2239 pi->destructions_p = 1;
2241 /* Trick the compiler into thinking we are at the file and line
2242 where DECL was declared so that error-messages make sense, and so
2243 that the debugger will show somewhat sensible file and line
2244 information. */
2245 input_filename = DECL_SOURCE_FILE (decl);
2246 lineno = DECL_SOURCE_LINE (decl);
2248 /* Because of:
2250 [class.access.spec]
2252 Access control for implicit calls to the constructors,
2253 the conversion functions, or the destructor called to
2254 create and destroy a static data member is performed as
2255 if these calls appeared in the scope of the member's
2256 class.
2258 we pretend we are in a static member function of the class of
2259 which the DECL is a member. */
2260 if (member_p (decl))
2262 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
2263 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
2266 /* Conditionalize this initialization on being in the right priority
2267 and being initializing/finalizing appropriately. */
2268 guard_if_stmt = begin_if_stmt ();
2269 cond = cp_build_binary_op (EQ_EXPR,
2270 priority_decl,
2271 build_int_2 (priority, 0));
2272 init_cond = initp ? integer_one_node : integer_zero_node;
2273 init_cond = cp_build_binary_op (EQ_EXPR,
2274 initialize_p_decl,
2275 init_cond);
2276 cond = cp_build_binary_op (TRUTH_ANDIF_EXPR, cond, init_cond);
2278 /* Assume we don't need a guard. */
2279 guard = NULL_TREE;
2280 /* We need a guard if this is an object with external linkage that
2281 might be initialized in more than one place. (For example, a
2282 static data member of a template, when the data member requires
2283 construction.) */
2284 if (TREE_PUBLIC (decl) && (DECL_COMMON (decl)
2285 || DECL_ONE_ONLY (decl)
2286 || DECL_WEAK (decl)))
2288 tree guard_cond;
2290 guard = get_guard (decl);
2292 /* When using __cxa_atexit, we just check the GUARD as we would
2293 for a local static. */
2294 if (flag_use_cxa_atexit)
2296 /* When using __cxa_atexit, we never try to destroy
2297 anything from a static destructor. */
2298 my_friendly_assert (initp, 20000629);
2299 guard_cond = get_guard_cond (guard);
2301 /* If we don't have __cxa_atexit, then we will be running
2302 destructors from .fini sections, or their equivalents. So,
2303 we need to know how many times we've tried to initialize this
2304 object. We do initializations only if the GUARD is zero,
2305 i.e., if we are the first to initialize the variable. We do
2306 destructions only if the GUARD is one, i.e., if we are the
2307 last to destroy the variable. */
2308 else if (initp)
2309 guard_cond
2310 = cp_build_binary_op (EQ_EXPR,
2311 build_unary_op (PREINCREMENT_EXPR,
2312 guard,
2313 /*noconvert=*/1),
2314 integer_one_node);
2315 else
2316 guard_cond
2317 = cp_build_binary_op (EQ_EXPR,
2318 build_unary_op (PREDECREMENT_EXPR,
2319 guard,
2320 /*noconvert=*/1),
2321 integer_zero_node);
2323 cond = cp_build_binary_op (TRUTH_ANDIF_EXPR, cond, guard_cond);
2326 finish_if_stmt_cond (cond, guard_if_stmt);
2328 /* If we're using __cxa_atexit, we have not already set the GUARD,
2329 so we must do so now. */
2330 if (guard && initp && flag_use_cxa_atexit)
2331 finish_expr_stmt (set_guard (guard));
2333 return guard_if_stmt;
2336 /* We've just finished generating code to do an initialization or
2337 finalization. GUARD_IF_STMT is the if-statement we used to guard
2338 the initialization. */
2340 static void
2341 finish_static_initialization_or_destruction (tree guard_if_stmt)
2343 finish_then_clause (guard_if_stmt);
2344 finish_if_stmt ();
2346 /* Now that we're done with DECL we don't need to pretend to be a
2347 member of its class any longer. */
2348 DECL_CONTEXT (current_function_decl) = NULL_TREE;
2349 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
2352 /* Generate code to do the initialization of DECL, a VAR_DECL with
2353 static storage duration. The initialization is INIT. */
2355 static void
2356 do_static_initialization (tree decl, tree init)
2358 tree guard_if_stmt;
2360 /* Set up for the initialization. */
2361 guard_if_stmt
2362 = start_static_initialization_or_destruction (decl,
2363 /*initp=*/1);
2365 /* Perform the initialization. */
2366 if (init)
2367 finish_expr_stmt (init);
2369 /* If we're using __cxa_atexit, register a a function that calls the
2370 destructor for the object. */
2371 if (flag_use_cxa_atexit)
2372 register_dtor_fn (decl);
2374 /* Finsh up. */
2375 finish_static_initialization_or_destruction (guard_if_stmt);
2378 /* Generate code to do the static destruction of DECL. If DECL may be
2379 initialized more than once in different object files, GUARD is the
2380 guard variable to check. PRIORITY is the priority for the
2381 destruction. */
2383 static void
2384 do_static_destruction (tree decl)
2386 tree guard_if_stmt;
2388 /* If we're using __cxa_atexit, then destructors are registered
2389 immediately after objects are initialized. */
2390 my_friendly_assert (!flag_use_cxa_atexit, 20000121);
2392 /* If we don't need a destructor, there's nothing to do. */
2393 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
2394 return;
2396 /* Actually do the destruction. */
2397 guard_if_stmt = start_static_initialization_or_destruction (decl,
2398 /*initp=*/0);
2399 finish_expr_stmt (build_cleanup (decl));
2400 finish_static_initialization_or_destruction (guard_if_stmt);
2403 /* VARS is a list of variables with static storage duration which may
2404 need initialization and/or finalization. Remove those variables
2405 that don't really need to be initialized or finalized, and return
2406 the resulting list. The order in which the variables appear in
2407 VARS is in reverse order of the order in which they should actually
2408 be initialized. The list we return is in the unreversed order;
2409 i.e., the first variable should be initialized first. */
2411 static tree
2412 prune_vars_needing_no_initialization (tree *vars)
2414 tree *var = vars;
2415 tree result = NULL_TREE;
2417 while (*var)
2419 tree t = *var;
2420 tree decl = TREE_VALUE (t);
2421 tree init = TREE_PURPOSE (t);
2423 /* Deal gracefully with error. */
2424 if (decl == error_mark_node)
2426 var = &TREE_CHAIN (t);
2427 continue;
2430 /* The only things that can be initialized are variables. */
2431 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 19990420);
2433 /* If this object is not defined, we don't need to do anything
2434 here. */
2435 if (DECL_EXTERNAL (decl))
2437 var = &TREE_CHAIN (t);
2438 continue;
2441 /* Also, if the initializer already contains errors, we can bail
2442 out now. */
2443 if (init && TREE_CODE (init) == TREE_LIST
2444 && value_member (error_mark_node, init))
2446 var = &TREE_CHAIN (t);
2447 continue;
2450 /* This variable is going to need initialization and/or
2451 finalization, so we add it to the list. */
2452 *var = TREE_CHAIN (t);
2453 TREE_CHAIN (t) = result;
2454 result = t;
2457 return result;
2460 /* Make sure we have told the back end about all the variables in
2461 VARS. */
2463 static void
2464 write_out_vars (tree vars)
2466 tree v;
2468 for (v = vars; v; v = TREE_CHAIN (v))
2469 if (! TREE_ASM_WRITTEN (TREE_VALUE (v)))
2470 rest_of_decl_compilation (TREE_VALUE (v), 0, 1, 1);
2473 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
2474 (otherwise) that will initialize all gobal objects with static
2475 storage duration having the indicated PRIORITY. */
2477 static void
2478 generate_ctor_or_dtor_function (bool constructor_p, int priority)
2480 char function_key;
2481 tree arguments;
2482 tree body;
2483 size_t i;
2485 /* We use `I' to indicate initialization and `D' to indicate
2486 destruction. */
2487 if (constructor_p)
2488 function_key = 'I';
2489 else
2490 function_key = 'D';
2492 /* Begin the function. */
2493 body = start_objects (function_key, priority);
2495 /* Call the static storage duration function with appropriate
2496 arguments. */
2497 if (ssdf_decls)
2498 for (i = 0; i < ssdf_decls->elements_used; ++i)
2500 arguments = tree_cons (NULL_TREE, build_int_2 (priority, 0),
2501 NULL_TREE);
2502 arguments = tree_cons (NULL_TREE, build_int_2 (constructor_p, 0),
2503 arguments);
2504 finish_expr_stmt (build_function_call (VARRAY_TREE (ssdf_decls, i),
2505 arguments));
2508 /* If we're generating code for the DEFAULT_INIT_PRIORITY, throw in
2509 calls to any functions marked with attributes indicating that
2510 they should be called at initialization- or destruction-time. */
2511 if (priority == DEFAULT_INIT_PRIORITY)
2513 tree fns;
2515 for (fns = constructor_p ? static_ctors : static_dtors;
2516 fns;
2517 fns = TREE_CHAIN (fns))
2518 finish_expr_stmt (build_function_call (TREE_VALUE (fns), NULL_TREE));
2521 /* Close out the function. */
2522 finish_objects (function_key, priority, body);
2525 /* Generate constructor and destructor functions for the priority
2526 indicated by N. */
2528 static int
2529 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n,
2530 void * data ATTRIBUTE_UNUSED)
2532 int priority = (int) n->key;
2533 priority_info pi = (priority_info) n->value;
2535 /* Generate the functions themselves, but only if they are really
2536 needed. */
2537 if (pi->initializations_p
2538 || (priority == DEFAULT_INIT_PRIORITY && static_ctors))
2539 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority);
2540 if (pi->destructions_p
2541 || (priority == DEFAULT_INIT_PRIORITY && static_dtors))
2542 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority);
2544 /* Keep iterating. */
2545 return 0;
2548 /* This routine is called from the last rule in yyparse ().
2549 Its job is to create all the code needed to initialize and
2550 destroy the global aggregates. We do the destruction
2551 first, since that way we only need to reverse the decls once. */
2553 void
2554 finish_file ()
2556 tree vars;
2557 bool reconsider;
2558 size_t i;
2560 at_eof = 1;
2562 /* Bad parse errors. Just forget about it. */
2563 if (! global_bindings_p () || current_class_type || decl_namespace_list)
2564 return;
2566 if (pch_file)
2567 c_common_write_pch ();
2569 /* Otherwise, GDB can get confused, because in only knows
2570 about source for LINENO-1 lines. */
2571 lineno -= 1;
2573 interface_unknown = 1;
2574 interface_only = 0;
2576 /* We now have to write out all the stuff we put off writing out.
2577 These include:
2579 o Template specializations that we have not yet instantiated,
2580 but which are needed.
2581 o Initialization and destruction for non-local objects with
2582 static storage duration. (Local objects with static storage
2583 duration are initialized when their scope is first entered,
2584 and are cleaned up via atexit.)
2585 o Virtual function tables.
2587 All of these may cause others to be needed. For example,
2588 instantiating one function may cause another to be needed, and
2589 generating the initializer for an object may cause templates to be
2590 instantiated, etc., etc. */
2592 timevar_push (TV_VARCONST);
2594 emit_support_tinfos ();
2598 tree t;
2599 size_t n_old, n_new;
2601 reconsider = false;
2603 /* If there are templates that we've put off instantiating, do
2604 them now. */
2605 instantiate_pending_templates ();
2607 /* Write out virtual tables as required. Note that writing out
2608 the virtual table for a template class may cause the
2609 instantiation of members of that class. If we write out
2610 vtables then we remove the class from our list so we don't
2611 have to look at it again. */
2613 while (keyed_classes != NULL_TREE
2614 && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
2616 reconsider = true;
2617 keyed_classes = TREE_CHAIN (keyed_classes);
2620 t = keyed_classes;
2621 if (t != NULL_TREE)
2623 tree next = TREE_CHAIN (t);
2625 while (next)
2627 if (maybe_emit_vtables (TREE_VALUE (next)))
2629 reconsider = true;
2630 TREE_CHAIN (t) = TREE_CHAIN (next);
2632 else
2633 t = next;
2635 next = TREE_CHAIN (t);
2639 /* Write out needed type info variables. We have to be careful
2640 looping through unemitted decls, because emit_tinfo_decl may
2641 cause other variables to be needed. We stick new elements
2642 (and old elements that we may need to reconsider) at the end
2643 of the array, then shift them back to the beginning once we're
2644 done. */
2646 n_old = VARRAY_ACTIVE_SIZE (unemitted_tinfo_decls);
2647 for (i = 0; i < n_old; ++i)
2649 tree tinfo_decl = VARRAY_TREE (unemitted_tinfo_decls, i);
2650 if (emit_tinfo_decl (tinfo_decl))
2651 reconsider = true;
2652 else
2653 VARRAY_PUSH_TREE (unemitted_tinfo_decls, tinfo_decl);
2656 /* The only elements we want to keep are the new ones. Copy
2657 them to the beginning of the array, then get rid of the
2658 leftovers. */
2659 n_new = VARRAY_ACTIVE_SIZE (unemitted_tinfo_decls) - n_old;
2660 memmove (&VARRAY_TREE (unemitted_tinfo_decls, 0),
2661 &VARRAY_TREE (unemitted_tinfo_decls, n_old),
2662 n_new * sizeof (tree));
2663 memset (&VARRAY_TREE (unemitted_tinfo_decls, n_new),
2665 n_old * sizeof (tree));
2666 VARRAY_ACTIVE_SIZE (unemitted_tinfo_decls) = n_new;
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
2672 loop. */
2673 vars = prune_vars_needing_no_initialization (&static_aggregates);
2675 if (vars)
2677 tree v;
2679 /* We need to start a new initialization function each time
2680 through the loop. That's because we need to know which
2681 vtables have been referenced, and TREE_SYMBOL_REFERENCED
2682 isn't computed until a function is finished, and written
2683 out. That's a deficiency in the back-end. When this is
2684 fixed, these initialization functions could all become
2685 inline, with resulting performance improvements. */
2686 tree ssdf_body = start_static_storage_duration_function ();
2688 /* Make sure the back end knows about all the variables. */
2689 write_out_vars (vars);
2691 /* First generate code to do all the initializations. */
2692 for (v = vars; v; v = TREE_CHAIN (v))
2693 do_static_initialization (TREE_VALUE (v),
2694 TREE_PURPOSE (v));
2696 /* Then, generate code to do all the destructions. Do these
2697 in reverse order so that the most recently constructed
2698 variable is the first destroyed. If we're using
2699 __cxa_atexit, then we don't need to do this; functions
2700 were registered at initialization time to destroy the
2701 local statics. */
2702 if (!flag_use_cxa_atexit)
2704 vars = nreverse (vars);
2705 for (v = vars; v; v = TREE_CHAIN (v))
2706 do_static_destruction (TREE_VALUE (v));
2708 else
2709 vars = NULL_TREE;
2711 /* Finish up the static storage duration function for this
2712 round. */
2713 finish_static_storage_duration_function (ssdf_body);
2715 /* All those initializations and finalizations might cause
2716 us to need more inline functions, more template
2717 instantiations, etc. */
2718 reconsider = true;
2721 for (i = 0; i < deferred_fns_used; ++i)
2723 tree decl = VARRAY_TREE (deferred_fns, i);
2725 import_export_decl (decl);
2727 /* Does it need synthesizing? */
2728 if (DECL_ARTIFICIAL (decl) && ! DECL_INITIAL (decl)
2729 && TREE_USED (decl)
2730 && (! DECL_REALLY_EXTERN (decl) || DECL_INLINE (decl)))
2732 /* Even though we're already at the top-level, we push
2733 there again. That way, when we pop back a few lines
2734 hence, all of our state is restored. Otherwise,
2735 finish_function doesn't clean things up, and we end
2736 up with CURRENT_FUNCTION_DECL set. */
2737 push_to_top_level ();
2738 synthesize_method (decl);
2739 pop_from_top_level ();
2740 reconsider = true;
2743 /* We lie to the back-end, pretending that some functions
2744 are not defined when they really are. This keeps these
2745 functions from being put out unnecessarily. But, we must
2746 stop lying when the functions are referenced, or if they
2747 are not comdat since they need to be put out now. This
2748 is done in a separate for cycle, because if some deferred
2749 function is contained in another deferred function later
2750 in deferred_fns varray, rest_of_compilation would skip
2751 this function and we really cannot expand the same
2752 function twice. */
2753 if (DECL_NOT_REALLY_EXTERN (decl)
2754 && DECL_INITIAL (decl)
2755 && DECL_NEEDED_P (decl))
2756 DECL_EXTERNAL (decl) = 0;
2758 /* If we're going to need to write this function out, and
2759 there's already a body for it, create RTL for it now.
2760 (There might be no body if this is a method we haven't
2761 gotten around to synthesizing yet.) */
2762 if (!DECL_EXTERNAL (decl)
2763 && DECL_NEEDED_P (decl)
2764 && DECL_SAVED_TREE (decl)
2765 && !TREE_ASM_WRITTEN (decl))
2767 int saved_not_really_extern;
2769 /* When we call finish_function in expand_body, it will
2770 try to reset DECL_NOT_REALLY_EXTERN so we save and
2771 restore it here. */
2772 saved_not_really_extern = DECL_NOT_REALLY_EXTERN (decl);
2773 /* Generate RTL for this function now that we know we
2774 need it. */
2775 expand_body (decl);
2776 /* Undo the damage done by finish_function. */
2777 DECL_EXTERNAL (decl) = 0;
2778 DECL_NOT_REALLY_EXTERN (decl) = saved_not_really_extern;
2779 /* If we're compiling -fsyntax-only pretend that this
2780 function has been written out so that we don't try to
2781 expand it again. */
2782 if (flag_syntax_only)
2783 TREE_ASM_WRITTEN (decl) = 1;
2784 reconsider = true;
2788 if (deferred_fns_used
2789 && wrapup_global_declarations (&VARRAY_TREE (deferred_fns, 0),
2790 deferred_fns_used))
2791 reconsider = true;
2792 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
2793 reconsider = true;
2795 /* Static data members are just like namespace-scope globals. */
2796 for (i = 0; i < pending_statics_used; ++i)
2798 tree decl = VARRAY_TREE (pending_statics, i);
2799 if (TREE_ASM_WRITTEN (decl))
2800 continue;
2801 import_export_decl (decl);
2802 if (DECL_NOT_REALLY_EXTERN (decl) && ! DECL_IN_AGGR_P (decl))
2803 DECL_EXTERNAL (decl) = 0;
2805 if (pending_statics
2806 && wrapup_global_declarations (&VARRAY_TREE (pending_statics, 0),
2807 pending_statics_used))
2808 reconsider = true;
2810 while (reconsider);
2812 /* All used inline functions must have a definition at this point. */
2813 for (i = 0; i < deferred_fns_used; ++i)
2815 tree decl = VARRAY_TREE (deferred_fns, i);
2817 if (TREE_USED (decl) && DECL_DECLARED_INLINE_P (decl)
2818 && !(TREE_ASM_WRITTEN (decl) || DECL_SAVED_TREE (decl)
2819 /* An explicit instantiation can be used to specify
2820 that the body is in another unit. It will have
2821 already verified there was a definition. */
2822 || DECL_EXPLICIT_INSTANTIATION (decl)))
2824 cp_warning_at ("inline function `%D' used but never defined", decl);
2825 /* This symbol is effectively an "extern" declaration now.
2826 This is not strictly necessary, but removes a duplicate
2827 warning. */
2828 TREE_PUBLIC (decl) = 1;
2833 /* We give C linkage to static constructors and destructors. */
2834 push_lang_context (lang_name_c);
2836 /* Generate initialization and destruction functions for all
2837 priorities for which they are required. */
2838 if (priority_info_map)
2839 splay_tree_foreach (priority_info_map,
2840 generate_ctor_and_dtor_functions_for_priority,
2841 /*data=*/0);
2842 else
2844 if (static_ctors)
2845 generate_ctor_or_dtor_function (/*constructor_p=*/true,
2846 DEFAULT_INIT_PRIORITY);
2847 if (static_dtors)
2848 generate_ctor_or_dtor_function (/*constructor_p=*/false,
2849 DEFAULT_INIT_PRIORITY);
2852 /* We're done with the splay-tree now. */
2853 if (priority_info_map)
2854 splay_tree_delete (priority_info_map);
2856 /* We're done with static constructors, so we can go back to "C++"
2857 linkage now. */
2858 pop_lang_context ();
2860 /* Now, issue warnings about static, but not defined, functions,
2861 etc., and emit debugging information. */
2862 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
2863 if (pending_statics)
2864 check_global_declarations (&VARRAY_TREE (pending_statics, 0),
2865 pending_statics_used);
2867 finish_repo ();
2869 /* The entire file is now complete. If requested, dump everything
2870 to a file. */
2872 int flags;
2873 FILE *stream = dump_begin (TDI_all, &flags);
2875 if (stream)
2877 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
2878 dump_end (TDI_all, stream);
2882 timevar_pop (TV_VARCONST);
2884 if (flag_detailed_statistics)
2886 dump_tree_statistics ();
2887 dump_time_statistics ();
2891 /* T is the parse tree for an expression. Return the expression after
2892 performing semantic analysis. */
2894 tree
2895 build_expr_from_tree (t)
2896 tree t;
2898 if (t == NULL_TREE || t == error_mark_node)
2899 return t;
2901 switch (TREE_CODE (t))
2903 case IDENTIFIER_NODE:
2904 return do_identifier (t, NULL_TREE);
2906 case LOOKUP_EXPR:
2907 if (LOOKUP_EXPR_GLOBAL (t))
2909 tree token = TREE_OPERAND (t, 0);
2910 return do_scoped_id (token, IDENTIFIER_GLOBAL_VALUE (token));
2912 else
2914 t = do_identifier (TREE_OPERAND (t, 0), NULL_TREE);
2915 if (TREE_CODE (t) == ALIAS_DECL)
2916 t = DECL_INITIAL (t);
2917 return t;
2920 case TEMPLATE_ID_EXPR:
2922 tree template;
2923 tree args;
2924 tree object;
2926 template = build_expr_from_tree (TREE_OPERAND (t, 0));
2927 args = build_expr_from_tree (TREE_OPERAND (t, 1));
2929 if (TREE_CODE (template) == COMPONENT_REF)
2931 object = TREE_OPERAND (template, 0);
2932 template = TREE_OPERAND (template, 1);
2934 else
2935 object = NULL_TREE;
2937 template = lookup_template_function (template, args);
2938 if (object)
2939 return build (COMPONENT_REF, TREE_TYPE (template),
2940 object, template);
2941 else
2942 return template;
2945 case INDIRECT_REF:
2946 return build_x_indirect_ref
2947 (build_expr_from_tree (TREE_OPERAND (t, 0)), "unary *");
2949 case CAST_EXPR:
2950 return build_functional_cast
2951 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
2953 case REINTERPRET_CAST_EXPR:
2954 return build_reinterpret_cast
2955 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
2957 case CONST_CAST_EXPR:
2958 return build_const_cast
2959 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
2961 case DYNAMIC_CAST_EXPR:
2962 return build_dynamic_cast
2963 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
2965 case STATIC_CAST_EXPR:
2966 return build_static_cast
2967 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
2969 case PREDECREMENT_EXPR:
2970 case PREINCREMENT_EXPR:
2971 case POSTDECREMENT_EXPR:
2972 case POSTINCREMENT_EXPR:
2973 case NEGATE_EXPR:
2974 case BIT_NOT_EXPR:
2975 case ABS_EXPR:
2976 case TRUTH_NOT_EXPR:
2977 case ADDR_EXPR:
2978 case CONVERT_EXPR: /* Unary + */
2979 case REALPART_EXPR:
2980 case IMAGPART_EXPR:
2981 if (TREE_TYPE (t))
2982 return t;
2983 return build_x_unary_op (TREE_CODE (t),
2984 build_expr_from_tree (TREE_OPERAND (t, 0)));
2986 case PLUS_EXPR:
2987 case MINUS_EXPR:
2988 case MULT_EXPR:
2989 case TRUNC_DIV_EXPR:
2990 case CEIL_DIV_EXPR:
2991 case FLOOR_DIV_EXPR:
2992 case ROUND_DIV_EXPR:
2993 case EXACT_DIV_EXPR:
2994 case BIT_AND_EXPR:
2995 case BIT_ANDTC_EXPR:
2996 case BIT_IOR_EXPR:
2997 case BIT_XOR_EXPR:
2998 case TRUNC_MOD_EXPR:
2999 case FLOOR_MOD_EXPR:
3000 case TRUTH_ANDIF_EXPR:
3001 case TRUTH_ORIF_EXPR:
3002 case TRUTH_AND_EXPR:
3003 case TRUTH_OR_EXPR:
3004 case RSHIFT_EXPR:
3005 case LSHIFT_EXPR:
3006 case RROTATE_EXPR:
3007 case LROTATE_EXPR:
3008 case EQ_EXPR:
3009 case NE_EXPR:
3010 case MAX_EXPR:
3011 case MIN_EXPR:
3012 case LE_EXPR:
3013 case GE_EXPR:
3014 case LT_EXPR:
3015 case GT_EXPR:
3016 case MEMBER_REF:
3017 return build_x_binary_op
3018 (TREE_CODE (t),
3019 build_expr_from_tree (TREE_OPERAND (t, 0)),
3020 build_expr_from_tree (TREE_OPERAND (t, 1)));
3022 case DOTSTAR_EXPR:
3023 return build_m_component_ref
3024 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3025 build_expr_from_tree (TREE_OPERAND (t, 1)));
3027 case SCOPE_REF:
3028 return build_offset_ref (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1));
3030 case ARRAY_REF:
3031 if (TREE_OPERAND (t, 0) == NULL_TREE)
3032 /* new-type-id */
3033 return build_nt (ARRAY_REF, NULL_TREE,
3034 build_expr_from_tree (TREE_OPERAND (t, 1)));
3035 return grok_array_decl (build_expr_from_tree (TREE_OPERAND (t, 0)),
3036 build_expr_from_tree (TREE_OPERAND (t, 1)));
3038 case SIZEOF_EXPR:
3039 case ALIGNOF_EXPR:
3041 tree r = build_expr_from_tree (TREE_OPERAND (t, 0));
3042 if (!TYPE_P (r))
3043 return TREE_CODE (t) == SIZEOF_EXPR ? expr_sizeof (r) : c_alignof_expr (r);
3044 else
3045 return cxx_sizeof_or_alignof_type (r, TREE_CODE (t), true);
3048 case MODOP_EXPR:
3049 return build_x_modify_expr
3050 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3051 TREE_CODE (TREE_OPERAND (t, 1)),
3052 build_expr_from_tree (TREE_OPERAND (t, 2)));
3054 case ARROW_EXPR:
3055 return build_x_arrow
3056 (build_expr_from_tree (TREE_OPERAND (t, 0)));
3058 case NEW_EXPR:
3059 return build_new
3060 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3061 build_expr_from_tree (TREE_OPERAND (t, 1)),
3062 build_expr_from_tree (TREE_OPERAND (t, 2)),
3063 NEW_EXPR_USE_GLOBAL (t));
3065 case DELETE_EXPR:
3066 return delete_sanity
3067 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3068 build_expr_from_tree (TREE_OPERAND (t, 1)),
3069 DELETE_EXPR_USE_VEC (t), DELETE_EXPR_USE_GLOBAL (t));
3071 case COMPOUND_EXPR:
3072 if (TREE_OPERAND (t, 1) == NULL_TREE)
3073 return build_x_compound_expr
3074 (build_expr_from_tree (TREE_OPERAND (t, 0)));
3075 else
3076 abort ();
3078 case METHOD_CALL_EXPR:
3079 if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
3081 tree ref = TREE_OPERAND (t, 0);
3082 tree name = TREE_OPERAND (ref, 1);
3084 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3085 name = build_nt (TEMPLATE_ID_EXPR,
3086 TREE_OPERAND (name, 0),
3087 build_expr_from_tree (TREE_OPERAND (name, 1)));
3089 return build_scoped_method_call
3090 (build_expr_from_tree (TREE_OPERAND (t, 1)),
3091 build_expr_from_tree (TREE_OPERAND (ref, 0)),
3092 name,
3093 build_expr_from_tree (TREE_OPERAND (t, 2)));
3095 else
3097 tree fn = TREE_OPERAND (t, 0);
3099 /* We can get a TEMPLATE_ID_EXPR here on code like:
3101 x->f<2>();
3103 so we must resolve that. However, we can also get things
3104 like a BIT_NOT_EXPR here, when referring to a destructor,
3105 and things like that are not correctly resolved by
3106 build_expr_from_tree. So, just use build_expr_from_tree
3107 when we really need it. */
3108 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3109 fn = lookup_template_function
3110 (TREE_OPERAND (fn, 0),
3111 build_expr_from_tree (TREE_OPERAND (fn, 1)));
3113 return build_method_call
3114 (build_expr_from_tree (TREE_OPERAND (t, 1)),
3116 build_expr_from_tree (TREE_OPERAND (t, 2)),
3117 NULL_TREE, LOOKUP_NORMAL);
3120 case CALL_EXPR:
3121 if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
3123 tree ref = TREE_OPERAND (t, 0);
3124 tree name = TREE_OPERAND (ref, 1);
3125 tree fn, scope, args;
3127 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3128 name = build_nt (TEMPLATE_ID_EXPR,
3129 TREE_OPERAND (name, 0),
3130 build_expr_from_tree (TREE_OPERAND (name, 1)));
3132 scope = build_expr_from_tree (TREE_OPERAND (ref, 0));
3133 args = build_expr_from_tree (TREE_OPERAND (t, 1));
3134 fn = resolve_scoped_fn_name (scope, name);
3136 return build_call_from_tree (fn, args, 1);
3138 else
3140 tree name = TREE_OPERAND (t, 0);
3141 tree id;
3142 tree args = build_expr_from_tree (TREE_OPERAND (t, 1));
3143 if (args != NULL_TREE && TREE_CODE (name) == LOOKUP_EXPR
3144 && !LOOKUP_EXPR_GLOBAL (name)
3145 && TREE_CODE ((id = TREE_OPERAND (name, 0))) == IDENTIFIER_NODE
3146 && (!current_class_type
3147 || !lookup_member (current_class_type, id, 0, false)))
3149 /* Do Koenig lookup if there are no class members. */
3150 name = do_identifier (id, args);
3152 else if (TREE_CODE (name) == TEMPLATE_ID_EXPR
3153 || ! really_overloaded_fn (name))
3154 name = build_expr_from_tree (name);
3156 if (TREE_CODE (name) == OFFSET_REF)
3157 return build_offset_ref_call_from_tree (name, args);
3158 if (TREE_CODE (name) == COMPONENT_REF)
3159 return finish_object_call_expr (TREE_OPERAND (name, 1),
3160 TREE_OPERAND (name, 0),
3161 args);
3162 name = convert_from_reference (name);
3163 return build_call_from_tree (name, args,
3164 /*disallow_virtual=*/false);
3167 case COND_EXPR:
3168 return build_x_conditional_expr
3169 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3170 build_expr_from_tree (TREE_OPERAND (t, 1)),
3171 build_expr_from_tree (TREE_OPERAND (t, 2)));
3173 case PSEUDO_DTOR_EXPR:
3174 return (finish_pseudo_destructor_expr
3175 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3176 build_expr_from_tree (TREE_OPERAND (t, 1)),
3177 build_expr_from_tree (TREE_OPERAND (t, 2))));
3179 case TREE_LIST:
3181 tree purpose, value, chain;
3183 if (t == void_list_node)
3184 return t;
3186 purpose = TREE_PURPOSE (t);
3187 if (purpose)
3188 purpose = build_expr_from_tree (purpose);
3189 value = TREE_VALUE (t);
3190 if (value)
3191 value = build_expr_from_tree (value);
3192 chain = TREE_CHAIN (t);
3193 if (chain && chain != void_type_node)
3194 chain = build_expr_from_tree (chain);
3195 return tree_cons (purpose, value, chain);
3198 case COMPONENT_REF:
3200 tree object = build_expr_from_tree (TREE_OPERAND (t, 0));
3201 tree member = TREE_OPERAND (t, 1);
3203 if (!CLASS_TYPE_P (TREE_TYPE (object)))
3205 if (TREE_CODE (member) == BIT_NOT_EXPR)
3206 return finish_pseudo_destructor_expr (object,
3207 NULL_TREE,
3208 TREE_TYPE (object));
3209 else if (TREE_CODE (member) == SCOPE_REF
3210 && (TREE_CODE (TREE_OPERAND (member, 1)) == BIT_NOT_EXPR))
3211 return finish_pseudo_destructor_expr (object,
3212 TREE_OPERAND (t, 0),
3213 TREE_TYPE (object));
3215 else if (TREE_CODE (member) == SCOPE_REF
3216 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
3218 tree tmpl;
3219 tree args;
3221 /* Lookup the template functions now that we know what the
3222 scope is. */
3223 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
3224 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
3225 member = lookup_qualified_name (TREE_OPERAND (member, 0),
3226 tmpl,
3227 /*is_type=*/0,
3228 /*flags=*/0);
3229 if (BASELINK_P (member))
3230 BASELINK_FUNCTIONS (member)
3231 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
3232 args);
3233 else
3235 error ("`%D' is not a member of `%T'",
3236 tmpl, TREE_TYPE (object));
3237 return error_mark_node;
3242 return finish_class_member_access_expr (object, member);
3245 case THROW_EXPR:
3246 return build_throw (build_expr_from_tree (TREE_OPERAND (t, 0)));
3248 case CONSTRUCTOR:
3250 tree r;
3251 tree elts;
3252 tree type = TREE_TYPE (t);
3253 bool purpose_p;
3255 /* digest_init will do the wrong thing if we let it. */
3256 if (type && TYPE_PTRMEMFUNC_P (type))
3257 return t;
3259 r = NULL_TREE;
3260 /* We do not want to process the purpose of aggregate
3261 initializers as they are identifier nodes which will be
3262 looked up by digest_init. */
3263 purpose_p = !(type && IS_AGGR_TYPE (type));
3264 for (elts = CONSTRUCTOR_ELTS (t); elts; elts = TREE_CHAIN (elts))
3266 tree purpose = TREE_PURPOSE (elts);
3267 tree value = TREE_VALUE (elts);
3269 if (purpose && purpose_p)
3270 purpose = build_expr_from_tree (purpose);
3271 value = build_expr_from_tree (value);
3272 r = tree_cons (purpose, value, r);
3275 r = build_nt (CONSTRUCTOR, NULL_TREE, nreverse (r));
3276 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
3278 if (type)
3279 return digest_init (type, r, 0);
3280 return r;
3283 case TYPEID_EXPR:
3284 if (TYPE_P (TREE_OPERAND (t, 0)))
3285 return get_typeid (TREE_OPERAND (t, 0));
3286 return build_typeid (build_expr_from_tree (TREE_OPERAND (t, 0)));
3288 case PARM_DECL:
3289 case VAR_DECL:
3290 return convert_from_reference (t);
3292 case VA_ARG_EXPR:
3293 return build_va_arg (build_expr_from_tree (TREE_OPERAND (t, 0)),
3294 TREE_TYPE (t));
3296 default:
3297 return t;
3301 /* FN is an OFFSET_REF indicating the function to call in parse-tree
3302 form; it has not yet been semantically analyzed. ARGS are the
3303 arguments to the function. They have already been semantically
3304 analzyed. */
3306 tree
3307 build_offset_ref_call_from_tree (tree fn, tree args)
3309 tree object_addr;
3311 my_friendly_assert (TREE_CODE (fn) == OFFSET_REF, 20020725);
3313 /* A qualified name corresponding to a non-static member
3314 function or a pointer-to-member is represented as an
3315 OFFSET_REF.
3317 For both of these function calls, FN will be an OFFSET_REF.
3319 struct A { void f(); };
3320 void A::f() { (A::f) (); }
3322 struct B { void g(); };
3323 void (B::*p)();
3324 void B::g() { (this->*p)(); } */
3326 /* This code is not really correct (for example, it does not
3327 handle the case that `A::f' is overloaded), but it is
3328 historically how we have handled this situation. */
3329 if (TREE_CODE (TREE_OPERAND (fn, 1)) == FIELD_DECL)
3330 /* This case should now be handled elsewhere. */
3331 abort ();
3332 else
3334 object_addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (fn, 0), 0);
3335 fn = TREE_OPERAND (fn, 1);
3336 fn = get_member_function_from_ptrfunc (&object_addr, fn);
3337 args = tree_cons (NULL_TREE, object_addr, args);
3339 return build_function_call (fn, args);
3342 /* FN indicates the function to call. Name resolution has been
3343 performed on FN. ARGS are the arguments to the function. They
3344 have already been semantically analyzed. DISALLOW_VIRTUAL is true
3345 if the function call should be determined at compile time, even if
3346 FN is virtual. */
3348 tree
3349 build_call_from_tree (tree fn, tree args, bool disallow_virtual)
3351 tree template_args;
3352 tree template_id;
3353 tree f;
3355 /* Check to see that name lookup has already been performed. */
3356 my_friendly_assert (TREE_CODE (fn) != OFFSET_REF, 20020725);
3357 my_friendly_assert (TREE_CODE (fn) != SCOPE_REF, 20020725);
3359 /* In the future all of this should be eliminated. Instead,
3360 name-lookup for a member function should simply return a
3361 baselink, instead of a FUNCTION_DECL, TEMPLATE_DECL, or
3362 TEMPLATE_ID_EXPR. */
3364 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3366 template_id = fn;
3367 template_args = TREE_OPERAND (fn, 1);
3368 fn = TREE_OPERAND (fn, 0);
3370 else
3372 template_id = NULL_TREE;
3373 template_args = NULL_TREE;
3376 f = (TREE_CODE (fn) == OVERLOAD) ? get_first_fn (fn) : fn;
3377 /* Make sure we have a baselink (rather than simply a
3378 FUNCTION_DECL) for a member function. */
3379 if (current_class_type
3380 && ((TREE_CODE (f) == FUNCTION_DECL
3381 && DECL_FUNCTION_MEMBER_P (f))
3382 || (DECL_FUNCTION_TEMPLATE_P (f)
3383 && DECL_FUNCTION_MEMBER_P (f))))
3385 f = lookup_member (current_class_type, DECL_NAME (f),
3386 /*protect=*/1, /*want_type=*/false);
3387 if (f)
3388 fn = f;
3391 if (template_id)
3393 if (BASELINK_P (fn))
3394 BASELINK_FUNCTIONS (fn) = build_nt (TEMPLATE_ID_EXPR,
3395 BASELINK_FUNCTIONS (fn),
3396 template_args);
3397 else
3398 fn = template_id;
3401 return finish_call_expr (fn, args, disallow_virtual);
3404 /* Returns true if ROOT (a namespace, class, or function) encloses
3405 CHILD. CHILD may be either a class type or a namespace. */
3407 bool
3408 is_ancestor (tree root, tree child)
3410 my_friendly_assert ((TREE_CODE (root) == NAMESPACE_DECL
3411 || TREE_CODE (root) == FUNCTION_DECL
3412 || CLASS_TYPE_P (root)), 20030307);
3413 my_friendly_assert ((TREE_CODE (child) == NAMESPACE_DECL
3414 || CLASS_TYPE_P (child)),
3415 20030307);
3417 /* The global namespace encloses everything. */
3418 if (root == global_namespace)
3419 return true;
3421 while (true)
3423 /* If we've run out of scopes, stop. */
3424 if (!child)
3425 return false;
3426 /* If we've reached the ROOT, it encloses CHILD. */
3427 if (root == child)
3428 return true;
3429 /* Go out one level. */
3430 if (TYPE_P (child))
3431 child = TYPE_NAME (child);
3432 child = DECL_CONTEXT (child);
3437 /* Return the namespace that is the common ancestor
3438 of two given namespaces. */
3440 tree
3441 namespace_ancestor (tree ns1, tree ns2)
3443 timevar_push (TV_NAME_LOOKUP);
3444 if (is_ancestor (ns1, ns2))
3445 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ns1);
3446 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
3447 namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2));
3450 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
3451 directive is not directly from the source. Also find the common
3452 ancestor and let our users know about the new namespace */
3453 static void
3454 add_using_namespace (tree user, tree used, bool indirect)
3456 tree t;
3457 timevar_push (TV_NAME_LOOKUP);
3458 /* Using oneself is a no-op. */
3459 if (user == used)
3461 timevar_pop (TV_NAME_LOOKUP);
3462 return;
3464 my_friendly_assert (TREE_CODE (user) == NAMESPACE_DECL, 380);
3465 my_friendly_assert (TREE_CODE (used) == NAMESPACE_DECL, 380);
3466 /* Check if we already have this. */
3467 t = purpose_member (used, DECL_NAMESPACE_USING (user));
3468 if (t != NULL_TREE)
3470 if (!indirect)
3471 /* Promote to direct usage. */
3472 TREE_INDIRECT_USING (t) = 0;
3473 timevar_pop (TV_NAME_LOOKUP);
3474 return;
3477 /* Add used to the user's using list. */
3478 DECL_NAMESPACE_USING (user)
3479 = tree_cons (used, namespace_ancestor (user, used),
3480 DECL_NAMESPACE_USING (user));
3482 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
3484 /* Add user to the used's users list. */
3485 DECL_NAMESPACE_USERS (used)
3486 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
3488 /* Recursively add all namespaces used. */
3489 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
3490 /* indirect usage */
3491 add_using_namespace (user, TREE_PURPOSE (t), 1);
3493 /* Tell everyone using us about the new used namespaces. */
3494 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
3495 add_using_namespace (TREE_PURPOSE (t), used, 1);
3496 timevar_pop (TV_NAME_LOOKUP);
3499 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
3500 duplicates. The first list becomes the tail of the result.
3502 The algorithm is O(n^2). We could get this down to O(n log n) by
3503 doing a sort on the addresses of the functions, if that becomes
3504 necessary. */
3506 static tree
3507 merge_functions (tree s1, tree s2)
3509 for (; s2; s2 = OVL_NEXT (s2))
3511 tree fn2 = OVL_CURRENT (s2);
3512 tree fns1;
3514 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
3516 tree fn1 = OVL_CURRENT (fns1);
3518 /* If the function from S2 is already in S1, there is no
3519 need to add it again. For `extern "C"' functions, we
3520 might have two FUNCTION_DECLs for the same function, in
3521 different namespaces; again, we only need one of them. */
3522 if (fn1 == fn2
3523 || (DECL_EXTERN_C_P (fn1) && DECL_EXTERN_C_P (fn2)
3524 && DECL_NAME (fn1) == DECL_NAME (fn2)))
3525 break;
3528 /* If we exhausted all of the functions in S1, FN2 is new. */
3529 if (!fns1)
3530 s1 = build_overload (fn2, s1);
3532 return s1;
3535 /* This should return an error not all definitions define functions.
3536 It is not an error if we find two functions with exactly the
3537 same signature, only if these are selected in overload resolution.
3538 old is the current set of bindings, new the freshly-found binding.
3539 XXX Do we want to give *all* candidates in case of ambiguity?
3540 XXX In what way should I treat extern declarations?
3541 XXX I don't want to repeat the entire duplicate_decls here */
3543 static cxx_binding *
3544 ambiguous_decl (tree name, cxx_binding *old, cxx_binding *new, int flags)
3546 tree val, type;
3547 my_friendly_assert (old != NULL, 393);
3548 /* Copy the value. */
3549 val = BINDING_VALUE (new);
3550 if (val)
3551 switch (TREE_CODE (val))
3553 case TEMPLATE_DECL:
3554 /* If we expect types or namespaces, and not templates,
3555 or this is not a template class. */
3556 if (LOOKUP_QUALIFIERS_ONLY (flags)
3557 && !DECL_CLASS_TEMPLATE_P (val))
3558 val = NULL_TREE;
3559 break;
3560 case TYPE_DECL:
3561 if (LOOKUP_NAMESPACES_ONLY (flags))
3562 val = NULL_TREE;
3563 break;
3564 case NAMESPACE_DECL:
3565 if (LOOKUP_TYPES_ONLY (flags))
3566 val = NULL_TREE;
3567 break;
3568 case FUNCTION_DECL:
3569 /* Ignore built-in functions that are still anticipated. */
3570 if (LOOKUP_QUALIFIERS_ONLY (flags) || DECL_ANTICIPATED (val))
3571 val = NULL_TREE;
3572 break;
3573 default:
3574 if (LOOKUP_QUALIFIERS_ONLY (flags))
3575 val = NULL_TREE;
3578 if (!BINDING_VALUE (old))
3579 BINDING_VALUE (old) = val;
3580 else if (val && val != BINDING_VALUE (old))
3582 if (is_overloaded_fn (BINDING_VALUE (old)) && is_overloaded_fn (val))
3583 BINDING_VALUE (old) = merge_functions (BINDING_VALUE (old), val);
3584 else
3586 /* Some declarations are functions, some are not. */
3587 if (flags & LOOKUP_COMPLAIN)
3589 /* If we've already given this error for this lookup,
3590 BINDING_VALUE (old) is error_mark_node, so let's not
3591 repeat ourselves. */
3592 if (BINDING_VALUE (old) != error_mark_node)
3594 error ("use of `%D' is ambiguous", name);
3595 cp_error_at (" first declared as `%#D' here",
3596 BINDING_VALUE (old));
3598 cp_error_at (" also declared as `%#D' here", val);
3600 BINDING_VALUE (old) = error_mark_node;
3603 /* ... and copy the type. */
3604 type = BINDING_TYPE (new);
3605 if (LOOKUP_NAMESPACES_ONLY (flags))
3606 type = NULL_TREE;
3607 if (!BINDING_TYPE (old))
3608 BINDING_TYPE (old) = type;
3609 else if (type && BINDING_TYPE (old) != type)
3611 if (flags & LOOKUP_COMPLAIN)
3613 error ("`%D' denotes an ambiguous type",name);
3614 cp_error_at (" first type here", BINDING_TYPE (old));
3615 cp_error_at (" other type here", type);
3618 return old;
3621 /* Subroutine of unualified_namespace_lookup:
3622 Add the bindings of NAME in used namespaces to VAL.
3623 We are currently looking for names in namespace SCOPE, so we
3624 look through USINGS for using-directives of namespaces
3625 which have SCOPE as a common ancestor with the current scope.
3626 Returns false on errors. */
3628 bool
3629 lookup_using_namespace (tree name, cxx_binding *val, tree usings, tree scope,
3630 int flags, tree *spacesp)
3632 tree iter;
3633 timevar_push (TV_NAME_LOOKUP);
3634 /* Iterate over all used namespaces in current, searching for using
3635 directives of scope. */
3636 for (iter = usings; iter; iter = TREE_CHAIN (iter))
3637 if (TREE_VALUE (iter) == scope)
3639 tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
3640 cxx_binding *val1 =
3641 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (used), name);
3642 if (spacesp)
3643 *spacesp = tree_cons (used, NULL_TREE, *spacesp);
3644 /* Resolve ambiguities. */
3645 if (val1)
3646 val = ambiguous_decl (name, val, val1, flags);
3648 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
3649 BINDING_VALUE (val) != error_mark_node);
3652 /* [namespace.qual]
3653 Accepts the NAME to lookup and its qualifying SCOPE.
3654 Returns the name/type pair found into the cxx_binding *RESULT,
3655 or false on error. */
3657 bool
3658 qualified_lookup_using_namespace (tree name, tree scope, cxx_binding *result,
3659 int flags)
3661 /* Maintain a list of namespaces visited... */
3662 tree seen = NULL_TREE;
3663 /* ... and a list of namespace yet to see. */
3664 tree todo = NULL_TREE;
3665 tree usings;
3666 timevar_push (TV_NAME_LOOKUP);
3667 /* Look through namespace aliases. */
3668 scope = ORIGINAL_NAMESPACE (scope);
3669 while (scope && result->value != error_mark_node)
3671 cxx_binding *binding =
3672 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3673 seen = tree_cons (scope, NULL_TREE, seen);
3674 if (binding)
3675 result = ambiguous_decl (name, result, binding, flags);
3676 if (!BINDING_VALUE (result) && !BINDING_TYPE (result))
3677 /* Consider using directives. */
3678 for (usings = DECL_NAMESPACE_USING (scope); usings;
3679 usings = TREE_CHAIN (usings))
3680 /* If this was a real directive, and we have not seen it. */
3681 if (!TREE_INDIRECT_USING (usings)
3682 && !purpose_member (TREE_PURPOSE (usings), seen))
3683 todo = tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo);
3684 if (todo)
3686 scope = TREE_PURPOSE (todo);
3687 todo = TREE_CHAIN (todo);
3689 else
3690 scope = NULL_TREE; /* If there never was a todo list. */
3692 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result->value != error_mark_node);
3695 /* [namespace.memdef]/2 */
3697 /* Set the context of a declaration to scope. Complain if we are not
3698 outside scope. */
3700 void
3701 set_decl_namespace (tree decl, tree scope, bool friendp)
3703 tree old;
3705 /* Get rid of namespace aliases. */
3706 scope = ORIGINAL_NAMESPACE (scope);
3708 /* It is ok for friends to be qualified in parallel space. */
3709 if (!friendp && !is_ancestor (current_namespace, scope))
3710 error ("declaration of `%D' not in a namespace surrounding `%D'",
3711 decl, scope);
3712 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3713 if (scope != current_namespace)
3715 /* See whether this has been declared in the namespace. */
3716 old = namespace_binding (DECL_NAME (decl), scope);
3717 if (!old)
3718 /* No old declaration at all. */
3719 goto complain;
3720 /* A template can be explicitly specialized in any namespace. */
3721 if (processing_explicit_instantiation)
3722 return;
3723 if (!is_overloaded_fn (decl))
3724 /* Don't compare non-function decls with decls_match here,
3725 since it can't check for the correct constness at this
3726 point. pushdecl will find those errors later. */
3727 return;
3728 /* Since decl is a function, old should contain a function decl. */
3729 if (!is_overloaded_fn (old))
3730 goto complain;
3731 if (processing_template_decl || processing_specialization)
3732 /* We have not yet called push_template_decl to turn a
3733 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations
3734 won't match. But, we'll check later, when we construct the
3735 template. */
3736 return;
3737 if (is_overloaded_fn (old))
3739 for (; old; old = OVL_NEXT (old))
3740 if (decls_match (decl, OVL_CURRENT (old)))
3741 return;
3743 else
3744 if (decls_match (decl, old))
3745 return;
3747 else
3748 return;
3749 complain:
3750 error ("`%D' should have been declared inside `%D'",
3751 decl, scope);
3754 /* Compute the namespace where a declaration is defined. */
3756 static tree
3757 decl_namespace (tree decl)
3759 timevar_push (TV_NAME_LOOKUP);
3760 if (TYPE_P (decl))
3761 decl = TYPE_STUB_DECL (decl);
3762 while (DECL_CONTEXT (decl))
3764 decl = DECL_CONTEXT (decl);
3765 if (TREE_CODE (decl) == NAMESPACE_DECL)
3766 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
3767 if (TYPE_P (decl))
3768 decl = TYPE_STUB_DECL (decl);
3769 my_friendly_assert (DECL_P (decl), 390);
3772 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, global_namespace);
3775 /* Return the namespace where the current declaration is declared. */
3777 tree
3778 current_decl_namespace (void)
3780 tree result;
3781 /* If we have been pushed into a different namespace, use it. */
3782 if (decl_namespace_list)
3783 return TREE_PURPOSE (decl_namespace_list);
3785 if (current_class_type)
3786 result = decl_namespace (TYPE_STUB_DECL (current_class_type));
3787 else if (current_function_decl)
3788 result = decl_namespace (current_function_decl);
3789 else
3790 result = current_namespace;
3791 return result;
3794 /* Temporarily set the namespace for the current declaration. */
3796 void
3797 push_decl_namespace (tree decl)
3799 if (TREE_CODE (decl) != NAMESPACE_DECL)
3800 decl = decl_namespace (decl);
3801 decl_namespace_list = tree_cons (ORIGINAL_NAMESPACE (decl),
3802 NULL_TREE, decl_namespace_list);
3805 void
3806 pop_decl_namespace (void)
3808 decl_namespace_list = TREE_CHAIN (decl_namespace_list);
3811 /* Enter a class or namespace scope. */
3813 void
3814 push_scope (tree t)
3816 if (TREE_CODE (t) == NAMESPACE_DECL)
3817 push_decl_namespace (t);
3818 else if CLASS_TYPE_P (t)
3819 push_nested_class (t);
3822 /* Leave scope pushed by push_scope. */
3824 void
3825 pop_scope (tree t)
3827 if (TREE_CODE (t) == NAMESPACE_DECL)
3828 pop_decl_namespace ();
3829 else if CLASS_TYPE_P (t)
3830 pop_nested_class ();
3833 /* [basic.lookup.koenig] */
3834 /* A nonzero return value in the functions below indicates an error. */
3836 struct arg_lookup
3838 tree name;
3839 tree namespaces;
3840 tree classes;
3841 tree functions;
3844 static bool arg_assoc (struct arg_lookup*, tree);
3845 static bool arg_assoc_args (struct arg_lookup*, tree);
3846 static bool arg_assoc_type (struct arg_lookup*, tree);
3847 static bool add_function (struct arg_lookup *, tree);
3848 static bool arg_assoc_namespace (struct arg_lookup *, tree);
3849 static bool arg_assoc_class (struct arg_lookup *, tree);
3850 static bool arg_assoc_template_arg (struct arg_lookup*, tree);
3852 /* Add a function to the lookup structure.
3853 Returns true on error. */
3855 static bool
3856 add_function (struct arg_lookup *k, tree fn)
3858 /* We used to check here to see if the function was already in the list,
3859 but that's O(n^2), which is just too expensive for function lookup.
3860 Now we deal with the occasional duplicate in joust. In doing this, we
3861 assume that the number of duplicates will be small compared to the
3862 total number of functions being compared, which should usually be the
3863 case. */
3865 /* We must find only functions, or exactly one non-function. */
3866 if (!k->functions)
3867 k->functions = fn;
3868 else if (is_overloaded_fn (k->functions) && is_overloaded_fn (fn))
3869 k->functions = build_overload (fn, k->functions);
3870 else
3872 tree f1 = OVL_CURRENT (k->functions);
3873 tree f2 = fn;
3874 if (is_overloaded_fn (f1))
3876 fn = f1; f1 = f2; f2 = fn;
3878 cp_error_at ("`%D' is not a function,", f1);
3879 cp_error_at (" conflict with `%D'", f2);
3880 error (" in call to `%D'", k->name);
3881 return true;
3884 return false;
3887 /* Add functions of a namespace to the lookup structure.
3888 Returns true on error. */
3890 static bool
3891 arg_assoc_namespace (struct arg_lookup *k, tree scope)
3893 tree value;
3895 if (purpose_member (scope, k->namespaces))
3896 return 0;
3897 k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces);
3899 value = namespace_binding (k->name, scope);
3900 if (!value)
3901 return false;
3903 for (; value; value = OVL_NEXT (value))
3904 if (add_function (k, OVL_CURRENT (value)))
3905 return true;
3907 return false;
3910 /* Adds everything associated with a template argument to the lookup
3911 structure. Returns true on error. */
3913 static bool
3914 arg_assoc_template_arg (struct arg_lookup *k, tree arg)
3916 /* [basic.lookup.koenig]
3918 If T is a template-id, its associated namespaces and classes are
3919 ... the namespaces and classes associated with the types of the
3920 template arguments provided for template type parameters
3921 (excluding template template parameters); the namespaces in which
3922 any template template arguments are defined; and the classes in
3923 which any member templates used as template template arguments
3924 are defined. [Note: non-type template arguments do not
3925 contribute to the set of associated namespaces. ] */
3927 /* Consider first template template arguments. */
3928 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3929 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
3930 return false;
3931 else if (TREE_CODE (arg) == TEMPLATE_DECL)
3933 tree ctx = CP_DECL_CONTEXT (arg);
3935 /* It's not a member template. */
3936 if (TREE_CODE (ctx) == NAMESPACE_DECL)
3937 return arg_assoc_namespace (k, ctx);
3938 /* Otherwise, it must be member template. */
3939 else
3940 return arg_assoc_class (k, ctx);
3942 /* It's not a template template argument, but it is a type template
3943 argument. */
3944 else if (TYPE_P (arg))
3945 return arg_assoc_type (k, arg);
3946 /* It's a non-type template argument. */
3947 else
3948 return false;
3951 /* Adds everything associated with class to the lookup structure.
3952 Returns true on error. */
3954 static bool
3955 arg_assoc_class (struct arg_lookup *k, tree type)
3957 tree list, friends, context;
3958 int i;
3960 /* Backend build structures, such as __builtin_va_list, aren't
3961 affected by all this. */
3962 if (!CLASS_TYPE_P (type))
3963 return false;
3965 if (purpose_member (type, k->classes))
3966 return false;
3967 k->classes = tree_cons (type, NULL_TREE, k->classes);
3969 context = decl_namespace (TYPE_MAIN_DECL (type));
3970 if (arg_assoc_namespace (k, context))
3971 return true;
3973 /* Process baseclasses. */
3974 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); i++)
3975 if (arg_assoc_class (k, TYPE_BINFO_BASETYPE (type, i)))
3976 return true;
3978 /* Process friends. */
3979 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
3980 list = TREE_CHAIN (list))
3981 if (k->name == FRIEND_NAME (list))
3982 for (friends = FRIEND_DECLS (list); friends;
3983 friends = TREE_CHAIN (friends))
3984 /* Only interested in global functions with potentially hidden
3985 (i.e. unqualified) declarations. */
3986 if (CP_DECL_CONTEXT (TREE_VALUE (friends)) == context)
3987 if (add_function (k, TREE_VALUE (friends)))
3988 return true;
3990 /* Process template arguments. */
3991 if (CLASSTYPE_TEMPLATE_INFO (type))
3993 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
3994 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
3995 arg_assoc_template_arg (k, TREE_VEC_ELT (list, i));
3998 return false;
4001 /* Adds everything associated with a given type.
4002 Returns 1 on error. */
4004 static bool
4005 arg_assoc_type (struct arg_lookup *k, tree type)
4007 switch (TREE_CODE (type))
4009 case ERROR_MARK:
4010 return false;
4011 case VOID_TYPE:
4012 case INTEGER_TYPE:
4013 case REAL_TYPE:
4014 case COMPLEX_TYPE:
4015 case VECTOR_TYPE:
4016 case CHAR_TYPE:
4017 case BOOLEAN_TYPE:
4018 return false;
4019 case RECORD_TYPE:
4020 if (TYPE_PTRMEMFUNC_P (type))
4021 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
4022 return arg_assoc_class (k, type);
4023 case POINTER_TYPE:
4024 case REFERENCE_TYPE:
4025 case ARRAY_TYPE:
4026 return arg_assoc_type (k, TREE_TYPE (type));
4027 case UNION_TYPE:
4028 case ENUMERAL_TYPE:
4029 return arg_assoc_namespace (k, decl_namespace (TYPE_MAIN_DECL (type)));
4030 case OFFSET_TYPE:
4031 /* Pointer to member: associate class type and value type. */
4032 if (arg_assoc_type (k, TYPE_OFFSET_BASETYPE (type)))
4033 return true;
4034 return arg_assoc_type (k, TREE_TYPE (type));
4035 case METHOD_TYPE:
4036 /* The basetype is referenced in the first arg type, so just
4037 fall through. */
4038 case FUNCTION_TYPE:
4039 /* Associate the parameter types. */
4040 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
4041 return true;
4042 /* Associate the return type. */
4043 return arg_assoc_type (k, TREE_TYPE (type));
4044 case TEMPLATE_TYPE_PARM:
4045 case BOUND_TEMPLATE_TEMPLATE_PARM:
4046 return false;
4047 case TYPENAME_TYPE:
4048 return false;
4049 case LANG_TYPE:
4050 if (type == unknown_type_node)
4051 return false;
4052 /* else fall through */
4053 default:
4054 abort ();
4056 return false;
4059 /* Adds everything associated with arguments. Returns true on error. */
4061 static bool
4062 arg_assoc_args (struct arg_lookup *k, tree args)
4064 for (; args; args = TREE_CHAIN (args))
4065 if (arg_assoc (k, TREE_VALUE (args)))
4066 return true;
4067 return false;
4070 /* Adds everything associated with a given tree_node. Returns 1 on error. */
4072 static bool
4073 arg_assoc (struct arg_lookup *k, tree n)
4075 if (n == error_mark_node)
4076 return false;
4078 if (TYPE_P (n))
4079 return arg_assoc_type (k, n);
4081 if (! type_unknown_p (n))
4082 return arg_assoc_type (k, TREE_TYPE (n));
4084 if (TREE_CODE (n) == ADDR_EXPR)
4085 n = TREE_OPERAND (n, 0);
4086 if (TREE_CODE (n) == COMPONENT_REF)
4087 n = TREE_OPERAND (n, 1);
4088 if (TREE_CODE (n) == OFFSET_REF)
4089 n = TREE_OPERAND (n, 1);
4090 while (TREE_CODE (n) == TREE_LIST)
4091 n = TREE_VALUE (n);
4092 if (TREE_CODE (n) == BASELINK)
4093 n = BASELINK_FUNCTIONS (n);
4095 if (TREE_CODE (n) == FUNCTION_DECL)
4096 return arg_assoc_type (k, TREE_TYPE (n));
4097 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
4099 /* [basic.lookup.koenig]
4101 If T is a template-id, its associated namespaces and classes
4102 are the namespace in which the template is defined; for
4103 member templates, the member template's class... */
4104 tree template = TREE_OPERAND (n, 0);
4105 tree args = TREE_OPERAND (n, 1);
4106 tree ctx;
4107 tree arg;
4109 if (TREE_CODE (template) == COMPONENT_REF)
4110 template = TREE_OPERAND (template, 1);
4112 /* First, the template. There may actually be more than one if
4113 this is an overloaded function template. But, in that case,
4114 we only need the first; all the functions will be in the same
4115 namespace. */
4116 template = OVL_CURRENT (template);
4118 ctx = CP_DECL_CONTEXT (template);
4120 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4122 if (arg_assoc_namespace (k, ctx) == 1)
4123 return true;
4125 /* It must be a member template. */
4126 else if (arg_assoc_class (k, ctx) == 1)
4127 return true;
4129 /* Now the arguments. */
4130 for (arg = args; arg != NULL_TREE; arg = TREE_CHAIN (arg))
4131 if (arg_assoc_template_arg (k, TREE_VALUE (arg)) == 1)
4132 return true;
4134 else
4136 my_friendly_assert (TREE_CODE (n) == OVERLOAD, 980715);
4138 for (; n; n = OVL_CHAIN (n))
4139 if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
4140 return true;
4143 return false;
4146 /* Performs Koenig lookup depending on arguments, where fns
4147 are the functions found in normal lookup. */
4149 tree
4150 lookup_arg_dependent (tree name, tree fns, tree args)
4152 struct arg_lookup k;
4153 tree fn = NULL_TREE;
4155 timevar_push (TV_NAME_LOOKUP);
4156 k.name = name;
4157 k.functions = fns;
4158 k.classes = NULL_TREE;
4160 /* Note that we've already looked at some namespaces during normal
4161 unqualified lookup, unless we found a decl in function scope. */
4162 if (fns)
4163 fn = OVL_CURRENT (fns);
4164 if (fn && TREE_CODE (fn) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (fn))
4165 k.namespaces = NULL_TREE;
4166 else
4167 unqualified_namespace_lookup (name, 0, &k.namespaces);
4169 arg_assoc_args (&k, args);
4170 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, k.functions);
4173 /* Process a namespace-alias declaration. */
4175 void
4176 do_namespace_alias (tree alias, tree namespace)
4178 if (TREE_CODE (namespace) != NAMESPACE_DECL)
4180 /* The parser did not find it, so it's not there. */
4181 error ("unknown namespace `%D'", namespace);
4182 return;
4185 namespace = ORIGINAL_NAMESPACE (namespace);
4187 /* Build the alias. */
4188 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
4189 DECL_NAMESPACE_ALIAS (alias) = namespace;
4190 DECL_EXTERNAL (alias) = 1;
4191 pushdecl (alias);
4194 /* Check a non-member using-declaration. Return the name and scope
4195 being used, and the USING_DECL, or NULL_TREE on failure. */
4197 static tree
4198 validate_nonmember_using_decl (tree decl, tree *scope, tree *name)
4200 *scope = global_namespace;
4201 *name = NULL_TREE;
4203 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
4205 *name = TREE_OPERAND (decl, 0);
4206 /* 7.3.3/5
4207 A using-declaration shall not name a template-id. */
4208 error ("a using-declaration cannot specify a template-id. Try `using %D'", *name);
4209 return NULL_TREE;
4212 if (TREE_CODE (decl) == NAMESPACE_DECL)
4214 error ("namespace `%D' not allowed in using-declaration", decl);
4215 return NULL_TREE;
4218 if (is_overloaded_fn (decl))
4219 decl = get_first_fn (decl);
4221 my_friendly_assert (DECL_P (decl), 20020908);
4223 if (TREE_CODE (decl) == CONST_DECL)
4224 /* Enumeration constants to not have DECL_CONTEXT set. */
4225 *scope = TYPE_CONTEXT (TREE_TYPE (decl));
4226 else
4227 *scope = DECL_CONTEXT (decl);
4228 if (!*scope)
4229 *scope = global_namespace;
4231 /* [namespace.udecl]
4232 A using-declaration for a class member shall be a
4233 member-declaration. */
4234 if (TYPE_P (*scope))
4236 error ("`%T' is not a namespace", *scope);
4237 return NULL_TREE;
4239 *name = DECL_NAME (decl);
4240 /* Make a USING_DECL. */
4241 return push_using_decl (*scope, *name);
4244 /* Process local and global using-declarations. */
4246 static void
4247 do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
4248 tree *newval, tree *newtype)
4250 cxx_binding decls;
4252 *newval = *newtype = NULL_TREE;
4253 cxx_binding_clear (&decls);
4254 if (!qualified_lookup_using_namespace (name, scope, &decls, 0))
4255 /* Lookup error */
4256 return;
4258 if (!decls.value && !decls.type)
4260 error ("`%D' not declared", name);
4261 return;
4264 /* Check for using functions. */
4265 if (decls.value && is_overloaded_fn (decls.value))
4267 tree tmp, tmp1;
4269 if (oldval && !is_overloaded_fn (oldval))
4271 if (!DECL_IMPLICIT_TYPEDEF_P (oldval))
4272 error ("`%D' is already declared in this scope", name);
4273 oldval = NULL_TREE;
4276 *newval = oldval;
4277 for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
4279 tree new_fn = OVL_CURRENT (tmp);
4281 /* [namespace.udecl]
4283 If a function declaration in namespace scope or block
4284 scope has the same name and the same parameter types as a
4285 function introduced by a using declaration the program is
4286 ill-formed. */
4287 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
4289 tree old_fn = OVL_CURRENT (tmp1);
4291 if (new_fn == old_fn)
4292 /* The function already exists in the current namespace. */
4293 break;
4294 else if (OVL_USED (tmp1))
4295 continue; /* this is a using decl */
4296 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
4297 TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
4299 /* There was already a non-using declaration in
4300 this scope with the same parameter types. If both
4301 are the same extern "C" functions, that's ok. */
4302 if (decls_match (new_fn, old_fn))
4304 /* If the OLD_FN was a builtin, there is now a
4305 real declaration. */
4306 if (DECL_ANTICIPATED (old_fn))
4307 DECL_ANTICIPATED (old_fn) = 0;
4308 break;
4310 else if (!DECL_ANTICIPATED (old_fn))
4312 /* If the OLD_FN was really declared, the
4313 declarations don't match. */
4314 error ("`%D' is already declared in this scope", name);
4315 break;
4318 /* If the OLD_FN was not really there, just ignore
4319 it and keep going. */
4323 /* If we broke out of the loop, there's no reason to add
4324 this function to the using declarations for this
4325 scope. */
4326 if (tmp1)
4327 continue;
4329 *newval = build_overload (OVL_CURRENT (tmp), *newval);
4330 if (TREE_CODE (*newval) != OVERLOAD)
4331 *newval = ovl_cons (*newval, NULL_TREE);
4332 OVL_USED (*newval) = 1;
4335 else
4337 *newval = decls.value;
4338 if (oldval && !decls_match (*newval, oldval))
4339 error ("`%D' is already declared in this scope", name);
4342 *newtype = decls.type;
4343 if (oldtype && *newtype && !same_type_p (oldtype, *newtype))
4345 error ("using declaration `%D' introduced ambiguous type `%T'",
4346 name, oldtype);
4347 return;
4351 /* Process a using-declaration not appearing in class or local scope. */
4353 void
4354 do_toplevel_using_decl (tree decl)
4356 tree scope, name;
4357 tree oldval, oldtype, newval, newtype;
4358 cxx_binding *binding;
4360 decl = validate_nonmember_using_decl (decl, &scope, &name);
4361 if (decl == NULL_TREE)
4362 return;
4364 binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name);
4366 oldval = BINDING_VALUE (binding);
4367 oldtype = BINDING_TYPE (binding);
4369 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
4371 /* Copy declarations found. */
4372 if (newval)
4373 BINDING_VALUE (binding) = newval;
4374 if (newtype)
4375 BINDING_TYPE (binding) = newtype;
4376 return;
4379 /* Process a using-declaration at function scope. */
4381 void
4382 do_local_using_decl (tree decl)
4384 tree scope, name;
4385 tree oldval, oldtype, newval, newtype;
4387 decl = validate_nonmember_using_decl (decl, &scope, &name);
4388 if (decl == NULL_TREE)
4389 return;
4391 if (building_stmt_tree ()
4392 && at_function_scope_p ())
4393 add_decl_stmt (decl);
4395 oldval = lookup_name_current_level (name);
4396 oldtype = lookup_type_current_level (name);
4398 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
4400 if (newval)
4402 if (is_overloaded_fn (newval))
4404 tree fn, term;
4406 /* We only need to push declarations for those functions
4407 that were not already bound in the current level.
4408 The old value might be NULL_TREE, it might be a single
4409 function, or an OVERLOAD. */
4410 if (oldval && TREE_CODE (oldval) == OVERLOAD)
4411 term = OVL_FUNCTION (oldval);
4412 else
4413 term = oldval;
4414 for (fn = newval; fn && OVL_CURRENT (fn) != term;
4415 fn = OVL_NEXT (fn))
4416 push_overloaded_decl (OVL_CURRENT (fn),
4417 PUSH_LOCAL | PUSH_USING);
4419 else
4420 push_local_binding (name, newval, PUSH_USING);
4422 if (newtype)
4423 set_identifier_type_value (name, newtype);
4426 tree
4427 do_class_using_decl (tree decl)
4429 tree name, value;
4431 if (TREE_CODE (decl) != SCOPE_REF
4432 || !TYPE_P (TREE_OPERAND (decl, 0)))
4434 error ("using-declaration for non-member at class scope");
4435 return NULL_TREE;
4437 name = TREE_OPERAND (decl, 1);
4438 if (TREE_CODE (name) == BIT_NOT_EXPR)
4440 error ("using-declaration for destructor");
4441 return NULL_TREE;
4443 else if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4445 name = TREE_OPERAND (name, 0);
4446 error ("a using-declaration cannot specify a template-id. Try `using %T::%D'", TREE_OPERAND (decl, 0), name);
4447 return NULL_TREE;
4449 if (TREE_CODE (name) == TYPE_DECL)
4451 tree type = TREE_TYPE (name);
4452 if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (name)))
4454 name = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type));
4455 error ("a using-declaration cannot specify a template-id.");
4456 return NULL_TREE;
4458 name = DECL_NAME (name);
4460 else if (TREE_CODE (name) == TEMPLATE_DECL)
4461 name = DECL_NAME (name);
4462 else if (BASELINK_P (name))
4464 tree fns;
4466 fns = BASELINK_FUNCTIONS (name);
4467 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
4469 fns = TREE_OPERAND (fns, 0);
4470 error ("a using-declaration cannot specify a template-id. Try `using %T::%D'",
4471 BASELINK_ACCESS_BINFO (name),
4472 DECL_NAME (get_first_fn (fns)));
4474 name = DECL_NAME (get_first_fn (fns));
4477 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 980716);
4479 value = build_lang_decl (USING_DECL, name, void_type_node);
4480 DECL_INITIAL (value) = TREE_OPERAND (decl, 0);
4481 return value;
4484 /* Process a using-directive. */
4486 void
4487 do_using_directive (tree namespace)
4489 if (building_stmt_tree ())
4490 add_stmt (build_stmt (USING_STMT, namespace));
4492 /* using namespace A::B::C; */
4493 if (TREE_CODE (namespace) == SCOPE_REF)
4494 namespace = TREE_OPERAND (namespace, 1);
4495 if (TREE_CODE (namespace) == IDENTIFIER_NODE)
4497 /* Lookup in lexer did not find a namespace. */
4498 if (!processing_template_decl)
4499 error ("namespace `%T' undeclared", namespace);
4500 return;
4502 if (TREE_CODE (namespace) != NAMESPACE_DECL)
4504 if (!processing_template_decl)
4505 error ("`%T' is not a namespace", namespace);
4506 return;
4508 namespace = ORIGINAL_NAMESPACE (namespace);
4509 if (!toplevel_bindings_p ())
4510 push_using_directive (namespace);
4511 else
4512 /* direct usage */
4513 add_using_namespace (current_namespace, namespace, 0);
4516 void
4517 check_default_args (tree x)
4519 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4520 bool saw_def = false;
4521 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4522 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4524 if (TREE_PURPOSE (arg))
4525 saw_def = true;
4526 else if (saw_def)
4528 cp_error_at ("default argument missing for parameter %P of `%+#D'",
4529 i, x);
4530 break;
4535 void
4536 mark_used (tree decl)
4538 TREE_USED (decl) = 1;
4539 if (processing_template_decl)
4540 return;
4542 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
4543 && !TREE_ASM_WRITTEN (decl))
4544 /* Remember it, so we can check it was defined. */
4545 defer_fn (decl);
4547 if (!skip_evaluation)
4548 assemble_external (decl);
4550 /* Is it a synthesized method that needs to be synthesized? */
4551 if (TREE_CODE (decl) == FUNCTION_DECL
4552 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
4553 && DECL_ARTIFICIAL (decl)
4554 && !DECL_THUNK_P (decl)
4555 && ! DECL_INITIAL (decl)
4556 /* Kludge: don't synthesize for default args. */
4557 && current_function_decl)
4559 synthesize_method (decl);
4560 /* If we've already synthesized the method we don't need to
4561 instantiate it, so we can return right away. */
4562 return;
4565 /* If this is a function or variable that is an instance of some
4566 template, we now know that we will need to actually do the
4567 instantiation. We check that DECL is not an explicit
4568 instantiation because that is not checked in instantiate_decl. */
4569 if ((DECL_NON_THUNK_FUNCTION_P (decl) || TREE_CODE (decl) == VAR_DECL)
4570 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
4571 && (!DECL_EXPLICIT_INSTANTIATION (decl)
4572 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_INLINE (decl))))
4573 instantiate_decl (decl, /*defer_ok=*/1);
4576 /* Called when a class-head is encountered. TAG_KIND is the class-key
4577 for the class. SCOPE, if non-NULL, is the type or namespace
4578 indicated in the nested-name-specifier for the declaration of the
4579 class. ID is the name of the class, if any; it may be a TYPE_DECL,
4580 or an IDENTIFIER_NODE. ATTRIBUTES are attributes that apply to the
4581 class.
4583 Return a TYPE_DECL for the class being defined. */
4585 tree
4586 handle_class_head (enum tag_types tag_kind, tree scope, tree id,
4587 tree attributes)
4589 tree decl = NULL_TREE;
4590 tree current = current_scope ();
4591 bool xrefd_p = false;
4592 bool new_type_p;
4593 tree context;
4595 if (current == NULL_TREE)
4596 current = current_namespace;
4598 if (scope)
4600 if (TREE_CODE (id) == TYPE_DECL)
4601 /* We must bash typedefs back to the main decl of the
4602 type. Otherwise we become confused about scopes. */
4603 decl = TYPE_MAIN_DECL (TREE_TYPE (id));
4604 else if (DECL_CLASS_TEMPLATE_P (id))
4605 decl = DECL_TEMPLATE_RESULT (id);
4606 else
4608 if (TYPE_P (scope))
4610 /* According to the suggested resolution of core issue
4611 180, 'typename' is assumed after a class-key. */
4612 decl = make_typename_type (scope, id, tf_error);
4613 if (decl != error_mark_node)
4614 decl = TYPE_MAIN_DECL (decl);
4615 else
4616 decl = NULL_TREE;
4618 else if (scope == current)
4620 /* We've been given AGGR SCOPE::ID, when we're already
4621 inside SCOPE. Be nice about it. */
4622 if (pedantic)
4623 pedwarn ("extra qualification `%T::' on member `%D' ignored",
4624 scope, id);
4626 else
4627 error ("`%T' does not have a class or union named `%D'",
4628 scope, id);
4632 if (!decl)
4634 decl = TYPE_MAIN_DECL (xref_tag (tag_kind, id, attributes, false));
4635 xrefd_p = true;
4638 if (!TYPE_BINFO (TREE_TYPE (decl)))
4640 error ("`%T' is not a class or union type", decl);
4641 return error_mark_node;
4644 /* For a definition, we want to enter the containing scope before
4645 looking up any base classes etc. Only do so, if this is different
4646 to the current scope. */
4647 context = CP_DECL_CONTEXT (decl);
4649 new_type_p = (current != context
4650 && TREE_CODE (context) != TEMPLATE_TYPE_PARM
4651 && TREE_CODE (context) != BOUND_TEMPLATE_TEMPLATE_PARM);
4652 if (new_type_p)
4653 push_scope (context);
4655 if (!xrefd_p
4656 && PROCESSING_REAL_TEMPLATE_DECL_P ()
4657 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
4658 decl = push_template_decl (decl);
4660 return decl;
4663 #include "gt-cp-decl2.h"