(read_braced_string): Check for EOF. If encountered issue an error message.
[official-gcc.git] / gcc / cp / decl2.c
blob3fa4d74c53eaa7b680c0ac96c23a71c64306fdcc
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 (unsigned);
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, location_t *);
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 location_t saved_loc = input_location;
183 if (til)
185 input_line = TINST_LINE (til);
186 input_filename = TINST_FILE (til);
188 warning ("template `%#D' instantiated in file without #pragma interface",
189 decl);
190 input_location = saved_loc;
192 else
193 cp_warning_at ("template `%#D' defined in file without #pragma interface",
194 decl);
197 /* A subroutine of the parser, to handle a component list. */
199 void
200 grok_x_components (tree specs)
202 tree t;
204 specs = strip_attrs (specs);
206 check_tag_decl (specs);
207 t = groktypename (build_tree_list (specs, NULL_TREE));
209 /* The only case where we need to do anything additional here is an
210 anonymous union field, e.g.: `struct S { union { int i; }; };'. */
211 if (t == NULL_TREE || !ANON_AGGR_TYPE_P (t))
212 return;
214 fixup_anonymous_aggr (t);
215 finish_member_declaration (build_decl (FIELD_DECL, NULL_TREE, t));
218 /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
219 appropriately. */
221 tree
222 cp_build_parm_decl (tree name, tree type)
224 tree parm = build_decl (PARM_DECL, name, type);
225 DECL_ARG_TYPE (parm) = type_passed_as (type);
226 return parm;
229 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
230 indicated NAME. */
232 tree
233 build_artificial_parm (tree name, tree type)
235 tree parm = cp_build_parm_decl (name, type);
236 DECL_ARTIFICIAL (parm) = 1;
237 /* All our artificial parms are implicitly `const'; they cannot be
238 assigned to. */
239 TREE_READONLY (parm) = 1;
240 return parm;
243 /* Constructors for types with virtual baseclasses need an "in-charge" flag
244 saying whether this constructor is responsible for initialization of
245 virtual baseclasses or not. All destructors also need this "in-charge"
246 flag, which additionally determines whether or not the destructor should
247 free the memory for the object.
249 This function adds the "in-charge" flag to member function FN if
250 appropriate. It is called from grokclassfn and tsubst.
251 FN must be either a constructor or destructor.
253 The in-charge flag follows the 'this' parameter, and is followed by the
254 VTT parm (if any), then the user-written parms. */
256 void
257 maybe_retrofit_in_chrg (tree fn)
259 tree basetype, arg_types, parms, parm, fntype;
261 /* If we've already add the in-charge parameter don't do it again. */
262 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
263 return;
265 /* When processing templates we can't know, in general, whether or
266 not we're going to have virtual baseclasses. */
267 if (uses_template_parms (fn))
268 return;
270 /* We don't need an in-charge parameter for constructors that don't
271 have virtual bases. */
272 if (DECL_CONSTRUCTOR_P (fn)
273 && !TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
274 return;
276 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
277 basetype = TREE_TYPE (TREE_VALUE (arg_types));
278 arg_types = TREE_CHAIN (arg_types);
280 parms = TREE_CHAIN (DECL_ARGUMENTS (fn));
282 /* If this is a subobject constructor or destructor, our caller will
283 pass us a pointer to our VTT. */
284 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
286 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
288 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
289 TREE_CHAIN (parm) = parms;
290 parms = parm;
292 /* ...and then to TYPE_ARG_TYPES. */
293 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
295 DECL_HAS_VTT_PARM_P (fn) = 1;
298 /* Then add the in-charge parm (before the VTT parm). */
299 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
300 TREE_CHAIN (parm) = parms;
301 parms = parm;
302 arg_types = hash_tree_chain (integer_type_node, arg_types);
304 /* Insert our new parameter(s) into the list. */
305 TREE_CHAIN (DECL_ARGUMENTS (fn)) = parms;
307 /* And rebuild the function type. */
308 fntype = build_cplus_method_type (basetype, TREE_TYPE (TREE_TYPE (fn)),
309 arg_types);
310 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
311 fntype = build_exception_variant (fntype,
312 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
313 TREE_TYPE (fn) = fntype;
315 /* Now we've got the in-charge parameter. */
316 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
319 /* Classes overload their constituent function names automatically.
320 When a function name is declared in a record structure,
321 its name is changed to it overloaded name. Since names for
322 constructors and destructors can conflict, we place a leading
323 '$' for destructors.
325 CNAME is the name of the class we are grokking for.
327 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
329 FLAGS contains bits saying what's special about today's
330 arguments. 1 == DESTRUCTOR. 2 == OPERATOR.
332 If FUNCTION is a destructor, then we must add the `auto-delete' field
333 as a second parameter. There is some hair associated with the fact
334 that we must "declare" this variable in the manner consistent with the
335 way the rest of the arguments were declared.
337 QUALS are the qualifiers for the this pointer. */
339 void
340 grokclassfn (tree ctype, tree function, enum overload_flags flags, tree quals)
342 tree fn_name = DECL_NAME (function);
343 int this_quals = TYPE_UNQUALIFIED;
345 /* Even within an `extern "C"' block, members get C++ linkage. See
346 [dcl.link] for details. */
347 SET_DECL_LANGUAGE (function, lang_cplusplus);
349 if (fn_name == NULL_TREE)
351 error ("name missing for member function");
352 fn_name = get_identifier ("<anonymous>");
353 DECL_NAME (function) = fn_name;
356 if (quals)
357 this_quals = grok_method_quals (ctype, function, quals);
359 if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
361 /* Must add the class instance variable up front. */
362 /* Right now we just make this a pointer. But later
363 we may wish to make it special. */
364 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (function)));
365 tree qual_type;
366 tree parm;
368 /* The `this' parameter is implicitly `const'; it cannot be
369 assigned to. */
370 this_quals |= TYPE_QUAL_CONST;
371 qual_type = cp_build_qualified_type (type, this_quals);
372 parm = build_artificial_parm (this_identifier, qual_type);
373 c_apply_type_quals_to_decl (this_quals, parm);
374 TREE_CHAIN (parm) = last_function_parms;
375 last_function_parms = parm;
378 DECL_ARGUMENTS (function) = last_function_parms;
379 DECL_CONTEXT (function) = ctype;
381 if (flags == DTOR_FLAG)
382 DECL_DESTRUCTOR_P (function) = 1;
384 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
385 maybe_retrofit_in_chrg (function);
387 if (flags == DTOR_FLAG)
389 DECL_DESTRUCTOR_P (function) = 1;
390 TYPE_HAS_DESTRUCTOR (ctype) = 1;
394 /* Create an ARRAY_REF, checking for the user doing things backwards
395 along the way. */
397 tree
398 grok_array_decl (tree array_expr, tree index_exp)
400 tree type = TREE_TYPE (array_expr);
401 tree p1, p2, i1, i2;
403 if (type == error_mark_node || index_exp == error_mark_node)
404 return error_mark_node;
405 if (processing_template_decl)
406 return build_min (ARRAY_REF, type ? TREE_TYPE (type) : NULL_TREE,
407 array_expr, index_exp);
409 if (type == NULL_TREE)
411 /* Something has gone very wrong. Assume we are mistakenly reducing
412 an expression instead of a declaration. */
413 error ("parser may be lost: is there a '{' missing somewhere?");
414 return NULL_TREE;
417 if (TREE_CODE (type) == OFFSET_TYPE
418 || TREE_CODE (type) == REFERENCE_TYPE)
419 type = TREE_TYPE (type);
421 /* If they have an `operator[]', use that. */
422 if (IS_AGGR_TYPE (type) || IS_AGGR_TYPE (TREE_TYPE (index_exp)))
423 return build_new_op (ARRAY_REF, LOOKUP_NORMAL,
424 array_expr, index_exp, NULL_TREE);
426 /* Otherwise, create an ARRAY_REF for a pointer or array type. It
427 is a little-known fact that, if `a' is an array and `i' is an
428 int, you can write `i[a]', which means the same thing as `a[i]'. */
430 if (TREE_CODE (type) == ARRAY_TYPE)
431 p1 = array_expr;
432 else
433 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
435 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
436 p2 = index_exp;
437 else
438 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
440 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr, false);
441 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp, false);
443 if ((p1 && i2) && (i1 && p2))
444 error ("ambiguous conversion for array subscript");
446 if (p1 && i2)
447 array_expr = p1, index_exp = i2;
448 else if (i1 && p2)
449 array_expr = p2, index_exp = i1;
450 else
452 error ("invalid types `%T[%T]' for array subscript",
453 type, TREE_TYPE (index_exp));
454 return error_mark_node;
457 if (array_expr == error_mark_node || index_exp == error_mark_node)
458 error ("ambiguous conversion for array subscript");
460 return build_array_ref (array_expr, index_exp);
463 /* Given the cast expression EXP, checking out its validity. Either return
464 an error_mark_node if there was an unavoidable error, return a cast to
465 void for trying to delete a pointer w/ the value 0, or return the
466 call to delete. If DOING_VEC is 1, we handle things differently
467 for doing an array delete. If DOING_VEC is 2, they gave us the
468 array size as an argument to delete.
469 Implements ARM $5.3.4. This is called from the parser. */
471 tree
472 delete_sanity (tree exp, tree size, int doing_vec, int use_global_delete)
474 tree t, type;
475 /* For a regular vector delete (aka, no size argument) we will pass
476 this down as a NULL_TREE into build_vec_delete. */
477 tree maxindex = NULL_TREE;
479 if (exp == error_mark_node)
480 return exp;
482 if (processing_template_decl)
484 t = build_min (DELETE_EXPR, void_type_node, exp, size);
485 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
486 DELETE_EXPR_USE_VEC (t) = doing_vec;
487 return t;
490 if (TREE_CODE (exp) == OFFSET_REF)
491 exp = resolve_offset_ref (exp);
492 exp = convert_from_reference (exp);
493 t = build_expr_type_conversion (WANT_POINTER, exp, true);
495 if (t == NULL_TREE || t == error_mark_node)
497 error ("type `%#T' argument given to `delete', expected pointer",
498 TREE_TYPE (exp));
499 return error_mark_node;
502 if (doing_vec == 2)
504 maxindex = cp_build_binary_op (MINUS_EXPR, size, integer_one_node);
505 pedwarn ("anachronistic use of array size in vector delete");
508 type = TREE_TYPE (t);
510 /* As of Valley Forge, you can delete a pointer to const. */
512 /* You can't delete functions. */
513 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
515 error ("cannot delete a function. Only pointer-to-objects are valid arguments to `delete'");
516 return error_mark_node;
519 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
520 if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
522 warning ("deleting `%T' is undefined", type);
523 doing_vec = 0;
526 /* An array can't have been allocated by new, so complain. */
527 if (TREE_CODE (t) == ADDR_EXPR
528 && TREE_CODE (TREE_OPERAND (t, 0)) == VAR_DECL
529 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == ARRAY_TYPE)
530 warning ("deleting array `%#D'", TREE_OPERAND (t, 0));
532 /* Deleting a pointer with the value zero is valid and has no effect. */
533 if (integer_zerop (t))
534 return build1 (NOP_EXPR, void_type_node, t);
536 if (doing_vec)
537 return build_vec_delete (t, maxindex, sfk_deleting_destructor,
538 use_global_delete);
539 else
540 return build_delete (type, t, sfk_deleting_destructor,
541 LOOKUP_NORMAL, use_global_delete);
544 /* Report an error if the indicated template declaration is not the
545 sort of thing that should be a member template. */
547 void
548 check_member_template (tree tmpl)
550 tree decl;
552 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
553 decl = DECL_TEMPLATE_RESULT (tmpl);
555 if (TREE_CODE (decl) == FUNCTION_DECL
556 || (TREE_CODE (decl) == TYPE_DECL
557 && IS_AGGR_TYPE (TREE_TYPE (decl))))
559 if (current_function_decl)
560 /* 14.5.2.2 [temp.mem]
562 A local class shall not have member templates. */
563 error ("invalid declaration of member template `%#D' in local class",
564 decl);
566 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
568 /* 14.5.2.3 [temp.mem]
570 A member function template shall not be virtual. */
571 error
572 ("invalid use of `virtual' in template declaration of `%#D'",
573 decl);
574 DECL_VIRTUAL_P (decl) = 0;
577 /* The debug-information generating code doesn't know what to do
578 with member templates. */
579 DECL_IGNORED_P (tmpl) = 1;
581 else
582 error ("template declaration of `%#D'", decl);
585 /* Return true iff TYPE is a valid Java parameter or return type. */
587 static bool
588 acceptable_java_type (tree type)
590 if (TREE_CODE (type) == VOID_TYPE || TYPE_FOR_JAVA (type))
591 return 1;
592 if (TREE_CODE (type) == POINTER_TYPE || TREE_CODE (type) == REFERENCE_TYPE)
594 type = TREE_TYPE (type);
595 if (TREE_CODE (type) == RECORD_TYPE)
597 tree args; int i;
598 if (! TYPE_FOR_JAVA (type))
599 return false;
600 if (! CLASSTYPE_TEMPLATE_INFO (type))
601 return true;
602 args = CLASSTYPE_TI_ARGS (type);
603 i = TREE_VEC_LENGTH (args);
604 while (--i >= 0)
606 type = TREE_VEC_ELT (args, i);
607 if (TREE_CODE (type) == POINTER_TYPE)
608 type = TREE_TYPE (type);
609 if (! TYPE_FOR_JAVA (type))
610 return false;
612 return true;
615 return false;
618 /* For a METHOD in a Java class CTYPE, return true if
619 the parameter and return types are valid Java types.
620 Otherwise, print appropriate error messages, and return false. */
622 bool
623 check_java_method (tree method)
625 bool jerr = false;
626 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
627 tree ret_type = TREE_TYPE (TREE_TYPE (method));
628 if (!acceptable_java_type (ret_type))
630 error ("Java method '%D' has non-Java return type `%T'",
631 method, ret_type);
632 jerr = true;
634 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
636 tree type = TREE_VALUE (arg_types);
637 if (!acceptable_java_type (type))
639 error ("Java method '%D' has non-Java parameter type `%T'",
640 method, type);
641 jerr = true;
644 return !jerr;
647 /* Sanity check: report error if this function FUNCTION is not
648 really a member of the class (CTYPE) it is supposed to belong to.
649 CNAME is the same here as it is for grokclassfn above. */
651 tree
652 check_classfn (tree ctype, tree function)
654 int ix;
655 int is_template;
657 if (DECL_USE_TEMPLATE (function)
658 && !(TREE_CODE (function) == TEMPLATE_DECL
659 && DECL_TEMPLATE_SPECIALIZATION (function))
660 && is_member_template (DECL_TI_TEMPLATE (function)))
661 /* Since this is a specialization of a member template,
662 we're not going to find the declaration in the class.
663 For example, in:
665 struct S { template <typename T> void f(T); };
666 template <> void S::f(int);
668 we're not going to find `S::f(int)', but there's no
669 reason we should, either. We let our callers know we didn't
670 find the method, but we don't complain. */
671 return NULL_TREE;
673 /* OK, is this a definition of a member template? */
674 is_template = (TREE_CODE (function) == TEMPLATE_DECL
675 || (processing_template_decl - template_class_depth (ctype)));
677 ix = lookup_fnfields_1 (complete_type (ctype),
678 DECL_CONSTRUCTOR_P (function) ? ctor_identifier :
679 DECL_DESTRUCTOR_P (function) ? dtor_identifier :
680 DECL_NAME (function));
682 if (ix >= 0)
684 tree methods = CLASSTYPE_METHOD_VEC (ctype);
685 tree fndecls, fndecl = 0;
686 bool is_conv_op;
687 const char *format = NULL;
689 push_scope (ctype);
690 for (fndecls = TREE_VEC_ELT (methods, ix);
691 fndecls; fndecls = OVL_NEXT (fndecls))
693 tree p1, p2;
695 fndecl = OVL_CURRENT (fndecls);
696 p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
697 p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
699 /* We cannot simply call decls_match because this doesn't
700 work for static member functions that are pretending to
701 be methods, and because the name may have been changed by
702 asm("new_name"). */
704 /* Get rid of the this parameter on functions that become
705 static. */
706 if (DECL_STATIC_FUNCTION_P (fndecl)
707 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
708 p1 = TREE_CHAIN (p1);
710 /* A member template definition only matches a member template
711 declaration. */
712 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
713 continue;
715 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
716 TREE_TYPE (TREE_TYPE (fndecl)))
717 && compparms (p1, p2)
718 && (DECL_TEMPLATE_SPECIALIZATION (function)
719 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
720 && (!DECL_TEMPLATE_SPECIALIZATION (function)
721 || (DECL_TI_TEMPLATE (function)
722 == DECL_TI_TEMPLATE (fndecl))))
723 break;
725 pop_scope (ctype);
726 if (fndecls)
727 return OVL_CURRENT (fndecls);
728 error ("prototype for `%#D' does not match any in class `%T'",
729 function, ctype);
730 is_conv_op = DECL_CONV_FN_P (fndecl);
732 if (is_conv_op)
733 ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
734 fndecls = TREE_VEC_ELT (methods, ix);
735 while (fndecls)
737 fndecl = OVL_CURRENT (fndecls);
738 fndecls = OVL_NEXT (fndecls);
740 if (!fndecls && is_conv_op)
742 if (TREE_VEC_LENGTH (methods) > ix)
744 ix++;
745 fndecls = TREE_VEC_ELT (methods, ix);
746 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
748 fndecls = NULL_TREE;
749 is_conv_op = false;
752 else
753 is_conv_op = false;
755 if (format)
756 format = " %#D";
757 else if (fndecls)
758 format = "candidates are: %#D";
759 else
760 format = "candidate is: %#D";
761 cp_error_at (format, fndecl);
764 else if (!COMPLETE_TYPE_P (ctype))
765 cxx_incomplete_type_error (function, ctype);
766 else
767 error ("no `%#D' member function declared in class `%T'",
768 function, ctype);
770 /* If we did not find the method in the class, add it to avoid
771 spurious errors (unless the CTYPE is not yet defined, in which
772 case we'll only confuse ourselves when the function is declared
773 properly within the class. */
774 if (COMPLETE_TYPE_P (ctype))
775 add_method (ctype, function, /*error_p=*/1);
776 return NULL_TREE;
779 /* We have just processed the DECL, which is a static data member.
780 Its initializer, if present, is INIT. The ASMSPEC_TREE, if
781 present, is the assembly-language name for the data member.
782 FLAGS is as for cp_finish_decl. */
784 void
785 finish_static_data_member_decl (tree decl, tree init, tree asmspec_tree,
786 int flags)
788 my_friendly_assert (TREE_PUBLIC (decl), 0);
790 DECL_CONTEXT (decl) = current_class_type;
792 /* We cannot call pushdecl here, because that would fill in the
793 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
794 the right thing, namely, to put this decl out straight away. */
795 /* current_class_type can be NULL_TREE in case of error. */
796 if (!asmspec_tree && current_class_type)
797 DECL_INITIAL (decl) = error_mark_node;
799 if (! processing_template_decl)
801 if (!pending_statics)
802 VARRAY_TREE_INIT (pending_statics, 32, "pending_statics");
803 VARRAY_PUSH_TREE (pending_statics, decl);
806 if (LOCAL_CLASS_P (current_class_type))
807 pedwarn ("local class `%#T' shall not have static data member `%#D'",
808 current_class_type, decl);
810 /* Static consts need not be initialized in the class definition. */
811 if (init != NULL_TREE && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
813 static int explained = 0;
815 error ("initializer invalid for static member with constructor");
816 if (!explained)
818 error ("(an out of class initialization is required)");
819 explained = 1;
821 init = NULL_TREE;
823 /* Force the compiler to know when an uninitialized static const
824 member is being used. */
825 if (CP_TYPE_CONST_P (TREE_TYPE (decl)) && init == 0)
826 TREE_USED (decl) = 1;
827 DECL_INITIAL (decl) = init;
828 DECL_IN_AGGR_P (decl) = 1;
830 cp_finish_decl (decl, init, asmspec_tree, flags);
833 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
834 of a structure component, returning a _DECL node.
835 QUALS is a list of type qualifiers for this decl (such as for declaring
836 const member functions).
838 This is done during the parsing of the struct declaration.
839 The _DECL nodes are chained together and the lot of them
840 are ultimately passed to `build_struct' to make the RECORD_TYPE node.
842 If class A defines that certain functions in class B are friends, then
843 the way I have set things up, it is B who is interested in permission
844 granted by A. However, it is in A's context that these declarations
845 are parsed. By returning a void_type_node, class A does not attempt
846 to incorporate the declarations of the friends within its structure.
848 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
849 CHANGES TO CODE IN `start_method'. */
851 tree
852 grokfield (tree declarator, tree declspecs, tree init, tree asmspec_tree,
853 tree attrlist)
855 tree value;
856 const char *asmspec = 0;
857 int flags = LOOKUP_ONLYCONVERTING;
859 if (declspecs == NULL_TREE
860 && TREE_CODE (declarator) == SCOPE_REF
861 && TREE_CODE (TREE_OPERAND (declarator, 1)) == IDENTIFIER_NODE)
863 /* Access declaration */
864 if (! IS_AGGR_TYPE_CODE (TREE_CODE (TREE_OPERAND (declarator, 0))))
866 else if (TREE_COMPLEXITY (declarator) == current_class_depth)
867 pop_nested_class ();
868 return do_class_using_decl (declarator);
871 if (init
872 && TREE_CODE (init) == TREE_LIST
873 && TREE_VALUE (init) == error_mark_node
874 && TREE_CHAIN (init) == NULL_TREE)
875 init = NULL_TREE;
877 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
878 if (! value || value == error_mark_node)
879 /* friend or constructor went bad. */
880 return value;
881 if (TREE_TYPE (value) == error_mark_node)
882 return error_mark_node;
884 if (TREE_CODE (value) == TYPE_DECL && init)
886 error ("typedef `%D' is initialized (use __typeof__ instead)", value);
887 init = NULL_TREE;
890 /* Pass friendly classes back. */
891 if (TREE_CODE (value) == VOID_TYPE)
892 return void_type_node;
894 if (DECL_NAME (value) != NULL_TREE
895 && IDENTIFIER_POINTER (DECL_NAME (value))[0] == '_'
896 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (value)), "_vptr"))
897 error ("member `%D' conflicts with virtual function table field name",
898 value);
900 /* Stash away type declarations. */
901 if (TREE_CODE (value) == TYPE_DECL)
903 DECL_NONLOCAL (value) = 1;
904 DECL_CONTEXT (value) = current_class_type;
906 if (CLASS_TYPE_P (TREE_TYPE (value)))
907 CLASSTYPE_GOT_SEMICOLON (TREE_TYPE (value)) = 1;
909 if (processing_template_decl)
910 value = push_template_decl (value);
912 return value;
915 if (DECL_IN_AGGR_P (value))
917 error ("`%D' is already defined in `%T'", value,
918 DECL_CONTEXT (value));
919 return void_type_node;
922 if (asmspec_tree)
923 asmspec = TREE_STRING_POINTER (asmspec_tree);
925 if (init)
927 if (TREE_CODE (value) == FUNCTION_DECL)
929 grok_function_init (value, init);
930 init = NULL_TREE;
932 else if (pedantic && TREE_CODE (value) != VAR_DECL)
933 /* Already complained in grokdeclarator. */
934 init = NULL_TREE;
935 else
937 /* We allow initializers to become parameters to base
938 initializers. */
939 if (TREE_CODE (init) == TREE_LIST)
941 if (TREE_CHAIN (init) == NULL_TREE)
942 init = TREE_VALUE (init);
943 else
944 init = digest_init (TREE_TYPE (value), init, (tree *)0);
947 if (!processing_template_decl)
949 if (TREE_CODE (init) == CONST_DECL)
950 init = DECL_INITIAL (init);
951 else if (TREE_READONLY_DECL_P (init))
952 init = decl_constant_value (init);
953 else if (TREE_CODE (init) == CONSTRUCTOR)
954 init = digest_init (TREE_TYPE (value), init, (tree *)0);
955 if (init == error_mark_node)
956 /* We must make this look different than `error_mark_node'
957 because `decl_const_value' would mis-interpret it
958 as only meaning that this VAR_DECL is defined. */
959 init = build1 (NOP_EXPR, TREE_TYPE (value), init);
960 else if (! TREE_CONSTANT (init))
962 /* We can allow references to things that are effectively
963 static, since references are initialized with the
964 address. */
965 if (TREE_CODE (TREE_TYPE (value)) != REFERENCE_TYPE
966 || (TREE_STATIC (init) == 0
967 && (!DECL_P (init) || DECL_EXTERNAL (init) == 0)))
969 error ("field initializer is not constant");
970 init = error_mark_node;
977 if (processing_template_decl && ! current_function_decl
978 && (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == FUNCTION_DECL))
979 value = push_template_decl (value);
981 if (attrlist)
982 cplus_decl_attributes (&value, attrlist, 0);
984 if (TREE_CODE (value) == VAR_DECL)
986 finish_static_data_member_decl (value, init, asmspec_tree,
987 flags);
988 return value;
990 if (TREE_CODE (value) == FIELD_DECL)
992 if (asmspec)
993 error ("`asm' specifiers are not permitted on non-static data members");
994 if (DECL_INITIAL (value) == error_mark_node)
995 init = error_mark_node;
996 cp_finish_decl (value, init, NULL_TREE, flags);
997 DECL_INITIAL (value) = init;
998 DECL_IN_AGGR_P (value) = 1;
999 return value;
1001 if (TREE_CODE (value) == FUNCTION_DECL)
1003 if (asmspec)
1005 /* This must override the asm specifier which was placed
1006 by grokclassfn. Lay this out fresh. */
1007 SET_DECL_RTL (value, NULL_RTX);
1008 SET_DECL_ASSEMBLER_NAME (value, get_identifier (asmspec));
1010 if (!DECL_FRIEND_P (value))
1011 grok_special_member_properties (value);
1013 cp_finish_decl (value, init, asmspec_tree, flags);
1015 /* Pass friends back this way. */
1016 if (DECL_FRIEND_P (value))
1017 return void_type_node;
1019 DECL_IN_AGGR_P (value) = 1;
1020 return value;
1022 abort ();
1023 /* NOTREACHED */
1024 return NULL_TREE;
1027 /* Like `grokfield', but for bitfields.
1028 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1030 tree
1031 grokbitfield (tree declarator, tree declspecs, tree width)
1033 register tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1034 0, NULL);
1036 if (! value) return NULL_TREE; /* friends went bad. */
1038 /* Pass friendly classes back. */
1039 if (TREE_CODE (value) == VOID_TYPE)
1040 return void_type_node;
1042 if (TREE_CODE (value) == TYPE_DECL)
1044 error ("cannot declare `%D' to be a bit-field type", value);
1045 return NULL_TREE;
1048 /* Usually, finish_struct_1 catches bitfields with invalid types.
1049 But, in the case of bitfields with function type, we confuse
1050 ourselves into thinking they are member functions, so we must
1051 check here. */
1052 if (TREE_CODE (value) == FUNCTION_DECL)
1054 error ("cannot declare bit-field `%D' with function type",
1055 DECL_NAME (value));
1056 return NULL_TREE;
1059 if (DECL_IN_AGGR_P (value))
1061 error ("`%D' is already defined in the class %T", value,
1062 DECL_CONTEXT (value));
1063 return void_type_node;
1066 if (TREE_STATIC (value))
1068 error ("static member `%D' cannot be a bit-field", value);
1069 return NULL_TREE;
1071 cp_finish_decl (value, NULL_TREE, NULL_TREE, 0);
1073 if (width != error_mark_node)
1075 constant_expression_warning (width);
1076 DECL_INITIAL (value) = width;
1077 SET_DECL_C_BIT_FIELD (value);
1080 DECL_IN_AGGR_P (value) = 1;
1081 return value;
1084 /* When a function is declared with an initializer,
1085 do the right thing. Currently, there are two possibilities:
1087 class B
1089 public:
1090 // initialization possibility #1.
1091 virtual void f () = 0;
1092 int g ();
1095 class D1 : B
1097 public:
1098 int d1;
1099 // error, no f ();
1102 class D2 : B
1104 public:
1105 int d2;
1106 void f ();
1109 class D3 : B
1111 public:
1112 int d3;
1113 // initialization possibility #2
1114 void f () = B::f;
1119 static void
1120 grok_function_init (tree decl, tree init)
1122 /* An initializer for a function tells how this function should
1123 be inherited. */
1124 tree type = TREE_TYPE (decl);
1126 if (TREE_CODE (type) == FUNCTION_TYPE)
1127 error ("initializer specified for non-member function `%D'", decl);
1128 else if (integer_zerop (init))
1129 DECL_PURE_VIRTUAL_P (decl) = 1;
1130 else
1131 error ("invalid initializer for virtual method `%D'", decl);
1134 void
1135 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1137 if (*decl == NULL_TREE || *decl == void_type_node)
1138 return;
1140 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1141 decl = &DECL_TEMPLATE_RESULT (*decl);
1143 decl_attributes (decl, attributes, flags);
1145 if (TREE_CODE (*decl) == TYPE_DECL)
1146 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1149 /* Return the name for the constructor (or destructor) for the
1150 specified class TYPE. When given a template, this routine doesn't
1151 lose the specialization. */
1153 tree
1154 constructor_name_full (tree type)
1156 type = TYPE_MAIN_VARIANT (type);
1157 if (CLASS_TYPE_P (type) && TYPE_WAS_ANONYMOUS (type)
1158 && TYPE_HAS_CONSTRUCTOR (type))
1159 return DECL_NAME (OVL_CURRENT (CLASSTYPE_CONSTRUCTORS (type)));
1160 else
1161 return TYPE_IDENTIFIER (type);
1164 /* Return the name for the constructor (or destructor) for the
1165 specified class. When given a template, return the plain
1166 unspecialized name. */
1168 tree
1169 constructor_name (tree type)
1171 tree name;
1172 name = constructor_name_full (type);
1173 if (IDENTIFIER_TEMPLATE (name))
1174 name = IDENTIFIER_TEMPLATE (name);
1175 return name;
1178 /* Returns TRUE if NAME is the name for the constructor for TYPE. */
1180 bool
1181 constructor_name_p (tree name, tree type)
1183 return (name == constructor_name (type)
1184 || name == constructor_name_full (type));
1188 /* Defer the compilation of the FN until the end of compilation. */
1190 void
1191 defer_fn (tree fn)
1193 if (DECL_DEFERRED_FN (fn))
1194 return;
1195 DECL_DEFERRED_FN (fn) = 1;
1196 if (!deferred_fns)
1197 VARRAY_TREE_INIT (deferred_fns, 32, "deferred_fns");
1199 VARRAY_PUSH_TREE (deferred_fns, fn);
1202 /* Walks through the namespace- or function-scope anonymous union OBJECT,
1203 building appropriate ALIAS_DECLs. Returns one of the fields for use in
1204 the mangled name. */
1206 static tree
1207 build_anon_union_vars (tree object)
1209 tree type = TREE_TYPE (object);
1210 tree main_decl = NULL_TREE;
1211 tree field;
1213 /* Rather than write the code to handle the non-union case,
1214 just give an error. */
1215 if (TREE_CODE (type) != UNION_TYPE)
1216 error ("anonymous struct not inside named type");
1218 for (field = TYPE_FIELDS (type);
1219 field != NULL_TREE;
1220 field = TREE_CHAIN (field))
1222 tree decl;
1223 tree ref;
1225 if (DECL_ARTIFICIAL (field))
1226 continue;
1227 if (TREE_CODE (field) != FIELD_DECL)
1229 cp_pedwarn_at ("\
1230 `%#D' invalid; an anonymous union can only have non-static data members",
1231 field);
1232 continue;
1235 if (TREE_PRIVATE (field))
1236 cp_pedwarn_at ("private member `%#D' in anonymous union", field);
1237 else if (TREE_PROTECTED (field))
1238 cp_pedwarn_at ("protected member `%#D' in anonymous union", field);
1240 if (processing_template_decl)
1241 ref = build_min_nt (COMPONENT_REF, object, DECL_NAME (field));
1242 else
1243 ref = build_class_member_access_expr (object, field, NULL_TREE,
1244 false);
1246 if (DECL_NAME (field))
1248 decl = build_decl (ALIAS_DECL, DECL_NAME (field), TREE_TYPE (field));
1249 DECL_INITIAL (decl) = ref;
1250 TREE_PUBLIC (decl) = 0;
1251 TREE_STATIC (decl) = 0;
1252 DECL_EXTERNAL (decl) = 1;
1253 decl = pushdecl (decl);
1255 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1256 decl = build_anon_union_vars (ref);
1257 else
1258 decl = 0;
1260 if (main_decl == NULL_TREE)
1261 main_decl = decl;
1264 return main_decl;
1267 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1268 anonymous union, then all members must be laid out together. PUBLIC_P
1269 is nonzero if this union is not declared static. */
1271 void
1272 finish_anon_union (tree anon_union_decl)
1274 tree type = TREE_TYPE (anon_union_decl);
1275 tree main_decl;
1276 bool public_p = TREE_PUBLIC (anon_union_decl);
1278 /* The VAR_DECL's context is the same as the TYPE's context. */
1279 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1281 if (TYPE_FIELDS (type) == NULL_TREE)
1282 return;
1284 if (public_p)
1286 error ("namespace-scope anonymous aggregates must be static");
1287 return;
1290 main_decl = build_anon_union_vars (anon_union_decl);
1291 if (main_decl == NULL_TREE)
1293 warning ("anonymous union with no members");
1294 return;
1297 if (!processing_template_decl)
1299 /* Use main_decl to set the mangled name. */
1300 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1301 mangle_decl (anon_union_decl);
1302 DECL_NAME (anon_union_decl) = NULL_TREE;
1305 pushdecl (anon_union_decl);
1306 if (building_stmt_tree ()
1307 && at_function_scope_p ())
1308 add_decl_stmt (anon_union_decl);
1309 else if (!processing_template_decl)
1310 rest_of_decl_compilation (anon_union_decl, NULL,
1311 toplevel_bindings_p (), at_eof);
1314 /* Auxiliary functions to make type signatures for
1315 `operator new' and `operator delete' correspond to
1316 what compiler will be expecting. */
1318 tree
1319 coerce_new_type (tree type)
1321 int e = 0;
1322 tree args = TYPE_ARG_TYPES (type);
1324 my_friendly_assert (TREE_CODE (type) == FUNCTION_TYPE, 20001107);
1326 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1327 e = 1, error ("`operator new' must return type `%T'", ptr_type_node);
1329 if (!args || args == void_list_node
1330 || !same_type_p (TREE_VALUE (args), size_type_node))
1332 e = 2;
1333 if (args && args != void_list_node)
1334 args = TREE_CHAIN (args);
1335 pedwarn ("`operator new' takes type `size_t' (`%T') as first parameter", size_type_node);
1337 switch (e)
1339 case 2:
1340 args = tree_cons (NULL_TREE, size_type_node, args);
1341 /* FALLTHROUGH */
1342 case 1:
1343 type = build_exception_variant
1344 (build_function_type (ptr_type_node, args),
1345 TYPE_RAISES_EXCEPTIONS (type));
1346 /* FALLTHROUGH */
1347 default:;
1349 return type;
1352 tree
1353 coerce_delete_type (tree type)
1355 int e = 0;
1356 tree args = TYPE_ARG_TYPES (type);
1358 my_friendly_assert (TREE_CODE (type) == FUNCTION_TYPE, 20001107);
1360 if (!same_type_p (TREE_TYPE (type), void_type_node))
1361 e = 1, error ("`operator delete' must return type `%T'", void_type_node);
1363 if (!args || args == void_list_node
1364 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1366 e = 2;
1367 if (args && args != void_list_node)
1368 args = TREE_CHAIN (args);
1369 error ("`operator delete' takes type `%T' as first parameter", ptr_type_node);
1371 switch (e)
1373 case 2:
1374 args = tree_cons (NULL_TREE, ptr_type_node, args);
1375 /* FALLTHROUGH */
1376 case 1:
1377 type = build_exception_variant
1378 (build_function_type (void_type_node, args),
1379 TYPE_RAISES_EXCEPTIONS (type));
1380 /* FALLTHROUGH */
1381 default:;
1384 return type;
1387 static void
1388 mark_vtable_entries (tree decl)
1390 tree entries = CONSTRUCTOR_ELTS (DECL_INITIAL (decl));
1392 for (; entries; entries = TREE_CHAIN (entries))
1394 tree fnaddr = TREE_VALUE (entries);
1395 tree fn;
1397 if (TREE_CODE (fnaddr) != ADDR_EXPR
1398 && TREE_CODE (fnaddr) != FDESC_EXPR)
1399 /* This entry is an offset: a virtual base class offset, a
1400 virtual call offset, an RTTI offset, etc. */
1401 continue;
1403 fn = TREE_OPERAND (fnaddr, 0);
1404 TREE_ADDRESSABLE (fn) = 1;
1405 /* When we don't have vcall offsets, we output thunks whenever
1406 we output the vtables that contain them. With vcall offsets,
1407 we know all the thunks we'll need when we emit a virtual
1408 function, so we emit the thunks there instead. */
1409 if (DECL_THUNK_P (fn))
1410 use_thunk (fn, /*emit_p=*/0);
1411 mark_used (fn);
1415 /* Set DECL up to have the closest approximation of "initialized common"
1416 linkage available. */
1418 void
1419 comdat_linkage (tree decl)
1421 if (flag_weak)
1422 make_decl_one_only (decl);
1423 else if (TREE_CODE (decl) == FUNCTION_DECL
1424 || (TREE_CODE (decl) == VAR_DECL && DECL_ARTIFICIAL (decl)))
1425 /* We can just emit function and compiler-generated variables
1426 statically; having multiple copies is (for the most part) only
1427 a waste of space.
1429 There are two correctness issues, however: the address of a
1430 template instantiation with external linkage should be the
1431 same, independent of what translation unit asks for the
1432 address, and this will not hold when we emit multiple copies of
1433 the function. However, there's little else we can do.
1435 Also, by default, the typeinfo implementation assumes that
1436 there will be only one copy of the string used as the name for
1437 each type. Therefore, if weak symbols are unavailable, the
1438 run-time library should perform a more conservative check; it
1439 should perform a string comparison, rather than an address
1440 comparison. */
1441 TREE_PUBLIC (decl) = 0;
1442 else
1444 /* Static data member template instantiations, however, cannot
1445 have multiple copies. */
1446 if (DECL_INITIAL (decl) == 0
1447 || DECL_INITIAL (decl) == error_mark_node)
1448 DECL_COMMON (decl) = 1;
1449 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1451 DECL_COMMON (decl) = 1;
1452 DECL_INITIAL (decl) = error_mark_node;
1454 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1456 /* We can't do anything useful; leave vars for explicit
1457 instantiation. */
1458 DECL_EXTERNAL (decl) = 1;
1459 DECL_NOT_REALLY_EXTERN (decl) = 0;
1463 if (DECL_LANG_SPECIFIC (decl))
1464 DECL_COMDAT (decl) = 1;
1467 /* For win32 we also want to put explicit instantiations in
1468 linkonce sections, so that they will be merged with implicit
1469 instantiations; otherwise we get duplicate symbol errors. */
1471 void
1472 maybe_make_one_only (tree decl)
1474 /* We used to say that this was not necessary on targets that support weak
1475 symbols, because the implicit instantiations will defer to the explicit
1476 one. However, that's not actually the case in SVR4; a strong definition
1477 after a weak one is an error. Also, not making explicit
1478 instantiations one_only means that we can end up with two copies of
1479 some template instantiations. */
1480 if (! flag_weak)
1481 return;
1483 /* We can't set DECL_COMDAT on functions, or finish_file will think
1484 we can get away with not emitting them if they aren't used. We need
1485 to for variables so that cp_finish_decl will update their linkage,
1486 because their DECL_INITIAL may not have been set properly yet. */
1488 make_decl_one_only (decl);
1490 if (TREE_CODE (decl) == VAR_DECL)
1492 DECL_COMDAT (decl) = 1;
1493 /* Mark it needed so we don't forget to emit it. */
1494 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)) = 1;
1498 /* Set TREE_PUBLIC and/or DECL_EXTERN on the vtable DECL,
1499 based on TYPE and other static flags.
1501 Note that anything public is tagged TREE_PUBLIC, whether
1502 it's public in this file or in another one. */
1504 void
1505 import_export_vtable (tree decl, tree type, int final)
1507 if (DECL_INTERFACE_KNOWN (decl))
1508 return;
1510 if (TYPE_FOR_JAVA (type))
1512 TREE_PUBLIC (decl) = 1;
1513 DECL_EXTERNAL (decl) = 1;
1514 DECL_INTERFACE_KNOWN (decl) = 1;
1516 else if (CLASSTYPE_INTERFACE_KNOWN (type))
1518 TREE_PUBLIC (decl) = 1;
1519 DECL_EXTERNAL (decl) = CLASSTYPE_INTERFACE_ONLY (type);
1520 DECL_INTERFACE_KNOWN (decl) = 1;
1522 else
1524 /* We can only wait to decide if we have real non-inline virtual
1525 functions in our class, or if we come from a template. */
1527 int found = (CLASSTYPE_TEMPLATE_INSTANTIATION (type)
1528 || CLASSTYPE_KEY_METHOD (type) != NULL_TREE);
1530 if (final || ! found)
1532 comdat_linkage (decl);
1533 DECL_EXTERNAL (decl) = 0;
1535 else
1537 TREE_PUBLIC (decl) = 1;
1538 DECL_EXTERNAL (decl) = 1;
1543 /* Determine whether or not we want to specifically import or export CTYPE,
1544 using various heuristics. */
1546 static void
1547 import_export_class (tree ctype)
1549 /* -1 for imported, 1 for exported. */
1550 int import_export = 0;
1552 /* It only makes sense to call this function at EOF. The reason is
1553 that this function looks at whether or not the first non-inline
1554 non-abstract virtual member function has been defined in this
1555 translation unit. But, we can't possibly know that until we've
1556 seen the entire translation unit. */
1557 my_friendly_assert (at_eof, 20000226);
1559 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1560 return;
1562 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma interface,
1563 we will have CLASSTYPE_INTERFACE_ONLY set but not
1564 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1565 heuristic because someone will supply a #pragma implementation
1566 elsewhere, and deducing it here would produce a conflict. */
1567 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1568 return;
1570 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1571 import_export = -1;
1572 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1573 import_export = 1;
1575 /* If we got -fno-implicit-templates, we import template classes that
1576 weren't explicitly instantiated. */
1577 if (import_export == 0
1578 && CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1579 && ! flag_implicit_templates)
1580 import_export = -1;
1582 /* Base our import/export status on that of the first non-inline,
1583 non-pure virtual function, if any. */
1584 if (import_export == 0
1585 && TYPE_POLYMORPHIC_P (ctype))
1587 tree method = CLASSTYPE_KEY_METHOD (ctype);
1588 if (method)
1589 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1592 #ifdef MULTIPLE_SYMBOL_SPACES
1593 if (import_export == -1)
1594 import_export = 0;
1595 #endif
1597 if (import_export)
1599 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1600 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1604 /* We need to describe to the assembler the relationship between
1605 a vtable and the vtable of the parent class. */
1607 static void
1608 output_vtable_inherit (tree vars)
1610 tree parent;
1611 rtx child_rtx, parent_rtx;
1613 child_rtx = XEXP (DECL_RTL (vars), 0); /* strip the mem ref */
1615 parent = binfo_for_vtable (vars);
1617 if (parent == TYPE_BINFO (DECL_CONTEXT (vars)))
1618 parent_rtx = const0_rtx;
1619 else if (parent)
1621 parent = get_vtbl_decl_for_binfo (TYPE_BINFO (BINFO_TYPE (parent)));
1622 parent_rtx = XEXP (DECL_RTL (parent), 0); /* strip the mem ref */
1624 else
1625 abort ();
1627 assemble_vtable_inherit (child_rtx, parent_rtx);
1630 /* If necessary, write out the vtables for the dynamic class CTYPE.
1631 Returns true if any vtables were emitted. */
1633 static bool
1634 maybe_emit_vtables (tree ctype)
1636 tree vtbl;
1637 tree primary_vtbl;
1639 /* If the vtables for this class have already been emitted there is
1640 nothing more to do. */
1641 primary_vtbl = CLASSTYPE_VTABLES (ctype);
1642 if (TREE_ASM_WRITTEN (primary_vtbl))
1643 return false;
1644 /* Ignore dummy vtables made by get_vtable_decl. */
1645 if (TREE_TYPE (primary_vtbl) == void_type_node)
1646 return false;
1648 import_export_class (ctype);
1649 import_export_vtable (primary_vtbl, ctype, 1);
1651 /* See if any of the vtables are needed. */
1652 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = TREE_CHAIN (vtbl))
1653 if (!DECL_EXTERNAL (vtbl) && DECL_NEEDED_P (vtbl))
1654 break;
1656 if (!vtbl)
1658 /* If the references to this class' vtables are optimized away,
1659 still emit the appropriate debugging information. See
1660 dfs_debug_mark. */
1661 if (DECL_COMDAT (primary_vtbl)
1662 && CLASSTYPE_DEBUG_REQUESTED (ctype))
1663 note_debug_info_needed (ctype);
1664 return false;
1667 /* The ABI requires that we emit all of the vtables if we emit any
1668 of them. */
1669 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = TREE_CHAIN (vtbl))
1671 /* Write it out. */
1672 import_export_vtable (vtbl, ctype, 1);
1673 mark_vtable_entries (vtbl);
1674 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
1675 store_init_value (vtbl, DECL_INITIAL (vtbl));
1677 if (write_symbols == DWARF_DEBUG || write_symbols == DWARF2_DEBUG)
1679 /* Mark the VAR_DECL node representing the vtable itself as a
1680 "gratuitous" one, thereby forcing dwarfout.c to ignore it.
1681 It is rather important that such things be ignored because
1682 any effort to actually generate DWARF for them will run
1683 into trouble when/if we encounter code like:
1685 #pragma interface
1686 struct S { virtual void member (); };
1688 because the artificial declaration of the vtable itself (as
1689 manufactured by the g++ front end) will say that the vtable
1690 is a static member of `S' but only *after* the debug output
1691 for the definition of `S' has already been output. This causes
1692 grief because the DWARF entry for the definition of the vtable
1693 will try to refer back to an earlier *declaration* of the
1694 vtable as a static member of `S' and there won't be one.
1695 We might be able to arrange to have the "vtable static member"
1696 attached to the member list for `S' before the debug info for
1697 `S' get written (which would solve the problem) but that would
1698 require more intrusive changes to the g++ front end. */
1700 DECL_IGNORED_P (vtbl) = 1;
1703 /* Always make vtables weak. */
1704 if (flag_weak)
1705 comdat_linkage (vtbl);
1707 rest_of_decl_compilation (vtbl, NULL, 1, 1);
1709 if (flag_vtable_gc)
1710 output_vtable_inherit (vtbl);
1712 /* Because we're only doing syntax-checking, we'll never end up
1713 actually marking the variable as written. */
1714 if (flag_syntax_only)
1715 TREE_ASM_WRITTEN (vtbl) = 1;
1718 /* Since we're writing out the vtable here, also write the debug
1719 info. */
1720 note_debug_info_needed (ctype);
1722 return true;
1725 /* Determines the proper settings of TREE_PUBLIC and DECL_EXTERNAL for an
1726 inline function or template instantiation at end-of-file. */
1728 void
1729 import_export_decl (tree decl)
1731 if (DECL_INTERFACE_KNOWN (decl))
1732 return;
1734 if (DECL_TEMPLATE_INSTANTIATION (decl)
1735 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
1737 DECL_NOT_REALLY_EXTERN (decl) = 1;
1738 if ((DECL_IMPLICIT_INSTANTIATION (decl)
1739 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
1740 && (flag_implicit_templates
1741 || (flag_implicit_inline_templates
1742 && DECL_DECLARED_INLINE_P (decl))))
1744 if (!TREE_PUBLIC (decl))
1745 /* Templates are allowed to have internal linkage. See
1746 [basic.link]. */
1748 else
1749 comdat_linkage (decl);
1751 else
1753 DECL_EXTERNAL (decl) = 1;
1754 DECL_NOT_REALLY_EXTERN (decl) = 0;
1757 else if (DECL_FUNCTION_MEMBER_P (decl))
1759 if (!DECL_DECLARED_INLINE_P (decl))
1761 tree ctype = DECL_CONTEXT (decl);
1762 import_export_class (ctype);
1763 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1765 DECL_NOT_REALLY_EXTERN (decl)
1766 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
1767 || (DECL_DECLARED_INLINE_P (decl)
1768 && ! flag_implement_inlines
1769 && !DECL_VINDEX (decl)));
1771 if (!DECL_NOT_REALLY_EXTERN (decl))
1772 DECL_EXTERNAL (decl) = 1;
1774 /* Always make artificials weak. */
1775 if (DECL_ARTIFICIAL (decl) && flag_weak)
1776 comdat_linkage (decl);
1777 else
1778 maybe_make_one_only (decl);
1781 else
1782 comdat_linkage (decl);
1784 else
1785 comdat_linkage (decl);
1787 DECL_INTERFACE_KNOWN (decl) = 1;
1790 /* Here, we only decide whether or not the tinfo node should be
1791 emitted with the vtable. IS_IN_LIBRARY is nonzero iff the
1792 typeinfo for TYPE should be in the runtime library. */
1794 void
1795 import_export_tinfo (tree decl, tree type, bool is_in_library)
1797 if (DECL_INTERFACE_KNOWN (decl))
1798 return;
1800 if (IS_AGGR_TYPE (type))
1801 import_export_class (type);
1803 if (IS_AGGR_TYPE (type) && CLASSTYPE_INTERFACE_KNOWN (type)
1804 && TYPE_POLYMORPHIC_P (type)
1805 /* If -fno-rtti, we're not necessarily emitting this stuff with
1806 the class, so go ahead and emit it now. This can happen when
1807 a class is used in exception handling. */
1808 && flag_rtti)
1810 DECL_NOT_REALLY_EXTERN (decl) = !CLASSTYPE_INTERFACE_ONLY (type);
1811 DECL_COMDAT (decl) = 0;
1813 else
1815 DECL_NOT_REALLY_EXTERN (decl) = 1;
1816 DECL_COMDAT (decl) = 1;
1819 /* Now override some cases. */
1820 if (flag_weak)
1821 DECL_COMDAT (decl) = 1;
1822 else if (is_in_library)
1823 DECL_COMDAT (decl) = 0;
1825 DECL_INTERFACE_KNOWN (decl) = 1;
1828 /* Return an expression that performs the destruction of DECL, which
1829 must be a VAR_DECL whose type has a non-trivial destructor, or is
1830 an array whose (innermost) elements have a non-trivial destructor. */
1832 tree
1833 build_cleanup (tree decl)
1835 tree temp;
1836 tree type = TREE_TYPE (decl);
1838 /* This function should only be called for declarations that really
1839 require cleanups. */
1840 my_friendly_assert (!TYPE_HAS_TRIVIAL_DESTRUCTOR (type), 20030106);
1842 /* Treat all objects with destructors as used; the destructor may do
1843 something substantive. */
1844 mark_used (decl);
1846 if (TREE_CODE (type) == ARRAY_TYPE)
1847 temp = decl;
1848 else
1850 cxx_mark_addressable (decl);
1851 temp = build1 (ADDR_EXPR, build_pointer_type (type), decl);
1853 temp = build_delete (TREE_TYPE (temp), temp,
1854 sfk_complete_destructor,
1855 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
1856 return temp;
1859 /* Returns the initialization guard variable for the variable DECL,
1860 which has static storage duration. */
1862 tree
1863 get_guard (tree decl)
1865 tree sname;
1866 tree guard;
1868 sname = mangle_guard_variable (decl);
1869 guard = IDENTIFIER_GLOBAL_VALUE (sname);
1870 if (! guard)
1872 tree guard_type;
1874 /* We use a type that is big enough to contain a mutex as well
1875 as an integer counter. */
1876 guard_type = long_long_integer_type_node;
1877 guard = build_decl (VAR_DECL, sname, guard_type);
1879 /* The guard should have the same linkage as what it guards. */
1880 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
1881 TREE_STATIC (guard) = TREE_STATIC (decl);
1882 DECL_COMMON (guard) = DECL_COMMON (decl);
1883 DECL_ONE_ONLY (guard) = DECL_ONE_ONLY (decl);
1884 if (TREE_PUBLIC (decl))
1885 DECL_WEAK (guard) = DECL_WEAK (decl);
1887 DECL_ARTIFICIAL (guard) = 1;
1888 TREE_USED (guard) = 1;
1889 pushdecl_top_level_and_finish (guard, NULL_TREE);
1891 return guard;
1894 /* Return those bits of the GUARD variable that should be set when the
1895 guarded entity is actually initialized. */
1897 static tree
1898 get_guard_bits (tree guard)
1900 /* We only set the first byte of the guard, in order to leave room
1901 for a mutex in the high-order bits. */
1902 guard = build1 (ADDR_EXPR,
1903 build_pointer_type (TREE_TYPE (guard)),
1904 guard);
1905 guard = build1 (NOP_EXPR,
1906 build_pointer_type (char_type_node),
1907 guard);
1908 guard = build1 (INDIRECT_REF, char_type_node, guard);
1910 return guard;
1913 /* Return an expression which determines whether or not the GUARD
1914 variable has already been initialized. */
1916 tree
1917 get_guard_cond (tree guard)
1919 tree guard_value;
1921 /* Check to see if the GUARD is zero. */
1922 guard = get_guard_bits (guard);
1923 guard_value = integer_zero_node;
1924 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
1925 guard_value = convert (TREE_TYPE (guard), guard_value);
1926 return cp_build_binary_op (EQ_EXPR, guard, guard_value);
1929 /* Return an expression which sets the GUARD variable, indicating that
1930 the variable being guarded has been initialized. */
1932 tree
1933 set_guard (tree guard)
1935 tree guard_init;
1937 /* Set the GUARD to one. */
1938 guard = get_guard_bits (guard);
1939 guard_init = integer_one_node;
1940 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
1941 guard_init = convert (TREE_TYPE (guard), guard_init);
1942 return build_modify_expr (guard, NOP_EXPR, guard_init);
1945 /* Start the process of running a particular set of global constructors
1946 or destructors. Subroutine of do_[cd]tors. */
1948 static tree
1949 start_objects (int method_type, int initp)
1951 tree fnname;
1952 tree body;
1953 char type[10];
1955 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
1957 if (initp != DEFAULT_INIT_PRIORITY)
1959 char joiner;
1961 #ifdef JOINER
1962 joiner = JOINER;
1963 #else
1964 joiner = '_';
1965 #endif
1967 sprintf (type, "%c%c%.5u", method_type, joiner, initp);
1969 else
1970 sprintf (type, "%c", method_type);
1972 fnname = get_file_function_name_long (type);
1974 start_function (void_list_node,
1975 make_call_declarator (fnname, void_list_node, NULL_TREE,
1976 NULL_TREE),
1977 NULL_TREE, SF_DEFAULT);
1979 /* It can be a static function as long as collect2 does not have
1980 to scan the object file to find its ctor/dtor routine. */
1981 TREE_PUBLIC (current_function_decl) = ! targetm.have_ctors_dtors;
1983 /* Mark this declaration as used to avoid spurious warnings. */
1984 TREE_USED (current_function_decl) = 1;
1986 /* Mark this function as a global constructor or destructor. */
1987 if (method_type == 'I')
1988 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
1989 else
1990 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
1991 DECL_LANG_SPECIFIC (current_function_decl)->decl_flags.u2sel = 1;
1993 body = begin_compound_stmt (/*has_no_scope=*/0);
1995 /* We cannot allow these functions to be elided, even if they do not
1996 have external linkage. And, there's no point in deferring
1997 copmilation of thes functions; they're all going to have to be
1998 out anyhow. */
1999 current_function_cannot_inline
2000 = "static constructors and destructors cannot be inlined";
2002 return body;
2005 /* Finish the process of running a particular set of global constructors
2006 or destructors. Subroutine of do_[cd]tors. */
2008 static void
2009 finish_objects (int method_type, int initp, tree body)
2011 tree fn;
2013 /* Finish up. */
2014 finish_compound_stmt (/*has_no_scope=*/0, body);
2015 fn = finish_function (0);
2016 expand_body (fn);
2018 /* When only doing semantic analysis, and no RTL generation, we
2019 can't call functions that directly emit assembly code; there is
2020 no assembly file in which to put the code. */
2021 if (flag_syntax_only)
2022 return;
2024 if (targetm.have_ctors_dtors)
2026 rtx fnsym = XEXP (DECL_RTL (fn), 0);
2027 if (method_type == 'I')
2028 (* targetm.asm_out.constructor) (fnsym, initp);
2029 else
2030 (* targetm.asm_out.destructor) (fnsym, initp);
2034 /* The names of the parameters to the function created to handle
2035 initializations and destructions for objects with static storage
2036 duration. */
2037 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
2038 #define PRIORITY_IDENTIFIER "__priority"
2040 /* The name of the function we create to handle initializations and
2041 destructions for objects with static storage duration. */
2042 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
2044 /* The declaration for the __INITIALIZE_P argument. */
2045 static GTY(()) tree initialize_p_decl;
2047 /* The declaration for the __PRIORITY argument. */
2048 static GTY(()) tree priority_decl;
2050 /* The declaration for the static storage duration function. */
2051 static GTY(()) tree ssdf_decl;
2053 /* All the static storage duration functions created in this
2054 translation unit. */
2055 static GTY(()) varray_type ssdf_decls;
2057 /* A map from priority levels to information about that priority
2058 level. There may be many such levels, so efficient lookup is
2059 important. */
2060 static splay_tree priority_info_map;
2062 /* Begins the generation of the function that will handle all
2063 initialization and destruction of objects with static storage
2064 duration. The function generated takes two parameters of type
2065 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
2066 nonzero, it performs initializations. Otherwise, it performs
2067 destructions. It only performs those initializations or
2068 destructions with the indicated __PRIORITY. The generated function
2069 returns no value.
2071 It is assumed that this function will only be called once per
2072 translation unit. */
2074 static tree
2075 start_static_storage_duration_function (unsigned count)
2077 tree parm_types;
2078 tree type;
2079 tree body;
2080 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
2082 /* Create the identifier for this function. It will be of the form
2083 SSDF_IDENTIFIER_<number>. */
2084 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
2086 /* Create the parameters. */
2087 parm_types = void_list_node;
2088 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
2089 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
2090 type = build_function_type (void_type_node, parm_types);
2092 /* Create the FUNCTION_DECL itself. */
2093 ssdf_decl = build_lang_decl (FUNCTION_DECL,
2094 get_identifier (id),
2095 type);
2096 TREE_PUBLIC (ssdf_decl) = 0;
2097 DECL_ARTIFICIAL (ssdf_decl) = 1;
2099 /* Put this function in the list of functions to be called from the
2100 static constructors and destructors. */
2101 if (!ssdf_decls)
2103 VARRAY_TREE_INIT (ssdf_decls, 32, "ssdf_decls");
2105 /* Take this opportunity to initialize the map from priority
2106 numbers to information about that priority level. */
2107 priority_info_map = splay_tree_new (splay_tree_compare_ints,
2108 /*delete_key_fn=*/0,
2109 /*delete_value_fn=*/
2110 (splay_tree_delete_value_fn) &free);
2112 /* We always need to generate functions for the
2113 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
2114 priorities later, we'll be sure to find the
2115 DEFAULT_INIT_PRIORITY. */
2116 get_priority_info (DEFAULT_INIT_PRIORITY);
2119 VARRAY_PUSH_TREE (ssdf_decls, ssdf_decl);
2121 /* Create the argument list. */
2122 initialize_p_decl = cp_build_parm_decl
2123 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
2124 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
2125 TREE_USED (initialize_p_decl) = 1;
2126 priority_decl = cp_build_parm_decl
2127 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
2128 DECL_CONTEXT (priority_decl) = ssdf_decl;
2129 TREE_USED (priority_decl) = 1;
2131 TREE_CHAIN (initialize_p_decl) = priority_decl;
2132 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
2134 /* Put the function in the global scope. */
2135 pushdecl (ssdf_decl);
2137 /* Start the function itself. This is equivalent to declarating the
2138 function as:
2140 static void __ssdf (int __initialize_p, init __priority_p);
2142 It is static because we only need to call this function from the
2143 various constructor and destructor functions for this module. */
2144 start_function (/*specs=*/NULL_TREE,
2145 ssdf_decl,
2146 /*attrs=*/NULL_TREE,
2147 SF_PRE_PARSED);
2149 /* Set up the scope of the outermost block in the function. */
2150 body = begin_compound_stmt (/*has_no_scope=*/0);
2152 /* This function must not be deferred because we are depending on
2153 its compilation to tell us what is TREE_SYMBOL_REFERENCED. */
2154 current_function_cannot_inline
2155 = "static storage duration functions cannot be inlined";
2157 return body;
2160 /* Finish the generation of the function which performs initialization
2161 and destruction of objects with static storage duration. After
2162 this point, no more such objects can be created. */
2164 static void
2165 finish_static_storage_duration_function (tree body)
2167 /* Close out the function. */
2168 finish_compound_stmt (/*has_no_scope=*/0, body);
2169 expand_body (finish_function (0));
2172 /* Return the information about the indicated PRIORITY level. If no
2173 code to handle this level has yet been generated, generate the
2174 appropriate prologue. */
2176 static priority_info
2177 get_priority_info (int priority)
2179 priority_info pi;
2180 splay_tree_node n;
2182 n = splay_tree_lookup (priority_info_map,
2183 (splay_tree_key) priority);
2184 if (!n)
2186 /* Create a new priority information structure, and insert it
2187 into the map. */
2188 pi = (priority_info) xmalloc (sizeof (struct priority_info_s));
2189 pi->initializations_p = 0;
2190 pi->destructions_p = 0;
2191 splay_tree_insert (priority_info_map,
2192 (splay_tree_key) priority,
2193 (splay_tree_value) pi);
2195 else
2196 pi = (priority_info) n->value;
2198 return pi;
2201 /* Set up to handle the initialization or destruction of DECL. If
2202 INITP is nonzero, we are initializing the variable. Otherwise, we
2203 are destroying it. */
2205 static tree
2206 start_static_initialization_or_destruction (tree decl, int initp)
2208 tree guard_if_stmt = NULL_TREE;
2209 int priority;
2210 tree cond;
2211 tree guard;
2212 tree init_cond;
2213 priority_info pi;
2215 /* Figure out the priority for this declaration. */
2216 priority = DECL_INIT_PRIORITY (decl);
2217 if (!priority)
2218 priority = DEFAULT_INIT_PRIORITY;
2220 /* Remember that we had an initialization or finalization at this
2221 priority. */
2222 pi = get_priority_info (priority);
2223 if (initp)
2224 pi->initializations_p = 1;
2225 else
2226 pi->destructions_p = 1;
2228 /* Trick the compiler into thinking we are at the file and line
2229 where DECL was declared so that error-messages make sense, and so
2230 that the debugger will show somewhat sensible file and line
2231 information. */
2232 input_location = DECL_SOURCE_LOCATION (decl);
2234 /* Because of:
2236 [class.access.spec]
2238 Access control for implicit calls to the constructors,
2239 the conversion functions, or the destructor called to
2240 create and destroy a static data member is performed as
2241 if these calls appeared in the scope of the member's
2242 class.
2244 we pretend we are in a static member function of the class of
2245 which the DECL is a member. */
2246 if (member_p (decl))
2248 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
2249 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
2252 /* Conditionalize this initialization on being in the right priority
2253 and being initializing/finalizing appropriately. */
2254 guard_if_stmt = begin_if_stmt ();
2255 cond = cp_build_binary_op (EQ_EXPR,
2256 priority_decl,
2257 build_int_2 (priority, 0));
2258 init_cond = initp ? integer_one_node : integer_zero_node;
2259 init_cond = cp_build_binary_op (EQ_EXPR,
2260 initialize_p_decl,
2261 init_cond);
2262 cond = cp_build_binary_op (TRUTH_ANDIF_EXPR, cond, init_cond);
2264 /* Assume we don't need a guard. */
2265 guard = NULL_TREE;
2266 /* We need a guard if this is an object with external linkage that
2267 might be initialized in more than one place. (For example, a
2268 static data member of a template, when the data member requires
2269 construction.) */
2270 if (TREE_PUBLIC (decl) && (DECL_COMMON (decl)
2271 || DECL_ONE_ONLY (decl)
2272 || DECL_WEAK (decl)))
2274 tree guard_cond;
2276 guard = get_guard (decl);
2278 /* When using __cxa_atexit, we just check the GUARD as we would
2279 for a local static. */
2280 if (flag_use_cxa_atexit)
2282 /* When using __cxa_atexit, we never try to destroy
2283 anything from a static destructor. */
2284 my_friendly_assert (initp, 20000629);
2285 guard_cond = get_guard_cond (guard);
2287 /* If we don't have __cxa_atexit, then we will be running
2288 destructors from .fini sections, or their equivalents. So,
2289 we need to know how many times we've tried to initialize this
2290 object. We do initializations only if the GUARD is zero,
2291 i.e., if we are the first to initialize the variable. We do
2292 destructions only if the GUARD is one, i.e., if we are the
2293 last to destroy the variable. */
2294 else if (initp)
2295 guard_cond
2296 = cp_build_binary_op (EQ_EXPR,
2297 build_unary_op (PREINCREMENT_EXPR,
2298 guard,
2299 /*noconvert=*/1),
2300 integer_one_node);
2301 else
2302 guard_cond
2303 = cp_build_binary_op (EQ_EXPR,
2304 build_unary_op (PREDECREMENT_EXPR,
2305 guard,
2306 /*noconvert=*/1),
2307 integer_zero_node);
2309 cond = cp_build_binary_op (TRUTH_ANDIF_EXPR, cond, guard_cond);
2312 finish_if_stmt_cond (cond, guard_if_stmt);
2314 /* If we're using __cxa_atexit, we have not already set the GUARD,
2315 so we must do so now. */
2316 if (guard && initp && flag_use_cxa_atexit)
2317 finish_expr_stmt (set_guard (guard));
2319 return guard_if_stmt;
2322 /* We've just finished generating code to do an initialization or
2323 finalization. GUARD_IF_STMT is the if-statement we used to guard
2324 the initialization. */
2326 static void
2327 finish_static_initialization_or_destruction (tree guard_if_stmt)
2329 finish_then_clause (guard_if_stmt);
2330 finish_if_stmt ();
2332 /* Now that we're done with DECL we don't need to pretend to be a
2333 member of its class any longer. */
2334 DECL_CONTEXT (current_function_decl) = NULL_TREE;
2335 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
2338 /* Generate code to do the initialization of DECL, a VAR_DECL with
2339 static storage duration. The initialization is INIT. */
2341 static void
2342 do_static_initialization (tree decl, tree init)
2344 tree guard_if_stmt;
2346 /* Set up for the initialization. */
2347 guard_if_stmt
2348 = start_static_initialization_or_destruction (decl,
2349 /*initp=*/1);
2351 /* Perform the initialization. */
2352 if (init)
2353 finish_expr_stmt (init);
2355 /* If we're using __cxa_atexit, register a a function that calls the
2356 destructor for the object. */
2357 if (flag_use_cxa_atexit)
2358 register_dtor_fn (decl);
2360 /* Finsh up. */
2361 finish_static_initialization_or_destruction (guard_if_stmt);
2364 /* Generate code to do the static destruction of DECL. If DECL may be
2365 initialized more than once in different object files, GUARD is the
2366 guard variable to check. PRIORITY is the priority for the
2367 destruction. */
2369 static void
2370 do_static_destruction (tree decl)
2372 tree guard_if_stmt;
2374 /* If we're using __cxa_atexit, then destructors are registered
2375 immediately after objects are initialized. */
2376 my_friendly_assert (!flag_use_cxa_atexit, 20000121);
2378 /* If we don't need a destructor, there's nothing to do. */
2379 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
2380 return;
2382 /* Actually do the destruction. */
2383 guard_if_stmt = start_static_initialization_or_destruction (decl,
2384 /*initp=*/0);
2385 finish_expr_stmt (build_cleanup (decl));
2386 finish_static_initialization_or_destruction (guard_if_stmt);
2389 /* VARS is a list of variables with static storage duration which may
2390 need initialization and/or finalization. Remove those variables
2391 that don't really need to be initialized or finalized, and return
2392 the resulting list. The order in which the variables appear in
2393 VARS is in reverse order of the order in which they should actually
2394 be initialized. The list we return is in the unreversed order;
2395 i.e., the first variable should be initialized first. */
2397 static tree
2398 prune_vars_needing_no_initialization (tree *vars)
2400 tree *var = vars;
2401 tree result = NULL_TREE;
2403 while (*var)
2405 tree t = *var;
2406 tree decl = TREE_VALUE (t);
2407 tree init = TREE_PURPOSE (t);
2409 /* Deal gracefully with error. */
2410 if (decl == error_mark_node)
2412 var = &TREE_CHAIN (t);
2413 continue;
2416 /* The only things that can be initialized are variables. */
2417 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 19990420);
2419 /* If this object is not defined, we don't need to do anything
2420 here. */
2421 if (DECL_EXTERNAL (decl))
2423 var = &TREE_CHAIN (t);
2424 continue;
2427 /* Also, if the initializer already contains errors, we can bail
2428 out now. */
2429 if (init && TREE_CODE (init) == TREE_LIST
2430 && value_member (error_mark_node, init))
2432 var = &TREE_CHAIN (t);
2433 continue;
2436 /* This variable is going to need initialization and/or
2437 finalization, so we add it to the list. */
2438 *var = TREE_CHAIN (t);
2439 TREE_CHAIN (t) = result;
2440 result = t;
2443 return result;
2446 /* Make sure we have told the back end about all the variables in
2447 VARS. */
2449 static void
2450 write_out_vars (tree vars)
2452 tree v;
2454 for (v = vars; v; v = TREE_CHAIN (v))
2455 if (! TREE_ASM_WRITTEN (TREE_VALUE (v)))
2456 rest_of_decl_compilation (TREE_VALUE (v), 0, 1, 1);
2459 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
2460 (otherwise) that will initialize all gobal objects with static
2461 storage duration having the indicated PRIORITY. */
2463 static void
2464 generate_ctor_or_dtor_function (bool constructor_p, int priority,
2465 location_t *locus)
2467 char function_key;
2468 tree arguments;
2469 tree fndecl;
2470 tree body;
2471 size_t i;
2473 input_location = *locus;
2474 locus->line++;
2476 /* We use `I' to indicate initialization and `D' to indicate
2477 destruction. */
2478 function_key = constructor_p ? 'I' : 'D';
2480 /* We emit the function lazily, to avoid generating empty
2481 global constructors and destructors. */
2482 body = NULL_TREE;
2484 /* Call the static storage duration function with appropriate
2485 arguments. */
2486 if (ssdf_decls)
2487 for (i = 0; i < ssdf_decls->elements_used; ++i)
2489 fndecl = VARRAY_TREE (ssdf_decls, i);
2491 /* Calls to pure or const functions will expand to nothing. */
2492 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
2494 if (! body)
2495 body = start_objects (function_key, priority);
2497 arguments = tree_cons (NULL_TREE, build_int_2 (priority, 0),
2498 NULL_TREE);
2499 arguments = tree_cons (NULL_TREE, build_int_2 (constructor_p, 0),
2500 arguments);
2501 finish_expr_stmt (build_function_call (fndecl, arguments));
2505 /* If we're generating code for the DEFAULT_INIT_PRIORITY, throw in
2506 calls to any functions marked with attributes indicating that
2507 they should be called at initialization- or destruction-time. */
2508 if (priority == DEFAULT_INIT_PRIORITY)
2510 tree fns;
2512 for (fns = constructor_p ? static_ctors : static_dtors;
2513 fns;
2514 fns = TREE_CHAIN (fns))
2516 fndecl = TREE_VALUE (fns);
2518 /* Calls to pure/const functions will expand to nothing. */
2519 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
2521 if (! body)
2522 body = start_objects (function_key, priority);
2523 finish_expr_stmt (build_function_call (fndecl, NULL_TREE));
2528 /* Close out the function. */
2529 if (body)
2530 finish_objects (function_key, priority, body);
2533 /* Generate constructor and destructor functions for the priority
2534 indicated by N. */
2536 static int
2537 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
2539 location_t *locus = data;
2540 int priority = (int) n->key;
2541 priority_info pi = (priority_info) n->value;
2543 /* Generate the functions themselves, but only if they are really
2544 needed. */
2545 if (pi->initializations_p
2546 || (priority == DEFAULT_INIT_PRIORITY && static_ctors))
2547 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
2548 if (pi->destructions_p
2549 || (priority == DEFAULT_INIT_PRIORITY && static_dtors))
2550 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
2552 /* Keep iterating. */
2553 return 0;
2556 /* This routine is called from the last rule in yyparse ().
2557 Its job is to create all the code needed to initialize and
2558 destroy the global aggregates. We do the destruction
2559 first, since that way we only need to reverse the decls once. */
2561 void
2562 finish_file ()
2564 tree vars;
2565 bool reconsider;
2566 size_t i;
2567 location_t locus;
2568 unsigned ssdf_count = 0;
2570 locus = input_location;
2571 at_eof = 1;
2573 /* Bad parse errors. Just forget about it. */
2574 if (! global_bindings_p () || current_class_type || decl_namespace_list)
2575 return;
2577 if (pch_file)
2578 c_common_write_pch ();
2580 /* Otherwise, GDB can get confused, because in only knows
2581 about source for LINENO-1 lines. */
2582 input_line -= 1;
2584 interface_unknown = 1;
2585 interface_only = 0;
2587 /* We now have to write out all the stuff we put off writing out.
2588 These include:
2590 o Template specializations that we have not yet instantiated,
2591 but which are needed.
2592 o Initialization and destruction for non-local objects with
2593 static storage duration. (Local objects with static storage
2594 duration are initialized when their scope is first entered,
2595 and are cleaned up via atexit.)
2596 o Virtual function tables.
2598 All of these may cause others to be needed. For example,
2599 instantiating one function may cause another to be needed, and
2600 generating the initializer for an object may cause templates to be
2601 instantiated, etc., etc. */
2603 timevar_push (TV_VARCONST);
2605 emit_support_tinfos ();
2609 tree t;
2610 size_t n_old, n_new;
2612 reconsider = false;
2614 /* If there are templates that we've put off instantiating, do
2615 them now. */
2616 instantiate_pending_templates ();
2618 /* Write out virtual tables as required. Note that writing out
2619 the virtual table for a template class may cause the
2620 instantiation of members of that class. If we write out
2621 vtables then we remove the class from our list so we don't
2622 have to look at it again. */
2624 while (keyed_classes != NULL_TREE
2625 && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
2627 reconsider = true;
2628 keyed_classes = TREE_CHAIN (keyed_classes);
2631 t = keyed_classes;
2632 if (t != NULL_TREE)
2634 tree next = TREE_CHAIN (t);
2636 while (next)
2638 if (maybe_emit_vtables (TREE_VALUE (next)))
2640 reconsider = true;
2641 TREE_CHAIN (t) = TREE_CHAIN (next);
2643 else
2644 t = next;
2646 next = TREE_CHAIN (t);
2650 /* Write out needed type info variables. We have to be careful
2651 looping through unemitted decls, because emit_tinfo_decl may
2652 cause other variables to be needed. We stick new elements
2653 (and old elements that we may need to reconsider) at the end
2654 of the array, then shift them back to the beginning once we're
2655 done. */
2657 n_old = VARRAY_ACTIVE_SIZE (unemitted_tinfo_decls);
2658 for (i = 0; i < n_old; ++i)
2660 tree tinfo_decl = VARRAY_TREE (unemitted_tinfo_decls, i);
2661 if (emit_tinfo_decl (tinfo_decl))
2662 reconsider = true;
2663 else
2664 VARRAY_PUSH_TREE (unemitted_tinfo_decls, tinfo_decl);
2667 /* The only elements we want to keep are the new ones. Copy
2668 them to the beginning of the array, then get rid of the
2669 leftovers. */
2670 n_new = VARRAY_ACTIVE_SIZE (unemitted_tinfo_decls) - n_old;
2671 memmove (&VARRAY_TREE (unemitted_tinfo_decls, 0),
2672 &VARRAY_TREE (unemitted_tinfo_decls, n_old),
2673 n_new * sizeof (tree));
2674 memset (&VARRAY_TREE (unemitted_tinfo_decls, n_new),
2676 n_old * sizeof (tree));
2677 VARRAY_ACTIVE_SIZE (unemitted_tinfo_decls) = n_new;
2679 /* The list of objects with static storage duration is built up
2680 in reverse order. We clear STATIC_AGGREGATES so that any new
2681 aggregates added during the initialization of these will be
2682 initialized in the correct order when we next come around the
2683 loop. */
2684 vars = prune_vars_needing_no_initialization (&static_aggregates);
2686 if (vars)
2688 tree v;
2690 /* We need to start a new initialization function each time
2691 through the loop. That's because we need to know which
2692 vtables have been referenced, and TREE_SYMBOL_REFERENCED
2693 isn't computed until a function is finished, and written
2694 out. That's a deficiency in the back-end. When this is
2695 fixed, these initialization functions could all become
2696 inline, with resulting performance improvements. */
2697 tree ssdf_body;
2699 /* Set the line and file, so that it is obviously not from
2700 the source file. */
2701 input_location = locus;
2702 ssdf_body = start_static_storage_duration_function (ssdf_count);
2704 /* Make sure the back end knows about all the variables. */
2705 write_out_vars (vars);
2707 /* First generate code to do all the initializations. */
2708 for (v = vars; v; v = TREE_CHAIN (v))
2709 do_static_initialization (TREE_VALUE (v),
2710 TREE_PURPOSE (v));
2712 /* Then, generate code to do all the destructions. Do these
2713 in reverse order so that the most recently constructed
2714 variable is the first destroyed. If we're using
2715 __cxa_atexit, then we don't need to do this; functions
2716 were registered at initialization time to destroy the
2717 local statics. */
2718 if (!flag_use_cxa_atexit)
2720 vars = nreverse (vars);
2721 for (v = vars; v; v = TREE_CHAIN (v))
2722 do_static_destruction (TREE_VALUE (v));
2724 else
2725 vars = NULL_TREE;
2727 /* Finish up the static storage duration function for this
2728 round. */
2729 input_location = locus;
2730 finish_static_storage_duration_function (ssdf_body);
2732 /* All those initializations and finalizations might cause
2733 us to need more inline functions, more template
2734 instantiations, etc. */
2735 reconsider = true;
2736 ssdf_count++;
2737 locus.line++;
2740 for (i = 0; i < deferred_fns_used; ++i)
2742 tree decl = VARRAY_TREE (deferred_fns, i);
2744 /* Does it need synthesizing? */
2745 if (DECL_ARTIFICIAL (decl) && ! DECL_INITIAL (decl)
2746 && TREE_USED (decl)
2747 && (! DECL_REALLY_EXTERN (decl) || DECL_INLINE (decl)))
2749 /* Even though we're already at the top-level, we push
2750 there again. That way, when we pop back a few lines
2751 hence, all of our state is restored. Otherwise,
2752 finish_function doesn't clean things up, and we end
2753 up with CURRENT_FUNCTION_DECL set. */
2754 push_to_top_level ();
2755 synthesize_method (decl);
2756 pop_from_top_level ();
2757 reconsider = true;
2760 /* If the function has no body, avoid calling
2761 import_export_decl. On a system without weak symbols,
2762 calling import_export_decl will make an inline template
2763 instantiation "static", which will result in errors about
2764 the use of undefined functions if there is no body for
2765 the function. */
2766 if (!DECL_SAVED_TREE (decl))
2767 continue;
2769 import_export_decl (decl);
2771 /* We lie to the back-end, pretending that some functions
2772 are not defined when they really are. This keeps these
2773 functions from being put out unnecessarily. But, we must
2774 stop lying when the functions are referenced, or if they
2775 are not comdat since they need to be put out now. This
2776 is done in a separate for cycle, because if some deferred
2777 function is contained in another deferred function later
2778 in deferred_fns varray, rest_of_compilation would skip
2779 this function and we really cannot expand the same
2780 function twice. */
2781 if (DECL_NOT_REALLY_EXTERN (decl)
2782 && DECL_INITIAL (decl)
2783 && DECL_NEEDED_P (decl))
2784 DECL_EXTERNAL (decl) = 0;
2786 /* If we're going to need to write this function out, and
2787 there's already a body for it, create RTL for it now.
2788 (There might be no body if this is a method we haven't
2789 gotten around to synthesizing yet.) */
2790 if (!DECL_EXTERNAL (decl)
2791 && DECL_NEEDED_P (decl)
2792 && DECL_SAVED_TREE (decl)
2793 && !TREE_ASM_WRITTEN (decl))
2795 int saved_not_really_extern;
2797 /* When we call finish_function in expand_body, it will
2798 try to reset DECL_NOT_REALLY_EXTERN so we save and
2799 restore it here. */
2800 saved_not_really_extern = DECL_NOT_REALLY_EXTERN (decl);
2801 /* Generate RTL for this function now that we know we
2802 need it. */
2803 expand_body (decl);
2804 /* Undo the damage done by finish_function. */
2805 DECL_EXTERNAL (decl) = 0;
2806 DECL_NOT_REALLY_EXTERN (decl) = saved_not_really_extern;
2807 /* If we're compiling -fsyntax-only pretend that this
2808 function has been written out so that we don't try to
2809 expand it again. */
2810 if (flag_syntax_only)
2811 TREE_ASM_WRITTEN (decl) = 1;
2812 reconsider = true;
2816 if (deferred_fns_used
2817 && wrapup_global_declarations (&VARRAY_TREE (deferred_fns, 0),
2818 deferred_fns_used))
2819 reconsider = true;
2820 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
2821 reconsider = true;
2823 /* Static data members are just like namespace-scope globals. */
2824 for (i = 0; i < pending_statics_used; ++i)
2826 tree decl = VARRAY_TREE (pending_statics, i);
2827 if (TREE_ASM_WRITTEN (decl))
2828 continue;
2829 import_export_decl (decl);
2830 if (DECL_NOT_REALLY_EXTERN (decl) && ! DECL_IN_AGGR_P (decl))
2831 DECL_EXTERNAL (decl) = 0;
2833 if (pending_statics
2834 && wrapup_global_declarations (&VARRAY_TREE (pending_statics, 0),
2835 pending_statics_used))
2836 reconsider = true;
2838 while (reconsider);
2840 /* All used inline functions must have a definition at this point. */
2841 for (i = 0; i < deferred_fns_used; ++i)
2843 tree decl = VARRAY_TREE (deferred_fns, i);
2845 if (TREE_USED (decl) && DECL_DECLARED_INLINE_P (decl)
2846 && !(TREE_ASM_WRITTEN (decl) || DECL_SAVED_TREE (decl)
2847 /* An explicit instantiation can be used to specify
2848 that the body is in another unit. It will have
2849 already verified there was a definition. */
2850 || DECL_EXPLICIT_INSTANTIATION (decl)))
2852 cp_warning_at ("inline function `%D' used but never defined", decl);
2853 /* This symbol is effectively an "extern" declaration now.
2854 This is not strictly necessary, but removes a duplicate
2855 warning. */
2856 TREE_PUBLIC (decl) = 1;
2861 /* We give C linkage to static constructors and destructors. */
2862 push_lang_context (lang_name_c);
2864 /* Generate initialization and destruction functions for all
2865 priorities for which they are required. */
2866 if (priority_info_map)
2867 splay_tree_foreach (priority_info_map,
2868 generate_ctor_and_dtor_functions_for_priority,
2869 /*data=*/&locus);
2870 else
2873 if (static_ctors)
2874 generate_ctor_or_dtor_function (/*constructor_p=*/true,
2875 DEFAULT_INIT_PRIORITY, &locus);
2876 if (static_dtors)
2877 generate_ctor_or_dtor_function (/*constructor_p=*/false,
2878 DEFAULT_INIT_PRIORITY, &locus);
2881 /* We're done with the splay-tree now. */
2882 if (priority_info_map)
2883 splay_tree_delete (priority_info_map);
2885 /* We're done with static constructors, so we can go back to "C++"
2886 linkage now. */
2887 pop_lang_context ();
2889 /* Now, issue warnings about static, but not defined, functions,
2890 etc., and emit debugging information. */
2891 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
2892 if (pending_statics)
2893 check_global_declarations (&VARRAY_TREE (pending_statics, 0),
2894 pending_statics_used);
2896 finish_repo ();
2898 /* The entire file is now complete. If requested, dump everything
2899 to a file. */
2901 int flags;
2902 FILE *stream = dump_begin (TDI_all, &flags);
2904 if (stream)
2906 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
2907 dump_end (TDI_all, stream);
2911 timevar_pop (TV_VARCONST);
2913 if (flag_detailed_statistics)
2915 dump_tree_statistics ();
2916 dump_time_statistics ();
2918 input_location = locus;
2921 /* T is the parse tree for an expression. Return the expression after
2922 performing semantic analysis. */
2924 tree
2925 build_expr_from_tree (t)
2926 tree t;
2928 if (t == NULL_TREE || t == error_mark_node)
2929 return t;
2931 switch (TREE_CODE (t))
2933 case IDENTIFIER_NODE:
2934 return do_identifier (t, NULL_TREE);
2936 case LOOKUP_EXPR:
2937 if (LOOKUP_EXPR_GLOBAL (t))
2939 tree token = TREE_OPERAND (t, 0);
2940 return do_scoped_id (token, IDENTIFIER_GLOBAL_VALUE (token));
2942 else
2944 t = do_identifier (TREE_OPERAND (t, 0), NULL_TREE);
2945 if (TREE_CODE (t) == ALIAS_DECL)
2946 t = DECL_INITIAL (t);
2947 return t;
2950 case TEMPLATE_ID_EXPR:
2952 tree template;
2953 tree args;
2954 tree object;
2956 template = build_expr_from_tree (TREE_OPERAND (t, 0));
2957 args = build_expr_from_tree (TREE_OPERAND (t, 1));
2959 if (TREE_CODE (template) == COMPONENT_REF)
2961 object = TREE_OPERAND (template, 0);
2962 template = TREE_OPERAND (template, 1);
2964 else
2965 object = NULL_TREE;
2967 template = lookup_template_function (template, args);
2968 if (object)
2969 return build (COMPONENT_REF, TREE_TYPE (template),
2970 object, template);
2971 else
2972 return template;
2975 case INDIRECT_REF:
2976 return build_x_indirect_ref
2977 (build_expr_from_tree (TREE_OPERAND (t, 0)), "unary *");
2979 case CAST_EXPR:
2980 return build_functional_cast
2981 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
2983 case REINTERPRET_CAST_EXPR:
2984 return build_reinterpret_cast
2985 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
2987 case CONST_CAST_EXPR:
2988 return build_const_cast
2989 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
2991 case DYNAMIC_CAST_EXPR:
2992 return build_dynamic_cast
2993 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
2995 case STATIC_CAST_EXPR:
2996 return build_static_cast
2997 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
2999 case PREDECREMENT_EXPR:
3000 case PREINCREMENT_EXPR:
3001 case POSTDECREMENT_EXPR:
3002 case POSTINCREMENT_EXPR:
3003 case NEGATE_EXPR:
3004 case BIT_NOT_EXPR:
3005 case ABS_EXPR:
3006 case TRUTH_NOT_EXPR:
3007 case ADDR_EXPR:
3008 case CONVERT_EXPR: /* Unary + */
3009 case REALPART_EXPR:
3010 case IMAGPART_EXPR:
3011 if (TREE_TYPE (t))
3012 return t;
3013 return build_x_unary_op (TREE_CODE (t),
3014 build_expr_from_tree (TREE_OPERAND (t, 0)));
3016 case PLUS_EXPR:
3017 case MINUS_EXPR:
3018 case MULT_EXPR:
3019 case TRUNC_DIV_EXPR:
3020 case CEIL_DIV_EXPR:
3021 case FLOOR_DIV_EXPR:
3022 case ROUND_DIV_EXPR:
3023 case EXACT_DIV_EXPR:
3024 case BIT_AND_EXPR:
3025 case BIT_ANDTC_EXPR:
3026 case BIT_IOR_EXPR:
3027 case BIT_XOR_EXPR:
3028 case TRUNC_MOD_EXPR:
3029 case FLOOR_MOD_EXPR:
3030 case TRUTH_ANDIF_EXPR:
3031 case TRUTH_ORIF_EXPR:
3032 case TRUTH_AND_EXPR:
3033 case TRUTH_OR_EXPR:
3034 case RSHIFT_EXPR:
3035 case LSHIFT_EXPR:
3036 case RROTATE_EXPR:
3037 case LROTATE_EXPR:
3038 case EQ_EXPR:
3039 case NE_EXPR:
3040 case MAX_EXPR:
3041 case MIN_EXPR:
3042 case LE_EXPR:
3043 case GE_EXPR:
3044 case LT_EXPR:
3045 case GT_EXPR:
3046 case MEMBER_REF:
3047 return build_x_binary_op
3048 (TREE_CODE (t),
3049 build_expr_from_tree (TREE_OPERAND (t, 0)),
3050 build_expr_from_tree (TREE_OPERAND (t, 1)));
3052 case DOTSTAR_EXPR:
3053 return build_m_component_ref
3054 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3055 build_expr_from_tree (TREE_OPERAND (t, 1)));
3057 case SCOPE_REF:
3058 return build_offset_ref (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1));
3060 case ARRAY_REF:
3061 if (TREE_OPERAND (t, 0) == NULL_TREE)
3062 /* new-type-id */
3063 return build_nt (ARRAY_REF, NULL_TREE,
3064 build_expr_from_tree (TREE_OPERAND (t, 1)));
3065 return grok_array_decl (build_expr_from_tree (TREE_OPERAND (t, 0)),
3066 build_expr_from_tree (TREE_OPERAND (t, 1)));
3068 case SIZEOF_EXPR:
3069 case ALIGNOF_EXPR:
3071 tree r = build_expr_from_tree (TREE_OPERAND (t, 0));
3072 if (!TYPE_P (r))
3073 return TREE_CODE (t) == SIZEOF_EXPR ? expr_sizeof (r) : c_alignof_expr (r);
3074 else
3075 return cxx_sizeof_or_alignof_type (r, TREE_CODE (t), true);
3078 case MODOP_EXPR:
3079 return build_x_modify_expr
3080 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3081 TREE_CODE (TREE_OPERAND (t, 1)),
3082 build_expr_from_tree (TREE_OPERAND (t, 2)));
3084 case ARROW_EXPR:
3085 return build_x_arrow
3086 (build_expr_from_tree (TREE_OPERAND (t, 0)));
3088 case NEW_EXPR:
3089 return build_new
3090 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3091 build_expr_from_tree (TREE_OPERAND (t, 1)),
3092 build_expr_from_tree (TREE_OPERAND (t, 2)),
3093 NEW_EXPR_USE_GLOBAL (t));
3095 case DELETE_EXPR:
3096 return delete_sanity
3097 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3098 build_expr_from_tree (TREE_OPERAND (t, 1)),
3099 DELETE_EXPR_USE_VEC (t), DELETE_EXPR_USE_GLOBAL (t));
3101 case COMPOUND_EXPR:
3102 if (TREE_OPERAND (t, 1) == NULL_TREE)
3103 return build_x_compound_expr
3104 (build_expr_from_tree (TREE_OPERAND (t, 0)));
3105 else
3106 abort ();
3108 case METHOD_CALL_EXPR:
3109 if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
3111 tree ref = TREE_OPERAND (t, 0);
3112 tree name = TREE_OPERAND (ref, 1);
3114 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3115 name = build_nt (TEMPLATE_ID_EXPR,
3116 TREE_OPERAND (name, 0),
3117 build_expr_from_tree (TREE_OPERAND (name, 1)));
3119 return build_scoped_method_call
3120 (build_expr_from_tree (TREE_OPERAND (t, 1)),
3121 build_expr_from_tree (TREE_OPERAND (ref, 0)),
3122 name,
3123 build_expr_from_tree (TREE_OPERAND (t, 2)));
3125 else
3127 tree fn = TREE_OPERAND (t, 0);
3129 /* We can get a TEMPLATE_ID_EXPR here on code like:
3131 x->f<2>();
3133 so we must resolve that. However, we can also get things
3134 like a BIT_NOT_EXPR here, when referring to a destructor,
3135 and things like that are not correctly resolved by
3136 build_expr_from_tree. So, just use build_expr_from_tree
3137 when we really need it. */
3138 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3139 fn = lookup_template_function
3140 (TREE_OPERAND (fn, 0),
3141 build_expr_from_tree (TREE_OPERAND (fn, 1)));
3143 return build_method_call
3144 (build_expr_from_tree (TREE_OPERAND (t, 1)),
3146 build_expr_from_tree (TREE_OPERAND (t, 2)),
3147 NULL_TREE, LOOKUP_NORMAL);
3150 case CALL_EXPR:
3151 if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
3153 tree ref = TREE_OPERAND (t, 0);
3154 tree name = TREE_OPERAND (ref, 1);
3155 tree fn, scope, args;
3157 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3158 name = build_nt (TEMPLATE_ID_EXPR,
3159 TREE_OPERAND (name, 0),
3160 build_expr_from_tree (TREE_OPERAND (name, 1)));
3162 scope = build_expr_from_tree (TREE_OPERAND (ref, 0));
3163 args = build_expr_from_tree (TREE_OPERAND (t, 1));
3164 fn = resolve_scoped_fn_name (scope, name);
3166 return build_call_from_tree (fn, args, 1);
3168 else
3170 tree name = TREE_OPERAND (t, 0);
3171 tree id;
3172 tree args = build_expr_from_tree (TREE_OPERAND (t, 1));
3173 if (args != NULL_TREE && TREE_CODE (name) == LOOKUP_EXPR
3174 && !LOOKUP_EXPR_GLOBAL (name)
3175 && TREE_CODE ((id = TREE_OPERAND (name, 0))) == IDENTIFIER_NODE
3176 && (!current_class_type
3177 || !lookup_member (current_class_type, id, 0, false)))
3179 /* Do Koenig lookup if there are no class members. */
3180 name = do_identifier (id, args);
3182 else if (TREE_CODE (name) == TEMPLATE_ID_EXPR
3183 || ! really_overloaded_fn (name))
3184 name = build_expr_from_tree (name);
3186 if (TREE_CODE (name) == OFFSET_REF)
3187 return build_offset_ref_call_from_tree (name, args);
3188 if (TREE_CODE (name) == COMPONENT_REF)
3189 return finish_object_call_expr (TREE_OPERAND (name, 1),
3190 TREE_OPERAND (name, 0),
3191 args);
3192 name = convert_from_reference (name);
3193 return build_call_from_tree (name, args,
3194 /*disallow_virtual=*/false);
3197 case COND_EXPR:
3198 return build_x_conditional_expr
3199 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3200 build_expr_from_tree (TREE_OPERAND (t, 1)),
3201 build_expr_from_tree (TREE_OPERAND (t, 2)));
3203 case PSEUDO_DTOR_EXPR:
3204 return (finish_pseudo_destructor_expr
3205 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3206 build_expr_from_tree (TREE_OPERAND (t, 1)),
3207 build_expr_from_tree (TREE_OPERAND (t, 2))));
3209 case TREE_LIST:
3211 tree purpose, value, chain;
3213 if (t == void_list_node)
3214 return t;
3216 purpose = TREE_PURPOSE (t);
3217 if (purpose)
3218 purpose = build_expr_from_tree (purpose);
3219 value = TREE_VALUE (t);
3220 if (value)
3221 value = build_expr_from_tree (value);
3222 chain = TREE_CHAIN (t);
3223 if (chain && chain != void_type_node)
3224 chain = build_expr_from_tree (chain);
3225 return tree_cons (purpose, value, chain);
3228 case COMPONENT_REF:
3230 tree object = build_expr_from_tree (TREE_OPERAND (t, 0));
3231 tree member = TREE_OPERAND (t, 1);
3233 if (!CLASS_TYPE_P (TREE_TYPE (object)))
3235 if (TREE_CODE (member) == BIT_NOT_EXPR)
3236 return finish_pseudo_destructor_expr (object,
3237 NULL_TREE,
3238 TREE_TYPE (object));
3239 else if (TREE_CODE (member) == SCOPE_REF
3240 && (TREE_CODE (TREE_OPERAND (member, 1)) == BIT_NOT_EXPR))
3241 return finish_pseudo_destructor_expr (object,
3242 TREE_OPERAND (t, 0),
3243 TREE_TYPE (object));
3245 else if (TREE_CODE (member) == SCOPE_REF
3246 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
3248 tree tmpl;
3249 tree args;
3251 /* Lookup the template functions now that we know what the
3252 scope is. */
3253 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
3254 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
3255 member = lookup_qualified_name (TREE_OPERAND (member, 0),
3256 tmpl,
3257 /*is_type=*/0,
3258 /*flags=*/0);
3259 if (BASELINK_P (member))
3260 BASELINK_FUNCTIONS (member)
3261 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
3262 args);
3263 else
3265 error ("`%D' is not a member of `%T'",
3266 tmpl, TREE_TYPE (object));
3267 return error_mark_node;
3272 return finish_class_member_access_expr (object, member);
3275 case THROW_EXPR:
3276 return build_throw (build_expr_from_tree (TREE_OPERAND (t, 0)));
3278 case CONSTRUCTOR:
3280 tree r;
3281 tree elts;
3282 tree type = TREE_TYPE (t);
3283 bool purpose_p;
3285 /* digest_init will do the wrong thing if we let it. */
3286 if (type && TYPE_PTRMEMFUNC_P (type))
3287 return t;
3289 r = NULL_TREE;
3290 /* We do not want to process the purpose of aggregate
3291 initializers as they are identifier nodes which will be
3292 looked up by digest_init. */
3293 purpose_p = !(type && IS_AGGR_TYPE (type));
3294 for (elts = CONSTRUCTOR_ELTS (t); elts; elts = TREE_CHAIN (elts))
3296 tree purpose = TREE_PURPOSE (elts);
3297 tree value = TREE_VALUE (elts);
3299 if (purpose && purpose_p)
3300 purpose = build_expr_from_tree (purpose);
3301 value = build_expr_from_tree (value);
3302 r = tree_cons (purpose, value, r);
3305 r = build_constructor (NULL_TREE, nreverse (r));
3306 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
3308 if (type)
3309 return digest_init (type, r, 0);
3310 return r;
3313 case TYPEID_EXPR:
3314 if (TYPE_P (TREE_OPERAND (t, 0)))
3315 return get_typeid (TREE_OPERAND (t, 0));
3316 return build_typeid (build_expr_from_tree (TREE_OPERAND (t, 0)));
3318 case PARM_DECL:
3319 case VAR_DECL:
3320 return convert_from_reference (t);
3322 case VA_ARG_EXPR:
3323 return build_va_arg (build_expr_from_tree (TREE_OPERAND (t, 0)),
3324 TREE_TYPE (t));
3326 default:
3327 return t;
3331 /* FN is an OFFSET_REF indicating the function to call in parse-tree
3332 form; it has not yet been semantically analyzed. ARGS are the
3333 arguments to the function. They have already been semantically
3334 analzyed. */
3336 tree
3337 build_offset_ref_call_from_tree (tree fn, tree args)
3339 tree object_addr;
3341 my_friendly_assert (TREE_CODE (fn) == OFFSET_REF, 20020725);
3343 /* A qualified name corresponding to a non-static member
3344 function or a pointer-to-member is represented as an
3345 OFFSET_REF.
3347 For both of these function calls, FN will be an OFFSET_REF.
3349 struct A { void f(); };
3350 void A::f() { (A::f) (); }
3352 struct B { void g(); };
3353 void (B::*p)();
3354 void B::g() { (this->*p)(); } */
3356 /* This code is not really correct (for example, it does not
3357 handle the case that `A::f' is overloaded), but it is
3358 historically how we have handled this situation. */
3359 if (TREE_CODE (TREE_OPERAND (fn, 1)) == FIELD_DECL)
3360 /* This case should now be handled elsewhere. */
3361 abort ();
3362 else
3364 object_addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (fn, 0), 0);
3365 fn = TREE_OPERAND (fn, 1);
3366 fn = get_member_function_from_ptrfunc (&object_addr, fn);
3367 args = tree_cons (NULL_TREE, object_addr, args);
3369 return build_function_call (fn, args);
3372 /* FN indicates the function to call. Name resolution has been
3373 performed on FN. ARGS are the arguments to the function. They
3374 have already been semantically analyzed. DISALLOW_VIRTUAL is true
3375 if the function call should be determined at compile time, even if
3376 FN is virtual. */
3378 tree
3379 build_call_from_tree (tree fn, tree args, bool disallow_virtual)
3381 tree template_args;
3382 tree template_id;
3383 tree f;
3385 /* Check to see that name lookup has already been performed. */
3386 my_friendly_assert (TREE_CODE (fn) != OFFSET_REF, 20020725);
3387 my_friendly_assert (TREE_CODE (fn) != SCOPE_REF, 20020725);
3389 /* In the future all of this should be eliminated. Instead,
3390 name-lookup for a member function should simply return a
3391 baselink, instead of a FUNCTION_DECL, TEMPLATE_DECL, or
3392 TEMPLATE_ID_EXPR. */
3394 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3396 template_id = fn;
3397 template_args = TREE_OPERAND (fn, 1);
3398 fn = TREE_OPERAND (fn, 0);
3400 else
3402 template_id = NULL_TREE;
3403 template_args = NULL_TREE;
3406 f = (TREE_CODE (fn) == OVERLOAD) ? get_first_fn (fn) : fn;
3407 /* Make sure we have a baselink (rather than simply a
3408 FUNCTION_DECL) for a member function. */
3409 if (current_class_type
3410 && ((TREE_CODE (f) == FUNCTION_DECL
3411 && DECL_FUNCTION_MEMBER_P (f))
3412 || (DECL_FUNCTION_TEMPLATE_P (f)
3413 && DECL_FUNCTION_MEMBER_P (f))))
3415 f = lookup_member (current_class_type, DECL_NAME (f),
3416 /*protect=*/1, /*want_type=*/false);
3417 if (f)
3418 fn = f;
3421 if (template_id)
3423 if (BASELINK_P (fn))
3424 BASELINK_FUNCTIONS (fn) = build_nt (TEMPLATE_ID_EXPR,
3425 BASELINK_FUNCTIONS (fn),
3426 template_args);
3427 else
3428 fn = template_id;
3431 return finish_call_expr (fn, args, disallow_virtual);
3434 /* Returns true if ROOT (a namespace, class, or function) encloses
3435 CHILD. CHILD may be either a class type or a namespace. */
3437 bool
3438 is_ancestor (tree root, tree child)
3440 my_friendly_assert ((TREE_CODE (root) == NAMESPACE_DECL
3441 || TREE_CODE (root) == FUNCTION_DECL
3442 || CLASS_TYPE_P (root)), 20030307);
3443 my_friendly_assert ((TREE_CODE (child) == NAMESPACE_DECL
3444 || CLASS_TYPE_P (child)),
3445 20030307);
3447 /* The global namespace encloses everything. */
3448 if (root == global_namespace)
3449 return true;
3451 while (true)
3453 /* If we've run out of scopes, stop. */
3454 if (!child)
3455 return false;
3456 /* If we've reached the ROOT, it encloses CHILD. */
3457 if (root == child)
3458 return true;
3459 /* Go out one level. */
3460 if (TYPE_P (child))
3461 child = TYPE_NAME (child);
3462 child = DECL_CONTEXT (child);
3467 /* Return the namespace that is the common ancestor
3468 of two given namespaces. */
3470 tree
3471 namespace_ancestor (tree ns1, tree ns2)
3473 timevar_push (TV_NAME_LOOKUP);
3474 if (is_ancestor (ns1, ns2))
3475 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ns1);
3476 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
3477 namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2));
3480 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
3481 directive is not directly from the source. Also find the common
3482 ancestor and let our users know about the new namespace */
3483 static void
3484 add_using_namespace (tree user, tree used, bool indirect)
3486 tree t;
3487 timevar_push (TV_NAME_LOOKUP);
3488 /* Using oneself is a no-op. */
3489 if (user == used)
3491 timevar_pop (TV_NAME_LOOKUP);
3492 return;
3494 my_friendly_assert (TREE_CODE (user) == NAMESPACE_DECL, 380);
3495 my_friendly_assert (TREE_CODE (used) == NAMESPACE_DECL, 380);
3496 /* Check if we already have this. */
3497 t = purpose_member (used, DECL_NAMESPACE_USING (user));
3498 if (t != NULL_TREE)
3500 if (!indirect)
3501 /* Promote to direct usage. */
3502 TREE_INDIRECT_USING (t) = 0;
3503 timevar_pop (TV_NAME_LOOKUP);
3504 return;
3507 /* Add used to the user's using list. */
3508 DECL_NAMESPACE_USING (user)
3509 = tree_cons (used, namespace_ancestor (user, used),
3510 DECL_NAMESPACE_USING (user));
3512 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
3514 /* Add user to the used's users list. */
3515 DECL_NAMESPACE_USERS (used)
3516 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
3518 /* Recursively add all namespaces used. */
3519 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
3520 /* indirect usage */
3521 add_using_namespace (user, TREE_PURPOSE (t), 1);
3523 /* Tell everyone using us about the new used namespaces. */
3524 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
3525 add_using_namespace (TREE_PURPOSE (t), used, 1);
3526 timevar_pop (TV_NAME_LOOKUP);
3529 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
3530 duplicates. The first list becomes the tail of the result.
3532 The algorithm is O(n^2). We could get this down to O(n log n) by
3533 doing a sort on the addresses of the functions, if that becomes
3534 necessary. */
3536 static tree
3537 merge_functions (tree s1, tree s2)
3539 for (; s2; s2 = OVL_NEXT (s2))
3541 tree fn2 = OVL_CURRENT (s2);
3542 tree fns1;
3544 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
3546 tree fn1 = OVL_CURRENT (fns1);
3548 /* If the function from S2 is already in S1, there is no
3549 need to add it again. For `extern "C"' functions, we
3550 might have two FUNCTION_DECLs for the same function, in
3551 different namespaces; again, we only need one of them. */
3552 if (fn1 == fn2
3553 || (DECL_EXTERN_C_P (fn1) && DECL_EXTERN_C_P (fn2)
3554 && DECL_NAME (fn1) == DECL_NAME (fn2)))
3555 break;
3558 /* If we exhausted all of the functions in S1, FN2 is new. */
3559 if (!fns1)
3560 s1 = build_overload (fn2, s1);
3562 return s1;
3565 /* This should return an error not all definitions define functions.
3566 It is not an error if we find two functions with exactly the
3567 same signature, only if these are selected in overload resolution.
3568 old is the current set of bindings, new the freshly-found binding.
3569 XXX Do we want to give *all* candidates in case of ambiguity?
3570 XXX In what way should I treat extern declarations?
3571 XXX I don't want to repeat the entire duplicate_decls here */
3573 static cxx_binding *
3574 ambiguous_decl (tree name, cxx_binding *old, cxx_binding *new, int flags)
3576 tree val, type;
3577 my_friendly_assert (old != NULL, 393);
3578 /* Copy the value. */
3579 val = BINDING_VALUE (new);
3580 if (val)
3581 switch (TREE_CODE (val))
3583 case TEMPLATE_DECL:
3584 /* If we expect types or namespaces, and not templates,
3585 or this is not a template class. */
3586 if (LOOKUP_QUALIFIERS_ONLY (flags)
3587 && !DECL_CLASS_TEMPLATE_P (val))
3588 val = NULL_TREE;
3589 break;
3590 case TYPE_DECL:
3591 if (LOOKUP_NAMESPACES_ONLY (flags))
3592 val = NULL_TREE;
3593 break;
3594 case NAMESPACE_DECL:
3595 if (LOOKUP_TYPES_ONLY (flags))
3596 val = NULL_TREE;
3597 break;
3598 case FUNCTION_DECL:
3599 /* Ignore built-in functions that are still anticipated. */
3600 if (LOOKUP_QUALIFIERS_ONLY (flags) || DECL_ANTICIPATED (val))
3601 val = NULL_TREE;
3602 break;
3603 default:
3604 if (LOOKUP_QUALIFIERS_ONLY (flags))
3605 val = NULL_TREE;
3608 if (!BINDING_VALUE (old))
3609 BINDING_VALUE (old) = val;
3610 else if (val && val != BINDING_VALUE (old))
3612 if (is_overloaded_fn (BINDING_VALUE (old)) && is_overloaded_fn (val))
3613 BINDING_VALUE (old) = merge_functions (BINDING_VALUE (old), val);
3614 else
3616 /* Some declarations are functions, some are not. */
3617 if (flags & LOOKUP_COMPLAIN)
3619 /* If we've already given this error for this lookup,
3620 BINDING_VALUE (old) is error_mark_node, so let's not
3621 repeat ourselves. */
3622 if (BINDING_VALUE (old) != error_mark_node)
3624 error ("use of `%D' is ambiguous", name);
3625 cp_error_at (" first declared as `%#D' here",
3626 BINDING_VALUE (old));
3628 cp_error_at (" also declared as `%#D' here", val);
3630 BINDING_VALUE (old) = error_mark_node;
3633 /* ... and copy the type. */
3634 type = BINDING_TYPE (new);
3635 if (LOOKUP_NAMESPACES_ONLY (flags))
3636 type = NULL_TREE;
3637 if (!BINDING_TYPE (old))
3638 BINDING_TYPE (old) = type;
3639 else if (type && BINDING_TYPE (old) != type)
3641 if (flags & LOOKUP_COMPLAIN)
3643 error ("`%D' denotes an ambiguous type",name);
3644 cp_error_at (" first type here", BINDING_TYPE (old));
3645 cp_error_at (" other type here", type);
3648 return old;
3651 /* Subroutine of unualified_namespace_lookup:
3652 Add the bindings of NAME in used namespaces to VAL.
3653 We are currently looking for names in namespace SCOPE, so we
3654 look through USINGS for using-directives of namespaces
3655 which have SCOPE as a common ancestor with the current scope.
3656 Returns false on errors. */
3658 bool
3659 lookup_using_namespace (tree name, cxx_binding *val, tree usings, tree scope,
3660 int flags, tree *spacesp)
3662 tree iter;
3663 timevar_push (TV_NAME_LOOKUP);
3664 /* Iterate over all used namespaces in current, searching for using
3665 directives of scope. */
3666 for (iter = usings; iter; iter = TREE_CHAIN (iter))
3667 if (TREE_VALUE (iter) == scope)
3669 tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
3670 cxx_binding *val1 =
3671 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (used), name);
3672 if (spacesp)
3673 *spacesp = tree_cons (used, NULL_TREE, *spacesp);
3674 /* Resolve ambiguities. */
3675 if (val1)
3676 val = ambiguous_decl (name, val, val1, flags);
3678 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
3679 BINDING_VALUE (val) != error_mark_node);
3682 /* [namespace.qual]
3683 Accepts the NAME to lookup and its qualifying SCOPE.
3684 Returns the name/type pair found into the cxx_binding *RESULT,
3685 or false on error. */
3687 bool
3688 qualified_lookup_using_namespace (tree name, tree scope, cxx_binding *result,
3689 int flags)
3691 /* Maintain a list of namespaces visited... */
3692 tree seen = NULL_TREE;
3693 /* ... and a list of namespace yet to see. */
3694 tree todo = NULL_TREE;
3695 tree usings;
3696 timevar_push (TV_NAME_LOOKUP);
3697 /* Look through namespace aliases. */
3698 scope = ORIGINAL_NAMESPACE (scope);
3699 while (scope && result->value != error_mark_node)
3701 cxx_binding *binding =
3702 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3703 seen = tree_cons (scope, NULL_TREE, seen);
3704 if (binding)
3705 result = ambiguous_decl (name, result, binding, flags);
3706 if (!BINDING_VALUE (result) && !BINDING_TYPE (result))
3707 /* Consider using directives. */
3708 for (usings = DECL_NAMESPACE_USING (scope); usings;
3709 usings = TREE_CHAIN (usings))
3710 /* If this was a real directive, and we have not seen it. */
3711 if (!TREE_INDIRECT_USING (usings)
3712 && !purpose_member (TREE_PURPOSE (usings), seen))
3713 todo = tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo);
3714 if (todo)
3716 scope = TREE_PURPOSE (todo);
3717 todo = TREE_CHAIN (todo);
3719 else
3720 scope = NULL_TREE; /* If there never was a todo list. */
3722 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result->value != error_mark_node);
3725 /* [namespace.memdef]/2 */
3727 /* Set the context of a declaration to scope. Complain if we are not
3728 outside scope. */
3730 void
3731 set_decl_namespace (tree decl, tree scope, bool friendp)
3733 tree old;
3735 /* Get rid of namespace aliases. */
3736 scope = ORIGINAL_NAMESPACE (scope);
3738 /* It is ok for friends to be qualified in parallel space. */
3739 if (!friendp && !is_ancestor (current_namespace, scope))
3740 error ("declaration of `%D' not in a namespace surrounding `%D'",
3741 decl, scope);
3742 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3743 if (scope != current_namespace)
3745 /* See whether this has been declared in the namespace. */
3746 old = namespace_binding (DECL_NAME (decl), scope);
3747 if (!old)
3748 /* No old declaration at all. */
3749 goto complain;
3750 /* A template can be explicitly specialized in any namespace. */
3751 if (processing_explicit_instantiation)
3752 return;
3753 if (!is_overloaded_fn (decl))
3754 /* Don't compare non-function decls with decls_match here,
3755 since it can't check for the correct constness at this
3756 point. pushdecl will find those errors later. */
3757 return;
3758 /* Since decl is a function, old should contain a function decl. */
3759 if (!is_overloaded_fn (old))
3760 goto complain;
3761 if (processing_template_decl || processing_specialization)
3762 /* We have not yet called push_template_decl to turn a
3763 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations
3764 won't match. But, we'll check later, when we construct the
3765 template. */
3766 return;
3767 if (is_overloaded_fn (old))
3769 for (; old; old = OVL_NEXT (old))
3770 if (decls_match (decl, OVL_CURRENT (old)))
3771 return;
3773 else
3774 if (decls_match (decl, old))
3775 return;
3777 else
3778 return;
3779 complain:
3780 error ("`%D' should have been declared inside `%D'",
3781 decl, scope);
3784 /* Compute the namespace where a declaration is defined. */
3786 static tree
3787 decl_namespace (tree decl)
3789 timevar_push (TV_NAME_LOOKUP);
3790 if (TYPE_P (decl))
3791 decl = TYPE_STUB_DECL (decl);
3792 while (DECL_CONTEXT (decl))
3794 decl = DECL_CONTEXT (decl);
3795 if (TREE_CODE (decl) == NAMESPACE_DECL)
3796 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
3797 if (TYPE_P (decl))
3798 decl = TYPE_STUB_DECL (decl);
3799 my_friendly_assert (DECL_P (decl), 390);
3802 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, global_namespace);
3805 /* Return the namespace where the current declaration is declared. */
3807 tree
3808 current_decl_namespace (void)
3810 tree result;
3811 /* If we have been pushed into a different namespace, use it. */
3812 if (decl_namespace_list)
3813 return TREE_PURPOSE (decl_namespace_list);
3815 if (current_class_type)
3816 result = decl_namespace (TYPE_STUB_DECL (current_class_type));
3817 else if (current_function_decl)
3818 result = decl_namespace (current_function_decl);
3819 else
3820 result = current_namespace;
3821 return result;
3824 /* Temporarily set the namespace for the current declaration. */
3826 void
3827 push_decl_namespace (tree decl)
3829 if (TREE_CODE (decl) != NAMESPACE_DECL)
3830 decl = decl_namespace (decl);
3831 decl_namespace_list = tree_cons (ORIGINAL_NAMESPACE (decl),
3832 NULL_TREE, decl_namespace_list);
3835 void
3836 pop_decl_namespace (void)
3838 decl_namespace_list = TREE_CHAIN (decl_namespace_list);
3841 /* Enter a class or namespace scope. */
3843 void
3844 push_scope (tree t)
3846 if (TREE_CODE (t) == NAMESPACE_DECL)
3847 push_decl_namespace (t);
3848 else if CLASS_TYPE_P (t)
3849 push_nested_class (t);
3852 /* Leave scope pushed by push_scope. */
3854 void
3855 pop_scope (tree t)
3857 if (TREE_CODE (t) == NAMESPACE_DECL)
3858 pop_decl_namespace ();
3859 else if CLASS_TYPE_P (t)
3860 pop_nested_class ();
3863 /* [basic.lookup.koenig] */
3864 /* A nonzero return value in the functions below indicates an error. */
3866 struct arg_lookup
3868 tree name;
3869 tree namespaces;
3870 tree classes;
3871 tree functions;
3874 static bool arg_assoc (struct arg_lookup*, tree);
3875 static bool arg_assoc_args (struct arg_lookup*, tree);
3876 static bool arg_assoc_type (struct arg_lookup*, tree);
3877 static bool add_function (struct arg_lookup *, tree);
3878 static bool arg_assoc_namespace (struct arg_lookup *, tree);
3879 static bool arg_assoc_class (struct arg_lookup *, tree);
3880 static bool arg_assoc_template_arg (struct arg_lookup*, tree);
3882 /* Add a function to the lookup structure.
3883 Returns true on error. */
3885 static bool
3886 add_function (struct arg_lookup *k, tree fn)
3888 /* We used to check here to see if the function was already in the list,
3889 but that's O(n^2), which is just too expensive for function lookup.
3890 Now we deal with the occasional duplicate in joust. In doing this, we
3891 assume that the number of duplicates will be small compared to the
3892 total number of functions being compared, which should usually be the
3893 case. */
3895 /* We must find only functions, or exactly one non-function. */
3896 if (!k->functions)
3897 k->functions = fn;
3898 else if (is_overloaded_fn (k->functions) && is_overloaded_fn (fn))
3899 k->functions = build_overload (fn, k->functions);
3900 else
3902 tree f1 = OVL_CURRENT (k->functions);
3903 tree f2 = fn;
3904 if (is_overloaded_fn (f1))
3906 fn = f1; f1 = f2; f2 = fn;
3908 cp_error_at ("`%D' is not a function,", f1);
3909 cp_error_at (" conflict with `%D'", f2);
3910 error (" in call to `%D'", k->name);
3911 return true;
3914 return false;
3917 /* Add functions of a namespace to the lookup structure.
3918 Returns true on error. */
3920 static bool
3921 arg_assoc_namespace (struct arg_lookup *k, tree scope)
3923 tree value;
3925 if (purpose_member (scope, k->namespaces))
3926 return 0;
3927 k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces);
3929 value = namespace_binding (k->name, scope);
3930 if (!value)
3931 return false;
3933 for (; value; value = OVL_NEXT (value))
3934 if (add_function (k, OVL_CURRENT (value)))
3935 return true;
3937 return false;
3940 /* Adds everything associated with a template argument to the lookup
3941 structure. Returns true on error. */
3943 static bool
3944 arg_assoc_template_arg (struct arg_lookup *k, tree arg)
3946 /* [basic.lookup.koenig]
3948 If T is a template-id, its associated namespaces and classes are
3949 ... the namespaces and classes associated with the types of the
3950 template arguments provided for template type parameters
3951 (excluding template template parameters); the namespaces in which
3952 any template template arguments are defined; and the classes in
3953 which any member templates used as template template arguments
3954 are defined. [Note: non-type template arguments do not
3955 contribute to the set of associated namespaces. ] */
3957 /* Consider first template template arguments. */
3958 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3959 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
3960 return false;
3961 else if (TREE_CODE (arg) == TEMPLATE_DECL)
3963 tree ctx = CP_DECL_CONTEXT (arg);
3965 /* It's not a member template. */
3966 if (TREE_CODE (ctx) == NAMESPACE_DECL)
3967 return arg_assoc_namespace (k, ctx);
3968 /* Otherwise, it must be member template. */
3969 else
3970 return arg_assoc_class (k, ctx);
3972 /* It's not a template template argument, but it is a type template
3973 argument. */
3974 else if (TYPE_P (arg))
3975 return arg_assoc_type (k, arg);
3976 /* It's a non-type template argument. */
3977 else
3978 return false;
3981 /* Adds everything associated with class to the lookup structure.
3982 Returns true on error. */
3984 static bool
3985 arg_assoc_class (struct arg_lookup *k, tree type)
3987 tree list, friends, context;
3988 int i;
3990 /* Backend build structures, such as __builtin_va_list, aren't
3991 affected by all this. */
3992 if (!CLASS_TYPE_P (type))
3993 return false;
3995 if (purpose_member (type, k->classes))
3996 return false;
3997 k->classes = tree_cons (type, NULL_TREE, k->classes);
3999 context = decl_namespace (TYPE_MAIN_DECL (type));
4000 if (arg_assoc_namespace (k, context))
4001 return true;
4003 /* Process baseclasses. */
4004 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); i++)
4005 if (arg_assoc_class (k, TYPE_BINFO_BASETYPE (type, i)))
4006 return true;
4008 /* Process friends. */
4009 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
4010 list = TREE_CHAIN (list))
4011 if (k->name == FRIEND_NAME (list))
4012 for (friends = FRIEND_DECLS (list); friends;
4013 friends = TREE_CHAIN (friends))
4014 /* Only interested in global functions with potentially hidden
4015 (i.e. unqualified) declarations. */
4016 if (CP_DECL_CONTEXT (TREE_VALUE (friends)) == context)
4017 if (add_function (k, TREE_VALUE (friends)))
4018 return true;
4020 /* Process template arguments. */
4021 if (CLASSTYPE_TEMPLATE_INFO (type))
4023 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
4024 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
4025 arg_assoc_template_arg (k, TREE_VEC_ELT (list, i));
4028 return false;
4031 /* Adds everything associated with a given type.
4032 Returns 1 on error. */
4034 static bool
4035 arg_assoc_type (struct arg_lookup *k, tree type)
4037 switch (TREE_CODE (type))
4039 case ERROR_MARK:
4040 return false;
4041 case VOID_TYPE:
4042 case INTEGER_TYPE:
4043 case REAL_TYPE:
4044 case COMPLEX_TYPE:
4045 case VECTOR_TYPE:
4046 case CHAR_TYPE:
4047 case BOOLEAN_TYPE:
4048 return false;
4049 case RECORD_TYPE:
4050 if (TYPE_PTRMEMFUNC_P (type))
4051 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
4052 return arg_assoc_class (k, type);
4053 case POINTER_TYPE:
4054 case REFERENCE_TYPE:
4055 case ARRAY_TYPE:
4056 return arg_assoc_type (k, TREE_TYPE (type));
4057 case UNION_TYPE:
4058 case ENUMERAL_TYPE:
4059 return arg_assoc_namespace (k, decl_namespace (TYPE_MAIN_DECL (type)));
4060 case OFFSET_TYPE:
4061 /* Pointer to member: associate class type and value type. */
4062 if (arg_assoc_type (k, TYPE_OFFSET_BASETYPE (type)))
4063 return true;
4064 return arg_assoc_type (k, TREE_TYPE (type));
4065 case METHOD_TYPE:
4066 /* The basetype is referenced in the first arg type, so just
4067 fall through. */
4068 case FUNCTION_TYPE:
4069 /* Associate the parameter types. */
4070 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
4071 return true;
4072 /* Associate the return type. */
4073 return arg_assoc_type (k, TREE_TYPE (type));
4074 case TEMPLATE_TYPE_PARM:
4075 case BOUND_TEMPLATE_TEMPLATE_PARM:
4076 return false;
4077 case TYPENAME_TYPE:
4078 return false;
4079 case LANG_TYPE:
4080 if (type == unknown_type_node)
4081 return false;
4082 /* else fall through */
4083 default:
4084 abort ();
4086 return false;
4089 /* Adds everything associated with arguments. Returns true on error. */
4091 static bool
4092 arg_assoc_args (struct arg_lookup *k, tree args)
4094 for (; args; args = TREE_CHAIN (args))
4095 if (arg_assoc (k, TREE_VALUE (args)))
4096 return true;
4097 return false;
4100 /* Adds everything associated with a given tree_node. Returns 1 on error. */
4102 static bool
4103 arg_assoc (struct arg_lookup *k, tree n)
4105 if (n == error_mark_node)
4106 return false;
4108 if (TYPE_P (n))
4109 return arg_assoc_type (k, n);
4111 if (! type_unknown_p (n))
4112 return arg_assoc_type (k, TREE_TYPE (n));
4114 if (TREE_CODE (n) == ADDR_EXPR)
4115 n = TREE_OPERAND (n, 0);
4116 if (TREE_CODE (n) == COMPONENT_REF)
4117 n = TREE_OPERAND (n, 1);
4118 if (TREE_CODE (n) == OFFSET_REF)
4119 n = TREE_OPERAND (n, 1);
4120 while (TREE_CODE (n) == TREE_LIST)
4121 n = TREE_VALUE (n);
4122 if (TREE_CODE (n) == BASELINK)
4123 n = BASELINK_FUNCTIONS (n);
4125 if (TREE_CODE (n) == FUNCTION_DECL)
4126 return arg_assoc_type (k, TREE_TYPE (n));
4127 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
4129 /* [basic.lookup.koenig]
4131 If T is a template-id, its associated namespaces and classes
4132 are the namespace in which the template is defined; for
4133 member templates, the member template's class... */
4134 tree template = TREE_OPERAND (n, 0);
4135 tree args = TREE_OPERAND (n, 1);
4136 tree ctx;
4137 tree arg;
4139 if (TREE_CODE (template) == COMPONENT_REF)
4140 template = TREE_OPERAND (template, 1);
4142 /* First, the template. There may actually be more than one if
4143 this is an overloaded function template. But, in that case,
4144 we only need the first; all the functions will be in the same
4145 namespace. */
4146 template = OVL_CURRENT (template);
4148 ctx = CP_DECL_CONTEXT (template);
4150 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4152 if (arg_assoc_namespace (k, ctx) == 1)
4153 return true;
4155 /* It must be a member template. */
4156 else if (arg_assoc_class (k, ctx) == 1)
4157 return true;
4159 /* Now the arguments. */
4160 for (arg = args; arg != NULL_TREE; arg = TREE_CHAIN (arg))
4161 if (arg_assoc_template_arg (k, TREE_VALUE (arg)) == 1)
4162 return true;
4164 else
4166 my_friendly_assert (TREE_CODE (n) == OVERLOAD, 980715);
4168 for (; n; n = OVL_CHAIN (n))
4169 if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
4170 return true;
4173 return false;
4176 /* Performs Koenig lookup depending on arguments, where fns
4177 are the functions found in normal lookup. */
4179 tree
4180 lookup_arg_dependent (tree name, tree fns, tree args)
4182 struct arg_lookup k;
4183 tree fn = NULL_TREE;
4185 timevar_push (TV_NAME_LOOKUP);
4186 k.name = name;
4187 k.functions = fns;
4188 k.classes = NULL_TREE;
4190 /* Note that we've already looked at some namespaces during normal
4191 unqualified lookup, unless we found a decl in function scope. */
4192 if (fns)
4193 fn = OVL_CURRENT (fns);
4194 if (fn && TREE_CODE (fn) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (fn))
4195 k.namespaces = NULL_TREE;
4196 else
4197 unqualified_namespace_lookup (name, 0, &k.namespaces);
4199 arg_assoc_args (&k, args);
4200 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, k.functions);
4203 /* Process a namespace-alias declaration. */
4205 void
4206 do_namespace_alias (tree alias, tree namespace)
4208 if (TREE_CODE (namespace) != NAMESPACE_DECL)
4210 /* The parser did not find it, so it's not there. */
4211 error ("unknown namespace `%D'", namespace);
4212 return;
4215 namespace = ORIGINAL_NAMESPACE (namespace);
4217 /* Build the alias. */
4218 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
4219 DECL_NAMESPACE_ALIAS (alias) = namespace;
4220 DECL_EXTERNAL (alias) = 1;
4221 pushdecl (alias);
4224 /* Check a non-member using-declaration. Return the name and scope
4225 being used, and the USING_DECL, or NULL_TREE on failure. */
4227 static tree
4228 validate_nonmember_using_decl (tree decl, tree *scope, tree *name)
4230 *scope = global_namespace;
4231 *name = NULL_TREE;
4233 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
4235 *name = TREE_OPERAND (decl, 0);
4236 /* 7.3.3/5
4237 A using-declaration shall not name a template-id. */
4238 error ("a using-declaration cannot specify a template-id. Try `using %D'", *name);
4239 return NULL_TREE;
4242 if (TREE_CODE (decl) == NAMESPACE_DECL)
4244 error ("namespace `%D' not allowed in using-declaration", decl);
4245 return NULL_TREE;
4248 if (TREE_CODE (decl) == SCOPE_REF)
4250 /* It's a nested name with template parameter dependent scope.
4251 This can only be using-declaration for class member. */
4252 error ("`%T' is not a namespace", TREE_OPERAND (decl, 0));
4253 return NULL_TREE;
4256 if (is_overloaded_fn (decl))
4257 decl = get_first_fn (decl);
4259 my_friendly_assert (DECL_P (decl), 20020908);
4261 if (TREE_CODE (decl) == CONST_DECL)
4262 /* Enumeration constants to not have DECL_CONTEXT set. */
4263 *scope = TYPE_CONTEXT (TREE_TYPE (decl));
4264 else
4265 *scope = DECL_CONTEXT (decl);
4266 if (!*scope)
4267 *scope = global_namespace;
4269 /* [namespace.udecl]
4270 A using-declaration for a class member shall be a
4271 member-declaration. */
4272 if (TYPE_P (*scope))
4274 error ("`%T' is not a namespace", *scope);
4275 return NULL_TREE;
4277 *name = DECL_NAME (decl);
4278 /* Make a USING_DECL. */
4279 return push_using_decl (*scope, *name);
4282 /* Process local and global using-declarations. */
4284 static void
4285 do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
4286 tree *newval, tree *newtype)
4288 cxx_binding decls;
4290 *newval = *newtype = NULL_TREE;
4291 cxx_binding_clear (&decls);
4292 if (!qualified_lookup_using_namespace (name, scope, &decls, 0))
4293 /* Lookup error */
4294 return;
4296 if (!decls.value && !decls.type)
4298 error ("`%D' not declared", name);
4299 return;
4302 /* Check for using functions. */
4303 if (decls.value && is_overloaded_fn (decls.value))
4305 tree tmp, tmp1;
4307 if (oldval && !is_overloaded_fn (oldval))
4309 if (!DECL_IMPLICIT_TYPEDEF_P (oldval))
4310 error ("`%D' is already declared in this scope", name);
4311 oldval = NULL_TREE;
4314 *newval = oldval;
4315 for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
4317 tree new_fn = OVL_CURRENT (tmp);
4319 /* [namespace.udecl]
4321 If a function declaration in namespace scope or block
4322 scope has the same name and the same parameter types as a
4323 function introduced by a using declaration the program is
4324 ill-formed. */
4325 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
4327 tree old_fn = OVL_CURRENT (tmp1);
4329 if (new_fn == old_fn)
4330 /* The function already exists in the current namespace. */
4331 break;
4332 else if (OVL_USED (tmp1))
4333 continue; /* this is a using decl */
4334 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
4335 TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
4337 /* There was already a non-using declaration in
4338 this scope with the same parameter types. If both
4339 are the same extern "C" functions, that's ok. */
4340 if (decls_match (new_fn, old_fn))
4342 /* If the OLD_FN was a builtin, there is now a
4343 real declaration. */
4344 if (DECL_ANTICIPATED (old_fn))
4345 DECL_ANTICIPATED (old_fn) = 0;
4346 break;
4348 else if (!DECL_ANTICIPATED (old_fn))
4350 /* If the OLD_FN was really declared, the
4351 declarations don't match. */
4352 error ("`%D' is already declared in this scope", name);
4353 break;
4356 /* If the OLD_FN was not really there, just ignore
4357 it and keep going. */
4361 /* If we broke out of the loop, there's no reason to add
4362 this function to the using declarations for this
4363 scope. */
4364 if (tmp1)
4365 continue;
4367 *newval = build_overload (OVL_CURRENT (tmp), *newval);
4368 if (TREE_CODE (*newval) != OVERLOAD)
4369 *newval = ovl_cons (*newval, NULL_TREE);
4370 OVL_USED (*newval) = 1;
4373 else
4375 *newval = decls.value;
4376 if (oldval && !decls_match (*newval, oldval))
4377 error ("`%D' is already declared in this scope", name);
4380 *newtype = decls.type;
4381 if (oldtype && *newtype && !same_type_p (oldtype, *newtype))
4383 error ("using declaration `%D' introduced ambiguous type `%T'",
4384 name, oldtype);
4385 return;
4389 /* Process a using-declaration not appearing in class or local scope. */
4391 void
4392 do_toplevel_using_decl (tree decl)
4394 tree scope, name;
4395 tree oldval, oldtype, newval, newtype;
4396 cxx_binding *binding;
4398 decl = validate_nonmember_using_decl (decl, &scope, &name);
4399 if (decl == NULL_TREE)
4400 return;
4402 binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name);
4404 oldval = BINDING_VALUE (binding);
4405 oldtype = BINDING_TYPE (binding);
4407 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
4409 /* Copy declarations found. */
4410 if (newval)
4411 BINDING_VALUE (binding) = newval;
4412 if (newtype)
4413 BINDING_TYPE (binding) = newtype;
4414 return;
4417 /* Process a using-declaration at function scope. */
4419 void
4420 do_local_using_decl (tree decl)
4422 tree scope, name;
4423 tree oldval, oldtype, newval, newtype;
4425 decl = validate_nonmember_using_decl (decl, &scope, &name);
4426 if (decl == NULL_TREE)
4427 return;
4429 if (building_stmt_tree ()
4430 && at_function_scope_p ())
4431 add_decl_stmt (decl);
4433 oldval = lookup_name_current_level (name);
4434 oldtype = lookup_type_current_level (name);
4436 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
4438 if (newval)
4440 if (is_overloaded_fn (newval))
4442 tree fn, term;
4444 /* We only need to push declarations for those functions
4445 that were not already bound in the current level.
4446 The old value might be NULL_TREE, it might be a single
4447 function, or an OVERLOAD. */
4448 if (oldval && TREE_CODE (oldval) == OVERLOAD)
4449 term = OVL_FUNCTION (oldval);
4450 else
4451 term = oldval;
4452 for (fn = newval; fn && OVL_CURRENT (fn) != term;
4453 fn = OVL_NEXT (fn))
4454 push_overloaded_decl (OVL_CURRENT (fn),
4455 PUSH_LOCAL | PUSH_USING);
4457 else
4458 push_local_binding (name, newval, PUSH_USING);
4460 if (newtype)
4461 set_identifier_type_value (name, newtype);
4464 tree
4465 do_class_using_decl (tree decl)
4467 tree name, value;
4469 if (TREE_CODE (decl) != SCOPE_REF
4470 || !TREE_OPERAND (decl, 0)
4471 || !TYPE_P (TREE_OPERAND (decl, 0)))
4473 error ("using-declaration for non-member at class scope");
4474 return NULL_TREE;
4476 name = TREE_OPERAND (decl, 1);
4477 if (TREE_CODE (name) == BIT_NOT_EXPR)
4479 error ("using-declaration for destructor");
4480 return NULL_TREE;
4482 else if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4484 name = TREE_OPERAND (name, 0);
4485 error ("a using-declaration cannot specify a template-id. Try `using %T::%D'", TREE_OPERAND (decl, 0), name);
4486 return NULL_TREE;
4488 if (TREE_CODE (name) == TYPE_DECL)
4490 tree type = TREE_TYPE (name);
4491 if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (name)))
4493 name = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type));
4494 error ("a using-declaration cannot specify a template-id.");
4495 return NULL_TREE;
4497 name = DECL_NAME (name);
4499 else if (TREE_CODE (name) == TEMPLATE_DECL)
4500 name = DECL_NAME (name);
4501 else if (BASELINK_P (name))
4503 tree fns;
4505 fns = BASELINK_FUNCTIONS (name);
4506 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
4508 fns = TREE_OPERAND (fns, 0);
4509 error ("a using-declaration cannot specify a template-id. Try `using %T::%D'",
4510 BASELINK_ACCESS_BINFO (name),
4511 DECL_NAME (get_first_fn (fns)));
4513 name = DECL_NAME (get_first_fn (fns));
4516 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 980716);
4518 value = build_lang_decl (USING_DECL, name, void_type_node);
4519 DECL_INITIAL (value) = TREE_OPERAND (decl, 0);
4520 return value;
4523 /* Process a using-directive. */
4525 void
4526 do_using_directive (tree namespace)
4528 if (building_stmt_tree ())
4529 add_stmt (build_stmt (USING_STMT, namespace));
4531 /* using namespace A::B::C; */
4532 if (TREE_CODE (namespace) == SCOPE_REF)
4533 namespace = TREE_OPERAND (namespace, 1);
4534 if (TREE_CODE (namespace) == IDENTIFIER_NODE)
4536 /* Lookup in lexer did not find a namespace. */
4537 if (!processing_template_decl)
4538 error ("namespace `%T' undeclared", namespace);
4539 return;
4541 if (TREE_CODE (namespace) != NAMESPACE_DECL)
4543 if (!processing_template_decl)
4544 error ("`%T' is not a namespace", namespace);
4545 return;
4547 namespace = ORIGINAL_NAMESPACE (namespace);
4548 if (!toplevel_bindings_p ())
4549 push_using_directive (namespace);
4550 else
4551 /* direct usage */
4552 add_using_namespace (current_namespace, namespace, 0);
4555 void
4556 check_default_args (tree x)
4558 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4559 bool saw_def = false;
4560 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4561 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4563 if (TREE_PURPOSE (arg))
4564 saw_def = true;
4565 else if (saw_def)
4567 cp_error_at ("default argument missing for parameter %P of `%+#D'",
4568 i, x);
4569 break;
4574 void
4575 mark_used (tree decl)
4577 TREE_USED (decl) = 1;
4578 if (processing_template_decl || skip_evaluation)
4579 return;
4581 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
4582 && !TREE_ASM_WRITTEN (decl))
4583 /* Remember it, so we can check it was defined. */
4584 defer_fn (decl);
4586 assemble_external (decl);
4588 /* Is it a synthesized method that needs to be synthesized? */
4589 if (TREE_CODE (decl) == FUNCTION_DECL
4590 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
4591 && DECL_ARTIFICIAL (decl)
4592 && !DECL_THUNK_P (decl)
4593 && ! DECL_INITIAL (decl)
4594 /* Kludge: don't synthesize for default args. */
4595 && current_function_decl)
4597 synthesize_method (decl);
4598 /* If we've already synthesized the method we don't need to
4599 instantiate it, so we can return right away. */
4600 return;
4603 /* If this is a function or variable that is an instance of some
4604 template, we now know that we will need to actually do the
4605 instantiation. We check that DECL is not an explicit
4606 instantiation because that is not checked in instantiate_decl. */
4607 if ((DECL_NON_THUNK_FUNCTION_P (decl) || TREE_CODE (decl) == VAR_DECL)
4608 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
4609 && (!DECL_EXPLICIT_INSTANTIATION (decl)
4610 || (TREE_CODE (decl) == FUNCTION_DECL
4611 && DECL_INLINE (DECL_TEMPLATE_RESULT
4612 (template_for_substitution (decl))))))
4614 bool defer;
4616 /* Normally, we put off instantiating functions in order to
4617 improve compile times. Maintaining a stack of active
4618 functions is expensive, and the inliner knows to
4619 instantiate any functions it might need.
4621 However, if instantiating this function might help us mark
4622 the current function TREE_NOTHROW, we go ahead and
4623 instantiate it now. */
4624 defer = (!flag_exceptions
4625 || TREE_CODE (decl) != FUNCTION_DECL
4626 /* If the called function can't throw, we don't need to
4627 generate its body to find that out. */
4628 || TREE_NOTHROW (decl)
4629 || !cfun
4630 /* If we already know the current function can't throw,
4631 then we don't need to work hard to prove it. */
4632 || TREE_NOTHROW (current_function_decl)
4633 /* If we already know that the current function *can*
4634 throw, there's no point in gathering more
4635 information. */
4636 || cp_function_chain->can_throw);
4638 instantiate_decl (decl, defer);
4642 /* Called when a class-head is encountered. TAG_KIND is the class-key
4643 for the class. SCOPE, if non-NULL, is the type or namespace
4644 indicated in the nested-name-specifier for the declaration of the
4645 class. ID is the name of the class, if any; it may be a TYPE_DECL,
4646 or an IDENTIFIER_NODE. ATTRIBUTES are attributes that apply to the
4647 class.
4649 Return a TYPE_DECL for the class being defined. */
4651 tree
4652 handle_class_head (enum tag_types tag_kind, tree scope, tree id,
4653 tree attributes)
4655 tree decl = NULL_TREE;
4656 tree current = current_scope ();
4657 bool xrefd_p = false;
4658 bool new_type_p;
4659 tree context;
4661 if (current == NULL_TREE)
4662 current = current_namespace;
4664 if (scope)
4666 if (TREE_CODE (id) == TYPE_DECL)
4667 /* We must bash typedefs back to the main decl of the
4668 type. Otherwise we become confused about scopes. */
4669 decl = TYPE_MAIN_DECL (TREE_TYPE (id));
4670 else if (DECL_CLASS_TEMPLATE_P (id))
4671 decl = DECL_TEMPLATE_RESULT (id);
4672 else
4674 if (TYPE_P (scope))
4676 /* According to the suggested resolution of core issue
4677 180, 'typename' is assumed after a class-key. */
4678 decl = make_typename_type (scope, id, tf_error);
4679 if (decl != error_mark_node)
4680 decl = TYPE_MAIN_DECL (decl);
4681 else
4682 decl = NULL_TREE;
4684 else if (scope == current)
4686 /* We've been given AGGR SCOPE::ID, when we're already
4687 inside SCOPE. Be nice about it. */
4688 if (pedantic)
4689 pedwarn ("extra qualification `%T::' on member `%D' ignored",
4690 scope, id);
4692 else
4693 error ("`%T' does not have a class or union named `%D'",
4694 scope, id);
4698 if (!decl)
4700 decl = xref_tag (tag_kind, id, attributes, false);
4701 if (decl == error_mark_node)
4702 return error_mark_node;
4703 decl = TYPE_MAIN_DECL (decl);
4704 xrefd_p = true;
4707 if (!TYPE_BINFO (TREE_TYPE (decl)))
4709 error ("`%T' is not a class or union type", decl);
4710 return error_mark_node;
4713 /* For a definition, we want to enter the containing scope before
4714 looking up any base classes etc. Only do so, if this is different
4715 to the current scope. */
4716 context = CP_DECL_CONTEXT (decl);
4718 new_type_p = (current != context
4719 && TREE_CODE (context) != TEMPLATE_TYPE_PARM
4720 && TREE_CODE (context) != BOUND_TEMPLATE_TEMPLATE_PARM);
4721 if (new_type_p)
4722 push_scope (context);
4724 if (!xrefd_p
4725 && PROCESSING_REAL_TEMPLATE_DECL_P ()
4726 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
4727 decl = push_template_decl (decl);
4729 return decl;
4732 #include "gt-cp-decl2.h"