cp:
[official-gcc.git] / gcc / cp / decl2.c
blobab29aeed45d1dd275189e067105e6344c348d483
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 = TREE_TYPE (array_expr);
402 tree p1, p2, i1, i2;
404 if (type == error_mark_node || index_exp == error_mark_node)
405 return error_mark_node;
406 if (processing_template_decl)
407 return build_min (ARRAY_REF, type ? TREE_TYPE (type) : NULL_TREE,
408 array_expr, index_exp);
410 if (type == NULL_TREE)
412 /* Something has gone very wrong. Assume we are mistakenly reducing
413 an expression instead of a declaration. */
414 error ("parser may be lost: is there a '{' missing somewhere?");
415 return NULL_TREE;
418 if (TREE_CODE (type) == OFFSET_TYPE
419 || TREE_CODE (type) == REFERENCE_TYPE)
420 type = TREE_TYPE (type);
422 /* If they have an `operator[]', use that. */
423 if (IS_AGGR_TYPE (type) || IS_AGGR_TYPE (TREE_TYPE (index_exp)))
424 return build_new_op (ARRAY_REF, LOOKUP_NORMAL,
425 array_expr, index_exp, NULL_TREE);
427 /* Otherwise, create an ARRAY_REF for a pointer or array type. It
428 is a little-known fact that, if `a' is an array and `i' is an
429 int, you can write `i[a]', which means the same thing as `a[i]'. */
431 if (TREE_CODE (type) == ARRAY_TYPE)
432 p1 = array_expr;
433 else
434 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
436 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
437 p2 = index_exp;
438 else
439 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
441 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr, false);
442 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp, false);
444 if ((p1 && i2) && (i1 && p2))
445 error ("ambiguous conversion for array subscript");
447 if (p1 && i2)
448 array_expr = p1, index_exp = i2;
449 else if (i1 && p2)
450 array_expr = p2, index_exp = i1;
451 else
453 error ("invalid types `%T[%T]' for array subscript",
454 type, TREE_TYPE (index_exp));
455 return error_mark_node;
458 if (array_expr == error_mark_node || index_exp == error_mark_node)
459 error ("ambiguous conversion for array subscript");
461 return build_array_ref (array_expr, index_exp);
464 /* Given the cast expression EXP, checking out its validity. Either return
465 an error_mark_node if there was an unavoidable error, return a cast to
466 void for trying to delete a pointer w/ the value 0, or return the
467 call to delete. If DOING_VEC is 1, we handle things differently
468 for doing an array delete. If DOING_VEC is 2, they gave us the
469 array size as an argument to delete.
470 Implements ARM $5.3.4. This is called from the parser. */
472 tree
473 delete_sanity (tree exp, tree size, int doing_vec, int use_global_delete)
475 tree t, type;
476 /* For a regular vector delete (aka, no size argument) we will pass
477 this down as a NULL_TREE into build_vec_delete. */
478 tree maxindex = NULL_TREE;
480 if (exp == error_mark_node)
481 return exp;
483 if (processing_template_decl)
485 t = build_min (DELETE_EXPR, void_type_node, exp, size);
486 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
487 DELETE_EXPR_USE_VEC (t) = doing_vec;
488 return t;
491 if (TREE_CODE (exp) == OFFSET_REF)
492 exp = resolve_offset_ref (exp);
493 exp = convert_from_reference (exp);
494 t = build_expr_type_conversion (WANT_POINTER, exp, true);
496 if (t == NULL_TREE || t == error_mark_node)
498 error ("type `%#T' argument given to `delete', expected pointer",
499 TREE_TYPE (exp));
500 return error_mark_node;
503 if (doing_vec == 2)
505 maxindex = cp_build_binary_op (MINUS_EXPR, size, integer_one_node);
506 pedwarn ("anachronistic use of array size in vector delete");
509 type = TREE_TYPE (t);
511 /* As of Valley Forge, you can delete a pointer to const. */
513 /* You can't delete functions. */
514 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
516 error ("cannot delete a function. Only pointer-to-objects are valid arguments to `delete'");
517 return error_mark_node;
520 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
521 if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
523 warning ("deleting `%T' is undefined", type);
524 doing_vec = 0;
527 /* An array can't have been allocated by new, so complain. */
528 if (TREE_CODE (t) == ADDR_EXPR
529 && TREE_CODE (TREE_OPERAND (t, 0)) == VAR_DECL
530 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == ARRAY_TYPE)
531 warning ("deleting array `%#D'", TREE_OPERAND (t, 0));
533 /* Deleting a pointer with the value zero is valid and has no effect. */
534 if (integer_zerop (t))
535 return build1 (NOP_EXPR, void_type_node, t);
537 if (doing_vec)
538 return build_vec_delete (t, maxindex, sfk_deleting_destructor,
539 use_global_delete);
540 else
541 return build_delete (type, t, sfk_deleting_destructor,
542 LOOKUP_NORMAL, use_global_delete);
545 /* Report an error if the indicated template declaration is not the
546 sort of thing that should be a member template. */
548 void
549 check_member_template (tree tmpl)
551 tree decl;
553 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
554 decl = DECL_TEMPLATE_RESULT (tmpl);
556 if (TREE_CODE (decl) == FUNCTION_DECL
557 || (TREE_CODE (decl) == TYPE_DECL
558 && IS_AGGR_TYPE (TREE_TYPE (decl))))
560 if (current_function_decl)
561 /* 14.5.2.2 [temp.mem]
563 A local class shall not have member templates. */
564 error ("invalid declaration of member template `%#D' in local class",
565 decl);
567 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
569 /* 14.5.2.3 [temp.mem]
571 A member function template shall not be virtual. */
572 error
573 ("invalid use of `virtual' in template declaration of `%#D'",
574 decl);
575 DECL_VIRTUAL_P (decl) = 0;
578 /* The debug-information generating code doesn't know what to do
579 with member templates. */
580 DECL_IGNORED_P (tmpl) = 1;
582 else
583 error ("template declaration of `%#D'", decl);
586 /* Return true iff TYPE is a valid Java parameter or return type. */
588 static bool
589 acceptable_java_type (tree type)
591 if (TREE_CODE (type) == VOID_TYPE || TYPE_FOR_JAVA (type))
592 return 1;
593 if (TREE_CODE (type) == POINTER_TYPE || TREE_CODE (type) == REFERENCE_TYPE)
595 type = TREE_TYPE (type);
596 if (TREE_CODE (type) == RECORD_TYPE)
598 tree args; int i;
599 if (! TYPE_FOR_JAVA (type))
600 return false;
601 if (! CLASSTYPE_TEMPLATE_INFO (type))
602 return true;
603 args = CLASSTYPE_TI_ARGS (type);
604 i = TREE_VEC_LENGTH (args);
605 while (--i >= 0)
607 type = TREE_VEC_ELT (args, i);
608 if (TREE_CODE (type) == POINTER_TYPE)
609 type = TREE_TYPE (type);
610 if (! TYPE_FOR_JAVA (type))
611 return false;
613 return true;
616 return false;
619 /* For a METHOD in a Java class CTYPE, return true if
620 the parameter and return types are valid Java types.
621 Otherwise, print appropriate error messages, and return false. */
623 bool
624 check_java_method (tree method)
626 bool jerr = false;
627 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
628 tree ret_type = TREE_TYPE (TREE_TYPE (method));
629 if (!acceptable_java_type (ret_type))
631 error ("Java method '%D' has non-Java return type `%T'",
632 method, ret_type);
633 jerr = true;
635 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
637 tree type = TREE_VALUE (arg_types);
638 if (!acceptable_java_type (type))
640 error ("Java method '%D' has non-Java parameter type `%T'",
641 method, type);
642 jerr = true;
645 return !jerr;
648 /* Sanity check: report error if this function FUNCTION is not
649 really a member of the class (CTYPE) it is supposed to belong to.
650 CNAME is the same here as it is for grokclassfn above. */
652 tree
653 check_classfn (tree ctype, tree function)
655 int ix;
656 int is_template;
658 if (DECL_USE_TEMPLATE (function)
659 && !(TREE_CODE (function) == TEMPLATE_DECL
660 && DECL_TEMPLATE_SPECIALIZATION (function))
661 && is_member_template (DECL_TI_TEMPLATE (function)))
662 /* Since this is a specialization of a member template,
663 we're not going to find the declaration in the class.
664 For example, in:
666 struct S { template <typename T> void f(T); };
667 template <> void S::f(int);
669 we're not going to find `S::f(int)', but there's no
670 reason we should, either. We let our callers know we didn't
671 find the method, but we don't complain. */
672 return NULL_TREE;
674 /* OK, is this a definition of a member template? */
675 is_template = (TREE_CODE (function) == TEMPLATE_DECL
676 || (processing_template_decl - template_class_depth (ctype)));
678 ix = lookup_fnfields_1 (complete_type (ctype),
679 DECL_CONSTRUCTOR_P (function) ? ctor_identifier :
680 DECL_DESTRUCTOR_P (function) ? dtor_identifier :
681 DECL_NAME (function));
683 if (ix >= 0)
685 tree methods = CLASSTYPE_METHOD_VEC (ctype);
686 tree fndecls, fndecl = 0;
687 bool is_conv_op;
688 const char *format = NULL;
690 push_scope (ctype);
691 for (fndecls = TREE_VEC_ELT (methods, ix);
692 fndecls; fndecls = OVL_NEXT (fndecls))
694 tree p1, p2;
696 fndecl = OVL_CURRENT (fndecls);
697 p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
698 p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
700 /* We cannot simply call decls_match because this doesn't
701 work for static member functions that are pretending to
702 be methods, and because the name may have been changed by
703 asm("new_name"). */
705 /* Get rid of the this parameter on functions that become
706 static. */
707 if (DECL_STATIC_FUNCTION_P (fndecl)
708 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
709 p1 = TREE_CHAIN (p1);
711 /* A member template definition only matches a member template
712 declaration. */
713 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
714 continue;
716 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
717 TREE_TYPE (TREE_TYPE (fndecl)))
718 && compparms (p1, p2)
719 && (DECL_TEMPLATE_SPECIALIZATION (function)
720 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
721 && (!DECL_TEMPLATE_SPECIALIZATION (function)
722 || (DECL_TI_TEMPLATE (function)
723 == DECL_TI_TEMPLATE (fndecl))))
724 break;
726 pop_scope (ctype);
727 if (fndecls)
728 return OVL_CURRENT (fndecls);
729 error ("prototype for `%#D' does not match any in class `%T'",
730 function, ctype);
731 is_conv_op = DECL_CONV_FN_P (fndecl);
733 if (is_conv_op)
734 ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
735 fndecls = TREE_VEC_ELT (methods, ix);
736 while (fndecls)
738 fndecl = OVL_CURRENT (fndecls);
739 fndecls = OVL_NEXT (fndecls);
741 if (!fndecls && is_conv_op)
743 if (TREE_VEC_LENGTH (methods) > ix)
745 ix++;
746 fndecls = TREE_VEC_ELT (methods, ix);
747 if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
749 fndecls = NULL_TREE;
750 is_conv_op = false;
753 else
754 is_conv_op = false;
756 if (format)
757 format = " %#D";
758 else if (fndecls)
759 format = "candidates are: %#D";
760 else
761 format = "candidate is: %#D";
762 cp_error_at (format, fndecl);
765 else if (!COMPLETE_TYPE_P (ctype))
766 cxx_incomplete_type_error (function, ctype);
767 else
768 error ("no `%#D' member function declared in class `%T'",
769 function, ctype);
771 /* If we did not find the method in the class, add it to avoid
772 spurious errors (unless the CTYPE is not yet defined, in which
773 case we'll only confuse ourselves when the function is declared
774 properly within the class. */
775 if (COMPLETE_TYPE_P (ctype))
776 add_method (ctype, function, /*error_p=*/1);
777 return NULL_TREE;
780 /* We have just processed the DECL, which is a static data member.
781 Its initializer, if present, is INIT. The ASMSPEC_TREE, if
782 present, is the assembly-language name for the data member.
783 FLAGS is as for cp_finish_decl. */
785 void
786 finish_static_data_member_decl (tree decl, tree init, tree asmspec_tree,
787 int flags)
789 my_friendly_assert (TREE_PUBLIC (decl), 0);
791 DECL_CONTEXT (decl) = current_class_type;
793 /* We cannot call pushdecl here, because that would fill in the
794 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
795 the right thing, namely, to put this decl out straight away. */
796 /* current_class_type can be NULL_TREE in case of error. */
797 if (!asmspec_tree && current_class_type)
798 DECL_INITIAL (decl) = error_mark_node;
800 if (! processing_template_decl)
802 if (!pending_statics)
803 VARRAY_TREE_INIT (pending_statics, 32, "pending_statics");
804 VARRAY_PUSH_TREE (pending_statics, decl);
807 if (LOCAL_CLASS_P (current_class_type))
808 pedwarn ("local class `%#T' shall not have static data member `%#D'",
809 current_class_type, decl);
811 /* Static consts need not be initialized in the class definition. */
812 if (init != NULL_TREE && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
814 static int explained = 0;
816 error ("initializer invalid for static member with constructor");
817 if (!explained)
819 error ("(an out of class initialization is required)");
820 explained = 1;
822 init = NULL_TREE;
824 /* Force the compiler to know when an uninitialized static const
825 member is being used. */
826 if (CP_TYPE_CONST_P (TREE_TYPE (decl)) && init == 0)
827 TREE_USED (decl) = 1;
828 DECL_INITIAL (decl) = init;
829 DECL_IN_AGGR_P (decl) = 1;
831 cp_finish_decl (decl, init, asmspec_tree, flags);
834 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
835 of a structure component, returning a _DECL node.
836 QUALS is a list of type qualifiers for this decl (such as for declaring
837 const member functions).
839 This is done during the parsing of the struct declaration.
840 The _DECL nodes are chained together and the lot of them
841 are ultimately passed to `build_struct' to make the RECORD_TYPE node.
843 If class A defines that certain functions in class B are friends, then
844 the way I have set things up, it is B who is interested in permission
845 granted by A. However, it is in A's context that these declarations
846 are parsed. By returning a void_type_node, class A does not attempt
847 to incorporate the declarations of the friends within its structure.
849 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
850 CHANGES TO CODE IN `start_method'. */
852 tree
853 grokfield (tree declarator, tree declspecs, tree init, tree asmspec_tree,
854 tree attrlist)
856 tree value;
857 const char *asmspec = 0;
858 int flags = LOOKUP_ONLYCONVERTING;
860 if (declspecs == NULL_TREE
861 && TREE_CODE (declarator) == SCOPE_REF
862 && TREE_CODE (TREE_OPERAND (declarator, 1)) == IDENTIFIER_NODE)
864 /* Access declaration */
865 if (! IS_AGGR_TYPE_CODE (TREE_CODE (TREE_OPERAND (declarator, 0))))
867 else if (TREE_COMPLEXITY (declarator) == current_class_depth)
868 pop_nested_class ();
869 return do_class_using_decl (declarator);
872 if (init
873 && TREE_CODE (init) == TREE_LIST
874 && TREE_VALUE (init) == error_mark_node
875 && TREE_CHAIN (init) == NULL_TREE)
876 init = NULL_TREE;
878 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
879 if (! value || value == error_mark_node)
880 /* friend or constructor went bad. */
881 return value;
882 if (TREE_TYPE (value) == error_mark_node)
883 return error_mark_node;
885 if (TREE_CODE (value) == TYPE_DECL && init)
887 error ("typedef `%D' is initialized (use __typeof__ instead)", value);
888 init = NULL_TREE;
891 /* Pass friendly classes back. */
892 if (value == void_type_node)
893 return value;
895 /* Pass friend decls back. */
896 if ((TREE_CODE (value) == FUNCTION_DECL
897 || TREE_CODE (value) == TEMPLATE_DECL)
898 && DECL_CONTEXT (value) != current_class_type)
899 return value;
901 if (DECL_NAME (value) != NULL_TREE
902 && IDENTIFIER_POINTER (DECL_NAME (value))[0] == '_'
903 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (value)), "_vptr"))
904 error ("member `%D' conflicts with virtual function table field name",
905 value);
907 /* Stash away type declarations. */
908 if (TREE_CODE (value) == TYPE_DECL)
910 DECL_NONLOCAL (value) = 1;
911 DECL_CONTEXT (value) = current_class_type;
913 if (CLASS_TYPE_P (TREE_TYPE (value)))
914 CLASSTYPE_GOT_SEMICOLON (TREE_TYPE (value)) = 1;
916 if (processing_template_decl)
917 value = push_template_decl (value);
919 return value;
922 if (DECL_IN_AGGR_P (value))
924 error ("`%D' is already defined in `%T'", value,
925 DECL_CONTEXT (value));
926 return void_type_node;
929 if (asmspec_tree)
930 asmspec = TREE_STRING_POINTER (asmspec_tree);
932 if (init)
934 if (TREE_CODE (value) == FUNCTION_DECL)
936 grok_function_init (value, init);
937 init = NULL_TREE;
939 else if (pedantic && TREE_CODE (value) != VAR_DECL)
940 /* Already complained in grokdeclarator. */
941 init = NULL_TREE;
942 else
944 /* We allow initializers to become parameters to base
945 initializers. */
946 if (TREE_CODE (init) == TREE_LIST)
948 if (TREE_CHAIN (init) == NULL_TREE)
949 init = TREE_VALUE (init);
950 else
951 init = digest_init (TREE_TYPE (value), init, (tree *)0);
954 if (!processing_template_decl)
956 if (TREE_CODE (init) == CONST_DECL)
957 init = DECL_INITIAL (init);
958 else if (TREE_READONLY_DECL_P (init))
959 init = decl_constant_value (init);
960 else if (TREE_CODE (init) == CONSTRUCTOR)
961 init = digest_init (TREE_TYPE (value), init, (tree *)0);
962 if (init != error_mark_node && ! TREE_CONSTANT (init))
964 /* We can allow references to things that are effectively
965 static, since references are initialized with the
966 address. */
967 if (TREE_CODE (TREE_TYPE (value)) != REFERENCE_TYPE
968 || (TREE_STATIC (init) == 0
969 && (!DECL_P (init) || DECL_EXTERNAL (init) == 0)))
971 error ("field initializer is not constant");
972 init = error_mark_node;
979 if (processing_template_decl
980 && (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == FUNCTION_DECL))
981 value = push_template_decl (value);
983 if (attrlist)
984 cplus_decl_attributes (&value, attrlist, 0);
986 if (TREE_CODE (value) == VAR_DECL)
988 finish_static_data_member_decl (value, init, asmspec_tree,
989 flags);
990 return value;
992 if (TREE_CODE (value) == FIELD_DECL)
994 if (asmspec)
995 error ("`asm' specifiers are not permitted on non-static data members");
996 if (DECL_INITIAL (value) == error_mark_node)
997 init = error_mark_node;
998 cp_finish_decl (value, init, NULL_TREE, flags);
999 DECL_INITIAL (value) = init;
1000 DECL_IN_AGGR_P (value) = 1;
1001 return value;
1003 if (TREE_CODE (value) == FUNCTION_DECL)
1005 if (asmspec)
1007 /* This must override the asm specifier which was placed
1008 by grokclassfn. Lay this out fresh. */
1009 SET_DECL_RTL (value, NULL_RTX);
1010 SET_DECL_ASSEMBLER_NAME (value, get_identifier (asmspec));
1012 if (!DECL_FRIEND_P (value))
1013 grok_special_member_properties (value);
1015 cp_finish_decl (value, init, asmspec_tree, flags);
1017 /* Pass friends back this way. */
1018 if (DECL_FRIEND_P (value))
1019 return void_type_node;
1021 DECL_IN_AGGR_P (value) = 1;
1022 return value;
1024 abort ();
1025 /* NOTREACHED */
1026 return NULL_TREE;
1029 /* Like `grokfield', but for bitfields.
1030 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1032 tree
1033 grokbitfield (tree declarator, tree declspecs, tree width)
1035 register tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1036 0, NULL);
1038 if (! value) return NULL_TREE; /* friends went bad. */
1040 /* Pass friendly classes back. */
1041 if (TREE_CODE (value) == VOID_TYPE)
1042 return void_type_node;
1044 if (TREE_CODE (value) == TYPE_DECL)
1046 error ("cannot declare `%D' to be a bit-field type", value);
1047 return NULL_TREE;
1050 /* Usually, finish_struct_1 catches bitfields with invalid types.
1051 But, in the case of bitfields with function type, we confuse
1052 ourselves into thinking they are member functions, so we must
1053 check here. */
1054 if (TREE_CODE (value) == FUNCTION_DECL)
1056 error ("cannot declare bit-field `%D' with function type",
1057 DECL_NAME (value));
1058 return NULL_TREE;
1061 if (DECL_IN_AGGR_P (value))
1063 error ("`%D' is already defined in the class %T", value,
1064 DECL_CONTEXT (value));
1065 return void_type_node;
1068 if (TREE_STATIC (value))
1070 error ("static member `%D' cannot be a bit-field", value);
1071 return NULL_TREE;
1073 cp_finish_decl (value, NULL_TREE, NULL_TREE, 0);
1075 if (width != error_mark_node)
1077 constant_expression_warning (width);
1078 DECL_INITIAL (value) = width;
1079 SET_DECL_C_BIT_FIELD (value);
1082 DECL_IN_AGGR_P (value) = 1;
1083 return value;
1086 /* When a function is declared with an initializer,
1087 do the right thing. Currently, there are two possibilities:
1089 class B
1091 public:
1092 // initialization possibility #1.
1093 virtual void f () = 0;
1094 int g ();
1097 class D1 : B
1099 public:
1100 int d1;
1101 // error, no f ();
1104 class D2 : B
1106 public:
1107 int d2;
1108 void f ();
1111 class D3 : B
1113 public:
1114 int d3;
1115 // initialization possibility #2
1116 void f () = B::f;
1121 static void
1122 grok_function_init (tree decl, tree init)
1124 /* An initializer for a function tells how this function should
1125 be inherited. */
1126 tree type = TREE_TYPE (decl);
1128 if (TREE_CODE (type) == FUNCTION_TYPE)
1129 error ("initializer specified for non-member function `%D'", decl);
1130 else if (integer_zerop (init))
1131 DECL_PURE_VIRTUAL_P (decl) = 1;
1132 else
1133 error ("invalid initializer for virtual method `%D'", decl);
1136 void
1137 cplus_decl_attributes (tree *decl, tree attributes, int flags)
1139 if (*decl == NULL_TREE || *decl == void_type_node)
1140 return;
1142 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1143 decl = &DECL_TEMPLATE_RESULT (*decl);
1145 decl_attributes (decl, attributes, flags);
1147 if (TREE_CODE (*decl) == TYPE_DECL)
1148 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1151 /* Return the name for the constructor (or destructor) for the
1152 specified class TYPE. When given a template, this routine doesn't
1153 lose the specialization. */
1155 tree
1156 constructor_name_full (tree type)
1158 type = TYPE_MAIN_VARIANT (type);
1159 if (CLASS_TYPE_P (type) && TYPE_WAS_ANONYMOUS (type)
1160 && TYPE_HAS_CONSTRUCTOR (type))
1161 return DECL_NAME (OVL_CURRENT (CLASSTYPE_CONSTRUCTORS (type)));
1162 else
1163 return TYPE_IDENTIFIER (type);
1166 /* Return the name for the constructor (or destructor) for the
1167 specified class. When given a template, return the plain
1168 unspecialized name. */
1170 tree
1171 constructor_name (tree type)
1173 tree name;
1174 name = constructor_name_full (type);
1175 if (IDENTIFIER_TEMPLATE (name))
1176 name = IDENTIFIER_TEMPLATE (name);
1177 return name;
1180 /* Returns TRUE if NAME is the name for the constructor for TYPE. */
1182 bool
1183 constructor_name_p (tree name, tree type)
1185 return (name == constructor_name (type)
1186 || name == constructor_name_full (type));
1190 /* Defer the compilation of the FN until the end of compilation. */
1192 void
1193 defer_fn (tree fn)
1195 if (DECL_DEFERRED_FN (fn))
1196 return;
1197 DECL_DEFERRED_FN (fn) = 1;
1198 DECL_DEFER_OUTPUT (fn) = 1;
1199 if (!deferred_fns)
1200 VARRAY_TREE_INIT (deferred_fns, 32, "deferred_fns");
1202 VARRAY_PUSH_TREE (deferred_fns, fn);
1205 /* Walks through the namespace- or function-scope anonymous union OBJECT,
1206 building appropriate ALIAS_DECLs. Returns one of the fields for use in
1207 the mangled name. */
1209 static tree
1210 build_anon_union_vars (tree object)
1212 tree type = TREE_TYPE (object);
1213 tree main_decl = NULL_TREE;
1214 tree field;
1216 /* Rather than write the code to handle the non-union case,
1217 just give an error. */
1218 if (TREE_CODE (type) != UNION_TYPE)
1219 error ("anonymous struct not inside named type");
1221 for (field = TYPE_FIELDS (type);
1222 field != NULL_TREE;
1223 field = TREE_CHAIN (field))
1225 tree decl;
1226 tree ref;
1228 if (DECL_ARTIFICIAL (field))
1229 continue;
1230 if (TREE_CODE (field) != FIELD_DECL)
1232 cp_pedwarn_at ("\
1233 `%#D' invalid; an anonymous union can only have non-static data members",
1234 field);
1235 continue;
1238 if (TREE_PRIVATE (field))
1239 cp_pedwarn_at ("private member `%#D' in anonymous union", field);
1240 else if (TREE_PROTECTED (field))
1241 cp_pedwarn_at ("protected member `%#D' in anonymous union", field);
1243 if (processing_template_decl)
1244 ref = build_min_nt (COMPONENT_REF, object, DECL_NAME (field));
1245 else
1246 ref = build_class_member_access_expr (object, field, NULL_TREE,
1247 false);
1249 if (DECL_NAME (field))
1251 decl = build_decl (ALIAS_DECL, DECL_NAME (field), TREE_TYPE (field));
1252 DECL_INITIAL (decl) = ref;
1253 TREE_PUBLIC (decl) = 0;
1254 TREE_STATIC (decl) = 0;
1255 DECL_EXTERNAL (decl) = 1;
1256 decl = pushdecl (decl);
1258 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1259 decl = build_anon_union_vars (ref);
1260 else
1261 decl = 0;
1263 if (main_decl == NULL_TREE)
1264 main_decl = decl;
1267 return main_decl;
1270 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1271 anonymous union, then all members must be laid out together. PUBLIC_P
1272 is nonzero if this union is not declared static. */
1274 void
1275 finish_anon_union (tree anon_union_decl)
1277 tree type = TREE_TYPE (anon_union_decl);
1278 tree main_decl;
1279 bool public_p = TREE_PUBLIC (anon_union_decl);
1281 /* The VAR_DECL's context is the same as the TYPE's context. */
1282 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1284 if (TYPE_FIELDS (type) == NULL_TREE)
1285 return;
1287 if (public_p)
1289 error ("namespace-scope anonymous aggregates must be static");
1290 return;
1293 main_decl = build_anon_union_vars (anon_union_decl);
1294 if (main_decl == NULL_TREE)
1296 warning ("anonymous union with no members");
1297 return;
1300 if (!processing_template_decl)
1302 /* Use main_decl to set the mangled name. */
1303 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1304 mangle_decl (anon_union_decl);
1305 DECL_NAME (anon_union_decl) = NULL_TREE;
1308 pushdecl (anon_union_decl);
1309 if (building_stmt_tree ()
1310 && at_function_scope_p ())
1311 add_decl_stmt (anon_union_decl);
1312 else if (!processing_template_decl)
1313 rest_of_decl_compilation (anon_union_decl, NULL,
1314 toplevel_bindings_p (), at_eof);
1317 /* Auxiliary functions to make type signatures for
1318 `operator new' and `operator delete' correspond to
1319 what compiler will be expecting. */
1321 tree
1322 coerce_new_type (tree type)
1324 int e = 0;
1325 tree args = TYPE_ARG_TYPES (type);
1327 my_friendly_assert (TREE_CODE (type) == FUNCTION_TYPE, 20001107);
1329 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1330 e = 1, error ("`operator new' must return type `%T'", ptr_type_node);
1332 if (!args || args == void_list_node
1333 || !same_type_p (TREE_VALUE (args), size_type_node))
1335 e = 2;
1336 if (args && args != void_list_node)
1337 args = TREE_CHAIN (args);
1338 pedwarn ("`operator new' takes type `size_t' (`%T') as first parameter", size_type_node);
1340 switch (e)
1342 case 2:
1343 args = tree_cons (NULL_TREE, size_type_node, args);
1344 /* FALLTHROUGH */
1345 case 1:
1346 type = build_exception_variant
1347 (build_function_type (ptr_type_node, args),
1348 TYPE_RAISES_EXCEPTIONS (type));
1349 /* FALLTHROUGH */
1350 default:;
1352 return type;
1355 tree
1356 coerce_delete_type (tree type)
1358 int e = 0;
1359 tree args = TYPE_ARG_TYPES (type);
1361 my_friendly_assert (TREE_CODE (type) == FUNCTION_TYPE, 20001107);
1363 if (!same_type_p (TREE_TYPE (type), void_type_node))
1364 e = 1, error ("`operator delete' must return type `%T'", void_type_node);
1366 if (!args || args == void_list_node
1367 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1369 e = 2;
1370 if (args && args != void_list_node)
1371 args = TREE_CHAIN (args);
1372 error ("`operator delete' takes type `%T' as first parameter", ptr_type_node);
1374 switch (e)
1376 case 2:
1377 args = tree_cons (NULL_TREE, ptr_type_node, args);
1378 /* FALLTHROUGH */
1379 case 1:
1380 type = build_exception_variant
1381 (build_function_type (void_type_node, args),
1382 TYPE_RAISES_EXCEPTIONS (type));
1383 /* FALLTHROUGH */
1384 default:;
1387 return type;
1390 static void
1391 mark_vtable_entries (tree decl)
1393 tree entries = CONSTRUCTOR_ELTS (DECL_INITIAL (decl));
1395 for (; entries; entries = TREE_CHAIN (entries))
1397 tree fnaddr = TREE_VALUE (entries);
1398 tree fn;
1400 STRIP_NOPS (fnaddr);
1402 if (TREE_CODE (fnaddr) != ADDR_EXPR
1403 && TREE_CODE (fnaddr) != FDESC_EXPR)
1404 /* This entry is an offset: a virtual base class offset, a
1405 virtual call offset, an RTTI offset, etc. */
1406 continue;
1408 fn = TREE_OPERAND (fnaddr, 0);
1409 TREE_ADDRESSABLE (fn) = 1;
1410 /* When we don't have vcall offsets, we output thunks whenever
1411 we output the vtables that contain them. With vcall offsets,
1412 we know all the thunks we'll need when we emit a virtual
1413 function, so we emit the thunks there instead. */
1414 if (DECL_THUNK_P (fn))
1415 use_thunk (fn, /*emit_p=*/0);
1416 mark_used (fn);
1420 /* Set DECL up to have the closest approximation of "initialized common"
1421 linkage available. */
1423 void
1424 comdat_linkage (tree decl)
1426 if (flag_weak)
1427 make_decl_one_only (decl);
1428 else if (TREE_CODE (decl) == FUNCTION_DECL
1429 || (TREE_CODE (decl) == VAR_DECL && DECL_ARTIFICIAL (decl)))
1430 /* We can just emit function and compiler-generated variables
1431 statically; having multiple copies is (for the most part) only
1432 a waste of space.
1434 There are two correctness issues, however: the address of a
1435 template instantiation with external linkage should be the
1436 same, independent of what translation unit asks for the
1437 address, and this will not hold when we emit multiple copies of
1438 the function. However, there's little else we can do.
1440 Also, by default, the typeinfo implementation assumes that
1441 there will be only one copy of the string used as the name for
1442 each type. Therefore, if weak symbols are unavailable, the
1443 run-time library should perform a more conservative check; it
1444 should perform a string comparison, rather than an address
1445 comparison. */
1446 TREE_PUBLIC (decl) = 0;
1447 else
1449 /* Static data member template instantiations, however, cannot
1450 have multiple copies. */
1451 if (DECL_INITIAL (decl) == 0
1452 || DECL_INITIAL (decl) == error_mark_node)
1453 DECL_COMMON (decl) = 1;
1454 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1456 DECL_COMMON (decl) = 1;
1457 DECL_INITIAL (decl) = error_mark_node;
1459 else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1461 /* We can't do anything useful; leave vars for explicit
1462 instantiation. */
1463 DECL_EXTERNAL (decl) = 1;
1464 DECL_NOT_REALLY_EXTERN (decl) = 0;
1468 if (DECL_LANG_SPECIFIC (decl))
1469 DECL_COMDAT (decl) = 1;
1472 /* For win32 we also want to put explicit instantiations in
1473 linkonce sections, so that they will be merged with implicit
1474 instantiations; otherwise we get duplicate symbol errors. */
1476 void
1477 maybe_make_one_only (tree decl)
1479 /* We used to say that this was not necessary on targets that support weak
1480 symbols, because the implicit instantiations will defer to the explicit
1481 one. However, that's not actually the case in SVR4; a strong definition
1482 after a weak one is an error. Also, not making explicit
1483 instantiations one_only means that we can end up with two copies of
1484 some template instantiations. */
1485 if (! flag_weak)
1486 return;
1488 /* We can't set DECL_COMDAT on functions, or finish_file will think
1489 we can get away with not emitting them if they aren't used. We need
1490 to for variables so that cp_finish_decl will update their linkage,
1491 because their DECL_INITIAL may not have been set properly yet. */
1493 make_decl_one_only (decl);
1495 if (TREE_CODE (decl) == VAR_DECL)
1497 DECL_COMDAT (decl) = 1;
1498 /* Mark it needed so we don't forget to emit it. */
1499 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)) = 1;
1503 /* Set TREE_PUBLIC and/or DECL_EXTERN on the vtable DECL,
1504 based on TYPE and other static flags.
1506 Note that anything public is tagged TREE_PUBLIC, whether
1507 it's public in this file or in another one. */
1509 void
1510 import_export_vtable (tree decl, tree type, int final)
1512 if (DECL_INTERFACE_KNOWN (decl))
1513 return;
1515 if (TYPE_FOR_JAVA (type))
1517 TREE_PUBLIC (decl) = 1;
1518 DECL_EXTERNAL (decl) = 1;
1519 DECL_INTERFACE_KNOWN (decl) = 1;
1521 else if (CLASSTYPE_INTERFACE_KNOWN (type))
1523 TREE_PUBLIC (decl) = 1;
1524 DECL_EXTERNAL (decl) = CLASSTYPE_INTERFACE_ONLY (type);
1525 DECL_INTERFACE_KNOWN (decl) = 1;
1527 else
1529 /* We can only wait to decide if we have real non-inline virtual
1530 functions in our class, or if we come from a template. */
1532 int found = (CLASSTYPE_TEMPLATE_INSTANTIATION (type)
1533 || CLASSTYPE_KEY_METHOD (type) != NULL_TREE);
1535 if (final || ! found)
1537 comdat_linkage (decl);
1538 DECL_EXTERNAL (decl) = 0;
1540 else
1542 TREE_PUBLIC (decl) = 1;
1543 DECL_EXTERNAL (decl) = 1;
1548 /* Determine whether or not we want to specifically import or export CTYPE,
1549 using various heuristics. */
1551 static void
1552 import_export_class (tree ctype)
1554 /* -1 for imported, 1 for exported. */
1555 int import_export = 0;
1557 /* It only makes sense to call this function at EOF. The reason is
1558 that this function looks at whether or not the first non-inline
1559 non-abstract virtual member function has been defined in this
1560 translation unit. But, we can't possibly know that until we've
1561 seen the entire translation unit. */
1562 my_friendly_assert (at_eof, 20000226);
1564 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1565 return;
1567 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma interface,
1568 we will have CLASSTYPE_INTERFACE_ONLY set but not
1569 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1570 heuristic because someone will supply a #pragma implementation
1571 elsewhere, and deducing it here would produce a conflict. */
1572 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1573 return;
1575 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1576 import_export = -1;
1577 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1578 import_export = 1;
1580 /* If we got -fno-implicit-templates, we import template classes that
1581 weren't explicitly instantiated. */
1582 if (import_export == 0
1583 && CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1584 && ! flag_implicit_templates)
1585 import_export = -1;
1587 /* Base our import/export status on that of the first non-inline,
1588 non-pure virtual function, if any. */
1589 if (import_export == 0
1590 && TYPE_POLYMORPHIC_P (ctype))
1592 tree method = CLASSTYPE_KEY_METHOD (ctype);
1593 if (method)
1594 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1597 #ifdef MULTIPLE_SYMBOL_SPACES
1598 if (import_export == -1)
1599 import_export = 0;
1600 #endif
1602 if (import_export)
1604 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1605 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1609 /* We need to describe to the assembler the relationship between
1610 a vtable and the vtable of the parent class. */
1612 void
1613 prepare_assemble_variable (tree vars)
1615 tree parent;
1616 rtx child_rtx, parent_rtx;
1618 if (!flag_vtable_gc || TREE_CODE (vars) != VAR_DECL
1619 || !DECL_VTABLE_OR_VTT_P (vars))
1620 return;
1622 child_rtx = XEXP (DECL_RTL (vars), 0); /* strip the mem ref */
1624 parent = binfo_for_vtable (vars);
1626 if (parent == TYPE_BINFO (DECL_CONTEXT (vars)))
1627 parent_rtx = const0_rtx;
1628 else if (parent)
1630 parent = get_vtbl_decl_for_binfo (TYPE_BINFO (BINFO_TYPE (parent)));
1631 parent_rtx = XEXP (DECL_RTL (parent), 0); /* strip the mem ref */
1633 else
1634 abort ();
1636 assemble_vtable_inherit (child_rtx, parent_rtx);
1639 /* If necessary, write out the vtables for the dynamic class CTYPE.
1640 Returns true if any vtables were emitted. */
1642 static bool
1643 maybe_emit_vtables (tree ctype)
1645 tree vtbl;
1646 tree primary_vtbl;
1648 /* If the vtables for this class have already been emitted there is
1649 nothing more to do. */
1650 primary_vtbl = CLASSTYPE_VTABLES (ctype);
1651 if (TREE_ASM_WRITTEN (primary_vtbl))
1652 return false;
1653 /* Ignore dummy vtables made by get_vtable_decl. */
1654 if (TREE_TYPE (primary_vtbl) == void_type_node)
1655 return false;
1657 import_export_class (ctype);
1658 import_export_vtable (primary_vtbl, ctype, 1);
1660 /* See if any of the vtables are needed. */
1661 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = TREE_CHAIN (vtbl))
1662 if (!DECL_EXTERNAL (vtbl) && DECL_NEEDED_P (vtbl))
1663 break;
1665 if (!vtbl)
1667 /* If the references to this class' vtables are optimized away,
1668 still emit the appropriate debugging information. See
1669 dfs_debug_mark. */
1670 if (DECL_COMDAT (primary_vtbl)
1671 && CLASSTYPE_DEBUG_REQUESTED (ctype))
1672 note_debug_info_needed (ctype);
1673 return false;
1676 /* The ABI requires that we emit all of the vtables if we emit any
1677 of them. */
1678 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = TREE_CHAIN (vtbl))
1680 /* Write it out. */
1681 import_export_vtable (vtbl, ctype, 1);
1682 mark_vtable_entries (vtbl);
1684 /* If we know that DECL is needed, mark it as such for the varpool. */
1685 if (CLASSTYPE_EXPLICIT_INSTANTIATION (ctype))
1686 cgraph_varpool_mark_needed_node (cgraph_varpool_node (vtbl));
1688 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
1689 store_init_value (vtbl, DECL_INITIAL (vtbl));
1691 if (write_symbols == DWARF_DEBUG || write_symbols == DWARF2_DEBUG)
1693 /* Mark the VAR_DECL node representing the vtable itself as a
1694 "gratuitous" one, thereby forcing dwarfout.c to ignore it.
1695 It is rather important that such things be ignored because
1696 any effort to actually generate DWARF for them will run
1697 into trouble when/if we encounter code like:
1699 #pragma interface
1700 struct S { virtual void member (); };
1702 because the artificial declaration of the vtable itself (as
1703 manufactured by the g++ front end) will say that the vtable
1704 is a static member of `S' but only *after* the debug output
1705 for the definition of `S' has already been output. This causes
1706 grief because the DWARF entry for the definition of the vtable
1707 will try to refer back to an earlier *declaration* of the
1708 vtable as a static member of `S' and there won't be one.
1709 We might be able to arrange to have the "vtable static member"
1710 attached to the member list for `S' before the debug info for
1711 `S' get written (which would solve the problem) but that would
1712 require more intrusive changes to the g++ front end. */
1714 DECL_IGNORED_P (vtbl) = 1;
1717 /* Always make vtables weak. */
1718 if (flag_weak)
1719 comdat_linkage (vtbl);
1721 rest_of_decl_compilation (vtbl, NULL, 1, 1);
1723 /* Because we're only doing syntax-checking, we'll never end up
1724 actually marking the variable as written. */
1725 if (flag_syntax_only)
1726 TREE_ASM_WRITTEN (vtbl) = 1;
1729 /* Since we're writing out the vtable here, also write the debug
1730 info. */
1731 note_debug_info_needed (ctype);
1733 return true;
1736 /* Determines the proper settings of TREE_PUBLIC and DECL_EXTERNAL for an
1737 inline function or template instantiation at end-of-file. */
1739 void
1740 import_export_decl (tree decl)
1742 if (DECL_INTERFACE_KNOWN (decl))
1743 return;
1745 if (DECL_TEMPLATE_INSTANTIATION (decl)
1746 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
1748 DECL_NOT_REALLY_EXTERN (decl) = 1;
1749 if ((DECL_IMPLICIT_INSTANTIATION (decl)
1750 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
1751 && (flag_implicit_templates
1752 || (flag_implicit_inline_templates
1753 && DECL_DECLARED_INLINE_P (decl))))
1755 if (!TREE_PUBLIC (decl))
1756 /* Templates are allowed to have internal linkage. See
1757 [basic.link]. */
1759 else
1760 comdat_linkage (decl);
1762 else
1764 DECL_EXTERNAL (decl) = 1;
1765 DECL_NOT_REALLY_EXTERN (decl) = 0;
1768 else if (DECL_FUNCTION_MEMBER_P (decl))
1770 if (!DECL_DECLARED_INLINE_P (decl))
1772 tree ctype = DECL_CONTEXT (decl);
1773 import_export_class (ctype);
1774 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1776 DECL_NOT_REALLY_EXTERN (decl)
1777 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
1778 || (DECL_DECLARED_INLINE_P (decl)
1779 && ! flag_implement_inlines
1780 && !DECL_VINDEX (decl)));
1782 if (!DECL_NOT_REALLY_EXTERN (decl))
1783 DECL_EXTERNAL (decl) = 1;
1785 /* Always make artificials weak. */
1786 if (DECL_ARTIFICIAL (decl) && flag_weak)
1787 comdat_linkage (decl);
1788 else
1789 maybe_make_one_only (decl);
1792 else
1793 comdat_linkage (decl);
1795 else
1796 comdat_linkage (decl);
1798 DECL_INTERFACE_KNOWN (decl) = 1;
1801 /* Here, we only decide whether or not the tinfo node should be
1802 emitted with the vtable. IS_IN_LIBRARY is nonzero iff the
1803 typeinfo for TYPE should be in the runtime library. */
1805 void
1806 import_export_tinfo (tree decl, tree type, bool is_in_library)
1808 if (DECL_INTERFACE_KNOWN (decl))
1809 return;
1811 if (IS_AGGR_TYPE (type))
1812 import_export_class (type);
1814 if (IS_AGGR_TYPE (type) && CLASSTYPE_INTERFACE_KNOWN (type)
1815 && TYPE_POLYMORPHIC_P (type)
1816 /* If -fno-rtti, we're not necessarily emitting this stuff with
1817 the class, so go ahead and emit it now. This can happen when
1818 a class is used in exception handling. */
1819 && flag_rtti)
1821 DECL_NOT_REALLY_EXTERN (decl) = !CLASSTYPE_INTERFACE_ONLY (type);
1822 DECL_COMDAT (decl) = 0;
1824 else
1826 DECL_NOT_REALLY_EXTERN (decl) = 1;
1827 DECL_COMDAT (decl) = 1;
1830 /* Now override some cases. */
1831 if (flag_weak)
1832 DECL_COMDAT (decl) = 1;
1833 else if (is_in_library)
1834 DECL_COMDAT (decl) = 0;
1836 DECL_INTERFACE_KNOWN (decl) = 1;
1839 /* Return an expression that performs the destruction of DECL, which
1840 must be a VAR_DECL whose type has a non-trivial destructor, or is
1841 an array whose (innermost) elements have a non-trivial destructor. */
1843 tree
1844 build_cleanup (tree decl)
1846 tree temp;
1847 tree type = TREE_TYPE (decl);
1849 /* This function should only be called for declarations that really
1850 require cleanups. */
1851 my_friendly_assert (!TYPE_HAS_TRIVIAL_DESTRUCTOR (type), 20030106);
1853 /* Treat all objects with destructors as used; the destructor may do
1854 something substantive. */
1855 mark_used (decl);
1857 if (TREE_CODE (type) == ARRAY_TYPE)
1858 temp = decl;
1859 else
1861 cxx_mark_addressable (decl);
1862 temp = build1 (ADDR_EXPR, build_pointer_type (type), decl);
1864 temp = build_delete (TREE_TYPE (temp), temp,
1865 sfk_complete_destructor,
1866 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
1867 return temp;
1870 /* Returns the initialization guard variable for the variable DECL,
1871 which has static storage duration. */
1873 tree
1874 get_guard (tree decl)
1876 tree sname;
1877 tree guard;
1879 sname = mangle_guard_variable (decl);
1880 guard = IDENTIFIER_GLOBAL_VALUE (sname);
1881 if (! guard)
1883 tree guard_type;
1885 /* We use a type that is big enough to contain a mutex as well
1886 as an integer counter. */
1887 guard_type = long_long_integer_type_node;
1888 guard = build_decl (VAR_DECL, sname, guard_type);
1890 /* The guard should have the same linkage as what it guards. */
1891 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
1892 TREE_STATIC (guard) = TREE_STATIC (decl);
1893 DECL_COMMON (guard) = DECL_COMMON (decl);
1894 DECL_ONE_ONLY (guard) = DECL_ONE_ONLY (decl);
1895 if (TREE_PUBLIC (decl))
1896 DECL_WEAK (guard) = DECL_WEAK (decl);
1898 DECL_ARTIFICIAL (guard) = 1;
1899 TREE_USED (guard) = 1;
1900 pushdecl_top_level_and_finish (guard, NULL_TREE);
1902 return guard;
1905 /* Return those bits of the GUARD variable that should be set when the
1906 guarded entity is actually initialized. */
1908 static tree
1909 get_guard_bits (tree guard)
1911 /* We only set the first byte of the guard, in order to leave room
1912 for a mutex in the high-order bits. */
1913 guard = build1 (ADDR_EXPR,
1914 build_pointer_type (TREE_TYPE (guard)),
1915 guard);
1916 guard = build1 (NOP_EXPR,
1917 build_pointer_type (char_type_node),
1918 guard);
1919 guard = build1 (INDIRECT_REF, char_type_node, guard);
1921 return guard;
1924 /* Return an expression which determines whether or not the GUARD
1925 variable has already been initialized. */
1927 tree
1928 get_guard_cond (tree guard)
1930 tree guard_value;
1932 /* Check to see if the GUARD is zero. */
1933 guard = get_guard_bits (guard);
1934 guard_value = integer_zero_node;
1935 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
1936 guard_value = convert (TREE_TYPE (guard), guard_value);
1937 return cp_build_binary_op (EQ_EXPR, guard, guard_value);
1940 /* Return an expression which sets the GUARD variable, indicating that
1941 the variable being guarded has been initialized. */
1943 tree
1944 set_guard (tree guard)
1946 tree guard_init;
1948 /* Set the GUARD to one. */
1949 guard = get_guard_bits (guard);
1950 guard_init = integer_one_node;
1951 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
1952 guard_init = convert (TREE_TYPE (guard), guard_init);
1953 return build_modify_expr (guard, NOP_EXPR, guard_init);
1956 /* Start the process of running a particular set of global constructors
1957 or destructors. Subroutine of do_[cd]tors. */
1959 static tree
1960 start_objects (int method_type, int initp)
1962 tree fnname;
1963 tree body;
1964 char type[10];
1966 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
1968 if (initp != DEFAULT_INIT_PRIORITY)
1970 char joiner;
1972 #ifdef JOINER
1973 joiner = JOINER;
1974 #else
1975 joiner = '_';
1976 #endif
1978 sprintf (type, "%c%c%.5u", method_type, joiner, initp);
1980 else
1981 sprintf (type, "%c", method_type);
1983 fnname = get_file_function_name_long (type);
1985 start_function (void_list_node,
1986 make_call_declarator (fnname, void_list_node, NULL_TREE,
1987 NULL_TREE),
1988 NULL_TREE, SF_DEFAULT);
1990 /* It can be a static function as long as collect2 does not have
1991 to scan the object file to find its ctor/dtor routine. */
1992 TREE_PUBLIC (current_function_decl) = ! targetm.have_ctors_dtors;
1994 /* Mark this declaration as used to avoid spurious warnings. */
1995 TREE_USED (current_function_decl) = 1;
1997 /* Mark this function as a global constructor or destructor. */
1998 if (method_type == 'I')
1999 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
2000 else
2001 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
2002 DECL_LANG_SPECIFIC (current_function_decl)->decl_flags.u2sel = 1;
2004 body = begin_compound_stmt (/*has_no_scope=*/0);
2006 /* We cannot allow these functions to be elided, even if they do not
2007 have external linkage. And, there's no point in deferring
2008 copmilation of thes functions; they're all going to have to be
2009 out anyhow. */
2010 current_function_cannot_inline
2011 = "static constructors and destructors cannot be inlined";
2013 return body;
2016 /* Finish the process of running a particular set of global constructors
2017 or destructors. Subroutine of do_[cd]tors. */
2019 static void
2020 finish_objects (int method_type, int initp, tree body)
2022 tree fn;
2024 /* Finish up. */
2025 finish_compound_stmt (/*has_no_scope=*/0, body);
2026 fn = finish_function (0);
2027 expand_or_defer_fn (fn);
2029 /* When only doing semantic analysis, and no RTL generation, we
2030 can't call functions that directly emit assembly code; there is
2031 no assembly file in which to put the code. */
2032 if (flag_syntax_only)
2033 return;
2035 if (targetm.have_ctors_dtors)
2037 rtx fnsym = XEXP (DECL_RTL (fn), 0);
2038 if (method_type == 'I')
2039 (* targetm.asm_out.constructor) (fnsym, initp);
2040 else
2041 (* targetm.asm_out.destructor) (fnsym, initp);
2045 /* The names of the parameters to the function created to handle
2046 initializations and destructions for objects with static storage
2047 duration. */
2048 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
2049 #define PRIORITY_IDENTIFIER "__priority"
2051 /* The name of the function we create to handle initializations and
2052 destructions for objects with static storage duration. */
2053 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
2055 /* The declaration for the __INITIALIZE_P argument. */
2056 static GTY(()) tree initialize_p_decl;
2058 /* The declaration for the __PRIORITY argument. */
2059 static GTY(()) tree priority_decl;
2061 /* The declaration for the static storage duration function. */
2062 static GTY(()) tree ssdf_decl;
2064 /* All the static storage duration functions created in this
2065 translation unit. */
2066 static GTY(()) varray_type ssdf_decls;
2068 /* A map from priority levels to information about that priority
2069 level. There may be many such levels, so efficient lookup is
2070 important. */
2071 static splay_tree priority_info_map;
2073 /* Begins the generation of the function that will handle all
2074 initialization and destruction of objects with static storage
2075 duration. The function generated takes two parameters of type
2076 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
2077 nonzero, it performs initializations. Otherwise, it performs
2078 destructions. It only performs those initializations or
2079 destructions with the indicated __PRIORITY. The generated function
2080 returns no value.
2082 It is assumed that this function will only be called once per
2083 translation unit. */
2085 static tree
2086 start_static_storage_duration_function (unsigned count)
2088 tree parm_types;
2089 tree type;
2090 tree body;
2091 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
2093 /* Create the identifier for this function. It will be of the form
2094 SSDF_IDENTIFIER_<number>. */
2095 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
2097 /* Create the parameters. */
2098 parm_types = void_list_node;
2099 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
2100 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
2101 type = build_function_type (void_type_node, parm_types);
2103 /* Create the FUNCTION_DECL itself. */
2104 ssdf_decl = build_lang_decl (FUNCTION_DECL,
2105 get_identifier (id),
2106 type);
2107 TREE_PUBLIC (ssdf_decl) = 0;
2108 DECL_ARTIFICIAL (ssdf_decl) = 1;
2110 /* Put this function in the list of functions to be called from the
2111 static constructors and destructors. */
2112 if (!ssdf_decls)
2114 VARRAY_TREE_INIT (ssdf_decls, 32, "ssdf_decls");
2116 /* Take this opportunity to initialize the map from priority
2117 numbers to information about that priority level. */
2118 priority_info_map = splay_tree_new (splay_tree_compare_ints,
2119 /*delete_key_fn=*/0,
2120 /*delete_value_fn=*/
2121 (splay_tree_delete_value_fn) &free);
2123 /* We always need to generate functions for the
2124 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
2125 priorities later, we'll be sure to find the
2126 DEFAULT_INIT_PRIORITY. */
2127 get_priority_info (DEFAULT_INIT_PRIORITY);
2130 VARRAY_PUSH_TREE (ssdf_decls, ssdf_decl);
2132 /* Create the argument list. */
2133 initialize_p_decl = cp_build_parm_decl
2134 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
2135 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
2136 TREE_USED (initialize_p_decl) = 1;
2137 priority_decl = cp_build_parm_decl
2138 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
2139 DECL_CONTEXT (priority_decl) = ssdf_decl;
2140 TREE_USED (priority_decl) = 1;
2142 TREE_CHAIN (initialize_p_decl) = priority_decl;
2143 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
2145 /* Put the function in the global scope. */
2146 pushdecl (ssdf_decl);
2148 /* Start the function itself. This is equivalent to declarating the
2149 function as:
2151 static void __ssdf (int __initialize_p, init __priority_p);
2153 It is static because we only need to call this function from the
2154 various constructor and destructor functions for this module. */
2155 start_function (/*specs=*/NULL_TREE,
2156 ssdf_decl,
2157 /*attrs=*/NULL_TREE,
2158 SF_PRE_PARSED);
2160 /* Set up the scope of the outermost block in the function. */
2161 body = begin_compound_stmt (/*has_no_scope=*/0);
2163 /* This function must not be deferred because we are depending on
2164 its compilation to tell us what is TREE_SYMBOL_REFERENCED. */
2165 current_function_cannot_inline
2166 = "static storage duration functions cannot be inlined";
2168 return body;
2171 /* Finish the generation of the function which performs initialization
2172 and destruction of objects with static storage duration. After
2173 this point, no more such objects can be created. */
2175 static void
2176 finish_static_storage_duration_function (tree body)
2178 /* Close out the function. */
2179 finish_compound_stmt (/*has_no_scope=*/0, body);
2180 expand_or_defer_fn (finish_function (0));
2183 /* Return the information about the indicated PRIORITY level. If no
2184 code to handle this level has yet been generated, generate the
2185 appropriate prologue. */
2187 static priority_info
2188 get_priority_info (int priority)
2190 priority_info pi;
2191 splay_tree_node n;
2193 n = splay_tree_lookup (priority_info_map,
2194 (splay_tree_key) priority);
2195 if (!n)
2197 /* Create a new priority information structure, and insert it
2198 into the map. */
2199 pi = (priority_info) xmalloc (sizeof (struct priority_info_s));
2200 pi->initializations_p = 0;
2201 pi->destructions_p = 0;
2202 splay_tree_insert (priority_info_map,
2203 (splay_tree_key) priority,
2204 (splay_tree_value) pi);
2206 else
2207 pi = (priority_info) n->value;
2209 return pi;
2212 /* Set up to handle the initialization or destruction of DECL. If
2213 INITP is nonzero, we are initializing the variable. Otherwise, we
2214 are destroying it. */
2216 static tree
2217 start_static_initialization_or_destruction (tree decl, int initp)
2219 tree guard_if_stmt = NULL_TREE;
2220 int priority;
2221 tree cond;
2222 tree guard;
2223 tree init_cond;
2224 priority_info pi;
2226 /* Figure out the priority for this declaration. */
2227 priority = DECL_INIT_PRIORITY (decl);
2228 if (!priority)
2229 priority = DEFAULT_INIT_PRIORITY;
2231 /* Remember that we had an initialization or finalization at this
2232 priority. */
2233 pi = get_priority_info (priority);
2234 if (initp)
2235 pi->initializations_p = 1;
2236 else
2237 pi->destructions_p = 1;
2239 /* Trick the compiler into thinking we are at the file and line
2240 where DECL was declared so that error-messages make sense, and so
2241 that the debugger will show somewhat sensible file and line
2242 information. */
2243 input_location = DECL_SOURCE_LOCATION (decl);
2245 /* Because of:
2247 [class.access.spec]
2249 Access control for implicit calls to the constructors,
2250 the conversion functions, or the destructor called to
2251 create and destroy a static data member is performed as
2252 if these calls appeared in the scope of the member's
2253 class.
2255 we pretend we are in a static member function of the class of
2256 which the DECL is a member. */
2257 if (member_p (decl))
2259 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
2260 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
2263 /* Conditionalize this initialization on being in the right priority
2264 and being initializing/finalizing appropriately. */
2265 guard_if_stmt = begin_if_stmt ();
2266 cond = cp_build_binary_op (EQ_EXPR,
2267 priority_decl,
2268 build_int_2 (priority, 0));
2269 init_cond = initp ? integer_one_node : integer_zero_node;
2270 init_cond = cp_build_binary_op (EQ_EXPR,
2271 initialize_p_decl,
2272 init_cond);
2273 cond = cp_build_binary_op (TRUTH_ANDIF_EXPR, cond, init_cond);
2275 /* Assume we don't need a guard. */
2276 guard = NULL_TREE;
2277 /* We need a guard if this is an object with external linkage that
2278 might be initialized in more than one place. (For example, a
2279 static data member of a template, when the data member requires
2280 construction.) */
2281 if (TREE_PUBLIC (decl) && (DECL_COMMON (decl)
2282 || DECL_ONE_ONLY (decl)
2283 || DECL_WEAK (decl)))
2285 tree guard_cond;
2287 guard = get_guard (decl);
2289 /* When using __cxa_atexit, we just check the GUARD as we would
2290 for a local static. */
2291 if (flag_use_cxa_atexit)
2293 /* When using __cxa_atexit, we never try to destroy
2294 anything from a static destructor. */
2295 my_friendly_assert (initp, 20000629);
2296 guard_cond = get_guard_cond (guard);
2298 /* If we don't have __cxa_atexit, then we will be running
2299 destructors from .fini sections, or their equivalents. So,
2300 we need to know how many times we've tried to initialize this
2301 object. We do initializations only if the GUARD is zero,
2302 i.e., if we are the first to initialize the variable. We do
2303 destructions only if the GUARD is one, i.e., if we are the
2304 last to destroy the variable. */
2305 else if (initp)
2306 guard_cond
2307 = cp_build_binary_op (EQ_EXPR,
2308 build_unary_op (PREINCREMENT_EXPR,
2309 guard,
2310 /*noconvert=*/1),
2311 integer_one_node);
2312 else
2313 guard_cond
2314 = cp_build_binary_op (EQ_EXPR,
2315 build_unary_op (PREDECREMENT_EXPR,
2316 guard,
2317 /*noconvert=*/1),
2318 integer_zero_node);
2320 cond = cp_build_binary_op (TRUTH_ANDIF_EXPR, cond, guard_cond);
2323 finish_if_stmt_cond (cond, guard_if_stmt);
2325 /* If we're using __cxa_atexit, we have not already set the GUARD,
2326 so we must do so now. */
2327 if (guard && initp && flag_use_cxa_atexit)
2328 finish_expr_stmt (set_guard (guard));
2330 return guard_if_stmt;
2333 /* We've just finished generating code to do an initialization or
2334 finalization. GUARD_IF_STMT is the if-statement we used to guard
2335 the initialization. */
2337 static void
2338 finish_static_initialization_or_destruction (tree guard_if_stmt)
2340 finish_then_clause (guard_if_stmt);
2341 finish_if_stmt ();
2343 /* Now that we're done with DECL we don't need to pretend to be a
2344 member of its class any longer. */
2345 DECL_CONTEXT (current_function_decl) = NULL_TREE;
2346 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
2349 /* Generate code to do the initialization of DECL, a VAR_DECL with
2350 static storage duration. The initialization is INIT. */
2352 static void
2353 do_static_initialization (tree decl, tree init)
2355 tree guard_if_stmt;
2357 /* Set up for the initialization. */
2358 guard_if_stmt
2359 = start_static_initialization_or_destruction (decl,
2360 /*initp=*/1);
2362 /* Perform the initialization. */
2363 if (init)
2364 finish_expr_stmt (init);
2366 /* If we're using __cxa_atexit, register a a function that calls the
2367 destructor for the object. */
2368 if (flag_use_cxa_atexit)
2369 register_dtor_fn (decl);
2371 /* Finsh up. */
2372 finish_static_initialization_or_destruction (guard_if_stmt);
2375 /* Generate code to do the static destruction of DECL. If DECL may be
2376 initialized more than once in different object files, GUARD is the
2377 guard variable to check. PRIORITY is the priority for the
2378 destruction. */
2380 static void
2381 do_static_destruction (tree decl)
2383 tree guard_if_stmt;
2385 /* If we're using __cxa_atexit, then destructors are registered
2386 immediately after objects are initialized. */
2387 my_friendly_assert (!flag_use_cxa_atexit, 20000121);
2389 /* If we don't need a destructor, there's nothing to do. */
2390 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
2391 return;
2393 /* Actually do the destruction. */
2394 guard_if_stmt = start_static_initialization_or_destruction (decl,
2395 /*initp=*/0);
2396 finish_expr_stmt (build_cleanup (decl));
2397 finish_static_initialization_or_destruction (guard_if_stmt);
2400 /* VARS is a list of variables with static storage duration which may
2401 need initialization and/or finalization. Remove those variables
2402 that don't really need to be initialized or finalized, and return
2403 the resulting list. The order in which the variables appear in
2404 VARS is in reverse order of the order in which they should actually
2405 be initialized. The list we return is in the unreversed order;
2406 i.e., the first variable should be initialized first. */
2408 static tree
2409 prune_vars_needing_no_initialization (tree *vars)
2411 tree *var = vars;
2412 tree result = NULL_TREE;
2414 while (*var)
2416 tree t = *var;
2417 tree decl = TREE_VALUE (t);
2418 tree init = TREE_PURPOSE (t);
2420 /* Deal gracefully with error. */
2421 if (decl == error_mark_node)
2423 var = &TREE_CHAIN (t);
2424 continue;
2427 /* The only things that can be initialized are variables. */
2428 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 19990420);
2430 /* If this object is not defined, we don't need to do anything
2431 here. */
2432 if (DECL_EXTERNAL (decl))
2434 var = &TREE_CHAIN (t);
2435 continue;
2438 /* Also, if the initializer already contains errors, we can bail
2439 out now. */
2440 if (init && TREE_CODE (init) == TREE_LIST
2441 && value_member (error_mark_node, init))
2443 var = &TREE_CHAIN (t);
2444 continue;
2447 /* This variable is going to need initialization and/or
2448 finalization, so we add it to the list. */
2449 *var = TREE_CHAIN (t);
2450 TREE_CHAIN (t) = result;
2451 result = t;
2454 return result;
2457 /* Make sure we have told the back end about all the variables in
2458 VARS. */
2460 static void
2461 write_out_vars (tree vars)
2463 tree v;
2465 for (v = vars; v; v = TREE_CHAIN (v))
2466 if (! TREE_ASM_WRITTEN (TREE_VALUE (v)))
2467 rest_of_decl_compilation (TREE_VALUE (v), 0, 1, 1);
2470 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
2471 (otherwise) that will initialize all gobal objects with static
2472 storage duration having the indicated PRIORITY. */
2474 static void
2475 generate_ctor_or_dtor_function (bool constructor_p, int priority,
2476 location_t *locus)
2478 char function_key;
2479 tree arguments;
2480 tree fndecl;
2481 tree body;
2482 size_t i;
2484 input_location = *locus;
2485 locus->line++;
2487 /* We use `I' to indicate initialization and `D' to indicate
2488 destruction. */
2489 function_key = constructor_p ? 'I' : 'D';
2491 /* We emit the function lazily, to avoid generating empty
2492 global constructors and destructors. */
2493 body = NULL_TREE;
2495 /* Call the static storage duration function with appropriate
2496 arguments. */
2497 if (ssdf_decls)
2498 for (i = 0; i < ssdf_decls->elements_used; ++i)
2500 fndecl = VARRAY_TREE (ssdf_decls, i);
2502 /* Calls to pure or const functions will expand to nothing. */
2503 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
2505 if (! body)
2506 body = start_objects (function_key, priority);
2508 arguments = tree_cons (NULL_TREE, build_int_2 (priority, 0),
2509 NULL_TREE);
2510 arguments = tree_cons (NULL_TREE, build_int_2 (constructor_p, 0),
2511 arguments);
2512 finish_expr_stmt (build_function_call (fndecl, arguments));
2516 /* If we're generating code for the DEFAULT_INIT_PRIORITY, throw in
2517 calls to any functions marked with attributes indicating that
2518 they should be called at initialization- or destruction-time. */
2519 if (priority == DEFAULT_INIT_PRIORITY)
2521 tree fns;
2523 for (fns = constructor_p ? static_ctors : static_dtors;
2524 fns;
2525 fns = TREE_CHAIN (fns))
2527 fndecl = TREE_VALUE (fns);
2529 /* Calls to pure/const functions will expand to nothing. */
2530 if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
2532 if (! body)
2533 body = start_objects (function_key, priority);
2534 finish_expr_stmt (build_function_call (fndecl, NULL_TREE));
2539 /* Close out the function. */
2540 if (body)
2541 finish_objects (function_key, priority, body);
2544 /* Generate constructor and destructor functions for the priority
2545 indicated by N. */
2547 static int
2548 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
2550 location_t *locus = data;
2551 int priority = (int) n->key;
2552 priority_info pi = (priority_info) n->value;
2554 /* Generate the functions themselves, but only if they are really
2555 needed. */
2556 if (pi->initializations_p
2557 || (priority == DEFAULT_INIT_PRIORITY && static_ctors))
2558 generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
2559 if (pi->destructions_p
2560 || (priority == DEFAULT_INIT_PRIORITY && static_dtors))
2561 generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
2563 /* Keep iterating. */
2564 return 0;
2567 /* Callgraph code does not understand the member pointers. Mark the methods
2568 referenced as used. */
2569 static tree
2570 mark_member_pointers (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
2571 void *data ATTRIBUTE_UNUSED)
2573 if (TREE_CODE (*tp) == PTRMEM_CST)
2574 cgraph_mark_needed_node (cgraph_node (PTRMEM_CST_MEMBER (*tp)), 1);
2575 return 0;
2578 /* Called via LANGHOOK_CALLGRAPH_LOWER_FUNCTION. It is supposed to lower
2579 frontend specific constructs that would otherwise confuse the middle end. */
2580 void
2581 lower_function (tree fn)
2583 walk_tree_without_duplicates (&DECL_SAVED_TREE (fn), mark_member_pointers,
2584 NULL);
2587 /* This routine is called from the last rule in yyparse ().
2588 Its job is to create all the code needed to initialize and
2589 destroy the global aggregates. We do the destruction
2590 first, since that way we only need to reverse the decls once. */
2592 void
2593 finish_file ()
2595 tree vars;
2596 bool reconsider;
2597 size_t i;
2598 location_t locus;
2599 unsigned ssdf_count = 0;
2601 locus = input_location;
2602 at_eof = 1;
2604 /* Bad parse errors. Just forget about it. */
2605 if (! global_bindings_p () || current_class_type || decl_namespace_list)
2606 return;
2608 if (pch_file)
2609 c_common_write_pch ();
2611 /* Otherwise, GDB can get confused, because in only knows
2612 about source for LINENO-1 lines. */
2613 input_line -= 1;
2615 interface_unknown = 1;
2616 interface_only = 0;
2618 /* We now have to write out all the stuff we put off writing out.
2619 These include:
2621 o Template specializations that we have not yet instantiated,
2622 but which are needed.
2623 o Initialization and destruction for non-local objects with
2624 static storage duration. (Local objects with static storage
2625 duration are initialized when their scope is first entered,
2626 and are cleaned up via atexit.)
2627 o Virtual function tables.
2629 All of these may cause others to be needed. For example,
2630 instantiating one function may cause another to be needed, and
2631 generating the initializer for an object may cause templates to be
2632 instantiated, etc., etc. */
2634 timevar_push (TV_VARCONST);
2636 emit_support_tinfos ();
2640 tree t;
2641 size_t n_old, n_new;
2643 reconsider = false;
2645 /* If there are templates that we've put off instantiating, do
2646 them now. */
2647 instantiate_pending_templates ();
2649 /* Write out virtual tables as required. Note that writing out
2650 the virtual table for a template class may cause the
2651 instantiation of members of that class. If we write out
2652 vtables then we remove the class from our list so we don't
2653 have to look at it again. */
2655 while (keyed_classes != NULL_TREE
2656 && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
2658 reconsider = true;
2659 keyed_classes = TREE_CHAIN (keyed_classes);
2662 t = keyed_classes;
2663 if (t != NULL_TREE)
2665 tree next = TREE_CHAIN (t);
2667 while (next)
2669 if (maybe_emit_vtables (TREE_VALUE (next)))
2671 reconsider = true;
2672 TREE_CHAIN (t) = TREE_CHAIN (next);
2674 else
2675 t = next;
2677 next = TREE_CHAIN (t);
2681 /* Write out needed type info variables. We have to be careful
2682 looping through unemitted decls, because emit_tinfo_decl may
2683 cause other variables to be needed. We stick new elements
2684 (and old elements that we may need to reconsider) at the end
2685 of the array, then shift them back to the beginning once we're
2686 done. */
2688 n_old = VARRAY_ACTIVE_SIZE (unemitted_tinfo_decls);
2689 for (i = 0; i < n_old; ++i)
2691 tree tinfo_decl = VARRAY_TREE (unemitted_tinfo_decls, i);
2692 if (emit_tinfo_decl (tinfo_decl))
2693 reconsider = true;
2694 else
2695 VARRAY_PUSH_TREE (unemitted_tinfo_decls, tinfo_decl);
2698 /* The only elements we want to keep are the new ones. Copy
2699 them to the beginning of the array, then get rid of the
2700 leftovers. */
2701 n_new = VARRAY_ACTIVE_SIZE (unemitted_tinfo_decls) - n_old;
2702 memmove (&VARRAY_TREE (unemitted_tinfo_decls, 0),
2703 &VARRAY_TREE (unemitted_tinfo_decls, n_old),
2704 n_new * sizeof (tree));
2705 memset (&VARRAY_TREE (unemitted_tinfo_decls, n_new),
2707 n_old * sizeof (tree));
2708 VARRAY_ACTIVE_SIZE (unemitted_tinfo_decls) = n_new;
2710 /* The list of objects with static storage duration is built up
2711 in reverse order. We clear STATIC_AGGREGATES so that any new
2712 aggregates added during the initialization of these will be
2713 initialized in the correct order when we next come around the
2714 loop. */
2715 vars = prune_vars_needing_no_initialization (&static_aggregates);
2717 if (vars)
2719 tree v;
2721 /* We need to start a new initialization function each time
2722 through the loop. That's because we need to know which
2723 vtables have been referenced, and TREE_SYMBOL_REFERENCED
2724 isn't computed until a function is finished, and written
2725 out. That's a deficiency in the back-end. When this is
2726 fixed, these initialization functions could all become
2727 inline, with resulting performance improvements. */
2728 tree ssdf_body;
2730 /* Set the line and file, so that it is obviously not from
2731 the source file. */
2732 input_location = locus;
2733 ssdf_body = start_static_storage_duration_function (ssdf_count);
2735 /* Make sure the back end knows about all the variables. */
2736 write_out_vars (vars);
2738 /* First generate code to do all the initializations. */
2739 for (v = vars; v; v = TREE_CHAIN (v))
2740 do_static_initialization (TREE_VALUE (v),
2741 TREE_PURPOSE (v));
2743 /* Then, generate code to do all the destructions. Do these
2744 in reverse order so that the most recently constructed
2745 variable is the first destroyed. If we're using
2746 __cxa_atexit, then we don't need to do this; functions
2747 were registered at initialization time to destroy the
2748 local statics. */
2749 if (!flag_use_cxa_atexit)
2751 vars = nreverse (vars);
2752 for (v = vars; v; v = TREE_CHAIN (v))
2753 do_static_destruction (TREE_VALUE (v));
2755 else
2756 vars = NULL_TREE;
2758 /* Finish up the static storage duration function for this
2759 round. */
2760 input_location = locus;
2761 finish_static_storage_duration_function (ssdf_body);
2763 /* All those initializations and finalizations might cause
2764 us to need more inline functions, more template
2765 instantiations, etc. */
2766 reconsider = true;
2767 ssdf_count++;
2768 locus.line++;
2771 for (i = 0; i < deferred_fns_used; ++i)
2773 tree decl = VARRAY_TREE (deferred_fns, i);
2775 /* Does it need synthesizing? */
2776 if (DECL_ARTIFICIAL (decl) && ! DECL_INITIAL (decl)
2777 && TREE_USED (decl)
2778 && (! DECL_REALLY_EXTERN (decl) || DECL_INLINE (decl)))
2780 /* Even though we're already at the top-level, we push
2781 there again. That way, when we pop back a few lines
2782 hence, all of our state is restored. Otherwise,
2783 finish_function doesn't clean things up, and we end
2784 up with CURRENT_FUNCTION_DECL set. */
2785 push_to_top_level ();
2786 synthesize_method (decl);
2787 pop_from_top_level ();
2788 reconsider = true;
2791 /* If the function has no body, avoid calling
2792 import_export_decl. On a system without weak symbols,
2793 calling import_export_decl will make an inline template
2794 instantiation "static", which will result in errors about
2795 the use of undefined functions if there is no body for
2796 the function. */
2797 if (!DECL_SAVED_TREE (decl))
2798 continue;
2800 import_export_decl (decl);
2802 /* We lie to the back-end, pretending that some functions
2803 are not defined when they really are. This keeps these
2804 functions from being put out unnecessarily. But, we must
2805 stop lying when the functions are referenced, or if they
2806 are not comdat since they need to be put out now. This
2807 is done in a separate for cycle, because if some deferred
2808 function is contained in another deferred function later
2809 in deferred_fns varray, rest_of_compilation would skip
2810 this function and we really cannot expand the same
2811 function twice. */
2812 if (DECL_NOT_REALLY_EXTERN (decl)
2813 && DECL_INITIAL (decl)
2814 && DECL_NEEDED_P (decl))
2815 DECL_EXTERNAL (decl) = 0;
2817 /* If we're going to need to write this function out, and
2818 there's already a body for it, create RTL for it now.
2819 (There might be no body if this is a method we haven't
2820 gotten around to synthesizing yet.) */
2821 if (!DECL_EXTERNAL (decl)
2822 && DECL_NEEDED_P (decl)
2823 && DECL_SAVED_TREE (decl)
2824 && !TREE_ASM_WRITTEN (decl)
2825 && (!flag_unit_at_a_time
2826 || !cgraph_node (decl)->local.finalized))
2828 /* We will output the function; no longer consider it in this
2829 loop. */
2830 DECL_DEFER_OUTPUT (decl) = 0;
2831 /* Generate RTL for this function now that we know we
2832 need it. */
2833 expand_or_defer_fn (decl);
2834 /* If we're compiling -fsyntax-only pretend that this
2835 function has been written out so that we don't try to
2836 expand it again. */
2837 if (flag_syntax_only)
2838 TREE_ASM_WRITTEN (decl) = 1;
2839 reconsider = true;
2843 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
2844 reconsider = true;
2846 /* Static data members are just like namespace-scope globals. */
2847 for (i = 0; i < pending_statics_used; ++i)
2849 tree decl = VARRAY_TREE (pending_statics, i);
2850 if (TREE_ASM_WRITTEN (decl))
2851 continue;
2852 import_export_decl (decl);
2853 if (DECL_NOT_REALLY_EXTERN (decl) && ! DECL_IN_AGGR_P (decl))
2854 DECL_EXTERNAL (decl) = 0;
2856 if (pending_statics
2857 && wrapup_global_declarations (&VARRAY_TREE (pending_statics, 0),
2858 pending_statics_used))
2859 reconsider = true;
2861 while (reconsider);
2863 /* All used inline functions must have a definition at this point. */
2864 for (i = 0; i < deferred_fns_used; ++i)
2866 tree decl = VARRAY_TREE (deferred_fns, i);
2868 if (TREE_USED (decl) && DECL_DECLARED_INLINE_P (decl)
2869 && !(TREE_ASM_WRITTEN (decl) || DECL_SAVED_TREE (decl)
2870 /* An explicit instantiation can be used to specify
2871 that the body is in another unit. It will have
2872 already verified there was a definition. */
2873 || DECL_EXPLICIT_INSTANTIATION (decl)))
2875 cp_warning_at ("inline function `%D' used but never defined", decl);
2876 /* This symbol is effectively an "extern" declaration now.
2877 This is not strictly necessary, but removes a duplicate
2878 warning. */
2879 TREE_PUBLIC (decl) = 1;
2884 /* We give C linkage to static constructors and destructors. */
2885 push_lang_context (lang_name_c);
2887 /* Generate initialization and destruction functions for all
2888 priorities for which they are required. */
2889 if (priority_info_map)
2890 splay_tree_foreach (priority_info_map,
2891 generate_ctor_and_dtor_functions_for_priority,
2892 /*data=*/&locus);
2893 else
2896 if (static_ctors)
2897 generate_ctor_or_dtor_function (/*constructor_p=*/true,
2898 DEFAULT_INIT_PRIORITY, &locus);
2899 if (static_dtors)
2900 generate_ctor_or_dtor_function (/*constructor_p=*/false,
2901 DEFAULT_INIT_PRIORITY, &locus);
2904 /* We're done with the splay-tree now. */
2905 if (priority_info_map)
2906 splay_tree_delete (priority_info_map);
2908 /* We're done with static constructors, so we can go back to "C++"
2909 linkage now. */
2910 pop_lang_context ();
2912 if (flag_unit_at_a_time)
2914 cgraph_finalize_compilation_unit ();
2915 cgraph_optimize ();
2918 /* Now, issue warnings about static, but not defined, functions,
2919 etc., and emit debugging information. */
2920 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
2921 if (pending_statics)
2922 check_global_declarations (&VARRAY_TREE (pending_statics, 0),
2923 pending_statics_used);
2925 finish_repo ();
2927 /* The entire file is now complete. If requested, dump everything
2928 to a file. */
2930 int flags;
2931 FILE *stream = dump_begin (TDI_all, &flags);
2933 if (stream)
2935 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
2936 dump_end (TDI_all, stream);
2940 timevar_pop (TV_VARCONST);
2942 if (flag_detailed_statistics)
2944 dump_tree_statistics ();
2945 dump_time_statistics ();
2947 input_location = locus;
2950 /* T is the parse tree for an expression. Return the expression after
2951 performing semantic analysis. */
2953 tree
2954 build_expr_from_tree (t)
2955 tree t;
2957 if (t == NULL_TREE || t == error_mark_node)
2958 return t;
2960 switch (TREE_CODE (t))
2962 case IDENTIFIER_NODE:
2963 return do_identifier (t, NULL_TREE);
2965 case LOOKUP_EXPR:
2966 if (LOOKUP_EXPR_GLOBAL (t))
2968 tree token = TREE_OPERAND (t, 0);
2969 return do_scoped_id (token, IDENTIFIER_GLOBAL_VALUE (token));
2971 else
2973 t = do_identifier (TREE_OPERAND (t, 0), NULL_TREE);
2974 if (TREE_CODE (t) == ALIAS_DECL)
2975 t = DECL_INITIAL (t);
2976 return t;
2979 case TEMPLATE_ID_EXPR:
2981 tree template;
2982 tree args;
2983 tree object;
2985 template = build_expr_from_tree (TREE_OPERAND (t, 0));
2986 args = build_expr_from_tree (TREE_OPERAND (t, 1));
2988 if (TREE_CODE (template) == COMPONENT_REF)
2990 object = TREE_OPERAND (template, 0);
2991 template = TREE_OPERAND (template, 1);
2993 else
2994 object = NULL_TREE;
2996 template = lookup_template_function (template, args);
2997 if (object)
2998 return build (COMPONENT_REF, TREE_TYPE (template),
2999 object, template);
3000 else
3001 return template;
3004 case INDIRECT_REF:
3005 return build_x_indirect_ref
3006 (build_expr_from_tree (TREE_OPERAND (t, 0)), "unary *");
3008 case CAST_EXPR:
3009 return build_functional_cast
3010 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3012 case REINTERPRET_CAST_EXPR:
3013 return build_reinterpret_cast
3014 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3016 case CONST_CAST_EXPR:
3017 return build_const_cast
3018 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3020 case DYNAMIC_CAST_EXPR:
3021 return build_dynamic_cast
3022 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3024 case STATIC_CAST_EXPR:
3025 return build_static_cast
3026 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3028 case PREDECREMENT_EXPR:
3029 case PREINCREMENT_EXPR:
3030 case POSTDECREMENT_EXPR:
3031 case POSTINCREMENT_EXPR:
3032 case NEGATE_EXPR:
3033 case BIT_NOT_EXPR:
3034 case ABS_EXPR:
3035 case TRUTH_NOT_EXPR:
3036 case ADDR_EXPR:
3037 case CONVERT_EXPR: /* Unary + */
3038 case REALPART_EXPR:
3039 case IMAGPART_EXPR:
3040 if (TREE_TYPE (t))
3041 return t;
3042 return build_x_unary_op (TREE_CODE (t),
3043 build_expr_from_tree (TREE_OPERAND (t, 0)));
3045 case PLUS_EXPR:
3046 case MINUS_EXPR:
3047 case MULT_EXPR:
3048 case TRUNC_DIV_EXPR:
3049 case CEIL_DIV_EXPR:
3050 case FLOOR_DIV_EXPR:
3051 case ROUND_DIV_EXPR:
3052 case EXACT_DIV_EXPR:
3053 case BIT_AND_EXPR:
3054 case BIT_ANDTC_EXPR:
3055 case BIT_IOR_EXPR:
3056 case BIT_XOR_EXPR:
3057 case TRUNC_MOD_EXPR:
3058 case FLOOR_MOD_EXPR:
3059 case TRUTH_ANDIF_EXPR:
3060 case TRUTH_ORIF_EXPR:
3061 case TRUTH_AND_EXPR:
3062 case TRUTH_OR_EXPR:
3063 case RSHIFT_EXPR:
3064 case LSHIFT_EXPR:
3065 case RROTATE_EXPR:
3066 case LROTATE_EXPR:
3067 case EQ_EXPR:
3068 case NE_EXPR:
3069 case MAX_EXPR:
3070 case MIN_EXPR:
3071 case LE_EXPR:
3072 case GE_EXPR:
3073 case LT_EXPR:
3074 case GT_EXPR:
3075 case MEMBER_REF:
3076 return build_x_binary_op
3077 (TREE_CODE (t),
3078 build_expr_from_tree (TREE_OPERAND (t, 0)),
3079 build_expr_from_tree (TREE_OPERAND (t, 1)));
3081 case DOTSTAR_EXPR:
3082 return build_m_component_ref
3083 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3084 build_expr_from_tree (TREE_OPERAND (t, 1)));
3086 case SCOPE_REF:
3087 return build_offset_ref (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1));
3089 case ARRAY_REF:
3090 if (TREE_OPERAND (t, 0) == NULL_TREE)
3091 /* new-type-id */
3092 return build_nt (ARRAY_REF, NULL_TREE,
3093 build_expr_from_tree (TREE_OPERAND (t, 1)));
3094 return grok_array_decl (build_expr_from_tree (TREE_OPERAND (t, 0)),
3095 build_expr_from_tree (TREE_OPERAND (t, 1)));
3097 case SIZEOF_EXPR:
3098 case ALIGNOF_EXPR:
3100 tree r = build_expr_from_tree (TREE_OPERAND (t, 0));
3101 if (!TYPE_P (r))
3102 return TREE_CODE (t) == SIZEOF_EXPR ? expr_sizeof (r) : c_alignof_expr (r);
3103 else
3104 return cxx_sizeof_or_alignof_type (r, TREE_CODE (t), true);
3107 case MODOP_EXPR:
3108 return build_x_modify_expr
3109 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3110 TREE_CODE (TREE_OPERAND (t, 1)),
3111 build_expr_from_tree (TREE_OPERAND (t, 2)));
3113 case ARROW_EXPR:
3114 return build_x_arrow
3115 (build_expr_from_tree (TREE_OPERAND (t, 0)));
3117 case NEW_EXPR:
3118 return build_new
3119 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3120 build_expr_from_tree (TREE_OPERAND (t, 1)),
3121 build_expr_from_tree (TREE_OPERAND (t, 2)),
3122 NEW_EXPR_USE_GLOBAL (t));
3124 case DELETE_EXPR:
3125 return delete_sanity
3126 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3127 build_expr_from_tree (TREE_OPERAND (t, 1)),
3128 DELETE_EXPR_USE_VEC (t), DELETE_EXPR_USE_GLOBAL (t));
3130 case COMPOUND_EXPR:
3131 if (TREE_OPERAND (t, 1) == NULL_TREE)
3132 return build_x_compound_expr
3133 (build_expr_from_tree (TREE_OPERAND (t, 0)));
3134 else
3135 abort ();
3137 case METHOD_CALL_EXPR:
3138 if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
3140 tree ref = TREE_OPERAND (t, 0);
3141 tree name = TREE_OPERAND (ref, 1);
3143 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3144 name = build_nt (TEMPLATE_ID_EXPR,
3145 TREE_OPERAND (name, 0),
3146 build_expr_from_tree (TREE_OPERAND (name, 1)));
3148 return build_scoped_method_call
3149 (build_expr_from_tree (TREE_OPERAND (t, 1)),
3150 build_expr_from_tree (TREE_OPERAND (ref, 0)),
3151 name,
3152 build_expr_from_tree (TREE_OPERAND (t, 2)));
3154 else
3156 tree fn = TREE_OPERAND (t, 0);
3158 /* We can get a TEMPLATE_ID_EXPR here on code like:
3160 x->f<2>();
3162 so we must resolve that. However, we can also get things
3163 like a BIT_NOT_EXPR here, when referring to a destructor,
3164 and things like that are not correctly resolved by
3165 build_expr_from_tree. So, just use build_expr_from_tree
3166 when we really need it. */
3167 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3168 fn = lookup_template_function
3169 (TREE_OPERAND (fn, 0),
3170 build_expr_from_tree (TREE_OPERAND (fn, 1)));
3172 return build_method_call
3173 (build_expr_from_tree (TREE_OPERAND (t, 1)),
3175 build_expr_from_tree (TREE_OPERAND (t, 2)),
3176 NULL_TREE, LOOKUP_NORMAL);
3179 case CALL_EXPR:
3180 if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
3182 tree ref = TREE_OPERAND (t, 0);
3183 tree name = TREE_OPERAND (ref, 1);
3184 tree fn, scope, args;
3186 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3187 name = build_nt (TEMPLATE_ID_EXPR,
3188 TREE_OPERAND (name, 0),
3189 build_expr_from_tree (TREE_OPERAND (name, 1)));
3191 scope = build_expr_from_tree (TREE_OPERAND (ref, 0));
3192 args = build_expr_from_tree (TREE_OPERAND (t, 1));
3193 fn = resolve_scoped_fn_name (scope, name);
3195 return build_call_from_tree (fn, args, 1);
3197 else
3199 tree name = TREE_OPERAND (t, 0);
3200 tree id;
3201 tree args = build_expr_from_tree (TREE_OPERAND (t, 1));
3202 if (args != NULL_TREE && TREE_CODE (name) == LOOKUP_EXPR
3203 && !LOOKUP_EXPR_GLOBAL (name)
3204 && TREE_CODE ((id = TREE_OPERAND (name, 0))) == IDENTIFIER_NODE
3205 && (!current_class_type
3206 || !lookup_member (current_class_type, id, 0, false)))
3208 /* Do Koenig lookup if there are no class members. */
3209 name = do_identifier (id, args);
3211 else if (TREE_CODE (name) == TEMPLATE_ID_EXPR
3212 || ! really_overloaded_fn (name))
3213 name = build_expr_from_tree (name);
3215 if (TREE_CODE (name) == OFFSET_REF)
3216 return build_offset_ref_call_from_tree (name, args);
3217 if (TREE_CODE (name) == COMPONENT_REF)
3218 return finish_object_call_expr (TREE_OPERAND (name, 1),
3219 TREE_OPERAND (name, 0),
3220 args);
3221 name = convert_from_reference (name);
3222 return build_call_from_tree (name, args,
3223 /*disallow_virtual=*/false);
3226 case COND_EXPR:
3227 return build_x_conditional_expr
3228 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3229 build_expr_from_tree (TREE_OPERAND (t, 1)),
3230 build_expr_from_tree (TREE_OPERAND (t, 2)));
3232 case PSEUDO_DTOR_EXPR:
3233 return (finish_pseudo_destructor_expr
3234 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3235 build_expr_from_tree (TREE_OPERAND (t, 1)),
3236 build_expr_from_tree (TREE_OPERAND (t, 2))));
3238 case TREE_LIST:
3240 tree purpose, value, chain;
3242 if (t == void_list_node)
3243 return t;
3245 purpose = TREE_PURPOSE (t);
3246 if (purpose)
3247 purpose = build_expr_from_tree (purpose);
3248 value = TREE_VALUE (t);
3249 if (value)
3250 value = build_expr_from_tree (value);
3251 chain = TREE_CHAIN (t);
3252 if (chain && chain != void_type_node)
3253 chain = build_expr_from_tree (chain);
3254 return tree_cons (purpose, value, chain);
3257 case COMPONENT_REF:
3259 tree object = build_expr_from_tree (TREE_OPERAND (t, 0));
3260 tree member = TREE_OPERAND (t, 1);
3262 if (!CLASS_TYPE_P (TREE_TYPE (object)))
3264 if (TREE_CODE (member) == BIT_NOT_EXPR)
3265 return finish_pseudo_destructor_expr (object,
3266 NULL_TREE,
3267 TREE_TYPE (object));
3268 else if (TREE_CODE (member) == SCOPE_REF
3269 && (TREE_CODE (TREE_OPERAND (member, 1)) == BIT_NOT_EXPR))
3270 return finish_pseudo_destructor_expr (object,
3271 TREE_OPERAND (t, 0),
3272 TREE_TYPE (object));
3274 else if (TREE_CODE (member) == SCOPE_REF
3275 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
3277 tree tmpl;
3278 tree args;
3280 /* Lookup the template functions now that we know what the
3281 scope is. */
3282 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
3283 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
3284 member = lookup_qualified_name (TREE_OPERAND (member, 0),
3285 tmpl,
3286 /*is_type=*/0,
3287 /*flags=*/0);
3288 if (BASELINK_P (member))
3289 BASELINK_FUNCTIONS (member)
3290 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
3291 args);
3292 else
3294 error ("`%D' is not a member of `%T'",
3295 tmpl, TREE_TYPE (object));
3296 return error_mark_node;
3301 return finish_class_member_access_expr (object, member);
3304 case THROW_EXPR:
3305 return build_throw (build_expr_from_tree (TREE_OPERAND (t, 0)));
3307 case CONSTRUCTOR:
3309 tree r;
3310 tree elts;
3311 tree type = TREE_TYPE (t);
3312 bool purpose_p;
3314 /* digest_init will do the wrong thing if we let it. */
3315 if (type && TYPE_PTRMEMFUNC_P (type))
3316 return t;
3318 r = NULL_TREE;
3319 /* We do not want to process the purpose of aggregate
3320 initializers as they are identifier nodes which will be
3321 looked up by digest_init. */
3322 purpose_p = !(type && IS_AGGR_TYPE (type));
3323 for (elts = CONSTRUCTOR_ELTS (t); elts; elts = TREE_CHAIN (elts))
3325 tree purpose = TREE_PURPOSE (elts);
3326 tree value = TREE_VALUE (elts);
3328 if (purpose && purpose_p)
3329 purpose = build_expr_from_tree (purpose);
3330 value = build_expr_from_tree (value);
3331 r = tree_cons (purpose, value, r);
3334 r = build_constructor (NULL_TREE, nreverse (r));
3335 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
3337 if (type)
3338 return digest_init (type, r, 0);
3339 return r;
3342 case TYPEID_EXPR:
3343 if (TYPE_P (TREE_OPERAND (t, 0)))
3344 return get_typeid (TREE_OPERAND (t, 0));
3345 return build_typeid (build_expr_from_tree (TREE_OPERAND (t, 0)));
3347 case PARM_DECL:
3348 case VAR_DECL:
3349 return convert_from_reference (t);
3351 case VA_ARG_EXPR:
3352 return build_va_arg (build_expr_from_tree (TREE_OPERAND (t, 0)),
3353 TREE_TYPE (t));
3355 default:
3356 return t;
3360 /* FN is an OFFSET_REF indicating the function to call in parse-tree
3361 form; it has not yet been semantically analyzed. ARGS are the
3362 arguments to the function. They have already been semantically
3363 analzyed. */
3365 tree
3366 build_offset_ref_call_from_tree (tree fn, tree args)
3368 tree object_addr;
3370 my_friendly_assert (TREE_CODE (fn) == OFFSET_REF, 20020725);
3372 /* A qualified name corresponding to a non-static member
3373 function or a pointer-to-member is represented as an
3374 OFFSET_REF.
3376 For both of these function calls, FN will be an OFFSET_REF.
3378 struct A { void f(); };
3379 void A::f() { (A::f) (); }
3381 struct B { void g(); };
3382 void (B::*p)();
3383 void B::g() { (this->*p)(); } */
3385 /* This code is not really correct (for example, it does not
3386 handle the case that `A::f' is overloaded), but it is
3387 historically how we have handled this situation. */
3388 if (TREE_CODE (TREE_OPERAND (fn, 1)) == FIELD_DECL)
3389 /* This case should now be handled elsewhere. */
3390 abort ();
3391 else
3393 object_addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (fn, 0), 0);
3394 fn = TREE_OPERAND (fn, 1);
3395 fn = get_member_function_from_ptrfunc (&object_addr, fn);
3396 args = tree_cons (NULL_TREE, object_addr, args);
3398 return build_function_call (fn, args);
3401 /* FN indicates the function to call. Name resolution has been
3402 performed on FN. ARGS are the arguments to the function. They
3403 have already been semantically analyzed. DISALLOW_VIRTUAL is true
3404 if the function call should be determined at compile time, even if
3405 FN is virtual. */
3407 tree
3408 build_call_from_tree (tree fn, tree args, bool disallow_virtual)
3410 tree template_args;
3411 tree template_id;
3412 tree f;
3414 /* Check to see that name lookup has already been performed. */
3415 my_friendly_assert (TREE_CODE (fn) != OFFSET_REF, 20020725);
3416 my_friendly_assert (TREE_CODE (fn) != SCOPE_REF, 20020725);
3418 /* In the future all of this should be eliminated. Instead,
3419 name-lookup for a member function should simply return a
3420 baselink, instead of a FUNCTION_DECL, TEMPLATE_DECL, or
3421 TEMPLATE_ID_EXPR. */
3423 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3425 template_id = fn;
3426 template_args = TREE_OPERAND (fn, 1);
3427 fn = TREE_OPERAND (fn, 0);
3429 else
3431 template_id = NULL_TREE;
3432 template_args = NULL_TREE;
3435 f = (TREE_CODE (fn) == OVERLOAD) ? get_first_fn (fn) : fn;
3436 /* Make sure we have a baselink (rather than simply a
3437 FUNCTION_DECL) for a member function. */
3438 if (current_class_type
3439 && ((TREE_CODE (f) == FUNCTION_DECL
3440 && DECL_FUNCTION_MEMBER_P (f))
3441 || (DECL_FUNCTION_TEMPLATE_P (f)
3442 && DECL_FUNCTION_MEMBER_P (f))))
3444 f = lookup_member (current_class_type, DECL_NAME (f),
3445 /*protect=*/1, /*want_type=*/false);
3446 if (f)
3447 fn = f;
3450 if (template_id)
3452 if (BASELINK_P (fn))
3453 BASELINK_FUNCTIONS (fn) = build_nt (TEMPLATE_ID_EXPR,
3454 BASELINK_FUNCTIONS (fn),
3455 template_args);
3456 else
3457 fn = template_id;
3460 return finish_call_expr (fn, args, disallow_virtual);
3463 /* Returns true if ROOT (a namespace, class, or function) encloses
3464 CHILD. CHILD may be either a class type or a namespace. */
3466 bool
3467 is_ancestor (tree root, tree child)
3469 my_friendly_assert ((TREE_CODE (root) == NAMESPACE_DECL
3470 || TREE_CODE (root) == FUNCTION_DECL
3471 || CLASS_TYPE_P (root)), 20030307);
3472 my_friendly_assert ((TREE_CODE (child) == NAMESPACE_DECL
3473 || CLASS_TYPE_P (child)),
3474 20030307);
3476 /* The global namespace encloses everything. */
3477 if (root == global_namespace)
3478 return true;
3480 while (true)
3482 /* If we've run out of scopes, stop. */
3483 if (!child)
3484 return false;
3485 /* If we've reached the ROOT, it encloses CHILD. */
3486 if (root == child)
3487 return true;
3488 /* Go out one level. */
3489 if (TYPE_P (child))
3490 child = TYPE_NAME (child);
3491 child = DECL_CONTEXT (child);
3496 /* Return the namespace that is the common ancestor
3497 of two given namespaces. */
3499 tree
3500 namespace_ancestor (tree ns1, tree ns2)
3502 timevar_push (TV_NAME_LOOKUP);
3503 if (is_ancestor (ns1, ns2))
3504 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ns1);
3505 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
3506 namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2));
3509 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
3510 directive is not directly from the source. Also find the common
3511 ancestor and let our users know about the new namespace */
3512 static void
3513 add_using_namespace (tree user, tree used, bool indirect)
3515 tree t;
3516 timevar_push (TV_NAME_LOOKUP);
3517 /* Using oneself is a no-op. */
3518 if (user == used)
3520 timevar_pop (TV_NAME_LOOKUP);
3521 return;
3523 my_friendly_assert (TREE_CODE (user) == NAMESPACE_DECL, 380);
3524 my_friendly_assert (TREE_CODE (used) == NAMESPACE_DECL, 380);
3525 /* Check if we already have this. */
3526 t = purpose_member (used, DECL_NAMESPACE_USING (user));
3527 if (t != NULL_TREE)
3529 if (!indirect)
3530 /* Promote to direct usage. */
3531 TREE_INDIRECT_USING (t) = 0;
3532 timevar_pop (TV_NAME_LOOKUP);
3533 return;
3536 /* Add used to the user's using list. */
3537 DECL_NAMESPACE_USING (user)
3538 = tree_cons (used, namespace_ancestor (user, used),
3539 DECL_NAMESPACE_USING (user));
3541 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
3543 /* Add user to the used's users list. */
3544 DECL_NAMESPACE_USERS (used)
3545 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
3547 /* Recursively add all namespaces used. */
3548 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
3549 /* indirect usage */
3550 add_using_namespace (user, TREE_PURPOSE (t), 1);
3552 /* Tell everyone using us about the new used namespaces. */
3553 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
3554 add_using_namespace (TREE_PURPOSE (t), used, 1);
3555 timevar_pop (TV_NAME_LOOKUP);
3558 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
3559 duplicates. The first list becomes the tail of the result.
3561 The algorithm is O(n^2). We could get this down to O(n log n) by
3562 doing a sort on the addresses of the functions, if that becomes
3563 necessary. */
3565 static tree
3566 merge_functions (tree s1, tree s2)
3568 for (; s2; s2 = OVL_NEXT (s2))
3570 tree fn2 = OVL_CURRENT (s2);
3571 tree fns1;
3573 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
3575 tree fn1 = OVL_CURRENT (fns1);
3577 /* If the function from S2 is already in S1, there is no
3578 need to add it again. For `extern "C"' functions, we
3579 might have two FUNCTION_DECLs for the same function, in
3580 different namespaces; again, we only need one of them. */
3581 if (fn1 == fn2
3582 || (DECL_EXTERN_C_P (fn1) && DECL_EXTERN_C_P (fn2)
3583 && DECL_NAME (fn1) == DECL_NAME (fn2)))
3584 break;
3587 /* If we exhausted all of the functions in S1, FN2 is new. */
3588 if (!fns1)
3589 s1 = build_overload (fn2, s1);
3591 return s1;
3594 /* This should return an error not all definitions define functions.
3595 It is not an error if we find two functions with exactly the
3596 same signature, only if these are selected in overload resolution.
3597 old is the current set of bindings, new the freshly-found binding.
3598 XXX Do we want to give *all* candidates in case of ambiguity?
3599 XXX In what way should I treat extern declarations?
3600 XXX I don't want to repeat the entire duplicate_decls here */
3602 static cxx_binding *
3603 ambiguous_decl (tree name, cxx_binding *old, cxx_binding *new, int flags)
3605 tree val, type;
3606 my_friendly_assert (old != NULL, 393);
3607 /* Copy the value. */
3608 val = BINDING_VALUE (new);
3609 if (val)
3610 switch (TREE_CODE (val))
3612 case TEMPLATE_DECL:
3613 /* If we expect types or namespaces, and not templates,
3614 or this is not a template class. */
3615 if (LOOKUP_QUALIFIERS_ONLY (flags)
3616 && !DECL_CLASS_TEMPLATE_P (val))
3617 val = NULL_TREE;
3618 break;
3619 case TYPE_DECL:
3620 if (LOOKUP_NAMESPACES_ONLY (flags))
3621 val = NULL_TREE;
3622 break;
3623 case NAMESPACE_DECL:
3624 if (LOOKUP_TYPES_ONLY (flags))
3625 val = NULL_TREE;
3626 break;
3627 case FUNCTION_DECL:
3628 /* Ignore built-in functions that are still anticipated. */
3629 if (LOOKUP_QUALIFIERS_ONLY (flags) || DECL_ANTICIPATED (val))
3630 val = NULL_TREE;
3631 break;
3632 default:
3633 if (LOOKUP_QUALIFIERS_ONLY (flags))
3634 val = NULL_TREE;
3637 if (!BINDING_VALUE (old))
3638 BINDING_VALUE (old) = val;
3639 else if (val && val != BINDING_VALUE (old))
3641 if (is_overloaded_fn (BINDING_VALUE (old)) && is_overloaded_fn (val))
3642 BINDING_VALUE (old) = merge_functions (BINDING_VALUE (old), val);
3643 else
3645 /* Some declarations are functions, some are not. */
3646 if (flags & LOOKUP_COMPLAIN)
3648 /* If we've already given this error for this lookup,
3649 BINDING_VALUE (old) is error_mark_node, so let's not
3650 repeat ourselves. */
3651 if (BINDING_VALUE (old) != error_mark_node)
3653 error ("use of `%D' is ambiguous", name);
3654 cp_error_at (" first declared as `%#D' here",
3655 BINDING_VALUE (old));
3657 cp_error_at (" also declared as `%#D' here", val);
3659 BINDING_VALUE (old) = error_mark_node;
3662 /* ... and copy the type. */
3663 type = BINDING_TYPE (new);
3664 if (LOOKUP_NAMESPACES_ONLY (flags))
3665 type = NULL_TREE;
3666 if (!BINDING_TYPE (old))
3667 BINDING_TYPE (old) = type;
3668 else if (type && BINDING_TYPE (old) != type)
3670 if (flags & LOOKUP_COMPLAIN)
3672 error ("`%D' denotes an ambiguous type",name);
3673 error ("%H first type here",
3674 &DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (BINDING_TYPE (old))));
3675 error ("%H other type here",
3676 &DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)));
3679 return old;
3682 /* Subroutine of unualified_namespace_lookup:
3683 Add the bindings of NAME in used namespaces to VAL.
3684 We are currently looking for names in namespace SCOPE, so we
3685 look through USINGS for using-directives of namespaces
3686 which have SCOPE as a common ancestor with the current scope.
3687 Returns false on errors. */
3689 bool
3690 lookup_using_namespace (tree name, cxx_binding *val, tree usings, tree scope,
3691 int flags, tree *spacesp)
3693 tree iter;
3694 timevar_push (TV_NAME_LOOKUP);
3695 /* Iterate over all used namespaces in current, searching for using
3696 directives of scope. */
3697 for (iter = usings; iter; iter = TREE_CHAIN (iter))
3698 if (TREE_VALUE (iter) == scope)
3700 tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
3701 cxx_binding *val1 =
3702 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (used), name);
3703 if (spacesp)
3704 *spacesp = tree_cons (used, NULL_TREE, *spacesp);
3705 /* Resolve ambiguities. */
3706 if (val1)
3707 val = ambiguous_decl (name, val, val1, flags);
3709 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
3710 BINDING_VALUE (val) != error_mark_node);
3713 /* [namespace.qual]
3714 Accepts the NAME to lookup and its qualifying SCOPE.
3715 Returns the name/type pair found into the cxx_binding *RESULT,
3716 or false on error. */
3718 bool
3719 qualified_lookup_using_namespace (tree name, tree scope, cxx_binding *result,
3720 int flags)
3722 /* Maintain a list of namespaces visited... */
3723 tree seen = NULL_TREE;
3724 /* ... and a list of namespace yet to see. */
3725 tree todo = NULL_TREE;
3726 tree usings;
3727 timevar_push (TV_NAME_LOOKUP);
3728 /* Look through namespace aliases. */
3729 scope = ORIGINAL_NAMESPACE (scope);
3730 while (scope && result->value != error_mark_node)
3732 cxx_binding *binding =
3733 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3734 seen = tree_cons (scope, NULL_TREE, seen);
3735 if (binding)
3736 result = ambiguous_decl (name, result, binding, flags);
3737 if (!BINDING_VALUE (result) && !BINDING_TYPE (result))
3738 /* Consider using directives. */
3739 for (usings = DECL_NAMESPACE_USING (scope); usings;
3740 usings = TREE_CHAIN (usings))
3741 /* If this was a real directive, and we have not seen it. */
3742 if (!TREE_INDIRECT_USING (usings)
3743 && !purpose_member (TREE_PURPOSE (usings), seen))
3744 todo = tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo);
3745 if (todo)
3747 scope = TREE_PURPOSE (todo);
3748 todo = TREE_CHAIN (todo);
3750 else
3751 scope = NULL_TREE; /* If there never was a todo list. */
3753 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result->value != error_mark_node);
3756 /* [namespace.memdef]/2 */
3758 /* Set the context of a declaration to scope. Complain if we are not
3759 outside scope. */
3761 void
3762 set_decl_namespace (tree decl, tree scope, bool friendp)
3764 tree old;
3766 /* Get rid of namespace aliases. */
3767 scope = ORIGINAL_NAMESPACE (scope);
3769 /* It is ok for friends to be qualified in parallel space. */
3770 if (!friendp && !is_ancestor (current_namespace, scope))
3771 error ("declaration of `%D' not in a namespace surrounding `%D'",
3772 decl, scope);
3773 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3774 if (scope != current_namespace)
3776 /* See whether this has been declared in the namespace. */
3777 old = namespace_binding (DECL_NAME (decl), scope);
3778 if (!old)
3779 /* No old declaration at all. */
3780 goto complain;
3781 /* A template can be explicitly specialized in any namespace. */
3782 if (processing_explicit_instantiation)
3783 return;
3784 if (!is_overloaded_fn (decl))
3785 /* Don't compare non-function decls with decls_match here,
3786 since it can't check for the correct constness at this
3787 point. pushdecl will find those errors later. */
3788 return;
3789 /* Since decl is a function, old should contain a function decl. */
3790 if (!is_overloaded_fn (old))
3791 goto complain;
3792 if (processing_template_decl || processing_specialization)
3793 /* We have not yet called push_template_decl to turn a
3794 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations
3795 won't match. But, we'll check later, when we construct the
3796 template. */
3797 return;
3798 if (is_overloaded_fn (old))
3800 for (; old; old = OVL_NEXT (old))
3801 if (decls_match (decl, OVL_CURRENT (old)))
3802 return;
3804 else
3805 if (decls_match (decl, old))
3806 return;
3808 else
3809 return;
3810 complain:
3811 error ("`%D' should have been declared inside `%D'",
3812 decl, scope);
3815 /* Compute the namespace where a declaration is defined. */
3817 static tree
3818 decl_namespace (tree decl)
3820 timevar_push (TV_NAME_LOOKUP);
3821 if (TYPE_P (decl))
3822 decl = TYPE_STUB_DECL (decl);
3823 while (DECL_CONTEXT (decl))
3825 decl = DECL_CONTEXT (decl);
3826 if (TREE_CODE (decl) == NAMESPACE_DECL)
3827 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
3828 if (TYPE_P (decl))
3829 decl = TYPE_STUB_DECL (decl);
3830 my_friendly_assert (DECL_P (decl), 390);
3833 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, global_namespace);
3836 /* Return the namespace where the current declaration is declared. */
3838 tree
3839 current_decl_namespace (void)
3841 tree result;
3842 /* If we have been pushed into a different namespace, use it. */
3843 if (decl_namespace_list)
3844 return TREE_PURPOSE (decl_namespace_list);
3846 if (current_class_type)
3847 result = decl_namespace (TYPE_STUB_DECL (current_class_type));
3848 else if (current_function_decl)
3849 result = decl_namespace (current_function_decl);
3850 else
3851 result = current_namespace;
3852 return result;
3855 /* Temporarily set the namespace for the current declaration. */
3857 void
3858 push_decl_namespace (tree decl)
3860 if (TREE_CODE (decl) != NAMESPACE_DECL)
3861 decl = decl_namespace (decl);
3862 decl_namespace_list = tree_cons (ORIGINAL_NAMESPACE (decl),
3863 NULL_TREE, decl_namespace_list);
3866 void
3867 pop_decl_namespace (void)
3869 decl_namespace_list = TREE_CHAIN (decl_namespace_list);
3872 /* Enter a class or namespace scope. */
3874 void
3875 push_scope (tree t)
3877 if (TREE_CODE (t) == NAMESPACE_DECL)
3878 push_decl_namespace (t);
3879 else if CLASS_TYPE_P (t)
3880 push_nested_class (t);
3883 /* Leave scope pushed by push_scope. */
3885 void
3886 pop_scope (tree t)
3888 if (TREE_CODE (t) == NAMESPACE_DECL)
3889 pop_decl_namespace ();
3890 else if CLASS_TYPE_P (t)
3891 pop_nested_class ();
3894 /* [basic.lookup.koenig] */
3895 /* A nonzero return value in the functions below indicates an error. */
3897 struct arg_lookup
3899 tree name;
3900 tree namespaces;
3901 tree classes;
3902 tree functions;
3905 static bool arg_assoc (struct arg_lookup*, tree);
3906 static bool arg_assoc_args (struct arg_lookup*, tree);
3907 static bool arg_assoc_type (struct arg_lookup*, tree);
3908 static bool add_function (struct arg_lookup *, tree);
3909 static bool arg_assoc_namespace (struct arg_lookup *, tree);
3910 static bool arg_assoc_class (struct arg_lookup *, tree);
3911 static bool arg_assoc_template_arg (struct arg_lookup*, tree);
3913 /* Add a function to the lookup structure.
3914 Returns true on error. */
3916 static bool
3917 add_function (struct arg_lookup *k, tree fn)
3919 /* We used to check here to see if the function was already in the list,
3920 but that's O(n^2), which is just too expensive for function lookup.
3921 Now we deal with the occasional duplicate in joust. In doing this, we
3922 assume that the number of duplicates will be small compared to the
3923 total number of functions being compared, which should usually be the
3924 case. */
3926 /* We must find only functions, or exactly one non-function. */
3927 if (!k->functions)
3928 k->functions = fn;
3929 else if (is_overloaded_fn (k->functions) && is_overloaded_fn (fn))
3930 k->functions = build_overload (fn, k->functions);
3931 else
3933 tree f1 = OVL_CURRENT (k->functions);
3934 tree f2 = fn;
3935 if (is_overloaded_fn (f1))
3937 fn = f1; f1 = f2; f2 = fn;
3939 cp_error_at ("`%D' is not a function,", f1);
3940 cp_error_at (" conflict with `%D'", f2);
3941 error (" in call to `%D'", k->name);
3942 return true;
3945 return false;
3948 /* Add functions of a namespace to the lookup structure.
3949 Returns true on error. */
3951 static bool
3952 arg_assoc_namespace (struct arg_lookup *k, tree scope)
3954 tree value;
3956 if (purpose_member (scope, k->namespaces))
3957 return 0;
3958 k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces);
3960 value = namespace_binding (k->name, scope);
3961 if (!value)
3962 return false;
3964 for (; value; value = OVL_NEXT (value))
3965 if (add_function (k, OVL_CURRENT (value)))
3966 return true;
3968 return false;
3971 /* Adds everything associated with a template argument to the lookup
3972 structure. Returns true on error. */
3974 static bool
3975 arg_assoc_template_arg (struct arg_lookup *k, tree arg)
3977 /* [basic.lookup.koenig]
3979 If T is a template-id, its associated namespaces and classes are
3980 ... the namespaces and classes associated with the types of the
3981 template arguments provided for template type parameters
3982 (excluding template template parameters); the namespaces in which
3983 any template template arguments are defined; and the classes in
3984 which any member templates used as template template arguments
3985 are defined. [Note: non-type template arguments do not
3986 contribute to the set of associated namespaces. ] */
3988 /* Consider first template template arguments. */
3989 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3990 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
3991 return false;
3992 else if (TREE_CODE (arg) == TEMPLATE_DECL)
3994 tree ctx = CP_DECL_CONTEXT (arg);
3996 /* It's not a member template. */
3997 if (TREE_CODE (ctx) == NAMESPACE_DECL)
3998 return arg_assoc_namespace (k, ctx);
3999 /* Otherwise, it must be member template. */
4000 else
4001 return arg_assoc_class (k, ctx);
4003 /* It's not a template template argument, but it is a type template
4004 argument. */
4005 else if (TYPE_P (arg))
4006 return arg_assoc_type (k, arg);
4007 /* It's a non-type template argument. */
4008 else
4009 return false;
4012 /* Adds everything associated with class to the lookup structure.
4013 Returns true on error. */
4015 static bool
4016 arg_assoc_class (struct arg_lookup *k, tree type)
4018 tree list, friends, context;
4019 int i;
4021 /* Backend build structures, such as __builtin_va_list, aren't
4022 affected by all this. */
4023 if (!CLASS_TYPE_P (type))
4024 return false;
4026 if (purpose_member (type, k->classes))
4027 return false;
4028 k->classes = tree_cons (type, NULL_TREE, k->classes);
4030 context = decl_namespace (TYPE_MAIN_DECL (type));
4031 if (arg_assoc_namespace (k, context))
4032 return true;
4034 /* Process baseclasses. */
4035 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); i++)
4036 if (arg_assoc_class (k, TYPE_BINFO_BASETYPE (type, i)))
4037 return true;
4039 /* Process friends. */
4040 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
4041 list = TREE_CHAIN (list))
4042 if (k->name == FRIEND_NAME (list))
4043 for (friends = FRIEND_DECLS (list); friends;
4044 friends = TREE_CHAIN (friends))
4045 /* Only interested in global functions with potentially hidden
4046 (i.e. unqualified) declarations. */
4047 if (CP_DECL_CONTEXT (TREE_VALUE (friends)) == context)
4048 if (add_function (k, TREE_VALUE (friends)))
4049 return true;
4051 /* Process template arguments. */
4052 if (CLASSTYPE_TEMPLATE_INFO (type))
4054 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
4055 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
4056 arg_assoc_template_arg (k, TREE_VEC_ELT (list, i));
4059 return false;
4062 /* Adds everything associated with a given type.
4063 Returns 1 on error. */
4065 static bool
4066 arg_assoc_type (struct arg_lookup *k, tree type)
4068 /* As we do not get the type of non-type dependent expressions
4069 right, we can end up with such things without a type. */
4070 if (!type)
4071 return false;
4073 switch (TREE_CODE (type))
4075 case ERROR_MARK:
4076 return false;
4077 case VOID_TYPE:
4078 case INTEGER_TYPE:
4079 case REAL_TYPE:
4080 case COMPLEX_TYPE:
4081 case VECTOR_TYPE:
4082 case CHAR_TYPE:
4083 case BOOLEAN_TYPE:
4084 return false;
4085 case RECORD_TYPE:
4086 if (TYPE_PTRMEMFUNC_P (type))
4087 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
4088 return arg_assoc_class (k, type);
4089 case POINTER_TYPE:
4090 case REFERENCE_TYPE:
4091 case ARRAY_TYPE:
4092 return arg_assoc_type (k, TREE_TYPE (type));
4093 case UNION_TYPE:
4094 case ENUMERAL_TYPE:
4095 return arg_assoc_namespace (k, decl_namespace (TYPE_MAIN_DECL (type)));
4096 case OFFSET_TYPE:
4097 /* Pointer to member: associate class type and value type. */
4098 if (arg_assoc_type (k, TYPE_OFFSET_BASETYPE (type)))
4099 return true;
4100 return arg_assoc_type (k, TREE_TYPE (type));
4101 case METHOD_TYPE:
4102 /* The basetype is referenced in the first arg type, so just
4103 fall through. */
4104 case FUNCTION_TYPE:
4105 /* Associate the parameter types. */
4106 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
4107 return true;
4108 /* Associate the return type. */
4109 return arg_assoc_type (k, TREE_TYPE (type));
4110 case TEMPLATE_TYPE_PARM:
4111 case BOUND_TEMPLATE_TEMPLATE_PARM:
4112 return false;
4113 case TYPENAME_TYPE:
4114 return false;
4115 case LANG_TYPE:
4116 if (type == unknown_type_node)
4117 return false;
4118 /* else fall through */
4119 default:
4120 abort ();
4122 return false;
4125 /* Adds everything associated with arguments. Returns true on error. */
4127 static bool
4128 arg_assoc_args (struct arg_lookup *k, tree args)
4130 for (; args; args = TREE_CHAIN (args))
4131 if (arg_assoc (k, TREE_VALUE (args)))
4132 return true;
4133 return false;
4136 /* Adds everything associated with a given tree_node. Returns 1 on error. */
4138 static bool
4139 arg_assoc (struct arg_lookup *k, tree n)
4141 if (n == error_mark_node)
4142 return false;
4144 if (TYPE_P (n))
4145 return arg_assoc_type (k, n);
4147 if (! type_unknown_p (n))
4148 return arg_assoc_type (k, TREE_TYPE (n));
4150 if (TREE_CODE (n) == ADDR_EXPR)
4151 n = TREE_OPERAND (n, 0);
4152 if (TREE_CODE (n) == COMPONENT_REF)
4153 n = TREE_OPERAND (n, 1);
4154 if (TREE_CODE (n) == OFFSET_REF)
4155 n = TREE_OPERAND (n, 1);
4156 while (TREE_CODE (n) == TREE_LIST)
4157 n = TREE_VALUE (n);
4158 if (TREE_CODE (n) == BASELINK)
4159 n = BASELINK_FUNCTIONS (n);
4161 if (TREE_CODE (n) == FUNCTION_DECL)
4162 return arg_assoc_type (k, TREE_TYPE (n));
4163 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
4165 /* [basic.lookup.koenig]
4167 If T is a template-id, its associated namespaces and classes
4168 are the namespace in which the template is defined; for
4169 member templates, the member template's class... */
4170 tree template = TREE_OPERAND (n, 0);
4171 tree args = TREE_OPERAND (n, 1);
4172 tree ctx;
4173 tree arg;
4175 if (TREE_CODE (template) == COMPONENT_REF)
4176 template = TREE_OPERAND (template, 1);
4178 /* First, the template. There may actually be more than one if
4179 this is an overloaded function template. But, in that case,
4180 we only need the first; all the functions will be in the same
4181 namespace. */
4182 template = OVL_CURRENT (template);
4184 ctx = CP_DECL_CONTEXT (template);
4186 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4188 if (arg_assoc_namespace (k, ctx) == 1)
4189 return true;
4191 /* It must be a member template. */
4192 else if (arg_assoc_class (k, ctx) == 1)
4193 return true;
4195 /* Now the arguments. */
4196 for (arg = args; arg != NULL_TREE; arg = TREE_CHAIN (arg))
4197 if (arg_assoc_template_arg (k, TREE_VALUE (arg)) == 1)
4198 return true;
4200 else
4202 my_friendly_assert (TREE_CODE (n) == OVERLOAD, 980715);
4204 for (; n; n = OVL_CHAIN (n))
4205 if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
4206 return true;
4209 return false;
4212 /* Performs Koenig lookup depending on arguments, where fns
4213 are the functions found in normal lookup. */
4215 tree
4216 lookup_arg_dependent (tree name, tree fns, tree args)
4218 struct arg_lookup k;
4219 tree fn = NULL_TREE;
4221 timevar_push (TV_NAME_LOOKUP);
4222 k.name = name;
4223 k.functions = fns;
4224 k.classes = NULL_TREE;
4226 /* Note that we've already looked at some namespaces during normal
4227 unqualified lookup, unless we found a decl in function scope. */
4228 if (fns)
4229 fn = OVL_CURRENT (fns);
4230 if (fn && TREE_CODE (fn) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (fn))
4231 k.namespaces = NULL_TREE;
4232 else
4233 unqualified_namespace_lookup (name, 0, &k.namespaces);
4235 arg_assoc_args (&k, args);
4236 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, k.functions);
4239 /* Process a namespace-alias declaration. */
4241 void
4242 do_namespace_alias (tree alias, tree namespace)
4244 if (TREE_CODE (namespace) != NAMESPACE_DECL)
4246 /* The parser did not find it, so it's not there. */
4247 error ("unknown namespace `%D'", namespace);
4248 return;
4251 namespace = ORIGINAL_NAMESPACE (namespace);
4253 /* Build the alias. */
4254 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
4255 DECL_NAMESPACE_ALIAS (alias) = namespace;
4256 DECL_EXTERNAL (alias) = 1;
4257 pushdecl (alias);
4260 /* Check a non-member using-declaration. Return the name and scope
4261 being used, and the USING_DECL, or NULL_TREE on failure. */
4263 static tree
4264 validate_nonmember_using_decl (tree decl, tree *scope, tree *name)
4266 *scope = global_namespace;
4267 *name = NULL_TREE;
4269 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
4271 *name = TREE_OPERAND (decl, 0);
4272 /* 7.3.3/5
4273 A using-declaration shall not name a template-id. */
4274 error ("a using-declaration cannot specify a template-id. Try `using %D'", *name);
4275 return NULL_TREE;
4278 if (TREE_CODE (decl) == NAMESPACE_DECL)
4280 error ("namespace `%D' not allowed in using-declaration", decl);
4281 return NULL_TREE;
4284 if (TREE_CODE (decl) == SCOPE_REF)
4286 /* It's a nested name with template parameter dependent scope.
4287 This can only be using-declaration for class member. */
4288 error ("`%T' is not a namespace", TREE_OPERAND (decl, 0));
4289 return NULL_TREE;
4292 if (is_overloaded_fn (decl))
4293 decl = get_first_fn (decl);
4295 my_friendly_assert (DECL_P (decl), 20020908);
4297 if (TREE_CODE (decl) == CONST_DECL)
4298 /* Enumeration constants to not have DECL_CONTEXT set. */
4299 *scope = TYPE_CONTEXT (TREE_TYPE (decl));
4300 else
4301 *scope = DECL_CONTEXT (decl);
4302 if (!*scope)
4303 *scope = global_namespace;
4305 /* [namespace.udecl]
4306 A using-declaration for a class member shall be a
4307 member-declaration. */
4308 if (TYPE_P (*scope))
4310 error ("`%T' is not a namespace", *scope);
4311 return NULL_TREE;
4313 *name = DECL_NAME (decl);
4314 /* Make a USING_DECL. */
4315 return push_using_decl (*scope, *name);
4318 /* Process local and global using-declarations. */
4320 static void
4321 do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
4322 tree *newval, tree *newtype)
4324 cxx_binding decls;
4326 *newval = *newtype = NULL_TREE;
4327 cxx_binding_clear (&decls);
4328 if (!qualified_lookup_using_namespace (name, scope, &decls, 0))
4329 /* Lookup error */
4330 return;
4332 if (!decls.value && !decls.type)
4334 error ("`%D' not declared", name);
4335 return;
4338 /* Check for using functions. */
4339 if (decls.value && is_overloaded_fn (decls.value))
4341 tree tmp, tmp1;
4343 if (oldval && !is_overloaded_fn (oldval))
4345 if (!DECL_IMPLICIT_TYPEDEF_P (oldval))
4346 error ("`%D' is already declared in this scope", name);
4347 oldval = NULL_TREE;
4350 *newval = oldval;
4351 for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
4353 tree new_fn = OVL_CURRENT (tmp);
4355 /* [namespace.udecl]
4357 If a function declaration in namespace scope or block
4358 scope has the same name and the same parameter types as a
4359 function introduced by a using declaration the program is
4360 ill-formed. */
4361 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
4363 tree old_fn = OVL_CURRENT (tmp1);
4365 if (new_fn == old_fn)
4366 /* The function already exists in the current namespace. */
4367 break;
4368 else if (OVL_USED (tmp1))
4369 continue; /* this is a using decl */
4370 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
4371 TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
4373 /* There was already a non-using declaration in
4374 this scope with the same parameter types. If both
4375 are the same extern "C" functions, that's ok. */
4376 if (decls_match (new_fn, old_fn))
4378 /* If the OLD_FN was a builtin, there is now a
4379 real declaration. */
4380 if (DECL_ANTICIPATED (old_fn))
4381 DECL_ANTICIPATED (old_fn) = 0;
4382 break;
4384 else if (!DECL_ANTICIPATED (old_fn))
4386 /* If the OLD_FN was really declared, the
4387 declarations don't match. */
4388 error ("`%D' is already declared in this scope", name);
4389 break;
4392 /* If the OLD_FN was not really there, just ignore
4393 it and keep going. */
4397 /* If we broke out of the loop, there's no reason to add
4398 this function to the using declarations for this
4399 scope. */
4400 if (tmp1)
4401 continue;
4403 *newval = build_overload (OVL_CURRENT (tmp), *newval);
4404 if (TREE_CODE (*newval) != OVERLOAD)
4405 *newval = ovl_cons (*newval, NULL_TREE);
4406 OVL_USED (*newval) = 1;
4409 else
4411 *newval = decls.value;
4412 if (oldval && !decls_match (*newval, oldval))
4413 error ("`%D' is already declared in this scope", name);
4416 *newtype = decls.type;
4417 if (oldtype && *newtype && !same_type_p (oldtype, *newtype))
4419 error ("using declaration `%D' introduced ambiguous type `%T'",
4420 name, oldtype);
4421 return;
4425 /* Process a using-declaration not appearing in class or local scope. */
4427 void
4428 do_toplevel_using_decl (tree decl)
4430 tree scope, name;
4431 tree oldval, oldtype, newval, newtype;
4432 cxx_binding *binding;
4434 decl = validate_nonmember_using_decl (decl, &scope, &name);
4435 if (decl == NULL_TREE)
4436 return;
4438 binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name);
4440 oldval = BINDING_VALUE (binding);
4441 oldtype = BINDING_TYPE (binding);
4443 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
4445 /* Copy declarations found. */
4446 if (newval)
4447 BINDING_VALUE (binding) = newval;
4448 if (newtype)
4449 BINDING_TYPE (binding) = newtype;
4450 return;
4453 /* Process a using-declaration at function scope. */
4455 void
4456 do_local_using_decl (tree decl)
4458 tree scope, name;
4459 tree oldval, oldtype, newval, newtype;
4461 decl = validate_nonmember_using_decl (decl, &scope, &name);
4462 if (decl == NULL_TREE)
4463 return;
4465 if (building_stmt_tree ()
4466 && at_function_scope_p ())
4467 add_decl_stmt (decl);
4469 oldval = lookup_name_current_level (name);
4470 oldtype = lookup_type_current_level (name);
4472 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
4474 if (newval)
4476 if (is_overloaded_fn (newval))
4478 tree fn, term;
4480 /* We only need to push declarations for those functions
4481 that were not already bound in the current level.
4482 The old value might be NULL_TREE, it might be a single
4483 function, or an OVERLOAD. */
4484 if (oldval && TREE_CODE (oldval) == OVERLOAD)
4485 term = OVL_FUNCTION (oldval);
4486 else
4487 term = oldval;
4488 for (fn = newval; fn && OVL_CURRENT (fn) != term;
4489 fn = OVL_NEXT (fn))
4490 push_overloaded_decl (OVL_CURRENT (fn),
4491 PUSH_LOCAL | PUSH_USING);
4493 else
4494 push_local_binding (name, newval, PUSH_USING);
4496 if (newtype)
4497 set_identifier_type_value (name, newtype);
4500 tree
4501 do_class_using_decl (tree decl)
4503 tree name, value;
4505 if (TREE_CODE (decl) != SCOPE_REF
4506 || !TREE_OPERAND (decl, 0)
4507 || !TYPE_P (TREE_OPERAND (decl, 0)))
4509 error ("using-declaration for non-member at class scope");
4510 return NULL_TREE;
4512 name = TREE_OPERAND (decl, 1);
4513 if (TREE_CODE (name) == BIT_NOT_EXPR)
4515 error ("using-declaration for destructor");
4516 return NULL_TREE;
4518 else if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4520 name = TREE_OPERAND (name, 0);
4521 error ("a using-declaration cannot specify a template-id. Try `using %T::%D'", TREE_OPERAND (decl, 0), name);
4522 return NULL_TREE;
4524 if (TREE_CODE (name) == TYPE_DECL)
4526 tree type = TREE_TYPE (name);
4527 if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (name)))
4529 name = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type));
4530 error ("a using-declaration cannot specify a template-id.");
4531 return NULL_TREE;
4533 name = DECL_NAME (name);
4535 else if (TREE_CODE (name) == TEMPLATE_DECL)
4536 name = DECL_NAME (name);
4537 else if (BASELINK_P (name))
4539 tree fns;
4541 fns = BASELINK_FUNCTIONS (name);
4542 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
4544 fns = TREE_OPERAND (fns, 0);
4545 error ("a using-declaration cannot specify a template-id. Try `using %T::%D'",
4546 BASELINK_ACCESS_BINFO (name),
4547 DECL_NAME (get_first_fn (fns)));
4549 name = DECL_NAME (get_first_fn (fns));
4552 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 980716);
4554 value = build_lang_decl (USING_DECL, name, void_type_node);
4555 DECL_INITIAL (value) = TREE_OPERAND (decl, 0);
4556 return value;
4559 /* Process a using-directive. */
4561 void
4562 do_using_directive (tree namespace)
4564 if (building_stmt_tree ())
4565 add_stmt (build_stmt (USING_STMT, namespace));
4567 /* using namespace A::B::C; */
4568 if (TREE_CODE (namespace) == SCOPE_REF)
4569 namespace = TREE_OPERAND (namespace, 1);
4570 if (TREE_CODE (namespace) == IDENTIFIER_NODE)
4572 /* Lookup in lexer did not find a namespace. */
4573 if (!processing_template_decl)
4574 error ("namespace `%T' undeclared", namespace);
4575 return;
4577 if (TREE_CODE (namespace) != NAMESPACE_DECL)
4579 if (!processing_template_decl)
4580 error ("`%T' is not a namespace", namespace);
4581 return;
4583 namespace = ORIGINAL_NAMESPACE (namespace);
4584 if (!toplevel_bindings_p ())
4585 push_using_directive (namespace);
4586 else
4587 /* direct usage */
4588 add_using_namespace (current_namespace, namespace, 0);
4591 void
4592 check_default_args (tree x)
4594 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4595 bool saw_def = false;
4596 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4597 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4599 if (TREE_PURPOSE (arg))
4600 saw_def = true;
4601 else if (saw_def)
4603 cp_error_at ("default argument missing for parameter %P of `%+#D'",
4604 i, x);
4605 break;
4610 void
4611 mark_used (tree decl)
4613 TREE_USED (decl) = 1;
4614 if (processing_template_decl || skip_evaluation)
4615 return;
4617 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
4618 && !TREE_ASM_WRITTEN (decl))
4619 /* Remember it, so we can check it was defined. */
4620 defer_fn (decl);
4622 assemble_external (decl);
4624 /* Is it a synthesized method that needs to be synthesized? */
4625 if (TREE_CODE (decl) == FUNCTION_DECL
4626 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
4627 && DECL_ARTIFICIAL (decl)
4628 && !DECL_THUNK_P (decl)
4629 && ! DECL_INITIAL (decl)
4630 /* Kludge: don't synthesize for default args. */
4631 && current_function_decl)
4633 synthesize_method (decl);
4634 /* If we've already synthesized the method we don't need to
4635 instantiate it, so we can return right away. */
4636 return;
4639 /* If this is a function or variable that is an instance of some
4640 template, we now know that we will need to actually do the
4641 instantiation. We check that DECL is not an explicit
4642 instantiation because that is not checked in instantiate_decl. */
4643 if ((DECL_NON_THUNK_FUNCTION_P (decl) || TREE_CODE (decl) == VAR_DECL)
4644 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
4645 && (!DECL_EXPLICIT_INSTANTIATION (decl)
4646 || (TREE_CODE (decl) == FUNCTION_DECL
4647 && DECL_INLINE (DECL_TEMPLATE_RESULT
4648 (template_for_substitution (decl))))))
4650 bool defer;
4652 /* Normally, we put off instantiating functions in order to
4653 improve compile times. Maintaining a stack of active
4654 functions is expensive, and the inliner knows to
4655 instantiate any functions it might need.
4657 However, if instantiating this function might help us mark
4658 the current function TREE_NOTHROW, we go ahead and
4659 instantiate it now. */
4660 defer = (!flag_exceptions
4661 || TREE_CODE (decl) != FUNCTION_DECL
4662 /* If the called function can't throw, we don't need to
4663 generate its body to find that out. */
4664 || TREE_NOTHROW (decl)
4665 || !cfun
4666 /* If we already know the current function can't throw,
4667 then we don't need to work hard to prove it. */
4668 || TREE_NOTHROW (current_function_decl)
4669 /* If we already know that the current function *can*
4670 throw, there's no point in gathering more
4671 information. */
4672 || cp_function_chain->can_throw);
4674 instantiate_decl (decl, defer);
4678 /* Called when a class-head is encountered. TAG_KIND is the class-key
4679 for the class. SCOPE, if non-NULL, is the type or namespace
4680 indicated in the nested-name-specifier for the declaration of the
4681 class. ID is the name of the class, if any; it may be a TYPE_DECL,
4682 or an IDENTIFIER_NODE. ATTRIBUTES are attributes that apply to the
4683 class.
4685 Return a TYPE_DECL for the class being defined. */
4687 tree
4688 handle_class_head (enum tag_types tag_kind, tree scope, tree id,
4689 tree attributes)
4691 tree decl = NULL_TREE;
4692 tree current = current_scope ();
4693 bool xrefd_p = false;
4694 bool new_type_p;
4695 tree context;
4697 if (current == NULL_TREE)
4698 current = current_namespace;
4700 if (scope)
4702 if (TREE_CODE (id) == TYPE_DECL)
4703 /* We must bash typedefs back to the main decl of the
4704 type. Otherwise we become confused about scopes. */
4705 decl = TYPE_MAIN_DECL (TREE_TYPE (id));
4706 else if (DECL_CLASS_TEMPLATE_P (id))
4707 decl = DECL_TEMPLATE_RESULT (id);
4708 else
4710 if (TYPE_P (scope))
4712 /* According to the suggested resolution of core issue
4713 180, 'typename' is assumed after a class-key. */
4714 decl = make_typename_type (scope, id, tf_error);
4715 if (decl != error_mark_node)
4716 decl = TYPE_MAIN_DECL (decl);
4717 else
4718 decl = NULL_TREE;
4720 else if (scope == current)
4722 /* We've been given AGGR SCOPE::ID, when we're already
4723 inside SCOPE. Be nice about it. */
4724 if (pedantic)
4725 pedwarn ("extra qualification `%T::' on member `%D' ignored",
4726 scope, id);
4728 else
4729 error ("`%T' does not have a class or union named `%D'",
4730 scope, id);
4734 if (!decl)
4736 decl = xref_tag (tag_kind, id, attributes, false);
4737 if (decl == error_mark_node)
4738 return error_mark_node;
4739 decl = TYPE_MAIN_DECL (decl);
4740 xrefd_p = true;
4743 if (!TYPE_BINFO (TREE_TYPE (decl)))
4745 error ("`%T' is not a class or union type", decl);
4746 return error_mark_node;
4749 /* For a definition, we want to enter the containing scope before
4750 looking up any base classes etc. Only do so, if this is different
4751 to the current scope. */
4752 context = CP_DECL_CONTEXT (decl);
4754 new_type_p = (current != context
4755 && TREE_CODE (context) != TEMPLATE_TYPE_PARM
4756 && TREE_CODE (context) != BOUND_TEMPLATE_TEMPLATE_PARM);
4757 if (new_type_p)
4758 push_scope (context);
4760 if (!xrefd_p
4761 && PROCESSING_REAL_TEMPLATE_DECL_P ()
4762 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
4763 decl = push_template_decl (decl);
4765 return decl;
4768 #include "gt-cp-decl2.h"