* decl2.c (mark_member_pointers): Rename to...
[official-gcc.git] / gcc / cp / decl2.c
blobeb32f49378d6a7277c343414b5bc1a77fe361985
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 #include "cgraph.h"
50 #include "tree-inline.h"
51 extern cpp_reader *parse_in;
53 /* This structure contains information about the initializations
54 and/or destructions required for a particular priority level. */
55 typedef struct priority_info_s {
56 /* Nonzero if there have been any initializations at this priority
57 throughout the translation unit. */
58 int initializations_p;
59 /* Nonzero if there have been any destructions at this priority
60 throughout the translation unit. */
61 int destructions_p;
62 } *priority_info;
64 static void mark_vtable_entries (tree);
65 static void grok_function_init (tree, tree);
66 static bool maybe_emit_vtables (tree);
67 static void add_using_namespace (tree, tree, bool);
68 static cxx_binding *ambiguous_decl (tree, cxx_binding *, cxx_binding *, int);
69 static tree build_anon_union_vars (tree);
70 static bool acceptable_java_type (tree);
71 static tree start_objects (int, int);
72 static void finish_objects (int, int, tree);
73 static tree merge_functions (tree, tree);
74 static tree decl_namespace (tree);
75 static tree validate_nonmember_using_decl (tree, tree *, tree *);
76 static void do_nonmember_using_decl (tree, tree, tree, tree, tree *, tree *);
77 static tree start_static_storage_duration_function (unsigned);
78 static void finish_static_storage_duration_function (tree);
79 static priority_info get_priority_info (int);
80 static void do_static_initialization (tree, tree);
81 static void do_static_destruction (tree);
82 static tree start_static_initialization_or_destruction (tree, int);
83 static void finish_static_initialization_or_destruction (tree);
84 static void generate_ctor_or_dtor_function (bool, int, location_t *);
85 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
86 void *);
87 static tree prune_vars_needing_no_initialization (tree *);
88 static void write_out_vars (tree);
89 static void import_export_class (tree);
90 static tree get_guard_bits (tree);
92 /* A list of static class variables. This is needed, because a
93 static class variable can be declared inside the class without
94 an initializer, and then initialized, statically, outside the class. */
95 static GTY(()) varray_type pending_statics;
96 #define pending_statics_used \
97 (pending_statics ? pending_statics->elements_used : 0)
99 /* A list of functions which were declared inline, but which we
100 may need to emit outline anyway. */
101 static GTY(()) varray_type deferred_fns;
102 #define deferred_fns_used \
103 (deferred_fns ? deferred_fns->elements_used : 0)
105 /* Flag used when debugging spew.c */
107 extern int spew_debug;
109 /* Nonzero if we're done parsing and into end-of-file activities. */
111 int at_eof;
113 /* Functions called along with real static constructors and destructors. */
115 tree static_ctors;
116 tree static_dtors;
118 /* The :: namespace. */
120 tree global_namespace;
122 /* Incorporate `const' and `volatile' qualifiers for member functions.
123 FUNCTION is a TYPE_DECL or a FUNCTION_DECL.
124 QUALS is a list of qualifiers. Returns any explicit
125 top-level qualifiers of the method's this pointer, anything other than
126 TYPE_UNQUALIFIED will be an extension. */
129 grok_method_quals (tree ctype, tree function, tree quals)
131 tree fntype = TREE_TYPE (function);
132 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
133 int type_quals = TYPE_UNQUALIFIED;
134 int dup_quals = TYPE_UNQUALIFIED;
135 int this_quals = TYPE_UNQUALIFIED;
139 int tq = cp_type_qual_from_rid (TREE_VALUE (quals));
141 if ((type_quals | this_quals) & tq)
142 dup_quals |= tq;
143 else if (tq & TYPE_QUAL_RESTRICT)
144 this_quals |= tq;
145 else
146 type_quals |= tq;
147 quals = TREE_CHAIN (quals);
149 while (quals);
151 if (dup_quals != TYPE_UNQUALIFIED)
152 error ("duplicate type qualifiers in %s declaration",
153 TREE_CODE (function) == FUNCTION_DECL
154 ? "member function" : "type");
156 ctype = cp_build_qualified_type (ctype, type_quals);
157 fntype = build_cplus_method_type (ctype, TREE_TYPE (fntype),
158 (TREE_CODE (fntype) == METHOD_TYPE
159 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
160 : TYPE_ARG_TYPES (fntype)));
161 if (raises)
162 fntype = build_exception_variant (fntype, raises);
164 TREE_TYPE (function) = fntype;
165 return this_quals;
168 /* Warn when -fexternal-templates is used and #pragma
169 interface/implementation is not used all the times it should be,
170 inform the user. */
172 void
173 warn_if_unknown_interface (tree decl)
175 static int already_warned = 0;
176 if (already_warned++)
177 return;
179 if (flag_alt_external_templates)
181 tree til = tinst_for_decl ();
182 location_t saved_loc = input_location;
184 if (til)
186 input_line = TINST_LINE (til);
187 input_filename = TINST_FILE (til);
189 warning ("template `%#D' instantiated in file without #pragma interface",
190 decl);
191 input_location = saved_loc;
193 else
194 cp_warning_at ("template `%#D' defined in file without #pragma interface",
195 decl);
198 /* A subroutine of the parser, to handle a component list. */
200 void
201 grok_x_components (tree specs)
203 tree t;
205 specs = strip_attrs (specs);
207 check_tag_decl (specs);
208 t = groktypename (build_tree_list (specs, NULL_TREE));
210 /* The only case where we need to do anything additional here is an
211 anonymous union field, e.g.: `struct S { union { int i; }; };'. */
212 if (t == NULL_TREE || !ANON_AGGR_TYPE_P (t))
213 return;
215 fixup_anonymous_aggr (t);
216 finish_member_declaration (build_decl (FIELD_DECL, NULL_TREE, t));
219 /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
220 appropriately. */
222 tree
223 cp_build_parm_decl (tree name, tree type)
225 tree parm = build_decl (PARM_DECL, name, type);
226 /* DECL_ARG_TYPE is only used by the back end and the back end never
227 sees templates. */
228 if (!processing_template_decl)
229 DECL_ARG_TYPE (parm) = type_passed_as (type);
230 return parm;
233 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
234 indicated NAME. */
236 tree
237 build_artificial_parm (tree name, tree type)
239 tree parm = cp_build_parm_decl (name, type);
240 DECL_ARTIFICIAL (parm) = 1;
241 /* All our artificial parms are implicitly `const'; they cannot be
242 assigned to. */
243 TREE_READONLY (parm) = 1;
244 return parm;
247 /* Constructors for types with virtual baseclasses need an "in-charge" flag
248 saying whether this constructor is responsible for initialization of
249 virtual baseclasses or not. All destructors also need this "in-charge"
250 flag, which additionally determines whether or not the destructor should
251 free the memory for the object.
253 This function adds the "in-charge" flag to member function FN if
254 appropriate. It is called from grokclassfn and tsubst.
255 FN must be either a constructor or destructor.
257 The in-charge flag follows the 'this' parameter, and is followed by the
258 VTT parm (if any), then the user-written parms. */
260 void
261 maybe_retrofit_in_chrg (tree fn)
263 tree basetype, arg_types, parms, parm, fntype;
265 /* If we've already add the in-charge parameter don't do it again. */
266 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
267 return;
269 /* When processing templates we can't know, in general, whether or
270 not we're going to have virtual baseclasses. */
271 if (uses_template_parms (fn))
272 return;
274 /* We don't need an in-charge parameter for constructors that don't
275 have virtual bases. */
276 if (DECL_CONSTRUCTOR_P (fn)
277 && !TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
278 return;
280 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
281 basetype = TREE_TYPE (TREE_VALUE (arg_types));
282 arg_types = TREE_CHAIN (arg_types);
284 parms = TREE_CHAIN (DECL_ARGUMENTS (fn));
286 /* If this is a subobject constructor or destructor, our caller will
287 pass us a pointer to our VTT. */
288 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
290 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
292 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
293 TREE_CHAIN (parm) = parms;
294 parms = parm;
296 /* ...and then to TYPE_ARG_TYPES. */
297 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
299 DECL_HAS_VTT_PARM_P (fn) = 1;
302 /* Then add the in-charge parm (before the VTT parm). */
303 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
304 TREE_CHAIN (parm) = parms;
305 parms = parm;
306 arg_types = hash_tree_chain (integer_type_node, arg_types);
308 /* Insert our new parameter(s) into the list. */
309 TREE_CHAIN (DECL_ARGUMENTS (fn)) = parms;
311 /* And rebuild the function type. */
312 fntype = build_cplus_method_type (basetype, TREE_TYPE (TREE_TYPE (fn)),
313 arg_types);
314 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
315 fntype = build_exception_variant (fntype,
316 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
317 TREE_TYPE (fn) = fntype;
319 /* Now we've got the in-charge parameter. */
320 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
323 /* Classes overload their constituent function names automatically.
324 When a function name is declared in a record structure,
325 its name is changed to it overloaded name. Since names for
326 constructors and destructors can conflict, we place a leading
327 '$' for destructors.
329 CNAME is the name of the class we are grokking for.
331 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
333 FLAGS contains bits saying what's special about today's
334 arguments. 1 == DESTRUCTOR. 2 == OPERATOR.
336 If FUNCTION is a destructor, then we must add the `auto-delete' field
337 as a second parameter. There is some hair associated with the fact
338 that we must "declare" this variable in the manner consistent with the
339 way the rest of the arguments were declared.
341 QUALS are the qualifiers for the this pointer. */
343 void
344 grokclassfn (tree ctype, tree function, enum overload_flags flags, tree quals)
346 tree fn_name = DECL_NAME (function);
347 int this_quals = TYPE_UNQUALIFIED;
349 /* Even within an `extern "C"' block, members get C++ linkage. See
350 [dcl.link] for details. */
351 SET_DECL_LANGUAGE (function, lang_cplusplus);
353 if (fn_name == NULL_TREE)
355 error ("name missing for member function");
356 fn_name = get_identifier ("<anonymous>");
357 DECL_NAME (function) = fn_name;
360 if (quals)
361 this_quals = grok_method_quals (ctype, function, quals);
363 if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
365 /* Must add the class instance variable up front. */
366 /* Right now we just make this a pointer. But later
367 we may wish to make it special. */
368 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (function)));
369 tree qual_type;
370 tree parm;
372 /* The `this' parameter is implicitly `const'; it cannot be
373 assigned to. */
374 this_quals |= TYPE_QUAL_CONST;
375 qual_type = cp_build_qualified_type (type, this_quals);
376 parm = build_artificial_parm (this_identifier, qual_type);
377 c_apply_type_quals_to_decl (this_quals, parm);
378 TREE_CHAIN (parm) = last_function_parms;
379 last_function_parms = parm;
382 DECL_ARGUMENTS (function) = last_function_parms;
383 DECL_CONTEXT (function) = ctype;
385 if (flags == DTOR_FLAG)
386 DECL_DESTRUCTOR_P (function) = 1;
388 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
389 maybe_retrofit_in_chrg (function);
391 if (flags == DTOR_FLAG)
393 DECL_DESTRUCTOR_P (function) = 1;
394 TYPE_HAS_DESTRUCTOR (ctype) = 1;
398 /* Create an ARRAY_REF, checking for the user doing things backwards
399 along the way. */
401 tree
402 grok_array_decl (tree array_expr, tree index_exp)
404 tree type;
405 tree expr;
406 tree orig_array_expr = array_expr;
407 tree orig_index_exp = index_exp;
409 if (error_operand_p (array_expr) || error_operand_p (index_exp))
410 return error_mark_node;
412 if (processing_template_decl)
414 if (type_dependent_expression_p (array_expr)
415 || type_dependent_expression_p (index_exp))
416 return build_min_nt (ARRAY_REF, array_expr, index_exp);
417 array_expr = build_non_dependent_expr (array_expr);
418 index_exp = build_non_dependent_expr (index_exp);
421 type = TREE_TYPE (array_expr);
422 my_friendly_assert (type, 20030626);
423 type = non_reference (type);
425 /* If they have an `operator[]', use that. */
426 if (IS_AGGR_TYPE (type) || IS_AGGR_TYPE (TREE_TYPE (index_exp)))
427 expr = build_new_op (ARRAY_REF, LOOKUP_NORMAL,
428 array_expr, index_exp, NULL_TREE);
429 else
431 tree p1, p2, i1, i2;
433 /* Otherwise, create an ARRAY_REF for a pointer or array type.
434 It is a little-known fact that, if `a' is an array and `i' is
435 an int, you can write `i[a]', which means the same thing as
436 `a[i]'. */
437 if (TREE_CODE (type) == ARRAY_TYPE)
438 p1 = array_expr;
439 else
440 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
442 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
443 p2 = index_exp;
444 else
445 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
447 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
448 false);
449 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
450 false);
452 if ((p1 && i2) && (i1 && p2))
453 error ("ambiguous conversion for array subscript");
455 if (p1 && i2)
456 array_expr = p1, index_exp = i2;
457 else if (i1 && p2)
458 array_expr = p2, index_exp = i1;
459 else
461 error ("invalid types `%T[%T]' for array subscript",
462 type, TREE_TYPE (index_exp));
463 return error_mark_node;
466 if (array_expr == error_mark_node || index_exp == error_mark_node)
467 error ("ambiguous conversion for array subscript");
469 expr = build_array_ref (array_expr, index_exp);
471 if (processing_template_decl && expr != error_mark_node)
472 return build_min (ARRAY_REF, TREE_TYPE (expr), orig_array_expr,
473 orig_index_exp);
474 return expr;
477 /* Given the cast expression EXP, checking out its validity. Either return
478 an error_mark_node if there was an unavoidable error, return a cast to
479 void for trying to delete a pointer w/ the value 0, or return the
480 call to delete. If DOING_VEC is 1, we handle things differently
481 for doing an array delete. If DOING_VEC is 2, they gave us the
482 array size as an argument to delete.
483 Implements ARM $5.3.4. This is called from the parser. */
485 tree
486 delete_sanity (tree exp, tree size, int doing_vec, int use_global_delete)
488 tree t, type;
489 /* For a regular vector delete (aka, no size argument) we will pass
490 this down as a NULL_TREE into build_vec_delete. */
491 tree maxindex = NULL_TREE;
493 if (exp == error_mark_node)
494 return exp;
496 if (processing_template_decl)
498 t = build_min (DELETE_EXPR, void_type_node, exp, size);
499 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
500 DELETE_EXPR_USE_VEC (t) = doing_vec;
501 return t;
504 exp = convert_from_reference (exp);
505 t = build_expr_type_conversion (WANT_POINTER, exp, true);
507 if (t == NULL_TREE || t == error_mark_node)
509 error ("type `%#T' argument given to `delete', expected pointer",
510 TREE_TYPE (exp));
511 return error_mark_node;
514 if (doing_vec == 2)
516 maxindex = cp_build_binary_op (MINUS_EXPR, size, integer_one_node);
517 pedwarn ("anachronistic use of array size in vector delete");
520 type = TREE_TYPE (t);
522 /* As of Valley Forge, you can delete a pointer to const. */
524 /* You can't delete functions. */
525 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
527 error ("cannot delete a function. Only pointer-to-objects are valid arguments to `delete'");
528 return error_mark_node;
531 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
532 if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
534 warning ("deleting `%T' is undefined", type);
535 doing_vec = 0;
538 /* An array can't have been allocated by new, so complain. */
539 if (TREE_CODE (t) == ADDR_EXPR
540 && TREE_CODE (TREE_OPERAND (t, 0)) == VAR_DECL
541 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == ARRAY_TYPE)
542 warning ("deleting array `%#D'", TREE_OPERAND (t, 0));
544 /* Deleting a pointer with the value zero is valid and has no effect. */
545 if (integer_zerop (t))
546 return build1 (NOP_EXPR, void_type_node, t);
548 if (doing_vec)
549 return build_vec_delete (t, maxindex, sfk_deleting_destructor,
550 use_global_delete);
551 else
552 return build_delete (type, t, sfk_deleting_destructor,
553 LOOKUP_NORMAL, use_global_delete);
556 /* Report an error if the indicated template declaration is not the
557 sort of thing that should be a member template. */
559 void
560 check_member_template (tree tmpl)
562 tree decl;
564 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
565 decl = DECL_TEMPLATE_RESULT (tmpl);
567 if (TREE_CODE (decl) == FUNCTION_DECL
568 || (TREE_CODE (decl) == TYPE_DECL
569 && IS_AGGR_TYPE (TREE_TYPE (decl))))
571 if (current_function_decl)
572 /* 14.5.2.2 [temp.mem]
574 A local class shall not have member templates. */
575 error ("invalid declaration of member template `%#D' in local class",
576 decl);
578 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
580 /* 14.5.2.3 [temp.mem]
582 A member function template shall not be virtual. */
583 error
584 ("invalid use of `virtual' in template declaration of `%#D'",
585 decl);
586 DECL_VIRTUAL_P (decl) = 0;
589 /* The debug-information generating code doesn't know what to do
590 with member templates. */
591 DECL_IGNORED_P (tmpl) = 1;
593 else
594 error ("template declaration of `%#D'", decl);
597 /* Return true iff TYPE is a valid Java parameter or return type. */
599 static bool
600 acceptable_java_type (tree type)
602 if (TREE_CODE (type) == VOID_TYPE || TYPE_FOR_JAVA (type))
603 return 1;
604 if (TREE_CODE (type) == POINTER_TYPE || TREE_CODE (type) == REFERENCE_TYPE)
606 type = TREE_TYPE (type);
607 if (TREE_CODE (type) == RECORD_TYPE)
609 tree args; int i;
610 if (! TYPE_FOR_JAVA (type))
611 return false;
612 if (! CLASSTYPE_TEMPLATE_INFO (type))
613 return true;
614 args = CLASSTYPE_TI_ARGS (type);
615 i = TREE_VEC_LENGTH (args);
616 while (--i >= 0)
618 type = TREE_VEC_ELT (args, i);
619 if (TREE_CODE (type) == POINTER_TYPE)
620 type = TREE_TYPE (type);
621 if (! TYPE_FOR_JAVA (type))
622 return false;
624 return true;
627 return false;
630 /* For a METHOD in a Java class CTYPE, return true if
631 the parameter and return types are valid Java types.
632 Otherwise, print appropriate error messages, and return false. */
634 bool
635 check_java_method (tree method)
637 bool jerr = false;
638 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
639 tree ret_type = TREE_TYPE (TREE_TYPE (method));
640 if (!acceptable_java_type (ret_type))
642 error ("Java method '%D' has non-Java return type `%T'",
643 method, ret_type);
644 jerr = true;
646 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
648 tree type = TREE_VALUE (arg_types);
649 if (!acceptable_java_type (type))
651 error ("Java method '%D' has non-Java parameter type `%T'",
652 method, type);
653 jerr = true;
656 return !jerr;
659 /* Sanity check: report error if this function FUNCTION is not
660 really a member of the class (CTYPE) it is supposed to belong to.
661 CNAME is the same here as it is for grokclassfn above. */
663 tree
664 check_classfn (tree ctype, tree function)
666 int ix;
667 int is_template;
669 if (DECL_USE_TEMPLATE (function)
670 && !(TREE_CODE (function) == TEMPLATE_DECL
671 && DECL_TEMPLATE_SPECIALIZATION (function))
672 && is_member_template (DECL_TI_TEMPLATE (function)))
673 /* Since this is a specialization of a member template,
674 we're not going to find the declaration in the class.
675 For example, in:
677 struct S { template <typename T> void f(T); };
678 template <> void S::f(int);
680 we're not going to find `S::f(int)', but there's no
681 reason we should, either. We let our callers know we didn't
682 find the method, but we don't complain. */
683 return NULL_TREE;
685 /* OK, is this a definition of a member template? */
686 is_template = (TREE_CODE (function) == TEMPLATE_DECL
687 || (processing_template_decl - template_class_depth (ctype)));
689 ix = lookup_fnfields_1 (complete_type (ctype),
690 DECL_CONSTRUCTOR_P (function) ? ctor_identifier :
691 DECL_DESTRUCTOR_P (function) ? dtor_identifier :
692 DECL_NAME (function));
694 if (ix >= 0)
696 tree methods = CLASSTYPE_METHOD_VEC (ctype);
697 tree fndecls, fndecl = 0;
698 bool is_conv_op;
699 const char *format = NULL;
701 push_scope (ctype);
702 for (fndecls = TREE_VEC_ELT (methods, ix);
703 fndecls; fndecls = OVL_NEXT (fndecls))
705 tree p1, p2;
707 fndecl = OVL_CURRENT (fndecls);
708 p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
709 p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
711 /* We cannot simply call decls_match because this doesn't
712 work for static member functions that are pretending to
713 be methods, and because the name may have been changed by
714 asm("new_name"). */
716 /* Get rid of the this parameter on functions that become
717 static. */
718 if (DECL_STATIC_FUNCTION_P (fndecl)
719 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
720 p1 = TREE_CHAIN (p1);
722 /* A member template definition only matches a member template
723 declaration. */
724 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
725 continue;
727 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
728 TREE_TYPE (TREE_TYPE (fndecl)))
729 && compparms (p1, p2)
730 && (DECL_TEMPLATE_SPECIALIZATION (function)
731 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
732 && (!DECL_TEMPLATE_SPECIALIZATION (function)
733 || (DECL_TI_TEMPLATE (function)
734 == DECL_TI_TEMPLATE (fndecl))))
735 break;
737 pop_scope (ctype);
738 if (fndecls)
739 return OVL_CURRENT (fndecls);
740 error ("prototype for `%#D' does not match any in class `%T'",
741 function, ctype);
742 is_conv_op = DECL_CONV_FN_P (fndecl);
744 if (is_conv_op)
745 ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
746 fndecls = TREE_VEC_ELT (methods, ix);
747 while (fndecls)
749 fndecl = OVL_CURRENT (fndecls);
750 fndecls = OVL_NEXT (fndecls);
752 if (!fndecls && is_conv_op)
754 if (TREE_VEC_LENGTH (methods) > ix)
756 ix++;
757 fndecls = TREE_VEC_ELT (methods, ix);
758 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
760 fndecls = NULL_TREE;
761 is_conv_op = false;
764 else
765 is_conv_op = false;
767 if (format)
768 format = " %#D";
769 else if (fndecls)
770 format = "candidates are: %#D";
771 else
772 format = "candidate is: %#D";
773 cp_error_at (format, fndecl);
776 else if (!COMPLETE_TYPE_P (ctype))
777 cxx_incomplete_type_error (function, ctype);
778 else
779 error ("no `%#D' member function declared in class `%T'",
780 function, ctype);
782 /* If we did not find the method in the class, add it to avoid
783 spurious errors (unless the CTYPE is not yet defined, in which
784 case we'll only confuse ourselves when the function is declared
785 properly within the class. */
786 if (COMPLETE_TYPE_P (ctype))
787 add_method (ctype, function, /*error_p=*/1);
788 return NULL_TREE;
791 /* We have just processed the DECL, which is a static data member.
792 Its initializer, if present, is INIT. The ASMSPEC_TREE, if
793 present, is the assembly-language name for the data member.
794 FLAGS is as for cp_finish_decl. */
796 void
797 finish_static_data_member_decl (tree decl, tree init, tree asmspec_tree,
798 int flags)
800 my_friendly_assert (TREE_PUBLIC (decl), 0);
802 DECL_CONTEXT (decl) = current_class_type;
804 /* We cannot call pushdecl here, because that would fill in the
805 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
806 the right thing, namely, to put this decl out straight away. */
807 /* current_class_type can be NULL_TREE in case of error. */
808 if (!asmspec_tree && current_class_type)
809 DECL_INITIAL (decl) = error_mark_node;
811 if (! processing_template_decl)
813 if (!pending_statics)
814 VARRAY_TREE_INIT (pending_statics, 32, "pending_statics");
815 VARRAY_PUSH_TREE (pending_statics, decl);
818 if (LOCAL_CLASS_P (current_class_type))
819 pedwarn ("local class `%#T' shall not have static data member `%#D'",
820 current_class_type, decl);
822 /* Static consts need not be initialized in the class definition. */
823 if (init != NULL_TREE && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
825 static int explained = 0;
827 error ("initializer invalid for static member with constructor");
828 if (!explained)
830 error ("(an out of class initialization is required)");
831 explained = 1;
833 init = NULL_TREE;
835 /* Force the compiler to know when an uninitialized static const
836 member is being used. */
837 if (CP_TYPE_CONST_P (TREE_TYPE (decl)) && init == 0)
838 TREE_USED (decl) = 1;
839 DECL_INITIAL (decl) = init;
840 DECL_IN_AGGR_P (decl) = 1;
842 cp_finish_decl (decl, init, asmspec_tree, flags);
845 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
846 of a structure component, returning a _DECL node.
847 QUALS is a list of type qualifiers for this decl (such as for declaring
848 const member functions).
850 This is done during the parsing of the struct declaration.
851 The _DECL nodes are chained together and the lot of them
852 are ultimately passed to `build_struct' to make the RECORD_TYPE node.
854 If class A defines that certain functions in class B are friends, then
855 the way I have set things up, it is B who is interested in permission
856 granted by A. However, it is in A's context that these declarations
857 are parsed. By returning a void_type_node, class A does not attempt
858 to incorporate the declarations of the friends within its structure.
860 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
861 CHANGES TO CODE IN `start_method'. */
863 tree
864 grokfield (tree declarator, tree declspecs, tree init, tree asmspec_tree,
865 tree attrlist)
867 tree value;
868 const char *asmspec = 0;
869 int flags = LOOKUP_ONLYCONVERTING;
871 if (declspecs == NULL_TREE
872 && TREE_CODE (declarator) == SCOPE_REF
873 && TREE_CODE (TREE_OPERAND (declarator, 1)) == IDENTIFIER_NODE)
875 /* Access declaration */
876 if (! IS_AGGR_TYPE_CODE (TREE_CODE (TREE_OPERAND (declarator, 0))))
878 else if (TREE_COMPLEXITY (declarator) == current_class_depth)
879 pop_nested_class ();
880 return do_class_using_decl (declarator);
883 if (init
884 && TREE_CODE (init) == TREE_LIST
885 && TREE_VALUE (init) == error_mark_node
886 && TREE_CHAIN (init) == NULL_TREE)
887 init = NULL_TREE;
889 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
890 if (! value || value == error_mark_node)
891 /* friend or constructor went bad. */
892 return value;
893 if (TREE_TYPE (value) == error_mark_node)
894 return error_mark_node;
896 if (TREE_CODE (value) == TYPE_DECL && init)
898 error ("typedef `%D' is initialized (use __typeof__ instead)", value);
899 init = NULL_TREE;
902 /* Pass friendly classes back. */
903 if (value == void_type_node)
904 return value;
906 /* Pass friend decls back. */
907 if ((TREE_CODE (value) == FUNCTION_DECL
908 || TREE_CODE (value) == TEMPLATE_DECL)
909 && DECL_CONTEXT (value) != current_class_type)
910 return value;
912 if (DECL_NAME (value) != NULL_TREE
913 && IDENTIFIER_POINTER (DECL_NAME (value))[0] == '_'
914 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (value)), "_vptr"))
915 error ("member `%D' conflicts with virtual function table field name",
916 value);
918 /* Stash away type declarations. */
919 if (TREE_CODE (value) == TYPE_DECL)
921 DECL_NONLOCAL (value) = 1;
922 DECL_CONTEXT (value) = current_class_type;
924 if (processing_template_decl)
925 value = push_template_decl (value);
927 return value;
930 if (DECL_IN_AGGR_P (value))
932 error ("`%D' is already defined in `%T'", value,
933 DECL_CONTEXT (value));
934 return void_type_node;
937 if (asmspec_tree)
938 asmspec = TREE_STRING_POINTER (asmspec_tree);
940 if (init)
942 if (TREE_CODE (value) == FUNCTION_DECL)
944 grok_function_init (value, init);
945 init = NULL_TREE;
947 else if (pedantic && TREE_CODE (value) != VAR_DECL)
948 /* Already complained in grokdeclarator. */
949 init = NULL_TREE;
950 else
952 /* We allow initializers to become parameters to base
953 initializers. */
954 if (TREE_CODE (init) == TREE_LIST)
956 if (TREE_CHAIN (init) == NULL_TREE)
957 init = TREE_VALUE (init);
958 else
959 init = digest_init (TREE_TYPE (value), init, (tree *)0);
962 if (!processing_template_decl)
964 if (TREE_CODE (init) == CONST_DECL)
965 init = DECL_INITIAL (init);
966 else if (TREE_READONLY_DECL_P (init))
967 init = decl_constant_value (init);
968 else if (TREE_CODE (init) == CONSTRUCTOR)
969 init = digest_init (TREE_TYPE (value), init, (tree *)0);
970 if (init != error_mark_node && ! TREE_CONSTANT (init))
972 /* We can allow references to things that are effectively
973 static, since references are initialized with the
974 address. */
975 if (TREE_CODE (TREE_TYPE (value)) != REFERENCE_TYPE
976 || (TREE_STATIC (init) == 0
977 && (!DECL_P (init) || DECL_EXTERNAL (init) == 0)))
979 error ("field initializer is not constant");
980 init = error_mark_node;
987 if (processing_template_decl
988 && (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == FUNCTION_DECL))
989 value = push_template_decl (value);
991 if (attrlist)
992 cplus_decl_attributes (&value, attrlist, 0);
994 if (TREE_CODE (value) == VAR_DECL)
996 finish_static_data_member_decl (value, init, asmspec_tree,
997 flags);
998 return value;
1000 if (TREE_CODE (value) == FIELD_DECL)
1002 if (asmspec)
1003 error ("`asm' specifiers are not permitted on non-static data members");
1004 if (DECL_INITIAL (value) == error_mark_node)
1005 init = error_mark_node;
1006 cp_finish_decl (value, init, NULL_TREE, flags);
1007 DECL_INITIAL (value) = init;
1008 DECL_IN_AGGR_P (value) = 1;
1009 return value;
1011 if (TREE_CODE (value) == FUNCTION_DECL)
1013 if (asmspec)
1015 /* This must override the asm specifier which was placed
1016 by grokclassfn. Lay this out fresh. */
1017 SET_DECL_RTL (value, NULL_RTX);
1018 SET_DECL_ASSEMBLER_NAME (value, get_identifier (asmspec));
1020 if (!DECL_FRIEND_P (value))
1021 grok_special_member_properties (value);
1023 cp_finish_decl (value, init, asmspec_tree, flags);
1025 /* Pass friends back this way. */
1026 if (DECL_FRIEND_P (value))
1027 return void_type_node;
1029 DECL_IN_AGGR_P (value) = 1;
1030 return value;
1032 abort ();
1033 /* NOTREACHED */
1034 return NULL_TREE;
1037 /* Like `grokfield', but for bitfields.
1038 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1040 tree
1041 grokbitfield (tree declarator, tree declspecs, tree width)
1043 register tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1044 0, NULL);
1046 if (! value) return NULL_TREE; /* friends went bad. */
1048 /* Pass friendly classes back. */
1049 if (TREE_CODE (value) == VOID_TYPE)
1050 return void_type_node;
1052 if (TREE_CODE (value) == TYPE_DECL)
1054 error ("cannot declare `%D' to be a bit-field type", value);
1055 return NULL_TREE;
1058 /* Usually, finish_struct_1 catches bitfields with invalid types.
1059 But, in the case of bitfields with function type, we confuse
1060 ourselves into thinking they are member functions, so we must
1061 check here. */
1062 if (TREE_CODE (value) == FUNCTION_DECL)
1064 error ("cannot declare bit-field `%D' with function type",
1065 DECL_NAME (value));
1066 return NULL_TREE;
1069 if (DECL_IN_AGGR_P (value))
1071 error ("`%D' is already defined in the class %T", value,
1072 DECL_CONTEXT (value));
1073 return void_type_node;
1076 if (TREE_STATIC (value))
1078 error ("static member `%D' cannot be a bit-field", value);
1079 return NULL_TREE;
1081 cp_finish_decl (value, NULL_TREE, NULL_TREE, 0);
1083 if (width != error_mark_node)
1085 constant_expression_warning (width);
1086 DECL_INITIAL (value) = width;
1087 SET_DECL_C_BIT_FIELD (value);
1090 DECL_IN_AGGR_P (value) = 1;
1091 return value;
1094 /* When a function is declared with an initializer,
1095 do the right thing. Currently, there are two possibilities:
1097 class B
1099 public:
1100 // initialization possibility #1.
1101 virtual void f () = 0;
1102 int g ();
1105 class D1 : B
1107 public:
1108 int d1;
1109 // error, no f ();
1112 class D2 : B
1114 public:
1115 int d2;
1116 void f ();
1119 class D3 : B
1121 public:
1122 int d3;
1123 // initialization possibility #2
1124 void f () = B::f;
1129 static void
1130 grok_function_init (tree decl, tree init)
1132 /* An initializer for a function tells how this function should
1133 be inherited. */
1134 tree type = TREE_TYPE (decl);
1136 if (TREE_CODE (type) == FUNCTION_TYPE)
1137 error ("initializer specified for non-member function `%D'", decl);
1138 else if (integer_zerop (init))
1139 DECL_PURE_VIRTUAL_P (decl) = 1;
1140 else
1141 error ("invalid initializer for virtual method `%D'", decl);
1144 void
1145 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1147 if (*decl == NULL_TREE || *decl == void_type_node)
1148 return;
1150 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1151 decl = &DECL_TEMPLATE_RESULT (*decl);
1153 decl_attributes (decl, attributes, flags);
1155 if (TREE_CODE (*decl) == TYPE_DECL)
1156 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1159 /* Return the name for the constructor (or destructor) for the
1160 specified class TYPE. When given a template, this routine doesn't
1161 lose the specialization. */
1163 tree
1164 constructor_name_full (tree type)
1166 type = TYPE_MAIN_VARIANT (type);
1167 if (CLASS_TYPE_P (type) && TYPE_WAS_ANONYMOUS (type)
1168 && TYPE_HAS_CONSTRUCTOR (type))
1169 return DECL_NAME (OVL_CURRENT (CLASSTYPE_CONSTRUCTORS (type)));
1170 else
1171 return TYPE_IDENTIFIER (type);
1174 /* Return the name for the constructor (or destructor) for the
1175 specified class. When given a template, return the plain
1176 unspecialized name. */
1178 tree
1179 constructor_name (tree type)
1181 tree name;
1182 name = constructor_name_full (type);
1183 if (IDENTIFIER_TEMPLATE (name))
1184 name = IDENTIFIER_TEMPLATE (name);
1185 return name;
1188 /* Returns TRUE if NAME is the name for the constructor for TYPE. */
1190 bool
1191 constructor_name_p (tree name, tree type)
1193 tree ctor_name;
1195 if (!name)
1196 return false;
1198 if (TREE_CODE (name) != IDENTIFIER_NODE)
1199 return false;
1201 ctor_name = constructor_name_full (type);
1202 if (name == ctor_name)
1203 return true;
1204 if (IDENTIFIER_TEMPLATE (ctor_name)
1205 && name == IDENTIFIER_TEMPLATE (ctor_name))
1206 return true;
1207 return false;
1211 /* Defer the compilation of the FN until the end of compilation. */
1213 void
1214 defer_fn (tree fn)
1216 if (DECL_DEFERRED_FN (fn))
1217 return;
1218 DECL_DEFERRED_FN (fn) = 1;
1219 DECL_DEFER_OUTPUT (fn) = 1;
1220 if (!deferred_fns)
1221 VARRAY_TREE_INIT (deferred_fns, 32, "deferred_fns");
1223 VARRAY_PUSH_TREE (deferred_fns, fn);
1226 /* Walks through the namespace- or function-scope anonymous union OBJECT,
1227 building appropriate ALIAS_DECLs. Returns one of the fields for use in
1228 the mangled name. */
1230 static tree
1231 build_anon_union_vars (tree object)
1233 tree type = TREE_TYPE (object);
1234 tree main_decl = NULL_TREE;
1235 tree field;
1237 /* Rather than write the code to handle the non-union case,
1238 just give an error. */
1239 if (TREE_CODE (type) != UNION_TYPE)
1240 error ("anonymous struct not inside named type");
1242 for (field = TYPE_FIELDS (type);
1243 field != NULL_TREE;
1244 field = TREE_CHAIN (field))
1246 tree decl;
1247 tree ref;
1249 if (DECL_ARTIFICIAL (field))
1250 continue;
1251 if (TREE_CODE (field) != FIELD_DECL)
1253 cp_pedwarn_at ("\
1254 `%#D' invalid; an anonymous union can only have non-static data members",
1255 field);
1256 continue;
1259 if (TREE_PRIVATE (field))
1260 cp_pedwarn_at ("private member `%#D' in anonymous union", field);
1261 else if (TREE_PROTECTED (field))
1262 cp_pedwarn_at ("protected member `%#D' in anonymous union", field);
1264 if (processing_template_decl)
1265 ref = build_min_nt (COMPONENT_REF, object, DECL_NAME (field));
1266 else
1267 ref = build_class_member_access_expr (object, field, NULL_TREE,
1268 false);
1270 if (DECL_NAME (field))
1272 decl = build_decl (ALIAS_DECL, DECL_NAME (field), TREE_TYPE (field));
1273 DECL_INITIAL (decl) = ref;
1274 TREE_PUBLIC (decl) = 0;
1275 TREE_STATIC (decl) = 0;
1276 DECL_EXTERNAL (decl) = 1;
1277 decl = pushdecl (decl);
1279 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1280 decl = build_anon_union_vars (ref);
1281 else
1282 decl = 0;
1284 if (main_decl == NULL_TREE)
1285 main_decl = decl;
1288 return main_decl;
1291 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1292 anonymous union, then all members must be laid out together. PUBLIC_P
1293 is nonzero if this union is not declared static. */
1295 void
1296 finish_anon_union (tree anon_union_decl)
1298 tree type = TREE_TYPE (anon_union_decl);
1299 tree main_decl;
1300 bool public_p = TREE_PUBLIC (anon_union_decl);
1302 /* The VAR_DECL's context is the same as the TYPE's context. */
1303 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1305 if (TYPE_FIELDS (type) == NULL_TREE)
1306 return;
1308 if (public_p)
1310 error ("namespace-scope anonymous aggregates must be static");
1311 return;
1314 main_decl = build_anon_union_vars (anon_union_decl);
1315 if (main_decl == NULL_TREE)
1317 warning ("anonymous union with no members");
1318 return;
1321 if (!processing_template_decl)
1323 /* Use main_decl to set the mangled name. */
1324 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1325 mangle_decl (anon_union_decl);
1326 DECL_NAME (anon_union_decl) = NULL_TREE;
1329 pushdecl (anon_union_decl);
1330 if (building_stmt_tree ()
1331 && at_function_scope_p ())
1332 add_decl_stmt (anon_union_decl);
1333 else if (!processing_template_decl)
1334 rest_of_decl_compilation (anon_union_decl, NULL,
1335 toplevel_bindings_p (), at_eof);
1338 /* Auxiliary functions to make type signatures for
1339 `operator new' and `operator delete' correspond to
1340 what compiler will be expecting. */
1342 tree
1343 coerce_new_type (tree type)
1345 int e = 0;
1346 tree args = TYPE_ARG_TYPES (type);
1348 my_friendly_assert (TREE_CODE (type) == FUNCTION_TYPE, 20001107);
1350 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1351 e = 1, error ("`operator new' must return type `%T'", ptr_type_node);
1353 if (!args || args == void_list_node
1354 || !same_type_p (TREE_VALUE (args), size_type_node))
1356 e = 2;
1357 if (args && args != void_list_node)
1358 args = TREE_CHAIN (args);
1359 pedwarn ("`operator new' takes type `size_t' (`%T') as first parameter", size_type_node);
1361 switch (e)
1363 case 2:
1364 args = tree_cons (NULL_TREE, size_type_node, args);
1365 /* FALLTHROUGH */
1366 case 1:
1367 type = build_exception_variant
1368 (build_function_type (ptr_type_node, args),
1369 TYPE_RAISES_EXCEPTIONS (type));
1370 /* FALLTHROUGH */
1371 default:;
1373 return type;
1376 tree
1377 coerce_delete_type (tree type)
1379 int e = 0;
1380 tree args = TYPE_ARG_TYPES (type);
1382 my_friendly_assert (TREE_CODE (type) == FUNCTION_TYPE, 20001107);
1384 if (!same_type_p (TREE_TYPE (type), void_type_node))
1385 e = 1, error ("`operator delete' must return type `%T'", void_type_node);
1387 if (!args || args == void_list_node
1388 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1390 e = 2;
1391 if (args && args != void_list_node)
1392 args = TREE_CHAIN (args);
1393 error ("`operator delete' takes type `%T' as first parameter", ptr_type_node);
1395 switch (e)
1397 case 2:
1398 args = tree_cons (NULL_TREE, ptr_type_node, args);
1399 /* FALLTHROUGH */
1400 case 1:
1401 type = build_exception_variant
1402 (build_function_type (void_type_node, args),
1403 TYPE_RAISES_EXCEPTIONS (type));
1404 /* FALLTHROUGH */
1405 default:;
1408 return type;
1411 static void
1412 mark_vtable_entries (tree decl)
1414 tree entries = CONSTRUCTOR_ELTS (DECL_INITIAL (decl));
1416 for (; entries; entries = TREE_CHAIN (entries))
1418 tree fnaddr = TREE_VALUE (entries);
1419 tree fn;
1421 STRIP_NOPS (fnaddr);
1423 if (TREE_CODE (fnaddr) != ADDR_EXPR
1424 && TREE_CODE (fnaddr) != FDESC_EXPR)
1425 /* This entry is an offset: a virtual base class offset, a
1426 virtual call offset, an RTTI offset, etc. */
1427 continue;
1429 fn = TREE_OPERAND (fnaddr, 0);
1430 TREE_ADDRESSABLE (fn) = 1;
1431 /* When we don't have vcall offsets, we output thunks whenever
1432 we output the vtables that contain them. With vcall offsets,
1433 we know all the thunks we'll need when we emit a virtual
1434 function, so we emit the thunks there instead. */
1435 if (DECL_THUNK_P (fn))
1436 use_thunk (fn, /*emit_p=*/0);
1437 mark_used (fn);
1441 /* Set DECL up to have the closest approximation of "initialized common"
1442 linkage available. */
1444 void
1445 comdat_linkage (tree decl)
1447 if (flag_weak)
1448 make_decl_one_only (decl);
1449 else if (TREE_CODE (decl) == FUNCTION_DECL
1450 || (TREE_CODE (decl) == VAR_DECL && DECL_ARTIFICIAL (decl)))
1451 /* We can just emit function and compiler-generated variables
1452 statically; having multiple copies is (for the most part) only
1453 a waste of space.
1455 There are two correctness issues, however: the address of a
1456 template instantiation with external linkage should be the
1457 same, independent of what translation unit asks for the
1458 address, and this will not hold when we emit multiple copies of
1459 the function. However, there's little else we can do.
1461 Also, by default, the typeinfo implementation assumes that
1462 there will be only one copy of the string used as the name for
1463 each type. Therefore, if weak symbols are unavailable, the
1464 run-time library should perform a more conservative check; it
1465 should perform a string comparison, rather than an address
1466 comparison. */
1467 TREE_PUBLIC (decl) = 0;
1468 else
1470 /* Static data member template instantiations, however, cannot
1471 have multiple copies. */
1472 if (DECL_INITIAL (decl) == 0
1473 || DECL_INITIAL (decl) == error_mark_node)
1474 DECL_COMMON (decl) = 1;
1475 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1477 DECL_COMMON (decl) = 1;
1478 DECL_INITIAL (decl) = error_mark_node;
1480 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1482 /* We can't do anything useful; leave vars for explicit
1483 instantiation. */
1484 DECL_EXTERNAL (decl) = 1;
1485 DECL_NOT_REALLY_EXTERN (decl) = 0;
1489 if (DECL_LANG_SPECIFIC (decl))
1490 DECL_COMDAT (decl) = 1;
1493 /* For win32 we also want to put explicit instantiations in
1494 linkonce sections, so that they will be merged with implicit
1495 instantiations; otherwise we get duplicate symbol errors. */
1497 void
1498 maybe_make_one_only (tree decl)
1500 /* We used to say that this was not necessary on targets that support weak
1501 symbols, because the implicit instantiations will defer to the explicit
1502 one. However, that's not actually the case in SVR4; a strong definition
1503 after a weak one is an error. Also, not making explicit
1504 instantiations one_only means that we can end up with two copies of
1505 some template instantiations. */
1506 if (! flag_weak)
1507 return;
1509 /* We can't set DECL_COMDAT on functions, or finish_file will think
1510 we can get away with not emitting them if they aren't used. We need
1511 to for variables so that cp_finish_decl will update their linkage,
1512 because their DECL_INITIAL may not have been set properly yet. */
1514 make_decl_one_only (decl);
1516 if (TREE_CODE (decl) == VAR_DECL)
1518 DECL_COMDAT (decl) = 1;
1519 /* Mark it needed so we don't forget to emit it. */
1520 mark_referenced (DECL_ASSEMBLER_NAME (decl));
1524 /* Set TREE_PUBLIC and/or DECL_EXTERN on the vtable DECL,
1525 based on TYPE and other static flags.
1527 Note that anything public is tagged TREE_PUBLIC, whether
1528 it's public in this file or in another one. */
1530 void
1531 import_export_vtable (tree decl, tree type, int final)
1533 if (DECL_INTERFACE_KNOWN (decl))
1534 return;
1536 if (TYPE_FOR_JAVA (type))
1538 TREE_PUBLIC (decl) = 1;
1539 DECL_EXTERNAL (decl) = 1;
1540 DECL_INTERFACE_KNOWN (decl) = 1;
1542 else if (CLASSTYPE_INTERFACE_KNOWN (type))
1544 TREE_PUBLIC (decl) = 1;
1545 DECL_EXTERNAL (decl) = CLASSTYPE_INTERFACE_ONLY (type);
1546 DECL_INTERFACE_KNOWN (decl) = 1;
1548 else
1550 /* We can only wait to decide if we have real non-inline virtual
1551 functions in our class, or if we come from a template. */
1553 int found = (CLASSTYPE_TEMPLATE_INSTANTIATION (type)
1554 || CLASSTYPE_KEY_METHOD (type) != NULL_TREE);
1556 if (final || ! found)
1558 comdat_linkage (decl);
1559 DECL_EXTERNAL (decl) = 0;
1561 else
1563 TREE_PUBLIC (decl) = 1;
1564 DECL_EXTERNAL (decl) = 1;
1569 /* Determine whether or not we want to specifically import or export CTYPE,
1570 using various heuristics. */
1572 static void
1573 import_export_class (tree ctype)
1575 /* -1 for imported, 1 for exported. */
1576 int import_export = 0;
1578 /* It only makes sense to call this function at EOF. The reason is
1579 that this function looks at whether or not the first non-inline
1580 non-abstract virtual member function has been defined in this
1581 translation unit. But, we can't possibly know that until we've
1582 seen the entire translation unit. */
1583 my_friendly_assert (at_eof, 20000226);
1585 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1586 return;
1588 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma interface,
1589 we will have CLASSTYPE_INTERFACE_ONLY set but not
1590 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1591 heuristic because someone will supply a #pragma implementation
1592 elsewhere, and deducing it here would produce a conflict. */
1593 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1594 return;
1596 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1597 import_export = -1;
1598 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1599 import_export = 1;
1601 /* If we got -fno-implicit-templates, we import template classes that
1602 weren't explicitly instantiated. */
1603 if (import_export == 0
1604 && CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1605 && ! flag_implicit_templates)
1606 import_export = -1;
1608 /* Base our import/export status on that of the first non-inline,
1609 non-pure virtual function, if any. */
1610 if (import_export == 0
1611 && TYPE_POLYMORPHIC_P (ctype))
1613 tree method = CLASSTYPE_KEY_METHOD (ctype);
1614 if (method)
1615 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1618 #ifdef MULTIPLE_SYMBOL_SPACES
1619 if (import_export == -1)
1620 import_export = 0;
1621 #endif
1623 if (import_export)
1625 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1626 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
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;
1638 bool needed = false;
1640 /* If the vtables for this class have already been emitted there is
1641 nothing more to do. */
1642 primary_vtbl = CLASSTYPE_VTABLES (ctype);
1643 if (TREE_ASM_WRITTEN (primary_vtbl))
1644 return false;
1645 /* Ignore dummy vtables made by get_vtable_decl. */
1646 if (TREE_TYPE (primary_vtbl) == void_type_node)
1647 return false;
1649 import_export_class (ctype);
1650 import_export_vtable (primary_vtbl, ctype, 1);
1652 /* See if any of the vtables are needed. */
1653 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = TREE_CHAIN (vtbl))
1654 if (!DECL_EXTERNAL (vtbl) && DECL_NEEDED_P (vtbl))
1655 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;
1666 else if (TREE_PUBLIC (vtbl) && !DECL_COMDAT (vtbl))
1667 needed = true;
1670 /* The ABI requires that we emit all of the vtables if we emit any
1671 of them. */
1672 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = TREE_CHAIN (vtbl))
1674 /* Write it out. */
1675 import_export_vtable (vtbl, ctype, 1);
1676 mark_vtable_entries (vtbl);
1678 /* If we know that DECL is needed, mark it as such for the varpool. */
1679 if (needed)
1680 cgraph_varpool_mark_needed_node (cgraph_varpool_node (vtbl));
1682 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
1683 store_init_value (vtbl, DECL_INITIAL (vtbl));
1685 if (write_symbols == DWARF_DEBUG || write_symbols == DWARF2_DEBUG)
1687 /* Mark the VAR_DECL node representing the vtable itself as a
1688 "gratuitous" one, thereby forcing dwarfout.c to ignore it.
1689 It is rather important that such things be ignored because
1690 any effort to actually generate DWARF for them will run
1691 into trouble when/if we encounter code like:
1693 #pragma interface
1694 struct S { virtual void member (); };
1696 because the artificial declaration of the vtable itself (as
1697 manufactured by the g++ front end) will say that the vtable
1698 is a static member of `S' but only *after* the debug output
1699 for the definition of `S' has already been output. This causes
1700 grief because the DWARF entry for the definition of the vtable
1701 will try to refer back to an earlier *declaration* of the
1702 vtable as a static member of `S' and there won't be one.
1703 We might be able to arrange to have the "vtable static member"
1704 attached to the member list for `S' before the debug info for
1705 `S' get written (which would solve the problem) but that would
1706 require more intrusive changes to the g++ front end. */
1708 DECL_IGNORED_P (vtbl) = 1;
1711 /* Always make vtables weak. */
1712 if (flag_weak)
1713 comdat_linkage (vtbl);
1715 rest_of_decl_compilation (vtbl, NULL, 1, 1);
1717 /* Because we're only doing syntax-checking, we'll never end up
1718 actually marking the variable as written. */
1719 if (flag_syntax_only)
1720 TREE_ASM_WRITTEN (vtbl) = 1;
1723 /* Since we're writing out the vtable here, also write the debug
1724 info. */
1725 note_debug_info_needed (ctype);
1727 return true;
1730 /* Determines the proper settings of TREE_PUBLIC and DECL_EXTERNAL for an
1731 inline function or template instantiation at end-of-file. */
1733 void
1734 import_export_decl (tree decl)
1736 if (DECL_INTERFACE_KNOWN (decl))
1737 return;
1739 if (DECL_TEMPLATE_INSTANTIATION (decl)
1740 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
1742 DECL_NOT_REALLY_EXTERN (decl) = 1;
1743 if ((DECL_IMPLICIT_INSTANTIATION (decl)
1744 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
1745 && (flag_implicit_templates
1746 || (flag_implicit_inline_templates
1747 && TREE_CODE (decl) == FUNCTION_DECL
1748 && DECL_DECLARED_INLINE_P (decl))))
1750 if (!TREE_PUBLIC (decl))
1751 /* Templates are allowed to have internal linkage. See
1752 [basic.link]. */
1754 else
1755 comdat_linkage (decl);
1757 else
1759 DECL_EXTERNAL (decl) = 1;
1760 DECL_NOT_REALLY_EXTERN (decl) = 0;
1763 else if (DECL_FUNCTION_MEMBER_P (decl))
1765 if (!DECL_DECLARED_INLINE_P (decl))
1767 tree ctype = DECL_CONTEXT (decl);
1768 import_export_class (ctype);
1769 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1771 DECL_NOT_REALLY_EXTERN (decl)
1772 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
1773 || (DECL_DECLARED_INLINE_P (decl)
1774 && ! flag_implement_inlines
1775 && !DECL_VINDEX (decl)));
1777 if (!DECL_NOT_REALLY_EXTERN (decl))
1778 DECL_EXTERNAL (decl) = 1;
1780 /* Always make artificials weak. */
1781 if (DECL_ARTIFICIAL (decl) && flag_weak)
1782 comdat_linkage (decl);
1783 else
1784 maybe_make_one_only (decl);
1787 else
1788 comdat_linkage (decl);
1790 else
1791 comdat_linkage (decl);
1793 DECL_INTERFACE_KNOWN (decl) = 1;
1796 /* Here, we only decide whether or not the tinfo node should be
1797 emitted with the vtable. IS_IN_LIBRARY is nonzero iff the
1798 typeinfo for TYPE should be in the runtime library. */
1800 void
1801 import_export_tinfo (tree decl, tree type, bool is_in_library)
1803 if (DECL_INTERFACE_KNOWN (decl))
1804 return;
1806 if (IS_AGGR_TYPE (type))
1807 import_export_class (type);
1809 if (IS_AGGR_TYPE (type) && CLASSTYPE_INTERFACE_KNOWN (type)
1810 && TYPE_POLYMORPHIC_P (type)
1811 /* If -fno-rtti, we're not necessarily emitting this stuff with
1812 the class, so go ahead and emit it now. This can happen when
1813 a class is used in exception handling. */
1814 && flag_rtti)
1816 DECL_NOT_REALLY_EXTERN (decl) = !CLASSTYPE_INTERFACE_ONLY (type);
1817 DECL_COMDAT (decl) = 0;
1819 else
1821 DECL_NOT_REALLY_EXTERN (decl) = 1;
1822 DECL_COMDAT (decl) = 1;
1825 /* Now override some cases. */
1826 if (flag_weak)
1827 DECL_COMDAT (decl) = 1;
1828 else if (is_in_library)
1829 DECL_COMDAT (decl) = 0;
1831 DECL_INTERFACE_KNOWN (decl) = 1;
1834 /* Return an expression that performs the destruction of DECL, which
1835 must be a VAR_DECL whose type has a non-trivial destructor, or is
1836 an array whose (innermost) elements have a non-trivial destructor. */
1838 tree
1839 build_cleanup (tree decl)
1841 tree temp;
1842 tree type = TREE_TYPE (decl);
1844 /* This function should only be called for declarations that really
1845 require cleanups. */
1846 my_friendly_assert (!TYPE_HAS_TRIVIAL_DESTRUCTOR (type), 20030106);
1848 /* Treat all objects with destructors as used; the destructor may do
1849 something substantive. */
1850 mark_used (decl);
1852 if (TREE_CODE (type) == ARRAY_TYPE)
1853 temp = decl;
1854 else
1856 cxx_mark_addressable (decl);
1857 temp = build1 (ADDR_EXPR, build_pointer_type (type), decl);
1859 temp = build_delete (TREE_TYPE (temp), temp,
1860 sfk_complete_destructor,
1861 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
1862 return temp;
1865 /* Returns the initialization guard variable for the variable DECL,
1866 which has static storage duration. */
1868 tree
1869 get_guard (tree decl)
1871 tree sname;
1872 tree guard;
1874 sname = mangle_guard_variable (decl);
1875 guard = IDENTIFIER_GLOBAL_VALUE (sname);
1876 if (! guard)
1878 tree guard_type;
1880 /* We use a type that is big enough to contain a mutex as well
1881 as an integer counter. */
1882 guard_type = long_long_integer_type_node;
1883 guard = build_decl (VAR_DECL, sname, guard_type);
1885 /* The guard should have the same linkage as what it guards. */
1886 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
1887 TREE_STATIC (guard) = TREE_STATIC (decl);
1888 DECL_COMMON (guard) = DECL_COMMON (decl);
1889 DECL_ONE_ONLY (guard) = DECL_ONE_ONLY (decl);
1890 if (TREE_PUBLIC (decl))
1891 DECL_WEAK (guard) = DECL_WEAK (decl);
1893 DECL_ARTIFICIAL (guard) = 1;
1894 TREE_USED (guard) = 1;
1895 pushdecl_top_level_and_finish (guard, NULL_TREE);
1897 return guard;
1900 /* Return those bits of the GUARD variable that should be set when the
1901 guarded entity is actually initialized. */
1903 static tree
1904 get_guard_bits (tree guard)
1906 /* We only set the first byte of the guard, in order to leave room
1907 for a mutex in the high-order bits. */
1908 guard = build1 (ADDR_EXPR,
1909 build_pointer_type (TREE_TYPE (guard)),
1910 guard);
1911 guard = build1 (NOP_EXPR,
1912 build_pointer_type (char_type_node),
1913 guard);
1914 guard = build1 (INDIRECT_REF, char_type_node, guard);
1916 return guard;
1919 /* Return an expression which determines whether or not the GUARD
1920 variable has already been initialized. */
1922 tree
1923 get_guard_cond (tree guard)
1925 tree guard_value;
1927 /* Check to see if the GUARD is zero. */
1928 guard = get_guard_bits (guard);
1929 guard_value = integer_zero_node;
1930 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
1931 guard_value = convert (TREE_TYPE (guard), guard_value);
1932 return cp_build_binary_op (EQ_EXPR, guard, guard_value);
1935 /* Return an expression which sets the GUARD variable, indicating that
1936 the variable being guarded has been initialized. */
1938 tree
1939 set_guard (tree guard)
1941 tree guard_init;
1943 /* Set the GUARD to one. */
1944 guard = get_guard_bits (guard);
1945 guard_init = integer_one_node;
1946 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
1947 guard_init = convert (TREE_TYPE (guard), guard_init);
1948 return build_modify_expr (guard, NOP_EXPR, guard_init);
1951 /* Start the process of running a particular set of global constructors
1952 or destructors. Subroutine of do_[cd]tors. */
1954 static tree
1955 start_objects (int method_type, int initp)
1957 tree fnname;
1958 tree body;
1959 char type[10];
1961 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
1963 if (initp != DEFAULT_INIT_PRIORITY)
1965 char joiner;
1967 #ifdef JOINER
1968 joiner = JOINER;
1969 #else
1970 joiner = '_';
1971 #endif
1973 sprintf (type, "%c%c%.5u", method_type, joiner, initp);
1975 else
1976 sprintf (type, "%c", method_type);
1978 fnname = get_file_function_name_long (type);
1980 start_function (void_list_node,
1981 make_call_declarator (fnname, void_list_node, NULL_TREE,
1982 NULL_TREE),
1983 NULL_TREE, SF_DEFAULT);
1985 /* It can be a static function as long as collect2 does not have
1986 to scan the object file to find its ctor/dtor routine. */
1987 TREE_PUBLIC (current_function_decl) = ! targetm.have_ctors_dtors;
1989 /* Mark this declaration as used to avoid spurious warnings. */
1990 TREE_USED (current_function_decl) = 1;
1992 /* Mark this function as a global constructor or destructor. */
1993 if (method_type == 'I')
1994 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
1995 else
1996 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
1997 DECL_LANG_SPECIFIC (current_function_decl)->decl_flags.u2sel = 1;
1999 body = begin_compound_stmt (/*has_no_scope=*/false);
2001 /* We cannot allow these functions to be elided, even if they do not
2002 have external linkage. And, there's no point in deferring
2003 compilation of thes functions; they're all going to have to be
2004 out anyhow. */
2005 current_function_cannot_inline
2006 = "static constructors and destructors cannot be inlined";
2008 return body;
2011 /* Finish the process of running a particular set of global constructors
2012 or destructors. Subroutine of do_[cd]tors. */
2014 static void
2015 finish_objects (int method_type, int initp, tree body)
2017 tree fn;
2019 /* Finish up. */
2020 finish_compound_stmt (body);
2021 fn = finish_function (0);
2022 expand_or_defer_fn (fn);
2024 /* When only doing semantic analysis, and no RTL generation, we
2025 can't call functions that directly emit assembly code; there is
2026 no assembly file in which to put the code. */
2027 if (flag_syntax_only)
2028 return;
2030 if (targetm.have_ctors_dtors)
2032 rtx fnsym = XEXP (DECL_RTL (fn), 0);
2033 if (method_type == 'I')
2034 (* targetm.asm_out.constructor) (fnsym, initp);
2035 else
2036 (* targetm.asm_out.destructor) (fnsym, initp);
2040 /* The names of the parameters to the function created to handle
2041 initializations and destructions for objects with static storage
2042 duration. */
2043 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
2044 #define PRIORITY_IDENTIFIER "__priority"
2046 /* The name of the function we create to handle initializations and
2047 destructions for objects with static storage duration. */
2048 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
2050 /* The declaration for the __INITIALIZE_P argument. */
2051 static GTY(()) tree initialize_p_decl;
2053 /* The declaration for the __PRIORITY argument. */
2054 static GTY(()) tree priority_decl;
2056 /* The declaration for the static storage duration function. */
2057 static GTY(()) tree ssdf_decl;
2059 /* All the static storage duration functions created in this
2060 translation unit. */
2061 static GTY(()) varray_type ssdf_decls;
2063 /* A map from priority levels to information about that priority
2064 level. There may be many such levels, so efficient lookup is
2065 important. */
2066 static splay_tree priority_info_map;
2068 /* Begins the generation of the function that will handle all
2069 initialization and destruction of objects with static storage
2070 duration. The function generated takes two parameters of type
2071 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
2072 nonzero, it performs initializations. Otherwise, it performs
2073 destructions. It only performs those initializations or
2074 destructions with the indicated __PRIORITY. The generated function
2075 returns no value.
2077 It is assumed that this function will only be called once per
2078 translation unit. */
2080 static tree
2081 start_static_storage_duration_function (unsigned count)
2083 tree parm_types;
2084 tree type;
2085 tree body;
2086 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
2088 /* Create the identifier for this function. It will be of the form
2089 SSDF_IDENTIFIER_<number>. */
2090 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
2092 /* Create the parameters. */
2093 parm_types = void_list_node;
2094 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
2095 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
2096 type = build_function_type (void_type_node, parm_types);
2098 /* Create the FUNCTION_DECL itself. */
2099 ssdf_decl = build_lang_decl (FUNCTION_DECL,
2100 get_identifier (id),
2101 type);
2102 TREE_PUBLIC (ssdf_decl) = 0;
2103 DECL_ARTIFICIAL (ssdf_decl) = 1;
2105 /* Put this function in the list of functions to be called from the
2106 static constructors and destructors. */
2107 if (!ssdf_decls)
2109 VARRAY_TREE_INIT (ssdf_decls, 32, "ssdf_decls");
2111 /* Take this opportunity to initialize the map from priority
2112 numbers to information about that priority level. */
2113 priority_info_map = splay_tree_new (splay_tree_compare_ints,
2114 /*delete_key_fn=*/0,
2115 /*delete_value_fn=*/
2116 (splay_tree_delete_value_fn) &free);
2118 /* We always need to generate functions for the
2119 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
2120 priorities later, we'll be sure to find the
2121 DEFAULT_INIT_PRIORITY. */
2122 get_priority_info (DEFAULT_INIT_PRIORITY);
2125 VARRAY_PUSH_TREE (ssdf_decls, ssdf_decl);
2127 /* Create the argument list. */
2128 initialize_p_decl = cp_build_parm_decl
2129 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
2130 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
2131 TREE_USED (initialize_p_decl) = 1;
2132 priority_decl = cp_build_parm_decl
2133 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
2134 DECL_CONTEXT (priority_decl) = ssdf_decl;
2135 TREE_USED (priority_decl) = 1;
2137 TREE_CHAIN (initialize_p_decl) = priority_decl;
2138 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
2140 /* Put the function in the global scope. */
2141 pushdecl (ssdf_decl);
2143 /* Start the function itself. This is equivalent to declarating the
2144 function as:
2146 static void __ssdf (int __initialize_p, init __priority_p);
2148 It is static because we only need to call this function from the
2149 various constructor and destructor functions for this module. */
2150 start_function (/*specs=*/NULL_TREE,
2151 ssdf_decl,
2152 /*attrs=*/NULL_TREE,
2153 SF_PRE_PARSED);
2155 /* Set up the scope of the outermost block in the function. */
2156 body = begin_compound_stmt (/*has_no_scope=*/false);
2158 /* This function must not be deferred because we are depending on
2159 its compilation to tell us what is TREE_SYMBOL_REFERENCED. */
2160 current_function_cannot_inline
2161 = "static storage duration functions cannot be inlined";
2163 return body;
2166 /* Finish the generation of the function which performs initialization
2167 and destruction of objects with static storage duration. After
2168 this point, no more such objects can be created. */
2170 static void
2171 finish_static_storage_duration_function (tree body)
2173 /* Close out the function. */
2174 finish_compound_stmt (body);
2175 expand_or_defer_fn (finish_function (0));
2178 /* Return the information about the indicated PRIORITY level. If no
2179 code to handle this level has yet been generated, generate the
2180 appropriate prologue. */
2182 static priority_info
2183 get_priority_info (int priority)
2185 priority_info pi;
2186 splay_tree_node n;
2188 n = splay_tree_lookup (priority_info_map,
2189 (splay_tree_key) priority);
2190 if (!n)
2192 /* Create a new priority information structure, and insert it
2193 into the map. */
2194 pi = xmalloc (sizeof (struct priority_info_s));
2195 pi->initializations_p = 0;
2196 pi->destructions_p = 0;
2197 splay_tree_insert (priority_info_map,
2198 (splay_tree_key) priority,
2199 (splay_tree_value) pi);
2201 else
2202 pi = (priority_info) n->value;
2204 return pi;
2207 /* Set up to handle the initialization or destruction of DECL. If
2208 INITP is nonzero, we are initializing the variable. Otherwise, we
2209 are destroying it. */
2211 static tree
2212 start_static_initialization_or_destruction (tree decl, int initp)
2214 tree guard_if_stmt = NULL_TREE;
2215 int priority;
2216 tree cond;
2217 tree guard;
2218 tree init_cond;
2219 priority_info pi;
2221 /* Figure out the priority for this declaration. */
2222 priority = DECL_INIT_PRIORITY (decl);
2223 if (!priority)
2224 priority = DEFAULT_INIT_PRIORITY;
2226 /* Remember that we had an initialization or finalization at this
2227 priority. */
2228 pi = get_priority_info (priority);
2229 if (initp)
2230 pi->initializations_p = 1;
2231 else
2232 pi->destructions_p = 1;
2234 /* Trick the compiler into thinking we are at the file and line
2235 where DECL was declared so that error-messages make sense, and so
2236 that the debugger will show somewhat sensible file and line
2237 information. */
2238 input_location = DECL_SOURCE_LOCATION (decl);
2240 /* Because of:
2242 [class.access.spec]
2244 Access control for implicit calls to the constructors,
2245 the conversion functions, or the destructor called to
2246 create and destroy a static data member is performed as
2247 if these calls appeared in the scope of the member's
2248 class.
2250 we pretend we are in a static member function of the class of
2251 which the DECL is a member. */
2252 if (member_p (decl))
2254 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
2255 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
2258 /* Conditionalize this initialization on being in the right priority
2259 and being initializing/finalizing appropriately. */
2260 guard_if_stmt = begin_if_stmt ();
2261 cond = cp_build_binary_op (EQ_EXPR,
2262 priority_decl,
2263 build_int_2 (priority, 0));
2264 init_cond = initp ? integer_one_node : integer_zero_node;
2265 init_cond = cp_build_binary_op (EQ_EXPR,
2266 initialize_p_decl,
2267 init_cond);
2268 cond = cp_build_binary_op (TRUTH_ANDIF_EXPR, cond, init_cond);
2270 /* Assume we don't need a guard. */
2271 guard = NULL_TREE;
2272 /* We need a guard if this is an object with external linkage that
2273 might be initialized in more than one place. (For example, a
2274 static data member of a template, when the data member requires
2275 construction.) */
2276 if (TREE_PUBLIC (decl) && (DECL_COMMON (decl)
2277 || DECL_ONE_ONLY (decl)
2278 || DECL_WEAK (decl)))
2280 tree guard_cond;
2282 guard = get_guard (decl);
2284 /* When using __cxa_atexit, we just check the GUARD as we would
2285 for a local static. */
2286 if (flag_use_cxa_atexit)
2288 /* When using __cxa_atexit, we never try to destroy
2289 anything from a static destructor. */
2290 my_friendly_assert (initp, 20000629);
2291 guard_cond = get_guard_cond (guard);
2293 /* If we don't have __cxa_atexit, then we will be running
2294 destructors from .fini sections, or their equivalents. So,
2295 we need to know how many times we've tried to initialize this
2296 object. We do initializations only if the GUARD is zero,
2297 i.e., if we are the first to initialize the variable. We do
2298 destructions only if the GUARD is one, i.e., if we are the
2299 last to destroy the variable. */
2300 else if (initp)
2301 guard_cond
2302 = cp_build_binary_op (EQ_EXPR,
2303 build_unary_op (PREINCREMENT_EXPR,
2304 guard,
2305 /*noconvert=*/1),
2306 integer_one_node);
2307 else
2308 guard_cond
2309 = cp_build_binary_op (EQ_EXPR,
2310 build_unary_op (PREDECREMENT_EXPR,
2311 guard,
2312 /*noconvert=*/1),
2313 integer_zero_node);
2315 cond = cp_build_binary_op (TRUTH_ANDIF_EXPR, cond, guard_cond);
2318 finish_if_stmt_cond (cond, guard_if_stmt);
2320 /* If we're using __cxa_atexit, we have not already set the GUARD,
2321 so we must do so now. */
2322 if (guard && initp && flag_use_cxa_atexit)
2323 finish_expr_stmt (set_guard (guard));
2325 return guard_if_stmt;
2328 /* We've just finished generating code to do an initialization or
2329 finalization. GUARD_IF_STMT is the if-statement we used to guard
2330 the initialization. */
2332 static void
2333 finish_static_initialization_or_destruction (tree guard_if_stmt)
2335 finish_then_clause (guard_if_stmt);
2336 finish_if_stmt ();
2338 /* Now that we're done with DECL we don't need to pretend to be a
2339 member of its class any longer. */
2340 DECL_CONTEXT (current_function_decl) = NULL_TREE;
2341 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
2344 /* Generate code to do the initialization of DECL, a VAR_DECL with
2345 static storage duration. The initialization is INIT. */
2347 static void
2348 do_static_initialization (tree decl, tree init)
2350 tree guard_if_stmt;
2352 /* Set up for the initialization. */
2353 guard_if_stmt
2354 = start_static_initialization_or_destruction (decl,
2355 /*initp=*/1);
2357 /* Perform the initialization. */
2358 if (init)
2359 finish_expr_stmt (init);
2361 /* If we're using __cxa_atexit, register a a function that calls the
2362 destructor for the object. */
2363 if (flag_use_cxa_atexit)
2364 register_dtor_fn (decl);
2366 /* Finsh up. */
2367 finish_static_initialization_or_destruction (guard_if_stmt);
2370 /* Generate code to do the static destruction of DECL. If DECL may be
2371 initialized more than once in different object files, GUARD is the
2372 guard variable to check. PRIORITY is the priority for the
2373 destruction. */
2375 static void
2376 do_static_destruction (tree decl)
2378 tree guard_if_stmt;
2380 /* If we're using __cxa_atexit, then destructors are registered
2381 immediately after objects are initialized. */
2382 my_friendly_assert (!flag_use_cxa_atexit, 20000121);
2384 /* If we don't need a destructor, there's nothing to do. */
2385 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
2386 return;
2388 /* Actually do the destruction. */
2389 guard_if_stmt = start_static_initialization_or_destruction (decl,
2390 /*initp=*/0);
2391 finish_expr_stmt (build_cleanup (decl));
2392 finish_static_initialization_or_destruction (guard_if_stmt);
2395 /* VARS is a list of variables with static storage duration which may
2396 need initialization and/or finalization. Remove those variables
2397 that don't really need to be initialized or finalized, and return
2398 the resulting list. The order in which the variables appear in
2399 VARS is in reverse order of the order in which they should actually
2400 be initialized. The list we return is in the unreversed order;
2401 i.e., the first variable should be initialized first. */
2403 static tree
2404 prune_vars_needing_no_initialization (tree *vars)
2406 tree *var = vars;
2407 tree result = NULL_TREE;
2409 while (*var)
2411 tree t = *var;
2412 tree decl = TREE_VALUE (t);
2413 tree init = TREE_PURPOSE (t);
2415 /* Deal gracefully with error. */
2416 if (decl == error_mark_node)
2418 var = &TREE_CHAIN (t);
2419 continue;
2422 /* The only things that can be initialized are variables. */
2423 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 19990420);
2425 /* If this object is not defined, we don't need to do anything
2426 here. */
2427 if (DECL_EXTERNAL (decl))
2429 var = &TREE_CHAIN (t);
2430 continue;
2433 /* Also, if the initializer already contains errors, we can bail
2434 out now. */
2435 if (init && TREE_CODE (init) == TREE_LIST
2436 && value_member (error_mark_node, init))
2438 var = &TREE_CHAIN (t);
2439 continue;
2442 /* This variable is going to need initialization and/or
2443 finalization, so we add it to the list. */
2444 *var = TREE_CHAIN (t);
2445 TREE_CHAIN (t) = result;
2446 result = t;
2449 return result;
2452 /* Make sure we have told the back end about all the variables in
2453 VARS. */
2455 static void
2456 write_out_vars (tree vars)
2458 tree v;
2460 for (v = vars; v; v = TREE_CHAIN (v))
2461 if (! TREE_ASM_WRITTEN (TREE_VALUE (v)))
2462 rest_of_decl_compilation (TREE_VALUE (v), 0, 1, 1);
2465 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
2466 (otherwise) that will initialize all gobal objects with static
2467 storage duration having the indicated PRIORITY. */
2469 static void
2470 generate_ctor_or_dtor_function (bool constructor_p, int priority,
2471 location_t *locus)
2473 char function_key;
2474 tree arguments;
2475 tree fndecl;
2476 tree body;
2477 size_t i;
2479 input_location = *locus;
2480 locus->line++;
2482 /* We use `I' to indicate initialization and `D' to indicate
2483 destruction. */
2484 function_key = constructor_p ? 'I' : 'D';
2486 /* We emit the function lazily, to avoid generating empty
2487 global constructors and destructors. */
2488 body = NULL_TREE;
2490 /* Call the static storage duration function with appropriate
2491 arguments. */
2492 if (ssdf_decls)
2493 for (i = 0; i < ssdf_decls->elements_used; ++i)
2495 fndecl = VARRAY_TREE (ssdf_decls, i);
2497 /* Calls to pure or const functions will expand to nothing. */
2498 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
2500 if (! body)
2501 body = start_objects (function_key, priority);
2503 arguments = tree_cons (NULL_TREE, build_int_2 (priority, 0),
2504 NULL_TREE);
2505 arguments = tree_cons (NULL_TREE, build_int_2 (constructor_p, 0),
2506 arguments);
2507 finish_expr_stmt (build_function_call (fndecl, arguments));
2511 /* If we're generating code for the DEFAULT_INIT_PRIORITY, throw in
2512 calls to any functions marked with attributes indicating that
2513 they should be called at initialization- or destruction-time. */
2514 if (priority == DEFAULT_INIT_PRIORITY)
2516 tree fns;
2518 for (fns = constructor_p ? static_ctors : static_dtors;
2519 fns;
2520 fns = TREE_CHAIN (fns))
2522 fndecl = TREE_VALUE (fns);
2524 /* Calls to pure/const functions will expand to nothing. */
2525 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
2527 if (! body)
2528 body = start_objects (function_key, priority);
2529 finish_expr_stmt (build_function_call (fndecl, NULL_TREE));
2534 /* Close out the function. */
2535 if (body)
2536 finish_objects (function_key, priority, body);
2539 /* Generate constructor and destructor functions for the priority
2540 indicated by N. */
2542 static int
2543 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
2545 location_t *locus = data;
2546 int priority = (int) n->key;
2547 priority_info pi = (priority_info) n->value;
2549 /* Generate the functions themselves, but only if they are really
2550 needed. */
2551 if (pi->initializations_p
2552 || (priority == DEFAULT_INIT_PRIORITY && static_ctors))
2553 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
2554 if (pi->destructions_p
2555 || (priority == DEFAULT_INIT_PRIORITY && static_dtors))
2556 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
2558 /* Keep iterating. */
2559 return 0;
2562 /* Callgraph code does not understand the member pointers. Mark the methods
2563 referenced as used. */
2564 static tree
2565 mark_member_pointers_and_eh_handlers (tree *tp,
2566 int *walk_subtrees,
2567 void *data ATTRIBUTE_UNUSED)
2569 /* Avoid useless walking of complex type and declaration nodes. */
2570 if (TYPE_P (*tp) || DECL_P (*tp))
2572 *walk_subtrees = 0;
2573 return 0;
2575 switch (TREE_CODE (*tp))
2577 case PTRMEM_CST:
2578 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (*tp)))
2579 cgraph_mark_needed_node (cgraph_node (PTRMEM_CST_MEMBER (*tp)), 1);
2580 break;
2582 /* EH handlers will emit EH tables referencing typeinfo. */
2583 case HANDLER:
2584 if (HANDLER_TYPE (*tp))
2586 tree tinfo = eh_type_info (HANDLER_TYPE (*tp));
2588 cgraph_varpool_mark_needed_node (cgraph_varpool_node (tinfo));
2590 break;
2592 case EH_SPEC_BLOCK:
2594 tree type;
2596 for (type = EH_SPEC_RAISES ((*tp)); type;
2597 type = TREE_CHAIN (type))
2599 tree tinfo = eh_type_info (TREE_VALUE (type));
2601 cgraph_varpool_mark_needed_node (cgraph_varpool_node (tinfo));
2604 break;
2605 default:
2606 break;
2608 return 0;
2611 /* Called via LANGHOOK_CALLGRAPH_LOWER_FUNCTION. It is supposed to lower
2612 frontend specific constructs that would otherwise confuse the middle end. */
2613 void
2614 lower_function (tree fn)
2616 walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
2617 mark_member_pointers_and_eh_handlers,
2618 NULL);
2621 /* This routine is called from the last rule in yyparse ().
2622 Its job is to create all the code needed to initialize and
2623 destroy the global aggregates. We do the destruction
2624 first, since that way we only need to reverse the decls once. */
2626 void
2627 finish_file ()
2629 tree vars;
2630 bool reconsider;
2631 size_t i;
2632 location_t locus;
2633 unsigned ssdf_count = 0;
2635 locus = input_location;
2636 at_eof = 1;
2638 /* Bad parse errors. Just forget about it. */
2639 if (! global_bindings_p () || current_class_type || decl_namespace_list)
2640 return;
2642 if (pch_file)
2643 c_common_write_pch ();
2645 /* Otherwise, GDB can get confused, because in only knows
2646 about source for LINENO-1 lines. */
2647 input_line -= 1;
2649 interface_unknown = 1;
2650 interface_only = 0;
2652 /* We now have to write out all the stuff we put off writing out.
2653 These include:
2655 o Template specializations that we have not yet instantiated,
2656 but which are needed.
2657 o Initialization and destruction for non-local objects with
2658 static storage duration. (Local objects with static storage
2659 duration are initialized when their scope is first entered,
2660 and are cleaned up via atexit.)
2661 o Virtual function tables.
2663 All of these may cause others to be needed. For example,
2664 instantiating one function may cause another to be needed, and
2665 generating the initializer for an object may cause templates to be
2666 instantiated, etc., etc. */
2668 timevar_push (TV_VARCONST);
2670 emit_support_tinfos ();
2674 tree t;
2675 size_t n_old, n_new;
2677 reconsider = false;
2679 /* If there are templates that we've put off instantiating, do
2680 them now. */
2681 instantiate_pending_templates ();
2683 /* Write out virtual tables as required. Note that writing out
2684 the virtual table for a template class may cause the
2685 instantiation of members of that class. If we write out
2686 vtables then we remove the class from our list so we don't
2687 have to look at it again. */
2689 while (keyed_classes != NULL_TREE
2690 && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
2692 reconsider = true;
2693 keyed_classes = TREE_CHAIN (keyed_classes);
2696 t = keyed_classes;
2697 if (t != NULL_TREE)
2699 tree next = TREE_CHAIN (t);
2701 while (next)
2703 if (maybe_emit_vtables (TREE_VALUE (next)))
2705 reconsider = true;
2706 TREE_CHAIN (t) = TREE_CHAIN (next);
2708 else
2709 t = next;
2711 next = TREE_CHAIN (t);
2715 /* Write out needed type info variables. We have to be careful
2716 looping through unemitted decls, because emit_tinfo_decl may
2717 cause other variables to be needed. We stick new elements
2718 (and old elements that we may need to reconsider) at the end
2719 of the array, then shift them back to the beginning once we're
2720 done. */
2722 n_old = VARRAY_ACTIVE_SIZE (unemitted_tinfo_decls);
2723 for (i = 0; i < n_old; ++i)
2725 tree tinfo_decl = VARRAY_TREE (unemitted_tinfo_decls, i);
2726 if (emit_tinfo_decl (tinfo_decl))
2727 reconsider = true;
2728 else
2729 VARRAY_PUSH_TREE (unemitted_tinfo_decls, tinfo_decl);
2732 /* The only elements we want to keep are the new ones. Copy
2733 them to the beginning of the array, then get rid of the
2734 leftovers. */
2735 n_new = VARRAY_ACTIVE_SIZE (unemitted_tinfo_decls) - n_old;
2736 memmove (&VARRAY_TREE (unemitted_tinfo_decls, 0),
2737 &VARRAY_TREE (unemitted_tinfo_decls, n_old),
2738 n_new * sizeof (tree));
2739 memset (&VARRAY_TREE (unemitted_tinfo_decls, n_new),
2741 n_old * sizeof (tree));
2742 VARRAY_ACTIVE_SIZE (unemitted_tinfo_decls) = n_new;
2744 /* The list of objects with static storage duration is built up
2745 in reverse order. We clear STATIC_AGGREGATES so that any new
2746 aggregates added during the initialization of these will be
2747 initialized in the correct order when we next come around the
2748 loop. */
2749 vars = prune_vars_needing_no_initialization (&static_aggregates);
2751 if (vars)
2753 tree v;
2755 /* We need to start a new initialization function each time
2756 through the loop. That's because we need to know which
2757 vtables have been referenced, and TREE_SYMBOL_REFERENCED
2758 isn't computed until a function is finished, and written
2759 out. That's a deficiency in the back-end. When this is
2760 fixed, these initialization functions could all become
2761 inline, with resulting performance improvements. */
2762 tree ssdf_body;
2764 /* Set the line and file, so that it is obviously not from
2765 the source file. */
2766 input_location = locus;
2767 ssdf_body = start_static_storage_duration_function (ssdf_count);
2769 /* Make sure the back end knows about all the variables. */
2770 write_out_vars (vars);
2772 /* First generate code to do all the initializations. */
2773 for (v = vars; v; v = TREE_CHAIN (v))
2774 do_static_initialization (TREE_VALUE (v),
2775 TREE_PURPOSE (v));
2777 /* Then, generate code to do all the destructions. Do these
2778 in reverse order so that the most recently constructed
2779 variable is the first destroyed. If we're using
2780 __cxa_atexit, then we don't need to do this; functions
2781 were registered at initialization time to destroy the
2782 local statics. */
2783 if (!flag_use_cxa_atexit)
2785 vars = nreverse (vars);
2786 for (v = vars; v; v = TREE_CHAIN (v))
2787 do_static_destruction (TREE_VALUE (v));
2789 else
2790 vars = NULL_TREE;
2792 /* Finish up the static storage duration function for this
2793 round. */
2794 input_location = locus;
2795 finish_static_storage_duration_function (ssdf_body);
2797 /* All those initializations and finalizations might cause
2798 us to need more inline functions, more template
2799 instantiations, etc. */
2800 reconsider = true;
2801 ssdf_count++;
2802 locus.line++;
2805 for (i = 0; i < deferred_fns_used; ++i)
2807 tree decl = VARRAY_TREE (deferred_fns, i);
2809 /* Does it need synthesizing? */
2810 if (DECL_ARTIFICIAL (decl) && ! DECL_INITIAL (decl)
2811 && TREE_USED (decl)
2812 && (! DECL_REALLY_EXTERN (decl) || DECL_INLINE (decl)))
2814 /* Even though we're already at the top-level, we push
2815 there again. That way, when we pop back a few lines
2816 hence, all of our state is restored. Otherwise,
2817 finish_function doesn't clean things up, and we end
2818 up with CURRENT_FUNCTION_DECL set. */
2819 push_to_top_level ();
2820 synthesize_method (decl);
2821 pop_from_top_level ();
2822 reconsider = true;
2825 /* If the function has no body, avoid calling
2826 import_export_decl. On a system without weak symbols,
2827 calling import_export_decl will make an inline template
2828 instantiation "static", which will result in errors about
2829 the use of undefined functions if there is no body for
2830 the function. */
2831 if (!DECL_SAVED_TREE (decl))
2832 continue;
2834 import_export_decl (decl);
2836 /* We lie to the back-end, pretending that some functions
2837 are not defined when they really are. This keeps these
2838 functions from being put out unnecessarily. But, we must
2839 stop lying when the functions are referenced, or if they
2840 are not comdat since they need to be put out now. This
2841 is done in a separate for cycle, because if some deferred
2842 function is contained in another deferred function later
2843 in deferred_fns varray, rest_of_compilation would skip
2844 this function and we really cannot expand the same
2845 function twice. */
2846 if (DECL_NOT_REALLY_EXTERN (decl)
2847 && DECL_INITIAL (decl)
2848 && DECL_NEEDED_P (decl))
2849 DECL_EXTERNAL (decl) = 0;
2851 /* If we're going to need to write this function out, and
2852 there's already a body for it, create RTL for it now.
2853 (There might be no body if this is a method we haven't
2854 gotten around to synthesizing yet.) */
2855 if (!DECL_EXTERNAL (decl)
2856 && DECL_NEEDED_P (decl)
2857 && DECL_SAVED_TREE (decl)
2858 && !TREE_ASM_WRITTEN (decl)
2859 && (!flag_unit_at_a_time
2860 || !cgraph_node (decl)->local.finalized))
2862 /* We will output the function; no longer consider it in this
2863 loop. */
2864 DECL_DEFER_OUTPUT (decl) = 0;
2865 /* Generate RTL for this function now that we know we
2866 need it. */
2867 expand_or_defer_fn (decl);
2868 /* If we're compiling -fsyntax-only pretend that this
2869 function has been written out so that we don't try to
2870 expand it again. */
2871 if (flag_syntax_only)
2872 TREE_ASM_WRITTEN (decl) = 1;
2873 reconsider = true;
2877 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
2878 reconsider = true;
2880 /* Static data members are just like namespace-scope globals. */
2881 for (i = 0; i < pending_statics_used; ++i)
2883 tree decl = VARRAY_TREE (pending_statics, i);
2884 if (TREE_ASM_WRITTEN (decl))
2885 continue;
2886 import_export_decl (decl);
2887 if (DECL_NOT_REALLY_EXTERN (decl) && ! DECL_IN_AGGR_P (decl))
2888 DECL_EXTERNAL (decl) = 0;
2890 if (pending_statics
2891 && wrapup_global_declarations (&VARRAY_TREE (pending_statics, 0),
2892 pending_statics_used))
2893 reconsider = true;
2895 while (reconsider);
2897 /* All used inline functions must have a definition at this point. */
2898 for (i = 0; i < deferred_fns_used; ++i)
2900 tree decl = VARRAY_TREE (deferred_fns, i);
2902 if (TREE_USED (decl) && DECL_DECLARED_INLINE_P (decl)
2903 && !(TREE_ASM_WRITTEN (decl) || DECL_SAVED_TREE (decl)
2904 /* An explicit instantiation can be used to specify
2905 that the body is in another unit. It will have
2906 already verified there was a definition. */
2907 || DECL_EXPLICIT_INSTANTIATION (decl)))
2909 cp_warning_at ("inline function `%D' used but never defined", decl);
2910 /* This symbol is effectively an "extern" declaration now.
2911 This is not strictly necessary, but removes a duplicate
2912 warning. */
2913 TREE_PUBLIC (decl) = 1;
2918 /* We give C linkage to static constructors and destructors. */
2919 push_lang_context (lang_name_c);
2921 /* Generate initialization and destruction functions for all
2922 priorities for which they are required. */
2923 if (priority_info_map)
2924 splay_tree_foreach (priority_info_map,
2925 generate_ctor_and_dtor_functions_for_priority,
2926 /*data=*/&locus);
2927 else
2930 if (static_ctors)
2931 generate_ctor_or_dtor_function (/*constructor_p=*/true,
2932 DEFAULT_INIT_PRIORITY, &locus);
2933 if (static_dtors)
2934 generate_ctor_or_dtor_function (/*constructor_p=*/false,
2935 DEFAULT_INIT_PRIORITY, &locus);
2938 /* We're done with the splay-tree now. */
2939 if (priority_info_map)
2940 splay_tree_delete (priority_info_map);
2942 /* We're done with static constructors, so we can go back to "C++"
2943 linkage now. */
2944 pop_lang_context ();
2946 if (flag_unit_at_a_time)
2948 cgraph_finalize_compilation_unit ();
2949 cgraph_optimize ();
2952 /* Now, issue warnings about static, but not defined, functions,
2953 etc., and emit debugging information. */
2954 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
2955 if (pending_statics)
2956 check_global_declarations (&VARRAY_TREE (pending_statics, 0),
2957 pending_statics_used);
2959 finish_repo ();
2961 /* The entire file is now complete. If requested, dump everything
2962 to a file. */
2964 int flags;
2965 FILE *stream = dump_begin (TDI_all, &flags);
2967 if (stream)
2969 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
2970 dump_end (TDI_all, stream);
2974 timevar_pop (TV_VARCONST);
2976 if (flag_detailed_statistics)
2978 dump_tree_statistics ();
2979 dump_time_statistics ();
2981 input_location = locus;
2984 /* FN is an OFFSET_REF indicating the function to call in parse-tree
2985 form; it has not yet been semantically analyzed. ARGS are the
2986 arguments to the function. They have already been semantically
2987 analzyed. */
2989 tree
2990 build_offset_ref_call_from_tree (tree fn, tree args)
2992 tree object_addr;
2993 tree orig_fn;
2994 tree orig_args;
2995 tree expr;
2997 orig_fn = fn;
2998 orig_args = args;
3000 if (processing_template_decl)
3002 tree object;
3003 tree object_type;
3005 my_friendly_assert (TREE_CODE (fn) == DOTSTAR_EXPR
3006 || TREE_CODE (fn) == MEMBER_REF,
3007 20030708);
3008 if (type_dependent_expression_p (fn)
3009 || any_type_dependent_arguments_p (args))
3010 return build_min_nt (CALL_EXPR, fn, args);
3012 /* Transform the arguments and add the implicit "this"
3013 parameter. That must be done before the FN is transformed
3014 because we depend on the form of FN. */
3015 args = build_non_dependent_args (args);
3016 object_type = TREE_TYPE (TREE_OPERAND (fn, 0));
3017 if (TREE_CODE (fn) == DOTSTAR_EXPR)
3018 object_type = build_pointer_type (non_reference (object_type));
3019 object = build (NON_DEPENDENT_EXPR, object_type);
3020 args = tree_cons (NULL_TREE, object, args);
3021 /* Now that the arguments are done, transform FN. */
3022 fn = build_non_dependent_expr (fn);
3025 /* A qualified name corresponding to a bound pointer-to-member is
3026 represented as an OFFSET_REF:
3028 struct B { void g(); };
3029 void (B::*p)();
3030 void B::g() { (this->*p)(); } */
3031 if (TREE_CODE (fn) == OFFSET_REF)
3033 object_addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (fn, 0), 0);
3034 fn = TREE_OPERAND (fn, 1);
3035 fn = get_member_function_from_ptrfunc (&object_addr, fn);
3036 args = tree_cons (NULL_TREE, object_addr, args);
3039 expr = build_function_call (fn, args);
3040 if (processing_template_decl && expr != error_mark_node)
3041 return build_min (CALL_EXPR, TREE_TYPE (expr), orig_fn, orig_args);
3042 return expr;
3045 /* Returns true if ROOT (a namespace, class, or function) encloses
3046 CHILD. CHILD may be either a class type or a namespace. */
3048 bool
3049 is_ancestor (tree root, tree child)
3051 my_friendly_assert ((TREE_CODE (root) == NAMESPACE_DECL
3052 || TREE_CODE (root) == FUNCTION_DECL
3053 || CLASS_TYPE_P (root)), 20030307);
3054 my_friendly_assert ((TREE_CODE (child) == NAMESPACE_DECL
3055 || CLASS_TYPE_P (child)),
3056 20030307);
3058 /* The global namespace encloses everything. */
3059 if (root == global_namespace)
3060 return true;
3062 while (true)
3064 /* If we've run out of scopes, stop. */
3065 if (!child)
3066 return false;
3067 /* If we've reached the ROOT, it encloses CHILD. */
3068 if (root == child)
3069 return true;
3070 /* Go out one level. */
3071 if (TYPE_P (child))
3072 child = TYPE_NAME (child);
3073 child = DECL_CONTEXT (child);
3078 /* Return the namespace that is the common ancestor
3079 of two given namespaces. */
3081 tree
3082 namespace_ancestor (tree ns1, tree ns2)
3084 timevar_push (TV_NAME_LOOKUP);
3085 if (is_ancestor (ns1, ns2))
3086 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ns1);
3087 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
3088 namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2));
3091 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
3092 directive is not directly from the source. Also find the common
3093 ancestor and let our users know about the new namespace */
3094 static void
3095 add_using_namespace (tree user, tree used, bool indirect)
3097 tree t;
3098 timevar_push (TV_NAME_LOOKUP);
3099 /* Using oneself is a no-op. */
3100 if (user == used)
3102 timevar_pop (TV_NAME_LOOKUP);
3103 return;
3105 my_friendly_assert (TREE_CODE (user) == NAMESPACE_DECL, 380);
3106 my_friendly_assert (TREE_CODE (used) == NAMESPACE_DECL, 380);
3107 /* Check if we already have this. */
3108 t = purpose_member (used, DECL_NAMESPACE_USING (user));
3109 if (t != NULL_TREE)
3111 if (!indirect)
3112 /* Promote to direct usage. */
3113 TREE_INDIRECT_USING (t) = 0;
3114 timevar_pop (TV_NAME_LOOKUP);
3115 return;
3118 /* Add used to the user's using list. */
3119 DECL_NAMESPACE_USING (user)
3120 = tree_cons (used, namespace_ancestor (user, used),
3121 DECL_NAMESPACE_USING (user));
3123 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
3125 /* Add user to the used's users list. */
3126 DECL_NAMESPACE_USERS (used)
3127 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
3129 /* Recursively add all namespaces used. */
3130 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
3131 /* indirect usage */
3132 add_using_namespace (user, TREE_PURPOSE (t), 1);
3134 /* Tell everyone using us about the new used namespaces. */
3135 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
3136 add_using_namespace (TREE_PURPOSE (t), used, 1);
3137 timevar_pop (TV_NAME_LOOKUP);
3140 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
3141 duplicates. The first list becomes the tail of the result.
3143 The algorithm is O(n^2). We could get this down to O(n log n) by
3144 doing a sort on the addresses of the functions, if that becomes
3145 necessary. */
3147 static tree
3148 merge_functions (tree s1, tree s2)
3150 for (; s2; s2 = OVL_NEXT (s2))
3152 tree fn2 = OVL_CURRENT (s2);
3153 tree fns1;
3155 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
3157 tree fn1 = OVL_CURRENT (fns1);
3159 /* If the function from S2 is already in S1, there is no
3160 need to add it again. For `extern "C"' functions, we
3161 might have two FUNCTION_DECLs for the same function, in
3162 different namespaces; again, we only need one of them. */
3163 if (fn1 == fn2
3164 || (DECL_EXTERN_C_P (fn1) && DECL_EXTERN_C_P (fn2)
3165 && DECL_NAME (fn1) == DECL_NAME (fn2)))
3166 break;
3169 /* If we exhausted all of the functions in S1, FN2 is new. */
3170 if (!fns1)
3171 s1 = build_overload (fn2, s1);
3173 return s1;
3176 /* This should return an error not all definitions define functions.
3177 It is not an error if we find two functions with exactly the
3178 same signature, only if these are selected in overload resolution.
3179 old is the current set of bindings, new the freshly-found binding.
3180 XXX Do we want to give *all* candidates in case of ambiguity?
3181 XXX In what way should I treat extern declarations?
3182 XXX I don't want to repeat the entire duplicate_decls here */
3184 static cxx_binding *
3185 ambiguous_decl (tree name, cxx_binding *old, cxx_binding *new, int flags)
3187 tree val, type;
3188 my_friendly_assert (old != NULL, 393);
3189 /* Copy the value. */
3190 val = BINDING_VALUE (new);
3191 if (val)
3192 switch (TREE_CODE (val))
3194 case TEMPLATE_DECL:
3195 /* If we expect types or namespaces, and not templates,
3196 or this is not a template class. */
3197 if (LOOKUP_QUALIFIERS_ONLY (flags)
3198 && !DECL_CLASS_TEMPLATE_P (val))
3199 val = NULL_TREE;
3200 break;
3201 case TYPE_DECL:
3202 if (LOOKUP_NAMESPACES_ONLY (flags))
3203 val = NULL_TREE;
3204 break;
3205 case NAMESPACE_DECL:
3206 if (LOOKUP_TYPES_ONLY (flags))
3207 val = NULL_TREE;
3208 break;
3209 case FUNCTION_DECL:
3210 /* Ignore built-in functions that are still anticipated. */
3211 if (LOOKUP_QUALIFIERS_ONLY (flags) || DECL_ANTICIPATED (val))
3212 val = NULL_TREE;
3213 break;
3214 default:
3215 if (LOOKUP_QUALIFIERS_ONLY (flags))
3216 val = NULL_TREE;
3219 if (!BINDING_VALUE (old))
3220 BINDING_VALUE (old) = val;
3221 else if (val && val != BINDING_VALUE (old))
3223 if (is_overloaded_fn (BINDING_VALUE (old)) && is_overloaded_fn (val))
3224 BINDING_VALUE (old) = merge_functions (BINDING_VALUE (old), val);
3225 else
3227 /* Some declarations are functions, some are not. */
3228 if (flags & LOOKUP_COMPLAIN)
3230 /* If we've already given this error for this lookup,
3231 BINDING_VALUE (old) is error_mark_node, so let's not
3232 repeat ourselves. */
3233 if (BINDING_VALUE (old) != error_mark_node)
3235 error ("use of `%D' is ambiguous", name);
3236 cp_error_at (" first declared as `%#D' here",
3237 BINDING_VALUE (old));
3239 cp_error_at (" also declared as `%#D' here", val);
3241 BINDING_VALUE (old) = error_mark_node;
3244 /* ... and copy the type. */
3245 type = BINDING_TYPE (new);
3246 if (LOOKUP_NAMESPACES_ONLY (flags))
3247 type = NULL_TREE;
3248 if (!BINDING_TYPE (old))
3249 BINDING_TYPE (old) = type;
3250 else if (type && BINDING_TYPE (old) != type)
3252 if (flags & LOOKUP_COMPLAIN)
3254 error ("`%D' denotes an ambiguous type",name);
3255 error ("%H first type here",
3256 &DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (BINDING_TYPE (old))));
3257 error ("%H other type here",
3258 &DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)));
3261 return old;
3264 /* Subroutine of unualified_namespace_lookup:
3265 Add the bindings of NAME in used namespaces to VAL.
3266 We are currently looking for names in namespace SCOPE, so we
3267 look through USINGS for using-directives of namespaces
3268 which have SCOPE as a common ancestor with the current scope.
3269 Returns false on errors. */
3271 bool
3272 lookup_using_namespace (tree name, cxx_binding *val, tree usings, tree scope,
3273 int flags, tree *spacesp)
3275 tree iter;
3276 timevar_push (TV_NAME_LOOKUP);
3277 /* Iterate over all used namespaces in current, searching for using
3278 directives of scope. */
3279 for (iter = usings; iter; iter = TREE_CHAIN (iter))
3280 if (TREE_VALUE (iter) == scope)
3282 tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
3283 cxx_binding *val1 =
3284 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (used), name);
3285 if (spacesp)
3286 *spacesp = tree_cons (used, NULL_TREE, *spacesp);
3287 /* Resolve ambiguities. */
3288 if (val1)
3289 val = ambiguous_decl (name, val, val1, flags);
3291 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
3292 BINDING_VALUE (val) != error_mark_node);
3295 /* [namespace.qual]
3296 Accepts the NAME to lookup and its qualifying SCOPE.
3297 Returns the name/type pair found into the cxx_binding *RESULT,
3298 or false on error. */
3300 bool
3301 qualified_lookup_using_namespace (tree name, tree scope, cxx_binding *result,
3302 int flags)
3304 /* Maintain a list of namespaces visited... */
3305 tree seen = NULL_TREE;
3306 /* ... and a list of namespace yet to see. */
3307 tree todo = NULL_TREE;
3308 tree usings;
3309 timevar_push (TV_NAME_LOOKUP);
3310 /* Look through namespace aliases. */
3311 scope = ORIGINAL_NAMESPACE (scope);
3312 while (scope && result->value != error_mark_node)
3314 cxx_binding *binding =
3315 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3316 seen = tree_cons (scope, NULL_TREE, seen);
3317 if (binding)
3318 result = ambiguous_decl (name, result, binding, flags);
3319 if (!BINDING_VALUE (result) && !BINDING_TYPE (result))
3320 /* Consider using directives. */
3321 for (usings = DECL_NAMESPACE_USING (scope); usings;
3322 usings = TREE_CHAIN (usings))
3323 /* If this was a real directive, and we have not seen it. */
3324 if (!TREE_INDIRECT_USING (usings)
3325 && !purpose_member (TREE_PURPOSE (usings), seen))
3326 todo = tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo);
3327 if (todo)
3329 scope = TREE_PURPOSE (todo);
3330 todo = TREE_CHAIN (todo);
3332 else
3333 scope = NULL_TREE; /* If there never was a todo list. */
3335 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result->value != error_mark_node);
3338 /* [namespace.memdef]/2 */
3340 /* Set the context of a declaration to scope. Complain if we are not
3341 outside scope. */
3343 void
3344 set_decl_namespace (tree decl, tree scope, bool friendp)
3346 tree old;
3348 /* Get rid of namespace aliases. */
3349 scope = ORIGINAL_NAMESPACE (scope);
3351 /* It is ok for friends to be qualified in parallel space. */
3352 if (!friendp && !is_ancestor (current_namespace, scope))
3353 error ("declaration of `%D' not in a namespace surrounding `%D'",
3354 decl, scope);
3355 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3356 if (scope != current_namespace)
3358 /* See whether this has been declared in the namespace. */
3359 old = namespace_binding (DECL_NAME (decl), scope);
3360 if (!old)
3361 /* No old declaration at all. */
3362 goto complain;
3363 /* A template can be explicitly specialized in any namespace. */
3364 if (processing_explicit_instantiation)
3365 return;
3366 if (!is_overloaded_fn (decl))
3367 /* Don't compare non-function decls with decls_match here,
3368 since it can't check for the correct constness at this
3369 point. pushdecl will find those errors later. */
3370 return;
3371 /* Since decl is a function, old should contain a function decl. */
3372 if (!is_overloaded_fn (old))
3373 goto complain;
3374 if (processing_template_decl || processing_specialization)
3375 /* We have not yet called push_template_decl to turn a
3376 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations
3377 won't match. But, we'll check later, when we construct the
3378 template. */
3379 return;
3380 if (is_overloaded_fn (old))
3382 for (; old; old = OVL_NEXT (old))
3383 if (decls_match (decl, OVL_CURRENT (old)))
3384 return;
3386 else
3387 if (decls_match (decl, old))
3388 return;
3390 else
3391 return;
3392 complain:
3393 error ("`%D' should have been declared inside `%D'",
3394 decl, scope);
3397 /* Compute the namespace where a declaration is defined. */
3399 static tree
3400 decl_namespace (tree decl)
3402 timevar_push (TV_NAME_LOOKUP);
3403 if (TYPE_P (decl))
3404 decl = TYPE_STUB_DECL (decl);
3405 while (DECL_CONTEXT (decl))
3407 decl = DECL_CONTEXT (decl);
3408 if (TREE_CODE (decl) == NAMESPACE_DECL)
3409 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
3410 if (TYPE_P (decl))
3411 decl = TYPE_STUB_DECL (decl);
3412 my_friendly_assert (DECL_P (decl), 390);
3415 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, global_namespace);
3418 /* Return the namespace where the current declaration is declared. */
3420 tree
3421 current_decl_namespace (void)
3423 tree result;
3424 /* If we have been pushed into a different namespace, use it. */
3425 if (decl_namespace_list)
3426 return TREE_PURPOSE (decl_namespace_list);
3428 if (current_class_type)
3429 result = decl_namespace (TYPE_STUB_DECL (current_class_type));
3430 else if (current_function_decl)
3431 result = decl_namespace (current_function_decl);
3432 else
3433 result = current_namespace;
3434 return result;
3437 /* Temporarily set the namespace for the current declaration. */
3439 void
3440 push_decl_namespace (tree decl)
3442 if (TREE_CODE (decl) != NAMESPACE_DECL)
3443 decl = decl_namespace (decl);
3444 decl_namespace_list = tree_cons (ORIGINAL_NAMESPACE (decl),
3445 NULL_TREE, decl_namespace_list);
3448 void
3449 pop_decl_namespace (void)
3451 decl_namespace_list = TREE_CHAIN (decl_namespace_list);
3454 /* Enter a class or namespace scope. */
3456 void
3457 push_scope (tree t)
3459 if (TREE_CODE (t) == NAMESPACE_DECL)
3460 push_decl_namespace (t);
3461 else if CLASS_TYPE_P (t)
3462 push_nested_class (t);
3465 /* Leave scope pushed by push_scope. */
3467 void
3468 pop_scope (tree t)
3470 if (TREE_CODE (t) == NAMESPACE_DECL)
3471 pop_decl_namespace ();
3472 else if CLASS_TYPE_P (t)
3473 pop_nested_class ();
3476 /* [basic.lookup.koenig] */
3477 /* A nonzero return value in the functions below indicates an error. */
3479 struct arg_lookup
3481 tree name;
3482 tree namespaces;
3483 tree classes;
3484 tree functions;
3487 static bool arg_assoc (struct arg_lookup*, tree);
3488 static bool arg_assoc_args (struct arg_lookup*, tree);
3489 static bool arg_assoc_type (struct arg_lookup*, tree);
3490 static bool add_function (struct arg_lookup *, tree);
3491 static bool arg_assoc_namespace (struct arg_lookup *, tree);
3492 static bool arg_assoc_class (struct arg_lookup *, tree);
3493 static bool arg_assoc_template_arg (struct arg_lookup*, tree);
3495 /* Add a function to the lookup structure.
3496 Returns true on error. */
3498 static bool
3499 add_function (struct arg_lookup *k, tree fn)
3501 /* We used to check here to see if the function was already in the list,
3502 but that's O(n^2), which is just too expensive for function lookup.
3503 Now we deal with the occasional duplicate in joust. In doing this, we
3504 assume that the number of duplicates will be small compared to the
3505 total number of functions being compared, which should usually be the
3506 case. */
3508 /* We must find only functions, or exactly one non-function. */
3509 if (!k->functions)
3510 k->functions = fn;
3511 else if (is_overloaded_fn (k->functions) && is_overloaded_fn (fn))
3512 k->functions = build_overload (fn, k->functions);
3513 else
3515 tree f1 = OVL_CURRENT (k->functions);
3516 tree f2 = fn;
3517 if (is_overloaded_fn (f1))
3519 fn = f1; f1 = f2; f2 = fn;
3521 cp_error_at ("`%D' is not a function,", f1);
3522 cp_error_at (" conflict with `%D'", f2);
3523 error (" in call to `%D'", k->name);
3524 return true;
3527 return false;
3530 /* Add functions of a namespace to the lookup structure.
3531 Returns true on error. */
3533 static bool
3534 arg_assoc_namespace (struct arg_lookup *k, tree scope)
3536 tree value;
3538 if (purpose_member (scope, k->namespaces))
3539 return 0;
3540 k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces);
3542 value = namespace_binding (k->name, scope);
3543 if (!value)
3544 return false;
3546 for (; value; value = OVL_NEXT (value))
3547 if (add_function (k, OVL_CURRENT (value)))
3548 return true;
3550 return false;
3553 /* Adds everything associated with a template argument to the lookup
3554 structure. Returns true on error. */
3556 static bool
3557 arg_assoc_template_arg (struct arg_lookup *k, tree arg)
3559 /* [basic.lookup.koenig]
3561 If T is a template-id, its associated namespaces and classes are
3562 ... the namespaces and classes associated with the types of the
3563 template arguments provided for template type parameters
3564 (excluding template template parameters); the namespaces in which
3565 any template template arguments are defined; and the classes in
3566 which any member templates used as template template arguments
3567 are defined. [Note: non-type template arguments do not
3568 contribute to the set of associated namespaces. ] */
3570 /* Consider first template template arguments. */
3571 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3572 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
3573 return false;
3574 else if (TREE_CODE (arg) == TEMPLATE_DECL)
3576 tree ctx = CP_DECL_CONTEXT (arg);
3578 /* It's not a member template. */
3579 if (TREE_CODE (ctx) == NAMESPACE_DECL)
3580 return arg_assoc_namespace (k, ctx);
3581 /* Otherwise, it must be member template. */
3582 else
3583 return arg_assoc_class (k, ctx);
3585 /* It's not a template template argument, but it is a type template
3586 argument. */
3587 else if (TYPE_P (arg))
3588 return arg_assoc_type (k, arg);
3589 /* It's a non-type template argument. */
3590 else
3591 return false;
3594 /* Adds everything associated with class to the lookup structure.
3595 Returns true on error. */
3597 static bool
3598 arg_assoc_class (struct arg_lookup *k, tree type)
3600 tree list, friends, context;
3601 int i;
3603 /* Backend build structures, such as __builtin_va_list, aren't
3604 affected by all this. */
3605 if (!CLASS_TYPE_P (type))
3606 return false;
3608 if (purpose_member (type, k->classes))
3609 return false;
3610 k->classes = tree_cons (type, NULL_TREE, k->classes);
3612 context = decl_namespace (TYPE_MAIN_DECL (type));
3613 if (arg_assoc_namespace (k, context))
3614 return true;
3616 /* Process baseclasses. */
3617 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); i++)
3618 if (arg_assoc_class (k, TYPE_BINFO_BASETYPE (type, i)))
3619 return true;
3621 /* Process friends. */
3622 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
3623 list = TREE_CHAIN (list))
3624 if (k->name == FRIEND_NAME (list))
3625 for (friends = FRIEND_DECLS (list); friends;
3626 friends = TREE_CHAIN (friends))
3627 /* Only interested in global functions with potentially hidden
3628 (i.e. unqualified) declarations. */
3629 if (CP_DECL_CONTEXT (TREE_VALUE (friends)) == context)
3630 if (add_function (k, TREE_VALUE (friends)))
3631 return true;
3633 /* Process template arguments. */
3634 if (CLASSTYPE_TEMPLATE_INFO (type))
3636 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
3637 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
3638 arg_assoc_template_arg (k, TREE_VEC_ELT (list, i));
3641 return false;
3644 /* Adds everything associated with a given type.
3645 Returns 1 on error. */
3647 static bool
3648 arg_assoc_type (struct arg_lookup *k, tree type)
3650 /* As we do not get the type of non-type dependent expressions
3651 right, we can end up with such things without a type. */
3652 if (!type)
3653 return false;
3655 if (TYPE_PTRMEM_P (type))
3657 /* Pointer to member: associate class type and value type. */
3658 if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type)))
3659 return true;
3660 return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type));
3662 else switch (TREE_CODE (type))
3664 case ERROR_MARK:
3665 return false;
3666 case VOID_TYPE:
3667 case INTEGER_TYPE:
3668 case REAL_TYPE:
3669 case COMPLEX_TYPE:
3670 case VECTOR_TYPE:
3671 case CHAR_TYPE:
3672 case BOOLEAN_TYPE:
3673 return false;
3674 case RECORD_TYPE:
3675 if (TYPE_PTRMEMFUNC_P (type))
3676 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
3677 return arg_assoc_class (k, type);
3678 case POINTER_TYPE:
3679 case REFERENCE_TYPE:
3680 case ARRAY_TYPE:
3681 return arg_assoc_type (k, TREE_TYPE (type));
3682 case UNION_TYPE:
3683 case ENUMERAL_TYPE:
3684 return arg_assoc_namespace (k, decl_namespace (TYPE_MAIN_DECL (type)));
3685 case METHOD_TYPE:
3686 /* The basetype is referenced in the first arg type, so just
3687 fall through. */
3688 case FUNCTION_TYPE:
3689 /* Associate the parameter types. */
3690 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
3691 return true;
3692 /* Associate the return type. */
3693 return arg_assoc_type (k, TREE_TYPE (type));
3694 case TEMPLATE_TYPE_PARM:
3695 case BOUND_TEMPLATE_TEMPLATE_PARM:
3696 return false;
3697 case TYPENAME_TYPE:
3698 return false;
3699 case LANG_TYPE:
3700 if (type == unknown_type_node)
3701 return false;
3702 /* else fall through */
3703 default:
3704 abort ();
3706 return false;
3709 /* Adds everything associated with arguments. Returns true on error. */
3711 static bool
3712 arg_assoc_args (struct arg_lookup *k, tree args)
3714 for (; args; args = TREE_CHAIN (args))
3715 if (arg_assoc (k, TREE_VALUE (args)))
3716 return true;
3717 return false;
3720 /* Adds everything associated with a given tree_node. Returns 1 on error. */
3722 static bool
3723 arg_assoc (struct arg_lookup *k, tree n)
3725 if (n == error_mark_node)
3726 return false;
3728 if (TYPE_P (n))
3729 return arg_assoc_type (k, n);
3731 if (! type_unknown_p (n))
3732 return arg_assoc_type (k, TREE_TYPE (n));
3734 if (TREE_CODE (n) == ADDR_EXPR)
3735 n = TREE_OPERAND (n, 0);
3736 if (TREE_CODE (n) == COMPONENT_REF)
3737 n = TREE_OPERAND (n, 1);
3738 if (TREE_CODE (n) == OFFSET_REF)
3739 n = TREE_OPERAND (n, 1);
3740 while (TREE_CODE (n) == TREE_LIST)
3741 n = TREE_VALUE (n);
3742 if (TREE_CODE (n) == BASELINK)
3743 n = BASELINK_FUNCTIONS (n);
3745 if (TREE_CODE (n) == FUNCTION_DECL)
3746 return arg_assoc_type (k, TREE_TYPE (n));
3747 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
3749 /* [basic.lookup.koenig]
3751 If T is a template-id, its associated namespaces and classes
3752 are the namespace in which the template is defined; for
3753 member templates, the member template's class... */
3754 tree template = TREE_OPERAND (n, 0);
3755 tree args = TREE_OPERAND (n, 1);
3756 tree ctx;
3757 int ix;
3759 if (TREE_CODE (template) == COMPONENT_REF)
3760 template = TREE_OPERAND (template, 1);
3762 /* First, the template. There may actually be more than one if
3763 this is an overloaded function template. But, in that case,
3764 we only need the first; all the functions will be in the same
3765 namespace. */
3766 template = OVL_CURRENT (template);
3768 ctx = CP_DECL_CONTEXT (template);
3770 if (TREE_CODE (ctx) == NAMESPACE_DECL)
3772 if (arg_assoc_namespace (k, ctx) == 1)
3773 return true;
3775 /* It must be a member template. */
3776 else if (arg_assoc_class (k, ctx) == 1)
3777 return true;
3779 /* Now the arguments. */
3780 for (ix = TREE_VEC_LENGTH (args); ix--;)
3781 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1)
3782 return true;
3784 else
3786 my_friendly_assert (TREE_CODE (n) == OVERLOAD, 980715);
3788 for (; n; n = OVL_CHAIN (n))
3789 if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
3790 return true;
3793 return false;
3796 /* Performs Koenig lookup depending on arguments, where fns
3797 are the functions found in normal lookup. */
3799 tree
3800 lookup_arg_dependent (tree name, tree fns, tree args)
3802 struct arg_lookup k;
3803 tree fn = NULL_TREE;
3805 timevar_push (TV_NAME_LOOKUP);
3806 k.name = name;
3807 k.functions = fns;
3808 k.classes = NULL_TREE;
3810 /* Note that we've already looked at some namespaces during normal
3811 unqualified lookup, unless we found a decl in function scope. */
3812 if (fns)
3813 fn = OVL_CURRENT (fns);
3814 if (fn && TREE_CODE (fn) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (fn))
3815 k.namespaces = NULL_TREE;
3816 else
3817 unqualified_namespace_lookup (name, 0, &k.namespaces);
3819 arg_assoc_args (&k, args);
3820 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, k.functions);
3823 /* Process a namespace-alias declaration. */
3825 void
3826 do_namespace_alias (tree alias, tree namespace)
3828 if (TREE_CODE (namespace) != NAMESPACE_DECL)
3830 /* The parser did not find it, so it's not there. */
3831 error ("unknown namespace `%D'", namespace);
3832 return;
3835 namespace = ORIGINAL_NAMESPACE (namespace);
3837 /* Build the alias. */
3838 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
3839 DECL_NAMESPACE_ALIAS (alias) = namespace;
3840 DECL_EXTERNAL (alias) = 1;
3841 pushdecl (alias);
3844 /* Check a non-member using-declaration. Return the name and scope
3845 being used, and the USING_DECL, or NULL_TREE on failure. */
3847 static tree
3848 validate_nonmember_using_decl (tree decl, tree *scope, tree *name)
3850 *scope = global_namespace;
3851 *name = NULL_TREE;
3853 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
3855 *name = TREE_OPERAND (decl, 0);
3856 /* 7.3.3/5
3857 A using-declaration shall not name a template-id. */
3858 error ("a using-declaration cannot specify a template-id. Try `using %D'", *name);
3859 return NULL_TREE;
3862 if (TREE_CODE (decl) == NAMESPACE_DECL)
3864 error ("namespace `%D' not allowed in using-declaration", decl);
3865 return NULL_TREE;
3868 if (TREE_CODE (decl) == SCOPE_REF)
3870 /* It's a nested name with template parameter dependent scope.
3871 This can only be using-declaration for class member. */
3872 error ("`%T' is not a namespace", TREE_OPERAND (decl, 0));
3873 return NULL_TREE;
3876 if (is_overloaded_fn (decl))
3877 decl = get_first_fn (decl);
3879 my_friendly_assert (DECL_P (decl), 20020908);
3881 if (TREE_CODE (decl) == CONST_DECL)
3882 /* Enumeration constants to not have DECL_CONTEXT set. */
3883 *scope = TYPE_CONTEXT (TREE_TYPE (decl));
3884 else
3885 *scope = DECL_CONTEXT (decl);
3886 if (!*scope)
3887 *scope = global_namespace;
3889 /* [namespace.udecl]
3890 A using-declaration for a class member shall be a
3891 member-declaration. */
3892 if (TYPE_P (*scope))
3894 error ("`%T' is not a namespace", *scope);
3895 return NULL_TREE;
3897 *name = DECL_NAME (decl);
3898 /* Make a USING_DECL. */
3899 return push_using_decl (*scope, *name);
3902 /* Process local and global using-declarations. */
3904 static void
3905 do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
3906 tree *newval, tree *newtype)
3908 cxx_binding decls;
3910 *newval = *newtype = NULL_TREE;
3911 cxx_binding_clear (&decls);
3912 if (!qualified_lookup_using_namespace (name, scope, &decls, 0))
3913 /* Lookup error */
3914 return;
3916 if (!decls.value && !decls.type)
3918 error ("`%D' not declared", name);
3919 return;
3922 /* Check for using functions. */
3923 if (decls.value && is_overloaded_fn (decls.value))
3925 tree tmp, tmp1;
3927 if (oldval && !is_overloaded_fn (oldval))
3929 if (!DECL_IMPLICIT_TYPEDEF_P (oldval))
3930 error ("`%D' is already declared in this scope", name);
3931 oldval = NULL_TREE;
3934 *newval = oldval;
3935 for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
3937 tree new_fn = OVL_CURRENT (tmp);
3939 /* [namespace.udecl]
3941 If a function declaration in namespace scope or block
3942 scope has the same name and the same parameter types as a
3943 function introduced by a using declaration the program is
3944 ill-formed. */
3945 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
3947 tree old_fn = OVL_CURRENT (tmp1);
3949 if (new_fn == old_fn)
3950 /* The function already exists in the current namespace. */
3951 break;
3952 else if (OVL_USED (tmp1))
3953 continue; /* this is a using decl */
3954 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
3955 TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
3957 /* There was already a non-using declaration in
3958 this scope with the same parameter types. If both
3959 are the same extern "C" functions, that's ok. */
3960 if (decls_match (new_fn, old_fn))
3962 /* If the OLD_FN was a builtin, there is now a
3963 real declaration. */
3964 if (DECL_ANTICIPATED (old_fn))
3965 DECL_ANTICIPATED (old_fn) = 0;
3966 break;
3968 else if (!DECL_ANTICIPATED (old_fn))
3970 /* If the OLD_FN was really declared, the
3971 declarations don't match. */
3972 error ("`%D' is already declared in this scope", name);
3973 break;
3976 /* If the OLD_FN was not really there, just ignore
3977 it and keep going. */
3981 /* If we broke out of the loop, there's no reason to add
3982 this function to the using declarations for this
3983 scope. */
3984 if (tmp1)
3985 continue;
3987 *newval = build_overload (OVL_CURRENT (tmp), *newval);
3988 if (TREE_CODE (*newval) != OVERLOAD)
3989 *newval = ovl_cons (*newval, NULL_TREE);
3990 OVL_USED (*newval) = 1;
3993 else
3995 *newval = decls.value;
3996 if (oldval && !decls_match (*newval, oldval))
3997 error ("`%D' is already declared in this scope", name);
4000 *newtype = decls.type;
4001 if (oldtype && *newtype && !same_type_p (oldtype, *newtype))
4003 error ("using declaration `%D' introduced ambiguous type `%T'",
4004 name, oldtype);
4005 return;
4009 /* Process a using-declaration not appearing in class or local scope. */
4011 void
4012 do_toplevel_using_decl (tree decl)
4014 tree scope, name;
4015 tree oldval, oldtype, newval, newtype;
4016 cxx_binding *binding;
4018 decl = validate_nonmember_using_decl (decl, &scope, &name);
4019 if (decl == NULL_TREE)
4020 return;
4022 binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name);
4024 oldval = BINDING_VALUE (binding);
4025 oldtype = BINDING_TYPE (binding);
4027 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
4029 /* Copy declarations found. */
4030 if (newval)
4031 BINDING_VALUE (binding) = newval;
4032 if (newtype)
4033 BINDING_TYPE (binding) = newtype;
4034 return;
4037 /* Process a using-declaration at function scope. */
4039 void
4040 do_local_using_decl (tree decl)
4042 tree scope, name;
4043 tree oldval, oldtype, newval, newtype;
4045 decl = validate_nonmember_using_decl (decl, &scope, &name);
4046 if (decl == NULL_TREE)
4047 return;
4049 if (building_stmt_tree ()
4050 && at_function_scope_p ())
4051 add_decl_stmt (decl);
4053 oldval = lookup_name_current_level (name);
4054 oldtype = lookup_type_current_level (name);
4056 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
4058 if (newval)
4060 if (is_overloaded_fn (newval))
4062 tree fn, term;
4064 /* We only need to push declarations for those functions
4065 that were not already bound in the current level.
4066 The old value might be NULL_TREE, it might be a single
4067 function, or an OVERLOAD. */
4068 if (oldval && TREE_CODE (oldval) == OVERLOAD)
4069 term = OVL_FUNCTION (oldval);
4070 else
4071 term = oldval;
4072 for (fn = newval; fn && OVL_CURRENT (fn) != term;
4073 fn = OVL_NEXT (fn))
4074 push_overloaded_decl (OVL_CURRENT (fn),
4075 PUSH_LOCAL | PUSH_USING);
4077 else
4078 push_local_binding (name, newval, PUSH_USING);
4080 if (newtype)
4081 set_identifier_type_value (name, newtype);
4084 tree
4085 do_class_using_decl (tree decl)
4087 tree name, value, scope, type;
4089 if (TREE_CODE (decl) != SCOPE_REF
4090 || !TREE_OPERAND (decl, 0)
4091 || !TYPE_P (TREE_OPERAND (decl, 0)))
4093 error ("using-declaration for non-member at class scope");
4094 return NULL_TREE;
4096 scope = TREE_OPERAND (decl, 0);
4097 name = TREE_OPERAND (decl, 1);
4098 if (TREE_CODE (name) == BIT_NOT_EXPR)
4100 error ("using-declaration cannot name destructor");
4101 return NULL_TREE;
4103 else if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4105 template_id_error:;
4107 error ("a using-declaration cannot specify a template-id");
4108 return NULL_TREE;
4110 if (TREE_CODE (name) == TYPE_DECL)
4112 if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (name)))
4113 goto template_id_error;
4114 name = DECL_NAME (name);
4116 else if (TREE_CODE (name) == TEMPLATE_DECL)
4117 name = DECL_NAME (name);
4118 else if (BASELINK_P (name))
4120 tree fns = BASELINK_FUNCTIONS (name);
4122 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
4123 goto template_id_error;
4124 name = DECL_NAME (get_first_fn (fns));
4127 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 980716);
4129 /* Dependent using decls have a NULL type, non-dependent ones have a
4130 void type. */
4131 type = dependent_type_p (scope) ? NULL_TREE : void_type_node;
4132 value = build_lang_decl (USING_DECL, name, type);
4133 DECL_INITIAL (value) = scope;
4134 return value;
4137 /* Process a using-directive. */
4139 void
4140 do_using_directive (tree namespace)
4142 if (building_stmt_tree ())
4143 add_stmt (build_stmt (USING_STMT, namespace));
4145 /* using namespace A::B::C; */
4146 if (TREE_CODE (namespace) == SCOPE_REF)
4147 namespace = TREE_OPERAND (namespace, 1);
4148 if (TREE_CODE (namespace) == IDENTIFIER_NODE)
4150 /* Lookup in lexer did not find a namespace. */
4151 if (!processing_template_decl)
4152 error ("namespace `%T' undeclared", namespace);
4153 return;
4155 if (TREE_CODE (namespace) != NAMESPACE_DECL)
4157 if (!processing_template_decl)
4158 error ("`%T' is not a namespace", namespace);
4159 return;
4161 namespace = ORIGINAL_NAMESPACE (namespace);
4162 if (!toplevel_bindings_p ())
4163 push_using_directive (namespace);
4164 else
4165 /* direct usage */
4166 add_using_namespace (current_namespace, namespace, 0);
4169 void
4170 check_default_args (tree x)
4172 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4173 bool saw_def = false;
4174 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4175 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4177 if (TREE_PURPOSE (arg))
4178 saw_def = true;
4179 else if (saw_def)
4181 cp_error_at ("default argument missing for parameter %P of `%+#D'",
4182 i, x);
4183 break;
4188 void
4189 mark_used (tree decl)
4191 TREE_USED (decl) = 1;
4192 if (processing_template_decl || skip_evaluation)
4193 return;
4195 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
4196 && !TREE_ASM_WRITTEN (decl))
4197 /* Remember it, so we can check it was defined. */
4198 defer_fn (decl);
4200 assemble_external (decl);
4202 /* Is it a synthesized method that needs to be synthesized? */
4203 if (TREE_CODE (decl) == FUNCTION_DECL
4204 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
4205 && DECL_ARTIFICIAL (decl)
4206 && !DECL_THUNK_P (decl)
4207 && ! DECL_INITIAL (decl)
4208 /* Kludge: don't synthesize for default args. */
4209 && current_function_decl)
4211 synthesize_method (decl);
4212 /* If we've already synthesized the method we don't need to
4213 instantiate it, so we can return right away. */
4214 return;
4217 /* If this is a function or variable that is an instance of some
4218 template, we now know that we will need to actually do the
4219 instantiation. We check that DECL is not an explicit
4220 instantiation because that is not checked in instantiate_decl. */
4221 if ((DECL_NON_THUNK_FUNCTION_P (decl) || TREE_CODE (decl) == VAR_DECL)
4222 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
4223 && (!DECL_EXPLICIT_INSTANTIATION (decl)
4224 || (TREE_CODE (decl) == FUNCTION_DECL
4225 && DECL_INLINE (DECL_TEMPLATE_RESULT
4226 (template_for_substitution (decl))))))
4228 bool defer;
4230 /* Normally, we put off instantiating functions in order to
4231 improve compile times. Maintaining a stack of active
4232 functions is expensive, and the inliner knows to
4233 instantiate any functions it might need.
4235 However, if instantiating this function might help us mark
4236 the current function TREE_NOTHROW, we go ahead and
4237 instantiate it now. */
4238 defer = (!flag_exceptions
4239 || TREE_CODE (decl) != FUNCTION_DECL
4240 /* If the called function can't throw, we don't need to
4241 generate its body to find that out. */
4242 || TREE_NOTHROW (decl)
4243 || !cfun
4244 /* If we already know the current function can't throw,
4245 then we don't need to work hard to prove it. */
4246 || TREE_NOTHROW (current_function_decl)
4247 /* If we already know that the current function *can*
4248 throw, there's no point in gathering more
4249 information. */
4250 || cp_function_chain->can_throw);
4252 instantiate_decl (decl, defer);
4256 #include "gt-cp-decl2.h"