cp:
[official-gcc.git] / gcc / cp / decl2.c
blob556f62a1472554dfcb8b2b29f832970f98babebf
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 (parm) = type_passed_as (type);
227 return parm;
230 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
231 indicated NAME. */
233 tree
234 build_artificial_parm (tree name, tree type)
236 tree parm = cp_build_parm_decl (name, type);
237 DECL_ARTIFICIAL (parm) = 1;
238 /* All our artificial parms are implicitly `const'; they cannot be
239 assigned to. */
240 TREE_READONLY (parm) = 1;
241 return parm;
244 /* Constructors for types with virtual baseclasses need an "in-charge" flag
245 saying whether this constructor is responsible for initialization of
246 virtual baseclasses or not. All destructors also need this "in-charge"
247 flag, which additionally determines whether or not the destructor should
248 free the memory for the object.
250 This function adds the "in-charge" flag to member function FN if
251 appropriate. It is called from grokclassfn and tsubst.
252 FN must be either a constructor or destructor.
254 The in-charge flag follows the 'this' parameter, and is followed by the
255 VTT parm (if any), then the user-written parms. */
257 void
258 maybe_retrofit_in_chrg (tree fn)
260 tree basetype, arg_types, parms, parm, fntype;
262 /* If we've already add the in-charge parameter don't do it again. */
263 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
264 return;
266 /* When processing templates we can't know, in general, whether or
267 not we're going to have virtual baseclasses. */
268 if (uses_template_parms (fn))
269 return;
271 /* We don't need an in-charge parameter for constructors that don't
272 have virtual bases. */
273 if (DECL_CONSTRUCTOR_P (fn)
274 && !TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
275 return;
277 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
278 basetype = TREE_TYPE (TREE_VALUE (arg_types));
279 arg_types = TREE_CHAIN (arg_types);
281 parms = TREE_CHAIN (DECL_ARGUMENTS (fn));
283 /* If this is a subobject constructor or destructor, our caller will
284 pass us a pointer to our VTT. */
285 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
287 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
289 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
290 TREE_CHAIN (parm) = parms;
291 parms = parm;
293 /* ...and then to TYPE_ARG_TYPES. */
294 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
296 DECL_HAS_VTT_PARM_P (fn) = 1;
299 /* Then add the in-charge parm (before the VTT parm). */
300 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
301 TREE_CHAIN (parm) = parms;
302 parms = parm;
303 arg_types = hash_tree_chain (integer_type_node, arg_types);
305 /* Insert our new parameter(s) into the list. */
306 TREE_CHAIN (DECL_ARGUMENTS (fn)) = parms;
308 /* And rebuild the function type. */
309 fntype = build_cplus_method_type (basetype, TREE_TYPE (TREE_TYPE (fn)),
310 arg_types);
311 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
312 fntype = build_exception_variant (fntype,
313 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
314 TREE_TYPE (fn) = fntype;
316 /* Now we've got the in-charge parameter. */
317 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
320 /* Classes overload their constituent function names automatically.
321 When a function name is declared in a record structure,
322 its name is changed to it overloaded name. Since names for
323 constructors and destructors can conflict, we place a leading
324 '$' for destructors.
326 CNAME is the name of the class we are grokking for.
328 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
330 FLAGS contains bits saying what's special about today's
331 arguments. 1 == DESTRUCTOR. 2 == OPERATOR.
333 If FUNCTION is a destructor, then we must add the `auto-delete' field
334 as a second parameter. There is some hair associated with the fact
335 that we must "declare" this variable in the manner consistent with the
336 way the rest of the arguments were declared.
338 QUALS are the qualifiers for the this pointer. */
340 void
341 grokclassfn (tree ctype, tree function, enum overload_flags flags, tree quals)
343 tree fn_name = DECL_NAME (function);
344 int this_quals = TYPE_UNQUALIFIED;
346 /* Even within an `extern "C"' block, members get C++ linkage. See
347 [dcl.link] for details. */
348 SET_DECL_LANGUAGE (function, lang_cplusplus);
350 if (fn_name == NULL_TREE)
352 error ("name missing for member function");
353 fn_name = get_identifier ("<anonymous>");
354 DECL_NAME (function) = fn_name;
357 if (quals)
358 this_quals = grok_method_quals (ctype, function, quals);
360 if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
362 /* Must add the class instance variable up front. */
363 /* Right now we just make this a pointer. But later
364 we may wish to make it special. */
365 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (function)));
366 tree qual_type;
367 tree parm;
369 /* The `this' parameter is implicitly `const'; it cannot be
370 assigned to. */
371 this_quals |= TYPE_QUAL_CONST;
372 qual_type = cp_build_qualified_type (type, this_quals);
373 parm = build_artificial_parm (this_identifier, qual_type);
374 c_apply_type_quals_to_decl (this_quals, parm);
375 TREE_CHAIN (parm) = last_function_parms;
376 last_function_parms = parm;
379 DECL_ARGUMENTS (function) = last_function_parms;
380 DECL_CONTEXT (function) = ctype;
382 if (flags == DTOR_FLAG)
383 DECL_DESTRUCTOR_P (function) = 1;
385 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
386 maybe_retrofit_in_chrg (function);
388 if (flags == DTOR_FLAG)
390 DECL_DESTRUCTOR_P (function) = 1;
391 TYPE_HAS_DESTRUCTOR (ctype) = 1;
395 /* Create an ARRAY_REF, checking for the user doing things backwards
396 along the way. */
398 tree
399 grok_array_decl (tree array_expr, tree index_exp)
401 tree type;
402 tree expr;
403 tree orig_array_expr = array_expr;
404 tree orig_index_exp = index_exp;
406 if (error_operand_p (array_expr) || error_operand_p (index_exp))
407 return error_mark_node;
409 if (processing_template_decl)
411 if (type_dependent_expression_p (array_expr)
412 || type_dependent_expression_p (index_exp))
413 return build_min_nt (ARRAY_REF, array_expr, index_exp);
414 array_expr = build_non_dependent_expr (array_expr);
415 index_exp = build_non_dependent_expr (index_exp);
418 type = TREE_TYPE (array_expr);
419 my_friendly_assert (type, 20030626);
420 type = non_reference (type);
422 /* If they have an `operator[]', use that. */
423 if (IS_AGGR_TYPE (type) || IS_AGGR_TYPE (TREE_TYPE (index_exp)))
424 expr = build_new_op (ARRAY_REF, LOOKUP_NORMAL,
425 array_expr, index_exp, NULL_TREE);
426 else
428 tree p1, p2, i1, i2;
430 /* Otherwise, create an ARRAY_REF for a pointer or array type.
431 It is a little-known fact that, if `a' is an array and `i' is
432 an int, you can write `i[a]', which means the same thing as
433 `a[i]'. */
434 if (TREE_CODE (type) == ARRAY_TYPE)
435 p1 = array_expr;
436 else
437 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
439 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
440 p2 = index_exp;
441 else
442 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
444 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
445 false);
446 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
447 false);
449 if ((p1 && i2) && (i1 && p2))
450 error ("ambiguous conversion for array subscript");
452 if (p1 && i2)
453 array_expr = p1, index_exp = i2;
454 else if (i1 && p2)
455 array_expr = p2, index_exp = i1;
456 else
458 error ("invalid types `%T[%T]' for array subscript",
459 type, TREE_TYPE (index_exp));
460 return error_mark_node;
463 if (array_expr == error_mark_node || index_exp == error_mark_node)
464 error ("ambiguous conversion for array subscript");
466 expr = build_array_ref (array_expr, index_exp);
468 if (processing_template_decl && expr != error_mark_node)
469 return build_min (ARRAY_REF, TREE_TYPE (expr), orig_array_expr,
470 orig_index_exp);
471 return expr;
474 /* Given the cast expression EXP, checking out its validity. Either return
475 an error_mark_node if there was an unavoidable error, return a cast to
476 void for trying to delete a pointer w/ the value 0, or return the
477 call to delete. If DOING_VEC is 1, we handle things differently
478 for doing an array delete. If DOING_VEC is 2, they gave us the
479 array size as an argument to delete.
480 Implements ARM $5.3.4. This is called from the parser. */
482 tree
483 delete_sanity (tree exp, tree size, int doing_vec, int use_global_delete)
485 tree t, type;
486 /* For a regular vector delete (aka, no size argument) we will pass
487 this down as a NULL_TREE into build_vec_delete. */
488 tree maxindex = NULL_TREE;
490 if (exp == error_mark_node)
491 return exp;
493 if (processing_template_decl)
495 t = build_min (DELETE_EXPR, void_type_node, exp, size);
496 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
497 DELETE_EXPR_USE_VEC (t) = doing_vec;
498 return t;
501 exp = convert_from_reference (exp);
502 t = build_expr_type_conversion (WANT_POINTER, exp, true);
504 if (t == NULL_TREE || t == error_mark_node)
506 error ("type `%#T' argument given to `delete', expected pointer",
507 TREE_TYPE (exp));
508 return error_mark_node;
511 if (doing_vec == 2)
513 maxindex = cp_build_binary_op (MINUS_EXPR, size, integer_one_node);
514 pedwarn ("anachronistic use of array size in vector delete");
517 type = TREE_TYPE (t);
519 /* As of Valley Forge, you can delete a pointer to const. */
521 /* You can't delete functions. */
522 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
524 error ("cannot delete a function. Only pointer-to-objects are valid arguments to `delete'");
525 return error_mark_node;
528 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
529 if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
531 warning ("deleting `%T' is undefined", type);
532 doing_vec = 0;
535 /* An array can't have been allocated by new, so complain. */
536 if (TREE_CODE (t) == ADDR_EXPR
537 && TREE_CODE (TREE_OPERAND (t, 0)) == VAR_DECL
538 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == ARRAY_TYPE)
539 warning ("deleting array `%#D'", TREE_OPERAND (t, 0));
541 /* Deleting a pointer with the value zero is valid and has no effect. */
542 if (integer_zerop (t))
543 return build1 (NOP_EXPR, void_type_node, t);
545 if (doing_vec)
546 return build_vec_delete (t, maxindex, sfk_deleting_destructor,
547 use_global_delete);
548 else
549 return build_delete (type, t, sfk_deleting_destructor,
550 LOOKUP_NORMAL, use_global_delete);
553 /* Report an error if the indicated template declaration is not the
554 sort of thing that should be a member template. */
556 void
557 check_member_template (tree tmpl)
559 tree decl;
561 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
562 decl = DECL_TEMPLATE_RESULT (tmpl);
564 if (TREE_CODE (decl) == FUNCTION_DECL
565 || (TREE_CODE (decl) == TYPE_DECL
566 && IS_AGGR_TYPE (TREE_TYPE (decl))))
568 if (current_function_decl)
569 /* 14.5.2.2 [temp.mem]
571 A local class shall not have member templates. */
572 error ("invalid declaration of member template `%#D' in local class",
573 decl);
575 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
577 /* 14.5.2.3 [temp.mem]
579 A member function template shall not be virtual. */
580 error
581 ("invalid use of `virtual' in template declaration of `%#D'",
582 decl);
583 DECL_VIRTUAL_P (decl) = 0;
586 /* The debug-information generating code doesn't know what to do
587 with member templates. */
588 DECL_IGNORED_P (tmpl) = 1;
590 else
591 error ("template declaration of `%#D'", decl);
594 /* Return true iff TYPE is a valid Java parameter or return type. */
596 static bool
597 acceptable_java_type (tree type)
599 if (TREE_CODE (type) == VOID_TYPE || TYPE_FOR_JAVA (type))
600 return 1;
601 if (TREE_CODE (type) == POINTER_TYPE || TREE_CODE (type) == REFERENCE_TYPE)
603 type = TREE_TYPE (type);
604 if (TREE_CODE (type) == RECORD_TYPE)
606 tree args; int i;
607 if (! TYPE_FOR_JAVA (type))
608 return false;
609 if (! CLASSTYPE_TEMPLATE_INFO (type))
610 return true;
611 args = CLASSTYPE_TI_ARGS (type);
612 i = TREE_VEC_LENGTH (args);
613 while (--i >= 0)
615 type = TREE_VEC_ELT (args, i);
616 if (TREE_CODE (type) == POINTER_TYPE)
617 type = TREE_TYPE (type);
618 if (! TYPE_FOR_JAVA (type))
619 return false;
621 return true;
624 return false;
627 /* For a METHOD in a Java class CTYPE, return true if
628 the parameter and return types are valid Java types.
629 Otherwise, print appropriate error messages, and return false. */
631 bool
632 check_java_method (tree method)
634 bool jerr = false;
635 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
636 tree ret_type = TREE_TYPE (TREE_TYPE (method));
637 if (!acceptable_java_type (ret_type))
639 error ("Java method '%D' has non-Java return type `%T'",
640 method, ret_type);
641 jerr = true;
643 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
645 tree type = TREE_VALUE (arg_types);
646 if (!acceptable_java_type (type))
648 error ("Java method '%D' has non-Java parameter type `%T'",
649 method, type);
650 jerr = true;
653 return !jerr;
656 /* Sanity check: report error if this function FUNCTION is not
657 really a member of the class (CTYPE) it is supposed to belong to.
658 CNAME is the same here as it is for grokclassfn above. */
660 tree
661 check_classfn (tree ctype, tree function)
663 int ix;
664 int is_template;
666 if (DECL_USE_TEMPLATE (function)
667 && !(TREE_CODE (function) == TEMPLATE_DECL
668 && DECL_TEMPLATE_SPECIALIZATION (function))
669 && is_member_template (DECL_TI_TEMPLATE (function)))
670 /* Since this is a specialization of a member template,
671 we're not going to find the declaration in the class.
672 For example, in:
674 struct S { template <typename T> void f(T); };
675 template <> void S::f(int);
677 we're not going to find `S::f(int)', but there's no
678 reason we should, either. We let our callers know we didn't
679 find the method, but we don't complain. */
680 return NULL_TREE;
682 /* OK, is this a definition of a member template? */
683 is_template = (TREE_CODE (function) == TEMPLATE_DECL
684 || (processing_template_decl - template_class_depth (ctype)));
686 ix = lookup_fnfields_1 (complete_type (ctype),
687 DECL_CONSTRUCTOR_P (function) ? ctor_identifier :
688 DECL_DESTRUCTOR_P (function) ? dtor_identifier :
689 DECL_NAME (function));
691 if (ix >= 0)
693 tree methods = CLASSTYPE_METHOD_VEC (ctype);
694 tree fndecls, fndecl = 0;
695 bool is_conv_op;
696 const char *format = NULL;
698 push_scope (ctype);
699 for (fndecls = TREE_VEC_ELT (methods, ix);
700 fndecls; fndecls = OVL_NEXT (fndecls))
702 tree p1, p2;
704 fndecl = OVL_CURRENT (fndecls);
705 p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
706 p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
708 /* We cannot simply call decls_match because this doesn't
709 work for static member functions that are pretending to
710 be methods, and because the name may have been changed by
711 asm("new_name"). */
713 /* Get rid of the this parameter on functions that become
714 static. */
715 if (DECL_STATIC_FUNCTION_P (fndecl)
716 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
717 p1 = TREE_CHAIN (p1);
719 /* A member template definition only matches a member template
720 declaration. */
721 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
722 continue;
724 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
725 TREE_TYPE (TREE_TYPE (fndecl)))
726 && compparms (p1, p2)
727 && (DECL_TEMPLATE_SPECIALIZATION (function)
728 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
729 && (!DECL_TEMPLATE_SPECIALIZATION (function)
730 || (DECL_TI_TEMPLATE (function)
731 == DECL_TI_TEMPLATE (fndecl))))
732 break;
734 pop_scope (ctype);
735 if (fndecls)
736 return OVL_CURRENT (fndecls);
737 error ("prototype for `%#D' does not match any in class `%T'",
738 function, ctype);
739 is_conv_op = DECL_CONV_FN_P (fndecl);
741 if (is_conv_op)
742 ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
743 fndecls = TREE_VEC_ELT (methods, ix);
744 while (fndecls)
746 fndecl = OVL_CURRENT (fndecls);
747 fndecls = OVL_NEXT (fndecls);
749 if (!fndecls && is_conv_op)
751 if (TREE_VEC_LENGTH (methods) > ix)
753 ix++;
754 fndecls = TREE_VEC_ELT (methods, ix);
755 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
757 fndecls = NULL_TREE;
758 is_conv_op = false;
761 else
762 is_conv_op = false;
764 if (format)
765 format = " %#D";
766 else if (fndecls)
767 format = "candidates are: %#D";
768 else
769 format = "candidate is: %#D";
770 cp_error_at (format, fndecl);
773 else if (!COMPLETE_TYPE_P (ctype))
774 cxx_incomplete_type_error (function, ctype);
775 else
776 error ("no `%#D' member function declared in class `%T'",
777 function, ctype);
779 /* If we did not find the method in the class, add it to avoid
780 spurious errors (unless the CTYPE is not yet defined, in which
781 case we'll only confuse ourselves when the function is declared
782 properly within the class. */
783 if (COMPLETE_TYPE_P (ctype))
784 add_method (ctype, function, /*error_p=*/1);
785 return NULL_TREE;
788 /* We have just processed the DECL, which is a static data member.
789 Its initializer, if present, is INIT. The ASMSPEC_TREE, if
790 present, is the assembly-language name for the data member.
791 FLAGS is as for cp_finish_decl. */
793 void
794 finish_static_data_member_decl (tree decl, tree init, tree asmspec_tree,
795 int flags)
797 my_friendly_assert (TREE_PUBLIC (decl), 0);
799 DECL_CONTEXT (decl) = current_class_type;
801 /* We cannot call pushdecl here, because that would fill in the
802 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
803 the right thing, namely, to put this decl out straight away. */
804 /* current_class_type can be NULL_TREE in case of error. */
805 if (!asmspec_tree && current_class_type)
806 DECL_INITIAL (decl) = error_mark_node;
808 if (! processing_template_decl)
810 if (!pending_statics)
811 VARRAY_TREE_INIT (pending_statics, 32, "pending_statics");
812 VARRAY_PUSH_TREE (pending_statics, decl);
815 if (LOCAL_CLASS_P (current_class_type))
816 pedwarn ("local class `%#T' shall not have static data member `%#D'",
817 current_class_type, decl);
819 /* Static consts need not be initialized in the class definition. */
820 if (init != NULL_TREE && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
822 static int explained = 0;
824 error ("initializer invalid for static member with constructor");
825 if (!explained)
827 error ("(an out of class initialization is required)");
828 explained = 1;
830 init = NULL_TREE;
832 /* Force the compiler to know when an uninitialized static const
833 member is being used. */
834 if (CP_TYPE_CONST_P (TREE_TYPE (decl)) && init == 0)
835 TREE_USED (decl) = 1;
836 DECL_INITIAL (decl) = init;
837 DECL_IN_AGGR_P (decl) = 1;
839 cp_finish_decl (decl, init, asmspec_tree, flags);
842 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
843 of a structure component, returning a _DECL node.
844 QUALS is a list of type qualifiers for this decl (such as for declaring
845 const member functions).
847 This is done during the parsing of the struct declaration.
848 The _DECL nodes are chained together and the lot of them
849 are ultimately passed to `build_struct' to make the RECORD_TYPE node.
851 If class A defines that certain functions in class B are friends, then
852 the way I have set things up, it is B who is interested in permission
853 granted by A. However, it is in A's context that these declarations
854 are parsed. By returning a void_type_node, class A does not attempt
855 to incorporate the declarations of the friends within its structure.
857 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
858 CHANGES TO CODE IN `start_method'. */
860 tree
861 grokfield (tree declarator, tree declspecs, tree init, tree asmspec_tree,
862 tree attrlist)
864 tree value;
865 const char *asmspec = 0;
866 int flags = LOOKUP_ONLYCONVERTING;
868 if (declspecs == NULL_TREE
869 && TREE_CODE (declarator) == SCOPE_REF
870 && TREE_CODE (TREE_OPERAND (declarator, 1)) == IDENTIFIER_NODE)
872 /* Access declaration */
873 if (! IS_AGGR_TYPE_CODE (TREE_CODE (TREE_OPERAND (declarator, 0))))
875 else if (TREE_COMPLEXITY (declarator) == current_class_depth)
876 pop_nested_class ();
877 return do_class_using_decl (declarator);
880 if (init
881 && TREE_CODE (init) == TREE_LIST
882 && TREE_VALUE (init) == error_mark_node
883 && TREE_CHAIN (init) == NULL_TREE)
884 init = NULL_TREE;
886 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
887 if (! value || value == error_mark_node)
888 /* friend or constructor went bad. */
889 return value;
890 if (TREE_TYPE (value) == error_mark_node)
891 return error_mark_node;
893 if (TREE_CODE (value) == TYPE_DECL && init)
895 error ("typedef `%D' is initialized (use __typeof__ instead)", value);
896 init = NULL_TREE;
899 /* Pass friendly classes back. */
900 if (value == void_type_node)
901 return value;
903 /* Pass friend decls back. */
904 if ((TREE_CODE (value) == FUNCTION_DECL
905 || TREE_CODE (value) == TEMPLATE_DECL)
906 && DECL_CONTEXT (value) != current_class_type)
907 return value;
909 if (DECL_NAME (value) != NULL_TREE
910 && IDENTIFIER_POINTER (DECL_NAME (value))[0] == '_'
911 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (value)), "_vptr"))
912 error ("member `%D' conflicts with virtual function table field name",
913 value);
915 /* Stash away type declarations. */
916 if (TREE_CODE (value) == TYPE_DECL)
918 DECL_NONLOCAL (value) = 1;
919 DECL_CONTEXT (value) = current_class_type;
921 if (CLASS_TYPE_P (TREE_TYPE (value)))
922 CLASSTYPE_GOT_SEMICOLON (TREE_TYPE (value)) = 1;
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 (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
2566 void *data ATTRIBUTE_UNUSED)
2568 if (TREE_CODE (*tp) == PTRMEM_CST
2569 && TYPE_PTRMEMFUNC_P (TREE_TYPE (*tp)))
2570 cgraph_mark_needed_node (cgraph_node (PTRMEM_CST_MEMBER (*tp)), 1);
2571 return 0;
2574 /* Called via LANGHOOK_CALLGRAPH_LOWER_FUNCTION. It is supposed to lower
2575 frontend specific constructs that would otherwise confuse the middle end. */
2576 void
2577 lower_function (tree fn)
2579 walk_tree_without_duplicates (&DECL_SAVED_TREE (fn), mark_member_pointers,
2580 NULL);
2583 /* This routine is called from the last rule in yyparse ().
2584 Its job is to create all the code needed to initialize and
2585 destroy the global aggregates. We do the destruction
2586 first, since that way we only need to reverse the decls once. */
2588 void
2589 finish_file ()
2591 tree vars;
2592 bool reconsider;
2593 size_t i;
2594 location_t locus;
2595 unsigned ssdf_count = 0;
2597 locus = input_location;
2598 at_eof = 1;
2600 /* Bad parse errors. Just forget about it. */
2601 if (! global_bindings_p () || current_class_type || decl_namespace_list)
2602 return;
2604 if (pch_file)
2605 c_common_write_pch ();
2607 /* Otherwise, GDB can get confused, because in only knows
2608 about source for LINENO-1 lines. */
2609 input_line -= 1;
2611 interface_unknown = 1;
2612 interface_only = 0;
2614 /* We now have to write out all the stuff we put off writing out.
2615 These include:
2617 o Template specializations that we have not yet instantiated,
2618 but which are needed.
2619 o Initialization and destruction for non-local objects with
2620 static storage duration. (Local objects with static storage
2621 duration are initialized when their scope is first entered,
2622 and are cleaned up via atexit.)
2623 o Virtual function tables.
2625 All of these may cause others to be needed. For example,
2626 instantiating one function may cause another to be needed, and
2627 generating the initializer for an object may cause templates to be
2628 instantiated, etc., etc. */
2630 timevar_push (TV_VARCONST);
2632 emit_support_tinfos ();
2636 tree t;
2637 size_t n_old, n_new;
2639 reconsider = false;
2641 /* If there are templates that we've put off instantiating, do
2642 them now. */
2643 instantiate_pending_templates ();
2645 /* Write out virtual tables as required. Note that writing out
2646 the virtual table for a template class may cause the
2647 instantiation of members of that class. If we write out
2648 vtables then we remove the class from our list so we don't
2649 have to look at it again. */
2651 while (keyed_classes != NULL_TREE
2652 && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
2654 reconsider = true;
2655 keyed_classes = TREE_CHAIN (keyed_classes);
2658 t = keyed_classes;
2659 if (t != NULL_TREE)
2661 tree next = TREE_CHAIN (t);
2663 while (next)
2665 if (maybe_emit_vtables (TREE_VALUE (next)))
2667 reconsider = true;
2668 TREE_CHAIN (t) = TREE_CHAIN (next);
2670 else
2671 t = next;
2673 next = TREE_CHAIN (t);
2677 /* Write out needed type info variables. We have to be careful
2678 looping through unemitted decls, because emit_tinfo_decl may
2679 cause other variables to be needed. We stick new elements
2680 (and old elements that we may need to reconsider) at the end
2681 of the array, then shift them back to the beginning once we're
2682 done. */
2684 n_old = VARRAY_ACTIVE_SIZE (unemitted_tinfo_decls);
2685 for (i = 0; i < n_old; ++i)
2687 tree tinfo_decl = VARRAY_TREE (unemitted_tinfo_decls, i);
2688 if (emit_tinfo_decl (tinfo_decl))
2689 reconsider = true;
2690 else
2691 VARRAY_PUSH_TREE (unemitted_tinfo_decls, tinfo_decl);
2694 /* The only elements we want to keep are the new ones. Copy
2695 them to the beginning of the array, then get rid of the
2696 leftovers. */
2697 n_new = VARRAY_ACTIVE_SIZE (unemitted_tinfo_decls) - n_old;
2698 memmove (&VARRAY_TREE (unemitted_tinfo_decls, 0),
2699 &VARRAY_TREE (unemitted_tinfo_decls, n_old),
2700 n_new * sizeof (tree));
2701 memset (&VARRAY_TREE (unemitted_tinfo_decls, n_new),
2703 n_old * sizeof (tree));
2704 VARRAY_ACTIVE_SIZE (unemitted_tinfo_decls) = n_new;
2706 /* The list of objects with static storage duration is built up
2707 in reverse order. We clear STATIC_AGGREGATES so that any new
2708 aggregates added during the initialization of these will be
2709 initialized in the correct order when we next come around the
2710 loop. */
2711 vars = prune_vars_needing_no_initialization (&static_aggregates);
2713 if (vars)
2715 tree v;
2717 /* We need to start a new initialization function each time
2718 through the loop. That's because we need to know which
2719 vtables have been referenced, and TREE_SYMBOL_REFERENCED
2720 isn't computed until a function is finished, and written
2721 out. That's a deficiency in the back-end. When this is
2722 fixed, these initialization functions could all become
2723 inline, with resulting performance improvements. */
2724 tree ssdf_body;
2726 /* Set the line and file, so that it is obviously not from
2727 the source file. */
2728 input_location = locus;
2729 ssdf_body = start_static_storage_duration_function (ssdf_count);
2731 /* Make sure the back end knows about all the variables. */
2732 write_out_vars (vars);
2734 /* First generate code to do all the initializations. */
2735 for (v = vars; v; v = TREE_CHAIN (v))
2736 do_static_initialization (TREE_VALUE (v),
2737 TREE_PURPOSE (v));
2739 /* Then, generate code to do all the destructions. Do these
2740 in reverse order so that the most recently constructed
2741 variable is the first destroyed. If we're using
2742 __cxa_atexit, then we don't need to do this; functions
2743 were registered at initialization time to destroy the
2744 local statics. */
2745 if (!flag_use_cxa_atexit)
2747 vars = nreverse (vars);
2748 for (v = vars; v; v = TREE_CHAIN (v))
2749 do_static_destruction (TREE_VALUE (v));
2751 else
2752 vars = NULL_TREE;
2754 /* Finish up the static storage duration function for this
2755 round. */
2756 input_location = locus;
2757 finish_static_storage_duration_function (ssdf_body);
2759 /* All those initializations and finalizations might cause
2760 us to need more inline functions, more template
2761 instantiations, etc. */
2762 reconsider = true;
2763 ssdf_count++;
2764 locus.line++;
2767 for (i = 0; i < deferred_fns_used; ++i)
2769 tree decl = VARRAY_TREE (deferred_fns, i);
2771 /* Does it need synthesizing? */
2772 if (DECL_ARTIFICIAL (decl) && ! DECL_INITIAL (decl)
2773 && TREE_USED (decl)
2774 && (! DECL_REALLY_EXTERN (decl) || DECL_INLINE (decl)))
2776 /* Even though we're already at the top-level, we push
2777 there again. That way, when we pop back a few lines
2778 hence, all of our state is restored. Otherwise,
2779 finish_function doesn't clean things up, and we end
2780 up with CURRENT_FUNCTION_DECL set. */
2781 push_to_top_level ();
2782 synthesize_method (decl);
2783 pop_from_top_level ();
2784 reconsider = true;
2787 /* If the function has no body, avoid calling
2788 import_export_decl. On a system without weak symbols,
2789 calling import_export_decl will make an inline template
2790 instantiation "static", which will result in errors about
2791 the use of undefined functions if there is no body for
2792 the function. */
2793 if (!DECL_SAVED_TREE (decl))
2794 continue;
2796 import_export_decl (decl);
2798 /* We lie to the back-end, pretending that some functions
2799 are not defined when they really are. This keeps these
2800 functions from being put out unnecessarily. But, we must
2801 stop lying when the functions are referenced, or if they
2802 are not comdat since they need to be put out now. This
2803 is done in a separate for cycle, because if some deferred
2804 function is contained in another deferred function later
2805 in deferred_fns varray, rest_of_compilation would skip
2806 this function and we really cannot expand the same
2807 function twice. */
2808 if (DECL_NOT_REALLY_EXTERN (decl)
2809 && DECL_INITIAL (decl)
2810 && DECL_NEEDED_P (decl))
2811 DECL_EXTERNAL (decl) = 0;
2813 /* If we're going to need to write this function out, and
2814 there's already a body for it, create RTL for it now.
2815 (There might be no body if this is a method we haven't
2816 gotten around to synthesizing yet.) */
2817 if (!DECL_EXTERNAL (decl)
2818 && DECL_NEEDED_P (decl)
2819 && DECL_SAVED_TREE (decl)
2820 && !TREE_ASM_WRITTEN (decl)
2821 && (!flag_unit_at_a_time
2822 || !cgraph_node (decl)->local.finalized))
2824 /* We will output the function; no longer consider it in this
2825 loop. */
2826 DECL_DEFER_OUTPUT (decl) = 0;
2827 /* Generate RTL for this function now that we know we
2828 need it. */
2829 expand_or_defer_fn (decl);
2830 /* If we're compiling -fsyntax-only pretend that this
2831 function has been written out so that we don't try to
2832 expand it again. */
2833 if (flag_syntax_only)
2834 TREE_ASM_WRITTEN (decl) = 1;
2835 reconsider = true;
2839 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
2840 reconsider = true;
2842 /* Static data members are just like namespace-scope globals. */
2843 for (i = 0; i < pending_statics_used; ++i)
2845 tree decl = VARRAY_TREE (pending_statics, i);
2846 if (TREE_ASM_WRITTEN (decl))
2847 continue;
2848 import_export_decl (decl);
2849 if (DECL_NOT_REALLY_EXTERN (decl) && ! DECL_IN_AGGR_P (decl))
2850 DECL_EXTERNAL (decl) = 0;
2852 if (pending_statics
2853 && wrapup_global_declarations (&VARRAY_TREE (pending_statics, 0),
2854 pending_statics_used))
2855 reconsider = true;
2857 while (reconsider);
2859 /* All used inline functions must have a definition at this point. */
2860 for (i = 0; i < deferred_fns_used; ++i)
2862 tree decl = VARRAY_TREE (deferred_fns, i);
2864 if (TREE_USED (decl) && DECL_DECLARED_INLINE_P (decl)
2865 && !(TREE_ASM_WRITTEN (decl) || DECL_SAVED_TREE (decl)
2866 /* An explicit instantiation can be used to specify
2867 that the body is in another unit. It will have
2868 already verified there was a definition. */
2869 || DECL_EXPLICIT_INSTANTIATION (decl)))
2871 cp_warning_at ("inline function `%D' used but never defined", decl);
2872 /* This symbol is effectively an "extern" declaration now.
2873 This is not strictly necessary, but removes a duplicate
2874 warning. */
2875 TREE_PUBLIC (decl) = 1;
2880 /* We give C linkage to static constructors and destructors. */
2881 push_lang_context (lang_name_c);
2883 /* Generate initialization and destruction functions for all
2884 priorities for which they are required. */
2885 if (priority_info_map)
2886 splay_tree_foreach (priority_info_map,
2887 generate_ctor_and_dtor_functions_for_priority,
2888 /*data=*/&locus);
2889 else
2892 if (static_ctors)
2893 generate_ctor_or_dtor_function (/*constructor_p=*/true,
2894 DEFAULT_INIT_PRIORITY, &locus);
2895 if (static_dtors)
2896 generate_ctor_or_dtor_function (/*constructor_p=*/false,
2897 DEFAULT_INIT_PRIORITY, &locus);
2900 /* We're done with the splay-tree now. */
2901 if (priority_info_map)
2902 splay_tree_delete (priority_info_map);
2904 /* We're done with static constructors, so we can go back to "C++"
2905 linkage now. */
2906 pop_lang_context ();
2908 if (flag_unit_at_a_time)
2910 cgraph_finalize_compilation_unit ();
2911 cgraph_optimize ();
2914 /* Now, issue warnings about static, but not defined, functions,
2915 etc., and emit debugging information. */
2916 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
2917 if (pending_statics)
2918 check_global_declarations (&VARRAY_TREE (pending_statics, 0),
2919 pending_statics_used);
2921 finish_repo ();
2923 /* The entire file is now complete. If requested, dump everything
2924 to a file. */
2926 int flags;
2927 FILE *stream = dump_begin (TDI_all, &flags);
2929 if (stream)
2931 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
2932 dump_end (TDI_all, stream);
2936 timevar_pop (TV_VARCONST);
2938 if (flag_detailed_statistics)
2940 dump_tree_statistics ();
2941 dump_time_statistics ();
2943 input_location = locus;
2946 /* FN is an OFFSET_REF indicating the function to call in parse-tree
2947 form; it has not yet been semantically analyzed. ARGS are the
2948 arguments to the function. They have already been semantically
2949 analzyed. */
2951 tree
2952 build_offset_ref_call_from_tree (tree fn, tree args)
2954 tree object_addr;
2955 tree orig_fn;
2956 tree orig_args;
2957 tree expr;
2959 orig_fn = fn;
2960 orig_args = args;
2962 if (processing_template_decl)
2964 tree object;
2965 tree object_type;
2967 my_friendly_assert (TREE_CODE (fn) == DOTSTAR_EXPR
2968 || TREE_CODE (fn) == MEMBER_REF,
2969 20030708);
2970 if (type_dependent_expression_p (fn)
2971 || any_type_dependent_arguments_p (args))
2972 return build_min_nt (CALL_EXPR, fn, args);
2974 /* Transform the arguments and add the implicit "this"
2975 parameter. That must be done before the FN is transformed
2976 because we depend on the form of FN. */
2977 args = build_non_dependent_args (args);
2978 object_type = TREE_TYPE (TREE_OPERAND (fn, 0));
2979 if (TREE_CODE (fn) == DOTSTAR_EXPR)
2980 object_type = build_pointer_type (non_reference (object_type));
2981 object = build (NON_DEPENDENT_EXPR, object_type);
2982 args = tree_cons (NULL_TREE, object, args);
2983 /* Now that the arguments are done, transform FN. */
2984 fn = build_non_dependent_expr (fn);
2987 /* A qualified name corresponding to a bound pointer-to-member is
2988 represented as an OFFSET_REF:
2990 struct B { void g(); };
2991 void (B::*p)();
2992 void B::g() { (this->*p)(); } */
2993 if (TREE_CODE (fn) == OFFSET_REF)
2995 object_addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (fn, 0), 0);
2996 fn = TREE_OPERAND (fn, 1);
2997 fn = get_member_function_from_ptrfunc (&object_addr, fn);
2998 args = tree_cons (NULL_TREE, object_addr, args);
3001 expr = build_function_call (fn, args);
3002 if (processing_template_decl && expr != error_mark_node)
3003 return build_min (CALL_EXPR, TREE_TYPE (expr), orig_fn, orig_args);
3004 return expr;
3007 /* Returns true if ROOT (a namespace, class, or function) encloses
3008 CHILD. CHILD may be either a class type or a namespace. */
3010 bool
3011 is_ancestor (tree root, tree child)
3013 my_friendly_assert ((TREE_CODE (root) == NAMESPACE_DECL
3014 || TREE_CODE (root) == FUNCTION_DECL
3015 || CLASS_TYPE_P (root)), 20030307);
3016 my_friendly_assert ((TREE_CODE (child) == NAMESPACE_DECL
3017 || CLASS_TYPE_P (child)),
3018 20030307);
3020 /* The global namespace encloses everything. */
3021 if (root == global_namespace)
3022 return true;
3024 while (true)
3026 /* If we've run out of scopes, stop. */
3027 if (!child)
3028 return false;
3029 /* If we've reached the ROOT, it encloses CHILD. */
3030 if (root == child)
3031 return true;
3032 /* Go out one level. */
3033 if (TYPE_P (child))
3034 child = TYPE_NAME (child);
3035 child = DECL_CONTEXT (child);
3040 /* Return the namespace that is the common ancestor
3041 of two given namespaces. */
3043 tree
3044 namespace_ancestor (tree ns1, tree ns2)
3046 timevar_push (TV_NAME_LOOKUP);
3047 if (is_ancestor (ns1, ns2))
3048 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ns1);
3049 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
3050 namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2));
3053 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
3054 directive is not directly from the source. Also find the common
3055 ancestor and let our users know about the new namespace */
3056 static void
3057 add_using_namespace (tree user, tree used, bool indirect)
3059 tree t;
3060 timevar_push (TV_NAME_LOOKUP);
3061 /* Using oneself is a no-op. */
3062 if (user == used)
3064 timevar_pop (TV_NAME_LOOKUP);
3065 return;
3067 my_friendly_assert (TREE_CODE (user) == NAMESPACE_DECL, 380);
3068 my_friendly_assert (TREE_CODE (used) == NAMESPACE_DECL, 380);
3069 /* Check if we already have this. */
3070 t = purpose_member (used, DECL_NAMESPACE_USING (user));
3071 if (t != NULL_TREE)
3073 if (!indirect)
3074 /* Promote to direct usage. */
3075 TREE_INDIRECT_USING (t) = 0;
3076 timevar_pop (TV_NAME_LOOKUP);
3077 return;
3080 /* Add used to the user's using list. */
3081 DECL_NAMESPACE_USING (user)
3082 = tree_cons (used, namespace_ancestor (user, used),
3083 DECL_NAMESPACE_USING (user));
3085 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
3087 /* Add user to the used's users list. */
3088 DECL_NAMESPACE_USERS (used)
3089 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
3091 /* Recursively add all namespaces used. */
3092 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
3093 /* indirect usage */
3094 add_using_namespace (user, TREE_PURPOSE (t), 1);
3096 /* Tell everyone using us about the new used namespaces. */
3097 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
3098 add_using_namespace (TREE_PURPOSE (t), used, 1);
3099 timevar_pop (TV_NAME_LOOKUP);
3102 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
3103 duplicates. The first list becomes the tail of the result.
3105 The algorithm is O(n^2). We could get this down to O(n log n) by
3106 doing a sort on the addresses of the functions, if that becomes
3107 necessary. */
3109 static tree
3110 merge_functions (tree s1, tree s2)
3112 for (; s2; s2 = OVL_NEXT (s2))
3114 tree fn2 = OVL_CURRENT (s2);
3115 tree fns1;
3117 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
3119 tree fn1 = OVL_CURRENT (fns1);
3121 /* If the function from S2 is already in S1, there is no
3122 need to add it again. For `extern "C"' functions, we
3123 might have two FUNCTION_DECLs for the same function, in
3124 different namespaces; again, we only need one of them. */
3125 if (fn1 == fn2
3126 || (DECL_EXTERN_C_P (fn1) && DECL_EXTERN_C_P (fn2)
3127 && DECL_NAME (fn1) == DECL_NAME (fn2)))
3128 break;
3131 /* If we exhausted all of the functions in S1, FN2 is new. */
3132 if (!fns1)
3133 s1 = build_overload (fn2, s1);
3135 return s1;
3138 /* This should return an error not all definitions define functions.
3139 It is not an error if we find two functions with exactly the
3140 same signature, only if these are selected in overload resolution.
3141 old is the current set of bindings, new the freshly-found binding.
3142 XXX Do we want to give *all* candidates in case of ambiguity?
3143 XXX In what way should I treat extern declarations?
3144 XXX I don't want to repeat the entire duplicate_decls here */
3146 static cxx_binding *
3147 ambiguous_decl (tree name, cxx_binding *old, cxx_binding *new, int flags)
3149 tree val, type;
3150 my_friendly_assert (old != NULL, 393);
3151 /* Copy the value. */
3152 val = BINDING_VALUE (new);
3153 if (val)
3154 switch (TREE_CODE (val))
3156 case TEMPLATE_DECL:
3157 /* If we expect types or namespaces, and not templates,
3158 or this is not a template class. */
3159 if (LOOKUP_QUALIFIERS_ONLY (flags)
3160 && !DECL_CLASS_TEMPLATE_P (val))
3161 val = NULL_TREE;
3162 break;
3163 case TYPE_DECL:
3164 if (LOOKUP_NAMESPACES_ONLY (flags))
3165 val = NULL_TREE;
3166 break;
3167 case NAMESPACE_DECL:
3168 if (LOOKUP_TYPES_ONLY (flags))
3169 val = NULL_TREE;
3170 break;
3171 case FUNCTION_DECL:
3172 /* Ignore built-in functions that are still anticipated. */
3173 if (LOOKUP_QUALIFIERS_ONLY (flags) || DECL_ANTICIPATED (val))
3174 val = NULL_TREE;
3175 break;
3176 default:
3177 if (LOOKUP_QUALIFIERS_ONLY (flags))
3178 val = NULL_TREE;
3181 if (!BINDING_VALUE (old))
3182 BINDING_VALUE (old) = val;
3183 else if (val && val != BINDING_VALUE (old))
3185 if (is_overloaded_fn (BINDING_VALUE (old)) && is_overloaded_fn (val))
3186 BINDING_VALUE (old) = merge_functions (BINDING_VALUE (old), val);
3187 else
3189 /* Some declarations are functions, some are not. */
3190 if (flags & LOOKUP_COMPLAIN)
3192 /* If we've already given this error for this lookup,
3193 BINDING_VALUE (old) is error_mark_node, so let's not
3194 repeat ourselves. */
3195 if (BINDING_VALUE (old) != error_mark_node)
3197 error ("use of `%D' is ambiguous", name);
3198 cp_error_at (" first declared as `%#D' here",
3199 BINDING_VALUE (old));
3201 cp_error_at (" also declared as `%#D' here", val);
3203 BINDING_VALUE (old) = error_mark_node;
3206 /* ... and copy the type. */
3207 type = BINDING_TYPE (new);
3208 if (LOOKUP_NAMESPACES_ONLY (flags))
3209 type = NULL_TREE;
3210 if (!BINDING_TYPE (old))
3211 BINDING_TYPE (old) = type;
3212 else if (type && BINDING_TYPE (old) != type)
3214 if (flags & LOOKUP_COMPLAIN)
3216 error ("`%D' denotes an ambiguous type",name);
3217 error ("%H first type here",
3218 &DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (BINDING_TYPE (old))));
3219 error ("%H other type here",
3220 &DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)));
3223 return old;
3226 /* Subroutine of unualified_namespace_lookup:
3227 Add the bindings of NAME in used namespaces to VAL.
3228 We are currently looking for names in namespace SCOPE, so we
3229 look through USINGS for using-directives of namespaces
3230 which have SCOPE as a common ancestor with the current scope.
3231 Returns false on errors. */
3233 bool
3234 lookup_using_namespace (tree name, cxx_binding *val, tree usings, tree scope,
3235 int flags, tree *spacesp)
3237 tree iter;
3238 timevar_push (TV_NAME_LOOKUP);
3239 /* Iterate over all used namespaces in current, searching for using
3240 directives of scope. */
3241 for (iter = usings; iter; iter = TREE_CHAIN (iter))
3242 if (TREE_VALUE (iter) == scope)
3244 tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
3245 cxx_binding *val1 =
3246 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (used), name);
3247 if (spacesp)
3248 *spacesp = tree_cons (used, NULL_TREE, *spacesp);
3249 /* Resolve ambiguities. */
3250 if (val1)
3251 val = ambiguous_decl (name, val, val1, flags);
3253 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
3254 BINDING_VALUE (val) != error_mark_node);
3257 /* [namespace.qual]
3258 Accepts the NAME to lookup and its qualifying SCOPE.
3259 Returns the name/type pair found into the cxx_binding *RESULT,
3260 or false on error. */
3262 bool
3263 qualified_lookup_using_namespace (tree name, tree scope, cxx_binding *result,
3264 int flags)
3266 /* Maintain a list of namespaces visited... */
3267 tree seen = NULL_TREE;
3268 /* ... and a list of namespace yet to see. */
3269 tree todo = NULL_TREE;
3270 tree usings;
3271 timevar_push (TV_NAME_LOOKUP);
3272 /* Look through namespace aliases. */
3273 scope = ORIGINAL_NAMESPACE (scope);
3274 while (scope && result->value != error_mark_node)
3276 cxx_binding *binding =
3277 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3278 seen = tree_cons (scope, NULL_TREE, seen);
3279 if (binding)
3280 result = ambiguous_decl (name, result, binding, flags);
3281 if (!BINDING_VALUE (result) && !BINDING_TYPE (result))
3282 /* Consider using directives. */
3283 for (usings = DECL_NAMESPACE_USING (scope); usings;
3284 usings = TREE_CHAIN (usings))
3285 /* If this was a real directive, and we have not seen it. */
3286 if (!TREE_INDIRECT_USING (usings)
3287 && !purpose_member (TREE_PURPOSE (usings), seen))
3288 todo = tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo);
3289 if (todo)
3291 scope = TREE_PURPOSE (todo);
3292 todo = TREE_CHAIN (todo);
3294 else
3295 scope = NULL_TREE; /* If there never was a todo list. */
3297 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result->value != error_mark_node);
3300 /* [namespace.memdef]/2 */
3302 /* Set the context of a declaration to scope. Complain if we are not
3303 outside scope. */
3305 void
3306 set_decl_namespace (tree decl, tree scope, bool friendp)
3308 tree old;
3310 /* Get rid of namespace aliases. */
3311 scope = ORIGINAL_NAMESPACE (scope);
3313 /* It is ok for friends to be qualified in parallel space. */
3314 if (!friendp && !is_ancestor (current_namespace, scope))
3315 error ("declaration of `%D' not in a namespace surrounding `%D'",
3316 decl, scope);
3317 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3318 if (scope != current_namespace)
3320 /* See whether this has been declared in the namespace. */
3321 old = namespace_binding (DECL_NAME (decl), scope);
3322 if (!old)
3323 /* No old declaration at all. */
3324 goto complain;
3325 /* A template can be explicitly specialized in any namespace. */
3326 if (processing_explicit_instantiation)
3327 return;
3328 if (!is_overloaded_fn (decl))
3329 /* Don't compare non-function decls with decls_match here,
3330 since it can't check for the correct constness at this
3331 point. pushdecl will find those errors later. */
3332 return;
3333 /* Since decl is a function, old should contain a function decl. */
3334 if (!is_overloaded_fn (old))
3335 goto complain;
3336 if (processing_template_decl || processing_specialization)
3337 /* We have not yet called push_template_decl to turn a
3338 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations
3339 won't match. But, we'll check later, when we construct the
3340 template. */
3341 return;
3342 if (is_overloaded_fn (old))
3344 for (; old; old = OVL_NEXT (old))
3345 if (decls_match (decl, OVL_CURRENT (old)))
3346 return;
3348 else
3349 if (decls_match (decl, old))
3350 return;
3352 else
3353 return;
3354 complain:
3355 error ("`%D' should have been declared inside `%D'",
3356 decl, scope);
3359 /* Compute the namespace where a declaration is defined. */
3361 static tree
3362 decl_namespace (tree decl)
3364 timevar_push (TV_NAME_LOOKUP);
3365 if (TYPE_P (decl))
3366 decl = TYPE_STUB_DECL (decl);
3367 while (DECL_CONTEXT (decl))
3369 decl = DECL_CONTEXT (decl);
3370 if (TREE_CODE (decl) == NAMESPACE_DECL)
3371 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
3372 if (TYPE_P (decl))
3373 decl = TYPE_STUB_DECL (decl);
3374 my_friendly_assert (DECL_P (decl), 390);
3377 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, global_namespace);
3380 /* Return the namespace where the current declaration is declared. */
3382 tree
3383 current_decl_namespace (void)
3385 tree result;
3386 /* If we have been pushed into a different namespace, use it. */
3387 if (decl_namespace_list)
3388 return TREE_PURPOSE (decl_namespace_list);
3390 if (current_class_type)
3391 result = decl_namespace (TYPE_STUB_DECL (current_class_type));
3392 else if (current_function_decl)
3393 result = decl_namespace (current_function_decl);
3394 else
3395 result = current_namespace;
3396 return result;
3399 /* Temporarily set the namespace for the current declaration. */
3401 void
3402 push_decl_namespace (tree decl)
3404 if (TREE_CODE (decl) != NAMESPACE_DECL)
3405 decl = decl_namespace (decl);
3406 decl_namespace_list = tree_cons (ORIGINAL_NAMESPACE (decl),
3407 NULL_TREE, decl_namespace_list);
3410 void
3411 pop_decl_namespace (void)
3413 decl_namespace_list = TREE_CHAIN (decl_namespace_list);
3416 /* Enter a class or namespace scope. */
3418 void
3419 push_scope (tree t)
3421 if (TREE_CODE (t) == NAMESPACE_DECL)
3422 push_decl_namespace (t);
3423 else if CLASS_TYPE_P (t)
3424 push_nested_class (t);
3427 /* Leave scope pushed by push_scope. */
3429 void
3430 pop_scope (tree t)
3432 if (TREE_CODE (t) == NAMESPACE_DECL)
3433 pop_decl_namespace ();
3434 else if CLASS_TYPE_P (t)
3435 pop_nested_class ();
3438 /* [basic.lookup.koenig] */
3439 /* A nonzero return value in the functions below indicates an error. */
3441 struct arg_lookup
3443 tree name;
3444 tree namespaces;
3445 tree classes;
3446 tree functions;
3449 static bool arg_assoc (struct arg_lookup*, tree);
3450 static bool arg_assoc_args (struct arg_lookup*, tree);
3451 static bool arg_assoc_type (struct arg_lookup*, tree);
3452 static bool add_function (struct arg_lookup *, tree);
3453 static bool arg_assoc_namespace (struct arg_lookup *, tree);
3454 static bool arg_assoc_class (struct arg_lookup *, tree);
3455 static bool arg_assoc_template_arg (struct arg_lookup*, tree);
3457 /* Add a function to the lookup structure.
3458 Returns true on error. */
3460 static bool
3461 add_function (struct arg_lookup *k, tree fn)
3463 /* We used to check here to see if the function was already in the list,
3464 but that's O(n^2), which is just too expensive for function lookup.
3465 Now we deal with the occasional duplicate in joust. In doing this, we
3466 assume that the number of duplicates will be small compared to the
3467 total number of functions being compared, which should usually be the
3468 case. */
3470 /* We must find only functions, or exactly one non-function. */
3471 if (!k->functions)
3472 k->functions = fn;
3473 else if (is_overloaded_fn (k->functions) && is_overloaded_fn (fn))
3474 k->functions = build_overload (fn, k->functions);
3475 else
3477 tree f1 = OVL_CURRENT (k->functions);
3478 tree f2 = fn;
3479 if (is_overloaded_fn (f1))
3481 fn = f1; f1 = f2; f2 = fn;
3483 cp_error_at ("`%D' is not a function,", f1);
3484 cp_error_at (" conflict with `%D'", f2);
3485 error (" in call to `%D'", k->name);
3486 return true;
3489 return false;
3492 /* Add functions of a namespace to the lookup structure.
3493 Returns true on error. */
3495 static bool
3496 arg_assoc_namespace (struct arg_lookup *k, tree scope)
3498 tree value;
3500 if (purpose_member (scope, k->namespaces))
3501 return 0;
3502 k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces);
3504 value = namespace_binding (k->name, scope);
3505 if (!value)
3506 return false;
3508 for (; value; value = OVL_NEXT (value))
3509 if (add_function (k, OVL_CURRENT (value)))
3510 return true;
3512 return false;
3515 /* Adds everything associated with a template argument to the lookup
3516 structure. Returns true on error. */
3518 static bool
3519 arg_assoc_template_arg (struct arg_lookup *k, tree arg)
3521 /* [basic.lookup.koenig]
3523 If T is a template-id, its associated namespaces and classes are
3524 ... the namespaces and classes associated with the types of the
3525 template arguments provided for template type parameters
3526 (excluding template template parameters); the namespaces in which
3527 any template template arguments are defined; and the classes in
3528 which any member templates used as template template arguments
3529 are defined. [Note: non-type template arguments do not
3530 contribute to the set of associated namespaces. ] */
3532 /* Consider first template template arguments. */
3533 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3534 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
3535 return false;
3536 else if (TREE_CODE (arg) == TEMPLATE_DECL)
3538 tree ctx = CP_DECL_CONTEXT (arg);
3540 /* It's not a member template. */
3541 if (TREE_CODE (ctx) == NAMESPACE_DECL)
3542 return arg_assoc_namespace (k, ctx);
3543 /* Otherwise, it must be member template. */
3544 else
3545 return arg_assoc_class (k, ctx);
3547 /* It's not a template template argument, but it is a type template
3548 argument. */
3549 else if (TYPE_P (arg))
3550 return arg_assoc_type (k, arg);
3551 /* It's a non-type template argument. */
3552 else
3553 return false;
3556 /* Adds everything associated with class to the lookup structure.
3557 Returns true on error. */
3559 static bool
3560 arg_assoc_class (struct arg_lookup *k, tree type)
3562 tree list, friends, context;
3563 int i;
3565 /* Backend build structures, such as __builtin_va_list, aren't
3566 affected by all this. */
3567 if (!CLASS_TYPE_P (type))
3568 return false;
3570 if (purpose_member (type, k->classes))
3571 return false;
3572 k->classes = tree_cons (type, NULL_TREE, k->classes);
3574 context = decl_namespace (TYPE_MAIN_DECL (type));
3575 if (arg_assoc_namespace (k, context))
3576 return true;
3578 /* Process baseclasses. */
3579 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); i++)
3580 if (arg_assoc_class (k, TYPE_BINFO_BASETYPE (type, i)))
3581 return true;
3583 /* Process friends. */
3584 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
3585 list = TREE_CHAIN (list))
3586 if (k->name == FRIEND_NAME (list))
3587 for (friends = FRIEND_DECLS (list); friends;
3588 friends = TREE_CHAIN (friends))
3589 /* Only interested in global functions with potentially hidden
3590 (i.e. unqualified) declarations. */
3591 if (CP_DECL_CONTEXT (TREE_VALUE (friends)) == context)
3592 if (add_function (k, TREE_VALUE (friends)))
3593 return true;
3595 /* Process template arguments. */
3596 if (CLASSTYPE_TEMPLATE_INFO (type))
3598 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
3599 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
3600 arg_assoc_template_arg (k, TREE_VEC_ELT (list, i));
3603 return false;
3606 /* Adds everything associated with a given type.
3607 Returns 1 on error. */
3609 static bool
3610 arg_assoc_type (struct arg_lookup *k, tree type)
3612 /* As we do not get the type of non-type dependent expressions
3613 right, we can end up with such things without a type. */
3614 if (!type)
3615 return false;
3617 if (TYPE_PTRMEM_P (type))
3619 /* Pointer to member: associate class type and value type. */
3620 if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type)))
3621 return true;
3622 return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type));
3624 else switch (TREE_CODE (type))
3626 case ERROR_MARK:
3627 return false;
3628 case VOID_TYPE:
3629 case INTEGER_TYPE:
3630 case REAL_TYPE:
3631 case COMPLEX_TYPE:
3632 case VECTOR_TYPE:
3633 case CHAR_TYPE:
3634 case BOOLEAN_TYPE:
3635 return false;
3636 case RECORD_TYPE:
3637 if (TYPE_PTRMEMFUNC_P (type))
3638 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
3639 return arg_assoc_class (k, type);
3640 case POINTER_TYPE:
3641 case REFERENCE_TYPE:
3642 case ARRAY_TYPE:
3643 return arg_assoc_type (k, TREE_TYPE (type));
3644 case UNION_TYPE:
3645 case ENUMERAL_TYPE:
3646 return arg_assoc_namespace (k, decl_namespace (TYPE_MAIN_DECL (type)));
3647 case METHOD_TYPE:
3648 /* The basetype is referenced in the first arg type, so just
3649 fall through. */
3650 case FUNCTION_TYPE:
3651 /* Associate the parameter types. */
3652 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
3653 return true;
3654 /* Associate the return type. */
3655 return arg_assoc_type (k, TREE_TYPE (type));
3656 case TEMPLATE_TYPE_PARM:
3657 case BOUND_TEMPLATE_TEMPLATE_PARM:
3658 return false;
3659 case TYPENAME_TYPE:
3660 return false;
3661 case LANG_TYPE:
3662 if (type == unknown_type_node)
3663 return false;
3664 /* else fall through */
3665 default:
3666 abort ();
3668 return false;
3671 /* Adds everything associated with arguments. Returns true on error. */
3673 static bool
3674 arg_assoc_args (struct arg_lookup *k, tree args)
3676 for (; args; args = TREE_CHAIN (args))
3677 if (arg_assoc (k, TREE_VALUE (args)))
3678 return true;
3679 return false;
3682 /* Adds everything associated with a given tree_node. Returns 1 on error. */
3684 static bool
3685 arg_assoc (struct arg_lookup *k, tree n)
3687 if (n == error_mark_node)
3688 return false;
3690 if (TYPE_P (n))
3691 return arg_assoc_type (k, n);
3693 if (! type_unknown_p (n))
3694 return arg_assoc_type (k, TREE_TYPE (n));
3696 if (TREE_CODE (n) == ADDR_EXPR)
3697 n = TREE_OPERAND (n, 0);
3698 if (TREE_CODE (n) == COMPONENT_REF)
3699 n = TREE_OPERAND (n, 1);
3700 if (TREE_CODE (n) == OFFSET_REF)
3701 n = TREE_OPERAND (n, 1);
3702 while (TREE_CODE (n) == TREE_LIST)
3703 n = TREE_VALUE (n);
3704 if (TREE_CODE (n) == BASELINK)
3705 n = BASELINK_FUNCTIONS (n);
3707 if (TREE_CODE (n) == FUNCTION_DECL)
3708 return arg_assoc_type (k, TREE_TYPE (n));
3709 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
3711 /* [basic.lookup.koenig]
3713 If T is a template-id, its associated namespaces and classes
3714 are the namespace in which the template is defined; for
3715 member templates, the member template's class... */
3716 tree template = TREE_OPERAND (n, 0);
3717 tree args = TREE_OPERAND (n, 1);
3718 tree ctx;
3719 int ix;
3721 if (TREE_CODE (template) == COMPONENT_REF)
3722 template = TREE_OPERAND (template, 1);
3724 /* First, the template. There may actually be more than one if
3725 this is an overloaded function template. But, in that case,
3726 we only need the first; all the functions will be in the same
3727 namespace. */
3728 template = OVL_CURRENT (template);
3730 ctx = CP_DECL_CONTEXT (template);
3732 if (TREE_CODE (ctx) == NAMESPACE_DECL)
3734 if (arg_assoc_namespace (k, ctx) == 1)
3735 return true;
3737 /* It must be a member template. */
3738 else if (arg_assoc_class (k, ctx) == 1)
3739 return true;
3741 /* Now the arguments. */
3742 for (ix = TREE_VEC_LENGTH (args); ix--;)
3743 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1)
3744 return true;
3746 else
3748 my_friendly_assert (TREE_CODE (n) == OVERLOAD, 980715);
3750 for (; n; n = OVL_CHAIN (n))
3751 if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
3752 return true;
3755 return false;
3758 /* Performs Koenig lookup depending on arguments, where fns
3759 are the functions found in normal lookup. */
3761 tree
3762 lookup_arg_dependent (tree name, tree fns, tree args)
3764 struct arg_lookup k;
3765 tree fn = NULL_TREE;
3767 timevar_push (TV_NAME_LOOKUP);
3768 k.name = name;
3769 k.functions = fns;
3770 k.classes = NULL_TREE;
3772 /* Note that we've already looked at some namespaces during normal
3773 unqualified lookup, unless we found a decl in function scope. */
3774 if (fns)
3775 fn = OVL_CURRENT (fns);
3776 if (fn && TREE_CODE (fn) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (fn))
3777 k.namespaces = NULL_TREE;
3778 else
3779 unqualified_namespace_lookup (name, 0, &k.namespaces);
3781 arg_assoc_args (&k, args);
3782 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, k.functions);
3785 /* Process a namespace-alias declaration. */
3787 void
3788 do_namespace_alias (tree alias, tree namespace)
3790 if (TREE_CODE (namespace) != NAMESPACE_DECL)
3792 /* The parser did not find it, so it's not there. */
3793 error ("unknown namespace `%D'", namespace);
3794 return;
3797 namespace = ORIGINAL_NAMESPACE (namespace);
3799 /* Build the alias. */
3800 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
3801 DECL_NAMESPACE_ALIAS (alias) = namespace;
3802 DECL_EXTERNAL (alias) = 1;
3803 pushdecl (alias);
3806 /* Check a non-member using-declaration. Return the name and scope
3807 being used, and the USING_DECL, or NULL_TREE on failure. */
3809 static tree
3810 validate_nonmember_using_decl (tree decl, tree *scope, tree *name)
3812 *scope = global_namespace;
3813 *name = NULL_TREE;
3815 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
3817 *name = TREE_OPERAND (decl, 0);
3818 /* 7.3.3/5
3819 A using-declaration shall not name a template-id. */
3820 error ("a using-declaration cannot specify a template-id. Try `using %D'", *name);
3821 return NULL_TREE;
3824 if (TREE_CODE (decl) == NAMESPACE_DECL)
3826 error ("namespace `%D' not allowed in using-declaration", decl);
3827 return NULL_TREE;
3830 if (TREE_CODE (decl) == SCOPE_REF)
3832 /* It's a nested name with template parameter dependent scope.
3833 This can only be using-declaration for class member. */
3834 error ("`%T' is not a namespace", TREE_OPERAND (decl, 0));
3835 return NULL_TREE;
3838 if (is_overloaded_fn (decl))
3839 decl = get_first_fn (decl);
3841 my_friendly_assert (DECL_P (decl), 20020908);
3843 if (TREE_CODE (decl) == CONST_DECL)
3844 /* Enumeration constants to not have DECL_CONTEXT set. */
3845 *scope = TYPE_CONTEXT (TREE_TYPE (decl));
3846 else
3847 *scope = DECL_CONTEXT (decl);
3848 if (!*scope)
3849 *scope = global_namespace;
3851 /* [namespace.udecl]
3852 A using-declaration for a class member shall be a
3853 member-declaration. */
3854 if (TYPE_P (*scope))
3856 error ("`%T' is not a namespace", *scope);
3857 return NULL_TREE;
3859 *name = DECL_NAME (decl);
3860 /* Make a USING_DECL. */
3861 return push_using_decl (*scope, *name);
3864 /* Process local and global using-declarations. */
3866 static void
3867 do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
3868 tree *newval, tree *newtype)
3870 cxx_binding decls;
3872 *newval = *newtype = NULL_TREE;
3873 cxx_binding_clear (&decls);
3874 if (!qualified_lookup_using_namespace (name, scope, &decls, 0))
3875 /* Lookup error */
3876 return;
3878 if (!decls.value && !decls.type)
3880 error ("`%D' not declared", name);
3881 return;
3884 /* Check for using functions. */
3885 if (decls.value && is_overloaded_fn (decls.value))
3887 tree tmp, tmp1;
3889 if (oldval && !is_overloaded_fn (oldval))
3891 if (!DECL_IMPLICIT_TYPEDEF_P (oldval))
3892 error ("`%D' is already declared in this scope", name);
3893 oldval = NULL_TREE;
3896 *newval = oldval;
3897 for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
3899 tree new_fn = OVL_CURRENT (tmp);
3901 /* [namespace.udecl]
3903 If a function declaration in namespace scope or block
3904 scope has the same name and the same parameter types as a
3905 function introduced by a using declaration the program is
3906 ill-formed. */
3907 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
3909 tree old_fn = OVL_CURRENT (tmp1);
3911 if (new_fn == old_fn)
3912 /* The function already exists in the current namespace. */
3913 break;
3914 else if (OVL_USED (tmp1))
3915 continue; /* this is a using decl */
3916 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
3917 TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
3919 /* There was already a non-using declaration in
3920 this scope with the same parameter types. If both
3921 are the same extern "C" functions, that's ok. */
3922 if (decls_match (new_fn, old_fn))
3924 /* If the OLD_FN was a builtin, there is now a
3925 real declaration. */
3926 if (DECL_ANTICIPATED (old_fn))
3927 DECL_ANTICIPATED (old_fn) = 0;
3928 break;
3930 else if (!DECL_ANTICIPATED (old_fn))
3932 /* If the OLD_FN was really declared, the
3933 declarations don't match. */
3934 error ("`%D' is already declared in this scope", name);
3935 break;
3938 /* If the OLD_FN was not really there, just ignore
3939 it and keep going. */
3943 /* If we broke out of the loop, there's no reason to add
3944 this function to the using declarations for this
3945 scope. */
3946 if (tmp1)
3947 continue;
3949 *newval = build_overload (OVL_CURRENT (tmp), *newval);
3950 if (TREE_CODE (*newval) != OVERLOAD)
3951 *newval = ovl_cons (*newval, NULL_TREE);
3952 OVL_USED (*newval) = 1;
3955 else
3957 *newval = decls.value;
3958 if (oldval && !decls_match (*newval, oldval))
3959 error ("`%D' is already declared in this scope", name);
3962 *newtype = decls.type;
3963 if (oldtype && *newtype && !same_type_p (oldtype, *newtype))
3965 error ("using declaration `%D' introduced ambiguous type `%T'",
3966 name, oldtype);
3967 return;
3971 /* Process a using-declaration not appearing in class or local scope. */
3973 void
3974 do_toplevel_using_decl (tree decl)
3976 tree scope, name;
3977 tree oldval, oldtype, newval, newtype;
3978 cxx_binding *binding;
3980 decl = validate_nonmember_using_decl (decl, &scope, &name);
3981 if (decl == NULL_TREE)
3982 return;
3984 binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name);
3986 oldval = BINDING_VALUE (binding);
3987 oldtype = BINDING_TYPE (binding);
3989 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
3991 /* Copy declarations found. */
3992 if (newval)
3993 BINDING_VALUE (binding) = newval;
3994 if (newtype)
3995 BINDING_TYPE (binding) = newtype;
3996 return;
3999 /* Process a using-declaration at function scope. */
4001 void
4002 do_local_using_decl (tree decl)
4004 tree scope, name;
4005 tree oldval, oldtype, newval, newtype;
4007 decl = validate_nonmember_using_decl (decl, &scope, &name);
4008 if (decl == NULL_TREE)
4009 return;
4011 if (building_stmt_tree ()
4012 && at_function_scope_p ())
4013 add_decl_stmt (decl);
4015 oldval = lookup_name_current_level (name);
4016 oldtype = lookup_type_current_level (name);
4018 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
4020 if (newval)
4022 if (is_overloaded_fn (newval))
4024 tree fn, term;
4026 /* We only need to push declarations for those functions
4027 that were not already bound in the current level.
4028 The old value might be NULL_TREE, it might be a single
4029 function, or an OVERLOAD. */
4030 if (oldval && TREE_CODE (oldval) == OVERLOAD)
4031 term = OVL_FUNCTION (oldval);
4032 else
4033 term = oldval;
4034 for (fn = newval; fn && OVL_CURRENT (fn) != term;
4035 fn = OVL_NEXT (fn))
4036 push_overloaded_decl (OVL_CURRENT (fn),
4037 PUSH_LOCAL | PUSH_USING);
4039 else
4040 push_local_binding (name, newval, PUSH_USING);
4042 if (newtype)
4043 set_identifier_type_value (name, newtype);
4046 tree
4047 do_class_using_decl (tree decl)
4049 tree name, value, scope, type;
4051 if (TREE_CODE (decl) != SCOPE_REF
4052 || !TREE_OPERAND (decl, 0)
4053 || !TYPE_P (TREE_OPERAND (decl, 0)))
4055 error ("using-declaration for non-member at class scope");
4056 return NULL_TREE;
4058 scope = TREE_OPERAND (decl, 0);
4059 name = TREE_OPERAND (decl, 1);
4060 if (TREE_CODE (name) == BIT_NOT_EXPR)
4062 error ("using-declaration cannot name destructor");
4063 return NULL_TREE;
4065 else if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4067 template_id_error:;
4069 error ("a using-declaration cannot specify a template-id");
4070 return NULL_TREE;
4072 if (TREE_CODE (name) == TYPE_DECL)
4074 if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (name)))
4075 goto template_id_error;
4076 name = DECL_NAME (name);
4078 else if (TREE_CODE (name) == TEMPLATE_DECL)
4079 name = DECL_NAME (name);
4080 else if (BASELINK_P (name))
4082 tree fns = BASELINK_FUNCTIONS (name);
4084 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
4085 goto template_id_error;
4086 name = DECL_NAME (get_first_fn (fns));
4089 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 980716);
4091 /* Dependent using decls have a NULL type, non-dependent ones have a
4092 void type. */
4093 type = dependent_type_p (scope) ? NULL_TREE : void_type_node;
4094 value = build_lang_decl (USING_DECL, name, type);
4095 DECL_INITIAL (value) = scope;
4096 return value;
4099 /* Process a using-directive. */
4101 void
4102 do_using_directive (tree namespace)
4104 if (building_stmt_tree ())
4105 add_stmt (build_stmt (USING_STMT, namespace));
4107 /* using namespace A::B::C; */
4108 if (TREE_CODE (namespace) == SCOPE_REF)
4109 namespace = TREE_OPERAND (namespace, 1);
4110 if (TREE_CODE (namespace) == IDENTIFIER_NODE)
4112 /* Lookup in lexer did not find a namespace. */
4113 if (!processing_template_decl)
4114 error ("namespace `%T' undeclared", namespace);
4115 return;
4117 if (TREE_CODE (namespace) != NAMESPACE_DECL)
4119 if (!processing_template_decl)
4120 error ("`%T' is not a namespace", namespace);
4121 return;
4123 namespace = ORIGINAL_NAMESPACE (namespace);
4124 if (!toplevel_bindings_p ())
4125 push_using_directive (namespace);
4126 else
4127 /* direct usage */
4128 add_using_namespace (current_namespace, namespace, 0);
4131 void
4132 check_default_args (tree x)
4134 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4135 bool saw_def = false;
4136 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4137 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4139 if (TREE_PURPOSE (arg))
4140 saw_def = true;
4141 else if (saw_def)
4143 cp_error_at ("default argument missing for parameter %P of `%+#D'",
4144 i, x);
4145 break;
4150 void
4151 mark_used (tree decl)
4153 TREE_USED (decl) = 1;
4154 if (processing_template_decl || skip_evaluation)
4155 return;
4157 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
4158 && !TREE_ASM_WRITTEN (decl))
4159 /* Remember it, so we can check it was defined. */
4160 defer_fn (decl);
4162 assemble_external (decl);
4164 /* Is it a synthesized method that needs to be synthesized? */
4165 if (TREE_CODE (decl) == FUNCTION_DECL
4166 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
4167 && DECL_ARTIFICIAL (decl)
4168 && !DECL_THUNK_P (decl)
4169 && ! DECL_INITIAL (decl)
4170 /* Kludge: don't synthesize for default args. */
4171 && current_function_decl)
4173 synthesize_method (decl);
4174 /* If we've already synthesized the method we don't need to
4175 instantiate it, so we can return right away. */
4176 return;
4179 /* If this is a function or variable that is an instance of some
4180 template, we now know that we will need to actually do the
4181 instantiation. We check that DECL is not an explicit
4182 instantiation because that is not checked in instantiate_decl. */
4183 if ((DECL_NON_THUNK_FUNCTION_P (decl) || TREE_CODE (decl) == VAR_DECL)
4184 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
4185 && (!DECL_EXPLICIT_INSTANTIATION (decl)
4186 || (TREE_CODE (decl) == FUNCTION_DECL
4187 && DECL_INLINE (DECL_TEMPLATE_RESULT
4188 (template_for_substitution (decl))))))
4190 bool defer;
4192 /* Normally, we put off instantiating functions in order to
4193 improve compile times. Maintaining a stack of active
4194 functions is expensive, and the inliner knows to
4195 instantiate any functions it might need.
4197 However, if instantiating this function might help us mark
4198 the current function TREE_NOTHROW, we go ahead and
4199 instantiate it now. */
4200 defer = (!flag_exceptions
4201 || TREE_CODE (decl) != FUNCTION_DECL
4202 /* If the called function can't throw, we don't need to
4203 generate its body to find that out. */
4204 || TREE_NOTHROW (decl)
4205 || !cfun
4206 /* If we already know the current function can't throw,
4207 then we don't need to work hard to prove it. */
4208 || TREE_NOTHROW (current_function_decl)
4209 /* If we already know that the current function *can*
4210 throw, there's no point in gathering more
4211 information. */
4212 || cp_function_chain->can_throw);
4214 instantiate_decl (decl, defer);
4218 /* Called when a class-head is encountered. TAG_KIND is the class-key
4219 for the class. SCOPE, if non-NULL, is the type or namespace
4220 indicated in the nested-name-specifier for the declaration of the
4221 class. ID is the name of the class, if any; it may be a TYPE_DECL,
4222 or an IDENTIFIER_NODE. ATTRIBUTES are attributes that apply to the
4223 class.
4225 Return a TYPE_DECL for the class being defined. */
4227 tree
4228 handle_class_head (enum tag_types tag_kind, tree scope, tree id,
4229 tree attributes)
4231 tree decl = NULL_TREE;
4232 tree current = current_scope ();
4233 bool xrefd_p = false;
4234 bool new_type_p;
4235 tree context;
4237 if (current == NULL_TREE)
4238 current = current_namespace;
4240 if (scope)
4242 if (TREE_CODE (id) == TYPE_DECL)
4243 /* We must bash typedefs back to the main decl of the
4244 type. Otherwise we become confused about scopes. */
4245 decl = TYPE_MAIN_DECL (TREE_TYPE (id));
4246 else if (DECL_CLASS_TEMPLATE_P (id))
4247 decl = DECL_TEMPLATE_RESULT (id);
4248 else
4250 if (TYPE_P (scope))
4252 /* According to the suggested resolution of core issue
4253 180, 'typename' is assumed after a class-key. */
4254 decl = make_typename_type (scope, id, tf_error);
4255 if (decl != error_mark_node)
4256 decl = TYPE_MAIN_DECL (decl);
4257 else
4258 decl = NULL_TREE;
4260 else if (scope == current)
4262 /* We've been given AGGR SCOPE::ID, when we're already
4263 inside SCOPE. Be nice about it. */
4264 if (pedantic)
4265 pedwarn ("extra qualification `%T::' on member `%D' ignored",
4266 scope, id);
4268 else
4269 error ("`%T' does not have a class or union named `%D'",
4270 scope, id);
4274 if (!decl)
4276 decl = xref_tag (tag_kind, id, attributes, false, false);
4277 if (decl == error_mark_node)
4278 return error_mark_node;
4279 decl = TYPE_MAIN_DECL (decl);
4280 xrefd_p = true;
4283 if (!TYPE_BINFO (TREE_TYPE (decl)))
4285 error ("`%T' is not a class or union type", decl);
4286 return error_mark_node;
4289 /* For a definition, we want to enter the containing scope before
4290 looking up any base classes etc. Only do so, if this is different
4291 to the current scope. */
4292 context = CP_DECL_CONTEXT (decl);
4294 new_type_p = (current != context
4295 && TREE_CODE (context) != TEMPLATE_TYPE_PARM
4296 && TREE_CODE (context) != BOUND_TEMPLATE_TEMPLATE_PARM);
4297 if (new_type_p)
4298 push_scope (context);
4300 if (!xrefd_p
4301 && PROCESSING_REAL_TEMPLATE_DECL_P ()
4302 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
4303 decl = push_template_decl (decl);
4305 return decl;
4308 #include "gt-cp-decl2.h"