2002-11-07 Jason Thorpe <thorpej@wasabisystems.com>
[official-gcc.git] / gcc / cp / decl2.c
blobae4ec12737c8cb845fa31ad0a87f14d23fda4e71
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC 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 GNU CC 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 GNU CC; 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 "tree.h"
34 #include "rtl.h"
35 #include "expr.h"
36 #include "flags.h"
37 #include "cp-tree.h"
38 #include "decl.h"
39 #include "lex.h"
40 #include "output.h"
41 #include "except.h"
42 #include "toplev.h"
43 #include "ggc.h"
44 #include "timevar.h"
45 #include "cpplib.h"
46 #include "target.h"
47 #include "c-common.h"
48 extern cpp_reader *parse_in;
50 /* This structure contains information about the initializations
51 and/or destructions required for a particular priority level. */
52 typedef struct priority_info_s {
53 /* Nonzero if there have been any initializations at this priority
54 throughout the translation unit. */
55 int initializations_p;
56 /* Nonzero if there have been any destructions at this priority
57 throughout the translation unit. */
58 int destructions_p;
59 } *priority_info;
61 static void mark_vtable_entries PARAMS ((tree));
62 static void grok_function_init PARAMS ((tree, tree));
63 static int maybe_emit_vtables (tree);
64 static int is_namespace_ancestor PARAMS ((tree, tree));
65 static void add_using_namespace PARAMS ((tree, tree, int));
66 static tree ambiguous_decl PARAMS ((tree, tree, tree,int));
67 static tree build_anon_union_vars PARAMS ((tree, tree*, int, int));
68 static int acceptable_java_type PARAMS ((tree));
69 static void output_vtable_inherit PARAMS ((tree));
70 static tree start_objects PARAMS ((int, int));
71 static void finish_objects PARAMS ((int, int, tree));
72 static tree merge_functions PARAMS ((tree, tree));
73 static tree decl_namespace PARAMS ((tree));
74 static tree validate_nonmember_using_decl PARAMS ((tree, tree *, tree *));
75 static void do_nonmember_using_decl PARAMS ((tree, tree, tree, tree,
76 tree *, tree *));
77 static tree start_static_storage_duration_function PARAMS ((void));
78 static void finish_static_storage_duration_function PARAMS ((tree));
79 static priority_info get_priority_info PARAMS ((int));
80 static void do_static_initialization PARAMS ((tree, tree));
81 static void do_static_destruction PARAMS ((tree));
82 static tree start_static_initialization_or_destruction PARAMS ((tree, int));
83 static void finish_static_initialization_or_destruction PARAMS ((tree));
84 static void generate_ctor_or_dtor_function PARAMS ((int, int));
85 static int generate_ctor_and_dtor_functions_for_priority
86 PARAMS ((splay_tree_node, void *));
87 static tree prune_vars_needing_no_initialization PARAMS ((tree));
88 static void write_out_vars PARAMS ((tree));
89 static void import_export_class PARAMS ((tree));
90 static tree key_method PARAMS ((tree));
91 static tree get_guard_bits PARAMS ((tree));
93 /* A list of static class variables. This is needed, because a
94 static class variable can be declared inside the class without
95 an initializer, and then initialized, statically, outside the class. */
96 static GTY(()) varray_type pending_statics;
97 #define pending_statics_used \
98 (pending_statics ? pending_statics->elements_used : 0)
100 /* A list of functions which were declared inline, but which we
101 may need to emit outline anyway. */
102 static GTY(()) varray_type deferred_fns;
103 #define deferred_fns_used \
104 (deferred_fns ? deferred_fns->elements_used : 0)
106 /* Flag used when debugging spew.c */
108 extern int spew_debug;
110 /* Nonzero if we're done parsing and into end-of-file activities. */
112 int at_eof;
114 /* Functions called along with real static constructors and destructors. */
116 tree static_ctors;
117 tree static_dtors;
119 /* The :: namespace. */
121 tree global_namespace;
123 /* Incorporate `const' and `volatile' qualifiers for member functions.
124 FUNCTION is a TYPE_DECL or a FUNCTION_DECL.
125 QUALS is a list of qualifiers. Returns any explicit
126 top-level qualifiers of the method's this pointer, anything other than
127 TYPE_UNQUALIFIED will be an extension. */
130 grok_method_quals (ctype, function, quals)
131 tree ctype, function, quals;
133 tree fntype = TREE_TYPE (function);
134 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
135 int type_quals = TYPE_UNQUALIFIED;
136 int dup_quals = TYPE_UNQUALIFIED;
137 int this_quals = TYPE_UNQUALIFIED;
141 int tq = cp_type_qual_from_rid (TREE_VALUE (quals));
143 if ((type_quals | this_quals) & tq)
144 dup_quals |= tq;
145 else if (tq & TYPE_QUAL_RESTRICT)
146 this_quals |= tq;
147 else
148 type_quals |= tq;
149 quals = TREE_CHAIN (quals);
151 while (quals);
153 if (dup_quals != TYPE_UNQUALIFIED)
154 error ("duplicate type qualifiers in %s declaration",
155 TREE_CODE (function) == FUNCTION_DECL
156 ? "member function" : "type");
158 ctype = cp_build_qualified_type (ctype, type_quals);
159 fntype = build_cplus_method_type (ctype, TREE_TYPE (fntype),
160 (TREE_CODE (fntype) == METHOD_TYPE
161 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
162 : TYPE_ARG_TYPES (fntype)));
163 if (raises)
164 fntype = build_exception_variant (fntype, raises);
166 TREE_TYPE (function) = fntype;
167 return this_quals;
170 /* Warn when -fexternal-templates is used and #pragma
171 interface/implementation is not used all the times it should be,
172 inform the user. */
174 void
175 warn_if_unknown_interface (decl)
176 tree decl;
178 static int already_warned = 0;
179 if (already_warned++)
180 return;
182 if (flag_alt_external_templates)
184 tree til = tinst_for_decl ();
185 int sl = lineno;
186 const char *sf = input_filename;
188 if (til)
190 lineno = TINST_LINE (til);
191 input_filename = TINST_FILE (til);
193 warning ("template `%#D' instantiated in file without #pragma interface",
194 decl);
195 lineno = sl;
196 input_filename = sf;
198 else
199 cp_warning_at ("template `%#D' defined in file without #pragma interface",
200 decl);
203 /* A subroutine of the parser, to handle a component list. */
205 void
206 grok_x_components (specs)
207 tree specs;
209 tree t;
211 specs = strip_attrs (specs);
213 check_tag_decl (specs);
214 t = groktypename (build_tree_list (specs, NULL_TREE));
216 /* The only case where we need to do anything additional here is an
217 anonymous union field, e.g.: `struct S { union { int i; }; };'. */
218 if (t == NULL_TREE || !ANON_AGGR_TYPE_P (t))
219 return;
221 fixup_anonymous_aggr (t);
222 finish_member_declaration (build_decl (FIELD_DECL, NULL_TREE, t));
225 /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
226 appropriately. */
228 tree
229 cp_build_parm_decl (name, type)
230 tree name;
231 tree type;
233 tree parm = build_decl (PARM_DECL, name, type);
234 DECL_ARG_TYPE (parm) = type_passed_as (type);
235 return parm;
238 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
239 indicated NAME. */
241 tree
242 build_artificial_parm (name, type)
243 tree name;
244 tree type;
246 tree parm = cp_build_parm_decl (name, type);
247 DECL_ARTIFICIAL (parm) = 1;
248 /* All our artificial parms are implicitly `const'; they cannot be
249 assigned to. */
250 TREE_READONLY (parm) = 1;
251 return parm;
254 /* Constructors for types with virtual baseclasses need an "in-charge" flag
255 saying whether this constructor is responsible for initialization of
256 virtual baseclasses or not. All destructors also need this "in-charge"
257 flag, which additionally determines whether or not the destructor should
258 free the memory for the object.
260 This function adds the "in-charge" flag to member function FN if
261 appropriate. It is called from grokclassfn and tsubst.
262 FN must be either a constructor or destructor.
264 The in-charge flag follows the 'this' parameter, and is followed by the
265 VTT parm (if any), then the user-written parms. */
267 void
268 maybe_retrofit_in_chrg (fn)
269 tree fn;
271 tree basetype, arg_types, parms, parm, fntype;
273 /* If we've already add the in-charge parameter don't do it again. */
274 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
275 return;
277 /* When processing templates we can't know, in general, whether or
278 not we're going to have virtual baseclasses. */
279 if (uses_template_parms (fn))
280 return;
282 /* We don't need an in-charge parameter for constructors that don't
283 have virtual bases. */
284 if (DECL_CONSTRUCTOR_P (fn)
285 && !TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
286 return;
288 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
289 basetype = TREE_TYPE (TREE_VALUE (arg_types));
290 arg_types = TREE_CHAIN (arg_types);
292 parms = TREE_CHAIN (DECL_ARGUMENTS (fn));
294 /* If this is a subobject constructor or destructor, our caller will
295 pass us a pointer to our VTT. */
296 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
298 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
300 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
301 TREE_CHAIN (parm) = parms;
302 parms = parm;
304 /* ...and then to TYPE_ARG_TYPES. */
305 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
307 DECL_HAS_VTT_PARM_P (fn) = 1;
310 /* Then add the in-charge parm (before the VTT parm). */
311 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
312 TREE_CHAIN (parm) = parms;
313 parms = parm;
314 arg_types = hash_tree_chain (integer_type_node, arg_types);
316 /* Insert our new parameter(s) into the list. */
317 TREE_CHAIN (DECL_ARGUMENTS (fn)) = parms;
319 /* And rebuild the function type. */
320 fntype = build_cplus_method_type (basetype, TREE_TYPE (TREE_TYPE (fn)),
321 arg_types);
322 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
323 fntype = build_exception_variant (fntype,
324 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
325 TREE_TYPE (fn) = fntype;
327 /* Now we've got the in-charge parameter. */
328 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
331 /* Classes overload their constituent function names automatically.
332 When a function name is declared in a record structure,
333 its name is changed to it overloaded name. Since names for
334 constructors and destructors can conflict, we place a leading
335 '$' for destructors.
337 CNAME is the name of the class we are grokking for.
339 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
341 FLAGS contains bits saying what's special about today's
342 arguments. 1 == DESTRUCTOR. 2 == OPERATOR.
344 If FUNCTION is a destructor, then we must add the `auto-delete' field
345 as a second parameter. There is some hair associated with the fact
346 that we must "declare" this variable in the manner consistent with the
347 way the rest of the arguments were declared.
349 QUALS are the qualifiers for the this pointer. */
351 void
352 grokclassfn (ctype, function, flags, quals)
353 tree ctype, function;
354 enum overload_flags flags;
355 tree quals;
357 tree fn_name = DECL_NAME (function);
358 int this_quals = TYPE_UNQUALIFIED;
360 /* Even within an `extern "C"' block, members get C++ linkage. See
361 [dcl.link] for details. */
362 SET_DECL_LANGUAGE (function, lang_cplusplus);
364 if (fn_name == NULL_TREE)
366 error ("name missing for member function");
367 fn_name = get_identifier ("<anonymous>");
368 DECL_NAME (function) = fn_name;
371 if (quals)
372 this_quals = grok_method_quals (ctype, function, quals);
374 if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
376 /* Must add the class instance variable up front. */
377 /* Right now we just make this a pointer. But later
378 we may wish to make it special. */
379 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (function)));
380 tree qual_type;
381 tree parm;
383 /* The `this' parameter is implicitly `const'; it cannot be
384 assigned to. */
385 this_quals |= TYPE_QUAL_CONST;
386 qual_type = cp_build_qualified_type (type, this_quals);
387 parm = build_artificial_parm (this_identifier, qual_type);
388 c_apply_type_quals_to_decl (this_quals, parm);
389 TREE_CHAIN (parm) = last_function_parms;
390 last_function_parms = parm;
393 DECL_ARGUMENTS (function) = last_function_parms;
394 DECL_CONTEXT (function) = ctype;
396 if (flags == DTOR_FLAG)
397 DECL_DESTRUCTOR_P (function) = 1;
399 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
400 maybe_retrofit_in_chrg (function);
402 if (flags == DTOR_FLAG)
404 DECL_DESTRUCTOR_P (function) = 1;
405 TYPE_HAS_DESTRUCTOR (ctype) = 1;
409 /* Create an ARRAY_REF, checking for the user doing things backwards
410 along the way. */
412 tree
413 grok_array_decl (array_expr, index_exp)
414 tree array_expr, index_exp;
416 tree type = TREE_TYPE (array_expr);
417 tree p1, p2, i1, i2;
419 if (type == error_mark_node || index_exp == error_mark_node)
420 return error_mark_node;
421 if (processing_template_decl)
422 return build_min (ARRAY_REF, type ? TREE_TYPE (type) : NULL_TREE,
423 array_expr, index_exp);
425 if (type == NULL_TREE)
427 /* Something has gone very wrong. Assume we are mistakenly reducing
428 an expression instead of a declaration. */
429 error ("parser may be lost: is there a '{' missing somewhere?");
430 return NULL_TREE;
433 if (TREE_CODE (type) == OFFSET_TYPE
434 || TREE_CODE (type) == REFERENCE_TYPE)
435 type = TREE_TYPE (type);
437 /* If they have an `operator[]', use that. */
438 if (IS_AGGR_TYPE (type) || IS_AGGR_TYPE (TREE_TYPE (index_exp)))
439 return build_opfncall (ARRAY_REF, LOOKUP_NORMAL,
440 array_expr, index_exp, NULL_TREE);
442 /* Otherwise, create an ARRAY_REF for a pointer or array type. It
443 is a little-known fact that, if `a' is an array and `i' is an
444 int, you can write `i[a]', which means the same thing as `a[i]'. */
446 if (TREE_CODE (type) == ARRAY_TYPE)
447 p1 = array_expr;
448 else
449 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, 0);
451 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
452 p2 = index_exp;
453 else
454 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, 0);
456 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr, 0);
457 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp, 0);
459 if ((p1 && i2) && (i1 && p2))
460 error ("ambiguous conversion for array subscript");
462 if (p1 && i2)
463 array_expr = p1, index_exp = i2;
464 else if (i1 && p2)
465 array_expr = p2, index_exp = i1;
466 else
468 error ("invalid types `%T[%T]' for array subscript",
469 type, TREE_TYPE (index_exp));
470 return error_mark_node;
473 if (array_expr == error_mark_node || index_exp == error_mark_node)
474 error ("ambiguous conversion for array subscript");
476 return build_array_ref (array_expr, index_exp);
479 /* Given the cast expression EXP, checking out its validity. Either return
480 an error_mark_node if there was an unavoidable error, return a cast to
481 void for trying to delete a pointer w/ the value 0, or return the
482 call to delete. If DOING_VEC is 1, we handle things differently
483 for doing an array delete. If DOING_VEC is 2, they gave us the
484 array size as an argument to delete.
485 Implements ARM $5.3.4. This is called from the parser. */
487 tree
488 delete_sanity (exp, size, doing_vec, use_global_delete)
489 tree exp, size;
490 int doing_vec, use_global_delete;
492 tree t, type;
493 /* For a regular vector delete (aka, no size argument) we will pass
494 this down as a NULL_TREE into build_vec_delete. */
495 tree maxindex = NULL_TREE;
497 if (exp == error_mark_node)
498 return exp;
500 if (processing_template_decl)
502 t = build_min (DELETE_EXPR, void_type_node, exp, size);
503 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
504 DELETE_EXPR_USE_VEC (t) = doing_vec;
505 return t;
508 if (TREE_CODE (exp) == OFFSET_REF)
509 exp = resolve_offset_ref (exp);
510 exp = convert_from_reference (exp);
511 t = stabilize_reference (exp);
512 t = build_expr_type_conversion (WANT_POINTER, t, 1);
514 if (t == NULL_TREE || t == error_mark_node)
516 error ("type `%#T' argument given to `delete', expected pointer",
517 TREE_TYPE (exp));
518 return error_mark_node;
521 if (doing_vec == 2)
523 maxindex = cp_build_binary_op (MINUS_EXPR, size, integer_one_node);
524 pedwarn ("anachronistic use of array size in vector delete");
527 type = TREE_TYPE (t);
529 /* As of Valley Forge, you can delete a pointer to const. */
531 /* You can't delete functions. */
532 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
534 error ("cannot delete a function. Only pointer-to-objects are valid arguments to `delete'");
535 return error_mark_node;
538 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
539 if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
541 warning ("deleting `%T' is undefined", type);
542 doing_vec = 0;
545 /* An array can't have been allocated by new, so complain. */
546 if (TREE_CODE (t) == ADDR_EXPR
547 && TREE_CODE (TREE_OPERAND (t, 0)) == VAR_DECL
548 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == ARRAY_TYPE)
549 warning ("deleting array `%#D'", TREE_OPERAND (t, 0));
551 /* Deleting a pointer with the value zero is valid and has no effect. */
552 if (integer_zerop (t))
553 return build1 (NOP_EXPR, void_type_node, t);
555 if (doing_vec)
556 return build_vec_delete (t, maxindex, sfk_deleting_destructor,
557 use_global_delete);
558 else
559 return build_delete (type, t, sfk_deleting_destructor,
560 LOOKUP_NORMAL, use_global_delete);
563 /* Report an error if the indicated template declaration is not the
564 sort of thing that should be a member template. */
566 void
567 check_member_template (tmpl)
568 tree tmpl;
570 tree decl;
572 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
573 decl = DECL_TEMPLATE_RESULT (tmpl);
575 if (TREE_CODE (decl) == FUNCTION_DECL
576 || (TREE_CODE (decl) == TYPE_DECL
577 && IS_AGGR_TYPE (TREE_TYPE (decl))))
579 if (current_function_decl)
580 /* 14.5.2.2 [temp.mem]
582 A local class shall not have member templates. */
583 error ("invalid declaration of member template `%#D' in local class",
584 decl);
586 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
588 /* 14.5.2.3 [temp.mem]
590 A member function template shall not be virtual. */
591 error
592 ("invalid use of `virtual' in template declaration of `%#D'",
593 decl);
594 DECL_VIRTUAL_P (decl) = 0;
597 /* The debug-information generating code doesn't know what to do
598 with member templates. */
599 DECL_IGNORED_P (tmpl) = 1;
601 else
602 error ("template declaration of `%#D'", decl);
605 /* Return true iff TYPE is a valid Java parameter or return type. */
607 static int
608 acceptable_java_type (type)
609 tree type;
611 if (TREE_CODE (type) == VOID_TYPE || TYPE_FOR_JAVA (type))
612 return 1;
613 if (TREE_CODE (type) == POINTER_TYPE || TREE_CODE (type) == REFERENCE_TYPE)
615 type = TREE_TYPE (type);
616 if (TREE_CODE (type) == RECORD_TYPE)
618 tree args; int i;
619 if (! TYPE_FOR_JAVA (type))
620 return 0;
621 if (! CLASSTYPE_TEMPLATE_INFO (type))
622 return 1;
623 args = CLASSTYPE_TI_ARGS (type);
624 i = TREE_VEC_LENGTH (args);
625 while (--i >= 0)
627 type = TREE_VEC_ELT (args, i);
628 if (TREE_CODE (type) == POINTER_TYPE)
629 type = TREE_TYPE (type);
630 if (! TYPE_FOR_JAVA (type))
631 return 0;
633 return 1;
636 return 0;
639 /* For a METHOD in a Java class CTYPE, return 1 if
640 the parameter and return types are valid Java types.
641 Otherwise, print appropriate error messages, and return 0. */
644 check_java_method (method)
645 tree method;
647 int jerr = 0;
648 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
649 tree ret_type = TREE_TYPE (TREE_TYPE (method));
650 if (! acceptable_java_type (ret_type))
652 error ("Java method '%D' has non-Java return type `%T'",
653 method, ret_type);
654 jerr++;
656 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
658 tree type = TREE_VALUE (arg_types);
659 if (! acceptable_java_type (type))
661 error ("Java method '%D' has non-Java parameter type `%T'",
662 method, type);
663 jerr++;
666 return jerr ? 0 : 1;
669 /* Sanity check: report error if this function FUNCTION is not
670 really a member of the class (CTYPE) it is supposed to belong to.
671 CNAME is the same here as it is for grokclassfn above. */
673 tree
674 check_classfn (ctype, function)
675 tree ctype, function;
677 tree fn_name = DECL_NAME (function);
678 tree fndecl, fndecls;
679 tree method_vec = CLASSTYPE_METHOD_VEC (complete_type (ctype));
680 tree *methods = 0;
681 tree *end = 0;
683 if (DECL_USE_TEMPLATE (function)
684 && !(TREE_CODE (function) == TEMPLATE_DECL
685 && DECL_TEMPLATE_SPECIALIZATION (function))
686 && is_member_template (DECL_TI_TEMPLATE (function)))
687 /* Since this is a specialization of a member template,
688 we're not going to find the declaration in the class.
689 For example, in:
691 struct S { template <typename T> void f(T); };
692 template <> void S::f(int);
694 we're not going to find `S::f(int)', but there's no
695 reason we should, either. We let our callers know we didn't
696 find the method, but we don't complain. */
697 return NULL_TREE;
699 if (method_vec != 0)
701 methods = &TREE_VEC_ELT (method_vec, 0);
702 end = TREE_VEC_END (method_vec);
704 /* First suss out ctors and dtors. */
705 if (*methods && fn_name == DECL_NAME (OVL_CURRENT (*methods))
706 && DECL_CONSTRUCTOR_P (function))
707 goto got_it;
708 if (*++methods && fn_name == DECL_NAME (OVL_CURRENT (*methods))
709 && DECL_DESTRUCTOR_P (function))
710 goto got_it;
712 while (++methods != end && *methods)
714 fndecl = *methods;
715 if (fn_name == DECL_NAME (OVL_CURRENT (*methods)))
717 got_it:
718 for (fndecls = *methods; fndecls != NULL_TREE;
719 fndecls = OVL_NEXT (fndecls))
721 fndecl = OVL_CURRENT (fndecls);
723 /* We cannot simply call decls_match because this
724 doesn't work for static member functions that are
725 pretending to be methods, and because the name
726 may have been changed by asm("new_name"). */
727 if (DECL_NAME (function) == DECL_NAME (fndecl))
729 tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
730 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
732 /* Get rid of the this parameter on functions that become
733 static. */
734 if (DECL_STATIC_FUNCTION_P (fndecl)
735 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
736 p1 = TREE_CHAIN (p1);
738 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
739 TREE_TYPE (TREE_TYPE (fndecl)))
740 && compparms (p1, p2)
741 && (DECL_TEMPLATE_SPECIALIZATION (function)
742 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
743 && (!DECL_TEMPLATE_SPECIALIZATION (function)
744 || (DECL_TI_TEMPLATE (function)
745 == DECL_TI_TEMPLATE (fndecl))))
746 return fndecl;
749 break; /* loser */
754 if (methods != end && *methods)
756 tree fndecl = *methods;
757 error ("prototype for `%#D' does not match any in class `%T'",
758 function, ctype);
759 cp_error_at ("candidate%s: %+#D", OVL_NEXT (fndecl) ? "s are" : " is",
760 OVL_CURRENT (fndecl));
761 while (fndecl = OVL_NEXT (fndecl), fndecl)
762 cp_error_at (" %#D", OVL_CURRENT(fndecl));
764 else
766 methods = 0;
767 if (!COMPLETE_TYPE_P (ctype))
768 cxx_incomplete_type_error (function, ctype);
769 else
770 error ("no `%#D' member function declared in class `%T'",
771 function, ctype);
774 /* If we did not find the method in the class, add it to avoid
775 spurious errors (unless the CTYPE is not yet defined, in which
776 case we'll only confuse ourselves when the function is declared
777 properly within the class. */
778 if (COMPLETE_TYPE_P (ctype))
779 add_method (ctype, function, /*error_p=*/1);
780 return NULL_TREE;
783 /* We have just processed the DECL, which is a static data member.
784 Its initializer, if present, is INIT. The ASMSPEC_TREE, if
785 present, is the assembly-language name for the data member.
786 FLAGS is as for cp_finish_decl. */
788 void
789 finish_static_data_member_decl (decl, init, asmspec_tree, flags)
790 tree decl;
791 tree init;
792 tree asmspec_tree;
793 int flags;
795 my_friendly_assert (TREE_PUBLIC (decl), 0);
797 DECL_CONTEXT (decl) = current_class_type;
799 /* We cannot call pushdecl here, because that would fill in the
800 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
801 the right thing, namely, to put this decl out straight away. */
802 /* current_class_type can be NULL_TREE in case of error. */
803 if (!asmspec_tree && current_class_type)
804 DECL_INITIAL (decl) = error_mark_node;
806 if (! processing_template_decl)
808 if (!pending_statics)
809 VARRAY_TREE_INIT (pending_statics, 32, "pending_statics");
810 VARRAY_PUSH_TREE (pending_statics, decl);
813 if (LOCAL_CLASS_P (current_class_type))
814 pedwarn ("local class `%#T' shall not have static data member `%#D'",
815 current_class_type, decl);
817 /* Static consts need not be initialized in the class definition. */
818 if (init != NULL_TREE && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
820 static int explained = 0;
822 error ("initializer invalid for static member with constructor");
823 if (!explained)
825 error ("(an out of class initialization is required)");
826 explained = 1;
828 init = NULL_TREE;
830 /* Force the compiler to know when an uninitialized static const
831 member is being used. */
832 if (CP_TYPE_CONST_P (TREE_TYPE (decl)) && init == 0)
833 TREE_USED (decl) = 1;
834 DECL_INITIAL (decl) = init;
835 DECL_IN_AGGR_P (decl) = 1;
837 cp_finish_decl (decl, init, asmspec_tree, flags);
840 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
841 of a structure component, returning a _DECL node.
842 QUALS is a list of type qualifiers for this decl (such as for declaring
843 const member functions).
845 This is done during the parsing of the struct declaration.
846 The _DECL nodes are chained together and the lot of them
847 are ultimately passed to `build_struct' to make the RECORD_TYPE node.
849 If class A defines that certain functions in class B are friends, then
850 the way I have set things up, it is B who is interested in permission
851 granted by A. However, it is in A's context that these declarations
852 are parsed. By returning a void_type_node, class A does not attempt
853 to incorporate the declarations of the friends within its structure.
855 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
856 CHANGES TO CODE IN `start_method'. */
858 tree
859 grokfield (declarator, declspecs, init, asmspec_tree, attrlist)
860 tree declarator, declspecs, init, asmspec_tree, attrlist;
862 tree value;
863 const char *asmspec = 0;
864 int flags = LOOKUP_ONLYCONVERTING;
866 /* Convert () initializers to = initializers. */
867 if (init == NULL_TREE && declarator != NULL_TREE
868 && TREE_CODE (declarator) == CALL_EXPR
869 && TREE_OPERAND (declarator, 0)
870 && (TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE
871 || TREE_CODE (TREE_OPERAND (declarator, 0)) == SCOPE_REF)
872 && parmlist_is_exprlist (CALL_DECLARATOR_PARMS (declarator)))
874 /* It's invalid to try to initialize a data member using a
875 functional notation, e.g.:
877 struct S {
878 static int i (3);
881 Explain that to the user. */
882 static int explained;
884 error ("invalid data member initialization");
885 if (!explained)
887 error ("(use `=' to initialize static data members)");
888 explained = 1;
891 declarator = TREE_OPERAND (declarator, 0);
892 flags = 0;
895 if (declspecs == NULL_TREE
896 && TREE_CODE (declarator) == SCOPE_REF
897 && TREE_CODE (TREE_OPERAND (declarator, 1)) == IDENTIFIER_NODE)
899 /* Access declaration */
900 if (! IS_AGGR_TYPE_CODE (TREE_CODE (TREE_OPERAND (declarator, 0))))
902 else if (TREE_COMPLEXITY (declarator) == current_class_depth)
903 pop_nested_class ();
904 return do_class_using_decl (declarator);
907 if (init
908 && TREE_CODE (init) == TREE_LIST
909 && TREE_VALUE (init) == error_mark_node
910 && TREE_CHAIN (init) == NULL_TREE)
911 init = NULL_TREE;
913 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
914 if (! value || value == error_mark_node)
915 /* friend or constructor went bad. */
916 return value;
917 if (TREE_TYPE (value) == error_mark_node)
918 return error_mark_node;
920 if (TREE_CODE (value) == TYPE_DECL && init)
922 error ("typedef `%D' is initialized (use __typeof__ instead)", value);
923 init = NULL_TREE;
926 /* Pass friendly classes back. */
927 if (TREE_CODE (value) == VOID_TYPE)
928 return void_type_node;
930 if (DECL_NAME (value) != NULL_TREE
931 && IDENTIFIER_POINTER (DECL_NAME (value))[0] == '_'
932 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (value)), "_vptr"))
933 error ("member `%D' conflicts with virtual function table field name",
934 value);
936 /* Stash away type declarations. */
937 if (TREE_CODE (value) == TYPE_DECL)
939 DECL_NONLOCAL (value) = 1;
940 DECL_CONTEXT (value) = current_class_type;
942 if (CLASS_TYPE_P (TREE_TYPE (value)))
943 CLASSTYPE_GOT_SEMICOLON (TREE_TYPE (value)) = 1;
945 if (processing_template_decl)
946 value = push_template_decl (value);
948 return value;
951 if (DECL_IN_AGGR_P (value))
953 error ("`%D' is already defined in `%T'", value,
954 DECL_CONTEXT (value));
955 return void_type_node;
958 if (asmspec_tree)
959 asmspec = TREE_STRING_POINTER (asmspec_tree);
961 if (init)
963 if (TREE_CODE (value) == FUNCTION_DECL)
965 grok_function_init (value, init);
966 init = NULL_TREE;
968 else if (pedantic && TREE_CODE (value) != VAR_DECL)
969 /* Already complained in grokdeclarator. */
970 init = NULL_TREE;
971 else
973 /* We allow initializers to become parameters to base
974 initializers. */
975 if (TREE_CODE (init) == TREE_LIST)
977 if (TREE_CHAIN (init) == NULL_TREE)
978 init = TREE_VALUE (init);
979 else
980 init = digest_init (TREE_TYPE (value), init, (tree *)0);
983 if (TREE_CODE (init) == CONST_DECL)
984 init = DECL_INITIAL (init);
985 else if (TREE_READONLY_DECL_P (init))
986 init = decl_constant_value (init);
987 else if (TREE_CODE (init) == CONSTRUCTOR)
988 init = digest_init (TREE_TYPE (value), init, (tree *)0);
989 if (init == error_mark_node)
990 /* We must make this look different than `error_mark_node'
991 because `decl_const_value' would mis-interpret it
992 as only meaning that this VAR_DECL is defined. */
993 init = build1 (NOP_EXPR, TREE_TYPE (value), init);
994 else if (processing_template_decl)
996 else if (! TREE_CONSTANT (init))
998 /* We can allow references to things that are effectively
999 static, since references are initialized with the address. */
1000 if (TREE_CODE (TREE_TYPE (value)) != REFERENCE_TYPE
1001 || (TREE_STATIC (init) == 0
1002 && (!DECL_P (init) || DECL_EXTERNAL (init) == 0)))
1004 error ("field initializer is not constant");
1005 init = error_mark_node;
1011 if (processing_template_decl && ! current_function_decl
1012 && (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == FUNCTION_DECL))
1013 value = push_template_decl (value);
1015 if (attrlist)
1016 cplus_decl_attributes (&value, attrlist, 0);
1018 if (TREE_CODE (value) == VAR_DECL)
1020 finish_static_data_member_decl (value, init, asmspec_tree,
1021 flags);
1022 return value;
1024 if (TREE_CODE (value) == FIELD_DECL)
1026 if (asmspec)
1027 error ("`asm' specifiers are not permitted on non-static data members");
1028 if (DECL_INITIAL (value) == error_mark_node)
1029 init = error_mark_node;
1030 cp_finish_decl (value, init, NULL_TREE, flags);
1031 DECL_INITIAL (value) = init;
1032 DECL_IN_AGGR_P (value) = 1;
1033 return value;
1035 if (TREE_CODE (value) == FUNCTION_DECL)
1037 if (asmspec)
1039 /* This must override the asm specifier which was placed
1040 by grokclassfn. Lay this out fresh. */
1041 SET_DECL_RTL (value, NULL_RTX);
1042 SET_DECL_ASSEMBLER_NAME (value, get_identifier (asmspec));
1044 if (!DECL_FRIEND_P (value))
1045 grok_special_member_properties (value);
1047 cp_finish_decl (value, init, asmspec_tree, flags);
1049 /* Pass friends back this way. */
1050 if (DECL_FRIEND_P (value))
1051 return void_type_node;
1053 DECL_IN_AGGR_P (value) = 1;
1054 return value;
1056 abort ();
1057 /* NOTREACHED */
1058 return NULL_TREE;
1061 /* Like `grokfield', but for bitfields.
1062 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1064 tree
1065 grokbitfield (declarator, declspecs, width)
1066 tree declarator, declspecs, width;
1068 register tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1069 0, NULL);
1071 if (! value) return NULL_TREE; /* friends went bad. */
1073 /* Pass friendly classes back. */
1074 if (TREE_CODE (value) == VOID_TYPE)
1075 return void_type_node;
1077 if (TREE_CODE (value) == TYPE_DECL)
1079 error ("cannot declare `%D' to be a bit-field type", value);
1080 return NULL_TREE;
1083 /* Usually, finish_struct_1 catches bitfields with invalid types.
1084 But, in the case of bitfields with function type, we confuse
1085 ourselves into thinking they are member functions, so we must
1086 check here. */
1087 if (TREE_CODE (value) == FUNCTION_DECL)
1089 error ("cannot declare bit-field `%D' with function type",
1090 DECL_NAME (value));
1091 return NULL_TREE;
1094 if (DECL_IN_AGGR_P (value))
1096 error ("`%D' is already defined in the class %T", value,
1097 DECL_CONTEXT (value));
1098 return void_type_node;
1101 if (TREE_STATIC (value))
1103 error ("static member `%D' cannot be a bit-field", value);
1104 return NULL_TREE;
1106 cp_finish_decl (value, NULL_TREE, NULL_TREE, 0);
1108 if (width != error_mark_node)
1110 constant_expression_warning (width);
1111 DECL_INITIAL (value) = width;
1112 SET_DECL_C_BIT_FIELD (value);
1115 DECL_IN_AGGR_P (value) = 1;
1116 return value;
1119 tree
1120 grokoptypename (declspecs, declarator)
1121 tree declspecs, declarator;
1123 tree t = grokdeclarator (declarator, declspecs, TYPENAME, 0, NULL);
1124 return mangle_conv_op_name_for_type (t);
1127 /* When a function is declared with an initializer,
1128 do the right thing. Currently, there are two possibilities:
1130 class B
1132 public:
1133 // initialization possibility #1.
1134 virtual void f () = 0;
1135 int g ();
1138 class D1 : B
1140 public:
1141 int d1;
1142 // error, no f ();
1145 class D2 : B
1147 public:
1148 int d2;
1149 void f ();
1152 class D3 : B
1154 public:
1155 int d3;
1156 // initialization possibility #2
1157 void f () = B::f;
1162 static void
1163 grok_function_init (decl, init)
1164 tree decl;
1165 tree init;
1167 /* An initializer for a function tells how this function should
1168 be inherited. */
1169 tree type = TREE_TYPE (decl);
1171 if (TREE_CODE (type) == FUNCTION_TYPE)
1172 error ("initializer specified for non-member function `%D'", decl);
1173 else if (integer_zerop (init))
1174 DECL_PURE_VIRTUAL_P (decl) = 1;
1175 else
1176 error ("invalid initializer for virtual method `%D'", decl);
1179 void
1180 cplus_decl_attributes (decl, attributes, flags)
1181 tree *decl, attributes;
1182 int flags;
1184 if (*decl == NULL_TREE || *decl == void_type_node)
1185 return;
1187 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1188 decl = &DECL_TEMPLATE_RESULT (*decl);
1190 decl_attributes (decl, attributes, flags);
1192 if (TREE_CODE (*decl) == TYPE_DECL)
1193 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1196 /* CONSTRUCTOR_NAME:
1197 Return the name for the constructor (or destructor) for the
1198 specified class. Argument can be RECORD_TYPE, TYPE_DECL, or
1199 IDENTIFIER_NODE. When given a template, this routine doesn't
1200 lose the specialization. */
1202 tree
1203 constructor_name_full (thing)
1204 tree thing;
1206 if (TREE_CODE (thing) == TEMPLATE_TYPE_PARM
1207 || TREE_CODE (thing) == BOUND_TEMPLATE_TEMPLATE_PARM
1208 || TREE_CODE (thing) == TYPENAME_TYPE)
1209 thing = TYPE_NAME (thing);
1210 else if (IS_AGGR_TYPE_CODE (TREE_CODE (thing)))
1212 if (TYPE_WAS_ANONYMOUS (thing) && TYPE_HAS_CONSTRUCTOR (thing))
1213 thing = DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (thing), 0)));
1214 else
1215 thing = TYPE_NAME (thing);
1217 if (TREE_CODE (thing) == TYPE_DECL
1218 || (TREE_CODE (thing) == TEMPLATE_DECL
1219 && TREE_CODE (DECL_TEMPLATE_RESULT (thing)) == TYPE_DECL))
1220 thing = DECL_NAME (thing);
1221 my_friendly_assert (TREE_CODE (thing) == IDENTIFIER_NODE, 197);
1222 return thing;
1225 /* CONSTRUCTOR_NAME:
1226 Return the name for the constructor (or destructor) for the
1227 specified class. Argument can be RECORD_TYPE, TYPE_DECL, or
1228 IDENTIFIER_NODE. When given a template, return the plain
1229 unspecialized name. */
1231 tree
1232 constructor_name (thing)
1233 tree thing;
1235 tree t;
1236 thing = constructor_name_full (thing);
1237 t = IDENTIFIER_TEMPLATE (thing);
1238 if (!t)
1239 return thing;
1240 return t;
1243 /* Returns TRUE if NAME is the name for the constructor for TYPE. */
1245 bool
1246 constructor_name_p (tree name, tree type)
1248 return (name == constructor_name (type)
1249 || name == constructor_name_full (type));
1253 /* Defer the compilation of the FN until the end of compilation. */
1255 void
1256 defer_fn (fn)
1257 tree fn;
1259 if (DECL_DEFERRED_FN (fn))
1260 return;
1261 DECL_DEFERRED_FN (fn) = 1;
1262 if (!deferred_fns)
1263 VARRAY_TREE_INIT (deferred_fns, 32, "deferred_fns");
1265 VARRAY_PUSH_TREE (deferred_fns, fn);
1268 /* Hunts through the global anonymous union ANON_DECL, building
1269 appropriate VAR_DECLs. Stores cleanups on the list of ELEMS, and
1270 returns a VAR_DECL whose size is the same as the size of the
1271 ANON_DECL, if one is available.
1273 FIXME: we should really handle anonymous unions by binding the names
1274 of the members to COMPONENT_REFs rather than this kludge. */
1276 static tree
1277 build_anon_union_vars (anon_decl, elems, static_p, external_p)
1278 tree anon_decl;
1279 tree* elems;
1280 int static_p;
1281 int external_p;
1283 tree type = TREE_TYPE (anon_decl);
1284 tree main_decl = NULL_TREE;
1285 tree field;
1287 /* Rather than write the code to handle the non-union case,
1288 just give an error. */
1289 if (TREE_CODE (type) != UNION_TYPE)
1290 error ("anonymous struct not inside named type");
1292 for (field = TYPE_FIELDS (type);
1293 field != NULL_TREE;
1294 field = TREE_CHAIN (field))
1296 tree decl;
1298 if (DECL_ARTIFICIAL (field))
1299 continue;
1300 if (TREE_CODE (field) != FIELD_DECL)
1302 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
1303 field);
1304 continue;
1307 if (TREE_PRIVATE (field))
1308 cp_pedwarn_at ("private member `%#D' in anonymous union", field);
1309 else if (TREE_PROTECTED (field))
1310 cp_pedwarn_at ("protected member `%#D' in anonymous union", field);
1312 if (DECL_NAME (field) == NULL_TREE
1313 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1315 decl = build_anon_union_vars (field, elems, static_p, external_p);
1316 if (!decl)
1317 continue;
1319 else if (DECL_NAME (field) == NULL_TREE)
1320 continue;
1321 else
1323 decl = build_decl (VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1324 /* tell `pushdecl' that this is not tentative. */
1325 DECL_INITIAL (decl) = error_mark_node;
1326 TREE_PUBLIC (decl) = 0;
1327 TREE_STATIC (decl) = static_p;
1328 DECL_EXTERNAL (decl) = external_p;
1329 decl = pushdecl (decl);
1330 DECL_INITIAL (decl) = NULL_TREE;
1333 /* Only write out one anon union element--choose the largest
1334 one. We used to try to find one the same size as the union,
1335 but that fails if the ABI forces us to align the union more
1336 strictly. */
1337 if (main_decl == NULL_TREE
1338 || tree_int_cst_lt (DECL_SIZE (main_decl), DECL_SIZE (decl)))
1340 if (main_decl)
1341 TREE_ASM_WRITTEN (main_decl) = 1;
1342 main_decl = decl;
1344 else
1345 /* ??? This causes there to be no debug info written out
1346 about this decl. */
1347 TREE_ASM_WRITTEN (decl) = 1;
1349 if (DECL_NAME (field) == NULL_TREE
1350 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1351 /* The remainder of the processing was already done in the
1352 recursive call. */
1353 continue;
1355 /* If there's a cleanup to do, it belongs in the
1356 TREE_PURPOSE of the following TREE_LIST. */
1357 *elems = tree_cons (NULL_TREE, decl, *elems);
1358 TREE_TYPE (*elems) = type;
1361 return main_decl;
1364 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1365 anonymous union, then all members must be laid out together. PUBLIC_P
1366 is nonzero if this union is not declared static. */
1368 void
1369 finish_anon_union (anon_union_decl)
1370 tree anon_union_decl;
1372 tree type = TREE_TYPE (anon_union_decl);
1373 tree main_decl;
1374 int public_p = TREE_PUBLIC (anon_union_decl);
1375 int static_p = TREE_STATIC (anon_union_decl);
1376 int external_p = DECL_EXTERNAL (anon_union_decl);
1378 /* The VAR_DECL's context is the same as the TYPE's context. */
1379 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1381 if (TYPE_FIELDS (type) == NULL_TREE)
1382 return;
1384 if (public_p)
1386 error ("namespace-scope anonymous aggregates must be static");
1387 return;
1390 if (!processing_template_decl)
1392 main_decl
1393 = build_anon_union_vars (anon_union_decl,
1394 &DECL_ANON_UNION_ELEMS (anon_union_decl),
1395 static_p, external_p);
1397 if (main_decl == NULL_TREE)
1399 warning ("anonymous aggregate with no members");
1400 return;
1403 if (static_p)
1405 make_decl_rtl (main_decl, 0);
1406 COPY_DECL_RTL (main_decl, anon_union_decl);
1407 expand_anon_union_decl (anon_union_decl,
1408 NULL_TREE,
1409 DECL_ANON_UNION_ELEMS (anon_union_decl));
1410 return;
1414 add_decl_stmt (anon_union_decl);
1417 /* Finish processing a builtin type TYPE. It's name is NAME,
1418 its fields are in the array FIELDS. LEN is the number of elements
1419 in FIELDS minus one, or put another way, it is the maximum subscript
1420 used in FIELDS.
1422 It is given the same alignment as ALIGN_TYPE. */
1424 void
1425 finish_builtin_type (type, name, fields, len, align_type)
1426 tree type;
1427 const char *name;
1428 tree fields[];
1429 int len;
1430 tree align_type;
1432 register int i;
1434 TYPE_FIELDS (type) = fields[0];
1435 for (i = 0; i < len; i++)
1437 layout_type (TREE_TYPE (fields[i]));
1438 DECL_FIELD_CONTEXT (fields[i]) = type;
1439 TREE_CHAIN (fields[i]) = fields[i+1];
1441 DECL_FIELD_CONTEXT (fields[i]) = type;
1442 TYPE_ALIGN (type) = TYPE_ALIGN (align_type);
1443 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (align_type);
1444 layout_type (type);
1445 #if 0 /* not yet, should get fixed properly later */
1446 TYPE_NAME (type) = make_type_decl (get_identifier (name), type);
1447 #else
1448 TYPE_NAME (type) = build_decl (TYPE_DECL, get_identifier (name), type);
1449 #endif
1450 TYPE_STUB_DECL (type) = TYPE_NAME (type);
1451 layout_decl (TYPE_NAME (type), 0);
1454 /* Auxiliary functions to make type signatures for
1455 `operator new' and `operator delete' correspond to
1456 what compiler will be expecting. */
1458 tree
1459 coerce_new_type (type)
1460 tree type;
1462 int e = 0;
1463 tree args = TYPE_ARG_TYPES (type);
1465 my_friendly_assert (TREE_CODE (type) == FUNCTION_TYPE, 20001107);
1467 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1468 e = 1, error ("`operator new' must return type `%T'", ptr_type_node);
1470 if (!args || args == void_list_node
1471 || !same_type_p (TREE_VALUE (args), c_size_type_node))
1473 e = 2;
1474 if (args && args != void_list_node)
1475 args = TREE_CHAIN (args);
1476 pedwarn ("`operator new' takes type `size_t' (`%T') as first parameter", c_size_type_node);
1478 switch (e)
1480 case 2:
1481 args = tree_cons (NULL_TREE, c_size_type_node, args);
1482 /* FALLTHROUGH */
1483 case 1:
1484 type = build_exception_variant
1485 (build_function_type (ptr_type_node, args),
1486 TYPE_RAISES_EXCEPTIONS (type));
1487 /* FALLTHROUGH */
1488 default:;
1490 return type;
1493 tree
1494 coerce_delete_type (type)
1495 tree type;
1497 int e = 0;
1498 tree args = TYPE_ARG_TYPES (type);
1500 my_friendly_assert (TREE_CODE (type) == FUNCTION_TYPE, 20001107);
1502 if (!same_type_p (TREE_TYPE (type), void_type_node))
1503 e = 1, error ("`operator delete' must return type `%T'", void_type_node);
1505 if (!args || args == void_list_node
1506 || !same_type_p (TREE_VALUE (args), ptr_type_node))
1508 e = 2;
1509 if (args && args != void_list_node)
1510 args = TREE_CHAIN (args);
1511 error ("`operator delete' takes type `%T' as first parameter", ptr_type_node);
1513 switch (e)
1515 case 2:
1516 args = tree_cons (NULL_TREE, ptr_type_node, args);
1517 /* FALLTHROUGH */
1518 case 1:
1519 type = build_exception_variant
1520 (build_function_type (void_type_node, args),
1521 TYPE_RAISES_EXCEPTIONS (type));
1522 /* FALLTHROUGH */
1523 default:;
1526 return type;
1529 static void
1530 mark_vtable_entries (decl)
1531 tree decl;
1533 tree entries = CONSTRUCTOR_ELTS (DECL_INITIAL (decl));
1535 for (; entries; entries = TREE_CHAIN (entries))
1537 tree fnaddr = TREE_VALUE (entries);
1538 tree fn;
1540 if (TREE_CODE (fnaddr) != ADDR_EXPR
1541 && TREE_CODE (fnaddr) != FDESC_EXPR)
1542 /* This entry is an offset: a virtual base class offset, a
1543 virtual call offset, an RTTI offset, etc. */
1544 continue;
1546 fn = TREE_OPERAND (fnaddr, 0);
1547 TREE_ADDRESSABLE (fn) = 1;
1548 /* When we don't have vcall offsets, we output thunks whenever
1549 we output the vtables that contain them. With vcall offsets,
1550 we know all the thunks we'll need when we emit a virtual
1551 function, so we emit the thunks there instead. */
1552 if (DECL_THUNK_P (fn))
1553 use_thunk (fn, /*emit_p=*/0);
1554 mark_used (fn);
1558 /* Set DECL up to have the closest approximation of "initialized common"
1559 linkage available. */
1561 void
1562 comdat_linkage (decl)
1563 tree decl;
1565 if (flag_weak)
1566 make_decl_one_only (decl);
1567 else if (TREE_CODE (decl) == FUNCTION_DECL
1568 || (TREE_CODE (decl) == VAR_DECL && DECL_ARTIFICIAL (decl)))
1569 /* We can just emit function and compiler-generated variables
1570 statically; having multiple copies is (for the most part) only
1571 a waste of space.
1573 There are two correctness issues, however: the address of a
1574 template instantiation with external linkage should be the
1575 same, independent of what translation unit asks for the
1576 address, and this will not hold when we emit multiple copies of
1577 the function. However, there's little else we can do.
1579 Also, by default, the typeinfo implementation assumes that
1580 there will be only one copy of the string used as the name for
1581 each type. Therefore, if weak symbols are unavailable, the
1582 run-time library should perform a more conservative check; it
1583 should perform a string comparison, rather than an address
1584 comparison. */
1585 TREE_PUBLIC (decl) = 0;
1586 else
1588 /* Static data member template instantiations, however, cannot
1589 have multiple copies. */
1590 if (DECL_INITIAL (decl) == 0
1591 || DECL_INITIAL (decl) == error_mark_node)
1592 DECL_COMMON (decl) = 1;
1593 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1595 DECL_COMMON (decl) = 1;
1596 DECL_INITIAL (decl) = error_mark_node;
1598 else
1600 /* We can't do anything useful; leave vars for explicit
1601 instantiation. */
1602 DECL_EXTERNAL (decl) = 1;
1603 DECL_NOT_REALLY_EXTERN (decl) = 0;
1607 if (DECL_LANG_SPECIFIC (decl))
1608 DECL_COMDAT (decl) = 1;
1611 /* For win32 we also want to put explicit instantiations in
1612 linkonce sections, so that they will be merged with implicit
1613 instantiations; otherwise we get duplicate symbol errors. */
1615 void
1616 maybe_make_one_only (decl)
1617 tree decl;
1619 /* We used to say that this was not necessary on targets that support weak
1620 symbols, because the implicit instantiations will defer to the explicit
1621 one. However, that's not actually the case in SVR4; a strong definition
1622 after a weak one is an error. Also, not making explicit
1623 instantiations one_only means that we can end up with two copies of
1624 some template instantiations. */
1625 if (! flag_weak)
1626 return;
1628 /* We can't set DECL_COMDAT on functions, or finish_file will think
1629 we can get away with not emitting them if they aren't used. We need
1630 to for variables so that cp_finish_decl will update their linkage,
1631 because their DECL_INITIAL may not have been set properly yet. */
1633 make_decl_one_only (decl);
1635 if (TREE_CODE (decl) == VAR_DECL)
1637 DECL_COMDAT (decl) = 1;
1638 /* Mark it needed so we don't forget to emit it. */
1639 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)) = 1;
1643 /* Returns the virtual function with which the vtable for TYPE is
1644 emitted, or NULL_TREE if that heuristic is not applicable to TYPE. */
1646 static tree
1647 key_method (type)
1648 tree type;
1650 tree method;
1652 if (TYPE_FOR_JAVA (type)
1653 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
1654 || CLASSTYPE_INTERFACE_KNOWN (type))
1655 return NULL_TREE;
1657 for (method = TYPE_METHODS (type); method != NULL_TREE;
1658 method = TREE_CHAIN (method))
1659 if (DECL_VINDEX (method) != NULL_TREE
1660 && ! DECL_DECLARED_INLINE_P (method)
1661 && ! DECL_PURE_VIRTUAL_P (method))
1662 return method;
1664 return NULL_TREE;
1667 /* Set TREE_PUBLIC and/or DECL_EXTERN on the vtable DECL,
1668 based on TYPE and other static flags.
1670 Note that anything public is tagged TREE_PUBLIC, whether
1671 it's public in this file or in another one. */
1673 void
1674 import_export_vtable (decl, type, final)
1675 tree decl, type;
1676 int final;
1678 if (DECL_INTERFACE_KNOWN (decl))
1679 return;
1681 if (TYPE_FOR_JAVA (type))
1683 TREE_PUBLIC (decl) = 1;
1684 DECL_EXTERNAL (decl) = 1;
1685 DECL_INTERFACE_KNOWN (decl) = 1;
1687 else if (CLASSTYPE_INTERFACE_KNOWN (type))
1689 TREE_PUBLIC (decl) = 1;
1690 DECL_EXTERNAL (decl) = CLASSTYPE_INTERFACE_ONLY (type);
1691 DECL_INTERFACE_KNOWN (decl) = 1;
1693 else
1695 /* We can only wait to decide if we have real non-inline virtual
1696 functions in our class, or if we come from a template. */
1698 int found = (CLASSTYPE_TEMPLATE_INSTANTIATION (type)
1699 || key_method (type));
1701 if (final || ! found)
1703 comdat_linkage (decl);
1704 DECL_EXTERNAL (decl) = 0;
1706 else
1708 TREE_PUBLIC (decl) = 1;
1709 DECL_EXTERNAL (decl) = 1;
1714 /* Determine whether or not we want to specifically import or export CTYPE,
1715 using various heuristics. */
1717 static void
1718 import_export_class (ctype)
1719 tree ctype;
1721 /* -1 for imported, 1 for exported. */
1722 int import_export = 0;
1724 /* It only makes sense to call this function at EOF. The reason is
1725 that this function looks at whether or not the first non-inline
1726 non-abstract virtual member function has been defined in this
1727 translation unit. But, we can't possibly know that until we've
1728 seen the entire translation unit. */
1729 my_friendly_assert (at_eof, 20000226);
1731 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1732 return;
1734 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma interface,
1735 we will have CLASSTYPE_INTERFACE_ONLY set but not
1736 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
1737 heuristic because someone will supply a #pragma implementation
1738 elsewhere, and deducing it here would produce a conflict. */
1739 if (CLASSTYPE_INTERFACE_ONLY (ctype))
1740 return;
1742 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1743 import_export = -1;
1744 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1745 import_export = 1;
1747 /* If we got -fno-implicit-templates, we import template classes that
1748 weren't explicitly instantiated. */
1749 if (import_export == 0
1750 && CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1751 && ! flag_implicit_templates)
1752 import_export = -1;
1754 /* Base our import/export status on that of the first non-inline,
1755 non-pure virtual function, if any. */
1756 if (import_export == 0
1757 && TYPE_POLYMORPHIC_P (ctype))
1759 tree method = key_method (ctype);
1760 if (method)
1761 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1764 #ifdef MULTIPLE_SYMBOL_SPACES
1765 if (import_export == -1)
1766 import_export = 0;
1767 #endif
1769 if (import_export)
1771 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1772 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1776 /* We need to describe to the assembler the relationship between
1777 a vtable and the vtable of the parent class. */
1779 static void
1780 output_vtable_inherit (vars)
1781 tree vars;
1783 tree parent;
1784 rtx child_rtx, parent_rtx;
1786 child_rtx = XEXP (DECL_RTL (vars), 0); /* strip the mem ref */
1788 parent = binfo_for_vtable (vars);
1790 if (parent == TYPE_BINFO (DECL_CONTEXT (vars)))
1791 parent_rtx = const0_rtx;
1792 else if (parent)
1794 parent = get_vtbl_decl_for_binfo (TYPE_BINFO (BINFO_TYPE (parent)));
1795 parent_rtx = XEXP (DECL_RTL (parent), 0); /* strip the mem ref */
1797 else
1798 abort ();
1800 assemble_vtable_inherit (child_rtx, parent_rtx);
1803 /* If necessary, write out the vtables for the dynamic class CTYPE.
1804 Returns non-zero if any vtables were emitted. */
1806 static int
1807 maybe_emit_vtables (tree ctype)
1809 tree vtbl;
1810 tree primary_vtbl;
1812 /* If the vtables for this class have already been emitted there is
1813 nothing more to do. */
1814 primary_vtbl = CLASSTYPE_VTABLES (ctype);
1815 if (TREE_ASM_WRITTEN (primary_vtbl))
1816 return 0;
1817 /* Ignore dummy vtables made by get_vtable_decl. */
1818 if (TREE_TYPE (primary_vtbl) == void_type_node)
1819 return 0;
1821 import_export_class (ctype);
1822 import_export_vtable (primary_vtbl, ctype, 1);
1824 /* See if any of the vtables are needed. */
1825 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = TREE_CHAIN (vtbl))
1826 if (!DECL_EXTERNAL (vtbl) && DECL_NEEDED_P (vtbl))
1827 break;
1829 if (!vtbl)
1831 /* If the references to this class' vtables are optimized away,
1832 still emit the appropriate debugging information. See
1833 dfs_debug_mark. */
1834 if (DECL_COMDAT (primary_vtbl)
1835 && CLASSTYPE_DEBUG_REQUESTED (ctype))
1836 note_debug_info_needed (ctype);
1837 return 0;
1840 /* The ABI requires that we emit all of the vtables if we emit any
1841 of them. */
1842 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = TREE_CHAIN (vtbl))
1844 /* Write it out. */
1845 import_export_vtable (vtbl, ctype, 1);
1846 mark_vtable_entries (vtbl);
1847 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
1848 store_init_value (vtbl, DECL_INITIAL (vtbl));
1850 if (write_symbols == DWARF_DEBUG || write_symbols == DWARF2_DEBUG)
1852 /* Mark the VAR_DECL node representing the vtable itself as a
1853 "gratuitous" one, thereby forcing dwarfout.c to ignore it.
1854 It is rather important that such things be ignored because
1855 any effort to actually generate DWARF for them will run
1856 into trouble when/if we encounter code like:
1858 #pragma interface
1859 struct S { virtual void member (); };
1861 because the artificial declaration of the vtable itself (as
1862 manufactured by the g++ front end) will say that the vtable
1863 is a static member of `S' but only *after* the debug output
1864 for the definition of `S' has already been output. This causes
1865 grief because the DWARF entry for the definition of the vtable
1866 will try to refer back to an earlier *declaration* of the
1867 vtable as a static member of `S' and there won't be one.
1868 We might be able to arrange to have the "vtable static member"
1869 attached to the member list for `S' before the debug info for
1870 `S' get written (which would solve the problem) but that would
1871 require more intrusive changes to the g++ front end. */
1873 DECL_IGNORED_P (vtbl) = 1;
1876 /* Always make vtables weak. */
1877 if (flag_weak)
1878 comdat_linkage (vtbl);
1880 rest_of_decl_compilation (vtbl, NULL, 1, 1);
1882 if (flag_vtable_gc)
1883 output_vtable_inherit (vtbl);
1885 /* Because we're only doing syntax-checking, we'll never end up
1886 actually marking the variable as written. */
1887 if (flag_syntax_only)
1888 TREE_ASM_WRITTEN (vtbl) = 1;
1891 /* Since we're writing out the vtable here, also write the debug
1892 info. */
1893 note_debug_info_needed (ctype);
1895 return 1;
1898 /* Determines the proper settings of TREE_PUBLIC and DECL_EXTERNAL for an
1899 inline function or template instantiation at end-of-file. */
1901 void
1902 import_export_decl (decl)
1903 tree decl;
1905 if (DECL_INTERFACE_KNOWN (decl))
1906 return;
1908 if (DECL_TEMPLATE_INSTANTIATION (decl)
1909 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
1911 DECL_NOT_REALLY_EXTERN (decl) = 1;
1912 if ((DECL_IMPLICIT_INSTANTIATION (decl)
1913 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
1914 && (flag_implicit_templates
1915 || (flag_implicit_inline_templates
1916 && DECL_DECLARED_INLINE_P (decl))))
1918 if (!TREE_PUBLIC (decl))
1919 /* Templates are allowed to have internal linkage. See
1920 [basic.link]. */
1922 else
1923 comdat_linkage (decl);
1925 else
1927 DECL_EXTERNAL (decl) = 1;
1928 DECL_NOT_REALLY_EXTERN (decl) = 0;
1931 else if (DECL_FUNCTION_MEMBER_P (decl))
1933 if (!DECL_DECLARED_INLINE_P (decl))
1935 tree ctype = DECL_CONTEXT (decl);
1936 import_export_class (ctype);
1937 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1939 DECL_NOT_REALLY_EXTERN (decl)
1940 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
1941 || (DECL_DECLARED_INLINE_P (decl)
1942 && ! flag_implement_inlines
1943 && !DECL_VINDEX (decl)));
1945 if (!DECL_NOT_REALLY_EXTERN (decl))
1946 DECL_EXTERNAL (decl) = 1;
1948 /* Always make artificials weak. */
1949 if (DECL_ARTIFICIAL (decl) && flag_weak)
1950 comdat_linkage (decl);
1951 else
1952 maybe_make_one_only (decl);
1955 else
1956 comdat_linkage (decl);
1958 else
1959 comdat_linkage (decl);
1961 DECL_INTERFACE_KNOWN (decl) = 1;
1964 /* Here, we only decide whether or not the tinfo node should be
1965 emitted with the vtable. IS_IN_LIBRARY is nonzero iff the
1966 typeinfo for TYPE should be in the runtime library. */
1968 void
1969 import_export_tinfo (decl, type, is_in_library)
1970 tree decl;
1971 tree type;
1972 int is_in_library;
1974 if (DECL_INTERFACE_KNOWN (decl))
1975 return;
1977 if (IS_AGGR_TYPE (type))
1978 import_export_class (type);
1980 if (IS_AGGR_TYPE (type) && CLASSTYPE_INTERFACE_KNOWN (type)
1981 && TYPE_POLYMORPHIC_P (type)
1982 /* If -fno-rtti, we're not necessarily emitting this stuff with
1983 the class, so go ahead and emit it now. This can happen when
1984 a class is used in exception handling. */
1985 && flag_rtti)
1987 DECL_NOT_REALLY_EXTERN (decl) = !CLASSTYPE_INTERFACE_ONLY (type);
1988 DECL_COMDAT (decl) = 0;
1990 else
1992 DECL_NOT_REALLY_EXTERN (decl) = 1;
1993 DECL_COMDAT (decl) = 1;
1996 /* Now override some cases. */
1997 if (flag_weak)
1998 DECL_COMDAT (decl) = 1;
1999 else if (is_in_library)
2000 DECL_COMDAT (decl) = 0;
2002 DECL_INTERFACE_KNOWN (decl) = 1;
2005 tree
2006 build_cleanup (decl)
2007 tree decl;
2009 tree temp;
2010 tree type = TREE_TYPE (decl);
2012 if (TREE_CODE (type) == ARRAY_TYPE)
2013 temp = decl;
2014 else
2016 cxx_mark_addressable (decl);
2017 temp = build1 (ADDR_EXPR, build_pointer_type (type), decl);
2019 temp = build_delete (TREE_TYPE (temp), temp,
2020 sfk_complete_destructor,
2021 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
2022 return temp;
2025 /* Returns the initialization guard variable for the variable DECL,
2026 which has static storage duration. */
2028 tree
2029 get_guard (decl)
2030 tree decl;
2032 tree sname;
2033 tree guard;
2035 sname = mangle_guard_variable (decl);
2036 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2037 if (! guard)
2039 tree guard_type;
2041 /* We use a type that is big enough to contain a mutex as well
2042 as an integer counter. */
2043 guard_type = long_long_integer_type_node;
2044 guard = build_decl (VAR_DECL, sname, guard_type);
2046 /* The guard should have the same linkage as what it guards. */
2047 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
2048 TREE_STATIC (guard) = TREE_STATIC (decl);
2049 DECL_COMMON (guard) = DECL_COMMON (decl);
2050 DECL_ONE_ONLY (guard) = DECL_ONE_ONLY (decl);
2051 if (TREE_PUBLIC (decl))
2052 DECL_WEAK (guard) = DECL_WEAK (decl);
2054 DECL_ARTIFICIAL (guard) = 1;
2055 TREE_USED (guard) = 1;
2056 pushdecl_top_level (guard);
2057 cp_finish_decl (guard, NULL_TREE, NULL_TREE, 0);
2059 return guard;
2062 /* Return those bits of the GUARD variable that should be set when the
2063 guarded entity is actually initialized. */
2065 static tree
2066 get_guard_bits (guard)
2067 tree guard;
2069 /* We only set the first byte of the guard, in order to leave room
2070 for a mutex in the high-order bits. */
2071 guard = build1 (ADDR_EXPR,
2072 build_pointer_type (TREE_TYPE (guard)),
2073 guard);
2074 guard = build1 (NOP_EXPR,
2075 build_pointer_type (char_type_node),
2076 guard);
2077 guard = build1 (INDIRECT_REF, char_type_node, guard);
2079 return guard;
2082 /* Return an expression which determines whether or not the GUARD
2083 variable has already been initialized. */
2085 tree
2086 get_guard_cond (guard)
2087 tree guard;
2089 tree guard_value;
2091 /* Check to see if the GUARD is zero. */
2092 guard = get_guard_bits (guard);
2093 guard_value = integer_zero_node;
2094 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2095 guard_value = convert (TREE_TYPE (guard), guard_value);
2096 return cp_build_binary_op (EQ_EXPR, guard, guard_value);
2099 /* Return an expression which sets the GUARD variable, indicating that
2100 the variable being guarded has been initialized. */
2102 tree
2103 set_guard (guard)
2104 tree guard;
2106 tree guard_init;
2108 /* Set the GUARD to one. */
2109 guard = get_guard_bits (guard);
2110 guard_init = integer_one_node;
2111 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
2112 guard_init = convert (TREE_TYPE (guard), guard_init);
2113 return build_modify_expr (guard, NOP_EXPR, guard_init);
2116 /* Start the process of running a particular set of global constructors
2117 or destructors. Subroutine of do_[cd]tors. */
2119 static tree
2120 start_objects (method_type, initp)
2121 int method_type, initp;
2123 tree fnname;
2124 tree body;
2125 char type[10];
2127 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
2129 if (initp != DEFAULT_INIT_PRIORITY)
2131 char joiner;
2133 #ifdef JOINER
2134 joiner = JOINER;
2135 #else
2136 joiner = '_';
2137 #endif
2139 sprintf (type, "%c%c%.5u", method_type, joiner, initp);
2141 else
2142 sprintf (type, "%c", method_type);
2144 fnname = get_file_function_name_long (type);
2146 start_function (void_list_node,
2147 make_call_declarator (fnname, void_list_node, NULL_TREE,
2148 NULL_TREE),
2149 NULL_TREE, SF_DEFAULT);
2151 /* It can be a static function as long as collect2 does not have
2152 to scan the object file to find its ctor/dtor routine. */
2153 TREE_PUBLIC (current_function_decl) = ! targetm.have_ctors_dtors;
2155 /* Mark this declaration as used to avoid spurious warnings. */
2156 TREE_USED (current_function_decl) = 1;
2158 /* Mark this function as a global constructor or destructor. */
2159 if (method_type == 'I')
2160 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
2161 else
2162 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
2163 DECL_LANG_SPECIFIC (current_function_decl)->decl_flags.u2sel = 1;
2165 body = begin_compound_stmt (/*has_no_scope=*/0);
2167 /* We cannot allow these functions to be elided, even if they do not
2168 have external linkage. And, there's no point in deferring
2169 copmilation of thes functions; they're all going to have to be
2170 out anyhow. */
2171 current_function_cannot_inline
2172 = "static constructors and destructors cannot be inlined";
2174 return body;
2177 /* Finish the process of running a particular set of global constructors
2178 or destructors. Subroutine of do_[cd]tors. */
2180 static void
2181 finish_objects (method_type, initp, body)
2182 int method_type, initp;
2183 tree body;
2185 tree fn;
2187 /* Finish up. */
2188 finish_compound_stmt (/*has_no_scope=*/0, body);
2189 fn = finish_function (0);
2190 expand_body (fn);
2192 /* When only doing semantic analysis, and no RTL generation, we
2193 can't call functions that directly emit assembly code; there is
2194 no assembly file in which to put the code. */
2195 if (flag_syntax_only)
2196 return;
2198 if (targetm.have_ctors_dtors)
2200 rtx fnsym = XEXP (DECL_RTL (fn), 0);
2201 if (method_type == 'I')
2202 (* targetm.asm_out.constructor) (fnsym, initp);
2203 else
2204 (* targetm.asm_out.destructor) (fnsym, initp);
2208 /* The names of the parameters to the function created to handle
2209 initializations and destructions for objects with static storage
2210 duration. */
2211 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
2212 #define PRIORITY_IDENTIFIER "__priority"
2214 /* The name of the function we create to handle initializations and
2215 destructions for objects with static storage duration. */
2216 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
2218 /* The declaration for the __INITIALIZE_P argument. */
2219 static GTY(()) tree initialize_p_decl;
2221 /* The declaration for the __PRIORITY argument. */
2222 static GTY(()) tree priority_decl;
2224 /* The declaration for the static storage duration function. */
2225 static GTY(()) tree ssdf_decl;
2227 /* All the static storage duration functions created in this
2228 translation unit. */
2229 static GTY(()) varray_type ssdf_decls;
2231 /* A map from priority levels to information about that priority
2232 level. There may be many such levels, so efficient lookup is
2233 important. */
2234 static splay_tree priority_info_map;
2236 /* Begins the generation of the function that will handle all
2237 initialization and destruction of objects with static storage
2238 duration. The function generated takes two parameters of type
2239 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
2240 nonzero, it performs initializations. Otherwise, it performs
2241 destructions. It only performs those initializations or
2242 destructions with the indicated __PRIORITY. The generated function
2243 returns no value.
2245 It is assumed that this function will only be called once per
2246 translation unit. */
2248 static tree
2249 start_static_storage_duration_function ()
2251 static unsigned ssdf_number;
2253 tree parm_types;
2254 tree type;
2255 tree body;
2256 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
2258 /* Create the identifier for this function. It will be of the form
2259 SSDF_IDENTIFIER_<number>. */
2260 sprintf (id, "%s_%u", SSDF_IDENTIFIER, ssdf_number++);
2261 if (ssdf_number == 0)
2263 /* Overflow occurred. That means there are at least 4 billion
2264 initialization functions. */
2265 sorry ("too many initialization functions required");
2266 abort ();
2269 /* Create the parameters. */
2270 parm_types = void_list_node;
2271 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
2272 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
2273 type = build_function_type (void_type_node, parm_types);
2275 /* Create the FUNCTION_DECL itself. */
2276 ssdf_decl = build_lang_decl (FUNCTION_DECL,
2277 get_identifier (id),
2278 type);
2279 TREE_PUBLIC (ssdf_decl) = 0;
2280 DECL_ARTIFICIAL (ssdf_decl) = 1;
2282 /* Put this function in the list of functions to be called from the
2283 static constructors and destructors. */
2284 if (!ssdf_decls)
2286 VARRAY_TREE_INIT (ssdf_decls, 32, "ssdf_decls");
2288 /* Take this opportunity to initialize the map from priority
2289 numbers to information about that priority level. */
2290 priority_info_map = splay_tree_new (splay_tree_compare_ints,
2291 /*delete_key_fn=*/0,
2292 /*delete_value_fn=*/
2293 (splay_tree_delete_value_fn) &free);
2295 /* We always need to generate functions for the
2296 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
2297 priorities later, we'll be sure to find the
2298 DEFAULT_INIT_PRIORITY. */
2299 get_priority_info (DEFAULT_INIT_PRIORITY);
2302 VARRAY_PUSH_TREE (ssdf_decls, ssdf_decl);
2304 /* Create the argument list. */
2305 initialize_p_decl = cp_build_parm_decl
2306 (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
2307 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
2308 TREE_USED (initialize_p_decl) = 1;
2309 priority_decl = cp_build_parm_decl
2310 (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
2311 DECL_CONTEXT (priority_decl) = ssdf_decl;
2312 TREE_USED (priority_decl) = 1;
2314 TREE_CHAIN (initialize_p_decl) = priority_decl;
2315 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
2317 /* Put the function in the global scope. */
2318 pushdecl (ssdf_decl);
2320 /* Start the function itself. This is equivalent to declarating the
2321 function as:
2323 static void __ssdf (int __initialize_p, init __priority_p);
2325 It is static because we only need to call this function from the
2326 various constructor and destructor functions for this module. */
2327 start_function (/*specs=*/NULL_TREE,
2328 ssdf_decl,
2329 /*attrs=*/NULL_TREE,
2330 SF_PRE_PARSED);
2332 /* Set up the scope of the outermost block in the function. */
2333 body = begin_compound_stmt (/*has_no_scope=*/0);
2335 /* This function must not be deferred because we are depending on
2336 its compilation to tell us what is TREE_SYMBOL_REFERENCED. */
2337 current_function_cannot_inline
2338 = "static storage duration functions cannot be inlined";
2340 return body;
2343 /* Finish the generation of the function which performs initialization
2344 and destruction of objects with static storage duration. After
2345 this point, no more such objects can be created. */
2347 static void
2348 finish_static_storage_duration_function (body)
2349 tree body;
2351 /* Close out the function. */
2352 finish_compound_stmt (/*has_no_scope=*/0, body);
2353 expand_body (finish_function (0));
2356 /* Return the information about the indicated PRIORITY level. If no
2357 code to handle this level has yet been generated, generate the
2358 appropriate prologue. */
2360 static priority_info
2361 get_priority_info (priority)
2362 int priority;
2364 priority_info pi;
2365 splay_tree_node n;
2367 n = splay_tree_lookup (priority_info_map,
2368 (splay_tree_key) priority);
2369 if (!n)
2371 /* Create a new priority information structure, and insert it
2372 into the map. */
2373 pi = (priority_info) xmalloc (sizeof (struct priority_info_s));
2374 pi->initializations_p = 0;
2375 pi->destructions_p = 0;
2376 splay_tree_insert (priority_info_map,
2377 (splay_tree_key) priority,
2378 (splay_tree_value) pi);
2380 else
2381 pi = (priority_info) n->value;
2383 return pi;
2386 /* Set up to handle the initialization or destruction of DECL. If
2387 INITP is nonzero, we are initializing the variable. Otherwise, we
2388 are destroying it. */
2390 static tree
2391 start_static_initialization_or_destruction (decl, initp)
2392 tree decl;
2393 int initp;
2395 tree guard_if_stmt = NULL_TREE;
2396 int priority;
2397 tree cond;
2398 tree guard;
2399 tree init_cond;
2400 priority_info pi;
2402 /* Figure out the priority for this declaration. */
2403 priority = DECL_INIT_PRIORITY (decl);
2404 if (!priority)
2405 priority = DEFAULT_INIT_PRIORITY;
2407 /* Remember that we had an initialization or finalization at this
2408 priority. */
2409 pi = get_priority_info (priority);
2410 if (initp)
2411 pi->initializations_p = 1;
2412 else
2413 pi->destructions_p = 1;
2415 /* Trick the compiler into thinking we are at the file and line
2416 where DECL was declared so that error-messages make sense, and so
2417 that the debugger will show somewhat sensible file and line
2418 information. */
2419 input_filename = DECL_SOURCE_FILE (decl);
2420 lineno = DECL_SOURCE_LINE (decl);
2422 /* Because of:
2424 [class.access.spec]
2426 Access control for implicit calls to the constructors,
2427 the conversion functions, or the destructor called to
2428 create and destroy a static data member is performed as
2429 if these calls appeared in the scope of the member's
2430 class.
2432 we pretend we are in a static member function of the class of
2433 which the DECL is a member. */
2434 if (member_p (decl))
2436 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
2437 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
2440 /* Conditionalize this initialization on being in the right priority
2441 and being initializing/finalizing appropriately. */
2442 guard_if_stmt = begin_if_stmt ();
2443 cond = cp_build_binary_op (EQ_EXPR,
2444 priority_decl,
2445 build_int_2 (priority, 0));
2446 init_cond = initp ? integer_one_node : integer_zero_node;
2447 init_cond = cp_build_binary_op (EQ_EXPR,
2448 initialize_p_decl,
2449 init_cond);
2450 cond = cp_build_binary_op (TRUTH_ANDIF_EXPR, cond, init_cond);
2452 /* Assume we don't need a guard. */
2453 guard = NULL_TREE;
2454 /* We need a guard if this is an object with external linkage that
2455 might be initialized in more than one place. (For example, a
2456 static data member of a template, when the data member requires
2457 construction.) */
2458 if (TREE_PUBLIC (decl) && (DECL_COMMON (decl)
2459 || DECL_ONE_ONLY (decl)
2460 || DECL_WEAK (decl)))
2462 tree guard_cond;
2464 guard = get_guard (decl);
2466 /* When using __cxa_atexit, we just check the GUARD as we would
2467 for a local static. */
2468 if (flag_use_cxa_atexit)
2470 /* When using __cxa_atexit, we never try to destroy
2471 anything from a static destructor. */
2472 my_friendly_assert (initp, 20000629);
2473 guard_cond = get_guard_cond (guard);
2475 /* If we don't have __cxa_atexit, then we will be running
2476 destructors from .fini sections, or their equivalents. So,
2477 we need to know how many times we've tried to initialize this
2478 object. We do initializations only if the GUARD is zero,
2479 i.e., if we are the first to initialize the variable. We do
2480 destructions only if the GUARD is one, i.e., if we are the
2481 last to destroy the variable. */
2482 else if (initp)
2483 guard_cond
2484 = cp_build_binary_op (EQ_EXPR,
2485 build_unary_op (PREINCREMENT_EXPR,
2486 guard,
2487 /*noconvert=*/1),
2488 integer_one_node);
2489 else
2490 guard_cond
2491 = cp_build_binary_op (EQ_EXPR,
2492 build_unary_op (PREDECREMENT_EXPR,
2493 guard,
2494 /*noconvert=*/1),
2495 integer_zero_node);
2497 cond = cp_build_binary_op (TRUTH_ANDIF_EXPR, cond, guard_cond);
2500 finish_if_stmt_cond (cond, guard_if_stmt);
2502 /* If we're using __cxa_atexit, we have not already set the GUARD,
2503 so we must do so now. */
2504 if (guard && initp && flag_use_cxa_atexit)
2505 finish_expr_stmt (set_guard (guard));
2507 return guard_if_stmt;
2510 /* We've just finished generating code to do an initialization or
2511 finalization. GUARD_IF_STMT is the if-statement we used to guard
2512 the initialization. */
2514 static void
2515 finish_static_initialization_or_destruction (guard_if_stmt)
2516 tree guard_if_stmt;
2518 finish_then_clause (guard_if_stmt);
2519 finish_if_stmt ();
2521 /* Now that we're done with DECL we don't need to pretend to be a
2522 member of its class any longer. */
2523 DECL_CONTEXT (current_function_decl) = NULL_TREE;
2524 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
2527 /* Generate code to do the initialization of DECL, a VAR_DECL with
2528 static storage duration. The initialization is INIT. */
2530 static void
2531 do_static_initialization (decl, init)
2532 tree decl;
2533 tree init;
2535 tree guard_if_stmt;
2537 /* Set up for the initialization. */
2538 guard_if_stmt
2539 = start_static_initialization_or_destruction (decl,
2540 /*initp=*/1);
2542 /* Perform the initialization. */
2543 if (init)
2544 finish_expr_stmt (init);
2546 /* If we're using __cxa_atexit, register a a function that calls the
2547 destructor for the object. */
2548 if (flag_use_cxa_atexit)
2549 register_dtor_fn (decl);
2551 /* Finsh up. */
2552 finish_static_initialization_or_destruction (guard_if_stmt);
2555 /* Generate code to do the static destruction of DECL. If DECL may be
2556 initialized more than once in different object files, GUARD is the
2557 guard variable to check. PRIORITY is the priority for the
2558 destruction. */
2560 static void
2561 do_static_destruction (decl)
2562 tree decl;
2564 tree guard_if_stmt;
2566 /* If we're using __cxa_atexit, then destructors are registered
2567 immediately after objects are initialized. */
2568 my_friendly_assert (!flag_use_cxa_atexit, 20000121);
2570 /* If we don't need a destructor, there's nothing to do. */
2571 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
2572 return;
2574 /* Actually do the destruction. */
2575 guard_if_stmt = start_static_initialization_or_destruction (decl,
2576 /*initp=*/0);
2577 finish_expr_stmt (build_cleanup (decl));
2578 finish_static_initialization_or_destruction (guard_if_stmt);
2581 /* VARS is a list of variables with static storage duration which may
2582 need initialization and/or finalization. Remove those variables
2583 that don't really need to be initialized or finalized, and return
2584 the resulting list. The order in which the variables appear in
2585 VARS is in reverse order of the order in which they should actually
2586 be initialized. The list we return is in the unreversed order;
2587 i.e., the first variable should be initialized first. */
2589 static tree
2590 prune_vars_needing_no_initialization (vars)
2591 tree vars;
2593 tree var;
2594 tree result;
2596 for (var = vars, result = NULL_TREE;
2597 var;
2598 var = TREE_CHAIN (var))
2600 tree decl = TREE_VALUE (var);
2601 tree init = TREE_PURPOSE (var);
2603 /* Deal gracefully with error. */
2604 if (decl == error_mark_node)
2605 continue;
2607 /* The only things that can be initialized are variables. */
2608 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 19990420);
2610 /* If this object is not defined, we don't need to do anything
2611 here. */
2612 if (DECL_EXTERNAL (decl))
2613 continue;
2615 /* Also, if the initializer already contains errors, we can bail
2616 out now. */
2617 if (init && TREE_CODE (init) == TREE_LIST
2618 && value_member (error_mark_node, init))
2619 continue;
2621 /* This variable is going to need initialization and/or
2622 finalization, so we add it to the list. */
2623 result = tree_cons (init, decl, result);
2626 return result;
2629 /* Make sure we have told the back end about all the variables in
2630 VARS. */
2632 static void
2633 write_out_vars (vars)
2634 tree vars;
2636 tree v;
2638 for (v = vars; v; v = TREE_CHAIN (v))
2639 if (! TREE_ASM_WRITTEN (TREE_VALUE (v)))
2640 rest_of_decl_compilation (TREE_VALUE (v), 0, 1, 1);
2643 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
2644 (otherwise) that will initialize all gobal objects with static
2645 storage duration having the indicated PRIORITY. */
2647 static void
2648 generate_ctor_or_dtor_function (constructor_p, priority)
2649 int constructor_p;
2650 int priority;
2652 char function_key;
2653 tree arguments;
2654 tree body;
2655 size_t i;
2657 /* We use `I' to indicate initialization and `D' to indicate
2658 destruction. */
2659 if (constructor_p)
2660 function_key = 'I';
2661 else
2662 function_key = 'D';
2664 /* Begin the function. */
2665 body = start_objects (function_key, priority);
2667 /* Call the static storage duration function with appropriate
2668 arguments. */
2669 for (i = 0; i < ssdf_decls->elements_used; ++i)
2671 arguments = tree_cons (NULL_TREE, build_int_2 (priority, 0),
2672 NULL_TREE);
2673 arguments = tree_cons (NULL_TREE, build_int_2 (constructor_p, 0),
2674 arguments);
2675 finish_expr_stmt (build_function_call (VARRAY_TREE (ssdf_decls, i),
2676 arguments));
2679 /* If we're generating code for the DEFAULT_INIT_PRIORITY, throw in
2680 calls to any functions marked with attributes indicating that
2681 they should be called at initialization- or destruction-time. */
2682 if (priority == DEFAULT_INIT_PRIORITY)
2684 tree fns;
2686 for (fns = constructor_p ? static_ctors : static_dtors;
2687 fns;
2688 fns = TREE_CHAIN (fns))
2689 finish_expr_stmt (build_function_call (TREE_VALUE (fns), NULL_TREE));
2692 /* Close out the function. */
2693 finish_objects (function_key, priority, body);
2696 /* Generate constructor and destructor functions for the priority
2697 indicated by N. */
2699 static int
2700 generate_ctor_and_dtor_functions_for_priority (n, data)
2701 splay_tree_node n;
2702 void *data ATTRIBUTE_UNUSED;
2704 int priority = (int) n->key;
2705 priority_info pi = (priority_info) n->value;
2707 /* Generate the functions themselves, but only if they are really
2708 needed. */
2709 if (pi->initializations_p
2710 || (priority == DEFAULT_INIT_PRIORITY && static_ctors))
2711 generate_ctor_or_dtor_function (/*constructor_p=*/1,
2712 priority);
2713 if (pi->destructions_p
2714 || (priority == DEFAULT_INIT_PRIORITY && static_dtors))
2715 generate_ctor_or_dtor_function (/*constructor_p=*/0,
2716 priority);
2718 /* Keep iterating. */
2719 return 0;
2722 /* This routine is called from the last rule in yyparse ().
2723 Its job is to create all the code needed to initialize and
2724 destroy the global aggregates. We do the destruction
2725 first, since that way we only need to reverse the decls once. */
2727 void
2728 finish_file ()
2730 tree vars;
2731 int reconsider;
2732 size_t i;
2734 at_eof = 1;
2736 /* Bad parse errors. Just forget about it. */
2737 if (! global_bindings_p () || current_class_type || decl_namespace_list)
2738 return;
2740 /* Otherwise, GDB can get confused, because in only knows
2741 about source for LINENO-1 lines. */
2742 lineno -= 1;
2744 interface_unknown = 1;
2745 interface_only = 0;
2747 /* We now have to write out all the stuff we put off writing out.
2748 These include:
2750 o Template specializations that we have not yet instantiated,
2751 but which are needed.
2752 o Initialization and destruction for non-local objects with
2753 static storage duration. (Local objects with static storage
2754 duration are initialized when their scope is first entered,
2755 and are cleaned up via atexit.)
2756 o Virtual function tables.
2758 All of these may cause others to be needed. For example,
2759 instantiating one function may cause another to be needed, and
2760 generating the initializer for an object may cause templates to be
2761 instantiated, etc., etc. */
2763 timevar_push (TV_VARCONST);
2765 emit_support_tinfos ();
2769 tree t;
2771 reconsider = 0;
2773 /* If there are templates that we've put off instantiating, do
2774 them now. */
2775 instantiate_pending_templates ();
2777 /* Write out virtual tables as required. Note that writing out
2778 the virtual table for a template class may cause the
2779 instantiation of members of that class. */
2780 for (t = dynamic_classes; t; t = TREE_CHAIN (t))
2781 if (maybe_emit_vtables (TREE_VALUE (t)))
2782 reconsider = 1;
2784 /* Write out needed type info variables. Writing out one variable
2785 might cause others to be needed. */
2786 if (walk_globals (unemitted_tinfo_decl_p, emit_tinfo_decl, /*data=*/0))
2787 reconsider = 1;
2789 /* The list of objects with static storage duration is built up
2790 in reverse order. We clear STATIC_AGGREGATES so that any new
2791 aggregates added during the initialization of these will be
2792 initialized in the correct order when we next come around the
2793 loop. */
2794 vars = prune_vars_needing_no_initialization (static_aggregates);
2795 static_aggregates = NULL_TREE;
2797 if (vars)
2799 tree v;
2801 /* We need to start a new initialization function each time
2802 through the loop. That's because we need to know which
2803 vtables have been referenced, and TREE_SYMBOL_REFERENCED
2804 isn't computed until a function is finished, and written
2805 out. That's a deficiency in the back-end. When this is
2806 fixed, these initialization functions could all become
2807 inline, with resulting performance improvements. */
2808 tree ssdf_body = start_static_storage_duration_function ();
2810 /* Make sure the back end knows about all the variables. */
2811 write_out_vars (vars);
2813 /* First generate code to do all the initializations. */
2814 for (v = vars; v; v = TREE_CHAIN (v))
2815 do_static_initialization (TREE_VALUE (v),
2816 TREE_PURPOSE (v));
2818 /* Then, generate code to do all the destructions. Do these
2819 in reverse order so that the most recently constructed
2820 variable is the first destroyed. If we're using
2821 __cxa_atexit, then we don't need to do this; functions
2822 were registered at initialization time to destroy the
2823 local statics. */
2824 if (!flag_use_cxa_atexit)
2826 vars = nreverse (vars);
2827 for (v = vars; v; v = TREE_CHAIN (v))
2828 do_static_destruction (TREE_VALUE (v));
2830 else
2831 vars = NULL_TREE;
2833 /* Finish up the static storage duration function for this
2834 round. */
2835 finish_static_storage_duration_function (ssdf_body);
2837 /* All those initializations and finalizations might cause
2838 us to need more inline functions, more template
2839 instantiations, etc. */
2840 reconsider = 1;
2843 /* Go through the various inline functions, and see if any need
2844 synthesizing. */
2845 for (i = 0; i < deferred_fns_used; ++i)
2847 tree decl = VARRAY_TREE (deferred_fns, i);
2848 import_export_decl (decl);
2849 if (DECL_ARTIFICIAL (decl) && ! DECL_INITIAL (decl)
2850 && TREE_USED (decl)
2851 && (! DECL_REALLY_EXTERN (decl) || DECL_INLINE (decl)))
2853 /* Even though we're already at the top-level, we push
2854 there again. That way, when we pop back a few lines
2855 hence, all of our state is restored. Otherwise,
2856 finish_function doesn't clean things up, and we end
2857 up with CURRENT_FUNCTION_DECL set. */
2858 push_to_top_level ();
2859 synthesize_method (decl);
2860 pop_from_top_level ();
2861 reconsider = 1;
2865 /* We lie to the back-end, pretending that some functions are
2866 not defined when they really are. This keeps these functions
2867 from being put out unnecessarily. But, we must stop lying
2868 when the functions are referenced, or if they are not comdat
2869 since they need to be put out now.
2870 This is done in a separate for cycle, because if some deferred
2871 function is contained in another deferred function later in
2872 deferred_fns varray, rest_of_compilation would skip this
2873 function and we really cannot expand the same function twice. */
2874 for (i = 0; i < deferred_fns_used; ++i)
2876 tree decl = VARRAY_TREE (deferred_fns, i);
2878 if (DECL_NOT_REALLY_EXTERN (decl)
2879 && DECL_INITIAL (decl)
2880 && DECL_NEEDED_P (decl))
2881 DECL_EXTERNAL (decl) = 0;
2884 for (i = 0; i < deferred_fns_used; ++i)
2886 tree decl = VARRAY_TREE (deferred_fns, i);
2888 /* If we're going to need to write this function out, and
2889 there's already a body for it, create RTL for it now.
2890 (There might be no body if this is a method we haven't
2891 gotten around to synthesizing yet.) */
2892 if (!DECL_EXTERNAL (decl)
2893 && DECL_NEEDED_P (decl)
2894 && DECL_SAVED_TREE (decl)
2895 && !TREE_ASM_WRITTEN (decl))
2897 int saved_not_really_extern;
2899 /* When we call finish_function in expand_body, it will
2900 try to reset DECL_NOT_REALLY_EXTERN so we save and
2901 restore it here. */
2902 saved_not_really_extern = DECL_NOT_REALLY_EXTERN (decl);
2903 /* Generate RTL for this function now that we know we
2904 need it. */
2905 expand_body (decl);
2906 /* Undo the damage done by finish_function. */
2907 DECL_EXTERNAL (decl) = 0;
2908 DECL_NOT_REALLY_EXTERN (decl) = saved_not_really_extern;
2909 /* If we're compiling -fsyntax-only pretend that this
2910 function has been written out so that we don't try to
2911 expand it again. */
2912 if (flag_syntax_only)
2913 TREE_ASM_WRITTEN (decl) = 1;
2914 reconsider = 1;
2918 if (deferred_fns_used
2919 && wrapup_global_declarations (&VARRAY_TREE (deferred_fns, 0),
2920 deferred_fns_used))
2921 reconsider = 1;
2922 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
2923 reconsider = 1;
2925 /* Static data members are just like namespace-scope globals. */
2926 for (i = 0; i < pending_statics_used; ++i)
2928 tree decl = VARRAY_TREE (pending_statics, i);
2929 if (TREE_ASM_WRITTEN (decl))
2930 continue;
2931 import_export_decl (decl);
2932 if (DECL_NOT_REALLY_EXTERN (decl) && ! DECL_IN_AGGR_P (decl))
2933 DECL_EXTERNAL (decl) = 0;
2935 if (pending_statics
2936 && wrapup_global_declarations (&VARRAY_TREE (pending_statics, 0),
2937 pending_statics_used))
2938 reconsider = 1;
2940 while (reconsider);
2942 /* We give C linkage to static constructors and destructors. */
2943 push_lang_context (lang_name_c);
2945 /* Generate initialization and destruction functions for all
2946 priorities for which they are required. */
2947 if (priority_info_map)
2948 splay_tree_foreach (priority_info_map,
2949 generate_ctor_and_dtor_functions_for_priority,
2950 /*data=*/0);
2952 /* We're done with the splay-tree now. */
2953 if (priority_info_map)
2954 splay_tree_delete (priority_info_map);
2956 /* We're done with static constructors, so we can go back to "C++"
2957 linkage now. */
2958 pop_lang_context ();
2960 /* Now, issue warnings about static, but not defined, functions,
2961 etc., and emit debugging information. */
2962 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
2963 if (pending_statics)
2964 check_global_declarations (&VARRAY_TREE (pending_statics, 0),
2965 pending_statics_used);
2967 finish_repo ();
2969 /* The entire file is now complete. If requested, dump everything
2970 to a file. */
2972 int flags;
2973 FILE *stream = dump_begin (TDI_all, &flags);
2975 if (stream)
2977 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
2978 dump_end (TDI_all, stream);
2982 timevar_pop (TV_VARCONST);
2984 if (flag_detailed_statistics)
2986 dump_tree_statistics ();
2987 dump_time_statistics ();
2991 /* This is something of the form 'A()()()()()+1' that has turned out to be an
2992 expr. Since it was parsed like a type, we need to wade through and fix
2993 that. Unfortunately, since operator() is left-associative, we can't use
2994 tail recursion. In the above example, TYPE is `A', and DECL is
2995 `()()()()()'.
2997 Maybe this shouldn't be recursive, but how often will it actually be
2998 used? (jason) */
3000 tree
3001 reparse_absdcl_as_expr (type, decl)
3002 tree type, decl;
3004 /* do build_functional_cast (type, NULL_TREE) at bottom */
3005 if (TREE_OPERAND (decl, 0) == NULL_TREE)
3006 return build_functional_cast (type, NULL_TREE);
3008 /* recurse */
3009 decl = reparse_absdcl_as_expr (type, TREE_OPERAND (decl, 0));
3011 return finish_call_expr (decl, NULL_TREE, /*disallow_virtual=*/false);
3014 /* This is something of the form `int ((int)(int)(int)1)' that has turned
3015 out to be an expr. Since it was parsed like a type, we need to wade
3016 through and fix that. Since casts are right-associative, we are
3017 reversing the order, so we don't have to recurse.
3019 In the above example, DECL is the `(int)(int)(int)', and EXPR is the
3020 `1'. */
3022 tree
3023 reparse_absdcl_as_casts (decl, expr)
3024 tree decl, expr;
3026 tree type;
3027 int non_void_p = 0;
3029 if (TREE_CODE (expr) == CONSTRUCTOR
3030 && TREE_TYPE (expr) == 0)
3032 type = groktypename (TREE_VALUE (CALL_DECLARATOR_PARMS (decl)));
3033 decl = TREE_OPERAND (decl, 0);
3035 if (processing_template_decl)
3036 TREE_TYPE (expr) = type;
3037 else
3039 expr = digest_init (type, expr, (tree *) 0);
3040 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3042 int failure = complete_array_type (type, expr, 1);
3043 my_friendly_assert (!failure, 78);
3048 while (decl)
3050 type = groktypename (TREE_VALUE (CALL_DECLARATOR_PARMS (decl)));
3051 decl = TREE_OPERAND (decl, 0);
3052 if (!VOID_TYPE_P (type))
3053 non_void_p = 1;
3054 expr = build_c_cast (type, expr);
3057 if (warn_old_style_cast && ! in_system_header
3058 && non_void_p && current_lang_name != lang_name_c)
3059 warning ("use of old-style cast");
3061 return expr;
3064 /* T is the parse tree for an expression. Return the expression after
3065 performing semantic analysis. */
3067 tree
3068 build_expr_from_tree (t)
3069 tree t;
3071 if (t == NULL_TREE || t == error_mark_node)
3072 return t;
3074 switch (TREE_CODE (t))
3076 case IDENTIFIER_NODE:
3077 return do_identifier (t, 0, NULL_TREE);
3079 case LOOKUP_EXPR:
3080 if (LOOKUP_EXPR_GLOBAL (t))
3082 tree token = TREE_OPERAND (t, 0);
3083 return do_scoped_id (token, IDENTIFIER_GLOBAL_VALUE (token));
3085 else
3086 return do_identifier (TREE_OPERAND (t, 0), 0, NULL_TREE);
3088 case TEMPLATE_ID_EXPR:
3090 tree template;
3091 tree args;
3092 tree object;
3094 template = build_expr_from_tree (TREE_OPERAND (t, 0));
3095 args = build_expr_from_tree (TREE_OPERAND (t, 1));
3097 if (TREE_CODE (template) == COMPONENT_REF)
3099 object = TREE_OPERAND (template, 0);
3100 template = TREE_OPERAND (template, 1);
3102 else
3103 object = NULL_TREE;
3105 template = lookup_template_function (template, args);
3106 if (object)
3107 return build (COMPONENT_REF, TREE_TYPE (template),
3108 object, template);
3109 else
3110 return template;
3113 case INDIRECT_REF:
3114 return build_x_indirect_ref
3115 (build_expr_from_tree (TREE_OPERAND (t, 0)), "unary *");
3117 case CAST_EXPR:
3118 return build_functional_cast
3119 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3121 case REINTERPRET_CAST_EXPR:
3122 return build_reinterpret_cast
3123 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3125 case CONST_CAST_EXPR:
3126 return build_const_cast
3127 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3129 case DYNAMIC_CAST_EXPR:
3130 return build_dynamic_cast
3131 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3133 case STATIC_CAST_EXPR:
3134 return build_static_cast
3135 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3137 case PREDECREMENT_EXPR:
3138 case PREINCREMENT_EXPR:
3139 case POSTDECREMENT_EXPR:
3140 case POSTINCREMENT_EXPR:
3141 case NEGATE_EXPR:
3142 case BIT_NOT_EXPR:
3143 case ABS_EXPR:
3144 case TRUTH_NOT_EXPR:
3145 case ADDR_EXPR:
3146 case CONVERT_EXPR: /* Unary + */
3147 case REALPART_EXPR:
3148 case IMAGPART_EXPR:
3149 if (TREE_TYPE (t))
3150 return t;
3151 return build_x_unary_op (TREE_CODE (t),
3152 build_expr_from_tree (TREE_OPERAND (t, 0)));
3154 case PLUS_EXPR:
3155 case MINUS_EXPR:
3156 case MULT_EXPR:
3157 case TRUNC_DIV_EXPR:
3158 case CEIL_DIV_EXPR:
3159 case FLOOR_DIV_EXPR:
3160 case ROUND_DIV_EXPR:
3161 case EXACT_DIV_EXPR:
3162 case BIT_AND_EXPR:
3163 case BIT_ANDTC_EXPR:
3164 case BIT_IOR_EXPR:
3165 case BIT_XOR_EXPR:
3166 case TRUNC_MOD_EXPR:
3167 case FLOOR_MOD_EXPR:
3168 case TRUTH_ANDIF_EXPR:
3169 case TRUTH_ORIF_EXPR:
3170 case TRUTH_AND_EXPR:
3171 case TRUTH_OR_EXPR:
3172 case RSHIFT_EXPR:
3173 case LSHIFT_EXPR:
3174 case RROTATE_EXPR:
3175 case LROTATE_EXPR:
3176 case EQ_EXPR:
3177 case NE_EXPR:
3178 case MAX_EXPR:
3179 case MIN_EXPR:
3180 case LE_EXPR:
3181 case GE_EXPR:
3182 case LT_EXPR:
3183 case GT_EXPR:
3184 case MEMBER_REF:
3185 return build_x_binary_op
3186 (TREE_CODE (t),
3187 build_expr_from_tree (TREE_OPERAND (t, 0)),
3188 build_expr_from_tree (TREE_OPERAND (t, 1)));
3190 case DOTSTAR_EXPR:
3191 return build_m_component_ref
3192 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3193 build_expr_from_tree (TREE_OPERAND (t, 1)));
3195 case SCOPE_REF:
3196 return build_offset_ref (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1));
3198 case ARRAY_REF:
3199 if (TREE_OPERAND (t, 0) == NULL_TREE)
3200 /* new-type-id */
3201 return build_nt (ARRAY_REF, NULL_TREE,
3202 build_expr_from_tree (TREE_OPERAND (t, 1)));
3203 return grok_array_decl (build_expr_from_tree (TREE_OPERAND (t, 0)),
3204 build_expr_from_tree (TREE_OPERAND (t, 1)));
3206 case SIZEOF_EXPR:
3207 case ALIGNOF_EXPR:
3209 tree r = build_expr_from_tree (TREE_OPERAND (t, 0));
3210 if (!TYPE_P (r))
3211 return TREE_CODE (t) == SIZEOF_EXPR ? expr_sizeof (r) : c_alignof_expr (r);
3212 else
3213 return cxx_sizeof_or_alignof_type (r, TREE_CODE (t), true);
3216 case MODOP_EXPR:
3217 return build_x_modify_expr
3218 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3219 TREE_CODE (TREE_OPERAND (t, 1)),
3220 build_expr_from_tree (TREE_OPERAND (t, 2)));
3222 case ARROW_EXPR:
3223 return build_x_arrow
3224 (build_expr_from_tree (TREE_OPERAND (t, 0)));
3226 case NEW_EXPR:
3227 return build_new
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)),
3231 NEW_EXPR_USE_GLOBAL (t));
3233 case DELETE_EXPR:
3234 return delete_sanity
3235 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3236 build_expr_from_tree (TREE_OPERAND (t, 1)),
3237 DELETE_EXPR_USE_VEC (t), DELETE_EXPR_USE_GLOBAL (t));
3239 case COMPOUND_EXPR:
3240 if (TREE_OPERAND (t, 1) == NULL_TREE)
3241 return build_x_compound_expr
3242 (build_expr_from_tree (TREE_OPERAND (t, 0)));
3243 else
3244 abort ();
3246 case METHOD_CALL_EXPR:
3247 if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
3249 tree ref = TREE_OPERAND (t, 0);
3250 tree name = TREE_OPERAND (ref, 1);
3252 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3253 name = build_nt (TEMPLATE_ID_EXPR,
3254 TREE_OPERAND (name, 0),
3255 build_expr_from_tree (TREE_OPERAND (name, 1)));
3257 return build_scoped_method_call
3258 (build_expr_from_tree (TREE_OPERAND (t, 1)),
3259 build_expr_from_tree (TREE_OPERAND (ref, 0)),
3260 name,
3261 build_expr_from_tree (TREE_OPERAND (t, 2)));
3263 else
3265 tree fn = TREE_OPERAND (t, 0);
3267 /* We can get a TEMPLATE_ID_EXPR here on code like:
3269 x->f<2>();
3271 so we must resolve that. However, we can also get things
3272 like a BIT_NOT_EXPR here, when referring to a destructor,
3273 and things like that are not correctly resolved by
3274 build_expr_from_tree. So, just use build_expr_from_tree
3275 when we really need it. */
3276 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3277 fn = lookup_template_function
3278 (TREE_OPERAND (fn, 0),
3279 build_expr_from_tree (TREE_OPERAND (fn, 1)));
3281 return build_method_call
3282 (build_expr_from_tree (TREE_OPERAND (t, 1)),
3284 build_expr_from_tree (TREE_OPERAND (t, 2)),
3285 NULL_TREE, LOOKUP_NORMAL);
3288 case CALL_EXPR:
3289 if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
3291 tree ref = TREE_OPERAND (t, 0);
3292 tree name = TREE_OPERAND (ref, 1);
3294 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3295 name = build_nt (TEMPLATE_ID_EXPR,
3296 TREE_OPERAND (name, 0),
3297 build_expr_from_tree (TREE_OPERAND (name, 1)));
3299 return build_member_call
3300 (build_expr_from_tree (TREE_OPERAND (ref, 0)),
3301 name,
3302 build_expr_from_tree (TREE_OPERAND (t, 1)));
3304 else
3306 tree name = TREE_OPERAND (t, 0);
3307 tree id;
3308 tree args = build_expr_from_tree (TREE_OPERAND (t, 1));
3309 if (args != NULL_TREE && TREE_CODE (name) == LOOKUP_EXPR
3310 && !LOOKUP_EXPR_GLOBAL (name)
3311 && TREE_CODE ((id = TREE_OPERAND (name, 0))) == IDENTIFIER_NODE
3312 && (!current_class_type
3313 || !lookup_member (current_class_type, id, 0, 0)))
3315 /* Do Koenig lookup if there are no class members. */
3316 name = do_identifier (id, 0, args);
3318 else if (TREE_CODE (name) == TEMPLATE_ID_EXPR
3319 || ! really_overloaded_fn (name))
3320 name = build_expr_from_tree (name);
3322 if (TREE_CODE (name) == OFFSET_REF)
3323 return build_offset_ref_call_from_tree (name, args);
3324 if (TREE_CODE (name) == COMPONENT_REF)
3325 return finish_object_call_expr (TREE_OPERAND (name, 1),
3326 TREE_OPERAND (name, 0),
3327 args);
3328 name = convert_from_reference (name);
3329 return build_call_from_tree (name, args,
3330 /*disallow_virtual=*/false);
3333 case COND_EXPR:
3334 return build_x_conditional_expr
3335 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3336 build_expr_from_tree (TREE_OPERAND (t, 1)),
3337 build_expr_from_tree (TREE_OPERAND (t, 2)));
3339 case PSEUDO_DTOR_EXPR:
3340 return (finish_pseudo_destructor_call_expr
3341 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3342 build_expr_from_tree (TREE_OPERAND (t, 1)),
3343 build_expr_from_tree (TREE_OPERAND (t, 2))));
3345 case TREE_LIST:
3347 tree purpose, value, chain;
3349 if (t == void_list_node)
3350 return t;
3352 purpose = TREE_PURPOSE (t);
3353 if (purpose)
3354 purpose = build_expr_from_tree (purpose);
3355 value = TREE_VALUE (t);
3356 if (value)
3357 value = build_expr_from_tree (value);
3358 chain = TREE_CHAIN (t);
3359 if (chain && chain != void_type_node)
3360 chain = build_expr_from_tree (chain);
3361 return tree_cons (purpose, value, chain);
3364 case COMPONENT_REF:
3366 tree object = build_expr_from_tree (TREE_OPERAND (t, 0));
3367 return finish_class_member_access_expr (object,
3368 TREE_OPERAND (t, 1));
3371 case THROW_EXPR:
3372 return build_throw (build_expr_from_tree (TREE_OPERAND (t, 0)));
3374 case CONSTRUCTOR:
3376 tree r;
3377 tree elts;
3378 tree type = TREE_TYPE (t);
3379 bool purpose_p;
3381 /* digest_init will do the wrong thing if we let it. */
3382 if (type && TYPE_PTRMEMFUNC_P (type))
3383 return t;
3385 r = NULL_TREE;
3386 /* We do not want to process the purpose of aggregate
3387 initializers as they are identifier nodes which will be
3388 looked up by digest_init. */
3389 purpose_p = !(type && IS_AGGR_TYPE (type));
3390 for (elts = CONSTRUCTOR_ELTS (t); elts; elts = TREE_CHAIN (elts))
3392 tree purpose = TREE_PURPOSE (elts);
3393 tree value = TREE_VALUE (elts);
3395 if (purpose && purpose_p)
3396 purpose = build_expr_from_tree (purpose);
3397 value = build_expr_from_tree (value);
3398 r = tree_cons (purpose, value, r);
3401 r = build_nt (CONSTRUCTOR, NULL_TREE, nreverse (r));
3402 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
3404 if (type)
3405 return digest_init (type, r, 0);
3406 return r;
3409 case TYPEID_EXPR:
3410 if (TYPE_P (TREE_OPERAND (t, 0)))
3411 return get_typeid (TREE_OPERAND (t, 0));
3412 return build_typeid (build_expr_from_tree (TREE_OPERAND (t, 0)));
3414 case VAR_DECL:
3415 return convert_from_reference (t);
3417 case VA_ARG_EXPR:
3418 return build_va_arg (build_expr_from_tree (TREE_OPERAND (t, 0)),
3419 TREE_TYPE (t));
3421 default:
3422 return t;
3426 /* FN is an OFFSET_REF indicating the function to call in parse-tree
3427 form; it has not yet been semantically analyzed. ARGS are the
3428 arguments to the function. They have already been semantically
3429 analzyed. */
3431 tree
3432 build_offset_ref_call_from_tree (tree fn, tree args)
3434 tree object_addr;
3436 my_friendly_assert (TREE_CODE (fn) == OFFSET_REF, 20020725);
3438 /* A qualified name corresponding to a non-static member
3439 function or a pointer-to-member is represented as an
3440 OFFSET_REF.
3442 For both of these function calls, FN will be an OFFSET_REF.
3444 struct A { void f(); };
3445 void A::f() { (A::f) (); }
3447 struct B { void g(); };
3448 void (B::*p)();
3449 void B::g() { (this->*p)(); } */
3451 /* This code is not really correct (for example, it does not
3452 handle the case that `A::f' is overloaded), but it is
3453 historically how we have handled this situation. */
3454 object_addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (fn, 0), 0);
3455 if (TREE_CODE (TREE_OPERAND (fn, 1)) == FIELD_DECL)
3456 fn = resolve_offset_ref (fn);
3457 else
3459 fn = TREE_OPERAND (fn, 1);
3460 fn = get_member_function_from_ptrfunc (&object_addr, fn);
3462 args = tree_cons (NULL_TREE, object_addr, args);
3463 return build_function_call (fn, args);
3466 /* FN indicates the function to call. Name resolution has been
3467 performed on FN. ARGS are the arguments to the function. They
3468 have already been semantically analyzed. DISALLOW_VIRTUAL is true
3469 if the function call should be determined at compile time, even if
3470 FN is virtual. */
3472 tree
3473 build_call_from_tree (tree fn, tree args, bool disallow_virtual)
3475 tree template_args;
3476 tree template_id;
3477 tree f;
3479 /* Check to see that name lookup has already been performed. */
3480 my_friendly_assert (TREE_CODE (fn) != OFFSET_REF, 20020725);
3481 my_friendly_assert (TREE_CODE (fn) != SCOPE_REF, 20020725);
3483 /* In the future all of this should be eliminated. Instead,
3484 name-lookup for a member function should simply return a
3485 baselink, instead of a FUNCTION_DECL, TEMPLATE_DECL, or
3486 TEMPLATE_ID_EXPR. */
3488 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3490 template_id = fn;
3491 template_args = TREE_OPERAND (fn, 1);
3492 fn = TREE_OPERAND (fn, 0);
3494 else
3496 template_id = NULL_TREE;
3497 template_args = NULL_TREE;
3500 f = (TREE_CODE (fn) == OVERLOAD) ? get_first_fn (fn) : fn;
3501 /* Make sure we have a baselink (rather than simply a
3502 FUNCTION_DECL) for a member function. */
3503 if (current_class_type
3504 && ((TREE_CODE (f) == FUNCTION_DECL
3505 && DECL_FUNCTION_MEMBER_P (f))
3506 || (DECL_FUNCTION_TEMPLATE_P (f)
3507 && DECL_FUNCTION_MEMBER_P (f))))
3509 f = lookup_member (current_class_type, DECL_NAME (f),
3510 /*protect=*/1, /*want_type=*/0);
3511 if (f)
3512 fn = f;
3515 if (template_id)
3517 if (BASELINK_P (fn))
3518 BASELINK_FUNCTIONS (fn) = build_nt (TEMPLATE_ID_EXPR,
3519 BASELINK_FUNCTIONS (fn),
3520 template_args);
3521 else
3522 fn = template_id;
3525 return finish_call_expr (fn, args, disallow_virtual);
3528 /* This is something of the form `int (*a)++' that has turned out to be an
3529 expr. It was only converted into parse nodes, so we need to go through
3530 and build up the semantics. Most of the work is done by
3531 build_expr_from_tree, above.
3533 In the above example, TYPE is `int' and DECL is `*a'. */
3535 tree
3536 reparse_decl_as_expr (type, decl)
3537 tree type, decl;
3539 decl = build_expr_from_tree (decl);
3540 if (type)
3541 return build_functional_cast (type, build_tree_list (NULL_TREE, decl));
3542 else
3543 return decl;
3546 /* This is something of the form `int (*a)' that has turned out to be a
3547 decl. It was only converted into parse nodes, so we need to do the
3548 checking that make_{pointer,reference}_declarator do. */
3550 tree
3551 finish_decl_parsing (decl)
3552 tree decl;
3554 switch (TREE_CODE (decl))
3556 case IDENTIFIER_NODE:
3557 return decl;
3558 case INDIRECT_REF:
3559 return make_pointer_declarator
3560 (NULL_TREE, finish_decl_parsing (TREE_OPERAND (decl, 0)));
3561 case ADDR_EXPR:
3562 return make_reference_declarator
3563 (NULL_TREE, finish_decl_parsing (TREE_OPERAND (decl, 0)));
3564 case BIT_NOT_EXPR:
3565 TREE_OPERAND (decl, 0) = finish_decl_parsing (TREE_OPERAND (decl, 0));
3566 return decl;
3567 case SCOPE_REF:
3568 push_nested_class (TREE_TYPE (TREE_OPERAND (decl, 0)), 3);
3569 TREE_COMPLEXITY (decl) = current_class_depth;
3570 return decl;
3571 case ARRAY_REF:
3572 TREE_OPERAND (decl, 0) = finish_decl_parsing (TREE_OPERAND (decl, 0));
3573 return decl;
3574 case TREE_LIST:
3575 /* For attribute handling. */
3576 TREE_VALUE (decl) = finish_decl_parsing (TREE_VALUE (decl));
3577 return decl;
3578 case TEMPLATE_ID_EXPR:
3579 return decl;
3580 default:
3581 abort ();
3582 return NULL_TREE;
3586 /* Return 1 if root encloses child. */
3588 static int
3589 is_namespace_ancestor (root, child)
3590 tree root, child;
3592 if (root == child)
3593 return 1;
3594 if (root == global_namespace)
3595 return 1;
3596 if (child == global_namespace)
3597 return 0;
3598 return is_namespace_ancestor (root, CP_DECL_CONTEXT (child));
3602 /* Return the namespace that is the common ancestor
3603 of two given namespaces. */
3605 tree
3606 namespace_ancestor (ns1, ns2)
3607 tree ns1, ns2;
3609 if (is_namespace_ancestor (ns1, ns2))
3610 return ns1;
3611 return namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2);
3614 /* Insert used into the using list of user. Set indirect_flag if this
3615 directive is not directly from the source. Also find the common
3616 ancestor and let our users know about the new namespace */
3617 static void
3618 add_using_namespace (user, used, indirect)
3619 tree user;
3620 tree used;
3621 int indirect;
3623 tree t;
3624 /* Using oneself is a no-op. */
3625 if (user == used)
3626 return;
3627 my_friendly_assert (TREE_CODE (user) == NAMESPACE_DECL, 380);
3628 my_friendly_assert (TREE_CODE (used) == NAMESPACE_DECL, 380);
3629 /* Check if we already have this. */
3630 t = purpose_member (used, DECL_NAMESPACE_USING (user));
3631 if (t != NULL_TREE)
3633 if (!indirect)
3634 /* Promote to direct usage. */
3635 TREE_INDIRECT_USING (t) = 0;
3636 return;
3639 /* Add used to the user's using list. */
3640 DECL_NAMESPACE_USING (user)
3641 = tree_cons (used, namespace_ancestor (user, used),
3642 DECL_NAMESPACE_USING (user));
3644 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
3646 /* Add user to the used's users list. */
3647 DECL_NAMESPACE_USERS (used)
3648 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
3650 /* Recursively add all namespaces used. */
3651 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
3652 /* indirect usage */
3653 add_using_namespace (user, TREE_PURPOSE (t), 1);
3655 /* Tell everyone using us about the new used namespaces. */
3656 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
3657 add_using_namespace (TREE_PURPOSE (t), used, 1);
3660 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
3661 duplicates. The first list becomes the tail of the result.
3663 The algorithm is O(n^2). We could get this down to O(n log n) by
3664 doing a sort on the addresses of the functions, if that becomes
3665 necessary. */
3667 static tree
3668 merge_functions (s1, s2)
3669 tree s1;
3670 tree s2;
3672 for (; s2; s2 = OVL_NEXT (s2))
3674 tree fn2 = OVL_CURRENT (s2);
3675 tree fns1;
3677 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
3679 tree fn1 = OVL_CURRENT (fns1);
3681 /* If the function from S2 is already in S1, there is no
3682 need to add it again. For `extern "C"' functions, we
3683 might have two FUNCTION_DECLs for the same function, in
3684 different namespaces; again, we only need one of them. */
3685 if (fn1 == fn2
3686 || (DECL_EXTERN_C_P (fn1) && DECL_EXTERN_C_P (fn2)
3687 && DECL_NAME (fn1) == DECL_NAME (fn2)))
3688 break;
3691 /* If we exhausted all of the functions in S1, FN2 is new. */
3692 if (!fns1)
3693 s1 = build_overload (fn2, s1);
3695 return s1;
3698 /* This should return an error not all definitions define functions.
3699 It is not an error if we find two functions with exactly the
3700 same signature, only if these are selected in overload resolution.
3701 old is the current set of bindings, new the freshly-found binding.
3702 XXX Do we want to give *all* candidates in case of ambiguity?
3703 XXX In what way should I treat extern declarations?
3704 XXX I don't want to repeat the entire duplicate_decls here */
3706 static tree
3707 ambiguous_decl (name, old, new, flags)
3708 tree name;
3709 tree old;
3710 tree new;
3711 int flags;
3713 tree val, type;
3714 my_friendly_assert (old != NULL_TREE, 393);
3715 /* Copy the value. */
3716 val = BINDING_VALUE (new);
3717 if (val)
3718 switch (TREE_CODE (val))
3720 case TEMPLATE_DECL:
3721 /* If we expect types or namespaces, and not templates,
3722 or this is not a template class. */
3723 if (LOOKUP_QUALIFIERS_ONLY (flags)
3724 && !DECL_CLASS_TEMPLATE_P (val))
3725 val = NULL_TREE;
3726 break;
3727 case TYPE_DECL:
3728 if (LOOKUP_NAMESPACES_ONLY (flags))
3729 val = NULL_TREE;
3730 break;
3731 case NAMESPACE_DECL:
3732 if (LOOKUP_TYPES_ONLY (flags))
3733 val = NULL_TREE;
3734 break;
3735 case FUNCTION_DECL:
3736 /* Ignore built-in functions that are still anticipated. */
3737 if (LOOKUP_QUALIFIERS_ONLY (flags) || DECL_ANTICIPATED (val))
3738 val = NULL_TREE;
3739 break;
3740 default:
3741 if (LOOKUP_QUALIFIERS_ONLY (flags))
3742 val = NULL_TREE;
3745 if (!BINDING_VALUE (old))
3746 BINDING_VALUE (old) = val;
3747 else if (val && val != BINDING_VALUE (old))
3749 if (is_overloaded_fn (BINDING_VALUE (old))
3750 && is_overloaded_fn (val))
3752 BINDING_VALUE (old) = merge_functions (BINDING_VALUE (old),
3753 val);
3755 else
3757 /* Some declarations are functions, some are not. */
3758 if (flags & LOOKUP_COMPLAIN)
3760 /* If we've already given this error for this lookup,
3761 BINDING_VALUE (old) is error_mark_node, so let's not
3762 repeat ourselves. */
3763 if (BINDING_VALUE (old) != error_mark_node)
3765 error ("use of `%D' is ambiguous", name);
3766 cp_error_at (" first declared as `%#D' here",
3767 BINDING_VALUE (old));
3769 cp_error_at (" also declared as `%#D' here", val);
3771 BINDING_VALUE (old) = error_mark_node;
3774 /* ... and copy the type. */
3775 type = BINDING_TYPE (new);
3776 if (LOOKUP_NAMESPACES_ONLY (flags))
3777 type = NULL_TREE;
3778 if (!BINDING_TYPE (old))
3779 BINDING_TYPE (old) = type;
3780 else if (type && BINDING_TYPE (old) != type)
3782 if (flags & LOOKUP_COMPLAIN)
3784 error ("`%D' denotes an ambiguous type",name);
3785 cp_error_at (" first type here", BINDING_TYPE (old));
3786 cp_error_at (" other type here", type);
3789 return old;
3792 /* Subroutine of unualified_namespace_lookup:
3793 Add the bindings of NAME in used namespaces to VAL.
3794 We are currently looking for names in namespace SCOPE, so we
3795 look through USINGS for using-directives of namespaces
3796 which have SCOPE as a common ancestor with the current scope.
3797 Returns zero on errors. */
3800 lookup_using_namespace (name, val, usings, scope, flags, spacesp)
3801 tree name, val, usings, scope;
3802 int flags;
3803 tree *spacesp;
3805 tree iter;
3806 tree val1;
3807 /* Iterate over all used namespaces in current, searching for using
3808 directives of scope. */
3809 for (iter = usings; iter; iter = TREE_CHAIN (iter))
3810 if (TREE_VALUE (iter) == scope)
3812 if (spacesp)
3813 *spacesp = tree_cons (TREE_PURPOSE (iter), NULL_TREE,
3814 *spacesp);
3815 val1 = binding_for_name (name, TREE_PURPOSE (iter));
3816 /* Resolve ambiguities. */
3817 val = ambiguous_decl (name, val, val1, flags);
3819 return BINDING_VALUE (val) != error_mark_node;
3822 /* [namespace.qual]
3823 Accepts the NAME to lookup and its qualifying SCOPE.
3824 Returns the name/type pair found into the CPLUS_BINDING RESULT,
3825 or 0 on error. */
3828 qualified_lookup_using_namespace (name, scope, result, flags)
3829 tree name;
3830 tree scope;
3831 tree result;
3832 int flags;
3834 /* Maintain a list of namespaces visited... */
3835 tree seen = NULL_TREE;
3836 /* ... and a list of namespace yet to see. */
3837 tree todo = NULL_TREE;
3838 tree usings;
3839 /* Look through namespace aliases. */
3840 scope = ORIGINAL_NAMESPACE (scope);
3841 while (scope && (result != error_mark_node))
3843 seen = tree_cons (scope, NULL_TREE, seen);
3844 result = ambiguous_decl (name, result,
3845 binding_for_name (name, scope), flags);
3846 if (!BINDING_VALUE (result) && !BINDING_TYPE (result))
3847 /* Consider using directives. */
3848 for (usings = DECL_NAMESPACE_USING (scope); usings;
3849 usings = TREE_CHAIN (usings))
3850 /* If this was a real directive, and we have not seen it. */
3851 if (!TREE_INDIRECT_USING (usings)
3852 && !purpose_member (TREE_PURPOSE (usings), seen))
3853 todo = tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo);
3854 if (todo)
3856 scope = TREE_PURPOSE (todo);
3857 todo = TREE_CHAIN (todo);
3859 else
3860 scope = NULL_TREE; /* If there never was a todo list. */
3862 return result != error_mark_node;
3865 /* [namespace.memdef]/2 */
3867 /* Set the context of a declaration to scope. Complain if we are not
3868 outside scope. */
3870 void
3871 set_decl_namespace (decl, scope, friendp)
3872 tree decl;
3873 tree scope;
3874 int friendp;
3876 tree old;
3878 /* Get rid of namespace aliases. */
3879 scope = ORIGINAL_NAMESPACE (scope);
3881 /* It is ok for friends to be qualified in parallel space. */
3882 if (!friendp && !is_namespace_ancestor (current_namespace, scope))
3883 error ("declaration of `%D' not in a namespace surrounding `%D'",
3884 decl, scope);
3885 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3886 if (scope != current_namespace)
3888 /* See whether this has been declared in the namespace. */
3889 old = namespace_binding (DECL_NAME (decl), scope);
3890 if (!old)
3891 /* No old declaration at all. */
3892 goto complain;
3893 /* A template can be explicitly specialized in any namespace. */
3894 if (processing_explicit_instantiation)
3895 return;
3896 if (!is_overloaded_fn (decl))
3897 /* Don't compare non-function decls with decls_match here,
3898 since it can't check for the correct constness at this
3899 point. pushdecl will find those errors later. */
3900 return;
3901 /* Since decl is a function, old should contain a function decl. */
3902 if (!is_overloaded_fn (old))
3903 goto complain;
3904 if (processing_template_decl || processing_specialization)
3905 /* We have not yet called push_template_decl to turn the
3906 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations
3907 won't match. But, we'll check later, when we construct the
3908 template. */
3909 return;
3910 for (; old; old = OVL_NEXT (old))
3911 if (decls_match (decl, OVL_CURRENT (old)))
3912 return;
3914 else
3915 return;
3916 complain:
3917 error ("`%D' should have been declared inside `%D'",
3918 decl, scope);
3921 /* Compute the namespace where a declaration is defined. */
3923 static tree
3924 decl_namespace (decl)
3925 tree decl;
3927 if (TYPE_P (decl))
3928 decl = TYPE_STUB_DECL (decl);
3929 while (DECL_CONTEXT (decl))
3931 decl = DECL_CONTEXT (decl);
3932 if (TREE_CODE (decl) == NAMESPACE_DECL)
3933 return decl;
3934 if (TYPE_P (decl))
3935 decl = TYPE_STUB_DECL (decl);
3936 my_friendly_assert (DECL_P (decl), 390);
3939 return global_namespace;
3942 /* Return the namespace where the current declaration is declared. */
3944 tree
3945 current_decl_namespace ()
3947 tree result;
3948 /* If we have been pushed into a different namespace, use it. */
3949 if (decl_namespace_list)
3950 return TREE_PURPOSE (decl_namespace_list);
3952 if (current_class_type)
3953 result = decl_namespace (TYPE_STUB_DECL (current_class_type));
3954 else if (current_function_decl)
3955 result = decl_namespace (current_function_decl);
3956 else
3957 result = current_namespace;
3958 return result;
3961 /* Temporarily set the namespace for the current declaration. */
3963 void
3964 push_decl_namespace (decl)
3965 tree decl;
3967 if (TREE_CODE (decl) != NAMESPACE_DECL)
3968 decl = decl_namespace (decl);
3969 decl_namespace_list = tree_cons (ORIGINAL_NAMESPACE (decl),
3970 NULL_TREE, decl_namespace_list);
3973 void
3974 pop_decl_namespace ()
3976 decl_namespace_list = TREE_CHAIN (decl_namespace_list);
3979 /* Enter a class or namespace scope. */
3981 void
3982 push_scope (t)
3983 tree t;
3985 if (TREE_CODE (t) == NAMESPACE_DECL)
3986 push_decl_namespace (t);
3987 else
3988 pushclass (t, 2);
3991 /* Leave scope pushed by push_scope. */
3993 void
3994 pop_scope (t)
3995 tree t;
3997 if (TREE_CODE (t) == NAMESPACE_DECL)
3998 pop_decl_namespace ();
3999 else
4000 popclass ();
4003 /* [basic.lookup.koenig] */
4004 /* A nonzero return value in the functions below indicates an error. */
4006 struct arg_lookup
4008 tree name;
4009 tree namespaces;
4010 tree classes;
4011 tree functions;
4014 static int arg_assoc PARAMS ((struct arg_lookup*, tree));
4015 static int arg_assoc_args PARAMS ((struct arg_lookup*, tree));
4016 static int arg_assoc_type PARAMS ((struct arg_lookup*, tree));
4017 static int add_function PARAMS ((struct arg_lookup *, tree));
4018 static int arg_assoc_namespace PARAMS ((struct arg_lookup *, tree));
4019 static int arg_assoc_class PARAMS ((struct arg_lookup *, tree));
4020 static int arg_assoc_template_arg PARAMS ((struct arg_lookup*, tree));
4022 /* Add a function to the lookup structure.
4023 Returns 1 on error. */
4025 static int
4026 add_function (k, fn)
4027 struct arg_lookup *k;
4028 tree fn;
4030 /* We used to check here to see if the function was already in the list,
4031 but that's O(n^2), which is just too expensive for function lookup.
4032 Now we deal with the occasional duplicate in joust. In doing this, we
4033 assume that the number of duplicates will be small compared to the
4034 total number of functions being compared, which should usually be the
4035 case. */
4037 /* We must find only functions, or exactly one non-function. */
4038 if (!k->functions)
4039 k->functions = fn;
4040 else if (is_overloaded_fn (k->functions) && is_overloaded_fn (fn))
4041 k->functions = build_overload (fn, k->functions);
4042 else
4044 tree f1 = OVL_CURRENT (k->functions);
4045 tree f2 = fn;
4046 if (is_overloaded_fn (f1))
4048 fn = f1; f1 = f2; f2 = fn;
4050 cp_error_at ("`%D' is not a function,", f1);
4051 cp_error_at (" conflict with `%D'", f2);
4052 error (" in call to `%D'", k->name);
4053 return 1;
4056 return 0;
4059 /* Add functions of a namespace to the lookup structure.
4060 Returns 1 on error. */
4062 static int
4063 arg_assoc_namespace (k, scope)
4064 struct arg_lookup *k;
4065 tree scope;
4067 tree value;
4069 if (purpose_member (scope, k->namespaces))
4070 return 0;
4071 k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces);
4073 value = namespace_binding (k->name, scope);
4074 if (!value)
4075 return 0;
4077 for (; value; value = OVL_NEXT (value))
4078 if (add_function (k, OVL_CURRENT (value)))
4079 return 1;
4081 return 0;
4084 /* Adds everything associated with a template argument to the lookup
4085 structure. Returns 1 on error. */
4087 static int
4088 arg_assoc_template_arg (k, arg)
4089 struct arg_lookup* k;
4090 tree arg;
4092 /* [basic.lookup.koenig]
4094 If T is a template-id, its associated namespaces and classes are
4095 ... the namespaces and classes associated with the types of the
4096 template arguments provided for template type parameters
4097 (excluding template template parameters); the namespaces in which
4098 any template template arguments are defined; and the classes in
4099 which any member templates used as template template arguments
4100 are defined. [Note: non-type template arguments do not
4101 contribute to the set of associated namespaces. ] */
4103 /* Consider first template template arguments. */
4104 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
4105 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
4106 return 0;
4107 else if (TREE_CODE (arg) == TEMPLATE_DECL)
4109 tree ctx = CP_DECL_CONTEXT (arg);
4111 /* It's not a member template. */
4112 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4113 return arg_assoc_namespace (k, ctx);
4114 /* Otherwise, it must be member template. */
4115 else
4116 return arg_assoc_class (k, ctx);
4118 /* It's not a template template argument, but it is a type template
4119 argument. */
4120 else if (TYPE_P (arg))
4121 return arg_assoc_type (k, arg);
4122 /* It's a non-type template argument. */
4123 else
4124 return 0;
4127 /* Adds everything associated with class to the lookup structure.
4128 Returns 1 on error. */
4130 static int
4131 arg_assoc_class (k, type)
4132 struct arg_lookup* k;
4133 tree type;
4135 tree list, friends, context;
4136 int i;
4138 /* Backend build structures, such as __builtin_va_list, aren't
4139 affected by all this. */
4140 if (!CLASS_TYPE_P (type))
4141 return 0;
4143 if (purpose_member (type, k->classes))
4144 return 0;
4145 k->classes = tree_cons (type, NULL_TREE, k->classes);
4147 context = decl_namespace (TYPE_MAIN_DECL (type));
4148 if (arg_assoc_namespace (k, context))
4149 return 1;
4151 /* Process baseclasses. */
4152 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); i++)
4153 if (arg_assoc_class (k, TYPE_BINFO_BASETYPE (type, i)))
4154 return 1;
4156 /* Process friends. */
4157 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
4158 list = TREE_CHAIN (list))
4159 if (k->name == TREE_PURPOSE (list))
4160 for (friends = TREE_VALUE (list); friends;
4161 friends = TREE_CHAIN (friends))
4162 /* Only interested in global functions with potentially hidden
4163 (i.e. unqualified) declarations. */
4164 if (TREE_PURPOSE (friends) == error_mark_node && TREE_VALUE (friends)
4165 && decl_namespace (TREE_VALUE (friends)) == context)
4166 if (add_function (k, TREE_VALUE (friends)))
4167 return 1;
4169 /* Process template arguments. */
4170 if (CLASSTYPE_TEMPLATE_INFO (type))
4172 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
4173 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
4174 arg_assoc_template_arg (k, TREE_VEC_ELT (list, i));
4177 return 0;
4180 /* Adds everything associated with a given type.
4181 Returns 1 on error. */
4183 static int
4184 arg_assoc_type (k, type)
4185 struct arg_lookup *k;
4186 tree type;
4188 switch (TREE_CODE (type))
4190 case VOID_TYPE:
4191 case INTEGER_TYPE:
4192 case REAL_TYPE:
4193 case COMPLEX_TYPE:
4194 case VECTOR_TYPE:
4195 case CHAR_TYPE:
4196 case BOOLEAN_TYPE:
4197 return 0;
4198 case RECORD_TYPE:
4199 if (TYPE_PTRMEMFUNC_P (type))
4200 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
4201 return arg_assoc_class (k, type);
4202 case POINTER_TYPE:
4203 case REFERENCE_TYPE:
4204 case ARRAY_TYPE:
4205 return arg_assoc_type (k, TREE_TYPE (type));
4206 case UNION_TYPE:
4207 case ENUMERAL_TYPE:
4208 return arg_assoc_namespace (k, decl_namespace (TYPE_MAIN_DECL (type)));
4209 case OFFSET_TYPE:
4210 /* Pointer to member: associate class type and value type. */
4211 if (arg_assoc_type (k, TYPE_OFFSET_BASETYPE (type)))
4212 return 1;
4213 return arg_assoc_type (k, TREE_TYPE (type));
4214 case METHOD_TYPE:
4215 /* The basetype is referenced in the first arg type, so just
4216 fall through. */
4217 case FUNCTION_TYPE:
4218 /* Associate the parameter types. */
4219 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
4220 return 1;
4221 /* Associate the return type. */
4222 return arg_assoc_type (k, TREE_TYPE (type));
4223 case TEMPLATE_TYPE_PARM:
4224 case BOUND_TEMPLATE_TEMPLATE_PARM:
4225 return 0;
4226 case TYPENAME_TYPE:
4227 return 0;
4228 case LANG_TYPE:
4229 if (type == unknown_type_node)
4230 return 0;
4231 /* else fall through */
4232 default:
4233 abort ();
4235 return 0;
4238 /* Adds everything associated with arguments. Returns 1 on error. */
4240 static int
4241 arg_assoc_args (k, args)
4242 struct arg_lookup* k;
4243 tree args;
4245 for (; args; args = TREE_CHAIN (args))
4246 if (arg_assoc (k, TREE_VALUE (args)))
4247 return 1;
4248 return 0;
4251 /* Adds everything associated with a given tree_node. Returns 1 on error. */
4253 static int
4254 arg_assoc (k, n)
4255 struct arg_lookup* k;
4256 tree n;
4258 if (n == error_mark_node)
4259 return 0;
4261 if (TYPE_P (n))
4262 return arg_assoc_type (k, n);
4264 if (! type_unknown_p (n))
4265 return arg_assoc_type (k, TREE_TYPE (n));
4267 if (TREE_CODE (n) == ADDR_EXPR)
4268 n = TREE_OPERAND (n, 0);
4269 if (TREE_CODE (n) == COMPONENT_REF)
4270 n = TREE_OPERAND (n, 1);
4271 if (TREE_CODE (n) == OFFSET_REF)
4272 n = TREE_OPERAND (n, 1);
4273 while (TREE_CODE (n) == TREE_LIST)
4274 n = TREE_VALUE (n);
4275 if (TREE_CODE (n) == BASELINK)
4276 n = BASELINK_FUNCTIONS (n);
4278 if (TREE_CODE (n) == FUNCTION_DECL)
4279 return arg_assoc_type (k, TREE_TYPE (n));
4280 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
4282 /* [basic.lookup.koenig]
4284 If T is a template-id, its associated namespaces and classes
4285 are the namespace in which the template is defined; for
4286 member templates, the member template's class... */
4287 tree template = TREE_OPERAND (n, 0);
4288 tree args = TREE_OPERAND (n, 1);
4289 tree ctx;
4290 tree arg;
4292 if (TREE_CODE (template) == COMPONENT_REF)
4293 template = TREE_OPERAND (template, 1);
4295 /* First, the template. There may actually be more than one if
4296 this is an overloaded function template. But, in that case,
4297 we only need the first; all the functions will be in the same
4298 namespace. */
4299 template = OVL_CURRENT (template);
4301 ctx = CP_DECL_CONTEXT (template);
4303 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4305 if (arg_assoc_namespace (k, ctx) == 1)
4306 return 1;
4308 /* It must be a member template. */
4309 else if (arg_assoc_class (k, ctx) == 1)
4310 return 1;
4312 /* Now the arguments. */
4313 for (arg = args; arg != NULL_TREE; arg = TREE_CHAIN (arg))
4314 if (arg_assoc_template_arg (k, TREE_VALUE (arg)) == 1)
4315 return 1;
4317 else
4319 my_friendly_assert (TREE_CODE (n) == OVERLOAD, 980715);
4321 for (; n; n = OVL_CHAIN (n))
4322 if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
4323 return 1;
4326 return 0;
4329 /* Performs Koenig lookup depending on arguments, where fns
4330 are the functions found in normal lookup. */
4332 tree
4333 lookup_arg_dependent (name, fns, args)
4334 tree name;
4335 tree fns;
4336 tree args;
4338 struct arg_lookup k;
4339 tree fn = NULL_TREE;
4341 k.name = name;
4342 k.functions = fns;
4343 k.classes = NULL_TREE;
4345 /* Note that we've already looked at some namespaces during normal
4346 unqualified lookup, unless we found a decl in function scope. */
4347 if (fns)
4348 fn = OVL_CURRENT (fns);
4349 if (fn && TREE_CODE (fn) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (fn))
4350 k.namespaces = NULL_TREE;
4351 else
4352 unqualified_namespace_lookup (name, 0, &k.namespaces);
4354 arg_assoc_args (&k, args);
4355 return k.functions;
4358 /* Process a namespace-alias declaration. */
4360 void
4361 do_namespace_alias (alias, namespace)
4362 tree alias, namespace;
4364 if (TREE_CODE (namespace) != NAMESPACE_DECL)
4366 /* The parser did not find it, so it's not there. */
4367 error ("unknown namespace `%D'", namespace);
4368 return;
4371 namespace = ORIGINAL_NAMESPACE (namespace);
4373 /* Build the alias. */
4374 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
4375 DECL_NAMESPACE_ALIAS (alias) = namespace;
4376 pushdecl (alias);
4379 /* Check a non-member using-declaration. Return the name and scope
4380 being used, and the USING_DECL, or NULL_TREE on failure. */
4382 static tree
4383 validate_nonmember_using_decl (decl, scope, name)
4384 tree decl;
4385 tree *scope;
4386 tree *name;
4388 if (TREE_CODE (decl) == SCOPE_REF)
4390 *scope = TREE_OPERAND (decl, 0);
4391 *name = TREE_OPERAND (decl, 1);
4393 if (!processing_template_decl)
4395 /* [namespace.udecl]
4396 A using-declaration for a class member shall be a
4397 member-declaration. */
4398 if(TREE_CODE (*scope) != NAMESPACE_DECL)
4400 if (TYPE_P (*scope))
4401 error ("`%T' is not a namespace", *scope);
4402 else
4403 error ("`%D' is not a namespace", *scope);
4404 return NULL_TREE;
4407 /* 7.3.3/5
4408 A using-declaration shall not name a template-id. */
4409 if (TREE_CODE (*name) == TEMPLATE_ID_EXPR)
4411 *name = TREE_OPERAND (*name, 0);
4412 error ("a using-declaration cannot specify a template-id. Try `using %D'", *name);
4413 return NULL_TREE;
4417 else if (TREE_CODE (decl) == IDENTIFIER_NODE
4418 || TREE_CODE (decl) == TYPE_DECL
4419 || TREE_CODE (decl) == TEMPLATE_DECL)
4421 *scope = global_namespace;
4422 *name = decl;
4424 else if (TREE_CODE (decl) == NAMESPACE_DECL)
4426 error ("namespace `%D' not allowed in using-declaration", decl);
4427 return NULL_TREE;
4429 else
4430 abort ();
4431 if (DECL_P (*name))
4432 *name = DECL_NAME (*name);
4433 /* Make a USING_DECL. */
4434 return push_using_decl (*scope, *name);
4437 /* Process local and global using-declarations. */
4439 static void
4440 do_nonmember_using_decl (scope, name, oldval, oldtype, newval, newtype)
4441 tree scope, name;
4442 tree oldval, oldtype;
4443 tree *newval, *newtype;
4445 tree decls;
4447 *newval = *newtype = NULL_TREE;
4448 decls = make_node (CPLUS_BINDING);
4449 if (!qualified_lookup_using_namespace (name, scope, decls, 0))
4450 /* Lookup error */
4451 return;
4453 if (!BINDING_VALUE (decls) && !BINDING_TYPE (decls))
4455 error ("`%D' not declared", name);
4456 return;
4459 /* Check for using functions. */
4460 if (BINDING_VALUE (decls) && is_overloaded_fn (BINDING_VALUE (decls)))
4462 tree tmp, tmp1;
4464 if (oldval && !is_overloaded_fn (oldval))
4466 duplicate_decls (OVL_CURRENT (BINDING_VALUE (decls)), oldval);
4467 oldval = NULL_TREE;
4470 *newval = oldval;
4471 for (tmp = BINDING_VALUE (decls); tmp; tmp = OVL_NEXT (tmp))
4473 tree new_fn = OVL_CURRENT (tmp);
4475 /* [namespace.udecl]
4477 If a function declaration in namespace scope or block
4478 scope has the same name and the same parameter types as a
4479 function introduced by a using declaration the program is
4480 ill-formed. */
4481 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
4483 tree old_fn = OVL_CURRENT (tmp1);
4485 if (new_fn == old_fn)
4486 /* The function already exists in the current namespace. */
4487 break;
4488 else if (OVL_USED (tmp1))
4489 continue; /* this is a using decl */
4490 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
4491 TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
4493 /* If this using declaration introduces a function
4494 recognized as a built-in, no longer mark it as
4495 anticipated in this scope. */
4496 if (DECL_ANTICIPATED (old_fn))
4498 DECL_ANTICIPATED (old_fn) = 0;
4499 break;
4502 /* There was already a non-using declaration in
4503 this scope with the same parameter types. If both
4504 are the same extern "C" functions, that's ok. */
4505 if (!decls_match (new_fn, old_fn))
4506 error ("`%D' is already declared in this scope", name);
4507 break;
4511 /* If we broke out of the loop, there's no reason to add
4512 this function to the using declarations for this
4513 scope. */
4514 if (tmp1)
4515 continue;
4517 *newval = build_overload (OVL_CURRENT (tmp), *newval);
4518 if (TREE_CODE (*newval) != OVERLOAD)
4519 *newval = ovl_cons (*newval, NULL_TREE);
4520 OVL_USED (*newval) = 1;
4523 else
4525 *newval = BINDING_VALUE (decls);
4526 if (oldval)
4527 duplicate_decls (*newval, oldval);
4530 *newtype = BINDING_TYPE (decls);
4531 if (oldtype && *newtype && oldtype != *newtype)
4533 error ("using declaration `%D' introduced ambiguous type `%T'",
4534 name, oldtype);
4535 return;
4539 /* Process a using-declaration not appearing in class or local scope. */
4541 void
4542 do_toplevel_using_decl (decl)
4543 tree decl;
4545 tree scope, name, binding;
4546 tree oldval, oldtype, newval, newtype;
4548 decl = validate_nonmember_using_decl (decl, &scope, &name);
4549 if (decl == NULL_TREE)
4550 return;
4552 binding = binding_for_name (name, current_namespace);
4554 oldval = BINDING_VALUE (binding);
4555 oldtype = BINDING_TYPE (binding);
4557 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
4559 /* Copy declarations found. */
4560 if (newval)
4561 BINDING_VALUE (binding) = newval;
4562 if (newtype)
4563 BINDING_TYPE (binding) = newtype;
4564 return;
4567 /* Process a using-declaration at function scope. */
4569 void
4570 do_local_using_decl (decl)
4571 tree decl;
4573 tree scope, name;
4574 tree oldval, oldtype, newval, newtype;
4576 decl = validate_nonmember_using_decl (decl, &scope, &name);
4577 if (decl == NULL_TREE)
4578 return;
4580 if (building_stmt_tree ()
4581 && at_function_scope_p ())
4582 add_decl_stmt (decl);
4584 oldval = lookup_name_current_level (name);
4585 oldtype = lookup_type_current_level (name);
4587 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
4589 if (newval)
4591 if (is_overloaded_fn (newval))
4593 tree fn, term;
4595 /* We only need to push declarations for those functions
4596 that were not already bound in the current level.
4597 The old value might be NULL_TREE, it might be a single
4598 function, or an OVERLOAD. */
4599 if (oldval && TREE_CODE (oldval) == OVERLOAD)
4600 term = OVL_FUNCTION (oldval);
4601 else
4602 term = oldval;
4603 for (fn = newval; fn && OVL_CURRENT (fn) != term;
4604 fn = OVL_NEXT (fn))
4605 push_overloaded_decl (OVL_CURRENT (fn),
4606 PUSH_LOCAL | PUSH_USING);
4608 else
4609 push_local_binding (name, newval, PUSH_USING);
4611 if (newtype)
4612 set_identifier_type_value (name, newtype);
4615 tree
4616 do_class_using_decl (decl)
4617 tree decl;
4619 tree name, value;
4621 if (TREE_CODE (decl) != SCOPE_REF
4622 || !TYPE_P (TREE_OPERAND (decl, 0)))
4624 error ("using-declaration for non-member at class scope");
4625 return NULL_TREE;
4627 name = TREE_OPERAND (decl, 1);
4628 if (TREE_CODE (name) == BIT_NOT_EXPR)
4630 error ("using-declaration for destructor");
4631 return NULL_TREE;
4633 else if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4635 name = TREE_OPERAND (name, 0);
4636 error ("a using-declaration cannot specify a template-id. Try `using %T::%D'", TREE_OPERAND (decl, 0), name);
4637 return NULL_TREE;
4639 if (TREE_CODE (name) == TYPE_DECL || TREE_CODE (name) == TEMPLATE_DECL)
4640 name = DECL_NAME (name);
4641 else if (BASELINK_P (name))
4643 name = BASELINK_FUNCTIONS (name);
4644 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4645 name = TREE_OPERAND (name, 0);
4646 name = DECL_NAME (get_first_fn (name));
4649 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 980716);
4651 value = build_lang_decl (USING_DECL, name, void_type_node);
4652 DECL_INITIAL (value) = TREE_OPERAND (decl, 0);
4653 return value;
4656 /* Process a using-directive. */
4658 void
4659 do_using_directive (namespace)
4660 tree namespace;
4662 if (building_stmt_tree ())
4663 add_stmt (build_stmt (USING_STMT, namespace));
4665 /* using namespace A::B::C; */
4666 if (TREE_CODE (namespace) == SCOPE_REF)
4667 namespace = TREE_OPERAND (namespace, 1);
4668 if (TREE_CODE (namespace) == IDENTIFIER_NODE)
4670 /* Lookup in lexer did not find a namespace. */
4671 if (!processing_template_decl)
4672 error ("namespace `%T' undeclared", namespace);
4673 return;
4675 if (TREE_CODE (namespace) != NAMESPACE_DECL)
4677 if (!processing_template_decl)
4678 error ("`%T' is not a namespace", namespace);
4679 return;
4681 namespace = ORIGINAL_NAMESPACE (namespace);
4682 if (!toplevel_bindings_p ())
4683 push_using_directive (namespace);
4684 else
4685 /* direct usage */
4686 add_using_namespace (current_namespace, namespace, 0);
4689 void
4690 check_default_args (x)
4691 tree x;
4693 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4694 int saw_def = 0, i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4695 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4697 if (TREE_PURPOSE (arg))
4698 saw_def = 1;
4699 else if (saw_def)
4701 cp_error_at ("default argument missing for parameter %P of `%+#D'",
4702 i, x);
4703 break;
4708 void
4709 mark_used (decl)
4710 tree decl;
4712 TREE_USED (decl) = 1;
4713 if (processing_template_decl)
4714 return;
4715 if (!skip_evaluation)
4716 assemble_external (decl);
4718 /* Is it a synthesized method that needs to be synthesized? */
4719 if (TREE_CODE (decl) == FUNCTION_DECL
4720 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
4721 && DECL_ARTIFICIAL (decl)
4722 && ! DECL_INITIAL (decl)
4723 /* Kludge: don't synthesize for default args. */
4724 && current_function_decl)
4726 synthesize_method (decl);
4727 /* If we've already synthesized the method we don't need to
4728 instantiate it, so we can return right away. */
4729 return;
4732 /* If this is a function or variable that is an instance of some
4733 template, we now know that we will need to actually do the
4734 instantiation. We check that DECL is not an explicit
4735 instantiation because that is not checked in instantiate_decl. */
4736 if ((DECL_NON_THUNK_FUNCTION_P (decl) || TREE_CODE (decl) == VAR_DECL)
4737 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
4738 && (!DECL_EXPLICIT_INSTANTIATION (decl)
4739 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_INLINE (decl))))
4740 instantiate_decl (decl, /*defer_ok=*/1);
4743 /* Helper function for class_head_decl and class_head_defn
4744 nonterminals. AGGR is the class, union or struct tag. SCOPE is the
4745 explicit scope used (NULL for no scope resolution). ID is the
4746 name. DEFN_P is true, if this is a definition of the class and
4747 NEW_TYPE_P is set to nonzero, if we push into the scope containing
4748 the to be defined aggregate.
4750 Return a TYPE_DECL for the type declared by ID in SCOPE. */
4752 tree
4753 handle_class_head (tag_kind, scope, id, attributes, defn_p, new_type_p)
4754 enum tag_types tag_kind;
4755 tree scope, id, attributes;
4756 int defn_p;
4757 int *new_type_p;
4759 tree decl = NULL_TREE;
4760 tree current = current_scope ();
4761 bool xrefd_p = false;
4763 if (current == NULL_TREE)
4764 current = current_namespace;
4766 *new_type_p = 0;
4768 if (scope)
4770 if (TREE_CODE (id) == TYPE_DECL)
4771 /* We must bash typedefs back to the main decl of the
4772 type. Otherwise we become confused about scopes. */
4773 decl = TYPE_MAIN_DECL (TREE_TYPE (id));
4774 else if (DECL_CLASS_TEMPLATE_P (id))
4775 decl = DECL_TEMPLATE_RESULT (id);
4776 else
4778 if (TYPE_P (scope))
4780 /* According to the suggested resolution of core issue
4781 180, 'typename' is assumed after a class-key. */
4782 decl = make_typename_type (scope, id, tf_error);
4783 if (decl != error_mark_node)
4784 decl = TYPE_MAIN_DECL (decl);
4785 else
4786 decl = NULL_TREE;
4788 else if (scope == current)
4790 /* We've been given AGGR SCOPE::ID, when we're already
4791 inside SCOPE. Be nice about it. */
4792 if (pedantic)
4793 pedwarn ("extra qualification `%T::' on member `%D' ignored",
4794 scope, id);
4796 else
4797 error ("`%T' does not have a class or union named `%D'",
4798 scope, id);
4802 if (!decl)
4804 decl = TYPE_MAIN_DECL (xref_tag (tag_kind, id, attributes, !defn_p));
4805 xrefd_p = true;
4808 if (!TYPE_BINFO (TREE_TYPE (decl)))
4810 error ("`%T' is not a class or union type", decl);
4811 return error_mark_node;
4814 if (defn_p)
4816 /* For a definition, we want to enter the containing scope
4817 before looking up any base classes etc. Only do so, if this
4818 is different to the current scope. */
4819 tree context = CP_DECL_CONTEXT (decl);
4821 *new_type_p = (current != context
4822 && TREE_CODE (context) != TEMPLATE_TYPE_PARM
4823 && TREE_CODE (context) != BOUND_TEMPLATE_TEMPLATE_PARM);
4824 if (*new_type_p)
4825 push_scope (context);
4827 if (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
4828 /* It is valid to define a class with a different class key,
4829 and this changes the default member access. */
4830 CLASSTYPE_DECLARED_CLASS (TREE_TYPE (decl))
4831 = (tag_kind == class_type);
4833 if (!xrefd_p && PROCESSING_REAL_TEMPLATE_DECL_P ())
4834 decl = push_template_decl (decl);
4837 return decl;
4840 #include "gt-cp-decl2.h"